Home > Blockchain >  How to correctly pass implicit parameters to a module?
How to correctly pass implicit parameters to a module?

Time:03-29

I'm trying to pass implicit parameters to a submodule in an instantiated module. The implicit parameter is the config package defined in rocketchipenter link description here, I want to use the config package to pass some constants to submodules.But when I use the module instantiation, the port I define in the submodule is always not found.

The top-level module is defined as follows:

package test
import chisel3._
import chisel3.util._
import chisel3.experimental.BaseModule
import chipsalliance.rocketchip.config._
class B extends Module{
  val io = IO(
     ...)
  val config = new TConfig
  val submod: Class[_ <: Any] = Class.forName("test.T").asInstanceOf[Class[_ <: BaseModule]]
  val u_T = Module(submod.getConstructor(classOf[Parameters]).newInstance(config) match {
    case a: RawModule => a
  })
   u_T.io <> ...
}

Define submodule and its parameters:

case class TParams(
  P: Int = 100
)

case object TKey extends Field[TParams()]

class TConfig extends Config((site, up, here) => {
  case TKey => {
    TParams(P=1024)
}
})

abstract class TModule(implicit val p: Parameters) extends Module
with HasParameters

trait HasParameters {
 implicit val p: Parameters
}

class T(implicit p: Parameters) extends TModule()(p)
{
  val io = IO(new Bundle{...})
}

When compiling, the report shows that I cannot find the io port of the submodule.

value io is not a member of chisel3.RawModule

CodePudding user response:

I assume you are instantiating your module using reflection because it is how rocket-chip does its top-level instantiation, but note that rocket-chip only does that because it accepts the name of it's top-level module via the command-line. If you know the class you want to instantiate, you can just do so directly:

class B extends Module {
  val io = IO(
     ...)
  // Note the use of implicit here will ensure the compiler finds it
  implicit val config = new TConfig
  val submod: test.T = Module(new test.T)
  u_T.io <> ...
}

Note that you can always pass implicit arguments explicitly (which is incidentally what you're doing in passing p to TModule (ie. extends TModule()(p):

class B extends Module {
  val io = IO(
     ...)
  // Passing it explicitly
  val config = new TConfig
  val submod: test.T = Module(new test.T()(config))
  u_T.io <> ...
}
  • Related