From 88265eec586046e6ec96b4615e5516be0f3d9e2c Mon Sep 17 00:00:00 2001 From: HappyQuark Date: Thu, 10 Sep 2020 04:18:08 +1000 Subject: Fix load memory from file to work with binary (#1583) * fix loadMemoryFromFile to work with binary Passed in hexOrBinary parameter to ChiselLoadMemoryAnnotation * Added test for binary format support in loadMemoryFromFile * Added test for binary format support in loadMemoryFromFile--- .../util/experimental/LoadMemoryTransform.scala | 2 +- .../scala/chiselTests/LoadMemoryFromFileSpec.scala | 29 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main/scala/chisel3/util/experimental/LoadMemoryTransform.scala b/src/main/scala/chisel3/util/experimental/LoadMemoryTransform.scala index 92bfcde7..4beafa19 100644 --- a/src/main/scala/chisel3/util/experimental/LoadMemoryTransform.scala +++ b/src/main/scala/chisel3/util/experimental/LoadMemoryTransform.scala @@ -112,7 +112,7 @@ object loadMemoryFromFile { fileName: String, hexOrBinary: MemoryLoadFileType.FileType = MemoryLoadFileType.Hex ): Unit = { - annotate(ChiselLoadMemoryAnnotation(memory, fileName)) + annotate(ChiselLoadMemoryAnnotation(memory, fileName, hexOrBinary)) } } diff --git a/src/test/scala/chiselTests/LoadMemoryFromFileSpec.scala b/src/test/scala/chiselTests/LoadMemoryFromFileSpec.scala index 529d90af..d151f24e 100644 --- a/src/test/scala/chiselTests/LoadMemoryFromFileSpec.scala +++ b/src/test/scala/chiselTests/LoadMemoryFromFileSpec.scala @@ -106,6 +106,19 @@ class HasComplexMemory(memoryDepth: Int) extends Module { io.value := memory(io.address) } +class HasBinarySupport(memoryDepth: Int, memoryType: Data) extends Module { + val io = IO(new Bundle { + val address = Input(UInt(memoryType.getWidth.W)) + val value = Output(memoryType) + }) + + val memory = Mem(memoryDepth, memoryType) + + loadMemoryFromFile(memory, "./mem", MemoryLoadFileType.Binary) + + io.value := memory(io.address) +} + /** * The following tests are a bit incomplete and check that the output verilog is properly constructed @@ -176,4 +189,20 @@ class LoadMemoryFromFileSpec extends AnyFreeSpec with Matchers { } + "Has binary format support" in { + val testDirName = "test_run_dir/binary_memory_load" + + val result = (new ChiselStage).execute( + args = Array("-X", "verilog", "--target-dir", testDirName), + annotations = Seq(ChiselGeneratorAnnotation(() => new HasBinarySupport(memoryDepth = 8, memoryType = UInt(16.W)))) + ) + + val dir = new File(testDirName) + val file = new File(dir, s"HasBinarySupport.HasBinarySupport.memory.v") + file.exists() should be (true) + val fileText = io.Source.fromFile(file).getLines().mkString("\n") + fileText should include (s"""$$readmemb("./mem", HasBinarySupport.memory);""") + file.delete() + } + } -- cgit v1.2.3