使用 'assign' 绑定模块端口
Using 'assign' for binding module ports
我是 Verilog 的新手并且还在学习它,但我对这种语言的第一印象是它全都与端口互连有关。于是,我天真地写了下面的代码
module mult4(input wire [3:0] x, y,
output wire [7:0] z)
sum8 sh, sl, ss;
assign sl.x = (y[0] ? x : 0),
sl.y = (y[1] ? x : 0) << 1,
sh.x = (y[2] ? x : 0) << 2,
sh.x = (y[3] ? x : 0) << 3,
ss.x = sl.z,
ss.y = sh.z,
sl.cin = 0,
sh.cin = 0,
ss.cin = 0,
z = ss.z;
endmodule
而且它根本行不通。这里 sum8
只是一个具有以下签名的 8 位加法器:
module sum8(input wire cin, // carry-in bit
input wire [7:0] x, y, // operands
output wire cout, // carry-out bit
output wire [7:0] z); // result
当然,我可以重写这段代码使其通过,但我想知道另一件事。有没有办法在 Verilog 中实现类似的外观,或者我可以定义端口映射的唯一地方是模块名称后的括号内?如果是这样,有什么原因吗?其他 HDL 有我想要的功能吗?
据我所知,在 Verilog 中只有两种连接端口的方法,按名称或按顺序。您可以查看 this article,其中介绍了执行此操作的方法。
但是,您可以使用 the port map
directive.
找到与您在 VHDL 中描述的内容类似的内容
最接近的是 SystemVerilog 中的 interface
结构。它看起来像这样:
interface adder8_itf;
wire cin; // carry-in bit
wire [7:0] x, y; // operands
wire cout; // carry-out bit
wire [7:0] z; // result
endinterface
module mult4(input wire [3:0] x, y,
output wire [7:0] z);
adder8_itf sh(), sl(), ss();
sum8 i1(sh), i2(sl), i3(ss);
assign sl.x = (y[0] ? x : 0),
sl.y = (y[1] ? x : 0) << 1,
sh.x = (y[2] ? x : 0) << 2,
sh.x = (y[3] ? x : 0) << 3,
ss.x = sl.z,
ss.y = sh.z,
sl.cin = 0,
sh.cin = 0,
ss.cin = 0,
z = ss.z;
endmodule
module sum8 (adder8_itf itf);
assign {itf.cout,itf.z} = itf.x +itf.y;
endmodule
不过,这可能需要更多工作,以便您可以以不同的方式组织端口分配。
我是 Verilog 的新手并且还在学习它,但我对这种语言的第一印象是它全都与端口互连有关。于是,我天真地写了下面的代码
module mult4(input wire [3:0] x, y,
output wire [7:0] z)
sum8 sh, sl, ss;
assign sl.x = (y[0] ? x : 0),
sl.y = (y[1] ? x : 0) << 1,
sh.x = (y[2] ? x : 0) << 2,
sh.x = (y[3] ? x : 0) << 3,
ss.x = sl.z,
ss.y = sh.z,
sl.cin = 0,
sh.cin = 0,
ss.cin = 0,
z = ss.z;
endmodule
而且它根本行不通。这里 sum8
只是一个具有以下签名的 8 位加法器:
module sum8(input wire cin, // carry-in bit
input wire [7:0] x, y, // operands
output wire cout, // carry-out bit
output wire [7:0] z); // result
当然,我可以重写这段代码使其通过,但我想知道另一件事。有没有办法在 Verilog 中实现类似的外观,或者我可以定义端口映射的唯一地方是模块名称后的括号内?如果是这样,有什么原因吗?其他 HDL 有我想要的功能吗?
据我所知,在 Verilog 中只有两种连接端口的方法,按名称或按顺序。您可以查看 this article,其中介绍了执行此操作的方法。
但是,您可以使用 the port map
directive.
最接近的是 SystemVerilog 中的 interface
结构。它看起来像这样:
interface adder8_itf;
wire cin; // carry-in bit
wire [7:0] x, y; // operands
wire cout; // carry-out bit
wire [7:0] z; // result
endinterface
module mult4(input wire [3:0] x, y,
output wire [7:0] z);
adder8_itf sh(), sl(), ss();
sum8 i1(sh), i2(sl), i3(ss);
assign sl.x = (y[0] ? x : 0),
sl.y = (y[1] ? x : 0) << 1,
sh.x = (y[2] ? x : 0) << 2,
sh.x = (y[3] ? x : 0) << 3,
ss.x = sl.z,
ss.y = sh.z,
sl.cin = 0,
sh.cin = 0,
ss.cin = 0,
z = ss.z;
endmodule
module sum8 (adder8_itf itf);
assign {itf.cout,itf.z} = itf.x +itf.y;
endmodule
不过,这可能需要更多工作,以便您可以以不同的方式组织端口分配。