--------------------------------------------------------------------------------
-- Company: 
-- Engineer:
--
-- Create Date:   17:16:42 13.05.2007
-- Design Name:   tb_mix_cpx
-- Module Name:   tb_mix_cpx.vhd
-- Project Name:  mix_cpx
-- Target Device:  
-- Tool versions:  
-- Description:   
-- 
--------------------------------------------------------------------------------
LIBRARY ieee;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.MATH_REAL.ALL;
USE ieee.numeric_std.ALL;
use std.textio.all; --  Imports the standard textio package.

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

library work;
use work.PCK_FIO.all;
use work.fixed_util_pkg.all;

ENTITY tb_cic_decim_pipe IS
	Generic
	(
		nstages			: integer := 3;
		ratio			: integer := 64;
		scale_nbits		: integer := 18;
		in_width		: integer := 18;
		in_width_frac	: integer := 16;
		out_width		: integer := 18;
		out_width_frac	: integer := 16
	);
END tb_cic_decim_pipe;

ARCHITECTURE behavior OF tb_cic_decim_pipe IS 

	-- Component Declaration for the Unit Under Test (UUT)
	COMPONENT cic_decim_pipe
	GENERIC
	(
		nstages			: integer;
		ratio			: integer;
		scale_nbits		: integer;
		in_width		: integer;
		in_width_frac	: integer;
		out_width		: integer;
		out_width_frac	: integer
	);
	PORT
	(
		clk				: in std_logic;
		rst				: in std_logic;
		vld_in			: in std_logic;
		din				: in sfixed;
		dout			: out sfixed;
		vld_out 		: out std_logic
	);
	END COMPONENT;

	--Constants
	constant PERIOD : time := 10 ns;
	

	--Inputs
	SIGNAL clk				: std_logic := '0';
	SIGNAL srst				: std_logic := '1';
	SIGNAL vld_in			: std_logic := '0';
	SIGNAL din				: sfixed(shi(in_width, in_width_frac) downto slo(in_width, in_width_frac));
	
	--Outputs
	SIGNAL dout				: sfixed(shi(out_width, out_width_frac) downto slo(out_width, out_width_frac));
	SIGNAL vld_out			: std_logic;
	
	SIGNAL fileout_enable   : std_logic := '0';

	signal XR, YR		: real;
	
BEGIN

	XR	<= to_real(din);
	YR	<= to_real(dout);
	
	-- Instantiate the Unit Under Test (UUT)
	uut : cic_decim_pipe
	GENERIC MAP
	(
		nstages			=> nstages,
		ratio			=> ratio,
		scale_nbits		=> scale_nbits,
		in_width		=> in_width,
		in_width_frac	=> in_width_frac,
		out_width		=> out_width,
		out_width_frac	=> out_width_frac
	)
	PORT MAP
	(
		clk				=> clk,
		rst				=> srst,
		vld_in			=> vld_in,
		din				=> din,
		dout			=> dout,
		vld_out			=> vld_out
	);

 	tb_clk : PROCESS
	BEGIN
		clk <= not clk;
		wait for PERIOD/2;
	END PROCESS;

	tb : PROCESS

	BEGIN

		-- Wait 100 ns for global reset to finish
		wait for 4*PERIOD;
		srst <= '0';
		
		------------------------------------------
		wait for 2*PERIOD;
		wait until rising_edge(clk);
		din		<= to_sfixed(0.0, din);
		vld_in	<= '1';

		wait for 49*PERIOD;
		wait until rising_edge(clk);

		din		<= to_sfixed(0.5, din);

		wait for 4999*PERIOD;
		wait until rising_edge(clk);

		din		<= to_sfixed(-0.5, din);

		wait for 99*PERIOD;
		wait until rising_edge(clk);

		din		<= to_sfixed(0.0, din);

		wait for 499*PERIOD;
		wait until rising_edge(clk);
		vld_in	<= '0';

		wait; 
	END PROCESS;
	
END;
