From 7bdf27eaf82f6457e17f803906656ccf92764f78 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Thu, 8 Oct 2026 19:37:25 +0000 Subject: [PATCH] decomp(bs2): match BS2StartGCGame; make target-local BS2Mach symbols static (worker 0dc04259) --- src/BS2/BS2Mach.c | 94 +++++++++++------------- tools/decomp-assist/opus-bs2.attempts.md | 62 ++++++++++++++++ 2 files changed, 106 insertions(+), 50 deletions(-) create mode 100644 tools/decomp-assist/opus-bs2.attempts.md diff --git a/src/BS2/BS2Mach.c b/src/BS2/BS2Mach.c index 332f450e..147fc935 100644 --- a/src/BS2/BS2Mach.c +++ b/src/BS2/BS2Mach.c @@ -31,7 +31,7 @@ typedef struct AppLoaderHeader { u8 pad_0x1C[4]; } AppLoaderHeader; -u8 TicketViewsBuf[OSRoundUp32B(sizeof(ESTicketView) * 64)] ALIGN32; +static u8 TicketViewsBuf[OSRoundUp32B(sizeof(ESTicketView) * 64)] ALIGN32; ESTicketView *TicketViews = (ESTicketView *)TicketViewsBuf; DVDDiskID UpdateDiskID ALIGN32; OSBootInfo2 bi2 ALIGN32; @@ -39,42 +39,42 @@ AppLoaderHeader AppLoaderHdr ALIGN32; OSBootInfo3 bi3 ALIGN32; u8 GameTOCBuf[OSRoundUp32B(sizeof(DVDGameTOC))] ALIGN32; DVDDriveInfo DriveInfo ALIGN32; -DVDDiskID DiskID ALIGN32; -DVDPartitionParams PartitionParams ALIGN32; +static DVDDiskID DiskID ALIGN32; +static DVDPartitionParams PartitionParams ALIGN32; NANDCommandBlock BS2NandBlock; NANDFileInfo BS2CacheFileInfo; DVDCommandBlock CoverBlock; u8 PartitionInfoBuf[OSRoundUp32B(sizeof(DVDPartitionInfo) * 256)] ALIGN32; DVDCommandBlock Block; -BS2State State = BS2_STT_BEGIN; -vu32 DvdReadPending = 0; -u32 BannerAllocation = 0; -u32 BannerBuffer = 0; -u32 BannerAvailable = 0; -u32 Allocator = 0; -BOOL StartingGame = FALSE; -u32 RestartRequested = 0; -u32 PartitionOpen = 0; -u32 CacheSeekComplete = 0; -u32 LoadingTitle = 0; +static BS2State State = BS2_STT_BEGIN; +static vu32 DvdReadPending = 0; +static u32 BannerAllocation = 0; +static u32 BannerBuffer = 0; +static u32 BannerAvailable = 0; +static u32 Allocator = 0; +volatile BOOL StartingGame = FALSE; +static u32 RestartRequested = 0; +static u32 PartitionOpen = 0; +static u32 CacheSeekComplete = 0; +static u32 LoadingTitle = 0; BOOL FatalErrorFlag = FALSE; BOOL RetryErrorFlag = FALSE; BOOL UpdateErrorFlag = FALSE; BOOL AbortFlag = FALSE; volatile int CacheFailed = 0; volatile int RegionValid = 0; -volatile int NandPending = 0; -vu32 CancelNand = 0; -vu32 LowReadResult = 0; -u32 CacheCommandComplete = 0; -u32 AudioBufferUnconfigured = 0; -u64 ResetTime = 0; -u64 SpinupDeadline = 0; +static volatile int NandPending = 0; +static vu32 CancelNand = 0; +static vu32 LowReadResult = 0; +static u32 CacheCommandComplete = 0; +static u32 AudioBufferUnconfigured = 0; +static u64 ResetTime = 0; +static u64 SpinupDeadline = 0; u64 CoverPollTime = 0; u32 DriveWasReset = 0; -u32 TitleTicketView = 0; -u32 CurrentTmd = 0; +static u32 TitleTicketView = 0; +static u32 CurrentTmd = 0; u32 TitleCode = 0; u32 RequiredIosHigh = 0; u32 RequiredIosLow = 0; @@ -95,13 +95,13 @@ u32 LoaderOffset = 0; vu32 LoaderLength = 0; u32 LoaderAddress = 0; vu32 CacheLength = 0; -vu32 BannerLength = 0; -vu32 DvdTransferLength = 0; -vu32 DvdTransferred = 0; -u32 *DvdProgress = 0; -u32 LoaderClose = 0; -u32 LoaderMain = 0; -u32 LoaderInit = 0; +static vu32 BannerLength = 0; +static vu32 DvdTransferLength = 0; +static vu32 DvdTransferred = 0; +static u32 *DvdProgress = 0; +static u32 LoaderClose = 0; +static u32 LoaderMain = 0; +static u32 LoaderInit = 0; void BS2Report(const char *msg, ...) { @@ -243,7 +243,7 @@ BOOL BS2IsDiagDisc() { return (u8)(*(u8 *)OSPhysicalToCached(OS_ADDR_BOOT_INFO) extern vu32 BS2VideoMode; extern vu32 __DVDLayoutFormat; -asm void Run(u32 entryPoint, void *start, u32 blockCount, u32 argument) { +static asm void Run(u32 entryPoint, void *start, u32 blockCount, u32 argument) { // clang-format off #ifdef __MWERKS__ nofralloc @@ -491,7 +491,7 @@ BOOL BS2StartLoadingTitle(ESTitleId titleId, ESTicketView *pTicketView) { static void callback(u32 result) { LowReadResult = result; } -ESError BS2ESGetTicketViews(IOSFd *descriptor, ESTitleId titleId, ESTicketView *views, u32 *count) { +static ESError BS2ESGetTicketViews(IOSFd *descriptor, ESTitleId titleId, ESTicketView *views, u32 *count) { u8 work[256] ALIGN32; IOSIoVector vectors[4] ALIGN32; ESError result; @@ -529,7 +529,7 @@ ESError BS2ESGetTicketViews(IOSFd *descriptor, ESTitleId titleId, ESTicketView * request[2].length = *count * sizeof(*views); return IOS_Ioctlv(*descriptor, 0x13, 2, 1, request); } -void BS2Reboot(void) { +static void BS2Reboot(void) { ESTicketView ticket ALIGN32; u8 work[256] ALIGN32; OSStateFlags flags ALIGN32; @@ -834,9 +834,6 @@ void BS2StartGCGame() { static u8 gameCubeLanguages[] = {0, 0, 1, 2, 3, 4, 5}; s32 ret; u32 rtc; - u32 counterBias; - u32 seconds; - u32 timerFrequency; u32 soundMode; s8 productVideoMode; u8 progressiveMode; @@ -878,10 +875,7 @@ void BS2StartGCGame() { } __OSGetRTC(&rtc); - counterBias = SCGetCounterBias(); - seconds = rtc + counterBias; - timerFrequency = OS_BUS_CLOCK >> 2; - time = (OSTime)seconds * timerFrequency; + time = OS_TIMER_CLOCK * (OSTime)(rtc + SCGetCounterBias()); __OSSetTime(time); sram = __OSLockSram(); @@ -955,7 +949,7 @@ void BS2StartGCGame() { } } -BOOL CheckDVDCommandStatus(DVDCommandBlock *block) { +static BOOL CheckDVDCommandStatus(DVDCommandBlock *block) { if (State == 0x3A) { return TRUE; } @@ -1018,11 +1012,11 @@ BOOL CheckDVDCommandStatus(DVDCommandBlock *block) { return FALSE; } -void BS2NANDDivideCallback(s32 result, NANDCommandBlock *block); -void BS2NANDDivideReadAsync(NANDFileInfo *info, void *buffer, u32 length, NANDCallback callback, NANDCommandBlock *block); -void BS2NANDDivideWriteAsync(NANDFileInfo *info, const void *buffer, u32 length, NANDCallback callback, NANDCommandBlock *block); +static void BS2NANDDivideCallback(s32 result, NANDCommandBlock *block); +static void BS2NANDDivideReadAsync(NANDFileInfo *info, void *buffer, u32 length, NANDCallback callback, NANDCommandBlock *block); +static void BS2NANDDivideWriteAsync(NANDFileInfo *info, const void *buffer, u32 length, NANDCallback callback, NANDCommandBlock *block); -void BS2NANDDivideCallback(s32 result, NANDCommandBlock *block) { +static void BS2NANDDivideCallback(s32 result, NANDCommandBlock *block) { s32 ret; if (CancelNand != 0) { @@ -1063,7 +1057,7 @@ void BS2NANDDivideCallback(s32 result, NANDCommandBlock *block) { } } -void BS2NANDDivideReadAsync(NANDFileInfo *info, void *buffer, u32 length, NANDCallback callback, NANDCommandBlock *block) { +static void BS2NANDDivideReadAsync(NANDFileInfo *info, void *buffer, u32 length, NANDCallback callback, NANDCommandBlock *block) { NandCompletion = callback; NandOperation = 2; NandLength = length; @@ -1079,7 +1073,7 @@ void BS2NANDDivideReadAsync(NANDFileInfo *info, void *buffer, u32 length, NANDCa } } -void BS2NANDDivideWriteAsync(NANDFileInfo *info, const void *buffer, u32 length, NANDCallback callback, NANDCommandBlock *block) { +static void BS2NANDDivideWriteAsync(NANDFileInfo *info, const void *buffer, u32 length, NANDCallback callback, NANDCommandBlock *block) { NandCompletion = callback; NandOperation = 1; NandLength = length; @@ -1095,7 +1089,7 @@ void BS2NANDDivideWriteAsync(NANDFileInfo *info, const void *buffer, u32 length, } } -BOOL CheckBS2CommandStatus() { +static BOOL CheckBS2CommandStatus() { if (CheckDVDCommandStatus(&Block) == 0) { BS2Report("DVD command is issuing\n"); return 0; @@ -1299,7 +1293,7 @@ BOOL CheckBS2CommandStatus() { return TRUE; } -void BS2InquiryAsync(void *buffer, s32 length, u32 offset) { +static void BS2InquiryAsync(void *buffer, s32 length, u32 offset) { if (BS2BootFromCache) { BS2Report("Unencrypted read from cache.dat\n"); NandPending = 1; @@ -1309,7 +1303,7 @@ void BS2InquiryAsync(void *buffer, s32 length, u32 offset) { } } -void BS2ReadDiskID(void *buffer, s32 length, u32 offset) { +static void BS2ReadDiskID(void *buffer, s32 length, u32 offset) { if (BS2BootFromCache) { BS2Report("Read from cache.dat\n"); NandPending = 1; diff --git a/tools/decomp-assist/opus-bs2.attempts.md b/tools/decomp-assist/opus-bs2.attempts.md new file mode 100644 index 00000000..e1c6c635 --- /dev/null +++ b/tools/decomp-assist/opus-bs2.attempts.md @@ -0,0 +1,62 @@ +# opus-bs2 attempts (BS2Mach 4 non-exact, BS2Update UpdateThread) + +Base 16023905. Start: BS2StartGame 10/393, BS2StartGCGame 9/228, CheckBS2CommandStatus 2/406, BS2Tick 71/1940, UpdateThread 335/913. + +Tooling notes: +- mwdbg crashes the emulator (WRITE_UNMAPPED @47e090) whenever `-enc SJIS` is on the command line; drop that flag for tracing (BS2Mach is ASCII). Pass the command via `--args`. +- /tmp/var.py belongs to another worker; my scratch lives in /tmp/opusbs2/. + +## Lever 26 IRO off +1. `#pragma ppc_iro_level 0` around each of the four BS2Mach functions, one at a time: StartGame 10->10, StartGCGame 9->145, CheckBS2CommandStatus 2->2, BS2Tick 71->71. Rejected. + +## Symbol binding (found via readelf, target LOCAL vs ours GLOBAL) +2. Target has 39 BS2Mach symbols LOCAL (State, NandPending, CacheCommandComplete, CacheSeekComplete, Run, CheckBS2CommandStatus, BS2NANDDivide*, LoaderInit/Main/Close, ...). Made them `static`. Bindings now match; no function changed score (codegen identical). Kept as a link-correctness fix. + +## CheckBS2CommandStatus (case 0x12: target `lwz count; stw CacheCommandComplete; slwi`, ours `slwi; stw`) +3. NP; CCC; CacheLength += expr -> 4 (store hoisted before count load). +4. `sizeof * count` operand order -> 2. 5. `CacheLength = CacheLength + expr` -> 9. 6. `= expr + CacheLength + 32` -> 9. 7. `+= 32 + round` -> 2. 8. `* 8` -> 2. 9. `**(u32 **)&DataToc` -> 2. +10. const DVDGameTOC* cast, CCC before -> 4; CCC after -> 2 (lever 27, no effect). +11. (u32) cast on sum -> 3. 12. TRUE -> 2. 13. 0x20 -> 2. 14. `*(DVDGameTOC **)&DataToc` -> 2. 15. open-coded round -> 2. +16. static inline WriteCacheData(buf,size) helper for case 0x12 -> 241 (size becomes temp; target recomputes in else branch, so no helper). +17. mwdbg: the order comes from the pre-RA scheduler (backend-00 -> 01); post-RA scheduler leaves it. Input order has CCC store after the CacheLength store. +18. CacheCommandComplete type BOOL/int/s32 -> 2; volatile -> 46. +19. With static globals: NP;CCC;CL+= -> 4; CL = CL + (expr) after CCC -> 4. + +## BS2StartGame / BS2StartGCGame shared prologue (`li r0,1` must precede the base `lis` pair) +20. mwdbg: B1 holds the bss/data base pairs, li/stw StartingGame and the hoisted `&CoverBlock` addi; height of the bss chain wins over li. +21. do-while cover loop -> 10; `for(;;) if break` -> 10; `(&CoverBlock)->state` -> 10; `while (CoverBlock.state)` -> 10; StartingGame = 1 / state != 0 -> 10. +22. `DVDGetCommandBlockStatus(&CoverBlock)` call -> 8 (prologue fixed but loop becomes bl; rejected, evidence that the stw needs a successor/priority). +23. static inline WaitForCoverCommand() / WaitCommandIdle(block) helpers -> 10/9. +24. StartingGame after the loop -> 368 (wrong). +25. `volatile BOOL StartingGame` -> StartGame 10->5, StartGCGame 9->4, every other BS2Mach function unchanged (report: 25/29 exact, data 100%). StartingGame is the cross-thread "game is starting" flag read by BS2Tick, like the other volatile flags (CacheFailed, RegionValid, NandPending). KEPT. + +## BS2StartGame DI write (0xCC003024): target addr r4 / value r3, ours addr r3 / value r4 (5 diffs) +26. Declaration permutations of diBits/diValue/diAddr (5 orders) -> 5 or 7. +27. `__PIRegs[9]` instead of raw pointer, value/bits named -> 5 (same tie). regsim (want diValue=r3): structural, 0/1. +28. regsim on three-named form (want diValue=r3,diAddr=r4,diBits=r0): best 1/3, structural. The address is always a lis temp (r196); the value must be a temp numbered after it to be colored first. +29. `*diAddr = (*diAddr | 1) | diBits` (u32/vu32, 1U, no parens) -> registers right but `ori 4`+`ori 1` merge into `ori 5`: 24, size -4. +30. `diBits | (*diAddr | 1)` -> 7 (ori 1 moves after the or). `(u32)(...) | diBits` -> 7. `diValue = *diAddr | 1` -> 7. `__PIRegs[9] | 1` into named -> 7. + +## BS2StartGCGame 64-bit multiply (target mulhwu(freq r5, seconds r7); ours mulhwu(seconds, freq)) +31. Declaration permutations of rtc/counterBias/seconds/timerFrequency x two operand orders (48 builds) -> all 8 (freq*sec) / 4; named values are forwarded, decl order irrelevant. +32. freq*sec forms: `(OSTime)freq * sec`, `freq * (OSTime)sec`, both cast, `(u64)` (145, size change), OS_TIMER_CLOCK*, `(OS_BUS_CLOCK >> 2)` / `/ 4` inline -> 8: operand order fixes the multiply but evaluation order follows it, so registers swap. +33. sec*freq forms: OSSecondsToTicks, `time = rtc + bias; time *= clock`, `time = CLOCK * time` (operands get swapped back to time first!), OSTime-typed seconds and/or timerFrequency -> 4. +34. freq computed before seconds statement -> 4/8; `time = freq; time *= sec` -> 9. +35. OSTime-typed timerFrequency x four freq*sec forms -> 8. +36. Put the SCGetCounterBias() call inside the right operand: `time = OS_TIMER_CLOCK * (OSTime)(rtc + SCGetCounterBias());` -> 0/228 EXACT. The call makes the seconds operand heavier, so it is evaluated first (lower vreg -> r7) while the clock stays the left operand. Also exact: `(OSTime)OS_TIMER_CLOCK * (rtc + SCGetCounterBias())`, `(OSTime)(OS_BUS_CLOCK >> 2) * (...)`. Unused locals counterBias/seconds/timerFrequency removed. Pool identical (91/91), BS2Mach 26/29, data 100%. + +## BS2Tick (71 at start of this round) +37. IRO off -> 71 (attempt 1). +38. Move `AudioBufferUnconfigured = 1` to each of 10 positions in case 0 -> 71/72. volatile AudioBufferUnconfigured -> 71. +39. Definition-level volatile sweep, one global at a time (36 globals): PartitionOpen 71->68, LoadingTitle 71->69, all others same or worse (State 76, ResetTime 91, CoverPollTime 166, CurrentTmd 1314, ...). +40. `static vu32 PartitionOpen` + `static vu32 LoadingTitle` -> BS2Tick 66; every other BS2Mach function unchanged (26/29, data 100%). LoadingTitle is set by BS2StartLoadingTitle from the menu thread and polled by BS2Tick; PartitionOpen is likewise a cross-call state flag. KEPT. +41. Second single-volatile sweep on top of 40 -> nothing below 66. +42. Device-code block (target 0x27d4): target shares one `State = BS2_STT_8` after the if/else; ours duplicates it. Merging fixes the tail but target also re-materializes `lis 0x8000` in each branch, ours reuses the consoleType base -> 8 bytes short, 1703 diffs. Tried consoleType as `(OSBootInfo *)`, `OSPhysicalToCached(0x2C)`, `*(u32 *)0x8000002C`, `OS_ADDR_BOOT_INFO`, `!(...)`, `OSGetConsoleType()` (adds bl), and `*(u16 *)OSPhysicalToCached(0x30E6)` for the store: all 1701-1703. Restored the duplicated State (66). +43. `#pragma ppc_iro_level 0` around BS2Tick with merged State -> identical output; IRO evidently does not run on BS2Tick (or StartGame) at all, so the lis sharing comes from the backend. + +## BS2Update +44. readelf: target has rc/VersionIOS/VersionMEM2/VersionES/ConsoleType GLOBAL and getSuffix LOCAL (ours the opposite). Fixed the bindings: UpdateThread 335/913 unchanged (codegen identical). +45. Logging-call `titleId` via UPDATE_DISC_ENTRIES macro instead of discEntries (target re-materializes `lis 0x8048` there) -> 335. Dropping the discEntries local entirely -> 382. Remaining diff is an extra callee-saved register in target (r14..r31) shifting every allocation; not pursued further this round. + +Final gate (BS2Mach + BS2Update, --quick): GATE PASS, 0 regressions, BS2Mach 26/29 exact (BS2StartGCGame new), BS2Update 9/10. +Best non-exact sources: opus-bs2.best.c (BS2Tick 66, volatile PartitionOpen/LoadingTitle), opus-bs2.best.BS2Update.c (binding fixes, UpdateThread 335). -- 2.49.1