Verilog中的表达符号
问题描述:
这是一小段Verilog代码。我希望它会返回三个相同的结果,所有8位表示为-1。Verilog中的表达符号
module trivial;
reg we;
reg [7:0] c;
initial
begin
c = 8'd3;
we = 1'b1;
$display ("res(we) = %d", (we ? (-$signed(c))/8'sd2 : 8'd0));
$display ("res(1) = %d", (1'b1 ? (-$signed(c))/8'sd2 : 8'd0));
$display ("res = %d", (-$signed(c))/8'sd2);
end
endmodule
简言之,我有标准(1364-2001)的版本在4.1.5节说,除法轮朝向零,所以-3/2 = -1。它还在第4.5节中说,操作符仅取决于操作数(编辑:但仅限于“自定义表达式”;事实证明有必要读取关于符号的部分以及宽度为的部分)。因此,具有划分的子表达式应该不受其所使用的上下文的影响,对于涉及$ signed的子表达式也是如此。所以结果应该都一样吗?
三个不同的模拟器不同意我。只有两个人彼此认同。明显的原因是使用未签名的部门而不是我期望的签名部门。 (-3 = 253和253/2 = 126.5)
有人可以告诉我,如果任何模拟器是正确的,为什么? (见下文)我显然一定会错过一些东西,但请问?非常感谢。 编辑:见上面我失踪的内容。我现在认为在Icarus中存在一个错误,另外两个模拟器是正确的
注意:三元选择中未使用的值似乎没有任何区别,无论是有符号还是无符号。 编辑:这是不正确,也许我忘了试之前修改测试保存有符号数的ModelSim的
Altera版:
$ vsim work.trivial -do 'run -all'
Reading C:/altera/12.1/modelsim_ase/tcl/vsim/pref.tcl
# 10.1b
# vsim -do {run -all} work.trivial
# Loading work.trivial
# run -all
# res(we) = 126
# res(1) = 126
# res = -1
GPL Cver
GPLCVER_2.12a of 05/16/07 (Cygwin32).
Copyright (c) 1991-2007 Pragmatic C Software Corp.
All Rights reserved. Licensed under the GNU General Public License (GPL).
See the 'COPYING' file for details. NO WARRANTY provided.
Today is Mon Jan 21 18:49:05 2013.
Compiling source file "trivial.v"
Highest level modules:
trivial
res(we) = 126
res(1) = 126
res = -1
伊卡洛斯的Verilog 0.9。 6
$ iverilog.exe trivial.v && vvp a.out
res(we) = 126
res(1) = -1
res = -1
答
NCSIM给出:
res(we) = 126
res(1) = 126
res = -1
但是,如果所有输入多路复用器被签名,我得到:
$display ("res(we) = %d", (we ? (-$signed(c))/8'sd2 : 8'sd0)); //last argument now signed
$display ("res(1) = %d", (1'b1 ? (-$signed(c))/8'sd2 : 8'sd0));
$display ("res = %d", (-$signed(c))/8'sd2);
res(we) = -1
res(1) = -1
res = -1
记住,如果我们做一个无符号数算术作为无符号做任何运算,利用位选择时,同样的情况:
reg signed [7:0] c;
c = c[7:0] + 7'sd1; //<-- this is unsigned
在多路复用器是一个单行表达式的一部分的示例中,我相信这在逻辑上被压平进行优化,因此,有符号/无符号的所有参数被取入考虑。
非常感谢您的答复。 – user1999655