Complete the code to declare a 2-to-4 decoder module with inputs and outputs.
module decoder2to4([1]); input [1:0] in; output reg [3:0] out; endmodule
The module declaration requires the module name followed by the port list in parentheses. Option A correctly declares the module with ports in and out.
Complete the always block sensitivity list to trigger on input changes.
always @([1]) begin case(in) 2'b00: out = 4'b0001; 2'b01: out = 4'b0010; 2'b10: out = 4'b0100; 2'b11: out = 4'b1000; endcase end
The sensitivity list * means the always block triggers on any change of signals used inside, which is suitable for combinational logic like a decoder.
Fix the error in the output assignment inside the always block.
always @(*) begin case(in) 2'b00: out = 4'b0001; 2'b01: out = 4'b0010; 2'b10: out = 4'b0100; 2'b11: out = [1]; endcase end
The output for input 2'b11 should be 4'b1000 to set the fourth output bit high. Option C correctly sets this 4-bit value.
Fill both blanks to complete a 3-to-8 decoder output assignment with enable signal.
always @(*) begin if (enable == [1]) begin out = 8'b[2]; end else begin out = 8'b00000000; end end
The decoder output is active when enable is 1, so the condition is enable == 1. The output for input 0 is 8'b00000001 to set the first bit.
Fill all three blanks to complete a parameterized decoder module header and output declaration.
module decoder#(parameter N = [1])(input [N-1:0] in, output reg [[2]:0] out); always @(*) begin out = 1 << [3]; end endmodule
The parameter N is set to 3 for a 3-bit input. The output width is 2^N - 1 = 7 (bits 7 down to 0). The shift amount is the input in to activate the correct output bit.
