Verilog实例数组对于一个定义好的简单module,例如加法器

Verilog 实例数组

对于一个定义明确的简单模块,比如加法器,如果我们要实例化它几十次上百次,而这些实例化的形式基本相同,那我们肯定不能单独配对。它是实例化的,此时我们可以使用一种方式来实例化一个数组,以便快速实例化。

例如,如果我们要实现的功能如下:

    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.

给定的端口定义如下

图片[1]-Verilog实例数组对于一个定义好的简单module,例如加法器-唐朝资源网

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

图片[2]-Verilog实例数组对于一个定义好的简单module,例如加法器-唐朝资源网

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

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

图片[3]-Verilog实例数组对于一个定义好的简单module,例如加法器-唐朝资源网

然后使用实例化数组语法

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模块。

© 版权声明
THE END
喜欢就支持一下吧
点赞119赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容