diff options
| author | Schuyler Eldridge | 2019-10-01 12:50:17 -0400 |
|---|---|---|
| committer | Schuyler Eldridge | 2019-11-05 22:42:40 -0500 |
| commit | a6d8799adb1ccad67dd33fe188f10852dd2d51ad (patch) | |
| tree | 1ac36698d50ee6bff1f53ba5b3cfc215921e56d9 | |
| parent | 92d88ffbe1663b4aa917c6c8b66a4fd697282410 (diff) | |
Don't use MuxLookup default for full mapping
This modifies MuxLookup to not use the 'default' mapping argument if a
"full" mapping is provided. A "full" mapping enumerates all possible
cases for a 'key' argument of a known size. This will check literal
values to ensure exhaustiveness holds.
Signed-off-by: Schuyler Eldridge <schuyler.eldridge@ibm.com>
Co-authored-by: Schuyler Eldridge <schuyler.eldridge@ibm.com>
Co-authored-by: Albert Magyar <albert.magyar@gmail.com>
| -rw-r--r-- | src/main/scala/chisel3/util/Mux.scala | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/src/main/scala/chisel3/util/Mux.scala b/src/main/scala/chisel3/util/Mux.scala index 342531bd..b9a5f576 100644 --- a/src/main/scala/chisel3/util/Mux.scala +++ b/src/main/scala/chisel3/util/Mux.scala @@ -63,10 +63,21 @@ object MuxLookup { * @return the value found or the default if not */ def apply[S <: UInt, T <: Data] (key: S, default: T, mapping: Seq[(S, T)]): T = { - var res = default - for ((k, v) <- mapping.reverse) - res = Mux(k === key, v, res) - res + /* If the mapping is defined for all possible values of the key, then don't use the default value */ + val (defaultx, mappingx) = key.widthOption match { + case Some(width) => + val keySetSize = BigInt(1) << width + val keyMask = keySetSize - 1 + val distinctLitKeys = mapping.flatMap(_._1.litOption).map(_ & keyMask).distinct + if (distinctLitKeys.size == keySetSize) { + (mapping.head._2, mapping.tail) + } else { + (default, mapping) + } + case None => (default, mapping) + } + + mappingx.foldLeft(defaultx){ case (d, (k, v)) => Mux(k === key, v, d) } } } |
