From c62b1a4663588c3c288dcc1b3f8f30e2a9205657 Mon Sep 17 00:00:00 2001 From: Felix Yan Date: Mon, 31 Jul 2023 04:28:33 +0300 Subject: [PATCH 1/3] Correct a typo in FailSpec.scala (#659) --- src/test/scala/treadle2/FailSpec.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/scala/treadle2/FailSpec.scala b/src/test/scala/treadle2/FailSpec.scala index 438f355a4..0b86f8bb2 100644 --- a/src/test/scala/treadle2/FailSpec.scala +++ b/src/test/scala/treadle2/FailSpec.scala @@ -9,7 +9,7 @@ import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers class FailSpec extends AnyFlatSpec with Matchers { - behavior.of("explict fail") + behavior.of("explicit fail") it should "fail a test with an explicit failure code" in { val input = From 0ce013bf716d9b0b9b1c5677fc7156db23364799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksandar=20Kondi=C4=87?= <55934296+konda-x1@users.noreply.github.com> Date: Tue, 1 Aug 2023 17:42:19 +0200 Subject: [PATCH 2/3] Replace getDataNames with DataMirror.fullModulePorts (#662) * Replace getDataNames with DataMirror.fullModulePorts Remove getDataNames. Add test with Record extending OpaqueType. * Add license header --- .../internal/BackendExecutive.scala | 11 +---- .../chiseltest/tests/OpaqueTypeTest.scala | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 src/test/scala/chiseltest/tests/OpaqueTypeTest.scala diff --git a/src/main/scala/chiseltest/internal/BackendExecutive.scala b/src/main/scala/chiseltest/internal/BackendExecutive.scala index 6a2937909..7e9e99d36 100644 --- a/src/main/scala/chiseltest/internal/BackendExecutive.scala +++ b/src/main/scala/chiseltest/internal/BackendExecutive.scala @@ -23,7 +23,7 @@ object BackendExecutive { val (highFirrtl, dut) = Compiler.elaborate(dutGen, testersAnnotationSeq, chiselAnnos) // extract port names - val portNames = DataMirror.modulePorts(dut).flatMap { case (name, data) => getDataNames(name, data).toList }.toMap + val portNames = DataMirror.fullModulePorts(dut).map(_.swap).toMap // compile to low firrtl val lowFirrtl = Compiler.toLowFirrtl(highFirrtl) @@ -55,15 +55,6 @@ object BackendExecutive { private def componentToName(component: ReferenceTarget): String = component.name - /** Returns a Seq of (data reference, fully qualified element names) for the input. - * name is the name of data - */ - private def getDataNames(name: String, data: Data): Seq[(Data, String)] = Seq(data -> name) ++ (data match { - case _: Element => Seq() - case b: Record => b.elements.toSeq.flatMap { case (n, e) => getDataNames(s"${name}_$n", e) } - case v: Vec[_] => v.zipWithIndex.flatMap { case (e, i) => getDataNames(s"${name}_$i", e) } - }) - /** This creates some kind of map of combinational paths between inputs and outputs. * * @param dut use this to figure out which paths involve top level iO diff --git a/src/test/scala/chiseltest/tests/OpaqueTypeTest.scala b/src/test/scala/chiseltest/tests/OpaqueTypeTest.scala new file mode 100644 index 000000000..1ab2caadb --- /dev/null +++ b/src/test/scala/chiseltest/tests/OpaqueTypeTest.scala @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: Apache-2.0 + +package chiseltest.tests + +import chisel3.experimental.BundleLiterals.AddBundleLiteralConstructor +import chisel3.experimental.OpaqueType +import chisel3._ +import chiseltest._ +import org.scalatest.flatspec.AnyFlatSpec + +import scala.collection.immutable.SeqMap + +class OpaqueTypeTest extends AnyFlatSpec with ChiselScalatestTester { + + class OpaqueRecord[T <: Data](val data: T) extends Record with OpaqueType { + val elements: SeqMap[String, T] = SeqMap("" -> data) + } + + class OpaquePassthrough[T <: Data](data: T) extends Module { + val in: OpaqueRecord[T] = IO(Input(new OpaqueRecord(data))) + val out: OpaqueRecord[T] = IO(Output(new OpaqueRecord(data))) + out := in + } + + def rec[T <: Data](_val: => T): OpaqueRecord[T] = new OpaqueRecord(_val.cloneType: T).Lit(_.data -> _val) + + def testPokeExpect[T <: Data](_val: => T): TestResult = + test(new OpaquePassthrough(_val.cloneType)) { dut => + dut.in.poke(rec(_val)) + dut.out.expect(rec(_val)) + } + + behavior of "OpaqueType" + + it should "poke and expect successfully" in { + testPokeExpect(4.U(6.W)) + testPokeExpect(-4.S(8.W)) + testPokeExpect(rec(5.U(3.W))) + } + +} From 94f5273ed191ea1a9fd3abf1abdeb46c4bd6dbc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksandar=20Kondi=C4=87?= Date: Mon, 21 Aug 2023 21:05:08 +0200 Subject: [PATCH 3/3] Replace getDataNames with DataMirror.fullModulePorts in PeekPokeTester (#667) --- .../internal/PeekPokeTesterBackend.scala | 9 +-------- .../scala/chiseltest/tests/OpaqueTypeTest.scala | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/main/scala/chiseltest/internal/PeekPokeTesterBackend.scala b/src/main/scala/chiseltest/internal/PeekPokeTesterBackend.scala index 2d97b5fd7..537e60127 100644 --- a/src/main/scala/chiseltest/internal/PeekPokeTesterBackend.scala +++ b/src/main/scala/chiseltest/internal/PeekPokeTesterBackend.scala @@ -28,7 +28,7 @@ object PeekPokeTesterBackend { ): AnnotationSeq = { val (sim, covAnnos, dut) = createTester(dutGen, defaults.addDefaultSimulator(annos), chiselAnnos) // extract port names - val portNames = DataMirror.modulePorts(dut).flatMap { case (name, data) => getDataNames(name, data).toList }.toMap + val portNames = DataMirror.fullModulePorts(dut).map(_.swap).toMap val localCtx = IOTestersContext(sim, portNames) // run tests @@ -48,13 +48,6 @@ object PeekPokeTesterBackend { finish(sim, covAnnos) } - /** Returns a Seq of (data reference, fully qualified element names) for the input. name is the name of data. */ - private def getDataNames(name: String, data: Data): Seq[(Data, String)] = Seq(data -> name) ++ (data match { - case _: Element => Seq() - case b: Record => b.elements.toSeq.flatMap { case (n, e) => getDataNames(s"${name}_$n", e) } - case v: Vec[_] => v.zipWithIndex.flatMap { case (e, i) => getDataNames(s"${name}_$i", e) } - }) - } private[chiseltest] case class IOTestersContext(backend: SimulatorContext, dataNames: Map[Data, String]) diff --git a/src/test/scala/chiseltest/tests/OpaqueTypeTest.scala b/src/test/scala/chiseltest/tests/OpaqueTypeTest.scala index 1ab2caadb..9b236d2bd 100644 --- a/src/test/scala/chiseltest/tests/OpaqueTypeTest.scala +++ b/src/test/scala/chiseltest/tests/OpaqueTypeTest.scala @@ -6,6 +6,7 @@ import chisel3.experimental.BundleLiterals.AddBundleLiteralConstructor import chisel3.experimental.OpaqueType import chisel3._ import chiseltest._ +import chiseltest.iotesters.PeekPokeTester import org.scalatest.flatspec.AnyFlatSpec import scala.collection.immutable.SeqMap @@ -30,6 +31,15 @@ class OpaqueTypeTest extends AnyFlatSpec with ChiselScalatestTester { dut.out.expect(rec(_val)) } + class PokeExpectTester[T <: Data](dut: OpaquePassthrough[T], _val: => T) extends PeekPokeTester(dut) { + poke(dut.in, IndexedSeq(_val.litValue)) + expect(dut.out, IndexedSeq(_val.litValue)) + } + + def testPokeExpectTester[T <: Data](_val: => T): Unit = + test(new OpaquePassthrough(_val.cloneType)) + .runPeekPoke(new PokeExpectTester(_, _val)) + behavior of "OpaqueType" it should "poke and expect successfully" in { @@ -38,4 +48,10 @@ class OpaqueTypeTest extends AnyFlatSpec with ChiselScalatestTester { testPokeExpect(rec(5.U(3.W))) } + it should "poke and expect successfully using PeekPokeTester" in { + testPokeExpectTester(4.U(6.W)) + testPokeExpectTester(-4.S(8.W)) + testPokeExpectTester(rec(5.U(3.W))) + } + }