Skip to content

Commit

Permalink
Fix load memory from file to work with binary (#1583)
Browse files Browse the repository at this point in the history
* 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

(cherry picked from commit 88265ee)
  • Loading branch information
Quarky93 authored and mergify-bot committed Sep 9, 2020
1 parent b207bc9 commit 793bddd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ object loadMemoryFromFile {
fileName: String,
hexOrBinary: MemoryLoadFileType.FileType = MemoryLoadFileType.Hex
): Unit = {
annotate(ChiselLoadMemoryAnnotation(memory, fileName))
annotate(ChiselLoadMemoryAnnotation(memory, fileName, hexOrBinary))
}
}

Expand Down
29 changes: 29 additions & 0 deletions src/test/scala/chiselTests/LoadMemoryFromFileSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,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
Expand Down Expand Up @@ -185,4 +198,20 @@ class LoadMemoryFromFileSpec extends FreeSpec 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()
}

}

0 comments on commit 793bddd

Please sign in to comment.