3 To 8 Decoder Using Task and Function
3 To 8 Decoder Using Task and Function
module decoder38(
input en,
input [2:0]in,
output reg [7:0]out
);
task automatic decoder_38;
input en1;
input [2:0]in1;
output [7:0]out1;
begin
if(!en)
out1=0;
else
begin
case({in1})
0:out1=8'b0000_0001;
1:out1=8'b00000_0010;
2:out1=8'b0000_0100;
3:out1=8'b0000_1000;
4:out1=8'b0001_0000;
5:out1=8'b0010_0000;
6:out1=8'b0100_0000;
7:out1=8'b1000_0000;
default:out1=8'bxxxx_xxxx;
endcase
end
end
endtask
always @ (*)
decoder_38(en,in,out);
endmodule
################################################################################
##########
################### Using Function ####################################
module decoder38(
input en,
input [2:0]in,
output reg [7:0]out
);
function automatic [7:0] decoder_38;
input en1;
input [2:0]in1;
begin
if(!en)
decoder_38=0;
else
begin
case({in1})
0:decoder_38=8'b0000_0001;
1:decoder_38=8'b00000_0010;
2:decoder_38=8'b0000_0100;
3:decoder_38=8'b0000_1000;
4:decoder_38=8'b0001_0000;
5:decoder_38=8'b0010_0000;
6:decoder_38=8'b0100_0000;
7:decoder_38=8'b1000_0000;
default:decoder_38=8'bxxxx_xxxx;
endcase
end
end
endfunction
always@(*)
out=decoder_38(en,in);
endmodule
################################################################################
###
######################### Test Fixture(Same for both) ##########################
#
module test;
// Inputs
reg en;
reg [2:0] in;
// Outputs
wire [7:0] out;
// Instantiate the Unit Under Test (UUT)
decoder38 uut (
.en(en),
.in(in),
.out(out)
);
initial begin
// Initialize Inputs
en = 0;
in = 0;
#20 en=1;
#20 in=1;
#20 in=2;
#20 in=3;
#20 in=4;
#20 in=5;
#20 in=6;
#20 in=7;
#20 in=3'bx;
#20 in=3'bz;
// Wait 100 ns for global reset to finish
#20 $finish;
// Add stimulus here
end
endmodule
#######################################################################