Search code examples
chisel

How do I write to a conditional output


In my code I have defined a conditional output:

class EccGenerate[D <: Data](data: D, doubleBit : Boolean = true) extends Module {
  val eccBits = calcCodeBits(data.getWidth)

  val io = IO(new Bundle {
    val in = Input(data.cloneType)
    val out = Output(UInt(eccBits.W))
    val par = if (doubleBit) Some(Output(Bool())) else None
  })

Trying to use the := operator on the par output fails, because it is not always an output. When using conditional inputs, I would use io.par.get() to retrieve the current value of the input, is there a corresponding primitive, operator or function call I can use to set the value of a conditional output?


Solution

  • The issue is that you can't connect to par because it's type is Option[Bool] and := isn't define for Option. You need to unpack it and assign to the Bool inside if the option contains something.

    The functional programming way of doing this would be:

    io.par.foreach(_ := foo)
    

    You can also be more verbose about it if you want:

    io.par match {
      case Some(a) => a := foo
      case None    =>
    }
    

    An if statement would also work:

    if (par.nonEmpty) {
      io.par.get := foo
    }