Explain why fork-join behaves differently when #10 begin A = 1'b0; B = 1'b1;end, and to #10; begin A = 1'b0; B = 1'b1; end

60 Views Asked by At

Please notice the semicolon after #10 delay in the second case.

I thought I understood fork-join, but after these outputs, I don't think I do. Can someone please explain why the semicolon is causing the difference? How does the code flow in fork-join when there is a semicolon after the delay?

In the first case, the code is:

module waveform_fork_join();

    reg A,B,C,D,E,F,en;
    
    initial
        fork
            en = 1'b0; A = 1'b1; B = 1'b0; C = 1'b0; D = 1'b0; E = 1'b1; F = 1'b1;
            #10 begin
            A = 1'b0; B = 1'b1; C = 1'b1; 
            end
            #20 begin
            A = 1'b1; B = 1'b0; D = 1'b1; E = 1'b0; 
            end
            #30 begin
            B = 1'b1; E = 1'b1; F = 1'b0; 
            end
            #40 begin
            en = 1'b1; B = 1'b0; D = 1'b0; F = 1'b1;
            end
            #50 begin
            B = 1'b1; 
            end
            #60 begin
            B = 1'b0; D = 1'b1; 
            end
            #70 B = 1;
            #80 $finish;
        join

endmodule

Output: Output of the code without semicolon after delay.

In the second case, code is:

module waveform_fork_join1();

    reg A,B,C,D,E,F,en;
    
    initial
        fork
            en = 1'b0; A = 1'b1; B = 1'b0; C = 1'b0; D = 1'b0; E = 1'b1; F = 1'b1;
            #10;    begin
            A = 1'b0; B = 1'b1; C = 1'b1; 
            end
            #20; begin
            A = 1'b1; B = 1'b0; D = 1'b1; E = 1'b0; 
            end
            #30; begin
            B = 1'b1; E = 1'b1; F = 1'b0; 
            end
            #40; begin
            en = 1'b1; B = 1'b0; D = 1'b0; F = 1'b1;
            end
            #50; begin
            B = 1'b1; 
            end
            #60; begin
            B = 1'b0; D = 1'b1; 
            end
            #70;    B = 1;
            #80 $finish;
        join

endmodule

Output: Output of the code with semicolon after delay

1

There are 1 best solutions below

0
toolic On

In waveform_fork_join, at time 10, the begin/end block executes, toggling the signals.

In waveform_fork_join1, at time 10, nothing happens because the semicolon creates a null statement. The begin/end block starting on the same line as #10; is a completely separate statement which executes at time 0.

The way you wrote your code is very confusing. Firstly, for such a simple example to demonstrate the difference in behavior, there is no need for so many variables. Consider this alternate way of writing your code:

module waveform_fork_join();
    reg A,B;
    
    initial
        fork
            A = 1'b1;
            B = 1'b0;
            #10 begin
                A = 1'b0;
                B = 1'b1;
            end
            #20 begin
                A = 1'b1;
                B = 1'b0; 
            end
            #30 begin
                B = 1'b1;
            end
        join
endmodule

module waveform_fork_join1();
    reg A,B;
    
    initial
        fork
            A = 1'b1;
            B = 1'b0;
            #10;
            begin
                A = 1'b0;
                B = 1'b1;
            end
            #20;
            begin
                A = 1'b1;
                B = 1'b0; 
            end
            #30;
            begin
                B = 1'b1;
            end
        join
endmodule

Perhaps that makes it clearer that the 3 delay statements have nothing to do with the begin/end code. In the 1st case, 5 things start execution at time 0. In the 2nd case, 8 things start execution at time 0.