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

    xex video: show the surface the way XAM does (0x2840/0x2844 + 0x60EC)

    xsdk (https://github.com/jessenic/xsdk, MIT) found on hardware that a
    title's buffer only shows when the surface goes into 0x2840/0x2844 and
    D1GRPH and 0x60EC (D1CRTC_DOUBLE_BUFFER_CONTROL) is set to 1, which the
    display clears, with the LUT filled first. The display now loads the LUT
    and shows a black buffer at init, commits every flip once the ring's
    scanout write has latched, and puts the kernel's surface back at shutdown.
    VdSetDisplayMode/VdTurnDisplayOn are no longer called.

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
 platform/xex/runtime/video.c      | 70 +++++++++++++++++++++++++++++++++------
 platform/xex/tests/video/main.cpp | 10 ------
 2 files changed, 60 insertions(+), 20 deletions(-)

diff --git a/platform/xex/runtime/video.c b/platform/xex/runtime/video.c
index c6f9d35..4d422a4 100644
--- a/platform/xex/runtime/video.c
+++ b/platform/xex/runtime/video.c
@@ -306,6 +306,7 @@ static struct {
     uint32_t pending_address;
     const char *error;
     int presented;
+    uint32_t surface_height;
 } display;

 /* fetch.rs: texture fetch constant of a tiled 8:8:8:8 front buffer */
@@ -355,6 +356,48 @@ static void retrain_edram(int at_init)

 static unsigned flip_count;

+/*
+ * Showing a surface the way XAM does (found on hardware by xsdk,
+ * https://github.com/jessenic/xsdk src/display.c, MIT): the surface goes
+ * into 0x2840/0x2844 and the D1GRPH registers, then 0x60EC
+ * (D1CRTC_DOUBLE_BUFFER_CONTROL) = 1 makes the display take it; the
+ * display clears it again. Without this the screen stays black.
+ */
+#define REG_SURFACE_2840        (0x2840 / 4)
+#define REG_PITCH_2844          (0x2844 / 4)
+#define REG_D1GRPH_PITCH        (0x6120 / 4)
+#define REG_D1GRPH_X_END        (0x6134 / 4)
+#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;
+static display_surface old_surface;
+
+static int display_apply(const display_surface *s)
+{
+    gpu_write(REG_SURFACE_2840, s->address);
+    gpu_write(REG_PITCH_2844, s->pitch);
+    gpu_write(SCANOUT_ADDRESS, s->address);
+    gpu_write(REG_D1GRPH_PITCH, s->pitch);
+    gpu_write(REG_D1GRPH_X_END, s->x_end);
+    gpu_write(REG_D1GRPH_Y_END, s->y_end);
+    gpu_write(REG_DOUBLE_BUFFER_CTL, 1);
+    uint64_t start = timebase_now();
+    int ok = 1;
+    while (gpu_read(REG_DOUBLE_BUFFER_CTL)) {
+        if (timebase_now() - start > 49875) { ok = 0; break; }   /* 1 ms; the display clears it at once */
+    }
+    gpu_write(REG_DOUBLE_BUFFER_CTL, 0);
+    return ok;
+}
+
+static int display_show(uint32_t address)
+{
+    display_surface s = { address, display.pitch_tiles * TILE_EDGE, display.pitch_tiles * TILE_EDGE,
+                          display.surface_height };
+    return display_apply(&s);
+}
+
 static void wait_for_flip(void)
 {
     if (!display.pending) return;
@@ -393,9 +436,11 @@ static void wait_for_flip(void)
         }
         low_priority_hint();
     }
+    int shown = display_show(display.pending_address);
     if (verbose) {
-        TRACE("flip %u: done, scanout %08x control %08x update %08x",
-              flip_count, gpu_read(SCANOUT_ADDRESS), gpu_read(SCANOUT_CONTROL), gpu_read(SCANOUT_UPDATE));
+        TRACE("flip %u: done, scanout %08x control %08x update %08x, 60EC %s",
+              flip_count, gpu_read(SCANOUT_ADDRESS), gpu_read(SCANOUT_CONTROL), gpu_read(SCANOUT_UPDATE),
+              shown ? "taken" : "TIMEOUT");
     }
     display.pending = 0;
 }
@@ -621,6 +666,17 @@ int xex_video_init(void)
      * arguments, so it is off unless a test turns it on. */
     if (xex_video_scaler_via_kernel) setup_scaler(sw, sh);
     display.presented = 0;
+    display.surface_height = sh;
+
+    /* The display's LUT is zero while a title runs: load a ramp, then show
+     * the (black) second buffer through the same path XAM uses. */
+    load_identity_gamma();
+    old_surface.address = gpu_read(SCANOUT_ADDRESS);
+    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);
+    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);
     return 0;
 }
@@ -646,14 +702,7 @@ void xex_video_present(void)
     front_buffer *fb = &display.buffers[display.back];
     submit_flip(fb->memory.physical, fb->fetch_constant);
     display.back ^= 1;
-    if (display.presented++ == 0) {
-        /* Direct3D sets the display mode once its first frame is queued
-         * (0x40000000: the bit it always sets, RGB, 8:8:8:8). */
-        xex_video_dump_registers("after first flip");
-        VdSetDisplayMode(0x40000000);
-        TRACE("VdSetDisplayMode(0x40000000) done");
-        load_identity_gamma();
-    }
+    if (display.presented++ == 0) xex_video_dump_registers("after first flip");
 }

 void xex_video_shutdown(void)
@@ -663,6 +712,7 @@ void xex_video_shutdown(void)
     /* leave the scanout at address 0 as the kernel's own ring does */
     submit_flip(0, display.buffers[display.back ^ 1].fetch_constant);
     wait_for_flip();
+    display_apply(&old_surface);
     ring_detach(&display.ring);
     VdShutdownEngines();
     display.open = 0;
diff --git a/platform/xex/tests/video/main.cpp b/platform/xex/tests/video/main.cpp
index 66f6512..a4ca595 100644
--- a/platform/xex/tests/video/main.cpp
+++ b/platform/xex/tests/video/main.cpp
@@ -102,16 +102,6 @@ int main(int, char **) {
                 xex_video_dump_registers("running");
                 xex_video_dump_lut();
             }
-            // Phases to find what keeps the picture black (the display
-            // registers already point at our buffer):
-            // Phases to find what keeps the picture black (display registers
-            // point at our buffer, the LUT is loaded after the first flip):
-            if (seconds == 6) {
-                logLine("PHASE 2 (6 s): VdTurnDisplayOn(0)");
-                unsigned r = xex_video_turn_display_on();
-                logLine("VdTurnDisplayOn -> %08x", r);
-                xex_video_dump_registers("phase 2");
-            }
             frames = 0;
             second = now;
         }