From fa98647959d6ef4ade5abb864cafc8cd4f358a1a Mon Sep 17 00:00:00 2001 From: meepingsnesroms Date: Fri, 1 Mar 2019 21:04:01 -0800 Subject: [PATCH] Fix invalid writes to storage area Also starting to render hires stuff, but crashes on BltChars --- src/debug/sandbox.c | 2 + src/emulator.c | 3 + src/hardwareRegisters.c | 6 +- src/hardwareRegistersAccessors.c.h | 2 + tools/palm/muExpansionDriver/hires.c | 42 +++++++-- tools/palm/muExpansionDriver/muExpDriver.c | 20 +++-- tools/palm/muExpansionDriver/palmOsPriv.h | 11 +++ tools/palm/muExpansionDriver/traps.c | 89 ++++++++++--------- tools/palm/muExpansionDriver/traps.h | 2 +- .../unimplementedFeatures.txt | 1 + 10 files changed, 120 insertions(+), 58 deletions(-) create mode 100644 tools/palm/muExpansionDriver/palmOsPriv.h diff --git a/src/debug/sandbox.c b/src/debug/sandbox.c index cb1e0aa..47686cc 100644 --- a/src/debug/sandbox.c +++ b/src/debug/sandbox.c @@ -415,6 +415,8 @@ uint32_t sandboxCommand(uint32_t command, void* data){ //HwrCalcDynamicRAMSize_10083B0A: //patchOsRom(0x5CC6, "203C000800004E75");//move.l 0x80000, d0; rts //patchOsRom(0x83B0A, "203C000800004E75");//move.l 0x80000, d0; rts + patchOsRom(0x5CC6, "203C001000004E75");//move.l 0x100000, d0; rts + patchOsRom(0x83B0A, "203C001000004E75");//move.l 0x100000, d0; rts //patch PrvChunkNew to only allocate in 4 byte intervals //PrvChunkNew_10020CBC: diff --git a/src/emulator.c b/src/emulator.c index cfe5da9..81dafef 100644 --- a/src/emulator.c +++ b/src/emulator.c @@ -109,6 +109,9 @@ uint32_t emulatorInit(buffer_t palmRomDump, buffer_t palmBootDump, uint32_t enab emulatorSoftReset(); setRtc(0, 0, 0, 0);//RTCTIME and DAYR are not cleared by reset, clear them manually in case the frontend doesnt set the RTC + //debug patches + sandboxCommand(SANDBOX_PATCH_OS, NULL); + emulatorInitialized = true; return EMU_ERROR_NONE; diff --git a/src/hardwareRegisters.c b/src/hardwareRegisters.c index 333fd06..cf0b266 100644 --- a/src/hardwareRegisters.c +++ b/src/hardwareRegisters.c @@ -117,7 +117,7 @@ int32_t interruptAcknowledge(int32_t intLevel){ void setBusErrorTimeOut(uint32_t address, bool isWrite){ uint8_t scr = registerArrayRead8(SCR); - debugLog("Bus error timeout, PC:0x%08X\n", flx68000GetPc()); + debugLog("Bus error timeout at:0x%08X, PC:0x%08X\n", address, flx68000GetPc()); registerArrayWrite8(SCR, scr | 0x80); if(scr & 0x10) flx68000BusError(address, isWrite); @@ -125,7 +125,7 @@ void setBusErrorTimeOut(uint32_t address, bool isWrite){ void setPrivilegeViolation(uint32_t address, bool isWrite){ uint8_t scr = registerArrayRead8(SCR); - debugLog("Privilege violation, PC:0x%08X\n", flx68000GetPc()); + debugLog("Privilege violation at:0x%08X, PC:0x%08X\n", address, flx68000GetPc()); registerArrayWrite8(SCR, scr | 0x20); if(scr & 0x10) flx68000BusError(address, isWrite); @@ -133,7 +133,7 @@ void setPrivilegeViolation(uint32_t address, bool isWrite){ void setWriteProtectViolation(uint32_t address){ uint8_t scr = registerArrayRead8(SCR); - debugLog("Write protect violation, PC:0x%08X\n", flx68000GetPc()); + debugLog("Write protect violation at:0x%08X, PC:0x%08X\n", address, flx68000GetPc()); registerArrayWrite8(SCR, scr | 0x40); if(scr & 0x10) flx68000BusError(address, true); diff --git a/src/hardwareRegistersAccessors.c.h b/src/hardwareRegistersAccessors.c.h index 76f5bb3..ab59828 100644 --- a/src/hardwareRegistersAccessors.c.h +++ b/src/hardwareRegistersAccessors.c.h @@ -207,6 +207,8 @@ static void setCsd(uint16_t value){ else chips[CHIP_DX_RAM].unprotectedSize = chips[CHIP_DX_RAM].lineSize / (1 << 7 - (value >> 11 & 0x0003)); + //debugLog("RAM unprotected size:0x%08X, bits:0x%02X\n", chips[CHIP_DX_RAM].unprotectedSize, ((value >> 11 & 0x0003) | (csControl1 & 0x4000 && csControl1 & 0x0010) * 0x0004)); + registerArrayWrite16(CSD, value); } diff --git a/tools/palm/muExpansionDriver/hires.c b/tools/palm/muExpansionDriver/hires.c index 35ea1b1..42c18aa 100644 --- a/tools/palm/muExpansionDriver/hires.c +++ b/tools/palm/muExpansionDriver/hires.c @@ -4,31 +4,61 @@ #include "debug.h" #include "globals.h" #include "traps.h" +#include "palmOsPriv.h" #include "palmGlobalDefines.h" #include "specs/emuFeatureRegisterSpec.h" +typedef struct{ + BitmapTypeV3 bitmap; + uint16_t* data; +}BitmapTypeV3Indirect; + + static Boolean setTungstenWDriverFramebuffer(uint16_t width, uint16_t height){ if(getGlobalVar(TUNGSTEN_W_DRIVERS_INSTALLED)){ - BitmapTypeV3* newBitmap; + BitmapTypeV3Indirect* newBitmap; + uint16_t* newBitmapData; WindowType* driverWindow; Err error; driverWindow = WinGetWindowPointer(WinGetDisplayWindow()); - newBitmap = BmpCreateBitmapV3(BmpCreate(width, height, 16, NULL, &error), kDensityDouble, NULL/*bitsP*/, NULL/*colorTableP*/); - if(error != errNone) + newBitmap = MemChunkNew(0, sizeof(BitmapTypeV3Indirect), memNewChunkFlagNonMovable | memNewChunkFlagAllowLarge); + if(!newBitmap){ + debugLog("Couldnt create new framebuffer bitmap struct!\n"); return false; + } + + MemSet(newBitmap, sizeof(BitmapTypeV3Indirect), 0x00); + newBitmap->bitmap.width = width; + newBitmap->bitmap.height = height; + newBitmap->bitmap.rowBytes = width * sizeof(uint16_t); + newBitmap->bitmap.flags.indirect = true; + newBitmap->bitmap.flags.forScreen = true; + newBitmap->bitmap.flags.directColor = true; + newBitmap->bitmap.size = sizeof(BitmapTypeV3); + newBitmap->bitmap.pixelFormat = pixelFormat565; + newBitmap->bitmap.compressionType = BitmapCompressionTypeNone; + newBitmap->bitmap.density = kDensityDouble; + + newBitmap->data = MemChunkNew(0, width * height * sizeof(uint16_t), memNewChunkFlagNonMovable | memNewChunkFlagAllowLarge); + if(!newBitmap->data){ + debugLog("Couldnt create new framebuffer:%d\n"); + return false; + } + + /*white out framebuffer*/ + MemSet(newBitmap->data, width * height * sizeof(uint16_t), 0xFF); /*clean up old framebuffer if its not the original one and set new one*/ driverWindow->bitmapP->flags.forScreen = false; BmpDelete(driverWindow->bitmapP); - newBitmap->flags.forScreen = true; - driverWindow->bitmapP = newBitmap; + driverWindow->bitmapP = &newBitmap->bitmap; /*tell the emu where the framebuffer is*/ - writeArbitraryMemory32(EMU_REG_ADDR(EMU_SRC), (uint32_t)BmpGetBits(newBitmap)); + writeArbitraryMemory32(EMU_REG_ADDR(EMU_SRC), (uint32_t)newBitmap->data); writeArbitraryMemory32(EMU_REG_ADDR(EMU_VALUE), (uint32_t)width << 16 | height); writeArbitraryMemory32(EMU_REG_ADDR(EMU_CMD), CMD_LCD_SET_FB); diff --git a/tools/palm/muExpansionDriver/muExpDriver.c b/tools/palm/muExpansionDriver/muExpDriver.c index 278d98a..6a0212e 100644 --- a/tools/palm/muExpansionDriver/muExpDriver.c +++ b/tools/palm/muExpansionDriver/muExpDriver.c @@ -21,7 +21,8 @@ static void setConfigDefaults(uint32_t* configFile){ UInt32 PilotMain(UInt16 cmd, MemPtr cmdBPB, UInt16 launchFlags){ DmOpenRef configDb; MemHandle configHandle; - uint32_t* configFile; + uint32_t* dmConfigFile; + uint32_t configFile[CONFIG_FILE_ENTRIES]; Err error; configDb = DmOpenDatabaseByTypeCreator('EMUC', 'GuiC', dmModeReadWrite); @@ -29,29 +30,27 @@ UInt32 PilotMain(UInt16 cmd, MemPtr cmdBPB, UInt16 launchFlags){ /*create db and set defaults if config doesnt exist*/ if(!configDb){ error = DmCreateDatabase(0/*cardNo*/, "Emu Config", 'GuiC', 'EMUC', true); - - debugLog("Tried to create db, err:%d\n", error); + if(error != errNone) + debugLog("Tried to create db, err:%d\n", error); configDb = DmOpenDatabaseByTypeCreator('EMUC', 'GuiC', dmModeReadWrite); - if(!configDb) debugLog("Cant find created db!\n"); configHandle = DmNewResource(configDb, 'CONF', 0, CONFIG_FILE_ENTRIES * sizeof(uint32_t)); - if(!configHandle) debugLog("Cant open db resource!\n"); - configFile = MemHandleLock(configHandle); + dmConfigFile = MemHandleLock(configHandle); setConfigDefaults(configFile); } else{ configHandle = DmGetResource('CONF', 0); - if(!configHandle) debugLog("Cant open db resource!\n"); - configFile = MemHandleLock(configHandle); + dmConfigFile = MemHandleLock(configHandle); + MemMove(configFile, dmConfigFile, CONFIG_FILE_ENTRIES * sizeof(uint32_t)); } if(cmd == sysAppLaunchCmdNormalLaunch) @@ -59,6 +58,11 @@ UInt32 PilotMain(UInt16 cmd, MemPtr cmdBPB, UInt16 launchFlags){ else if(cmd == sysAppLaunchCmdSystemReset) initBoot(configFile); + /*must use DmWrite to write to databases or a write protect violation may trigger*/ + error = DmWrite(dmConfigFile, 0, configFile, CONFIG_FILE_ENTRIES * sizeof(uint32_t)); + if(error != errNone) + debugLog("Coludnt write config file, err:%d\n", error); + MemHandleUnlock(configHandle); DmReleaseResource(configHandle); DmCloseDatabase(configDb); diff --git a/tools/palm/muExpansionDriver/palmOsPriv.h b/tools/palm/muExpansionDriver/palmOsPriv.h new file mode 100644 index 0000000..6a2bda0 --- /dev/null +++ b/tools/palm/muExpansionDriver/palmOsPriv.h @@ -0,0 +1,11 @@ +#ifndef PALM_OS_PRIV_H +#define PALM_OS_PRIV_H + +#define memNewChunkFlagAllowLarge 0x1000 + +#define FIXED_ADDRESS_VAR(a, t) (*((volatile t*)a)) + +/*these are all the Palm OS varibles the SDK dosent expose that I have found while decompiling Palm OS*/ +#define ScrStatePtr FIXED_ADDRESS_VAR(0x00000164, void*) + +#endif diff --git a/tools/palm/muExpansionDriver/traps.c b/tools/palm/muExpansionDriver/traps.c index 8bc8f9c..a7477a9 100644 --- a/tools/palm/muExpansionDriver/traps.c +++ b/tools/palm/muExpansionDriver/traps.c @@ -4,6 +4,7 @@ #include "debug.h" #include "armv5.h" #include "globals.h" +#include "palmOsPriv.h" #include "palmGlobalDefines.h" @@ -92,126 +93,136 @@ void emuErrDisplayFileLineMsg(const Char* const filename, UInt16 lineNo, const C debugLog("Error at:%s, Line:%d, Msg:%s\n", filename, lineNo, msg); } -Err emuHwrDisplayAttributes(Boolean set, UInt8 attribute, void* returnPtr){ +Err emuHwrDisplayAttributes(Boolean set, UInt8 attribute, void* dataPtr){ /*this function is exempt from formatting standards, it is meant as an exact C reconstrution of the Tungsten W HwrDisplayAttributes patched to work on an m515*/ static const char lcdControllerName[] = "MQ11xx LCD Controller"; Err error = errNone; + /*dispErrorClass | 0x01 = Cant Set???, also returned for null pointers*/ + /*dispErrorClass | 0x04 = Attribute Doesnt Exist??*/ + switch(attribute){ case 0: /*display controler ID, MediaQ GPU*/ - (UInt32*)returnPtr = '12MQ'; + (UInt32*)dataPtr = '12MQ'; break; case 1: /*???*/ - (UInt16*)returnPtr = 1; + (UInt16*)dataPtr = 1; break; case 2: /*???*/ - (UInt16*)returnPtr = 1; + (UInt16*)dataPtr = 1; break; case 3: /*???*/ - (UInt16*)returnPtr = 0x808B; + (UInt16*)dataPtr = 0x808B; break; case 4: /*read some random RAM value and fail if 0*/ - if(readArbitraryMemory32(0x164)) - (UInt16*)returnPtr = 0x10; + if(ScrStatePtr) + (UInt16*)dataPtr = 0x10; else error = dispErrorClass | 0x04; break; case 5: /*???*/ - (UInt16*)returnPtr = 8; + (UInt16*)dataPtr = 8; break; case 6: /*???*/ - (UInt16*)returnPtr = 0x10; + (UInt16*)dataPtr = 0x10; break; case 7: - /*display size something, PrvDisplaySize(Uint8 unknown, void* returnPtr)*/ - /*TODO*/ + /*display size something, width???, PrvDisplaySize(Boolean set, void* dataPtr)*/ + /*UNTESTED*/ + (UInt16*)dataPtr = 320; break; case 8: - /*display size something, PrvDisplaySize(Uint8 unknown, void* returnPtr)*/ - /*TODO*/ + /*display size something, height???, PrvDisplaySize(Boolean set, void* dataPtr)*/ + /*UNTESTED*/ + (UInt16*)dataPtr = 320; break; case 9: - /*display chip address space start address*/ - (UInt32*)returnPtr = 0x1F000000; + /*display chip framebuffer start address*/ + (UInt32*)dataPtr = 0x1F000000; break; case 10: - /*display chip address space size*/ - (UInt32*)returnPtr = 0x3C000; + /*display chip framebuffer size*/ + (UInt32*)dataPtr = 0x3C000; break; case 11: /*???*/ - (UInt16*)returnPtr = 1; + (UInt16*)dataPtr = 1; break; case 12: /*full name of display controller*/ - StrCopy((char*)returnPtr, lcdControllerName); + StrCopy((char*)dataPtr, lcdControllerName); break; case 13: - /*display size base address*/ - /*TODO*/ + /*display base address*/ + /*UNTESTED*/ + (UInt32*)dataPtr = 0x1F000000; break; case 14: - /*display size depth*/ - /*TODO*/ + /*display depth*/ + /*UNTESTED*/ + (UInt16*)dataPtr = 16; break; case 15: - /*read some random RAM value and write to returnPtr*/ + /*read some random RAM value and write to dataPtr*/ if(readArbitraryMemory8(0x36C)) - (UInt16*)returnPtr = 0x140; + (UInt16*)dataPtr = 0x140; else - (UInt16*)returnPtr = 0xA0; + (UInt16*)dataPtr = 0xA0; break; case 16: - /*read some random RAM value and write to returnPtr*/ + /*read some random RAM value and write to dataPtr*/ if(readArbitraryMemory8(0x36C)) - (UInt16*)returnPtr = 0x140; + (UInt16*)dataPtr = 0x140; else - (UInt16*)returnPtr = 0xA0; + (UInt16*)dataPtr = 0xA0; break; case 17: /*display row bytes*/ - /*TODO*/ + /*UNTESTED*/ + (UInt16*)dataPtr = 320 * sizeof(uint16_t); break; case 18: /*display backlight*/ - /*TODO*/ + /*UNTESTED*/ + (UInt8*)dataPtr = 0x01; break; case 19: /*display contrast*/ - /*TODO*/ + /*UNTESTED*/ + (UInt8*)dataPtr = 0x01; break; /*case 20: has no handler*/ case 21: /*display debug indicator*/ - /*TODO*/ + /*TODO, shouldnt be needed*/ break; case 22: @@ -219,7 +230,7 @@ Err emuHwrDisplayAttributes(Boolean set, UInt8 attribute, void* returnPtr){ if(set) error = dispErrorClass | 0x01; else - (UInt32*)returnPtr = 0x1F03C000; + (UInt32*)dataPtr = 0x1F03C000; break; /*case 23: has no handler*/ @@ -229,27 +240,25 @@ Err emuHwrDisplayAttributes(Boolean set, UInt8 attribute, void* returnPtr){ case 27: /*???*/ - (UInt16*)returnPtr = 0x90; + (UInt16*)dataPtr = 0x90; break; case 28: /*???*/ - (UInt16*)returnPtr = 0x40; + (UInt16*)dataPtr = 0x40; break; case 29: /*???*/ - (UInt16*)returnPtr = 0x20; + (UInt16*)dataPtr = 0x20; break; case 30: /*???*/ - (UInt8*)returnPtr = 1; + (UInt8*)dataPtr = 1; break; default: - /*attribute > 30*/ - /*dispErrorClass | 0x04 = Invalid Param???*/ debugLog("Invalid display attribute requested:%d\n", attribute); error = dispErrorClass | 0x04; break; diff --git a/tools/palm/muExpansionDriver/traps.h b/tools/palm/muExpansionDriver/traps.h index 5a60df1..ad972bc 100644 --- a/tools/palm/muExpansionDriver/traps.h +++ b/tools/palm/muExpansionDriver/traps.h @@ -6,6 +6,6 @@ UInt32 emuPceNativeCall(NativeFuncType *nativeFuncP, void *userDataP); UInt32 emuKeyCurrentState(void); void emuErrDisplayFileLineMsg(const Char* const filename, UInt16 lineNo, const Char* const msg); -Err emuHwrDisplayAttributes(Boolean set, UInt8 attribute, void* returnPtr); +Err emuHwrDisplayAttributes(Boolean set, UInt8 attribute, void* dataPtr); #endif diff --git a/tools/palm/muExpansionDriver/unimplementedFeatures.txt b/tools/palm/muExpansionDriver/unimplementedFeatures.txt index 4adbc48..d72b0ef 100644 --- a/tools/palm/muExpansionDriver/unimplementedFeatures.txt +++ b/tools/palm/muExpansionDriver/unimplementedFeatures.txt @@ -6,5 +6,6 @@ ARM support OS 5 audio API Custom FB mode GUI +Setting window color like OS 5 Fixed: \ No newline at end of file