iBug
iBug

Reputation: 37307

How to use a chisel3.experimental.ChiselEnum in an I/O port?

Consider this code:

import chisel3.experimental.ChiselEnum

object MyEnum extends ChiselEnum {
  val A, B = Value
}

class UseEnumIO extends Module {
  val io = IO(new Bundle {
    val in = Input(UInt(1.W))
    val out = Output(Bool())
  })

  io.out := MuxLookup(io.in, false.B, Array(
    MyEnum.A -> true.B,
    MyEnum.B -> true.B
  ))
}

I need to use an IO port which is supposed to be a ChiselEnum object in a MuxLookup.

This is the error message I got from SBT:

[error]  found   : scala.collection.mutable.WrappedArray[(MyEnum.Type, chisel3.core.Bool)]

while Scala inferred that [S <: chisel3.UInt,T <: chisel3.Data]

I also tried val in = Input(MyEnum.Type) which gave me a more serious error.

val defaultVersions = Map(
  "chisel3" -> "3.2-SNAPSHOT
)

Upvotes: 3

Views: 698

Answers (2)

Jack Koenig
Jack Koenig

Reputation: 6064

MuxLookup requires UInts (or Bools) for the selector. From the API docs:

def apply[S <: UInt, T <: Data](key: S, default: T, mapping: Seq[(S, T)]): T

This is probably an oversight, MuxLookup predates ChiselEnum by a lot of time so it wasn't made with it in mind, I have filed an issue to support this use case. You can use Chick's workaround in the meantime.

Upvotes: 2

Chick Markley
Chick Markley

Reputation: 4051

I'm not quite sure why this doesn't work, but the following work-around might help. Try

  io.out := MuxLookup(io.in, false.B, Seq(
    MyEnum.A.asUInt -> true.B,
    MyEnum.B.asUInt -> true.B
  ))

it seems to work to me. I'll keep looking for a reason that the more obvious simple syntax does not work.

Upvotes: 2

Related Questions