Menu
Coddy logo textTech

Disable Statement

Part of the Fundamentals section of Coddy's Verilog journey — lesson 62 of 90.

The disable statement stops the execution of a named block (like a forever loop, begin-end block, or task). It is useful for breaking out of loops that would otherwise run forever.

Syntax:

disable block_name;

The block must be labeled with a name.

Disable with Forever Loop

begin : counter // counter gives the block the name counter.
  forever begin
    #10 count = count + 1;
    if (count == 5) begin
      disable counter;   // disable counter stops that entire named block.
    end
  end
end

The forever loop is inside a named block counter. The disable counter statement is placed inside the loop. When count reaches 5, disable counter executes and stops the entire block counter — which immediately stops the forever loop as well.

Disable in a Separate Block

begin : counter
  forever begin
    #10 count = count + 1;
  end
end

initial begin
  #50;
  disable counter;   // Stops the counter block from outside
end

Here, the forever loop runs continuously inside the named block counter. The disable counter statement is placed in a separate initial block. After 50 time units, disable counter executes and stops the counter block — which terminates the forever loop from the outside.

Important Rules

RuleExplanation
Block must be namedbegin : block_name
Disable stops execution immediatelyCode stops at that moment
Can be used from anywhereFrom any block in the module
Useful for stopping forever loopsOtherwise they never stop
challenge icon

Challenge

You are given a module with a counter that runs forever. 

What to do:

Complete the empty block by replacing each comment with the correct code:

  1. Add #50; to wait 50 time units
  2. Add disable counter; to stop the counter block
  3. Add $display("Counter stopped"); to print the message
  4. Add $finish; to end the simulation

Cheat sheet

The disable statement stops execution of a named block (e.g., forever loop). The block must be labeled using begin : block_name.

Disable from inside the block:

begin : counter
  forever begin
    #10 count = count + 1;
    if (count == 5) disable counter;
  end
end

Disable from a separate block:

begin : counter
  forever begin
    #10 count = count + 1;
  end
end

initial begin
  #50;
  disable counter; // stops the named block from outside
end

Try it yourself

module disable_challenge;
  integer count;
  
  initial begin
    count = 0;
    fork
      begin : counter
        forever begin
          #10 count = count + 1;
          $display("count = %d", count);
        end
      end
      
      begin
        // Step 1: Wait 50 time units
        
        // Step 2: Disable the counter block
        
        // Step 3: Print "Counter stopped"
        
        // Step 4: End the simulation
        
      end
    join
  end
endmodule
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals