ID 683283
Date 9/24/2018
Public

## 3.4.11.2. Manually Specifying Enumerated Types Using the enum_encoding Attribute

### Specifying an Arbitrary User Encoding for Enumerated Type

By default, the Intel® Quartus® Prime software one-hot encodes all enumerated types you defined. With the enum_encoding attribute, you can specify the logic encoding for an enumerated type and override the default one-hot encoding to improve the logic efficiency.

Note: If an enumerated type represents the states of a state machine, using the enum_encoding attribute to specify a manual state encoding prevents the Compiler from recognizing state machines based on the enumerated type. Instead, the Compiler processes these state machines as regular logic with the encoding specified by the attribute, and the Report window for your project does not list these states machines as state machines. If you want to control the encoding for a recognized state machine, use the State Machine Processing logic option and the syn_encoding synthesis attribute.

To use the enum_encoding attribute in a VHDL design file, associate the attribute with the enumeration type whose encoding you want to control. The enum_encoding attribute must follow the enumeration type definition, but precede its use. In addition, the attribute value should 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 the number of enumeration literals in your enumeration type. In addition, the encodings should 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.

In this example, 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";

### Encoded Enumeration Literals

This example shows the encoded enumeration literals:

apple   = "11" orange  = "01" pear    = "10" mango   = "00"

### Specifying the “gray” Encoding Style or Enumeration Type

Altera recommends that you specify an encoding style, rather than a manual user encoding, especially when the enumeration type has a large number of enumeration literals. The Intel® Quartus® Prime software can implement Enumeration Types with the different encoding styles, as shown in this table.

Table 17.  enum_encoding Attribute Values
Attribute Value Enumeration Types
"default" Use an encoding based on the number of enumeration literals in the enumeration type. If the number of literals are fewer than five, use the "sequential" encoding. If the number of literals are more than five, but fewer than 50 literals, use a "one-hot" encoding. Otherwise, use a "gray" encoding.
"sequential" Use a binary encoding in which the first enumeration literal in the enumeration type has encoding 0 and the second 1.
"gray" Use an encoding in which the encodings for adjacent enumeration literals differ by exactly one bit. An N-bit gray code can represent 2N values.
"johnson" Use an encoding similar to a gray code. An N-bit Johnson code can represent at most 2N states, but requires less logic than a gray encoding.
"one-hot" The default encoding style requiring N bits, in which N is the number of enumeration literals in the enumeration type.

In Specifying an Arbitrary User Encoding for Enumerated Type, the enum_encoding attribute manually specified a gray encoding for the enumeration type fruit. You can also concisely write this example by specifying the "gray" encoding style instead of a manual encoding, as shown in the following example:

type fruit is (apple, orange, pear, mango);
attribute enum_encoding : string;
attribute enum_encoding of fruit : type is "gray";