* support for cart secure area

* fix bug in THUMB LDMIA
This commit is contained in:
StapleButter 2017-01-23 02:26:05 +01:00
parent fe3a189406
commit 296212ac49
6 changed files with 55 additions and 19 deletions

View File

@ -74,14 +74,18 @@ void ARM::JumpTo(u32 addr, bool restorecpsr)
{
if (restorecpsr)
{
//if (Num==1 && (CPSR&0x1F)==0x12)
// printf("return from IRQ %08X -> %08X, SP=%08X, %08X\n", R[15], addr, R[13], Read32(0x0380FF7C));
RestoreCPSR();
if (CPSR & 0x20) addr |= 0x1;
else addr &= ~0x1;
}
if (addr == 0x02000800)
{
printf("!!!!!!!! %08X\n", R[15]);
printf("%08X %08X %08X %08X\n", Read32(0x02000000), Read32(0x0200000C), Read32(0x02000800), Read32(0x02000804));
}
if (addr & 0x1)
{
addr &= ~0x1;

View File

@ -743,6 +743,7 @@ s32 T_STMIA(ARM* cpu)
}
}
// TODO: check "Rb included in Rlist" case
cpu->R[(cpu->CurInstr >> 8) & 0x7] = base;
return cycles - C_S(1);
@ -764,7 +765,8 @@ s32 T_LDMIA(ARM* cpu)
}
}
cpu->R[(cpu->CurInstr >> 8) & 0x7] = base;
if (!(cpu->CurInstr & (1<<((cpu->CurInstr >> 8) & 0x7))))
cpu->R[(cpu->CurInstr >> 8) & 0x7] = base;
return cycles;
}

View File

@ -112,7 +112,6 @@ void DMA::Start()
if (CurSrcAddr==0x04100010 && RemCount==1 && (Cnt & 0x07E00000)==0x07000000 &&
((CPU==0 && StartMode==0x06) || (CPU==1 && StartMode==0x12)))
{
printf("CART DMA %08X\n", CurDstAddr);
NDSCart::DMA(CurDstAddr);
Cnt &= ~0x80000000;
if (Cnt & 0x40000000)

View File

@ -1146,8 +1146,6 @@ u32 ARM7Read32(u32 addr)
void ARM7Write8(u32 addr, u8 val)
{
if (addr==0x3807764) printf("DERP! %02X %08X\n", val, ARM7->R[15]);
if (addr==0x27FFCE4) printf("FIRMWARE STATUS8 %04X %08X\n", val, ARM7->R[15]);
switch (addr & 0xFF800000)
{
case 0x02000000:
@ -1184,9 +1182,6 @@ void ARM7Write8(u32 addr, u8 val)
void ARM7Write16(u32 addr, u16 val)
{
if (addr == ARM7->R[15]) printf("!!!!!!!!!!!!7777 %08X %04X\n", addr, val);
if (addr==0x3807764) printf("DERP! %04X %08X\n", val, ARM7->R[15]);
if (addr==0x27FF816) printf("RTC STATUS %04X %08X\n", val, ARM7->R[15]);
if (addr==0x27FFCE4) printf("FIRMWARE STATUS %04X %08X\n", val, ARM7->R[15]);
switch (addr & 0xFF800000)
{
case 0x02000000:
@ -1227,7 +1222,6 @@ void ARM7Write16(u32 addr, u16 val)
void ARM7Write32(u32 addr, u32 val)
{
if (addr == ARM7->R[15]) printf("!!!!!!!!!!!!7777 %08X %08X\n", addr, val);
if (addr==0x27FFCE4) printf("FIRMWARE STATUS32 %08X %08X\n", val, ARM7->R[15]);
switch (addr & 0xFF800000)
{
case 0x02000000:
@ -2022,7 +2016,7 @@ void ARM7IOWrite32(u32 addr, u32 val)
case 0x040000D0: DMAs[6]->WriteCnt(val); return;
case 0x040000D4: DMAs[7]->SrcAddr = val; return;
case 0x040000D8: DMAs[7]->DstAddr = val; return;
case 0x040000DC: DMAs[7]->WriteCnt(val); return;
case 0x040000DC: DMAs[7]->WriteCnt(val); printf("start dma3 %08X %08X %08X\n", val, ARM7->R[15], ARM7Read32(ARM7->R[13]+24));return;
case 0x04000100:
Timers[4].Reload = val & 0xFFFF;

View File

@ -38,6 +38,7 @@ bool CartInserted;
u8* CartROM;
u32 CartROMSize;
u32 CartID;
bool CartIsHomebrew;
u32 CmdEncMode;
u32 DataEncMode;
@ -174,6 +175,7 @@ void Reset()
CartROM = NULL;
CartROMSize = 0;
CartID = 0;
CartIsHomebrew = false;
CmdEncMode = 0;
DataEncMode = 0;
@ -213,6 +215,30 @@ void LoadROM(char* path)
// it just has to stay the same throughout gameplay
CartID = 0x00001FC2;
u32 arm9base = *(u32*)&CartROM[0x20];
if (arm9base < 0x8000)
{
if (arm9base >= 0x4000)
{
// reencrypt secure area if needed
if (*(u32*)&CartROM[arm9base] == 0xE7FFDEFF)
{
printf("Re-encrypting cart secure area\n");
strncpy((char*)&CartROM[arm9base], "encryObj", 8);
Key1_InitKeycode(gamecode, 3, 2);
for (u32 i = 0; i < 0x800; i += 8)
Key1_Encrypt((u32*)&CartROM[arm9base + i]);
Key1_InitKeycode(gamecode, 2, 2);
Key1_Encrypt((u32*)&CartROM[arm9base]);
}
}
else
CartIsHomebrew = true;
}
// encryption
Key1_InitKeycode(gamecode, 2, 2);
}
@ -231,7 +257,11 @@ void ReadROM(u32 addr, u32 len, u32 offset)
void ReadROM_B7(u32 addr, u32 len, u32 offset)
{
addr &= (CartROMSize-1);
if (addr < 0x8000) addr = 0x8000 + (addr & 0x1FF);
if (!CartIsHomebrew)
{
if (addr < 0x8000)
addr = 0x8000 + (addr & 0x1FF);
}
memcpy(DataOut+offset, CartROM+addr, len);
}
@ -318,11 +348,11 @@ void WriteCnt(u32 val)
*(u32*)&cmd[4] = *(u32*)&ROMCommand[4];
}
printf("ROM COMMAND %04X %08X %02X%02X%02X%02X%02X%02X%02X%02X SIZE %04X\n",
/*printf("ROM COMMAND %04X %08X %02X%02X%02X%02X%02X%02X%02X%02X SIZE %04X\n",
SPICnt, ROMCnt,
cmd[0], cmd[1], cmd[2], cmd[3],
cmd[4], cmd[5], cmd[6], cmd[7],
datasize);
datasize);*/
switch (cmd[0])
{
@ -380,6 +410,13 @@ void WriteCnt(u32 val)
*(u32*)&DataOut[pos] = CartID;
break;
case 0x20:
{
u32 addr = (cmd[2] & 0xF0) << 8;
ReadROM(addr, 0x1000, 0);
}
break;
case 0xA0:
CmdEncMode = 2;
break;

View File

@ -10,7 +10,7 @@
1481161027 c:\documents\sources\melonds\types.h
1485111787 source:c:\documents\sources\melonds\nds.cpp
1485133568 source:c:\documents\sources\melonds\nds.cpp
<stdio.h>
<string.h>
"NDS.h"
@ -24,7 +24,7 @@
"RTC.h"
"Wifi.h"
1485106814 source:c:\documents\sources\melonds\arm.cpp
1485133372 source:c:\documents\sources\melonds\arm.cpp
<stdio.h>
"NDS.h"
"ARM.h"
@ -64,7 +64,7 @@
1480957165 c:\documents\sources\melonds\arminterpreter_loadstore.h
1481203284 source:c:\documents\sources\melonds\arminterpreter_loadstore.cpp
1485133521 source:c:\documents\sources\melonds\arminterpreter_loadstore.cpp
<stdio.h>
"ARM.h"
@ -106,7 +106,7 @@
1484612398 c:\documents\sources\melonds\fifo.h
"types.h"
1485113211 source:c:\documents\sources\melonds\dma.cpp
1485133888 source:c:\documents\sources\melonds\dma.cpp
<stdio.h>
"NDS.h"
"DMA.h"
@ -135,7 +135,7 @@
1485112531 c:\documents\sources\melonds\ndscart.h
"types.h"
1485112522 source:c:\documents\sources\melonds\ndscart.cpp
1485134328 source:c:\documents\sources\melonds\ndscart.cpp
<stdio.h>
<string.h>
"NDS.h"