2016-12-05 17:02:29 +00:00
|
|
|
/*
|
2018-09-15 00:32:13 +00:00
|
|
|
Copyright 2016-2019 StapleButter
|
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-12-03 15:13:04 +00:00
|
|
|
#include <stdio.h>
|
2017-01-30 18:11:29 +00:00
|
|
|
#include <string.h>
|
2016-12-03 15:13:04 +00:00
|
|
|
#include "NDS.h"
|
2016-12-05 22:17:03 +00:00
|
|
|
#include "ARM.h"
|
2016-12-03 15:13:04 +00:00
|
|
|
|
2016-12-05 17:02:29 +00:00
|
|
|
|
2016-12-03 15:13:04 +00:00
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
void ARMv5::CP15Reset()
|
2016-12-03 15:13:04 +00:00
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
CP15Control = 0x78; // dunno
|
2016-12-03 15:13:04 +00:00
|
|
|
|
|
|
|
DTCMSetting = 0;
|
|
|
|
ITCMSetting = 0;
|
2017-01-30 18:11:29 +00:00
|
|
|
|
|
|
|
memset(ITCM, 0, 0x8000);
|
|
|
|
memset(DTCM, 0, 0x4000);
|
|
|
|
|
|
|
|
ITCMSize = 0;
|
|
|
|
DTCMBase = 0xFFFFFFFF;
|
|
|
|
DTCMSize = 0;
|
2016-12-03 15:13:04 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
void ARMv5::CP15DoSavestate(Savestate* file)
|
2018-09-15 00:47:34 +00:00
|
|
|
{
|
|
|
|
file->Section("CP15");
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
file->Var32(&CP15Control);
|
2018-09-15 00:47:34 +00:00
|
|
|
|
|
|
|
file->Var32(&DTCMSetting);
|
|
|
|
file->Var32(&ITCMSetting);
|
|
|
|
|
|
|
|
if (!file->Saving)
|
|
|
|
{
|
|
|
|
UpdateDTCMSetting();
|
|
|
|
UpdateITCMSetting();
|
|
|
|
}
|
|
|
|
|
|
|
|
file->VarArray(ITCM, 0x8000);
|
|
|
|
file->VarArray(DTCM, 0x4000);
|
|
|
|
}
|
|
|
|
|
2016-12-03 15:13:04 +00:00
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
void ARMv5::UpdateDTCMSetting()
|
2016-12-03 15:13:04 +00:00
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
if (CP15Control & (1<<16))
|
2016-12-03 15:13:04 +00:00
|
|
|
{
|
2017-01-30 18:11:29 +00:00
|
|
|
DTCMBase = DTCMSetting & 0xFFFFF000;
|
|
|
|
DTCMSize = 0x200 << ((DTCMSetting >> 1) & 0x1F);
|
2017-05-27 21:47:20 +00:00
|
|
|
//printf("DTCM [%08X] enabled at %08X, size %X\n", DTCMSetting, DTCMBase, DTCMSize);
|
2016-12-03 15:13:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-30 18:11:29 +00:00
|
|
|
DTCMBase = 0xFFFFFFFF;
|
|
|
|
DTCMSize = 0;
|
2017-05-27 21:47:20 +00:00
|
|
|
//printf("DTCM disabled\n");
|
2016-12-03 15:13:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
void ARMv5::UpdateITCMSetting()
|
2016-12-03 15:13:04 +00:00
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
if (CP15Control & (1<<18))
|
2016-12-03 15:13:04 +00:00
|
|
|
{
|
2017-01-30 18:11:29 +00:00
|
|
|
ITCMSize = 0x200 << ((ITCMSetting >> 1) & 0x1F);
|
2017-05-27 21:47:20 +00:00
|
|
|
//printf("ITCM [%08X] enabled at %08X, size %X\n", ITCMSetting, 0, ITCMSize);
|
2016-12-03 15:13:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-30 18:11:29 +00:00
|
|
|
ITCMSize = 0;
|
2017-05-27 21:47:20 +00:00
|
|
|
//printf("ITCM disabled\n");
|
2016-12-03 15:13:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
void ARMv5::CP15Write(u32 id, u32 val)
|
2016-12-03 15:13:04 +00:00
|
|
|
{
|
2017-01-30 17:36:11 +00:00
|
|
|
//printf("CP15 write op %03X %08X %08X\n", id, val, NDS::ARM9->R[15]);
|
|
|
|
|
2016-12-03 15:13:04 +00:00
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case 0x100:
|
|
|
|
val &= 0x000FF085;
|
2018-12-04 16:54:10 +00:00
|
|
|
CP15Control &= ~0x000FF085;
|
|
|
|
CP15Control |= val;
|
2016-12-03 15:13:04 +00:00
|
|
|
UpdateDTCMSetting();
|
|
|
|
UpdateITCMSetting();
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
2016-12-05 22:17:03 +00:00
|
|
|
case 0x704:
|
2016-12-23 20:22:22 +00:00
|
|
|
case 0x782:
|
2018-12-04 16:54:10 +00:00
|
|
|
Halt(1);
|
2016-12-05 22:17:03 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
2017-01-16 03:47:37 +00:00
|
|
|
case 0x761:
|
|
|
|
//printf("inval data cache %08X\n", val);
|
|
|
|
return;
|
|
|
|
case 0x762:
|
2017-01-31 23:24:36 +00:00
|
|
|
//printf("inval data cache SI\n");
|
2017-01-16 03:47:37 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case 0x7A1:
|
2017-01-17 01:29:25 +00:00
|
|
|
//printf("flush data cache %08X\n", val);
|
2017-01-16 03:47:37 +00:00
|
|
|
return;
|
|
|
|
case 0x7A2:
|
2017-01-31 23:24:36 +00:00
|
|
|
//printf("flush data cache SI\n");
|
2017-01-16 03:47:37 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
2016-12-03 15:13:04 +00:00
|
|
|
case 0x910:
|
|
|
|
DTCMSetting = val;
|
|
|
|
UpdateDTCMSetting();
|
|
|
|
return;
|
|
|
|
case 0x911:
|
|
|
|
ITCMSetting = val;
|
|
|
|
UpdateITCMSetting();
|
|
|
|
return;
|
|
|
|
}
|
2017-01-16 03:47:37 +00:00
|
|
|
|
2017-02-03 15:57:31 +00:00
|
|
|
if ((id&0xF00)!=0x700)
|
|
|
|
printf("unknown CP15 write op %03X %08X\n", id, val);
|
2016-12-03 15:13:04 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
u32 ARMv5::CP15Read(u32 id)
|
2016-12-03 15:13:04 +00:00
|
|
|
{
|
2017-01-31 17:41:31 +00:00
|
|
|
//printf("CP15 read op %03X %08X\n", id, NDS::ARM9->R[15]);
|
|
|
|
|
2016-12-03 15:13:04 +00:00
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case 0x000: // CPU ID
|
|
|
|
case 0x003:
|
|
|
|
case 0x004:
|
|
|
|
case 0x005:
|
|
|
|
case 0x006:
|
|
|
|
case 0x007:
|
|
|
|
return 0x41059461;
|
|
|
|
|
2017-01-31 17:41:31 +00:00
|
|
|
case 0x001: // cache type
|
|
|
|
return 0x0F0D2112;
|
2016-12-03 15:13:04 +00:00
|
|
|
|
|
|
|
case 0x002: // TCM size
|
|
|
|
return (6 << 6) | (5 << 18);
|
|
|
|
|
|
|
|
|
|
|
|
case 0x100: // control reg
|
2018-12-04 16:54:10 +00:00
|
|
|
return CP15Control;
|
2016-12-03 15:13:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
case 0x910:
|
|
|
|
return DTCMSetting;
|
|
|
|
case 0x911:
|
|
|
|
return ITCMSetting;
|
|
|
|
}
|
|
|
|
|
2017-01-16 03:47:37 +00:00
|
|
|
printf("unknown CP15 read op %03X\n", id);
|
2016-12-03 15:13:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-30 18:11:29 +00:00
|
|
|
|
|
|
|
// TCM are handled here.
|
|
|
|
// TODO: later on, handle PU, and maybe caches
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
u32 ARMv5::CodeRead32(u32 addr)
|
2017-01-30 18:11:29 +00:00
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
// PU/cache check here
|
2017-01-30 18:11:29 +00:00
|
|
|
|
|
|
|
if (addr < ITCMSize)
|
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
CodeRegion = NDS::Region9_ITCM;
|
|
|
|
return *(u32*)&ITCM[addr & 0x7FFF];
|
2017-01-30 18:11:29 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
u32 ret;
|
|
|
|
CodeRegion = NDS::ARM9Read32(addr, &ret);
|
|
|
|
return ret;
|
2017-01-30 18:11:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
bool ARMv5::DataRead8(u32 addr, u32* val, u32 flags)
|
2017-01-30 18:11:29 +00:00
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
// PU/cache check here
|
|
|
|
|
2017-01-30 18:11:29 +00:00
|
|
|
if (addr < ITCMSize)
|
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
DataRegion = NDS::Region9_ITCM;
|
|
|
|
DataCycles += 1;
|
2017-01-30 18:11:29 +00:00
|
|
|
*val = *(u8*)&ITCM[addr & 0x7FFF];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (addr >= DTCMBase && addr < (DTCMBase + DTCMSize))
|
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
DataRegion = NDS::Region9_ITCM;
|
|
|
|
DataCycles += 1;
|
2017-01-30 18:11:29 +00:00
|
|
|
*val = *(u8*)&DTCM[(addr - DTCMBase) & 0x3FFF];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
DataRegion = NDS::ARM9Read8(addr, val);
|
|
|
|
if (flags & RWFlags_Nonseq)
|
|
|
|
DataCycles = NDS::ARM9MemTimings[DataRegion][0];
|
|
|
|
else
|
|
|
|
DataCycles += NDS::ARM9MemTimings[DataRegion][1];
|
|
|
|
return true;
|
2017-01-30 18:11:29 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
bool ARMv5::DataRead16(u32 addr, u32* val, u32 flags)
|
2017-01-30 18:11:29 +00:00
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
addr &= ~1;
|
|
|
|
|
|
|
|
// PU/cache check here
|
|
|
|
|
2017-01-30 18:11:29 +00:00
|
|
|
if (addr < ITCMSize)
|
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
DataRegion = NDS::Region9_ITCM;
|
|
|
|
DataCycles += 1;
|
2017-01-30 18:11:29 +00:00
|
|
|
*val = *(u16*)&ITCM[addr & 0x7FFF];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (addr >= DTCMBase && addr < (DTCMBase + DTCMSize))
|
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
DataRegion = NDS::Region9_ITCM;
|
|
|
|
DataCycles += 1;
|
2017-01-30 18:11:29 +00:00
|
|
|
*val = *(u16*)&DTCM[(addr - DTCMBase) & 0x3FFF];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
DataRegion = NDS::ARM9Read16(addr, val);
|
|
|
|
if (flags & RWFlags_Nonseq)
|
|
|
|
DataCycles = NDS::ARM9MemTimings[DataRegion][0];
|
|
|
|
else
|
|
|
|
DataCycles += NDS::ARM9MemTimings[DataRegion][1];
|
|
|
|
return true;
|
2017-01-30 18:11:29 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
bool ARMv5::DataRead32(u32 addr, u32* val, u32 flags)
|
2017-01-30 18:11:29 +00:00
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
addr &= ~3;
|
|
|
|
|
|
|
|
// PU/cache check here
|
|
|
|
|
2017-01-30 18:11:29 +00:00
|
|
|
if (addr < ITCMSize)
|
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
DataRegion = NDS::Region9_ITCM;
|
|
|
|
DataCycles += 1;
|
2017-01-30 18:11:29 +00:00
|
|
|
*val = *(u32*)&ITCM[addr & 0x7FFF];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (addr >= DTCMBase && addr < (DTCMBase + DTCMSize))
|
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
DataRegion = NDS::Region9_ITCM;
|
|
|
|
DataCycles += 1;
|
2017-01-30 18:11:29 +00:00
|
|
|
*val = *(u32*)&DTCM[(addr - DTCMBase) & 0x3FFF];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
DataRegion = NDS::ARM9Read32(addr, val);
|
|
|
|
if (flags & RWFlags_Nonseq)
|
|
|
|
DataCycles = NDS::ARM9MemTimings[DataRegion][2];
|
|
|
|
else
|
|
|
|
DataCycles += NDS::ARM9MemTimings[DataRegion][3];
|
|
|
|
return true;
|
2017-01-30 18:11:29 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
bool ARMv5::DataWrite8(u32 addr, u8 val, u32 flags)
|
2017-01-30 18:11:29 +00:00
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
// PU/cache check here
|
|
|
|
|
2017-01-30 18:11:29 +00:00
|
|
|
if (addr < ITCMSize)
|
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
DataRegion = NDS::Region9_ITCM;
|
|
|
|
DataCycles += 1;
|
2017-01-30 18:11:29 +00:00
|
|
|
*(u8*)&ITCM[addr & 0x7FFF] = val;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (addr >= DTCMBase && addr < (DTCMBase + DTCMSize))
|
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
DataRegion = NDS::Region9_ITCM;
|
|
|
|
DataCycles += 1;
|
2017-01-30 18:11:29 +00:00
|
|
|
*(u8*)&DTCM[(addr - DTCMBase) & 0x3FFF] = val;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
DataRegion = NDS::ARM9Write8(addr, val);
|
|
|
|
if (flags & RWFlags_Nonseq)
|
|
|
|
DataCycles = NDS::ARM9MemTimings[DataRegion][0];
|
|
|
|
else
|
|
|
|
DataCycles += NDS::ARM9MemTimings[DataRegion][1];
|
|
|
|
return true;
|
2017-01-30 18:11:29 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
bool ARMv5::DataWrite16(u32 addr, u16 val, u32 flags)
|
2017-01-30 18:11:29 +00:00
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
addr &= ~1;
|
|
|
|
|
|
|
|
// PU/cache check here
|
|
|
|
|
2017-01-30 18:11:29 +00:00
|
|
|
if (addr < ITCMSize)
|
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
DataRegion = NDS::Region9_ITCM;
|
|
|
|
DataCycles += 1;
|
2017-01-30 18:11:29 +00:00
|
|
|
*(u16*)&ITCM[addr & 0x7FFF] = val;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (addr >= DTCMBase && addr < (DTCMBase + DTCMSize))
|
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
DataRegion = NDS::Region9_ITCM;
|
|
|
|
DataCycles += 1;
|
2017-01-30 18:11:29 +00:00
|
|
|
*(u16*)&DTCM[(addr - DTCMBase) & 0x3FFF] = val;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
DataRegion = NDS::ARM9Write16(addr, val);
|
|
|
|
if (flags & RWFlags_Nonseq)
|
|
|
|
DataCycles = NDS::ARM9MemTimings[DataRegion][0];
|
|
|
|
else
|
|
|
|
DataCycles += NDS::ARM9MemTimings[DataRegion][1];
|
|
|
|
return true;
|
2017-01-30 18:11:29 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
bool ARMv5::DataWrite32(u32 addr, u32 val, u32 flags)
|
2017-01-30 18:11:29 +00:00
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
addr &= ~3;
|
|
|
|
|
|
|
|
// PU/cache check here
|
|
|
|
|
2017-01-30 18:11:29 +00:00
|
|
|
if (addr < ITCMSize)
|
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
DataRegion = NDS::Region9_ITCM;
|
|
|
|
DataCycles += 1;
|
2017-01-30 18:11:29 +00:00
|
|
|
*(u32*)&ITCM[addr & 0x7FFF] = val;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (addr >= DTCMBase && addr < (DTCMBase + DTCMSize))
|
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
DataRegion = NDS::Region9_ITCM;
|
|
|
|
DataCycles += 1;
|
2017-01-30 18:11:29 +00:00
|
|
|
*(u32*)&DTCM[(addr - DTCMBase) & 0x3FFF] = val;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
DataRegion = NDS::ARM9Write32(addr, val);
|
|
|
|
if (flags & RWFlags_Nonseq)
|
|
|
|
DataCycles = NDS::ARM9MemTimings[DataRegion][2];
|
|
|
|
else
|
|
|
|
DataCycles += NDS::ARM9MemTimings[DataRegion][3];
|
|
|
|
return true;
|
2017-01-30 18:11:29 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
void ARMv5::GetCodeMemRegion(u32 addr, NDS::MemRegion* region)
|
2018-11-04 22:21:58 +00:00
|
|
|
{
|
|
|
|
if (addr < ITCMSize)
|
|
|
|
{
|
2018-12-04 16:54:10 +00:00
|
|
|
region->Region = NDS::Region9_ITCM;
|
2018-11-04 22:21:58 +00:00
|
|
|
region->Mem = ITCM;
|
|
|
|
region->Mask = 0x7FFF;
|
2018-12-04 16:54:10 +00:00
|
|
|
return;
|
2018-11-04 22:21:58 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 16:54:10 +00:00
|
|
|
NDS::ARM9GetMemRegion(addr, false, &CodeMem);
|
2018-11-04 22:21:58 +00:00
|
|
|
}
|
|
|
|
|