Search code examples
chisel

How to do a vector of modules?


I want to instantiate a one dimensional array of element, and element extends Module. How would I do this? If I say my best guess, which is:

val elements = Vec( 64, new element )

I get the following error message:

[error] /Users/mykland/work/chisel/array.scala:20: overloaded method value apply with alternatives:
[error]   [T <: Chisel.Data](n: Int, gen: => T)Chisel.Vec[T] <and>
[error]   [T <: Chisel.Data](elt0: T, elts: T*)Chisel.Vec[T] <and>
[error]   [T <: Chisel.Data](gen: => T, n: Int)Chisel.Vec[T]
[error]  cannot be applied to (Int, ascenium.element)
[error]     val elements    = Vec( 64, new element )
[error]                       ^

Thanks in advance for any assistance you can provide.


Solution

  • Edit: I'm adding what I think is a better way to generate a vector of modules:

    val my_args = Seq(1,2,3,4)
    val exe_units = for (i <- 0 until num_units) yield
    {
       val exe_unit = Module(new AluExeUnit(args = my_args(i)))
       // any wiring or other logic can go here
       exe_unit
    }
    

    Notice that this method allows you to individually tailor each unit differently, and returns a Seq() of Chisel modules. It will generate better looking hardware too.

    But if you really need to be able to dynamically index into your array of Modules, you can pull out a Vec() of the IOs like this:

    val exe_units_io = Vec(exe_units.map(_.io))
    

    (This is the old suggestion, which I think is less good).

    You can create a Vec of Modules as follows:

    val vec_of_elements = Vec.fill(n) {Module(new MyElement(my_args)).io }
    

    But notice, a Vec can really only be either of Wires or of Registers, so we've really just created a Vec of the IO Wires, which just so happen to create the Modules we care about in the process.