systemverilog中阻塞和非阻塞事件以及同步

时间:2022-11-22 08:26:25

一、SV中非阻塞事件

module test;

event ev1, ev2;

//belong to logic function part
always@(ev1)
$display("ev1 triggered at %0ts", $time);

initial wait(ev2.triggered)
$display("ev2 triggered at %0ts", $time);

//belong to stimulus
initial begin
->> #5 ev1; //"->>": non-blocking event, delay 5 cycle triggering the event
end

initial begin
->> @(ev1) ev2;       //event ev1 and ev2 will triggered simutaneous,but ev1 is earlier.
end

endmodule

//Makefile
comp:
vcs -sverilog Synchronous multi-events.sv -l comp.log
run:
./simv -l run.log
clean:
\rm -rf csrc simv* *.log

//run.log
ev1 triggered at 5s

ev2 triggered at 5s

//--------------------------------------------------------------------------------------------------------------


二、SV中阻塞事件

program p;

event e1, e2;

initial begin

$display("@%0t: 1: before trigger", $time);

->e1; //首先这种事件是一个零宽度的脉冲

@e2;

$display("@%0t: 1: after trigger", $time);

end

initial begin

$display("@%0t: 2: before trigger", $time);

->e2;

@e1;

$display("@%0t: 2: after trigger", $time);

end

endprogram

//Makefile
comp:
vcs -sverilog *..sv -l comp.log
run:
./simv -l run.log
clean:
\rm -rf csrc simv* *.log

//run.log
@0: 1: before trigger

@0: 2: before trigger

@0: 1: after trigger

#解释:

两个initial块同一时刻执行,但即便是在同一时刻运行也有先后顺序,按照书写顺序从上到下运行(试验过两个initial块前后位置调换,结果恰好相反),即,同一时刻事件e1先执行完,在@e2处等待,同时呢,事件e2也被触发,虽然在同一时刻,但由于e1早于e2被触发,所以,后续e2的触发是@e2继续执行,但当到@e1时,由于此时事件e1已经被触发,程序会堵在@e1处。呈现如上log中的结果。