凿子代码转换

Raf*_*ael 5 chisel

所以,我有一个关于Chisel代码转换的理论问题.

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

关键是我无法弄清楚这种"神奇"是如何起作用的.我的猜测是从Chisel到Verilog/C++的代码转换都是基于Scala反射.但我不确定,因为我找不到任何与此主题相关的内容.

那么,它是关于反思吗?如果是这样,我们的运行时反射是编译时间吗?有人可以给我一个线索吗?

非常感谢.

Jac*_*nig 6

Fundamentally, writing Chisel is writing a Scala program to generate a circuit. What you're describing sounds a bit like High-Level Synthesis which is quite different from Chisel. Rather than mapping Scala (or Java) primitives to hardware, Chisel executes Scala code to construct a hardware AST that is then compiled to Verilog.

I'll try to make this a little more clear with an annotated example.

// 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) 
}
Run Code Online (Sandbox Code Playgroud)

  • 导出 Firrtl 的是 Scala 描述本身。这不是*真正*的反射;像 `UInt` 和 `:=` 这样的 Chisel 函数调用实际上会改变由 Chisel 在给定模块内构造的 AST。Chisel 从顶层模块开始遍历这个 AST,以发出 Firrtl。我们*确实*实际上使用反射,但仅用于命名连线和寄存器,而不用于构建 AST。 (3认同)
  • 但是 Chisel 会检查自己以生成 FIRRTL 吗?当你编译并运行你给出的例子时,Chisel中的硬件描述如何解释自己并导出firrtl?我只能认为它与反射一起工作。 (2认同)