summaryrefslogtreecommitdiffstats
path: root/fpga/hw-v2/src/usbrx/datapath/usbrx_decimate.vhd
blob: e3be853d08d40b230cc42c5d378d0c6e1eddb024 (plain)
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
---------------------------------------------------------------------------------------------------
-- Filename    : usbrx_decimate.vhd
-- Project     : OsmoSDR FPGA Firmware
-- Purpose     : Variable decimation filter
--               (possible factors: 1,2,4,8,16,32,64)
---------------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------
-- Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany --
-- written by Matthias Kleffel                                                   --
--                                                                               --
-- This program is free software; you can redistribute it and/or modify          --
-- it under the terms of the GNU General Public License as published by          --
-- the Free Software Foundation as version 3 of the License, or                  --
--                                                                               --
-- This program 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 General Public License V3 for more details.                               --
--                                                                               --
-- You should have received a copy of the GNU General Public License             --
-- along with this program. If not, see <http://www.gnu.org/licenses/>.          --
-----------------------------------------------------------------------------------

-------------------------------------------------------------------------------
-- decimation filter ----------------------------------------------------------
-------------------------------------------------------------------------------

library ieee;
	use ieee.std_logic_1164.all;
	use ieee.numeric_std.all;
library work;
	use work.all;
	use work.mt_toolbox.all;
	use work.mt_filter.all;
	use work.usbrx.all;

entity usbrx_decimate is
	port (
		-- common
		clk     : in  std_logic;
		reset   : in  std_logic;
		
		-- config
		config  : in  usbrx_fil_config_t;
		
		-- input
		in_clk  : in  std_logic;
		in_i    : in  signed(15 downto 0);
		in_q    : in  signed(15 downto 0);
		
		-- output
		out_clk : out std_logic;
		out_i   : out signed(15 downto 0);
		out_q   : out signed(15 downto 0)
	);
end usbrx_decimate;

architecture rtl of usbrx_decimate is		

	-- config
	signal active : std_logic_vector(5 downto 0);

	-- adapted input
	signal in_si : fir_dataword18;
	signal in_sq : fir_dataword18;
	
	-- filter input
	signal fil_in_clk  : std_logic_vector(5 downto 0);
	signal fil_in_i	   : fir_databus18(5 downto 0);
	signal fil_in_q    : fir_databus18(5 downto 0);
	
	-- filter output
	signal fil_out_clk : std_logic_vector(5 downto 0);
	signal fil_out_i   : fir_databus18(5 downto 0);
	signal fil_out_q   : fir_databus18(5 downto 0);
	
	-- unclipped output
	signal nxt_clk : std_logic;
	signal nxt_i   : fir_dataword18;
	signal nxt_q   : fir_dataword18;
	
begin
	
	-- convert input into 18bit signed
	in_si <= signed(in_i) & "00";
	in_sq <= signed(in_q) & "00";
	
	-- control logic
	process(clk)
		variable tmp_i,tmp_q : fir_dataword18;
	begin
		if rising_edge(clk) then
			
			-- get active stages
			case to_integer(config.decim) is
				when 0      => active <= "000000";
				when 1      => active <= "000001";
				when 2      => active <= "000011";
				when 3      => active <= "000111";
				when 4      => active <= "001111";
				when 5      => active <= "011111";
				when others => active <= "111111";
			end case;
			
			-- select output
			case to_integer(config.decim) is
				when 0 => 
					nxt_clk <= in_clk;
					nxt_i   <= in_si;
					nxt_q   <= in_sq;
				when 1 => 
					nxt_clk <= fil_out_clk(0);
					nxt_i   <= fil_out_i(0);
					nxt_q   <= fil_out_q(0);
				when 2 => 
					nxt_clk <= fil_out_clk(1);
					nxt_i   <= fil_out_i(1);
					nxt_q   <= fil_out_q(1);
				when 3 => 
					nxt_clk <= fil_out_clk(2);
					nxt_i   <= fil_out_i(2);
					nxt_q   <= fil_out_q(2);
				when 4 => 
					nxt_clk <= fil_out_clk(3);
					nxt_i   <= fil_out_i(3);
					nxt_q   <= fil_out_q(3);
				when 5 => 
					nxt_clk <= fil_out_clk(4);
					nxt_i   <= fil_out_i(4);
					nxt_q   <= fil_out_q(4);
				when others => 
					nxt_clk <= fil_out_clk(5);
					nxt_i   <= fil_out_i(5);
					nxt_q   <= fil_out_q(5);
			end case;
			
			-- set output
			out_clk <= nxt_clk;
			if nxt_clk='1' then
				tmp_i := nxt_i + 2;
				tmp_q := nxt_q + 2;
				out_i <= tmp_i(17 downto 2);
				out_q <= tmp_q(17 downto 2);
			end if;
			
			-- handle reset
			if reset='1' then
				active  <= (others=>'0');
				nxt_clk <= '0';
				nxt_i   <= (others=>'0');
				nxt_q   <= (others=>'0');
				out_clk <= '0';
				out_i   <= (others=>'0');
				out_q   <= (others=>'0');
			end if;
		end if;
	end process;
	
	
	process(in_clk,in_si,in_sq,fil_out_clk,fil_out_i,fil_out_q,active)
	begin
		-- feed first filter stage
		fil_in_clk(0) <= in_clk and active(0);
		fil_in_i(0) <= in_si;
		fil_in_q(0) <= in_sq;
	
		-- chain remaining filter stages
		for i in 1 to 5 loop
			fil_in_clk(i) <= fil_out_clk(i-1) and active(i-1);
			fil_in_i(i) <= fil_out_i(i-1);
			fil_in_q(i) <= fil_out_q(i-1);
		end loop;
	end process;
	
	-- filter instance #1 (stage 0)
	hbf1: entity usbrx_halfband
		generic map (
			N => 1
		)
		port map (
			-- common
			clk     => clk,
			reset   => reset,
			
			-- input
			in_clk  => fil_in_clk(0 downto 0),
			in_i    => fil_in_i(0 downto 0),
			in_q    => fil_in_q(0 downto 0),
			
			-- output
			out_clk => fil_out_clk(0 downto 0),
			out_i   => fil_out_i(0 downto 0),
			out_q   => fil_out_q(0 downto 0)
		);

	-- filter instance #2 (stage 1-5)
	hbf2: entity usbrx_halfband
		generic map (
			N => 5
		)
		port map (
			-- common
			clk     => clk,
			reset   => reset,
			
			-- input
			in_clk  => fil_in_clk(5 downto 1),
			in_i    => fil_in_i(5 downto 1),
			in_q    => fil_in_q(5 downto 1),
			
			-- output
			out_clk => fil_out_clk(5 downto 1),
			out_i   => fil_out_i(5 downto 1),
			out_q   => fil_out_q(5 downto 1)
		);
		
end rtl;