Chisel:Verilog 为 Sint 和 UInt 生成的代码

Chisel: Verilog generated code for Sint and UInt

当使用 SInt 和 UInt 实现加法器时,我得到了相同的 Verilog 代码,请参见下面的代码,

import Chisel._

class Unsigned_Adder extends Module{ 
  val io = new Bundle{
    val a =      UInt(INPUT, 16)
    val b =      UInt(INPUT, 16)
    val out =    UInt(OUTPUT)
  }
  io.out := io.a + io.b
}

 import Chisel._

class Signed_Adder extends Module{ 
  val io = new Bundle{
    val a =      SInt(INPUT, 16)
    val b =      SInt(INPUT, 16)
    val out =    SInt(OUTPUT)
  }
  io.out := io.a + io.b
}

这将生成相同的 Verilog 代码,

module Signed_Adder(
    input [15:0] io_a,
    input [15:0] io_b,
    output[15:0] io_out
);

  wire[15:0] T0;


  assign io_out = T0;
  assign T0 = io_a + io_b;
endmodule

当然,模块名称会有所不同。使用乘法运算符 (*)

在 chisel 中实现乘法器时
io.out := io.a * io.b  

我将为 UInt 和 SInt 获取不同的 Verilog 代码,其中 SInt 中的代码将类似于

module Multi(
    input [15:0] io_a,
    input [15:0] io_b,
    output[31:0] io_out
);

  wire[31:0] T0;


  assign io_out = T0;
  assign T0 = $signed(io_a) * $signed(io_b);
endmodule

$signed 添加到代码中。这是为什么?为什么在加法的情况下我得到相同的 Verilog 代码,但在乘法的情况下我得到为 UInt 和 SInt 生成的不同代码?

加法如果变量大小相等,加法器不关心符号,由于溢出位,加法将正确。 但是有了乘法,我们必须知道符号才能驾驭它。

有关 verilog 中有符号算术的更多信息,请参阅此文档: enter link description here

如果你使用+&那么Addr.io.out的宽度将是17位

而在verilog中,这个Addr不会像FabienM所说的那样关心符号,因为它会被上层设计处理,就像unsigned to signed transform然后连接到这个Addr,它会被转换为2 - 补充形式。