如何在凿子中获取 UInt() 的大小?

Fab*_*enM 3 hdl chisel

也许这很容易,但我不能简单地找到如何在 Chisel 中获取 UInt() 值的位大小?

我知道如何通过声明设置大小:

val a = UInt(INPUT, 16)
Run Code Online (Sandbox Code Playgroud)

但是要获得“a”大小,是否有类似的属性:

val size = a.?
Run Code Online (Sandbox Code Playgroud)

或者 :

val size = width(a)
Run Code Online (Sandbox Code Playgroud)

Chi*_*ley 6

几件事。首先,看起来您正在使用 Chisel 2 语义。您可能应该使用 Chisel 3 语义,这意味着您应该编写

val a = Input(UInt(16.W))
Run Code Online (Sandbox Code Playgroud)

快速回答是您可以获得如下宽度:

val theWidth = if(io.in0.widthKnown) io.in0.getWidth else -1
Run Code Online (Sandbox Code Playgroud)

或使用匹配

val theWidth = io.in0.widthOption match {
  case Some(w) => w
  case None => -1 // you decide what you want the unknown case to be.
}
Run Code Online (Sandbox Code Playgroud)

您现在拥有Scala变量theWidth中的宽度值,它是一个 Int,必须使用 if 或 match,因为原则上宽度可能未定义。

更长的答案是你应该小心想要这样做。theWidth在电路生成时进行评估,如果正在使用宽度推断(如果您正在查询 chisel 类型的宽度,通常是这种情况)您将无法看到它,因为宽度推断是在电路详述之后完成的它由Firrtl编译器处理。

您可能应该将您想要知道的宽度设置为电路的参数,并使用它而不是 widthOption。就像是。

class X(ioWidth: Int) extends Module {
  val io = IO( new Bundle {
    val in0 = Input(UInt(ioWidth.W))
    ...
  })

  val reg = Reg(UInt((ioWidth * 2).W)) // using width parameter here.
  ...
}
Run Code Online (Sandbox Code Playgroud)