简体   繁体   English

如何在 Chisel3 中初始化 Mem 的数据

[英]How to initialize the data of a Mem in Chisel3

有没有什么简单的方法可以从文件中初始化 Mem 的数据(类似于 Verilog 中的“readmemh”)?

There is now a mechanism in the chisel stack to annotate memories to use readmemh or readmemb in simulation.现在chisel 堆栈中有一种机制可以注释内存以在模拟中使用readmemh 或readmemb。 Check out the documentation in the Chisel Wiki查看Chisel Wiki 中的文档

Also, see here in Chisel Testers for concrete usage examples另外,请参见Chisel Testers中的具体用法示例

There is no way built in to Chisel to do this. Chisel 没有内置的方法来做到这一点。 One of the guiding philosophies of Chisel is that what you simulate is what you build into an ASIC. Chisel 的指导理念之一是,您模拟的是您在 ASIC 中构建的内容。 Since there is no way in an ASIC to initialize an SRAM, there's no way to initialize memories in Chisel.由于 ASIC 无法初始化 SRAM,因此无法在 Chisel 中初始化存储器。

You can work around this by using a Reg of Vec (which can be initialized).您可以使用 Vec 的 Reg(可以初始化)来解决此问题。 If the target design is an ASIC, you could create a library that uses a state machine to initialize a given Mem after reset.如果目标设计是 ASIC,您可以创建一个库,该库使用状态机在复位后初始化给定的 Mem。 If the target design is an FPGA, you could use BlackBox and just write Verilog for the Mem.如果目标设计是 FPGA,您可以使用 BlackBox 并为 Mem 编写 Verilog。

Attending the ASPIRE retreat this week, I heard that there is a way to code parameterized black boxes in Chisel that can have both:本周参加ASPIRE务虚会,听说有一种方法可以在Chisel中编写参数化黑匣子,可以兼得:
1) Verilog code attached to it (so you could use $readmemh to initialize the ROM), and 1) 附带的 Verilog 代码(因此您可以使用 $readmemh 来初始化 ROM),以及
2) Scala code attached to it (so that the firrtl-interpreter can simulate the ROM). 2) 附加的 Scala 代码(以便 firrtl-interpreter 可以模拟 ROM)。
I don't know how to do either of these yet.我还不知道如何做到这两点。 Perhaps Jack or others can elaborate.也许杰克或其他人可以详细说明。

The following function is an approximation to Verilog's readmemh:以下函数是 Verilog 的 readmemh 的近似值:

object Tools {

  def readmemh(path: String): Array[BigInt] = {
    val buffer = new ArrayBuffer[BigInt]
    for (line <- Source.fromFile(path).getLines) {
      val tokens: Array[String] = line.split("(//)").map(_.trim)
      if (tokens.length > 0 && tokens(0) != "") {
        val i = Integer.parseInt(tokens(0), 16)
        buffer.append(i)
      }
    }
    buffer.toArray
  }
}

And then in SpinalHDL然后在 SpinalHDL 中

  rom.initialContent = Tools.readmemh(romfile)

or Chisel3:或凿子3:

  VecInit(Tools.readmemh(romfile))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM