enum_encoding VHDL Synthesis Attribute
A VHDL synthesis attribute that specifies the logic encoding for
an Enumeration Type. By default, the
Quartus® Prime software
"one-hot"
encodes all user-defined Enumeration Types. With this attribute,
you can override this default "one-hot"
encoding to improve the efficiency of the logic
Quartus® Prime
synthesizes for your Enumeration Type.
enum_encoding
attribute to specify a manual state encoding prevents
the Compiler from recognizing state machines based on the Enumeration Type. Instead,
the
Compiler processes these state machines as "regular" logic using the encoding specified
by the attribute, and they are not listed as state machines in the Report window for
the
project. If you wish to control the encoding for a recognized state machine, use the
State Machine Processing logic option or the
syn_encoding
attribute. For more information on properly
implementing state machines in VHDL, refer to Implementing State Machines.
To use the enum_encoding
attribute in a VHDL Design File (.vhd) Definition, first declare the attribute with a
string
type using an Attribute Declaration. Then,
associate the attribute with the Enumeration Type whose encoding
you wish to control. The enum_encoding
attribute must
follow the Enumeration Type Definition but precede its use. In
addition, the attribute value must be a string literal that
specifies either an arbitrary user encoding or an encoding style of
"default"
, "sequential"
,
"gray"
, "johnson"
, or
"one-hot"
.
An arbitrary user encoding consists of a space-delimited list of
encodings. The list must contain as many encodings as there are
enumeration literals in your Enumeration Type. In addition, the
encodings must all have the same length, and each encoding must
consist solely of values from the std_ulogic
type
declared by the std_logic_1164
package in the
IEEE
library. For example, in the following code
fragment, the enum_encoding
attribute specifies an
arbitrary user encoding for the Enumeration Type
fruit
.
type fruit is (apple, orange, pear, mango);
attribute enum_encoding : string;
attribute enum_encoding of fruit : type is "11 01 10 00";
In this example, the enumeration literals are encoded as follows:
apple = "11"
orange = "01"
pear = "10"
mango = "00"
Sometimes you wish to specify an encoding style, rather than a manual user encoding, especially when the Enumeration Type has a large number of enumeration literals. The Quartus® Prime software can implement Enumeration Types with four different encoding styles:
"default"
— Choose an encoding based on the number of states in the enumeration type. If there are fewer than five states, use thes"sequential"
encoding. If there are more than five but fewer than 50 states, use a"one-hot"
encoding. Otherwise, use a"gray"
encoding."sequential"
— Use a binary encoding in which the first state in the enumeration type has encoding 0, the second 1, and so on."gray"
— Use an encoding in which the encodings for adjacent states differ by exactly one bit. An M-bit"gray"
code can represent 2M states.- "
johnson
"— Use an M-bit encoding in which the encodings for adjacent states differ by exactly one bit. An M-bit"johnson"
code can represent at most 2 times M states but requires less logic than a"gray"
encoding. "one-hot"
— The default encoding style requiring N bits, where N is the number of states in the enumeration type.
Observe that in the previous example, the
enum_encoding
attribute manually specified a
"gray"
encoding
for the Enumeration Type fruit
. This example could be
written more concisely by specifying the "gray"
encoding style instead of a manual encoding:
type fruit is (apple, orange, pear, mango);
attribute enum_encoding : string;
attribute enum_encoding of fruit : type is "gray";