mirror of
https://github.com/TheBozos/JC2_Recomp.git
synced 2026-12-01 07:38:28 -10:00
--win-battles zeroes the enemies' HP two seconds into a battle: the units are 0x9B0-byte records (+0x3E4 HP, +0x68C the side, 3 Levant's, 0 the enemies'), in four copies of the table that all have to be written; each table is found by its first side-3 record. --poke ADDR,VALUE,FRAME writes a word at a frame. With it the stage 15 boss is won and its scene plays back to the Jade Throne Room; the forest's stages 16 to 29 taken with the level cheats. Co-Authored-By: Claude Opus 5.5 <[email protected]>
374 lines
16 KiB
C++
374 lines
16 KiB
C++
// Jade Cocoon 2 (SLUS-20309) on BozoEngine: the game's recompiled code over native services. jc2 opens the window
|
|
// (SDL3, OpenGL 3.3: BozoEngine's platform layer); jc2-headless runs without one (checks, scripted runs).
|
|
#include "bozo/audio/wav.h"
|
|
#include "bozo/ee/dispatcher.h"
|
|
#include "bozo/host/free_host.h"
|
|
#include "bozo/libs/libraries.h"
|
|
#include "bozo/port.h"
|
|
#include "jc2_functions.h"
|
|
#include <array>
|
|
#include <algorithm>
|
|
#include <functional>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <vector>
|
|
#ifdef JC2_WITH_WINDOW
|
|
#include "bozo/app/app.h"
|
|
#include "bozo/app/audio_device.h"
|
|
#include "bozo/app/device_pad.h"
|
|
#include "bozo/app/frame_pacer.h"
|
|
#include "bozo/app/video_settings.h"
|
|
#include "bozo/app/window.h"
|
|
#include <filesystem>
|
|
#endif
|
|
|
|
struct Jc2Overlay
|
|
{
|
|
uint32_t load, end;
|
|
uint8_t identity[64];
|
|
const BozoFunctionEntry *functions;
|
|
const size_t *count;
|
|
const BozoResumeEntry *resume;
|
|
const size_t *resumeCount;
|
|
};
|
|
extern const Jc2Overlay jc2_overlays[];
|
|
extern const size_t jc2_overlay_count;
|
|
|
|
namespace jc2
|
|
{
|
|
void install(Runtime &, bozo::port::Registry &);
|
|
void installAudio(bozo::libs::Libraries &, bool trace);
|
|
void installDraw(Runtime &, bozo::port::Registry &);
|
|
void installMeshes(Runtime &, bozo::port::Registry &);
|
|
void installDebug(uint8_t *ram, std::function<void(const std::string &)> message);
|
|
void requestScene(int scene, uint64_t atFrame);
|
|
void installDebugOverrides(bozo::port::Registry &);
|
|
void installIpu(bozo::port::Registry &);
|
|
void requestBattle(int opponent, uint64_t atFrame, bool group);
|
|
void debugFrame(uint8_t *ram, uint64_t frame);
|
|
void requestStory(int kind, int value, bool set);
|
|
void requestPoke(uint32_t address, uint32_t value, uint64_t frame);
|
|
void requestWinBattles(bool on); // 0 chapter, 1 story flag, 2 scene played, 3 forest stage, 4 sub-area, 5 exits open, 6 warp, 7 warp to the exit
|
|
// Save states: JC2's part (jc2_port.cpp, jc2_audio.cpp, jc2_meshes.cpp, jc2_2d.cpp).
|
|
void portState(bozo::state::Archive &, Runtime &);
|
|
void audioState(bozo::state::Archive &);
|
|
std::string audioUnsaveable();
|
|
void meshesState(bozo::state::Archive &);
|
|
void uploadsState(bozo::state::Archive &);
|
|
void installUploads(bozo::libs::Libraries &, uint8_t *ram);
|
|
void reset2D();
|
|
} // namespace jc2
|
|
|
|
static int run(int argc, char **argv)
|
|
{
|
|
std::setvbuf(stdout, nullptr, _IONBF, 0); // nothing lost when a run ends abruptly
|
|
bozo::host::FreeOptions options;
|
|
options.framePeriodCycles = 294912000 / 60;
|
|
options.elf = JC2_DEFAULT_ELF;
|
|
options.symbolMap = JC2_SYMBOL_MAP;
|
|
options.iso = JC2_DEFAULT_ISO;
|
|
bool window = false;
|
|
#ifdef JC2_WITH_WINDOW
|
|
window = true;
|
|
bozo::app::setIdentity({"Jade Cocoon 2", "JC2_Recomp", "jc2-recomp", "SLUS_203.09"});
|
|
bozo::app::WindowOptions windowOptions;
|
|
bool mute = false; // no audio device: test runs stay silent (--mute, --hidden)
|
|
windowOptions.title = "Jade Cocoon 2 - BozoEngine";
|
|
windowOptions.width = 960;
|
|
windowOptions.height = 720;
|
|
#endif
|
|
options.frames = window ? 0 : 600;
|
|
options.printGameOutput = true;
|
|
std::string wav; // the mixer's output recorded (16-bit stereo, 48 kHz)
|
|
int scene = -1; // --scene: ask the game for this scene (jc2_debug.cpp), at --scene-at's frame
|
|
uint64_t sceneAt = 0;
|
|
int battle = -1; // --battle: fight this opponent (jc2_debug.cpp), at --battle-at's frame
|
|
uint64_t battleAt = 0;
|
|
bool battleGroup = false; // --battle-group: N is a battle group (the game's 2000), not a hunter
|
|
std::vector<std::array<int, 3>> story; // --chapter, --story-flag, --scene-done, --forest-stage, --subarea
|
|
std::string dumpRam; // EE RAM at the end of the run (a fault, the frame limit): the boundary to analyse offline
|
|
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 == "--elf" && i + 1 < argc)
|
|
options.elf = argv[++i];
|
|
else if (a == "--iso" && i + 1 < argc)
|
|
options.iso = argv[++i];
|
|
else if (a == "--out" && i + 1 < argc)
|
|
options.out = argv[++i];
|
|
else if (a == "--wav" && i + 1 < argc)
|
|
wav = argv[++i];
|
|
else if (a == "--dump-ram" && i + 1 < argc)
|
|
dumpRam = argv[++i];
|
|
else if (a == "--profile-io")
|
|
options.profileIo = true;
|
|
else if (a == "--load-state" && i + 1 < argc)
|
|
options.loadState = argv[++i];
|
|
else if (a == "--load-state-at" && i + 1 < argc)
|
|
options.loadStateFrame = std::stoul(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]);
|
|
else if (a == "--scene" && i + 1 < argc)
|
|
scene = std::stoi(argv[++i]);
|
|
else if (a == "--scene-at" && i + 1 < argc)
|
|
sceneAt = std::stoull(argv[++i]);
|
|
else if (a == "--battle" && i + 1 < argc)
|
|
battle = std::stoi(argv[++i]);
|
|
else if (a == "--battle-group" && i + 1 < argc)
|
|
battle = std::stoi(argv[++i]), battleGroup = true;
|
|
else if (a == "--battle-at" && i + 1 < argc)
|
|
battleAt = std::stoull(argv[++i]);
|
|
else if (a == "--chapter" && i + 1 < argc)
|
|
story.push_back({0, std::stoi(argv[++i]), 1});
|
|
else if ((a == "--story-flag" || a == "--clear-story-flag") && i + 1 < argc)
|
|
story.push_back({1, std::stoi(argv[++i]), a == "--story-flag"});
|
|
else if ((a == "--scene-done" || a == "--scene-not-done") && i + 1 < argc)
|
|
story.push_back({2, std::stoi(argv[++i]), a == "--scene-done"});
|
|
else if (a == "--open-exits")
|
|
story.push_back({5, 0, true});
|
|
else if (a == "--poke" && i + 1 < argc)
|
|
{
|
|
// ADDR,VALUE,FRAME (hex or decimal)
|
|
const std::string v = argv[++i];
|
|
const size_t c1 = v.find(','), c2 = v.find(',', c1 + 1);
|
|
jc2::requestPoke(uint32_t(std::stoul(v.substr(0, c1), nullptr, 0)),
|
|
uint32_t(std::stoll(v.substr(c1 + 1, c2 - c1 - 1), nullptr, 0)),
|
|
std::stoull(v.substr(c2 + 1)));
|
|
}
|
|
else if (a == "--win-battles")
|
|
jc2::requestWinBattles(true);
|
|
else if (a == "--warp-exit")
|
|
story.push_back({7, 0, true});
|
|
else if (a == "--warp" && i + 1 < argc)
|
|
{
|
|
const std::string xz = argv[++i];
|
|
const size_t comma = xz.find(',');
|
|
const int x = int(std::stof(xz.substr(0, comma)) * 16.0f), z = int(std::stof(xz.substr(comma + 1)) * 16.0f);
|
|
story.push_back({6, (x << 16) | (z & 0xFFFF), true});
|
|
}
|
|
else if (a == "--forest-stage" && i + 1 < argc)
|
|
story.push_back({3, std::stoi(argv[++i]), 1});
|
|
else if (a == "--subarea" && i + 1 < argc)
|
|
story.push_back({4, std::stoi(argv[++i]), 1});
|
|
else if (a == "--headless")
|
|
window = false;
|
|
#ifdef JC2_WITH_WINDOW
|
|
else if (a == "--no-vsync")
|
|
windowOptions.vsync = false;
|
|
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]));
|
|
else if (a == "--quit-after" && i + 1 < argc)
|
|
windowOptions.quitAfterFrames = std::stoull(argv[++i]);
|
|
else if (a == "--hidden")
|
|
{
|
|
windowOptions.hidden = true; // a test run: the window never shows, captures read the offscreen picture
|
|
mute = true;
|
|
}
|
|
else if (a == "--mute")
|
|
mute = true;
|
|
#endif
|
|
else
|
|
{
|
|
std::fprintf(stderr, "Usage: jc2-headless [--frames N] [--pad-script FILE] [--card-dir DIR] [--elf FILE]\n"
|
|
" [--iso FILE] [--out FILE] [--dump-ram FILE] [--profile-io]\n"
|
|
" [--load-state FILE [--load-state-at FRAME]] [--save-state FILE --save-state-at FRAME]\n"
|
|
" [--scene N [--scene-at FRAME]] [--battle N | --battle-group N [--battle-at FRAME]]\n"
|
|
" story: [--chapter N] [--story-flag N] [--clear-story-flag N] [--scene-done N]\n"
|
|
" [--scene-not-done N] [--forest-stage N] [--subarea N] [--open-exits] [--warp X,Z] [--warp-exit] [--poke ADDR,VALUE,FRAME] [--win-battles]\n"
|
|
" window: [--hidden (silent too)] [--mute] [--capture-dir DIR --capture-frame N ...] [--quit-after N]\n");
|
|
return a == "--help" ? 0 : 2;
|
|
}
|
|
}
|
|
#ifdef JC2_WITH_WINDOW
|
|
// A played run keeps its saves: the user folder's memory cards unless --card-dir names others.
|
|
if (window && options.cardDir.empty())
|
|
options.cardDir = bozo::app::userDirectory().string();
|
|
#endif
|
|
options.realTime = window;
|
|
options.gameTag = "SLUS-20309";
|
|
bozo::host::FreeHost host(options);
|
|
bozo::ee::Dispatcher d(host);
|
|
d.setFunctions(jc2_function_table, jc2_function_count, jc2_resume_table, jc2_resume_count);
|
|
// The code overlays: a module's code is called only while its header is at its load address.
|
|
for (size_t k = 0; k < jc2_overlay_count; ++k)
|
|
{
|
|
const Jc2Overlay &o = jc2_overlays[k];
|
|
d.runtime().functions.addModule(o.load, o.end, d.runtime().memory.ram() + o.load,
|
|
std::vector<uint8_t>(o.identity, o.identity + 64), o.functions, *o.count,
|
|
o.resume, *o.resumeCount);
|
|
}
|
|
bozo::port::Registry registry;
|
|
jc2::install(d.runtime(), registry);
|
|
jc2::installDraw(d.runtime(), registry);
|
|
jc2::installMeshes(d.runtime(), registry);
|
|
jc2::installDebugOverrides(registry);
|
|
jc2::installIpu(registry);
|
|
// JC2_TRACE_SIF=1: the IOP modules the game asks for and the RPC services it binds.
|
|
const bool traceSif = std::getenv("JC2_TRACE_SIF") != nullptr;
|
|
jc2::installAudio(host.libraries(), traceSif);
|
|
jc2::installUploads(host.libraries(), d.runtime().memory.ram());
|
|
// A disc read finishes after the drive's time, its callback then: the title menu clears its glow texture's
|
|
// pointer right after asking for it and counts on the read's callback setting it later (otherwise the menu's
|
|
// records land in a zero-size block and the save loaded there fails its check).
|
|
host.libraries().cd.timedCompletion = true;
|
|
// Save states: JC2's own part (the engine saves its runtime, libraries and host).
|
|
host.stateSections()->add({"jc2",
|
|
[&](bozo::state::Archive &a) {
|
|
jc2::portState(a, d.runtime());
|
|
jc2::audioState(a);
|
|
jc2::meshesState(a);
|
|
jc2::uploadsState(a); // last: older states end before it
|
|
if (a.loading())
|
|
jc2::reset2D();
|
|
},
|
|
[] { return jc2::audioUnsaveable(); }});
|
|
std::string error;
|
|
if (!host.setUp(d, jc2_external_table, jc2_external_count, ®istry, error))
|
|
{
|
|
std::fprintf(stderr, "%s\n", error.c_str());
|
|
return 2;
|
|
}
|
|
jc2::installDebug(d.runtime().memory.ram(), [](const std::string &message)
|
|
{
|
|
#ifdef JC2_WITH_WINDOW
|
|
bozo::app::notify(message);
|
|
#else
|
|
(void)message;
|
|
#endif
|
|
});
|
|
if (scene >= 0)
|
|
jc2::requestScene(scene, sceneAt);
|
|
if (battle >= 0)
|
|
jc2::requestBattle(battle, battleAt, battleGroup);
|
|
for (const auto &e : story)
|
|
jc2::requestStory(e[0], e[1], e[2] != 0);
|
|
bozo::audio::WavWriter recording;
|
|
if (!wav.empty() && !recording.open(wav))
|
|
{
|
|
std::fprintf(stderr, "cannot open WAV %s\n", wav.c_str());
|
|
return 2;
|
|
}
|
|
#ifdef JC2_WITH_WINDOW
|
|
bozo::app::FramePacer pacer;
|
|
pacer.setPaced(window);
|
|
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 JC2_WITH_WINDOW
|
|
if (window && !mute)
|
|
audio.push(pcm, count);
|
|
#endif
|
|
};
|
|
host.onFrame = [&](uint64_t f, const R5900Context *ctx)
|
|
{
|
|
jc2::debugFrame(d.runtime().memory.ram(), f);
|
|
#ifdef JC2_WITH_WINDOW
|
|
pacer.frameEnd();
|
|
#endif
|
|
if (f % 60 == 0)
|
|
std::printf("[jc2] frame=%llu pc=%x cycles=%llu\n", (unsigned long long)f, ctx->pc,
|
|
(unsigned long long)d.now());
|
|
};
|
|
auto play = [&]() -> int
|
|
{
|
|
#ifdef JC2_WITH_WINDOW
|
|
if (window && !mute)
|
|
audio.open();
|
|
#endif
|
|
uint64_t frames = 0;
|
|
try
|
|
{
|
|
frames = host.run();
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
std::fprintf(stderr, "[jc2] 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("[jc2] frames=%llu end=%s\n", (unsigned long long)frames, host.endReason().c_str());
|
|
const auto &stats = host.stats();
|
|
std::printf("[jc2] 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("[jc2] calls: %llu kernel, %llu library, %llu port overrides\n", (unsigned long long)stats.kernelCalls,
|
|
(unsigned long long)stats.libraryCalls, (unsigned long long)stats.overrideCalls);
|
|
// JC2_CALL_STATS=1: the native library calls by function, most first.
|
|
if (std::getenv("JC2_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("[jc2:lib] %10llu %s\n", (unsigned long long)count, name.c_str());
|
|
}
|
|
for (const auto &[name, count] : stats.unanswered)
|
|
std::printf("[jc2] unanswered: %s x%llu\n", name.c_str(), (unsigned long long)count);
|
|
for (const auto &[site, counts] : stats.ioSites)
|
|
std::printf("[jc2: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;
|
|
};
|
|
#ifdef JC2_WITH_WINDOW
|
|
if (window)
|
|
{
|
|
bozo::app::Game game;
|
|
game.run = play;
|
|
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);
|
|
};
|
|
const int result = bozo::app::runWindow(windowOptions, game, &devices);
|
|
recording.close();
|
|
return result;
|
|
}
|
|
#endif
|
|
const int result = play();
|
|
recording.close();
|
|
return result;
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
try
|
|
{
|
|
return run(argc, argv);
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
std::fprintf(stderr, "[jc2] setup failure: %s\n", e.what());
|
|
return 1;
|
|
}
|
|
}
|