2016-12-05 10:02:29 -07: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-02 18:38:58 -06:00
|
|
|
|
|
|
|
#ifndef ARM_H
|
|
|
|
#define ARM_H
|
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
#include "NDS.h"
|
2017-01-30 11:11:29 -07:00
|
|
|
#include "CP15.h"
|
2016-11-02 18:38:58 -06:00
|
|
|
|
2016-11-24 10:31:49 -07:00
|
|
|
// lame
|
|
|
|
#define C_S(x) x
|
|
|
|
#define C_N(x) x
|
2016-11-24 16:08:53 -07:00
|
|
|
#define C_I(x) x
|
|
|
|
|
|
|
|
#define ROR(x, n) (((x) >> (n)) | ((x) << (32-(n))))
|
2016-11-24 10:31:49 -07:00
|
|
|
|
2016-11-02 18:38:58 -06:00
|
|
|
class ARM
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ARM(u32 num);
|
|
|
|
~ARM(); // destroy shit
|
|
|
|
|
2016-11-24 10:31:49 -07:00
|
|
|
void Reset();
|
|
|
|
|
2016-12-05 09:08:24 -07:00
|
|
|
void JumpTo(u32 addr, bool restorecpsr = false);
|
2016-11-24 16:08:53 -07:00
|
|
|
void RestoreCPSR();
|
|
|
|
|
2016-12-05 15:17:03 -07:00
|
|
|
void Halt(u32 halt)
|
|
|
|
{
|
|
|
|
Halted = halt;
|
|
|
|
}
|
|
|
|
|
2017-01-30 19:54:51 -07:00
|
|
|
s32 Execute();
|
2016-11-02 18:38:58 -06:00
|
|
|
|
2016-11-24 16:08:53 -07:00
|
|
|
bool CheckCondition(u32 code)
|
|
|
|
{
|
|
|
|
if (code == 0xE) return true;
|
|
|
|
if (ConditionTable[code] & (1 << (CPSR>>28))) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetC(bool c)
|
|
|
|
{
|
|
|
|
if (c) CPSR |= 0x20000000;
|
|
|
|
else CPSR &= ~0x20000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetNZ(bool n, bool z)
|
|
|
|
{
|
|
|
|
CPSR &= ~0xC0000000;
|
|
|
|
if (n) CPSR |= 0x80000000;
|
|
|
|
if (z) CPSR |= 0x40000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetNZCV(bool n, bool z, bool c, bool v)
|
|
|
|
{
|
|
|
|
CPSR &= ~0xF0000000;
|
|
|
|
if (n) CPSR |= 0x80000000;
|
|
|
|
if (z) CPSR |= 0x40000000;
|
|
|
|
if (c) CPSR |= 0x20000000;
|
|
|
|
if (v) CPSR |= 0x10000000;
|
|
|
|
}
|
|
|
|
|
2016-12-02 20:41:10 -07:00
|
|
|
void UpdateMode(u32 oldmode, u32 newmode);
|
|
|
|
|
2016-12-03 19:20:50 -07:00
|
|
|
void TriggerIRQ();
|
|
|
|
|
2016-12-02 17:31:33 -07:00
|
|
|
|
2017-01-30 10:36:11 -07:00
|
|
|
u16 CodeRead16(u32 addr)
|
2016-12-02 17:31:33 -07:00
|
|
|
{
|
2017-01-30 10:36:11 -07:00
|
|
|
u16 val;
|
|
|
|
// TODO eventually: on ARM9, THUMB opcodes are prefetched with 32bit reads
|
2016-12-02 17:31:33 -07:00
|
|
|
if (!Num)
|
|
|
|
{
|
2017-01-30 11:11:29 -07:00
|
|
|
if (!CP15::HandleCodeRead16(addr, &val))
|
|
|
|
val = NDS::ARM9Read16(addr);
|
2016-12-02 17:31:33 -07:00
|
|
|
}
|
|
|
|
else
|
2017-01-30 10:36:11 -07:00
|
|
|
val = NDS::ARM7Read16(addr);
|
|
|
|
|
|
|
|
Cycles += Waitstates[0][(addr>>24)&0xF];
|
|
|
|
return val;
|
2016-12-02 17:31:33 -07:00
|
|
|
}
|
|
|
|
|
2017-01-30 10:36:11 -07:00
|
|
|
u32 CodeRead32(u32 addr)
|
2016-11-02 18:38:58 -06:00
|
|
|
{
|
2017-01-30 10:36:11 -07:00
|
|
|
u32 val;
|
|
|
|
if (!Num)
|
|
|
|
{
|
2017-01-30 11:11:29 -07:00
|
|
|
if (!CP15::HandleCodeRead32(addr, &val))
|
|
|
|
val = NDS::ARM9Read32(addr);
|
2017-01-30 10:36:11 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
val = NDS::ARM7Read32(addr);
|
|
|
|
|
|
|
|
Cycles += Waitstates[1][(addr>>24)&0xF];
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
u8 DataRead8(u32 addr, u32 forceuser=0)
|
|
|
|
{
|
|
|
|
u8 val;
|
|
|
|
if (!Num)
|
|
|
|
{
|
2017-01-30 11:11:29 -07:00
|
|
|
if (!CP15::HandleDataRead8(addr, &val, forceuser))
|
|
|
|
val = NDS::ARM9Read8(addr);
|
2017-01-30 10:36:11 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
val = NDS::ARM7Read8(addr);
|
|
|
|
|
|
|
|
Cycles += Waitstates[3][(addr>>24)&0xF];
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
u16 DataRead16(u32 addr, u32 forceuser=0)
|
|
|
|
{
|
|
|
|
u16 val;
|
2016-12-02 17:31:33 -07:00
|
|
|
addr &= ~1;
|
|
|
|
if (!Num)
|
|
|
|
{
|
2017-01-30 11:11:29 -07:00
|
|
|
if (!CP15::HandleDataRead16(addr, &val, forceuser))
|
|
|
|
val = NDS::ARM9Read16(addr);
|
2016-12-02 17:31:33 -07:00
|
|
|
}
|
|
|
|
else
|
2017-01-30 10:36:11 -07:00
|
|
|
val = NDS::ARM7Read16(addr);
|
|
|
|
|
|
|
|
Cycles += Waitstates[2][(addr>>24)&0xF];
|
|
|
|
return val;
|
2016-12-02 17:31:33 -07:00
|
|
|
}
|
|
|
|
|
2017-01-30 10:36:11 -07:00
|
|
|
u32 DataRead32(u32 addr, u32 forceuser=0)
|
2016-12-02 17:31:33 -07:00
|
|
|
{
|
2017-01-30 10:36:11 -07:00
|
|
|
u32 val;
|
2016-12-02 17:31:33 -07:00
|
|
|
addr &= ~3;
|
|
|
|
if (!Num)
|
|
|
|
{
|
2017-01-30 11:11:29 -07:00
|
|
|
if (!CP15::HandleDataRead32(addr, &val, forceuser))
|
|
|
|
val = NDS::ARM9Read32(addr);
|
2016-12-02 17:31:33 -07:00
|
|
|
}
|
|
|
|
else
|
2017-01-30 10:36:11 -07:00
|
|
|
val = NDS::ARM7Read32(addr);
|
|
|
|
|
|
|
|
Cycles += Waitstates[3][(addr>>24)&0xF];
|
|
|
|
return val;
|
2016-12-02 17:31:33 -07:00
|
|
|
}
|
|
|
|
|
2017-01-30 10:36:11 -07:00
|
|
|
void DataWrite8(u32 addr, u8 val, u32 forceuser=0)
|
2016-12-02 17:31:33 -07:00
|
|
|
{
|
|
|
|
if (!Num)
|
|
|
|
{
|
2017-01-30 11:11:29 -07:00
|
|
|
if (!CP15::HandleDataWrite8(addr, val, forceuser))
|
|
|
|
NDS::ARM9Write8(addr, val);
|
2016-12-02 17:31:33 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
NDS::ARM7Write8(addr, val);
|
2017-01-30 10:36:11 -07:00
|
|
|
|
|
|
|
Cycles += Waitstates[3][(addr>>24)&0xF];
|
2016-12-02 17:31:33 -07:00
|
|
|
}
|
|
|
|
|
2017-01-30 10:36:11 -07:00
|
|
|
void DataWrite16(u32 addr, u16 val, u32 forceuser=0)
|
2016-12-02 17:31:33 -07:00
|
|
|
{
|
|
|
|
addr &= ~1;
|
|
|
|
if (!Num)
|
|
|
|
{
|
2017-01-30 11:11:29 -07:00
|
|
|
if (!CP15::HandleDataWrite16(addr, val, forceuser))
|
|
|
|
NDS::ARM9Write16(addr, val);
|
2016-12-02 17:31:33 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
NDS::ARM7Write16(addr, val);
|
2017-01-30 10:36:11 -07:00
|
|
|
|
|
|
|
Cycles += Waitstates[2][(addr>>24)&0xF];
|
2016-12-02 17:31:33 -07:00
|
|
|
}
|
|
|
|
|
2017-01-30 10:36:11 -07:00
|
|
|
void DataWrite32(u32 addr, u32 val, u32 forceuser=0)
|
2016-12-02 17:31:33 -07:00
|
|
|
{
|
|
|
|
addr &= ~3;
|
|
|
|
if (!Num)
|
|
|
|
{
|
2017-01-30 11:11:29 -07:00
|
|
|
if (!CP15::HandleDataWrite32(addr, val, forceuser))
|
|
|
|
NDS::ARM9Write32(addr, val);
|
2016-12-02 17:31:33 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
NDS::ARM7Write32(addr, val);
|
|
|
|
|
2017-01-30 10:36:11 -07:00
|
|
|
Cycles += Waitstates[3][(addr>>24)&0xF];
|
2016-11-02 18:38:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
u32 Num;
|
|
|
|
|
2017-01-30 10:36:11 -07:00
|
|
|
// waitstates:
|
|
|
|
// 0=code16 1=code32 2=data16 3=data32
|
|
|
|
// TODO eventually: nonsequential waitstates
|
|
|
|
s32 Waitstates[4][16];
|
|
|
|
|
2016-12-05 09:08:24 -07:00
|
|
|
s32 Cycles;
|
2017-01-30 19:54:51 -07:00
|
|
|
s32 CyclesToRun;
|
2016-12-05 15:17:03 -07:00
|
|
|
u32 Halted;
|
2016-12-05 09:08:24 -07:00
|
|
|
|
2016-11-02 18:38:58 -06:00
|
|
|
u32 R[16]; // heh
|
|
|
|
u32 CPSR;
|
|
|
|
u32 R_FIQ[8]; // holding SPSR too
|
|
|
|
u32 R_SVC[3];
|
|
|
|
u32 R_ABT[3];
|
|
|
|
u32 R_IRQ[3];
|
|
|
|
u32 R_UND[3];
|
|
|
|
u32 CurInstr;
|
2017-01-30 10:36:11 -07:00
|
|
|
u32 NextInstr[2];
|
2016-11-02 18:38:58 -06:00
|
|
|
|
|
|
|
u32 ExceptionBase;
|
2016-11-24 16:08:53 -07:00
|
|
|
|
|
|
|
static u32 ConditionTable[16];
|
2016-12-23 13:22:22 -07:00
|
|
|
|
|
|
|
u32 debug;
|
2016-11-02 18:38:58 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // ARM_H
|