进位链加法器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38352854/article/details/79636819

  一、进位链加法器的原理    



二、进位链加法器的verilog源代码

//module name:carry_chain_adder
//module function: the 8 bit carry chain adder
`timescale 1ns / 1ps

`define DSIZE  8
module carry_chain_adder (

    //inputs
      x
     ,y
     ,cin

    //outputs
     ,sum
     ,cout
);

//parameter


//inputs
input [`DSIZE-1:0] x;
input [`DSIZE-1:0] y;
input cin;

//outputs: the registers must be initialized to make the simulation more accuracy
output reg [`DSIZE-1:0] sum = `DSIZE'd00000000;
output reg cout = 0;
 
//registers
reg q[`DSIZE:0];
reg p[`DSIZE-1:0];
reg g[`DSIZE-1:0];

always @(x or y or cin)begin:ADDER
    integer i;
    q[0] = cin;
    for(i = 0;i < `DSIZE; i = i + 1) begin
        p[i] = x[i]^y[i];
        g[i] = y[i];
        q[i+1] = (p[i])?q[i]:g[i];
        sum[i] = p[i]^q[i];
    end
    cout = q[`DSIZE];

end//begin

endmodule

三、testbench仿真代码

`timescale 1ns / 1ps
module carry_chain_adder_tb();

//parameters
parameter DSIZE = 8;

//inputs
reg [DSIZE-1:0] x;
reg [DSIZE-1:0] y;
reg cin;

//outputs
wire [DSIZE-1:0] sum;
wire cout;

//generate clock

//initialization
reg [5:0] i = 6'b0;
initial begin
        cin = 1'b0;
        x = 8'b00000000;
        y = 8'b00000000;
    #10 x = 8'b00000001;
        y = 8'b00000011;
    for(i = 0;i < 15;i = i + 1) begin
    #10    x = x + 1;
        y = y + 1;
    end//begin
end //begin

//instantiation
carry_chain_adder u1(                
                   .x(x),
                   .y(y),
                   .cin(cin),
                   .sum(sum),
                   .cout(cout)
                );
endmodule


四、仿真结果


五、总结

一、在仿真之前需将源代码中的输出寄存器进行初始化,否则会在仿真波形在开始时出现红线。

二、仿真过程中使用脚本文件来进行仿真可以缩短仿真的时间,提高仿真的效率。

三、若代码中出现了parameter定义了某个常量时,常量的使用必须要在定义之后,模块的端口声明中也不能采用此定义。例如此例中的DSIZE不能在端口声明中出现,如果想这样使用,则应使用`define DSIZE在模块之外来定义,然后再模块之中使用`define来代替常量。即`define所声明的常量替代范围更大。



















猜你喜欢

转载自blog.csdn.net/qq_38352854/article/details/79636819