Skip to content

PS2 games in the browser: static recompilation to WebAssembly with a native runtime

C++

Savestates: --save-state frame:file and --load-state file

Samir Nuri463b934a8ba4
diff --git a/host/main.cpp b/host/main.cpp
index a885286..ba8c8cf 100644
--- a/host/main.cpp
+++ b/host/main.cpp
@@ -23,7 +23,8 @@ int usage() {
"usage: ps2run <elf> [--arg text]... [--frames n] [--ntsc] [--misses file]\n"
" [--disc image.iso] [--memcard card.ps2] [--pal | --ntsc] [--trace-syscalls] [--trace-io] [--trace-threads]\n"
" [--trace-iop] [--trace-iop-hle] [--frames-dir dir] [--frame-every n]\n"
- " [--press frame:button[+button][:frames]]... [--window]\n");
+ " [--press frame:button[+button][:frames]]... [--window]\n"
+ " [--save-state frame:file] [--load-state file]\n");
return 2;
}
@@ -33,6 +34,7 @@ int main(int argc, char **argv) {
if (argc < 2) return usage();
std::vector<std::string> args{argv[1]};
[[maybe_unused]] bool window = false;
+ std::string load_path;
for (int i = 2; i < argc; ++i) {
const std::string a = argv[i];
if (a == "--arg" && i + 1 < argc) args.push_back(argv[++i]);
@@ -59,6 +61,14 @@ int main(int argc, char **argv) {
else if (a == "--window") window = true;
#endif
else if (a == "--frame-every" && i + 1 < argc) ps2::g_options.frame_every = static_cast<std::uint32_t>(std::strtoul(argv[++i], nullptr, 10));
+ else if (a == "--save-state" && i + 1 < argc) {
+ const std::string spec = argv[++i];
+ const auto colon = spec.find(':');
+ if (colon == std::string::npos) return usage();
+ ps2::g_options.save_state_frame = std::strtoull(spec.substr(0, colon).c_str(), nullptr, 10);
+ ps2::g_options.save_state_path = spec.substr(colon + 1);
+ }
+ else if (a == "--load-state" && i + 1 < argc) load_path = argv[++i];
else return usage();
}
@@ -73,6 +83,7 @@ int main(int argc, char **argv) {
return 1;
}
ps2::boot(entry, args);
+ if (!load_path.empty() && !ps2::load_state(load_path)) return 1;
#ifdef PS2_PROFILE
const char *profile = std::getenv("PS2_PROFILE");
const char *profile_from = std::getenv("PS2_PROFILE_FROM");
diff --git a/runtime/include/ps2/state.hpp b/runtime/include/ps2/state.hpp
new file mode 100644
index 0000000..29bef43
--- /dev/null
+++ b/runtime/include/ps2/state.hpp
@@ -0,0 +1,169 @@
+#pragma once
+
+#include <cstddef>
+#include <cstdint>
+#include <cstring>
+#include <deque>
+#include <map>
+#include <set>
+#include <stdexcept>
+#include <string>
+#include <type_traits>
+#include <vector>
+
+namespace ps2::state {
+
+class Stream {
+public:
+ explicit Stream(bool saving) : saving_(saving) {}
+ virtual ~Stream() = default;
+ bool saving() const { return saving_; }
+ bool loading() const { return !saving_; }
+ virtual void bytes(void *data, std::size_t size) = 0;
+
+ template <typename T>
+ void pod(T &v) {
+ static_assert(std::is_trivially_copyable_v<T>);
+ bytes(&v, sizeof v);
+ }
+
+ void tag(const char *name) {
+ char text[8] = {};
+ std::strncpy(text, name, sizeof text);
+ char got[8];
+ std::memcpy(got, text, sizeof got);
+ bytes(got, sizeof got);
+ if (loading() && std::memcmp(got, text, sizeof got) != 0) throw std::runtime_error(std::string("state section mismatch at ") + name);
+ }
+
+ std::uint32_t count(std::size_t n) {
+ std::uint32_t v = static_cast<std::uint32_t>(n);
+ pod(v);
+ return v;
+ }
+
+ void str(std::string &s) {
+ const std::uint32_t n = count(s.size());
+ if (loading()) s.resize(n);
+ bytes(s.data(), n);
+ }
+
+ template <typename T>
+ void vec(std::vector<T> &v) {
+ static_assert(std::is_trivially_copyable_v<T>);
+ const std::uint32_t n = count(v.size());
+ if (loading()) v.resize(n);
+ bytes(v.data(), static_cast<std::size_t>(n) * sizeof(T));
+ }
+
+ template <typename T, typename F>
+ void vec(std::vector<T> &v, F each) {
+ const std::uint32_t n = count(v.size());
+ if (loading()) v.assign(n, T{});
+ for (T &item : v) each(item);
+ }
+
+ template <typename T>
+ void deque(std::deque<T> &d) {
+ static_assert(std::is_trivially_copyable_v<T>);
+ const std::uint32_t n = count(d.size());
+ if (loading()) d.assign(n, T{});
+ for (T &item : d) pod(item);
+ }
+
+ template <typename T, typename F>
+ void deque(std::deque<T> &d, F each) {
+ const std::uint32_t n = count(d.size());
+ if (loading()) d.assign(n, T{});
+ for (T &item : d) each(item);
+ }
+
+ template <typename T>
+ void set(std::set<T> &s) {
+ static_assert(std::is_trivially_copyable_v<T>);
+ const std::uint32_t n = count(s.size());
+ if (saving()) {
+ for (T item : s) pod(item);
+ return;
+ }
+ s.clear();
+ for (std::uint32_t i = 0; i < n; ++i) {
+ T item{};
+ pod(item);
+ s.insert(item);
+ }
+ }
+
+ template <typename K, typename V, typename F>
+ void map(std::map<K, V> &m, F each) {
+ static_assert(std::is_trivially_copyable_v<K>);
+ const std::uint32_t n = count(m.size());
+ if (saving()) {
+ for (auto &[k, v] : m) {
+ K key = k;
+ pod(key);
+ each(v);
+ }
+ return;
+ }
+ m.clear();
+ for (std::uint32_t i = 0; i < n; ++i) {
+ K key{};
+ pod(key);
+ each(m[key]);
+ }
+ }
+
+ template <typename K, typename V>
+ void map(std::map<K, V> &m) {
+ map(m, [this](V &v) { pod(v); });
+ }
+
+ template <typename V, typename F>
+ void string_map(std::map<std::string, V> &m, F each) {
+ const std::uint32_t n = count(m.size());
+ if (saving()) {
+ for (auto &[k, v] : m) {
+ std::string key = k;
+ str(key);
+ each(v);
+ }
+ return;
+ }
+ m.clear();
+ for (std::uint32_t i = 0; i < n; ++i) {
+ std::string key;
+ str(key);
+ each(m[key]);
+ }
+ }
+
+private:
+ bool saving_;
+};
+
+class Writer : public Stream {
+public:
+ Writer() : Stream(true) {}
+ void bytes(void *data, std::size_t size) override {
+ const auto *p = static_cast<const std::uint8_t *>(data);
+ out.insert(out.end(), p, p + size);
+ }
+ std::vector<std::uint8_t> out;
+};
+
+class Reader : public Stream {
+public:
+ Reader(const std::uint8_t *data, std::size_t size) : Stream(false), at_(data), end_(data + size) {}
+ void bytes(void *data, std::size_t size) override {
+ if (static_cast<std::size_t>(end_ - at_) < size) throw std::runtime_error("state file is truncated");
+ std::memcpy(data, at_, size);
+ at_ += size;
+ }
+
+private:
+ const std::uint8_t *at_;
+ const std::uint8_t *end_;
+};
+
+}
diff --git a/runtime/include/ps2/system.hpp b/runtime/include/ps2/system.hpp
index 3849dc1..d9b7c8c 100644
--- a/runtime/include/ps2/system.hpp
+++ b/runtime/include/ps2/system.hpp
@@ -39,6 +39,8 @@ struct Options {
std::function<void(const std::string &name, const std::uint8_t *code, std::size_t size)> on_vu_program;
std::string memcard_path;
std::function<void()> on_memcard_saved;
+ std::string save_state_path;
+ std::uint64_t save_state_frame = 0;
};
extern Options g_options;
@@ -69,6 +71,8 @@ bool load_elf(const std::string &path, std::uint32_t &entry);
void boot(std::uint32_t entry, const std::vector<std::string> &args);
int run();
void halt(const std::string &reason);
+bool save_state(const std::string &path);
+bool load_state(const std::string &path);
bool halted();
std::uint64_t frame_count();
diff --git a/runtime/src/dmac.cpp b/runtime/src/dmac.cpp
index e1129e6..eeb3090 100644
--- a/runtime/src/dmac.cpp
+++ b/runtime/src/dmac.cpp
@@ -10,6 +10,8 @@
#include "ps2/system.hpp"
#include "vif.hpp"
+#include "ps2/state.hpp"
+
#include <cstdio>
#include <cstdlib>
#include <cstring>
@@ -377,3 +379,19 @@ void write(std::uint32_t a, std::uint32_t v) {
}
}
+
+namespace ps2::dmac {
+
+void state(ps2::state::Stream &s) {
+ s.tag("dmac");
+ s.pod(g_ch);
+ s.pod(g_ctrl);
+ s.pod(g_pcr);
+ s.pod(g_sqwc);
+ s.pod(g_rbsr);
+ s.pod(g_rbor);
+ s.pod(g_stadr);
+ s.pod(g_enable);
+}
+
+}
diff --git a/runtime/src/dmac.hpp b/runtime/src/dmac.hpp
index 14c955a..4ceb9e7 100644
--- a/runtime/src/dmac.hpp
+++ b/runtime/src/dmac.hpp
@@ -2,8 +2,14 @@
#include <cstdint>
+namespace ps2::state {
+class Stream;
+}
+
namespace ps2::dmac {
+void state(ps2::state::Stream &s);
+
void reset();
void stats();
bool owns(std::uint32_t a);
diff --git a/runtime/src/gs/gif.cpp b/runtime/src/gs/gif.cpp
index 6f70d73..67890e7 100644
--- a/runtime/src/gs/gif.cpp
+++ b/runtime/src/gs/gif.cpp
@@ -3,6 +3,8 @@
#include "gs.hpp"
#include "queue.hpp"
+#include "ps2/state.hpp"
+
#include <algorithm>
#include <cstdio>
#include <cstring>
@@ -192,3 +194,15 @@ void write(std::uint32_t a, std::uint32_t v) {
}
}
+
+namespace ps2::gif {
+
+void state(ps2::state::Stream &s) {
+ s.tag("gif");
+ s.pod(g_paths);
+ s.pod(g_stat);
+ s.pod(g_tags);
+ s.pod(g_scan);
+}
+
+}
diff --git a/runtime/src/gs/gif.hpp b/runtime/src/gs/gif.hpp
index 78236d9..6eb7f3b 100644
--- a/runtime/src/gs/gif.hpp
+++ b/runtime/src/gs/gif.hpp
@@ -2,8 +2,14 @@
#include <cstdint>
+namespace ps2::state {
+class Stream;
+}
+
namespace ps2::gif {
+void state(ps2::state::Stream &s);
+
void reset();
void stats();
void transfer(int path, const std::uint8_t *data, std::uint32_t qwc);
diff --git a/runtime/src/gs/gpu/gpu.cpp b/runtime/src/gs/gpu/gpu.cpp
index b6e1bb9..1225b3d 100644
--- a/runtime/src/gs/gpu/gpu.cpp
+++ b/runtime/src/gs/gpu/gpu.cpp
@@ -686,9 +686,9 @@ void submit(WGPUTextureView present_view = nullptr) {
g_clut_loads = 0;
}
-void read_back(WGPUBuffer source, std::uint64_t bytes, void *out) {
+void read_back(WGPUBuffer source, std::uint64_t bytes, void *out, std::uint64_t offset = 0) {
WGPUCommandEncoder enc = wgpuDeviceCreateCommandEncoder(g_device, nullptr);
- wgpuCommandEncoderCopyBufferToBuffer(enc, source, 0, g_readback, 0, bytes);
+ wgpuCommandEncoderCopyBufferToBuffer(enc, source, offset, g_readback, 0, bytes);
WGPUCommandBuffer cmd = wgpuCommandEncoderFinish(enc, nullptr);
wgpuQueueSubmit(g_queue, 1, &cmd);
wgpuCommandBufferRelease(cmd);
@@ -849,7 +849,7 @@ bool init(Mode m) {
swizzle_tables(tables);
g_tables = make_buffer(tables.size() * 4, WGPUBufferUsage_Storage | WGPUBufferUsage_CopyDst);
wgpuQueueWriteBuffer(g_queue, g_tables, 0, tables.data(), tables.size() * 4);
- g_clut = make_buffer(static_cast<std::uint64_t>(kClutSlots) * kClutEntries * 4, WGPUBufferUsage_Storage);
+ g_clut = make_buffer(static_cast<std::uint64_t>(kClutSlots) * kClutEntries * 4, WGPUBufferUsage_Storage | WGPUBufferUsage_CopySrc | WGPUBufferUsage_CopyDst);
g_scratch = make_buffer((static_cast<std::uint64_t>(g_copy_words) + 2ull * g_field_words) * 4, WGPUBufferUsage_Storage | WGPUBufferUsage_CopySrc);
g_cache = make_buffer(static_cast<std::uint64_t>(kCacheWords) * 4, WGPUBufferUsage_Storage | WGPUBufferUsage_CopyDst);
g_cache_copy.assign(kCacheWords, 0);
@@ -1161,6 +1161,32 @@ void download(std::uint8_t *memory) {
read_back(g_scratch, kMemorySize, memory);
}
+void read_state(std::vector<std::uint32_t> &vram, std::uint32_t &scale, std::vector<std::uint32_t> &clut) {
+ flush_all();
+ submit();
+ scale = g_scale;
+ vram.resize(static_cast<std::size_t>(kMemorySize / 4) * g_scale * g_scale);
+ read_back(g_vram, vram.size() * 4, vram.data());
+ clut.resize(kClutEntries);
+ read_back(g_clut, kClutEntries * 4, clut.data(), static_cast<std::uint64_t>(g_clut_slot) * kClutEntries * 4);
+}
+
+void write_state(const std::vector<std::uint32_t> &vram, std::uint32_t scale, const std::vector<std::uint32_t> &clut) {
+ wait_idle();
+ const std::size_t words = kMemorySize / 4;
+ const std::uint32_t from = scale * scale, to = g_scale * g_scale;
+ if (from == to && vram.size() == words * to) {
+ wgpuQueueWriteBuffer(g_queue, g_vram, 0, vram.data(), vram.size() * 4);
+ } else {
+ std::vector<std::uint32_t> layers(words * to);
+ for (std::size_t w = 0; w < words; ++w)
+ for (std::uint32_t k = 0; k < to; ++k) layers[w * to + k] = vram[w * from];
+ wgpuQueueWriteBuffer(g_queue, g_vram, 0, layers.data(), layers.size() * 4);
+ }
+ if (clut.size() >= kClutEntries) wgpuQueueWriteBuffer(g_queue, g_clut, static_cast<std::uint64_t>(g_clut_slot) * kClutEntries * 4, clut.data(), kClutEntries * 4);
+ wait_idle();
+}
+
void stats() {
if (g_mode == Mode::off) return;
std::fprintf(stderr, "[gpu] %llu submits, dispatches:", static_cast<unsigned long long>(g_submits));
diff --git a/runtime/src/gs/gpu/gpu.hpp b/runtime/src/gs/gpu/gpu.hpp
index e6ea437..fb90a6d 100644
--- a/runtime/src/gs/gpu/gpu.hpp
+++ b/runtime/src/gs/gpu/gpu.hpp
@@ -33,6 +33,8 @@ void load_clut(std::uint64_t tex0, std::uint64_t texclut);
void flush();
void wait_idle();
void download(std::uint8_t *memory);
+void read_state(std::vector<std::uint32_t> &vram, std::uint32_t &scale, std::vector<std::uint32_t> &clut);
+void write_state(const std::vector<std::uint32_t> &vram, std::uint32_t scale, const std::vector<std::uint32_t> &clut);
bool display(const DisplaySource &src, std::vector<std::uint32_t> &rgba, std::uint32_t &width, std::uint32_t &height);
bool display_async(const DisplaySource &src, std::vector<std::uint32_t> &rgba, std::uint32_t &width, std::uint32_t &height);
bool attach_surface(const SurfaceTarget &target);
diff --git a/runtime/src/gs/gs.cpp b/runtime/src/gs/gs.cpp
index a65b169..368a5f3 100644
--- a/runtime/src/gs/gs.cpp
+++ b/runtime/src/gs/gs.cpp
@@ -8,6 +8,8 @@
#include "gpu/gpu.hpp"
#endif
+#include "ps2/state.hpp"
+
#include <algorithm>
#include <atomic>
#include <functional>
@@ -916,3 +918,58 @@ void stats() {
}
}
+
+namespace ps2::gs {
+
+void state(ps2::state::Stream &s) {
+ s.tag("gs");
+ flush_and_wait();
+ s.pod(g_state);
+ s.pod(g_pmode);
+ s.pod(g_smode1);
+ s.pod(g_smode2);
+ s.pod(g_srfsh);
+ s.pod(g_synch1);
+ s.pod(g_synch2);
+ s.pod(g_syncv);
+ s.pod(g_dispfb);
+ s.pod(g_display);
+ s.pod(g_extbuf);
+ s.pod(g_extdata);
+ s.pod(g_extwrite);
+ s.pod(g_bgcolor);
+ s.pod(g_csr);
+ s.pod(g_imr);
+ s.pod(g_busdir);
+ s.pod(g_siglblid);
+ s.pod(g_primitives);
+ s.pod(g_odd_field);
+ s.pod(g_transfer);
+ s.pod(g_carry);
+ s.pod(g_carried);
+ std::vector<std::uint32_t> vram, clut;
+ std::uint32_t scale = 1;
+#ifdef PS2_GPU
+ if (s.saving() && gpu_on()) {
+ gpu::download(g_memory);
+ gpu::read_state(vram, scale, clut);
+ }
+#endif
+ s.bytes(g_memory, kMemorySize);
+ s.pod(scale);
+ s.vec(vram);
+ s.vec(clut);
+#ifdef PS2_GPU
+ if (s.loading() && gpu_on()) {
+ if (vram.empty()) {
+ vram.resize(kMemorySize / 4);
+ std::memcpy(vram.data(), g_memory, kMemorySize);
+ clut.assign(g_state.clut, g_state.clut + 512);
+ scale = 1;
+ }
+ gpu::write_state(vram, scale, clut);
+ }
+#endif
+}
+
+}
diff --git a/runtime/src/gs/gs.hpp b/runtime/src/gs/gs.hpp
index feba96c..f2b6db6 100644
--- a/runtime/src/gs/gs.hpp
+++ b/runtime/src/gs/gs.hpp
@@ -6,6 +6,10 @@
#include <functional>
#include <vector>
+namespace ps2::state {
+class Stream;
+}
+
namespace ps2::gs {
inline constexpr std::uint32_t kMemorySize = 4u << 20;
@@ -42,6 +46,7 @@ extern State g_state;
std::uint8_t *memory();
void init();
+void state(ps2::state::Stream &s);
void reset();
void write(std::uint32_t reg, std::uint64_t value);
void signal(std::uint32_t reg, std::uint64_t value);
diff --git a/runtime/src/hw.cpp b/runtime/src/hw.cpp
index 1d9de29..8dce4a7 100644
--- a/runtime/src/hw.cpp
+++ b/runtime/src/hw.cpp
@@ -11,6 +11,8 @@
#include "ps2/memory.hpp"
#include "ps2/system.hpp"
+#include "ps2/state.hpp"
+
#include <cstdio>
#include <cstring>
#include <map>
@@ -276,3 +278,23 @@ void write128(std::uint32_t a, const Reg128 &v) {
}
}
+
+namespace ps2::hw {
+
+std::function<void()> event_action(int id) {
+ if (id == kEventVblankEnd) return [] { intc_raise(3); };
+ if (id >= kEventTimer0 && id <= kEventTimer3) return [n = id - kEventTimer0] { timer_event(n); };
+ return [] {};
+}
+
+void state(ps2::state::Stream &s) {
+ s.tag("eehw");
+ s.pod(g_intc_stat);
+ s.pod(g_intc_mask);
+ s.pod(g_dmac_stat);
+ s.map(g_regs);
+ s.pod(g_crt);
+ s.pod(g_timers);
+}
+
+}
diff --git a/runtime/src/hw.hpp b/runtime/src/hw.hpp
index cf00db2..bac955d 100644
--- a/runtime/src/hw.hpp
+++ b/runtime/src/hw.hpp
@@ -3,6 +3,11 @@
#include "ps2/cpu.hpp"
#include <cstdint>
+#include <functional>
+
+namespace ps2::state {
+class Stream;
+}
namespace ps2 {
@@ -15,6 +20,9 @@ std::uint32_t physical(std::uint32_t a);
namespace hw {
+void state(ps2::state::Stream &s);
+std::function<void()> event_action(int id);
+
enum Event {
kEventVblank,
kEventVblankEnd,
diff --git a/runtime/src/iop/cdvd.cpp b/runtime/src/iop/cdvd.cpp
index 875eb24..c638ef3 100644
--- a/runtime/src/iop/cdvd.cpp
+++ b/runtime/src/iop/cdvd.cpp
@@ -2,6 +2,8 @@
#include "ps2/disc.hpp"
+#include "ps2/state.hpp"
+
#include <algorithm>
#include <cstdio>
#include <ctime>
@@ -58,22 +60,13 @@ std::vector<std::uint8_t> read_disc_file(const std::string &path) {
return data;
}
-bool cd_read(std::uint32_t lsn, std::uint32_t sectors, std::uint32_t buffer, std::uint32_t mode, std::function<void()> done);
-
namespace {
-std::function<void()> g_ee_done;
-
bool start_read(std::uint32_t lsn, std::uint32_t sectors, std::uint32_t buffer, std::uint32_t, bool) {
if (g_cd.busy) return false;
g_cd.busy = true;
g_cd.error = 0;
- schedule(now() + read_cycles(sectors), [lsn, sectors, buffer]() {
- std::vector<std::uint8_t> data(static_cast<std::size_t>(sectors) * disc::kSector);
- disc::read(lsn, sectors, data.data());
- for (std::size_t i = 0; i < data.size(); ++i) write8(buffer + static_cast<std::uint32_t>(i), data[i]);
- finish_read();
- });
+ schedule(now() + read_cycles(sectors), Job{kJobCdRead, lsn, sectors, buffer});
return true;
}
@@ -81,6 +74,12 @@ bool start_read(std::uint32_t lsn, std::uint32_t sectors, std::uint32_t buffer,
void install_cdvd() {
g_cd = CdState{};
+ register_job(kJobCdRead, [](const Job &j) {
+ std::vector<std::uint8_t> data(static_cast<std::size_t>(j.b) * disc::kSector);
+ disc::read(j.a, j.b, data.data());
+ for (std::size_t i = 0; i < data.size(); ++i) write8(j.c + static_cast<std::uint32_t>(i), data[i]);
+ finish_read();
+ });
register_hle_library("cdvdman", 0x0101);
def("cdvdman", 4, "sceCdInit", [](Cpu &c) { c.r[2] = 1; });
def("cdvdman", 5, "sceCdStandby", [](Cpu &c) { c.r[2] = 1; });
@@ -155,17 +154,6 @@ void install_cdvd() {
bool cd_busy() { return g_cd.busy; }
-bool cd_read(std::uint32_t lsn, std::uint32_t sectors, std::uint32_t buffer, std::uint32_t mode, std::function<void()> done) {
- if (!start_read(lsn, sectors, buffer, mode, false)) return false;
- g_ee_done = std::move(done);
- schedule(now() + read_cycles(sectors) + 1, []() {
- auto d = std::move(g_ee_done);
- g_ee_done = nullptr;
- if (d) d();
- });
- return true;
-}
-
}
std::int32_t load_module(const std::string &path, const std::string &args, std::function<void(int, int)> done) {
@@ -180,3 +168,15 @@ std::int32_t load_module(const std::string &path, const std::string &args, std::
}
}
+
+namespace ps2::iop::kernel {
+
+void cdvd_state(ps2::state::Stream &s) {
+ s.tag("cdvd");
+ s.pod(g_cd.busy);
+ s.pod(g_cd.callback);
+ s.pod(g_cd.error);
+ s.vec(g_cd.sync_waiters);
+}
+
+}
diff --git a/runtime/src/iop/cdvdfsv.cpp b/runtime/src/iop/cdvdfsv.cpp
index 4f498c5..66a0d6a 100644
--- a/runtime/src/iop/cdvdfsv.cpp
+++ b/runtime/src/iop/cdvdfsv.cpp
@@ -4,6 +4,8 @@
#include "ps2/disc.hpp"
#include "ps2/memory.hpp"
+#include "ps2/state.hpp"
+
#include <cstdio>
#include <ctime>
#include <vector>
@@ -32,6 +34,19 @@ void read_to_ee(std::uint32_t sector, std::uint32_t count, std::uint32_t dst) {
}
void install_cdvdfsv() {
+ g_streaming = false;
+ g_stream_pos = 0;
+ register_job(kJobFsvRead, [](const Job &j) {
+ read_to_ee(j.a, j.b, j.c);
+ sif::complete_call(0x80000595u);
+ });
+ register_job(kJobFsvComplete, [](const Job &) { sif::complete_call(0x80000595u); });
+ register_job(kJobFsvIopRead, [](const Job &j) {
+ std::vector<std::uint8_t> data(static_cast<std::size_t>(j.b) * disc::kSector);
+ disc::read(j.a, j.b, data.data());
+ for (std::size_t i = 0; i < data.size(); ++i) write8(j.c + static_cast<std::uint32_t>(i), data[i]);
+ sif::complete_call(0x80000595u);
+ });
sif::register_hle_server(0x80000592u, [](std::uint32_t, std::uint32_t, std::uint32_t, std::uint32_t ret, std::uint32_t ret_size) {
if (ret_size >= 16) set_ee32(ret + 12, 0xFF);
return true;
@@ -77,15 +92,12 @@ void install_cdvdfsv() {
switch (method) {
case 0x01:
if (ret_size >= 4) set_ee32(ret, 0);
- schedule(now() + delay(count), [sector, count, dst]() {
- read_to_ee(sector, count, dst);
- sif::complete_call(0x80000595u);
- });
+ schedule(now() + delay(count), Job{kJobFsvRead, sector, count, dst});
return false;
case 0x04: set_ee32(ret, 1); return true;
case 0x05: return true;
case 0x08:
- schedule(now() + delay(0), []() { sif::complete_call(0x80000595u); });
+ schedule(now() + delay(0), Job{kJobFsvComplete});
return false;
case 0x09: {
const std::uint32_t cmd = ee32(args + 12);
@@ -95,10 +107,7 @@ void install_cdvdfsv() {
const std::uint32_t pos = g_stream_pos;
g_stream_pos += count;
set_ee32(ret, count);
- schedule(now() + delay(count), [pos, count, dst]() {
- read_to_ee(pos, count, dst);
- sif::complete_call(0x80000595u);
- });
+ schedule(now() + delay(count), Job{kJobFsvRead, pos, count, dst});
return false;
}
case 3: g_streaming = false; set_ee32(ret, 1); return true;
@@ -110,12 +119,7 @@ void install_cdvdfsv() {
}
case 0x0D:
if (ret_size >= 4) set_ee32(ret, 0);
- schedule(now() + delay(count), [sector, count, dst]() {
- std::vector<std::uint8_t> data(static_cast<std::size_t>(count) * disc::kSector);
- disc::read(sector, count, data.data());
- for (std::size_t i = 0; i < data.size(); ++i) write8(dst + static_cast<std::uint32_t>(i), data[i]);
- sif::complete_call(0x80000595u);
- });
+ schedule(now() + delay(count), Job{kJobFsvIopRead, sector, count, dst});
return false;
case 0x0E: set_ee32(ret, 2); return true;
default:
@@ -148,3 +152,13 @@ void install_cdvdfsv() {
}
}
+
+namespace ps2::iop::kernel {
+
+void cdvdfsv_state(ps2::state::Stream &s) {
+ s.tag("cdvdfsv");
+ s.pod(g_streaming);
+ s.pod(g_stream_pos);
+}
+
+}
diff --git a/runtime/src/iop/hw.cpp b/runtime/src/iop/hw.cpp
index 31d7b28..21f2405 100644
--- a/runtime/src/iop/hw.cpp
+++ b/runtime/src/iop/hw.cpp
@@ -5,6 +5,8 @@
#include "sio2.hpp"
#include "spu2.hpp"
+#include "ps2/state.hpp"
+
#include <cstdio>
#include <map>
#include <set>
@@ -104,7 +106,7 @@ void start_dma(unsigned ch) {
if (kernel::g_trace_modules) std::fprintf(stderr, "[iop-dma] start ch %u madr 0x%08X bcr 0x%08X chcr 0x%08X\n", ch, d.madr, d.bcr, d.chcr);
const std::uint32_t words = (d.bcr & 0xFFFFu) * ((d.bcr >> 16) ? (d.bcr >> 16) : 1u);
if (ch == 4 || ch == 7) {
- spu2::dma(ch == 4 ? 0 : 1, d.madr, words, (d.chcr & 1u) != 0, [ch]() { dma_done(ch); });
+ spu2::dma(ch == 4 ? 0 : 1, d.madr, words, (d.chcr & 1u) != 0, static_cast<int>(ch));
return;
}
if (ch == 11 || ch == 12) {
@@ -118,6 +120,7 @@ void start_dma(unsigned ch) {
}
void reset() {
+ kernel::register_job(kernel::kJobDmaDone, [](const kernel::Job &j) { dma_done(j.a); });
g_istat = g_imask = g_dicr = g_dicr2 = 0;
g_ictrl = 1;
g_regs.clear();
@@ -267,3 +270,20 @@ std::uint16_t *spu2_ram() { return spu2::ram(); }
}
}
+
+namespace ps2::iop::hw {
+
+void state(ps2::state::Stream &s) {
+ s.tag("iophw");
+ s.pod(g_istat);
+ s.pod(g_imask);
+ s.pod(g_ictrl);
+ s.pod(g_dicr);
+ s.pod(g_dicr2);
+ s.map(g_regs);
+ s.pod(g_dma);
+ s.pod(g_ohci);
+ s.pod(g_scratchpad);
+}
+
+}
diff --git a/runtime/src/iop/hw.hpp b/runtime/src/iop/hw.hpp
index 7ae0c9e..eb91f86 100644
--- a/runtime/src/iop/hw.hpp
+++ b/runtime/src/iop/hw.hpp
@@ -2,8 +2,14 @@
#include <cstdint>
+namespace ps2::state {
+class Stream;
+}
+
namespace ps2::iop::hw {
+void state(ps2::state::Stream &s);
+
void reset();
void raise_interrupt(unsigned line);
void set_interrupt_mask(unsigned line, bool enabled);
diff --git a/runtime/src/iop/io.cpp b/runtime/src/iop/io.cpp
index 9bd1009..c00c0bf 100644
--- a/runtime/src/iop/io.cpp
+++ b/runtime/src/iop/io.cpp
@@ -3,6 +3,8 @@
#include "ps2/disc.hpp"
+#include "ps2/state.hpp"
+
#include <algorithm>
#include <cstdio>
#include <map>
@@ -169,3 +171,18 @@ void install_io() {
}
}
+
+namespace ps2::iop::io {
+
+void state(ps2::state::Stream &s) {
+ s.tag("iopio");
+ s.map(g_files, [&s](File &f) {
+ s.pod(f.sector);
+ s.pod(f.size);
+ s.pod(f.position);
+ s.vec(f.memory);
+ s.pod(f.in_memory);
+ });
+}
+
+}
diff --git a/runtime/src/iop/io.hpp b/runtime/src/iop/io.hpp
index b2538c6..8fe15d9 100644
--- a/runtime/src/iop/io.hpp
+++ b/runtime/src/iop/io.hpp
@@ -3,8 +3,14 @@
#include <cstdint>
#include <string>
+namespace ps2::state {
+class Stream;
+}
+
namespace ps2::iop::io {
+void state(ps2::state::Stream &s);
+
int open(const std::string &path, std::uint32_t flags);
int close(int fd);
int read(int fd, std::uint8_t *out, std::uint32_t size);
diff --git a/runtime/src/iop/iop.hpp b/runtime/src/iop/iop.hpp
index 72f50d4..bbcc121 100644
--- a/runtime/src/iop/iop.hpp
+++ b/runtime/src/iop/iop.hpp
@@ -6,8 +6,14 @@
#include <string>
#include <vector>
+namespace ps2::state {
+class Stream;
+}
+
namespace ps2::iop {
+void state(ps2::state::Stream &s);
+
inline constexpr std::uint32_t kRamSize = 2u << 20;
inline constexpr std::uint32_t kClockDivider = 8;
inline constexpr std::uint32_t kHleOpcode = 0x3Fu;
diff --git a/runtime/src/iop/kernel.cpp b/runtime/src/iop/kernel.cpp
index 601cd77..d25eab3 100644
--- a/runtime/src/iop/kernel.cpp
+++ b/runtime/src/iop/kernel.cpp
@@ -1,6 +1,11 @@
#include "kernel.hpp"
#include "hw.hpp"
+#include "io.hpp"
+#include "sio2.hpp"
+#include "spu2.hpp"
+
+#include "ps2/state.hpp"
#include <algorithm>
#include <cstdio>
@@ -9,6 +14,7 @@
#include <memory>
#include <queue>
#include <set>
+#include <stdexcept>
namespace ps2::iop {
@@ -36,6 +42,7 @@ struct HleEntry {
struct Event {
std::uint64_t at;
std::uint64_t seq;
+ Job job;
std::function<void()> action;
bool operator>(const Event &o) const { return at != o.at ? at > o.at : seq > o.seq; }
};
@@ -243,7 +250,17 @@ std::uint64_t now() { return g_now; }
std::uint64_t usec_to_cycles(std::uint64_t usec) { return usec * 36864u / 1000u; }
void schedule(std::uint64_t at, std::function<void()> action) {
- g_events.push(Event{at, ++g_event_seq, std::move(action)});
+ g_events.push(Event{at, ++g_event_seq, Job{}, std::move(action)});
+}
+
+namespace {
+JobHandler g_jobs[kJobCount] = {};
+}
+
+void register_job(std::uint32_t kind, JobHandler handler) { g_jobs[kind] = handler; }
+
+void schedule(std::uint64_t at, const Job &job) {
+ g_events.push(Event{at, ++g_event_seq, job, nullptr});
}
Thread *current() {
@@ -554,9 +571,10 @@ void run(std::uint64_t ee_cycles) {
// A thread inside CpuSuspendIntr cannot be preempted: interrupt-side events wait for CpuResumeIntr.
if (!g_suspended || !running || running->status != kRun) {
while (!g_events.empty() && g_events.top().at <= g_now) {
- auto action = g_events.top().action;
+ const Event event = g_events.top();
g_events.pop();
- action();
+ if (event.job.kind) g_jobs[event.job.kind](event.job);
+ else event.action();
}
deliver_interrupts();
if (g_switch) do_switch();
@@ -678,3 +696,97 @@ void set_trace(bool modules_on, bool hle_on) {
}
}
+
+namespace ps2::iop::kernel {
+
+void state(ps2::state::Stream &s) {
+ s.tag("iopkern");
+ if (s.saving() && g_nesting) throw std::runtime_error("the IOP is inside a guest call");
+ s.map(g_threads, [&s](Thread &t) {
+ if (s.saving() && t.on_exit) throw std::runtime_error("an IOP module is still starting");
+ s.pod(t.id);
+ s.pod(t.status);
+ s.pod(t.priority);
+ s.pod(t.init_priority);
+ s.pod(t.entry);
+ s.pod(t.stack);
+ s.pod(t.stack_size);
+ s.pod(t.gp);
+ s.pod(t.attr);
+ s.pod(t.option);
+ s.pod(t.wakeups);
+ s.pod(t.wait);
+ s.pod(t.wait_id);
+ s.pod(t.order);
+ s.pod(t.context);
+ s.pod(t.rpc_queue);
+ s.pod(t.rpc_server);
+ s.pod(t.event_bits);
+ s.pod(t.event_mode);
+ s.pod(t.event_result);
+ s.pod(t.message_out);
+ s.pod(t.suspended);
+ });
+ s.map(g_modules, [&s](Module &m) {
+ s.pod(m.id);
+ s.str(m.name);
+ s.pod(m.base);
+ s.pod(m.size);
+ s.pod(m.entry);
+ s.pod(m.gp);
+ });
+ s.string_map(g_libraries, [&s](std::uint32_t &table) { s.pod(table); });
+ s.map(g_used);
+ s.map(g_interrupts);
+ std::vector<Event> events;
+ if (s.saving()) {
+ auto pending = g_events;
+ for (; !pending.empty(); pending.pop()) {
+ if (!pending.top().job.kind) throw std::runtime_error("an IOP event cannot be saved");
+ events.push_back(pending.top());
+ }
+ }
+ s.vec(events, [&s](Event &e) {
+ s.pod(e.at);
+ s.pod(e.seq);
+ s.pod(e.job);
+ });
+ if (s.loading()) {
+ while (!g_events.empty()) g_events.pop();
+ for (const Event &e : events) g_events.push(e);
+ }
+ s.pod(g_current);
+ s.pod(g_next_thread);
+ s.pod(g_next_module);
+ s.pod(g_order);
+ s.pod(g_switch);
+ s.pod(g_kernel_top);
+ s.pod(g_event_seq);
+ s.pod(g_now);
+ s.pod(g_target);
+ s.pod(g_ee_remainder);
+ s.pod(g_suspended);
+ s.pod(g_nested_done);
+ s.pod(g_return_stub);
+ s.pod(g_exit_stub);
+}
+
+}
+
+namespace ps2::iop {
+
+void state(ps2::state::Stream &s) {
+ s.tag("iop");
+ s.pod(g_cpu);
+ s.bytes(g_ram, kRamSize);
+ kernel::state(s);
+ kernel::libs_state(s);
+ kernel::cdvd_state(s);
+ kernel::cdvdfsv_state(s);
+ hw::state(s);
+ sio2::state(s);
+ spu2::state(s);
+ io::state(s);
+}
+
+}
diff --git a/runtime/src/iop/kernel.hpp b/runtime/src/iop/kernel.hpp
index b2ad251..43159af 100644
--- a/runtime/src/iop/kernel.hpp
+++ b/runtime/src/iop/kernel.hpp
@@ -8,8 +8,17 @@
#include <string>
#include <vector>
+namespace ps2::state {
+class Stream;
+}
+
namespace ps2::iop::kernel {
+void state(ps2::state::Stream &s);
+void libs_state(ps2::state::Stream &s);
+void cdvd_state(ps2::state::Stream &s);
+void cdvdfsv_state(ps2::state::Stream &s);
+
enum Status { kRun = 1, kReady = 2, kWait = 4, kSuspend = 8, kWaitSuspend = 12, kDormant = 16 };
enum WaitKind { kWaitNone, kWaitSleep, kWaitDelay, kWaitSema, kWaitEvent, kWaitMbx, kWaitFpl, kWaitVblank, kWaitRpc, kWaitHle };
@@ -42,6 +51,29 @@ std::uint64_t now();
std::uint64_t usec_to_cycles(std::uint64_t usec);
void schedule(std::uint64_t at, std::function<void()> action);
+enum JobKind : std::uint32_t {
+ kJobNone,
+ kJobHardTimer,
+ kJobAlarm,
+ kJobDeleteThread,
+ kJobDelayWake,
+ kJobSpuTick,
+ kJobDmaDone,
+ kJobCdRead,
+ kJobFsvRead,
+ kJobFsvComplete,
+ kJobFsvIopRead,
+ kJobSifDmaCallback,
+ kJobCount
+};
+struct Job {
+ std::uint32_t kind = kJobNone;
+ std::uint32_t a = 0, b = 0, c = 0, d = 0;
+};
+using JobHandler = void (*)(const Job &);
+void register_job(std::uint32_t kind, JobHandler handler);
+void schedule(std::uint64_t at, const Job &job);
+
Thread *current();
Thread *thread(int id);
int create_thread(std::uint32_t entry, std::uint32_t stack_size, int priority, std::uint32_t gp, std::uint32_t attr, std::uint32_t option);
diff --git a/runtime/src/iop/libs.cpp b/runtime/src/iop/libs.cpp
index 4a32812..2c80087 100644
--- a/runtime/src/iop/libs.cpp
+++ b/runtime/src/iop/libs.cpp
@@ -2,6 +2,8 @@
#include "hw.hpp"
+#include "ps2/state.hpp"
+
#include <algorithm>
#include <cstdio>
#include <deque>
@@ -117,6 +119,9 @@ std::vector<int> g_vblank_start_waiters, g_vblank_end_waiters;
std::map<std::uint64_t, Alarm> g_alarms;
std::uint64_t g_next_alarm = 1;
int g_next_object = 1;
+std::uint64_t g_rotate_order = 1ull << 40;
+std::uint32_t g_strtok_next = 0;
+std::uint32_t g_next_hard_timer = 1;
void signal_sema(Cpu &c, int id, bool from_interrupt) {
auto it = g_semas.find(id);
@@ -212,7 +217,7 @@ void arm_hard_timer(std::uint32_t id) {
HardTimer &t = g_hard_timers[id];
const std::uint64_t generation = ++t.generation;
if (!t.running || !t.handler || !t.compare) return;
- schedule(now() + timer_cycles(t, t.compare), [id, generation]() { fire_hard_timer(id, generation); });
+ schedule(now() + timer_cycles(t, t.compare), Job{kJobHardTimer, id, static_cast<std::uint32_t>(generation), static_cast<std::uint32_t>(generation >> 32)});
}
void fire_alarm(std::uint64_t key) {
@@ -224,7 +229,7 @@ void fire_alarm(std::uint64_t key) {
if (again) {
const std::uint64_t k = g_next_alarm++;
g_alarms[k] = Alarm{a.handler, a.arg, true};
- schedule(now() + again, [k]() { fire_alarm(k); });
+ schedule(now() + again, Job{kJobAlarm, static_cast<std::uint32_t>(k), static_cast<std::uint32_t>(k >> 32)});
}
}
@@ -331,7 +336,7 @@ void install_thbase() {
Thread *t = current();
const int id = t ? t->id : 0;
exit_thread(c);
- if (id) schedule(now() + 1, [id]() { delete_thread(id); });
+ if (id) schedule(now() + 1, Job{kJobDeleteThread, static_cast<std::uint32_t>(id)});
});
auto terminate = [](Cpu &c) {
Thread *t = thread(static_cast<int>(arg(c, 0)));
@@ -358,9 +363,8 @@ void install_thbase() {
def("thbase", 15, "iChangeThreadPriority", change_priority);
auto rotate = [](Cpu &c) {
Thread *t = current();
- static std::uint64_t order = 1ull << 40;
if (t && (arg(c, 0) == 0 || static_cast<int>(arg(c, 0)) == t->priority)) {
- t->order = ++order;
+ t->order = ++g_rotate_order;
reschedule();
}
c.r[2] = 0;
@@ -437,17 +441,14 @@ void install_thbase() {
if (!t) return;
const int id = t->id;
block(c, kWaitDelay, 0);
- schedule(now() + std::max<std::uint64_t>(1, usec_to_cycles(arg(c, 0))), [id]() {
- Thread *th = thread(id);
- if (th && th->wait == kWaitDelay) wake(*th, 0);
- });
+ schedule(now() + std::max<std::uint64_t>(1, usec_to_cycles(arg(c, 0))), Job{kJobDelayWake, static_cast<std::uint32_t>(id)});
});
def("thbase", 34, "GetSystemTime", [](Cpu &c) { write_clock(arg(c, 0), now()); c.r[2] = 0; });
auto set_alarm = [](Cpu &c) {
const std::uint64_t delay = read_clock(arg(c, 0));
const std::uint64_t key = g_next_alarm++;
g_alarms[key] = Alarm{arg(c, 1), arg(c, 2), true};
- schedule(now() + std::max<std::uint64_t>(1, delay), [key]() { fire_alarm(key); });
+ schedule(now() + std::max<std::uint64_t>(1, delay), Job{kJobAlarm, static_cast<std::uint32_t>(key), static_cast<std::uint32_t>(key >> 32)});
c.r[2] = 0;
};
def("thbase", 35, "SetAlarm", set_alarm);
@@ -789,13 +790,12 @@ void install_sysclib() {
c.r[2] = pos == std::string::npos ? 0u : arg(c, 0) + static_cast<std::uint32_t>(pos);
});
def("sysclib", 35, "strtok", [](Cpu &c) {
- static std::uint32_t saved = 0;
- std::uint32_t p = arg(c, 0) ? arg(c, 0) : saved;
+ std::uint32_t p = arg(c, 0) ? arg(c, 0) : g_strtok_next;
const std::string delim = ram_string(arg(c, 1), 256);
const auto is_delim = [&delim](std::uint8_t ch) { return ch && delim.find(static_cast<char>(ch)) != std::string::npos; };
while (p && is_delim(read8(p))) ++p;
if (!p || !read8(p)) {
- saved = 0;
+ g_strtok_next = 0;
c.r[2] = 0;
return;
}
@@ -803,9 +803,9 @@ void install_sysclib() {
while (read8(p) && !is_delim(read8(p))) ++p;
if (read8(p)) {
write8(p, 0);
- saved = p + 1;
+ g_strtok_next = p + 1;
} else {
- saved = p;
+ g_strtok_next = p;
}
c.r[2] = token;
});
@@ -944,7 +944,7 @@ void install_vblank() {
void install_timrman() {
register_hle_library("timrman", 0x0101);
- def("timrman", 4, "AllocHardTimer", [](Cpu &c) { static std::uint32_t next = 1; c.r[2] = next++; });
+ def("timrman", 4, "AllocHardTimer", [](Cpu &c) { c.r[2] = g_next_hard_timer++; });
def("timrman", 5, "ReferHardTimer", [](Cpu &c) { c.r[2] = 1; });
def("timrman", 6, "FreeHardTimer", [](Cpu &c) { c.r[2] = 0; });
def("timrman", 7, "SetTimerMode", [](Cpu &c) { c.r[2] = 0; });
@@ -1058,7 +1058,19 @@ void install_libraries() {
g_vblank_start_waiters.clear();
g_vblank_end_waiters.clear();
g_alarms.clear();
+ g_hard_timers.clear();
g_next_object = 1;
+ g_next_alarm = 1;
+ g_rotate_order = 1ull << 40;
+ g_strtok_next = 0;
+ g_next_hard_timer = 1;
+ register_job(kJobHardTimer, [](const Job &j) { fire_hard_timer(j.a, j.b | (static_cast<std::uint64_t>(j.c) << 32)); });
+ register_job(kJobAlarm, [](const Job &j) { fire_alarm(j.a | (static_cast<std::uint64_t>(j.b) << 32)); });
+ register_job(kJobDeleteThread, [](const Job &j) { delete_thread(static_cast<int>(j.a)); });
+ register_job(kJobDelayWake, [](const Job &j) {
+ Thread *th = thread(static_cast<int>(j.a));
+ if (th && th->wait == kWaitDelay) wake(*th, 0);
+ });
install_sysmem();
install_loadcore();
install_intrman();
@@ -1077,6 +1089,50 @@ void install_libraries() {
install_misc();
}
+void libs_state(ps2::state::Stream &s) {
+ s.tag("ioplibs");
+ s.map(g_semas, [&s](Sema &v) {
+ s.pod(v.count);
+ s.pod(v.max);
+ s.pod(v.attr);
+ s.pod(v.option);
+ s.deque(v.waiters);
+ });
+ s.map(g_events, [&s](EventFlag &v) {
+ s.pod(v.bits);
+ s.pod(v.attr);
+ s.pod(v.option);
+ s.deque(v.waiters);
+ });
+ s.map(g_mbxs, [&s](Mbx &v) {
+ s.pod(v.attr);
+ s.pod(v.option);
+ s.deque(v.messages);
+ s.deque(v.waiters);
+ });
+ s.map(g_fpls, [&s](Fpl &v) {
+ s.pod(v.attr);
+ s.pod(v.option);
+ s.pod(v.block_size);
+ s.pod(v.blocks);
+ s.pod(v.base);
+ s.vec(v.free);
+ s.deque(v.waiters);
+ });
+ s.map(g_heaps, [&s](std::set<std::uint32_t> &v) { s.set(v); });
+ s.vec(g_vblank_handlers);
+ s.vec(g_vblank_start_waiters);
+ s.vec(g_vblank_end_waiters);
+ s.map(g_alarms);
+ s.map(g_hard_timers);
+ s.pod(g_next_alarm);
+ s.pod(g_next_object);
+ s.pod(g_rotate_order);
+ s.pod(g_strtok_next);
+ s.pod(g_next_hard_timer);
+ s.pod(g_dma_direction);
+}
+
}
namespace ps2::iop {
diff --git a/runtime/src/iop/sif.cpp b/runtime/src/iop/sif.cpp
index f40cf30..62018b7 100644
--- a/runtime/src/iop/sif.cpp
+++ b/runtime/src/iop/sif.cpp
@@ -7,8 +7,11 @@
#include "ps2/memory.hpp"
#include "ps2/system.hpp"
+#include "ps2/state.hpp"
+
#include <cstdio>
#include <deque>
+#include <stdexcept>
#include <map>
#include <vector>
@@ -73,6 +76,7 @@ std::uint32_t g_sreg[32] = {};
std::map<std::uint32_t, int> g_iop_clients;
std::uint32_t g_iop_rpc_id = 1;
std::uint32_t g_loop_stub = 0, g_finish_stub = 0;
+std::uint32_t g_iop_dma_id = 1, g_ee_dma_id = 1;
template <typename T>
T ee_read(std::uint32_t a) {
@@ -315,6 +319,7 @@ void rpc_finish(iop::Cpu &c) {
}
void install_iop_side() {
+ iop::kernel::register_job(iop::kernel::kJobSifDmaCallback, [](const iop::kernel::Job &j) { iop::kernel::call_guest(j.a, j.b); });
g_loop_stub = iop::kernel::hle_stub(rpc_loop);
g_finish_stub = iop::kernel::hle_stub(rpc_finish);
@@ -334,9 +339,8 @@ void install_iop_side() {
}
std::memcpy(g_ram + (dst & (kRamSize - 1)), iop::ram_ptr(src), std::min(size, kRamSize - (dst & (kRamSize - 1))));
}
- static std::uint32_t id = 1;
- c.r[2] = id++;
- if (callback) iop::kernel::schedule(iop::kernel::now() + 32, [callback, data]() { iop::kernel::call_guest(callback, data); });
+ c.r[2] = g_iop_dma_id++;
+ if (callback) iop::kernel::schedule(iop::kernel::now() + 32, iop::kernel::Job{iop::kernel::kJobSifDmaCallback, callback, data});
};
def("sifman", 7, "sceSifSetDma", [set_dma](iop::Cpu &c) { set_dma(c, 0, 0); });
def("sifman", 8, "sceSifDmaStat", [](iop::Cpu &c) { c.r[2] = static_cast<std::uint32_t>(-1); });
@@ -650,6 +654,10 @@ void reset() {
g_cmd_handlers.clear();
g_iop_clients.clear();
std::memset(g_sreg, 0, sizeof g_sreg);
+ g_iop_dma_id = g_ee_dma_id = 1;
+ g_iop_rpc_id = 1;
+ g_fileio_async = false;
+ g_fileio_result = 0;
}
std::uint32_t get_reg(std::uint32_t reg) {
@@ -670,7 +678,6 @@ void set_reg(std::uint32_t reg, std::uint32_t value) {
}
std::uint32_t set_dma(std::uint32_t transfers, std::uint32_t count) {
- static std::uint32_t id = 1;
if (!count) count = 1;
for (std::uint32_t i = 0; i < count; ++i) {
const std::uint32_t base = transfers + i * 16;
@@ -682,7 +689,7 @@ std::uint32_t set_dma(std::uint32_t transfers, std::uint32_t count) {
else if (dst == kCmdBuffer) command(src, size);
else std::memcpy(iop::ram_ptr(dst), g_ram + src, std::min(size, iop::kRamSize - (iop::phys(dst) & (iop::kRamSize - 1))));
}
- return id++;
+ return g_ee_dma_id++;
}
void packet_processed() {
@@ -763,3 +770,56 @@ void install_sif() {
}
}
+
+namespace ps2::sif {
+
+void state(ps2::state::Stream &s) {
+ s.tag("sif");
+ s.pod(g_mainaddr);
+ s.pod(g_msflag);
+ s.pod(g_smflag);
+ s.pod(g_ee_recv);
+ s.pod(g_data_addr);
+ s.pod(g_processed);
+ s.deque(g_outgoing, [&s](std::vector<std::uint8_t> &packet) { s.vec(packet); });
+ std::map<std::uint32_t, Server> servers;
+ if (s.saving()) servers = g_servers;
+ s.map(servers, [&s](Server &server) {
+ s.pod(server.hle);
+ s.pod(server.sd);
+ s.pod(server.func);
+ s.pod(server.buf);
+ s.pod(server.queue);
+ });
+ if (s.loading()) {
+ for (auto &[id, server] : servers) {
+ if (!server.hle) continue;
+ const auto it = g_servers.find(id);
+ if (it == g_servers.end() || !it->second.hle) throw std::runtime_error("an HLE RPC server is missing");
+ server.invoke = it->second.invoke;
+ }
+ g_servers = std::move(servers);
+ }
+ s.map(g_binds);
+ s.map(g_held_calls);
+ s.map(g_requests, [&s](std::deque<Request> &queue) {
+ s.deque(queue, [&s](Request &r) {
+ s.pod(r.call);
+ s.pod(r.size);
+ s.vec(r.data);
+ });
+ });
+ s.map(g_queue_threads);
+ s.map(g_cmd_handlers);
+ s.pod(g_sreg);
+ s.map(g_iop_clients);
+ s.pod(g_iop_rpc_id);
+ s.pod(g_loop_stub);
+ s.pod(g_finish_stub);
+ s.pod(g_iop_dma_id);
+ s.pod(g_ee_dma_id);
+ s.pod(g_fileio_async);
+ s.pod(g_fileio_result);
+}
+
+}
diff --git a/runtime/src/iop/sif.hpp b/runtime/src/iop/sif.hpp
index adbd0c6..614af34 100644
--- a/runtime/src/iop/sif.hpp
+++ b/runtime/src/iop/sif.hpp
@@ -3,8 +3,14 @@
#include <cstdint>
#include <functional>
+namespace ps2::state {
+class Stream;
+}
+
namespace ps2::sif {
+void state(ps2::state::Stream &s);
+
using HleServer = std::function<bool(std::uint32_t method, std::uint32_t args, std::uint32_t args_size, std::uint32_t ret, std::uint32_t ret_size)>;
void reset();
diff --git a/runtime/src/iop/sio2.cpp b/runtime/src/iop/sio2.cpp
index 2d13861..fa0070e 100644
--- a/runtime/src/iop/sio2.cpp
+++ b/runtime/src/iop/sio2.cpp
@@ -4,6 +4,8 @@
#include "iop.hpp"
#include "ps2/system.hpp"
+#include "ps2/state.hpp"
+
#include <algorithm>
#include <cstdio>
#include <cstdlib>
@@ -370,3 +372,22 @@ void set_axis(int pad, int axis, std::uint8_t value) {
}
}
+
+namespace ps2::iop::sio2 {
+
+void state(ps2::state::Stream &s) {
+ s.tag("sio2");
+ s.pod(g_send3);
+ s.pod(g_ctrl1);
+ s.pod(g_ctrl2);
+ s.pod(g_index);
+ s.pod(g_stat);
+ s.deque(g_in);
+ s.deque(g_out);
+ s.pod(g_pads);
+ s.pod(g_dma);
+ s.pod(g_card.address);
+ s.pod(g_card.term);
+}
+
+}
diff --git a/runtime/src/iop/sio2.hpp b/runtime/src/iop/sio2.hpp
index 0a013c1..263422b 100644
--- a/runtime/src/iop/sio2.hpp
+++ b/runtime/src/iop/sio2.hpp
@@ -2,8 +2,14 @@
#include <cstdint>
+namespace ps2::state {
+class Stream;
+}
+
namespace ps2::iop::sio2 {
+void state(ps2::state::Stream &s);
+
void reset();
std::uint32_t read(std::uint32_t a);
void write(std::uint32_t a, std::uint32_t v);
diff --git a/runtime/src/iop/spu2.cpp b/runtime/src/iop/spu2.cpp
index 8005b5f..1ecfe27 100644
--- a/runtime/src/iop/spu2.cpp
+++ b/runtime/src/iop/spu2.cpp
@@ -4,6 +4,8 @@
#include "iop.hpp"
#include "kernel.hpp"
+#include "ps2/state.hpp"
+
#include <algorithm>
#include <cstdio>
#include <cstdlib>
@@ -55,7 +57,7 @@ struct Voice {
struct Transfer {
std::uint32_t madr = 0, bytes = 0;
- std::function<void()> done;
+ int channel = -1;
};
struct Core {
@@ -210,9 +212,9 @@ void feed_input(int core) {
t.madr += chunk;
t.bytes -= chunk;
if (!t.bytes) {
- auto done = std::move(t.done);
+ const int channel = t.channel;
k.transfers.pop_front();
- if (done) kernel::schedule(kernel::now() + 32, std::move(done));
+ if (channel >= 0) kernel::schedule(kernel::now() + 32, kernel::Job{kernel::kJobDmaDone, static_cast<std::uint32_t>(channel)});
}
}
}
@@ -263,14 +265,14 @@ void tick() {
feed_input(0);
feed_input(1);
g_next_tick += kBatch * kCyclesPerSample;
- kernel::schedule(g_next_tick, tick);
+ kernel::schedule(g_next_tick, kernel::Job{kernel::kJobSpuTick});
}
void keep_ticking() {
if (g_tick_pending) return;
g_tick_pending = true;
g_next_tick = kernel::now() + kBatch * kCyclesPerSample;
- kernel::schedule(g_next_tick, tick);
+ kernel::schedule(g_next_tick, kernel::Job{kernel::kJobSpuTick});
}
std::uint32_t set_hi(std::uint32_t address, std::uint32_t v) { return ((v & 0xFu) << 16) | (address & 0xFFFFu); }
@@ -442,6 +444,7 @@ void write_global(std::uint32_t off, std::uint32_t value) {
}
void reset() {
+ kernel::register_job(kernel::kJobSpuTick, [](const kernel::Job &) { tick(); });
std::fill(g_ram.begin(), g_ram.end(), 0);
for (Core &k : g_cores) k = Core{};
g_spdif_out = g_spdif_mode = g_spdif_media = 0;
@@ -480,11 +483,11 @@ void write(std::uint32_t a, std::uint32_t v, unsigned size) {
} else write_global(off, v);
}
-void dma(int core, std::uint32_t madr, std::uint32_t words, bool to_spu, std::function<void()> done) {
+void dma(int core, std::uint32_t madr, std::uint32_t words, bool to_spu, int channel) {
keep_ticking();
Core &k = g_cores[core];
if (to_spu && (k.admas & (1u << core))) {
- k.transfers.push_back(Transfer{madr, words * 4, std::move(done)});
+ k.transfers.push_back(Transfer{madr, words * 4, channel});
feed_input(core);
return;
}
@@ -497,7 +500,7 @@ void dma(int core, std::uint32_t madr, std::uint32_t words, bool to_spu, std::fu
else std::memcpy(iop, spu, 2);
}
k.tsa = (k.tsa + halfwords) & kRamMask;
- kernel::schedule(kernel::now() + 64 + words / 4, std::move(done));
+ if (channel >= 0) kernel::schedule(kernel::now() + 64 + words / 4, kernel::Job{kernel::kJobDmaDone, static_cast<std::uint32_t>(channel)});
}
void set_sink(std::function<void(const std::int16_t *, std::size_t)> sink) { g_sink = std::move(sink); }
@@ -525,3 +528,38 @@ void dump_audio(const char *path) {
}
}
+
+namespace ps2::iop::spu2 {
+
+void state(ps2::state::Stream &s) {
+ s.tag("spu2");
+ s.vec(g_ram);
+ for (Core &k : g_cores) {
+ s.pod(k.voices);
+ s.pod(k.attr);
+ s.pod(k.admas);
+ s.pod(k.irqa);
+ s.pod(k.tsa);
+ s.pod(k.mvol);
+ s.pod(k.bvol);
+ s.pod(k.avol);
+ s.pod(k.evol);
+ s.pod(k.irq);
+ s.pod(k.input);
+ s.pod(k.input_read);
+ s.pod(k.input_count);
+ s.deque(k.transfers, [&s](Transfer &t) {
+ s.pod(t.madr);
+ s.pod(t.bytes);
+ s.pod(t.channel);
+ });
+ s.pod(k.regs);
+ }
+ s.pod(g_spdif_out);
+ s.pod(g_spdif_mode);
+ s.pod(g_spdif_media);
+ s.pod(g_next_tick);
+ s.pod(g_tick_pending);
+}
+
+}
diff --git a/runtime/src/iop/spu2.hpp b/runtime/src/iop/spu2.hpp
index f8e0580..ce03c9e 100644
--- a/runtime/src/iop/spu2.hpp
+++ b/runtime/src/iop/spu2.hpp
@@ -3,13 +3,19 @@
#include <cstdint>
#include <functional>
+namespace ps2::state {
+class Stream;
+}
+
namespace ps2::iop::spu2 {
+void state(ps2::state::Stream &s);
+
void reset();
std::uint16_t *ram();
std::uint32_t read(std::uint32_t a, unsigned size);
void write(std::uint32_t a, std::uint32_t v, unsigned size);
-void dma(int core, std::uint32_t madr, std::uint32_t words, bool to_spu, std::function<void()> done);
+void dma(int core, std::uint32_t madr, std::uint32_t words, bool to_spu, int channel);
void dump_audio(const char *path);
void dump_voices();
void set_sink(std::function<void(const std::int16_t *samples, std::size_t frames)> sink);
diff --git a/runtime/src/ipu/ipu.cpp b/runtime/src/ipu/ipu.cpp
index fb7b19d..facd227 100644
--- a/runtime/src/ipu/ipu.cpp
+++ b/runtime/src/ipu/ipu.cpp
@@ -4,6 +4,8 @@
#include "../dmac.hpp"
#include "../hw.hpp"
+#include "ps2/state.hpp"
+
#include <algorithm>
#include <cmath>
#include <cstdio>
@@ -682,3 +684,33 @@ void stats() {
}
}
+
+namespace ps2::ipu {
+
+void state(ps2::state::Stream &s) {
+ s.tag("ipu");
+ s.vec(g_in);
+ s.pod(g_bit);
+ s.vec(g_out);
+ s.pod(g_out_pos);
+ s.pod(g_ctrl);
+ s.pod(g_result);
+ s.pod(g_command);
+ s.pod(g_last);
+ s.pod(g_busy);
+ s.pod(g_decoding);
+ s.pod(g_interrupt);
+ s.pod(g_starved);
+ s.pod(g_phase);
+ s.pod(g_qsc);
+ s.pod(g_count);
+ s.pod(g_first);
+ s.pod(g_intra_iq);
+ s.pod(g_nonintra_iq);
+ s.pod(g_vqclut);
+ s.pod(g_th0);
+ s.pod(g_th1);
+ s.pod(g_dc);
+}
+
+}
diff --git a/runtime/src/ipu/ipu.hpp b/runtime/src/ipu/ipu.hpp
index 6a4fbd1..5dc3f1e 100644
--- a/runtime/src/ipu/ipu.hpp
+++ b/runtime/src/ipu/ipu.hpp
@@ -2,8 +2,14 @@
#include <cstdint>
+namespace ps2::state {
+class Stream;
+}
+
namespace ps2::ipu {
+void state(ps2::state::Stream &s);
+
void reset();
std::uint32_t read32(std::uint32_t a);
void write32(std::uint32_t a, std::uint32_t v);
diff --git a/runtime/src/kernel.cpp b/runtime/src/kernel.cpp
index 543069b..47de34f 100644
--- a/runtime/src/kernel.cpp
+++ b/runtime/src/kernel.cpp
@@ -6,7 +6,10 @@
#include "ps2/memory.hpp"
#include "ps2/system.hpp"
+#include "ps2/state.hpp"
+
#include <algorithm>
+#include <stdexcept>
#include <cstdio>
#include <cstdlib>
#include <cstring>
@@ -829,3 +832,38 @@ void set_interrupts(Cpu &c, bool enabled) {
}
}
+
+namespace ps2::kernel {
+
+std::function<void()> alarm_action(int id) {
+ return [id]() { fire_alarm(id); };
+}
+
+void state(ps2::state::Stream &s) {
+ s.tag("eekern");
+ if (s.saving() && (g_in_handler || !g_guest_calls.empty())) throw std::runtime_error("the EE is inside an interrupt handler");
+ s.map(g_threads);
+ s.map(g_semas, [&s](Semaphore &v) {
+ s.pod(v.count);
+ s.pod(v.max);
+ s.pod(v.init);
+ s.pod(v.attr);
+ s.pod(v.option);
+ s.deque(v.waiters);
+ });
+ s.vec(g_intc_handlers);
+ s.vec(g_dmac_handlers);
+ s.map(g_alarms);
+ s.pod(g_current);
+ s.pod(g_next_handler);
+ s.pod(g_order);
+ s.pod(g_reschedule);
+ s.vec(g_pending_alarms);
+ s.pod(g_vsync_flag);
+ s.pod(g_vsync_data);
+ s.pod(g_osd_config);
+ s.pod(g_boot_argc);
+ s.pod(g_boot_argv);
+}
+
+}
diff --git a/runtime/src/kernel.hpp b/runtime/src/kernel.hpp
index 08c176b..001f238 100644
--- a/runtime/src/kernel.hpp
+++ b/runtime/src/kernel.hpp
@@ -3,9 +3,17 @@
#include "ps2/cpu.hpp"
#include <cstdint>
+#include <functional>
+
+namespace ps2::state {
+class Stream;
+}
namespace ps2::kernel {
+void state(ps2::state::Stream &s);
+std::function<void()> alarm_action(int id);
+
void reset();
void start_main_thread(std::uint32_t entry, std::uint32_t gp, std::uint32_t argc, std::uint32_t argv);
void handle_syscall(Cpu &c, std::uint32_t pc);
diff --git a/runtime/src/system.cpp b/runtime/src/system.cpp
index 3f95806..8943b0b 100644
--- a/runtime/src/system.cpp
+++ b/runtime/src/system.cpp
@@ -14,6 +14,7 @@
#include "kernel.hpp"
#include "ps2/generated.hpp"
#include "ps2/memory.hpp"
+#include "ps2/state.hpp"
#include "ps2/system.hpp"
#ifdef __EMSCRIPTEN__
@@ -29,6 +30,7 @@
#include <fstream>
#include <cstdlib>
#include <iterator>
+#include <stdexcept>
namespace ps2 {
@@ -52,6 +54,8 @@ bool g_halted = false;
std::string g_halt_reason;
std::uint64_t g_frames = 0;
std::uint64_t g_iop_synced = 0;
+bool g_state_request = false;
+bool g_resumed = false;
std::uint64_t next_event_time() {
std::uint64_t t = UINT64_MAX;
@@ -133,6 +137,7 @@ void vblank() {
schedule(hw::kEventVblankEnd + 100, now() + 18874u * 22u, [] { iop::vblank(false); });
kernel::on_vblank_start();
if (g_options.max_frames && g_frames >= g_options.max_frames) halt("frame limit reached");
+ if (g_options.save_state_frame && g_frames == g_options.save_state_frame) g_state_request = true;
schedule(hw::kEventVblank, now() + frame_cycles(), vblank);
}
@@ -189,7 +194,7 @@ void schedule(int event, std::uint64_t at, std::function<void()> action) {
bool preempt(Cpu &c) {
run_due_events();
refill(c);
- return g_halted || kernel::must_unwind();
+ return g_halted || g_state_request || kernel::must_unwind();
}
void halt(const std::string &reason) {
@@ -200,6 +205,86 @@ void halt(const std::string &reason) {
bool halted() { return g_halted; }
+namespace {
+
+constexpr char kStateMagic[8] = {'P', 'S', '2', 'S', 'T', 'A', 'T', 'E'};
+constexpr std::uint32_t kStateVersion = 1;
+
+std::function<void()> event_action(int id) {
+ if (id == hw::kEventVblank) return vblank;
+ if (id == hw::kEventVblankEnd + 100) return [] { iop::vblank(false); };
+ if (id >= hw::kEventCount) return kernel::alarm_action(id - hw::kEventCount);
+ return hw::event_action(id);
+}
+
+void machine_state(state::Stream &s) {
+ char magic[8];
+ std::memcpy(magic, kStateMagic, sizeof magic);
+ s.bytes(magic, sizeof magic);
+ std::uint32_t version = kStateVersion;
+ s.pod(version);
+ if (std::memcmp(magic, kStateMagic, sizeof magic) != 0) throw std::runtime_error("not a state file");
+ if (version != kStateVersion) throw std::runtime_error("state file from another version");
+ s.tag("ee");
+ s.pod(g_cpu);
+ s.bytes(g_ram, kRamSize);
+ s.bytes(g_spr, kScratchpadSize);
+ s.pod(g_base);
+ s.pod(g_given);
+ s.pod(g_frames);
+ s.pod(g_iop_synced);
+ s.pod(g_options.pal);
+ s.vec(g_events, [&s](Event &e) {
+ s.pod(e.id);
+ s.pod(e.at);
+ if (s.loading()) e.action = event_action(e.id);
+ });
+ kernel::state(s);
+ hw::state(s);
+ dmac::state(s);
+ vif::state(s);
+ vu::state(s);
+ gif::state(s);
+ ipu::state(s);
+ gs::state(s);
+ iop::state(s);
+ sif::state(s);
+ s.tag("end");
+}
+
+}
+
+bool save_state(const std::string &path) {
+ try {
+ state::Writer w;
+ machine_state(w);
+ std::ofstream out(path, std::ios::binary);
+ out.write(reinterpret_cast<const char *>(w.out.data()), static_cast<std::streamsize>(w.out.size()));
+ if (!out) throw std::runtime_error("cannot write " + path);
+ std::fprintf(stderr, "[state] saved frame %llu to %s (%zu KB)\n", static_cast<unsigned long long>(g_frames), path.c_str(), w.out.size() >> 10);
+ return true;
+ } catch (const std::exception &e) {
+ std::fprintf(stderr, "[state] cannot save: %s\n", e.what());
+ return false;
+ }
+}
+
+bool load_state(const std::string &path) {
+ try {
+ std::ifstream in(path, std::ios::binary);
+ if (!in) throw std::runtime_error("cannot open " + path);
+ const std::vector<std::uint8_t> data{std::istreambuf_iterator<char>(in), {}};
+ state::Reader r(data.data(), data.size());
+ machine_state(r);
+ g_resumed = true;
+ std::fprintf(stderr, "[state] loaded frame %llu from %s\n", static_cast<unsigned long long>(g_frames), path.c_str());
+ return true;
+ } catch (const std::exception &e) {
+ std::fprintf(stderr, "[state] cannot load: %s\n", e.what());
+ return false;
+ }
+}
+
std::uint64_t frame_count() { return g_frames; }
bool load_elf(const std::string &path, std::uint32_t &entry) {
@@ -261,8 +346,12 @@ void boot(std::uint32_t entry, const std::vector<std::string> &args) {
int run() {
Cpu &c = g_cpu;
- set_budget(c, kMaxSlice);
+ if (!g_resumed) set_budget(c, kMaxSlice);
while (!g_halted) {
+ if (g_state_request) {
+ g_state_request = false;
+ save_state(g_options.save_state_path);
+ }
c.pc &= ~kUnwind;
run_due_events();
kernel::service(c);
diff --git a/runtime/src/vif.cpp b/runtime/src/vif.cpp
index 9a6a1e9..d202a69 100644
--- a/runtime/src/vif.cpp
+++ b/runtime/src/vif.cpp
@@ -6,6 +6,8 @@
#include "hw.hpp"
#include "vu_micro.hpp"
+#include "ps2/state.hpp"
+
#include <cstdio>
#include <cstring>
#include <vector>
@@ -348,3 +350,32 @@ void write(int unit, std::uint32_t reg, std::uint32_t value) {
}
}
+
+namespace ps2::vif {
+
+void state(ps2::state::Stream &s) {
+ s.tag("vif");
+ for (Unit *u : {&g_units[0], &g_units[1], &g_vu_side}) {
+ s.pod(u->stat);
+ s.pod(u->err);
+ s.pod(u->mark);
+ s.pod(u->cycle);
+ s.pod(u->mode);
+ s.pod(u->num);
+ s.pod(u->mask);
+ s.pod(u->code);
+ s.pod(u->itops);
+ s.pod(u->base);
+ s.pod(u->ofst);
+ s.pod(u->tops);
+ s.pod(u->itop);
+ s.pod(u->top);
+ s.pod(u->row);
+ s.pod(u->col);
+ s.pod(u->dbf);
+ s.pod(u->stalled);
+ s.vec(u->pending);
+ }
+}
+
+}
diff --git a/runtime/src/vif.hpp b/runtime/src/vif.hpp
index 49802c4..45624eb 100644
--- a/runtime/src/vif.hpp
+++ b/runtime/src/vif.hpp
@@ -2,8 +2,14 @@
#include <cstdint>
+namespace ps2::state {
+class Stream;
+}
+
namespace ps2::vif {
+void state(ps2::state::Stream &s);
+
void reset();
void reset_vu_side();
void stats();
diff --git a/runtime/src/vu_micro.cpp b/runtime/src/vu_micro.cpp
index 97afc87..63ebba1 100644
--- a/runtime/src/vu_micro.cpp
+++ b/runtime/src/vu_micro.cpp
@@ -9,6 +9,8 @@
#include "ps2/vu.hpp"
#include "ps2/vu_ops.hpp"
+#include "ps2/state.hpp"
+
#include <atomic>
#include <cmath>
#include <cstdio>
@@ -907,3 +909,22 @@ void vcallms(Cpu &, std::uint32_t address) { vu::run(0, address); }
}
__attribute__((weak)) void register_generated_vu() {}
+
+namespace ps2::vu {
+
+void state(ps2::state::Stream &s) {
+ s.tag("vu");
+ s.pod(g_vu1);
+ s.pod(g_top);
+ s.pod(g_itop);
+ s.bytes(g_vu0_code, sizeof g_vu0_code);
+ s.bytes(g_vu0_mem, sizeof g_vu0_mem);
+ s.bytes(g_vu1_code, sizeof g_vu1_code);
+ s.bytes(g_vu1_mem, sizeof g_vu1_mem);
+ if (s.loading()) {
+ code_changed(0);
+ code_changed(1);
+ }
+}
+
+}
diff --git a/runtime/src/vu_micro.hpp b/runtime/src/vu_micro.hpp
index 65718a8..2b853a1 100644
--- a/runtime/src/vu_micro.hpp
+++ b/runtime/src/vu_micro.hpp
@@ -4,8 +4,14 @@
#include <cstdint>
+namespace ps2::state {
+class Stream;
+}
+
namespace ps2::vu {
+void state(ps2::state::Stream &s);
+
void reset();
void run(int unit, std::uint32_t address);
std::uint32_t tpc(int unit);