-- VHDL Frequency Counter is Copyright (c) 2011 by Jason Burke
-- You are free to use this work, or any derivative works for
-- whatever purpose you wish so long as this copyright notice
-- is retained in the source code and mentioned in any associated
-- documentation or display of other copyright notices.
--
-- Revision 1.1

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
--use IEEE.STD_LOGIC_UNSIGNED.ALL;

-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
-- use IEEE.NUMERIC_STD.ALL;

entity freq_counter is
    Port ( clk_ref   : in     STD_LOGIC;                    -- clock reference (4MHz)
           fin       : in     STD_LOGIC;                    -- signal to be counted
           seg       : out    STD_LOGIC_VECTOR(6 downto 0) := "0000000";-- 7-segment output (a=lsb)
           disp      : buffer STD_LOGIC_VECTOR(5 downto 0) := "111110"-- active low for 5 displays
           );
end freq_counter;

architecture Behavioral of freq_counter is
   Type bcd_array_t is ARRAY(0 to 5) of UNSIGNED(3 downto 0);

   Signal count_gate : UNSIGNED(17 downto 0) := (others => '0');-- counter for gate clock
   Signal count_disp : UNSIGNED(10 downto 0) := b"100_0000_0000";-- counter for display clock
   Signal clk_gate   : STD_LOGIC := '0';                    -- gate clock output
   Signal clk_disp   : STD_LOGIC := '0';                    -- display clock output
   Signal bcd_r      : bcd_array_t;                         -- full count for each counter from last gate time
   Signal bcd        : UNSIGNED(3 downto 0) := "0000";      -- bcd value to be output
begin

   -- implement a 7-segment decoder
   SSD: BLOCK
   BEGIN
      seg <= "0111111" when bcd = 0 else                    -- decode 0 to 7-segment
             "0000110" when bcd = 1 else                    -- decode 1 to 7-segment
             "1011011" when bcd = 2 else                    -- decode 2 to 7-segment
             "1001111" when bcd = 3 else                    -- decode 3 to 7-segment
             "1100110" when bcd = 4 else                    -- decode 4 to 7-segment
             "1101101" when bcd = 5 else                    -- decode 5 to 7-segment
             "1111101" when bcd = 6 else                    -- decode 6 to 7-segment
             "0000111" when bcd = 7 else                    -- decode 7 to 7-segment
             "1111111" when bcd = 8 else                    -- decode 8 to 7-segment
             "1101111" when bcd = 9 else                    -- decode 9 to 7-segment
             "0111110";                                     -- invalid BCD input
   END BLOCK SSD;
          
   -- implement the gate clock by dividing clk_ref clock down to 10 Hz
   process
      begin
         wait until (clk_ref'EVENT and clk_ref='1');
            if count_gate < 199999 then            -- check for maximum count (must be < 2^18)
               count_gate <= count_gate + 1;       -- increment counter
            else
               clk_gate <= not clk_gate;           -- toggle gate clock
               count_gate <= (others => '0');      -- reset counter
            end if;
   end process;

   -- implement the display clock by dividing clk_ref clock downto 1000 Hz
   process
      begin
         wait until (clk_ref'EVENT and clk_ref='1');
            if count_disp < 1999 then              -- check for maximum count (must be < 2^12)
               count_disp <= count_disp + 1;       -- increment counter
            else
               clk_disp <= not clk_disp;           -- toggle gate clock
               count_disp <= (others => '0');      -- reset counter
            end if;
   end process;

   -- Count positive edges on fin
   -- Note:  There are inferred latches in this design for clk_edge and bcd_r.  This is not
   --        the canonical way to implement this circuit and is in theory subject to race
   --        conditions which could cause an occasional miscount.  However, for the purpose
   --        of displaying the frequency of the input to a user, it works fine and I have
   --        never witnessed any display anomalies myself.
   process(fin,clk_gate)
      variable clk_edge : STD_LOGIC := '1';        -- edge detection of clk_gate
      variable bcd_array : bcd_array_t;            -- bcd counter array
      
      begin
         if (clk_gate='1' and clk_edge='1') then
            clk_edge := '0';                       -- only reset once per gate period
            bcd_r <= bcd_array;                    -- save present counts
            for i in bcd_r'RANGE loop
               bcd_array(i) := "0000";             -- reset bcd counters
            end loop;
         elsif (clk_gate='0' and clk_edge='0') then
            clk_edge := '1';                       -- enable edge detect on clk_gate
         elsif rising_edge(fin) then               -- count rising edges on fin
            if bcd_array(0) /= 9 then
               bcd_array(0) := bcd_array(0) + 1;   -- increment 10Hz BCD counter
            elsif bcd_array(1) /= 9 then
               bcd_array(0) := "0000";
               bcd_array(1) := bcd_array(1) + 1;   -- increment 100Hz BCD counter
            elsif bcd_array(2) /= 9 then
               bcd_array(0) := "0000";
               bcd_array(1) := "0000";
               bcd_array(2) := bcd_array(2) + 1;   -- increment 1KHz BCD counter
            elsif bcd_array(3) /= 9 then
               bcd_array(0) := "0000";
               bcd_array(1) := "0000";
               bcd_array(2) := "0000";
               bcd_array(3) := bcd_array(3) + 1;   -- increment 10KHz BCD counter
            elsif bcd_array(4) /= 9 then
               bcd_array(0) := "0000";
               bcd_array(1) := "0000";
               bcd_array(2) := "0000";
               bcd_array(3) := "0000";
               bcd_array(4) := bcd_array(4) + 1;   -- increment 100KHz BCD counter
            else
               bcd_array(0) := "0000";
               bcd_array(1) := "0000";
               bcd_array(2) := "0000";
               bcd_array(3) := "0000";
               bcd_array(4) := "0000";
               bcd_array(5) := bcd_array(5) + 1;   -- increment 1MHz BCD counter
            end if;
         end if;
   end process;
   
   -- Manage LED displays
   -- For each rising edge of clk_disp, we copy one of the six saved counts to the display
   -- indicated by port disp and then we rotate the bits in disp.  This multiplexed
   -- arrangement saves us from creating six separate seven-segment decoders, and saves us
   -- from having to wire up separate lines for each of six seven-segment decoders as well.
   -- The price we pay is having to update the displays fast enough to avoid any noticable
   -- flicker and to drive the displays harder because we have a 1/6 duty cycle.
   process
      begin
         wait until (clk_disp'EVENT and clk_disp='1');-- wait one clock period
            case disp is
               when "011111" =>                    -- least significant digit (will be rotated left at end of process)
                  bcd <= bcd_r(0);                 -- copy 10Hz count from bcd_shadow to current display position
               when "111110" =>
                  bcd <= bcd_r(1);                 -- copy 100Hz count from bcd_shadow to current display position
               when "111101" =>
                  bcd <= bcd_r(2);                 -- copy 1KHz count from bcd_shadow to current display position
               when "111011" =>
                  bcd <= bcd_r(3);                 -- copy 10KHz count from bcd_shadow to current display position
               when "110111" =>
                  bcd <= bcd_r(4);                 -- copy 100KHz count from bcd_shadow to current display position
               when "101111" =>
                  bcd <= bcd_r(5);                 -- copy 1MHz count from bcd_shadow to current display position
               when others =>
                  null;
            end case;
            
            -- rotate display position to the left by one position
            if disp = "011111" then
               disp <= "111110";
            else
               disp <= disp(disp'LEFT-1 downto 0) & '1';
            end if;
   end process;
   
end Behavioral;
