Skip to content

ยง 1.5. Selection and Decoding

A multiplexer picks one of several inputs by a select number; a decoder turns a number into one active line. Both are built from the gates of ยง 1.3, and both keep the gates' four-state behavior in their compact forms.

Component Description Structural Behavioral
Mux<N,W> Passes the selected one of N W-bit inputs Yes Yes
Decoder<N> Raises the one of 2^N outputs that the N-bit address selects Yes Yes

1.5.1. Mux<N,W>

Mux<N,W> passes one of its N inputs, each W bits wide, to its output. N is 2, 4, 8, 16 or 32 and W is 1, 4, 8, 16 or 32; each geometry is its own contract logic.mux.<N>.<W>, returned by circuit::word::mux(N, W). The recorded example is Mux<2,1>.

IO pins

Pin Direction Width Description
A, B Input W each The two inputs (N = 2)
IN0 โ€ฆ IN<Nโˆ’1> Input W each The inputs (N > 2)
SEL Input logโ‚‚(N) Which input to pass
OUT Output W The selected input

Behavior

With a binary SEL of value k, OUT equals input k (A for 0 and B for 1 when N = 2). Each output bit is chosen independently, by a tree of two-input muxes: level 0 pairs neighbouring inputs by SEL bit 0, level 1 pairs those results by SEL bit 1, and so on.

C++ example

#include <cstdint>
#include <vector>

uint64_t mux_ref(const std::vector<uint64_t>& inputs, unsigned select) {
    return inputs.at(select);
}

This binary function describes the settled output. It is not a registered implementation.

Unknown inputs

A two-input mux is built as (A AND NOT SEL) OR (B AND SEL), not as an optimistic conditional: with SEL unknown, the output is 0 when both data bits are 0, and X otherwise (even when both are 1, because each AND term alone is unknown). With SEL known, the unselected input does not matter; the selected one passes through the gates, so a Z there gives X. A tree applies this rule at every level.

Implementation

Structural implementation

  • Mux<2,1>: NOT_SEL (NOT), AND_A (A AND NOT SEL), AND_B (B AND SEL) and OR_OUT.
  • Mux<N,1> for N > 2: SEL_SPLIT and a balanced tree of Mux<2,1> named MUX_L<level>_<index>, level โ„“ selecting by SEL bit โ„“.
  • Mux<2,W> for W > 1: A_SPLIT, B_SPLIT, one shared NOT_SEL, and for each bit SELECT_A_<i>, SELECT_B_<i> and COMBINE_<i> (the same AND/AND/OR as Mux<2,1>), joined by OUT_JOIN.
  • Mux<N,W> for N > 2 and W > 1: a splitter per input, one Mux<N,1> per bit (MUX_BIT_<i>) sharing SEL, and OUT_JOIN.

Behavioral implementation

One leaf evaluates, for every output bit, the same tree of two-input muxes with the gate rule above, and publishes the word on the delays proven on the gates (ยง 1.2 Compact parts). Every geometry has it.

Examples

All 10 recorded rows of Mux2Width1Test, in checkpoint order. Outputs are test expectations verified against the simulation. Time is the absolute settled checkpoint time in simulator units, not milliseconds or CPU cycles.

Default scenario profile: fidelity-gate-v1 / structural (ed03f70afe360dba). Changing fidelity or the circuit can change these times.

Checkpointzero-based number ยท timeline label Timesimulator units AInput ยท 1 bit BInput ยท 1 bit SELInput ยท 1 bit OUTExpected output ยท 1 bit
#0row-03 1 0 0 1
#1row-16 0 1 0 0
#2row-29 0 1 1 1
#3row-312 1 0 1 0
#4row-415 0 X 1 X
#5row-517 0 Z 1 X
#6row-620 X 1 1 1
#7row-722 Z 1 1 1
#8row-825 0 1 X X
#9row-927 0 1 Z X

Scroll within the table for all rows and pins. Buses use hexadecimal; values containing X/Z use MSB-first binary. Inputs show the declared values in effect at each checkpoint, including retained inputs. Sequential tests also replay preparation changes between checkpoints, such as lowering the clock. Use the matching checkpoint label in the simulation below.

The table selects each input with the other at the opposite value, then drives unknown and floating selects and data.

Embedded simulation

The mux-2-w1 scenario runs Mux2Width1Test. Inspect AND_A and AND_B with SEL unknown to see why equal data inputs do not make the output known.

Open Mux<2,1> in the simulator

Source and tests

Test Coverage
Mux2Width1Test, Mux4Width1Test, Mux8Width1Test, Mux32Width1Test Every select value of the one-bit muxes, with unknown and floating selects and data
Mux2Width4Test, Mux8Width8Test, Mux2Width32Test, Mux4Width32Test, Mux8Width32Test, Mux32Width32Test Every select value of word muxes with distinct data words, and four-state rows
CompactFormEquivalenceTest Both forms of every geometry at W = 1, 4, 8 and 16 on binary, dense four-state and single-unknown vectors (W = 32 in the tests above)

1.5.2. Decoder<N>

Decoder<N> raises the one output line whose number is the N-bit address, while ENABLE is high. N is 1 to 5. The contract is logic.decoder.<N>.full; the variant logic.decoder.<N>.zero-disabled ties output 0 low and builds no logic for it (the register file uses it, because register x0 has no storage). The recorded example is Decoder<5>.

IO pins

Pin Direction Width Description
ADDR Input N The output number
ENABLE Input 1 Low forces every output low
OUT0 โ€ฆ OUT<2^Nโˆ’1> Output 1 each OUTk is high when ENABLE is high and ADDR = k

Behavior

With binary inputs exactly one output is high when ENABLE is high, and none when it is low. In the zero-disabled variant OUT0 is always low.

C++ example

#include <cstdint>
#include <vector>

std::vector<bool> decoder_ref(unsigned address, bool enable, unsigned bits) {
    std::vector<bool> out(1u << bits, false);
    if (enable) out.at(address) = true;
    return out;
}

This binary function describes the settled outputs. It is not a registered implementation.

Unknown inputs

Each output is an AND of ENABLE and one literal (the bit or its inverse) per address bit. A low ENABLE makes every output 0. An unknown or floating address bit makes X every output whose other literals are all 1 and 0 every output that another literal already rules out.

Implementation

Structural implementation

At N = 5: ADDR_SPLIT, one word inverter NOT_ADDR (NOTGate<5>) with its bits split by NOT_ADDR_BITS, predecoding gates LOW_0 โ€ฆ LOW_3 (address bits 0โ€“1), HIGH_PAIR_0 โ€ฆ HIGH_PAIR_7 (bits 2โ€“3) and HIGH_0 โ€ฆ HIGH_7 (with bit 4), and one MATCH_<k> AND per output joining a low and a high term. The 32 matches are joined into MATCH_WORD, ANDed with ENABLE spread across the word (ENABLE_WORD) in one word gate ENABLE (ANDGate<32>, or ANDGate<31> in the zero-disabled variant), and split to the outputs by OUT_BITS. At N = 1 to 4 each output is a balanced AND tree of ENABLE and its N literals, after one inverter per address bit.

Behavioral implementation

At N = 5 one leaf evaluates the same literals and AND terms with the gate rule, so unknown address bits give the gates' outputs, on the delays proven on the gates. At N = 1 to 4 the compact form evaluates the decoder's own gate list (ยง 1.1); it is chosen only explicitly, for example by collapsing the part in the simulator.

Examples

All 40 recorded rows of DecoderWidth5Test, in checkpoint order. Outputs are test expectations verified against the simulation. Time is the absolute settled checkpoint time in simulator units, not milliseconds or CPU cycles.

Default scenario profile: fidelity-gate-v1 / structural (ed03f70afe360dba). Changing fidelity or the circuit can change these times.

Checkpointzero-based number ยท timeline label Timesimulator units ADDRInput ยท 5 bit ENABLEInput ยท 1 bit OUT0Expected output ยท 1 bit OUT1Expected output ยท 1 bit OUT10Expected output ยท 1 bit OUT11Expected output ยท 1 bit OUT12Expected output ยท 1 bit OUT13Expected output ยท 1 bit OUT14Expected output ยท 1 bit OUT15Expected output ยท 1 bit OUT16Expected output ยท 1 bit OUT17Expected output ยท 1 bit OUT18Expected output ยท 1 bit OUT19Expected output ยท 1 bit OUT2Expected output ยท 1 bit OUT20Expected output ยท 1 bit OUT21Expected output ยท 1 bit OUT22Expected output ยท 1 bit OUT23Expected output ยท 1 bit OUT24Expected output ยท 1 bit OUT25Expected output ยท 1 bit OUT26Expected output ยท 1 bit OUT27Expected output ยท 1 bit OUT28Expected output ยท 1 bit OUT29Expected output ยท 1 bit OUT3Expected output ยท 1 bit OUT30Expected output ยท 1 bit OUT31Expected output ยท 1 bit OUT4Expected output ยท 1 bit OUT5Expected output ยท 1 bit OUT6Expected output ยท 1 bit OUT7Expected output ยท 1 bit OUT8Expected output ยท 1 bit OUT9Expected output ยท 1 bit
#0row-05 0x00 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#1row-110 0x01 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#2row-215 0x02 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#3row-320 0x03 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
#4row-426 0x04 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
#5row-531 0x05 1 0 0 0 0 0 0 0 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0 0 0 0 0 0
#32row-32173 0x00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#33row-33178 0x10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#34row-34184 0x1F 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#35row-35190 0bXXXXX 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#36row-36192 0bXXXXX 1 X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X
#37row-37194 0bZZZZZ 1 X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X
#38row-38200 0x10 X 0 0 0 0 0 0 0 0 X 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#39row-39202 0x10 Z 0 0 0 0 0 0 0 0 X 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Scroll within the table for all rows and pins. Buses use hexadecimal; values containing X/Z use MSB-first binary. Inputs show the declared values in effect at each checkpoint, including retained inputs. Sequential tests also replay preparation changes between checkpoints, such as lowering the clock. Use the matching checkpoint label in the simulation below.

The table drives every address with ENABLE high, then disabled and four-state cases.

Embedded simulation

The decoder-address5 scenario runs DecoderWidth5Test.

Open Decoder<5> in the simulator

Source and tests

Test Coverage
DecoderWidth5Test, DecoderWidth5ZeroDisabledTest Every address of both 5-bit variants with ENABLE high and low, on both forms
WidthComponentsTest The narrower decoders
GateListCompactEquivalenceTest The gate-list compact forms of the 1- to 4-bit decoders against their gates