-------------------------------------------------------------------------
-- Project: FIFO, generic FIFOs written in VHDL
-- Release 1
--
-- Copyright (C) 2007 J. Ahrensfeld
--
-- This library is free software; you can redistribute it and/or
-- modify it under the terms of the GNU Lesser General Public
-- License as published by the Free Software Foundation; either
-- version 2.1 of the License, or (at your option) any later version.
--
-- This library is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-- Lesser General Public License for more details.
--
-- You should have received a copy of the GNU Lesser General Public
-- License along with this library; if not, write to the Free Software
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
--
-- For questions and ideas, please contact the author at jens@jayfield.org
--
-----------------------------------------------------------------------

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;

