HDLBits 刷题笔记42:Generate for-loop: 100-bit binary adder 2 (Adder100i)

题目描述:https://hdlbits.01xz.net/wiki/Adder100i

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.

理解生成语句:

        生成语句可以动态的生成verilog代码,当对矢量中的多个位进行重复操作时,或者当进行多个模块的实例引用的重复操作时,或者根据参数的定义来确定程序中是否应该包含某段Verilog代码的时候,使用生成语句能大大简化程序的编写过程。

       生成语句生成的实例范围,关键字generate-endgenerate用来指定该范围。生成实例可以是以下的一个或多个类型: 

(1)模块;(2)用户定义原语;(3)门级语句;(4)连续赋值语句;(5)initial和always块。

generate语句有generate-for,generate-if,generate-case三种语句。
generate-for语句
(1) 必须有genvar关键字定义for语句的变量。
(2)for语句的内容必须加begin和end(即使就一句)。
(3)for语句必须有个名字。

我的解决方案:

module top_module( 
    input [99:0] a, b,
    input cin,
    output [99:0] cout,
    output [99:0] sum );
    add add0(a[0], b[0], cin, cout[0], sum[0]);
	genvar i;
    generate 
        for(i = 1; i < 100; i = i + 1) begin: myadd
            add addi(a[i], b[i], cout[i-1], cout[i], sum[i]);
        end
    endgenerate
endmodule

module add(
    input a1,
    input b1,
    input ci1,
    output co1,
    output s1 );
    assign s1 = a1 ^ b1 ^ ci1;
    assign co1 = (a1 & b1) | (a1 & ci1) |(b1 & ci1);
endmodule

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值