pktzj117

Testbench的编写(一)

0
阅读(2835)

摘要

1.时钟信号

1. Clock logic

// Declare a clock period constant.  Parameter ClockPeriod = 10; // Clock Generation method 1:  initial begin 
forever Clock = #(ClockPeriod / 2) ~ Clock; 

end // Clock Generation method 2: initial begin 
always #(ClockPeriod / 2) Clock = ~Clock; 

end 

2.激励信号

1)绝对时间激励

 initial begin   

 Reset = 1;  
 Load = 0;  
 Count_UpDn = 0;  

   #100 Reset = 0;
   #20 Load = 1;
   #20 Count_UpDn = 1; 

 end

2)相对时间激励

always @ (posedge clock)
TB_Count <= TB_Count + 1;
initial begin if (TB_Count <= 5)
  begin
  Reset = 1;
  Load  = 0;
  Count_UpDn = 0;
end else begin
  Reset = 0;
  Load = 1;
  Count_UpDn = 0;
end
initial begin if (Count == 1100) begin
    Count_UpDn <= 0;
    $display ("Terminal Count Reachcd,now counting down.");
     end
end

3)显示结果

// pipes the ASCII results to the terminal or text editor  initial begin

$timeformat(-9,1,"ns",12);
$display(" Time Clk Rst Ld SftRg Data Sel");
$monitor("%t %b %b %b %b %b %b", $realtime,  
         clock, reset, load, shiftreg, data, sel);
end
关键字 $display在终端屏幕上输出用(“…”)括起的附加的文字。关键字$monitor操作不同,
它的输出是由事件驱动的。例子中的变量$realtime(用户用于赋值以当前的仿真时间)用于
触发信号列表中各个值的显示。信号表由变量 $realtime开始,随后是需要显示的信号
(clock, reset, load等)。起头的一连串“%”关键字是一些格式标识符,用来控制信号列
表中的数值以何种格式显示。格式列表是有位置关系的――每个格式标识符对应于信号列表中的
相应位置的信号。比如%t标识符规定了$realtime的值以时间格式给出,而第一个%b标识符规
定clock信号的值以二进制形式给出。Verilog还提供其它的一些格式标识符,比如%h说明十
六进制,%d说明十进制,%c说明显示为八进制。