Advanced Parameterization Manual in Chisel

Viewed 557

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?

2 Answers

The = in (implicit _p: Option[Parameters] = None) is assigning a default value of None to the parameter _p. That means that unless the otherwise specified there is no Parameter instance assigned to _p.

Just in case you are asking about the => in (c: =>T), the => is means that the first parameter c is a reference to a function that returns an instance of T, where T is a subclass of Module.

There's a bunch of idiomatic features of Scala being employed here: Function Currying, implicit parameters, Functions as first class citizens of the language. It's worth a taking a bit of time to learn the syntax of these things. Check out Chisel's generator-bootcamp tutorial particularly section 3.2 and 3.3 for some of the ways Chisel takes advantage of Scala's syntax

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
Related