Search code examples
chisel

Advanced Parameterization Manual in Chisel


This is inside the chisel library

object Module { 

  // returns a new Module of type T, initialized with a Parameters instance if  _p !=None.

  def apply[T<:Module](c: =>T)(implicit _p: Option[Parameters] = None):T

}

I don't understand the =sign in the parameters. What does it represents?


Solution

  • This example has two = signs. The first corresponds to By-name parameters: https://docs.scala-lang.org/tour/by-name-parameters.html.

    The former is important because Modules in Chisel must wrapped in Module(...) when they are constructed. We generally accomplish using call by-name:

    class MyModule extends Module {
      ...
    }
    // This works!
    def func(mod: => MyModule) = {
      val instance = Module(mod) // The module is constructed inside Module(...)
    }
    func(new MyModule)
    
    // This doesn't work!
    def func(mod: MyModule) = {
      val instance = Module(mod)
    }
    func(new MyModule) // The module is constructed too early, here!
    

    The second is a Default parameter: https://docs.scala-lang.org/tour/default-parameter-values.html. It's mainly a convenience thing:

    def func(x: Int = 3) = { println(x) }
    func(5) // prints 5
    func()  // prints 3