@@ -7,55 +7,38 @@ package chisel3.util
77
88import chisel3 ._
99import chisel3 .experimental .SourceInfo
10+ import chisel3 .internal .Builder
1011
11- /** Builds a Mux tree out of the input signal vector using a one hot encoded
12- * select signal. Returns the output of the Mux tree.
13- *
14- * @example {{{
15- * val hotValue = chisel3.util.Mux1H(Seq(
16- * io.selector(0) -> 2.U,
17- * io.selector(1) -> 4.U,
18- * io.selector(2) -> 8.U,
19- * io.selector(4) -> 11.U,
20- * ))
21- * }}}
22- *
23- * @note results unspecified unless exactly one select signal is high
24- */
25- object Mux1H {
26- def apply [T <: Data ](sel : Seq [Bool ], in : Seq [T ]): T = {
27- require(sel.size == in.size, s " Mux1H: input Seqs must have the same length, got sel ${sel.size} and in ${in.size}" )
28- apply(sel.zip(in))
12+ private [chisel3] trait Mux1HImpl {
13+ protected def _applyImpl [T <: Data ](sel : Seq [Bool ], in : Seq [T ])(implicit sourceInfo : SourceInfo ): T = {
14+ if (sel.size != in.size) {
15+ Builder .error(s " Mux1H: input Seqs must have the same length, got sel ${sel.size} and in ${in.size}" )
16+ }
17+ _applyImpl(sel.zip(in))
2918 }
30- def apply [T <: Data ](in : Iterable [(Bool , T )]): T = SeqUtils .oneHotMux(in)
31- def apply [T <: Data ](sel : UInt , in : Seq [T ]): T =
32- apply((0 until in.size).map(sel(_)), in)
33- def apply (sel : UInt , in : UInt ): Bool = (sel & in).orR
19+
20+ protected def _applyImpl [T <: Data ](in : Iterable [(Bool , T )])(implicit sourceInfo : SourceInfo ): T =
21+ SeqUtils .oneHotMux(in)
22+
23+ protected def _applyImpl [T <: Data ](sel : UInt , in : Seq [T ])(implicit sourceInfo : SourceInfo ): T =
24+ _applyImpl((0 until in.size).map(sel(_)), in)
25+
26+ protected def _applyImpl (sel : UInt , in : UInt )(implicit sourceInfo : SourceInfo ): Bool = (sel & in).orR
3427}
3528
36- /** Builds a Mux tree under the assumption that multiple select signals
37- * can be enabled. Priority is given to the first select signal.
38- *
39- * @example {{{
40- * val hotValue = chisel3.util.PriorityMux(Seq(
41- * io.selector(0) -> 2.U,
42- * io.selector(1) -> 4.U,
43- * io.selector(2) -> 8.U,
44- * io.selector(4) -> 11.U,
45- * ))
46- * }}}
47- * Returns the output of the Mux tree.
48- */
49- object PriorityMux {
50- def apply [T <: Data ](in : Seq [(Bool , T )]): T = SeqUtils .priorityMux(in)
51- def apply [T <: Data ](sel : Seq [Bool ], in : Seq [T ]): T = {
52- require(
53- sel.size == in.size,
54- s " PriorityMux: input Seqs must have the same length, got sel ${sel.size} and in ${in.size}"
55- )
56- apply(sel.zip(in))
29+ private [chisel3] trait PriorityMuxImpl {
30+
31+ protected def _applyImpl [T <: Data ](in : Seq [(Bool , T )]): T = SeqUtils .priorityMux(in)
32+
33+ protected def _applyImpl [T <: Data ](sel : Seq [Bool ], in : Seq [T ])(implicit sourceInfo : SourceInfo ): T = {
34+ if (sel.size != in.size) {
35+ Builder .error(s " PriorityMux: input Seqs must have the same length, got sel ${sel.size} and in ${in.size}" )
36+ }
37+ _applyImpl(sel.zip(in))
5738 }
58- def apply [T <: Data ](sel : Bits , in : Seq [T ]): T = apply((0 until in.size).map(sel(_)), in)
39+
40+ protected def _applyImpl [T <: Data ](sel : Bits , in : Seq [T ])(implicit sourceInfo : SourceInfo ): T =
41+ _applyImpl((0 until in.size).map(sel(_)), in)
5942}
6043
6144private [chisel3] trait MuxLookupImpl {
0 commit comments