Verilog Block statements
There are ways to group a set of statements together that are syntactically equivalent to a single statement and are known as block statements. There are two kinds of block statements: sequential and parallel.
Statements are wrapped using
end keywords and will be executed sequentially in the given order, one after the other. Delay values are treated relative to the time of execution of the previous statement. After all the statements within the block are executed control may be passed elsewhere.
module design0; bit [31:0] data; // "initial" block starts at time 0 initial begin // After 10 time units, data becomes 0xfe #10 data = 8'hfe; $display ("[Time=%0t] data=0x%0h", $time, data); // After 20 time units, data becomes 0x11 #20 data = 8'h11; $display ("[Time=%0t] data=0x%0h", $time, data); end endmodule
In the example above, first statement in the
begin-end block will be executed at 10 time units, and the second statement at 30 time units because of the relative nature. It is 20 time units after execution of the previous statement.
ncsim> run [Time=10] data=0xfe [Time=30] data=0x11 ncsim: *W,RNQUIE: Simulation is complete.
Click to try this example in a simulator!
A parallel block can execute statements concurrently and delay control can be used to provide time-ordering of the assignments. Statements are launched in parallel by wrapping them within the
initial begin #10 data = 8'hfe; fork #20 data = 8'h11; #10 data = 8'h00; join end
In the example above,
fork-join block will be launched after executing the statement at 10 time units. Statements within this block will be executed in parallel and the first statement that will be launched will be the one where data is assigned a value of 8'h00 since the delay for that is 10 time units after the launch of the fork-join. After 10 more time units, the first statement will be launched and data will get the value 8'h11.
initial begin #10 data = 8'hfe; fork #10 data = 8'h11; begin #20 data = 8'h00; #30 data = 8'haa; end join end
There is a
begin-end block in the example above, and all statements within the begin-end block will be executed sequentially, but the block itself will be launched in parallel along with the other statements. So, data will get 8'h11 at 20 time units, 8'h00 at 30 time units and 8'haa at 60 time units.
Naming of blocks
Both sequential and parallel blocks can be named by adding
: name_of_block after the keywords
fork. By doing so, the block can be referenced in a
begin : name_seq [statements] end fork : name_fork [statements] join