Verilog刷题[hdlbits] :Module add

题目:Module add

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, the add16 module performs 16-bit a + b + cin, while your module performs 32-bit a + b).

  • 您将获得一个执行16位加法的模块add16。实例化其中两个以创建一个32位加法器。一个add16模块计算加法结果的下16位,而第二个add16模块在接收到第一个加法器的执行后计算结果的上16位。您的32位加法器不需要处理低位向本位的进位输入信号(假设为0)或本位向高位的进位输出信号(忽略),但内部模块需要这样做才能正常工作。(换句话说,add16模块执行16位a + b + cin,而您的模块执行32位a + b)。

Connect the modules together as shown in the diagram below. The provided module add16 has the following declaration:

  • 将模块连接在一起,如下图所示。所提供的模块add16有以下声明:
    module add16 ( input15:0 a, input15:0 b, input cin, output15:0 sum, output cout );
objectivec 复制代码
module top_module(
    input [31:0] a,
    input [31:0] b,
    output [31:0] sum
);
    //定义低16位加法器的输入与输出信号
    wire [15:0] add1_a;
    wire [15:0] add1_b;
    wire        cout1; //进位信号
    
   //定义高16位加法器的输入与输出信号
    wire [15:0] add2_a;
    wire [15:0] add2_b;
   	
    //对应进行赋值
    assign add1_a = a[15:0];
    assign add1_b = b[15:0];
    
    assign add2_a = a[31:16];
    assign add2_b = b[31:16];
    
    //低16位
    add16 add16_init_1(
        .a(add1_a),
        .b(add1_b),
        .cin(1'b0),
        .sum(sum[15:0]),
        .cout(cout1)
    );
    
    //高16位 
    add16 add16_init_2(
        .a(add2_a),
        .b(add2_b),
        .cin(cout1),
        .sum(sum[31:16])
    );
    
endmodule

上面的代码定义了 wire型变量使代码更加明了,但也可以使用下面的方法,只需定义一个进位信号即可。

objectivec 复制代码
module top_module(
    input [31:0] a,
    input [31:0] b,
    output [31:0] sum
);
  
    wire        cout1; //进位信号
    //低16位
    add16 add16_init_1(
        .a(a[15:0]),
        .b(b[15:0]),
        .cin(1'b0),
        .sum(sum[15:0]),
        .cout(cout1)
    );
    
    //高16位 
    add16 add16_init_2(
        .a(a[31:16]),
        .b(b[31:16]),
        .cin(cout1),
        .sum(sum[31:16])
    );
    
endmodule
相关推荐
自小吃多13 小时前
【Verilog/FPGA】存储器(数组)
嵌入式硬件·fpga开发
szxinmai主板定制专家13 小时前
RK3588+FPGA异构架构|高速数据采集+边缘AI落地应用全解析
人工智能·嵌入式硬件·fpga开发·架构·zynq
明德扬18 小时前
FPGA MIPI 多路视频聚合方案
fpga开发·mipi视频拼接·mipi多路拼接·mipi协议·mipi视频拼接方案·fpga mipi方案·mipi csi
XMAIPC_Robot18 小时前
为什么大型储能需要边缘 AI 协调控制器?RK3588+FPGA 高速采集方案
人工智能·嵌入式硬件·fpga开发·arm+fpga·rk3588+fpga·协调控制器
szxinmai主板定制专家19 小时前
RK3576+CODESYS+RK182X+FPGA异构架构:半导体精密设备一体化控制器方案
人工智能·fpga开发·架构·rk3576+codesys
szxinmai主板定制专家19 小时前
半导体设备控制器:RK3576+CODESYS+RK182X异构架构,实现晶圆视觉检测与运动联动闭环
人工智能·fpga开发·架构·视觉检测·边缘计算·codesys·rk3576
m0_5474866620 小时前
《数字电路与逻辑设计:基于Verilog HDL和Quartus Prime》全套PPT课件2026
verilog·数字电路·数字逻辑电路
szxinmai主板定制专家20 小时前
FPGA高速IO+RK182X AI算力|RK3576+CODESYS工业自动化运动控制平台设计
运维·人工智能·嵌入式硬件·fpga开发·自动化·zynq
FPGA小徐20 小时前
原理图分析:Type-C 转串口(CH340C)最小系统板 CSDN 博客
fpga开发
智感子21 小时前
FPGA采集:高采样率数据流与存储
fpga开发