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 块的行为语句内使用这个变量。这样做可以避免直接修改输入端口的值。

相关推荐
未若君雅裁5 分钟前
LeetCode 18 - 四数之和 详解笔记
java·数据结构·笔记·算法·leetcode
受之以蒙30 分钟前
具身智能的“任督二脉”:用 Rust ndarray 打通数据闭环的最后一公里
人工智能·笔记·rust
chushiyunen1 小时前
django使用笔记
笔记·python·django
YJlio2 小时前
进程和诊断工具学习笔记(8.19):Hyper-V 来宾调试与符号配置 —— 在虚拟化场景下用 LiveKd 抓现场
网络·笔记·学习
星轨初途2 小时前
《数据结构二叉树之堆 —— 优先队列与排序的高效实现(2)(下)》
c语言·开发语言·数据结构·经验分享·笔记·性能优化
d111111111d3 小时前
MPU6050简介(学习笔记)
笔记·stm32·单片机·嵌入式硬件·学习
两个人的幸福online3 小时前
cocos 的笔记(不定期完善)
笔记
摇滚侠6 小时前
Vue 项目实战《尚医通》,预约挂号就诊人组件搭建上,笔记40
前端·javascript·vue.js·笔记
心无旁骛~11 小时前
Masquerade 总结笔记:解锁野外人类视频的机器人政策学习潜力
笔记·机器人
谅望者12 小时前
数据分析笔记14:Python文件操作
大数据·数据库·笔记·python·数据挖掘·数据分析