Debian Codegraph @

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

raw
1 -- Copyright (c) 2019 Nuand LLC
2 --
3 -- LICENSE TBD
4
5 -- From Altera Solution ID rd05312011_49, need the following for Qsys to use VHDL 2008:
6 -- altera vhdl_input_version vhdl_2008
7
8 library ieee;
9 use ieee.std_logic_1164.all;
10 use ieee.numeric_std.all;
11
12 library work;
13 use work.tone_generator_p.all;
14
15 -- Register map:
16 -- 0 control/status
17 -- control (write):
18 -- bit 0: append
19 -- bit 1: clear
20 -- status (read):
21 -- bit 0: running
22 -- bit 1: queue full
23 -- bits 8-15: number of items in the queue (unsigned)
24 -- others: reserved/undef
25 -- 1 dphase - phase rotation per clock cycle (signed)
26 -- one full rotation is 8192, and this is signed, so:
27 -- dphase = 4096 / (f_clock / f_tone)
28 -- 2 duration - in clocks (unsigned)
29
30 entity tone_generator_hw is
31 generic (
32 QUEUE_LENGTH : positive := 8;
33 ADDR_WIDTH : positive := 2;
34 DATA_WIDTH : positive := 32
35 );
36 port (
37 -- Control signals
38 clock : in std_logic;
39 reset : in std_logic;
40
41 -- Samples
42 sample_clk : in std_logic;
43 sample_i : out signed(15 downto 0);
44 sample_q : out signed(15 downto 0);
45 sample_valid : out std_logic;
46
47 -- Memory mapped interface
48 addr : in std_logic_vector(ADDR_WIDTH-1 downto 0);
49 din : in std_logic_vector(DATA_WIDTH-1 downto 0);
50 dout : out std_logic_vector(DATA_WIDTH-1 downto 0);
51 write : in std_logic;
52 read : in std_logic;
53 waitreq : out std_logic;
54 readack : out std_logic;
55
56 -- Status indicators
57 queue_empty : out std_logic;
58 queue_full : out std_logic;
59 running : out std_logic
60 );
61 end entity tone_generator_hw;
62
63 architecture arch of tone_generator_hw is
64 --------------------------------------------------------------------------------
65 -- Types
66 --------------------------------------------------------------------------------
67 constant NUM_REGS : natural := 3;
68
69 type tone_entries_t is array(natural range <>) of tone_generator_input_t;
70
71 type tone_queue_t is record
72 entries : tone_entries_t(0 to QUEUE_LENGTH-1);
73 front : natural range 0 to QUEUE_LENGTH-1; -- next pop
74 rear : natural range 0 to QUEUE_LENGTH-1; -- last push
75 count : natural range 0 to QUEUE_LENGTH; -- items in queue
76 end record tone_queue_t;
77
78 type fsm_t is (IDLE, CTRL_DISPATCH, CTRL_CLEAR, QUEUE_CLEAR, QUEUE_PUSH,
79 QUEUE_POP, TONE_GENERATE, WAIT_FOR_ACTIVE);
80
81 type state_t is record
82 state : fsm_t;
83 queue : tone_queue_t;
84 tone : tone_generator_input_t;
85 op_pending : boolean;
86 end record state_t;
87
88 type register_t is array(natural range din'range) of std_logic;
89 type registers_t is array(natural range 0 to NUM_REGS-1) of register_t;
90
91 type control_reg_t is record
92 append : boolean;
93 clear : boolean;
94 end record control_reg_t;
95
96 --------------------------------------------------------------------------------
97 -- Signals
98 --------------------------------------------------------------------------------
99 signal current : state_t;
100 signal future : state_t;
101
102 signal current_regs : registers_t;
103 signal future_regs : registers_t;
104
105 signal pend_ctrl : boolean;
106
107 signal uaddr : natural range 0 to 2**addr'high;
108
109 signal tgen_in : tone_generator_input_t;
110 signal tgen_out : tone_generator_output_t;
111 signal tgen_reset : std_logic;
112
113 signal tgen_out_a : std_logic;
114 signal tgen_out_v : std_logic;
115
116 signal q_full_i : std_logic;
117 signal q_empty_i : std_logic;
118 signal running_i : std_logic;
119
120 --------------------------------------------------------------------------------
121 -- Subprograms
122 --------------------------------------------------------------------------------
123 function to_slv (val : register_t) return std_logic_vector is
124 begin
125 return std_logic_vector(unsigned(val));
126 end function to_slv;
127
128 function to_sl (val : boolean) return std_logic is
129 begin
130 if (val) then
131 return '1';
132 else
133 return '0';
134 end if;
135 end function to_sl;
136
137 function to_reg (val : std_logic_vector) return register_t is
138 begin
139 return register_t(val);
140 end function to_reg;
141
142 function next_index (idx : natural) return natural is
143 begin
144 return (idx + 1) mod QUEUE_LENGTH;
145 end function next_index;
146
147 procedure queue_push (signal q_in : in tone_queue_t;
148 signal q_out : out tone_queue_t;
149 constant tone : in tone_generator_input_t;
150 success : out boolean) is
151 variable next_i : natural;
152 variable ql : natural;
153 begin
154 q_out <= q_in;
155 next_i := next_index(q_in.rear);
156 ql := q_in.count;
157
158 if (q_in.count >= QUEUE_LENGTH) then
159 -- queue is full
160 success := false;
161 else
162 -- add the entry at the NEXT rear index
163 q_out.entries(next_i) <= tone;
164
165 -- update indexes and counts
166 ql := q_in.count + 1;
167 q_out.count <= ql;
168 q_out.rear <= next_i;
169 success := true;
170 end if;
171
172 --report "queue_push:" &
173 -- " prev_rear=" & to_string(q_in.rear) &
174 -- " this_rear=" & to_string(next_i) &
175 -- " prev_count=" & to_string(q_in.count) &
176 -- " this_count=" & to_string(ql) &
177 -- " success=" & to_string(success);
178 end procedure queue_push;
179
180 procedure queue_pop (signal q_in : in tone_queue_t;
181 signal q_out : out tone_queue_t;
182 signal tone : out tone_generator_input_t;
183 success : out boolean) is
184 variable next_i : natural;
185 variable ql : natural;
186 begin
187 q_out <= q_in;
188 next_i := next_index(q_in.front);
189 ql := q_in.count;
190
191 if (q_in.count = 0) then
192 -- queue is empty
193 success := false;
194 else
195 -- pop the entry at the CURRENT front index
196 tone <= q_in.entries(q_in.front);
197
198 -- update indexes and counts
199 ql := q_in.count - 1;
200 q_out.count <= ql;
201 q_out.front <= next_i;
202 success := true;
203 end if;
204
205 --report "queue_pop:" &
206 -- " prev_front=" & to_string(q_in.front) &
207 -- " this_front=" & to_string(next_i) &
208 -- " prev_count=" & to_string(q_in.count) &
209 -- " this_count=" & to_string(ql) &
210 -- " success=" & to_string(success);
211 end procedure queue_pop;
212
213 function unpack_control (reg : register_t) return control_reg_t is
214 variable rv : control_reg_t;
215 begin
216 rv.append := (reg(0) = '1');
217 rv.clear := (reg(1) = '1');
218 return rv;
219 end function unpack_control;
220
221 function unpack_control (reg : std_logic_vector) return control_reg_t is
222 begin
223 return unpack_control(to_reg(reg));
224 end function unpack_control;
225
226 function NULL_REGISTERS return registers_t is
227 begin
228 return (others => (others => '0'));
229 end function NULL_REGISTERS;
230
231 function NULL_TONE_QUEUE return tone_queue_t is
232 variable rv : tone_queue_t;
233 begin
234 rv.entries := (others => NULL_TONE_GENERATOR_INPUT);
235 rv.front := 0;
236 rv.rear := QUEUE_LENGTH-1;
237 return rv;
238 end function NULL_TONE_QUEUE;
239
240 function NULL_STATE return state_t is
241 variable rv : state_t;
242 begin
243 rv.state := IDLE;
244 rv.queue := NULL_TONE_QUEUE;
245 rv.tone := NULL_TONE_GENERATOR_INPUT;
246 rv.op_pending := false;
247 return rv;
248 end function NULL_STATE;
249
250 begin
251 -- Output mappings
252 sample_i <= tgen_out.re;
253 sample_q <= tgen_out.im;
254 sample_valid <= tgen_out.valid;
255 queue_full <= q_full_i;
256 queue_empty <= q_empty_i;
257 running <= running_i;
258
259 -- For convenience
260 uaddr <= to_integer(unsigned(addr));
261
262 -- Status indicators
263 q_empty_i <= '1' when (current.queue.count = 0) else '0';
264 q_full_i <= '1' when (current.queue.count >= QUEUE_LENGTH) else '0';
265 running_i <= '1' when (tgen_out_a = '1' or tgen_out_v = '1') else '0';
266
267 -- Tone generator interface (with clock domain crossing assist)
268 -- reset, tgen_in: system clock -> sample clock
269 -- tgen_out: sample clock -> system clock
270 U_reset_sync : entity work.reset_synchronizer
271 generic map (
272 INPUT_LEVEL => '1',
273 OUTPUT_LEVEL => '1'
274 )
275 port map (
276 clock => sample_clk,
277 async => reset,
278 sync => tgen_reset
279 );
280
281 U_tgen_in_valid_sync : entity work.synchronizer
282 generic map (
283 RESET_LEVEL => '0'
284 )
285 port map (
286 reset => tgen_reset,
287 clock => sample_clk,
288 async => current.tone.valid,
289 sync => tgen_in.valid
290 );
291
292 generate_sync_tgen_in_dphase : for i in tgen_in.dphase'range generate
293 U_tgen_in_dphase : entity work.synchronizer
294 generic map (
295 RESET_LEVEL => '0'
296 )
297 port map (
298 reset => tgen_reset,
299 clock => sample_clk,
300 async => current.tone.dphase(i),
301 sync => tgen_in.dphase(i)
302 );
303 end generate;
304
305 generate_sync_tgen_in_duration : for i in tgen_in.duration'range generate
306 U_tgen_in_duration : entity work.synchronizer
307 generic map (
308 RESET_LEVEL => '0'
309 )
310 port map (
311 reset => tgen_reset,
312 clock => sample_clk,
313 async => current.tone.duration(i),
314 sync => tgen_in.duration(i)
315 );
316 end generate;
317
318 U_tgen_out_valid_sync : entity work.synchronizer
319 port map (
320 reset => reset,
321 clock => clock,
322 async => tgen_out.valid,
323 sync => tgen_out_v
324 );
325
326 U_tgen_out_active_sync : entity work.synchronizer
327 port map (
328 reset => reset,
329 clock => clock,
330 async => tgen_out.active,
331 sync => tgen_out_a
332 );
333
334 U_tone_generator : entity work.tone_generator
335 port map (
336 clock => sample_clk,
337 reset => tgen_reset,
338 inputs => tgen_in,
339 outputs => tgen_out
340 );
341
342 -- Avalon-MM interface
343 waitreq <= '1' when (current.op_pending or pend_ctrl) else '0';
344
345 mm_read : process(clock, reset)
346 begin
347 if (reset = '1') then
348 readack <= '0';
349 dout <= (others => '0');
350
351 elsif (rising_edge(clock)) then
352 -- hold off ack until there's no ops pending (race condition)
353 readack <= to_sl(read = '1' and not current.op_pending);
354
355 if (uaddr = 0) then
356 -- count of items in queue
357 dout <= (others => '0');
358 dout(0) <= running_i;
359 dout(1) <= q_full_i;
360 dout(15 downto 8) <=
361 std_logic_vector(to_unsigned(current.queue.count, 8));
362 else
363 dout <= to_slv(current_regs(uaddr));
364 end if;
365 end if;
366 end process mm_read;
367
368 mm_write : process(clock, reset)
369 begin
370 if (reset = '1') then
371 future_regs <= (others => (others => '0'));
372 pend_ctrl <= false;
373
374 elsif (rising_edge(clock)) then
375 if (current.op_pending) then
376 -- our previous pend_ctrl has been acknowledged
377 pend_ctrl <= false;
378 end if;
379
380 if (write = '1' and uaddr < NUM_REGS) then
381 future_regs(uaddr) <= to_reg(din);
382
383 if (uaddr = 0) then
384 -- send a pend_ctrl signal for the state machine to pick up
385 pend_ctrl <= true;
386 end if;
387 end if;
388 end if;
389 end process mm_write;
390
391 -- State machinery
392 sync_proc : process(clock, reset)
393 begin
394 if (reset = '1') then
395 current <= NULL_STATE;
396 current_regs <= NULL_REGISTERS;
397
398 elsif (rising_edge(clock)) then
399 current <= future;
400 current_regs <= future_regs;
401 end if;
402 end process sync_proc;
403
404 comb_proc : process(all)
405 variable ctrl_reg : control_reg_t;
406 variable tone_val : tone_generator_input_t := NULL_TONE_GENERATOR_INPUT;
407 variable success : boolean := true;
408 begin
409 future <= current;
410
411 future.op_pending <= current.op_pending or pend_ctrl;
412 future.tone.valid <= '0';
413
414 tone_val := NULL_TONE_GENERATOR_INPUT;
415
416 case (current.state) is
417 when IDLE =>
418 if (current.op_pending) then
419 -- we have a control message to deal with
420 future.state <= CTRL_DISPATCH;
421 end if;
422
423 if (current.queue.count > 0) then
424 -- there is work to do
425 if (tgen_out_a = '0') then
426 -- let's do it!
427 future.state <= QUEUE_POP;
428 end if;
429 end if;
430
431 when CTRL_DISPATCH =>
432 ctrl_reg := unpack_control(current_regs(0));
433
434 -- default
435 future.state <= CTRL_CLEAR;
436
437 -- append
438 if (ctrl_reg.append) then
439 future.state <= QUEUE_PUSH;
440 end if;
441
442 -- clear (overrides append)
443 if (ctrl_reg.clear) then
444 future.state <= QUEUE_CLEAR;
445 end if;
446
447 when CTRL_CLEAR =>
448 future.op_pending <= false;
449 future.state <= IDLE;
450
451 when QUEUE_CLEAR =>
452 future.queue <= NULL_TONE_QUEUE;
453 future.state <= CTRL_CLEAR;
454
455 when QUEUE_PUSH =>
456 tone_val.dphase
457 := signed(current_regs(1)(tone_val.dphase'range));
458 tone_val.duration
459 := unsigned(current_regs(2)(tone_val.duration'range));
460
461 queue_push(current.queue, future.queue, tone_val, success);
462
463 assert success report "QUEUE_PUSH failed" severity failure;
464
465 future.state <= CTRL_CLEAR;
466
467 when QUEUE_POP =>
468 queue_pop(current.queue, future.queue, future.tone, success);
469
470 assert success report "QUEUE_POP failed" severity failure;
471
472 future.state <= TONE_GENERATE;
473
474 if (not success) then
475 future.state <= IDLE;
476 end if;
477
478 when TONE_GENERATE =>
479 future.tone.valid <= '1';
480 future.state <= WAIT_FOR_ACTIVE;
481
482 when WAIT_FOR_ACTIVE =>
483 future.tone.valid <= '1';
484
485 if (tgen_out_a = '1') then
486 future.tone.valid <= '0';
487 future.state <= IDLE;
488 end if;
489
490 end case;
491 end process comb_proc;
492
493 end architecture arch;