One-Hot Validation
Part of the RTL Design & Verification section of Coddy's Verilog journey. Lesson 3 of 38.
One-hot data has exactly one asserted bit. A decoder generates such data, while validation checks incoming data before using it. For known binary values, a nonzero value is one-hot when bits & (bits - 1) is zero. This section does not use that shortcut to classify X or Z.
Relevant excerpt; surrounding declarations and connections are supplied in the challenge.
assign one_hot = (bits != 0) && ((bits & (bits - 1)) == 0);The nonzero check excludes idle; the bit trick rejects multiple set bits.
Distinguish zero, exactly one set bit and multiple set bits when validating requests.
Challenge
MediumFor known 4-bit input bits, set valid only when exactly one bit is high. Use the nonzero check together with bits & (bits - 1). Output columns: valid. All stimulus values are known binary values.
Complete design.v and preserve its module names and ports. The locked testbench.v supplies input changes and prints the outputs after they settle. It chooses a scenario using a simulator argument such as +CASE=1; no standard input is required. Do not add printing, delays or simulation termination to the design. Expected output is one row of decimal values per observation, separated by one space and ending with a newline.
Try it yourself
module dut (input [3:0] bits, output valid);
// Replace this placeholder with your design.
assign valid = 0;
endmoduleThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in RTL Design & Verification
1Reliable Combinational RTL
Defaults Prevent LatchesPriority EncodersOne-Hot ValidationSafe Case DecodingRecap - Request RouterPractice on your own: Online Verilog compiler