Search code examples
scalachisel

Chisel: Access to Module Parameters from Tester


How does one access the parameters used to construct a Module from inside the Tester that is testing it?

In the test below I am passing the parameters explicitly both to the Module and to the Tester. I would prefer not to have to pass them to the Tester but instead extract them from the module that was also passed in.

Also I am new to scala/chisel so any tips on bad techniques I'm using would be appreciated :).

import Chisel._
import math.pow

class TestA(dataWidth: Int, arrayLength: Int) extends Module {
  val dataType = Bits(INPUT, width = dataWidth)
  val arrayType = Vec(gen = dataType, n = arrayLength)
  val io = new Bundle {
    val i_valid = Bool(INPUT)
    val i_data = dataType
    val i_array = arrayType
    val o_valid = Bool(OUTPUT)
    val o_data = dataType.flip
    val o_array = arrayType.flip
  }
  io.o_valid := io.i_valid
  io.o_data := io.i_data
  io.o_array := io.i_array
}

class TestATests(c: TestA, dataWidth: Int, arrayLength: Int) extends Tester(c) {
  val maxData = pow(2, dataWidth).toInt
  for (t <- 0 until 16) {
    val i_valid = rnd.nextInt(2)
    val i_data = rnd.nextInt(maxData)
    val i_array = List.fill(arrayLength)(rnd.nextInt(maxData))
    poke(c.io.i_valid, i_valid)
    poke(c.io.i_data, i_data)
    (c.io.i_array, i_array).zipped foreach {
      (element,value) => poke(element, value)
    }
    expect(c.io.o_valid, i_valid)
    expect(c.io.o_data, i_data)
    (c.io.o_array, i_array).zipped foreach {
      (element,value) => poke(element, value)
    }
    step(1)
  }
}    

object TestAObject {
  def main(args: Array[String]): Unit = {
    val tutArgs = args.slice(0, args.length)
    val dataWidth = 5
    val arrayLength = 6
    chiselMainTest(tutArgs, () => Module(
      new TestA(dataWidth=dataWidth, arrayLength=arrayLength))){
      c => new TestATests(c, dataWidth=dataWidth, arrayLength=arrayLength)
    }
   }
}

Solution

  • If you make the arguments dataWidth and arrayLength members of TestA you can just reference them. In Scala this can be accomplished by inserting val into the argument list:

    class TestA(val dataWidth: Int, val arrayLength: Int) extends Module ...
    

    Then you can reference them from the test as members with c.dataWidth or c.arrayLength