LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.NUMERIC_STD.ALL;

package fifo_ctrl_pkg is

	-- Constants
	-- Types
	
	-- Functions
	function bin2gray(xb : unsigned) return unsigned;
	function gray2bin(xg : unsigned) return unsigned;

end fifo_ctrl_pkg;

package body fifo_ctrl_pkg is

	function bin2gray(xb : unsigned) return unsigned is
	variable xg : unsigned(xb'left downto xb'right);
	begin
	
    	xg(xg'left) := xb(xb'left);
		for i in 1 to xb'left loop
			xg(xg'left-i) := xb(xb'left-i+1) xor xb(xb'left-i);
		end loop;
	
		return xg;
	end bin2gray;
	
	function gray2bin(xg : unsigned) return unsigned is
	variable xb : unsigned(xg'left downto xg'right);
	begin

     	xb(xb'left) := xg(xg'left);
		for i in 1 to xg'left loop
			xb(xb'left-i) := xb(xb'left-i+1) xor xg(xg'left-i);
		end loop;
		
		return xb;
	end gray2bin;

end fifo_ctrl_pkg;

