题目描述:https://hdlbits.01xz.net/wiki/Adder100i
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.
理解生成语句:
生成语句可以动态的生成verilog代码,当对矢量中的多个位进行重复操作时,或者当进行多个模块的实例引用的重复操作时,或者根据参数的定义来确定程序中是否应该包含某段Verilog代码的时候,使用生成语句能大大简化程序的编写过程。
生成语句生成的实例范围,关键字generate-endgenerate用来指定该范围。生成实例可以是以下的一个或多个类型:
(1)模块;(2)用户定义原语;(3)门级语句;(4)连续赋值语句;(5)initial和always块。
generate语句有generate-for,generate-if,generate-case三种语句。
generate-for语句
(1) 必须有genvar关键字定义for语句的变量。
(2)for语句的内容必须加begin和end(即使就一句)。
(3)for语句必须有个名字。
我的解决方案:
module top_module(
input [99:0] a, b,
input cin,
output [99:0] cout,
output [99:0] sum );
add add0(a[0], b[0], cin, cout[0], sum[0]);
genvar i;
generate
for(i = 1; i < 100; i = i + 1) begin: myadd
add addi(a[i], b[i], cout[i-1], cout[i], sum[i]);
end
endgenerate
endmodule
module add(
input a1,
input b1,
input ci1,
output co1,
output s1 );
assign s1 = a1 ^ b1 ^ ci1;
assign co1 = (a1 & b1) | (a1 & ci1) |(b1 & ci1);
endmodule