forked from xesscorp/VHDL_Lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Common.vhd
158 lines (130 loc) · 4.42 KB
/
Common.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
--**********************************************************************
-- Copyright (c) 1997-2014 by XESS Corp <http://www.xess.com>.
-- All rights reserved.
--
-- This library is free software; you can redistribute it and/or
-- modify it under the terms of the GNU Lesser General Public
-- License as published by the Free Software Foundation; either
-- version 3.0 of the License, or (at your option) any later version.
--
-- This library is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-- Lesser General Public License for more details.
--
-- You should have received a copy of the GNU Lesser General Public
-- License along with this library. If not, see
-- <http://www.gnu.org/licenses/>.
--**********************************************************************
----------------------------------------------------------------------------------
-- Commonly-used functions and constants.
----------------------------------------------------------------------------------
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
package CommonPckg is
constant YES : std_logic := '1';
constant NO : std_logic := '0';
constant HI : std_logic := '1';
constant LO : std_logic := '0';
constant ONE : std_logic := '1';
constant ZERO : std_logic := '0';
constant HIZ : std_logic := 'Z';
-- FPGA chip families.
type FpgaFamily_t is (SPARTAN3A_E, SPARTAN6_E);
-- XESS FPGA boards.
type XessBoard_t is (XULA_E, XULA2_E);
-- Convert a Boolean to a std_logic.
function BooleanToStdLogic(b : in boolean) return std_logic;
-- Find the base-2 logarithm of a number.
function Log2(v : in natural) return natural;
-- Select one of two integers based on a Boolean.
function IntSelect(s : in boolean; a : in integer; b : in integer) return integer;
-- Select one of two reals based on a Boolean.
function RealSelect(s : in boolean; a : in real; b : in real) return real;
-- Convert a binary number to a graycode number.
function BinaryToGray(b : in std_logic_vector) return std_logic_vector;
-- Convert a graycode number to a binary number.
function GrayToBinary(g : in std_logic_vector) return std_logic_vector;
-- Find the maximum of two integers.
function IntMax(a : in integer; b : in integer) return integer;
end package;
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
package body CommonPckg is
-- Convert a Boolean to a std_logic.
function BooleanToStdLogic(b : in boolean) return std_logic is
variable s : std_logic;
begin
if b then
s := '1';
else
s := '0';
end if;
return s;
end function BooleanToStdLogic;
-- Find the base 2 logarithm of a number.
function Log2(v : in natural) return natural is
variable n : natural;
variable logn : natural;
begin
n := 1;
for i in 0 to 128 loop
logn := i;
exit when (n >= v);
n := n * 2;
end loop;
return logn;
end function Log2;
-- Select one of two integers based on a Boolean.
function IntSelect(s : in boolean; a : in integer; b : in integer) return integer is
begin
if s then
return a;
else
return b;
end if;
return a;
end function IntSelect;
-- Select one of two reals based on a Boolean.
function RealSelect(s : in boolean; a : in real; b : in real) return real is
begin
if s then
return a;
else
return b;
end if;
return a;
end function RealSelect;
-- Convert a binary number to a graycode number.
function BinaryToGray(b : in std_logic_vector) return std_logic_vector is
variable g : std_logic_vector(b'range);
begin
for i in b'low to b'high-1 loop
g(i) := b(i) xor b(i+1);
end loop;
g(b'high) := b(b'high);
return g;
end function BinaryToGray;
-- Convert a graycode number to a binary number.
function GrayToBinary(g : in std_logic_vector) return std_logic_vector is
variable b : std_logic_vector(g'range);
begin
b(b'high) := g(b'high);
for i in g'high-1 downto g'low loop
b(i) := b(i+1) xor g(i);
end loop;
return b;
end function GrayToBinary;
-- Find the maximum of two integers.
function IntMax(a : in integer; b : in integer) return integer is
begin
if a > b then
return a;
else
return b;
end if;
return a;
end function IntMax;
end package body;