G1.8.1 One-Hot to Binary with a Validity Check

EasyImplementIcarus · browser samples / server checksReview the topic

Turn an 8-bit one-hot word into the index of its hot bit, and flag every word that is not one-hot.

Hardware context: The grant logger of an eight-way arbiter and the cursor logic of a UTF-8 serial terminal. Encode a one-hot grant word to an index and flag zero and multi-hot words.

An arbiter grants one of eight requesters with a one-hot word. A logging block needs the number of the granted requester. If the arbiter ever grants two at once, or none, the log must show it as invalid.

This is an Implement problem. Write onehot_enc. When hot has exactly one bit set, index is the position of that bit and valid is 1. For any other word, index is 0 and valid is 0.

The same arbiter has a debug mode that drives the grant word inverted. The output cold is 1 when exactly one bit of hot is 0: a one-cold word.

The check applies ten words, one per nanosecond, and records index, valid and cold after each.

Signals in the Timeline

SignalWidthRole
script80input of each check
Full specification
The module (keep the name and the ports)
module onehot_enc (
  input  logic [7:0] hot,     // should have exactly one bit set
  output logic [2:0] index,   // the position of that bit, or 0
  output logic       valid,   // 1 when hot is one-hot
  output logic       cold     // 1 when hot is one-cold: exactly one bit is 0
);
  // your design
endmodule
hotindexvalid
exactly one bit setthe position of that bit, 0 to 71
no bit set00
two or more bits set00

cold is 1 exactly when one bit of hot is 0 and the other seven are 1. It does not depend on valid.

The result fields are index_step0, valid_step0 and cold_step0 for the first word, up to the same names with step9.

Examples

Example 1

Low One-Hot Words
Input
hot 00000001; hot 00000010; hot 00000100; hot 00001000; hot 00000001; hot 00001000; hot 00000100; hot 00000010; hot 00000001; hot 00000010
Output
index_step0 = 0, valid_step0 = yes, cold_step0 = no, index_step1 = 1, valid_step1 = yes, cold_step1 = no, index_step2 = 2, valid_step2 = yes, cold_step2 = no, index_step3 = 3, valid_step3 = yes, cold_step3 = no, index_step4 = 0, valid_step4 = yes, cold_step4 = no, index_step5 = 3, valid_step5 = yes, cold_step5 = no, index_step6 = 2, valid_step6 = yes, cold_step6 = no, index_step7 = 1, valid_step7 = yes, cold_step7 = no, index_step8 = 0, valid_step8 = yes, cold_step8 = no, index_step9 = 1, valid_step9 = yes, cold_step9 = no

Each word has one bit set in the low nibble. The index is its position, 0 to 3, and valid is 1 in every step.

Example 2

Every One-Hot Word
Input
hot 00010000; hot 00100000; hot 01000000; hot 10000000; hot 00000001; hot 00000010; hot 00000100; hot 00001000; hot 10000000; hot 00010000
Output
index_step0 = 4, valid_step0 = yes, cold_step0 = no, index_step1 = 5, valid_step1 = yes, cold_step1 = no, index_step2 = 6, valid_step2 = yes, cold_step2 = no, index_step3 = 7, valid_step3 = yes, cold_step3 = no, index_step4 = 0, valid_step4 = yes, cold_step4 = no, index_step5 = 1, valid_step5 = yes, cold_step5 = no, index_step6 = 2, valid_step6 = yes, cold_step6 = no, index_step7 = 3, valid_step7 = yes, cold_step7 = no, index_step8 = 7, valid_step8 = yes, cold_step8 = no, index_step9 = 4, valid_step9 = yes, cold_step9 = no

Each word has one bit set, now in the high nibble too. The index runs up to 7 and valid is 1 throughout.

Example 3

The Empty Word Between Valid Ones
Input
hot 00000000; hot 00000100; hot 00000000; hot 01000000; hot 00000000; hot 00000001; hot 00000000; hot 10000000; hot 00000000; hot 00000000
Output
index_step0 = 0, valid_step0 = no, cold_step0 = no, index_step1 = 2, valid_step1 = yes, cold_step1 = no, index_step2 = 0, valid_step2 = no, cold_step2 = no, index_step3 = 6, valid_step3 = yes, cold_step3 = no, index_step4 = 0, valid_step4 = no, cold_step4 = no, index_step5 = 0, valid_step5 = yes, cold_step5 = no, index_step6 = 0, valid_step6 = no, cold_step6 = no, index_step7 = 7, valid_step7 = yes, cold_step7 = no, index_step8 = 0, valid_step8 = no, cold_step8 = no, index_step9 = 0, valid_step9 = no, cold_step9 = no

The word 0 has no hot bit. It gives index 0 and valid 0, which tells it apart from the word 0000_0001.

Constraints and Contract

  • Inputs are known 0/1 values. Each word is held for one nanosecond and the outputs are sampled at its end.
  • Keep the module name and the ports. The design is combinational.

Hints

Hint 1

Write valid first. A word is one-hot when it is not zero and clearing its lowest 1 leaves nothing.

Hint 2

For the index, a loop over the eight bits can remember the position of a set bit. Start from 0.

Hint 3

An encoder built only from OR gates mixes the indices of two hot bits. Use valid to force the index to 0. For cold, invert hot and apply the one-hot test.

What the grader checks: Nine directed and eight seeded cases of ten steps each run on the block. All three outputs are compared after every step with a model written in JavaScript.

17 checks on Icarus · browser samples / server checks. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 3 steps and the final capstone

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

Low One-Hot Words

Input
hot 00000001; hot 00000010; hot 00000100; hot 00001000; hot 00000001; hot 00001000; hot 00000100; hot 00000010; hot 00000001; hot 00000010
Expected output
index_step0 = 0, valid_step0 = yes, cold_step0 = no, index_step1 = 1, valid_step1 = yes, cold_step1 = no, index_step2 = 2, valid_step2 = yes, cold_step2 = no, index_step3 = 3, valid_step3 = yes, cold_step3 = no, index_step4 = 0, valid_step4 = yes, cold_step4 = no, index_step5 = 3, valid_step5 = yes, cold_step5 = no, index_step6 = 2, valid_step6 = yes, cold_step6 = no, index_step7 = 1, valid_step7 = yes, cold_step7 = no, index_step8 = 0, valid_step8 = yes, cold_step8 = no, index_step9 = 1, valid_step9 = yes, cold_step9 = no

Each word has one bit set in the low nibble. The index is its position, 0 to 3, and valid is 1 in every step.

Run samples executes all 3 public checks. Check all tests runs 17 checks on the server.

Icarus · browser samples / server checks