Files
901ea27228 Diagnostics read once (SBC_ENV): the game thread 2.9x faster; M_19_02's black screen explained (#16)
Seventeen diagnostic switches called getenv per draw or per frame; on Windows
getenv takes the environment lock and scans it: 64% of the game thread on
M_19_03. SBC_ENV reads each once (6.0 s -> 2.1 s for 300 frames there).

M_19_02 stays black because, warped to from the story's opening, it does not
fit in memory: 7.4 MB of its DAT spill into the main heap and MapInitSystem's
pools come out empty ("MemblkInit: Memory Alloc Error !"), the task pool among
them, so the map script (player, fade-in) never starts. The game's own
allocations over the same files; notes in docs/RECOMP_NOTES.md.

SBC_TRACE_ENTRANCES lists a map's places; SBC_TRACE_PRINTF / LOADS / TEXREAD no
longer need SBC_PROBE.

Co-authored-by: Thomas Martin <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
2026-11-30 18:29:58 +01:00

330 lines
16 KiB
C++

// Steambot Chronicles' frame, native: the end of each frame's DMA and the frame boundary.
//
// The game's frame (docs/RECOMP_NOTES.md, "Drawing"): its draw functions fill 128 packet buckets (0x5d5180, 0x80 apart).
// At the frame's end 0x264340 waits for the previous frame (0x264960 sleeps until the pending bits at gp-0x7c98 are
// 0), swaps the buffers (0x2648c0), adds a GS FINISH to the master list, takes the frame semaphore (gp-0x7e74), sets
// pending bit 4 and calls the kick 0x25efb0. The kick links the buckets into one chain of DMA `call` tags and starts
// VIF1's chain with TTE: CHCR = 0x145 at 0x25f0dc. On the console the GS's FINISH interrupt (INTC handler 0x264700)
// then clears bit 4 and signals the semaphore; the vblank handler 0x2644c0 clears bit 0 once its field count is
// reached and wakes the main thread when no bit is left.
//
// There is no DMAC, VIF or GS here: the CHCR store is a native site that takes the chain and answers the FINISH at
// once, as the handler would, and ends the host's frame.
#include "sbc_port.h"
#include "bozo/ee/kernel.h"
#include "bozo/port.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
namespace sbc::render
{
namespace
{
uint8_t *g_ram = nullptr;
uint32_t rd32(uint32_t a)
{
uint32_t v;
std::memcpy(&v, g_ram + (a & 0x1FFFFFFu), 4);
return v;
}
uint64_t rd64(uint32_t a)
{
uint64_t v;
std::memcpy(&v, g_ram + (a & 0x1FFFFFFu), 8);
return v;
}
void wr32(uint32_t a, uint32_t v) { std::memcpy(g_ram + (a & 0x1FFFFFFu), &v, 4); }
void wr8(uint32_t a, uint8_t v) { g_ram[a & 0x1FFFFFFu] = v; }
// The scheduler's words, from the game's $gp.
struct Words
{
uint32_t gp;
uint32_t pending() const { return gp - 0x7c98u; } // bit 0 the field count, 1 the alarm, 2 the GS
uint32_t semaphore() const { return gp - 0x7e74u; } // the frame semaphore's id
uint32_t mainThread() const { return gp - 0x7c6cu; }
uint32_t finishState() const { return gp - 0x7ca8u; } // 2 already signalled; 1 / 3 the vblank watchdog signalled (a missed FINISH)
uint32_t graphics() const { return gp - 0x7c70u; } // the graphics state (its +0x240: woken)
};
uint64_t g_frames = 0;
// The GS's privileged registers the game writes (PMODE 0x00 ... BGCOLOR 0xe0), by index (address bits 4-7):
// the display for the native picture.
uint64_t g_gs[16] = {};
// The instruction at a native site: what it loads or stores, from where.
struct Access
{
uint32_t op, rt, address;
bool load() const { return op != 0x28u && op != 0x29u && op != 0x2bu && op != 0x3fu && op != 0x1fu; }
};
Access decode(const R5900Context *ctx, uint32_t pc)
{
const uint32_t word = rd32(pc);
const int32_t offset = static_cast<int16_t>(word & 0xFFFFu);
return {word >> 26, (word >> 16) & 31u, ctx->r[(word >> 21) & 31u].uw[0] + static_cast<uint32_t>(offset)};
}
// A load answered with `value`, extended as the instruction would.
void answer(R5900Context *ctx, const Access &a, uint64_t value)
{
if (!a.rt)
return;
switch (a.op)
{
case 0x23u: ctx->r[a.rt].sd[0] = static_cast<int32_t>(value); break; // lw
case 0x27u: ctx->r[a.rt].ud[0] = static_cast<uint32_t>(value); break; // lwu
default: ctx->r[a.rt].ud[0] = value; break; // ld
}
}
// A source chain with TTE: each piece of the VIF stream handed to `take` in order (the tag's upper 64 bits, two
// VIF codes, then its data). Tags refe 0, cnt 1, next 2, ref 3, refs 4, call 5, ret 6, end 7; two call levels.
template <class Take> bool walk(uint32_t tadr, Take &&take)
{
uint32_t stack[2] = {}, depth = 0;
for (int guard = 0; guard < 1000000; ++guard)
{
const uint64_t tag = rd64(tadr);
const uint32_t qwc = static_cast<uint32_t>(tag & 0xFFFFu), id = static_cast<uint32_t>(tag >> 28) & 7u;
const uint32_t addr = static_cast<uint32_t>(tag >> 32) & 0x0FFFFFF0u;
take(tadr + 8, 8u);
switch (id)
{
case 0: take(addr, qwc * 16); return true; // refe
case 1: take(tadr + 16, qwc * 16); tadr += 16 + qwc * 16; break; // cnt
case 2: take(tadr + 16, qwc * 16); tadr = addr; break; // next
case 3: case 4: take(addr, qwc * 16); tadr += 16; break; // ref, refs
case 5: // call
take(tadr + 16, qwc * 16);
if (depth >= 2)
return false;
stack[depth++] = tadr + 16 + qwc * 16;
tadr = addr;
break;
case 6: // ret
take(tadr + 16, qwc * 16);
if (!depth)
return true;
tadr = stack[--depth];
break;
default: take(tadr + 16, qwc * 16); return true; // end
}
}
return false;
}
// The frame's VIF stream as one run of bytes (SBC_TRACE_KICK: what it holds).
std::vector<uint8_t> stream(uint32_t chain, bool &complete)
{
std::vector<uint8_t> out;
complete = walk(chain, [&](uint32_t at, uint32_t bytes) {
const uint8_t *p = g_ram + (at & 0x1FFFFFFu);
out.insert(out.end(), p, p + bytes);
});
return out;
}
// The VIF codes of a stream, counted by command (UNPACK as one), with DIRECT's and MPG's sizes.
struct VifTally
{
uint32_t commands[0x80] = {};
uint64_t directQw = 0, mpgPairs = 0, unpackWords = 0;
bool broken = false;
};
VifTally tally(const std::vector<uint8_t> &s)
{
VifTally v;
uint32_t cl = 1, wl = 1;
size_t at = 0;
auto word = [&](size_t i) { uint32_t w; std::memcpy(&w, s.data() + i, 4); return w; };
while (at + 4 <= s.size())
{
const uint32_t code = word(at), cmd = (code >> 24) & 0x7Fu, imm = code & 0xFFFFu, num = (code >> 16) & 0xFFu;
at += 4;
if (cmd >= 0x60u)
{
++v.commands[0x60];
const uint32_t vn = (cmd >> 2) & 3u, vl = cmd & 3u, n = num ? num : 256u;
const uint32_t bits = vl == 3u ? 16u : (32u >> vl) * (vn + 1u);
const uint32_t elements = cl >= wl ? n : (n / wl) * cl + std::min(n % wl, cl);
const uint32_t words = (elements * bits + 31u) / 32u;
v.unpackWords += words;
at += words * 4u;
continue;
}
++v.commands[cmd];
switch (cmd)
{
case 0x00: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x10: case 0x11:
case 0x13: case 0x14: case 0x15: case 0x17: break;
case 0x01: cl = imm & 0xFFu; wl = (imm >> 8) & 0xFFu; break; // STCYCL
case 0x20: at += 4; break; // STMASK
case 0x30: case 0x31: at += 16; break; // STROW, STCOL
case 0x4A: { const uint32_t pairs = num ? num : 256u; v.mpgPairs += pairs; at += pairs * 8u; break; }
case 0x50: case 0x51: { const uint32_t qw = imm ? imm : 65536u; v.directQw += qw; at += qw * 16u; break; }
default: v.broken = true; return v;
}
}
return v;
}
// SBC_TRACE_GIF=<kick>: that frame's DIRECT GIF packets as tags, A+D writes and image sizes (a diagnostic for
// reading which of the game's functions drew what; nothing is drawn from it).
void traceGif(const std::vector<uint8_t> &s)
{
auto word = [&](size_t i) { uint32_t w; std::memcpy(&w, s.data() + i, 4); return w; };
auto dword = [&](size_t i) { uint64_t w; std::memcpy(&w, s.data() + i, 8); return w; };
uint32_t cl = 1, wl = 1;
size_t at = 0;
while (at + 4 <= s.size())
{
const uint32_t code = word(at), cmd = (code >> 24) & 0x7Fu, imm = code & 0xFFFFu, num = (code >> 16) & 0xFFu;
at += 4;
if (cmd >= 0x60u)
{
const uint32_t vn = (cmd >> 2) & 3u, vl = cmd & 3u, n = num ? num : 256u;
const uint32_t bits = vl == 3u ? 16u : (32u >> vl) * (vn + 1u);
const uint32_t elements = cl >= wl ? n : (n / wl) * cl + std::min(n % wl, cl);
at += (elements * bits + 31u) / 32u * 4u;
continue;
}
if (cmd == 0x01) { cl = imm & 0xFFu; wl = (imm >> 8) & 0xFFu; }
else if (cmd == 0x20) at += 4;
else if (cmd == 0x30 || cmd == 0x31) at += 16;
else if (cmd == 0x4A) at += (num ? num : 256u) * 8u;
else if (cmd == 0x50 || cmd == 0x51)
{
const size_t end = at + (imm ? imm : 65536u) * 16u;
std::printf("[sbc:gif] DIRECT %u qw at %zu\n", imm, at);
while (at + 16 <= end && at + 16 <= s.size())
{
const uint64_t lo = dword(at), hi = dword(at + 8);
const uint32_t nloop = lo & 0x7FFFu, flg = (lo >> 58) & 3u, nreg = (lo >> 60) ? uint32_t(lo >> 60) : 16u;
at += 16;
std::printf("[sbc:gif] tag nloop %u flg %u nreg %u eop %u pre %u prim %03llx regs %016llx\n", nloop,
flg, nreg, unsigned((lo >> 15) & 1), unsigned((lo >> 46) & 1),
static_cast<unsigned long long>((lo >> 47) & 0x7FF), static_cast<unsigned long long>(hi));
if (flg == 0)
{
for (uint32_t i = 0; i < nloop * nreg && at + 16 <= end; ++i, at += 16)
{
const uint32_t r = (hi >> (4 * (i % nreg))) & 15u;
if (r == 0xE)
std::printf("[sbc:gif] A+D %02llx = %016llx\n", static_cast<unsigned long long>(dword(at + 8) & 0xFF),
static_cast<unsigned long long>(dword(at)));
else if (i < 4 * nreg)
std::printf("[sbc:gif] r%x %016llx %016llx\n", r, static_cast<unsigned long long>(dword(at)),
static_cast<unsigned long long>(dword(at + 8)));
}
}
else if (flg == 1)
at += (nloop * nreg + 1) / 2 * 16;
else
at += nloop * 16;
}
at = end;
}
}
}
} // namespace
uint64_t frames() { return g_frames; }
bool registerSite(uint8_t *rdram, R5900Context *ctx, Runtime *rt, uint32_t pc)
{
g_ram = rdram;
const Access a = decode(ctx, pc);
const uint32_t device = a.address & 0x1FFFFFFFu;
if (device >= 0x12000000u && device < 0x12001000u)
{
if (a.load())
answer(ctx, a, g_gs[(device >> 4) & 15u]);
else
g_gs[(device >> 4) & 15u] = ctx->r[a.rt].ud[0];
return true;
}
if (device == 0x12001000u) // CSR
{
if (a.load()) // FIELD (bit 13) from the vblank count, FINISH (bit 1) done: no GS work is ever pending
answer(ctx, a, ((bozo::port::vblankCount(rt) & 1u) << 13) | 2u);
return true;
}
if (device >= 0x10008000u && device < 0x1000A040u) // the graphics DMA channels: idle, their writes dropped
{
if (a.load())
answer(ctx, a, 0);
return true;
}
return false;
}
// 0x25f0dc (the kick's delay slot): sw a0, -0x7000(v1), CHCR = 0x145. s0 is the chain's first tag.
bool kick(uint8_t *rdram, R5900Context *ctx, Runtime *rt)
{
g_ram = rdram;
const Words w{ctx->r[28].uw[0]};
const uint32_t chain = ctx->r[16].uw[0] & 0x0FFFFFF0u;
// SBC_DUMP_RAM=<kick>: guest RAM (32 MB) and $gp to build/ram_<kick>.bin at that kick, for reading the game's
// data offline (tools/).
if (const char *e = SBC_ENV("SBC_DUMP_RAM"); e && g_frames == std::strtoull(e, nullptr, 0))
{
char name[64];
std::snprintf(name, sizeof name, "build/ram_%llu.bin", static_cast<unsigned long long>(g_frames));
if (FILE *f = std::fopen(name, "wb"))
{
std::fwrite(rdram, 1, 0x2000000u, f);
std::fclose(f);
std::printf("[sbc] RAM at kick %llu to %s (gp %08x)\n", static_cast<unsigned long long>(g_frames), name, w.gp);
}
}
static const bool trace = std::getenv("SBC_TRACE_KICK") != nullptr;
if (trace)
{
bool complete = false;
const std::vector<uint8_t> s = stream(chain, complete);
const VifTally v = tally(s);
if (const char *g = SBC_ENV("SBC_TRACE_GIF"); g && g_frames == std::strtoull(g, nullptr, 0))
traceGif(s);
std::printf("[sbc:kick] frame %llu chain %08x: %zu bytes%s; DIRECT %u (%llu qw), UNPACK %u (%llu words), MSCAL %u, "
"MSCNT %u, MPG %u (%llu pairs), FLUSH %u/%u/%u, STROW %u, MSKPATH3 %u%s\n",
static_cast<unsigned long long>(g_frames), chain, s.size(), complete ? "" : " (cut)",
v.commands[0x50] + v.commands[0x51], static_cast<unsigned long long>(v.directQw), v.commands[0x60],
static_cast<unsigned long long>(v.unpackWords), v.commands[0x14] + v.commands[0x15], v.commands[0x17],
v.commands[0x4A], static_cast<unsigned long long>(v.mpgPairs), v.commands[0x10], v.commands[0x11],
v.commands[0x13], v.commands[0x30], v.commands[0x06], v.broken ? " (unknown code)" : "");
}
// The FINISH handler 0x264700, at once: bit 4 cleared, the frame semaphore signalled unless it already is,
// the main thread woken if nothing else is pending.
bozo::ee::Kernel &k = bozo::port::kernel(rt);
const uint32_t pending = rd32(w.pending()) & ~4u;
wr32(w.pending(), pending);
const int32_t semaphore = static_cast<int32_t>(rd32(w.semaphore()));
if (const bozo::ee::Semaphore *s = k.findSema(semaphore))
{
if (s->count)
wr8(w.finishState(), 2);
else
{
k.signalSema(semaphore, true);
// no state written: the handler sets one only when the semaphore was already signalled (2)
}
}
if (!pending)
{
wr32(rd32(w.graphics()) + 0x240u, 1);
const int32_t main = static_cast<int32_t>(rd32(w.mainThread()));
k.cancelWakeupThread(main, true);
k.wakeupThread(main, true);
}
++g_frames;
draw2d::publish();
bozo::port::host(rt).frameEnd(rt, ctx);
return true;
}
} // namespace sbc::render