简体   繁体   中英

In chisel 3, how to initialize memory test code with text file

I wanted to initialize memory test code in chisel 3.

I referred the code from this website ( https://www.chisel-lang.org/chisel3/docs/appendix/experimental-features#loading-memories )

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 }
  }
}

This code works well when it is compiled to verilog.

So, i thought that it also can emit number in tester code.

    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)
        }
    }
}

However, this test code doesn't work well.

Its output is just zero.

I want to know how to initialize chisel test code with text file.

It's seem to be a path problem. Give the path of your memory content file in tester code when you instantiate module:

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

If your memory.hex is well formated it should solve the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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