Model the DC windows and VIC compose, and scan out what they describe

The display model took each window's line stride from PRESCALED_SIZE
rather than LINE_STRIDE, and kept one set of window parameters for all
four windows, so window D's 656-pixel log window leaked into window A.
Its block-linear decoder also had two GOB address bits swapped. The mock
VIC only memcpy'd, so Nyx's rotated frame never existed. The renderer
papered over all of this with per-address heuristics: a "known menu
stride" of 2624, forced block-linear decoding of the IPL carveout, and a
hard-coded 1280x720 geometry for Nyx's framebuffer.

- DC: a register file per window (A-D), reached through the indirect
  pages per DISPLAY_WINDOW_HEADER or the A/B/C direct ranges. Writes
  land in the assembly copy, and WIN_x_ACT_REQ promotes it to the active
  copy the scan-out reads. Other DC registers read back what was
  written. STATE_CONTROL reads 0, and INT_STATUS reports frame end and
  vblank, which bdk polls before DSI commands.
- VIC: on COMPOSE_START, read bdk's vic_config_t from guest memory and
  draw the enabled pitch slots into the target, with scaling, clipping
  and the output flip/transpose. Nyx's 270-degree rotation now happens.
- Scan-out: fetch each enabled window per SURFACE_KIND (pitch, tiled or
  block linear, per TRM 20.1.2 Figure 47), LINE_STRIDE and COLOR_DEPTH,
  turn it per WIN_OPTIONS (H/V direction, SCAN_COLUMN), scale it from
  PRESCALED_SIZE to SIZE, place it at POSITION, and stack and blend the
  windows by depth and BLEND_*. When the surface on show was turned on
  its way to the panel, by the DC or by VIC, the view undoes the turn,
  so Nyx still shows landscape. All the heuristics are gone.

hwtest's frame and Nyx's home screen are pixel-identical to before.
Nyx's error console (window D over window A) now shows; before, it was
invisible.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YYewwDPujjo5B2xRyC5Grb
This commit is contained in:
Claude committed 2026-09-25 18:40:26 +00:00
1 parent 39bfad6a0b
commit e32052ffd5
6 files changed
+795 -543

No files matched your search

+394 -310
View File
@@ -6,6 +6,7 @@
#include <SDL2/SDL.h>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
@@ -55,141 +56,335 @@ bool sdl_display_init() {
return true;
}
// ---- Scan-out ------------------------------------------------------------
//
// The frame is built the way the display controller builds it (TRM 24):
// every enabled window's surface is fetched per its SURFACE_KIND (pitch,
// 16x16 tiled or block linear) and LINE_STRIDE, turned per WIN_OPTIONS
// (H/V direction, SCAN_COLUMN), scaled from PRESCALED_SIZE to SIZE and
// placed at POSITION, the windows stacked by layer depth and blended per
// BLEND_*. The result is the 720x1280 portrait picture the panel receives.
//
// For the host window that picture is then turned: when the surface on show
// was itself turned on its way to the panel - by the DC (SCAN_COLUMN) or by
// a VIC compose, as Nyx does - the turn is undone, so a GUI drawn landscape
// is shown landscape. A surface drawn straight into portrait (hekate's TUI,
// hwtest) is shown as the panel gets it. Ctrl+R / Ctrl+Shift+R override the
// turn.
namespace {
constexpr uint32_t kPanelW = 720, kPanelH = 1280;
// A picture turn as a 2x2 matrix on centred coordinates: out = M * in.
struct Turn {
int a, b, c, d; // [[a b] [c d]]
};
Turn turn_of(uint32_t xf) { // dcwin::XF_* (flip, then transpose)
int fx = (xf & dcwin::XF_FLIP_X) ? -1 : 1;
int fy = (xf & dcwin::XF_FLIP_Y) ? -1 : 1;
if (xf & dcwin::XF_TRANSPOSE)
return {0, fy, fx, 0};
return {fx, 0, 0, fy};
}
Turn operator*(const Turn &l, const Turn &r) {
return {l.a * r.a + l.b * r.c, l.a * r.b + l.b * r.d,
l.c * r.a + l.d * r.c, l.c * r.b + l.d * r.d};
}
// The view rotation (as the render loop applies it, see rot below) that
// undoes turn t, or 0 when t is a mirror image, which no rotation undoes.
int undo_rot(const Turn &t) {
if (t.a * t.d - t.b * t.c != 1)
return 0;
Turn inv = {t.a, t.c, t.b, t.d}; // a rotation's inverse is its transpose
if (inv.a == 0 && inv.b == -1)
return 1;
if (inv.a == -1)
return 2;
if (inv.a == 0 && inv.b == 1)
return 3;
return 0;
}
uint32_t depth_bpp(uint32_t depth) {
if (depth <= 3)
return 1; // palettised
if (depth <= 7 || (depth >= 0x1E && depth <= 0x21))
return 2;
return 4;
}
// One pixel of a DC_WIN_COLOR_DEPTH format as ARGB8888.
uint32_t depth_argb(const uint8_t *p, uint32_t depth) {
auto x5 = [](uint32_t v) { return (v << 3) | (v >> 2); };
auto x6 = [](uint32_t v) { return (v << 2) | (v >> 4); };
switch (depth) {
case 0x4: { // B4G4R4A4
uint32_t v = p[0] | (p[1] << 8);
return ((v >> 12) * 0x11u) << 24 | ((v >> 8) & 0xF) * 0x11u << 16 |
((v >> 4) & 0xF) * 0x11u << 8 | (v & 0xF) * 0x11u;
}
case 0x5: { // B5G5R5A
uint32_t v = p[0] | (p[1] << 8);
return ((v & 0x8000) ? 0xFF000000u : 0) | x5((v >> 10) & 0x1F) << 16 |
x5((v >> 5) & 0x1F) << 8 | x5(v & 0x1F);
}
case 0x7: { // AB5G5R5
uint32_t v = p[0] | (p[1] << 8);
return ((v & 1) ? 0xFF000000u : 0) | x5(v >> 11) << 16 |
x5((v >> 6) & 0x1F) << 8 | x5((v >> 1) & 0x1F);
}
case 0xD: // R8G8B8A8
case 0xF:
return (uint32_t)p[3] << 24 | (uint32_t)p[0] << 16 |
(uint32_t)p[1] << 8 | p[2];
default:
break;
}
switch (depth_bpp(depth)) {
case 1:
return 0xFF000000u | p[0] * 0x010101u;
case 2: { // B5G6R5 and anything else 16-bit
uint32_t v = p[0] | (p[1] << 8);
return 0xFF000000u | x5(v >> 11) << 16 | x6((v >> 5) & 0x3F) << 8 |
x5(v & 0x1F);
}
default: // B8G8R8A8 (bytes B, G, R, A)
return (uint32_t)p[3] << 24 | (uint32_t)p[2] << 16 |
(uint32_t)p[1] << 8 | p[0];
}
}
struct Win {
int idx = 0;
uint64_t addr = 0;
uint32_t depth = 0, bpp = 4, kind = 0, gob_h = 1, stride = 0, gobs = 0;
uint32_t src_w = 0, src_h = 0; // the surface, before the DC turns it
uint32_t pre_w = 0, pre_h = 0; // PRESCALED_SIZE, in pixels (post-turn)
uint32_t out_w = 0, out_h = 0; // SIZE
int32_t x = 0, y = 0;
uint32_t xf = 0, layer = 0, blend_layer = 0, blend_match = 0;
size_t len = 0;
const uint8_t *mem = nullptr;
std::vector<uint8_t> copy; // when the surface is not in the DRAM window
};
// Byte offset of surface pixel (sx, sy).
inline size_t fetch_off(const Win &w, uint32_t sx, uint32_t sy) {
uint32_t xb = sx * w.bpp;
switch (w.kind) {
case 1: // 16x16-byte tiles, row-major
return (size_t)(sy / 16) * w.stride * 16 + (xb / 16) * 256 +
(sy % 16) * 16 + (xb % 16);
case 2: { // block linear, TRM 20.1.2 (Figure 47 sector order)
uint32_t gob_y = sy / 8;
size_t gob = ((size_t)(gob_y / w.gob_h) * w.gobs + xb / 64) * w.gob_h +
gob_y % w.gob_h;
uint32_t x = xb % 64, y = sy % 8;
return gob * 512 + (x / 32) * 256 + (y / 2) * 64 + ((x % 32) / 16) * 32 +
(y % 2) * 16 + (x % 16);
}
default:
return (size_t)sy * w.stride + xb;
}
}
// Build window w from its active register file; false if it shows nothing.
bool load_window(EmuState *state, uc_engine *uc, int idx, const uint32_t *r,
int swizzle_override, Win &w) {
if (!(r[dcwin::OPTIONS] & dcwin::WIN_ENABLE))
return false;
w.idx = idx;
w.depth = r[dcwin::COLOR_DEPTH] & 0x7F;
w.bpp = depth_bpp(w.depth);
uint32_t sk = r[dcwin::SURFACE_KIND];
w.kind = sk & 3;
w.gob_h = 1u << ((sk >> 4) & 7);
if (swizzle_override == 0) {
w.kind = 0;
} else if (swizzle_override == 2 && w.kind != 2) {
w.kind = 2;
w.gob_h = 16;
}
w.out_w = r[dcwin::SIZE] & 0x1FFF;
w.out_h = (r[dcwin::SIZE] >> 16) & 0x1FFF;
w.pre_w = (r[dcwin::PRESCALED] & 0x7FFF) / w.bpp;
w.pre_h = (r[dcwin::PRESCALED] >> 16) & 0x1FFF;
if (!w.pre_w || !w.pre_h) { // never programmed: no scaling
w.pre_w = w.out_w;
w.pre_h = w.out_h;
}
if (!w.out_w || !w.out_h)
return false;
uint32_t opt = r[dcwin::OPTIONS];
w.xf = ((opt & dcwin::H_DIRECTION) ? dcwin::XF_FLIP_X : 0) |
((opt & dcwin::V_DIRECTION) ? dcwin::XF_FLIP_Y : 0) |
((opt & dcwin::SCAN_COLUMN) ? dcwin::XF_TRANSPOSE : 0);
// SCAN_COLUMN walks the surface's columns: it is pre_h wide, pre_w tall.
bool column = w.xf & dcwin::XF_TRANSPOSE;
w.src_w = column ? w.pre_h : w.pre_w;
w.src_h = column ? w.pre_w : w.pre_h;
w.x = (int16_t)(r[dcwin::POSITION] & 0xFFFF);
w.y = (int16_t)(r[dcwin::POSITION] >> 16);
w.blend_layer = r[dcwin::BLEND_LAYER];
w.blend_match = r[dcwin::BLEND_MATCH];
w.layer = w.blend_layer & 0xFF;
w.stride = r[dcwin::LINE_STRIDE] & 0xFFFF;
if (!w.stride)
w.stride = w.src_w * w.bpp;
w.addr = (uint64_t)r[dcwin::START_ADDR] + (int64_t)state->manual_offset;
switch (w.kind) {
case 2:
w.gobs = (w.stride + 63) / 64;
w.len = (size_t)w.gobs * 512 * w.gob_h *
((w.src_h + 8 * w.gob_h - 1) / (8 * w.gob_h));
break;
case 1:
w.len = (size_t)w.stride * ((w.src_h + 15) / 16) * 16;
break;
default:
w.len = (size_t)w.stride * w.src_h;
break;
}
if (w.len == 0 || w.len > 64u * 1024 * 1024)
return false;
if (state->dram_low_ptr && w.addr >= DRAM_BASE &&
w.addr + w.len <= DRAM_BASE + DRAM_WINDOW_SIZE) {
w.mem = state->dram_low_ptr + (w.addr - DRAM_BASE);
} else {
w.copy.resize(w.len);
if (uc_mem_read(uc, w.addr, w.copy.data(), w.len) != UC_ERR_OK)
return false;
w.mem = w.copy.data();
}
return true;
}
// Blend factor (0..255) per BLEND_MATCH_SELECT (TRM 24.10.13).
uint32_t src_factor(uint32_t sel, uint32_t k1, uint32_t sa) {
switch (sel) {
case 1: return 255;
case 2: case 3: return k1; // K1, K1 x dst alpha (opaque dst)
case 4: return 255 - k1; // 1 - K1 x dst alpha
case 5: return k1 * sa / 255; // K1 x src alpha
default: return 0;
}
}
uint32_t dst_factor(uint32_t sel, uint32_t k1, uint32_t k2, uint32_t sa) {
switch (sel) {
case 1: return 255;
case 2: case 4: return k1;
case 3: return k2;
case 5: case 7: return 255 - k1;
case 6: return 255 - k1 * sa / 255;
default: return 0;
}
}
// Draw window w onto the panel picture.
void draw_window(const Win &w, std::vector<uint32_t> &panel) {
bool blend = !(w.blend_layer & (1u << 24));
uint32_t k1 = (w.blend_layer >> 8) & 0xFF, k2 = (w.blend_layer >> 16) & 0xFF;
uint32_t ssel = w.blend_match & 7, dsel = (w.blend_match >> 4) & 7;
bool hflip = w.xf & dcwin::XF_FLIP_X, vflip = w.xf & dcwin::XF_FLIP_Y;
bool column = w.xf & dcwin::XF_TRANSPOSE;
// Column scaling map, worked out once per window rather than per pixel.
static std::vector<uint32_t> txs;
txs.resize(w.out_w);
for (uint32_t ox = 0; ox < w.out_w; ox++)
txs[ox] = (uint32_t)((uint64_t)ox * w.pre_w / w.out_w);
for (uint32_t oy = 0; oy < w.out_h; oy++) {
int32_t py = w.y + (int32_t)oy;
if (py < 0 || py >= (int32_t)kPanelH)
continue;
uint32_t ty = (uint32_t)((uint64_t)oy * w.pre_h / w.out_h);
for (uint32_t ox = 0; ox < w.out_w; ox++) {
int32_t px = w.x + (int32_t)ox;
if (px < 0 || px >= (int32_t)kPanelW)
continue;
uint32_t tx = txs[ox];
// (tx, ty) is post-turn; walk it back to the surface.
uint32_t a = column ? ty : tx, b = column ? tx : ty;
uint32_t sx = hflip ? w.src_w - 1 - a : a;
uint32_t sy = vflip ? w.src_h - 1 - b : b;
size_t off = fetch_off(w, sx, sy);
if (off + w.bpp > w.len)
continue;
uint32_t s = depth_argb(w.mem + off, w.depth);
uint32_t &d = panel[(size_t)py * kPanelW + px];
if (!blend) {
d = s | 0xFF000000u;
continue;
}
uint32_t sa = s >> 24;
uint32_t fs = src_factor(ssel, k1, sa), fd = dst_factor(dsel, k1, k2, sa);
uint32_t o = 0xFF000000u;
for (int sh = 0; sh < 24; sh += 8) {
uint32_t c = (((s >> sh) & 0xFF) * fs + ((d >> sh) & 0xFF) * fd) / 255;
o |= (c > 255 ? 255 : c) << sh;
}
d = o;
}
}
}
} // namespace
void sdl_display_update(EmuState *state, uc_engine *uc) {
if (!renderer)
return;
uint32_t width = state->fb_width, height = state->fb_height,
stride = state->fb_stride;
if (width < 16 || height < 16)
return;
// If the current fb_addr is empty but we have a saved Window A with
// content, prefer the Window A surface. This handles the case where
// Hekate's TUI renders to IPL_FB (0xF5A00000) but the DC was repointed
// to NYX_FB (0xF6200000) by a failed Nyx init sequence.
uint64_t eff_addr = state->fb_addr;
if (state->winA_addr && state->winA_addr != eff_addr) {
// Quick check: sample a few dwords from fb_addr.
uint32_t sample[4] = {0};
uc_mem_read(uc, eff_addr, sample, sizeof(sample));
bool empty = (sample[0] == 0 && sample[1] == 0 &&
sample[2] == 0 && sample[3] == 0);
if (empty) {
// Check if winA_addr has content.
uc_mem_read(uc, state->winA_addr, sample, sizeof(sample));
if (sample[0] != 0 || sample[1] != 0 ||
sample[2] != 0 || sample[3] != 0) {
eff_addr = state->winA_addr;
width = state->winA_w;
height = state->winA_h;
stride = state->winA_stride;
}
}
// The windows on show, bottom first. Until the payload programs the DC,
// the emulator's own framebuffer stands in as window A.
std::vector<Win> wins;
uint32_t boot_win[dcwin::kRegs] = {};
boot_win[dcwin::OPTIONS] = dcwin::WIN_ENABLE;
boot_win[dcwin::COLOR_DEPTH] = 0xC; // B8G8R8A8
boot_win[dcwin::SIZE] = kPanelH << 16 | kPanelW;
boot_win[dcwin::LINE_STRIDE] = kPanelW * 4;
boot_win[dcwin::BLEND_LAYER] = dcwin::BLEND_LAYER_RESET;
boot_win[dcwin::START_ADDR] = (uint32_t)FB_BASE;
for (int i = 0; i < 4; i++) {
const uint32_t *r = state->dc_programmed ? state->dc_win_active[i]
: (i == 0 ? boot_win : nullptr);
if (!r)
continue;
Win w;
if (load_window(state, uc, i, r, g_swizzle_override, w))
wins.push_back(std::move(w));
}
// Deeper layers first; equal depths stack A (bottom) to D (top).
std::stable_sort(wins.begin(), wins.end(), [](const Win &l, const Win &r) {
return l.layer > r.layer;
});
// Helper: is the address inside the IPL framebuffer carveout?
auto addr_in_ipl_fb = [](uint64_t a) -> bool {
return a >= FB_BASE && a < FB_BASE + FB_SIZE;
};
// Auto-detect first; a manual override is applied on top further down.
int rot = -1;
uint32_t sw = state->fb_swizzle;
uint32_t eff_stride = stride;
uint32_t bpp = 4;
uint32_t bh = state->fb_bh ? state->fb_bh : 16; // Block height in GOBs
// --- OVERRIDES & AUTO-DETECTS ---
constexpr uint64_t kNyxFbBase = 0xF6200000ULL;
if (g_swizzle_override != -1) {
sw = (uint32_t)g_swizzle_override; // Manual User Override (S Key)
} else if (eff_addr == kNyxFbBase) {
// Nyx GUI uses VIC to rotate its 1280x720 landscape UI into 720x1280 portrait mode (NYX_FB_ADDRESS).
// Our emulator's mock VIC only memcpys the data, so 0xF6200000 actually holds a 1280x720 landscape image!
// Since the DC is programmed for 720x1280, it will scramble it.
// We override the display geometry here to render the raw 1280x720 memory correctly.
sw = 0;
width = 1280;
height = 720;
eff_stride = 1280 * 4; // 32-bit XRGB
rot = 0;
bh = 4;
} else if (eff_addr == FB_BASE && sw == 0 && width == 720u &&
height == 1280u && eff_stride == width * 4u) {
// Hekate TUI or static bootlogo: natively rendered as 720x1280 pitch-linear portrait.
// Do not apply any automatic layout overrides.
sw = 0;
rot = 0; // The UI is drawn sideways in memory to appear upright on a landscape physical Switch screen.
// On PC, we want to view it rotated so it resembles the physical landscape Switch!
} else if (addr_in_ipl_fb(eff_addr) && sw == 0) {
// IPL carveout with Sw=0: Hekate writes block-linear data but the DC
// surface kind says pitch. Detect when the stride/dimensions don't
// match a coherent pitch-linear layout and force block-linear decode.
bool coherent_pitch =
width > 0 && (eff_stride == width * 4u || eff_stride == width * 2u);
bool portrait_stride_mismatch =
height == 1280u && width == 720u && eff_stride != width * 4u;
bool known_menu_stride = (eff_stride == 2624u && !coherent_pitch);
// crop_portrait only forces BL when stride is incoherent — if stride
// matches width*bpp, the data may already be VIC-de-swizzled pitch.
bool crop_portrait_incoherent =
height == 1280u && width < 720u && width >= 640u && !coherent_pitch;
if (!coherent_pitch || portrait_stride_mismatch || known_menu_stride ||
crop_portrait_incoherent) {
sw = 2; // Force block-linear decode
bh = 4; // Hekate menu uses BH=4
}
// The largest window decides how the picture is turned for the host.
const Win *primary = nullptr;
for (const Win &w : wins)
if (!primary || (uint64_t)w.out_w * w.out_h >
(uint64_t)primary->out_w * primary->out_h ||
((uint64_t)w.out_w * w.out_h ==
(uint64_t)primary->out_w * primary->out_h &&
w.idx < primary->idx))
primary = &w;
int rot = 0;
if (primary) {
Turn t = turn_of(primary->xf);
if (state->vic_out_addr && primary->addr == state->vic_out_addr)
t = t * turn_of(state->vic_out_xform);
rot = undo_rot(t);
}
// Final heuristic: NEVER auto-rotate 720x1280 portrait buffers to landscape!
if (rot == -1) {
if (width == 720 && height == 1280) {
rot = 0; // Hekate already drew it sideways for physical portrait, so rot=0 makes it look like a phone on PC monitor!
// Actually wait: if drawn upright in memory (row-by-row), then rot=0 produces a tall 720x1280 window on PC where it is perfectly upright!
// The user wants the white lines to be horizontal. This means we MUST let the window be 720x1280!
} else {
rot = (height > width) ? 1 : 0; // Legacy heuristic for other stuff
}
}
// Manual rotation override always wins. The auto-detect branches above set
// rot = 0 for Nyx and Hekate TUI surfaces; if the user pressed R/Shift+R
// they expect the rotation to actually change, regardless of what the
// auto-detect picked. -1 means "use auto-detect", anything else overrides.
state->last_auto_rot.store((uint32_t)(rot & 3));
state->last_auto_rot.store((uint32_t)rot);
if (state->rotation_override != -1)
rot = state->rotation_override & 3;
// --- Compute GOB stride for block-linear ---
uint32_t sw_gobs = (width * bpp + 63) / 64;
if (sw == 2) {
// For IPL carveout portrait surfaces, Hekate allocates a full 720px-wide
// BL surface even when the DC window is cropped to 656px.
if (addr_in_ipl_fb(state->fb_addr) && state->fb_addr != kNyxFbBase &&
bpp == 4 && height == 1280u && width >= 640u && width <= 720u) {
uint32_t sw_720 = (720u * bpp + 63) / 64u;
if (sw_gobs < sw_720)
sw_gobs = sw_720;
}
// If DC provides a stride that's GOB-aligned, prefer it
if (eff_stride >= 64u && (eff_stride % 64u) == 0u) {
uint32_t stride_gobs = eff_stride / 64u;
if (stride_gobs > sw_gobs)
sw_gobs = stride_gobs;
}
}
uint32_t out_w = (rot & 1) ? height : width;
uint32_t out_h = (rot & 1) ? width : height;
// Cache the effective rotation and output dims so the SDL event handler can
// invert the framebuffer→window transform when mapping mouse → panel coords.
state->last_rot.store((uint32_t)((rot < 0) ? 0 : (rot & 3)));
uint32_t out_w = (rot & 1) ? kPanelH : kPanelW;
uint32_t out_h = (rot & 1) ? kPanelW : kPanelH;
// The SDL event handler inverts this view to map mouse -> panel coords.
state->last_rot.store((uint32_t)rot);
state->last_out_w.store(out_w);
state->last_out_h.store(out_h);
@@ -197,8 +392,8 @@ void sdl_display_update(EmuState *state, uc_engine *uc) {
if (!texture || out_w != last_w || out_h != last_h) {
if (texture)
SDL_DestroyTexture(texture);
// Use ARGB8888: natively matches Tegra's BGRA memory layout on Little
// Endian
// ARGB8888 matches the B8G8R8A8 bytes of a Tegra surface on a
// little-endian host.
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING, out_w, out_h);
last_w = out_w;
@@ -206,194 +401,84 @@ void sdl_display_update(EmuState *state, uc_engine *uc) {
SDL_SetWindowSize(window, out_w, out_h);
}
static uint32_t last_log_p[6] = {0}; // Addr, W, H, Rot, Sw, BH
if (state->fb_addr != last_log_p[0] || width != last_log_p[1] ||
height != last_log_p[2] || (uint32_t)rot != last_log_p[3] ||
sw != last_log_p[4] || bh != last_log_p[5]) {
printf("[display] LATCH: 0x%X (%dx%d), Rot: %d (Manual: %d), Sw: %d, "
"GOBs: %u, BH: %u, BPP: %u\n",
(uint32_t)state->fb_addr, width, height, rot,
state->rotation_override, sw, sw_gobs, bh, bpp * 8);
last_log_p[0] = state->fb_addr;
last_log_p[1] = width;
last_log_p[2] = height;
last_log_p[3] = rot;
last_log_p[4] = sw;
last_log_p[5] = bh;
// Skip the rebuild and the texture upload when neither the surfaces' bytes
// nor the way they are laid out changed since the last frame - the common
// case for a payload sitting on a menu or waiting on hardware.
std::vector<uint64_t> shape = {(uint64_t)rot, wins.size()}, where;
for (const Win &w : wins) {
shape.insert(shape.end(),
{(uint64_t)w.idx, w.addr, w.len, w.depth, w.kind, w.gob_h,
w.stride, (uint64_t)w.pre_w << 32 | w.pre_h,
(uint64_t)w.out_w << 32 | w.out_h, w.xf, w.blend_layer,
w.blend_match});
where.push_back((uint64_t)(uint32_t)w.x << 32 | (uint32_t)w.y);
}
static std::vector<uint64_t> last_shape, last_where;
static std::vector<uint8_t> last_bytes;
size_t total = 0;
for (const Win &w : wins)
total += w.len;
bool same = shape == last_shape && where == last_where &&
last_bytes.size() == total;
for (size_t i = 0, pos = 0; same && i < wins.size(); pos += wins[i++].len)
same = memcmp(last_bytes.data() + pos, wins[i].mem, wins[i].len) == 0;
eff_addr =
(eff_addr ? eff_addr : FB_BASE) +
(uint64_t)(int64_t)state->manual_offset;
// Compute framebuffer byte size
size_t fb_size = (sw == 2) ? ((size_t)sw_gobs * ((height + 7) / 8) * 512)
: ((size_t)eff_stride * height);
if (fb_size < 1024)
return;
if (sw != 2 && eff_stride < 16)
return; // Pitch-linear sanity check only
if (fb_size > 32 * 1024 * 1024)
fb_size = 32 * 1024 * 1024;
// The surface is read straight out of the host memory backing emulated
// DRAM when it lives there (it always does for hekate/Nyx), instead of a
// uc_mem_read into a fresh 3.5 MB vector - and the converted frame reuses
// one buffer too. The old code allocated and zero-filled two such vectors
// on every frame, 60 times a second, which showed up in profiles as page-
// fault and munmap churn on the emulation thread.
static std::vector<uint8_t> fallback;
const uint8_t *src = nullptr;
if (state->dram_low_ptr && eff_addr >= DRAM_BASE &&
eff_addr + fb_size <= DRAM_BASE + DRAM_WINDOW_SIZE) {
src = state->dram_low_ptr + (eff_addr - DRAM_BASE);
} else {
fallback.resize(fb_size);
if (uc_mem_read(uc, eff_addr, fallback.data(), fb_size) == UC_ERR_OK)
src = fallback.data();
}
// Skip the de-swizzle and the texture upload when neither the surface
// bytes nor the way they are laid out has changed since the last frame -
// the common case for a payload sitting on a menu or waiting on hardware.
struct Layout {
uint64_t addr = 0;
uint32_t w = 0, h = 0, stride = 0, sw = 0, bh = 0, gobs = 0, out_w = 0,
out_h = 0;
int rot = 0;
bool operator==(const Layout &o) const {
return addr == o.addr && w == o.w && h == o.h && stride == o.stride &&
sw == o.sw && bh == o.bh && gobs == o.gobs && out_w == o.out_w &&
out_h == o.out_h && rot == o.rot;
}
};
static Layout last_layout;
static std::vector<uint8_t> last_src;
Layout layout;
layout.addr = eff_addr;
layout.w = width;
layout.h = height;
layout.stride = eff_stride;
layout.sw = sw;
layout.bh = bh;
layout.gobs = sw_gobs;
layout.out_w = out_w;
layout.out_h = out_h;
layout.rot = rot;
bool unchanged = src && layout == last_layout && last_src.size() == fb_size &&
memcmp(last_src.data(), src, fb_size) == 0;
static bool snapshot_pending = true;
if (src && !unchanged) {
last_layout = layout;
last_src.assign(src, src + fb_size);
if (!same) {
// Log layout changes, but not moves: bdk slides its log window in.
if (shape != last_shape)
printf("[display] scan-out: %zu window(s), view %ux%u, rot %d (auto %u)\n",
wins.size(), out_w, out_h, rot, state->last_auto_rot.load());
last_shape = shape;
last_where = where;
last_bytes.resize(total);
size_t pos = 0;
for (const Win &w : wins) {
memcpy(last_bytes.data() + pos, w.mem, w.len);
pos += w.len;
}
snapshot_pending = true;
const uint8_t *buf_data = src;
std::vector<uint32_t> &proc = g_frame;
proc.assign((size_t)out_w * out_h, 0xFF000000); // opaque black
for (uint32_t sy = 0; sy < height; sy++) {
for (uint32_t sx = 0; sx < width; sx++) {
uint32_t off = 0;
if (sw == 2) {
if (bpp == 4) {
// Use tegra_bl_byte_off_rgba8888 formula inline for 32bpp
uint32_t gob_x = sx / 16u;
uint32_t pixel_x = sx % 16u;
uint32_t gob_y = sy / 8u;
uint32_t line_y = sy % 8u;
uint32_t block_y = gob_y / bh;
uint32_t gob_in_block_y = gob_y % bh;
static std::vector<uint32_t> panel;
panel.assign((size_t)kPanelW * kPanelH, 0xFF000000u); // black background
for (const Win &w : wins)
draw_window(w, panel);
uint32_t idx =
(block_y * sw_gobs * bh * 128u) + (gob_x * bh * 128u) +
(gob_in_block_y * 128u) + (pixel_x & 3u) +
((line_y & 1u) << 2) + ((pixel_x & 4u) << 1) +
((line_y & 2u) << 3) + ((pixel_x & 8u) << 2) +
((line_y & 4u) << 4);
off = idx; // Already in uint32 units
} else {
// 16bpp block-linear (Nyx RGB565)
uint32_t byte_x = sx * bpp;
uint32_t gx = byte_x / 64;
uint32_t px_in = byte_x % 64;
uint32_t gy = sy / 8;
uint32_t ly = sy % 8;
uint32_t gob_off = ((ly >> 1) << 7) + ((px_in >> 4) << 5) +
((ly & 1) << 4) + (px_in & 0xF);
uint32_t gob_idx =
(gy / bh) * (sw_gobs * bh) + gx * bh + (gy % bh);
uint32_t byte_off = gob_idx * 512 + gob_off;
// Read 16-bit pixel and expand to 32-bit ARGB
if (byte_off + 2 <= fb_size) {
uint16_t u16 = *(const uint16_t *)&buf_data[byte_off];
uint32_t p = (((u16 >> 11) & 0x1F) << 19) |
(((u16 >> 5) & 0x3F) << 10) |
((u16 & 0x1F) << 3) | 0xFF000000;
uint32_t dx = sx, dy = sy;
switch (rot) {
case 1: dx = height - 1 - sy; dy = sx; break;
case 2: dx = width - 1 - sx; dy = height - 1 - sy; break;
case 3: dx = sy; dy = width - 1 - sx; break;
default: break;
}
if (dx < out_w && dy < out_h)
proc[dy * out_w + dx] = p;
}
continue; // Skip the common 32bpp path below
}
} else {
off = (sy * eff_stride + sx * bpp) / 4;
}
if (off < fb_size / 4) {
uint32_t p = ((const uint32_t *)buf_data)[off];
uint32_t dx = sx, dy = sy;
switch (rot) {
case 1:
dx = height - 1 - sy;
dy = sx;
break;
case 2:
dx = width - 1 - sx;
dy = height - 1 - sy;
break;
case 3:
dx = sy;
dy = width - 1 - sx;
break;
default:
break; // 0
}
if (dx < out_w && dy < out_h) {
// Native BGRA to ARGB8888 conversion mapping (forces A to 0xFF)
proc[dy * out_w + dx] = p | 0xFF000000;
}
// Turn the panel picture for the view: a panel pixel (sx, sy) of the
// W x H picture lands at
// rot 1: (H-1-sy, sx) 2: (W-1-sx, H-1-sy) 3: (sy, W-1-sx)
g_frame.resize((size_t)out_w * out_h);
for (uint32_t sy = 0; sy < kPanelH; sy++) {
const uint32_t *row = &panel[(size_t)sy * kPanelW];
for (uint32_t sx = 0; sx < kPanelW; sx++) {
uint32_t dx = sx, dy = sy;
switch (rot) {
case 1: dx = kPanelH - 1 - sy; dy = sx; break;
case 2: dx = kPanelW - 1 - sx; dy = kPanelH - 1 - sy; break;
case 3: dx = sy; dy = kPanelW - 1 - sx; break;
default: break;
}
g_frame[(size_t)dy * out_w + dx] = row[sx];
}
}
SDL_UpdateTexture(texture, nullptr, proc.data(), out_w * 4);
SDL_UpdateTexture(texture, nullptr, g_frame.data(), out_w * 4);
}
if (src) {
// Snapshot for PNG conversion — at most once a second (~60 frames), and
// only when the frame has changed since the last one written, so an idle
// payload no longer rewrites a 3.5 MB file every second.
static int snap_counter = 0;
const std::vector<uint32_t> &proc = g_frame;
if (++snap_counter >= 60 && snapshot_pending && !proc.empty()) {
snap_counter = 0;
snapshot_pending = false;
FILE *f = fopen("last_fb.rgba", "wb");
if (f) {
fwrite(proc.data(), 1, proc.size() * 4, f);
fclose(f);
FILE *fm = fopen("last_fb.meta", "w");
if (fm) {
fprintf(fm, "%u %u\n", out_w, out_h);
fclose(fm);
}
// Snapshot for PNG conversion - at most once a second (~60 frames), and
// only when the frame has changed since the last one written, so an idle
// payload does not rewrite a 3.5 MB file every second.
static int snap_counter = 0;
if (++snap_counter >= 60 && snapshot_pending && !g_frame.empty()) {
snap_counter = 0;
snapshot_pending = false;
FILE *f = fopen("last_fb.rgba", "wb");
if (f) {
fwrite(g_frame.data(), 1, g_frame.size() * 4, f);
fclose(f);
FILE *fm = fopen("last_fb.meta", "w");
if (fm) {
fprintf(fm, "%u %u\n", out_w, out_h);
fclose(fm);
}
}
}
@@ -426,12 +511,11 @@ static bool window_to_panel(EmuState *state, int mx, int my,
if (tx >= out_w) tx = out_w - 1;
if (ty >= out_h) ty = out_h - 1;
// With the auto-detected rotation, the rendered SDL view represents the
// physical Switch screen in its intended orientation (post-rotation, post-
// de-swizzle): what the user sees is what they would see looking at a
// Switch held landscape. A manual override (R / Shift+R) turns that view
// by a further quarter-turn or two, so undo exactly that extra turn first:
// the render maps a source pixel (sx, sy) of a W x H image to
// With the auto-detected turn, the view shows the picture the way the
// payload drew it (Nyx: landscape, as on a Switch held landscape). A
// manual override (Ctrl+R / Ctrl+Shift+R) turns that view by a further
// quarter-turn or two, so undo exactly that extra turn first: the render
// maps a source pixel (sx, sy) of a W x H image to
// 1: (H-1-sy, sx) 2: (W-1-sx, H-1-sy) 3: (sy, W-1-sx)
uint32_t delta = (state->last_rot.load() - state->last_auto_rot.load()) & 3;
uint32_t aw = (delta & 1) ? out_h : out_w; // auto-rotated view's size