HDLBits Bcdadd100

原始题目:

You are provided with a BCD one-digit adder named bcd_fadd that adds two BCD digits and carry-in, and produces a sum and carry-out.

module bcd_fadd {
   input [3:0] a,
   input [3:0] b,
   input     cin,
   output   cout,
   output [3:0] sum );

Instantiate 100 copies of bcd_fadd to create a 100-digit BCD ripple-carry adder. Your adder should add two 100-digit BCD numbers (packed into 400-bit vectors) and a carry-in to produce a 100-digit sum and carry out.

Module Declaration

module top_module( 
   input [399:0] a, b,
   input cin,
   output cout,
   output [399:0] sum );

Hint

An instance array or generate statement would be useful here.

Generate for语句可以一次性例化多个相同的模块,并且这些模块是并行运行的。又因为如此,我们无法串行地将前一个全加器的 cout 传递给下一个全加器的 cin ,只能定义 n 个中间变量传递。

另外,由于下标中存在变量,我们无法通过指定起始下标和终止下标的方式,因为这样会发生 xx is not a constant File 的编译错误;我们只能通过指定起始下标和位长的方式来编写。

学习 verilog 的第三天,总是觉得自己脑子被了驴踢了一样,总是有奇奇怪怪的小错误。

指定位长的时候,为啥我用升序和降序都是对的?这两个不是应该有大小端的区别吗?

module top_module( 
    input [399:0] a, b,
    input cin,
    output cout,
    output [399:0] sum );

    reg cins[100:0];
    always @(*) begin
        cins[0] <= cin;
        cout <= cins[100];
    end
    genvar i;
    generate
        for (i = 0; i < 100; i++) begin:genblock
            bcd_fadd myfadd (.a(a[i*4+3-:4]), .b(b[i*4+3-:4]), .cin(cins[i]), .cout(cins[i+1]), .sum(sum[i*4+3-:4]));
        end
    endgenerate
endmodule

by SDUST weilinfox

posted @ 2021-04-02 14:48  桜風の狐  阅读(684)  评论(0编辑  收藏  举报