// Dispatcher.cpp: implementation of the CDispatcher class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "archimedes.h"
#include "Dispatcher.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif



//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDispatcher::CDispatcher()
{
	TRACE("construct CDispatcher \n");

	profiler = new CProfiler();
}

CDispatcher::~CDispatcher()
{
	delete profiler;
}

// takes an address and executes as much recompiled code as exists
// from that address, recompiling where desirable before returning
// to the interpreter
// ??? incomplete

void CDispatcher::attemptDispatch(uint32 address)
{
	
	executeRecompiledCode(address);
	
	// either we've finished executing recompiled code or there wasn't
	// any in existence for the current address

	// while(true)
	//		if current physical address recompilable
	//			increment counter for this block of code
	//			if counter > threshold

					profiler->recompile(address);
					executeRecompiledCode(address);
				
				
	//			else
	//				// no recompiled code to execute so leave function
	//				return;


	//			endif
	//		endif
	// endwhile


	// ???
	// note, this is very aggressive at recompiling and could result in
	// a very low initialisation so it may be desirable to return to the
	// interpreter more to get a more consistent speed
}


// executes as much recompiled code as exists

void CDispatcher::executeRecompiledCode(uint32 address)
{
	// while the current PC has recompiled code
	//		execute it and update ARM state

	// on returning, the chunk will have put all the flags
	// etc back in the right positions, we can largely assume that
	// the prefetch has been invalidated (though it might not have been
	// - think about the copro instruction case !! )
	// since the I, F and M flags may have been changed in the
	// last instruction in the chunk will need to test them in
	// order to change the mode if necessary

	// note the address returned in vPC from recompiled code is the address of the
	// last instruction + 8 for pipelining effect
	// need to check this ok for the return in event of leaveBranchForward/Backward
}
