rand/srand stubs use the host std::rand: the guest seed is lost and RAND_MAX changes (Tekken 4 CPU opponents never vary)
I maintainer di solito rispondono entro 5 giorni
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 48/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- cpp
- Ambito
- backend, operating-systems
Direzione di ricerca
Start with ps2xRuntime/include/ps2_call_list.h and the rand/srand stubs in ps2xRuntime/src/lib/Kernel/Stubs/LibC.cpp; trace how resolveStubTarget selects them and how unmatched guest functions run. Check the guest reent state and EE newlib behavior described in the issue. Done when guest seeding, range, and sequence are preserved, including when only one symbol matches.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Summary
ps2_stubs::rand and ps2_stubs::srand (ps2xRuntime/src/lib/Kernel/Stubs/LibC.cpp) replace the game's libc rand/srand with the host's std::rand/std::srand:
void rand(...) { setReturnS32(ctx, std::rand() & 0x7FFF); }
void srand(...) { std::srand(getRegU32(ctx, 4)); setReturnS32(ctx, 0); }
This changes guest behaviour in three ways:
- The guest seed is lost when only one of the pair is matched by name. The stub is chosen per function name (
resolveStubTarget). In Tekken 4 (PAL, SCES-50878) the symbol match findsrand(0x2A3178) but notsrand(0x2A3168, left asFUN_002a3168). So the guestsrandruns as recompiled code and stores the seed in the libc reent struct. The stubbedrandthen reads the hoststd::randstate, which nothing ever seeds, and gives the same sequence on every boot. - Different range. The stub returns 0..0x7FFF, but the EE newlib
RAND_MAXthat games are compiled against is 0x7FFFFFFF. Code that scales byRAND_MAX(e.g.rand() / (float)RAND_MAX) gets values 65536 times too small, andrand() % ngets a different distribution. - Different sequence. Even when both functions are stubbed, the host generator isn't newlib's. Seeded sequences (demos, replays, AI) then differ from PCSX2 and real hardware. The host
std::randstate is also shared with anything else in the process that calls it.
Symptom
In Tekken 4 the CPU opponent was always the same character (Law in Arcade, Steve in Story mode), whatever the input timing. PCSX2 picks a different opponent each time. The CPU fighters' AI also draws from rand.
Workaround in our game override
We register an override on the game's rand address that runs the EE libc routine on the guest state:
// EE newlib rand: next = next * 1103515245 + 12345 (32-bit); return next & 0x7FFFFFFF.
// In this game the state is the 32-bit word at _impure_ptr + 88 (_impure_ptr at 0x3059D4).
void guestRand(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
{
ctx->pc = getRegU32(ctx, 31);
uint32_t reent = 0, next = 0;
std::memcpy(&reent, rdram + 0x003059D4u, 4);
uint8_t *state = getMemPtr(rdram, reent + 88u);
if (!state) { setReturnS32(ctx, 0); return; }
std::memcpy(&next, state, 4);
next = next * 1103515245u + 12345u;
std::memcpy(state, &next, 4);
setReturnS32(ctx, static_cast<int32_t>(next & 0x7FFFFFFFu));
}
With it, the opponents vary as they do in PCSX2.
Suggested fix
rand and srand are pure integer code on guest memory with no host dependency. The simplest fix is to stop stubbing them: drop X(rand) and X(srand) from ps2xRuntime/include/ps2_call_list.h and let the recompiled guest code run. That keeps the game's own state, seed, range and sequence, whichever of the two the symbol match finds.
If a stub is preferred, it should at least keep its own state (not the host std::rand) and use newlib's LCG and RAND_MAX of 0x7FFFFFFF. It would still lose the seed whenever only one of the pair is matched by name, though, so not stubbing looks safer.
Checked against main at e563698.
- Lingua principale
- C++
- Stelle
- 3.3k
- Fork
- 149
- Merge medio
- 8h 46m
- PR unite (30g)
- 1
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di ran-j/PS2Recomp
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
ran-j/PS2Recomp#393 · 1 commento ·
I maintainer di solito rispondono entro 5 giorni
-
EE sceCdStatus stub always reports READ (6), hanging games that wait for PAUSEForse già presa @ahmedgamalalzatary l’ha presa 12 giorni fa. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 95/100
I maintainer di solito rispondono entro 5 giorni
-
VU0 macro VSQI / VSQD: address and source registers swapped in generated codeForse già presa @yanm1103 l’ha presa 20 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 5 giorni
-
Runtime: SampleTexture casts unbounded or NaN texture coordinates to int, which is undefined behaviourForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
ran-j/PS2Recomp#273 · 2 commenti ·
I maintainer di solito rispondono entro 5 giorni
-
Build: /Qspectre- is MSVC-only and breaks every Release target under clang-clForse già presa @ahmedgamalalzatary l’ha presa 12 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
I maintainer di solito rispondono entro 5 giorni
Tutte le issue di ran-j/PS2Recomp
Issue simili
-
Linux npm packaging leaves workspace files that block subsequent CI checkoutForse già presa @Spockkk0225 l’ha presa oggi. Apertaci
Difficoltà 2/5 1-3 ore Idoneità per principianti 73/100
alibaba/neug#1195 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 3 giorni
-
Bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
SCIInstitute/SCIRun#2909 ·
I maintainer di solito rispondono entro 2 giorni
-
area: packaging blocked dependencies P3: low post-stable value: medium
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
patrick-hudson/put.gd-desktop#1210 ·
I maintainer di solito rispondono entro 1 giorno