From d0b1bbada095ffde86d9d1afb58af698a681ea96 Mon Sep 17 00:00:00 2001 From: Rey Tucker Date: Wed, 16 Jan 2019 13:25:56 -0500 Subject: [PATCH] tone_generator: only engage/disengage at zero crossings this reduces "key click", also ensures we don't just leave DC hanging out --- .../tone_generator/vhdl/tone_generator.vhd | 220 ++++++++++++++---- 1 file changed, 175 insertions(+), 45 deletions(-) diff --git a/hdl/fpga/ip/nuand/tone_generator/vhdl/tone_generator.vhd b/hdl/fpga/ip/nuand/tone_generator/vhdl/tone_generator.vhd index 14058f89..d93cbb92 100644 --- a/hdl/fpga/ip/nuand/tone_generator/vhdl/tone_generator.vhd +++ b/hdl/fpga/ip/nuand/tone_generator/vhdl/tone_generator.vhd @@ -74,9 +74,6 @@ library work; use work.tone_generator_p.all; entity tone_generator is - generic ( - HOLDOVER_CLOCKS : natural := 16 - ); port ( clock : in std_logic; reset : in std_logic; @@ -86,15 +83,26 @@ entity tone_generator is end entity tone_generator; architecture arch of tone_generator is - type fsm_t is (IDLE, START_TONE, WAIT_FOR_COUNTDOWN, TAIL); + constant HOLDCOUNT : natural := 32; + + type fsm_t is (IDLE, WAIT_FOR_ACK, START_TONE, ACTIVE_TONE, FINISH_TONE, + WAIT_FOR_ZERO, HOLD); type state_t is record state : fsm_t; is_active : boolean; - countdown : natural; - tailcount : natural; - tone_in : tone_generator_input_t; + nco_reset : std_logic; + downcount : natural; + holdcount : natural; + tone_latch : tone_generator_input_t; nco_in : nco_input_t; + last_i : signed(15 downto 0); + last_q : signed(15 downto 0); + zero_hold_i : boolean; + zero_hold_q : boolean; + zero_xing_i : boolean; + zero_xing_q : boolean; + tone_pending : boolean; end record state_t; function NULL_STATE return state_t is @@ -102,13 +110,26 @@ architecture arch of tone_generator is begin rv.state := IDLE; rv.is_active := false; - rv.countdown := 0; - rv.tailcount := 0; - rv.tone_in := NULL_TONE_GENERATOR_INPUT; + rv.nco_reset := '1'; + rv.downcount := 0; + rv.holdcount := 0; + rv.tone_latch := NULL_TONE_GENERATOR_INPUT; rv.nco_in := (dphase => (others => '0'), valid => '0'); + rv.last_i := (others => '0'); + rv.last_q := (others => '0'); + rv.zero_hold_i := true; + rv.zero_hold_q := true; + rv.zero_xing_i := false; + rv.zero_xing_q := false; + rv.tone_pending := false; return rv; end function NULL_STATE; + function is_zero_cross(a : signed ; b : signed) return boolean is + begin + return (a >= 0 and b <= 0) or (a <= 0 and b >= 0); + end function is_zero_cross; + signal current : state_t; signal future : state_t; @@ -117,13 +138,13 @@ begin U_nco : entity work.nco port map ( clock => clock, - reset => reset, + reset => current.nco_reset, inputs => current.nco_in, outputs => nco_out ); - outputs.re <= nco_out.re; - outputs.im <= nco_out.im; + outputs.re <= (others => '0') when current.zero_hold_i else nco_out.re; + outputs.im <= (others => '0') when current.zero_hold_q else nco_out.im; outputs.valid <= nco_out.valid; outputs.active <= '1' when current.is_active else '0'; @@ -141,52 +162,161 @@ begin begin future <= current; - future.is_active <= false; - future.nco_in.valid <= '0'; + -- Keep last sample, so we can determine if we find a zero-crossing + if (nco_out.valid = '1') then + future.last_i <= nco_out.re; + future.last_q <= nco_out.im; + + future.zero_xing_i <= is_zero_cross(current.last_i, nco_out.re); + future.zero_xing_q <= is_zero_cross(current.last_q, nco_out.im); + end if; + + -- Latch valid inputs... + if (inputs.valid = '1') then + future.tone_latch <= inputs; + future.tone_pending <= true; + end if; + + -- Defaults: active if tone pending, nco_in valid, nco not reset + future.is_active <= current.tone_pending; + future.nco_in.valid <= '1'; + future.nco_reset <= '0'; case (current.state) is when IDLE => - if (inputs.valid = '1') then - future.tone_in <= inputs; - future.state <= START_TONE; + -- Zero all outputs, reset the NCO while idle + future.zero_hold_i <= true; + future.zero_hold_q <= true; + future.nco_reset <= '1'; + future.nco_in.valid <= '0'; + + -- We have a tone to do, folks + if (current.tone_pending) then + future.nco_reset <= '0'; + future.state <= WAIT_FOR_ACK; end if; - when START_TONE => - future.countdown <= to_integer(current.tone_in.duration); - future.tailcount <= HOLDOVER_CLOCKS; - future.nco_in.dphase <= current.tone_in.dphase; - future.nco_in.valid <= '1'; - future.is_active <= true; - future.state <= WAIT_FOR_COUNTDOWN; - - when WAIT_FOR_COUNTDOWN => - future.nco_in.valid <= '1'; - future.is_active <= true; - - if (current.countdown > 0) then - future.countdown <= current.countdown - 1; + when WAIT_FOR_ACK => -- Wait until valid is deasserted + if (inputs.valid = '0') then + future.state <= START_TONE; end if; - if (current.countdown = 0) then - future.state <= TAIL; + when START_TONE => -- Set up NCO, counters, etc + -- We are active + future.is_active <= true; + + -- Set up our downcounts + future.downcount <= to_integer(current.tone_latch.duration); + future.holdcount <= HOLDCOUNT; + + -- Configure the NCO + future.nco_in.dphase <= current.tone_latch.dphase; + + -- Update state + future.tone_pending <= false; + future.state <= ACTIVE_TONE; + + when ACTIVE_TONE => -- Tone is coming out + -- We are active + future.is_active <= true; + + -- Wait for a zero-crossing to un-zero NCO output + if (current.nco_in.dphase /= 0) then + if (current.zero_hold_i and current.zero_xing_i) then + future.zero_hold_i <= false; + end if; + + if (current.zero_hold_q and current.zero_xing_q) then + future.zero_hold_q <= false; + end if; + else + future.zero_hold_i <= true; + future.zero_hold_q <= true; end if; - when TAIL => - future.nco_in.dphase <= (others => '0'); - future.nco_in.valid <= '1'; - future.is_active <= true; - - if (current.tailcount > 0) then - future.tailcount <= current.tailcount - 1; + -- Countdown + if (current.downcount > 0) then + future.downcount <= current.downcount - 1; + else + future.state <= FINISH_TONE; end if; - if (current.tailcount = 0) then + when FINISH_TONE => -- Determine next action + -- Decrement hold count + if (current.holdcount > 0) then + future.holdcount <= current.holdcount - 1; + end if; + + -- We have a few situations to handle: + -- + -- current dphase next dphase next state + -- 1. 0 0 START_TONE + -- 2. 0 != 0 START_TONE + -- 3. 0 none IDLE + -- 4. not 0 0 Zero-xing -> START_TONE + -- 5. not 0 != 0 START_TONE + -- 6. not 0 none Zero-xing -> IDLE + + if (current.nco_in.dphase /= 0) then + -- Cases 4, 5, and 6: go to another state + future.state <= WAIT_FOR_ZERO; + else + if (current.tone_pending) then + -- Cases 1 and 2 + future.state <= WAIT_FOR_ACK; + else + -- Case 3 + future.state <= HOLD; + end if; + end if; + + when WAIT_FOR_ZERO => -- Wait for zero crossing + -- Decrement hold count + if (current.holdcount > 0) then + future.holdcount <= current.holdcount - 1; + end if; + + -- Snatch the next zero-crossing + if (not current.zero_hold_i and current.zero_xing_i) then + future.zero_hold_i <= true; + end if; + + if (not current.zero_hold_q and current.zero_xing_q) then + future.zero_hold_q <= true; + end if; + + -- We are going to handle these cases here: + -- + -- current dphase next dphase next state + -- 4. not 0 0 Zero-xing -> START_TONE + -- 5. not 0 != 0 START_TONE + -- 6. not 0 none Zero-xing -> IDLE + + if (current.tone_pending and current.tone_latch.dphase /= 0) + then + -- Case 5 + future.state <= WAIT_FOR_ACK; + elsif (current.zero_hold_i and current.zero_hold_q) then + if (current.tone_pending) then + -- Case 4 + future.state <= WAIT_FOR_ACK; + else + -- Case 6 + future.state <= HOLD; + end if; + end if; + + when HOLD => -- Keep the NCO spinning to avoid discontinuities + -- Decrement hold count + if (current.holdcount > 0) then + future.holdcount <= current.holdcount - 1; + else future.state <= IDLE; end if; - if (inputs.valid = '1') then - future.tone_in <= inputs; - future.state <= START_TONE; + -- We have a tone to do, folks + if (current.tone_pending) then + future.state <= WAIT_FOR_ACK; end if; end case;