
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use ieee.numeric_std.all;
use IEEE.MATH_REAL.ALL;

library ieee_proposed;
use ieee_proposed.fixed_float_types.all;
use ieee_proposed.fixed_pkg.all;

library work;
use work.fixed_util_pkg.all;

-------------------------------------------------------------------------------
package nco_pkg is

	constant tpd : time := 0 ns;

	type phase_relation_t is (phase_90deg, phase_270deg);
	
	type debug_out_t is record 
		lfsr : unsigned(31 downto 0);
		i_ud : real;
		q_ud : real;
		i_d : real;
		q_d : real;
	end record;

	constant lfsr_poly : natural_array_t(3 to 24) := 
	(
		12,			-- 3
		24,			-- 4
		58,			-- 5
		114,		-- 6
		210,		-- 7
		500,		-- 8
		1000,		-- 9
		1824,		-- 10
		3712,		-- 11
		7184,		-- 12
		14592,		-- 13
		28674,		-- 14
		59392,		-- 15
		106512,		-- 16
		245760,		-- 17
		466944,		-- 18
		933888,		-- 19
		1654784,	-- 20
		3735552,	-- 21
		7342080,	-- 22
		14745600,	-- 23
		14811138	-- 24
	);
	
	function sin_tbl(nsamples : integer; omega, phi_in : real) return real_array_t;
	function cos_tbl(nsamples : integer; omega, phi_in : real) return real_array_t;
	
end; -- package nco_pkg;

-------------------------------------------------------------------------------
package body nco_pkg is

	function sin_tbl(nsamples : integer; omega, phi_in : real) return real_array_t is
	variable tbl : real_array_t (0 to nsamples-1);
	variable phi : real := phi_in;
	variable dphi : real := 2.0*pi*omega;

	begin
		for i in 0 to nsamples-1 loop
			tbl(i) := sin(phi);
			phi := phi + dphi;
		end loop;

		return tbl;
	end sin_tbl;

	-------------------------------------------------------------
	function cos_tbl(nsamples : integer; omega, phi_in : real) return real_array_t is
	variable tbl : real_array_t (0 to nsamples-1);
	variable phi : real := phi_in;
	variable dphi : real := 2.0*pi*omega;

	begin
		for i in 0 to nsamples-1 loop
			tbl(i) := cos(phi);
			phi := phi + dphi;
		end loop;

		return tbl;
	end cos_tbl;

-------------------------------------------------------------------------------
end; -- package nco_pkg;
