// ###########################################################################
// #
// # OSCABoard - Definition of Top-Level Class for OSCA Board
// # Copyright (C) 2009-2011 Tony Friery [DialTone]
// #
// # ALL RIGHTS RESERVED
// #
// ###########################################################################

#ifndef OSCABoardH
#define OSCABoardH


#include <Classes.hpp>

#include "z80.h"
#include "GR32.hpp"
#include "OSCAVideo.h"
#include "OSCAPIC.h"
#include "OSCATimer.h"
#include "OSCAPS2Devices.h"
#include "OSCAMathsAssist.h"
#include "OSCAKeyboard.h"
#include "OSCAMouse.h"
#include "OSCASDMMCCard.h"
#include "OSCAAudio.h"
#include "CPUThread.h"
#include "DisplayThread.h"

///////////////////////////////////////////////////////////////////////
//
//		Definition of Board Class
//
///////////////////////////////////////////////////////////////////////

class TOSCABoard : public z80cpu
{
	private:
		HWND									mainHandle;								// HWND for main form
		Z80Thread							*m_pCPUThread;							// Processor Thread
		VideoThread							*m_pVideoThread;						// Video Thread
      bool									m_bRasterElapsed;
		TOSCAVideo							*VideoModule;							// OSCA Video Hardware
		TOSCAPIC								*PICModule;								// OSCA PIC Device
		TOSCATimer							*TimerModule;							// OSCA Timer Module
		TOSCAKeyboard						*KeyboardDevice;						// OSCA Keyboard Interface
		TOSCAMouse							*MouseDevice;							// OSCA Mouse Interface
		TOSCAMathsAssist					*MathsUnit;								// OSCA Maths Assist Unit
		TOSCASDCard							*SDMMCDevice;							// OSCA SD/MMC Card Device
		TOSCAAudioSystem					*AudioSystem;							// OSCA Audio Subsystem
		bool									fWriteSupport;							// Make SD Card writable?
		bool									fSuppressWritePrompt;				// Suppress Write Prompt?
		UnicodeString						fImageFilename;						// SD Card Filename
		bool									fSmoothStretch;						// Smooth scalind

		void __fastcall   				LoadCurrentSettings(void); 		// Read settings from Registry
		void __fastcall   				SaveCurrentSettings(void);			// Save settings to Registry

	public:
		// Memory
		byte									BootROM[512];							// 512 Bytes of ROM
		byte									MainRAM[(16 * 0x8000)];				// 16 pages of 32K RAM

		// Internal Registers
		byte									fSysMemSelect;							// sys_mem_select register
		byte									fSysAltWrPg;							// sys_alt_write_page
		byte									fSysLowPage;							// sys_low_page
		word									fVRAMLocation;							// sys_vram_location
		byte									fSysIRQEn;								// sys_irq_enable
		bool									fJoystickPort;							// Joystick# (false=0, true=1)

		bool                 			NMIRequired;							// Has an NMI been requested?
		bool									IRQRequired;							// Is an IRQ Necessary?
		unsigned int         			CycleCount, AudioCycleCount;     // Used for managing "housekeeping"
		unsigned int						VideoCycleCount;

		void __fastcall					LineCopWrite(word A, byte V);		// Write function used by LineCop

		byte __fastcall     				Z80_In(word Port);              	// Input byte from Port
		void __fastcall             	Z80_Out(word Port,byte Value);  	// Output byte to Port
		unsigned __fastcall         	Z80_RDMEM(word A);              	// Read byte from given Mem Location
		void __fastcall             	Z80_WRMEM(word A,byte V);       	// Write byte to Memory
		virtual unsigned __fastcall 	Z80_RDSTACK(word A);            	// Read byte from given Mem Location
		virtual void __fastcall     	Z80_WRSTACK(word A,byte V);     	// Write byte to Memory
		void __fastcall             	Z80_Patch (Z80_Regs *Regs);     	// Called when ED FE occurs. Can be used
																							// to emulate disk access etc.
		void __fastcall             	Z80_Reti (void);                	// Called when RETI occurs
		void __fastcall             	Z80_Retn (void);                	// Called when RETN occurs
		int __fastcall              	Z80_Interrupt(void);
		bool __fastcall             	Z80_InstHook(int PC);           	// Called prior each instruction
		int __fastcall						GetCyclesPerSecond(void);			// Obtain required MHz
		bool __fastcall					SDReading(void);
		bool __fastcall					SDWriting(void);
		bool __fastcall					CanClose(void);

		void __fastcall					Execute(void);
		void              				PowerOnReset(void);
		void 									KeyPressed(WORD Key, int Shift);
		void 									KeyReleased(WORD Key, int Shift);

		TOSCAVideo * __fastcall			GetVideoModule(void);
		void __fastcall					ShutdownThreads(void);

		__fastcall 							TOSCABoard(HWND handle);
		virtual __fastcall 				~TOSCABoard();
};

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

#endif

