2016-12-05 17:02:29 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016-2017 StapleButter
|
|
|
|
|
|
|
|
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"
|
2016-11-03 00:38:58 +00:00
|
|
|
|
|
|
|
|
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;
|
2017-01-30 17:36:11 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 16; i++)
|
|
|
|
{
|
|
|
|
Waitstates[0][i] = 1;
|
|
|
|
Waitstates[1][i] = 1;
|
|
|
|
Waitstates[2][i] = 1;
|
|
|
|
Waitstates[3][i] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!num)
|
|
|
|
{
|
|
|
|
// ARM9
|
|
|
|
Waitstates[0][0x2] = 1; // main RAM timing, assuming cache hit
|
|
|
|
Waitstates[0][0x3] = 4;
|
|
|
|
Waitstates[0][0x4] = 4;
|
|
|
|
Waitstates[0][0x5] = 5;
|
|
|
|
Waitstates[0][0x6] = 5;
|
|
|
|
Waitstates[0][0x7] = 4;
|
|
|
|
Waitstates[0][0x8] = 19;
|
|
|
|
Waitstates[0][0x9] = 19;
|
|
|
|
Waitstates[0][0xF] = 4;
|
|
|
|
|
|
|
|
Waitstates[1][0x2] = 1;
|
|
|
|
Waitstates[1][0x3] = 8;
|
|
|
|
Waitstates[1][0x4] = 8;
|
|
|
|
Waitstates[1][0x5] = 10;
|
|
|
|
Waitstates[1][0x6] = 10;
|
|
|
|
Waitstates[1][0x7] = 8;
|
|
|
|
Waitstates[1][0x8] = 38;
|
|
|
|
Waitstates[1][0x9] = 38;
|
|
|
|
Waitstates[1][0xF] = 8;
|
|
|
|
|
|
|
|
Waitstates[2][0x2] = 1;
|
|
|
|
Waitstates[2][0x3] = 2;
|
|
|
|
Waitstates[2][0x4] = 2;
|
|
|
|
Waitstates[2][0x5] = 2;
|
|
|
|
Waitstates[2][0x6] = 2;
|
|
|
|
Waitstates[2][0x7] = 2;
|
|
|
|
Waitstates[2][0x8] = 12;
|
|
|
|
Waitstates[2][0x9] = 12;
|
|
|
|
Waitstates[2][0xA] = 20;
|
|
|
|
Waitstates[2][0xF] = 2;
|
|
|
|
|
|
|
|
Waitstates[3][0x2] = 1;
|
|
|
|
Waitstates[3][0x3] = 2;
|
|
|
|
Waitstates[3][0x4] = 2;
|
|
|
|
Waitstates[3][0x5] = 4;
|
|
|
|
Waitstates[3][0x6] = 4;
|
|
|
|
Waitstates[3][0x7] = 2;
|
|
|
|
Waitstates[3][0x8] = 24;
|
|
|
|
Waitstates[3][0x9] = 24;
|
|
|
|
Waitstates[3][0xA] = 20;
|
|
|
|
Waitstates[3][0xF] = 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// ARM7
|
|
|
|
Waitstates[0][0x0] = 1;
|
|
|
|
Waitstates[0][0x2] = 1;
|
|
|
|
Waitstates[0][0x3] = 1;
|
|
|
|
Waitstates[0][0x4] = 1;
|
|
|
|
Waitstates[0][0x6] = 1;
|
|
|
|
Waitstates[0][0x8] = 6;
|
|
|
|
Waitstates[0][0x9] = 6;
|
|
|
|
|
|
|
|
Waitstates[1][0x0] = 1;
|
|
|
|
Waitstates[1][0x2] = 2;
|
|
|
|
Waitstates[1][0x3] = 1;
|
|
|
|
Waitstates[1][0x4] = 1;
|
|
|
|
Waitstates[1][0x6] = 2;
|
|
|
|
Waitstates[1][0x8] = 12;
|
|
|
|
Waitstates[1][0x9] = 12;
|
|
|
|
|
|
|
|
Waitstates[2][0x0] = 1;
|
|
|
|
Waitstates[2][0x2] = 1;
|
|
|
|
Waitstates[2][0x3] = 1;
|
|
|
|
Waitstates[2][0x4] = 1;
|
|
|
|
Waitstates[2][0x6] = 1;
|
|
|
|
Waitstates[2][0x8] = 6;
|
|
|
|
Waitstates[2][0x9] = 6;
|
|
|
|
Waitstates[2][0xA] = 10;
|
|
|
|
|
|
|
|
Waitstates[3][0x0] = 1;
|
|
|
|
Waitstates[3][0x2] = 2;
|
|
|
|
Waitstates[3][0x3] = 1;
|
|
|
|
Waitstates[3][0x4] = 1;
|
|
|
|
Waitstates[3][0x6] = 2;
|
|
|
|
Waitstates[3][0x8] = 12;
|
|
|
|
Waitstates[3][0x9] = 12;
|
|
|
|
Waitstates[3][0xA] = 10;
|
|
|
|
}
|
2016-11-24 17:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ARM::~ARM()
|
|
|
|
{
|
|
|
|
// dorp
|
|
|
|
}
|
2016-11-03 00:38:58 +00:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
// zorp
|
|
|
|
JumpTo(ExceptionBase);
|
|
|
|
}
|
|
|
|
|
2016-12-05 16:08:24 +00:00
|
|
|
void ARM::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
|
|
|
|
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;
|
2017-01-30 17:36:11 +00:00
|
|
|
NextInstr[0] = CodeRead16(addr);
|
|
|
|
NextInstr[1] = CodeRead16(addr+2);
|
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;
|
2017-01-30 17:36:11 +00:00
|
|
|
NextInstr[0] = CodeRead32(addr);
|
|
|
|
NextInstr[1] = CodeRead32(addr+4);
|
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:
|
|
|
|
CPSR = R_FIQ[8];
|
|
|
|
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:
|
|
|
|
printf("!! attempt to restore CPSR under bad mode %02X\n", CPSR&0x1F);
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-01-31 02:54:51 +00:00
|
|
|
s32 ARM::Execute()
|
2016-11-03 00:38:58 +00:00
|
|
|
{
|
2016-12-06 16:32:51 +00:00
|
|
|
if (Halted)
|
|
|
|
{
|
|
|
|
if (NDS::HaltInterrupted(Num))
|
2017-01-31 02:54:51 +00:00
|
|
|
{
|
2017-01-18 16:57:12 +00:00
|
|
|
Halted = 0;
|
2017-01-31 02:54:51 +00:00
|
|
|
if (NDS::IME[Num]&1)
|
|
|
|
TriggerIRQ();
|
|
|
|
}
|
2016-12-06 16:32:51 +00:00
|
|
|
else
|
2017-01-30 17:36:11 +00:00
|
|
|
{
|
2017-01-31 02:54:51 +00:00
|
|
|
Cycles = CyclesToRun;
|
2017-01-30 17:36:11 +00:00
|
|
|
return Cycles;
|
|
|
|
}
|
2016-12-06 16:32:51 +00:00
|
|
|
}
|
2016-12-05 22:17:03 +00:00
|
|
|
|
2017-01-30 17:36:11 +00:00
|
|
|
Cycles = 0;
|
2016-12-23 20:22:22 +00:00
|
|
|
u32 addr = R[15] - (CPSR&0x20 ? 4:8);
|
|
|
|
u32 cpsr = CPSR;
|
2016-12-05 16:08:24 +00:00
|
|
|
|
2017-01-31 02:54:51 +00:00
|
|
|
while (Cycles < CyclesToRun)
|
2016-11-03 00:38:58 +00:00
|
|
|
{
|
2017-01-16 03:47:37 +00:00
|
|
|
//if(Num==1)printf("%08X %08X\n", R[15] - (CPSR&0x20 ? 4:8), NextInstr);
|
|
|
|
|
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];
|
|
|
|
NextInstr[1] = CodeRead16(R[15]);
|
2016-12-05 16:08:24 +00:00
|
|
|
|
2016-12-03 02:10:26 +00:00
|
|
|
// actually execute
|
|
|
|
u32 icode = (CurInstr >> 6);
|
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];
|
|
|
|
NextInstr[1] = CodeRead32(R[15]);
|
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
|
|
|
}
|
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)
|
|
|
|
{
|
2017-01-31 02:54:51 +00:00
|
|
|
if (Halted == 1)
|
|
|
|
Cycles = CyclesToRun;
|
|
|
|
break;
|
2017-01-30 17:36:11 +00:00
|
|
|
}
|
2016-12-06 16:32:51 +00:00
|
|
|
if (NDS::HaltInterrupted(Num))
|
|
|
|
{
|
|
|
|
if (NDS::IME[Num]&1)
|
|
|
|
TriggerIRQ();
|
|
|
|
}
|
2016-12-23 20:22:22 +00:00
|
|
|
|
2017-01-18 02:11:07 +00:00
|
|
|
// temp. debug cruft
|
2016-12-23 20:22:22 +00:00
|
|
|
addr = R[15] - (CPSR&0x20 ? 4:8);
|
|
|
|
cpsr = CPSR;
|
2016-11-03 00:38:58 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 02:54:51 +00:00
|
|
|
if (Halted == 2)
|
|
|
|
Halted = 0;
|
|
|
|
|
2017-01-30 17:36:11 +00:00
|
|
|
return Cycles;
|
2016-11-03 00:38:58 +00:00
|
|
|
}
|