0% found this document useful (0 votes)
684 views

3 To 8 Decoder Using Task and Function

The document describes a module that implements a 3-bit to 8-bit decoder using both a task and a function. The module takes in an enable signal and 3-bit input and outputs an 8-bit code. The task and function implementations contain identical logic that decodes the 3-bit input into a unique 8-bit output code depending on the input value. A test fixture is also provided that applies different input patterns and checks the output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
684 views

3 To 8 Decoder Using Task and Function

The document describes a module that implements a 3-bit to 8-bit decoder using both a task and a function. The module takes in an enable signal and 3-bit input and outputs an 8-bit code. The task and function implementations contain identical logic that decodes the 3-bit input into a unique 8-bit output code depending on the input value. A test fixture is also provided that applies different input patterns and checks the output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

################### Using Task #####################

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
#######################################################################

You might also like