--------------------------------------------------------------------------
-- Project: JIPS, a portable 32-bit RISC CPU written in VHDL
-- This file: Types, constants and functions for JIPS
--
-- Copyright (C) 2008 J. Ahrensfeld
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program 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 General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
--
-- 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;
use IEEE.MATH_REAL.ALL;

--------------------------------------------------------------------------
package mips_util_pkg is


	function to_mips_01(x : unsigned) return unsigned;
	function lg2(x : natural) return natural;
	function po2(x : natural) return natural;

end mips_util_pkg;

--------------------------------------------------------------------------
package body mips_util_pkg is

	function to_mips_01(x : unsigned) return unsigned is
	variable result : unsigned (x'left downto x'right);
	begin
		for i in x'left downto x'right loop
			if x(i) = '1' then
				result(i)	:= '1';
			else 
				result(i)	:= '0';
			end if;
		end loop;
		return result;
	end to_mips_01;

	function lg2(x : natural) return natural is
	begin
		return natural(ceil(log2(real(x))));
	end lg2;	
	
	function po2(x : natural) return natural is
	begin

		return 2**lg2(x);
	end po2;	

--------------------------------------------------------------------------
end mips_util_pkg;

