Encoder and Decoder
Encoder and Decoder
4:2 ENCODER
• An encoder is a digital circuit
that converts a set of binary
inputs into a unique binary
code.
• An encoder is a combinational
circuit that takes 2ⁿ input lines and
produces n output lines.
• 2ⁿ to n Encoder
2
Structural Modeling
module encoder4to2(
input D0, D1, D2, D3,
output Y1, Y0
);
initial begin
$display("Time | D3 D2 D1 D0 | Y1 Y0");
$monitor("%4dns | %b %b %b %b | %b %b", $time, D3, D2, D1, D0, Y1, Y0);
D3 = 0; D2 = 0; D1 = 0; D0 = 1; #10; // Expect Y1Y0 = 00
D3 = 0; D2 = 0; D1 = 1; D0 = 0; #10; // Expect Y1Y0 = 01
D3 = 0; D2 = 1; D1 = 0; D0 = 0; #10; // Expect Y1Y0 = 10
D3 = 1; D2 = 0; D1 = 0; D0 = 0; #10; // Expect Y1Y0 = 11
D3 = 1; D2 = 1; D1 = 0; D0 = 0; #10; // May still produce Y1Y0 = 11
$finish;
end
endmodule
Simulation
Dataflow Modeling
module encoder4to2 (
input [3:0] D,
output wire [1:0] Y
);
endmodule
IF STATEMENT :
endmodule
case statements
The case statement has a total of three variations: case, casex and casez. .
The case statement matches the value exactly against each case label.
If an exact match is not found, the default statement will be executed.
case (expression)
value1: statement1;
value2: statement2;
default: statement_default;
endcase
casex,casez: considers all x and z values as don’t care.
casex (opcode)
4'b01x1: action = 2;
default: action = 0;
endcase
end
Behavioral Modeling
module encoder4to2 (
input [3:0] D,
output reg [1:0] Y
);
● When multiple input lines are active high at the same time, then the
input that has the highest priority is considered first to generate the
output.
4:2 Priority Encoder
module priority_encoder_4to2 (
input D3, D2, D1, D0,
output reg Y1, Y0
);
endmodule
module tb_priority_encoder_4to2;
initial begin
$display("Time | D3 D2 D1 D0 | Y1 Y0");
$monitor("%4dns | %b %b %b %b | %b %b", $time, D3, D2, D1, D0, Y1, Y0);
{D3, D2, D1, D0} = 4'b0001; #10; // D0 -> 00
{D3, D2, D1, D0} = 4'b0010; #10; // D1 -> 01
{D3, D2, D1, D0} = 4'b0100; #10; // D2 -> 10
{D3, D2, D1, D0} = 4'b1000; #10; // D3 -> 11
• n to 2ⁿ Decoder
20
Structural Modeling
module decoder2to4 (
input a, b, e,
output Y0, Y1, Y2, Y3
);
module decoder2to4 (
input a, b, e,
output wire Y0, Y1, Y2, Y3
);
initial begin
$display("Time | e a b | Y0 Y1 Y2 Y3");
$monitor("%4dns | %b %b %b | %b %b %b %b", $time, e, a, b, Y0, Y1, Y2, Y3);
e = 1; a = 0; b = 0; #10; // Expect Y0 = 1
e = 1; a = 0; b = 1; #10; // Expect Y1 = 1
e = 1; a = 1; b = 0; #10; // Expect Y2 = 1
e = 1; a = 1; b = 1; #10; // Expect Y3 = 1
e = 0; a = 0; b = 0; #10;
e = 0; a = 0; b = 1; #10;
e = 0; a = 1; b = 0; #10;
e = 0; a = 1; b = 1; #10;
$finish;
end
endmodule
Simulation
Behavioral Modeling
module decoder2to4 (
input a, b, e,
output reg Y0, Y1, Y2, Y3
);
always @(*) begin
if (e) begin
case ({a, b})
2'b00: Y0 = 1;
2'b01: Y1 = 1;
2'b10: Y2 = 1; 25
2'b11: Y3 = 1;
endcase
end
end
endmodule
2 TO 4 DECODER
module decoder2_4(
input [1:0]in,
output [3:0]out
);
reg out;
always @ (*)
begin
case (in)
2'b00:out=4'b0001;
2'b01:out=4'b0010;
2'b10:out=4'b0100; 26
2'b11:out=4'b1000;
endcase
end
endmodule
2 TO 4 DECODER TEST BENCH
module decoder2_4_tb();
reg [1:0]in;
wire [3:0]out;
decoder2_4 dut(.in(in),.out(out));
initial
begin
in=2'b01;
#200;
in=2'b11; 27
#200
$stop;
end
endmodule
Full Adder using decoder
module full_adder_decoder (
input A, B, Cin,
output S, Cout
);
wire [7:0] m; // Decoder outputs m0 to m7
endmodule
module tb_full_adder_decoder;
reg A, B, Cin;
wire S, Cout;
full_adder_decoder uut (
.A(A), .B(B), .Cin(Cin),
.S(S), .Cout(Cout)
);
initial begin
$display("A B Cin | S Cout");
$monitor("%b %b %b | %b %b", A, B, Cin, S, Cout);
A = 0; B = 0; Cin = 0; #10;
A = 0; B = 0; Cin = 1; #10;
A = 0; B = 1; Cin = 0; #10;
A = 0; B = 1; Cin = 1; #10;
A = 1; B = 0; Cin = 0; #10;
A = 1; B = 0; Cin = 1; #10;
A = 1; B = 1; Cin = 0; #10;
A = 1; B = 1; Cin = 1; #10;
$finish;
end
endmodule
Simulation