HDLBits Bcdadd100

原始题目:git

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.ide

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 Declarationcode

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

Hintip

An instance array or generate statement would be useful here.ci

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

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

学习 verilog 的第三天,老是以为本身脑子被了驴踢了同样,老是有奇奇怪怪的小错误。io

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

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

相关文章
相关标签/搜索