#include "dolphin/os.h"
#include "dolphin/DVDPriv.h"
#include "dolphin/db.h"
#include "dolphin/os/OSBootInfo.h"
extern OSTime __OSGetSystemTime();
extern char _db_stack_end[];
#define OS_BI2_DEBUG_ADDRESS 0x800000F4
#define DEBUGFLAG_ADDR 0x800030E8
#define OS_DEBUG_ADDRESS_2 0x800030E9
#define OS_CURRENTCONTEXT_PADDR 0x00C0
extern char* __OSResetSWInterruptHandler[];
vu16 __OSDeviceCode : (OS_BASE_CACHED | 0x30E6);
static DVDDriveInfo DriveInfo ATTRIBUTE_ALIGN(32);
static DVDCommandBlock DriveBlock;
static OSBootInfo* BootInfo;
static u32* BI2DebugFlag;
static u32* BI2DebugFlagHolder;
__declspec(weak) BOOL __OSIsGcam = FALSE;
static f64 ZeroF;
static f32 ZeroPS[2];
static BOOL AreWeInitialized = FALSE;
static __OSExceptionHandler* OSExceptionTable;
OSTime __OSStartTime;
BOOL __OSInIPL;
extern u8 __ArenaHi[];
extern u8 __ArenaLo[];
extern u32 __DVDLongFileNameFlag;
extern u32 __PADSpec;
#define OS_EXCEPTIONTABLE_ADDR 0x3000
#define OS_DBJUMPPOINT_ADDR 0x60
#define OS_CACHED_REGION_PREFIX 0x8000
#define OS_BI2_DEBUG_ADDRESS 0x800000F4
#define OS_BI2_DEBUGFLAG_OFFSET 0xC
#define PAD3_BUTTON_ADDR 0x800030E4
#define OS_DVD_DEVICECODE 0x800030E6
#define DEBUGFLAG_ADDR 0x800030E8
#define OS_DEBUG_ADDRESS_2 0x800030E9
#define DB_EXCEPTIONRET_OFFSET 0xC
#define DB_EXCEPTIONDEST_OFFSET 0x8
#define MSR_RI_BIT 0x1E
void OSDefaultExceptionHandler(__OSException exception, OSContext* context);
extern BOOL __DBIsExceptionMarked(__OSException);
static void OSExceptionInit(void);
asm void __OSFPRInit(void)
{
nofralloc
mfmsr r3
ori r3, r3, 0x2000
mtmsr r3
mfspr r3, 0x398
rlwinm. r3, r3, 3, 31, 31
beq SkipPairedSingles
lis r3, ZeroPS@ha
addi r3, r3, ZeroPS@l
psq_l fp0, 0(r3), 0, 0
ps_mr fp1, fp0
ps_mr fp2, fp0
ps_mr fp3, fp0
ps_mr fp4, fp0
ps_mr fp5, fp0
ps_mr fp6, fp0
ps_mr fp7, fp0
ps_mr fp8, fp0
ps_mr fp9, fp0
ps_mr fp10, fp0
ps_mr fp11, fp0
ps_mr fp12, fp0
ps_mr fp13, fp0
ps_mr fp14, fp0
ps_mr fp15, fp0
ps_mr fp16, fp0
ps_mr fp17, fp0
ps_mr fp18, fp0
ps_mr fp19, fp0
ps_mr fp20, fp0
ps_mr fp21, fp0
ps_mr fp22, fp0
ps_mr fp23, fp0
ps_mr fp24, fp0
ps_mr fp25, fp0
ps_mr fp26, fp0
ps_mr fp27, fp0
ps_mr fp28, fp0
ps_mr fp29, fp0
ps_mr fp30, fp0
ps_mr fp31, fp0
SkipPairedSingles:
lfd fp0, ZeroF
fmr fp1, fp0
fmr fp2, fp0
fmr fp3, fp0
fmr fp4, fp0
fmr fp5, fp0
fmr fp6, fp0
fmr fp7, fp0
fmr fp8, fp0
fmr fp9, fp0
fmr fp10, fp0
fmr fp11, fp0
fmr fp12, fp0
fmr fp13, fp0
fmr fp14, fp0
fmr fp15, fp0
fmr fp16, fp0
fmr fp17, fp0
fmr fp18, fp0
fmr fp19, fp0
fmr fp20, fp0
fmr fp21, fp0
fmr fp22, fp0
fmr fp23, fp0
fmr fp24, fp0
fmr fp25, fp0
fmr fp26, fp0
fmr fp27, fp0
fmr fp28, fp0
fmr fp29, fp0
fmr fp30, fp0
fmr fp31, fp0
mtfsf 0xFF, fp0
blr
}
u32 OSGetConsoleType() {
if (BootInfo == NULL || BootInfo->consoleType == 0) {
return OS_CONSOLE_ARTHUR;
}
return BootInfo->consoleType;
}
void* __OSSavedRegionStart;
void* __OSSavedRegionEnd;
extern u32 BOOT_REGION_START : 0x812FDFF0;
extern u32 BOOT_REGION_END : 0x812FDFEC;
void ClearArena(void) {
if ((u32)(OSGetResetCode() + 0x80000000) != 0U) {
__OSSavedRegionStart = 0U;
__OSSavedRegionEnd = 0U;
memset(OSGetArenaLo(), 0U, (u32)OSGetArenaHi() - (u32)OSGetArenaLo());
return;
}
__OSSavedRegionStart = (void*)BOOT_REGION_START;
__OSSavedRegionEnd = (void*)BOOT_REGION_END;
if (BOOT_REGION_START == 0U) {
memset(OSGetArenaLo(), 0U, (u32)OSGetArenaHi() - (u32)OSGetArenaLo());
return;
}
if ((u32)OSGetArenaLo() < (u32)__OSSavedRegionStart) {
if ((u32)OSGetArenaHi() <= (u32)__OSSavedRegionStart) {
memset((u32)OSGetArenaLo(), 0U, (u32)OSGetArenaHi() - (u32)OSGetArenaLo());
return;
}
memset(OSGetArenaLo(), 0U, (u32)__OSSavedRegionStart - (u32)OSGetArenaLo());
if ((u32)OSGetArenaHi() > (u32)__OSSavedRegionEnd) {
memset((u32)__OSSavedRegionEnd, 0, (u32)OSGetArenaHi() - (u32)__OSSavedRegionEnd);
}
}
}
static void InquiryCallback(s32 result, DVDCommandBlock* block) {
switch (block->state) {
case 0:
__OSDeviceCode = (u16)(0x8000 | DriveInfo.deviceCode);
break;
default:
__OSDeviceCode = 1;
break;
}
}
void OSInit(void) {
BI2Debug* DebugInfo;
void* debugArenaLo;
u32 inputConsoleType;
u32 tdev;
if ((BOOL)AreWeInitialized == FALSE) {
AreWeInitialized = TRUE;
__OSStartTime = __OSGetSystemTime();
OSDisableInterrupts();
PPCDisableSpeculation();
PPCSetFpNonIEEEMode();
BI2DebugFlag = 0;
BootInfo = (OSBootInfo*)OS_BASE_CACHED;
__DVDLongFileNameFlag = (u32)0;
DebugInfo = (BI2Debug*)*((u32*)OS_BI2_DEBUG_ADDRESS);
if (DebugInfo != NULL) {
BI2DebugFlag = &DebugInfo->debugFlag;
__PADSpec = (u32)DebugInfo->padSpec;
*((u8*)DEBUGFLAG_ADDR) = (u8)*BI2DebugFlag;
*((u8*)OS_DEBUG_ADDRESS_2) = (u8)__PADSpec;
} else if (BootInfo->arenaHi) {
BI2DebugFlagHolder = (u32*)*((u8*)DEBUGFLAG_ADDR);
BI2DebugFlag = (u32*)&BI2DebugFlagHolder;
__PADSpec = (u32) * ((u8*)OS_DEBUG_ADDRESS_2);
}
__DVDLongFileNameFlag = 1;
OSSetArenaLo((BootInfo->arenaLo == NULL) ? __ArenaLo : BootInfo->arenaLo);
if ((BootInfo->arenaLo == NULL) && (BI2DebugFlag != 0) && (*BI2DebugFlag < 2)) {
debugArenaLo = (char*)(((u32)_db_stack_end + 0x1f) & ~0x1f);
OSSetArenaLo(debugArenaLo);
}
OSSetArenaHi((BootInfo->arenaHi == NULL) ? __ArenaHi : BootInfo->arenaHi);
OSExceptionInit();
__OSInitSystemCall();
OSInitAlarm();
__OSModuleInit();
__OSInterruptInit();
__OSSetInterruptHandler(__OS_INTERRUPT_PI_RSW, (void*)__OSResetSWInterruptHandler);
__OSContextInit();
__OSCacheInit();
EXIInit();
SIInit();
__OSInitSram();
__OSThreadInit();
__OSInitAudioSystem();
PPCMthid2(PPCMfhid2() & 0xBFFFFFFF);
if ((BOOL)__OSInIPL == FALSE) {
__OSInitMemoryProtection();
}
OSReport("\nDolphin OS $Revision: 54 $.\n");
OSReport("Kernel built : %s %s\n", "Jun 5 2002", "02:09:12");
OSReport("Console Type : ");
if (BootInfo == NULL || (inputConsoleType = BootInfo->consoleType) == 0) {
inputConsoleType = OS_CONSOLE_ARTHUR;
} else {
inputConsoleType = BootInfo->consoleType;
}
switch (inputConsoleType & 0xffff0000) {
case OS_CONSOLE_RETAIL:
OSReport("Retail %d\n", inputConsoleType);
break;
default:
switch (inputConsoleType & 0x0000ffff) {
case OS_CONSOLE_EMULATOR:
OSReport("Mac Emulator\n");
break;
case OS_CONSOLE_PC_EMULATOR:
OSReport("PC Emulator\n");
break;
case OS_CONSOLE_ARTHUR:
OSReport("EPPC Arthur\n");
break;
case OS_CONSOLE_MINNOW:
OSReport("EPPC Minnow\n");
break;
default:
tdev = ((u32)inputConsoleType & 0x0000ffff);
OSReport("Development HW%d (%08x)\n", tdev - 3, inputConsoleType);
break;
}
break;
}
OSReport("Memory %d MB\n", (u32)BootInfo->memorySize >> 0x14U);
OSReport("Arena : 0x%x - 0x%x\n", OSGetArenaLo(), OSGetArenaHi());
if (BI2DebugFlag && ((*BI2DebugFlag) >= 2)) {
EnableMetroTRKInterrupts();
}
ClearArena();
OSEnableInterrupts();
if ((BOOL)__OSInIPL == FALSE) {
DVDInit();
if ((BOOL)__OSIsGcam) {
__OSDeviceCode = 0x9000;
return;
}
DCInvalidateRange(&DriveInfo, sizeof(DriveInfo));
DVDInquiryAsync((char*)&DriveBlock, &DriveInfo, InquiryCallback);
}
}
}
static u32 __OSExceptionLocations[] = {
0x00000100, 0x00000200, 0x00000300, 0x00000400, 0x00000500, 0x00000600, 0x00000700, 0x00000800,
0x00000900, 0x00000C00, 0x00000D00, 0x00000F00, 0x00001300, 0x00001400, 0x00001700,
};
void __OSEVStart(void);
void __OSEVEnd(void);
void __OSEVSetNumber(void);
void __OSExceptionVector(void);
void __DBVECTOR(void);
void __OSDBINTSTART(void);
void __OSDBINTEND(void);
void __OSDBJUMPSTART(void);
void __OSDBJUMPEND(void);
#define NOP 0x60000000
__OSExceptionHandler __OSSetExceptionHandler(__OSException exception, __OSExceptionHandler handler);
static void OSExceptionInit(void) {
__OSException exception;
void* destAddr;
u32* opCodeAddr;
u32 oldOpCode;
u8* handlerStart;
u32 handlerSize;
opCodeAddr = (u32*)__OSEVSetNumber;
oldOpCode = *opCodeAddr;
handlerStart = (u8*)__OSEVStart;
handlerSize = (u32)((u8*)__OSEVEnd - (u8*)__OSEVStart);
destAddr = (void*)OSPhysicalToCached(OS_DBJUMPPOINT_ADDR);
if (*(u32*)destAddr == 0)
{
DBPrintf("Installing OSDBIntegrator\n");
memcpy(destAddr, (void*)__OSDBINTSTART, (u32)__OSDBINTEND - (u32)__OSDBINTSTART);
DCFlushRangeNoSync(destAddr, (u32)__OSDBINTEND - (u32)__OSDBINTSTART);
__sync();
ICInvalidateRange(destAddr, (u32)__OSDBINTEND - (u32)__OSDBINTSTART);
}
for (exception = 0; exception < __OS_EXCEPTION_MAX; exception++) {
if (BI2DebugFlag && (*BI2DebugFlag >= 2) && __DBIsExceptionMarked(exception)) {
DBPrintf(">>> OSINIT: exception %d commandeered by TRK\n", exception);
continue;
}
*opCodeAddr = oldOpCode | exception;
if (__DBIsExceptionMarked(exception)) {
DBPrintf(">>> OSINIT: exception %d vectored to debugger\n", exception);
memcpy((void*)__DBVECTOR, (void*)__OSDBJUMPSTART, (u32)__OSDBJUMPEND - (u32)__OSDBJUMPSTART);
} else {
u32* ops = (u32*)__DBVECTOR;
int cb;
for (cb = 0; cb < (u32)__OSDBJUMPEND - (u32)__OSDBJUMPSTART; cb += sizeof(u32)) {
*ops++ = NOP;
}
}
destAddr = (void*)OSPhysicalToCached(__OSExceptionLocations[(u32)exception]);
memcpy(destAddr, handlerStart, handlerSize);
DCFlushRangeNoSync(destAddr, handlerSize);
__sync();
ICInvalidateRange(destAddr, handlerSize);
}
OSExceptionTable = OSPhysicalToCached(OS_EXCEPTIONTABLE_ADDR);
for (exception = 0; exception < __OS_EXCEPTION_MAX; exception++) {
__OSSetExceptionHandler(exception, OSDefaultExceptionHandler);
}
*opCodeAddr = oldOpCode;
DBPrintf("Exceptions initialized...\n");
}
static asm void __OSDBIntegrator(void) {
nofralloc
entry __OSDBINTSTART
li r5, OS_DBINTERFACE_ADDR
mflr r3
stw r3, DB_EXCEPTIONRET_OFFSET(r5)
lwz r3, DB_EXCEPTIONDEST_OFFSET(r5)
oris r3, r3, OS_CACHED_REGION_PREFIX
mtlr r3
li r3, 0x30
mtmsr r3
blr
entry __OSDBINTEND
}
static asm void __OSDBJump(void){
nofralloc
entry __OSDBJUMPSTART
bla OS_DBJUMPPOINT_ADDR
entry __OSDBJUMPEND
}
__OSExceptionHandler
__OSSetExceptionHandler(__OSException exception, __OSExceptionHandler handler) {
__OSExceptionHandler oldHandler;
oldHandler = OSExceptionTable[exception];
OSExceptionTable[exception] = handler;
return oldHandler;
}
__OSExceptionHandler __OSGetExceptionHandler(__OSException exception) {
return OSExceptionTable[exception];
}
static asm void OSExceptionVector(void) {
nofralloc
entry __OSEVStart
mtsprg 0, r4
lwz r4, OS_CURRENTCONTEXT_PADDR
stw r3, OS_CONTEXT_R3(r4)
mfsprg r3, 0
stw r3, OS_CONTEXT_R4(r4)
stw r5, OS_CONTEXT_R5(r4)
lhz r3, OS_CONTEXT_STATE(r4)
ori r3, r3, OS_CONTEXT_STATE_EXC
sth r3, OS_CONTEXT_STATE(r4)
mfcr r3
stw r3, OS_CONTEXT_CR(r4)
mflr r3
stw r3, OS_CONTEXT_LR(r4)
mfctr r3
stw r3, OS_CONTEXT_CTR(r4)
mfxer r3
stw r3, OS_CONTEXT_XER(r4)
mfsrr0 r3
stw r3, OS_CONTEXT_SRR0(r4)
mfsrr1 r3
stw r3, OS_CONTEXT_SRR1(r4)
mr r5, r3
entry __DBVECTOR
nop
mfmsr r3
ori r3, r3, 0x30
mtsrr1 r3
entry __OSEVSetNumber
addi r3, 0, 0x0000
lwz r4, 0xD4
rlwinm. r5, r5, 0, MSR_RI_BIT, MSR_RI_BIT
bne recoverable
addis r5, 0, OSDefaultExceptionHandler@ha
addi r5, r5, OSDefaultExceptionHandler@l
mtsrr0 r5
rfi
recoverable:
rlwinm r5, r3, 2, 22, 29
lwz r5, OS_EXCEPTIONTABLE_ADDR(r5)
mtsrr0 r5
rfi
entry __OSEVEnd
nop
}
void __OSUnhandledException(__OSException exception, OSContext* context, u32 dsisr, u32 dar);
asm void OSDefaultExceptionHandler(register __OSException exception, register OSContext* context) {
nofralloc
OS_EXCEPTION_SAVE_GPRS(context)
mfdsisr r5
mfdar r6
stwu r1,-8(r1)
b __OSUnhandledException
}
void __OSPSInit(void)
{
PPCMthid2(PPCMfhid2() | 0xA0000000);
ICFlashInvalidate();
__sync();
asm
{
li r3, 0
mtspr GQR0, r3
}
}
vu32 __DIRegs[16] : 0xCC006000;
#define DI_CONFIG_IDX 0x9
#define DI_CONFIG_CONFIG_MASK 0xFF
u32 __OSGetDIConfig(void) { return (__DIRegs[DI_CONFIG_IDX] & DI_CONFIG_CONFIG_MASK); }
void OSRegisterVersion(const char* id) { OSReport("%s\n", id); }