Files
319fb191b3 Cheats: a Cheats tab (F2) with infinite HP, fuel, boost, money, never hungry, one-hit kills and position bookmarks (#12)
- runtime/sbc_cheats.cpp, applied at each frame's end on the game thread (Onimusha's oni_cheats layout): the
  Trotmobile slots (0x58f690, 0xa360 apart; HP / fuel / boost as (max, current) floats at +0x50 / +0x5c / +0x64, the
  damage at 0x1eec50), the money (0x587408), the stomach (0x587404), the player character (*(0x58f640) + 0x10 and its
  copies).
- One-hit kills keep every other Trotmobile at 1 HP, so the game's own hit and defeat follow.
- Position bookmarks save where Vanilla stands and bring her back (on foot, same area).
- runtime/app/sbc_cheats_ui.cpp: the tab (window build); SBC_CHEAT_* for runs without a window.
- Tested on the beach: money held at 9,999,999, the other slot at 1 HP, a bookmark saved at frame 8000 and restored
  at 8300.

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

295 lines
13 KiB
C++

// Steambot Chronicles on BozoEngine: the headless runner (sbc-headless) and the window (steambot).
// The headless runner prints what is not native yet: the end reason, unknown device accesses by site, library calls by
// name (SBC_CALL_STATS=1), unanswered calls. Each stop is the next thing to name or answer.
// From Amplitude_Recomp's amp_main.cpp and Onimusha_Recomp's oni_main.cpp (the overlay modules).
#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 "sbc_cheats.h"
#include "sbc_functions.h"
#include "sbc_port.h"
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <filesystem>
#include <string>
#include <vector>
#ifdef SBC_WITH_WINDOW
#include "bozo/app/app.h"
#include "bozo/app/audio_device.h"
#include "bozo/app/device_pad.h"
#include "bozo/app/video_settings.h"
#include "bozo/app/window.h"
#endif
int main(int argc, char **argv)
{
bozo::host::FreeOptions options;
options.framePeriodCycles = 294912000 / 60; // NTSC (VMODE = NTSC in SYSTEM.CNF)
bool window = false;
bool mute = false; // --mute: no sound through the speakers (hidden runs, tests, captures)
#ifdef SBC_WITH_WINDOW
window = true;
bozo::app::setIdentity({"Steambot Chronicles", "Steambot_Chronicles_Recomp", "steambot-recomp", "SLUS_213.44"});
bozo::app::WindowOptions windowOptions;
windowOptions.title = "Steambot Chronicles - BozoEngine";
windowOptions.width = 960;
windowOptions.height = 720;
// SBC_CAPTURE_FRAMES=<item>[,<item>...], each <frame> or <first>:<last>:<step>: the window's picture at those game
// frames, as frame_<n>.png in PS2X_CAPTURE_WINDOW_DIR.
if (const char *e = std::getenv("SBC_CAPTURE_FRAMES"))
for (const char *p = e; p && *p;)
{
unsigned long long first, last, step;
if (std::sscanf(p, "%llu:%llu:%llu", &first, &last, &step) == 3 && step > 0)
for (unsigned long long f = first; f <= last; f += step)
windowOptions.captureFrames.push_back(f);
else if (std::sscanf(p, "%llu", &first) == 1)
windowOptions.captureFrames.push_back(first);
p = std::strchr(p, ',');
if (p)
++p;
}
#endif
options.elf = SBC_DEFAULT_ELF;
options.iso = SBC_DEFAULT_ISO;
// SLUS_213.44 is stripped: the symbol map (recomp/sbc_symbols.txt) names the functions the engine answers.
options.symbolMap = SBC_SYMBOL_MAP;
// The boot waits for the IOP modules' RPC servers before any frame ends: they advance while it waits.
options.advanceLibrariesWhileStalled = true;
options.frames = window ? 0 : 600;
options.printGameOutput = 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 == "--symbols" && i + 1 < argc)
options.symbolMap = 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 == "--mute")
mute = 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 SBC_WITH_WINDOW
else if (a == "--no-vsync")
windowOptions.vsync = false;
else if (a == "--fullscreen")
windowOptions.fullscreen = true;
else if (a == "--hidden") // no visible window: it presents offscreen (screenshots and captures still work)
windowOptions.hidden = true;
#endif
else
{
std::fprintf(stderr, "Usage: steambot [--frames N] [--headless] [--elf FILE] [--iso FILE] [--symbols FILE]\n"
" [--card-dir DIR] [--pad-script FILE] [--wav FILE] [--out FILE]\n"
" [--profile-io] [--quiet] [--mute]\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;
options.gameTag = "SLUS-21344"; // a save state loads only into Steambot Chronicles
bozo::host::FreeHost host(options);
bozo::ee::Dispatcher d(host);
sbc::setLibraries(&host.libraries());
// libmpeg: the SDK's own layout, not Twisted Metal: Black's fixed addresses (the engine's default reference layout).
// The player (0x2711b0 reads with sceCdStRead and demuxes, 0x273390 decodes) is Sony's sample, as Shinobido's: each
// stream packet's callback runs inside the demux (its locks around the IPU input ring), pictures as decoded.
// Its callbacks take the IPU input ring's lock: they run on the calling thread, as the SDK makes them
// (callbacksOnCaller), and its Nodata callback is the SDK's (it returns 1 while there is input).
host.libraries().mpegOptions = {.referenceLayout = false, .paced = false, .sdkNodata = true, .picturesAhead = 32,
.sdkDemux = true, .callbacksOnCaller = true};
d.setFunctions(sbc_function_table, sbc_function_count, sbc_resume_table, sbc_resume_count);
bozo::port::Registry registry;
sbc::installAudio(host.libraries(), registry);
sbc::install(d.runtime(), registry);
std::string error;
if (!host.setUp(d, sbc_external_table, sbc_external_count, &registry, error))
{
std::fprintf(stderr, "%s\n", error.c_str());
return 2;
}
const size_t modules = sbc::registerModules(d.runtime());
std::printf("[sbc] %zu overlay modules registered\n", modules);
bozo::app::FramePacer pacer;
pacer.setPaced(window && !std::getenv("SBC_UNPACED"));
// SBC_THREADS_AT=<frame>: every thread's state and pc at that frame (where a loop spins).
const uint64_t threadsAt = std::getenv("SBC_THREADS_AT") ? std::strtoull(std::getenv("SBC_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("[sbc: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());
// SBC_WATCH=<address>[,<address>...]: a word of RAM printed at each frame end where it changed.
if (static const char *watch = std::getenv("SBC_WATCH"); watch)
{
static std::vector<std::pair<uint32_t, uint32_t>> words;
if (words.empty())
for (const char *p = watch; p && *p;)
{
words.emplace_back(static_cast<uint32_t>(std::strtoul(p, nullptr, 16)), 0xDEADBEEFu);
p = std::strchr(p, ',');
if (p)
++p;
}
for (auto &[address, last] : words)
{
uint32_t v;
std::memcpy(&v, d.runtime().memory.ram() + (address & 0x1FFFFFFu), 4);
if (v != last)
std::printf("[sbc:watch] frame %llu: %08x = %08x (was %08x)\n", (unsigned long long)f, address, v, last);
last = v;
}
}
sbc::cheats::apply(d.runtime().memory.ram(), f); // the frame boundary: the game thread
pacer.frameEnd();
if (f % 60 == 0)
std::printf("[sbc] 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 SBC_WITH_WINDOW
sbc::cheats::registerMenu();
bozo::app::DevicePad devices;
bozo::app::AudioDevice audio;
if (window && options.padScript.empty())
host.libraries().pad.input = &devices;
#endif
host.onAudio = [&](uint64_t, const int16_t *pcm, size_t count)
{
if (recording.isOpen())
recording.write(pcm, count);
#ifdef SBC_WITH_WINDOW
if (window && !mute)
audio.push(pcm, count);
#endif
};
auto run = [&]()
{
#ifdef SBC_WITH_WINDOW
if (window && !mute)
audio.open();
#endif
uint64_t frames = 0;
try
{
frames = host.run();
}
catch (const std::exception &e)
{
std::fprintf(stderr, "[sbc] 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("[sbc] frames=%llu end=%s\n", (unsigned long long)frames, host.endReason().c_str());
sbc::probe::report();
// SBC_THREADS_END=1: every thread at the end, each of its contexts (pc, ra, sp): where a deadlock waits.
if (std::getenv("SBC_THREADS_END"))
for (const auto &[id, t] : d.kernel().threads())
{
std::printf("[sbc:threads] %d state=%u wait=%u object=%u entry=%x\n", id, unsigned(t->state), t->waitReason,
t->waitObject, t->entry);
for (const auto &f : t->frames)
std::printf("[sbc:threads] pc=%x ra=%x sp=%x (%s)\n", f->ctx.pc, f->ctx.r[31].uw[0], f->ctx.r[29].uw[0],
host.functionAt(f->ctx.pc).c_str());
}
const auto &stats = host.stats();
std::printf("[sbc] 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("[sbc] 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("[sbc:unanswered] %10llu %s\n", (unsigned long long)count, name.c_str());
if (std::getenv("SBC_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("[sbc:lib] %10llu %s\n", (unsigned long long)count, name.c_str());
}
for (const auto &[site, counts] : stats.ioSites)
std::printf("[sbc: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 SBC_WITH_WINDOW
if (window)
{
// A hidden window (a scripted run, captures) reads and writes no player's file: a test's own one in build/.
if (windowOptions.hidden && !std::getenv("PS2X_VIDEO_CONFIG"))
{
const std::string none = (std::filesystem::current_path() / "build" / "sbc_test_video.ini").string();
#ifdef _WIN32
_putenv_s("PS2X_VIDEO_CONFIG", none.c_str());
#else
setenv("PS2X_VIDEO_CONFIG", none.c_str(), 1);
#endif
}
bozo::app::video::loadAtStartup(windowOptions.width, windowOptions.height);
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)); };
host.onStateMessage = [](bool, const std::string &message)
{
std::fprintf(stderr, "[state] %s\n", message.c_str());
bozo::app::notify(message);
};
result = bozo::app::runWindow(windowOptions, game, &devices);
}
else
#endif
result = run();
recording.close();
return result;
}