首页 > 代码库 > 全加器

全加器

/*4位全加器全加器需要有输入输出,需要有下级向上进位的输入,需要有向上一位进位的输出。大家看一下,这个模块已经包含全部的输入输出信息。大家都知道,N位加法器得出来的出来的和最多是N+1位因此可以清晰从下面代码中看到相关信息。然后assign用的是阻塞赋值。相加即满足相关的需求。*/module adder4(cout,sum,ina,inb,cin);    output[3:0] sum;    output cout;    input[3:0] ina,inb;    input cin;        assign {cout,sum}=ina+inb+cin;endmodule

在写testbeach文件之前,先普及一点testbeach的知识:
一般来讲,在数据类型声明时,和被测模块的输入端口相连的信号定义为reg类型,这样便于在initial语句和always语句块中对其进行赋值;和被测模块输出端口相连的信号定义为wire类型,便于进行检测。Testbench模块最重要的的任务就是利用各种合法的语句,产生适当的时序和数据,以完成测试,并达到覆盖率要求。

那么testbeach文件如下:

/*File Name    :    test_adder4.vDescription    :    The testbench of the adder_4.vWritten    By    :    LiMingData        :    2011/04/18 20:13modefied    :    在仿真的时候,把延时从10ns改为5ns            :    cout显示为2位*///test_adder4 (top-level module)`timescale 1ns/1nsmodule test_adder4;        //Declare variables    wire[3:0] sum;    wire cout;    reg[3:0] ina,inb;    reg cin;        //Instantiate the module adder4    adder4 adder4_1(cout,sum,ina,inb,cin);        //Stimulate the inputs, Finish the stimulation at 90 time units    initial        begin            #0 ina = 4‘b0001; inb = 4‘b1010; cin = 1‘b0;            #5 ina = 4‘b0010; inb = 4‘b1010; cin = 1‘b1;            #5 ina = 4‘b0010; inb = 4‘b1110; cin = 1‘b0;            #5 ina = 4‘b0011; inb = 4‘b1100; cin = 1‘b1;            #5 ina = 4‘b0111; inb = 4‘b1001; cin = 1‘b0;            #5 ina = 4‘b0001; inb = 4‘b1100; cin = 1‘b1;            #5 ina = 4‘b0011; inb = 4‘b1100; cin = 1‘b0;            #5 ina = 4‘b0111; inb = 4‘b1111; cin = 1‘b1;            #5 $finish;        end        initial        $monitor("At time %t, ina(%b) + inb(%b) + cin(%b) =  sum(%b)(%2d),cout(%b)",$time, ina, inb, cin, sum, sum, cout);    initial        begin                        $dumpfile("test.vcd");            $dumpvars(0,test_adder4);        endendmodule

全加器