RTL Foundations / F04

Muxes and Combinational Logic: What Happens When a Branch Is Missing?

Specify a three-channel selector, then use waveforms and synthesized logic to find two separate bugs in combinational RTL.

Lesson film

Film chapters

TranscriptCaptionsDownload film

Our panel started with two sensor channels, A and B. In the previous lesson, we used Boolean conditions to decide whether a reading had the right format. Now the panel needs to choose a source. Adding sensor C gives us three channels, so the controller moves to a two-bit selector. That gives us four patterns for three channels. What should the interface deliver when the selector is 11?

Leaving that row out can make the panel display an old reading as if it belonged to the current selection. We’ll put the same 11 after two different channels and use the waveform to find where the old value came from.

How two inputs share one output

A multiplexer, or mux, connects several sources to one data path. With just A and B, sel_i needs one bit. A zero selects A. A one selects B. The data paths are eight bits wide, and the control signal chooses an entire group of eight.

assign data_o = sel_i ? b_i : a_i;

We can write the same binary selection rule procedurally with if/else, or enumerate both selector values with case. The continuous assignment drives a connection as its source changes. Inside a procedure, a blocking assignment with = completes before the next statement in the block. Both styles can describe combinational logic. The conditional operator, if and case handle X and Z in different ways. Their equivalence here assumes a known zero or one on the selector.[1]

Write all four rows for the three-channel interface

The expanded panel uses sel_i to select the reading currently on display. The interface carries an eight-bit data_o and a one-bit valid_o. The receiver displays the data only when valid_o is one. A sensor can legitimately measure zero, so testing the data for zero would also discard a real reading.

sel_iSourcedata_ovalid_o
00Aa_i1
01Bb_i1
10Cc_i1
11Unassigned8'h000
A two-bit selector controls three eight-bit inputs. Codes 00, 01 and 10 connect A, B and C; code 11 sends zero and clears valid.
Here the selector is on B. All three sensors can change while the output follows the selected channel.

Our panel ignores the data field when valid_o=0. The fourth row sets that field to a predictable value, zero, so it is recognizable during debugging. An interface that reserves a special value for “no sample” would need a different fourth row.

always_comb begin
    case (sel_i)
        2'b00: {valid_o, data_o} = {1'b1, a_i};
        2'b01: {valid_o, data_o} = {1'b1, b_i};
        2'b10: {valid_o, data_o} = {1'b1, c_i};
        default: {valid_o, data_o} = {1'b0, 8'h00};
    endcase
end

The braces concatenate the two fields. The first bit sets valid_o and the other eight set data_o. Plain case compares every position. Neither 2'bx1 nor 2'b1z matches a known binary item, so the RTL simulation takes default for both patterns. This result describes four-state simulation. Electrical behavior requires a separate model.[1]

An always_comb procedure runs once at simulation time zero. After that, the language derives which changes will make it run again, including applicable reads inside called functions. One procedure owns both outputs in this module, and every execution determines their current values. The older always @* can describe this same simple mux. We use always_comb to express the combinational intent, which the synthesis tool can check against the behavior.[1]

Which source wins when both request the panel?

Some controllers receive separate requests from A and B without receiving a selector. Suppose the panel must show A whenever A requests it, even when B is also requesting. This is a separate selection example from the three-channel decoder.

req_a_ireq_b_iCurrent output
00data_o=8'h00, valid_o=0
01B’s data, valid_o=1
10A’s data, valid_o=1
11A’s data, valid_o=1
always_comb begin
    if (req_a_i)        {valid_o, data_o} = {1'b1, a_i};
    else if (req_b_i)   {valid_o, data_o} = {1'b1, b_i};
    else                {valid_o, data_o} = {1'b0, 8'h00};
end

The first true condition wins. B gets the panel only if the A branch did not run. Swapping the conditions would give B priority in the last row. With two separate if statements using blocking assignments on both outputs, both statements would run when both requests are one. The later assignment would then determine the result. The overlap row makes the priority explicit.[1]

Why does the same 11 produce two answers?

Return to the three-channel decoder and remove just its default branch. Start with 01. B’s A5 appears on data_o and valid_o goes high. Now switch to 11. The case statement has no matching item, so this execution leaves the outputs alone. The panel still sees A5 marked as valid. Next select 00, which brings A’s 12 to the output. Apply the same 11 again and the panel keeps 12.

The selector runs through 01, 11, 00 and 11. The repaired decoder sends zero and clears valid both times 11 appears; the incomplete decoder keeps A5 and then 12, with valid high.
The first four intervals of the Icarus simulation each last 5 ns. The two identical `11` inputs produce different outputs in the incomplete decoder because they follow different selections.

A circuit preserving this behavior needs a way to hold data_o. The three assigned selector values act as an enable. While one is selected, a latch can pass the new data. When the selector reaches 11, the latch holds its previous value. Here the enable is a logic level determined by the selector. F07 will cover storage that samples on a clock edge.

Yosys 0.33 rejects the incomplete always_comb with a latch diagnostic. To inspect the structure, the lab uses always @* for the same incomplete assignments. That produces one eight-bit $dlatch driving data_o. All assigned branches give valid_o the value one, which Yosys optimizes to a constant one in this generic netlist. Read that structure separately from the initialization behavior of the source simulation.

The repair can live in a default branch. It can also assign both outputs to 00/0 at the start of the block and override them for the three legal selectors. Either way, trace every reachable path and check that it gives every output bit a value. Assigning zero to data_o while leaving valid_o unchanged would still violate the interface table.

Every path assigns an output, yet the output can still be stale

The second bug needs no branch. d_i is a one-bit input, and staged is an intermediate signal. The requirement says that y_o follows the current d_i. The block assigns both signals on every execution, but it reads staged before updating it.

always_comb begin
    y_o = staged;
    staged = d_i;
end

The first blocking assignment copies the old value of staged to y_o. Only the second line updates staged from the current input. SystemVerilog excludes an expression that the block also writes from its inferred sensitivity list. This write to staged therefore does not trigger a second pass through the block. Swapping the lines lets y_o read the value computed for the current input.[1][2]

The input d_i is 0, 1, 0. Reading before writing gives X, 0, 1 in RTL simulation. The repaired RTL and the synthesized connection for the incorrect RTL both give 0, 1, 0.
Both orderings execute every assignment. Putting the write first makes the next statement read the value just computed.

Icarus prints X → 0 → 1 for the incorrect source and 0 → 1 → 0 for the repair. In this example, Yosys implements both orders as a direct connection from d_i to y_o. When we run the same testbench against the emitted Verilog for the incorrect source, all three observations follow the current input. The replay exposes the difference between RTL simulation and the synthesized connection. The repaired example can assign staged and y_o in one block because it computes the dependency in order and both signals have a clear procedural driver. With no other use for the intermediate signal, assign y_o = d_i; expresses the direct connection.

What if an output feeds its own input?

In the missing-branch example, the output retained a value computed earlier. A combinational feedback path instead takes the output straight back to an input. Consider a mux with one branch connected to its own inverted output.

assign y_o = enable_i ? ~y_o : 1'b0;

With enable_i=0, the mux selects a constant zero. With enable_i=1, the circuit requires y_o=~y_o. If the output is zero, the expression demands one. If it is one, the expression demands zero. The binary model has no stable value that satisfies the equation. A zero-delay simulation may produce an unknown, keep reevaluating, or stop with a diagnostic. A simulated X does not measure the circuit’s physical delay or oscillation frequency.[2]

The mux selects zero or the inverse of y_o. When enable_i is one, the output loops through an inverter and back to the mux.
Following y_o through the inverter leads back to the same output. The synthesis tool finds that cycle in the logic model.

The feedback circuit is isolated from the other exercises. The Yosys scc pass finds one group of mutually dependent logic cells, and check -assert stops with a loop diagnostic. Each tool invocation has a 30-second limit. The lab runs no simulation on this feedback module.

Use the same inputs to check behavior and structure

Download the F04 RTL, tests and VCD files, then run bash run.sh from the extracted directory on Linux or WSL. The testbench supplies its own expected outputs from the interface table. It uses two sets of readings and exercises all 16 ordered pairs of binary selector values. It also tries two four-state patterns and changes channel B’s reading while the selector stays on B.

EvidenceObserved result
Three-channel selection, Icarus Verilog 12.0The repair differs from the table on 0 of 41 samples. The missing branch gives 12 mismatches.
Request priorityAll four cases match the table, including A winning when both requests are high.
Assignment order and netlist replayThe incorrect source fails all 3 samples. The repaired source and the netlist emitted from the incorrect source pass all 3.
Yosys 0.33 generic netlistsThe complete decoder has mux and decoding logic, with no latch or flip-flop. The incomplete always @* version has one eight-bit latch driving data_o.
Feedback diagnosticThe tool finds one loop and check -assert exits with an error.

See the tool output summary and the evidence manifest with SHA-256 hashes. These results come from RTL simulation, generic netlist replay and synthesis checks. The browser exercise demonstrates the same interface rules as a conceptual model. Physical propagation and board behavior would require a separate experiment.

Transfer exercise: When does a zero default break the interface?

An older panel connects to data_o alone. It displays 00 through FE as readings and reserves FF to mean “no sample.” The new decoder sets valid_o=0 on 11, but the old panel has no connection to that signal. What must the decoder send on data_o for 11? Design a test that shows what goes wrong if it sends 00.

Reference answer

Here data_o carries both the reading and the “no sample” status. The 11 row must send FF. First choose B with a legitimate reading of 00, then give the decoder 11. The panel should switch from a zero reading to “no sample.” If the default still sends 00, it keeps displaying a valid zero. This protocol reserves FF, so it cannot carry all 256 bit patterns as measurements. Supporting every pattern would require the receiver to learn the valid status through a separate part of its interface.

We found where the stale reading came from by specifying all four selection rows and approaching the same 11 from different histories. In the next lesson, the data path performs arithmetic. We will trace which bits carry the result of an addition and which ones determine a signed comparison.

Sources and further reading

  1. IEEE Std 1800-2017 covers always_comb in §9.2.2.2–9.2.2.2.2, assignments in §10.3–10.4, ?: in §11.4.11, and conditional and case statements in §12.4–12.5. Standard PDF hosted by an MIT course
  2. Tools. Verilator documents a static warning for reading a signal before writing it inside always_comb. Yosys documents logic-loop detection in check. Verilator ALWCOMBORDER · Yosys check · Icarus language modes

Select an unused channel

Channels A, B and C carry hexadecimal 12, A5 and 3C. Select B, then 11. Compare the complete case with the version missing an assignment.

    

Download RTL, testbench and results

Check your understanding

1. The selector is 10 and channel C currently carries 00. What does the panel receive?
2. A and B request the panel at the same time. The code tests if (req_a_i) before else if (req_b_i). Which source wins?
3. With default removed, apply 01, 11, 00, 11. B carries A5 and A carries 12. What are the two data_o values while 11 is selected?
4. An always_comb block executes y_o = staged before staged = d_i. If staged was 0 and d_i changes to 1, what does that execution assign to y_o?
5. What relation does assign y_o = enable_i ? ~y_o : 1'b0 impose when enable_i is 1?