You are on page 1of 2

module encoder_16_4(

enable,
// tin hieu cho phep
binary_out, // 4 bit ngo ra
encoder_in // 16-bit ngo vao
);
//ngo vao
input enable;
input [15:0] encoder_in;
//ngo ra
output [3:0] binary_out;
//kieu ngo ra
reg [3:0] binary_out;
//chuong trinh chinh
always @ (enable or encoder_in)
begin
binary_out = 0;
if (enable) begin
case (encoder_in)
16'h0002 : binary_out = 4'd1;
16'h0004 : binary_out = 4'd2;
16'h0008 : binary_out = 4'd3;
16'h0010 : binary_out = 4'd4;
16'h0020 : binary_out = 4'd5;
16'h0040 : binary_out = 4'd6;
16'h0080 : binary_out = 4'd7;
16'h0100 : binary_out = 4'd8;
16'h0200 : binary_out = 4'd9;
16'h0400 : binary_out = 4'd10;
16'h0800 : binary_out = 4'd11;
16'h1000 : binary_out = 4'd12;
16'h2000 : binary_out = 4'd13;
16'h4000 : binary_out = 4'd14;
16'h8000 : binary_out = 4'd15;
endcase
end
end
endmodule

module tb_encoder_16_4;
//ngo vao gan gia tri test
reg enable;
reg [15:0] encoder_in;
//ngo ra quan sat
wire [3:0] binary_out;
//goi module test
encoder_16_4 encoder(
enable,
// tin hieu cho phep
binary_out, // 4 bit ngo ra
encoder_in // 16-bit ngo vao
);
//phan gan gia tri test
initial

begin
enable = 1'b0;
encoder_in = 16'h0001;
#500
enable = 1'b1;
#200
encoder_in = 16'h0002;
#200
encoder_in = 16'h0004;
#200
encoder_in = 16'h0008;
#200
encoder_in = 16'h0010;
#200
encoder_in = 16'h0020;
#200
encoder_in = 16'h0040;
#200
encoder_in = 16'h0080;
#200
encoder_in = 16'h0100;
#200
encoder_in = 16'h0200;
#200
encoder_in = 16'h0400;
#200
encoder_in = 16'h0800;
#200
encoder_in = 16'h1000;
#200
encoder_in = 16'h2000;
#200
encoder_in = 16'h4000;
#200
encoder_in = 16'h8000;
#200
encoder_in = 16'h1010;
end
endmodule

You might also like