-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathpsi_common_logic_pkg.vhd
250 lines (217 loc) · 7.49 KB
/
psi_common_logic_pkg.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
------------------------------------------------------------------------------
-- Copyright (c) 2018 by Paul Scherrer Institute, Switzerland
-- All rights reserved.
-- Authors: Oliver Bruendler
------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
library work;
use work.psi_common_math_pkg.all;
------------------------------------------------------------------------------
-- Package Header
------------------------------------------------------------------------------
package psi_common_logic_pkg is
function zeros_vector(size : in natural) return std_logic_vector;
function ones_vector(size : in natural) return std_logic_vector;
function partially_ones_vector(size : in natural;
ones_nb : in natural)
return std_logic_vector;
function shift_left(arg : in std_logic_vector;
bits : in integer;
fill : in std_logic := '0')
return std_logic_vector;
function shift_right(arg : in std_logic_vector;
bits : in integer;
fill : in std_logic := '0')
return std_logic_vector;
function binary_to_gray(binary : in std_logic_vector)
return std_logic_vector;
function gray_to_binary(gray : in std_logic_vector)
return std_logic_vector;
-- Parallel Prefix Computation of the OR function
-- Input --> Output
-- 0100 --> 0111
-- 0101 --> 0111
-- 0011 --> 0011
-- 0010 --> 0011
function ppc_or(inp : in std_logic_vector)
return std_logic_vector;
function int_to_std_logic(int : in integer)
return std_logic;
function reduce_or(vec : in std_logic_vector)
return std_logic;
function reduce_and(vec : in std_logic_vector)
return std_logic;
function to_01X(inp : in std_logic)
return std_logic;
function to_01X(inp : in std_logic_vector)
return std_logic_vector;
function invert_bit_order(inp : in std_logic_vector)
return std_logic_vector;
end psi_common_logic_pkg;
------------------------------------------------------------------------------
-- Package Body
------------------------------------------------------------------------------
package body psi_common_logic_pkg is
-- *** ZerosVector ***
function zeros_vector(size : in natural) return std_logic_vector is
constant c : std_logic_vector(size - 1 downto 0) := (others => '0');
begin
return c;
end function;
-- *** OnesVector ***
function ones_vector(size : in natural) return std_logic_vector is
constant c : std_logic_vector(size - 1 downto 0) := (others => '1');
begin
return c;
end function;
-- *** PartiallyOnesVector ***
function partially_ones_vector(size : in natural;
ones_nb : in natural)
return std_logic_vector is
variable v_low : std_logic_vector(size downto 0);
variable v_high : std_logic_vector(size downto 0);
variable v_plus_1 : std_logic_vector(size + 1 downto 0); -- We need this to avoid synthesis problems with Xilinx ISE
variable v : std_logic_vector(size - 1 downto 0);
begin
v_low := (others => '1');
v_high := (others => '0');
v_plus_1 := v_high(size - ones_nb downto 0) & v_low(ones_nb downto 0);
v := v_plus_1(size downto 1);
return v;
end function;
-- *** ShiftLeft ***
function shift_left(arg : in std_logic_vector;
bits : in integer;
fill : in std_logic := '0')
return std_logic_vector is
constant argDt : std_logic_vector(arg'high downto arg'low) := arg;
variable v : std_logic_vector(argDt'range);
begin
if bits < 0 then
return shift_right(argDt, -bits, fill);
else
v(v'left downto bits) := argDt(argDt'left - bits downto argDt'right);
v(bits - 1 downto v'right) := (others => fill);
return v;
end if;
end function;
-- *** ShiftRight ***
function shift_right(arg : in std_logic_vector;
bits : in integer;
fill : in std_logic := '0')
return std_logic_vector is
constant argDt : std_logic_vector(arg'high downto arg'low) := arg;
variable v : std_logic_vector(argDt'range);
begin
if bits < 0 then
return shift_left(argDt, -bits, fill);
else
v(v'left - bits downto v'right) := argDt(argDt'left downto bits);
v(v'left downto v'left - bits + 1) := (others => fill);
return v;
end if;
end function;
-- *** BinaryToGray ***
function binary_to_gray(binary : in std_logic_vector)
return std_logic_vector is
variable Gray_v : std_logic_vector(binary'range);
begin
Gray_v := binary xor ('0' & binary(binary'high downto binary'low + 1));
return Gray_v;
end function;
-- *** GrayToBinary ***
function gray_to_binary(gray : in std_logic_vector)
return std_logic_vector is
variable Binary_v : std_logic_vector(gray'range);
begin
Binary_v(Binary_v'high) := gray(gray'high);
for b in gray'high - 1 downto gray'low loop
Binary_v(b) := gray(b) xor Binary_v(b + 1);
end loop;
return Binary_v;
end function;
-- *** PpcOr ***
function ppc_or(inp : in std_logic_vector)
return std_logic_vector is
constant Stages_c : integer := log2ceil(inp'length);
constant Pwr2Width_c : integer := 2**Stages_c;
type StageOut_t is array (natural range <>) of std_logic_vector(Pwr2Width_c - 1 downto 0);
variable StageOut_v : StageOut_t(0 to Stages_c);
variable BinCnt_v : unsigned(Pwr2Width_c - 1 downto 0);
begin
StageOut_v(0) := (others => '0');
StageOut_v(0)(inp'length - 1 downto 0) := inp;
for stage in 0 to Stages_c - 1 loop
BinCnt_v := (others => '0');
for idx in 0 to Pwr2Width_c - 1 loop
if BinCnt_v(stage) = '0' then
StageOut_v(stage + 1)(idx) := StageOut_v(stage)(idx) or StageOut_v(stage)((idx / (2**stage) + 1) * 2**stage);
else
StageOut_v(stage + 1)(idx) := StageOut_v(stage)(idx);
end if;
BinCnt_v := BinCnt_v + 1;
end loop;
end loop;
return StageOut_v(Stages_c)(inp'length - 1 downto 0);
end function;
function int_to_std_logic(int : in integer)
return std_logic is
begin
if int = 1 then
return '1';
elsif int = 0 then
return '0';
else
return 'X';
end if;
end function;
function reduce_or(vec : in std_logic_vector)
return std_logic is
variable tmp : std_logic;
begin
tmp := '0';
for i in vec'low to vec'high loop
tmp := tmp or vec(i);
end loop;
return tmp;
end function;
function reduce_and(vec : in std_logic_vector)
return std_logic is
variable tmp : std_logic;
begin
tmp := '1';
for i in vec'low to vec'high loop
tmp := tmp and vec(i);
end loop;
return tmp;
end function;
function to_01X(inp : in std_logic)
return std_logic is
begin
case inp is
when '0' | 'L' => return '0';
when '1' | 'H' => return '1';
when others => return 'X';
end case;
end function;
function to_01X(inp : in std_logic_vector)
return std_logic_vector is
variable tmp : std_logic_vector(inp'range);
begin
for i in inp'low to inp'high loop
tmp(i) := to_01X(inp(i));
end loop;
return tmp;
end function;
function invert_bit_order(inp : in std_logic_vector)
return std_logic_vector is
variable tmp : std_logic_vector(inp'range);
begin
for i in inp'low to inp'high loop
tmp(tmp'high - i) := inp(i);
end loop;
return tmp;
end function;
end package body;