我花了相当长的时间调试一些 Verilog 代码,才意识到设计在整个过程中都是正确的,并且无论出于何种原因,Verilog 的
display
函数都有意想不到的行为。
我有以下测试台文件:
`timescale 1ns / 1ps
module tb ();
logic signed [7:0] Y;
logic signed [15:0] X;
logic signed [23:0] Z;
logic clk;
reg [8:0] counter;
// instantiate device under test
CSAM dut (Z,X,Y);
// 2 ns clock
initial
begin
clk = 1'b1;
assign counter = 8'b0;
forever #10 clk = ~clk;
end
initial
begin
//while (1)
//begin
#10 X = 16'b0000000001111011;
#0 Y = 8'b11110011;
#20 X = 16'b0000000001111011;
#0 Y = 8'b11110011;
$display ("Test data:\ninput A is 0b'%16b or 0h'%4h or %d\ninput B is 0b'%8b or 0h'%2h or %d\noutput is 0b'%24b or 0h'%6h or %d\n", X,X,X,Y,Y,Y,Z,Z,Z);
#20 X = 16'b0000000000001111;
#0 Y = 8'b00000000;
$display ("Test data:\ninput A is 0b'%16b or 0h'%4h or %d\ninput B is 0b'%8b or 0h'%2h or %d\noutput is 0b'%24b or 0h'%6h or %d\n", X,X,X,Y,Y,Y,Z,Z,Z);
#20 X = 16'b0000000000001111;
#0 Y = 8'b11111111;
$display ("Test data:\ninput A is 0b'%16b or 0h'%4h or %d\ninput B is 0b'%8b or 0h'%2h or %d\noutput is 0b'%24b or 0h'%6h or %d\n", X,X,X,Y,Y,Y,Z,Z,Z);
#20 X = 16'b11111111111111;
#0 Y = 8'b11111111;
$display ("Test data:\ninput A is 0b'%16b or 0h'%4h or %d\ninput B is 0b'%8b or 0h'%2h or %d\noutput is 0b'%24b or 0h'%6h or %d\n", X,X,X,Y,Y,Y,Z,Z,Z);
#20 X = 16'b1010101010101010;
#0 Y = 8'b01010101;
$display ("Test data:\ninput A is 0b'%16b or 0h'%4h or %d\ninput B is 0b'%8b or 0h'%2h or %d\noutput is 0b'%24b or 0h'%6h or %d\n", X,X,X,Y,Y,Y,Z,Z,Z);
#20 X = 16'b0101010101010101;
#0 Y = 8'b00101010;
$display ("Test data:\ninput A is 0b'%16b or 0h'%4h or %d\ninput B is 0b'%8b or 0h'%2h or %d\noutput is 0b'%24b or 0h'%6h or %d\n", X,X,X,Y,Y,Y,Z,Z,Z);
end
endmodule
你可以看到我正在运行一些输入 X、Y 和 Z 的测试是输出。
我的显示函数以十进制、十六进制和二进制形式打印 X、Y、Z 以提高可读性。
$display ("Test data:\ninput A is 0b'%16b or 0h'%4h or %d\ninput B is 0b'%8b or 0h'%2h or %d\noutput is 0b'%24b or 0h'%6h or %d\n", X,X,X,Y,Y,Y,Z,Z,Z);
Modelsim 终端中的输出如下所示:
# Test data:
# input A is 0b'0000000001111011 or 0h'007b or 123
# input B is 0b'11110011 or 0h'f3 or -13
# output is 0b'111111111111100111000001 or 0h'fff9c1 or -1599
#
# Test data:
# input A is 0b'0000000000001111 or 0h'000f or 15
# input B is 0b'00000000 or 0h'00 or 0
# output is 0b'111111111111111100111101 or 0h'ffff3d or -195
#
# Test data:
# input A is 0b'0000000000001111 or 0h'000f or 15
# input B is 0b'11111111 or 0h'ff or -1
# output is 0b'000000000000000000000000 or 0h'000000 or 0
您可以看到对于 15x0 和 15x-1,结果实际上是不正确的。但是,如果我在 Modelsim 的波形查看器中手动检查 Z 波形,我可以看到答案实际上是正确的。所以不知何故,显示功能没有按预期运行,我想知道我能做些什么来解决这个问题。对我来说特别奇怪的是,输入 X、Y 在所有测试中都正确显示,但 Z 却没有。