In this exercise, you will create a circuit with two levels of hierarchy. Your top_module
will instantiate two copies of add16
(provided), each of which will instantiate 16 copies of add1
(which you must write). Thus, you must write two modules: top_module
and add1
.
Like module_add, you are given a module add16
that performs a 16-bit addition. You must 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. Your 32-bit adder does not need to handle carry-in (assume 0) or carry-out (ignored).
Connect the add16
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 );
Within each add16
, 16 full adders (module add1
, not provided) are instantiated to actually perform the addition. You must write the full adder module that has the following declaration:
module add1 ( input a, input b, input cin, output sum, output cout );
Recall that a full adder computes the sum and carry-out of a+b+cin.
In summary, there are three modules in this design:
top_module
— Your top-level module that contains two of...add16
, provided — A 16-bit adder module that is composed of 16 of...add1
— A 1-bit full adder module.
If your submission is missing a module add1
, you will get an error message that says Error (12006): Node instance "user_fadd[0].a1" instantiates undefined entity "add1"
.
译:
在这个练习中,您将创建一个具有两个层级结构的电路。您的顶级模块`top_module`将实例化两个`add16`模块(已提供),每个模块将实例化16个`add1`模块(您需要编写)。因此,您需要编写两个模块:`top_module`和`add1`。
如同`module_add`一样,您被给定了一个`add16`模块,它执行16位加法。您必须实例化两个这样的模块来创建一个32位加法器。一个`add16`模块计算加法结果的低16位,而第二个`add16`模块计算结果的高16位。您的32位加法器不需要处理进位输入(假设为0)或进位输出(将被忽略)。
请根据下图所示连接`add16`模块。所提供的`add16`模块具有以下声明:
```verilog
module add16 (input[15:0] a, input[15:0] b, input cin, output[15:0] sum, output cout);
```
在每个`add16`模块内部,实例化了16个全加器(`module add1`,未提供)来实际执行加法。您必须编写全加器模块,它具有以下声明:
```verilog
module add1 (input a, input b, input cin, output sum, output cout);
```
请记住,全加器计算的是`a+b+cin`的和与进位输出。
总结来说,这个设计中有三个模块:
- `top_module` — 您的顶级模块,包含两个`add16`模块的实例。
- `add16`(已提供)— 一个由16个`add1`模块组成的16位加法器模块。
- `add1` — 一个1位全加器模块。
module top_module (
input [31:0] a,
input [31:0] b,
output [31:0] sum
);//
wire [15:0]sum1,sum2;
wire cout_1;
add16 add_1(a[15:0],b[15:0],1'b0,sum1[15:0],cout_1);
add16 add_2(a[31:16],b[31:16],cout_1,sum2[15:0]);
assign sum={sum2,sum1};
endmodule
module add1 ( input a, input b, input cin, output sum, output cout );
// Full adder module here
assign {cout,sum} = a + b + cin;
endmodule
代码解释:
用于创建一个32位加法器电路。这个电路由两个主要模块组成:top_module
和add1
。下面是对这两个模块的详细解释:
顶级模块 top_module
这个模块是整个加法器电路的顶层模块,它接收两个32位的输入a
和b
,并输出一个32位的和sum
。为了实现32位加法,该模块使用了两个16位加法器add16
模块。
input [31:0] a
和input [31:0] b
:这是32位的输入信号,分别代表要相加的两个数。output [31:0] sum
:这是输出信号,代表两个输入数相加的结果。wire [15:0] sum1, sum2
:这是两个16位的中间信号,分别用于存储add16
模块的输出。wire cout_1
:这是一个1位的中间信号,用于传递第一个add16
模块产生的进位。
add16 add_1(a[15:0], b[15:0], 1'b0, sum1[15:0], cout_1);
:这行代码实例化了第一个add16
模块,用于计算输入数的低16位相加的结果。由于这是最低位的加法,所以进位输入cin
被设置为0(1'b0
表示1位的二进制0)。
add16 add_2(a[31:16], b[31:16], cout_1, sum2[15:0]);
:这行代码实例化了第二个add16
模块,用于计算输入数的高16位相加的结果。这个模块接收第一个add16
模块产生的进位cout_1
作为进位输入。
最后,assign sum={sum2, sum1};
这行代码将两个16位的和sum1
和sum2
组合起来,形成最终的32位和,然后赋值给输出sum
。
全加器模块 add1
这个模块实现了一个1位全加器的功能,它接收三个输入信号:两个加数位a
和b
,以及一个进位输入cin
,并输出和sum
和进位输出cout
。
input a
和input b
:这是两个1位的输入信号,分别代表要相加的两个数位。input cin
:这是1位的进位输入信号。output sum
:这是1位的和输出信号。output cout
:这是1位的进位输出信号。
assign {cout, sum} = a + b + cin;
:这行代码使用Verilog的位拼接和加法运算符来计算三个输入信号的和以及产生的进位。结果被赋值给sum
和cout
。
总的来说,这段代码定义了一个32位加法器电路,它通过将两个16位加法器模块和1位全加器模块组合起来,实现了32位的加法运算。
在Verilog中,assign {cout, sum} = a + b + cin;
这行代码是在定义一个全加器的行为。这里的a
、b
和cin
是输入信号,cout
和sum
是输出信号。全加器的作用是将两个一位二进制数a
和b
相加,并考虑可能的进位输入cin
,然后输出和sum
以及可能的进位输出cout
。
运行结果: