-------------------------------------------------------------------------------- -- UPC - EETAC - EEL - CSD - DIGSYS http://digsys.upc.edu -------------------------------------------------------------------------------- -- P8. Tutorial on synchronous frequency dividers -- Divider by 2 with asynchronous clear direct (CD), count enable (CE) and -- terminal count (TC2)to allow chaining with similar blocks. -- TC2 is active high only one CLK period every 2, in this case a square -- waveform if CE = 1 -- Plan: FSM -- Reference schematic: -------------------------------------------------------------------------------- LIBRARY IEEE; USE IEEE.STD_LOGIC_1164.ALL; USE IEEE.STD_LOGIC_ARITH.ALL; USE IEEE.STD_LOGIC_UNSIGNED.ALL; ENTITY freq_div_2 IS PORT(CD,CLK,CE : IN std_logic; TC2 : OUT std_logic ); END freq_div_2; ARCHITECTURE FSM_like OF freq_div_2 IS -- "Max_Count" is 2 - 1 = 1 in binary (the last state of the counter). CONSTANT Max_Count : STD_LOGIC := '1'; -- 1 CONSTANT Reset : STD_LOGIC := '0'; -- Internal wires SIGNAL present_state,future_state: STD_LOGIC := Reset; -- Remember that this idea of ":= Reset;" initalising the wires, has sense only -- for the simulator, not for a real synthesised flatened circuit, -- in which CD is used to reset the flip-flops BEGIN ------ The only clocked block : the state register state_register: PROCESS (CD, CLK, future_state) BEGIN IF CD = '1' THEN -- reset counter present_state <= Reset; ELSIF (CLK'EVENT and CLK = '1') THEN -- D-type flip-flop present_state <= future_state; END IF; END PROCESS state_register; ------ CC1. combinational system for calculating next state CS_1: PROCESS (present_state, CE) BEGIN IF CE = '1' THEN IF(present_state = Max_Count ) THEN -- because there are only 2 states --> toggle future_state <= Reset; ELSE future_state <= Max_Count; END IF; ELSE future_state <= present_state; -- count disable END IF; END PROCESS CS_1; ------ CC2: Combinational circuit for calculating output logic TC2 <= '1' WHEN ((present_state = Max_count) AND CE = '1') ELSE '0'; END FSM_like;