Challenge - 5 Problems
Encoder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this 4-to-2 priority encoder?
Given the following Verilog code for a 4-to-2 priority encoder, what is the output
y when the input in is 4'b1010?Verilog
module priority_encoder_4to2(input [3:0] in, output reg [1:0] y); always @(*) begin casez(in) 4'b1???: y = 2'b11; 4'b01??: y = 2'b10; 4'b001?: y = 2'b01; 4'b0001: y = 2'b00; default: y = 2'b00; endcase end endmodule
Attempts:
2 left
💡 Hint
Look at the highest priority bit set to 1 in the input.
✗ Incorrect
The input 4'b1010 has bit 3 and bit 1 set. The highest priority bit is bit 3 (leftmost). According to the casez, 4'b1??? matches and outputs 2'b11.
🧠 Conceptual
intermediateWhich statement about encoders is true?
Select the correct statement about a priority encoder.
Attempts:
2 left
💡 Hint
Think about what 'priority' means in this context.
✗ Incorrect
A priority encoder outputs the binary code corresponding to the highest priority input line that is active (logic 1).
🔧 Debug
advancedFind the error in this 8-to-3 encoder code
This Verilog code is intended to implement an 8-to-3 encoder. What error will it cause when synthesized or simulated?
Verilog
module encoder_8to3(input [7:0] in, output reg [2:0] y); always @(*) begin case(in) 8'b00000001: y = 3'b000; 8'b00000010: y = 3'b001; 8'b00000100: y = 3'b010; 8'b00001000: y = 3'b011; 8'b00010000: y = 3'b100; 8'b00100000: y = 3'b101; 8'b01000000: y = 3'b110; 8'b10000000: y = 3'b111; default: y = 3'bxxx; endcase end endmodule
Attempts:
2 left
💡 Hint
Consider what happens if more than one input bit is high.
✗ Incorrect
The code uses a case statement that matches only when exactly one bit is set. If multiple bits are set, no case matches, and the output is undefined or incorrect. This is not a priority encoder and will cause synthesis issues.
📝 Syntax
advancedWhich option fixes the syntax error in this encoder code?
This Verilog snippet has a syntax error. Which option fixes it?
Verilog
module encoder_4to2(input [3:0] in, output reg [1:0] y); always @(*) begin if (in[3]) y = 2'b11; else if (in[2]) y = 2'b10 else if (in[1]) y = 2'b01; else y = 2'b00; end endmodule
Attempts:
2 left
💡 Hint
Look carefully at the line endings.
✗ Incorrect
The line 'else if (in[2]) y = 2'b10' is missing a semicolon at the end, causing a syntax error.
🚀 Application
expertHow many output bits are needed for a 16-to-n encoder?
You want to design an encoder that takes 16 input lines and outputs the binary code of the active input. How many bits should the output have?
Attempts:
2 left
💡 Hint
Think about how many bits are needed to represent numbers from 0 to 15.
✗ Incorrect
To represent 16 different inputs, you need enough bits to count from 0 to 15, which requires 4 bits.
