Search code examples
chisel

Chisel3: Vec indexWhere expected Bool, actual Any


In Chisel, I have a Vec of Bools coming into a module. I would like to know the index of the first False which occurs.

To obtain this, I tried to use the following:

val faultIndex = Wire(UInt)

faultIndex := comparison.indexWhere(x:Bool => x === false.B)

When I put this in, an error was highlighted:

Unspecified value parameters: from: Int
Type mismatch, expected Bool => Bool, actual: Bool => Any
Type mismatch, expected Bool => Boolean, actual: Bool => Any
Cannot resolve symbol x
Cannot resolve symbol x

What is the proper way to use this function?


Solution

  • There are 2 minor syntax issues here:

    val faultIndex = Wire(UInt())
    

    Note the () after UInt. You can think about this as constructing a fresh type object rather than pointing to the static object called UInt.

    There are a few ways to express the indexWhere:

    faultIndex := comparison.indexWhere((x: Bool) => x === false.B) // Note parentheses
    // or
    faultIndex := comparison.indexWhere(x => x === false.B) // Type is inferred
    // or
    faultIndex := comparison.indexWhere(_ === false.B) // underscore shorthand
    // alternatively
    faultIndex := comparison.indexWhere(x => !x) // !x is equivalent to x === false.B
    // or
    faultIndex := comparison.indexWhere(!_)      // More underscore shorthand
    
    

    Executable example: https://scastie.scala-lang.org/uHCX5wxgSzu6wXqa9OJdRA