2010-05-23 10:05:41 +00:00
|
|
|
#include "stdafx.h"
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2010-05-23 10:05:41 +00:00
|
|
|
CSystemTimer::CSystemTimer( int & NextTimer ) :
|
2010-07-05 11:29:46 +00:00
|
|
|
m_NextTimer(NextTimer),
|
|
|
|
m_inFixTimer(false)
|
2010-05-23 10:05:41 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSystemTimer::Reset ( void )
|
|
|
|
{
|
|
|
|
//initialise Structure
|
|
|
|
for (int i = 0; i < MaxTimer; i++)
|
|
|
|
{
|
|
|
|
m_TimerDetatils[i].Active = false;
|
|
|
|
m_TimerDetatils[i].CyclesToTimer = 0;
|
|
|
|
}
|
2010-07-05 11:29:46 +00:00
|
|
|
m_Current = UnknownTimer;
|
|
|
|
m_LastUpdate = 0;
|
|
|
|
m_NextTimer = 0;
|
2010-06-12 02:02:06 +00:00
|
|
|
|
|
|
|
SetTimer(ViTimer,50000,false);
|
2010-07-05 11:29:46 +00:00
|
|
|
SetCompareTimer();
|
2010-05-23 10:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSystemTimer::SetTimer ( TimerType Type, DWORD Cycles, bool bRelative )
|
|
|
|
{
|
|
|
|
if (Type >= MaxTimer || Type == UnknownTimer)
|
|
|
|
{
|
|
|
|
_Notify->BreakPoint(__FILE__,__LINE__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
UpdateTimers();
|
|
|
|
|
|
|
|
m_TimerDetatils[Type].Active = true;
|
|
|
|
if (bRelative)
|
|
|
|
{
|
|
|
|
if (m_TimerDetatils[Type].Active)
|
|
|
|
{
|
|
|
|
m_TimerDetatils[Type].CyclesToTimer += Cycles; //Add to the timer
|
|
|
|
} else {
|
2010-07-05 11:29:46 +00:00
|
|
|
m_TimerDetatils[Type].CyclesToTimer = (__int64)Cycles - (__int64)m_NextTimer; //replace the new cycles
|
2010-05-23 10:05:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-07-05 11:29:46 +00:00
|
|
|
m_TimerDetatils[Type].CyclesToTimer = (__int64)Cycles - (__int64)m_NextTimer; //replace the new cycles
|
2010-05-23 10:05:41 +00:00
|
|
|
}
|
|
|
|
FixTimers();
|
|
|
|
}
|
|
|
|
|
2010-06-14 21:14:58 +00:00
|
|
|
DWORD CSystemTimer::GetTimer ( TimerType Type )
|
|
|
|
{
|
|
|
|
if (Type >= MaxTimer || Type == UnknownTimer)
|
|
|
|
{
|
|
|
|
_Notify->BreakPoint(__FILE__,__LINE__);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!m_TimerDetatils[Type].Active)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return m_TimerDetatils[Type].CyclesToTimer + m_NextTimer;
|
|
|
|
}
|
|
|
|
|
2010-05-23 10:05:41 +00:00
|
|
|
void CSystemTimer::StopTimer ( TimerType Type )
|
|
|
|
{
|
|
|
|
if (Type >= MaxTimer || Type == UnknownTimer)
|
|
|
|
{
|
|
|
|
_Notify->BreakPoint(__FILE__,__LINE__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_TimerDetatils[Type].Active = false;
|
|
|
|
FixTimers();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CSystemTimer::FixTimers (void)
|
|
|
|
{
|
2010-07-05 11:29:46 +00:00
|
|
|
|
|
|
|
if (m_inFixTimer)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_inFixTimer = true;
|
2010-05-23 10:05:41 +00:00
|
|
|
|
2010-07-05 11:29:46 +00:00
|
|
|
UpdateTimers();
|
|
|
|
if (GetTimer(CompareTimer) > 0x60000000)
|
|
|
|
{
|
|
|
|
SetCompareTimer();
|
|
|
|
}
|
|
|
|
|
2010-05-23 10:05:41 +00:00
|
|
|
//Update the cycles for the remaining number of cycles to timer
|
2010-07-05 11:29:46 +00:00
|
|
|
int count;
|
2010-05-23 10:05:41 +00:00
|
|
|
for (count = 0; count < MaxTimer; count++)
|
|
|
|
{
|
|
|
|
if (!m_TimerDetatils[count].Active)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2010-07-05 11:29:46 +00:00
|
|
|
m_TimerDetatils[count].CyclesToTimer += m_NextTimer;
|
2010-05-23 10:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Set Max timer
|
2010-07-05 11:29:46 +00:00
|
|
|
m_NextTimer = 0x7FFFFFFF;
|
2010-05-23 10:05:41 +00:00
|
|
|
|
|
|
|
//Find the smallest timer left to go
|
|
|
|
for (count = 0; count < MaxTimer; count++)
|
|
|
|
{
|
|
|
|
if (!m_TimerDetatils[count].Active)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2010-07-05 11:29:46 +00:00
|
|
|
if (m_TimerDetatils[count].CyclesToTimer >= m_NextTimer)
|
2010-05-23 10:05:41 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2010-07-05 11:29:46 +00:00
|
|
|
m_NextTimer = m_TimerDetatils[count].CyclesToTimer;
|
2010-05-23 10:05:41 +00:00
|
|
|
m_Current = (TimerType)count;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Move the timer back this value
|
|
|
|
for (count = 0; count < MaxTimer; count++)
|
|
|
|
{
|
|
|
|
if (!m_TimerDetatils[count].Active)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2010-07-05 11:29:46 +00:00
|
|
|
m_TimerDetatils[count].CyclesToTimer -= m_NextTimer;
|
2010-05-23 10:05:41 +00:00
|
|
|
}
|
2010-07-05 11:29:46 +00:00
|
|
|
m_LastUpdate = m_NextTimer;
|
|
|
|
m_inFixTimer = false;
|
2010-05-23 10:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSystemTimer::UpdateTimers ( void )
|
|
|
|
{
|
2010-07-05 11:29:46 +00:00
|
|
|
int TimeTaken = m_LastUpdate - m_NextTimer;
|
2010-05-23 10:05:41 +00:00
|
|
|
if (TimeTaken != 0)
|
|
|
|
{
|
2010-07-05 11:29:46 +00:00
|
|
|
m_LastUpdate = m_NextTimer;
|
2010-05-23 10:05:41 +00:00
|
|
|
_Reg->COUNT_REGISTER += TimeTaken;
|
|
|
|
_Reg->RANDOM_REGISTER -= TimeTaken / g_CountPerOp;
|
|
|
|
while ((int)_Reg->RANDOM_REGISTER < (int)_Reg->WIRED_REGISTER)
|
|
|
|
{
|
|
|
|
_Reg->RANDOM_REGISTER += 32 - _Reg->WIRED_REGISTER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSystemTimer::TimerDone (void)
|
|
|
|
{
|
|
|
|
UpdateTimers();
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2010-05-23 10:05:41 +00:00
|
|
|
/* DWORD LastTimer;
|
|
|
|
if (Profiling) {
|
|
|
|
LastTimer = StartTimer(Timer_Done);
|
|
|
|
}
|
|
|
|
#if (!defined(EXTERNAL_RELEASE))
|
|
|
|
if (LogOptions.GenerateLog && LogOptions.LogExceptions && !LogOptions.NoInterrupts) {
|
|
|
|
LogMessage("%08X: Timer Done (Type: %d CurrentTimer: %d)", *_PROGRAM_COUNTER, m_Current, *_Timer );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
*/
|
|
|
|
switch (m_Current) {
|
|
|
|
case CSystemTimer::CompareTimer:
|
|
|
|
_Reg->FAKE_CAUSE_REGISTER |= CAUSE_IP7;
|
2010-06-04 06:25:07 +00:00
|
|
|
_Reg->CheckInterrupts();
|
2010-05-23 10:05:41 +00:00
|
|
|
UpdateCompareTimer();
|
|
|
|
break;
|
|
|
|
case CSystemTimer::SoftResetTimer:
|
|
|
|
_SystemTimer->StopTimer(CSystemTimer::SoftResetTimer);
|
2010-06-12 02:02:06 +00:00
|
|
|
_System->ExternalEvent(SysEvent_ResetCPU_SoftDone);
|
2010-05-23 10:05:41 +00:00
|
|
|
break;
|
|
|
|
case CSystemTimer::SiTimer:
|
|
|
|
_SystemTimer->StopTimer(CSystemTimer::SiTimer);
|
|
|
|
_Reg->MI_INTR_REG |= MI_INTR_SI;
|
|
|
|
_Reg->SI_STATUS_REG |= SI_STATUS_INTERRUPT;
|
2010-06-04 06:25:07 +00:00
|
|
|
_Reg->CheckInterrupts();
|
2010-05-23 10:05:41 +00:00
|
|
|
break;
|
|
|
|
case CSystemTimer::PiTimer:
|
|
|
|
_SystemTimer->StopTimer(CSystemTimer::PiTimer);
|
|
|
|
_Reg->PI_STATUS_REG &= ~PI_STATUS_DMA_BUSY;
|
|
|
|
_Reg->MI_INTR_REG |= MI_INTR_PI;
|
2010-06-04 06:25:07 +00:00
|
|
|
_Reg->CheckInterrupts();
|
2010-05-23 10:05:41 +00:00
|
|
|
break;
|
|
|
|
case CSystemTimer::ViTimer:
|
2010-06-07 02:23:58 +00:00
|
|
|
try
|
|
|
|
{
|
2010-06-12 02:02:06 +00:00
|
|
|
_System->RefreshScreen();
|
2010-06-07 02:23:58 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
WriteTraceF(TraceError,"Exception caught in Refresh Screen\nFile: %s\nLine: %d",__FILE__,__LINE__);
|
|
|
|
}
|
2010-05-23 10:05:41 +00:00
|
|
|
_Reg->MI_INTR_REG |= MI_INTR_VI;
|
2010-06-04 06:25:07 +00:00
|
|
|
_Reg->CheckInterrupts();
|
2010-05-23 10:05:41 +00:00
|
|
|
break;
|
|
|
|
case CSystemTimer::RspTimer:
|
|
|
|
_SystemTimer->StopTimer(CSystemTimer::RspTimer);
|
|
|
|
RunRsp();
|
|
|
|
break;
|
|
|
|
case CSystemTimer::AiTimer:
|
|
|
|
_SystemTimer->StopTimer(CSystemTimer::AiTimer);
|
2010-06-14 21:14:58 +00:00
|
|
|
_Audio->TimerDone();
|
2010-05-23 10:05:41 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BreakPoint(__FILE__,__LINE__);
|
|
|
|
}
|
|
|
|
//CheckTimer();
|
|
|
|
/*if (Profiling) {
|
|
|
|
StartTimer(LastTimer);
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
|
2010-07-05 11:29:46 +00:00
|
|
|
void CSystemTimer::SetCompareTimer ( void )
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2010-07-05 11:29:46 +00:00
|
|
|
DWORD NextCompare = 0x7FFFFFFF;
|
|
|
|
if (_Reg)
|
2010-05-23 10:05:41 +00:00
|
|
|
{
|
2010-07-05 11:29:46 +00:00
|
|
|
NextCompare = _Reg->COMPARE_REGISTER - _Reg->COUNT_REGISTER;
|
|
|
|
if ((NextCompare & 0x80000000) != 0)
|
|
|
|
{
|
|
|
|
NextCompare = 0x7FFFFFFF;
|
|
|
|
}
|
2010-05-23 10:05:41 +00:00
|
|
|
}
|
2010-07-05 11:29:46 +00:00
|
|
|
SetTimer(CompareTimer,NextCompare,false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSystemTimer::UpdateCompareTimer ( void )
|
|
|
|
{
|
|
|
|
SetCompareTimer();
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2010-06-14 21:14:58 +00:00
|
|
|
bool CSystemTimer::SaveAllowed ( void )
|
|
|
|
{
|
2010-07-05 11:29:46 +00:00
|
|
|
if (GetTimer(CompareTimer) <= 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2010-06-14 21:14:58 +00:00
|
|
|
for (int i = 0; i < MaxTimer; i++)
|
|
|
|
{
|
|
|
|
if (i == CompareTimer) { continue; }
|
|
|
|
if (i == ViTimer) { continue; }
|
|
|
|
if (m_TimerDetatils[i].Active)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
2010-06-14 21:14:58 +00:00
|
|
|
return true;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|