SystemVerilog的几种模块实例化连接方式

子模块代码

后面的例子中acc_accum会用到这些子模块

parameter logic [7:0] My_DataIn = 8'hFF;
module alu (
    output reg [7:0] alu_out,
    output reg       zero,
    input      [7:0] ain,
    bin,
    input      [2:0] opcode
);
    // RTL code for the alu module
endmodule
module accum (
    output reg [7:0] dataout,
    input      [7:0] datain = My_DataIn,
    input            clk,
    rst_n = 1'b1
);
    // RTL code for the accumulator module
endmodule
module xtend (
    output reg [7:0] dout,
    input            din,
    input            clk,
    rst = 1'b0
);
    // RTL code for the sign-extension module
endmodule

1. 按端口顺序进行位置连接

在模块实例化中列出的端口表达式与实例化模块内声明的端口之间建立连接的一种方法是有序列表;也就是说,模块实例列出的端口表达式应与模块声明中列出的端口的顺序相同。
连接可以是对reg或wire的简单引用、表达式或空白。表达式可用于为模块输入端口提供值。空白端口连接应表示端口不连接的情况。但是,如果省略了与具有默认值的输入端口的端口连接(由逗号分隔列表中缺少的参数表示),则应使用默认值。

module alu_accum1 (
    output [15:0] dataout,
    input  [ 7:0] ain,
    bin,
    input  [ 2:0] opcode,
    input         clk,
    rst_n,
    rst
);
    wire [7:0] alu_out;
alu alu (alu_out, , ain, bin, opcode); // zero output is unconnected 
accum accum (dataout[7:0], alu_out, clk, rst_n); 
xtend xtend (dataout[15:8], alu_out[7], clk); // rst gets default 
// value 1'b0 
endmodule

2. 使用完全显式连接的命名端口连接

语法如下:
module_name instance_name ( .port_name(expression), .port_name(expression) );

module alu_accum2 (
    output [15:0] dataout,
    input  [ 7:0] ain,
    bin,
    input  [ 2:0] opcode,
    input         clk,
    rst_n,
    rst
);
    wire [7:0] alu_out;
    alu alu (
        .alu_out(alu_out),
        .zero(),
        .ain(ain),
        .bin(bin),
        .opcode(opcode)
    );
    // zero output is unconnected 
    accum accum (
        .dataout(dataout[7:0]),
        .datain(alu_out),
        .clk(clk)
    );
    // rst_n is not in the port list and so gets default value 1'b1 
    xtend xtend (
        .dout(dataout[15:8]),
        .din (alu_out[7]),
        .clk (clk),
        .rst ()
    );
    // rst has a default value, but has an empty port connection, 
    // therefore it is left unconnected 
endmodule

3. 使用隐式连接的命名端口连接

在以下alu_accum3示例中,实例化的alu模块的所有端口名称都与连接到端口的声明名称相匹配,除了未连接的zero端口,它使用命名端口连接列出,表明该端口未连接。对于实例化模块上所有名称和等效类型匹配的连接,都进行了隐式的.name端口连接。

module alu_accum3 (
    output [15:0] dataout,
    input  [ 7:0] ain,
    bin,
    input  [ 2:0] opcode,
    input         clk,
    rst_n
);
    wire [7:0] alu_out;
    alu alu (
        .alu_out,
        .zero(),
        .ain,
        .bin,
        .opcode
    );
    accum accum (
        .dataout(dataout[7:0]),
        .datain (alu_out),
        .clk,
        .rst_n  ()
    );
    xtend xtend (
        .dout(dataout[15:8]),
        .din (alu_out[7]),
        .clk,
        .rst
    );
    // Error: rst does not exist in the instantiation module
endmodule

4. 使用通配符端口名称的命名端口连接

SystemVerilog 可以使用 .* 通配符语法对所有端口进行隐式实例化,只要实例端口名称与连接端口名称匹配,并且它们的数据类型相同。这样就无需列出任何端口,只要连接声明的名称和类型与实例端口的名称和等效类型匹配。这种隐式端口连接样式用于指示所有端口名称和类型都与连接匹配,其中重点只放在异常端口上。命名端口连接可以与 .* 连接混合使用,以将端口连接覆盖为不同的表达式或使端口保持未连接状态。隐式 .* 端口连接语法可以极大地促进快速生成块级测试台,其中选择的所有测试台声明都与实例化的模块端口名称和类型匹配。
隐式 .* 端口连接在语义上等同于实例化模块中声明的每个端口的隐式 .name 端口连接,但有以下两个例外:

  1. 如果实例化使用 .name 端口连接,则不应使用该端口的默认值。如果实例化范围内不存在该名称,则会出现错误。但是,当使用 .* 时,如果实例化范围内不存在该名称,则应使用默认值。在这种情况下,如果特定实例化确实需要未连接的端口,则除了 .* 之外,还可以使用 .name()。
  2. 使用 .* 无法为从包中通配符导入名称创建足够的引用。命名或隐式 .name 连接可以与 .* 连接混合使用,以便为从包中通配符导入名称创建足够的引用。

在同一个 alu_accum4 示例中,accum 模块有一个名为 dataout 的 8 位端口,该端口连接到一个名为 dataout 的 16 位总线。由于 dataout 的内部和外部大小不匹配,因此必须按名称连接端口,显示 16 位总线的哪些位连接到 8 位端口。accum 上的 datain 端口通过不同的名称 (alu_out) 连接到总线;因此,此端口也是按名称连接的。clk 端口使用隐式 .* 端口连接进行连接,而 rst_n 在实例化级别不存在,因此使用默认的 rst_n 值。同样在同一个 alu_accum4 示例中,xtend 模块有一个名为 dout 的 8 位输出端口和一个名为 din 的 1 位输入端口。由于这两个端口名称均与连接声明的名称(或大小)不匹配,因此两者都按名称连接。 clk 端口使用隐式 .* 端口连接进行连接,而 rst 在实例化级别不存在,因此使用默认的 rst 值。

module alu_accum4 (
    output [15:0] dataout,
    input  [ 7:0] ain,
    bin,
    input  [ 2:0] opcode,
    input         clk
);
    wire [7:0] alu_out;
alu alu (.*, .zero());
accum accum (.*, .dataout(dataout[7:0]), .datain(alu_out));
xtend xtend (.*, .dout(dataout[15:8]), .din(alu_out[7]));
endmodule

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值