library ieee;
library flasher; 

use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;  

use flasher.test_pkg.all;
use flasher.ks8695px_bfm_pkg.all;
use flasher.ks8695px_signals_pkg.all; 

use work.globals.all;

entity ks8695px_tst1 is
end ks8695px_tst1;

architecture ks8695px_tst1_hdl of ks8695px_tst1 is

   -- drive reset_n signal X number of clocks
   procedure reset (
      constant p_cnt    : in integer;
      signal p_reset_n  : out std_logic
   ) is
   begin
      p_reset_n <= '1';
      resetloop: for i in 1 to p_cnt loop
         wait on clock until clock = '1';
         p_reset_n <= '0';
      end loop resetloop;
      p_reset_n <= '1';	
	  for i in 1 to 10 loop	   
         wait until rising_edge(clock);
	  end loop;
   end reset;  

begin

   ---------------------------------------------------------------
   -- Test process where all our BFM commands will be executed
   -- from.  In this example, reset is driven from the test
   -- and only one process can execute cycles to the IBC BFM.  
   ---------------------------------------------------------------

   test: process  
   	  variable data_returned : std_logic_vector(15 downto 0);
   begin

      -----------------------------------------------------------
      -- This signal is located in the ibc_bfm_pkg.vhd file,
      -- used to issue debugging cycle information from the BFM
      -----------------------------------------------------------
      --ibc_debug <= true;

      -- reset sequence
      reset(10,async_reset_n);

      ---------------------------------------------------------------
      -- basic read/write cycles using either std_logic or 
      -- string input format (overloaded in the ibc_bfm_pkg.vhd file)
      ---------------------------------------------------------------
 	  
      wr(PeriodRegisterAddress		,X"000A",cmd);
      wr(FlashOnRegisterAddress		,X"0004",cmd);	   
      wr(FlashOffRegisterAddress	,X"0002",cmd);	
      wr(SampleOnRegisterAddress	,X"0005",cmd);	   
      wr(SampleOffRegisterAddress	,X"0001",cmd); 
	  wr(FlashesRegisterAddress		,X"0007",cmd);
      rd(PeriodRegisterAddress		,X"000A",cmd);
      rd(FlashOnRegisterAddress		,X"0004",cmd);	   
      rd(FlashOffRegisterAddress	,X"0002",cmd);	
      rd(SampleOnRegisterAddress	,X"0005",cmd);	   
      rd(SampleOffRegisterAddress	,X"0001",cmd);
	  rd(FlashesRegisterAddress		,X"0007",cmd);
      rd(RevisionRegisterAddress	,X"BEEF",cmd);	
	  wr(CommandRegisterAddress		,X"0001",cmd);		 
	  
	  polling_loop : loop  	
		  rd(StatusRegisterAddress  ,X"0000",cmd);
		  get_rdata(data_returned, cmd);
		  exit when data_returned(0) = '1';
	  end loop polling_loop;
		  
      wr(PeriodRegisterAddress		,X"0100",cmd);
      wr(FlashOnRegisterAddress		,X"0040",cmd);	   
      wr(FlashOffRegisterAddress	,X"0020",cmd);	
      wr(SampleOnRegisterAddress	,X"0050",cmd);	   
      wr(SampleOffRegisterAddress	,X"0010",cmd); 
	  wr(FlashesRegisterAddress		,X"000F",cmd);
      rd(PeriodRegisterAddress		,X"0100",cmd);
      rd(FlashOnRegisterAddress		,X"0040",cmd);	   
      rd(FlashOffRegisterAddress	,X"0020",cmd);	
      rd(SampleOnRegisterAddress	,X"0050",cmd);	   
      rd(SampleOffRegisterAddress	,X"0010",cmd);
	  rd(FlashesRegisterAddress		,X"000F",cmd);
      rd(RevisionRegisterAddress	,X"BEEF",cmd);	
	  wr(CommandRegisterAddress		,X"0001",cmd); 
	  
	  idle(cmd);

	   wait for 10 ms;
      -----------------------------------------------------------------
      -------------- END OF TEST routine, do not edit! ----------------
      -----------------------------------------------------------------
      assert false report "*** TEST IS FINISHED ***" severity failure;
      wait for 1 ns;    -- required so compiler doesn't complain!
      -----------------------------------------------------------------

   end process test;

end ks8695px_tst1_hdl;

