--
--
--  v9918.vhd
--
--

library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_unsigned.all;

entity v9918 is
  port(
    pSltClk21    : IN std_logic;

    pSltClk     : IN std_logic;
    pSltRst_n   : IN std_logic;
    pSltSltsl_n : IN std_logic;
    pSltIorq_n  : IN std_logic;
    pSltRd_n    : IN std_logic;
    pSltWr_n    : IN std_logic;
    pSltAdr     : IN std_logic_vector(15 downto 0);
    pSltDat     : INOUT std_logic_vector(7 downto 0);
    pSltBdir_n  : OUT std_logic;
    pSltCs1     : IN std_logic;
    pSltCs2     : IN std_logic;
    pSltCs12    : IN std_logic;
    pSltRfsh_n  : IN std_logic;
    pSltWait_n  : IN std_logic;
    pSltInt_n   : IN std_logic;
    pSltM1_n    : IN std_logic;
    pSltMerq_n  : IN std_logic;
--    pSltClk2    : IN std_logic;
    pSltRsv5    : OUT std_logic;
    pSltRsv16   : OUT std_logic;

    pRamCeX_n   : OUT std_logic;
    pRamOeX_n   : OUT std_logic;
    pRamWeX_n   : OUT std_logic;
    pRamAdrX    : OUT std_logic_vector(18 downto 0);
    pRamDatX    : INOUT std_logic_vector(7 downto 0);

    pRamCeY_n   : OUT std_logic;
    pRamOeY_n   : OUT std_logic;
    pRamWeY_n   : OUT std_logic;
    pRamAdrY    : OUT std_logic_vector(18 downto 0);
    pRamDatY    : INOUT std_logic_vector(7 downto 0);

-- Vide Output
    pVideoR	: OUT std_logic_vector( 5 downto 0);
    pVideoG	: OUT std_logic_vector( 5 downto 0);
    pVideoB	: OUT std_logic_vector( 5 downto 0);

    pVideoHS_n	: OUT std_logic;
    pVideoVS_n	: OUT std_logic;
    pVideoCS_n	: OUT std_logic
 );
end v9918;


architecture RTL of v9918 is
  signal pSltClk_n   : std_logic;
-- H counter
  signal h_counter : std_logic_vector(10 downto 0);
  signal v_counter : std_logic_vector(10 downto 0);
  signal field	: std_logic;
-- sync state register
  signal sstate : std_logic_vector( 1 downto 0);

  constant sstate_A : std_logic_vector := "00";
  constant sstate_B : std_logic_vector := "01";
  constant sstate_C : std_logic_vector := "10";
  constant sstate_D : std_logic_vector := "11";

  signal VideoHS_n : std_logic;
  signal VideoVS_n : std_logic;

  signal output :std_logic;
-- グラフィック表示エリア検出用
  signal window_x :std_logic;
  signal window_y :std_logic;
  signal window :std_logic;
-- 周辺枠エリア検出用
  signal bwindow_x :std_logic;
  signal bwindow_y :std_logic;
  signal bwindow :std_logic;
begin

  ----------------------------------------------------------------
  -- Dummy pin
  ----------------------------------------------------------------
  pSltRsv5  <= '1';
  pSltRsv16 <= '1';

  pSltClk_n <= not pSltClk;

  pSltBdir_n <= '0' when pSltSltsl_n = '0' and pSltRd_n = '0' else '1';

  pSltDat   <= (others => 'Z');

  pVideoCS_n <= VideoHS_n xor VideoVS_n;
  pVideoHS_n <= VideoHS_n;
  pVideoVS_n <= VideoVS_n;

  process( pSltClk21, pSltRst_n )
  begin
    if (pSltRst_n = '0') then
		h_counter <= (others => '0');
		v_counter <= (others => '0');
		VideoHS_n <= '1';
		VideoVS_n <= '1';
		pVideoR <= "000000";
		pVideoG <= "000000";
		pVideoB <= "000000";
    elsif (pSltClk21'event and pSltClk21 = '1') then
	if( h_counter = 1363 ) then
	 	h_counter <= (others => '0' );
	else
		h_counter <= h_counter + 1;
	end if;

	if( (h_counter = 681) or (h_counter = 1363) ) then
		-- 525 ライン * 2 = 1050
		if( v_counter = 1049 ) then
			v_counter <= (others => '0');
		else 
			v_counter <= v_counter + 1;
		end if;
	end if;

	if( (v_counter = 0) or
		(v_counter = 12) or
		(v_counter = 525) or
		(v_counter = 537) ) then 
		sstate <= sstate_A;
	elsif( (v_counter = 6) or
		(v_counter = 531) ) then 
		sstate <= sstate_B;
	elsif( (v_counter = 18) or
		(v_counter = 543) ) then 
		sstate <= sstate_C;
	end if;

-- field 信号生成
	if( v_counter = 525 ) then
		field <= '1';
	elsif( v_counter = 0 ) then
		field <= '0';
	end if;

-- H syncパルス発生
	if( sstate = sstate_A ) then
		if( (h_counter = 1) or (h_counter = 1+682) ) then
			VideoHS_n <= '0';		-- pulse on
 		elsif( (h_counter = 51) or (h_counter = 51+682) ) then
			VideoHS_n <= '1';		-- pulse off
		end if;
	elsif( sstate = sstate_B ) then
		if( (h_counter = 1364-100+1) or (h_counter = 682-100+1) ) then
			VideoHS_n <= '0';		-- pulse on
 		elsif( (h_counter = 1) or (h_counter = 1+682) ) then
			VideoHS_n <= '1';		-- pulse off
		end if;
	elsif( sstate = sstate_C ) then
		if( h_counter = 1 ) then
			VideoHS_n <= '0';		-- pulse on
 		elsif( h_counter = 101 ) then
			VideoHS_n <= '1';		-- pulse off
		end if;
	end if;

-- V syncパルス発生
	if( sstate = sstate_B ) then
		VideoVS_n <= '0';
	else
		VideoVS_n <= '1';
	end if;	

-- window 信号生成
	if( h_counter = 230-1 ) then
		window_x <= '1';
	elsif( h_counter = 230+1024-1 ) then
		window_x <= '0';
	end if;

	if( (v_counter = 60) or (v_counter = 525+60) ) then
		window_y <= '1';
	elsif( (v_counter = 60+212*2) or (v_counter = 525+60+212*2) ) then
		window_y <= '0';
	end if;

	if( (window_x = '1') and (window_y = '1') )then
		window <= '1';
	else
		window <= '0';
	end if;

-- bwindow 信号生成
	if( h_counter = 200-1 ) then
		bwindow_x <= '1';
	elsif( h_counter = 1363-1 ) then
		bwindow_x <= '0';
	end if;

	if( (v_counter = 10*2-1) or (v_counter = 525+10*2-1) ) then
		bwindow_y <= '1';
	elsif( (v_counter = 524-1) or (v_counter = 525+524-1) ) then
		bwindow_y <= '0';
	end if;

	if( (bwindow_x = '1') and (bwindow_y = '1') )then
		bwindow <= '1';
	else
		bwindow <= '0';
	end if;

-- test color 発生
	if( h_counter = 400 ) then
		output <= '1';
	elsif( h_counter = 1000 ) then
		output <= '0';
	end if;

	if( bwindow = '1' ) then
		if( (window = '1') and (output = '1') ) then
			pVideoR <= "001110";
			pVideoG <= "001110";
--			pVideoB <= "001110";
--			if( field = '1' ) then
--				pVideoB <= "011111";
--			else
				pVideoB(0) <= h_counter(1);
				pVideoB(1) <= h_counter(2);
				pVideoB(2) <= h_counter(3);
				pVideoB(3) <= h_counter(4);
				pVideoB(4) <= h_counter(5);
				pVideoB(5) <= '0';
--			end if;
		elsif( (window = '1') and (output = '0') ) then
			pVideoR <= "000000";
			pVideoG <= "000000";
			pVideoB <= "000000";
		else
                        -- 外枠は青
			pVideoR <= "000000";
			pVideoG <= "000000";
			pVideoB <= "010000";
		end if;
	else
		pVideoR <= "000000";
		pVideoG <= "000000";
		pVideoB <= "000000";
	end if;
    end if;
  end process;

end RTL;
