--- /dev/null
+--------------------------------------------------
+-- Convert gray code to a binary code
+-- Tobias Weber
+-- Ruhr University Bochum
+-- based on code by Niklaus Berger
+-------------------------------------------------
+library ieee;
+use ieee.std_logic_1164.all;
+use ieee.numeric_std.all;
+
+entity gray_to_binary is
+ generic(NBITS : integer := 10);
+ port (
+ clk : in std_logic; -- clk input
+ reset : in std_logic; -- reset input
+ gray_in : in std_logic_vector (NBITS - 1 downto 0); -- gray counter input
+ bin_out : out std_logic_vector (NBITS - 1 downto 0) -- binary counter output
+ );
+end gray_to_binary;
+
+architecture rtl of gray_to_binary is
+
+begin
+
+ process(clk)
+ variable decoding : std_logic_vector(NBITS - 1 downto 0);
+ begin
+ if rising_edge(clk) then
+ if reset = '1' then
+ bin_out <= (others => '0');
+ else
+ decoding(NBITS - 1) := gray_in(NBITS - 1);
+ for i in NBITS - 2 downto 0 loop
+ decoding(i) := gray_in(i) xor decoding(i + 1);
+ end loop;
+ bin_out <= decoding;
+ end if;
+ end if;
+ end process;
+
+end rtl;