`

Verilog中for语句的使用,简单testbench的写法

阅读更多

1,for语句的使用

`timescale	1ns/1ns	
module add16(a,b,c0,sum,cout);
	input	[15:0]	a,b;
	input		c0;
	output	[15:0]	sum;
	output		cout;
	
    	reg 	[15:0]	p,g,sum;  
    	reg 	[16:0] 	CA;	   
	reg		cout;
    	integer 		i;

	always @(a or b) 
		for(i=0;i<=15;i=i+1)
			begin 
				p[i] = a[i] ^ b[i];
				g[i] = a[i] & b[i];
			end
							
    	always @(p or g or c0)        
		begin					
        			CA[0] = c0;	  //CA[i] : carry to i bit
		         	for( i=0; i<=15; i=i+1)	
				 begin					  
                 				CA[i+1] = g[i] | ( p[i] & CA[i]);
			               		sum[i] 	= p[i] ^ CA[i];
         				end	
			cout=CA[16];
	    	end 
		
endmodule

 

2,简单Testbench的写法

`timescale	1ns/1ns
module	add16_tb;
	reg[15:0]		a,b;
	reg		c0;
	wire[15:0]	sum;
	wire		cout;
	
	add16 test(a,b,c0,sum,cout);
	
	initial 
		begin 		
			$display("");
			$display("+++++++++++++++++++++++++++++++++++++++++++++++");
			$display("********	The start of the test	*****************");
			$display("*******a*******b*******c0*******sum********cout"); 
			$display("============================  | ===============");
			$monitor("       %h,   %h,   %b,     | %h,      %b",a,b,c0,sum,cout);
					a=16'h1111;b=16'h1111;c0=0;
			#100	a=16'hffff;b=16'h0000;c0=0;
			#100	a=16'hffff;b=16'h0000;c0=1;
			#100	a=16'h1111;b=16'h1111;c0=0;
			#100	a=16'h2222;b=16'h1111;c0=0;
			#100	a=16'h1111;b=16'h4444;c0=0;
			#100	a=16'h9999;b=16'h4444;c0=0;
			#100
			$display("============================  | ===============");
			$display("********	The end of the test	*****************");
			$display("+++++++++++++++++++++++++++++++++++++++++++++++");
			$display("");
			#100	$finish;
		end
		
endmodule

 

3,总结

a,for语句i需要定义成integer 类型,另外注意表达式写成i=i+1不要写成i++;

b,另外,Verilog中用任何语句都要在头脑中有个硬件的概念,for语句也是,就如上面的加法器一样,在写hdl前在草图上画一个还是要的;

c,Testbench还是要用Active HDL,modelsim或LDV跑一跑才行;;

分享到:
评论

相关推荐

    verilog testbench的写法

    Verilog作为一种硬件描述语言(HDL),广泛应用于数字电路的设计和测试中,因此,掌握Verilog testbench的编写对于硬件工程师来说尤为重要。 在编写Verilog testbench时,首先需要理解Verilog的基本语法和结构,...

    Modelsim工程建立与testbench写法

    #### 二、Testbench写法 **1. 测试文件的作用** - **产生激励**:为设计文件提供必要的输入信号,模拟实际工作环境。 - **接收反馈**:监控设计文件的输出,以验证其行为是否符合预期。 **2. 变量定义与初始化** ...

    testbench写法

    ### Verilog HDL 及 Testbench 写法详解 #### 一、Verilog HDL 基本概念 Verilog HDL 是一种广泛应用于电子工程领域中的硬件描述语言,主要用于数字电路系统的描述、仿真和验证。它提供了一种标准化的方式来描述...

    vcs和verdi联合仿真(初学者可以用来熟悉脚本写法).pdf

    在编写Testbench时,需要加入能够触发波形记录的语句,并定义相应的宏。这些操作对于初学者来说可能较为复杂,但通过逐步学习和实践,可以掌握这些工具的使用方法,并进行高效的数字电路仿真工作。

    verilog语言编码规范

    8. **空格与tab之争**: 在Verilog代码中使用空格而非tab键进行缩进,以避免不同编辑器设置导致的格式差异。 9. **行内与行间限制**: 每行代码长度应不超过一定数量(如80个字符),以提高代码的可读性。 10. **注释*...

    用的是FPGA相关的东西

    - **遍历测试**:使用`for`循环来实现遍历测试,可以有效地覆盖多种测试情况。 - **系统函数和任务的使用**:Verilog提供了丰富的内置系统函数和任务,如`$display`、`$readmemh`等,可以帮助开发者完成日志记录、...

Global site tag (gtag.js) - Google Analytics