Debian Codegraph @

hdl/fpga/ip/nuand/synthesis/iq_correction.vhd

raw
1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
4 use ieee.math_real.all;
5
6
7
8 entity iq_correction is
9 generic(
10 INPUT_WIDTH : positive := 16;
11 OUTPUT_WIDTH: positive := 16;
12 DC_WIDTH : positive := 16;
13 GAIN_WIDTH : positive := 16;
14 PHASE_WIDTH : positive := 16;
15
16 Q : positive := 12
17
18 );
19
20 port(
21 clock : in std_logic;
22 reset : in std_logic;
23
24 --input signal
25 in_real : in signed(INPUT_WIDTH-1 downto 0);
26 in_imag : in signed(INPUT_WIDTH-1 downto 0);
27 in_valid : in std_logic;
28
29 --output signal
30 out_real : out signed(OUTPUT_WIDTH-1 downto 0);
31 out_imag : out signed(OUTPUT_WIDTh-1 downto 0);
32 out_valid : out std_logic;
33
34 --correction signals
35 dc_real : in signed(DC_WIDTH-1 downto 0);
36 dc_imag : in signed(DC_WIDTH-1 downto 0);
37
38 gain : in signed(GAIN_WIDTH-1 downto 0);
39 phase : in signed(PHASE_WIDTH-1 downto 0);
40
41 correction_valid: in std_logic
42 );
43 end entity;
44
45 architecture rx of iq_correction is
46
47 signal dc_balanced_real : signed(INPUT_WIDTH-1 downto 0);
48 signal dc_balanced_imag : signed(INPUT_WIDTH-1 downto 0);
49 signal dc_balanced_valid : std_logic;
50
51 signal gain_corrected_real : signed(INPUT_WIDTH-1 downto 0);
52 signal gain_corrected_imag : signed(INPUT_WIDTH-1 downto 0);
53 signal gain_corrected_valid : std_logic;
54
55 signal phase_corrected_real : signed(OUTPUT_WIDTH-1 downto 0);
56 signal phase_corrected_imag : signed(OUTPUT_WIDTH-1 downto 0);
57 signal phase_corrected_valid : std_logic;
58
59 signal phase_correction : signed(OUTPUT_WIDTH-1 downto 0);
60
61
62 begin
63 --
64 U_correction_lookup : entity work.tan_table
65 generic map(
66 STEPS => 2**Q,
67 ANGLE => MATH_PI*(10.0/180.0)
68 )
69 port map
70 (
71 clock => clock,
72 reset => reset,
73
74 phase => phase,
75 phase_valid => correction_valid,
76
77 y => phase_correction,
78 y_valid => open
79 );
80
81 --apply DC correction to real and imag
82 apply_dc : process(clock, reset)
83 begin
84 if reset = '1' then
85 --
86 dc_balanced_real <= (others => '0');
87 dc_balanced_imag <= (others => '0');
88 dc_balanced_valid <= '0';
89 elsif rising_edge(clock) then
90 dc_balanced_valid <= '0';
91 if in_valid = '1' then
92 dc_balanced_real <= in_real - dc_real;
93 dc_balanced_imag <= in_imag - dc_imag;
94 dc_balanced_valid <= '1';
95 end if;
96 end if;
97 end process;
98
99 --apply gain correction to real
100 apply_gain : process(clock, reset)
101 begin
102 if reset = '1' then
103 gain_corrected_real <= (others => '0');
104 gain_corrected_imag <= (others => '0');
105 gain_corrected_valid <= '0';
106 elsif rising_edge(clock) then
107 gain_corrected_valid <= '0';
108 if dc_balanced_valid = '1' then
109 gain_corrected_real <= resize(shift_right(dc_balanced_real*gain,Q),gain_corrected_real'length);
110 gain_corrected_imag <= dc_balanced_imag;
111 gain_corrected_valid <= '1';
112 end if;
113 end if;
114 end process;
115
116 --apply phase correction to imag
117 apply_phase : process(clock, reset)
118 variable register_offset_real : signed(OUTPUT_WIDTH-1 downto 0);
119 variable register_offset_imag : signed(OUTPUT_WIDTH-1 downto 0);
120 variable register_real : signed(OUTPUT_WIDTH-1 downto 0);
121 variable register_imag : signed(OUTPUT_WIDTH-1 downto 0);
122 variable register_valid : std_logic;
123
124 begin
125 if reset = '1' then
126 phase_corrected_real <= (others => '0');
127 phase_corrected_imag <= (others => '0');
128 phase_corrected_valid <= '0';
129 register_offset_real := (others => '0');
130 register_offset_imag := (others => '0');
131 register_real := (others => '0');
132 register_imag := (others => '0');
133 register_valid := '0';
134 elsif rising_edge(clock) then
135 phase_corrected_valid <= '0';
136 if register_valid = '1' then
137 phase_corrected_real <= register_real + register_offset_real;
138 phase_corrected_imag <= register_imag + register_offset_imag;
139 phase_corrected_valid <= '1';
140 end if;
141
142 register_valid := '0';
143 if gain_corrected_valid = '1' then
144 register_offset_real := resize(shift_right(gain_corrected_imag*phase_correction,Q),phase_corrected_real'length);
145 register_offset_imag := resize(shift_right(gain_corrected_real*phase_correction,Q),phase_corrected_imag'length);
146 register_real := gain_corrected_real;
147 register_imag := gain_corrected_imag;
148 register_valid := '1';
149 end if;
150 end if;
151 end process;
152
153 out_real <= phase_corrected_real;
154 out_imag <= phase_corrected_imag;
155 out_valid <= phase_Corrected_valid;
156
157 end architecture;
158
159
160
161 architecture tx of iq_correction is
162
163 signal dc_balanced_real : signed(INPUT_WIDTH-1 downto 0);
164 signal dc_balanced_imag : signed(INPUT_WIDTH-1 downto 0);
165 signal dc_balanced_valid : std_logic;
166
167 signal gain_corrected_real : signed(INPUT_WIDTH-1 downto 0);
168 signal gain_corrected_imag : signed(INPUT_WIDTH-1 downto 0);
169 signal gain_corrected_valid : std_logic;
170
171 signal phase_corrected_real : signed(OUTPUT_WIDTH-1 downto 0);
172 signal phase_corrected_imag : signed(OUTPUT_WIDTH-1 downto 0);
173 signal phase_corrected_valid : std_logic;
174
175 signal phase_correction : signed(OUTPUT_WIDTH-1 downto 0);
176
177 begin
178
179 --apply gain correction to real
180 apply_gain : process(clock, reset)
181 begin
182 if reset = '1' then
183 gain_corrected_real <= (others => '0');
184 gain_corrected_imag <= (others => '0');
185 gain_corrected_valid <= '0';
186 elsif rising_edge(clock) then
187 gain_corrected_valid <= '0';
188 if in_valid = '1' then
189 gain_corrected_real <= resize(shift_right(in_real*gain,Q),gain_corrected_real'length);
190 gain_corrected_imag <= in_imag;
191 gain_corrected_valid <= '1';
192 end if;
193 end if;
194 end process;
195
196
197 --
198 U_phase_correction_lookup : entity work.tan_table
199 generic map(
200 STEPS => 2**Q,
201 ANGLE => MATH_PI*(10.0/180.0)
202 )
203 port map
204 (
205 clock => clock,
206 reset => reset,
207
208 phase => phase,
209 phase_valid => correction_valid,
210
211 y => phase_correction,
212 y_valid => open
213 );
214
215 --apply phase correction to imag
216 apply_phase : process(clock, reset)
217 variable register_offset_real : signed(OUTPUT_WIDTH-1 downto 0);
218 variable register_offset_imag : signed(OUTPUT_WIDTH-1 downto 0);
219 variable register_real : signed(OUTPUT_WIDTH-1 downto 0);
220 variable register_imag : signed(OUTPUT_WIDTH-1 downto 0);
221 variable register_valid : std_logic;
222
223 begin
224 if reset = '1' then
225 phase_corrected_real <= (others => '0');
226 phase_corrected_imag <= (others => '0');
227 phase_corrected_valid <= '0';
228 register_offset_real := (others => '0');
229 register_offset_imag := (others => '0');
230 register_real := (others => '0');
231 register_imag := (others => '0');
232 register_valid := '0';
233 elsif rising_edge(clock) then
234 phase_corrected_valid <= '0';
235 if register_valid = '1' then
236 phase_corrected_real <= register_real + register_offset_real;
237 phase_corrected_imag <= register_imag + register_offset_imag;
238 phase_corrected_valid <= '1';
239 end if;
240
241 register_valid := '0';
242 if gain_corrected_valid = '1' then
243 register_offset_real := resize(shift_right(gain_corrected_imag*phase_correction,Q),phase_corrected_real'length);
244 register_offset_imag := resize(shift_right(gain_corrected_real*phase_correction,Q),phase_corrected_imag'length);
245 register_real := gain_corrected_real;
246 register_imag := gain_corrected_imag;
247 register_valid := '1';
248 end if;
249 end if;
250 end process;
251
252
253
254 --apply DC correction to real and imag
255 apply_dc : process(clock, reset)
256 begin
257 if reset = '1' then
258 --
259 dc_balanced_real <= (others => '0');
260 dc_balanced_imag <= (others => '0');
261 dc_balanced_valid <= '0';
262 elsif rising_edge(clock) then
263 dc_balanced_valid <= '0';
264 if phase_corrected_valid = '1' then
265 dc_balanced_real <= phase_corrected_real - dc_real;
266 dc_balanced_imag <= phase_corrected_imag - dc_imag;
267 dc_balanced_valid <= '1';
268 end if;
269 end if;
270 end process;
271
272 out_real <= dc_balanced_real;
273 out_imag <= dc_balanced_imag;
274 out_valid <= dc_balanced_valid;
275
276 end architecture;