我是 SystemVerilog 新手,想编写一个模块来执行具有相应权重的输入的加权和。这是我的模块代码:
module Layer #(parameter num_inps=2)(out, inps, ws, clk, rst);
output reg out;
input real inps[num_inps];
input logic clk, rst;
input real ws[num_inps];
real temp[num_inps];
genvar i;
generate
for(i=0; i<num_inps; i++) begin
assign temp[i] = inps[i] * ws[i];
end
endgenerate
always @( posedge clk, posedge rst) begin : res
if (rst==1) begin
out = 0;
end
else begin
out = 0;
begin
for(int j=0; j<num_inps; j++) begin
out = out + temp[j];
end
end
end
end
endmodule
这是我的测试平台代码
module main_tb();
real out;
real inps[2], ws[2];
logic clk, rst;
Layer l1(out, inps, ws, clk, rst);
initial begin
$dumpfile("ow.vcd");
$dumpvars(0, l1);
inps[0] = 1.0;
inps[1] = 2.0;
ws[0] = 5.0;
ws[1] = 3.0;
rst = 0;
clk = 0;
#10
clk = 1;
#10
clk = 0;
$display(out);
#10
$finish;
end
endmodule
我面临问题,因为当我实例化我的层模块并传递适当的输入时,
out
结果是1.0000而不是11.0000。我是否错误地使用了always块?如果是这样,请帮我纠正这个问题。非常感谢!
我目前正在使用 Icarus verilog 进行模拟,这是我传递给编译的标志:
iverilog -g2012 -o output layer.sv main.sv
vvp output
问题是您在
out
模块中将 reg
声明为 1 位 Layer
。已知值只能是 0 和 1。您在测试台中正确地将其声明为 real
类型。
改变:
output reg out;
至:
output real out;
进行此更改后,它将显示
11
。