Exp 3. Encoders & Decoders
Exp 3. Encoders & Decoders
Exp 3. Encoders & Decoders
: 3
Encoders and Decoders
DATE:
Aim: To simulate Encoders and decoders in behavioral, structural and data flow model of Verilog HDL.
Program:
4 to 2 line Encoder:
Behavioral Model
module encoder_4x2_b(
input [3:0] in,
output reg [1:0] out
);
endmodule
structural Model
module encoder_4x2_s(
input [3:0] in,
output [1:0] out);
or G1(out[1],in[3],in[2]);
or G2(out[0],in[3],in[1]);
endmodule
endmodule
Testbench:
module encoder_4x2_tb();
reg [3:0] in;
wire [1:0] out;
initial begin
$monitor("in = %b, out = %b", in, out);
in = 4'b1000; #10;
in = 4'b0100; #10;
in = 4'b0010; #10;
in = 4'b0001; #10;
in = 4'b0000; #10;
$finish;
end
endmodule
8 to 3 line Encoder:
module encoder_8x3_b(
);
case (in)
endcase
end
endmodule
module priority_encoder_8x3_b(
);
casex (in)
endcase
end
endmodule
Testbench:
module encoder_8x3_tb();
reg [7:0] in;
wire [2:0] out;
initial begin
$monitor("in = %b, out = %b", in, out);
in = 8'b10000000; #10;
in = 8'b01000000; #10;
in = 8'b00100000; #10;
in = 8'b00010000; #10;
in = 8'b00001000; #10;
in = 8'b00000100; #10;
in = 8'b00000010; #10;
in = 8'b00000001; #10;
in = 8'b00000000; #10;
$finish;
end
endmodule
8 to 3 Priority Encoder:
module priority_encoder_8x3_b(
input [7:0] in,
output reg [2:0] out
);
endmodule
Testbench:
module prio_encoder_8x3_tb();
reg [7:0] in;
wire [2:0] out;
initial begin
$monitor("in = %b, out = %b", in, out);
in = 8'b10000000; #10;
in = 8'b01000000; #10;
in = 8'b00100000; #10;
in = 8'b00010000; #10;
in = 8'b00001000; #10;
in = 8'b00000111; #10;
in = 8'b00000011; #10;
in = 8'b00000001; #10;
in = 8'b00000000; #10;
$finish;
end
endmodule
Decoders:
2 to 4 Decoder
Behavioral Model
structural Model
endmodule
data flow model
endmodule
Testbench:
module decoder_2x4_tb();
wire [3:0] out;
reg [1:0] in;
decoder_2x4_df M1(in, out);
initial begin
$monitor("in = %b, out = %b", in, out);
in = 2'b00; #10;
in = 2'b01; #10;
in = 2'b10; #10;
in = 2'b11; #10;
$finish;
end
endmodule
3 to 8 Decoder
case (in)
4'b0001: out = 8'b00000001;
4'b0011: out = 8'b00000010;
4'b0101: out = 8'b00000100;
4'b0111: out = 8'b00001000;
4'b1001: out = 8'b00010000;
4'b1011: out = 8'b00100000;
4'b1101: out = 8'b01000000;
4'b1111: out = 8'b10000000; //last bit is enable
default: out = 8'b11111111;
endcase
end
endmodule
Testbench:
module decoder_3x8_tb;
initial begin
$monitor("in = %b out = %b", in, out);
in = 4'b0001; #10;
in = 4'b0011; #10;
in = 4'b0101; #10;
in = 4'b0111; #10;
in = 4'b1001; #10;
in = 4'b1011; #10;
in = 4'b1101; #10;
in = 4'b1111; #10;
$finish;
end
endmodule
Testbench:
module t_decoder5to32;
reg [4:0] A;
wire [31:0] Y;
decoder5to32 uut(A, Y);
initial begin
$monitor("A = %b, Y = %b", A, Y);
A = 5'b00000; #10;
A = 5'b00001; #10;
A = 5'b00010; #10;
A = 5'b00011; #10;
A = 5'b00100; #10;
A = 5'b00101; #10;
A = 5'b00110; #10;
A = 5'b00111; #10;
A = 5'b01000; #10;
A = 5'b01001; #10;
A = 5'b01010; #10;
A = 5'b01011; #10;
A = 5'b01100; #10;
A = 5'b01101; #10;
A = 5'b01110; #10;
A = 5'b01111; #10;
A = 5'b10000; #10;
A = 5'b10001; #10;
A = 5'b10010; #10;
A = 5'b10011; #10;
A = 5'b10100; #10;
A = 5'b10101; #10;
A = 5'b10110; #10;
A = 5'b10111; #10;
A = 5'b11000; #10;
A = 5'b11001; #10;
A = 5'b11010; #10;
A = 5'b11011; #10;
A = 5'b11100; #10;
A = 5'b11101; #10;
A = 5'b11110; #10;
A = 5'b11111; #10;
$finish;
end
endmodule
Circuit diagram:
Encoder:
4 to 2 line Encoder:
Q0 = D1 | D3
8 to 3 line Encoder:
8 to 3 Priority Encoder:
Decoders:
2 to 4 Decoder
3 to 8 Decoder
5 to 32 line decoder using 2 to 4 line and 3 to 8 line decoder
Simulation Waveform:
4 to 2 line Encoder:
8 to 3 line Encoder:
8 to 3 Priority Encoder:
Decoders:
2 to 4 Decoder
3 to 8 Decoder
Thus the logic circuits for the adders and subtractors are designed in Verilog HDL and the output
combinations are verified in Modelsim.