verilog“~”运算符加法运算给出了不需要的结果

verilog "~" operator in addition operation gives unwanted result

在以下简化的 Verilog 代码中:

wire [31:0] depth;
wire mode_u2 = 1'h0;

assign depth = 'h80 + (~mode_u2);

如果我做深度显示,用VCS模拟(2014.12-1)

$display("depth is 0x%2x", depth);

我得到 0x7f,而不是预期的 0x81。 ~mode_u2 似乎被视为负 1。

如果我把~mode_u2改成!mode_u2。我得到了预期的 0x81。

更有趣的是,如果我做 wire mode = ~mode_u2 然后 assign depth = 'h80 + (~mode) 而不是 0x80,我得到 0x7e

我是不是漏掉了什么?

有人可以解释为什么 ~+ 操作中会这样吗?或者这是其中一种模拟和综合是不同的情况?

非常感谢!!

威利

在加法完成之前,加法运算符的操作数需要扩展到左侧的大小(或两个操作数的最大宽度,具体取决于上下文)。

在这种情况下mode_u2需要扩展到32位。我无法为此找到参考,但看起来位扩展优先于 ~ 运算符。这意味着:

depth = 'h80 + (~mode_u2) = 
         32'h0000_0080 + (~32h0000_0000) = 
         32'h0000_0080 + 32'hffff_ffff = 
         32'h0000_007f

! 运算符的结果,但是根据定义是一个位,我的猜测是位扩展发生了两次:

depth = 'h80 + (!mode_u2) = 
         32'h0000_0080 + (!32'h0000_0000) = 
         32'h0000_0080 + 1'h1 = 
         32'h0000_0080 + 32'h0000_0001 = 
         32'h0000_0081

mode类似:

depth = 'h80 + (~mode) = 
         32'h0000_0080 + (~32'h0000_0001) = 
         32'h0000_0080 + 32'hffff_fffe = 
         32'h0000_007e