Check that a 5-bit word has exactly two 1s, and decode it with the weights 7, 4, 2, 1 and 0.
Hardware context: The receiver of a serial sensor link, a network card that drops packets, and a bar-code digit reader. Check and decode a 2-out-of-5 code word.
A sorting machine reads digits from a bar code where each digit is five bars and exactly two are tall. A dirty label can add or drop a tall bar. The reader must reject such a digit before it routes a parcel.
This is an Implement problem. Write two_of_five. valid is 1 exactly when two bits of w are set. digit is the decoded digit when w is valid and 0 otherwise.
The bits have the weights 7, 4, 2, 1 and 0, from bit 4 down to bit 0. The digit is the sum of the weights of the two set bits. The sum 11 (7 + 4) stands for the digit 0.
The check applies sixteen words, one per nanosecond, and records valid and digit after each.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
script | 80 | input of each check |
Full specification
module two_of_five (
input logic [4:0] w, // weights 7, 4, 2, 1, 0 from bit 4 down to bit 0
output logic valid, // 1 when exactly two bits of w are set
output logic [3:0] digit // the decoded digit, or 0 when w is not valid
);
// your design
endmodule
| w | valid | digit |
|---|---|---|
| exactly two bits set, weight sum 1 to 9 | 1 | the weight sum |
| exactly two bits set, weight sum 11 | 1 | 0 |
| zero, one, three, four or five bits set | 0 | 0 |
The result fields are valid_step0 and digit_step0 for the first word, up to valid_step15 and digit_step15.
Examples
Example 1
Code Words for 1, 2 and 0 Among Errors- Input
w 00011; w 00101; w 00111; w 00000; w 00011; w 00001; w 11000; w 11100; w 00101; w 00110; w 00011; w 01000; w 00101; w 00000; w 11000; w 00011
- Output
valid_step0 = yes, digit_step0 = 1, valid_step1 = yes, digit_step1 = 2, valid_step2 = no, digit_step2 = 0, valid_step3 = no, digit_step3 = 0, valid_step4 = yes, digit_step4 = 1, valid_step5 = no, digit_step5 = 0, valid_step6 = yes, digit_step6 = 0, valid_step7 = no, digit_step7 = 0, valid_step8 = yes, digit_step8 = 2, valid_step9 = yes, digit_step9 = 3, valid_step10 = yes, digit_step10 = 1, valid_step11 = no, digit_step11 = 0, valid_step12 = yes, digit_step12 = 2, valid_step13 = no, digit_step13 = 0, valid_step14 = yes, digit_step14 = 0, valid_step15 = yes, digit_step15 = 1
00011 is the digit 1 (weights 1 and 0) and 00101 is 2. 11000 is 7 + 4 = 11, which stands for 0. Every word with one, three or no set bits gives valid 0 and digit 0.
Example 2
Words with Three 1s- Input
w 00111; w 01011; w 01101; w 01110; w 10011; w 10101; w 10110; w 11001; w 11010; w 11100; w 00111; w 01011; w 10011; w 11100; w 01110; w 10101
- Output
valid_step0 = no, digit_step0 = 0, valid_step1 = no, digit_step1 = 0, valid_step2 = no, digit_step2 = 0, valid_step3 = no, digit_step3 = 0, valid_step4 = no, digit_step4 = 0, valid_step5 = no, digit_step5 = 0, valid_step6 = no, digit_step6 = 0, valid_step7 = no, digit_step7 = 0, valid_step8 = no, digit_step8 = 0, valid_step9 = no, digit_step9 = 0, valid_step10 = no, digit_step10 = 0, valid_step11 = no, digit_step11 = 0, valid_step12 = no, digit_step12 = 0, valid_step13 = no, digit_step13 = 0, valid_step14 = no, digit_step14 = 0, valid_step15 = no, digit_step15 = 0
Every word here has three set bits: one 0 turned into a 1. Each gives valid 0 and digit 0.
Example 3
Words with No, One, Four or Five 1s- Input
w 00000; w 00001; w 00010; w 00100; w 01000; w 10000; w 01111; w 10111; w 11011; w 11101; w 11110; w 11111; w 00000; w 10000; w 00001; w 11111
- Output
valid_step0 = no, digit_step0 = 0, valid_step1 = no, digit_step1 = 0, valid_step2 = no, digit_step2 = 0, valid_step3 = no, digit_step3 = 0, valid_step4 = no, digit_step4 = 0, valid_step5 = no, digit_step5 = 0, valid_step6 = no, digit_step6 = 0, valid_step7 = no, digit_step7 = 0, valid_step8 = no, digit_step8 = 0, valid_step9 = no, digit_step9 = 0, valid_step10 = no, digit_step10 = 0, valid_step11 = no, digit_step11 = 0, valid_step12 = no, digit_step12 = 0, valid_step13 = no, digit_step13 = 0, valid_step14 = no, digit_step14 = 0, valid_step15 = no, digit_step15 = 0
None of these has exactly two set bits, so every step gives valid 0 and digit 0.
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
Count the set bits first. Only a count of exactly 2 is valid.
Hint 2
Multiply each bit by its weight and add. Bit 0 has the weight 0, so it only counts toward the number of set bits.
Hint 3
Two cases need care: the sum 11, and any word that is not valid.
What the grader checks: Four cases mix code words with errors and two cases cover all 32 words. Two more repeat the code words and single errors, and six seeded cases follow. Both outputs are compared after every step with a model written in JavaScript.
14 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