2016-12-05 17:02:29 +00:00
|
|
|
/*
|
2022-01-09 01:15:50 +00:00
|
|
|
Copyright 2016-2022 melonDS team
|
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>
|
|
|
|
#include "NDS.h"
|
|
|
|
#include "ARMInterpreter.h"
|
2016-11-24 23:08:53 +00:00
|
|
|
#include "ARMInterpreter_ALU.h"
|
2016-11-24 17:31:49 +00:00
|
|
|
#include "ARMInterpreter_Branch.h"
|
2016-12-03 00:31:33 +00:00
|
|
|
#include "ARMInterpreter_LoadStore.h"
|
2023-03-23 17:04:38 +00:00
|
|
|
#include "Platform.h"
|
2016-11-24 17:31:49 +00:00
|
|
|
|
2023-03-23 17:04:38 +00:00
|
|
|
using Platform::Log;
|
|
|
|
using Platform::LogLevel;
|
2016-11-24 17:31:49 +00:00
|
|
|
|
|
|
|
namespace ARMInterpreter
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2017-01-30 17:36:11 +00:00
|
|
|
void A_UNK(ARM* cpu)
|
2016-11-24 17:31:49 +00:00
|
|
|
{
|
2023-03-23 17:04:38 +00:00
|
|
|
Log(LogLevel::Warn, "undefined ARM%d instruction %08X @ %08X\n", cpu->Num?7:9, cpu->CurInstr, cpu->R[15]-8);
|
2017-06-13 13:09:39 +00:00
|
|
|
//for (int i = 0; i < 16; i++) printf("R%d: %08X\n", i, cpu->R[i]);
|
|
|
|
//NDS::Halt();
|
|
|
|
u32 oldcpsr = cpu->CPSR;
|
2018-12-07 13:20:38 +00:00
|
|
|
cpu->CPSR &= ~0xBF;
|
|
|
|
cpu->CPSR |= 0x9B;
|
2017-06-13 13:09:39 +00:00
|
|
|
cpu->UpdateMode(oldcpsr, cpu->CPSR);
|
|
|
|
|
|
|
|
cpu->R_UND[2] = oldcpsr;
|
|
|
|
cpu->R[14] = cpu->R[15] - 4;
|
|
|
|
cpu->JumpTo(cpu->ExceptionBase + 0x04);
|
2016-11-24 17:31:49 +00:00
|
|
|
}
|
|
|
|
|
2017-01-30 17:36:11 +00:00
|
|
|
void T_UNK(ARM* cpu)
|
2016-11-24 17:31:49 +00:00
|
|
|
{
|
2023-03-23 17:04:38 +00:00
|
|
|
Log(LogLevel::Warn, "undefined THUMB%d instruction %04X @ %08X\n", cpu->Num?7:9, cpu->CurInstr, cpu->R[15]-4);
|
2017-06-13 13:09:39 +00:00
|
|
|
//NDS::Halt();
|
|
|
|
u32 oldcpsr = cpu->CPSR;
|
2018-12-07 13:20:38 +00:00
|
|
|
cpu->CPSR &= ~0xBF;
|
|
|
|
cpu->CPSR |= 0x9B;
|
2017-06-13 13:09:39 +00:00
|
|
|
cpu->UpdateMode(oldcpsr, cpu->CPSR);
|
|
|
|
|
|
|
|
cpu->R_UND[2] = oldcpsr;
|
|
|
|
cpu->R[14] = cpu->R[15] - 2;
|
|
|
|
cpu->JumpTo(cpu->ExceptionBase + 0x04);
|
2016-11-24 17:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-01-30 17:36:11 +00:00
|
|
|
void A_MSR_IMM(ARM* cpu)
|
2016-12-03 01:09:04 +00:00
|
|
|
{
|
|
|
|
u32* psr;
|
|
|
|
if (cpu->CurInstr & (1<<22))
|
|
|
|
{
|
|
|
|
switch (cpu->CPSR & 0x1F)
|
|
|
|
{
|
|
|
|
case 0x11: psr = &cpu->R_FIQ[7]; break;
|
|
|
|
case 0x12: psr = &cpu->R_IRQ[2]; break;
|
|
|
|
case 0x13: psr = &cpu->R_SVC[2]; break;
|
2021-10-28 17:45:32 +00:00
|
|
|
case 0x14:
|
|
|
|
case 0x15:
|
|
|
|
case 0x16:
|
2016-12-03 01:09:04 +00:00
|
|
|
case 0x17: psr = &cpu->R_ABT[2]; break;
|
2021-10-28 17:45:32 +00:00
|
|
|
case 0x18:
|
|
|
|
case 0x19:
|
|
|
|
case 0x1A:
|
2016-12-03 01:09:04 +00:00
|
|
|
case 0x1B: psr = &cpu->R_UND[2]; break;
|
2021-10-28 17:45:32 +00:00
|
|
|
default:
|
|
|
|
cpu->AddCycles_C();
|
|
|
|
return;
|
2016-12-03 01:09:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
psr = &cpu->CPSR;
|
|
|
|
|
2016-12-03 03:41:10 +00:00
|
|
|
u32 oldpsr = *psr;
|
|
|
|
|
2016-12-03 01:09:04 +00:00
|
|
|
u32 mask = 0;
|
2017-01-18 02:11:07 +00:00
|
|
|
if (cpu->CurInstr & (1<<16)) mask |= 0x000000FF;
|
2016-12-03 01:09:04 +00:00
|
|
|
if (cpu->CurInstr & (1<<17)) mask |= 0x0000FF00;
|
|
|
|
if (cpu->CurInstr & (1<<18)) mask |= 0x00FF0000;
|
|
|
|
if (cpu->CurInstr & (1<<19)) mask |= 0xFF000000;
|
|
|
|
|
2017-01-18 02:11:07 +00:00
|
|
|
if (!(cpu->CurInstr & (1<<22)))
|
|
|
|
mask &= 0xFFFFFFDF;
|
|
|
|
|
2016-12-03 01:09:04 +00:00
|
|
|
if ((cpu->CPSR & 0x1F) == 0x10) mask &= 0xFFFFFF00;
|
|
|
|
|
|
|
|
u32 val = ROR((cpu->CurInstr & 0xFF), ((cpu->CurInstr >> 7) & 0x1E));
|
|
|
|
|
2021-10-28 17:45:32 +00:00
|
|
|
// bit4 is forced to 1
|
|
|
|
val |= 0x00000010;
|
|
|
|
|
2016-12-03 01:09:04 +00:00
|
|
|
*psr &= ~mask;
|
|
|
|
*psr |= (val & mask);
|
|
|
|
|
2016-12-03 03:41:10 +00:00
|
|
|
if (!(cpu->CurInstr & (1<<22)))
|
|
|
|
cpu->UpdateMode(oldpsr, cpu->CPSR);
|
2018-12-04 16:54:10 +00:00
|
|
|
|
|
|
|
cpu->AddCycles_C();
|
2016-12-03 01:09:04 +00:00
|
|
|
}
|
|
|
|
|
2017-01-30 17:36:11 +00:00
|
|
|
void A_MSR_REG(ARM* cpu)
|
2016-12-03 01:09:04 +00:00
|
|
|
{
|
|
|
|
u32* psr;
|
|
|
|
if (cpu->CurInstr & (1<<22))
|
|
|
|
{
|
|
|
|
switch (cpu->CPSR & 0x1F)
|
|
|
|
{
|
|
|
|
case 0x11: psr = &cpu->R_FIQ[7]; break;
|
|
|
|
case 0x12: psr = &cpu->R_IRQ[2]; break;
|
|
|
|
case 0x13: psr = &cpu->R_SVC[2]; break;
|
2021-10-28 17:45:32 +00:00
|
|
|
case 0x14:
|
|
|
|
case 0x15:
|
|
|
|
case 0x16:
|
2016-12-03 01:09:04 +00:00
|
|
|
case 0x17: psr = &cpu->R_ABT[2]; break;
|
2021-10-28 17:45:32 +00:00
|
|
|
case 0x18:
|
|
|
|
case 0x19:
|
|
|
|
case 0x1A:
|
2016-12-03 01:09:04 +00:00
|
|
|
case 0x1B: psr = &cpu->R_UND[2]; break;
|
2021-10-28 17:45:32 +00:00
|
|
|
default:
|
|
|
|
cpu->AddCycles_C();
|
|
|
|
return;
|
2016-12-03 01:09:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
psr = &cpu->CPSR;
|
|
|
|
|
2016-12-03 03:41:10 +00:00
|
|
|
u32 oldpsr = *psr;
|
|
|
|
|
2016-12-03 01:09:04 +00:00
|
|
|
u32 mask = 0;
|
2017-01-16 03:47:37 +00:00
|
|
|
if (cpu->CurInstr & (1<<16)) mask |= 0x000000FF;
|
2016-12-03 01:09:04 +00:00
|
|
|
if (cpu->CurInstr & (1<<17)) mask |= 0x0000FF00;
|
|
|
|
if (cpu->CurInstr & (1<<18)) mask |= 0x00FF0000;
|
|
|
|
if (cpu->CurInstr & (1<<19)) mask |= 0xFF000000;
|
|
|
|
|
2017-01-16 03:47:37 +00:00
|
|
|
if (!(cpu->CurInstr & (1<<22)))
|
|
|
|
mask &= 0xFFFFFFDF;
|
|
|
|
|
2016-12-03 01:09:04 +00:00
|
|
|
if ((cpu->CPSR & 0x1F) == 0x10) mask &= 0xFFFFFF00;
|
|
|
|
|
|
|
|
u32 val = cpu->R[cpu->CurInstr & 0xF];
|
|
|
|
|
2021-10-28 17:45:32 +00:00
|
|
|
// bit4 is forced to 1
|
|
|
|
val |= 0x00000010;
|
|
|
|
|
2016-12-03 01:09:04 +00:00
|
|
|
*psr &= ~mask;
|
|
|
|
*psr |= (val & mask);
|
|
|
|
|
2016-12-03 03:41:10 +00:00
|
|
|
if (!(cpu->CurInstr & (1<<22)))
|
|
|
|
cpu->UpdateMode(oldpsr, cpu->CPSR);
|
2018-12-04 16:54:10 +00:00
|
|
|
|
|
|
|
cpu->AddCycles_C();
|
2016-12-03 01:09:04 +00:00
|
|
|
}
|
|
|
|
|
2017-01-30 17:36:11 +00:00
|
|
|
void A_MRS(ARM* cpu)
|
2016-12-03 01:09:04 +00:00
|
|
|
{
|
|
|
|
u32 psr;
|
|
|
|
if (cpu->CurInstr & (1<<22))
|
|
|
|
{
|
|
|
|
switch (cpu->CPSR & 0x1F)
|
|
|
|
{
|
|
|
|
case 0x11: psr = cpu->R_FIQ[7]; break;
|
|
|
|
case 0x12: psr = cpu->R_IRQ[2]; break;
|
|
|
|
case 0x13: psr = cpu->R_SVC[2]; break;
|
2021-10-28 17:45:32 +00:00
|
|
|
case 0x14:
|
|
|
|
case 0x15:
|
|
|
|
case 0x16:
|
2016-12-03 01:09:04 +00:00
|
|
|
case 0x17: psr = cpu->R_ABT[2]; break;
|
2021-10-28 17:45:32 +00:00
|
|
|
case 0x18:
|
|
|
|
case 0x19:
|
|
|
|
case 0x1A:
|
2016-12-03 01:09:04 +00:00
|
|
|
case 0x1B: psr = cpu->R_UND[2]; break;
|
2021-10-28 17:45:32 +00:00
|
|
|
default: psr = cpu->CPSR; break;
|
2016-12-03 01:09:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
psr = cpu->CPSR;
|
|
|
|
|
|
|
|
cpu->R[(cpu->CurInstr>>12) & 0xF] = psr;
|
2018-12-04 16:54:10 +00:00
|
|
|
cpu->AddCycles_C();
|
2016-12-03 01:09:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-30 17:36:11 +00:00
|
|
|
void A_MCR(ARM* cpu)
|
2016-12-03 03:41:10 +00:00
|
|
|
{
|
2021-10-28 17:47:26 +00:00
|
|
|
if ((cpu->CPSR & 0x1F) == 0x10)
|
|
|
|
return A_UNK(cpu);
|
|
|
|
|
2016-12-03 03:41:10 +00:00
|
|
|
u32 cp = (cpu->CurInstr >> 8) & 0xF;
|
2016-12-03 15:13:04 +00:00
|
|
|
//u32 op = (cpu->CurInstr >> 21) & 0x7;
|
2016-12-03 03:41:10 +00:00
|
|
|
u32 cn = (cpu->CurInstr >> 16) & 0xF;
|
|
|
|
u32 cm = cpu->CurInstr & 0xF;
|
|
|
|
u32 cpinfo = (cpu->CurInstr >> 5) & 0x7;
|
|
|
|
|
|
|
|
if (cpu->Num==0 && cp==15)
|
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
((ARMv5*)cpu)->CP15Write((cn<<8)|(cm<<4)|cpinfo, cpu->R[(cpu->CurInstr>>12)&0xF]);
|
2016-12-03 03:41:10 +00:00
|
|
|
}
|
2017-06-13 15:44:35 +00:00
|
|
|
else if (cpu->Num==1 && cp==14)
|
|
|
|
{
|
2023-03-23 17:04:38 +00:00
|
|
|
Log(LogLevel::Debug, "MCR p14,%d,%d,%d on ARM7\n", cn, cm, cpinfo);
|
2017-06-13 15:44:35 +00:00
|
|
|
}
|
2016-12-03 03:41:10 +00:00
|
|
|
else
|
|
|
|
{
|
2023-03-23 17:04:38 +00:00
|
|
|
Log(LogLevel::Warn, "bad MCR opcode p%d,%d,%d,%d on ARM%d\n", cp, cn, cm, cpinfo, cpu->Num?7:9);
|
2017-06-13 15:44:35 +00:00
|
|
|
return A_UNK(cpu); // TODO: check what kind of exception it really is
|
2016-12-03 03:41:10 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
cpu->AddCycles_CI(1 + 1); // TODO: checkme
|
2016-12-03 03:41:10 +00:00
|
|
|
}
|
|
|
|
|
2017-01-30 17:36:11 +00:00
|
|
|
void A_MRC(ARM* cpu)
|
2016-12-03 03:41:10 +00:00
|
|
|
{
|
2021-10-28 17:47:26 +00:00
|
|
|
if ((cpu->CPSR & 0x1F) == 0x10)
|
|
|
|
return A_UNK(cpu);
|
|
|
|
|
2016-12-03 03:41:10 +00:00
|
|
|
u32 cp = (cpu->CurInstr >> 8) & 0xF;
|
2016-12-03 15:13:04 +00:00
|
|
|
//u32 op = (cpu->CurInstr >> 21) & 0x7;
|
2016-12-03 03:41:10 +00:00
|
|
|
u32 cn = (cpu->CurInstr >> 16) & 0xF;
|
|
|
|
u32 cm = cpu->CurInstr & 0xF;
|
|
|
|
u32 cpinfo = (cpu->CurInstr >> 5) & 0x7;
|
|
|
|
|
|
|
|
if (cpu->Num==0 && cp==15)
|
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
cpu->R[(cpu->CurInstr>>12)&0xF] = ((ARMv5*)cpu)->CP15Read((cn<<8)|(cm<<4)|cpinfo);
|
2016-12-03 03:41:10 +00:00
|
|
|
}
|
2017-06-13 15:44:35 +00:00
|
|
|
else if (cpu->Num==1 && cp==14)
|
|
|
|
{
|
2023-03-23 17:04:38 +00:00
|
|
|
Log(LogLevel::Debug, "MRC p14,%d,%d,%d on ARM7\n", cn, cm, cpinfo);
|
2017-06-13 15:44:35 +00:00
|
|
|
}
|
2016-12-03 03:41:10 +00:00
|
|
|
else
|
|
|
|
{
|
2023-03-23 17:04:38 +00:00
|
|
|
Log(LogLevel::Warn, "bad MRC opcode p%d,%d,%d,%d on ARM%d\n", cp, cn, cm, cpinfo, cpu->Num?7:9);
|
2017-06-13 15:44:35 +00:00
|
|
|
return A_UNK(cpu); // TODO: check what kind of exception it really is
|
2016-12-03 03:41:10 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
cpu->AddCycles_CI(2 + 1); // TODO: checkme
|
2016-12-03 03:41:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-03 01:09:04 +00:00
|
|
|
|
2017-01-30 17:36:11 +00:00
|
|
|
void A_SVC(ARM* cpu)
|
2017-01-16 03:47:37 +00:00
|
|
|
{
|
|
|
|
u32 oldcpsr = cpu->CPSR;
|
2018-12-07 13:20:38 +00:00
|
|
|
cpu->CPSR &= ~0xBF;
|
|
|
|
cpu->CPSR |= 0x93;
|
2017-01-16 03:47:37 +00:00
|
|
|
cpu->UpdateMode(oldcpsr, cpu->CPSR);
|
|
|
|
|
|
|
|
cpu->R_SVC[2] = oldcpsr;
|
|
|
|
cpu->R[14] = cpu->R[15] - 4;
|
|
|
|
cpu->JumpTo(cpu->ExceptionBase + 0x08);
|
|
|
|
}
|
|
|
|
|
2017-01-30 17:36:11 +00:00
|
|
|
void T_SVC(ARM* cpu)
|
2016-12-05 16:08:24 +00:00
|
|
|
{
|
|
|
|
u32 oldcpsr = cpu->CPSR;
|
2018-12-07 13:20:38 +00:00
|
|
|
cpu->CPSR &= ~0xBF;
|
|
|
|
cpu->CPSR |= 0x93;
|
2016-12-05 16:08:24 +00:00
|
|
|
cpu->UpdateMode(oldcpsr, cpu->CPSR);
|
|
|
|
|
|
|
|
cpu->R_SVC[2] = oldcpsr;
|
|
|
|
cpu->R[14] = cpu->R[15] - 2;
|
|
|
|
cpu->JumpTo(cpu->ExceptionBase + 0x08);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-01-30 17:36:11 +00:00
|
|
|
#define INSTRFUNC_PROTO(x) void (*x)(ARM* cpu)
|
2016-11-24 17:31:49 +00:00
|
|
|
#include "ARM_InstrTable.h"
|
|
|
|
#undef INSTRFUNC_PROTO
|
|
|
|
|
|
|
|
}
|