verilog之task用法实例

    该用法的代码源自夏宇闻老师的教材。

    源代码:

 1 module traffic_lights; 
 2 reg clock, red, amber, green;
 3 parameter  on = 1, off = 0, red_tics = 350, 
 4 amber_tics = 30, green_tics = 200;
 5 // initialize colors. 
 6 initial red = off; 
 7 initial amber = off; 
 8 initial green = off;
 9 always begin  // sequence to control the lights. 
10     red = on;   // turn red light on 
11     light(red, red_tics);         // and wait.
12     green = on;  // turn green light on 
13     light(green, green_tics);     // and wait.
14     amber = on;  // turn amber light on 
15     light(amber, amber_tics);     // and wait. 
16 end
17 // task to wait for 'tics' positive edge clocks 
18 // before turning 'color' light off. 
19 task light; 
20     output color; 
21     input [31:0] tics; 
22     begin 
23         repeat (tics) @ (posedge clock); 
24         color = off;  // turn light off. 
25     end 
26 endtask
27 always begin  // waveform for the clock. 
28     #100 clock = 0; 
29     #100 clock = 1; 
30 end 
31 endmodule // traffic_lights.

    测试代码run.do:

1 vlib work
2 vlog -novopt traffic_lights.v
3 vsim -voptargs=+acc work.traffic_lights
4 add wave *
5 
6 run 1000us

    测试代码自动运行run.bat:

1 vsim -do run.do

    运行结果如下:

posted on 2014-01-05 23:31  ZcsTech  阅读(3126)  评论(0编辑  收藏  举报

导航