Skip to content
This repository has been archived by the owner on Aug 19, 2024. It is now read-only.

Bugfix: loadMemoryFromFile support absolute paths #693

Merged
merged 3 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/scala/treadle2/executable/Memory.scala
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ class MemoryInitializer(engine: ExecutionEngine) extends LazyLogging {
private object MemoryFileParser {
def parse(filename: String, base: Int): Seq[BigInt] = {
require(base == 16 || base == 2)
val fullName = os.pwd / os.RelPath(filename)
val fullName = os.Path(filename, base=os.pwd)
os.read.lines(fullName).flatMap(l => parseLine(l.trim, base))
}
private def parseLine(line: String, base: Int): Seq[BigInt] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import chiseltest.simulator.RequiresVerilator
import firrtl2.options.TargetDirAnnotation
import org.scalatest.freespec.AnyFreeSpec

class UsesMem(memoryDepth: Int, memoryType: Bits) extends Module {
class UsesMem(memoryDepth: Int, memoryType: Bits, fileName: String) extends Module {
val io = IO(new Bundle {
val address = Input(UInt(memoryType.getWidth.W))
val value = Output(memoryType)
Expand All @@ -19,7 +19,7 @@ class UsesMem(memoryDepth: Int, memoryType: Bits) extends Module {

val memory = Mem(memoryDepth, memoryType)

loadMemoryFromFileInline(memory, "src/test/resources/iotesters/mem1.txt")
loadMemoryFromFileInline(memory, fileName)

io.value := memory(io.address)

Expand All @@ -28,6 +28,9 @@ class UsesMem(memoryDepth: Int, memoryType: Bits) extends Module {
low.io.address := io.address
io.value2 := low.io.value
}
object UsesMem {
val MEM1 = "src/test/resources/iotesters/mem1.txt"
nbfalcon marked this conversation as resolved.
Show resolved Hide resolved
}

class UsesMemLow(memoryDepth: Int, memoryType: Data) extends Module {
val io = IO(new Bundle {
Expand Down Expand Up @@ -56,8 +59,21 @@ class LoadMemoryFromFileSpec extends AnyFreeSpec with ChiselScalatestTester {
"Users can specify a source file to load memory from" taggedAs RequiresVerilator in {

val targetDir = TargetDirAnnotation("test_run_dir/load_mem_test")
test(new UsesMem(memoryDepth = 8, memoryType = UInt(16.W)))
test(new UsesMem(memoryDepth = 8, memoryType = UInt(16.W), "src/test/resources/iotesters/mem1.txt"))
.withAnnotations(Seq(VerilatorBackendAnnotation, targetDir))
.runPeekPoke(new LoadMemoryFromFileTester(_))
}

"Treadle supports loadFromFileInline using absolute paths" in {
// An absolute path
val path: os.Path = os.pwd / os.RelPath(UsesMem.MEM1)
test(new UsesMem(memoryDepth = 8, memoryType = UInt(16.W), path.toString()))
.runPeekPoke(new LoadMemoryFromFileTester(_))
}

"Treadle also supports loadFromFileInline using relative paths" in {
// An absolute path
test(new UsesMem(memoryDepth = 8, memoryType = UInt(16.W), UsesMem.MEM1))
.runPeekPoke(new LoadMemoryFromFileTester(_))
}
}
Loading