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

Abierto
#406 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 5 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
1-2 días
Aptitud para principiantes
48/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
cpp

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
C++
Estrellas
3.3k
Forks
149
Merge medio
8 h 46 min
PR fusionados (30 d)
1

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de ran-j/PS2Recomp

Todos los issues de ran-j/PS2Recomp

Issues similares

Más issues de C++

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.