55
66- :func:`.mux` for a multiplexer that selects between an arbitrary number of options.
77
8- - :ref:`conditional_assignment` for a more readable alternative to :func:`.mux`.
8+ - :ref:`conditional_assignment` for a more readable alternative to nested
9+ :func:`selects<.select>` and :func:`muxes<.mux>`.
910
1011The functions below provide more complex alternatives.
1112"""
@@ -21,6 +22,33 @@ def prioritized_mux(selects: list[WireVector], vals: list[WireVector]) -> WireVe
2122
2223 If none of the ``selects`` are ``1``, the last ``val`` is returned.
2324
25+ .. doctest only::
26+
27+ >>> import pyrtl
28+ >>> pyrtl.reset_working_block()
29+
30+ Example::
31+
32+ >>> selects = [pyrtl.Input(name=f"select{i}", bitwidth=1)
33+ ... for i in range(3)]
34+ >>> vals = [pyrtl.Const(n) for n in range(2, 5)]
35+ >>> output = pyrtl.Output(name="output")
36+
37+ >>> output <<= pyrtl.rtllib.muxes.prioritized_mux(selects, vals)
38+
39+ >>> sim = pyrtl.Simulation()
40+ >>> sim.step(provided_inputs={"select0": 1, "select1": 0, "select2": 0})
41+ >>> sim.inspect("output")
42+ 2
43+
44+ >>> sim.step(provided_inputs={"select0": 0, "select1": 1, "select2": 0})
45+ >>> sim.inspect("output")
46+ 3
47+
48+ >>> sim.step(provided_inputs={"select0": 0, "select1": 0, "select2": 0})
49+ >>> sim.inspect("output")
50+ 4
51+
2452 :param selects: A list of :class:`WireVectors<.WireVector>` signaling whether a wire
2553 should be chosen.
2654 :param vals: Values to return when the corresponding ``select`` value is ``1``.
@@ -62,6 +90,34 @@ def sparse_mux(sel: WireVector, vals: dict[int, WireVector]) -> WireVector:
6290 ``sparse_mux`` supports not having a full specification. Indices that are not
6391 specified are treated as don't-cares.
6492
93+ .. doctest only::
94+
95+ >>> import pyrtl
96+ >>> pyrtl.reset_working_block()
97+
98+ Example::
99+
100+ >>> select = pyrtl.Input(name="select", bitwidth=3)
101+ >>> vals = {2: pyrtl.Const(3),
102+ ... 4: pyrtl.Const(5),
103+ ... pyrtl.rtllib.muxes.SparseDefault: pyrtl.Const(7)}
104+ >>> output = pyrtl.Output(name="output")
105+
106+ >>> output <<= pyrtl.rtllib.muxes.sparse_mux(select, vals)
107+
108+ >>> sim = pyrtl.Simulation()
109+ >>> sim.step(provided_inputs={"select": 2})
110+ >>> sim.inspect("output")
111+ 3
112+
113+ >>> sim.step(provided_inputs={"select": 4})
114+ >>> sim.inspect("output")
115+ 5
116+
117+ >>> sim.step(provided_inputs={"select": 3})
118+ >>> sim.inspect("output")
119+ 7
120+
65121 :param sel: Select wire, which chooses one of the mux input ``vals`` to output.
66122 :param vals: :class:`dict` of mux input values. If the special key
67123 :data:`SparseDefault` exists, it specifies the ``sparse_mux``'s default value.
@@ -217,8 +273,8 @@ def finalize(self):
217273def demux (select : WireVector ) -> tuple [WireVector , ...]:
218274 """Demultiplexes a wire of arbitrary bitwidth.
219275
220- This effectively converts an unsigned binary value into a unary value, returning
221- each bit of the unary value as a separate :class:`.WireVector`.
276+ This effectively converts an unsigned binary value into a one-hot encoded value,
277+ returning each bit of the one-hot encoded value as a separate :class:`.WireVector`.
222278
223279 .. doctest only::
224280
@@ -229,26 +285,26 @@ def demux(select: WireVector) -> tuple[WireVector, ...]:
229285
230286 >>> input = pyrtl.Input(bitwidth=3)
231287
232- >>> output = pyrtl.rtllib.muxes.demux(input)
233- >>> len(output )
288+ >>> outputs = pyrtl.rtllib.muxes.demux(input)
289+ >>> len(outputs )
234290 8
235- >>> len(output [0])
291+ >>> len(outputs [0])
236292 1
237- >>> for i, wire in enumerate(output ):
238- ... wire.name = f"output [{i}]"
293+ >>> for i, wire in enumerate(outputs ):
294+ ... wire.name = f"outputs [{i}]"
239295
240296 >>> sim = pyrtl.Simulation()
241297 >>> sim.step(provided_inputs={input.name: 5})
242298
243- >>> sim.inspect("output [4]")
299+ >>> sim.inspect("outputs [4]")
244300 0
245- >>> sim.inspect("output [5]")
301+ >>> sim.inspect("outputs [5]")
246302 1
247- >>> sim.inspect("output [6]")
303+ >>> sim.inspect("outputs [6]")
248304 0
249305
250- In the example above, ``len(output )`` is ``8`` because ``2 ** 3 == 8``, and
251- ``output [5]`` is ``1`` because the output index ``5`` matches the input value.
306+ In the example above, ``len(outputs )`` is ``8`` because ``2 ** 3 == 8``, and
307+ ``outputs [5]`` is ``1`` because the output index ``5`` matches the input value.
252308
253309 See :func:`.binary_to_one_hot`, which performs a similar operation.
254310
0 commit comments