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

Commit

Permalink
Bugfix: loadMemoryFromFile support absolute paths (#693)
Browse files Browse the repository at this point in the history
Previously, specifying an absolute path in loadMemoryFromFileInline
would cause an error when running with treadle, because the path was
absolutized using os.path.RelPath. The latter function throws an
exception for absolute paths.

Now both absolute and relative paths work (matching the behaviour of
Verilator).

Note that this has no security implications, since the previous
"relative-path" based implementation also allowed "../../" paths;
existing relative paths should not be affected.
  • Loading branch information
nbfalcon authored Nov 16, 2023
1 parent 1caca88 commit 5b9c0b1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
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 @@ -3,14 +3,14 @@
package chiseltest.iotesters.examples

import chisel3._
import chiseltest.iotesters._
import chisel3.util.experimental.loadMemoryFromFileInline
import chiseltest._
import chiseltest.iotesters._
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"
}

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

0 comments on commit 5b9c0b1

Please sign in to comment.