为什么在单个 Verilog 语句(即 ~x + 1'b1)中计算二进制补码会产生错误答案?

Why is computing two's compliment in a single Verilog statement (i.e. ~x + 1'b1) producing the wrong answer?

我意识到在单个 verilog 语句中执行 2 的恭维(如下面的 '' 所示)给出了错误的答案。但我不明白为什么。有人可以帮忙解释一下吗?运算符优先级似乎无法解释问题。

module test();
  logic [7:0] x;
  logic [7:0] x_1s;
  logic [3:0] x_2_0_2s_compl_type1;
  logic [3:0] x_2_0_2s_compl_type2;

  assign x = '0;

  assign x_2_0_2s_compl_type1 = ~x[2:0] + 1'b1; // gives the wrong answer

  assign x_1s = ~x;
  assign x_2_0_2s_compl_type2 = x_1s[2:0] +1'b1; // gives correct answer

  always @* begin
    $display("x = %b",x);
    $display("x_2_0_2s_compl_type1 = %b",x_2_0_2s_compl_type1);
    $display("x_1s = %b",x_1s);
    $display("x_2_0_2s_compl_type2 = %b",x_2_0_2s_compl_type2);
  end

endmodule

模拟结果:

x = 00000000
x_2_0_2s_compl_type1 = 0000
x_1s = 11111111
x_2_0_2s_compl_type2 = 1000

1800-2017 LRM 中的 11.8.2 表达式求值步骤 部分解释了这一点。基本上,上下文确定的表达式中的操作数得到扩展以匹配目标赋值的大小 before 应用任何运算符。所以 ~x[2:0] 变成 ~(4'b000)

你可以写一个表达式来做你想做的事情

assign x_2_0_2s_compl_type1 = {~x}[2:0] + 1'b1;

assign x_2_0_2s_compl_type1 = 3'(~x) + 1'b1;