在凿子3中,如何用文本文件初始化内存测试代码



我想初始化凿子3中的内存测试代码。

我引用了这个网站上的代码(https://www.chisel-lang.org/chisel3/docs/appendix/experimental-features#loading-存储器(

import chisel3._
import chisel3.util.experimental.loadMemoryFromFileInline
class InitMemInline(memoryFile: String = " My text file location ") extends Module {
val width: Int = 32
val io = IO(new Bundle {
val enable = Input(Bool())
val write = Input(Bool())
val addr = Input(UInt(10.W))
val dataIn = Input(UInt(width.W))
val dataOut = Output(UInt(width.W))
})
val mem = SyncReadMem(1024, UInt(width.W))
// Initialize memory
if (memoryFile.trim().nonEmpty) {
loadMemoryFromFileInline(mem, memoryFile)
}
io.dataOut := DontCare
when(io.enable) {
val rdwrPort = mem(io.addr)
when (io.write) { rdwrPort := io.dataIn }
.otherwise    { io.dataOut := rdwrPort }
}
}

此代码在编译到verilog时运行良好。

所以,我认为它也可以在测试程序代码中发出数字。

it should "read memory" in{
test (new InitMemInline) { c=>
c.io.enable.poke(true.B)
c.io.addr.poke(0.U)

c.clock.step(1)
c.io.dataOut.expect(1.U)
c.io.addr.poke(1.U)
c.clock.step(1)
c.io.dataOut.expect(2.U)
}
}
}

但是,这个测试代码不能很好地工作。

它的输出只是零。

我想知道如何用文本文件初始化凿测试代码。

这似乎是一个路径问题。当您实例化模块时,在测试仪代码中给出内存内容文件的路径:

//...
it should "read memory" in{
test (new InitMemInline("/the/path/to/memory.hex")) { c=>
//...

如果你的memory.hex格式正确,它应该能解决问题。

最新更新