View difference between Paste ID: Rtyc4RiH and CqCur39F
SHOW: | | - or go back to the newest paste.
1
-- --------------------------------------------------------
2
-- Generic 8 point DIF FFT algorithm using a register
3
-- array for data and coefficients
4
5
PACKAGE n_bits_int IS -- User defined types
6
SUBTYPE U9 IS INTEGER RANGE 0 TO 2**9-1;
7
SUBTYPE S16 IS INTEGER RANGE -2**15 TO 2**15-1;
8
SUBTYPE S32 IS INTEGER RANGE -2147483647 TO 2147483647;
9
TYPE ARRAY0_7S16 IS ARRAY (0 TO 7) of S16;
10
TYPE ARRAY0_255S16 IS ARRAY (0 TO 15) of S16;
11
TYPE ARRAY0_127S16 IS ARRAY (0 TO 7) of S16;
12
TYPE STATE_TYPE IS
13
(start, load, calc, update, reverse, done);
14
END n_bits_int;
15
LIBRARY work; USE work.n_bits_int.ALL;
16
LIBRARY ieee; USE ieee.std_logic_1164.ALL;
17
USE ieee.std_logic_arith.ALL;
18
USE ieee.std_logic_signed.ALL;
19
20
-- --------------------------------------------------------
21
ENTITY fft256 IS ------> Interface
22
	PORT  (clk, reset : IN STD_LOGIC; -- Clock and reset
23
		    xr_in, xi_in : IN S16; -- Real and imag. input
24
			fft_valid : OUT STD_LOGIC; -- FFT output is valid
25
			fftr, ffti : OUT S16; -- Real and imag. output
26
			rcount_o : OUT U9; -- Bitreverese index counter
27
			xr_out, xi_out : OUT ARRAY0_7S16; -- First 8 reg. files
28
			stage_o, gcount_o : OUT U9; --Stage and group count
29
			i1_o, i2_o : OUT U9; -- (Dual) data index
30
			k1_o, k2_o : OUT U9; -- Index offset
31
			w_o, dw_o : OUT U9; -- Cos/Sin (increment) angle
32
			wo : OUT U9); -- Decision tree location loop FSM
33
END fft256;
34
-- --------------------------------------------------------
35
36
ARCHITECTURE fpga OF fft256 IS
37
	SIGNAL s : STATE_TYPE; -- State machine variable
38
	CONSTANT N : U9 := 16; -- Number of points
39
	CONSTANT ldN : U9 := 4; -- Log_2 number of points
40
	-- Register array for 16 bit precision:
41
	SIGNAL xr, xi : ARRAY0_255S16:=(others=>0);
42
	SIGNAL w : U9 := 0;
43
	-- sine and cosine coefficient arrays
44
-- --------------------------------------------------------
45
	CONSTANT cos_rom : ARRAY0_127S16 := (16384,15136,11585,6270,0,-6270,-11585,-15136);
46
47
--	CONSTANT cos_rom : ARRAY0_127S16 := (16384,16379,16364,16340
48
--	,16305,16261,16207,16143,16069,15986,15893,15791,15679,
49
--	15557,15426,15286,15137,14978,14811,14635,14449,14256,
50
--	14053,13842,13623,13395,13160,12916,12665,12406,12140,
51
--	11866,11585,11297,11003,10702,10394,10080,9760,9434,9102,
52
--	8765,8423,8076,7723,7366,7005,6639,6270,5897,5520,5139,
53
--	4756,4370,3981,3590,3196,2801,2404,2006,1606,1205,804,402
54
--	,0,-402,-804,-1205,-1606,-2006,-2404,-2801,-3196,-3590,
55
--	-3981,-4370,-4756,-5139,-5520,-5897,-6270,-6639,-7005,
56
--	-7366,-7723,-8076,-8423,-8765,-9102,-9434,-9760,-10080,
57
--	-10394,-10702,-11003,-11297,-11585,-11866,-12140,-12406,
58
--	-12665,-12916,-13160,-13395,-13623,-13842,-14053,-14256,
59
--	-14449,-14635,-14811,-14978,-15137,-15286,-15426,-15557,
60
--	-15679,-15791,-15893,-15986,-16069,-16143,-16207,-16261,
61
--	-16305,-16340,-16364,-16379);
62
-- --------------------------------------------------------
63
--	--CONSTANT sin_rom : ARRAY0_127S16 := (0,402,804,1205,1606,
64
--	--2006,2404,2801,3196,3590,3981,4370,4756,5139,5520,5897,
65
--	--6270,6639,7005,7366,7723,8076,8423,8765,9102,9434,9760,
66
--	--10080,10394,10702,11003,11297,11585,11866,12140,12406,
67
--	--12665,12916,13160,13395,13623,13842,14053,14256,14449,
68
--	14635,14811,14978,15137,15286,15426,15557,15679,15791,
69
--	15893,15986,16069,16143,16207,16261,16305,16340,16364,
70
--	16379,16384,16379,16364,16340,16305,16261,16207,16143,
71
--	16069,15986,15893,15791,15679,15557,15426,15286,15137,
72
--	14978,14811,14635,14449,14256,14053,13842,13623,13395,
73
--	13160,12916,12665,12406,12140,11866,11585,11297,11003,
74
--	10702,10394,10080,9760,9434,9102,8765,8423,8076,7723,
75
--	7366,7005,6639,6270,5897,5520,5139,4756,4370,3981,3590,
76
--	3196,2801,2404,2006,1606,1205,804,402);
77
	constant sin_rom : array0_127s16 := (0,6270,11585,15136,16384,15136,11585,6270);
78
79
SIGNAL sin , cos : S16;
80
BEGIN
81
82
	sin_read: PROCESS (clk)
83
	BEGIN
84
		IF falling_edge(clk) THEN
85
		sin <= sin_rom(w); -- Read from ROM
86
		END IF;
87
	END PROCESS;
88
89
	cos_read: PROCESS (clk)
90
	BEGIN
91
		IF falling_edge(clk) THEN
92
		cos <= cos_rom(w); -- Read from ROM
93
		END IF;
94
	END PROCESS;
95
96
	States: PROCESS(clk, reset, w)-----> FFT in behavioral style
97
	VARIABLE i1, i2, gcount, k1, k2 : U9 := 0;
98
	VARIABLE stage, dw, count, rcount : U9 := 0;
99
	VARIABLE tr, ti : S16 := 0;
100
	VARIABLE slv, rslv : STD_LOGIC_VECTOR(0 TO ldN-1);
101
	BEGIN
102
		IF reset = '1' THEN -- Asynchronous reset
103
			s <= start;
104
		ELSIF rising_edge(clk) THEN
105
			CASE s IS -- Next State assignments
106
				WHEN start =>
107
					s <= load; count := 0;
108
					gcount := 0; stage:= 1; i1:=0; i2 := N/2; k1:=N;
109
					k2:=N/2; dw := 1; fft_valid <= '0';
110
				WHEN load => -- Read in all data from I/O ports
111
					xr(count) <= xr_in; xi(count) <= xi_in;
112
					count := count + 1;
113
					IF count = N THEN s <= calc;
114
					ELSE s <= load;
115
					END IF;
116
				WHEN calc => -- Do the butterfly computation
117
					tr := xr(i1) - xr(i2);
118
					xr(i1) <= xr(i1) + xr(i2);
119
					ti := xi(i1) - xi(i2);
120
					xi(i1) <= xi(i1) + xi(i2);
121
					xr(i2) <= (cos * tr + sin * ti)/2**14;
122
					xi(i2) <= (cos * ti - sin * tr)/2**14;
123
					s <= update;
124
				WHEN update => -- All counters and pointers
125
					s <= calc; -- By default do next butterfly
126
					i1 := i1 + k1; -- Next butterfly in group
127
					i2 := i1 + k2;
128
					wo <= 1;
129
					IF i1 >= N-1 THEN -- All butterflies done in group?
130
						gcount := gcount + 1;
131
						i1 := gcount;
132
						i2 := i1 + k2;
133
						wo <= 2;
134
						IF gcount >= k2 THEN-- All groups done in stages?
135
							gcount := 0; i1 := 0; i2 := k2;
136
							dw := dw * 2;
137
							stage := stage + 1;
138
							wo <= 3;
139
							IF stage > ldN THEN -- All stages done
140
								s <= reverse;
141
								count := 0;
142
								wo <= 4;
143
							ELSE -- Start new stage
144
								k1 := k2; k2 := k2/2;
145
								i1 := 0; i2 := k2;
146
								w <= 0;
147
								wo <= 5;
148
							END IF;
149
						ELSE -- Start new group
150
							i1 := gcount; i2 := i1 + k2;
151
							w <= w + dw;
152
							wo <= 6;
153
						END IF;
154
					END IF;
155
			WHEN reverse => -- Apply bitreverse
156
				fft_valid <= '1';
157
				slv := CONV_STD_LOGIC_VECTOR(count, ldn);
158
				FOR i IN 0 TO ldn-1 LOOP
159
					rslv(i) := slv(ldn-i-1);
160
				END LOOP;
161
				rcount := CONV_INTEGER('0' & rslv);
162
				fftr <= xr(rcount); ffti <= xi(rcount);
163
				count := count + 1;
164
				IF count >= N THEN s <= done;
165
				ELSE s <= reverse;
166
				END IF;
167
			WHEN done => -- Output of results
168
				s <= start; -- Start next cycle
169
			END CASE;
170
		END IF;
171
		i1_o<=i1; -- Provide some test signals as outputs
172
		i2_o<=i2;
173
		stage_o<=stage;
174
		gcount_o <= gcount;
175
		k1_o <= k1;
176
		k2_o<=k2;
177
		w_o<=w;
178
		dw_o<=dw;
179
		rcount_o <= rcount;
180
	END PROCESS States;
181
	Rk: FOR k IN 0 TO 7 GENERATE -- Show first 8
182
		xr_out(k) <= xr(k); -- register values
183
		xi_out(k) <= xi(k);
184
	END GENERATE;
185
END fpga;