20

I am trying to create a few constants and assign hex numbers to them; however, I keep getting errors.

I want the constant FOO_CONST to be equal to 0x38

Like this...

constant FOO_CONST : integer := x"38";

The error:

Type integer does not match with a string literal

I've tried a few variants with no success.

3
  • 2
    Integers don't have bits. The hex value is a bit string literal with a string value equivalent assignable to single dimensional arrays. An integer requires an abstract literal, a based literal of the form 16#38#. Where 16 is the base, the '#' is a delimiter. See IEEE Std 1076-2008 15. Lexical elements, 15.5.3 Based literals. You could also use ieee.std_logic_1164.all; use ieee.numeric_std.all; and constant FOO_CONST : integer := to_integer(unsigned'(x"38"));converting a bit string literal to an integer specifying sign extension with the required qualified expression.
    – user1155120
    Aug 4, 2016 at 22:48
  • What you probably want is something like: constant FOO_CONST : unsigned(7 downto 0) := x"38"; One tends to use unsigned or std_logic_vector more often than integer in VHDL for synthesis. Aug 5, 2016 at 16:49
  • You could also use the # numeric literal. If you use it with 16 as the prefix, then it'll accept a base 16 number
    – Bettorun
    Aug 6, 2016 at 23:25

2 Answers 2

32

You can specify a base for integers by using the format base#value#:

constant FOO_CONST : integer := 16#38#;
8

In general, you can use literals in expressions as follows:

Numeric literals may be expressed in any base from 2 to 16. They may also be broken up using underscore, for clarity.

FOO_CONST_HEX <= 16#FF#;
FOO_CONST_BIN <= 2#1010_1010#;
FOO_CONST_BROKEN := 1_000_000.0; -- breaking the number using _

To answer the question clearly, you can do as Erasmus Cedernaes suggested:

constant FOO_CONST: integer:= 16#38#;

OR

constant FOO_CONST : std_logic_vector := X"38"; -- if you will convert it to a std_logic_vector later

Literals for arrays of characters, such as string, bit_vector and std_logic_vector are placed in double quotes:

constant FLAG :bit_vector(0 to 7) := "11111111";
constant MSG : string := "Hello";

Numeric literals with a decimal point are real, those without are integer;

constant FREEZE : integer := 32;
constant TEMP : real := 32.0;

Real numbers may be expressed in exponential form:

FACTOR := 2.2E-6;

Literals of type time (and other physical types) must have units. The units should be preceded by a space, although some tools may not require this:

constant DEL1 :time := 10 ns;
constant DEL2 :time := 2.27 us;

Literals of enumerated types may either be characters (as for bit and std_logic), or identifiers:

type MY_LOGIC is ('X','0','1','Z');
type T_STATE is (IDLE, READ, END_CYC);
signal CLK : MY_LOGIC := '0';
signal STATE : T_STATE := IDLE;

Bit vector literals may be expressed in binary (default), octal or hex. They may also contain embedded underscores for clarity. These forms may not be used as std_logic_vector literals:

BIT_8_BUS <= B"1111_1111";
BIT_9_BUS <= O"353";
BIT_16_BUS <= X"AA55";

Notice that:

Literals are supported for synthesis, providing they are of a type acceptable to the logic synthesis tool. They are either synthesized as connections to logic '1' or '0' or are used to help minimize the number of gates required.

Reference

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.