Conditional port in the bit module

I have a choice that is usually not needed. However, to support this feature, some I / O ports must be added to the original I / O port of the module.

I do it like this:

import Chisel._

class TestModule extends Module {

  class IOBundle extends Bundle {
    val i = Bool(INPUT)
    val o = Bool(OUTPUT)
  }

  class IOBundle_EXT extends IOBundle {
    val o_ext = Bool(OUTPUT)
  }

  val io = if(true) new IOBundle_EXT else new IOBundle;

  io.o := io.i
  io.o_ext := io.i

}

      

After starting sbt "run TestModule --backend c --compile --test --genHarness" the compiler complains:

[error] xxxx/test/condi_port.scala:17: value o_ext is not a member of TestModule.this.IOBundle
[error]   io.o_ext := io.i
[error]      ^
[error] one error found
[error] (compile:compile) Compilation failed

      

So the if statement has no effect. val io is still assigned to IOBundle and not extended IOBoundle_EXT, which doesn't make any sense to me.

+3


source to share


3 answers


FOB now supports options in I / O packages.

I've looked into the options here ( https://github.com/ucb-bar/riscv-boom/commit/da6edcb4b7bec341e31a55567ee04c8a1431d659 ) as an example , but here's a summary:



class MyBundle extends Bundle
{
   val my_ext = if (SOME_SWITCH) Some(ExtBundle) else None
}

...

io.my_ext match
{
   case Some(b: ExtBundle) =>
      my_ext.a := Bool(false)
      ...
   case _ => require (!SOME_SWITCH)
}

      

It's incredibly verbose, but I was able to get it to work even when doing bulk connections and hiding packets in packets, etc.

+2


source


Although the compiler could determine that only one result is possible (the expression is always true), the type system sets the type of the result to be the largest common subtype of the two possible (true or false) subexpressions.

You can check it trivially like this:



scala> val result = if (true) 1 else "one"
result: Any = 1

      

+2


source


Try the following:

import Chisel._

class TestModule(val useEXT : Boolean) extends Module {

  class IOBundle extends Bundle {
    val i = Bool(INPUT)
    val o = Bool(OUTPUT)
  }

  class IOBundle_EXT extends IOBundle {
    val o_ext = Bool(OUTPUT)
  }

  val io = { 
    if(useEXT) { 
      val res = new IOBundle_EXT; res.o_ext := res.i; res 
    } else {
      new IOBundle }};

  io.o := io.i

}

      

0


source







All Articles