Skip to content

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

C++

Bring up Burnout 3 Takedown to its front-end menus

Adrian Gąsioreka85a2b80f03e
diff --git a/runtime/src/gs/gif.cpp b/runtime/src/gs/gif.cpp
index e9937dd..74c6c88 100644
--- a/runtime/src/gs/gif.cpp
+++ b/runtime/src/gs/gif.cpp
@@ -186,14 +186,20 @@ void stats() {
}
std::uint32_t read(std::uint32_t a) {
- if ((a & 0xF0u) == 0x20u) return g_stat;
+ // While PATH3 is masked a real GIF holds the stalled channel 2 transfer in its FIFO, and
+ // games wait for FQC to read non-zero before they touch the GS; transfers here are
+ // immediate, so report the FIFO full for as long as the mask is up.
+ if ((a & 0xF0u) == 0x20u) return g_stat | ((g_stat & 2u) ? 16u << 24 : 0u);
return 0;
}
void write(std::uint32_t a, std::uint32_t v) {
if ((a & 0xF0u) == 0x00u && (v & 1u)) reset();
+ if ((a & 0xF0u) == 0x10u) g_stat = (g_stat & ~1u) | (v & 1u);
}
+void set_path3_masked(bool masked) { g_stat = (g_stat & ~2u) | (masked ? 2u : 0u); }
+
}
namespace ps2::gif {
diff --git a/runtime/src/gs/gif.hpp b/runtime/src/gs/gif.hpp
index 6eb7f3b..4b3fcd4 100644
--- a/runtime/src/gs/gif.hpp
+++ b/runtime/src/gs/gif.hpp
@@ -19,5 +19,7 @@ void scan_signals(int path, const std::uint8_t *data, std::uint32_t qwc);
bool busy(int path);
std::uint32_t read(std::uint32_t a);
void write(std::uint32_t a, std::uint32_t v);
+// MSKPATH3 from VIF1, reported back to the EE in GIF_STAT.M3P
+void set_path3_masked(bool masked);
}
diff --git a/runtime/src/hw.cpp b/runtime/src/hw.cpp
index 6a4d746..cb1fc99 100644
--- a/runtime/src/hw.cpp
+++ b/runtime/src/hw.cpp
@@ -25,7 +25,10 @@ namespace {
constexpr std::uint32_t kIntcStat = 0x1000F000u, kIntcMask = 0x1000F010u;
constexpr std::uint32_t kDmacStat = 0x1000E010u;
-std::uint32_t g_intc_stat = 0, g_intc_mask = 0;
+// g_intc_latched is what the guest reads back: the kernel acknowledging an interrupt before
+// running the handlers must not hide it from code that polls INTC_STAT, the way sceGsSyncV
+// waits for VBlank start while a VBlank handler is installed. Only a guest write clears it.
+std::uint32_t g_intc_stat = 0, g_intc_mask = 0, g_intc_latched = 0;
std::uint32_t g_dmac_stat = 0;
std::map<std::uint32_t, std::uint64_t> g_regs;
std::set<std::uint32_t> g_reported;
@@ -108,7 +111,7 @@ bool is_timer(std::uint32_t a) { return a >= 0x10000000u && a < 0x10002000u && (
}
void reset() {
- g_intc_stat = g_intc_mask = g_dmac_stat = 0;
+ g_intc_stat = g_intc_mask = g_intc_latched = g_dmac_stat = 0;
gs::reset();
gif::reset();
vif::reset();
@@ -130,6 +133,7 @@ std::uint32_t intc_pending() { return g_intc_stat & g_intc_mask; }
void intc_raise(unsigned cause) {
g_intc_stat |= 1u << cause;
+ g_intc_latched |= 1u << cause;
if (g_intc_mask & (1u << cause)) kernel::wake_idle();
}
@@ -176,7 +180,7 @@ void dmac_raise(unsigned channel) {
}
std::uint32_t read32(std::uint32_t a) {
- if (a == kIntcStat) return g_intc_stat;
+ if (a == kIntcStat) return g_intc_stat | g_intc_latched;
if (a == kIntcMask) return g_intc_mask;
if (a == kDmacStat) return g_dmac_stat;
if (a >= 0x12000000u && a < 0x12002000u) return static_cast<std::uint32_t>(gs::read_privileged(a & ~7u) >> ((a & 4u) * 8));
@@ -204,7 +208,7 @@ std::uint32_t read32(std::uint32_t a) {
}
void write32(std::uint32_t a, std::uint32_t v) {
- if (a == kIntcStat) { g_intc_stat &= ~v; return; }
+ if (a == kIntcStat) { g_intc_stat &= ~v; g_intc_latched &= ~v; return; }
if (a == kIntcMask) { g_intc_mask ^= v; return; }
if (a == kDmacStat) {
g_dmac_stat &= ~(v & 0xFFFFu);
diff --git a/runtime/src/iop/libs.cpp b/runtime/src/iop/libs.cpp
index 44af960..f78f456 100644
--- a/runtime/src/iop/libs.cpp
+++ b/runtime/src/iop/libs.cpp
@@ -887,6 +887,20 @@ void install_modload() {
if (arg(c, 2)) write32(arg(c, 2), n);
c.r[2] = 0;
});
+ def("modload", 18, "GetModuleIdListByName", [](Cpu &c) {
+ std::string name = ram_string(arg(c, 0), 64);
+ for (char &ch : name) ch = static_cast<char>(std::tolower(static_cast<unsigned char>(ch)));
+ std::uint32_t n = 0;
+ for (const auto &[id, m] : modules()) {
+ std::string a = m.name;
+ for (char &ch : a) ch = static_cast<char>(std::tolower(static_cast<unsigned char>(ch)));
+ if (a != name) continue;
+ if (n < arg(c, 2)) write32(arg(c, 1) + 4 * n, static_cast<std::uint32_t>(id));
+ ++n;
+ }
+ if (arg(c, 3)) write32(arg(c, 3), n);
+ c.r[2] = 0;
+ });
def("modload", 17, "ReferModuleStatus", [](Cpu &c) {
auto it = modules().find(static_cast<int>(arg(c, 0)));
if (it == modules().end()) { c.r[2] = static_cast<std::uint32_t>(kErrorUnknownModule); return; }
diff --git a/runtime/src/iop/sif.cpp b/runtime/src/iop/sif.cpp
index 5c744c4..9bf4d7b 100644
--- a/runtime/src/iop/sif.cpp
+++ b/runtime/src/iop/sif.cpp
@@ -647,6 +647,9 @@ void install_servers() {
switch (method) {
case 1: store<std::uint32_t>(g_ram + ret, iop::kernel::alloc(ee_read<std::uint32_t>(args))); break;
case 2: iop::kernel::free(ee_read<std::uint32_t>(args)); store<std::uint32_t>(g_ram + ret, 0); break;
+ // sceSifAllocSysMemory: size first, then the allocation type and address, which a
+ // first-fit heap can ignore. Answering 0 here sends the game to wipe IOP memory from 0.
+ case 4: store<std::uint32_t>(g_ram + ret, iop::kernel::alloc(ee_read<std::uint32_t>(args))); break;
case 5: store<std::uint32_t>(g_ram + ret, iop::kRamSize); break;
case 6: store<std::uint32_t>(g_ram + ret, iop::kernel::free_max()); break;
case 7: store<std::uint32_t>(g_ram + ret, iop::kernel::free_total()); break;
diff --git a/runtime/src/iop/spu2.cpp b/runtime/src/iop/spu2.cpp
index 531c764..d6287da 100644
--- a/runtime/src/iop/spu2.cpp
+++ b/runtime/src/iop/spu2.cpp
@@ -24,6 +24,8 @@ constexpr unsigned kBatch = 32;
constexpr unsigned kInputFrames = 512;
constexpr std::uint32_t kInputBase[2] = {0x2000u, 0x2400u};
constexpr unsigned kSpuLine = 9;
+constexpr std::uint32_t kRingWords = 0x200;
+static_assert(kRingWords % kBatch == 0, "a batch must not straddle the ring end");
enum Phase { kOff, kAttack, kDecay, kSustain, kRelease };
@@ -86,6 +88,7 @@ bool g_record = false;
std::function<void(const std::int16_t *, std::size_t)> g_sink;
std::uint64_t g_next_tick = 0;
bool g_tick_pending = false;
+std::uint32_t g_ring_pos = 0;
void check_irq(std::uint32_t address, std::uint32_t words) {
for (Core &k : g_cores) {
@@ -170,7 +173,9 @@ std::int32_t volume(std::uint16_t reg, Envelope &e) {
return e.level;
}
-void render_voice(Voice &v, std::int32_t (*sum)[2]) {
+std::int16_t clamp16(std::int32_t v) { return static_cast<std::int16_t>(std::clamp(v, -32768, 32767)); }
+
+void render_voice(Voice &v, std::int32_t (*sum)[2], std::int16_t *raw) {
if (v.key_on) key_on(v);
const std::uint32_t step = std::min<std::uint32_t>(v.pitch, 0x3FFFu);
const bool sweep_l = v.voll & 0x8000u, sweep_r = v.volr & 0x8000u;
@@ -193,6 +198,7 @@ void render_voice(Voice &v, std::int32_t (*sum)[2]) {
}
update_envelope(v);
const std::int32_t out = sample * v.env.level >> 15;
+ if (raw) raw[n] = clamp16(out);
sum[n][0] += out * (sweep_l ? volume(v.voll, v.vol[0]) : fixed_l) >> 15;
sum[n][1] += out * (sweep_r ? volume(v.volr, v.vol[1]) : fixed_r) >> 15;
}
@@ -227,11 +233,13 @@ void feed_input(int core) {
}
}
-void render_input(int core, std::int32_t &left, std::int32_t &right) {
+void render_input(int core, std::int32_t &left, std::int32_t &right, std::int16_t &raw_l, std::int16_t &raw_r) {
Core &k = g_cores[core];
if (!k.input_count) return;
const unsigned slot = k.input_read;
check_irq(kInputBase[core] + slot, 1);
+ raw_l = k.input[slot][0];
+ raw_r = k.input[slot][1];
left += k.input[slot][0] * static_cast<std::int16_t>(k.bvol[0]) >> 15;
right += k.input[slot][1] * static_cast<std::int16_t>(k.bvol[1]) >> 15;
k.input_read = (k.input_read + 1) % kInputFrames;
@@ -240,31 +248,59 @@ void render_input(int core, std::int32_t &left, std::int32_t &right) {
std::int32_t master(std::uint16_t reg) { return reg & 0x8000u ? 0x7FFF : static_cast<std::int16_t>(reg << 1); }
+// Each core streams every sample into fixed rings in SPU RAM, per core c at c*0x800: input L/R (core 1's input is core 0's
+// output), voice 1, voice 3; at 0x1000+c*0x800: dry L/R, wet L/R. Those writes trip IRQA, and some games (Burnout 3:
+// IRQA 0x400) use that as their audio clock. One range check per ring per batch keeps it cheap.
+void write_rings(int core, const std::int16_t (*rings)[kBatch]) {
+ for (std::uint32_t r = 0; r < 8; ++r) {
+ const std::uint32_t at = (r < 4 ? 0u : 0x1000u) + core * 0x800u + (r & 3u) * kRingWords + g_ring_pos;
+ for (unsigned n = 0; n < kBatch; ++n) g_ram[at + n] = static_cast<std::uint16_t>(rings[r][n]);
+ check_irq(at, kBatch);
+ }
+}
+
void tick() {
std::int16_t out[kBatch * 2];
std::int32_t sum[2][kBatch][2] = {};
+ std::int16_t rings[2][8][kBatch] = {};
static const bool count = std::getenv("PS2_SPU2_COUNT") != nullptr;
static std::uint64_t ticks = 0, active = 0, phases[5] = {};
for (int c = 0; c < 2; ++c) {
- for (Voice &v : g_cores[c].voices) {
+ for (int i = 0; i < 24; ++i) {
+ Voice &v = g_cores[c].voices[i];
if (!v.key_on && v.phase == kOff) continue;
if (count) {
++active;
++phases[v.phase];
}
- render_voice(v, sum[c]);
+ render_voice(v, sum[c], i == 1 ? rings[c][2] : i == 3 ? rings[c][3] : nullptr);
+ }
+ // Reverb is not modelled, so the wet rings stay silent.
+ for (unsigned n = 0; n < kBatch; ++n) {
+ rings[c][4][n] = clamp16(sum[c][n][0]);
+ rings[c][5][n] = clamp16(sum[c][n][1]);
}
- for (unsigned n = 0; n < kBatch; ++n) render_input(c, sum[c][n][0], sum[c][n][1]);
+ std::int16_t unused[2];
+ for (unsigned n = 0; n < kBatch; ++n)
+ render_input(c, sum[c][n][0], sum[c][n][1], c ? unused[0] : rings[0][0][n], c ? unused[1] : rings[0][1][n]);
}
for (unsigned n = 0; n < kBatch; ++n) {
std::int32_t mix[2] = {};
for (int c = 0; c < 2; ++c) {
- mix[0] += sum[c][n][0] * master(g_cores[c].mvol[0]) >> 15;
- mix[1] += sum[c][n][1] * master(g_cores[c].mvol[1]) >> 15;
+ const std::int32_t l = sum[c][n][0] * master(g_cores[c].mvol[0]) >> 15, r = sum[c][n][1] * master(g_cores[c].mvol[1]) >> 15;
+ if (!c) {
+ rings[1][0][n] = clamp16(l);
+ rings[1][1][n] = clamp16(r);
+ }
+ mix[0] += l;
+ mix[1] += r;
}
- out[n * 2] = static_cast<std::int16_t>(std::clamp(mix[0], -32768, 32767));
- out[n * 2 + 1] = static_cast<std::int16_t>(std::clamp(mix[1], -32768, 32767));
+ out[n * 2] = clamp16(mix[0]);
+ out[n * 2 + 1] = clamp16(mix[1]);
}
+ write_rings(0, rings[0]);
+ write_rings(1, rings[1]);
+ g_ring_pos = (g_ring_pos + kBatch) % kRingWords;
if (count && ++ticks % 15000 == 0)
std::fprintf(stderr, "[spu2] %.1f voices per batch (attack %.1f decay %.1f sustain %.1f release %.1f)\n", static_cast<double>(active) / ticks,
static_cast<double>(phases[1]) / ticks, static_cast<double>(phases[2]) / ticks, static_cast<double>(phases[3]) / ticks, static_cast<double>(phases[4]) / ticks);
@@ -463,6 +499,7 @@ void reset() {
g_record = std::getenv("PS2_DUMP_AUDIO") != nullptr;
g_next_tick = 0;
g_tick_pending = false;
+ g_ring_pos = 0;
}
std::uint16_t *ram() { return g_ram.data(); }
@@ -574,6 +611,7 @@ void state(ps2::state::Stream &s) {
s.pod(g_spdif_media);
s.pod(g_next_tick);
s.pod(g_tick_pending);
+ s.pod(g_ring_pos);
}
}
diff --git a/runtime/src/system.cpp b/runtime/src/system.cpp
index ca8f06c..c1bf803 100644
--- a/runtime/src/system.cpp
+++ b/runtime/src/system.cpp
@@ -210,7 +210,7 @@ bool halted() { return g_halted; }
namespace {
constexpr char kStateMagic[8] = {'P', 'S', '2', 'S', 'T', 'A', 'T', 'E'};
-constexpr std::uint32_t kStateVersion = 2;
+constexpr std::uint32_t kStateVersion = 3;
std::function<void()> event_action(int id) {
if (id == hw::kEventVblank) return vblank;
diff --git a/runtime/src/vif.cpp b/runtime/src/vif.cpp
index c6a718a..7060071 100644
--- a/runtime/src/vif.cpp
+++ b/runtime/src/vif.cpp
@@ -229,7 +229,7 @@ void execute(int unit, std::uint32_t cmd, const std::uint32_t *data) {
case 0x03: u.base = imm & 0x3FFu; break;
case 0x04: u.itops = imm & 0x3FFu; break;
case 0x05: u.mode = imm & 3u; break;
- case 0x06: break;
+ case 0x06: gif::set_path3_masked((imm >> 15) & 1u); break;
case 0x07: u.mark = imm; break;
case 0x10: case 0x11: case 0x13: break;
case 0x14: case 0x15: run_vu(unit, imm * 8u); break;