Files
Thomas MartinandClaude Opus 5.5 68ab220fc5 Settings inside the game: L1 / R1 pages in the OPTION screen
The game's own OPTION screen (the title's, and SELECT's in play) turns
with L1 / R1 to the port's pages: Graphics (display mode, resolution,
render scale, anti-aliasing, texture filter, frame rate, widescreen;
APPLY saves video.ini), Cheats (and the room warp), Save States (the
F5 / F7 slots, save and load) and Close Game. They are drawn the way
Options draws itself: its backdrop, its orbs, arrows and highlight bar,
and the game's message font with Prop_tbl's widths, through the native
2D (a new oni::sprite::overlay drawn at Pit_trans's start, under the
fades). Overrides of syou_init, syou_move, sprite_disp and mess_disp;
nothing written to the game's memory, no save-state section added.

BozoEngine 5d34d41 (#151): captureStatePicture gives an in-game save
its slot picture in F2's Save states tab.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-26 20:40:20 +01:00

471 lines
23 KiB
C++

// Onimusha: Warlords on BozoEngine: the headless runner (oni-headless) and the window (onimusha).
// The headless runner prints what is not native yet: the end reason, unknown device accesses by site, library calls by
// name (ONI_CALL_STATS=1). Each stop is the next thing to name or answer.
#include "bozo/app/frame_pacer.h"
#include "bozo/audio/wav.h"
#include "bozo/ee/dispatcher.h"
#include "bozo/host/free_host.h"
#include "bozo/port.h"
#include "oni_functions.h"
#include "oni_bot.h"
#include "oni_cheats.h"
#include "oni_vu1.h"
#include "oni_port.h"
#include "oni_display.h"
#include "oni_menu.h"
#include "oni_render.h"
#include <algorithm>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <filesystem>
#include <string>
#include <vector>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#endif
#ifdef ONI_WITH_WINDOW
#include "bozo/app/app.h"
#include "bozo/app/audio_device.h"
#include "bozo/app/device_pad.h"
#include "bozo/app/window.h"
#include "bozo/app/video_settings.h"
#endif
namespace
{
// The folder of this executable ("" when unknown).
std::filesystem::path executableFolder()
{
#ifdef _WIN32
std::wstring buffer(32768, L'\0');
const DWORD n = GetModuleFileNameW(nullptr, buffer.data(), static_cast<DWORD>(buffer.size()));
if (n == 0 || n >= buffer.size())
return {};
buffer.resize(n);
return std::filesystem::path(buffer).parent_path();
#else
std::error_code ec;
const std::filesystem::path self = std::filesystem::read_symlink("/proc/self/exe", ec);
return ec ? std::filesystem::path() : self.parent_path();
#endif
}
// A game folder staged by tools/oni_build.py (the release's launcher builds it on the player's machine) holds what
// the build's absolute paths name in a developer's tree: the prepared ELF (oni.elf) and disc.iso beside the
// executable. Each is taken from there when present, so the folder can be moved; --elf and --iso still come first.
// Only a folder the stage step made (it writes oni_build.json): a developer's build folder is left as it is.
void useStagedFolder(bozo::host::FreeOptions &options)
{
const std::filesystem::path here = executableFolder();
std::error_code ec;
if (here.empty() || !std::filesystem::exists(here / "oni_build.json", ec))
return;
if (std::filesystem::exists(here / "oni.elf", ec))
options.elf = (here / "oni.elf").string();
if (std::filesystem::exists(here / "disc.iso", ec))
options.iso = (here / "disc.iso").string();
}
} // namespace
int main(int argc, char **argv)
{
bozo::host::FreeOptions options;
options.framePeriodCycles = 294912000 / 60;
bool window = false;
#ifdef ONI_WITH_WINDOW
window = true;
bozo::app::setIdentity({"Onimusha: Warlords", "Onimusha_Recomp", "onimusha-recomp", "SLUS_200.18"});
bozo::app::WindowOptions windowOptions;
windowOptions.title = "Onimusha: Warlords - BozoEngine";
windowOptions.width = 960;
windowOptions.height = 720;
#endif
options.elf = ONI_DEFAULT_ELF;
options.iso = ONI_DEFAULT_ISO;
useStagedFolder(options); // (a release's game folder: oni.elf and disc.iso beside the executable)
options.frames = window ? 0 : 600;
options.printGameOutput = true;
// CpEEInit_RpcClient busy-waits (CpEEWait, no syscall) for Capcom's driver to register its RPC servers before
// the first frame: the IOP modules must advance while no frame ends.
options.advanceLibrariesWhileStalled = true;
std::string wav, dumpRam;
for (int i = 1; i < argc; ++i)
{
const std::string a = argv[i];
if (a == "--frames" && i + 1 < argc)
options.frames = std::stoul(argv[++i]);
else if (a == "--pad-script" && i + 1 < argc)
options.padScript = argv[++i];
else if (a == "--card-dir" && i + 1 < argc)
options.cardDir = argv[++i];
else if (a == "--out" && i + 1 < argc)
options.out = argv[++i];
else if (a == "--elf" && i + 1 < argc)
options.elf = argv[++i];
else if (a == "--iso" && i + 1 < argc)
options.iso = argv[++i];
else if (a == "--wav" && i + 1 < argc)
wav = argv[++i];
else if (a == "--headless")
window = false;
else if (a == "--profile-io")
options.profileIo = true;
else if (a == "--quiet")
options.printGameOutput = false;
else if (a == "--dump-ram" && i + 1 < argc)
dumpRam = argv[++i];
else if (a == "--load-state" && i + 1 < argc)
options.loadState = argv[++i];
else if (a == "--save-state" && i + 1 < argc)
options.saveState = argv[++i];
else if (a == "--save-state-at" && i + 1 < argc)
options.saveStateFrame = std::stoul(argv[++i]);
#ifdef ONI_WITH_WINDOW
else if (a == "--no-vsync")
windowOptions.vsync = false;
else if (a == "--hidden") // no window on the desktop and no sound: captures from the offscreen picture
windowOptions.hidden = true;
else if (a == "--fullscreen")
windowOptions.fullscreen = true;
else if (a == "--capture-dir" && i + 1 < argc)
windowOptions.captureDir = argv[++i];
else if (a == "--capture-frame" && i + 1 < argc)
windowOptions.captureFrames.push_back(std::stoull(argv[++i]));
#endif
else
{
std::fprintf(stderr, "Usage: onimusha [--frames N] [--headless] [--elf FILE] [--iso FILE] [--card-dir DIR]\n"
" [--pad-script FILE] [--wav FILE] [--out FILE] [--profile-io] [--quiet]\n"
" [--dump-ram FILE] [--load-state FILE] [--save-state FILE --save-state-at N]\n"
" [--fullscreen] [--no-vsync] [--hidden]\n");
return a == "--help" ? 0 : 2;
}
}
options.realTime = window;
#ifdef ONI_WITH_WINDOW
// The memory cards of a played game live in the user folder (%APPDATA%\Onimusha_Recomp: mc0, mc1), so a save
// outlives the session without --card-dir. Headless and scripted runs (a pad script, the story bot, a hidden
// window's captures) keep fresh cards in memory unless given --card-dir, so a test never touches the player's
// saves; ONI_MEMORY_CARDS=1 keeps them in memory for a played game too.
if (window && !windowOptions.hidden && options.padScript.empty() && !std::getenv("ONI_BOT") && options.cardDir.empty() &&
!std::getenv("ONI_MEMORY_CARDS") && !bozo::app::userDirectory().empty())
{
options.cardDir = bozo::app::userDirectory().string();
std::printf("[oni] memory cards in %s\n", options.cardDir.c_str());
}
#endif
options.gameTag = "SLUS-20018"; // a save state loads only into Onimusha
bozo::host::FreeHost host(options);
bozo::ee::Dispatcher d(host);
oni::installAudio(host.libraries(), d.runtime().memory.ram());
// libmpeg for Sony's ezmpeg player (movie.c): nothing written at Twisted Metal: Black's addresses, the SDK's results;
// each picture held until its fields are due; the stream demuxed with the string callbacks (no Nodata input).
host.libraries().mpegOptions = {.referenceLayout = false, .paced = true, .sdkNodata = false};
d.setFunctions(oni_function_table, oni_function_count, oni_resume_table, oni_resume_count);
bozo::port::Registry registry;
oni::install(d.runtime(), registry);
oni::menu::install(registry); // the port's pages in the game's OPTION screen
oni::installState(*host.stateSections());
oni::vu1::setThreaded(window); // the models' chains on a thread of their own in real time
std::string error;
if (!host.setUp(d, oni_external_table, oni_external_count, &registry, error))
{
std::fprintf(stderr, "%s\n", error.c_str());
return 2;
}
const size_t modules = oni::registerModules(d.runtime());
std::printf("[oni] %zu overlay modules registered\n", modules);
bozo::app::FramePacer pacer;
pacer.setPaced(window);
// ONI_THREADS_AT=<frame>: every thread's state and pc at that frame (where a loop spins).
const uint64_t threadsAt = std::getenv("ONI_THREADS_AT") ? std::strtoull(std::getenv("ONI_THREADS_AT"), nullptr, 0) : 0;
host.onFrame = [&](uint64_t f, const R5900Context *ctx)
{
if (threadsAt && f == threadsAt)
for (const auto &[id, t] : d.kernel().threads())
std::printf("[oni:threads] %d state=%u wait=%u object=%u entry=%x pc=%x %s\n", id, unsigned(t->state),
t->waitReason, t->waitObject, t->entry, t->frames.empty() ? 0 : t->frames.front()->ctx.pc,
host.functionAt(t->frames.empty() ? 0 : t->frames.front()->ctx.pc).c_str());
// ONI_SCE_TRACE=<n>: every n frames, the event scripts' tasks (10 at 0x28e860, 0x160 apart: +1 active, +4 the
// bytecode pc; the opcode's handler from the table at 0x1f4060).
static const uint64_t sceEvery = std::getenv("ONI_SCE_TRACE") ? std::strtoull(std::getenv("ONI_SCE_TRACE"), nullptr, 0) : 0;
if (sceEvery && f % sceEvery == 0)
{
const uint8_t *ram = d.runtime().memory.ram();
// cd_work 0x3dc5c0 (struct _CD_W): what Wsleeping waits on.
constexpr uint32_t cd = 0x3dc5c0;
std::printf("[oni:sce] frame %llu cd: read_flag %d r_no %u,%u,%u voice queue %u..%u bg_req %u bg_stck %u "
"stream flag %u read %u\n",
(unsigned long long)f, static_cast<int8_t>(ram[cd + 0x27]), ram[cd + 0x24], ram[cd + 0x25],
ram[cd + 0x26], ram[cd + 0x176], ram[cd + 0x177], ram[cd + 0xbd], ram[cd + 0xbe], ram[cd + 0xba],
ram[cd + 0xbb]);
// gTransADS 0x3dbbc0: the sound driver's ADS (voice stream) status, its RPC reply (bit 0: busy).
std::printf("[oni:sce] frame %llu ads:", (unsigned long long)f);
for (int k = 0; k < 8; ++k)
{
uint32_t v;
std::memcpy(&v, ram + 0x3dbbc0 + 4 * k, 4);
std::printf(" %08x", v);
}
std::printf("\n");
for (uint32_t i = 0; i < 10; ++i)
{
const uint32_t task = 0x28e860 + i * 0x160;
if (!ram[task + 1])
continue;
uint32_t pc, handler;
std::memcpy(&pc, ram + task + 4, 4);
const uint8_t op = ram[pc & 0x1ffffff];
std::memcpy(&handler, ram + 0x1f4060 + op * 4, 4);
std::printf("[oni:sce] frame %llu task %u pc %x op %02x %s bytes", (unsigned long long)f, i, pc, op,
host.functionAt(handler).c_str());
for (int k = 0; k < 8; ++k)
std::printf(" %02x", ram[(pc + k) & 0x1ffffff]);
std::printf("\n");
}
}
// ONI_CUT_TRACE=1: the stage, room and cut (0x2875e0 / e2 / e4) and the cut's background movies (0x28287d) as
// they change.
static const bool cutTrace = std::getenv("ONI_CUT_TRACE") != nullptr;
if (cutTrace)
{
const uint8_t *ram = d.runtime().memory.ram();
int16_t place[3];
std::memcpy(place, ram + 0x2875e0, sizeof place);
const uint32_t key = static_cast<uint32_t>(place[0] & 0xff) << 24 | static_cast<uint32_t>(place[1] & 0xff) << 16 |
static_cast<uint32_t>(place[2] & 0xff) << 8 | ram[0x28287d];
static uint32_t last = ~0u;
if (key != last)
std::printf("[oni:cut] frame %llu stage %d room %d cut %d, %u background movies\n", (unsigned long long)f,
place[0], place[1], place[2], ram[0x28287d]);
last = key;
}
// ONI_PLAYER_TRACE=<n>: every n frames, the player's position (_OCW 0x287ed0: Pos +0x70, Dir_y +0xc2), life,
// the game mode and the place.
static const uint64_t playerEvery = std::getenv("ONI_PLAYER_TRACE") ? std::strtoull(std::getenv("ONI_PLAYER_TRACE"), nullptr, 0) : 0;
if (playerEvery && f % playerEvery == 0)
{
const uint8_t *ram = d.runtime().memory.ram();
float pos[3];
int16_t dir, life, place[3];
std::memcpy(pos, ram + 0x287ed0 + 0x70, sizeof pos);
std::memcpy(&dir, ram + 0x287ed0 + 0xc2, 2);
std::memcpy(&life, ram + 0x287ed0 + 0x6d0, 2);
std::memcpy(place, ram + 0x2875e0, sizeof place);
std::printf("[oni:player] frame %llu place %d:%d:%d mode %u pos %.0f %.0f %.0f dir %d life %d\n", (unsigned long long)f,
place[0], place[1], place[2], ram[0x27de30 + 0x44b0], pos[0], pos[1], pos[2], dir, life);
}
// ONI_WATCH=<addr>[,<addr>...]: a 32-bit word of the game's RAM each, logged when it changes (at frame ends).
static const std::vector<uint32_t> watch = []
{
std::vector<uint32_t> out;
if (const char *s = std::getenv("ONI_WATCH"))
for (char *end = nullptr; *s; s = *end ? end + 1 : end)
out.push_back(static_cast<uint32_t>(std::strtoul(s, &end, 16)) & 0x1fffffcu);
return out;
}();
if (!watch.empty())
{
static std::vector<uint32_t> last(watch.size(), 0xdeadbeefu);
const uint8_t *ram = d.runtime().memory.ram();
for (size_t k = 0; k < watch.size(); ++k)
{
uint32_t v;
std::memcpy(&v, ram + watch[k], 4);
if (v != last[k])
std::printf("[oni:watch] frame %llu %06x = %08x\n", (unsigned long long)f, watch[k], v);
last[k] = v;
}
}
// ONI_FADE_TRACE=1: fade.c's Fade_w (0x27c370: active, out, status, type, ...) and the game mode as they change.
static const bool fadeTrace = std::getenv("ONI_FADE_TRACE") != nullptr;
if (fadeTrace)
{
const uint8_t *ram = d.runtime().memory.ram();
uint64_t key;
std::memcpy(&key, ram + 0x27c370, 8);
uint32_t stop;
std::memcpy(&stop, ram + 0x27de30 + 0x4a60, 4); // Stop_flg[0]
key ^= static_cast<uint64_t>(ram[0x27de30 + 0x44b0]) << 56 ^ static_cast<uint64_t>(stop) * 0x9e3779b97f4a7c15ull;
static uint64_t last = ~0ull;
if (key != last)
std::printf("[oni:fade] frame %llu stop %08x mode %u fade %02x %02x %02x %02x %02x %02x %02x %02x\n", (unsigned long long)f, stop,
ram[0x27de30 + 0x44b0], ram[0x27c370], ram[0x27c371], ram[0x27c372], ram[0x27c373],
ram[0x27c374], ram[0x27c375], ram[0x27c376], ram[0x27c377]);
last = key;
}
oni::bot::frame(d.runtime().memory.ram(), f); // the story bot's next input (ONI_BOT)
if (oni::menu::takeQuitRequest()) // OPTION's Close Game page
host.requestStop();
// Widescreen (oni_display.h): the next frame's zoom from the game's mode, the window's crop with it.
oni::display::frame(d.runtime().memory.ram(), oni::movie::shown() != nullptr);
#ifdef ONI_WITH_WINDOW
bozo::app::setPictureCrop(oni::display::zoomed() ? 16.0f / 9.0f : 0.0f);
#endif
oni::cheats::apply(d.runtime().memory.ram()); // the frame boundary: the game thread
pacer.frameEnd();
// ONI_AUDIO_STATS: the frame rate every 120 frames, on stderr beside the sound device's lines.
static const bool rateStats = std::getenv("ONI_AUDIO_STATS") != nullptr;
if (rateStats && f % 120 == 0)
{
static auto last = std::chrono::steady_clock::now();
const auto now = std::chrono::steady_clock::now();
std::fprintf(stderr, "[oni:rate] frame %llu: %.1f fps\n", (unsigned long long)f,
120.0 / std::chrono::duration<double>(now - last).count());
last = now;
}
if (f % 60 == 0)
std::printf("[oni] frame=%llu pc=%x cycles=%llu\n", (unsigned long long)f, ctx->pc,
(unsigned long long)d.now());
};
bozo::audio::WavWriter recording;
if (!wav.empty() && !recording.open(wav))
{
std::fprintf(stderr, "cannot open WAV %s\n", wav.c_str());
return 2;
}
#ifdef ONI_WITH_WINDOW
oni::cheats::registerMenu();
bozo::app::DevicePad devices;
// ONI_AUDIO_STATS=1: the sound device's queue, underruns and drops every 2 s on stderr.
bozo::app::AudioOptions audioOptions;
audioOptions.stats = std::getenv("ONI_AUDIO_STATS") != nullptr;
bozo::app::AudioDevice audio(audioOptions);
if (window && options.padScript.empty())
host.libraries().pad.input = &devices;
#endif
oni::bot::start(host); // ONI_BOT=<script>: the story bot as player 1's controller
host.onAudio = [&](uint64_t, const int16_t *pcm, size_t count)
{
if (recording.isOpen())
recording.write(pcm, count);
#ifdef ONI_WITH_WINDOW
if (window)
{
// A hidden window's device (ONI_AUDIO_STATS) gets silence of the same length: its clock, queue and
// underruns as a played game's, nothing heard. (SDL's dummy driver keeps no real clock: it took samples
// faster than 48 kHz and counted underruns that were its own.)
if (windowOptions.hidden)
{
static std::vector<int16_t> silence;
silence.assign(count * 2, 0); // (count: stereo frames)
audio.push(silence.data(), count);
}
else
audio.push(pcm, count);
}
#endif
};
auto run = [&]()
{
#ifdef ONI_WITH_WINDOW
if (window)
{
// A hidden window plays no sound; with ONI_AUDIO_STATS it opens the device and gives it silence (below):
// a scripted run's underruns counted on the real device's clock, nothing heard.
if (!windowOptions.hidden || std::getenv("ONI_AUDIO_STATS"))
audio.open();
}
#endif
uint64_t frames = 0;
try
{
frames = host.run();
}
catch (const std::exception &e)
{
std::fprintf(stderr, "[oni] native failure: %s\n", e.what());
return 1;
}
if (!dumpRam.empty())
if (FILE *f = std::fopen(dumpRam.c_str(), "wb"))
{
std::fwrite(d.runtime().memory.ram(), 1, 0x2000000u, f);
std::fclose(f);
}
std::printf("[oni] frames=%llu end=%s\n", (unsigned long long)frames, host.endReason().c_str());
const auto &stats = host.stats();
std::printf("[oni] compatibility: %llu register accesses, %llu unknown; %llu native VU0 calls\n",
(unsigned long long)stats.ioAccesses, (unsigned long long)stats.ioUnknown,
(unsigned long long)stats.vu0Calls);
std::printf("[oni] calls: %llu kernel, %llu library, %llu port overrides\n",
(unsigned long long)stats.kernelCalls, (unsigned long long)stats.libraryCalls,
(unsigned long long)stats.overrideCalls);
for (const auto &[name, count] : stats.unanswered)
std::printf("[oni:unanswered] %10llu %s\n", (unsigned long long)count, name.c_str());
if (std::getenv("ONI_CALL_STATS"))
{
std::vector<std::pair<uint64_t, std::string>> byName;
for (const auto &[name, count] : stats.libraryByName)
byName.emplace_back(count, name);
std::sort(byName.rbegin(), byName.rend());
for (const auto &[count, name] : byName)
std::printf("[oni:lib] %10llu %s\n", (unsigned long long)count, name.c_str());
}
for (const auto &[site, counts] : stats.ioSites)
std::printf("[oni:io] pc=%08x address=%08x reads=%llu writes=%llu %s\n", site.first, site.second,
(unsigned long long)counts[0], (unsigned long long)counts[1],
host.functionAt(site.first).c_str());
return host.endReason().rfind("FAULT:", 0) == 0 ? 1 : 0;
};
int result;
#ifdef ONI_WITH_WINDOW
if (window)
{
// The player's video settings (video.ini in the user folder, the F2 Video tab: window size and mode, render
// scale, anti-aliasing, frame rate, texture smoothing and upscaling, texture pack) over the defaults, the
// environment over the file (PS2X_RENDER_SCALE, ...). A hidden window (a scripted run, captures) reads no file:
// the environment alone, so a player's settings never change a test's pictures. Without this the Video tab drew
// its choices and none of them was applied.
if (windowOptions.hidden && !std::getenv("PS2X_VIDEO_CONFIG"))
{
const std::string none = (std::filesystem::temp_directory_path() / "oni_no_video.ini").string();
#ifdef _WIN32
_putenv_s("PS2X_VIDEO_CONFIG", none.c_str());
#else
setenv("PS2X_VIDEO_CONFIG", none.c_str(), 1);
#endif
}
// The port's own options (onimusha.ini; ONI_WIDESCREEN over it; a hidden window: the environment alone).
oni::display::load(windowOptions.hidden ? nullptr : bozo::app::userDirectory().string().c_str());
if (oni::display::widescreen())
windowOptions.width = 1280, windowOptions.height = 720; // (the default window 16:9 then)
bozo::app::video::loadAtStartup(windowOptions.width, windowOptions.height);
if (windowOptions.fullscreen)
bozo::app::video::edited().mode = bozo::app::video::WindowMode::Borderless;
windowOptions.videoSettings = true;
bozo::app::Game game;
game.run = run;
game.stop = [&] { host.requestStop(); };
game.framesEnded = [&] { return host.framesEnded(); };
game.saveState = [&](int slot) { host.requestSave(bozo::app::stateSlotPath(slot)); };
game.loadState = [&](int slot) { host.requestLoad(bozo::app::stateSlotPath(slot)); };
// OPTION's pages: the Graphics page edits the Video tab's settings, the Save States page the F5 / F7 slots.
oni::menu::Access access;
access.videoShared = [] { return bozo::app::video::shared(); };
access.videoPost = [](const bozo::app::video::Settings &s, bool save) { bozo::app::video::post(s, save); };
access.slotSummary = [](int slot) { return bozo::app::stateSlotSummary(slot); };
access.saveSlot = [&](int slot)
{
game.saveState(slot);
bozo::app::captureStatePicture(slot); // the slot's picture in the F2 Save states tab
};
access.loadSlot = game.loadState;
access.slots = bozo::app::kStateSlots;
oni::menu::setAccess(std::move(access));
host.onStateMessage = [](bool, const std::string &message) { bozo::app::notify(message); };
result = bozo::app::runWindow(windowOptions, game, &devices);
}
else
#endif
result = run();
recording.close();
return result;
}