`rand()` stub returns 15 bits instead of newlib's 31 bits, which breaks game randomness (particles collapse to one point)
メンテナーはふだん 3 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 74/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- cpp
調査の方向性
バグは ps2xRuntime/src/lib/Kernel/Stubs/LibC.cpp の rand スタブにあり、std::rand() を 0x7FFF でマスクしています。隣の srand スタブと、それが使うヘルパー(setReturnS32、getRegU32)を読んでください。両方を、issue に記載のとおり 1 で初期化された静的状態を保持する 64 ビット LCG に置き換えてください。rand が 31 ビットの値を返し、srand が同じ状態を設定し、既存のランタイムテストが引き続き通れば完了です。
索引モデルが issue の本文から書いたものです。
説明
Summary
ps2_stubs::rand (ps2xRuntime/src/lib/Kernel/Stubs/LibC.cpp) returns std::rand() & 0x7FFF. PS2 games link the
SDK's newlib, where rand() returns 31 bits (RAND_MAX = 0x7FFFFFFF). Game code that scales the result with 31-bit
math gets a nearly constant value, so anything random comes out identical.
void rand(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
{
setReturnS32(ctx, std::rand() & 0x7FFF);
}
Observed in
Jet Li: Rise to Honor (SCUS-97279). Its particle helper RandomNum (0x270CC0) computes
(rand() - 0x3FFFFFFF) / 2^30 to get a value in [-1, 1). With a 15-bit rand() that is always about -1.0, so every
particle in a burst gets the same velocity, spin and fade. The glass-door shatter and smoke in level 1 were missing.
A GS capture showed the particles were drawn, but stacked on about 12 positions for 342 vertices, with alpha 0-5.
After the fix the effect renders, and ambient particles elsewhere had about 3x more live particles and normal alpha.
Why the stub is wrong in general
- The 15-bit mask matches neither the guest ABI (newlib, 31 bits) nor non-MSVC hosts. glibc's
RAND_MAXis
2^31-1, so& 0x7FFFtruncates there too. std::rand()is host state, so the sequence differs between Windows, Linux and macOS builds and from the console.
The guest'ssrandseed only reachesstd::srand.
Suggested fix
Implement newlib's generator, a 64-bit LCG with seed 1, in the stubs, and have srand set the same state:
static uint64_t s_randNext = 1; // newlib's initial seed
void rand(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
{
s_randNext = s_randNext * 6364136223846793005ull + 1;
setReturnS32(ctx, static_cast<int32_t>((s_randNext >> 32) & 0x7FFFFFFF));
}
void srand(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
{
s_randNext = getRegU32(ctx, 4);
setReturnS32(ctx, 0);
}
This is newlib's rand/srand (libc/stdlib/rand.c), so recompiled games then see the same sequence as on the
console. It is deterministic across host platforms too. (newlib keeps the state in the reent struct; one global is
equivalent for the single EE executor.)
How to check whether a game is affected
Search the generated code for ps2_stubs::rand( to find the game's rand address, then look at its callers. A
caller that subtracts about 0x3FFFFFFF, divides by 2^30 or 2^31, or masks with 0x7FFFFFFF expects 31 bits.
- 主要言語
- C++
- スター
- 3.3k
- フォーク
- 149
- 平均マージ
- 8時間 46分
- マージ済み PR(30日)
- 1
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
ran-j/PS2Recomp のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 77/100
メンテナーはふだん 3 日以内に返信
-
PSLLH/PSRLH/PSRAH: shift amounts 16-31 should use sa[3:0] (SSE gives 0 / sign fill)対応中かも @dean-k-pearce が 4 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
メンテナーはふだん 3 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
ran-j/PS2Recomp#393 · コメント 1 件 ·
メンテナーはふだん 3 日以内に返信
-
EE sceCdStatus stub always reports READ (6), hanging games that wait for PAUSE対応中かも @ahmedgamalalzatary が 30 日前に担当しました。 オープン
難易度 1/5 1時間未満 初心者へのやさしさ 95/100
メンテナーはふだん 3 日以内に返信
-
VU0 macro VSQI / VSQD: address and source registers swapped in generated code対応中かも @yanm1103 が 38 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 3 日以内に返信
ran-j/PS2Recomp の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 80/100
ESPresense/ESPresense#2680 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1時間未満 初心者へのやさしさ 75/100
stew675/llama-cpp-rdna-boosts#348 ·
メンテナーはふだん 1 日以内に返信
-
Documentation: Add comprehensive newcomer onboarding guide, project structure, and local usage to README対応中かも @Ash-Blanc が今日担当しました。 オープンarea/other status/bot-triaged status/need-triage
難易度 2/5 半日 初心者へのやさしさ 85/100
-
bug
難易度 1/5 1時間未満 初心者へのやさしさ 85/100
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信