我一直在尝试构建一个模块,该模块返回(3 位)输入的二进制补码表示(第一位是符号)。我认为以下代码在概念上是正确的,但我可能遗漏了它的结构:当我尝试编译时,出现以下错误:
(vlog-2110) Illegal reference to net "f_o".
(vlog-2110) Illegal reference to net "f_o".
(vlog-2110) Illegal reference to net "f_o".
Run Code Online (Sandbox Code Playgroud)
搜索该错误表明,在同时使用变量作为输入和输出时通常会出现该错误,但这不是我的情况。你能指出错误在哪里吗?
module ca2 (a_i,f_o);
input [2:0] a_i;
output [2:0] f_o;
always @(a_i[2:0] or f_o[2:0])
begin
if (a_i[2] == 1)
begin
f_o[2] = a_i[2];
f_o[1:0] = (~a_i[1:0] + 'b1);
end
else
begin
f_o = a_i;
end
end
endmodule
Run Code Online (Sandbox Code Playgroud)
小智 2
在 Verilog 中,在大多数情况下,未声明的标识符被视为隐式连线声明。由于 f_o 尚未声明,编译器将其视为连线,而不是变量。这会导致编译器抱怨所有的分配。
// What was typed
module ca2 (a_i,f_o);
input [2:0] a_i;
output [2:0] f_o;
// What the compiler implicitly declares
wire [2:0] a_i;
wire [2:0] f_o;
Run Code Online (Sandbox Code Playgroud)
要修复它,您可以声明变量或同时声明端口和变量。
module ca2 (a_i,f_o);
input [2:0] a_i;
output [2:0] f_o;
reg [2:0] f_o;
module ca2 (a_i,f_o);
input [2:0] a_i;
output reg [2:0] f_o;
Run Code Online (Sandbox Code Playgroud)