Codiode

Ternary Operator Safety

MediumVerilog / SystemVerilogBuildFree

A missing else in an always @(*) block is the classic way to get an accidental latch. A ternary expression can't have that bug, because ? : always needs both branches.

A priority data router selects one of three 8-bit data inputs based on their respective valid flags. The router evaluates the valid flags in strict alphabetical priority. If valid_a is asserted, it outputs data_a. If valid_a is deasserted but valid_b is asserted, it outputs data_b. If only valid_c is asserted, it outputs data_c. If no valid flags are asserted, it outputs a fallback default_data value.

This circuit is purely combinational. There is no clock and no reset. Outputs must reflect input changes instantaneously. Priority logic dictates that a higher-priority valid flag completely overrides any lower-priority flags, ignoring their data entirely.

Signal Direction Width Description
valid_a input 1 Valid flag for data input A
valid_b input 1 Valid flag for data input B
valid_c input 1 Valid flag for data input C
data_a input 8 Data input A; highest priority
data_b input 8 Data input B; medium priority
data_c input 8 Data input C; lowest priority
default_data input 8 Fallback data when no valid flags are asserted
out_data output 8 Selected 8-bit output data

Constraints

  • The design must be purely combinational with no inferred latches.
  • You must resolve priority in the exact order: A (highest), then B, then C (lowest).
  • You must use a single continuous assignment (assign) with nested ternary operators (? :).
  • Do not use always blocks, if-else statements, or case statements.

Topics

combinationalroutingcontinuous-assignmentternary-operator

Solve this problem

Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against the problem's testbench. You get the waveform back, not a stored answer.

Free to solve. A Codiode account keeps your progress.

The circuit builder and code editor need a desktop screen. On a phone, read the problem here and open it on a laptop to solve.

Part of Synthesis Intuition

Problem 3 of 25 in the Synthesis Intuition track.

Related problems

Browse all problems · Verilog practice problems · Learning tracks

Solve this on a laptopSend yourself the link and continue there.