为什么在单个Verilog语句中计算两个恭维(即~x + 1'b1)会产生错误的答案?

问题描述 投票:0回答:1

我意识到在单个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
verilog system-verilog hdl
1个回答
3
投票

第11.8.2节评估1800-2017 LRM中表达式的步骤解释了这一点。基本上,在应用任何运算符之前,上下文确定的表达式中的操作数会扩展为与目标赋值的大小相匹配。所以~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;
© www.soinside.com 2019 - 2024. All rights reserved.