我是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中实现类似的外观和感觉,或者我可以定义端口映射的唯一地方是在模块名称之后的括号内?如果是这样,有什么原因吗?其他高清有我想要的功能吗? 最接近的是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中只有两种连接端口的方法,按名称或按顺序。你可以看看这篇文章,它解释了如何做到这一点。
然而,您可以找到类似于您在VHDL中使用port map
指令描述的内容。