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

    xex: audio latency test; count driver callbacks

    tests/audio clicks and flashes together every second (and on A) with 2,
    4, 8 or 15 frames queued ahead, to tell the title's queue apart from the
    driver/output in the ~0.5 s sound delay seen in FAKE-08.

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
 platform/xex/Makefile             |   2 +-
 platform/xex/README.md            |   1 +
 platform/xex/runtime/audio.c      |   7 +++
 platform/xex/runtime/xex_audio.h  |   3 +
 platform/xex/tests/audio/main.cpp | 118 ++++++++++++++++++++++++++++++++++++++
 platform/xex/tests/audio/xex.toml |   3 +
 6 files changed, 133 insertions(+), 1 deletion(-)

diff --git a/platform/xex/Makefile b/platform/xex/Makefile
index a2b20d1..1f95bf1 100644
--- a/platform/xex/Makefile
+++ b/platform/xex/Makefile
@@ -29,7 +29,7 @@ RUNTIME_SRC := runtime/crt0.S runtime/crt.c runtime/sys.c runtime/fs.c runtime/p
                runtime/extra_imports.S sdk/thunks/ppc32.S
 RUNTIME_OBJ := $(patsubst %,$(BUILD)/%.o,$(RUNTIME_SRC))

-TESTS := hello fs video fault
+TESTS := hello fs video fault audio

 #---------------------------------------------------------------------------------
 # FAKE-08: the emulator core (../../source, ../../libs) and source/XexHost.cpp
diff --git a/platform/xex/README.md b/platform/xex/README.md
index 018e145..e560f7f 100644
--- a/platform/xex/README.md
+++ b/platform/xex/README.md
@@ -119,6 +119,7 @@ The source is `source/XexHost.cpp`, the same logic as the libxenon host
 | Test | Checks |
 |---|---|
 | `tests/hello` | static constructors, C++ exceptions, `std::regex`, an 896 KB deep stack (progress in `game:/stack-test.txt`), notifications, exit to dashboard |
+| `tests/audio` | latency: a click and a white flash together every second (A: at once); up/down set the frames queued ahead (2, 4, 8, 15); logs the driver's callback rate to `game:/audio-log.txt` |
 | `tests/fault` | stores to address 0 after 2 s: the fault report must land in `game:/crash.txt` and the console go back to the 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 (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/audio.c b/platform/xex/runtime/audio.c
index a486f64..d97c806 100644
--- a/platform/xex/runtime/audio.c
+++ b/platform/xex/runtime/audio.c
@@ -21,6 +21,7 @@ static PVOID volatile driver;      /* 0 while closed */
 static volatile uint32_t queued;   /* written by the title */
 static volatile uint32_t taken;    /* written by the callback */
 static volatile uint32_t silent;
+static volatile uint32_t calls;     /* callbacks so far */
 static XAUDIORENDERDRVCLIENT client;

 /* Kernel thread, once per frame. Called through xex_audio_callback_entry
@@ -28,6 +29,7 @@ static XAUDIORENDERDRVCLIENT client;
 void xex_audio_callback(PXAUDIORENDERDRVCALLBACKDATA data)
 {
     (void)data;
+    calls = calls + 1;
     PVOID d = driver;
     if (!d) return;
     uint32_t t = taken;
@@ -85,6 +87,11 @@ void xex_audio_queue(void)
     queued = queued + 1;
 }

+unsigned xex_audio_callbacks(void)
+{
+    return calls;
+}
+
 unsigned xex_audio_underruns(void)
 {
     return silent;
diff --git a/platform/xex/runtime/xex_audio.h b/platform/xex/runtime/xex_audio.h
index 7f9b508..cc02a2b 100644
--- a/platform/xex/runtime/xex_audio.h
+++ b/platform/xex/runtime/xex_audio.h
@@ -34,6 +34,9 @@ unsigned xex_audio_queued(void);
 float *xex_audio_next(void);
 void xex_audio_queue(void);

+/* Callbacks from the driver so far (one per 5.33 ms frame). */
+unsigned xex_audio_callbacks(void);
+
 /* Frames the driver played silence for, because the ring was empty. */
 unsigned xex_audio_underruns(void);

diff --git a/platform/xex/tests/audio/main.cpp b/platform/xex/tests/audio/main.cpp
new file mode 100644
index 0000000..27b25fd
--- /dev/null
+++ b/platform/xex/tests/audio/main.cpp
@@ -0,0 +1,118 @@
+// Runtime check 5: audio latency. Every second a click and a white flash
+// start in the same frame; A clicks and flashes at once. Up/down choose how
+// many 5.33 ms frames are kept queued ahead (2, 4, 8, 15): if the click
+// trails the flash by the same amount at 2 as at 15, the delay is past the
+// title (the kernel's driver or the output). Logs to game:/audio-log.txt.
+// Back+Start or the Guide's Xbox Home leaves.
+#include <cstdio>
+#include <cstdarg>
+#include <cstring>
+#include <cmath>
+
+#include "xex_runtime.h"
+#include "xex_video.h"
+#include "xex_input.h"
+#include "xex_audio.h"
+
+static void logLine(const char *fmt, ...) {
+    char line[256];
+    va_list ap;
+    va_start(ap, fmt);
+    vsnprintf(line, sizeof line, fmt, ap);
+    va_end(ap);
+    printf("audio: %s\n", line);
+    if (FILE *f = fopen("game:/audio-log.txt", "a")) {
+        fprintf(f, "%s\n", line);
+        fclose(f);
+    }
+}
+
+static const unsigned AheadChoices[4] = {2, 4, 8, 15};
+
+int main(int, char **) {
+    remove("game:/audio-log.txt");
+    xex_video_trace = logLine;
+    if (xex_video_init() != 0) {
+        logLine("video init failed: %s", xex_video_error());
+        return 1;
+    }
+    xex_video_trace = nullptr;
+    if (xex_audio_open() != 0) {
+        logLine("audio open failed");
+        xex_notify("audio test: XAudio registration failed");
+        xex_sleep_ms(3000);
+        return 1;
+    }
+    logLine("audio open");
+
+    const uint64_t freq = xex_timebase_freq();
+    uint64_t start = xex_timebase(), lastLog = start;
+    unsigned lastCalls = xex_audio_callbacks();
+    int aheadChoice = 1;
+    uint16_t lastButtons = 0;
+    int clickSamplesLeft = 0;     // samples of the current click still to queue
+    double phase = 0;
+    int flashFrames = 0;
+    uint64_t nextBeat = start + freq;
+
+    for (;;) {
+        xex_pad pad;
+        xex_input_get(0, &pad);
+        uint16_t pressed = pad.buttons & ~lastButtons;
+        lastButtons = pad.buttons;
+        if ((pad.buttons & XEX_PAD_BACK) && (pad.buttons & XEX_PAD_START)) break;
+        if (pressed & XEX_PAD_UP && aheadChoice < 3) aheadChoice++;
+        if (pressed & XEX_PAD_DOWN && aheadChoice > 0) aheadChoice--;
+        if (pressed & (XEX_PAD_UP | XEX_PAD_DOWN)) logLine("frames ahead: %u", AheadChoices[aheadChoice]);
+
+        uint64_t now = xex_timebase();
+        bool click = (pressed & XEX_PAD_A) != 0;
+        if (now >= nextBeat) { click = true; nextBeat += freq; }
+        if (click) { clickSamplesLeft = 2400; flashFrames = 3; }   // 50 ms tone
+
+        // keep the queue at the chosen depth; the click starts in the next
+        // frame queued, the same video frame as the flash
+        unsigned ahead = AheadChoices[aheadChoice];
+        while (xex_audio_queued() < ahead) {
+            float *frame = xex_audio_next();
+            if (!frame) break;
+            for (int i = 0; i < XEX_AUDIO_FRAME_SAMPLES; i++) {
+                float v = 0;
+                if (clickSamplesLeft > 0) {
+                    v = 0.5f * (float)sin(phase);
+                    phase += 2 * M_PI * 1000.0 / XEX_AUDIO_RATE;
+                    clickSamplesLeft--;
+                }
+                frame[i] = v;
+                frame[XEX_AUDIO_FRAME_SAMPLES + i] = v;
+            }
+            xex_audio_queue();
+        }
+
+        xex_frame f = xex_video_begin();
+        uint32_t bg = flashFrames > 0 ? xex_video_rgb(255, 255, 255) : xex_video_rgb(20, 20, 40);
+        if (flashFrames > 0) flashFrames--;
+        for (int y = 0; y < f.window_height; y++)
+            for (int x = 0; x < f.window_width; x++) {
+                uint32_t c = bg;
+                // bars: frames ahead chosen (yellow), queued now (green)
+                if (y >= 40 && y < 80 && x >= 40 && x < 40 + (int)ahead * 40 && (x - 40) % 40 < 34)
+                    c = xex_video_rgb(255, 236, 39);
+                if (y >= 100 && y < 140 && x >= 40 && x < 40 + (int)xex_audio_queued() * 40 && (x - 40) % 40 < 34)
+                    c = xex_video_rgb(0, 228, 54);
+                f.pixels[xex_video_tiled_index(f.window_x + x, f.window_y + y, f.pitch)] = c;
+            }
+        xex_video_present();
+
+        if (now - lastLog >= 2 * freq) {
+            unsigned calls = xex_audio_callbacks();
+            logLine("%.1f s: callbacks %u/s, queued %u (ahead %u), underruns %u",
+                    (double)(now - start) / freq, (unsigned)((calls - lastCalls) * freq / (now - lastLog)),
+                    xex_audio_queued(), ahead, xex_audio_underruns());
+            lastCalls = calls;
+            lastLog = now;
+        }
+    }
+    logLine("exit");
+    return 0;
+}
diff --git a/platform/xex/tests/audio/xex.toml b/platform/xex/tests/audio/xex.toml
new file mode 100644
index 0000000..0854722
--- /dev/null
+++ b/platform/xex/tests/audio/xex.toml
@@ -0,0 +1,3 @@
+title_id = 0x46383038   # "F808"
+load_address = 0x82000000
+module_flags = ["TITLE"]