PS2 games in the browser: static recompilation to WebAssembly with a native runtime
C++
ipu: decode MPEG-2 macroblocks for the game's movie player
Samir Nuri4ac801e38af8
diff --git a/CMakeLists.txt b/CMakeLists.txtindex 2098bd3..1a34b3f 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -27,6 +27,7 @@ add_library(ps2runtime STATICruntime/src/dmac.cppruntime/src/vif.cppruntime/src/vu_micro.cpp+ runtime/src/ipu/ipu.cppruntime/src/gs/gs.cppruntime/src/gs/gif.cppruntime/src/gs/raster.cppdiff --git a/runtime/src/dmac.cpp b/runtime/src/dmac.cppindex d24a624..8b7f927 100644--- a/runtime/src/dmac.cpp+++ b/runtime/src/dmac.cpp@@ -3,6 +3,7 @@#include "kernel.hpp"#include "gs/gif.hpp"+#include "ipu/ipu.hpp"#include "hw.hpp"#include "ps2/memory.hpp"#include "ps2/system.hpp"@@ -19,7 +20,7 @@ namespace {struct Channel {std::uint32_t chcr = 0, madr = 0, qwc = 0, tadr = 0, asr[2] = {}, sadr = 0;- bool paused = false, pause_end = false;+ bool paused = false, pause_end = false, tag_end = false;};Channel g_ch[10];@@ -69,7 +70,7 @@ void pause(int ch, bool end) {void finish(int ch) {Channel &c = g_ch[ch];static const bool trace = std::getenv("PS2_TRACE_DMA") != nullptr;- if (trace && ch <= 2) std::fprintf(stderr, "[dma] ch%d done chcr 0x%08X tadr 0x%08X madr 0x%08X\n", ch, c.chcr, c.tadr, c.madr);+ if (trace && ch <= 4) std::fprintf(stderr, "[dma] ch%d done chcr 0x%08X tadr 0x%08X madr 0x%08X\n", ch, c.chcr, c.tadr, c.madr);c.chcr &= ~0x100u;hw::dmac_raise(static_cast<unsigned>(ch));}@@ -165,6 +166,7 @@ void run_chain(int ch) {void start(int ch) {Channel &c = g_ch[ch];c.paused = false;+ c.tag_end = false;++g_starts[ch];static const bool trace = std::getenv("PS2_TRACE_DMA") != nullptr;if (trace) std::fprintf(stderr, "[dma] ch%d chcr 0x%08X madr 0x%08X qwc %u tadr 0x%08X thread %d%s\n", ch, c.chcr, c.madr, c.qwc, c.tadr,@@ -173,6 +175,11 @@ void start(int ch) {finish(ch);return;}+ if (ch == 3 || ch == 4) {+ if (ch == 3 && !c.qwc) finish(ch);+ else ipu::dma_started(ch);+ return;+ }const std::uint32_t mode = (c.chcr >> 2) & 3u;if (mode == 1 && ch != 8) run_chain(ch);else run_normal(ch);@@ -192,6 +199,55 @@ void resume(int ch) {run_chain(ch);}+bool ipu_pull(std::uint8_t *qword) {+ Channel &c = g_ch[4];+ if (!(c.chcr & 0x100u)) return false;+ const bool chain = ((c.chcr >> 2) & 3u) == 1;+ while (!c.qwc) {+ if (!chain || c.tag_end) {+ finish(4);+ return false;+ }+ std::uint64_t tag;+ std::memcpy(&tag, source(c.tadr), 8);+ c.chcr = (c.chcr & 0xFFFFu) | (static_cast<std::uint32_t>(tag >> 16) << 16);+ const std::uint32_t qwc = static_cast<std::uint32_t>(tag) & 0xFFFFu;+ const std::uint32_t id = static_cast<std::uint32_t>(tag >> 28) & 7u;+ const std::uint32_t addr = (static_cast<std::uint32_t>(tag >> 32) & 0x7FFFFFF0u) | (static_cast<std::uint32_t>(tag >> 63) << 31);+ const bool irq = (tag >> 31) & 1u, tie = c.chcr & 0x80u;+ std::uint32_t data = c.tadr + 16;+ switch (id) {+ case 0: data = addr; c.tadr += 16; c.tag_end = true; break;+ case 1: c.tadr = data + qwc * 16; break;+ case 2: c.tadr = addr; break;+ case 3: case 4: data = addr; c.tadr += 16; break;+ default: c.tag_end = true; break;+ }+ if (tie && irq) c.tag_end = true;+ static const bool trace = std::getenv("PS2_TRACE_DMA") != nullptr;+ if (trace) std::fprintf(stderr, "[dma] ch4 tag id %u qwc %u addr 0x%08X data 0x%08X next 0x%08X end %d\n", id, qwc, addr, data, c.tadr, c.tag_end);+ c.madr = data;+ c.qwc = qwc;+ if (!qwc && c.tag_end) {+ finish(4);+ return false;+ }+ }+ std::memcpy(qword, source(c.madr), 16);+ c.madr += 16;+ if (!--c.qwc && (!chain || c.tag_end)) finish(4);+ return true;+}++bool ipu_push(const std::uint8_t *qword) {+ Channel &c = g_ch[3];+ if (!(c.chcr & 0x100u) || !c.qwc) return false;+ std::memcpy(target(c.madr), qword, 16);+ c.madr += 16;+ if (!--c.qwc) finish(3);+ return true;+}+void reset() {for (Channel &c : g_ch) c = Channel{};g_ctrl = g_pcr = g_sqwc = g_rbsr = g_rbor = g_stadr = g_enable = 0;diff --git a/runtime/src/dmac.hpp b/runtime/src/dmac.hppindex 5955403..14c955a 100644--- a/runtime/src/dmac.hpp+++ b/runtime/src/dmac.hpp@@ -10,5 +10,7 @@ bool owns(std::uint32_t a);std::uint32_t read(std::uint32_t a);void write(std::uint32_t a, std::uint32_t v);void resume(int ch);+bool ipu_pull(std::uint8_t *qword);+bool ipu_push(const std::uint8_t *qword);}diff --git a/runtime/src/hw.cpp b/runtime/src/hw.cppindex b92591a..df8cca6 100644--- a/runtime/src/hw.cpp+++ b/runtime/src/hw.cpp@@ -4,6 +4,7 @@#include "gs/gif.hpp"#include "gs/gs.hpp"#include "iop/sif.hpp"+#include "ipu/ipu.hpp"#include "vif.hpp"#include "vu_micro.hpp"#include "kernel.hpp"@@ -111,6 +112,7 @@ void reset() {vif::reset();vu::reset();dmac::reset();+ ipu::reset();g_regs.clear();for (Timer &t : g_timers) t = Timer{};g_timers[3].mode = 0x183u;@@ -178,6 +180,7 @@ std::uint32_t read32(std::uint32_t a) {if (a >= 0x10003000u && a < 0x10003800u) return gif::read(a);if (a >= 0x10003800u && a < 0x10003C00u) return vif::read(0, a & 0x3FFu);if (a >= 0x10003C00u && a < 0x10004000u) return vif::read(1, a & 0x3FFu);+ if (a >= 0x10002000u && a < 0x10002040u) return ipu::read32(a);if (is_timer(a)) {const int n = timer_index(a);switch (a & 0x30u) {@@ -212,6 +215,7 @@ void write32(std::uint32_t a, std::uint32_t v) {if (a >= 0x10003000u && a < 0x10003800u) { gif::write(a, v); return; }if (a >= 0x10003800u && a < 0x10003C00u) { vif::write(0, a & 0x3FFu, v); return; }if (a >= 0x10003C00u && a < 0x10004000u) { vif::write(1, a & 0x3FFu, v); return; }+ if ((a >= 0x10002000u && a < 0x10002040u) || (a >= 0x10007010u && a < 0x10007020u)) { ipu::write32(a, v); return; }if (is_timer(a)) {const int n = timer_index(a);Timer &t = g_timers[n];@@ -254,6 +258,10 @@ void read128(std::uint32_t a, Reg128 &out) {gs::read_image(reinterpret_cast<std::uint8_t *>(&out), 1);return;}+ if (a >= 0x10007000u && a < 0x10007010u) {+ ipu::read_fifo(reinterpret_cast<std::uint8_t *>(&out));+ return;+ }report("read128", a, 0);}@@ -262,6 +270,7 @@ void write128(std::uint32_t a, const Reg128 &v) {if (a >= 0x10004000u && a < 0x10005000u) { vif::transfer(0, bytes, 1); return; }if (a >= 0x10005000u && a < 0x10006000u) { vif::transfer(1, bytes, 1); return; }if (a >= 0x10006000u && a < 0x10007000u) { gif::transfer(3, bytes, 1); return; }+ if (a >= 0x10007010u && a < 0x10007020u) { ipu::write_fifo(bytes); return; }report("write128", a, v.d[0]);}diff --git a/runtime/src/ipu/NOTICE b/runtime/src/ipu/NOTICEnew file mode 100644index 0000000..4322d02--- /dev/null+++ b/runtime/src/ipu/NOTICE@@ -0,0 +1,26 @@+The VLC tables in tables.hpp were adapted from Play!, a PlayStation 2+emulator, which carries the following notice.++Copyright (c) 2006-2026, Jean-Philip Desjardins+All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright notice,+ this list of conditions and the following disclaimer.+ * Redistributions in binary form must reproduce the above copyright+ notice, this list of conditions and the following disclaimer in the+ documentation and/or other materials provided with the distribution.++THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE+DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH+DAMAGE.\ No newline at end of filediff --git a/runtime/src/ipu/ipu.cpp b/runtime/src/ipu/ipu.cppnew file mode 100644index 0000000..020c3c9--- /dev/null+++ b/runtime/src/ipu/ipu.cpp@@ -0,0 +1,680 @@+#include "ipu.hpp"++#include "tables.hpp"+#include "../dmac.hpp"+#include "../hw.hpp"++#include <algorithm>+#include <cmath>+#include <cstdio>+#include <cstdlib>+#include <cstring>+#include <vector>++namespace ps2::ipu {++namespace {++using namespace tables;++struct NeedData {};+struct BadCode {};++constexpr std::uint32_t kEcd = 0x4000u, kScd = 0x8000u, kReset = 0x40000000u;+constexpr std::size_t kFifoBytes = 128;++constexpr std::uint8_t kDefaultIntra[64] = {+ 8, 16, 19, 22, 26, 27, 29, 34, 16, 16, 22, 24, 27, 29, 34, 37, 19, 22, 26, 27, 29, 34, 34, 38, 22, 22, 26, 27, 29, 34, 37, 40,+ 22, 26, 27, 29, 32, 35, 40, 48, 26, 27, 29, 32, 35, 40, 48, 58, 26, 27, 29, 34, 38, 46, 56, 69, 27, 29, 35, 38, 46, 56, 69, 83,+};++constexpr std::uint8_t kNonLinearScale[32] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 18, 20, 22,+ 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72, 80, 88, 96, 104, 112};++struct Entry {+ std::uint8_t length = 0;+ std::uint32_t value = 0;+};++struct Lookup {+ unsigned bits = 0;+ std::vector<Entry> entries;+};++struct DctEntry {+ std::uint8_t length = 0;+ std::uint8_t run = 0;+ std::int16_t level = 0;+};++struct DctLookup {+ unsigned bits = 0;+ std::vector<DctEntry> entries;+};++template <std::size_t N>+Lookup build(const Vlc (&table)[N]) {+ Lookup l;+ for (const Vlc &e : table) l.bits = std::max<unsigned>(l.bits, e.length);+ l.entries.resize(std::size_t{1} << l.bits);+ for (const Vlc &e : table) {+ const unsigned shift = l.bits - e.length;+ for (std::uint32_t i = 0; i < (1u << shift); ++i) l.entries[(static_cast<std::uint32_t>(e.code) << shift) | i] = {e.length, e.value};+ }+ return l;+}++template <std::size_t N>+DctLookup build(const Dct (&table)[N]) {+ DctLookup l;+ for (const Dct &e : table) l.bits = std::max<unsigned>(l.bits, e.length);+ l.entries.resize(std::size_t{1} << l.bits);+ for (const Dct &e : table) {+ const unsigned shift = l.bits - e.length;+ for (std::uint32_t i = 0; i < (1u << shift); ++i) l.entries[(static_cast<std::uint32_t>(e.code) << shift) | i] = {e.length, e.run, e.level};+ }+ return l;+}++struct Tables {+ Lookup mbai = build(kMbAddressIncrement), mb_i = build(kMbTypeI), mb_p = build(kMbTypeP), mb_b = build(kMbTypeB);+ Lookup motion = build(kMotionCode), dmv = build(kDmVector), dc_luma = build(kDcSizeLuma), dc_chroma = build(kDcSizeChroma);+ Lookup cbp = build(kCodedBlockPattern);+ DctLookup dct0 = build(kDctTable0), dct1 = build(kDctTable1);+ double cosines[8][8];++ Tables() {+ for (int x = 0; x < 8; ++x)+ for (int u = 0; u < 8; ++u)+ cosines[x][u] = (u ? 0.5 : 0.5 / std::sqrt(2.0)) * std::cos((2 * x + 1) * u * 3.14159265358979323846 / 16.0);+ }+};++const Tables &tables() {+ static const Tables t;+ return t;+}++enum Command { kBclr, kIdec, kBdec, kVdec, kFdec, kSetiq, kSetvq, kCsc, kPack, kSetth };++std::vector<std::uint8_t> g_in;+std::size_t g_bit = 0;+std::vector<std::uint8_t> g_out;+std::size_t g_out_pos = 0;++std::uint32_t g_ctrl = 0, g_result = 0, g_command = 0;+int g_last = -1;+bool g_busy = false, g_decoding = false, g_interrupt = true;+int g_phase = 0;+std::uint32_t g_qsc = 0, g_count = 0;+bool g_first = true;+std::uint8_t g_intra_iq[64], g_nonintra_iq[64];+std::uint16_t g_vqclut[16];+std::uint16_t g_th0 = 0, g_th1 = 0;+std::int16_t g_dc[3];+std::uint64_t g_commands[16], g_stalls = 0, g_out_qwords = 0;++bool pull() {+ std::uint8_t q[16];+ if (!dmac::ipu_pull(q)) return false;+ g_in.insert(g_in.end(), q, q + 16);+ return true;+}++bool have(std::size_t bits) {+ while (g_in.size() * 8 < g_bit + bits)+ if (!pull()) return false;+ return true;+}++std::size_t available() { return g_in.size() * 8 > g_bit ? g_in.size() * 8 - g_bit : 0; }++std::uint32_t bits_at(std::size_t bit, unsigned n) {+ if (!n) return 0;+ std::uint64_t v = 0;+ const std::size_t byte = bit >> 3;+ for (std::size_t i = 0; i < 8; ++i) v = (v << 8) | (byte + i < g_in.size() ? g_in[byte + i] : 0u);+ return static_cast<std::uint32_t>((v << (bit & 7u)) >> (64 - n));+}++std::uint32_t peek(unsigned n) {+ if (!have(n)) throw NeedData{};+ return bits_at(g_bit, n);+}++std::uint32_t get(unsigned n) {+ const std::uint32_t v = peek(n);+ g_bit += n;+ return v;+}++void skip(unsigned n) {+ if (!have(n)) throw NeedData{};+ g_bit += n;+}++std::uint32_t peek_soft(unsigned n, std::size_t &avail) {+ have(n);+ avail = available();+ return bits_at(g_bit, n);+}++std::uint32_t decode(const Lookup &l) {+ std::size_t avail;+ const Entry &e = l.entries[peek_soft(l.bits, avail)];+ if (!e.length) {+ if (avail < l.bits) throw NeedData{};+ throw BadCode{};+ }+ if (e.length > avail) throw NeedData{};+ g_bit += e.length;+ return e.value;+}++std::uint32_t peek_now() {+ const unsigned n = static_cast<unsigned>(std::min<std::size_t>(32, available()));+ return n ? bits_at(g_bit, n) << (32 - n) : 0u;+}++void commit() {+ const std::size_t drop = std::min(g_bit / 128 * 16, g_in.size() / 16 * 16);+ g_in.erase(g_in.begin(), g_in.begin() + static_cast<std::ptrdiff_t>(drop));+ g_bit -= drop * 8;+}++void top_up() {+ while (g_in.size() < kFifoBytes && pull()) {}+}++unsigned dc_precision() { return (g_ctrl >> 16) & 3u; }+bool alternate_scan() { return g_ctrl & 0x100000u; }+bool intra_vlc_one() { return g_ctrl & 0x200000u; }+bool nonlinear_q() { return g_ctrl & 0x400000u; }+bool mpeg1() { return g_ctrl & 0x800000u; }+unsigned picture_type() { return (g_ctrl >> 24) & 7u; }++int read_dc_diff(int channel) {+ const unsigned size = decode(channel ? tables().dc_chroma : tables().dc_luma) & 0xFFFFu;+ if (!size) return 0;+ const int v = static_cast<int>(get(size));+ return v < (1 << (size - 1)) ? v + 1 - (1 << size) : v;+}++bool read_coefficient(const DctLookup &l, int &run, int &level) {+ std::size_t avail;+ const DctEntry &e = l.entries[peek_soft(l.bits, avail)];+ if (!e.length) {+ if (avail < l.bits) throw NeedData{};+ throw BadCode{};+ }+ if (e.length > avail) throw NeedData{};+ g_bit += e.length;+ if (e.run == kEscape) {+ run = static_cast<int>(get(6));+ if (mpeg1()) {+ int v = static_cast<int>(get(8));+ if (v == 0) v = static_cast<int>(get(8));+ else if (v == 128) v = static_cast<int>(get(8)) - 256;+ else if (v > 128) v -= 256;+ level = v;+ } else {+ const int v = static_cast<int>(get(12));+ level = v & 0x800 ? v - 0x1000 : v;+ }+ return true;+ }+ run = e.run;+ level = get(1) ? -e.level : e.level;+ return true;+}++void idct(const int *in, std::int16_t *out) {+ const auto &c = tables().cosines;+ double rows[8][8];+ for (int v = 0; v < 8; ++v)+ for (int x = 0; x < 8; ++x) {+ double s = 0;+ for (int u = 0; u < 8; ++u) s += c[x][u] * in[v * 8 + u];+ rows[v][x] = s;+ }+ for (int y = 0; y < 8; ++y)+ for (int x = 0; x < 8; ++x) {+ double s = 0;+ for (int v = 0; v < 8; ++v) s += c[y][v] * rows[v][x];+ out[y * 8 + x] = static_cast<std::int16_t>(std::clamp(static_cast<int>(std::floor(s + 0.5)), -256, 255));+ }+}++void decode_block(std::int16_t *out, int channel, bool intra, std::uint32_t qsc) {+ int coef[64] = {};+ int index = 0;+ const DctLookup &table = intra && intra_vlc_one() ? tables().dct1 : tables().dct0;+ const bool table_one = &table == &tables().dct1;+ if (intra) {+ g_dc[channel] = static_cast<std::int16_t>(g_dc[channel] + read_dc_diff(channel));+ coef[0] = g_dc[channel];+ index = 1;+ } else if (peek(1)) {+ coef[0] = get(2) & 1u ? -1 : 1;+ index = 1;+ }+ for (;;) {+ if (index) {+ if (table_one ? peek(4) == 6u : peek(2) == 2u) {+ skip(table_one ? 4 : 2);+ break;+ }+ }+ int run = 0, level = 0;+ read_coefficient(table, run, level);+ index += run;+ if (index >= 64) throw BadCode{};+ coef[index++] = level;+ }+ const std::uint8_t *scan = alternate_scan() ? kAlternate : kZigzag;+ const std::uint8_t *weights = intra ? g_intra_iq : g_nonintra_iq;+ const int scale = mpeg1() || !nonlinear_q() ? static_cast<int>(qsc) * 2 : kNonLinearScale[qsc & 31u];+ int f[64] = {};+ int sum = 0;+ for (int k = 0; k < 64; ++k) {+ const int v = coef[k];+ const int r = scan[k];+ int x;+ if (intra && k == 0) {+ x = v * (8 >> dc_precision());+ } else {+ if (!v) continue;+ const int sign = v > 0 ? 1 : -1;+ x = intra ? (v * weights[r] * scale * 2) / 32 : ((2 * v + sign) * weights[r] * scale) / 32;+ if (mpeg1() && x && !(x & 1)) x -= sign;+ }+ x = std::clamp(x, -2048, 2047);+ f[r] = x;+ sum += x;+ }+ if (!mpeg1() && !(sum & 1)) f[63] += f[63] & 1 ? -1 : 1;+ idct(f, out);+}++void decode_macroblock(std::int16_t *raw, bool intra, bool field, bool dc_reset, std::uint32_t qsc) {+ const unsigned cbp = intra ? 0x3Fu : decode(tables().cbp) & 0xFFu;+ if (dc_reset) g_dc[0] = g_dc[1] = g_dc[2] = static_cast<std::int16_t>(128 << dc_precision());+ std::int16_t blocks[6][64] = {};+ for (int b = 0; b < 6; ++b)+ if (cbp & (32u >> b)) decode_block(blocks[b], b < 4 ? 0 : b - 3, intra, qsc);+ for (int i = 0; i < 8; ++i) {+ const int top = field ? 2 * i : i, bottom = field ? 2 * i + 1 : i + 8;+ std::memcpy(raw + top * 16, blocks[0] + i * 8, 16);+ std::memcpy(raw + top * 16 + 8, blocks[1] + i * 8, 16);+ std::memcpy(raw + bottom * 16, blocks[2] + i * 8, 16);+ std::memcpy(raw + bottom * 16 + 8, blocks[3] + i * 8, 16);+ }+ std::memcpy(raw + 256, blocks[4], 128);+ std::memcpy(raw + 320, blocks[5], 128);+}++void convert(const std::uint8_t *raw, bool rgb16) {+ std::uint8_t pixels[1024];+ std::size_t size = 0;+ for (int y = 0; y < 16; ++y)+ for (int x = 0; x < 16; ++x) {+ const int lum = 149 * std::max(0, raw[y * 16 + x] - 16);+ const int cb = raw[256 + (y / 2) * 8 + x / 2] - 128, cr = raw[320 + (y / 2) * 8 + x / 2] - 128;+ const int r = std::clamp((lum + 204 * cr + 64) >> 7, 0, 255);+ const int g = std::clamp((lum - 50 * cb - 104 * cr + 64) >> 7, 0, 255);+ const int b = std::clamp((lum + 258 * cb + 64) >> 7, 0, 255);+ int a = 0x80;+ if (r < g_th0 && g < g_th0 && b < g_th0) a = 0;+ else if (r < g_th1 && g < g_th1 && b < g_th1) a = 0x40;+ if (rgb16) {+ const std::uint16_t p = static_cast<std::uint16_t>((r >> 3) | ((g >> 3) << 5) | ((b >> 3) << 10) | (a & 0x80 ? 0x8000 : 0));+ std::memcpy(pixels + size, &p, 2);+ size += 2;+ } else {+ const std::uint8_t p[4] = {static_cast<std::uint8_t>(r), static_cast<std::uint8_t>(g), static_cast<std::uint8_t>(b), static_cast<std::uint8_t>(a)};+ std::memcpy(pixels + size, p, 4);+ size += 4;+ }+ }+ g_out.insert(g_out.end(), pixels, pixels + size);+}++void emit_raw16(const std::int16_t *raw) {+ const auto *bytes = reinterpret_cast<const std::uint8_t *>(raw);+ g_out.insert(g_out.end(), bytes, bytes + 768);+}++bool start_code_follows() {+ std::size_t avail;+ const std::uint32_t next = peek_soft(8, avail);+ return avail >= 8 && next == 0;+}++template <typename F>+bool atomic(F &&body) {+ const std::size_t bit = g_bit;+ std::int16_t dc[3];+ std::memcpy(dc, g_dc, sizeof dc);+ const std::size_t out = g_out.size();+ try {+ body();+ return true;+ } catch (const NeedData &) {+ ++g_stalls;+ g_bit = bit;+ std::memcpy(g_dc, dc, sizeof dc);+ g_out.resize(out);+ return false;+ }+}++bool run_idec() {+ const bool dtd = (g_command >> 24) & 1u, rgb16 = (g_command >> 27) & 1u;+ for (;;) {+ bool ok = true;+ switch (g_phase) {+ case 0:+ ok = atomic([&] { skip(g_command & 0x3Fu); });+ if (ok) g_phase = 1;+ break;+ case 1:+ ok = atomic([&] {+ const std::uint32_t type = decode(tables().mb_i) & 0xFFFFu;+ const bool field = dtd && get(1);+ std::uint32_t qsc = g_qsc;+ if (type & 0x10u) qsc = get(5);+ std::int16_t raw[384];+ decode_macroblock(raw, true, field, g_first, qsc);+ std::uint8_t raw8[384];+ for (int i = 0; i < 384; ++i) raw8[i] = static_cast<std::uint8_t>(std::clamp<int>(raw[i], 0, 255));+ convert(raw8, rgb16);+ g_qsc = qsc;+ });+ if (ok) {+ g_first = false;+ g_phase = 2;+ }+ break;+ case 2:+ ok = atomic([&] {+ std::size_t avail;+ const std::uint32_t next = peek_soft(8, avail);+ if (avail < 8) throw NeedData{};+ if (next == 0) {+ g_bit = (g_bit + 7) & ~std::size_t{7};+ g_phase = 3;+ return;+ }+ std::uint32_t v;+ do v = decode(tables().mbai) & 0xFFFFu;+ while (v == 0x22u || v == 0x23u);+ g_phase = 1;+ });+ break;+ case 3:+ ok = atomic([&] {+ for (;;) {+ std::size_t avail;+ const std::uint32_t code = peek_soft(24, avail);+ if (avail < 24 || code == 1) {+ g_phase = 4;+ return;+ }+ if (code != 0) throw BadCode{};+ g_bit += 8;+ }+ });+ break;+ default:+ g_ctrl |= kScd;+ return true;+ }+ if (!ok) return false;+ commit();+ }+}++bool run_command() {+ const std::uint32_t cmd = g_command;+ switch (cmd >> 28) {+ case kBclr:+ g_in.clear();+ g_bit = cmd & 0x7Fu;+ return true;+ case kIdec:+ return run_idec();+ case kBdec: {+ const bool ok = atomic([&] {+ skip(cmd & 0x3Fu);+ std::int16_t raw[384];+ decode_macroblock(raw, (cmd >> 27) & 1u, (cmd >> 25) & 1u, (cmd >> 26) & 1u, (cmd >> 16) & 0x1Fu);+ emit_raw16(raw);+ });+ if (!ok) return false;+ commit();+ if (start_code_follows()) {+ g_ctrl |= kScd;+ g_interrupt = false;+ }+ return true;+ }+ case kVdec: {+ const bool ok = atomic([&] {+ skip(cmd & 0x3Fu);+ const Lookup *table = nullptr;+ switch ((cmd >> 26) & 3u) {+ case 0: table = &tables().mbai; break;+ case 1:+ switch (picture_type()) {+ case 1: table = &tables().mb_i; break;+ case 2: table = &tables().mb_p; break;+ case 3: table = &tables().mb_b; break;+ default: throw BadCode{};+ }+ break;+ case 2: table = &tables().motion; break;+ default: table = &tables().dmv; break;+ }+ g_result = decode(*table);+ });+ if (ok) commit();+ return ok;+ }+ case kFdec: {+ const bool ok = atomic([&] {+ skip(cmd & 0x3Fu);+ g_result = peek(32);+ });+ if (ok) commit();+ return ok;+ }+ case kSetiq: {+ std::uint8_t *matrix = (cmd >> 27) & 1u ? g_nonintra_iq : g_intra_iq;+ std::uint8_t values[64];+ const bool ok = atomic([&] {+ skip(cmd & 0x3Fu);+ for (std::uint8_t &v : values) v = static_cast<std::uint8_t>(get(8));+ });+ if (!ok) return false;+ for (int k = 0; k < 64; ++k) matrix[kZigzag[k]] = values[k];+ commit();+ return true;+ }+ case kSetvq: {+ std::uint16_t values[16];+ const bool ok = atomic([&] {+ skip(cmd & 0x3Fu);+ for (std::uint16_t &v : values) v = static_cast<std::uint16_t>(get(16));+ });+ if (!ok) return false;+ std::memcpy(g_vqclut, values, sizeof values);+ commit();+ return true;+ }+ case kCsc:+ while (g_count) {+ std::uint8_t raw[384];+ const bool ok = atomic([&] {+ for (std::uint8_t &v : raw) v = static_cast<std::uint8_t>(get(8));+ convert(raw, (cmd >> 27) & 1u);+ });+ if (!ok) return false;+ commit();+ --g_count;+ }+ return true;+ case kSetth:+ g_th0 = static_cast<std::uint16_t>(cmd & 0x1FFu);+ g_th1 = static_cast<std::uint16_t>((cmd >> 16) & 0x1FFu);+ return true;+ default:+ std::fprintf(stderr, "[ipu] command 0x%08X is not implemented\n", cmd);+ return true;+ }+}++void drain() {+ while (g_out_pos + 16 <= g_out.size() && dmac::ipu_push(g_out.data() + g_out_pos)) {+ g_out_pos += 16;+ ++g_out_qwords;+ }+ if (g_out_pos == g_out.size()) {+ g_out.clear();+ g_out_pos = 0;+ }+}++void step() {+ if (g_busy && g_decoding) {+ try {+ if (run_command()) g_decoding = false;+ } catch (const BadCode &) {+ std::fprintf(stderr, "[ipu] bad code in command 0x%08X\n", g_command);+ g_ctrl |= kEcd;+ g_decoding = false;+ g_busy = false;+ }+ }+ drain();+ if (g_busy && !g_decoding && g_out.empty()) {+ g_busy = false;+ if (g_interrupt) hw::intc_raise(8);+ }+ top_up();+}++}++void reset() {+ g_in.clear();+ g_out.clear();+ g_bit = g_out_pos = 0;+ g_ctrl = g_result = g_command = 0;+ g_last = -1;+ g_busy = g_decoding = false;+ std::memcpy(g_intra_iq, kDefaultIntra, 64);+ std::fill(std::begin(g_nonintra_iq), std::end(g_nonintra_iq), 16);+ std::memset(g_vqclut, 0, sizeof g_vqclut);+ g_th0 = g_th1 = 0;+ std::memset(g_dc, 0, sizeof g_dc);+}++std::uint32_t read32(std::uint32_t a) {+ switch (a & 0xFFFFFFFCu) {+ case 0x10002000u:+ return g_last == kVdec || g_last == kFdec ? g_result : peek_now();+ case 0x10002004u:+ return g_busy ? 0x80000000u : 0u;+ case 0x10002010u: {+ const std::uint32_t ifc = static_cast<std::uint32_t>(std::min<std::size_t>(8, g_in.size() / 16));+ const std::uint32_t ofc = static_cast<std::uint32_t>(std::min<std::size_t>(8, (g_out.size() - g_out_pos) / 16));+ return (g_ctrl & ~0x800000FFu) | (g_busy ? 0x80000000u : 0u) | ifc | (ofc << 4);+ }+ case 0x10002020u: {+ const std::uint32_t bp = static_cast<std::uint32_t>(g_bit & 127u);+ std::uint32_t ifc = static_cast<std::uint32_t>(g_in.size() / 16 - std::min(g_in.size() / 16, g_bit / 128));+ std::uint32_t fp = 0;+ if (bp && ifc) {+ fp = 1;+ --ifc;+ }+ return (fp << 16) | (std::min<std::uint32_t>(ifc, 8) << 8) | bp;+ }+ case 0x10002030u:+ return g_busy ? 0u : peek_now();+ case 0x10002034u:+ return g_busy || available() < 32 ? 0x80000000u : 0u;+ default:+ return 0;+ }+}++void write32(std::uint32_t a, std::uint32_t v) {+ switch (a) {+ case 0x10002000u: {+ }+ g_ctrl &= ~(kEcd | kScd);+ ++g_commands[v >> 28];+ g_command = v;+ g_last = static_cast<int>(v >> 28);+ g_busy = g_decoding = g_interrupt = true;+ g_phase = 0;+ g_qsc = (v >> 16) & 0x1Fu;+ g_first = true;+ g_count = v & 0x7FFu;+ step();+ {+ static const char *trace = std::getenv("PS2_TRACE_IPU");+ static std::uint64_t traced = 0, from = trace ? std::strtoull(trace, nullptr, 10) : 0;+ if (trace && ++traced >= from && traced < from + 200000)+ std::fprintf(stderr, "[ipu] cmd 0x%08X -> result %08X ctrl %08X bit %zu next %08X%08X\n", v, g_result, g_ctrl, g_bit,+ bits_at(g_bit, 32), bits_at(g_bit + 32, 32));+ }+ break;+ case 0x10002010u:+ if (v & kReset) {+ g_in.clear();+ g_out.clear();+ g_bit = g_out_pos = 0;+ g_busy = g_decoding = false;+ g_th0 = g_th1 = 0;+ }+ g_ctrl = (g_ctrl & ~0x3FFF0000u) | (v & 0x3FFF0000u);+ break;+ default:+ if (a >= 0x10007010u && a < 0x10007020u) {+ const std::uint8_t *bytes = reinterpret_cast<const std::uint8_t *>(&v);+ g_in.insert(g_in.end(), bytes, bytes + 4);+ step();+ }+ break;+ }+}++void write_fifo(const std::uint8_t *qword) {+ g_in.insert(g_in.end(), qword, qword + 16);+ step();+}++void read_fifo(std::uint8_t *qword) {+ if (g_out_pos + 16 <= g_out.size()) {+ std::memcpy(qword, g_out.data() + g_out_pos, 16);+ g_out_pos += 16;+ } else {+ std::memset(qword, 0, 16);+ }+ step();+}++void dma_started(int) { step(); }++void stats() {+ std::fprintf(stderr, "[ipu] commands:");+ for (int i = 0; i < 16; ++i)+ if (g_commands[i]) std::fprintf(stderr, " %X:%llu", i, static_cast<unsigned long long>(g_commands[i]));+ std::fprintf(stderr, " stalls %llu out %llu qwords, busy %d decoding %d cmd 0x%08X in %zu bytes bit %zu out %zu\n",+ static_cast<unsigned long long>(g_stalls), static_cast<unsigned long long>(g_out_qwords), g_busy, g_decoding, g_command,+ g_in.size(), g_bit, g_out.size() - g_out_pos);+}++}diff --git a/runtime/src/ipu/ipu.hpp b/runtime/src/ipu/ipu.hppnew file mode 100644index 0000000..6a4fbd1--- /dev/null+++ b/runtime/src/ipu/ipu.hpp@@ -0,0 +1,15 @@+#pragma once++#include <cstdint>++namespace ps2::ipu {++void reset();+std::uint32_t read32(std::uint32_t a);+void write32(std::uint32_t a, std::uint32_t v);+void write_fifo(const std::uint8_t *qword);+void read_fifo(std::uint8_t *qword);+void dma_started(int channel);+void stats();++}diff --git a/runtime/src/ipu/tables.hpp b/runtime/src/ipu/tables.hppnew file mode 100644index 0000000..1b4dfe5--- /dev/null+++ b/runtime/src/ipu/tables.hpp@@ -0,0 +1,145 @@+#pragma once++#include <cstdint>++// Adapted from Play! (BSD-2-Clause), see NOTICE in this directory.+namespace ps2::ipu::tables {++struct Vlc {+ std::uint16_t code;+ std::uint8_t length;+ std::uint32_t value;+};++struct Dct {+ std::uint16_t code;+ std::uint8_t length;+ std::uint8_t run;+ std::int16_t level;+};++inline constexpr std::uint8_t kEscape = 255;++constexpr Vlc kMbAddressIncrement[] = {+ {0x1, 1, 0x10001}, {0x3, 3, 0x30002}, {0x2, 3, 0x30003}, {0x3, 4, 0x40004}, {0x2, 4, 0x40005},+ {0x3, 5, 0x50006}, {0x2, 5, 0x50007}, {0x7, 7, 0x70008}, {0x6, 7, 0x70009}, {0xB, 8, 0x8000A},+ {0xA, 8, 0x8000B}, {0x9, 8, 0x8000C}, {0x8, 8, 0x8000D}, {0x7, 8, 0x8000E}, {0x6, 8, 0x8000F},+ {0x17, 10, 0xA0010}, {0x16, 10, 0xA0011}, {0x15, 10, 0xA0012}, {0x14, 10, 0xA0013}, {0x13, 10, 0xA0014},+ {0x12, 10, 0xA0015}, {0x23, 11, 0xB0016}, {0x22, 11, 0xB0017}, {0x21, 11, 0xB0018}, {0x20, 11, 0xB0019},+ {0x1F, 11, 0xB001A}, {0x1E, 11, 0xB001B}, {0x1D, 11, 0xB001C}, {0x1C, 11, 0xB001D}, {0x1B, 11, 0xB001E},+ {0x1A, 11, 0xB001F}, {0x19, 11, 0xB0020}, {0x18, 11, 0xB0021}, {0xF, 11, 0xB0022}, {0x8, 11, 0xB0023},+};++constexpr Vlc kMbTypeI[] = {+ {0x1, 1, 0x10001}, {0x1, 2, 0x20011},+};++constexpr Vlc kMbTypeP[] = {+ {0x1, 1, 0x1000A}, {0x1, 2, 0x20002}, {0x1, 3, 0x30008}, {0x3, 5, 0x50001}, {0x2, 5, 0x5001A},+ {0x1, 5, 0x50012}, {0x1, 6, 0x60011},+};++constexpr Vlc kMbTypeB[] = {+ {0x2, 2, 0x2000C}, {0x3, 2, 0x2000E}, {0x2, 3, 0x30004}, {0x3, 3, 0x30006}, {0x2, 4, 0x40008},+ {0x3, 4, 0x4000A}, {0x3, 5, 0x50001}, {0x2, 5, 0x5001E}, {0x3, 6, 0x6001A}, {0x2, 6, 0x60016},+ {0x1, 6, 0x60011},+};++constexpr Vlc kMotionCode[] = {+ {0x1, 1, 0x10000}, {0x2, 3, 0x30001}, {0x3, 3, 0x3FFFF}, {0x2, 4, 0x40002}, {0x3, 4, 0x4FFFE},+ {0x2, 5, 0x50003}, {0x3, 5, 0x5FFFD}, {0x6, 7, 0x70004}, {0x7, 7, 0x7FFFC}, {0xA, 8, 0x80005},+ {0xB, 8, 0x8FFFB}, {0x8, 8, 0x80006}, {0x9, 8, 0x8FFFA}, {0x6, 8, 0x80007}, {0x7, 8, 0x8FFF9},+ {0x16, 10, 0xA0008}, {0x17, 10, 0xAFFF8}, {0x14, 10, 0xA0009}, {0x15, 10, 0xAFFF7}, {0x12, 10, 0xA000A},+ {0x13, 10, 0xAFFF6}, {0x22, 11, 0xB000B}, {0x23, 11, 0xBFFF5}, {0x20, 11, 0xB000C}, {0x21, 11, 0xBFFF4},+ {0x1E, 11, 0xB000D}, {0x1F, 11, 0xBFFF3}, {0x1C, 11, 0xB000E}, {0x1D, 11, 0xBFFF2}, {0x1A, 11, 0xB000F},+ {0x1B, 11, 0xBFFF1}, {0x18, 11, 0xB0010}, {0x19, 11, 0xBFFF0},+};++constexpr Vlc kDmVector[] = {+ {0x0, 1, 0x10000}, {0x2, 2, 0x20001}, {0x3, 2, 0x2FFFF},+};++constexpr Vlc kDcSizeLuma[] = {+ {0x0, 2, 0x1}, {0x1, 2, 0x2}, {0x4, 3, 0x0}, {0x5, 3, 0x3}, {0x6, 3, 0x4}, {0xE, 4, 0x5}, {0x1E, 5, 0x6},+ {0x3E, 6, 0x7}, {0x7E, 7, 0x8}, {0xFE, 8, 0x9}, {0x1FE, 9, 0xA}, {0x1FF, 9, 0xB},+};++constexpr Vlc kDcSizeChroma[] = {+ {0x0, 2, 0x0}, {0x1, 2, 0x1}, {0x2, 2, 0x2}, {0x6, 3, 0x3}, {0xE, 4, 0x4}, {0x1E, 5, 0x5}, {0x3E, 6, 0x6},+ {0x7E, 7, 0x7}, {0xFE, 8, 0x8}, {0x1FE, 9, 0x9}, {0x3FE, 10, 0xA}, {0x3FF, 10, 0xB},+};++constexpr Vlc kCodedBlockPattern[] = {+ {0x7, 3, 0x3C}, {0xD, 4, 0x4}, {0xC, 4, 0x8}, {0xB, 4, 0x10}, {0xA, 4, 0x20}, {0x13, 5, 0xC}, {0x12, 5, 0x30},+ {0x11, 5, 0x14}, {0x10, 5, 0x28}, {0xF, 5, 0x1C}, {0xE, 5, 0x2C}, {0xD, 5, 0x34}, {0xC, 5, 0x38}, {0xB, 5, 0x1},+ {0xA, 5, 0x3D}, {0x9, 5, 0x2}, {0x8, 5, 0x3E}, {0xF, 6, 0x18}, {0xE, 6, 0x24}, {0xD, 6, 0x3}, {0xC, 6, 0x3F},+ {0x17, 7, 0x5}, {0x16, 7, 0x9}, {0x15, 7, 0x11}, {0x14, 7, 0x21}, {0x13, 7, 0x6}, {0x12, 7, 0xA},+ {0x11, 7, 0x12}, {0x10, 7, 0x22}, {0x1F, 8, 0x7}, {0x1E, 8, 0xB}, {0x1D, 8, 0x13}, {0x1C, 8, 0x23},+ {0x1B, 8, 0xD}, {0x1A, 8, 0x31}, {0x19, 8, 0x15}, {0x18, 8, 0x29}, {0x17, 8, 0xE}, {0x16, 8, 0x32},+ {0x15, 8, 0x16}, {0x14, 8, 0x2A}, {0x13, 8, 0xF}, {0x12, 8, 0x33}, {0x11, 8, 0x17}, {0x10, 8, 0x2B},+ {0xF, 8, 0x19}, {0xE, 8, 0x25}, {0xD, 8, 0x1A}, {0xC, 8, 0x26}, {0xB, 8, 0x1D}, {0xA, 8, 0x2D}, {0x9, 8, 0x35},+ {0x8, 8, 0x39}, {0x7, 8, 0x1E}, {0x6, 8, 0x2E}, {0x5, 8, 0x36}, {0x4, 8, 0x3A}, {0x7, 9, 0x1F}, {0x6, 9, 0x2F},+ {0x5, 9, 0x37}, {0x4, 9, 0x3B}, {0x3, 9, 0x1B}, {0x2, 9, 0x27}, {0x1, 9, 0x0},+};++constexpr Dct kDctTable0[] = {+ {0x3, 2, 0, 1}, {0x3, 3, 1, 1}, {0x4, 4, 0, 2}, {0x5, 4, 2, 1}, {0x5, 5, 0, 3}, {0x7, 5, 3, 1}, {0x6, 5, 4, 1},+ {0x6, 6, 1, 2}, {0x7, 6, 5, 1}, {0x5, 6, 6, 1}, {0x4, 6, 7, 1}, {0x1, 6, 255, 0}, {0x6, 7, 0, 4},+ {0x4, 7, 2, 2}, {0x7, 7, 8, 1}, {0x5, 7, 9, 1}, {0x26, 8, 0, 5}, {0x21, 8, 0, 6}, {0x25, 8, 1, 3},+ {0x24, 8, 3, 2}, {0x27, 8, 10, 1}, {0x23, 8, 11, 1}, {0x22, 8, 12, 1}, {0x20, 8, 13, 1}, {0xA, 10, 0, 7},+ {0xC, 10, 1, 4}, {0xB, 10, 2, 3}, {0xF, 10, 4, 2}, {0x9, 10, 5, 2}, {0xE, 10, 14, 1}, {0xD, 10, 15, 1},+ {0x8, 10, 16, 1}, {0x1D, 12, 0, 8}, {0x18, 12, 0, 9}, {0x13, 12, 0, 10}, {0x10, 12, 0, 11}, {0x1B, 12, 1, 5},+ {0x14, 12, 2, 4}, {0x1C, 12, 3, 3}, {0x12, 12, 4, 3}, {0x1E, 12, 6, 2}, {0x15, 12, 7, 2}, {0x11, 12, 8, 2},+ {0x1F, 12, 17, 1}, {0x1A, 12, 18, 1}, {0x19, 12, 19, 1}, {0x17, 12, 20, 1}, {0x16, 12, 21, 1},+ {0x1A, 13, 0, 12}, {0x19, 13, 0, 13}, {0x18, 13, 0, 14}, {0x17, 13, 0, 15}, {0x16, 13, 1, 6}, {0x15, 13, 1, 7},+ {0x14, 13, 2, 5}, {0x13, 13, 3, 4}, {0x12, 13, 5, 3}, {0x11, 13, 9, 2}, {0x10, 13, 10, 2}, {0x1F, 13, 22, 1},+ {0x1E, 13, 23, 1}, {0x1D, 13, 24, 1}, {0x1C, 13, 25, 1}, {0x1B, 13, 26, 1}, {0x1F, 14, 0, 16},+ {0x1E, 14, 0, 17}, {0x1D, 14, 0, 18}, {0x1C, 14, 0, 19}, {0x1B, 14, 0, 20}, {0x1A, 14, 0, 21},+ {0x19, 14, 0, 22}, {0x18, 14, 0, 23}, {0x17, 14, 0, 24}, {0x16, 14, 0, 25}, {0x15, 14, 0, 26},+ {0x14, 14, 0, 27}, {0x13, 14, 0, 28}, {0x12, 14, 0, 29}, {0x11, 14, 0, 30}, {0x10, 14, 0, 31},+ {0x18, 15, 0, 32}, {0x17, 15, 0, 33}, {0x16, 15, 0, 34}, {0x15, 15, 0, 35}, {0x14, 15, 0, 36},+ {0x13, 15, 0, 37}, {0x12, 15, 0, 38}, {0x11, 15, 0, 39}, {0x10, 15, 0, 40}, {0x1F, 15, 1, 8}, {0x1E, 15, 1, 9},+ {0x1D, 15, 1, 10}, {0x1C, 15, 1, 11}, {0x1B, 15, 1, 12}, {0x1A, 15, 1, 13}, {0x19, 15, 1, 14},+ {0x13, 16, 1, 15}, {0x12, 16, 1, 16}, {0x11, 16, 1, 17}, {0x10, 16, 1, 18}, {0x14, 16, 6, 3}, {0x1A, 16, 11, 2},+ {0x19, 16, 12, 2}, {0x18, 16, 13, 2}, {0x17, 16, 14, 2}, {0x16, 16, 15, 2}, {0x15, 16, 16, 2},+ {0x1F, 16, 27, 1}, {0x1E, 16, 28, 1}, {0x1D, 16, 29, 1}, {0x1C, 16, 30, 1}, {0x1B, 16, 31, 1},+};++constexpr Dct kDctTable1[] = {+ {0x2, 2, 0, 1}, {0x2, 3, 1, 1}, {0x6, 3, 0, 2}, {0x7, 4, 0, 3}, {0x5, 5, 2, 1}, {0x7, 5, 3, 1}, {0x6, 5, 1, 2},+ {0x1C, 5, 0, 4}, {0x1D, 5, 0, 5}, {0x6, 6, 4, 1}, {0x7, 6, 5, 1}, {0x5, 6, 0, 6}, {0x4, 6, 0, 7},+ {0x1, 6, 255, 0}, {0x79, 7, 1, 3}, {0x7A, 7, 10, 1}, {0x7, 7, 2, 2}, {0x5, 7, 8, 1}, {0x78, 7, 9, 1},+ {0x6, 7, 6, 1}, {0x4, 7, 7, 1}, {0x7B, 7, 0, 8}, {0x7C, 7, 0, 9}, {0x26, 8, 3, 2}, {0x21, 8, 11, 1},+ {0x25, 8, 12, 1}, {0x24, 8, 13, 1}, {0x27, 8, 1, 4}, {0xFC, 8, 2, 3}, {0xFD, 8, 4, 2}, {0xFA, 8, 0, 12},+ {0xFB, 8, 0, 13}, {0xFE, 8, 0, 14}, {0xFF, 8, 0, 15}, {0x23, 8, 0, 10}, {0x22, 8, 0, 11}, {0x20, 8, 1, 5},+ {0x4, 9, 5, 2}, {0x5, 9, 14, 1}, {0x7, 9, 15, 1}, {0xD, 10, 16, 1}, {0xC, 10, 2, 4}, {0x1C, 12, 3, 3},+ {0x12, 12, 4, 3}, {0x1E, 12, 6, 2}, {0x15, 12, 7, 2}, {0x11, 12, 8, 2}, {0x1F, 12, 17, 1}, {0x1A, 12, 18, 1},+ {0x19, 12, 19, 1}, {0x17, 12, 20, 1}, {0x16, 12, 21, 1}, {0x16, 13, 1, 6}, {0x15, 13, 1, 7}, {0x14, 13, 2, 5},+ {0x13, 13, 3, 4}, {0x12, 13, 5, 3}, {0x11, 13, 9, 2}, {0x10, 13, 10, 2}, {0x1F, 13, 22, 1}, {0x1E, 13, 23, 1},+ {0x1D, 13, 24, 1}, {0x1C, 13, 25, 1}, {0x1B, 13, 26, 1}, {0x1F, 14, 0, 16}, {0x1E, 14, 0, 17},+ {0x1D, 14, 0, 18}, {0x1C, 14, 0, 19}, {0x1B, 14, 0, 20}, {0x1A, 14, 0, 21}, {0x19, 14, 0, 22},+ {0x18, 14, 0, 23}, {0x17, 14, 0, 24}, {0x16, 14, 0, 25}, {0x15, 14, 0, 26}, {0x14, 14, 0, 27},+ {0x13, 14, 0, 28}, {0x12, 14, 0, 29}, {0x11, 14, 0, 30}, {0x10, 14, 0, 31}, {0x18, 15, 0, 32},+ {0x17, 15, 0, 33}, {0x16, 15, 0, 34}, {0x15, 15, 0, 35}, {0x14, 15, 0, 36}, {0x13, 15, 0, 37},+ {0x12, 15, 0, 38}, {0x11, 15, 0, 39}, {0x10, 15, 0, 40}, {0x1F, 15, 1, 8}, {0x1E, 15, 1, 9}, {0x1D, 15, 1, 10},+ {0x1C, 15, 1, 11}, {0x1B, 15, 1, 12}, {0x1A, 15, 1, 13}, {0x19, 15, 1, 14}, {0x13, 16, 1, 15},+ {0x12, 16, 1, 16}, {0x11, 16, 1, 17}, {0x10, 16, 1, 18}, {0x14, 16, 6, 3}, {0x1A, 16, 11, 2}, {0x19, 16, 12, 2},+ {0x18, 16, 13, 2}, {0x17, 16, 14, 2}, {0x16, 16, 15, 2}, {0x15, 16, 16, 2}, {0x1F, 16, 27, 1},+ {0x1E, 16, 28, 1}, {0x1D, 16, 29, 1}, {0x1C, 16, 30, 1}, {0x1B, 16, 31, 1},+};++constexpr std::uint8_t kZigzag[64] = {+ 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5,+ 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,+ 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,+ 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63,+};++constexpr std::uint8_t kAlternate[64] = {+ 0, 8, 16, 24, 1, 9, 2, 10, 17, 25, 32, 40, 48, 56, 57, 49,+ 41, 33, 26, 18, 3, 11, 4, 12, 19, 27, 34, 42, 50, 58, 35, 43,+ 51, 59, 20, 28, 5, 13, 6, 14, 21, 29, 36, 44, 52, 60, 37, 45,+ 53, 61, 22, 30, 7, 15, 23, 31, 38, 46, 54, 62, 39, 47, 55, 63,+};++}diff --git a/runtime/src/system.cpp b/runtime/src/system.cppindex 6919981..0b7ae7c 100644--- a/runtime/src/system.cpp+++ b/runtime/src/system.cpp@@ -6,6 +6,7 @@#include "iop/iop.hpp"#include "iop/sif.hpp"#include "iop/sio2.hpp"+#include "ipu/ipu.hpp"#include "ps2/disc.hpp"#include "kernel.hpp"#include "ps2/generated.hpp"@@ -258,6 +259,7 @@ int run() {}}gs::stats();+ ipu::stats();dmac::stats();gif::stats();vif::stats();