Advertisement
playduck

VHDL Gray Code to Binary

Oct 19th, 2024
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 1.40 KB | Source Code | 0 0
  1. library IEEE;
  2. use IEEE.STD_LOGIC;
  3. use IEEE.NUMERIC_STD.ALL;
  4.  
  5. -- Entity to convert Gray code to binary
  6. entity gray_code_converter is
  7.     Generic (
  8.         WIDTH : positive := 4
  9.     );
  10.     Port (
  11.         clk : in  STD_LOGIC;
  12.         rst : in  STD_LOGIC;
  13.         gray : in  STD_LOGIC_VECTOR (WIDTH-1 downto 0);
  14.         binary : out  STD_LOGIC_VECTOR (WIDTH-1 downto 0)
  15.     );
  16. end gray_code_converter;
  17.  
  18. architecture Behavioral of gray_code_converter is
  19.     signal binary_int : STD_LOGIC_VECTOR (WIDTH-1 downto 0);
  20. begin
  21.     -- Convert Gray code to binary
  22.     process(clk)
  23.     begin
  24.         if rising_edge(clk) then
  25.             if rst = '1' then
  26.                 binary_int <= (others => '0');
  27.             else
  28.                 -- MSB of binary output is the same as MSB of Gray code
  29.                 binary_int(binary_int'high) <= gray(gray'high);
  30.  
  31.                 -- Calculate remaining bits of binary output
  32.                 for i in binary_int'high-1 downto 0 loop
  33.                     binary_int(i) <= gray(i+1) xor gray(i);
  34.                 end loop;
  35.             end if;
  36.         end if;
  37.     end process;
  38.  
  39.     -- Pipeline the output
  40.     process(clk)
  41.     begin
  42.         if rising_edge(clk) then
  43.             if rst = '1' then
  44.                 binary <= (others => '0');
  45.             else
  46.                 binary <= binary_int;
  47.             end if;
  48.         end if;
  49.     end process;
  50. end Behavioral;
Tags: VHDL Gray Code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement