This component includes the verified RTL Verilog code for some encoders and decoders widely used. The collection includes priority encoders, thermometer decoders, grey code and more linke
thermometer decoder
The thermometer decoder decodes a binary number into a thermometer vector with the number of bits set to 1 equal the input binary starting at bit 0
grey code
The Grey code is a binary code where two successive values differ in only one bit. The code is designed to prevent spurious output from potentially meta-stable circuit nodes and is used widely for clock domain synchronizers.
One-hot decoder
Priority encoder
Priority encoder serves to find the first bit which is set in a vector and return its position as binary number.
Collection Content
Thermometer decoder
The thermometer decoder decodes a binary number into a thermometer vector with the number of bits set to 1 equal the input binary starting at bit 0
For example a thermometer of 8 bits would have the following truth table:
binary |
thermometer |
3’b000 |
8’b00000000 |
3’b001 |
8’b00000001 |
3’b010 |
8’b00000011 |
3’b011 |
8’b00000111 |
3’b100 |
8’b00001111 |
3’b101 |
8’b00011111 |
3’b110 |
8’b00111111 |
3’b111 |
8’b01111111 |
There are other types of thermometer representations using inversed bits or starting with a 1 at the lowest bit when the binary is 0. For example, a PPC computation would use and inverse thermometer decoder.
Signal name |
Direction/width |
description |
Bin_in |
Input [WIDTH -1:0] |
Binary decoder input |
Thermo_out |
Output Log(2) of WIDTH rounded up |
Thermometer decoder output |
One-hot to binary decoder
The one-hot to binary decoder, takes as input a one-hot vector and returns the number of the bit which is set.
For example a one-hot 2 binary decoder of 8 bits would have the following truth table:
One-hot |
binary |
8’b00000001 |
3’b000 |
8’b00000010 |
3’b001 |
8’b00000100 |
3’b010 |
8’b00001000 |
3’b011 |
8’b00010000 |
3’b100 |
8’b00100000 |
3’b101 |
8’b01000000 |
3’b110 |
8’b10000000 |
3’b111 |
Signal name |
Direction/width |
description |
onehot_in |
Input [WIDTH -1:0] |
One-hot input |
bin_out |
Output Log(2) of WIDTH rounded up |
Binary decoded output |
The opposite encoding of a binary into a onehot vector can be accomplished using the below Verilog code, care must be taken to restrict the value of the binary number to the number of bits available in the one-hot output to prevent unexpected results.
// binary to one-hot encoding
assign onehot_out = ('b1 << bin_in);
//
Priority encoder
Priority encoder serves to find the first bit which is set in a vector and return its position as binary number. The following truth table shows an 8 bit priority encoder functionality:
In |
Binary out |
8’b???????1 |
3’b000 |
8’b??????10 |
3’b001 |
8’b?????100 |
3’b010 |
8’b????1000 |
3’b011 |
8’b???10000 |
3’b100 |
8’b??100000 |
3’b101 |
8’b?1000000 |
3’b110 |
8’b10000000 |
3’b111 |
Multiple bits may be asserted but the encoder would ignore all except the lowest one which is asserted.
The priority order may be different, so the highest priority bit is the upper bit of the vector, depending on the application.
Grey code encoder and decoder
The Grey code is a binary code where two successive values differ in only one bit. The code is designed to prevent spurious output from potentially meta-stable circuit nodes and is used widely for clock domain synchronizers. The construction of a grey code is built on a mirroring principle also called reflect-and-prefix where each size of the code is built from the previous by mirroring and adding a prefix of 0 to the first part and 1 to the second. This method of cons
truction makes the code cyclic and therefore suitable for cyclic counter applications such as asynchronous FIFO pointers.
For more details on the theoretical aspects of the grey code, please see Wikipedia page under Grey code
The construction of a grey code from binary follows this rule:
grey = bin ^ bin/2
For example:
Dec |
Binary |
binary/2 |
Gray |
0 |
000 |
000 |
000 |
1 |
001 |
000 |
001 |
2 |
010 |
001 |
011 |
3 |
011 |
001 |
010 |
4 |
100 |
010 |
110 |
5 |
101 |
010 |
111 |
6 |
110 |
011 |
101 |
7 |
111 |
011 |
100 |
The decoding of a grey into binary follows a more complex computation where each binary bit is the XOR of all the grey code bits higher than it in the grey code vector
Using the above example:
// for a grey = 'b111
Binary[0] = Grey[2] ^ Grey[1] ^ Grey[0] = 1 ^ 1 ^ 1 = 1
Binary[1] = Grey[1] ^ Grey[1] = 1 ^ 1 = 0
Binary[2] = Grey[2] = 1 = 0
Binary = 'b101 (5)
//