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

相关推荐
Amazing_Cacao7 分钟前
CFCA精品可可工艺师认证课程初级校准:破解机器黑盒,实现物理参数与最终风味的精准对齐
笔记
所愿ღ13 分钟前
SSM框架-Spring1
java·开发语言·笔记·spring
其实防守也摸鱼26 分钟前
计算机组成原理--5.输入/输出(I/O)系统
服务器·笔记·深度学习·计算机网络·知识图谱·工具·计算机组成原理
tq108630 分钟前
自觉决断与习得考古:自我意识觉醒的三阶模型
笔记
YYYing.35 分钟前
【C++项目之高并发内存池 (二)】整体框架设计与ThreadCache的初步实现
笔记·高并发·线程池·c/c++
愚昧之山绝望之谷开悟之坡37 分钟前
hermes里面的skill和mcp大白话介绍
笔记
Flittly38 分钟前
【SpringSecurity新手村系列】(5)RBAC角色权限与账户状态校验
java·spring boot·笔记·安全·spring·ai
YaBingSec1 小时前
玄机靶场-2024ccb初赛sc05 WP
android·运维·网络·笔记·安全·ssh
所愿ღ1 小时前
SSM框架-Spring2
java·开发语言·笔记·spring
菜菜的顾清寒1 小时前
力扣笔记自用
笔记·算法·leetcode