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

相关推荐
红花与香菇2____2 小时前
【学习笔记】Cadence电子设计全流程(二)原理图库的创建与设计(上)
笔记·嵌入式硬件·学习·pcb设计·cadence·pcb工艺
拥有一颗学徒的心5 小时前
鸿蒙第三方库MMKV源码学习笔记
笔记·学习·性能优化·harmonyos
俊哥V9 小时前
[笔记.AI]如何判断模型是否通过剪枝、量化、蒸馏生成?
人工智能·笔记
【云轩】11 小时前
用DeepSeek零基础预测《哪吒之魔童闹海》票房——从数据爬取到模型实战
经验分享·笔记
此去经年。11 小时前
I2C学习笔记-软件模拟I2C
笔记·单片机·学习
汇能感知11 小时前
汇能感知的光谱相机/模块产品有哪些?
经验分享·笔记·科技
安和昂13 小时前
effective-Objective-C第六章阅读笔记
开发语言·笔记·objective-c
雅俗共赏10014 小时前
提升信息检索准确性和效率的搜索技巧
笔记·搜索引擎
孤寂大仙v14 小时前
侯捷 C++ 课程学习笔记:设计模式在面向对象开发中的应用
c++·笔记·学习
W.KN15 小时前
算法日常刷题笔记(1)
笔记