rand/srand stubs use the host std::rand: the guest seed is lost and RAND_MAX changes (Tekken 4 CPU opponents never vary)

Aperta
#406 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

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

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:

  1. 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 finds rand (0x2A3178) but not srand (0x2A3168, left as FUN_002a3168). So the guest srand runs as recompiled code and stores the seed in the libc reent struct. The stubbed rand then reads the host std::rand state, which nothing ever seeds, and gives the same sequence on every boot.
  2. Different range. The stub returns 0..0x7FFF, but the EE newlib RAND_MAX that games are compiled against is 0x7FFFFFFF. Code that scales by RAND_MAX (e.g. rand() / (float)RAND_MAX) gets values 65536 times too small, and rand() % n gets a different distribution.
  3. 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::rand state 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

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di ran-j/PS2Recomp

Tutte le issue di ran-j/PS2Recomp

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.