2

What is the proper way to test a Chisel function not part of a Module that generates hardware constructs?

object Util {
  def getSquare(vec2d: Vec[Vec[Bool]]) : Seq[Seq[Bool]] = {
    val size = vec2d.size max vec2d(0).size
    return Seq.fill(size, size) {Wire(Bool())}
  }
}

How can I test this function? Because it isn't a Module, the standard Chisel test format complains.

1
  • Instead of using an object, should I be using scala traits instead? Commented Oct 8, 2021 at 16:16

1 Answer 1

3

In general you can only use code that generates chisel hardware constructs within a Module (literals are an exception). So the typical methodology would be to write a wrapper and see that the generated code contains what you expect. For example here's a little test of your function

  "test non-module generator" in {

    // pads out rectangular vec into square nested seq, fills with values based on indices
    class Wrapper extends Module {
      val square = (Util.getSquare(Vec(4, Vec(2,Bool()))))
      square.indices.foreach { i => square(i).indices.foreach { j => square(i)(j) := ((i + j ) % 2 == 0).B}}
      val out = IO(Output(Vec(4, Vec(4,Bool()))))
      out.indices.foreach { i => out(i).indices.foreach { j => out(i)(j) := square(i)(j)}}
    }

    val firrtlSource = ChiselStage.emitFirrtl(new Wrapper)
    println(firrtlSource)

    firrtlSource should contain ("wire square_4_4")
  }

You can also test your wrapper function with the chiseltest basic test harness, for example:

test(new Wrapper) { dut =>
  dut.out(3)(3).expect(true.B)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.