perfopt
perfopt

Reputation: 77

How to create a array/vec of Chisel modules

I created a CHISEL Class (extends) module called SaturatingCounter (code below in case it is relevant).

I want an array/Seq of these counters in another module.

EDIT: I found this answer. But when I try

 val vec_of_elements = Vec.fill(10) {Module(SaturatingCounter(4)).io}

I get an error

Error:(72, 29) value fill is not a member of object chisel3.Vec
  val vec_of_elements = Vec.fill(10) {Module(SaturatingCounter(4)).io}

Rest of the original question

So I tried creating a Vec as shown below. That does not work because my Class is not of type Data. I could make it inherit Data but since I am still a beginner that looks like a big leap.

var foo = Vec(10,SaturatingCounter(4))

Error message:

Error:(46, 23) inferred type arguments [something.SaturatingCounter] do not conform to method apply's type parameter bounds [T <: chisel3.Data]
  var foo = Vec(10,SaturatingCounter(4))

Error:(46, 47) type mismatch;
 found   : something.SaturatingCounter
 required: T
  var foo = Vec(10,SaturatingCounter(4))

Is the right solution to create a Array/Sequence of SaturatingCounter s?

SaturatingCounter:

class SaturatingCounter (bits: Int) extends Module {
  require(bits >= 2)
  val io = IO(new Bundle {
    val valOut: SInt = Output(SInt(bits.W))
    val inc: SInt = Input(SInt(bits.W))
  })

  val count = RegInit(SInt(bits.W), 0.S)

  val minVal: SInt = ((1 << (bits-1)).asUInt())(bits-1,0).asSInt()
  val maxVal: SInt = (((1 << bits) - 1) >> 1).asSInt(bits.W)

  var sum : SInt = count + io.inc

  var operandsSignSame : Bool = io.inc(bits-1) === count(bits-1)
  var sumSignDifferent: Bool = sum(bits-1) =/= count(bits-1)

  when(operandsSignSame && sumSignDifferent) {
    when(count(bits-1)) {
      count := minVal
    }.otherwise {
      count := maxVal
    }
  }.otherwise {
    count := sum
  }
  io.valOut := count
}

object SaturatingCounter {
  def apply(bits: Int): SaturatingCounter = new SaturatingCounter(bits)
}

Upvotes: 2

Views: 2032

Answers (1)

Chick Markley
Chick Markley

Reputation: 4051

Try using this to get the create the modules and get access to their io.

  val vec_of_elements = Vec(10, Module(SaturatingCounter(4)).io)

That works form me.

Note the .io, by adding that you are getting some of type Data and you have access to all the ios of the modules you've created here.

In general you only need a Vec if you want to use hardware based indexing of the elements, or your Elements are part of an IO. If you don't need that you can just use a Scala collection like Seq, Array or similar.

Upvotes: 1

Related Questions