GS library: sceGsResetGraph's display mode kept in the library, not read back from the scratchpad

5030dbb made sceGsSetDefDispEnv and the Z buffer's address read the display mode back from scratchpad 0x100, where
the reference also leaves ResetGraph's parameters. The game's own code reuses that scratchpad afterwards (TMB from its
fourth frame), so SMODE2 and DISPLAY came out non-interlaced where the reference's are interlaced. TMB's pinned boot
check (tmb-check, boot.clg) diverged there at frame 4; it passes again: 3000 frames, all 4,650,558 records, draw lists
on every checked frame. Libraries::gsVideo keeps them (saved in the states); the scratchpad still gets them.

sceGsExecStoreImage's refusal names its caller.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Thomas MartinandClaude Opus 5.5 committed 2026-11-24 00:15:35 +01:00
1 parent e15775a22d
commit 13cddc4904
4 files changed
+31 -10

No files matched your search

+4
View File
@@ -258,6 +258,10 @@ namespace bozo::libs
// Opaque libdma SDK configuration returned by GetEnv. Native services do not
// turn it into DMA controller registers or execute a hardware environment.
std::array<uint8_t, 20> dmaEnvironment{};
// sceGsResetGraph's display parameters {interlace, video mode, field mode, 3}, as libgraph keeps them: the
// SetDef functions read them here. The reference also leaves them at scratchpad 0x100, which the game's own
// code reuses afterwards (TMB from its fourth frame), so they are not read back from there.
std::array<uint8_t, 4> gsVideo{};
MpegState mpeg;
MpegOptions mpegOptions;
// The console's own video standard: what sceGsResetGraph's video mode 0 ("the console's") means. A port sets
+13 -9
View File
@@ -6,6 +6,8 @@
#include "bozo/ee/dispatcher.h"
#include "bozo/port.h"
#include <cstring>
#include <format>
#include <stdexcept>
namespace bozo::libs
@@ -36,6 +38,7 @@ namespace bozo::libs
const uint8_t param[4] = {static_cast<uint8_t>(g.arg(1)), static_cast<uint8_t>(g.arg(2)),
static_cast<uint8_t>(g.arg(3)), 3};
g.write(kScratchpadBase + 0x100u, param, 4);
std::memcpy(l.gsVideo.data(), param, 4);
if (ee::Dispatcher *d = l.dispatcher())
{
// PAL: mode 3, or mode 0 (the console's own standard) on a PAL console (Libraries::palConsole).
@@ -59,12 +62,12 @@ namespace bozo::libs
void resetPath(Libraries &, Guest &) {}
// sceGsSetDefDispEnv(disp, psm, w, h, dx, dy): PMODE, SMODE2, DISPFB, DISPLAY, BGCOLOR (64 bits each).
void setDefDispEnv(Libraries &, Guest &g)
void setDefDispEnv(Libraries &l, Guest &g)
{
const uint32_t disp = g.arg(0);
const int32_t psm = s16(g.arg(1)), w = s16(g.arg(2)), h = s16(g.arg(3)), dx = s16(g.arg(4)), dy = s16(g.arg(5));
// Read only the SDK's scalar video parameters written by ResetGraph, never a packet.
const auto *mode = g.in(kScratchpadBase + 0x100u, 3);
// Only the SDK's scalar video parameters ResetGraph kept, never a packet.
const uint8_t *mode = l.gsVideo.data();
const uint64_t smode2 = mode[0] == 0 ? 2 : (mode[2] ? 3 : 1);
const uint64_t dispfb = (static_cast<uint64_t>(psm & 0xf) << 15) | (static_cast<uint64_t>(((w + 63) >> 6) & 0x3f) << 9);
write64(g, disp, 0x66);
@@ -97,25 +100,25 @@ namespace bozo::libs
// The Z buffer's base after the frame buffers (sceGszbufaddr): in 2048-word pages, two frame buffers unless
// interlaced frame mode NTSC... as the game's libgraph decides it (its GParam's first 48 bits equal to 1).
int32_t zbufAddress(Guest &g, int32_t psm, int32_t w, int32_t h)
int32_t zbufAddress(const Libraries &l, int32_t psm, int32_t w, int32_t h)
{
const int32_t pw = ((w + 63) >= 0 ? w + 63 : w + 126) >> 6;
const int32_t ph = (psm & 2) ? (((h + 63) >= 0 ? h + 63 : h + 126) >> 6) : (((h + 31) >= 0 ? h + 31 : h + 62) >> 5);
const int32_t pages = pw * ph;
const auto *mode = g.in(kScratchpadBase + 0x100u, 3);
const uint8_t *mode = l.gsVideo.data();
const bool single = mode[0] == 1 && mode[1] == 0 && mode[2] == 0;
return single ? static_cast<int16_t>(pages) : static_cast<int16_t>(static_cast<uint32_t>(pages) << 1);
}
// sceGsSetDefDrawEnv / sceGsSetDefDrawEnv2(draw, psm, w, h, ztest, zpsm): eight A+D pairs for context 1 or 2.
void drawEnv(Libraries &, Guest &g, bool second)
void drawEnv(Libraries &l, Guest &g, bool second)
{
const uint32_t p = g.arg(0);
const int32_t psm = s16(g.arg(1)), w = s16(g.arg(2)), h = s16(g.arg(3)), ztest = s16(g.arg(4)), zpsm = s16(g.arg(5));
const uint64_t fbw = second ? static_cast<uint64_t>((w >> 6) & 0x3f) : static_cast<uint64_t>(((w + 63) >> 6) & 0x3f);
write64(g, p + 0, (static_cast<uint64_t>(psm & 0xf) << 24) | (fbw << 16));
write64(g, p + 8, second ? kFrame2 : kFrame1);
uint64_t zbuf = sx(zbufAddress(g, psm, w, h)) | (static_cast<uint64_t>(zpsm & 0xf) << 24);
uint64_t zbuf = sx(zbufAddress(l, psm, w, h)) | (static_cast<uint64_t>(zpsm & 0xf) << 24);
if (ztest == 0)
zbuf |= 1ull << 32; // ZMSK
write64(g, p + 16, zbuf);
@@ -271,9 +274,10 @@ namespace bozo::libs
// A native port must implement readback at the original high-level caller, using its
// render target and rectangle. Do not parse a console packet or fabricate a black image.
void execStoreImage(Libraries &, Guest &)
void execStoreImage(Libraries &, Guest &g)
{
throw std::runtime_error("sceGsExecStoreImage requires a native renderer readback override");
throw std::runtime_error(std::format("sceGsExecStoreImage requires a native renderer readback override "
"(called from 0x{:x})", g.context()->r[31].uw[0]));
}
// sceGsSyncV(mode): the game waits for the next vblank, and the game frame ends there. The reference returns
+1 -1
View File
@@ -103,7 +103,7 @@ namespace bozo::libs
throw state::Error("save state: " + why);
a.begin("libraries");
heap.serialize(a);
fields(a, rand.state, framesEnded, dmaEnvironment);
fields(a, rand.state, framesEnded, dmaEnvironment, gsVideo);
a.begin("waits");
uint64_t n = state::count(a, continuations_.size());
if (!a.loading())
+13
View File
@@ -889,6 +889,19 @@ int main()
m.call("sqrt", {});
eq("software-double sqrt in v0", static_cast<long long>(m.ctx.r[2].ud[0] == std::bit_cast<uint64_t>(1.5)), 1);
}
{
// sceGsResetGraph's display mode stays the library's: the game may reuse scratchpad 0x100 (where the reference
// also leaves it) before sceGsSetDefDispEnv (TMB from its fourth frame). NTSC interlaced, field mode off.
Machine m;
m.call("sceGsResetGraph", {0, 1, 2, 0});
uint8_t *scratch = m.rt->memory.flat() + bozo::GuestMemory::flatOffset(bozo::kScratchpadBase + 0x100u, 4);
eq("ResetGraph leaves its parameters at scratchpad 0x100", scratch[0] == 1 && scratch[1] == 2 && scratch[3] == 3, 1);
std::memset(scratch, 0, 4);
m.call("sceGsSetDefDispEnv", {0x100000, 0, 640, 448, 0, 0});
uint64_t smode2 = 0;
std::memcpy(&smode2, m.rt->memory.ram() + 0x100008, 8);
eq("SetDefDispEnv's SMODE2 after the game reused the scratchpad", static_cast<long long>(smode2), 1);
}
{
// Native graphics must reject the retired graphics paths without blocking movie input, T0 or VU0 data.
for (uint32_t address : {0x10003000u, 0x10003C00u, 0x10008000u, 0x10009030u, 0x1000A000u,