generate for-loop:100-bit binary adder2

Create a 100-bit binary ripple-carry adder by instantiating 100 full adders. The adder adds two 100-bit numbers and a carry-in to produce a 100-bit sum and carry out. To encourage you to actually instantiate full adders, also output the carry-out from each full adder in the ripple-carry adder. cout[99] is the final carry-out from the last full adder, and is the carry-out you usually see.

Adder100i - HDLBits (01xz.net)

 1 module top_module( 
 2     input [99:0] a, b,
 3     input cin,
 4     output [99:0] cout,
 5     output [99:0] sum );
 6  assign sum[0] = cin^ a[0] ^ b[0];
 7     assign cout[0] = (a[0]&b[0]) | (a[0]&cin) | (b[0]&cin);   //这个是全加器的代数式
 8     
 9     genvar i;   //genvar又是什么了?
10     generate
11         for(i=1;i<100;i=i+1)begin: adder
12             assign sum[i] = cout[i-1]^a[i]^b[i];
13             assign cout[i] = (a[i]&b[i]) | (a[i]&cout[i-1]) | (b[i]&cout[i-1]);
14         end   //既然都写了i的循环,为什么还要assigna[0]了
15     endgenerate
16 
17 endmodule

 再写:

genvar和integer的区别 - CSDN文库

//既然都写了i的循环,为什么还要assigna[0]了  这个注释解释一下,因为i=0的时候直接写,有一个cout[i-1]的影响,是写不出来的
所以要单独声明
module top_module( 
    input [99:0] a, b,
    input cin,
    output [99:0] cout,
    output [99:0] sum );
	genvar i;
    generate
        for(i=0;i<100;i++)
            begin:adder
                if(i==0)
                    assign{cout[0],sum[0]}=a[0]+b[0]+cin;
                else
                    assign{cout[i],sum[i]}=a[i]+b[i]+cout[i-1];
            end
    endgenerate
endmodule

  

posted @ 2023-04-20 11:01  江左子固  阅读(72)  评论(0)    收藏  举报