TITLE "SINC_controller";

Constant HDD_TYPE = 1;

SUBDESIGN SP047032
	(
	EPM_RES		: INPUT;
	HD_WE[2..1]	: OUTPUT;
	HD_OE[2..1]	: OUTPUT;
	HD_CS[2..1]	: OUTPUT;
	HD_CSS[2..1]: OUTPUT;

	CLK_WG		: OUTPUT;
	RDATA		: INPUT;
	QDAT		: OUTPUT;
	RDAT		: OUTPUT;

	D0			: INPUT;
	MA0			: INPUT;
	STE			: INPUT;
	WSTB,RSTB	: INPUT;

	/WR			: INPUT;
	/RD			: INPUT;
	HDD_CS		: INPUT;

	/MR,/IO		: INPUT;
	CLKZZ		: OUTPUT;

	/RTSB,/DTRB	: INPUT;

	10K_D0		: OUTPUT;
	10K_CLK		: OUTPUT;
	/CONFIG		: OUTPUT;
	CNF_ON		: INPUT;
	TG42_OUT	: OUTPUT;
	TG42_IN		: INPUT;

	CLK_PRC		: OUTPUT;

	MA7			: INPUT;

--	/WRH		: INPUT;
	/WRH		: OUTPUT;

--	/RDH		: OUTPUT; -> NA HD_CS1

	)
VARIABLE

	CT[2..0]	: DFF;
	CLKZZ1		: NODE;
	STWG[2..0]	: DFF;
	WGR[4..0]	: DFF;
	CT_WG		: NODE;
	CT_WG1		: NODE;
	TB_WG		: NODE;
	CODE[4..0]	: DFF;
	TURBING		: NODE;
	RDAT_X		: NODE;
	HD_CS_CLK	: NODE;
	MXX			: NODE;

BEGIN

--	====== Oscillator ==========
	TG42_OUT 	= EXP(TG42_IN);
--	TG42_OUT 	= !(TG42_IN);

--	tG42_OUT 	= LCELL(EXP(TG42_IN));
--	============================

--	====== FLEX Loading ========

	10K_CLK		= LCELL((/MR or /WR or CNF_ON) &
--						(/IO or /WR or HDD_CS or !CNF_ON) &
						(/MR or /RD or HDD_CS or !CNF_ON));
	CODE[].clk	= 10K_CLK;
	CODE[].clrn	= /CONFIG & (/IO or /RD or HDD_CS);

IF !CNF_ON THEN
	CODE[].d	= (CODE[3..0],(CODE2 xor 10K_D0));
ELSE
	CODE[].d	= (CODE[3..0],!CODE0);
END IF;
	10K_D0		= LCELL(CODE4 xor D0);

--	============================

--	======= CP FRQ =============

	CT[].clk	= (TG42_IN xor !CT1);
	CT[].d		= CT[] + 1;

--	IF CT[] == 2 THEN
--		CT[].d		= GND;
--	ELSE
--		CT[].d		= CT[] + 1;
--	END IF;

	CLKZZ		= TRI(CT1,!CNF_ON);

--	============================

--	========	FDD controller	==================

	TURBING		= EXP(EXP(TURBING & !WSTB & !RSTB) & !STE & !/DTRB);
--	TURBING		= GND;
--	TB_WG		= /DTRB or TURBING;
	TB_WG		= /DTRB;

--	CT_WG		= TFF(VCC,(CT1 xor (CT_WG & (/DTRB or TURBING))),,);
	CT_WG		= TFF(VCC,(CT1 xor (CT_WG & TURBING)),,);

	CT_WG1		= TFF(VCC,(CT1 xor (CT_WG1 & /DTRB)),,);
--	STWG[].clk	= LCELL(CT_WG xor STWG2);

	STWG[].clk	= (CT_WG xor STWG2);
	STWG[].d	= STWG[] + 1;
	CLK_WG		= STWG2;

	CLK_PRC		= STWG0;
--	CLK_PRC		= STWG1;

	WGR[].clk	= CT_WG1;
	IF !RDAT THEN
		TABLE WGR[3..0] => WGR[3..0].d;
			0 => 4; 1 => 5; 2 => 4; 3 => 5;
			4 => 6; 5 => 7; 6 => 8; 7 => 8;
			8 => 9; 9 => 9; 10 => 10; 11 => 11;
		   12 => 12; 13 => 13; 14 => 14; 15 => 15;
		END TABLE;
		WGR4.d	= WGR4;

--		IF WGR[3..0] == 0 THEN
--			WGR[3..0].d		= 3;
--			WGR4.d			= WGR4;
--		ELSE
--			WGR[].d			= WGR[] + 1;
--		END IF;
	ELSE
		IF WGR[3..0] == 0 THEN
			WGR[3..0].d		= 3;
			WGR4.d			= WGR4;
		ELSE
			WGR[].d			= WGR[] + 1;
		END IF;
	END IF;

	QDAT	= WGR4;
--	RDAT_X	= DFF(RDATA,CT_WG1,,);
	RDAT_X	= EXP(EXP(RDAT_X & EXP(!RDATA & !CT_WG1)) & EXP(RDATA & !CT_WG1));
	RDAT	= DFF((RDAT_X or !DFF(RDAT_X,CT_WG1,,)),CT_WG1,,);

--	==============================================

--	========= HDD Controller =========

IF HDD_TYPE == 0 THEN

	MXX	= CODE0;

	HD_CS2		= LCELL(HDD_CS or EXP(!HDD_CS));

	HD_CS_CLK	= HD_CS2 or EXP(EXP(EXP(EXP(EXP(!HD_CS2)))));

	HD_CSS1		= HDD_CS or MA7;
	HD_CSS2		= HDD_CS or !MA7;


	HD_CS1		= (/RD or  MA0 or HD_CS_CLK or /IO) &
					(/RD or CODE0 or HDD_CS or /MR) ;	% /RDH %

	/WRH 		= (/WR or !MA0 or HD_CS_CLK or /IO);	% /WRH %


	HD_OE1		= (HDD_CS or /RD or !MA0 or /IO) &
					(HDD_CS or /RD or !CODE0 or /MR);

	HD_WE1		= HDD_CS or /RD or (/IO & /MR);


	HD_OE2		= HDD_CS or /WR or !MA0;


	HD_WE2		= HDD_CS or /WR or MA0 or /IO;

--	======= RECONFIG =========

	/CONFIG	= LCELL(EPM_RES & (HDD_CS or /MR or /WR));

ELSE

	MXX	= CODE0;

	HD_CS2		= LCELL(HDD_CS or EXP(!HDD_CS) or /WRH);

	HD_CS_CLK	= HDD_CS or EXP(EXP(EXP(EXP(EXP(!HDD_CS)))));

	HD_CSS1		= HDD_CS or MA7;
	HD_CSS2		= HDD_CS or !MA7;

	HD_CS1		= HDD_CS or /WR;

	HD_OE1		= (HD_CS_CLK or /RD or !MA0 or /IO) &
				   (HD_CS_CLK or /WR or !/WRH or !MA0 or /IO) &
					(HDD_CS or /RD or !CODE0 or /MR);

--	HD_WE1		= (HDD_CS or (/RD & /WR) or /IO);

	HD_WE1		= DFF(GND,!HDD_CS,,!(/RD & /WR));

--	HD_OE1		= (HDD_CS or /RD or !MA0) or (HDD_CS or /RD or );

--	HD_WE1		= (HDD_CS or /RD or MA0 or /IO);

	HD_WE2		= (/WR or !MA0 or HD_CS_CLK or /IO);	% /WRH %

	HD_OE2		= (/RD or  MA0 or HD_CS_CLK or /IO) &
					(/RD or CODE0 or HDD_CS or /MR) ;	% /RDH %


	/WRH = DFF((/WRH or !MA0),(/IO or /WR or HDD_CS),(/IO or /RD or HDD_CS),);

--	======= RECONFIG =========

	/CONFIG	= LCELL(EPM_RES & (HDD_CS or /MR or MA7 or /WR));

END IF;


END;

