Complete the code to declare a 4-to-2 encoder module with inputs and outputs.
module encoder_4to2(input [3:0] [1], output reg [1:0] y); endmodule
The input vector is named data to represent the 4 input lines.
Complete the code to start the always block triggered by input changes.
always @([1]) begin
// encoding logic
end* causes incomplete sensitivity.The * means the always block triggers on any change of inputs.
Fix the error in the case statement selector to use the input vector.
case([1]) 4'b0001: y = 2'b00; 4'b0010: y = 2'b01; 4'b0100: y = 2'b10; 4'b1000: y = 2'b11; default: y = 2'b00; endcase
The case selector must be the input vector data to encode the input lines.
Fill both blanks to complete the module declaration and output port type.
module encoder_4to2([1] [3:0] data, [2] [1:0] y); endmodule
The input port uses input and the output port uses output reg because y is assigned inside an always block.
Fill all three blanks to complete the encoding logic with priority if multiple inputs are active.
always @(*) begin if (data[0]) y = [1]; else if (data[1]) y = [2]; else if (data[2]) y = [3]; else y = 2'b11; end
The encoder outputs the binary code of the first active input bit with priority from bit 0 to bit 3.
