Chisel invert Vec[Bool] one-liner

Viewed 89

Is there a one-liner that takes a Vec[Bool] and creates an inverted version of it?

Here is an example of taking 37 bits, inverting them, then doing an OR reduction across all of them. Is there a one-liner that can replace the assignment of inv_a?

class MyModule extends Module {
  val io = IO(new Bundle {
    val a = Input(Vec(37, Bool()))
    val b = Output(Bool())
  })

  val inv_a = Wire(Vec(37, Bool()))
  for (i <- 0 until 37) {
    inv_a(i) := ~io.a(i)
  }

  io.b := inv_a.reduce((a, b) => (a | b))
}
2 Answers

I think this does what you want.

val inv_a = (~ io.a.asUInt).asBools()

Alternatively, you could map on the input Vec to invert each element:

val inv_a = io.a.map(!_)

Example in a Scastie (using Chisel v3.4.4): https://scastie.scala-lang.org/bcQIihPMThelC9h4jDfyxQ

Note that the type of inv_a is a Seq[Bool] which you can still do your reduce on, but if you need a Vec[Bool] back you'll need to wrap the result in VecInit(...):

val inv_a: Vec[Bool] = VecInit(io.a.map(!_))
Related