grayCount7.vhd


--
-- 7-bit gray counter
--

library ieee;
use ieee.std_logic_1164.all;

entity grayCount7 is
     port( c : in std_logic;
         nr : in std_logic;
         q : out std_logic_vector(6 downto 0));
end grayCount7;

architecture rtl of grayCount7 is

     signal jCount : std_logic_vector(1 downto 0); -- 2-bit jonson counter
     signal jCount01 : std_logic; -- jCount = "01"
     signal jCount10 : std_logic; -- jCount = "10"

     signal bufQ : std_logic_vector(6 downto 1); -- gray counter(without bit-0)
     signal invQ : std_logic_vector(6 downto 1); -- invert pattern

begin

-- ### register discription ###
     syncModule : process(nr,c)
     begin
           if (nr = '0') then
                jCount <= (others => '0'); -- asynchronous rest
                bufQ <= (others => '0');
           elsif (c'event and c = '1') then
                jCount <= jCount(0) & (not jCount(1)); -- jonson counter
                bufQ <= bufQ xor invQ; -- invert gray-counter's bit
           end if;
     end process;

-- ### logic discription ###
-- *** decord jonson counter ***
     jCount10 <= '1' when (jCount = "10") else '0';
     jCount01 <= '1' when (jCount = "01") else '0';

-- *** generate invert pattern ***
     invQ(6) <= jCount10 and (not bufQ(4)) and (not bufQ(3)) and (not bufQ(2)) and (not bufQ(1));
     invQ(5) <= jCount10 and bufQ(4) and (not bufQ(3)) and (not bufQ(2)) and (not bufQ(1));
     invQ(4) <= jCount10 and bufQ(3) and (not bufQ(2)) and (not bufQ(1));
     invQ(3) <= jCount10 and bufQ(2) and (not bufQ(1));
     invQ(2) <= jCount10 and bufQ(1);
     invQ(1) <= jCount01;

-- *** assign output data ***
     q(6 downto 1) <= bufQ;
     q(0) <= jCount(0);

end rtl;