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

相关推荐
Hello_Embed9 分钟前
嵌入式上位机开发入门(二):常用 API
笔记·stm32·嵌入式·信息与通信
九成宫9 分钟前
IT项目管理期末复习——Chapter 3 项目管理过程组:案例研究
笔记·项目管理·软件工程
寒秋花开曾相惜11 分钟前
(学习笔记)3.8 指针运算(3.8.5 变长数组)
java·c语言·开发语言·笔记·学习
南境十里·墨染春水12 分钟前
C++笔记 构造函数 析构函数 及二者关系(面向对象)
开发语言·c++·笔记·ecmascript
talen_hx29635 分钟前
《零基础入门Spark》学习笔记 Day 07
笔记·学习·spark
Cathy Bryant1 小时前
拓扑学:曲面与圆环
笔记·线性代数·矩阵·拓扑学
凉、介1 小时前
SylixOS 多核启动
服务器·笔记·学习·嵌入式·sylixos
Yu_Lijing1 小时前
基于C++的《Head First设计模式》笔记——原型模式
c++·笔记·设计模式
猹叉叉(学习版)2 小时前
【系统分析师_知识点整理】 8.项目管理
笔记·项目管理·软考·系统分析师
hssfscv2 小时前
软件设计师 试题三 面向对象——UML事物、关系、图
笔记·学习·uml