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

相關文章
相關標籤/搜索