Debian Codegraph @

hdl/fpga/ip/nuand/tone_generator/vhdl/tone_generator.vhd

raw
1 -- Copyright (c) 2019 Nuand LLC
2 --
3 -- Permission is hereby granted, free of charge, to any person obtaining a copy
4 -- of this software and associated documentation files (the "Software"), to deal
5 -- in the Software without restriction, including without limitation the rights
6 -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 -- copies of the Software, and to permit persons to whom the Software is
8 -- furnished to do so, subject to the following conditions:
9 --
10 -- The above copyright notice and this permission notice shall be included in
11 -- all copies or substantial portions of the Software.
12 --
13 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 -- THE SOFTWARE.
20
21 -- From Altera Solution ID rd05312011_49, need the following for Qsys to use VHDL 2008:
22 -- altera vhdl_input_version vhdl_2008
23
24 library ieee;
25 use ieee.std_logic_1164.all;
26 use ieee.numeric_std.all;
27
28 package tone_generator_p is
29 type tone_generator_input_t is record
30 dphase : signed(15 downto 0);
31 duration : unsigned(31 downto 0);
32 valid : std_logic;
33 end record;
34
35 type tone_generator_output_t is record
36 re : signed(15 downto 0);
37 im : signed(15 downto 0);
38 valid : std_logic;
39 active : std_logic;
40 end record;
41
42 function NULL_TONE_GENERATOR_INPUT return tone_generator_input_t;
43 function NULL_TONE_GENERATOR_OUTPUT return tone_generator_output_t;
44 end package tone_generator_p;
45
46 package body tone_generator_p is
47 function NULL_TONE_GENERATOR_INPUT return tone_generator_input_t is
48 variable rv : tone_generator_input_t;
49 begin
50 rv.dphase := (others => '0');
51 rv.duration := (others => '0');
52 rv.valid := '0';
53 return rv;
54 end function NULL_TONE_GENERATOR_INPUT;
55
56 function NULL_TONE_GENERATOR_OUTPUT return tone_generator_output_t is
57 variable rv : tone_generator_output_t;
58 begin
59 rv.re := (others => '0');
60 rv.im := (others => '0');
61 rv.valid := '0';
62 rv.active := '0';
63 return rv;
64 end function NULL_TONE_GENERATOR_OUTPUT;
65 end package body tone_generator_p;
66
67 library ieee;
68 use ieee.std_logic_1164.all;
69 use ieee.numeric_std.all;
70 use ieee.math_real.all;
71
72 library work;
73 use work.nco_p.all;
74 use work.tone_generator_p.all;
75
76 entity tone_generator is
77 port (
78 clock : in std_logic;
79 reset : in std_logic;
80 inputs : in tone_generator_input_t;
81 outputs : out tone_generator_output_t
82 );
83 end entity tone_generator;
84
85 architecture arch of tone_generator is
86 constant HOLDCOUNT : natural := 32;
87
88 type fsm_t is (IDLE, WAIT_FOR_ACK, START_TONE, ACTIVE_TONE, FINISH_TONE,
89 WAIT_FOR_ZERO, HOLD);
90
91 type state_t is record
92 state : fsm_t;
93 is_active : boolean;
94 nco_reset : std_logic;
95 downcount : natural;
96 holdcount : natural;
97 tone_latch : tone_generator_input_t;
98 nco_in : nco_input_t;
99 last_i : signed(15 downto 0);
100 last_q : signed(15 downto 0);
101 zero_hold_i : boolean;
102 zero_hold_q : boolean;
103 zero_xing_i : boolean;
104 zero_xing_q : boolean;
105 tone_pending : boolean;
106 end record state_t;
107
108 function NULL_STATE return state_t is
109 variable rv : state_t;
110 begin
111 rv.state := IDLE;
112 rv.is_active := false;
113 rv.nco_reset := '1';
114 rv.downcount := 0;
115 rv.holdcount := 0;
116 rv.tone_latch := NULL_TONE_GENERATOR_INPUT;
117 rv.nco_in := (dphase => (others => '0'), valid => '0');
118 rv.last_i := (others => '0');
119 rv.last_q := (others => '0');
120 rv.zero_hold_i := true;
121 rv.zero_hold_q := true;
122 rv.zero_xing_i := false;
123 rv.zero_xing_q := false;
124 rv.tone_pending := false;
125 return rv;
126 end function NULL_STATE;
127
128 function is_zero_cross(a : signed ; b : signed) return boolean is
129 begin
130 return (a >= 0 and b <= 0) or (a <= 0 and b >= 0);
131 end function is_zero_cross;
132
133 signal current : state_t;
134 signal future : state_t;
135
136 signal nco_out : nco_output_t;
137 begin
138 U_nco : entity work.nco
139 port map (
140 clock => clock,
141 reset => current.nco_reset,
142 inputs => current.nco_in,
143 outputs => nco_out
144 );
145
146 outputs.re <= (others => '0') when current.zero_hold_i else nco_out.re;
147 outputs.im <= (others => '0') when current.zero_hold_q else nco_out.im;
148 outputs.valid <= nco_out.valid when (current.nco_reset = '0') else '0';
149 outputs.active <= '1' when current.is_active else '0';
150
151 -- State machinery
152 sync_proc : process(clock, reset)
153 begin
154 if (reset = '1') then
155 current <= NULL_STATE;
156 elsif (rising_edge(clock)) then
157 current <= future;
158 end if;
159 end process sync_proc;
160
161 comb_proc : process(all)
162 begin
163 future <= current;
164
165 -- Keep last sample, so we can determine if we find a zero-crossing
166 if (nco_out.valid = '1') then
167 future.last_i <= nco_out.re;
168 future.last_q <= nco_out.im;
169
170 future.zero_xing_i <= is_zero_cross(current.last_i, nco_out.re);
171 future.zero_xing_q <= is_zero_cross(current.last_q, nco_out.im);
172 end if;
173
174 -- Latch valid inputs...
175 if (inputs.valid = '1') then
176 future.tone_latch <= inputs;
177 future.tone_pending <= true;
178 end if;
179
180 -- Defaults: active if tone pending, nco_in valid, nco not reset
181 future.is_active <= current.tone_pending;
182 future.nco_in.valid <= '1';
183 future.nco_reset <= '0';
184
185 case (current.state) is
186 when IDLE =>
187 -- Zero all outputs, reset the NCO while idle
188 future.zero_hold_i <= true;
189 future.zero_hold_q <= true;
190 future.nco_reset <= '1';
191 future.nco_in.valid <= '0';
192
193 -- We have a tone to do, folks
194 if (current.tone_pending) then
195 future.nco_reset <= '0';
196 future.state <= WAIT_FOR_ACK;
197 end if;
198
199 when WAIT_FOR_ACK => -- Wait until valid is deasserted
200 if (inputs.valid = '0') then
201 future.state <= START_TONE;
202 end if;
203
204 when START_TONE => -- Set up NCO, counters, etc
205 -- We are active
206 future.is_active <= true;
207
208 -- Set up our downcounts
209 future.downcount <= to_integer(current.tone_latch.duration);
210 future.holdcount <= HOLDCOUNT;
211
212 -- Configure the NCO
213 future.nco_in.dphase <= current.tone_latch.dphase;
214
215 -- Update state
216 future.tone_pending <= false;
217 future.state <= ACTIVE_TONE;
218
219 when ACTIVE_TONE => -- Tone is coming out
220 -- We are active
221 future.is_active <= true;
222
223 -- Wait for a zero-crossing to un-zero NCO output
224 if (current.nco_in.dphase /= 0) then
225 if (current.zero_hold_i and current.zero_xing_i) then
226 future.zero_hold_i <= false;
227 end if;
228
229 if (current.zero_hold_q and current.zero_xing_q) then
230 future.zero_hold_q <= false;
231 end if;
232 else
233 future.zero_hold_i <= true;
234 future.zero_hold_q <= true;
235 end if;
236
237 -- Countdown
238 if (current.downcount > 0) then
239 future.downcount <= current.downcount - 1;
240 else
241 future.state <= FINISH_TONE;
242 end if;
243
244 when FINISH_TONE => -- Determine next action
245 -- Decrement hold count
246 if (current.holdcount > 0) then
247 future.holdcount <= current.holdcount - 1;
248 end if;
249
250 -- We have a few situations to handle:
251 --
252 -- current dphase next dphase next state
253 -- 1. 0 0 START_TONE
254 -- 2. 0 != 0 START_TONE
255 -- 3. 0 none IDLE
256 -- 4. not 0 0 Zero-xing -> START_TONE
257 -- 5. not 0 != 0 START_TONE
258 -- 6. not 0 none Zero-xing -> IDLE
259
260 if (current.nco_in.dphase /= 0) then
261 -- Cases 4, 5, and 6: go to another state
262 future.state <= WAIT_FOR_ZERO;
263 else
264 if (current.tone_pending) then
265 -- Cases 1 and 2
266 future.state <= WAIT_FOR_ACK;
267 else
268 -- Case 3
269 future.state <= HOLD;
270 end if;
271 end if;
272
273 when WAIT_FOR_ZERO => -- Wait for zero crossing
274 -- Decrement hold count
275 if (current.holdcount > 0) then
276 future.holdcount <= current.holdcount - 1;
277 end if;
278
279 -- Snatch the next zero-crossing
280 if (not current.zero_hold_i and current.zero_xing_i) then
281 future.zero_hold_i <= true;
282 end if;
283
284 if (not current.zero_hold_q and current.zero_xing_q) then
285 future.zero_hold_q <= true;
286 end if;
287
288 -- We are going to handle these cases here:
289 --
290 -- current dphase next dphase next state
291 -- 4. not 0 0 Zero-xing -> START_TONE
292 -- 5. not 0 != 0 START_TONE
293 -- 6. not 0 none Zero-xing -> IDLE
294
295 if (current.tone_pending and current.tone_latch.dphase /= 0)
296 then
297 -- Case 5
298 future.state <= WAIT_FOR_ACK;
299 elsif (current.zero_hold_i and current.zero_hold_q) then
300 if (current.tone_pending) then
301 -- Case 4
302 future.state <= WAIT_FOR_ACK;
303 else
304 -- Case 6
305 future.state <= HOLD;
306 end if;
307 end if;
308
309 when HOLD => -- Keep the NCO spinning to avoid discontinuities
310 -- Decrement hold count
311 if (current.holdcount > 0) then
312 future.holdcount <= current.holdcount - 1;
313 else
314 future.state <= IDLE;
315 end if;
316
317 -- We have a tone to do, folks
318 if (current.tone_pending) then
319 future.state <= WAIT_FOR_ACK;
320 end if;
321
322 end case;
323 end process comb_proc;
324 end architecture arch;