Else body 正在 System Verilog 中执行

Else body is executing in System Verilog

我正在使用以下代码在 System Verilog 中开发一个模块:

module my_module (input             [7:0] rd_i // rd_i = 00001001
                 ,input             [7:0] rs_i // rs_i = 10010010
                 ,output logic  [7:0] result_o // result_o = 00001010
);

    always_comb
        if ((rd_i << 4) & 8'hF0 == rs_i & 8'hF0) begin
            result_o = 'b0101;
        end
        else begin
            result_o = 'b1010;
        end
endmodule

module my_module_tb();
    logic [7:0] rd_i = 'b00001001;
    logic [7:0] rs_i = 'b10010010;
    logic [7:0] result_o;

    my_module uut (
        .rd_i(rd_i),
        .rs_i(rs_i),
        .result_o(result_o)
    );
endmodule

我似乎应该期望 result_o = 5,但它等于 10。我不明白为什么我的 if 条件评估为假。

问题是运算符优先级。 IEEE Std 1800-2012,第 11.3.2 运算符优先级 部分显示 == 运算符的优先级高于二进制 & 运算符。这意味着您的代码行为如下,添加了括号:

    if ((rd_i << 4) & (8'hF0 == rs_i) & 8'hF0) begin

由于 rs_i 不等于 8'hF0,因此 if 子句为假。

要获得所需的行为,请按如下方式添加括号:

    if ( ((rd_i << 4) & 8'hF0) == (rs_i & 8'hF0) ) begin