Skip to content

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

C++

gs: read half-height buffers in interlaced frame mode

Samir Nuri219e2a261f04
diff --git a/runtime/src/gs/gs.cpp b/runtime/src/gs/gs.cpp
index d2a1822..393ee92 100644
--- a/runtime/src/gs/gs.cpp
+++ b/runtime/src/gs/gs.cpp
@@ -560,25 +560,25 @@ bool display(std::vector<std::uint32_t> &rgba, std::uint32_t &width, std::uint32
width = dw / magh;
height = dh / magv;
if (!width || !height || width > 2048 || height > 2048) return false;
- const bool frame_mode = (g_smode2 & 2u) && (g_smode2 & 1u);
+ const bool halved = (g_smode2 & 3u) == 3u;
rgba.resize(static_cast<std::size_t>(width) * height);
for (std::uint32_t y = 0; y < height; ++y)
for (std::uint32_t x = 0; x < width; ++x) {
- std::uint32_t c = read_pixel(psm, fbp * 32, fbw, dbx + x, dby + y);
+ std::uint32_t c = read_pixel(psm, fbp * 32, fbw, dbx + x, dby + (halved ? y / 2 : y));
if (psm == PSMCT16 || psm == PSMCT16S)
c = ((c & 0x1Fu) << 3) | ((c & 0x3E0u) << 6) | ((c & 0x7C00u) << 9) | 0xFF000000u;
rgba[static_cast<std::size_t>(y) * width + x] = c | 0xFF000000u;
}
- (void)frame_mode;
return true;
}
-std::uint64_t primitives_drawn() {
- std::fprintf(stderr, "[gs] writes:");
+std::uint64_t primitives_drawn() { return g_primitives; }
+
+void stats() {
+ std::fprintf(stderr, "[gs] %llu primitives drawn, register writes:", static_cast<unsigned long long>(g_primitives));
for (int r = 0; r < 128; ++r)
if (g_writes[r]) std::fprintf(stderr, " %02X:%llu", r, static_cast<unsigned long long>(g_writes[r]));
std::fprintf(stderr, "\n");
- return g_primitives;
}
}
diff --git a/runtime/src/gs/gs.hpp b/runtime/src/gs/gs.hpp
index 493e349..2bae157 100644
--- a/runtime/src/gs/gs.hpp
+++ b/runtime/src/gs/gs.hpp
@@ -57,5 +57,6 @@ void write_pixel(std::uint32_t psm, std::uint32_t bp, std::uint32_t bw, std::uin
bool display(std::vector<std::uint32_t> &rgba, std::uint32_t &width, std::uint32_t &height);
std::uint64_t primitives_drawn();
+void stats();
}
diff --git a/runtime/src/system.cpp b/runtime/src/system.cpp
index 7c874c8..7699460 100644
--- a/runtime/src/system.cpp
+++ b/runtime/src/system.cpp
@@ -86,6 +86,7 @@ void dump_frame() {
void vblank() {
++g_frames;
+ if (g_options.trace_iop_modules && g_frames % 25 == 0) std::fprintf(stderr, "[frame] %llu prims %llu\n", static_cast<unsigned long long>(g_frames), static_cast<unsigned long long>(gs::primitives_drawn()));
if (!g_options.frames_dir.empty() && g_options.frame_every && g_frames % g_options.frame_every == 0) dump_frame();
hw::vblank_start();
iop::vblank(true);
@@ -239,7 +240,14 @@ int run() {
std::fprintf(stderr, "\n");
}
}
- std::fprintf(stderr, "[gs] %llu primitives drawn\n", static_cast<unsigned long long>(gs::primitives_drawn()));
+ if (const char *path = std::getenv("PS2_DUMP")) {
+ if (std::FILE *f = std::fopen(path, "wb")) {
+ std::fwrite(g_ram, 1, kRamSize, f);
+ std::fwrite(iop::ram_ptr(0), 1, iop::kRamSize, f);
+ std::fclose(f);
+ }
+ }
+ gs::stats();
dmac::stats();
gif::stats();
vif::stats();