2009-09-08 12:08:10 +00:00
|
|
|
/* PCSX2 - PS2 Emulator for PCs
|
|
|
|
* Copyright (C) 2002-2009 PCSX2 Dev Team
|
|
|
|
*
|
|
|
|
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU Lesser General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
2009-02-09 21:15:56 +00:00
|
|
|
*
|
2009-09-08 12:08:10 +00:00
|
|
|
* PCSX2 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 PCSX2.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
2009-02-09 21:15:56 +00:00
|
|
|
*/
|
|
|
|
|
2009-09-08 12:08:10 +00:00
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
#include "PrecompiledHeader.h"
|
2009-03-13 04:49:23 +00:00
|
|
|
#include "IopCommon.h"
|
2009-11-14 12:02:56 +00:00
|
|
|
|
|
|
|
#include "Sio.h"
|
|
|
|
#include "Sif.h"
|
2009-03-01 21:49:17 +00:00
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
using namespace R3000A;
|
|
|
|
|
|
|
|
R3000Acpu *psxCpu;
|
|
|
|
|
|
|
|
// used for constant propagation
|
|
|
|
u32 g_psxConstRegs[32];
|
|
|
|
u32 g_psxHasConstReg, g_psxFlushedConstReg;
|
|
|
|
|
|
|
|
// Controls when branch tests are performed.
|
|
|
|
u32 g_psxNextBranchCycle = 0;
|
|
|
|
|
|
|
|
// This value is used when the IOP execution is broken to return control to the EE.
|
|
|
|
// (which happens when the IOP throws EE-bound interrupts). It holds the value of
|
|
|
|
// psxCycleEE (which is set to zero to facilitate the code break), so that the unrun
|
|
|
|
// cycles can be accounted for later.
|
|
|
|
s32 psxBreak = 0;
|
|
|
|
|
|
|
|
// tracks the IOP's current sync status with the EE. When it dips below zero,
|
|
|
|
// control is returned to the EE.
|
|
|
|
s32 psxCycleEE = -1;
|
|
|
|
|
|
|
|
// Used to signal to the EE when important actions that need IOP-attention have
|
|
|
|
// happened (hsyncs, vsyncs, IOP exceptions, etc). IOP runs code whenever this
|
|
|
|
// is true, even if it's already running ahead a bit.
|
|
|
|
bool iopBranchAction = false;
|
|
|
|
|
|
|
|
bool iopEventTestIsActive = false;
|
|
|
|
|
2009-10-05 02:15:49 +00:00
|
|
|
__aligned16 psxRegisters psxRegs;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
void psxReset()
|
|
|
|
{
|
2009-09-23 12:53:32 +00:00
|
|
|
memzero(psxRegs);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
psxRegs.pc = 0xbfc00000; // Start in bootstrap
|
|
|
|
psxRegs.CP0.n.Status = 0x10900000; // COP0 enabled | BEV = 1 | TS = 1
|
|
|
|
psxRegs.CP0.n.PRid = 0x0000001f; // PRevID = Revision ID, same as the IOP R3000A
|
|
|
|
|
|
|
|
psxBreak = 0;
|
|
|
|
psxCycleEE = -1;
|
|
|
|
g_psxNextBranchCycle = psxRegs.cycle + 4;
|
|
|
|
|
|
|
|
psxHwReset();
|
|
|
|
psxBiosInit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void psxShutdown() {
|
|
|
|
psxBiosShutdown();
|
|
|
|
//psxCpu->Shutdown();
|
|
|
|
}
|
|
|
|
|
2009-10-20 20:02:07 +00:00
|
|
|
void __fastcall psxException(u32 code, u32 bd)
|
|
|
|
{
|
2009-03-27 06:34:51 +00:00
|
|
|
// PSXCPU_LOG("psxException %x: %x, %x", code, psxHu32(0x1070), psxHu32(0x1074));
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
//Console.WriteLn("!! psxException %x: %x, %x", code, psxHu32(0x1070), psxHu32(0x1074));
|
2009-02-09 21:15:56 +00:00
|
|
|
// Set the Cause
|
|
|
|
psxRegs.CP0.n.Cause &= ~0x7f;
|
|
|
|
psxRegs.CP0.n.Cause |= code;
|
|
|
|
|
|
|
|
// Set the EPC & PC
|
|
|
|
if (bd)
|
|
|
|
{
|
2009-03-27 06:34:51 +00:00
|
|
|
PSXCPU_LOG("bd set");
|
2009-02-09 21:15:56 +00:00
|
|
|
psxRegs.CP0.n.Cause|= 0x80000000;
|
|
|
|
psxRegs.CP0.n.EPC = (psxRegs.pc - 4);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
psxRegs.CP0.n.EPC = (psxRegs.pc);
|
|
|
|
|
|
|
|
if (psxRegs.CP0.n.Status & 0x400000)
|
|
|
|
psxRegs.pc = 0xbfc00180;
|
|
|
|
else
|
|
|
|
psxRegs.pc = 0x80000080;
|
|
|
|
|
|
|
|
// Set the Status
|
|
|
|
psxRegs.CP0.n.Status = (psxRegs.CP0.n.Status &~0x3f) |
|
|
|
|
((psxRegs.CP0.n.Status & 0xf) << 2);
|
|
|
|
|
|
|
|
/*if ((((PSXMu32(psxRegs.CP0.n.EPC) >> 24) & 0xfe) == 0x4a)) {
|
|
|
|
// "hokuto no ken" / "Crash Bandicot 2" ... fix
|
|
|
|
PSXMu32(psxRegs.CP0.n.EPC)&= ~0x02000000;
|
|
|
|
}*/
|
|
|
|
|
2009-08-04 08:21:10 +00:00
|
|
|
if (!CHECK_IOPREC)
|
|
|
|
{
|
2009-11-23 06:54:24 +00:00
|
|
|
// HLE Bios Handlers, enabled for interpreters only.
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
u32 call = psxRegs.GPR.n.t1 & 0xff;
|
|
|
|
switch (psxRegs.pc & 0x1fffff) {
|
|
|
|
case 0xa0:
|
|
|
|
|
|
|
|
if (call != 0x28 && call != 0xe)
|
2009-03-27 06:34:51 +00:00
|
|
|
PSXBIOS_LOG("Bios call a0: %s (%x) %x,%x,%x,%x", biosA0n[call], call, psxRegs.GPR.n.a0, psxRegs.GPR.n.a1, psxRegs.GPR.n.a2, psxRegs.GPR.n.a3);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if (biosA0[call])
|
|
|
|
biosA0[call]();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0xb0:
|
|
|
|
if (call != 0x17 && call != 0xb)
|
2009-03-27 06:34:51 +00:00
|
|
|
PSXBIOS_LOG("Bios call b0: %s (%x) %x,%x,%x,%x", biosB0n[call], call, psxRegs.GPR.n.a0, psxRegs.GPR.n.a1, psxRegs.GPR.n.a2, psxRegs.GPR.n.a3);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if (biosB0[call])
|
|
|
|
biosB0[call]();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0xc0:
|
2009-03-27 06:34:51 +00:00
|
|
|
PSXBIOS_LOG("Bios call c0: %s (%x) %x,%x,%x,%x", biosC0n[call], call, psxRegs.GPR.n.a0, psxRegs.GPR.n.a1, psxRegs.GPR.n.a2, psxRegs.GPR.n.a3);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if (biosC0[call])
|
|
|
|
biosC0[call]();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*if (psxRegs.CP0.n.Cause == 0x400 && (!(psxHu32(0x1450) & 0x8))) {
|
2009-08-25 09:19:45 +00:00
|
|
|
hwIntcIrq(INTC_SBUS);
|
2009-02-09 21:15:56 +00:00
|
|
|
}*/
|
|
|
|
}
|
|
|
|
|
|
|
|
__forceinline void psxSetNextBranch( u32 startCycle, s32 delta )
|
|
|
|
{
|
|
|
|
// typecast the conditional to signed so that things don't blow up
|
|
|
|
// if startCycle is greater than our next branch cycle.
|
|
|
|
|
|
|
|
if( (int)(g_psxNextBranchCycle - startCycle) > delta )
|
|
|
|
g_psxNextBranchCycle = startCycle + delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
__forceinline void psxSetNextBranchDelta( s32 delta )
|
|
|
|
{
|
|
|
|
psxSetNextBranch( psxRegs.cycle, delta );
|
|
|
|
}
|
|
|
|
|
|
|
|
__forceinline int psxTestCycle( u32 startCycle, s32 delta )
|
|
|
|
{
|
|
|
|
// typecast the conditional to signed so that things don't explode
|
|
|
|
// if the startCycle is ahead of our current cpu cycle.
|
|
|
|
|
|
|
|
return (int)(psxRegs.cycle - startCycle) >= delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
__forceinline void PSX_INT( IopEventId n, s32 ecycle )
|
|
|
|
{
|
|
|
|
// Generally speaking games shouldn't throw ints that haven't been cleared yet.
|
2009-06-26 00:30:19 +00:00
|
|
|
// It's usually indicative of something amiss in our emulation, so uncomment this
|
2009-02-09 21:15:56 +00:00
|
|
|
// code to help trap those sort of things.
|
|
|
|
|
|
|
|
// Exception: IRQ16 - SIO - it drops ints like crazy when handling PAD stuff.
|
2010-01-15 15:21:43 +00:00
|
|
|
if( /*n!=16 &&*/ psxRegs.interrupt & (1<<n) )
|
|
|
|
DevCon.Warning( "***** IOP > Twice-thrown int on IRQ %d", n );
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
psxRegs.interrupt |= 1 << n;
|
|
|
|
|
|
|
|
psxRegs.sCycle[n] = psxRegs.cycle;
|
|
|
|
psxRegs.eCycle[n] = ecycle;
|
|
|
|
|
|
|
|
psxSetNextBranchDelta( ecycle );
|
|
|
|
|
|
|
|
if( psxCycleEE < 0 )
|
|
|
|
{
|
|
|
|
// The EE called this int, so inform it to branch as needed:
|
|
|
|
// fixme - this doesn't take into account EE/IOP sync (the IOP may be running
|
|
|
|
// ahead or behind the EE as per the EEsCycles value)
|
|
|
|
s32 iopDelta = (g_psxNextBranchCycle-psxRegs.cycle)*8;
|
|
|
|
cpuSetNextBranchDelta( iopDelta );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static __forceinline void IopTestEvent( IopEventId n, void (*callback)() )
|
|
|
|
{
|
|
|
|
if( !(psxRegs.interrupt & (1 << n)) ) return;
|
|
|
|
|
|
|
|
if( psxTestCycle( psxRegs.sCycle[n], psxRegs.eCycle[n] ) )
|
|
|
|
{
|
|
|
|
psxRegs.interrupt &= ~(1 << n);
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
psxSetNextBranch( psxRegs.sCycle[n], psxRegs.eCycle[n] );
|
|
|
|
}
|
|
|
|
|
2009-12-02 14:21:54 +00:00
|
|
|
static __forceinline void sifHackInterrupt()
|
|
|
|
{
|
|
|
|
// No reason -- just that sometimes the SIF fell asleep, and this wakes it up.
|
|
|
|
|
2009-12-02 15:18:24 +00:00
|
|
|
iopIntcIrq( 3 ); // IOP DMAC int
|
|
|
|
//hwIntcIrq(INTC_SBUS); // EE's SIF BUS notifier... maybe or maybe not needed?
|
|
|
|
|
|
|
|
// hack is rescheduled as needed by the event handler (depending on if it's actively
|
|
|
|
// signalling an interrupt or not).. better there than here.
|
2009-12-02 14:21:54 +00:00
|
|
|
//PSX_INT( IopEvt_SIFhack, 128 );
|
|
|
|
}
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
static __forceinline void _psxTestInterrupts()
|
|
|
|
{
|
|
|
|
IopTestEvent(IopEvt_SIF0, sif0Interrupt); // SIF0
|
|
|
|
IopTestEvent(IopEvt_SIF1, sif1Interrupt); // SIF1
|
|
|
|
IopTestEvent(IopEvt_SIO, sioInterrupt);
|
|
|
|
IopTestEvent(IopEvt_CdvdRead, cdvdReadInterrupt);
|
|
|
|
|
2009-12-02 14:21:54 +00:00
|
|
|
#if IOP_ENABLE_SIF_HACK
|
|
|
|
IopTestEvent(IopEvt_SIFhack, sifHackInterrupt);
|
|
|
|
#endif
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
// Profile-guided Optimization (sorta)
|
|
|
|
// The following ints are rarely called. Encasing them in a conditional
|
|
|
|
// as follows helps speed up most games.
|
|
|
|
|
|
|
|
if( psxRegs.interrupt & ( (1ul<<5) | (3ul<<11) | (3ul<<20) | (3ul<<17) ) )
|
|
|
|
{
|
|
|
|
IopTestEvent(IopEvt_Cdvd, cdvdActionInterrupt);
|
|
|
|
IopTestEvent(IopEvt_Dma11, psxDMA11Interrupt); // SIO2
|
|
|
|
IopTestEvent(IopEvt_Dma12, psxDMA12Interrupt); // SIO2
|
|
|
|
IopTestEvent(IopEvt_Cdrom, cdrInterrupt);
|
|
|
|
IopTestEvent(IopEvt_CdromRead, cdrReadInterrupt);
|
|
|
|
IopTestEvent(IopEvt_DEV9, dev9Interrupt);
|
|
|
|
IopTestEvent(IopEvt_USB, usbInterrupt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-08 17:15:51 +00:00
|
|
|
__releaseinline void psxBranchTest()
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
if( psxTestCycle( psxNextsCounter, psxNextCounter ) )
|
|
|
|
{
|
|
|
|
psxRcntUpdate();
|
|
|
|
iopBranchAction = true;
|
|
|
|
}
|
2009-05-06 23:51:27 +00:00
|
|
|
else
|
|
|
|
{
|
2009-02-09 21:15:56 +00:00
|
|
|
// start the next branch at the next counter event by default
|
|
|
|
// the interrupt code below will assign nearer branches if needed.
|
2009-05-06 23:51:27 +00:00
|
|
|
g_psxNextBranchCycle = psxNextsCounter+psxNextCounter;
|
|
|
|
}
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
if (psxRegs.interrupt)
|
|
|
|
{
|
|
|
|
iopEventTestIsActive = true;
|
|
|
|
_psxTestInterrupts();
|
|
|
|
iopEventTestIsActive = false;
|
|
|
|
}
|
|
|
|
|
2009-12-02 15:18:24 +00:00
|
|
|
if( (psxHu32(0x1078) != 0) && ((psxHu32(0x1070) & psxHu32(0x1074)) != 0) )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-12-02 15:18:24 +00:00
|
|
|
if( (psxRegs.CP0.n.Status & 0xFE01) >= 0x401 )
|
|
|
|
{
|
|
|
|
PSXCPU_LOG("Interrupt: %x %x", psxHu32(0x1070), psxHu32(0x1074));
|
|
|
|
psxException(0, 0);
|
|
|
|
iopBranchAction = true;
|
|
|
|
|
|
|
|
// No need to execute the SIFhack after cpuExceptions, since these by nature break SIF's
|
|
|
|
// thread sleep hangs and allow the IOP to "come back to life."
|
|
|
|
psxRegs.interrupt &= ~IopEvt_SIFhack;
|
|
|
|
}
|
2009-12-02 14:21:54 +00:00
|
|
|
}
|
2009-12-02 15:18:24 +00:00
|
|
|
|
|
|
|
if( IOP_ENABLE_SIF_HACK && !iopBranchAction && !(psxRegs.interrupt & IopEvt_SIFhack) )
|
2009-12-02 14:21:54 +00:00
|
|
|
{
|
|
|
|
// Safeguard: since we're not executing an exception vector, we should schedule a SIF wakeup
|
2009-12-02 15:18:24 +00:00
|
|
|
// just in case. (and don't reschedule it if it's already scheduled, since that would just
|
|
|
|
// delay the previously scheduled one, and we don't want that)
|
2009-12-02 14:21:54 +00:00
|
|
|
|
|
|
|
// (TODO: The endless loop in question is a branch instruction that branches to itself endlessly,
|
|
|
|
// waiting for SIF to wake it up via any cpuException. We could check for that instruction
|
|
|
|
// location and only schedule a SIF fix when it's detected... But for now this is easy and gives
|
|
|
|
// us good control over testing parameters...)
|
|
|
|
|
2009-12-02 15:18:24 +00:00
|
|
|
PSX_INT( IopEvt_SIFhack, 96 );
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void iopTestIntc()
|
|
|
|
{
|
|
|
|
if( psxHu32(0x1078) == 0 ) return;
|
|
|
|
if( (psxHu32(0x1070) & psxHu32(0x1074)) == 0 ) return;
|
|
|
|
|
|
|
|
if( !eeEventTestIsActive )
|
|
|
|
{
|
|
|
|
// An iop exception has occurred while the EE is running code.
|
|
|
|
// Inform the EE to branch so the IOP can handle it promptly:
|
|
|
|
|
|
|
|
cpuSetNextBranchDelta( 16 );
|
|
|
|
iopBranchAction = true;
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
//Console.Error( "** IOP Needs an EE EventText, kthx ** %d", psxCycleEE );
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
// Note: No need to set the iop's branch delta here, since the EE
|
|
|
|
// will run an IOP branch test regardless.
|
|
|
|
}
|
|
|
|
else if( !iopEventTestIsActive )
|
|
|
|
psxSetNextBranchDelta( 2 );
|
|
|
|
}
|
|
|
|
|