凿码变换

标签 chisel

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

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

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

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

非常感谢。

最佳答案

从根本上说,编写 Chisel 就是编写 Scala 程序来生成电路。你所描述的听起来有点像高级合成,它与凿子有很大的不同。 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/

相关文章:

scala - Rebased 并面临 Scala 依赖问题

chisel - 在 Chisel3 中公开仅模拟行为

chisel - 如何将Vec的各个元素相加?

chisel - 如何表达Vec的指定索引范围?

chisel - 如何在chisel中自由给vec类型变量赋值?

scala - 引用未完全初始化

hdl - 如何在Chisel中正确定义输出Reg

chisel - 当我们应该在 chisel3 中使用 ":="而不是 "="时,同样的情况是 "when"和 "if"

scala - 在for循环chisel3中使用yield时如何访问前一个元素

chisel - 简单的凿子双端口内存读取端口问题