因此,我有一个关于Chisel代码转换的理论问题。

我知道Chisel实际上是Scala定义的集合,因此它被编译为Java字节码,而Java字节码又在JVM中运行,就像魔术一样,它为较早版本的Chisel吐出了Verilog等效描述,甚至是C++描述。

关键是我无法弄清楚这种“魔术”是如何工作的。我的猜测是,从Chisel到Verilog/C++的代码转换全部基于Scala反射。但是我不确定,因为找不到与该主题相关的任何内容。

那么,这与反射(reflection)有关吗?如果是这样,它是我们运行时反射(reflect)的编译时间吗?
有人可以给我一个提示吗?

非常感谢。

最佳答案

从根本上讲,编写Chisel就是在编写Scala程序以生成电路。您所描述的内容听起来有点像高级合成,与Chisel完全不同。 Chisel不会将Scala(或Java)原语映射到硬件,而是执行Scala代码来构造hardware AST,然后将其编译为Verilog。

我将通过一个带注释的示例来使这一点更加清楚。

// The body of a Scala class is the default constructor
// MyModule's default constructor has a single Int argument
// Superclass Module is a chisel3 Class that begins construction of a hardware module
// Implicit clock and reset inputs are added by the Module constructor
class MyModule(width: Int) extends Module {
  // io is a required field for subclasses of Module
  // new Bundle creates an instance of an anonymous subclass of Chisel's Bundle (like a struct)
  // When executing the function IO(...), Chisel adds ports to the Module based on the Bundle object
  val io = IO(new Bundle {
    val in = Input(UInt(width.W)) // Input port with width defined by parameter
    val out = Output(UInt()) // Output port with width inferred by Chisel
  })

  // A Scala println that will print at elaboration time each time this Module is instantiated
  // This does NOT create a node in the Module AST
  println(s"Constructing MyModule with width $width")

  // Adds a register declaration node to the Module AST
  // This counter register resets to the value of input port io.in
  // The implicit clock and reset inputs feed into this node
  val counter = RegInit(io.in)

  // Adds an addition node to the hardware AST with operands counter and 1
  val inc = counter + 1.U // + is overloaded, this is actually a Chisel function call

  // Connects the output of the addition node to the "next" value of counter
  counter := inc

  // Adds a printf node to the Module AST that will print at simulation time
  // The value of counter feeds into this node
  printf("counter = %d\n", counter)
}

关于凿子代码转换,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44548198/

10-11 23:54