forked from owocomm-0/fpga-fft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfft_types.vhd
353 lines (295 loc) · 9.75 KB
/
fft_types.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
library ieee;
use ieee.numeric_std.all;
use ieee.std_logic_1164.all;
package fft_types is
attribute delay: integer;
-- GLOBAL PARAMETERS
-- maximum supported bit width of the signed integers in a complex value
constant COMPLEXWIDTH: integer := 48;
-- use an additional output register when the address width of the
-- ram inside a transposer or reorderBuffer is at least this value
constant TRANSPOSER_OREG_THRESHOLD: integer := 7;
-- use two additional output registers when the address width of the
-- ram inside a transposer or reorderBuffer is at least this value
constant TRANSPOSER_OREG2_THRESHOLD: integer := 10;
-- use BRAM instead of LUTRAM when the address width of the
-- ram inside a transposer or reorderBuffer is at least this value
constant TRANSPOSER_BRAM_THRESHOLD: integer := 7;
-- the following constants apply to AXI large FFT and may be overridden by
-- generic parameters passed to the instance.
-- which bit of din_tuser controls twiddle pre-multiply enable
constant AXIFFT_FLAG_TWIDDLE_MULTIPLY: integer := 2;
-- which bit of din_tuser controls input burst transpose (reorders words within a burst)
constant AXIFFT_FLAG_INPUT_BURST_TRANSPOSE: integer := 3;
-- which bit of din_tuser controls output burst transpose (reorders words within a burst)
constant AXIFFT_FLAG_OUTPUT_BURST_TRANSPOSE: integer := 4;
attribute relDelay: integer;
type complex is record
re: signed(COMPLEXWIDTH-1 downto 0);
im: signed(COMPLEXWIDTH-1 downto 0);
end record;
function "+" (Left, Right: complex) return complex;
function "-" (Left, Right: complex) return complex;
function "-" (Right: complex) return complex;
function "*" (Left: complex; Right: integer) return complex;
function "/" (Left: complex; Right: integer) return complex;
function to_complex (re,im: integer) return complex;
function to_complex (re,im: signed) return complex;
function complex_re(val: complex; bits: integer) return signed;
function complex_im(val: complex; bits: integer) return signed;
function complex_pack(val: complex; bits: integer) return std_logic_vector;
function complex_unpack(val: std_logic_vector) return complex;
function saturate (val: complex; bits: integer) return complex;
function keepNBits (val: complex; bits: integer) return complex;
function shift_left(val: complex; N: integer) return complex;
function shift_right(val: complex; N: integer) return complex;
function complex_swap(val: complex) return complex;
function rotate_quarter(val: complex) return complex;
function rotate_mquarter(val: complex) return complex;
function round_convergent(val: complex; enable: std_logic; position: integer) return complex;
type complexArray is array(integer range<>) of complex;
function reverse_bits(val: unsigned) return unsigned;
function reverse_bits(val, bits: integer) return integer;
-- returns ceil(log2(val))
function ceilLog2(val: integer) return integer;
function complex_str(val: complex) return String;
function iif(Cond: BOOLEAN; If_True, If_False: integer) return integer;
type scalingModes is (SCALE_DIV_N, SCALE_DIV_SQRT_N, SCALE_NONE);
function scalingShift(scale: scalingModes; order: integer) return integer;
function fft_spdf_halfstage_delay(N: integer; butterfly2: boolean) return integer;
function fft_spdf_stage_delay(N: integer) return integer;
end package;
package body fft_types is
function "+" (Left, Right: complex) return complex is
variable res: complex;
begin
res.re := Left.re + Right.re;
res.im := Left.im + Right.im;
return res;
end function;
function "-" (Left, Right: complex) return complex is
variable res: complex;
begin
res.re := Left.re - Right.re;
res.im := Left.im - Right.im;
return res;
end function;
function "-" (Right: complex) return complex is
variable res: complex;
begin
res.re := -Right.re;
res.im := -Right.im;
return res;
end function;
function "*" (Left: complex; Right: integer) return complex is
variable res: complex;
begin
res.re := Left.re * Right;
res.im := Left.im * Right;
return res;
end function;
function "/" (Left: complex; Right: integer) return complex is
variable res: complex;
begin
res.re := Left.re / Right;
res.im := Left.im / Right;
return res;
end function;
function to_complex (re,im: integer) return complex is
variable res: complex;
begin
res.re := to_signed(re, COMPLEXWIDTH);
res.im := to_signed(im, COMPLEXWIDTH);
return res;
end function;
function to_complex (re,im: signed) return complex is
variable res: complex;
begin
res.re := resize(re, COMPLEXWIDTH);
res.im := resize(im, COMPLEXWIDTH);
return res;
end function;
function complex_re(val: complex; bits: integer) return signed is
begin
return val.re(bits-1 downto 0); --to_signed(val.re, bits);
end function;
function complex_im(val: complex; bits: integer) return signed is
begin
return val.im(bits-1 downto 0); --to_signed(val.im, bits);
end function;
function complex_pack(val: complex; bits: integer) return std_logic_vector is
begin
return std_logic_vector(val.im(bits-1 downto 0))
& std_logic_vector(val.re(bits-1 downto 0));
end function;
function complex_unpack(val: std_logic_vector) return complex is
variable dataBits: integer := val'length / 2;
begin
return to_complex(signed(val(dataBits-1 downto 0)),
signed(val(val'left downto dataBits)));
end function;
function saturate(val: complex; bits: integer) return complex is
variable res: complex;
--variable max1: integer := (2**(bits-1))-1;
--variable min1: integer := 1-(2**(bits-1));
variable max1: signed(COMPLEXWIDTH-1 downto 0) := to_signed((2**(bits-1))-1, COMPLEXWIDTH);
variable min1: signed(COMPLEXWIDTH-1 downto 0) := to_signed((2**(bits-1))-1, COMPLEXWIDTH);
begin
res := val;
if(res.re > max1) then
res.re := max1;
end if;
if(res.re < min1) then
res.re := min1;
end if;
if(res.im > max1) then
res.im := max1;
end if;
if(res.im < min1) then
res.im := min1;
end if;
return res;
end function;
function keepNBits(val: complex; bits: integer) return complex is
variable re1, im1: signed(COMPLEXWIDTH-1 downto 0);
variable res: complex;
begin
re1 := val.re; --to_signed(val.re, 32);
im1 := val.im; --to_signed(val.im, 32);
res.re := resize(re1(bits-1 downto 0), COMPLEXWIDTH);
res.im := resize(im1(bits-1 downto 0), COMPLEXWIDTH);
return res;
end function;
function shift_left(val: complex; N: integer) return complex is
variable res: complex;
begin
res.re := shift_left(val.re, N);
res.im := shift_left(val.im, N);
return res;
end function;
function shift_right(val: complex; N: integer) return complex is
variable res: complex;
begin
res.re := shift_right(val.re, N);
res.im := shift_right(val.im, N);
return res;
end function;
function complex_swap(val: complex) return complex is
variable res: complex;
begin
res.im := val.re;
res.re := val.im;
return res;
end function;
function rotate_quarter(val: complex) return complex is
variable res: complex;
begin
res.im := val.re;
res.re := -val.im;
return res;
end function;
function rotate_mquarter(val: complex) return complex is
variable res: complex;
begin
res.im := -val.re;
res.re := val.im;
return res;
end function;
function round_convergent(val: complex; enable: std_logic; position: integer) return complex is
variable res: complex;
variable c1, c2: signed(position+1 downto 0);
begin
if position = -1 then
return val;
end if;
c1 := "0" & enable & (position-1 downto 0=>'0');
c2 := "00" & (position-1 downto 0=>enable);
if val.re(position+1) = '1' then
res.re := val.re + c1;
else
res.re := val.re + c2;
end if;
if val.im(position+1) = '1' then
res.im := val.im + c1;
else
res.im := val.im + c2;
end if;
return res;
end function;
function reverse_bits(val: unsigned) return unsigned is
variable res: unsigned(val'RANGE);
begin
for i in val'RANGE loop
res(i) := val(val'left + val'right - i);
end loop;
return res;
end function;
function reverse_bits(val, bits: integer) return integer is
begin
return to_integer(reverse_bits(to_unsigned(val, bits)));
end function;
function ceilLog2(val: integer) return integer is
variable tmp: integer;
begin
for I in 0 to 32 loop
tmp := 2**I;
if tmp >= val then
return I;
end if;
end loop;
return 32;
end function;
function complex_str(val: complex) return String is
begin
return integer'image(to_integer(val.re))
& " "
& integer'image(to_integer(val.im));
end function;
function iif(Cond: BOOLEAN; If_True, If_False: integer) return integer is
begin
if (Cond = TRUE) then
return(If_True);
else
return(If_False);
end if;
end function iif;
function scalingShift(scale: scalingModes; order: integer) return integer is
begin
if scale=SCALE_DIV_N then
return order;
elsif scale=SCALE_DIV_SQRT_N then
return order/2;
else
return 0;
end if;
end function;
function fft_spdf_halfstage_delay(N: integer; butterfly2: boolean) return integer is
begin
return iif(butterfly2, 2**(N-2), 2**(N-1)) + 2 + 1;
end function;
function fft_spdf_stage_delay(N: integer) return integer is
begin
return fft_spdf_halfstage_delay(N, true) + fft_spdf_halfstage_delay(N, false);
end function;
end package body;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
use work.fft_types.all;
-- shift register of len stages
entity sr_complex is
generic(len: integer := 8);
Port (clk : in STD_LOGIC;
din : in complex;
dout : out complex);
end;
architecture a of sr_complex is
type arr_t is array(len downto 0) of complex;
signal arr: arr_t;
begin
g: for I in 0 to len-1 generate
arr(I) <= arr(I+1) when rising_edge(clk);
end generate;
arr(len) <= din;
dout <= arr(0);
end a;