8

Is there any way to use pre-defined types from STD_LOGIC_1164 or STD_NUMERIC to represent an integer ranging from 0 to 2^32-1 ? (considering the default integer type ranges from -2^31-1 to 2^31-1)

I need to implement 32-bit counter and was looking for some way to save code using integer type instead of std_logic_vector.. Any design pattern for this ?

Or, better asked: Whats the best way to declare a 32-bit (unsigned) integer supporting the operations >/<, =, +-/ ?

Tahnks in advance

Edit1: One option I found was to declare a signal as std_logic_vector(31 downto 0), and to perform conversions when doing comparisons or +- operations.. ex: counter <= counter + std_logic_vector(unsigned(value) + 1).. Still haven't found a way to do division though (in case,for example, 1/4 of the value of counter is needed)

mbrandalero
  • 386
  • 1
  • 3
  • 15
  • Prefer numeric_std over std_logic_vector where you can. For division by 4 (or powers of 2) either look at the shift operators in numeric_std, or slice the vector yourself. output <= "00" & input(31 downto 2); –  Nov 19 '12 at 08:40

2 Answers2

9

Using the Integer types, you cannot (at least, not portably; there may be some VHDL tools that go beyond the minimum and offer a 64-bit integer)

Using IEEE.numeric_std, you can declare an Unsigned with a full 32-bit range (or 53-bit if you wish) and it should do everything you want.need, unless I misunderstand what you are asking.

  • This indeed solved the issue. I declared my signal as unsigned(31 downto 0) and now it supports the operations I needed (sometimes though using std_logic_vector(counter) to map to outputs). Thanks for the answer. – mbrandalero Nov 18 '12 at 23:21
  • 1
    How would one write large integers to a text file using write() and writeline()? Normally, I convert to integer like this to_integer(unsigned(sig1)). I can't believe we are using an HDL that cannot natively write big numbers to files. – Pedro_Uno Jul 20 '15 at 13:43
6
use ieee.numeric_std.all;

and then use the unsigned data type - this operates as a bit vector with mathematical operations defined for it. You can choose how many bits you'd like. For example:

signal mynum : unsigned(234 downto 0)
Martin Thompson
  • 16,395
  • 1
  • 38
  • 56