表达的标志在Verilog

时间:2013-01-22 09:21:23

标签: verilog

这是一小段Verilog代码。我希望它返回三个相同的结果,所有8位表示为-1。

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)

有人可以告诉我是否有任何模拟器是正确的,为什么? (见下文)我显然必须遗漏一些东西,但请问什么?非常感谢。 编辑:请参阅上面我缺少的内容。我现在认为伊卡洛斯有一个错误,而另外两个模拟器是正确的

注意:三元选择中未使用的值似乎没有任何区别,无论是有符号还是无符号。 编辑:这是不正确的,也许我在重试签名号码之前忘记保存修改后的测试

Altera版的Modelsim:

$ 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

Icarus Verilog 0.9.6

$ iverilog.exe trivial.v && vvp a.out
res(we) = 126
res(1)  =   -1
res =   -1

1 个答案:

答案 0 :(得分:2)

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

在示例中,mux是单行表达式的一部分,我假设它在逻辑上被展平以进行优化,因此考虑了所有参数的有符号/无符号。