Verilog刷题笔记29

题目:

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.

解题:

bash 复制代码
module top_module( 
    input [99:0] a, b,
    input cin,
    output [99:0] cout,
    output [99:0] sum );
    integer i;
    reg cined;
    always@(*)begin
        cined=cin;
        for(i=0;i<100;i++)
            begin
                sum[i]=cined^a[i]^b[i];
                cout[i]=(a[i]&b[i])|((a[i]^b[i])&cined);
                cined=cout[i];
            end
    end 

endmodule

结果正确:

说明:

为什么需要reg cined?

输入端口在 Verilog 中是不允许赋值的,因为它是顶层模块的输入端口,需要从模块外部提供数据。因此创建了一个cined(内部的变量)来代替 "cin",然后在 always 块的行为语句内使用这个变量。这样做可以避免直接修改输入端口的值。

相关推荐
程序员大宝10133 分钟前
如何设计三高架构
笔记
成都犀牛43 分钟前
LangGraph 深度学习笔记:构建真实世界的智能代理
人工智能·pytorch·笔记·python·深度学习
sealaugh321 小时前
docker(学习笔记第一课) 使用nginx +https + wordpress
笔记·学习·docker
村头的猫1 小时前
如何通过 noindex 阻止网页被搜索引擎编入索引?
前端·经验分享·笔记·搜索引擎
Resurgence032 小时前
建造者模式Builder Pattern
笔记·建造者模式
z2014z3 小时前
第11章 结构 笔记
笔记·c#
FF-Studio4 小时前
【DSP笔记 · 第5章】数字滤波器的蓝图:从数学公式到硬件实现的艺术
笔记·fpga开发·自动化·音视频·音频·信号处理
大磕学家ZYX5 小时前
JavaScript学习笔记
javascript·笔记·学习
囚生CY12 小时前
【学习笔记】Langchain基础(二)
笔记·学习·langchain
忘川w16 小时前
《网络安全与防护》知识点复习
笔记·安全·web安全·网络安全