【问题标题】:How to use arithmetic shift & selector in verilog?如何在verilog中使用算术移位和选择器?
【发布时间】:2015-01-22 15:51:57
【问题描述】:

我想同时使用选择器和算术移位。 但是这段代码执行失败,结果只是逻辑移位。

module multiplier(x1, x2, x1x2);
input [15:0] x1, x2;
output [15:0] x1x2;
assign x1x2 = 
x2[13]? ($signed(x1)>>>4'd1) : 16'b0000000000000000;
endmodule

在没有选择器的情况下,算术移位成功完成。

module multiplier(x1, x2, x1x2);
input [15:0] x1, x2;
output [15:0] x1x2;
assign x1x2 = $signed(x1)>>>4'd1;
endmodule

如何同时使用选择器和算术移位?

【问题讨论】:

  • 可能与文字 16'b0 是无符号的事实有关;如果你把它变成一个有符号的文字,它会起作用吗? (16'sb0)

标签: math verilog bit-shift shift


【解决方案1】:

Verilog 在有选择时几乎总是会选择无符号,而且选择器逻辑似乎允许 Verilog 进行选择。

如果不同,有几个解决方案:

  • 使用两行:
    wire [15:0] x1_shift = $signed(x1)>>>4'd1;
    assign x1x2 = x2[13] ? x1_shift : 16'b0;
  • 使用 Curly 代替括号:
    assign x1x2 = x2[13]? <b>{</b> $signed(x1)&gt;&gt;&gt;4'd1 <b>}</b> : 16'b0;
  • 硬编码移位:

    assign x1x2 = x2[13] ? {x1[15],x1[15:1]} : 16'b0;
  • 签署所有条件:(正如 Unn 指出的 16'b0 未签名)

    assign x1x2 = x2[13] ? ($signed(x1)&gt;&gt;&gt;4'd1) : $signed(16'b0); // least recommenced
  • 使用 SystemVerilog,您还可以进行大小转换:

    assign x1x2 = x2[13] ? 16'($signed(x1)&gt;&gt;&gt;4'd1) : 16'b0; // SV only, not Verilog

工作示例here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多