不好意思写,每日一题,太打脸了,看着写吧,题目如上
具体要求:
You are given a module
add16
that performs a 16-bit addition. Instantiate two of them to create a 32-bit adder. One add16 module computes the lower 16 bits of the addition result, while the second add16 module computes the upper 16 bits of the result, after receiving the carry-out from the first adder. Your 32-bit adder does not need to handle carry-in (assume 0) or carry-out (ignored), but the internal modules need to in order to function correctly. (In other words, theadd16
module performs 16-bit a + b + cin, while your module performs 32-bit a + b).Connect the modules together as shown in the diagram below. The provided module
add16
has the following declaration:
module add16 ( input[15:0] a, input[15:0] b, input cin, output[15:0] sum, output cout );
这道题就是要求我们用2个16位的加法器组成1个32位的加法器,另外在题目中还说明了虽然这个32位的加法器不需要处理进位,但是内部调用16位的加法器之间还是存在进位处理的
那么我们的输入是32位的就应该分成2个16位的数据,才能处理;或者是按照图内的处理方法分开调用。至于低16位的cin应该是给1'b0,之后把低位的cout给高位的cin
程序如下:
module top_module(
input [31:0] a,
input [31:0] b,
output [31:0] sum
);
reg lower_cout;//这里最好用wire型变量,在正式的程序中,reg变量好像是不可以用来连接两个模块的
add16 add16_lower(
.a(a[15:0]),
.b(b[15:0]),
.cin(1'b0),
.sum(sum[15:0]),
.cout(lower_cout)
);
add16 add16_higher(
.a(a[31:16]),
.b(b[31:16]),
.cin(lower_cout),
.sum(sum[31:16])
);
endmodule