2016-12-05 10:02:29 -07:00
|
|
|
/*
|
2018-09-14 18:32:13 -06:00
|
|
|
Copyright 2016-2019 StapleButter
|
2016-12-05 10:02:29 -07: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-02 18:38:58 -06:00
|
|
|
|
|
|
|
#ifndef ARM_H
|
|
|
|
#define ARM_H
|
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
#include <algorithm>
|
|
|
|
|
2016-11-02 18:38:58 -06:00
|
|
|
#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 16:08:53 -07:00
|
|
|
#define ROR(x, n) (((x) >> (n)) | ((x) << (32-(n))))
|
2016-11-24 10:31:49 -07:00
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
RWFlags_Nonseq = (1<<5),
|
|
|
|
RWFlags_ForceUser = (1<<21),
|
|
|
|
};
|
|
|
|
|
2016-11-02 18:38:58 -06:00
|
|
|
class ARM
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ARM(u32 num);
|
|
|
|
~ARM(); // destroy shit
|
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
virtual void Reset();
|
|
|
|
|
|
|
|
virtual void DoSavestate(Savestate* file);
|
2016-11-24 10:31:49 -07:00
|
|
|
|
2018-11-07 10:38:54 -07:00
|
|
|
void SetClockShift(u32 shift)
|
|
|
|
{
|
|
|
|
ClockShift = shift;
|
|
|
|
ClockDiffMask = (1<<shift) - 1;
|
|
|
|
}
|
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
virtual void CalculateTimings() = 0;
|
2018-09-14 18:47:34 -06:00
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
virtual void JumpTo(u32 addr, bool restorecpsr = false) = 0;
|
2016-11-24 16:08:53 -07:00
|
|
|
void RestoreCPSR();
|
|
|
|
|
2016-12-05 15:17:03 -07:00
|
|
|
void Halt(u32 halt)
|
|
|
|
{
|
2017-04-12 20:16:57 -06:00
|
|
|
if (halt==2 && Halted==1) return;
|
2016-12-05 15:17:03 -07:00
|
|
|
Halted = halt;
|
|
|
|
}
|
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
// TODO: is this actually used??
|
2017-04-22 08:47:31 -06:00
|
|
|
void CheckIRQ()
|
|
|
|
{
|
|
|
|
if (!(NDS::IME[Num] & 0x1)) return;
|
|
|
|
if (NDS::IF[Num] & NDS::IE[Num])
|
|
|
|
{
|
|
|
|
TriggerIRQ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
virtual s32 Execute() = 0;
|
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();
|
|
|
|
|
2018-11-04 15:21:58 -07:00
|
|
|
void SetupCodeMem(u32 addr);
|
|
|
|
|
2016-12-02 17:31:33 -07:00
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
virtual bool DataRead8(u32 addr, u32* val, u32 flags) = 0;
|
|
|
|
virtual bool DataRead16(u32 addr, u32* val, u32 flags) = 0;
|
|
|
|
virtual bool DataRead32(u32 addr, u32* val, u32 flags) = 0;
|
|
|
|
virtual bool DataWrite8(u32 addr, u8 val, u32 flags) = 0;
|
|
|
|
virtual bool DataWrite16(u32 addr, u16 val, u32 flags) = 0;
|
|
|
|
virtual bool DataWrite32(u32 addr, u32 val, u32 flags) = 0;
|
2018-11-04 15:21:58 -07:00
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
virtual void AddCycles_C() = 0;
|
|
|
|
virtual void AddCycles_CI(s32 num) = 0;
|
|
|
|
virtual void AddCycles_CDI() = 0;
|
|
|
|
virtual void AddCycles_CD() = 0;
|
2018-11-04 15:21:58 -07:00
|
|
|
|
2017-01-30 10:36:11 -07:00
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
u32 Num;
|
|
|
|
|
|
|
|
// shift relative to system clock
|
|
|
|
// 0=33MHz 1=66MHz 2=133MHz
|
|
|
|
u32 ClockShift;
|
|
|
|
u32 ClockDiffMask;
|
|
|
|
|
|
|
|
s32 Cycles;
|
|
|
|
s32 CyclesToRun;
|
|
|
|
u32 Halted;
|
|
|
|
|
|
|
|
int CodeRegion;
|
|
|
|
|
|
|
|
int DataRegion;
|
|
|
|
s32 DataCycles;
|
|
|
|
|
|
|
|
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;
|
|
|
|
u32 NextInstr[2];
|
|
|
|
|
|
|
|
u32 ExceptionBase;
|
|
|
|
|
|
|
|
NDS::MemRegion CodeMem;
|
|
|
|
|
|
|
|
static u32 ConditionTable[16];
|
|
|
|
};
|
|
|
|
|
|
|
|
class ARMv5 : public ARM
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ARMv5();
|
|
|
|
|
|
|
|
void Reset();
|
|
|
|
|
|
|
|
void DoSavestate(Savestate* file);
|
|
|
|
|
|
|
|
void CalculateTimings();
|
|
|
|
|
|
|
|
void JumpTo(u32 addr, bool restorecpsr = false);
|
|
|
|
|
|
|
|
s32 Execute();
|
|
|
|
|
|
|
|
// all code accesses are forced nonseq 32bit
|
|
|
|
u32 CodeRead32(u32 addr);
|
|
|
|
|
|
|
|
bool DataRead8(u32 addr, u32* val, u32 flags);
|
|
|
|
bool DataRead16(u32 addr, u32* val, u32 flags);
|
|
|
|
bool DataRead32(u32 addr, u32* val, u32 flags);
|
|
|
|
bool DataWrite8(u32 addr, u8 val, u32 flags);
|
|
|
|
bool DataWrite16(u32 addr, u16 val, u32 flags);
|
|
|
|
bool DataWrite32(u32 addr, u32 val, u32 flags);
|
|
|
|
|
|
|
|
void AddCycles_C()
|
|
|
|
{
|
|
|
|
// code only. always nonseq 32-bit for ARM9.
|
|
|
|
Cycles += NDS::ARM9MemTimings[CodeRegion][2];
|
2016-12-02 17:31:33 -07:00
|
|
|
}
|
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
void AddCycles_CI(s32 num)
|
2016-11-02 18:38:58 -06:00
|
|
|
{
|
2018-12-04 09:54:10 -07:00
|
|
|
// code+internal
|
|
|
|
Cycles += NDS::ARM9MemTimings[CodeRegion][2] + num;
|
|
|
|
}
|
2018-11-04 15:21:58 -07:00
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
void AddCycles_CDI()
|
|
|
|
{
|
|
|
|
// LDR/LDM cycles. ARM9 seems to skip the internal cycle there.
|
|
|
|
// TODO: ITCM data fetches shouldn't be parallelized, they say
|
|
|
|
s32 numC = NDS::ARM9MemTimings[CodeRegion][2];
|
|
|
|
s32 numD = DataCycles;
|
2018-11-04 15:21:58 -07:00
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
if (DataRegion != CodeRegion)
|
|
|
|
Cycles += std::max(numC + numD - 6, std::max(numC, numD));
|
2017-01-30 10:36:11 -07:00
|
|
|
else
|
2018-12-04 09:54:10 -07:00
|
|
|
Cycles += numC + numD;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddCycles_CD()
|
|
|
|
{
|
|
|
|
// TODO: ITCM data fetches shouldn't be parallelized, they say
|
|
|
|
s32 numC = NDS::ARM9MemTimings[CodeRegion][2];
|
|
|
|
s32 numD = DataCycles;
|
|
|
|
|
|
|
|
if (DataRegion != CodeRegion)
|
|
|
|
Cycles += std::max(numC + numD - 6, std::max(numC, numD));
|
|
|
|
else
|
|
|
|
Cycles += numC + numD;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetCodeMemRegion(u32 addr, NDS::MemRegion* region);
|
|
|
|
|
|
|
|
void CP15Reset();
|
|
|
|
void CP15DoSavestate(Savestate* file);
|
|
|
|
|
|
|
|
void UpdateDTCMSetting();
|
|
|
|
void UpdateITCMSetting();
|
|
|
|
|
|
|
|
void CP15Write(u32 id, u32 val);
|
|
|
|
u32 CP15Read(u32 id);
|
|
|
|
|
|
|
|
u32 CP15Control;
|
2017-01-30 10:36:11 -07:00
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
u32 DTCMSetting, ITCMSetting;
|
|
|
|
|
|
|
|
u8 ITCM[0x8000];
|
|
|
|
u32 ITCMSize;
|
|
|
|
u8 DTCM[0x4000];
|
|
|
|
u32 DTCMBase, DTCMSize;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ARMv4 : public ARM
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ARMv4();
|
|
|
|
|
|
|
|
void CalculateTimings();
|
|
|
|
|
|
|
|
void JumpTo(u32 addr, bool restorecpsr = false);
|
|
|
|
|
|
|
|
s32 Execute();
|
|
|
|
|
|
|
|
u16 CodeRead16(u32 addr)
|
|
|
|
{
|
|
|
|
u32 ret;
|
|
|
|
CodeRegion = NDS::ARM7Read16(addr, &ret);
|
|
|
|
return ret;
|
2017-01-30 10:36:11 -07:00
|
|
|
}
|
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
u32 CodeRead32(u32 addr)
|
|
|
|
{
|
|
|
|
u32 ret;
|
|
|
|
CodeRegion = NDS::ARM7Read32(addr, &ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2017-01-30 10:36:11 -07:00
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
bool DataRead8(u32 addr, u32* val, u32 flags)
|
2017-01-30 10:36:11 -07:00
|
|
|
{
|
2018-12-04 09:54:10 -07:00
|
|
|
DataRegion = NDS::ARM7Read8(addr, val);
|
|
|
|
if (flags & RWFlags_Nonseq)
|
|
|
|
DataCycles = NDS::ARM7MemTimings[DataRegion][0];
|
2017-01-30 10:36:11 -07:00
|
|
|
else
|
2018-12-04 09:54:10 -07:00
|
|
|
DataCycles += NDS::ARM7MemTimings[DataRegion][1];
|
2017-01-30 10:36:11 -07:00
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
return true;
|
2017-01-30 10:36:11 -07:00
|
|
|
}
|
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
bool DataRead16(u32 addr, u32* val, u32 flags)
|
2017-01-30 10:36:11 -07:00
|
|
|
{
|
2016-12-02 17:31:33 -07:00
|
|
|
addr &= ~1;
|
2018-12-04 09:54:10 -07:00
|
|
|
|
|
|
|
DataRegion = NDS::ARM7Read16(addr, val);
|
|
|
|
if (flags & RWFlags_Nonseq)
|
|
|
|
DataCycles = NDS::ARM7MemTimings[DataRegion][0];
|
2016-12-02 17:31:33 -07:00
|
|
|
else
|
2018-12-04 09:54:10 -07:00
|
|
|
DataCycles += NDS::ARM7MemTimings[DataRegion][1];
|
2017-01-30 10:36:11 -07:00
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
return true;
|
2016-12-02 17:31:33 -07:00
|
|
|
}
|
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
bool DataRead32(u32 addr, u32* val, u32 flags)
|
2016-12-02 17:31:33 -07:00
|
|
|
{
|
|
|
|
addr &= ~3;
|
2018-12-04 09:54:10 -07:00
|
|
|
|
|
|
|
DataRegion = NDS::ARM7Read32(addr, val);
|
|
|
|
if (flags & RWFlags_Nonseq)
|
|
|
|
DataCycles = NDS::ARM7MemTimings[DataRegion][2];
|
2016-12-02 17:31:33 -07:00
|
|
|
else
|
2018-12-04 09:54:10 -07:00
|
|
|
DataCycles += NDS::ARM7MemTimings[DataRegion][3];
|
2017-01-30 10:36:11 -07:00
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
return true;
|
2016-12-02 17:31:33 -07:00
|
|
|
}
|
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
bool DataWrite8(u32 addr, u8 val, u32 flags)
|
2016-12-02 17:31:33 -07:00
|
|
|
{
|
2018-12-04 09:54:10 -07:00
|
|
|
DataRegion = NDS::ARM7Write8(addr, val);
|
|
|
|
if (flags & RWFlags_Nonseq)
|
|
|
|
DataCycles = NDS::ARM7MemTimings[DataRegion][0];
|
2016-12-02 17:31:33 -07:00
|
|
|
else
|
2018-12-04 09:54:10 -07:00
|
|
|
DataCycles += NDS::ARM7MemTimings[DataRegion][1];
|
2017-01-30 10:36:11 -07:00
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
return true;
|
2016-12-02 17:31:33 -07:00
|
|
|
}
|
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
bool DataWrite16(u32 addr, u16 val, u32 flags)
|
2016-12-02 17:31:33 -07:00
|
|
|
{
|
|
|
|
addr &= ~1;
|
2018-12-04 09:54:10 -07:00
|
|
|
|
|
|
|
DataRegion = NDS::ARM7Write16(addr, val);
|
|
|
|
if (flags & RWFlags_Nonseq)
|
|
|
|
DataCycles = NDS::ARM7MemTimings[DataRegion][0];
|
2016-12-02 17:31:33 -07:00
|
|
|
else
|
2018-12-04 09:54:10 -07:00
|
|
|
DataCycles += NDS::ARM7MemTimings[DataRegion][1];
|
2017-01-30 10:36:11 -07:00
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
return true;
|
2016-12-02 17:31:33 -07:00
|
|
|
}
|
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
bool DataWrite32(u32 addr, u32 val, u32 flags)
|
2016-12-02 17:31:33 -07:00
|
|
|
{
|
|
|
|
addr &= ~3;
|
2018-12-04 09:54:10 -07:00
|
|
|
|
|
|
|
DataRegion = NDS::ARM7Write32(addr, val);
|
|
|
|
if (flags & RWFlags_Nonseq)
|
|
|
|
DataCycles = NDS::ARM7MemTimings[DataRegion][2];
|
2016-12-02 17:31:33 -07:00
|
|
|
else
|
2018-12-04 09:54:10 -07:00
|
|
|
DataCycles += NDS::ARM7MemTimings[DataRegion][3];
|
2016-12-02 17:31:33 -07:00
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
return true;
|
2016-11-02 18:38:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
void AddCycles_C()
|
|
|
|
{
|
|
|
|
// code only. this code fetch is sequential.
|
|
|
|
Cycles += NDS::ARM7MemTimings[CodeRegion][(CPSR&0x20)?1:3];
|
|
|
|
}
|
2017-01-30 10:36:11 -07:00
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
void AddCycles_CI(s32 num)
|
|
|
|
{
|
|
|
|
// code+internal. results in a nonseq code fetch.
|
|
|
|
Cycles += NDS::ARM7MemTimings[CodeRegion][(CPSR&0x20)?0:2] + num;
|
|
|
|
}
|
2016-12-05 09:08:24 -07:00
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
void AddCycles_CDI()
|
|
|
|
{
|
|
|
|
// LDR/LDM cycles.
|
|
|
|
s32 numC = NDS::ARM7MemTimings[CodeRegion][(CPSR&0x20)?0:2];
|
|
|
|
s32 numD = DataCycles;
|
2016-11-02 18:38:58 -06:00
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
if (DataRegion == NDS::Region7_MainRAM)
|
|
|
|
{
|
|
|
|
if (CodeRegion == NDS::Region7_MainRAM)
|
|
|
|
Cycles += numC + numD;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
numC++;
|
|
|
|
Cycles += std::max(numC + numD - 3, std::max(numC, numD));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (CodeRegion == NDS::Region7_MainRAM)
|
|
|
|
{
|
|
|
|
numD++;
|
|
|
|
Cycles += std::max(numC + numD - 3, std::max(numC, numD));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Cycles += numC + numD + 1;
|
|
|
|
}
|
|
|
|
}
|
2016-11-24 16:08:53 -07:00
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
void AddCycles_CD()
|
|
|
|
{
|
|
|
|
// TODO: max gain should be 5c when writing to mainRAM
|
|
|
|
s32 numC = NDS::ARM7MemTimings[CodeRegion][(CPSR&0x20)?0:2];
|
|
|
|
s32 numD = DataCycles;
|
2016-12-23 13:22:22 -07:00
|
|
|
|
2018-12-04 09:54:10 -07:00
|
|
|
if (DataRegion == NDS::Region7_MainRAM)
|
|
|
|
{
|
|
|
|
if (CodeRegion == NDS::Region7_MainRAM)
|
|
|
|
Cycles += numC + numD;
|
|
|
|
else
|
|
|
|
Cycles += std::max(numC + numD - 3, std::max(numC, numD));
|
|
|
|
}
|
|
|
|
else if (CodeRegion == NDS::Region7_MainRAM)
|
|
|
|
{
|
|
|
|
Cycles += std::max(numC + numD - 3, std::max(numC, numD));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Cycles += numC + numD;
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 18:38:58 -06:00
|
|
|
};
|
|
|
|
|
2017-06-13 03:17:22 -06:00
|
|
|
namespace ARMInterpreter
|
|
|
|
{
|
|
|
|
|
|
|
|
void A_UNK(ARM* cpu);
|
|
|
|
void T_UNK(ARM* cpu);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-11-02 18:38:58 -06:00
|
|
|
#endif // ARM_H
|