\nI use chisel6.2.0. Here is the structure of my project :
\nexample\n└── src\n│ └── hex\n│ │ └── example.hex.txt\n│ └── main/scala/circuit\n│ │ └── IMemory.scala\n│ └── test/scala/circuit\n│ └── IMemorySpec.scala\n└--build.sbt\n
\nbuild.sbt
\nThisBuild / scalaVersion := "2.13.12"\nThisBuild / version := "0.1.0"\nThisBuild / organization := "%ORGANIZATION%"\n\nval chiselVersion = "6.2.0"\n\nlazy val root = (project in file("."))\n .settings(\n name := "%NAME%",\n libraryDependencies ++= Seq(\n "org.chipsalliance" %% "chisel" % chiselVersion,\n "org.scalatest" %% "scalatest" % "3.2.16" % "test"\n ),\n scalacOptions ++= Seq(\n "-language:reflectiveCalls",\n "-deprecation",\n "-feature",\n "-Xcheckinit",\n "-Ymacro-annotations"\n ),\n addCompilerPlugin(\n "org.chipsalliance" % "chisel-plugin" % chiselVersion cross CrossVersion.full\n )\n )\n
\nexample.hex.txt
\n003100b3\n40628033\n06432283\n06512423\n064000ef\n
\nIMemory.scala
\npackage circuit\n\nimport chisel3._\nimport chisel3.util.experimental.loadMemoryFromFileInline\nimport circt.stage.ChiselStage\n\nclass IMemoryIO extends Bundle {\n val rdAddr = Input(UInt(5.W))\n val rdData = Output(UInt(32.W))\n val wrAddr = Input(UInt(5.W))\n val wrData = Input(UInt(32.W))\n val wrEna = Input(UInt(1.W))\n}\n\nclass IMemory(memoryFile: String = "") extends Module {\n val io = IO(new IMemoryIO)\n val mem = SyncReadMem(1024, UInt(32.W))\n io.rdData := mem.read(io.rdAddr)\n io.rdData := mem(io.rdAddr)\n when(io.wrEna === 1.U) {\n mem.write(io.wrAddr, io.wrData)\n }\n if (memoryFile.trim().nonEmpty) {\n loadMemoryFromFileInline(mem, memoryFile)\n }\n}\n
\nIMemorySpec.scala
\npackage circuit\nimport chisel3._\nimport chisel3.experimental.BundleLiterals._\nimport chisel3.simulator.EphemeralSimulator._\nimport org.scalatest.freespec.AnyFreeSpec\nimport org.scalatest.matchers.must.Matchers\nimport java.io.File\nclass IMemorySpec extends AnyFreeSpec with Matchers {\n "write memory" in {\n simulate(new IMemory("src/hex/example.hex.txt")) { dut =>\n dut.io.wrAddr.poke(4.U)\n dut.io.wrEna.poke(1.U)\n dut.io.wrData.poke(0x003100b3.U)\n dut.clock.step()\n dut.io.rdAddr.poke(4.U)\n dut.clock.step()\n dut.io.rdData.expect(0x003100b3.U)\n }\n }\n "loadMemory from file" in {\n simulate(new IMemory("src/hex/example.hex.txt")) { dut =>\n dut.io.rdAddr.poke(0.U)\n dut.clock.step()\n dut.io.rdData.expect(0x003100b3.U)\n }\n }\n}\n
\nwhen I run sbt "testOnly circuit.IMemorySpec
, the output is :
[info] - file should exist\n[info] - write memory\n[info] - loadMemory from file *** FAILED ***\n[info] chisel3.simulator.PeekPokeAPI$FailedExpectationException: Failed Expectation: Observed value '0' != 3211443. Expectation failed: observed value 0 != 3211443\n
\nObserved value '0' != 3211443.
. I can't read the data I expect.
I can sure it is not a path mistake. because when I'm using chisel5.1.0 and chiseltest, it works。\nHere is my build.sbt.
\n// See README.md for license details.\n\nThisBuild / scalaVersion := "2.13.12"\nThisBuild / version := "0.1.0"\nThisBuild / organization := "%ORGANIZATION%"\n\nval chiselVersion = "5.1.0"\n\nlazy val root = (project in file("."))\n .settings(\n name := "%NAME%",\n libraryDependencies ++= Seq(\n "org.chipsalliance" %% "chisel" % chiselVersion,\n "edu.berkeley.cs" %% "chiseltest" % "5.0.2" % "test"\n ),\n scalacOptions ++= Seq(\n "-language:reflectiveCalls",\n "-deprecation",\n "-feature",\n "-Xcheckinit",\n "-Ymacro-annotations"\n ),\n addCompilerPlugin(\n "org.chipsalliance" % "chisel-plugin" % chiselVersion cross CrossVersion.full\n )\n )\n
\nand my test
\npackage circuit\n\nimport chisel3._\nimport chiseltest._\n\nimport org.scalatest.freespec.AnyFreeSpec\nimport chisel3.experimental.BundleLiterals._\n\nclass IMemorySpec extends AnyFreeSpec with ChiselScalatestTester {\n "memory initial should work by example.hex.txt" in {\n test(new IMemory("src/hex/example.hex.txt")) { dut =>\n dut.io.rdAddr.poke(0.U)\n dut.clock.step()\n dut.io.rdData.expect(0x003100b3.U)\n dut.io.rdAddr.poke(1.U)\n dut.clock.step()\n dut.io.rdData.expect(0x40628033.U)\n dut.io.rdAddr.poke(2.U)\n dut.clock.step()\n dut.io.rdData.expect(0x06432283.U)\n dut.io.rdAddr.poke(3.U)\n dut.clock.step()\n dut.io.rdData.expect(0x06512423.U)\n }\n }\n}\n
\nit works as I expect.
\n","author":{"@type":"Person","name":"Fang Yongrui"},"upvoteCount":1,"answerCount":0,"acceptedAnswer":null}}Reputation: 11
I have the same problem as this one: in-chisel-3-how-to-initialize-memory-test-code-with-text-file, my test code indicates that loadMemoryFromFile makes no sense. I think my filepath is correct, but it just not work.
I use chisel6.2.0. Here is the structure of my project :
example
└── src
│ └── hex
│ │ └── example.hex.txt
│ └── main/scala/circuit
│ │ └── IMemory.scala
│ └── test/scala/circuit
│ └── IMemorySpec.scala
└--build.sbt
build.sbt
ThisBuild / scalaVersion := "2.13.12"
ThisBuild / version := "0.1.0"
ThisBuild / organization := "%ORGANIZATION%"
val chiselVersion = "6.2.0"
lazy val root = (project in file("."))
.settings(
name := "%NAME%",
libraryDependencies ++= Seq(
"org.chipsalliance" %% "chisel" % chiselVersion,
"org.scalatest" %% "scalatest" % "3.2.16" % "test"
),
scalacOptions ++= Seq(
"-language:reflectiveCalls",
"-deprecation",
"-feature",
"-Xcheckinit",
"-Ymacro-annotations"
),
addCompilerPlugin(
"org.chipsalliance" % "chisel-plugin" % chiselVersion cross CrossVersion.full
)
)
example.hex.txt
003100b3
40628033
06432283
06512423
064000ef
IMemory.scala
package circuit
import chisel3._
import chisel3.util.experimental.loadMemoryFromFileInline
import circt.stage.ChiselStage
class IMemoryIO extends Bundle {
val rdAddr = Input(UInt(5.W))
val rdData = Output(UInt(32.W))
val wrAddr = Input(UInt(5.W))
val wrData = Input(UInt(32.W))
val wrEna = Input(UInt(1.W))
}
class IMemory(memoryFile: String = "") extends Module {
val io = IO(new IMemoryIO)
val mem = SyncReadMem(1024, UInt(32.W))
io.rdData := mem.read(io.rdAddr)
io.rdData := mem(io.rdAddr)
when(io.wrEna === 1.U) {
mem.write(io.wrAddr, io.wrData)
}
if (memoryFile.trim().nonEmpty) {
loadMemoryFromFileInline(mem, memoryFile)
}
}
IMemorySpec.scala
package circuit
import chisel3._
import chisel3.experimental.BundleLiterals._
import chisel3.simulator.EphemeralSimulator._
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.must.Matchers
import java.io.File
class IMemorySpec extends AnyFreeSpec with Matchers {
"write memory" in {
simulate(new IMemory("src/hex/example.hex.txt")) { dut =>
dut.io.wrAddr.poke(4.U)
dut.io.wrEna.poke(1.U)
dut.io.wrData.poke(0x003100b3.U)
dut.clock.step()
dut.io.rdAddr.poke(4.U)
dut.clock.step()
dut.io.rdData.expect(0x003100b3.U)
}
}
"loadMemory from file" in {
simulate(new IMemory("src/hex/example.hex.txt")) { dut =>
dut.io.rdAddr.poke(0.U)
dut.clock.step()
dut.io.rdData.expect(0x003100b3.U)
}
}
}
when I run sbt "testOnly circuit.IMemorySpec
, the output is :
[info] - file should exist
[info] - write memory
[info] - loadMemory from file *** FAILED ***
[info] chisel3.simulator.PeekPokeAPI$FailedExpectationException: Failed Expectation: Observed value '0' != 3211443. Expectation failed: observed value 0 != 3211443
Observed value '0' != 3211443.
. I can't read the data I expect.
I can sure it is not a path mistake. because when I'm using chisel5.1.0 and chiseltest, it works。 Here is my build.sbt.
// See README.md for license details.
ThisBuild / scalaVersion := "2.13.12"
ThisBuild / version := "0.1.0"
ThisBuild / organization := "%ORGANIZATION%"
val chiselVersion = "5.1.0"
lazy val root = (project in file("."))
.settings(
name := "%NAME%",
libraryDependencies ++= Seq(
"org.chipsalliance" %% "chisel" % chiselVersion,
"edu.berkeley.cs" %% "chiseltest" % "5.0.2" % "test"
),
scalacOptions ++= Seq(
"-language:reflectiveCalls",
"-deprecation",
"-feature",
"-Xcheckinit",
"-Ymacro-annotations"
),
addCompilerPlugin(
"org.chipsalliance" % "chisel-plugin" % chiselVersion cross CrossVersion.full
)
)
and my test
package circuit
import chisel3._
import chiseltest._
import org.scalatest.freespec.AnyFreeSpec
import chisel3.experimental.BundleLiterals._
class IMemorySpec extends AnyFreeSpec with ChiselScalatestTester {
"memory initial should work by example.hex.txt" in {
test(new IMemory("src/hex/example.hex.txt")) { dut =>
dut.io.rdAddr.poke(0.U)
dut.clock.step()
dut.io.rdData.expect(0x003100b3.U)
dut.io.rdAddr.poke(1.U)
dut.clock.step()
dut.io.rdData.expect(0x40628033.U)
dut.io.rdAddr.poke(2.U)
dut.clock.step()
dut.io.rdData.expect(0x06432283.U)
dut.io.rdAddr.poke(3.U)
dut.clock.step()
dut.io.rdData.expect(0x06512423.U)
}
}
}
it works as I expect.
Upvotes: 1
Views: 170