// ###########################################################################
// #
// # OSCAPIC - Definition of OSCA PIC/EEPROM Module
// # Copyright (C) 2009-2011 Tony Friery [DialTone]
// #
// # ALL RIGHTS RESERVED
// #
// ###########################################################################

#ifndef OSCAPICH
#define OSCAPICH

#include "z80.h"
#include <System.hpp>

///////////////////////////////////////////////////////////////////////
//
//		Definition of PIC/EEPROM Class
//
///////////////////////////////////////////////////////////////////////

class TOSCAPIC
{
	private:
		typedef enum {
			PIC_IDLE,			// Command Available
			PIC_CMDWAIT,		// Waiting Command Value
			PIC_WAITX1,			// Waiting Command Part 2
			PIC_WAITX2,			// Waiting Command Part 3
			PIC_WAITL,			// Waiting Low Address
			PIC_WAITM,			// Waiting Mid Address
			PIC_WAITH,			// Waiting High Address
			PIC_EXECUTE			// Performing Command
		} PicOpModes;

		byte					EEPROM[(512 * 1024)];	// 512k EEPROM

		bool              fClock;              // Current Clock Line
		bool					fData;					// Current Data Line
		int					fBitCount;				// Current Incoming Bit Count
		byte					fCurrentByte;			// Incoming Byte being built
		PicOpModes			fCurrentMode;			// Current Operating Mode
		byte					fCurrentCmd;			// Current Command
		unsigned int		fCurrentAddr;			// Address Value being Built
		bool					fBurstReady;			// Databurst Byte Ready

		unsigned int		fConfigBaseAddress;	// Base Address to use for FPGA Config
		unsigned int		fBurstAddress;			// Burst Address Base
		unsigned int		fBurstLength;			// Length of Burst (bytes)
		unsigned int		fProgAddress;			// Address to program EEPROM
		unsigned int		fBurstCount;			// Number of burst bytes remaining
		byte					fBurstData;				// Current burst data value

		void __fastcall	ByteReceived(void);

	public:
		bool __fastcall	IsDataBurstReady(void);
		byte __fastcall	GetDataBurstByte(void);
		void __fastcall	LoadEEPROM(UnicodeString Name, unsigned int BaseAddr);
		void __fastcall 	Reset(void);
		void __fastcall	Write(bool Clock, bool Data);

		__fastcall 			TOSCAPIC();
};

//---------------------------------------------------------------------------

#endif

