Files
Thomas MartinandClaude Opus 5.5 0d5dd2d33f The game's own GIF packets, read natively at their send
Every GS packet the game builds itself is now read from the structures it
was built from, as a list of register writes, instead of parsing GIF tags:
Spr_draw's sprites, Screen_blur_draw, Gifreg_set_dma's tables, scroll_disp's
rain and mist layers, oniGsMoveImage, the effects' sprite arrays (add_scr_efc,
esp_sp00), StencilMapPolygon's pond and Md_draw_semi's semi-transparent parts.
ONI_NATIVE_GIF=0 keeps the packets, =compare checks both kick by kick:
identical on all 188 story states (126,821 sends, 19.5 million kicks).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-29 16:51:10 +01:00

174 lines
9.2 KiB
C++

// Onimusha's 3D models. The game turns each model into VIF1 packets for its VU1 programs (Md_draw_entry_vu0 /
// Md_draw_entry_clip build them from the model's own data, sys_sub.c) and queues them: Md_draw_entry(tag) 0x130cc0
// appends the tag at G + 0x5bdc (G = 0x27de30) and starts the queue if it is idle; Md_tag_trans(tag) 0x130e40 starts
// VIF1's DMA on it (the busy flag 0x287222 set), and Md_tag_trans_cb 0x130da0 (the DMA end interrupt) moves the queue's
// read pointer (G + 0x5be0) on, counts it down (0x287221) and starts the next tag, or marks the queue idle (0x287220,
// 0x287221 zero) at its end.
//
// Natively Md_tag_trans runs each queued chain at once and completes the whole queue (as if every transfer had ended):
// a model Md_draw_entry_clip queued drawn from the game's own data (runtime/oni_vu1.cpp's native models,
// docs/NATIVE_3D.md), anything else on VIF1 and VU1 as runtime/oni_vu1.cpp has them (the translated microprograms).
#include "oni_render.h"
#include "bozo/ee/dispatcher.h"
#include "oni_functions.h"
#include "oni_gsnative.h"
#include "oni_vu1.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
namespace oni::model
{
namespace
{
constexpr uint32_t kG = 0x27de30;
constexpr uint32_t kQueueRead = kG + 0x5be0;
constexpr uint32_t kRunning = 0x287220, kPending = 0x287221, kBusy = 0x287222;
uint32_t read32(const uint8_t *ram, uint32_t a)
{
uint32_t v;
std::memcpy(&v, ram + (a & 0x1ffffffu), 4);
return v;
}
void write32(uint8_t *ram, uint32_t a, uint32_t v) { std::memcpy(ram + (a & 0x1ffffffu), &v, 4); }
// Md_tag_trans(tag): this tag and every one queued after it "transferred" at once: what Md_tag_trans_cb does
// for each.
void tagTrans(uint8_t *ram, R5900Context *ctx, Runtime *rt)
{
uint8_t *spr = bozo::port::scratchpad(rt);
oni::vu1::runChain(ram, spr, ctx->r[4].uw[0]);
ram[kBusy] = 0;
for (int guard = 0; guard < 4096; ++guard)
{
const uint32_t read = read32(ram, kQueueRead) + 4;
write32(ram, kQueueRead, read);
ram[kPending] = static_cast<uint8_t>(ram[kPending] - 1);
const uint32_t next = read32(ram, read);
if (next == 0)
{
ram[kRunning] = 0;
ram[kPending] = 0;
break;
}
oni::vu1::runChain(ram, spr, next);
}
ctx->pc = ctx->r[31].uw[0];
}
// Gifreg_set_dma(pD, length): its table read natively (runtime/oni_gsnative.cpp) for the send it makes, then the
// game's own (it still builds and sends the packet: the send then draws the table).
std::vector<oni::native3d::GsOp> g_gifregOps;
bool g_gifregRead = false;
void gifregSetDma(uint8_t *ram, R5900Context *ctx, Runtime *rt)
{
g_gifregOps.clear();
g_gifregRead = oni::gsnative::registers(ram, ctx->r[4].uw[0], ctx->r[5].uw[0], g_gifregOps);
Gifreg_set_dma_0x12e6d0(ram, ctx, rt);
g_gifregRead = false;
}
// oniGsMoveImage(sbp, sbw, spsm, dbp, dbw, dpsm, sx, sy, dx, dy, w, h, dir): its arguments read natively
// (runtime/oni_gsnative.cpp) for the send it makes, then the game's own.
std::vector<oni::native3d::GsOp> g_moveOps;
bool g_moveRead = false;
void gsMoveImage(uint8_t *ram, R5900Context *ctx, Runtime *rt)
{
const uint32_t sp = ctx->r[29].uw[0];
auto stack = [&](uint32_t k) { int16_t v; std::memcpy(&v, ram + ((sp + 8 * k) & 0x1ffffffu), 2); return v; };
const int16_t args[13] = {static_cast<int16_t>(ctx->r[4].uw[0]), static_cast<int16_t>(ctx->r[5].uw[0]),
static_cast<int16_t>(ctx->r[6].uw[0]), static_cast<int16_t>(ctx->r[7].uw[0]),
static_cast<int16_t>(ctx->r[8].uw[0]), static_cast<int16_t>(ctx->r[9].uw[0]),
static_cast<int16_t>(ctx->r[10].uw[0]), static_cast<int16_t>(ctx->r[11].uw[0]),
stack(0), stack(1), stack(2), stack(3), stack(4)};
g_moveOps.clear();
oni::gsnative::moveImage(args, g_moveOps);
g_moveRead = true;
oniGsMoveImage_0x1c1140(ram, ctx, rt);
g_moveRead = false;
}
// Md_draw_entry_clip(pCw): the model's tag (its work's +0x60) noted with its OCW, then the game's own.
void drawEntryClip(uint8_t *ram, R5900Context *ctx, Runtime *rt)
{
const uint32_t ocw = ctx->r[4].uw[0];
oni::vu1::noteModel(read32(ram, ocw + 0xa0), ocw);
Md_draw_entry_clip_0x1316e0(ram, ctx, rt);
}
} // namespace
// sceDmaSend(channel, tag): the chains the game sends itself (sprites, effects, the screen blur, the background's
// own); BozoEngine's libdma completes them without reading them (native graphics). ONI_DMA_TRACE=1 lists them.
void dmaSend(uint8_t *ram, R5900Context *ctx, Runtime *rt)
{
static const bool trace = std::getenv("ONI_DMA_TRACE") != nullptr;
if (trace)
{
const uint32_t handle = ctx->r[4].uw[0], tag = ctx->r[5].uw[0];
std::printf("[oni:dma] sceDmaSend channel %x tag %x from %x\n", handle, tag, ctx->r[31].uw[0]);
}
// The GIF's chains drawn natively, except the background's (Bg_disp draws the picture itself).
static const bool gif = [] { const char *e = std::getenv("ONI_GIF_CHAINS"); return !(e && *e == '0'); }();
const uint32_t from = ctx->r[31].uw[0];
if (gif && ctx->r[4].uw[0] == 0x1000a000u && !(from >= 0x122040u && from < 0x122188u))
{
// The sends read natively at the send (runtime/oni_gsnative.cpp): Spr_draw's, Screen_blur_draw's,
// Gifreg_set_dma's, scroll_disp's, oniGsMoveImage's, Esp_sp_draw's (the effects' sprite arrays).
std::vector<oni::native3d::GsOp> native;
bool read = (from == 0x180b5cu && oni::gsnative::sprites(ram, native)) ||
(from == 0x18ae74u && oni::gsnative::blur(ram, native)) ||
(from == 0x145fdcu && oni::gsnative::scroll(ram, native));
if (from == 0x12e7a4u && g_gifregRead)
native = std::move(g_gifregOps), read = true;
if (from == 0x1c12e0u && g_moveRead)
native = std::move(g_moveOps), read = true;
if (from == 0x180de4u)
read = oni::gsnative::espSprites(ram, ctx->r[5].uw[0], native);
oni::vu1::runGifChain(ram, bozo::port::scratchpad(rt), ctx->r[5].uw[0], read ? &native : nullptr);
}
bozo::port::callLibrary(rt, ctx, 0x101c50);
}
// sceDmaSendN(channel, address, qwc): a normal-mode transfer. On the GIF's channel the game sends packets it built
// itself this way: StencilMapPolygon's (a room's animated water, the background movie kept in block 0x1a40 drawn
// through the cut's polygons: stage 3 room 7's pond). BozoEngine's libdma completes it without reading it.
void dmaSendN(uint8_t *ram, R5900Context *ctx, Runtime *rt)
{
static const bool trace = std::getenv("ONI_DMA_TRACE") != nullptr;
const uint32_t handle = ctx->r[4].uw[0], address = ctx->r[5].uw[0], qwc = ctx->r[6].uw[0];
if (trace)
std::printf("[oni:dma] sceDmaSendN channel %x address %x qwc %u from %x\n", handle, address, qwc, ctx->r[31].uw[0]);
static const bool gif = [] { const char *e = std::getenv("ONI_GIF_CHAINS"); return !(e && *e == '0'); }();
if (gif && handle == 0x1000a000u && qwc > 0 && qwc < 0x10000u)
{
// StencilMapPolygon's send read natively from its arrays (runtime/oni_gsnative.cpp).
std::vector<oni::native3d::GsOp> native;
const bool read = ctx->r[31].uw[0] == 0x1b668cu && oni::gsnative::stencil(ram, address, qwc, native);
oni::vu1::runGifData(ram, bozo::port::scratchpad(rt), address, qwc, read ? &native : nullptr);
}
bozo::port::callLibrary(rt, ctx, 0x101d28);
}
// ONI_CD_TRACE=1: every sceCdRead (sector, count, buffer, caller).
void cdRead(uint8_t *ram, R5900Context *ctx, Runtime *rt)
{
std::fprintf(stderr, "[oni:cd] sceCdRead lsn %u sectors %u buffer %x from %x\n", ctx->r[4].uw[0], ctx->r[5].uw[0],
ctx->r[6].uw[0], ctx->r[31].uw[0]);
bozo::port::callLibrary(rt, ctx, 0x1121c0);
}
void install(bozo::port::Registry &r)
{
if (std::getenv("ONI_CD_TRACE"))
r.registerFunction(0x1121c0, cdRead); // sceCdRead
r.registerFunction(0x101c50, dmaSend); // sceDmaSend
r.registerFunction(0x101d28, dmaSendN); // sceDmaSendN
r.registerFunction(0x130e40, tagTrans); // Md_tag_trans
r.registerFunction(0x1316e0, drawEntryClip); // Md_draw_entry_clip: each model's tag and OCW
r.registerFunction(0x12e6d0, gifregSetDma); // Gifreg_set_dma: its table read natively
r.registerFunction(0x1c1140, gsMoveImage); // oniGsMoveImage: its arguments read natively
}
} // namespace oni::model