简体   繁体   中英

Conditionnal Module instantiation un Chisel

I'm trying to instanciate one of two chisel Module according to boolean parameter.

val useLib = true
val myModule = if(useLib) Module(new MyModule1()) else Module (new MyModule2())

But that doesn't work. Chisel doesn't recognize io interface:

[error] /path/to/source/mysource.scala:59:13: value io is not a member of Any
[error]   myModule.io.pdm <> io.pdm
[error]            ^

And of course, MyModule1() and MyModule2() have same io interfaces.

Is it possible to conditionnaly instantiate Module() as we do with preprocessor in C or C++?

I've written a new doc about upgrading from Chisel 3.4 to 3.5 that deals with this issue. It's not live on the website yet but will be once Chisel 3.5.0-RC2 is released. Here's a link to the doc: https://gist.github.com/jackkoenig/4949f6a455ae74923bbcce10dbf846b5#value-io-is-not-a-member-of-chisel3module

In sort, from Scala's perspective, MyModule1 and MyModule2 actually do not have the same interface, even though they are structurally the same. The trick is to factor out that interface into a named Bundle class and then use that in each of those modules. You then make each Module extend a trait that has that interface, and then Scala will know that the interfaces are the same.

For more information and examples, see the above linked doc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM