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

相关推荐
中屹指纹浏览器40 分钟前
浏览器网络栈隔离技术研究:TCP/IP底层指纹生成与规避原理
经验分享·笔记
sulikey1 小时前
个人Linux操作系统学习笔记2 - gcc与库的理解
linux·笔记·学习·操作系统·gcc·
愚昧之山绝望之谷开悟之坡2 小时前
什么是Linter?什么是沙箱!
linux·笔记
菜鸡儿齐2 小时前
编程范式学习笔记
笔记·学习
可依软件crf2863 小时前
推荐一款特别的笔记软件:星轨笔记。普通用户免费功能也基本够用了,我已经免费使用几个月了。
笔记
三品吉他手会点灯3 小时前
C语言学习笔记 - 35.数据类型 - printf函数的非输出控制符与格式优化
c语言·开发语言·笔记·学习
sakiko_3 小时前
Swift学习笔记28-缓存
笔记·学习·swift
xian_wwq4 小时前
【学习笔记】探讨大模型应用安全建设系列3——护栏选型与输入输出防护
笔记·学习
晓梦林4 小时前
translate靶场学习笔记
笔记·学习·安全·web安全
阿Y加油吧4 小时前
两道经典动态规划题:乘积最大子数组 & 分割等和子集 复盘笔记
笔记·算法·动态规划