I am trying to code a generic one-hot binary decoder in VHDL. The
following is the current code that I have come up with:
1 | library ieee;
|
2 | use ieee.std_logic_1164.all;
|
3 | use ieee.numeric_std.all;
|
4 |
|
5 | entity address_decoder is
|
6 | generic(
|
7 | NUM_BITS: natural := 3);
|
8 | port(
|
9 | input: in std_logic_vector(NUM_BITS - 1 downto 0);
|
10 | output: out std_logic_vector((2 ** NUM_BITS) - 1 downto 0));
|
11 | end address_decoder;
|
12 |
|
13 | architecture structural of address_decoder is
|
14 | begin
|
15 | G_OUTPUT: for i in 0 to (2 ** NUM_BITS) - 1 generate
|
16 | output(i) <= '1' when unsigned(input) = i else '0';
|
17 | end generate G_OUTPUT;
|
18 | end structural;
|
This code generates a comparator (is-equal) in series with a multiplexer
to choose between '0' or '1' according to the output of the comparator
for each of the final output bits, as in inferred from the 'when'
statement. However, if I use the following architecture, a decoder
component will be correctly inferred, but also a latch for the output
bits, which I presume is due to incomplete assignment to the output
vector.
1 | architecture structural of address_decoder is
|
2 | begin
|
3 | -- Compiler nags the choice must be a constant.
|
4 | --output <= (to_integer(unsigned(input)) => '1', others => '0');
|
5 |
|
6 | -- Try this incomplete assignment instead.
|
7 | output(to_integer(unsigned(input))) <= '1';
|
8 | end structural;
|
So my question is, if there is any way to produce only a primitive
decoder component without latched output? or the above series of
comparators and multiplexers are close enough equivalents of a binary
decoder in gate level terms?