[22] | 1 | -- Package File Template |
---|
| 2 | -- |
---|
| 3 | -- Purpose: This package defines supplemental types, subtypes, |
---|
| 4 | -- constants, and functions |
---|
| 5 | |
---|
| 6 | |
---|
| 7 | library IEEE; |
---|
| 8 | use IEEE.STD_LOGIC_1164.all; |
---|
| 9 | |
---|
| 10 | package conversions is |
---|
| 11 | |
---|
| 12 | |
---|
| 13 | -- type <new_type> is |
---|
| 14 | -- record |
---|
| 15 | -- <type_name> : std_logic_vector( 7 downto 0); |
---|
| 16 | -- <type_name> : std_logic; |
---|
| 17 | --end record; |
---|
| 18 | |
---|
| 19 | -- Declare constants |
---|
| 20 | |
---|
| 21 | --constant <constant_name> : time := <time_unit> ns; |
---|
| 22 | --constant <constant_name> : integer := <value>; |
---|
| 23 | |
---|
| 24 | -- Declare functions and procedure |
---|
| 25 | |
---|
| 26 | function to_uint ( a: in std_ulogic_vector) return integer; |
---|
| 27 | function to_vector (size: integer; val: integer) return std_ulogic_vector; |
---|
| 28 | |
---|
| 29 | end conversions; |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | package body conversions is |
---|
| 33 | |
---|
| 34 | -- Convert a std_ulogic_vector to an unsigned integer |
---|
| 35 | -- |
---|
| 36 | function to_uint (a: std_ulogic_vector) return integer is |
---|
| 37 | alias av: std_ulogic_vector (1 to a'length) is a; |
---|
| 38 | variable val: integer := 0; |
---|
| 39 | variable b: integer := 1; |
---|
| 40 | begin |
---|
| 41 | for i in a'length downto 1 loop |
---|
| 42 | if (av(i) = '1') then -- if LSB is '1', |
---|
| 43 | val := val + b; -- add value for current bit position |
---|
| 44 | end if; |
---|
| 45 | b := b * 2; -- Shift left 1 bit |
---|
| 46 | end loop; |
---|
| 47 | |
---|
| 48 | return val; |
---|
| 49 | end to_uint; |
---|
| 50 | |
---|
| 51 | -------------------------------------------------------- |
---|
| 52 | -- Convert an integer to a std_ulogic_vector |
---|
| 53 | -- |
---|
| 54 | function to_vector (size: integer; val: integer) return std_ulogic_vector is |
---|
| 55 | variable vec: std_ulogic_vector (1 to size); |
---|
| 56 | variable a: integer; |
---|
| 57 | begin |
---|
| 58 | a := val; |
---|
| 59 | for i in size downto 1 loop |
---|
| 60 | if ((a mod 2) = 1) then |
---|
| 61 | vec(i) := '1'; |
---|
| 62 | else |
---|
| 63 | vec(i) := '0'; |
---|
| 64 | end if; |
---|
| 65 | a := a / 2; |
---|
| 66 | end loop; |
---|
| 67 | return vec; |
---|
| 68 | end to_vector; |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | end conversions; |
---|