summaryrefslogtreecommitdiff
path: root/src/main/scala/chisel3/util/Mux.scala
diff options
context:
space:
mode:
authorJim Lawson2016-10-06 08:57:10 -0700
committerJim Lawson2016-10-06 08:57:10 -0700
commit82625071405672eb4a19363d6f73f359ac28a7f5 (patch)
treedee5beff0e7333fa86c1cdcdb79c0d111114b8c9 /src/main/scala/chisel3/util/Mux.scala
parentb7c6e0d1a2098b545938a5a8dfce2b1d9294532f (diff)
parent7de30c2b893a3f24d43f2e131557430eb64f6bc8 (diff)
Merge branch 'master' into tobits-deprecation
Diffstat (limited to 'src/main/scala/chisel3/util/Mux.scala')
-rw-r--r--src/main/scala/chisel3/util/Mux.scala65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/main/scala/chisel3/util/Mux.scala b/src/main/scala/chisel3/util/Mux.scala
new file mode 100644
index 00000000..245de67e
--- /dev/null
+++ b/src/main/scala/chisel3/util/Mux.scala
@@ -0,0 +1,65 @@
+// See LICENSE for license details.
+
+/** Mux circuit generators.
+ */
+
+package chisel3.util
+
+import chisel3._
+import chisel3.core.SeqUtils
+
+/** Builds a Mux tree out of the input signal vector using a one hot encoded
+ * select signal. Returns the output of the Mux tree.
+ *
+ * @note results undefined if multiple select signals are simultaneously high
+ */
+object Mux1H {
+ def apply[T <: Data](sel: Seq[Bool], in: Seq[T]): T =
+ apply(sel zip in)
+ def apply[T <: Data](in: Iterable[(Bool, T)]): T = SeqUtils.oneHotMux(in)
+ def apply[T <: Data](sel: UInt, in: Seq[T]): T =
+ apply((0 until in.size).map(sel(_)), in)
+ def apply(sel: UInt, in: UInt): Bool = (sel & in).orR
+}
+
+/** Builds a Mux tree under the assumption that multiple select signals
+ * can be enabled. Priority is given to the first select signal.
+ *
+ * Returns the output of the Mux tree.
+ */
+object PriorityMux {
+ def apply[T <: Data](in: Seq[(Bool, T)]): T = SeqUtils.priorityMux(in)
+ def apply[T <: Data](sel: Seq[Bool], in: Seq[T]): T = apply(sel zip in)
+ def apply[T <: Data](sel: Bits, in: Seq[T]): T = apply((0 until in.size).map(sel(_)), in)
+}
+
+/** Creates a cascade of n Muxs to search for a key value. */
+object MuxLookup {
+ /** @param key a key to search for
+ * @param default a default value if nothing is found
+ * @param mapping a sequence to search of keys and values
+ * @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
+ }
+}
+
+/** Given an association of values to enable signals, returns the first value with an associated
+ * high enable signal.
+ */
+object MuxCase {
+ /** @param default the default value if none are enabled
+ * @param mapping a set of data values with associated enables
+ * @return the first value in mapping that is enabled */
+ def apply[T <: Data] (default: T, mapping: Seq[(Bool, T)]): T = {
+ var res = default
+ for ((t, v) <- mapping.reverse){
+ res = Mux(t, v, res)
+ }
+ res
+ }
+}