2016-12-05 17:02:29 +00:00
|
|
|
/*
|
2020-02-14 19:18:08 +00:00
|
|
|
Copyright 2016-2020 Arisotura
|
2016-12-05 17:02:29 +00:00
|
|
|
|
|
|
|
This file is part of melonDS.
|
|
|
|
|
|
|
|
melonDS 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 3 of the License, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
melonDS 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 melonDS. If not, see http://www.gnu.org/licenses/.
|
|
|
|
*/
|
|
|
|
|
2016-11-24 17:31:49 +00:00
|
|
|
#include <stdio.h>
|
2016-11-03 00:38:58 +00:00
|
|
|
#include "NDS.h"
|
2016-11-24 17:31:49 +00:00
|
|
|
#include "ARM.h"
|
|
|
|
#include "ARMInterpreter.h"
|
2020-02-14 22:34:26 +00:00
|
|
|
#include "AREngine.h"
|
2016-11-03 00:38:58 +00:00
|
|
|
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
// instruction timing notes
|
|
|
|
//
|
|
|
|
// * simple instruction: 1S (code)
|
|
|
|
// * LDR: 1N+1N+1I (code/data/internal)
|
|
|
|
// * STR: 1N+1N (code/data)
|
|
|
|
// * LDM: 1N+1N+(n-1)S+1I
|
|
|
|
// * STM: 1N+1N+(n-1)S
|
|
|
|
// * MUL/etc: 1N+xI (code/internal)
|
|
|
|
// * branch: 1N+1S (code/code) (pipeline refill)
|
|
|
|
//
|
|
|
|
// MUL/MLA seems to take 1I on ARM9
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-11-24 23:08:53 +00:00
|
|
|
u32 ARM::ConditionTable[16] =
|
|
|
|
{
|
|
|
|
0xF0F0, // EQ
|
|
|
|
0x0F0F, // NE
|
|
|
|
0xCCCC, // CS
|
|
|
|
0x3333, // CC
|
|
|
|
0xFF00, // MI
|
|
|
|
0x00FF, // PL
|
|
|
|
0xAAAA, // VS
|
|
|
|
0x5555, // VC
|
|
|
|
0x0C0C, // HI
|
|
|
|
0xF3F3, // LS
|
|
|
|
0xAA55, // GE
|
|
|
|
0x55AA, // LT
|
|
|
|
0x0A05, // GT
|
|
|
|
0xF5FA, // LE
|
|
|
|
0xFFFF, // AL
|
|
|
|
0x0000 // NE
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-11-03 00:38:58 +00:00
|
|
|
ARM::ARM(u32 num)
|
|
|
|
{
|
|
|
|
// well uh
|
|
|
|
Num = num;
|
2016-11-24 17:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ARM::~ARM()
|
|
|
|
{
|
|
|
|
// dorp
|
|
|
|
}
|
2016-11-03 00:38:58 +00:00
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
ARMv5::ARMv5() : ARM(0)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
ARMv4::ARMv4() : ARM(1)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
2016-11-24 17:31:49 +00:00
|
|
|
void ARM::Reset()
|
|
|
|
{
|
2016-12-05 16:08:24 +00:00
|
|
|
Cycles = 0;
|
2016-12-23 20:22:22 +00:00
|
|
|
Halted = 0;
|
2016-12-05 16:08:24 +00:00
|
|
|
|
2019-06-08 20:16:51 +00:00
|
|
|
IRQ = 0;
|
|
|
|
|
2016-11-03 00:38:58 +00:00
|
|
|
for (int i = 0; i < 16; i++)
|
|
|
|
R[i] = 0;
|
|
|
|
|
2016-11-24 23:08:53 +00:00
|
|
|
CPSR = 0x000000D3;
|
|
|
|
|
2016-11-24 17:31:49 +00:00
|
|
|
ExceptionBase = Num ? 0x00000000 : 0xFFFF0000;
|
2016-11-03 00:38:58 +00:00
|
|
|
|
2018-11-04 22:21:58 +00:00
|
|
|
CodeMem.Mem = NULL;
|
|
|
|
|
2016-11-03 00:38:58 +00:00
|
|
|
// zorp
|
|
|
|
JumpTo(ExceptionBase);
|
|
|
|
}
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
void ARMv5::Reset()
|
|
|
|
{
|
|
|
|
CP15Reset();
|
2018-12-07 13:20:38 +00:00
|
|
|
ARM::Reset();
|
2018-12-04 16:54:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-17 22:27:55 +00:00
|
|
|
void ARM::DoSavestate(Savestate* file)
|
2018-09-15 00:47:34 +00:00
|
|
|
{
|
2018-10-17 22:27:55 +00:00
|
|
|
file->Section((char*)(Num ? "ARM7" : "ARM9"));
|
2018-09-15 00:47:34 +00:00
|
|
|
|
2018-10-17 22:27:55 +00:00
|
|
|
file->Var32((u32*)&Cycles);
|
2019-01-05 04:28:58 +00:00
|
|
|
//file->Var32((u32*)&CyclesToRun);
|
2018-09-15 00:47:34 +00:00
|
|
|
file->Var32(&Halted);
|
|
|
|
|
|
|
|
file->VarArray(R, 16*sizeof(u32));
|
|
|
|
file->Var32(&CPSR);
|
|
|
|
file->VarArray(R_FIQ, 8*sizeof(u32));
|
|
|
|
file->VarArray(R_SVC, 3*sizeof(u32));
|
|
|
|
file->VarArray(R_ABT, 3*sizeof(u32));
|
|
|
|
file->VarArray(R_IRQ, 3*sizeof(u32));
|
|
|
|
file->VarArray(R_UND, 3*sizeof(u32));
|
|
|
|
file->Var32(&CurInstr);
|
|
|
|
file->VarArray(NextInstr, 2*sizeof(u32));
|
|
|
|
|
|
|
|
file->Var32(&ExceptionBase);
|
2018-11-04 22:21:58 +00:00
|
|
|
|
|
|
|
if (!file->Saving)
|
2018-12-11 20:50:28 +00:00
|
|
|
{
|
|
|
|
if (!Num)
|
|
|
|
{
|
|
|
|
SetupCodeMem(R[15]); // should fix it
|
|
|
|
((ARMv5*)this)->RegionCodeCycles = ((ARMv5*)this)->MemTimings[R[15] >> 12][0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CodeRegion = R[15] >> 24;
|
|
|
|
CodeCycles = R[15] >> 15; // cheato
|
|
|
|
}
|
|
|
|
}
|
2018-11-04 22:21:58 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
void ARMv5::DoSavestate(Savestate* file)
|
|
|
|
{
|
|
|
|
ARM::DoSavestate(file);
|
|
|
|
CP15DoSavestate(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-04 22:21:58 +00:00
|
|
|
void ARM::SetupCodeMem(u32 addr)
|
|
|
|
{
|
|
|
|
if (!Num)
|
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
((ARMv5*)this)->GetCodeMemRegion(addr, &CodeMem);
|
2018-11-04 22:21:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-11 15:41:18 +00:00
|
|
|
// not sure it's worth it for the ARM7
|
|
|
|
// esp. as everything there generally runs on WRAM
|
|
|
|
// and due to how it's mapped, we can't use this optimization
|
|
|
|
//NDS::ARM7GetMemRegion(addr, false, &CodeMem);
|
2018-11-04 22:21:58 +00:00
|
|
|
}
|
2018-09-15 00:47:34 +00:00
|
|
|
}
|
2019-06-09 14:06:01 +00:00
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
void ARMv5::JumpTo(u32 addr, bool restorecpsr)
|
2016-11-03 00:38:58 +00:00
|
|
|
{
|
2016-12-05 16:08:24 +00:00
|
|
|
if (restorecpsr)
|
|
|
|
{
|
|
|
|
RestoreCPSR();
|
2016-12-23 20:22:22 +00:00
|
|
|
|
2016-12-05 16:08:24 +00:00
|
|
|
if (CPSR & 0x20) addr |= 0x1;
|
|
|
|
else addr &= ~0x1;
|
|
|
|
}
|
2016-12-03 14:15:34 +00:00
|
|
|
|
2017-03-23 22:47:55 +00:00
|
|
|
// aging cart debug crap
|
|
|
|
//if (addr == 0x0201764C) printf("capture test %d: R1=%08X\n", R[6], R[1]);
|
|
|
|
//if (addr == 0x020175D8) printf("capture test %d: res=%08X\n", R[6], R[0]);
|
2017-03-23 16:14:48 +00:00
|
|
|
|
2018-12-09 00:17:05 +00:00
|
|
|
u32 oldregion = R[15] >> 24;
|
|
|
|
u32 newregion = addr >> 24;
|
|
|
|
|
2018-12-11 16:59:52 +00:00
|
|
|
RegionCodeCycles = MemTimings[addr >> 12][0];
|
2018-12-04 16:54:10 +00:00
|
|
|
|
|
|
|
if (addr & 0x1)
|
|
|
|
{
|
|
|
|
addr &= ~0x1;
|
|
|
|
R[15] = addr+2;
|
|
|
|
|
2018-12-11 15:41:18 +00:00
|
|
|
if (newregion != oldregion) SetupCodeMem(addr);
|
2018-12-04 16:54:10 +00:00
|
|
|
|
|
|
|
// two-opcodes-at-once fetch
|
|
|
|
// doesn't matter if we put garbage in the MSbs there
|
|
|
|
if (addr & 0x2)
|
|
|
|
{
|
2019-01-04 20:47:06 +00:00
|
|
|
NextInstr[0] = CodeRead32(addr-2, true) >> 16;
|
|
|
|
Cycles += CodeCycles;
|
|
|
|
NextInstr[1] = CodeRead32(addr+2, false);
|
|
|
|
Cycles += CodeCycles;
|
2018-12-04 16:54:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-04 20:47:06 +00:00
|
|
|
NextInstr[0] = CodeRead32(addr, true);
|
2018-12-04 16:54:10 +00:00
|
|
|
NextInstr[1] = NextInstr[0] >> 16;
|
2019-01-04 20:47:06 +00:00
|
|
|
Cycles += CodeCycles;
|
2018-12-04 16:54:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CPSR |= 0x20;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
addr &= ~0x3;
|
|
|
|
R[15] = addr+4;
|
|
|
|
|
2018-12-11 15:41:18 +00:00
|
|
|
if (newregion != oldregion) SetupCodeMem(addr);
|
2018-12-04 16:54:10 +00:00
|
|
|
|
2019-01-04 20:47:06 +00:00
|
|
|
NextInstr[0] = CodeRead32(addr, true);
|
|
|
|
Cycles += CodeCycles;
|
|
|
|
NextInstr[1] = CodeRead32(addr+4, false);
|
|
|
|
Cycles += CodeCycles;
|
2018-12-04 16:54:10 +00:00
|
|
|
|
|
|
|
CPSR &= ~0x20;
|
|
|
|
}
|
2018-12-07 13:20:38 +00:00
|
|
|
|
|
|
|
/*if (!(PU_Map[addr>>12] & 0x04))
|
|
|
|
{
|
|
|
|
printf("jumped to %08X. very bad\n", addr);
|
|
|
|
PrefetchAbort();
|
|
|
|
return;
|
|
|
|
}*/
|
2020-02-24 17:31:44 +00:00
|
|
|
|
|
|
|
NDS::MonitorARM9Jump(addr);
|
2018-12-04 16:54:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ARMv4::JumpTo(u32 addr, bool restorecpsr)
|
|
|
|
{
|
|
|
|
if (restorecpsr)
|
|
|
|
{
|
|
|
|
RestoreCPSR();
|
|
|
|
|
|
|
|
if (CPSR & 0x20) addr |= 0x1;
|
|
|
|
else addr &= ~0x1;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 oldregion = R[15] >> 23;
|
|
|
|
u32 newregion = addr >> 23;
|
|
|
|
|
2018-12-09 00:17:05 +00:00
|
|
|
CodeRegion = addr >> 24;
|
|
|
|
CodeCycles = addr >> 15; // cheato
|
|
|
|
|
2016-12-05 16:08:24 +00:00
|
|
|
if (addr & 0x1)
|
2016-12-03 02:10:26 +00:00
|
|
|
{
|
2016-12-05 16:08:24 +00:00
|
|
|
addr &= ~0x1;
|
2016-12-03 02:10:26 +00:00
|
|
|
R[15] = addr+2;
|
2018-11-04 22:21:58 +00:00
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
//if (newregion != oldregion) SetupCodeMem(addr);
|
2018-11-04 22:21:58 +00:00
|
|
|
|
2017-01-30 17:36:11 +00:00
|
|
|
NextInstr[0] = CodeRead16(addr);
|
|
|
|
NextInstr[1] = CodeRead16(addr+2);
|
2018-12-09 00:17:05 +00:00
|
|
|
Cycles += NDS::ARM7MemTimings[CodeCycles][0] + NDS::ARM7MemTimings[CodeCycles][1];
|
2018-11-04 22:21:58 +00:00
|
|
|
|
2016-12-03 02:10:26 +00:00
|
|
|
CPSR |= 0x20;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-05 16:08:24 +00:00
|
|
|
addr &= ~0x3;
|
2016-12-03 02:10:26 +00:00
|
|
|
R[15] = addr+4;
|
2018-11-04 22:21:58 +00:00
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
//if (newregion != oldregion) SetupCodeMem(addr);
|
2018-11-04 22:21:58 +00:00
|
|
|
|
2017-01-30 17:36:11 +00:00
|
|
|
NextInstr[0] = CodeRead32(addr);
|
|
|
|
NextInstr[1] = CodeRead32(addr+4);
|
2018-12-09 00:17:05 +00:00
|
|
|
Cycles += NDS::ARM7MemTimings[CodeCycles][2] + NDS::ARM7MemTimings[CodeCycles][3];
|
2018-11-04 22:21:58 +00:00
|
|
|
|
2016-12-03 02:10:26 +00:00
|
|
|
CPSR &= ~0x20;
|
|
|
|
}
|
2016-11-03 00:38:58 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 23:08:53 +00:00
|
|
|
void ARM::RestoreCPSR()
|
|
|
|
{
|
2016-12-04 02:20:50 +00:00
|
|
|
u32 oldcpsr = CPSR;
|
|
|
|
|
2016-12-03 16:58:24 +00:00
|
|
|
switch (CPSR & 0x1F)
|
|
|
|
{
|
|
|
|
case 0x11:
|
2017-02-09 13:18:05 +00:00
|
|
|
CPSR = R_FIQ[7];
|
2016-12-03 16:58:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x12:
|
|
|
|
CPSR = R_IRQ[2];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x13:
|
|
|
|
CPSR = R_SVC[2];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x17:
|
|
|
|
CPSR = R_ABT[2];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x1B:
|
|
|
|
CPSR = R_UND[2];
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2017-02-05 15:50:20 +00:00
|
|
|
printf("!! attempt to restore CPSR under bad mode %02X, %08X\n", CPSR&0x1F, R[15]);
|
2016-12-03 16:58:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-12-04 02:20:50 +00:00
|
|
|
|
|
|
|
UpdateMode(oldcpsr, CPSR);
|
2016-11-24 23:08:53 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 03:41:10 +00:00
|
|
|
void ARM::UpdateMode(u32 oldmode, u32 newmode)
|
|
|
|
{
|
|
|
|
u32 temp;
|
|
|
|
#define SWAP(a, b) temp = a; a = b; b = temp;
|
|
|
|
|
|
|
|
if ((oldmode & 0x1F) == (newmode & 0x1F)) return;
|
|
|
|
|
|
|
|
switch (oldmode & 0x1F)
|
|
|
|
{
|
|
|
|
case 0x11:
|
|
|
|
SWAP(R[8], R_FIQ[0]);
|
|
|
|
SWAP(R[9], R_FIQ[1]);
|
|
|
|
SWAP(R[10], R_FIQ[2]);
|
|
|
|
SWAP(R[11], R_FIQ[3]);
|
|
|
|
SWAP(R[12], R_FIQ[4]);
|
|
|
|
SWAP(R[13], R_FIQ[5]);
|
|
|
|
SWAP(R[14], R_FIQ[6]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x12:
|
|
|
|
SWAP(R[13], R_IRQ[0]);
|
|
|
|
SWAP(R[14], R_IRQ[1]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x13:
|
|
|
|
SWAP(R[13], R_SVC[0]);
|
|
|
|
SWAP(R[14], R_SVC[1]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x17:
|
|
|
|
SWAP(R[13], R_ABT[0]);
|
|
|
|
SWAP(R[14], R_ABT[1]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x1B:
|
|
|
|
SWAP(R[13], R_UND[0]);
|
|
|
|
SWAP(R[14], R_UND[1]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (newmode & 0x1F)
|
|
|
|
{
|
|
|
|
case 0x11:
|
|
|
|
SWAP(R[8], R_FIQ[0]);
|
|
|
|
SWAP(R[9], R_FIQ[1]);
|
|
|
|
SWAP(R[10], R_FIQ[2]);
|
|
|
|
SWAP(R[11], R_FIQ[3]);
|
|
|
|
SWAP(R[12], R_FIQ[4]);
|
|
|
|
SWAP(R[13], R_FIQ[5]);
|
|
|
|
SWAP(R[14], R_FIQ[6]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x12:
|
|
|
|
SWAP(R[13], R_IRQ[0]);
|
|
|
|
SWAP(R[14], R_IRQ[1]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x13:
|
|
|
|
SWAP(R[13], R_SVC[0]);
|
|
|
|
SWAP(R[14], R_SVC[1]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x17:
|
|
|
|
SWAP(R[13], R_ABT[0]);
|
|
|
|
SWAP(R[14], R_ABT[1]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x1B:
|
|
|
|
SWAP(R[13], R_UND[0]);
|
|
|
|
SWAP(R[14], R_UND[1]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef SWAP
|
2018-12-07 13:20:38 +00:00
|
|
|
|
|
|
|
if (Num == 0)
|
|
|
|
{
|
2019-06-10 16:24:38 +00:00
|
|
|
/*if ((newmode & 0x1F) == 0x10)
|
2018-12-07 13:20:38 +00:00
|
|
|
((ARMv5*)this)->PU_Map = ((ARMv5*)this)->PU_UserMap;
|
|
|
|
else
|
|
|
|
((ARMv5*)this)->PU_Map = ((ARMv5*)this)->PU_PrivMap;*/
|
|
|
|
//if ((newmode & 0x1F) == 0x10) printf("!! USER MODE\n");
|
|
|
|
}
|
2016-12-03 03:41:10 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 02:20:50 +00:00
|
|
|
void ARM::TriggerIRQ()
|
|
|
|
{
|
2017-01-18 02:11:07 +00:00
|
|
|
if (CPSR & 0x80)
|
2016-12-05 22:17:03 +00:00
|
|
|
return;
|
|
|
|
|
2016-12-04 02:20:50 +00:00
|
|
|
u32 oldcpsr = CPSR;
|
|
|
|
CPSR &= ~0xFF;
|
|
|
|
CPSR |= 0xD2;
|
|
|
|
UpdateMode(oldcpsr, CPSR);
|
|
|
|
|
|
|
|
R_IRQ[2] = oldcpsr;
|
2016-12-05 16:08:24 +00:00
|
|
|
R[14] = R[15] + (oldcpsr & 0x20 ? 2 : 0);
|
2016-12-04 02:20:50 +00:00
|
|
|
JumpTo(ExceptionBase + 0x18);
|
2020-02-14 22:34:26 +00:00
|
|
|
|
|
|
|
// ARDS cheat support
|
|
|
|
// normally, those work by hijacking the ARM7 VBlank handler
|
|
|
|
if (Num == 1)
|
|
|
|
{
|
|
|
|
if ((NDS::IF[1] & NDS::IE[1]) & (1<<NDS::IRQ_VBlank))
|
|
|
|
AREngine::RunCheats();
|
|
|
|
}
|
2016-12-04 02:20:50 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 13:20:38 +00:00
|
|
|
void ARMv5::PrefetchAbort()
|
|
|
|
{
|
|
|
|
printf("prefetch abort\n");
|
|
|
|
|
|
|
|
u32 oldcpsr = CPSR;
|
|
|
|
CPSR &= ~0xBF;
|
|
|
|
CPSR |= 0x97;
|
|
|
|
UpdateMode(oldcpsr, CPSR);
|
|
|
|
|
|
|
|
// this shouldn't happen, but if it does, we're stuck in some nasty endless loop
|
|
|
|
// so better take care of it
|
|
|
|
if (!(PU_Map[ExceptionBase>>12] & 0x04))
|
|
|
|
{
|
|
|
|
printf("!!!!! EXCEPTION REGION NOT READABLE. THIS IS VERY BAD!!\n");
|
|
|
|
NDS::Stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-10 16:24:38 +00:00
|
|
|
R_ABT[2] = oldcpsr;
|
2018-12-07 13:20:38 +00:00
|
|
|
R[14] = R[15] + (oldcpsr & 0x20 ? 2 : 0);
|
|
|
|
JumpTo(ExceptionBase + 0x0C);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ARMv5::DataAbort()
|
|
|
|
{
|
|
|
|
printf("data abort\n");
|
|
|
|
|
|
|
|
u32 oldcpsr = CPSR;
|
|
|
|
CPSR &= ~0xBF;
|
|
|
|
CPSR |= 0x97;
|
|
|
|
UpdateMode(oldcpsr, CPSR);
|
|
|
|
|
2019-06-10 16:24:38 +00:00
|
|
|
R_ABT[2] = oldcpsr;
|
2018-12-07 13:20:38 +00:00
|
|
|
R[14] = R[15] + (oldcpsr & 0x20 ? 6 : 4);
|
|
|
|
JumpTo(ExceptionBase + 0x10);
|
|
|
|
}
|
2019-03-14 23:58:29 +00:00
|
|
|
|
2019-01-05 04:28:58 +00:00
|
|
|
void ARMv5::Execute()
|
2016-11-03 00:38:58 +00:00
|
|
|
{
|
2016-12-06 16:32:51 +00:00
|
|
|
if (Halted)
|
|
|
|
{
|
2017-04-13 02:16:57 +00:00
|
|
|
if (Halted == 2)
|
|
|
|
{
|
|
|
|
Halted = 0;
|
|
|
|
}
|
2018-12-04 16:54:10 +00:00
|
|
|
else if (NDS::HaltInterrupted(0))
|
2017-01-31 02:54:51 +00:00
|
|
|
{
|
2017-01-18 16:57:12 +00:00
|
|
|
Halted = 0;
|
2018-12-04 16:54:10 +00:00
|
|
|
if (NDS::IME[0] & 0x1)
|
2018-12-11 14:56:34 +00:00
|
|
|
TriggerIRQ();
|
2017-01-31 02:54:51 +00:00
|
|
|
}
|
2016-12-06 16:32:51 +00:00
|
|
|
else
|
2017-01-30 17:36:11 +00:00
|
|
|
{
|
2019-01-05 04:28:58 +00:00
|
|
|
NDS::ARM9Timestamp = NDS::ARM9Target;
|
|
|
|
return;
|
2017-01-30 17:36:11 +00:00
|
|
|
}
|
2016-12-06 16:32:51 +00:00
|
|
|
}
|
2016-12-05 22:17:03 +00:00
|
|
|
|
2019-01-05 04:28:58 +00:00
|
|
|
while (NDS::ARM9Timestamp < NDS::ARM9Target)
|
2016-11-03 00:38:58 +00:00
|
|
|
{
|
2016-12-03 02:10:26 +00:00
|
|
|
if (CPSR & 0x20) // THUMB
|
2016-11-24 23:08:53 +00:00
|
|
|
{
|
2016-12-03 02:10:26 +00:00
|
|
|
// prefetch
|
|
|
|
R[15] += 2;
|
2017-01-30 17:36:11 +00:00
|
|
|
CurInstr = NextInstr[0];
|
|
|
|
NextInstr[0] = NextInstr[1];
|
2018-12-11 16:59:52 +00:00
|
|
|
if (R[15] & 0x2) { NextInstr[1] >>= 16; CodeCycles = 0; }
|
2019-01-04 20:47:06 +00:00
|
|
|
else NextInstr[1] = CodeRead32(R[15], false);
|
2016-12-05 16:08:24 +00:00
|
|
|
|
2016-12-03 02:10:26 +00:00
|
|
|
// actually execute
|
2018-12-04 16:54:10 +00:00
|
|
|
u32 icode = (CurInstr >> 6) & 0x3FF;
|
2017-01-30 17:36:11 +00:00
|
|
|
ARMInterpreter::THUMBInstrTable[icode](this);
|
2016-11-24 23:08:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-03 02:10:26 +00:00
|
|
|
// prefetch
|
|
|
|
R[15] += 4;
|
2017-01-30 17:36:11 +00:00
|
|
|
CurInstr = NextInstr[0];
|
|
|
|
NextInstr[0] = NextInstr[1];
|
2019-01-04 20:47:06 +00:00
|
|
|
NextInstr[1] = CodeRead32(R[15], false);
|
2016-12-05 16:08:24 +00:00
|
|
|
|
2016-12-03 02:10:26 +00:00
|
|
|
// actually execute
|
|
|
|
if (CheckCondition(CurInstr >> 28))
|
|
|
|
{
|
|
|
|
u32 icode = ((CurInstr >> 4) & 0xF) | ((CurInstr >> 16) & 0xFF0);
|
2017-01-30 17:36:11 +00:00
|
|
|
ARMInterpreter::ARMInstrTable[icode](this);
|
2016-12-03 02:10:26 +00:00
|
|
|
}
|
|
|
|
else if ((CurInstr & 0xFE000000) == 0xFA000000)
|
|
|
|
{
|
2017-01-30 17:36:11 +00:00
|
|
|
ARMInterpreter::A_BLX_IMM(this);
|
2016-12-03 02:10:26 +00:00
|
|
|
}
|
2018-12-04 16:54:10 +00:00
|
|
|
else
|
|
|
|
AddCycles_C();
|
2016-11-24 23:08:53 +00:00
|
|
|
}
|
2016-12-06 16:32:51 +00:00
|
|
|
|
2017-01-18 00:33:06 +00:00
|
|
|
// TODO optimize this shit!!!
|
2017-01-30 17:36:11 +00:00
|
|
|
if (Halted)
|
|
|
|
{
|
2019-01-05 04:28:58 +00:00
|
|
|
if (Halted == 1 && NDS::ARM9Timestamp < NDS::ARM9Target)
|
2018-12-11 02:08:46 +00:00
|
|
|
{
|
2019-01-05 04:28:58 +00:00
|
|
|
NDS::ARM9Timestamp = NDS::ARM9Target;
|
2018-12-11 02:08:46 +00:00
|
|
|
}
|
2017-01-31 02:54:51 +00:00
|
|
|
break;
|
2017-01-30 17:36:11 +00:00
|
|
|
}
|
2019-06-08 20:16:51 +00:00
|
|
|
/*if (NDS::IF[0] & NDS::IE[0])
|
2018-12-04 16:54:10 +00:00
|
|
|
{
|
|
|
|
if (NDS::IME[0] & 0x1)
|
|
|
|
TriggerIRQ();
|
2019-06-08 20:16:51 +00:00
|
|
|
}*/
|
|
|
|
if (IRQ) TriggerIRQ();
|
2019-01-05 04:28:58 +00:00
|
|
|
|
|
|
|
NDS::ARM9Timestamp += Cycles;
|
|
|
|
Cycles = 0;
|
2018-12-04 16:54:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Halted == 2)
|
|
|
|
Halted = 0;
|
|
|
|
}
|
|
|
|
|
2019-01-05 04:28:58 +00:00
|
|
|
void ARMv4::Execute()
|
2018-12-04 16:54:10 +00:00
|
|
|
{
|
|
|
|
if (Halted)
|
|
|
|
{
|
|
|
|
if (Halted == 2)
|
|
|
|
{
|
|
|
|
Halted = 0;
|
|
|
|
}
|
|
|
|
else if (NDS::HaltInterrupted(1))
|
|
|
|
{
|
|
|
|
Halted = 0;
|
|
|
|
if (NDS::IME[1] & 0x1)
|
|
|
|
TriggerIRQ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-05 04:28:58 +00:00
|
|
|
NDS::ARM7Timestamp = NDS::ARM7Target;
|
|
|
|
return;
|
2018-12-04 16:54:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-05 04:28:58 +00:00
|
|
|
while (NDS::ARM7Timestamp < NDS::ARM7Target)
|
2018-12-04 16:54:10 +00:00
|
|
|
{
|
|
|
|
if (CPSR & 0x20) // THUMB
|
|
|
|
{
|
|
|
|
// prefetch
|
|
|
|
R[15] += 2;
|
|
|
|
CurInstr = NextInstr[0];
|
|
|
|
NextInstr[0] = NextInstr[1];
|
|
|
|
NextInstr[1] = CodeRead16(R[15]);
|
|
|
|
|
|
|
|
// actually execute
|
|
|
|
u32 icode = (CurInstr >> 6);
|
|
|
|
ARMInterpreter::THUMBInstrTable[icode](this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// prefetch
|
|
|
|
R[15] += 4;
|
|
|
|
CurInstr = NextInstr[0];
|
|
|
|
NextInstr[0] = NextInstr[1];
|
|
|
|
NextInstr[1] = CodeRead32(R[15]);
|
|
|
|
|
|
|
|
// actually execute
|
|
|
|
if (CheckCondition(CurInstr >> 28))
|
|
|
|
{
|
|
|
|
u32 icode = ((CurInstr >> 4) & 0xF) | ((CurInstr >> 16) & 0xFF0);
|
|
|
|
ARMInterpreter::ARMInstrTable[icode](this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
AddCycles_C();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO optimize this shit!!!
|
|
|
|
if (Halted)
|
|
|
|
{
|
2019-01-05 04:28:58 +00:00
|
|
|
if (Halted == 1 && NDS::ARM7Timestamp < NDS::ARM7Target)
|
2018-12-11 02:08:46 +00:00
|
|
|
{
|
2019-01-05 04:28:58 +00:00
|
|
|
NDS::ARM7Timestamp = NDS::ARM7Target;
|
2018-12-11 02:08:46 +00:00
|
|
|
}
|
2018-12-04 16:54:10 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-06-08 20:16:51 +00:00
|
|
|
/*if (NDS::IF[1] & NDS::IE[1])
|
2016-12-06 16:32:51 +00:00
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
if (NDS::IME[1] & 0x1)
|
2016-12-06 16:32:51 +00:00
|
|
|
TriggerIRQ();
|
2019-06-08 20:16:51 +00:00
|
|
|
}*/
|
|
|
|
if (IRQ) TriggerIRQ();
|
2019-01-05 04:28:58 +00:00
|
|
|
|
|
|
|
NDS::ARM7Timestamp += Cycles;
|
|
|
|
Cycles = 0;
|
2016-11-03 00:38:58 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 02:54:51 +00:00
|
|
|
if (Halted == 2)
|
|
|
|
Halted = 0;
|
2016-11-03 00:38:58 +00:00
|
|
|
}
|