Testing Chisel object functions that use hardware constructs

148 views Asked by At

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

There are 1 answers

0
Chick Markley On BEST ANSWER

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