// ###########################################################################
// #
// # OSCAMathsAssist - Definition of OSCA RS232
// # Copyright (C) 2009-2012 Tony Friery [DialTone]
// #
// # ALL RIGHTS RESERVED
// #
// ###########################################################################

#ifndef OSCASerialH
#define OSCASerialH

#include "z80.h"
#include <Classes.hpp>
#include <deque>

using std::deque;
class TOSCABoard;

///////////////////////////////////////////////////////////////////////
//
//		Definition of RS232 Device Handler Class
//
///////////////////////////////////////////////////////////////////////

class TOSCASerial
{
	private:
		TOSCABoard				*fOwner;
		deque<byte>				fTXBuffer;					// Bytes received from OSCA
		deque<byte>				fRXBuffer;					// Bytes ready for OSCA to receive
		bool						fTransferActive;  		// File transfer in progress
		bool						fTransferDirection;     // true = receiving from OSCA to PC
		unsigned int			fTransferSize;				// Size of file
		unsigned int			fTransferTimeout;
		UnicodeString			fTransferFilename;
		TMemoryStream			*fQueuedFile;

		void						StuffFileHeader(UnicodeString FileName);
		void						StuffBuffer(byte *buffer);
		UINT32					CalculateCRC(byte *buffer);

	public:
		void __fastcall		QueueFile(UnicodeString FileName);
		bool __fastcall		TransmitPending(void);
		bool __fastcall		ReceivePending(void);
		bool __fastcall		TransferActive(void);
		void __fastcall		WriteByte(byte Value);
		byte __fastcall		ReadByte(void);
		void __fastcall		Reset(void);
		void __fastcall		Tick(unsigned int NumCycles);

		__fastcall 				TOSCASerial(TOSCABoard *owner);
		__fastcall 				~TOSCASerial();
};

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

#endif
