foxygit / fake-08-xbox360 Log in
commit 4675c4c8e66eacb4bf4c7d5f9a4accfd95751509
Author:     mrfox <jens.se@icloud.com>
AuthorDate: Wed Nov 25 16:34:01 2026 +0100
Commit:     mrfox <jens.se@icloud.com>
CommitDate: Wed Nov 25 16:34:01 2026 +0100

    xex video: show the Guide (XAM's surface from VdSwap)

    VdSwap gets real outputs; while XAM draws the Guide it hands back XAM's
    surface, which is shown in place of the title's with the scaler set for
    its size, and the kernel's scaling comes back when it closes. The title
    describes its display to XAM (graphics notification event 1) at init.
    Approach from xsdk (https://github.com/jessenic/xsdk, MIT). The video
    test no longer ends by itself while the Guide shows.

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
 platform/xex/README.md               |   8 +-
 platform/xex/runtime/extra_imports.S |   4 +
 platform/xex/runtime/video.c         | 150 +++++++++++++++++++++++++++++++++--
 platform/xex/runtime/xex_video.h     |   4 +
 platform/xex/tests/video/main.cpp    |   7 +-
 5 files changed, 164 insertions(+), 9 deletions(-)

diff --git a/platform/xex/README.md b/platform/xex/README.md
index 98c1b92..a6f08f0 100644
--- a/platform/xex/README.md
+++ b/platform/xex/README.md
@@ -59,6 +59,12 @@ read-only, so file writes only work on a console.
   is committed the way XAM does it (0x2840/0x2844 + D1GRPH, then 0x60EC = 1);
   found by [xsdk](https://github.com/jessenic/xsdk) (docs/HARDWARE.md).
   Pixels: `B << 24 | G << 16 | R << 8 | A`.
+- **Guide**: every frame goes through `VdSwap` with the kernel's system
+  command buffer; while XAM draws the Guide, `VdSwap` hands back XAM's
+  surface, which is shown instead (scaler set for its size, the kernel's
+  scaling back afterwards), and XAM is told the title's display size
+  (graphics notification event 1). As found by xsdk. A title must not end by
+  itself while the Guide shows (`xex_video_guide_visible()`).
 - **Input** (`xex_input.h`): `XamInputGetState` per port.

 ## Tests
@@ -67,4 +73,4 @@ read-only, so file writes only work on a console.
 |---|---|
 | `tests/hello` | static constructors, C++ exceptions, `std::regex`, notifications, exit to dashboard |
 | `tests/fs` | time, `clock()`, directory listing, write/append/read/seek/stat/mkdir/remove on every device; writes `game:/fs-test-report.txt` |
-| `tests/video` | CPU-drawn frames at 60 fps through the title's own GPU ring, controller input (d-pad moves a square, A/B/X/Y colour it); a row of 16 boxes lights up with the pressed buttons and every controller change is logged to `game:/video-log.txt`; exits after 60 s or on Back+Start |
+| `tests/video` | CPU-drawn frames at 60 fps through the title's own GPU ring, controller input (d-pad moves a square, A/B/X/Y colour it); a row of 16 boxes lights up with the pressed buttons and every controller change is logged to `game:/video-log.txt`; exits after 60 s (not while the Guide shows) or on Back+Start; the Xbox button opens the Guide, whose Xbox Home leaves |
diff --git a/platform/xex/runtime/extra_imports.S b/platform/xex/runtime/extra_imports.S
index 2ebb0ad..4d30aa8 100644
--- a/platform/xex/runtime/extra_imports.S
+++ b/platform/xex/runtime/extra_imports.S
@@ -46,3 +46,7 @@ xbox360_stub VdQueryVideoMode, 0x010001ca
 	xbox360_end VdInitializeScalerCommandBuffer, 0x010001c5
 .endif

+
+/* VOID VdCallGraphicsNotificationRoutines(ULONG Event, PVOID Data) - xboxkrnl 0x1B1.
+ * Event 1 with 64 bytes describes the title's display to XAM (as D3D does). */
+xbox360_stub VdCallGraphicsNotificationRoutines, 0x010001b1
diff --git a/platform/xex/runtime/video.c b/platform/xex/runtime/video.c
index 902a877..70483aa 100644
--- a/platform/xex/runtime/video.c
+++ b/platform/xex/runtime/video.c
@@ -25,6 +25,8 @@ extern ULONG VdInitializeScalerCommandBuffer(ULONG source_xy, ULONG source_wh, U
                                              ULONG horizontal_filter_type, void *horizontal_filter,
                                              ULONG unknown9, PULONG commands, ULONG command_dwords);

+extern void VdCallGraphicsNotificationRoutines(ULONG event, const void *data);
+
 void (*xex_video_trace)(const char *fmt, ...);
 #define TRACE(...) do { if (xex_video_trace) xex_video_trace(__VA_ARGS__); } while (0)

@@ -291,6 +293,8 @@ typedef struct {
     uint32_t fetch_constant[6];
 } front_buffer;

+typedef struct { uint32_t address, pitch, x_end, y_end; } display_surface_t;
+
 static struct {
     int open;
     ring_t ring;
@@ -304,6 +308,13 @@ static struct {
     const char *error;
     int presented;
     uint32_t surface_height;
+    /* The Guide: XAM's surface shows in place of ours while VdSwap hands it back. */
+    int pending_system;
+    display_surface_t pending_surface;
+    int showing_system;
+    unsigned system_frames;
+    int guide_supported;
+    uint32_t output_width, output_height;
 } display;

 /* fetch.rs: texture fetch constant of a tiled 8:8:8:8 front buffer */
@@ -367,9 +378,46 @@ static unsigned flip_count;
 #define REG_D1GRPH_Y_END        (0x6138 / 4)
 #define REG_DOUBLE_BUFFER_CTL   (0x60ec / 4)

-typedef struct { uint32_t address, pitch, x_end, y_end; } display_surface;
+typedef display_surface_t display_surface;
 static display_surface old_surface;

+/*
+ * Scaler registers (byte offsets; xsdk src/display.c): enable, tap control,
+ * viewport size, viewport width / 4 - 1, then filter control, scale (8.24)
+ * and start phase, horizontal and vertical. 0x65CC bit 16 holds them until
+ * it's released.
+ */
+#define SCALER_REGISTERS 10
+static const uint16_t scaler_offsets[SCALER_REGISTERS] = {
+    0x6590, 0x6594, 0x6584, 0x65e8, 0x65b0, 0x65b4, 0x65b8, 0x65c0, 0x65c4, 0x65c8};
+#define REG_SCALER_UPDATE (0x65cc / 4)
+static uint32_t kernel_scaler[SCALER_REGISTERS];
+
+static void scaler_write(const uint32_t values[SCALER_REGISTERS])
+{
+    gpu_write(REG_SCALER_UPDATE, 0x10000);
+    for (int i = 0; i < SCALER_REGISTERS; i++) gpu_write(scaler_offsets[i] / 4, values[i]);
+    gpu_write(REG_SCALER_UPDATE, 0);
+}
+
+/* The scaler filling the output with a width x height surface: 2 taps each
+ * way through the built-in filter, phase (scale + taps + 1) / 2. */
+static void scaler_plan(uint32_t width, uint32_t height, uint32_t values[SCALER_REGISTERS])
+{
+    uint32_t h = (uint32_t)(((uint64_t)width << 24) / display.output_width);
+    uint32_t v = (uint32_t)(((uint64_t)height << 24) / display.output_height);
+    values[0] = (h != 1u << 24 || v != 1u << 24);
+    values[1] = 0x101;
+    values[2] = (width << 16) | height;
+    values[3] = width / 4 - 1;
+    values[4] = kernel_scaler[4] | 0x100;
+    values[5] = h;
+    values[6] = (3u << 15) + (h >> 9);
+    values[7] = kernel_scaler[7] | 0x100;
+    values[8] = v;
+    values[9] = (3u << 15) + (v >> 9);
+}
+
 static int display_apply(const display_surface *s)
 {
     gpu_write(REG_SURFACE_2840, s->address);
@@ -395,6 +443,26 @@ static int display_show(uint32_t address)
     return display_apply(&s);
 }

+static void show_system_surface(void)
+{
+    static uint32_t planned_for = 0;
+    static uint32_t values[SCALER_REGISTERS];
+    display_surface *sys = &display.pending_surface;
+    uint32_t key = (sys->pitch << 16) | sys->y_end;
+    if (!display.showing_system || planned_for != key) {
+        scaler_plan(sys->x_end, sys->y_end, values);
+        scaler_write(values);
+        planned_for = key;
+        if (display.system_frames < 2)
+            TRACE("guide: XAM surface %08x %ux%u pitch %u", sys->address, sys->x_end, sys->y_end, sys->pitch);
+    }
+    display_surface s = *sys;
+    s.x_end = s.pitch;
+    display_apply(&s);
+    display.showing_system = 1;
+    display.system_frames++;
+}
+
 static void wait_for_flip(void)
 {
     if (!display.pending) return;
@@ -413,6 +481,18 @@ static void wait_for_flip(void)
         }
         low_priority_hint();
     }
+    if (display.pending_system) {
+        /* XAM's commands have drawn the Guide into its surface */
+        show_system_surface();
+        display.pending = 0;
+        return;
+    }
+    if (display.showing_system) {
+        /* the Guide closed: the kernel's scaling for our surface again */
+        scaler_write(kernel_scaler);
+        display.showing_system = 0;
+        TRACE("guide: closed after %u frames", display.system_frames);
+    }
     /* ... then the display controller takes the address ... */
     start = timebase_now();
     while (gpu_read(SCANOUT_ADDRESS) != display.pending_address) {
@@ -442,7 +522,7 @@ static void wait_for_flip(void)
     display.pending = 0;
 }

-static void submit_flip(uint32_t address, const uint32_t fetch_constant[6])
+static void submit_flip(uint32_t address, const uint32_t fetch_constant[6], uint32_t virtual_address)
 {
     VD_SYSTEM_COMMAND_BUFFER system_buffer;
     ULONG identifier = 0;
@@ -460,9 +540,25 @@ static void submit_flip(uint32_t address, const uint32_t fetch_constant[6])
     w[6] = type0(SCANOUT_FORMAT_1930, 1);
     w[7] = 0;

-    ULONG out0, out1, out2, out3, out4;
+    /* VdSwap hands back XAM's surface here while the Guide shows
+     * (xsdk docs/HARDWARE.md, "The Guide shows through VdSwap"). */
+    ULONG shown_address = virtual_address, format = 6, color_space = 0;
+    ULONG shown_width = display.pitch_tiles * TILE_EDGE, shown_height = display.surface_height;
     VdSwap((PULONG)(w + FLIP_DWORDS), (const ULONG *)fetch_constant, (PULONG)identifier_address,
-           &system_buffer, identifier, &out0, &out1, &out2, &out3, &out4);
+           &system_buffer, identifier, &shown_address, &format, &color_space, &shown_width, &shown_height);
+
+    display.pending_system = 0;
+    if (display.guide_supported && system_buffer.Address && system_buffer.Dwords &&
+        shown_address != virtual_address && shown_address && format == 6 && color_space == 0 &&
+        shown_width && shown_height) {
+        /* show XAM's surface, not ours: the scanout writes become NOPs */
+        for (unsigned i = 0; i < FLIP_DWORDS; i++) w[i] = 0x80000000u;
+        display.pending_system = 1;
+        display.pending_surface.address = MmGetPhysicalAddress((PVOID)shown_address);
+        display.pending_surface.pitch = (shown_width + 31) & ~31u;
+        display.pending_surface.x_end = shown_width;
+        display.pending_surface.y_end = shown_height;
+    }

     ring_commit(&display.ring, FLIP_DWORDS + SWAP_DWORDS);
     ring_kick(&display.ring);
@@ -593,6 +689,45 @@ static void load_identity_gamma(void)

 int xex_video_scaler_via_kernel = 0;

+/*
+ * Tells XAM the title's display (graphics notification event 1, 64 bytes,
+ * as D3D does), so it draws the frame under the Guide at the right size
+ * (xsdk src/present.c describe_display).
+ */
+typedef struct {
+    uint16_t front_width, front_height, output_width, output_height;
+    uint32_t left, top, right, bottom;
+    uint32_t scaled_width, scaled_height;
+    uint32_t filters[8];
+} display_description;
+
+static void describe_display(uint32_t width, uint32_t height)
+{
+    uint32_t mode[16];
+    memset(mode, 0, sizeof(mode));
+    VdQueryVideoMode(mode);
+    display.output_width = mode[0];
+    display.output_height = mode[1];
+    /* XAM's surface is scaled by hand; xsdk only tried progressive modes */
+    display.guide_supported = mode[0] && mode[1] && !mode[2];
+    TRACE("guide: output %ux%u interlaced %u -> %s", (unsigned)mode[0], (unsigned)mode[1], (unsigned)mode[2],
+          display.guide_supported ? "supported" : "not supported");
+    if (!display.guide_supported) return;
+    display_description d;
+    memset(&d, 0, sizeof(d));
+    d.front_width = (uint16_t)width;
+    d.front_height = (uint16_t)height;
+    d.output_width = (uint16_t)display.output_width;
+    d.output_height = (uint16_t)display.output_height;
+    d.right = width;
+    d.bottom = height;
+    d.scaled_width = display.output_width;
+    d.scaled_height = display.output_height;
+    VdCallGraphicsNotificationRoutines(1, &d);
+}
+
+int xex_video_guide_visible(void) { return display.showing_system; }
+
 const char *xex_video_error(void)
 {
     return display.error ? display.error : "";
@@ -667,6 +802,8 @@ int xex_video_init(void)
     old_surface.pitch = gpu_read(REG_D1GRPH_PITCH);
     old_surface.x_end = gpu_read(REG_D1GRPH_X_END);
     old_surface.y_end = gpu_read(REG_D1GRPH_Y_END);
+    for (int i = 0; i < SCALER_REGISTERS; i++) kernel_scaler[i] = gpu_read(scaler_offsets[i] / 4);
+    describe_display(sw, sh);
     int shown = display_show(display.buffers[1].memory.physical);
     TRACE("show buffer 1: 60EC %s, scanout %08x", shown ? "taken" : "TIMEOUT", gpu_read(SCANOUT_ADDRESS));
     atexit(xex_video_shutdown);
@@ -692,7 +829,7 @@ void xex_video_present(void)
 {
     if (!display.open) return;
     front_buffer *fb = &display.buffers[display.back];
-    submit_flip(fb->memory.physical, fb->fetch_constant);
+    submit_flip(fb->memory.physical, fb->fetch_constant, (uint32_t)fb->memory.words);
     display.back ^= 1;
     if (display.presented++ == 0) xex_video_dump_registers("after first flip");
 }
@@ -702,7 +839,8 @@ void xex_video_shutdown(void)
     if (!display.open) return;
     wait_for_flip();
     /* leave the scanout at address 0 as the kernel's own ring does */
-    submit_flip(0, display.buffers[display.back ^ 1].fetch_constant);
+    if (display.showing_system) scaler_write(kernel_scaler);
+    submit_flip(0, display.buffers[display.back ^ 1].fetch_constant, 0);
     wait_for_flip();
     display_apply(&old_surface);
     ring_detach(&display.ring);
diff --git a/platform/xex/runtime/xex_video.h b/platform/xex/runtime/xex_video.h
index e19b4cf..790345e 100644
--- a/platform/xex/runtime/xex_video.h
+++ b/platform/xex/runtime/xex_video.h
@@ -48,6 +48,10 @@ void xex_video_dump_lut(void);
  * (hangs on hardware so far). */
 extern int xex_video_scaler_via_kernel;

+/* Nonzero while the Guide (XAM's surface) is on screen. Don't end the
+ * title by itself then: XAM waits for a swap that never comes. */
+int xex_video_guide_visible(void);
+
 /* Gives the GPU back to the system (also done at exit). */
 void xex_video_shutdown(void);

diff --git a/platform/xex/tests/video/main.cpp b/platform/xex/tests/video/main.cpp
index 708f0cd..feaa023 100644
--- a/platform/xex/tests/video/main.cpp
+++ b/platform/xex/tests/video/main.cpp
@@ -1,7 +1,8 @@
 // Runtime check 3: CPU-drawn frames through the title's own GPU ring
 // (xex_video) and controller input (xex_input). Moving gradient, a square
 // moved with the d-pad/left stick, A/B/X/Y change its colour, a bar shows
-// the frame rate. Back+Start returns to the dashboard.
+// the frame rate. The Xbox button opens the Guide (Xbox Home leaves);
+// Back+Start returns to the dashboard.
 #include <cstdio>
 #include <cstdint>
 #include <cstdarg>
@@ -91,7 +92,9 @@ int main(int, char **) {
             logPadChanges(port, xex_input_get(port, &other), other);
         }
         // stop by itself after 20 s, so a black screen still tells whether the loop runs
-        if (xex_timebase() - startTime > 60 * freq) {
+        // never end by ourselves while the Guide shows: XAM would wait for a
+        // swap that never comes
+        if (xex_timebase() - startTime > 60 * freq && !xex_video_guide_visible()) {
             logLine("60 s elapsed, exiting");
             break;
         }