decomp(bs2): match CheckBS2CommandStatus, BS2Tick 57 -> 4 (local statics, u64 globals, proven volatile flags) #1308

Merged
coah80 merged 1 commits from agent/w0929/o2-bs2mach-land into main 2026-10-09 05:09:21 +00:00
3 changed files with 170 additions and 65 deletions

View File

@@ -23681,8 +23681,8 @@ RegionValid = .sbss:0x81698A48; // type:object size:0x4 data:4byte
NandPending = .sbss:0x81698A4C; // type:object size:0x4 scope:local data:4byte
CancelNand = .sbss:0x81698A50; // type:object size:0x4 scope:local data:4byte
LowReadResult = .sbss:0x81698A54; // type:object size:0x4 scope:local data:4byte
CacheCommandComplete = .sbss:0x81698A58; // type:object size:0x4 scope:local data:4byte
AudioBufferUnconfigured = .sbss:0x81698A5C; // type:object size:0x4 scope:local data:4byte
CacheCommandComplete$1963 = .sbss:0x81698A58; // type:object size:0x4 scope:local data:4byte
AudioBufferUnconfigured$2065 = .sbss:0x81698A5C; // type:object size:0x4 scope:local data:4byte
ResetTime = .sbss:0x81698A60; // type:object size:0x8 scope:local data:4byte
SpinupDeadline = .sbss:0x81698A68; // type:object size:0x8 scope:local data:4byte
CoverPollTime = .sbss:0x81698A70; // type:object size:0x8 data:4byte
@@ -23690,15 +23690,13 @@ DriveWasReset = .sbss:0x81698A78; // type:object size:0x4 data:4byte
TitleTicketView = .sbss:0x81698A7C; // type:object size:0x4 scope:local data:4byte
CurrentTmd = .sbss:0x81698A80; // type:object size:0x4 scope:local data:4byte
TitleCode = .sbss:0x81698A84; // type:object size:0x4 data:4byte
RequiredIosHigh = .sbss:0x81698A88; // type:object size:0x4 data:4byte
RequiredIosLow = .sbss:0x81698A8C; // type:object size:0x4 data:4byte
RequiredIos = .sbss:0x81698A88; // type:object size:0x8 data:4byte
GamePartition = .sbss:0x81698A90; // type:object size:0x4 data:4byte
UpdatePartition = .sbss:0x81698A94; // type:object size:0x4 data:4byte
PartitionCursor = .sbss:0x81698A98; // type:object size:0x4 data:4byte
DataToc = .sbss:0x81698A9C; // type:object size:0x4 data:4byte
GameToc = .sbss:0x81698AA0; // type:object size:0x8 data:4byte
CoverOpenTimeHigh = .sbss:0x81698AA8; // type:object size:0x4 data:4byte
CoverOpenTimeLow = .sbss:0x81698AAC; // type:object size:0x4 data:4byte
CoverOpenTime = .sbss:0x81698AA8; // type:object size:0x8 data:4byte
NandTransferred = .sbss:0x81698AB0; // type:object size:0x4 data:4byte
NandLength = .sbss:0x81698AB4; // type:object size:0x4 data:4byte
NandBuffer = .sbss:0x81698AB8; // type:object size:0x4 data:4byte

View File

@@ -54,54 +54,50 @@ 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 vu32 RestartRequested = 0;
static vu32 PartitionOpen = 0;
static u32 CacheSeekComplete = 0;
static u32 LoadingTitle = 0;
BOOL FatalErrorFlag = FALSE;
static vu32 LoadingTitle = 0;
volatile BOOL FatalErrorFlag = FALSE;
BOOL RetryErrorFlag = FALSE;
BOOL UpdateErrorFlag = FALSE;
BOOL AbortFlag = FALSE;
volatile BOOL AbortFlag = FALSE;
volatile int CacheFailed = 0;
volatile int RegionValid = 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;
static u32 TitleTicketView = 0;
static u32 CurrentTmd = 0;
u32 TitleCode = 0;
u32 RequiredIosHigh = 0;
u32 RequiredIosLow = 0;
u32 GamePartition = 0;
u32 UpdatePartition = 0;
u32 PartitionCursor = 0;
u32 DataToc = 0;
u64 GameToc = 0;
u32 CoverOpenTimeHigh = 0;
u32 CoverOpenTimeLow = 0;
vu32 NandTransferred = 0;
vu32 NandLength = 0;
u8 *NandBuffer = NULL;
NANDFileInfo *NandFile = NULL;
vu32 NandOperation = 0;
volatile NANDCallback NandCompletion = NULL;
u32 LoaderOffset = 0;
vu32 LoaderLength = 0;
u32 LoaderAddress = 0;
vu32 CacheLength = 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;
static u32 LoaderInit;
static u32 LoaderMain;
static u32 LoaderClose;
static u32 *DvdProgress;
static vu32 DvdTransferred;
static vu32 DvdTransferLength;
static vu32 BannerLength;
vu32 CacheLength;
u32 LoaderAddress;
vu32 LoaderLength;
u32 LoaderOffset;
volatile NANDCallback NandCompletion;
vu32 NandOperation;
NANDFileInfo *NandFile;
u8 *NandBuffer;
vu32 NandLength;
vu32 NandTransferred;
OSTime CoverOpenTime;
u64 GameToc;
u32 DataToc;
u32 PartitionCursor;
u32 UpdatePartition;
u32 GamePartition;
u64 RequiredIos;
u32 TitleCode;
static u32 CurrentTmd;
static u32 TitleTicketView;
u32 DriveWasReset;
u64 CoverPollTime;
static u64 SpinupDeadline;
static u64 ResetTime;
void BS2Report(const char *msg, ...) {
@@ -667,8 +663,8 @@ void BS2StartGame() {
__OSWriteStateFlags(&stateFlags);
__OSClearRTCFlags();
titleType = RequiredIosHigh;
titleCode = RequiredIosLow;
titleType = (u32)(RequiredIos >> 32);
titleCode = (u32)RequiredIos;
ticketCount = 1;
ticketViews = TicketViews;
if ((titleCode | titleType) == 0) {
@@ -982,9 +978,7 @@ static BOOL CheckDVDCommandStatus(DVDCommandBlock *block) {
case 4:
case 5:
if (State == 7 || State == 9 || State == 10) {
u64 systemTime = __OSGetSystemTime();
CoverOpenTimeLow = (u32)systemTime;
CoverOpenTimeHigh = (u32)(systemTime >> 32);
CoverOpenTime = __OSGetSystemTime();
State = BS2_STT_66;
} else {
State = BS2_STT_NO_DISK;
@@ -1099,6 +1093,8 @@ static void BS2NANDDivideWriteAsync(NANDFileInfo *info, const void *buffer, u32
}
static BOOL CheckBS2CommandStatus() {
static u32 CacheCommandComplete = 0;
if (CheckDVDCommandStatus(&Block) == 0) {
BS2Report("DVD command is issuing\n");
return 0;
@@ -1181,8 +1177,8 @@ static BOOL CheckBS2CommandStatus() {
if (BS2BootCaching != 0) {
BS2Report("Write partition ifno\n");
NandPending = 1;
CacheLength += OSRoundUp32B(((DVDGameTOC *)DataToc)->partitionCount * sizeof(DVDPartitionInfo)) + 32;
CacheCommandComplete = 1;
CacheLength += OSRoundUp32B(((DVDGameTOC *)DataToc)->partitionCount * sizeof(DVDPartitionInfo)) + 32;
if ((unsigned int)CacheLength > 0xb00000) {
BS2NANDCallback(-1, NULL);
return 1;
@@ -1324,6 +1320,7 @@ static void BS2ReadDiskID(void *buffer, s32 length, u32 offset) {
}
BS2State BS2Tick() {
static u32 AudioBufferUnconfigured;
u32 interruptsEnabled = OSDisableInterrupts();
u32 loaderAddress;
u32 loaderLength;
@@ -1338,7 +1335,6 @@ BS2State BS2Tick() {
u64 currentTime;
BOOL regionMatches;
char productRegion;
u32 iosHigh;
char *ticketByte;
u32 entryCount;
DVDGameTOC *dataToc;
@@ -1351,17 +1347,16 @@ BS2State BS2Tick() {
BS2Report("Drive Reset : %d\n", BS2DriveReset);
BS2Report("Wait Spinup : %d\n", BS2WaitSpinup);
BS2Report("Boot From Cache : %d\n", BS2BootFromCache);
DvdProgress = (u32 *)&(*(u32 *)0x800030d4);
DvdProgress = (u32 *)0x800030d4;
PartitionParams.numTmdBytes = 0;
PartitionParams.numCertBytes = 0;
PartitionParams.dataWordOffset = 0;
(*(u32 *)0x800030d4) = 0;
*DvdProgress = 0;
DvdTransferred = 0;
DvdTransferLength = 0;
status = BS2NoDisk;
AudioBufferUnconfigured = 1;
CoverBlock.state = 0;
if (status != 0)
if (BS2NoDisk != 0)
State = BS2_STT_NO_DISK;
else if (BS2DriveReset != 0)
State = BS2_STT_2;
@@ -1440,13 +1435,14 @@ BS2State BS2Tick() {
case 5:
status = CheckBS2CommandStatus();
if (status != 0) {
if ((((const OSBootInfo *)0x80000000)->consoleType & 0xf0000000) == 0) {
OSBootInfo *bootInfo = (OSBootInfo *)OSPhysicalToCached(OS_ADDR_BOOT_INFO);
if ((bootInfo->consoleType & 0xf0000000) == 0) {
__OSDeviceCode = 0x8002;
State = BS2_STT_8;
} else {
__OSDeviceCode = DriveInfo.deviceCode | 0x8000;
State = BS2_STT_8;
}
State = BS2_STT_8;
}
break;
case 6:
@@ -1882,9 +1878,7 @@ invalidRvlRegion:
(u32)((ESTitleMeta *)CurrentTmd)->head.sysVersion);
BS2Report("Title ID ... 0x%08X%08X\n", (u32)(((ESTitleMeta *)CurrentTmd)->head.titleId >> 32),
(u32)((ESTitleMeta *)CurrentTmd)->head.titleId);
iosHigh = (u32)(((ESTitleMeta *)CurrentTmd)->head.sysVersion >> 32);
RequiredIosLow = (u32)((ESTitleMeta *)CurrentTmd)->head.sysVersion;
RequiredIosHigh = iosHigh;
RequiredIos = ((ESTitleMeta *)CurrentTmd)->head.sysVersion;
titleCode = (u32)((ESTitleMeta *)CurrentTmd)->head.titleId;
TitleCode = titleCode;
if (((ESTitleMeta *)CurrentTmd)->head.sysVersion == 0x0000000100000010ULL) {
@@ -2072,7 +2066,8 @@ invalidRvlRegion:
} else {
if ((BannerBuffer != 0) && (BannerLength != 0)) {
if (BannerLength < ((bannerFile.length + 0x1fU) & 0xffffffe0))
OSPanic("BS2Mach.c", 0x12d3, "BS2 ERROR >>> Banner buffer is not enough to load banner file (0x%08x)");
OSPanic("BS2Mach.c", 0x12d3, "BS2 ERROR >>> Banner buffer is not enough to load banner file (0x%08x)",
bannerFile.length);
BS2Report("BannerBufferAddr : %08X\n", BannerBuffer);
} else
OSPanic("BS2Mach.c", 0x12da, "BS2 ERROR >>> MEMAllocator and banner buffer is not set");
@@ -2321,7 +2316,7 @@ invalidRvlRegion:
break;
case 0x42:
currentTime = __OSGetSystemTime();
if ((OSTime)(currentTime - (((u64)CoverOpenTimeHigh << 32) | CoverOpenTimeLow)) < OSMillisecondsToTicks((OSTime)350))
if ((OSTime)(currentTime - CoverOpenTime) < OSMillisecondsToTicks((OSTime)350))
break;
currentTime = __OSGetSystemTime();
CoverPollTime = currentTime;

View File

@@ -0,0 +1,112 @@
# o2-bs2mach attempts (BS2Mach: CheckBS2CommandStatus, BS2Tick)
Worktree data-d7, branch agent/w1009/o2-bs2mach, base 20cf9f9f (origin/main).
Start: 27/29 exact, CheckBS2CommandStatus 2/406, BS2Tick 57/1940, data 158528/158528, pool 91/91.
## CheckBS2CommandStatus (lever 35)
Target case 0x12 issues `lwz count; stw CacheCommandComplete; slwi`: the completion
store fills the load-latency slot, so it has no edge to the partition-count load
through `DataToc`. A file-scope static may alias that pointer load, a
function-local static whose address is never taken may not.
1. `static u32 CacheCommandComplete = 0;` inside CheckBS2CommandStatus (it is the
only user), completion stored before the length update as in every other case:
CheckBS2CommandStatus 0/406. Every other function unchanged, pool identical.
The static lands at the end of .sbss (after LoaderInit) instead of 0x50.
2. Compiler probe (same flags): initialized objects and function statics are
emitted in parse order, tentative definitions after them in reverse definition
order (`I1 I2 L$4 I3 T4 T3 T2 T1`).
3. AudioBufferUnconfigured..LoaderInit made tentative (initializers dropped) and
listed in reverse: .sbss order and offsets identical to the target, 28/29
exact, data 158528/158528. Target symbol renamed `CacheCommandComplete$1964`
in config/43U/symbols.txt (project convention for function statics).
## BS2Tick (57 at start)
4. Same lever in case 0: the target schedules `li 1` first and keeps the
`AudioBufferUnconfigured = 1` store free of the absolute `*(u32 *)0x800030d4`
store (unknown storage aliases every file-scope object, not a function static).
AudioBufferUnconfigured, ResetTime and SpinupDeadline are only used by BS2Tick
and sit right after CacheCommandComplete in .sbss. All three as BS2Tick
statics: 57 -> 52; only AudioBufferUnconfigured as a static gives the same 52,
so ResetTime/SpinupDeadline stay file-scope tentatives. Symbol renamed
`AudioBufferUnconfigured$2066`. .sbss order identical.
5. Store-before-load pairs the target keeps and we hoist (case 0x30
`PartitionOpen = 0; if (RegionValid == 0)`, case 0x34/0x35
`LoadingTitle = 0; switch (Block.state)`, case 0x40 `AbortFlag = 0;
if (RestartRequested == 0)` and `RestartRequested = 0; if (FatalErrorFlag)`).
Compiler probe with the unit's flags (/tmp/o2-bs2mach/probe/t2.c, t3.c): a
store to one global followed by a load of another keeps its order only when
BOTH are volatile; non-volatile/volatile, volatile/non-volatile, extern and
address-taken globals are all reordered. So the target proves PartitionOpen,
LoadingTitle, AbortFlag, RestartRequested and FatalErrorFlag volatile
(RegionValid and DVDCommandBlock.state already are). AbortFlag is also read in
BS2DVDCallback (interrupt context); RestartRequested/AbortFlag writers disable
interrupts. One-at-a-time sweeps could not see the case-0x40 chain because it
needs all three together. All five: 52 -> 42, other 28 functions unchanged.
6. Device-code block (case 5): the initial PCode has a separate `lis 0x8000` per
branch and backend value numbering merges them into the consoleType base. Per
the MWCC decomp (ValueNumbering.c), `li`/`lis` are only value-numbered when the
destination is a temporary inside the coalesce window; named locals never are.
Reading consoleType through a named `OSBootInfo *bootInfo` and sharing the
`State = BS2_STT_8` tail: 42 -> 28, block exact.
7. Case 0: `DvdProgress = (u32 *)0x800030d4; ... *DvdProgress = 0;` (as case 2
writes it) and testing `BS2NoDisk` directly: 28 -> 24 -> 12 together with 8.
8. CoverOpenTimeHigh/Low are one 8-aligned big-endian OSTime: a single
`OSTime CoverOpenTime` keeps CheckDVDCommandStatus exact (u64 store is low word
first, as in the target) and makes the case-0x42 zero high word a later temp,
fixing that whole register cluster (24 -> 14 at the time).
9. Banner check: the target loads bannerFile.length straight into r6 because it is
the 4th OSPanic argument; the format string has `(0x%08x)` and our call had no
argument. Passing bannerFile.length fixes the cluster.
10. RequiredIosHigh/Low are likewise one 8-aligned u64 (BS2StartGame reads it as a
title ID with the `== 0 -> 1:3` default). `u64 RequiredIos` gives the target
pair coloring (hi r0, lo r3, TMD r4) and keeps BS2StartGame exact.
symbols.txt: CoverOpenTime and RequiredIos as size-8 objects (only BS2Mach
referenced the halves; every relocation resolves to the same address).
BS2Tick 4/1940 left: case 0 `CoverBlock.state = 0` is ordered before the
volatile BS2NoDisk load (target: after).
### Volatile flags: per-flag evidence (current source, BS2Tick 4 diffs)
Target pairs a store to one global with a later load of another and keeps that
order; MWCC only does so when both are volatile (probe t2/t3: non-volatile,
one-sided, extern and address-taken pairs are all reordered).
- 0x3b04 case 0x30: `stw PartitionOpen; lwz RegionValid` (RegionValid volatile).
- 0x3d68 case 0x34/0x35: `stw LoadingTitle; lwz Block.state` (state volatile).
- 0x40a0 case 0x40: `stw AbortFlag; lwz RestartRequested`, then
`stw RestartRequested; lwz FatalErrorFlag`.
Dropping any single flag: PartitionOpen 4->7, LoadingTitle 4->6, AbortFlag 4->7,
RestartRequested 4->9, FatalErrorFlag 4->6; no other function changes.
### Last 4 diffs: case 0 `CoverBlock.state = 0` vs the BS2NoDisk test
Target: `lwz r0,BS2NoDisk; stw r4,AUC; cmpwi r0,0; stw r27,0xc(r3); beq`.
Ours: the CoverBlock store precedes the load. Everything else in the block,
including every register, already matches.
- BS2NoDisk, DvdTransferred and DvdTransferLength are volatile (non-volatile
BS2NoDisk hoists the load above both DvdTransfer stores; non-volatile
DvdTransferred breaks BS2DVDCallback's order against DvdReadPending).
- DVDCommandBlock.state reads are volatile in the target (BS2StartGame and
BS2StartGCGame poll it with a re-read every iteration; case 0x34 above). With
`state` non-volatile case 0 becomes exact but those break.
- So the source reads BS2NoDisk before the store. Holding it in any variable
(named, block-scoped, inline parameter/local, any same-width type) makes it a
named/`@` local, numbered below statement temps and colored after the constant
1: registers rotate (1 r0, BS2NoDisk r4, &PP r4, &0x800030d4 r5), 10 diffs.
mwdbg capture o2-bs2mach-st + regsim what-if: coloring that holder first gives
exactly the target (only those 4 nodes change). Narrow holders (u8/s8/u16/s16)
force a statement temp and get everything right except `clrlwi.` vs `cmpwi`
(1 diff). A 64-bit round trip is folded away.
- Diagnostic only, both exact 29/29, both forbidden, not committed:
`if ((BS2NoDisk | (CoverBlock.state = 0)) != 0)` (read then store in one
expression) and `*(s32 *)&CoverBlock.state = 0;` (non-volatile store).
No natural spelling found: hoisting a common leading store out of all three
branches doesn't happen (size grows); statement order grid (84 builds),
inline helpers, casts and holder types all stay at 4-10.
- Pragma sweep (combosweep, single + pairwise): BS2Tick 4 -> 4.
### Tooling note
`#pragma push / ppc_iro_level 0 / pop` tightly around BS2Tick (the last function
in the file) leaves it byte-identical, which earlier rounds read as "IRO does not
run on BS2Tick". A file-level `#pragma ppc_iro_level 0` changes BS2Tick in 1910
instructions, so IRO does run; only the scoped placement is not honored for it.