|
|
|
|
|
|
|
|
|
|
|
|
Encoders
|
|
|
|
|
|
|
|
|
|
|
|
Encoder - Using if-else Statement
|
|
|
|
|
|
1 //-----------------------------------------------------
2 // Design Name : encoder_using_if
3 // File Name : encoder_using_if.v
4 // Function : Encoder using If
5 // Coder : Deepak Kumar Tala
6 //-----------------------------------------------------
7 module encoder_using_if(
8 binary_out , // 4 bit binary output
9 encoder_in , // 16-bit input
10 enable // Enable for the encoder
11 );
12 //-----------Output Ports---------------
13 output [3:0] binary_out ;
14 //-----------Input Ports---------------
15 input enable ;
16 input [15:0] encoder_in ;
17 //------------Internal Variables--------
18 reg [3:0] binary_out ;
19 //-------------Code Start-----------------
20 always @ (enable or encoder_in)
21 begin
22 binary_out = 0;
23 if (enable) begin
24 if (encoder_in == 16'h0002) begin
25 binary_out = 1;
26 end if (encoder_in == 16'h0004) begin
27 binary_out = 2;
28 end if (encoder_in == 16'h0008) begin
29 binary_out = 3;
30 end if (encoder_in == 16'h0010) begin
31 binary_out = 4;
32 end if (encoder_in == 16'h0020) begin
33 binary_out = 5;
34 end if (encoder_in == 16'h0040) begin
35 binary_out = 6;
36 end if (encoder_in == 16'h0080) begin
37 binary_out = 7;
38 end if (encoder_in == 16'h0100) begin
39 binary_out = 8;
40 end if (encoder_in == 16'h0200) begin
41 binary_out = 9;
42 end if (encoder_in == 16'h0400) begin
43 binary_out = 10;
44 end if (encoder_in == 16'h0800) begin
45 binary_out = 11;
46 end if (encoder_in == 16'h1000) begin
47 binary_out = 12;
48 end if (encoder_in == 16'h2000) begin
49 binary_out = 13;
50 end if (encoder_in == 16'h4000) begin
51 binary_out = 14;
52 end if (encoder_in == 16'h8000) begin
53 binary_out = 15;
54 end
55 end
56 end
57
58 endmodule
You could download file encoder_using_if.v here
|
|
|
|
|
|
Encoder - Using case Statement
|
|
|
|
|
|
1 //-----------------------------------------------------
2 // Design Name : encoder_using_case
3 // File Name : encoder_using_case.v
4 // Function : Encoder using Case
5 // Coder : Deepak Kumar Tala
6 //-----------------------------------------------------
7 module encoder_using_case(
8 binary_out , // 4 bit binary Output
9 encoder_in , // 16-bit Input
10 enable // Enable for the encoder
11 );
12 output [3:0] binary_out ;
13 input enable ;
14 input [15:0] encoder_in ;
15
16 reg [3:0] binary_out ;
17
18 always @ (enable or encoder_in)
19 begin
20 binary_out = 0;
21 if (enable) begin
22 case (encoder_in)
23 16'h0002 : binary_out = 1;
24 16'h0004 : binary_out = 2;
25 16'h0008 : binary_out = 3;
26 16'h0010 : binary_out = 4;
27 16'h0020 : binary_out = 5;
28 16'h0040 : binary_out = 6;
29 16'h0080 : binary_out = 7;
30 16'h0100 : binary_out = 8;
31 16'h0200 : binary_out = 9;
32 16'h0400 : binary_out = 10;
33 16'h0800 : binary_out = 11;
34 16'h1000 : binary_out = 12;
35 16'h2000 : binary_out = 13;
36 16'h4000 : binary_out = 14;
37 16'h8000 : binary_out = 15;
38 endcase
39 end
40 end
41
42 endmodule
You could download file encoder_using_case.v here
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Copyright © 1998-2014 |
Deepak Kumar Tala - All rights reserved |
Do you have any Comment? mail me at:deepak@asic-world.com
|
|