我正在使用vivado v2016.4行为模拟来模拟下面显示的verilog代码。测试夹具代码也显示在主verilog代码下面。
控制台输出显示:
A = -13
B = 4
C = 16332
此外,如果我遍历寄存器变量,我会看到以下内容:
ff3 for A
004 for B
003fcc for C
如果我使用,乘法会得到正确的结果(-52或ffffcc(
C[23:0] = $signed(A[11:0])*signed(B[11:0]);
或
C[23:0] = $signed(A[11:0]*B[11:0]);
为什么我需要使用$signed来获得正确的结果?
Stephen
主要Verilog代码:
module test1(
input CLK,
input RST_AL,
input signed [11:0]A,
input signed [11:0]B
);
reg signed [10:0]z;
reg signed [4:0]x;
reg signed [4:0]y;
reg signed [23:0]C;
initial x = 0;
always @(posedge CLK, negedge RST_AL) begin
if(RST_AL == 0) begin
x[4:0] <= 0;
y[4:0] <= 0;
z[10:0] <= 0;
C[23:0] <= 0;
end else begin
C[23:0] = A[11:0]*B[11:0];
$display("A = %d",A);
$display("B = %d",B);
$display("C = %d",C);
end
end
endmodule
测试夹具代码:
module test1_testfix;
reg RST_AL;
reg CLK;
reg signed [11:0]A;
reg signed [11:0]B;
test1 uut (
CLK,
RST_AL,
A,
B
);
initial begin
// Initialize Inputs
RST_AL = 0;
CLK = 0;
A = -13;
B = 4;
#100
RST_AL = 1;
#100000000
RST_AL = 1;
end
always
#5 CLK = ! CLK;
endmodule
这是因为信号的部分选择总是无符号的。即使在选择整个范围时也是如此。因此,最好不要使用零件选择。你本可以写:
C = A *B;