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

相关推荐
chushiyunen3 分钟前
prompt提示词工程笔记
笔记·prompt
hanlin035 分钟前
刷题笔记:力扣第6题-Z字形变换
笔记·算法·leetcode
一个人旅程~1 小时前
虚数与量子迷踪
经验分享·笔记·微信·电脑·量子计算
智者知已应修善业9 小时前
【proteus中lm339电压滞回比较器达到三角波转换成方波】2023-4-13
驱动开发·经验分享·笔记·硬件架构·proteus·硬件工程
551只玄猫10 小时前
新编大学德语1第三版笔记 第5课Essen und Trinken
笔记·学习笔记·德语·外语·德语a1·自学德语·新编大学德语
不只会拍照的程序猿11 小时前
《嵌入式AI筑基笔记02:Python数据类型01,从C的“硬核”到Python的“包容”》
人工智能·笔记·python
中屹指纹浏览器15 小时前
2026指纹浏览器性能瓶颈分析与优化技巧
经验分享·笔记
雷工笔记16 小时前
随笔|走!跳楼去!
笔记
就叫飞六吧16 小时前
国产数据库gbase8s安装-网盘
笔记
网络工程小王17 小时前
【大数据技术详解】——Kibana(学习笔记)
大数据·笔记·学习