-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtil_package.vhd
93 lines (62 loc) · 3.4 KB
/
Util_package.vhd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
-------------------------------------------------------------------------
-- Design unit: Util package
-- Description: Package with some general functions/procedures
-------------------------------------------------------------------------
library IEEE;
use IEEE.std_logic_1164.all;
package Util_package is
-- Converts an hexadecimal string digit into a 4 bits std_logic_vector.
function CharToStdLogicVector(digit: character) return std_logic_vector;
-- Converts an hexadecimal string number into a std_logic_vector.
-- The vector length depends on the number of string characteres (string length * 4).
function StringToStdLogicVector(value: string) return std_logic_vector;
end Util_package;
package body Util_package is
-- Converts an hexadecimal string digit into a std_logic_vector(3 downto 0).
function CharToStdLogicVector(digit: character) return std_logic_vector is
variable binaryDigit: std_logic_vector(3 downto 0);
begin
case (digit) is
when '0' => binaryDigit := "0000";
when '1' => binaryDigit := "0001";
when '2' => binaryDigit := "0010";
when '3' => binaryDigit := "0011";
when '4' => binaryDigit := "0100";
when '5' => binaryDigit := "0101";
when '6' => binaryDigit := "0110";
when '7' => binaryDigit := "0111";
when '8' => binaryDigit := "1000";
when '9' => binaryDigit := "1001";
when 'A' | 'a' => binaryDigit := "1010";
when 'B' | 'b' => binaryDigit := "1011";
when 'C' | 'c' => binaryDigit := "1100";
when 'D' | 'd' => binaryDigit := "1101";
when 'E' | 'e' => binaryDigit := "1110";
when 'F' | 'f' => binaryDigit := "1111";
when others => binaryDigit := "0000";
end case;
return binaryDigit;
end CharToStdLogicVector;
-- Converts an hexadecimal string number into a std_logic_vector.
-- The vector length depends on the number of string characteres (string length * 4).
function StringToStdLogicVector(value: string) return std_logic_vector is
variable binaryValue: std_logic_vector(value'length*4-1 downto 0);
variable vectorLength: integer;
variable high, low: integer;
begin
-- Each string digits is converted into a 4 bits binary digit.
vectorLength := value'length*4;
-- String characters range from 1 to n.
-- The left most character has the index 1.
-- Ex: value: string:= "5496" -> value(1) = 5, value(2) = 4, value(3) = 9, value(4) = 6
-- Converts the string number character by character.
-- The left most character is placed at the right most 4 bits in the std_logic_vector.
-- The right most character is placed at the left most 4 bits in the std_logic_vector.
for i in value'range loop
high := vectorLength - (4*(i-1)) - 1;
low := high-3;
binaryValue(high downto low) := CharToStdLogicVector(value(i));
end loop;
return binaryValue;
end StringToStdLogicVector;
end Util_package;