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

相关推荐
懒惰的bit9 天前
STM32F103C8T6 学习笔记摘要(四)
笔记·stm32·学习
zkyqss9 天前
OVS Faucet练习(下)
linux·笔记·openstack
浦东新村轱天乐9 天前
【麻省理工】《how to speaking》笔记
笔记
奔跑的蜗牛AZ9 天前
TiDB 字符串行转列与 JSON 数据查询优化知识笔记
笔记·json·tidb
cwtlw9 天前
Excel学习03
笔记·学习·其他·excel
杭州杭州杭州9 天前
计算机网络笔记
笔记·计算机网络
cyborg9 天前
终于再也不用在notion中写公式了
笔记
循环过三天9 天前
1.2、CAN总线帧格式
笔记·stm32·单片机·嵌入式硬件·学习
循环过三天9 天前
1.1、CAN总线简介
笔记·stm32·单片机·嵌入式硬件·学习
mooyuan天天9 天前
pikachu靶场通关笔记43 SSRF关卡01-CURL(三种方法渗透)
笔记·安全·web安全·ssrf漏洞