//----------------------------------------------------------------------------
//
// File:		device.hpp
// Date:		27-Mar-1998
// Programmer:	Marc Rousseau
//
// Description:
//
// Copyright (c) 1998-2004 Marc Rousseau, All Rights Reserved.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
//
// Revision History:
//
//----------------------------------------------------------------------------

#ifndef DEVICE_HPP_
#define DEVICE_HPP_

#include "cBaseObject.hpp"
#include "istateobject.hpp"
#include "idevice.hpp"
#include <set>
#include <map>

class cDevice :
	public virtual cBaseObject,
//	public virtual iStateObject,
	public virtual iDevice
{
protected:

	cRefPtr<iCartridge> m_pROM;
	cRefPtr<iTMS9900>   m_pCPU;
	int                 m_CRU;
	bool                m_IsValid;
	bool                m_IsRecognized;
	UINT8               m_TrapIndex;

	typedef std::map<ADDRESS,std::set<ADDRESS>> DataPool;

	DataPool            m_mapMemRead;
	DataPool            m_mapMemWrite;
	DataPool            m_mapIORead;
	DataPool            m_mapIOWrite;

public:

	cDevice( iCartridge *rom );

	// iBaseObject Methods
	virtual const void *GetInterface( const std::string &name ) const override;

	// iStateObject Methods
//	virtual bool LoadState( FILE * ) override;
//	virtual bool SaveState( FILE * ) override;

	// iDevice Methods
	virtual const char *GetName( ) override			{ return "Unknown"; }
	virtual bool Initialize( ) override				{ return true; }
	virtual void SetCPU( iTMS9900 *cpu ) override	{ m_pCPU = cpu; }
	virtual int GetCRU( ) const override			{ return m_CRU; }
	virtual iCartridge *GetROM( ) const override	{ return m_pROM; }
	virtual void Activate( ) override;
	virtual void DeActivate( ) override;
	virtual void WriteCRU( ADDRESS, int ) override;
	virtual int ReadCRU( ADDRESS ) override;

protected:

	static void DumpDataPool( const DataPool &pool, const std::string &header );
	static UINT8 TrapFunction( void *, int, bool, ADDRESS, UINT8 );

	bool RegisterTrapHandler( );
	void DeRegisterTrapHandler( );

	virtual UINT8 WriteMemory( ADDRESS, UINT8 );
	virtual UINT8 ReadMemory( ADDRESS, UINT8 );

	virtual ~cDevice( );

private:

	cDevice( const cDevice & ) = delete;			// no implementation
	void operator =( const cDevice & ) = delete;	// no implementation

};

#endif
