HDLBits->Circuits->Arithmetic Circuitd->3-bit binary adder

Verilog实例数组

对于一个定义好的简单module,例如加法器之类,如果我们要对其进行几十次几百次的例化,并且这些例化基本都是相同的形式,那么我们肯定不能一个个的单独对其进行例化,此时我们就可以使用一种例化数组的方式进行快速的例化。
举个例子,如果我们要实现的功能如下:

    Now that you know how to build a full adder, make 3 instances of it to create a 3-bit binary ripple-carry adder. The adder adds two 3-bit numbers and a carry-in to produce a 3-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[2] is the final carry-out from the last full adder, and is the carry-out you usually see.

给出的端口定义如下

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

那么我们可以先定义一个一位带进位的加法模块

module Adder1( 
    input a, b, cin,
    output cout, sum );
    assign {cout,sum} = a + b + cin;
endmodule

然后使用实例化数组语法

module Adder3( 
    input [2:0] a, b,
    input cin,
    output [2:0] cout,
    output [2:0] sum );
    Adder1 Adder1[2:0](//将我们例化的模块写成一个一位加法器的三倍位宽
        .a(a),//每一个端口的位宽都是原来一位加法器的三倍位宽
        .b(b),
        .cin({cout[1:0],cin}),
        .cout(cout),
        .sum(sum)
    );

上面这段例化实际是例化了三个add模块,名字分别为 add_3[0], add_3[1], add_3[2];
从原理图我们就可以看出确实是例化了三个Adder1模块.

posted @ 2022-06-23 21:14  TwoDogJay  阅读(68)  评论(0编辑  收藏  举报