commit c144b67739162cb147619f962c6e04069cf4099f
Author: mrfox <jens.se@icloud.com>
AuthorDate: Tue Nov 24 22:28:37 2026 +0100
Commit: mrfox <jens.se@icloud.com>
CommitDate: Tue Nov 24 22:28:37 2026 +0100
xex runtime: video (own GPU ring + VdSwap flips) and controller input
- runtime/video.c: C port of xenon-forge's xbox360-video: takes the GPU
engines (VdInitializeEngines, kernel microcode), runs a command ring,
flips two tiled 32 bpp front buffers with VdSwap at vertical blank,
EDRAM retraining like D3D, gives the engines back at exit.
Same tiled layout/pixel format as libxenon's framebuffer.
- runtime/input.c: XamInputGetState wrapper.
- tests/video: moving gradient, square on d-pad/stick, colours on
A/B/X/Y, fps bar; Back+Start exits. Runs in Xenia (1280x720).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
platform/xex/Makefile | 3 +-
platform/xex/runtime/input.c | 25 ++
platform/xex/runtime/video.c | 468 ++++++++++++++++++++++++++++++++++++++
platform/xex/runtime/xex_input.h | 41 ++++
platform/xex/runtime/xex_video.h | 59 +++++
platform/xex/tests/video/main.cpp | 77 +++++++
platform/xex/tests/video/xex.toml | 3 +
7 files changed, 675 insertions(+), 1 deletion(-)
diff --git a/platform/xex/Makefile b/platform/xex/Makefile
index 33354c9..e63f5b5 100644
--- a/platform/xex/Makefile
+++ b/platform/xex/Makefile
@@ -25,10 +25,11 @@ LDFLAGS := $(MACH) -nostartfiles -nostdlib -T $(CURDIR)/runtime/link.ld -Wl,--g
LIBS := -L$(DEVKITXENON)/xenon/lib/32 -Wl,--start-group -lstdc++ -lm -lc -lgcc -Wl,--end-group
RUNTIME_SRC := runtime/crt0.S runtime/crt.c runtime/sys.c runtime/fs.c runtime/posix.c \
+ runtime/video.c runtime/input.c \
runtime/extra_imports.S sdk/thunks/ppc32.S
RUNTIME_OBJ := $(patsubst %,$(BUILD)/%.o,$(RUNTIME_SRC))
-TESTS := hello fs
+TESTS := hello fs video
.PHONY: all tests runtime clean
all: tests
diff --git a/platform/xex/runtime/input.c b/platform/xex/runtime/input.c
new file mode 100644
index 0000000..505c46a
--- /dev/null
+++ b/platform/xex/runtime/input.c
@@ -0,0 +1,25 @@
+/* Controllers through XamInputGetState (xam.xex ordinal 401). */
+#include <string.h>
+
+#include <xbox360/base.h>
+#include <xbox360/xam/input.h>
+
+#include "xex_input.h"
+
+int xex_input_get(int port, xex_pad *pad)
+{
+ XINPUT_STATE state;
+ memset(&state, 0, sizeof(state));
+ memset(pad, 0, sizeof(*pad));
+ if (XamInputGetState((DWORD)port, XINPUT_FLAG_GAMEPAD, &state) != 0) {
+ return -1;
+ }
+ pad->buttons = state.Gamepad.wButtons;
+ pad->left_trigger = state.Gamepad.bLeftTrigger;
+ pad->right_trigger = state.Gamepad.bRightTrigger;
+ pad->lx = state.Gamepad.sThumbLX;
+ pad->ly = state.Gamepad.sThumbLY;
+ pad->rx = state.Gamepad.sThumbRX;
+ pad->ry = state.Gamepad.sThumbRY;
+ return 0;
+}
diff --git a/platform/xex/runtime/video.c b/platform/xex/runtime/video.c
new file mode 100644
index 0000000..8cf3635
--- /dev/null
+++ b/platform/xex/runtime/video.c
@@ -0,0 +1,468 @@
+/*
+ * Scanout of CPU-drawn frames: C port of xenon-forge's xbox360-video crate
+ * (src/crates/xbox360-video: lib.rs, ring.rs, memory.rs, fetch.rs;
+ * https://github.com/landaire/xenon-forge, MIT/Apache-2.0).
+ *
+ * The title takes the GPU engines (VdInitializeEngines with the kernel's
+ * microcode), runs a command ring of its own, and flips between two tiled
+ * front buffers with VdSwap at vertical blank. xex_video_shutdown (also run
+ * at exit) gives the engines back so the dashboard can draw again.
+ */
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <xbox360/xboxkrnl.h>
+
+#include "xex_video.h"
+
+/* ------------------------------------------------------------------------ */
+/* GPU registers, memory, processor helpers */
+
+#define GPU_REGISTERS 0x7fc80000u
+
+static inline uint32_t gpu_read(uint32_t index)
+{
+ return *(volatile uint32_t *)(GPU_REGISTERS + index * 4);
+}
+
+static inline void gpu_write(uint32_t index, uint32_t value)
+{
+ *(volatile uint32_t *)(GPU_REGISTERS + index * 4) = value;
+}
+
+static inline void cpu_sync(void) { __asm__ volatile("sync" ::: "memory"); }
+static inline void low_priority_hint(void) { __asm__ volatile("or 31, 31, 31" ::: "memory"); }
+
+#define MEMORY_REGION_TITLE 1
+#define PAGE_READWRITE_ 0x004
+#define PAGE_NOCACHE_ 0x200
+#define PAGE_WRITECOMBINE_ 0x400
+
+typedef struct {
+ uint32_t *words; /* virtual */
+ uint32_t physical; /* GPU address */
+ uint32_t bytes;
+} phys_buffer;
+
+static int phys_alloc(phys_buffer *b, uint32_t bytes, uint32_t caching)
+{
+ bytes = (bytes + 0xfff) & ~0xfffu;
+ void *p = MmAllocatePhysicalMemoryEx(MEMORY_REGION_TITLE, bytes, PAGE_READWRITE_ | caching,
+ 0, 0x1fffffff, 0x1000);
+ if (!p) return -1;
+ b->words = (uint32_t *)p;
+ b->physical = MmGetPhysicalAddress(p);
+ b->bytes = bytes;
+ return 0;
+}
+
+static void phys_free(phys_buffer *b)
+{
+ if (b->words) {
+ MmFreePhysicalMemory(MEMORY_REGION_TITLE, b->words);
+ b->words = NULL;
+ }
+}
+
+static inline uint32_t read_word(const phys_buffer *b, uint32_t offset)
+{
+ return ((volatile uint32_t *)b->words)[offset / 4];
+}
+
+/* ------------------------------------------------------------------------ */
+/* Command ring (ring.rs) */
+
+#define RING_WRITE_POINTER 0x1c5
+#define RING_BYTES 0x8000u
+#define RING_DWORDS (RING_BYTES / 4)
+#define GRANULE_DWORDS 8u
+#define WRITEBACK_BYTES 0x1000u
+#define ENGINE_INIT_MASK 0x3ffu
+#define ENGINE_INIT_STEP 0x80u
+#define ENGINE_INIT_WORD12 0x00010800u
+#define REGISTER_0D02 0x0d02
+#define KERNEL_REGISTER_0D02 0x800u
+#define PIPELINE_CONTROL_05C8 0x05c8
+#define PIPELINE_CONTROL_05C8_VALUE 0x00020000u
+#define ENGINE_INIT_WORD13 7u
+#define WAIT_MEMORY_EQUAL 0x13u
+#define WAIT_INTERVAL 0x100u
+#define EVENT_SHADER_DONE 3u
+#define ENDIAN_8_IN_32 2u
+#define SWAP_IDENTIFIER_OFFSET 0x08u
+#define READ_POINTER_OFFSET 0x3cu
+#define PIPELINE_FENCE_OFFSET 0x40u
+
+#define OPCODE_NOP 0x10
+#define OPCODE_WAIT 0x3c
+#define OPCODE_EVENT_WRITE 0x58
+#define OPCODE_REGISTER_RMW 0x21
+#define OPCODE_ENGINE_INIT 0x48
+
+static inline uint32_t type0(uint32_t reg, uint32_t count)
+{
+ return ((count - 1) << 16) | reg;
+}
+
+static inline uint32_t type3(uint32_t opcode, uint32_t count)
+{
+ return 0xc0000000u | ((count - 1) << 16) | (opcode << 8);
+}
+
+typedef struct {
+ phys_buffer commands;
+ phys_buffer writeback;
+ uint32_t write; /* next dword, from the ring base */
+ uint32_t read; /* last read pointer read back */
+ uint32_t fence;
+} ring_t;
+
+static uint32_t ring_read_pointer(const ring_t *r)
+{
+ return read_word(&r->writeback, READ_POINTER_OFFSET);
+}
+
+static uint32_t ring_free(const ring_t *r)
+{
+ return (r->read - r->write - 1) % RING_DWORDS;
+}
+
+static void ring_kick(ring_t *r)
+{
+ uint32_t next = (r->write + GRANULE_DWORDS - 1) / GRANULE_DWORDS * GRANULE_DWORDS;
+ uint32_t pad = next - r->write;
+ if (pad == 1) pad += GRANULE_DWORDS; /* a NOP is at least two dwords */
+ if (pad != 0) {
+ r->commands.words[r->write] = type3(OPCODE_NOP, pad - 1);
+ }
+ r->write = (r->write + pad) % RING_DWORDS;
+ cpu_sync(); /* write-combined stores reach memory before the GPU looks */
+ gpu_write(RING_WRITE_POINTER, r->write);
+}
+
+static void ring_wait_free(ring_t *r, uint32_t dwords)
+{
+ while (ring_free(r) < dwords) {
+ r->read = ring_read_pointer(r);
+ if (ring_free(r) < dwords) low_priority_hint();
+ }
+}
+
+static uint32_t *ring_reserve(ring_t *r, uint32_t dwords)
+{
+ uint32_t needed = dwords + GRANULE_DWORDS + 1;
+ if (r->write + needed > RING_DWORDS || ring_free(r) < needed) {
+ ring_kick(r);
+ }
+ if (r->write + needed > RING_DWORDS) {
+ uint32_t tail = RING_DWORDS - r->write;
+ ring_wait_free(r, tail);
+ r->commands.words[r->write] = type3(OPCODE_NOP, tail - 1);
+ r->write = 0;
+ ring_kick(r);
+ }
+ ring_wait_free(r, needed);
+ return &r->commands.words[r->write];
+}
+
+static void ring_commit(ring_t *r, uint32_t dwords)
+{
+ r->write += dwords;
+}
+
+static void ring_wait_for_pipeline(ring_t *r)
+{
+ r->fence++;
+ uint32_t address = r->writeback.physical + PIPELINE_FENCE_OFFSET;
+ uint32_t *w = ring_reserve(r, 10);
+ w[0] = type3(OPCODE_EVENT_WRITE, 3);
+ w[1] = EVENT_SHADER_DONE;
+ w[2] = address | ENDIAN_8_IN_32;
+ w[3] = r->fence;
+ w[4] = type3(OPCODE_WAIT, 5);
+ w[5] = WAIT_MEMORY_EQUAL;
+ w[6] = address | ENDIAN_8_IN_32;
+ w[7] = r->fence;
+ w[8] = 0xffffffffu;
+ w[9] = WAIT_INTERVAL;
+ ring_commit(r, 10);
+}
+
+static int ring_pipeline_drained(const ring_t *r)
+{
+ return read_word(&r->writeback, PIPELINE_FENCE_OFFSET) == r->fence;
+}
+
+static void ring_block_until_idle(ring_t *r)
+{
+ uint32_t *w = ring_reserve(r, 2);
+ w[0] = type0(PIPELINE_CONTROL_05C8, 1);
+ w[1] = PIPELINE_CONTROL_05C8_VALUE;
+ ring_commit(r, 2);
+ ring_wait_for_pipeline(r);
+ ring_kick(r);
+ while (!ring_pipeline_drained(r)) low_priority_hint();
+}
+
+static void ring_wait_idle(const ring_t *r)
+{
+ while (ring_read_pointer(r) != r->write) low_priority_hint();
+}
+
+static int ring_init(ring_t *r)
+{
+ memset(r, 0, sizeof(*r));
+ if (phys_alloc(&r->commands, RING_BYTES, PAGE_WRITECOMBINE_) != 0) return -1;
+ if (phys_alloc(&r->writeback, WRITEBACK_BYTES, PAGE_NOCACHE_) != 0) {
+ phys_free(&r->commands);
+ return -1;
+ }
+ memset(r->writeback.words, 0, WRITEBACK_BYTES);
+
+ /* log2(dwords) - 1 and log2(bytes / 512), as D3D derives them */
+ VdInitializeRingBuffer(r->commands.physical, 12 /* log2(0x2000) - 1 */);
+ VdEnableRingBufferRPtrWriteBack(r->writeback.physical + READ_POINTER_OFFSET, 6 /* log2(0x8000 >> 9) */);
+ VdSetSystemCommandBufferGpuIdentifierAddress((PULONG)((uint32_t)r->writeback.words + SWAP_IDENTIFIER_OFFSET));
+
+ uint32_t *w = ring_reserve(r, 21);
+ w[0] = type3(OPCODE_ENGINE_INIT, 18);
+ w[1] = ENGINE_INIT_MASK;
+ w[2] = 0;
+ w[3] = 0;
+ for (int i = 0; i < 8; i++) w[4 + i] = (uint32_t)i * ENGINE_INIT_STEP;
+ w[12] = ENGINE_INIT_WORD12;
+ w[13] = ENGINE_INIT_WORD13;
+ for (int i = 14; i < 19; i++) w[i] = 0;
+ w[19] = type0(REGISTER_0D02, 1);
+ w[20] = ENGINE_INIT_WORD12;
+ ring_commit(r, 21);
+ ring_kick(r);
+ return 0;
+}
+
+static void ring_detach(ring_t *r)
+{
+ ring_block_until_idle(r);
+ ring_wait_idle(r);
+ gpu_write(REGISTER_0D02, KERNEL_REGISTER_0D02);
+ VdSetSystemCommandBufferGpuIdentifierAddress(NULL);
+}
+
+/* ------------------------------------------------------------------------ */
+/* Display (lib.rs) */
+
+#define TILE_EDGE 32
+#define SCANOUT_CONTROL 0x1841
+#define SCANOUT_ADDRESS 0x1844
+#define SCANOUT_UPDATE 0x1851
+#define UPDATE_PENDING (1u << 0)
+#define UPDATE_TAKEN (1u << 1)
+#define SCANOUT_FORMAT_1930 0x1930
+#define SCANOUT_CONTROL_KEEP 0xfffff8ffu
+#define SWAP_DWORDS 64u
+#define FLIP_DWORDS (2u + 4u + 2u)
+#define RETRAIN_COMMAND_DWORDS 0x1000u
+#define RETRAIN_WORK_BYTES 0x800u
+
+typedef struct {
+ phys_buffer memory;
+ uint32_t fetch_constant[6];
+} front_buffer;
+
+static struct {
+ int open;
+ ring_t ring;
+ front_buffer buffers[2];
+ int back;
+ uint32_t pitch_tiles;
+ uint32_t tile_x, tile_y, width, height;
+ int pending;
+ uint32_t pending_identifier;
+ uint32_t pending_address;
+ const char *error;
+} display;
+
+/* fetch.rs: texture fetch constant of a tiled 8:8:8:8 front buffer */
+static void front_buffer_fetch(uint32_t out[6], uint32_t base, uint32_t pitch_tiles,
+ uint32_t width, uint32_t height)
+{
+ out[0] = (1u << 31) | (pitch_tiles << 22) | 2u; /* tiled, pitch, texture */
+ out[1] = base | (2u << 6) | 6u; /* 8-in-32, k_8_8_8_8 */
+ out[2] = (width - 1) | ((height - 1) << 13);
+ out[3] = 0;
+ out[4] = 0;
+ out[5] = 1u << 9; /* 2D */
+}
+
+static int retraining_after_swap_due;
+
+static void retrain_edram(int at_init)
+{
+ static uint8_t work[RETRAIN_WORK_BYTES] __attribute__((aligned(16)));
+ ULONG flags = at_init ? 1 : 0;
+
+ memset(work, 0, sizeof(work));
+ KeEnterCriticalRegion();
+ if (at_init) {
+ VdRetrainEDRAMWorker(0);
+ }
+ ULONG state = 0;
+ PULONG commands = NULL;
+ ULONG due = VdRetrainEDRAM(flags, &commands, RETRAIN_COMMAND_DWORDS, &state, work, RETRAIN_WORK_BYTES);
+ if (due) {
+ if (!at_init) {
+ ring_block_until_idle(&display.ring);
+ }
+ for (;;) {
+ uint32_t *start = ring_reserve(&display.ring, RETRAIN_COMMAND_DWORDS);
+ PULONG cursor = (PULONG)start;
+ ULONG more = VdRetrainEDRAM(flags, &cursor, RETRAIN_COMMAND_DWORDS, &state, work, RETRAIN_WORK_BYTES);
+ ring_commit(&display.ring, (uint32_t)((uint32_t *)cursor - start));
+ ring_block_until_idle(&display.ring);
+ if (!more) break;
+ }
+ retraining_after_swap_due = 1;
+ }
+ KeLeaveCriticalRegion();
+}
+
+static void wait_for_flip(void)
+{
+ if (!display.pending) return;
+ /* the swap identifier lands first ... */
+ for (;;) {
+ uint32_t completed = read_word(&display.ring.writeback, SWAP_IDENTIFIER_OFFSET);
+ if ((int32_t)(completed - display.pending_identifier) >= 0) break;
+ low_priority_hint();
+ }
+ /* ... then the display controller takes the address ... */
+ while (gpu_read(SCANOUT_ADDRESS) != display.pending_address) low_priority_hint();
+ /* ... and latches it at the next blank: one flip per refresh */
+ while (gpu_read(SCANOUT_UPDATE) & (UPDATE_PENDING | UPDATE_TAKEN)) low_priority_hint();
+ display.pending = 0;
+}
+
+static void submit_flip(uint32_t address, const uint32_t fetch_constant[6])
+{
+ VD_SYSTEM_COMMAND_BUFFER system_buffer;
+ ULONG identifier = 0;
+ memset(&system_buffer, 0, sizeof(system_buffer));
+ VdGetSystemCommandBuffer(&system_buffer, &identifier);
+
+ uint32_t identifier_address = (uint32_t)display.ring.writeback.words + SWAP_IDENTIFIER_OFFSET;
+ uint32_t *w = ring_reserve(&display.ring, FLIP_DWORDS + SWAP_DWORDS);
+ w[0] = type0(SCANOUT_ADDRESS, 1);
+ w[1] = address;
+ w[2] = type3(OPCODE_REGISTER_RMW, 3);
+ w[3] = SCANOUT_CONTROL;
+ w[4] = SCANOUT_CONTROL_KEEP;
+ w[5] = 0;
+ w[6] = type0(SCANOUT_FORMAT_1930, 1);
+ w[7] = 0;
+
+ ULONG out0, out1, out2, out3, out4;
+ VdSwap((PULONG)(w + FLIP_DWORDS), (const ULONG *)fetch_constant, (PULONG)identifier_address,
+ &system_buffer, identifier, &out0, &out1, &out2, &out3, &out4);
+
+ ring_commit(&display.ring, FLIP_DWORDS + SWAP_DWORDS);
+ ring_kick(&display.ring);
+ retrain_edram(0);
+
+ display.pending = 1;
+ display.pending_identifier = identifier;
+ display.pending_address = address;
+}
+
+const char *xex_video_error(void)
+{
+ return display.error ? display.error : "";
+}
+
+int xex_video_init(void)
+{
+ if (display.open) return 0;
+
+ VD_DISPLAY_INFORMATION info;
+ memset(&info, 0, sizeof(info));
+ VdGetCurrentDisplayInformation(&info);
+ uint32_t sw = info.SurfaceWidth, sh = info.SurfaceHeight;
+ if (sw == 0 || sh == 0 || sw % TILE_EDGE != 0) {
+ display.error = "unsupported display mode";
+ return -1;
+ }
+ if (info.Left % TILE_EDGE || info.Top % TILE_EDGE || info.Right <= info.Left ||
+ info.Bottom <= info.Top || info.Right > sw || info.Bottom > sh) {
+ display.error = "unsupported display window";
+ return -1;
+ }
+ display.pitch_tiles = sw / TILE_EDGE;
+ display.tile_x = info.Left / TILE_EDGE;
+ display.tile_y = info.Top / TILE_EDGE;
+ display.width = info.Right - info.Left;
+ display.height = info.Bottom - info.Top;
+
+ uint32_t tiles_high = (sh + TILE_EDGE - 1) / TILE_EDGE;
+ uint32_t bytes = display.pitch_tiles * tiles_high * TILE_EDGE * TILE_EDGE * 4;
+ for (int i = 0; i < 2; i++) {
+ front_buffer *fb = &display.buffers[i];
+ if (phys_alloc(&fb->memory, bytes, PAGE_WRITECOMBINE_) != 0) {
+ display.error = "out of memory (front buffer)";
+ return -1;
+ }
+ memset(fb->memory.words, 0, fb->memory.bytes);
+ front_buffer_fetch(fb->fetch_constant, (uint32_t)fb->memory.words, display.pitch_tiles, sw, sh);
+ }
+
+ /* Null microcode selects the kernel's own. */
+ VdInitializeEngines(0, NULL, NULL, NULL, NULL);
+ if (ring_init(&display.ring) != 0) {
+ VdShutdownEngines();
+ display.error = "out of memory (ring)";
+ return -1;
+ }
+ display.open = 1;
+ display.back = 0;
+ display.pending = 0;
+ retrain_edram(1);
+ atexit(xex_video_shutdown);
+ return 0;
+}
+
+xex_frame xex_video_begin(void)
+{
+ xex_frame f;
+ memset(&f, 0, sizeof(f));
+ if (!display.open) return f;
+ wait_for_flip();
+ f.pixels = display.buffers[display.back].memory.words;
+ f.pitch = (int)(display.pitch_tiles * TILE_EDGE);
+ f.window_x = (int)(display.tile_x * TILE_EDGE);
+ f.window_y = (int)(display.tile_y * TILE_EDGE);
+ f.window_width = (int)display.width;
+ f.window_height = (int)display.height;
+ return f;
+}
+
+void xex_video_present(void)
+{
+ if (!display.open) return;
+ front_buffer *fb = &display.buffers[display.back];
+ submit_flip(fb->memory.physical, fb->fetch_constant);
+ display.back ^= 1;
+}
+
+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);
+ wait_for_flip();
+ ring_detach(&display.ring);
+ VdShutdownEngines();
+ display.open = 0;
+ for (int i = 0; i < 2; i++) phys_free(&display.buffers[i].memory);
+ phys_free(&display.ring.commands);
+ phys_free(&display.ring.writeback);
+}
diff --git a/platform/xex/runtime/xex_input.h b/platform/xex/runtime/xex_input.h
new file mode 100644
index 0000000..511e19d
--- /dev/null
+++ b/platform/xex/runtime/xex_input.h
@@ -0,0 +1,41 @@
+/* Controllers through XamInputGetState. */
+#ifndef XEX_INPUT_H
+#define XEX_INPUT_H
+
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* XINPUT_GAMEPAD_* button bits */
+#define XEX_PAD_UP 0x0001
+#define XEX_PAD_DOWN 0x0002
+#define XEX_PAD_LEFT 0x0004
+#define XEX_PAD_RIGHT 0x0008
+#define XEX_PAD_START 0x0010
+#define XEX_PAD_BACK 0x0020
+#define XEX_PAD_LTHUMB 0x0040
+#define XEX_PAD_RTHUMB 0x0080
+#define XEX_PAD_LB 0x0100
+#define XEX_PAD_RB 0x0200
+#define XEX_PAD_A 0x1000
+#define XEX_PAD_B 0x2000
+#define XEX_PAD_X 0x4000
+#define XEX_PAD_Y 0x8000
+
+typedef struct {
+ uint16_t buttons;
+ uint8_t left_trigger, right_trigger;
+ int16_t lx, ly, rx, ry;
+} xex_pad;
+
+/* Reads controller `port` (0-3). Returns 0 when connected; otherwise the
+ * state is zeroed and -1 is returned. */
+int xex_input_get(int port, xex_pad *pad);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/platform/xex/runtime/xex_video.h b/platform/xex/runtime/xex_video.h
new file mode 100644
index 0000000..ad54cd8
--- /dev/null
+++ b/platform/xex/runtime/xex_video.h
@@ -0,0 +1,59 @@
+/*
+ * Scanout of CPU-drawn frames for a title: the title takes the GPU engines
+ * like Direct3D does and flips between two tiled 32 bpp front buffers at
+ * vertical blank. C port of xenon-forge's xbox360-video crate
+ * (https://github.com/landaire/xenon-forge, MIT/Apache-2.0).
+ */
+#ifndef XEX_VIDEO_H
+#define XEX_VIDEO_H
+
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct {
+ /* Back buffer: tiled surface, 32x32-pixel tiles of 4 KB (see
+ * xex_video_tiled_index). Write-combined: write in address order. */
+ uint32_t *pixels;
+ /* Surface width in pixels (multiple of 32), for tiled indexing. */
+ int pitch;
+ /* Displayed window inside the surface (tile aligned). */
+ int window_x, window_y, window_width, window_height;
+} xex_frame;
+
+/* Takes the GPU and creates two front buffers of the current display
+ * size. Returns 0 on success, -1 on failure (see xex_video_error). */
+int xex_video_init(void);
+const char *xex_video_error(void);
+
+/* Waits until the previous flip has been latched (so the back buffer is
+ * off screen) and returns the back buffer. */
+xex_frame xex_video_begin(void);
+
+/* Flips to the back buffer at the next vertical blank. */
+void xex_video_present(void);
+
+/* Gives the GPU back to the system (also done at exit). */
+void xex_video_shutdown(void);
+
+/* Word index of pixel (x, y) in a tiled surface of `pitch` pixels. The
+ * pixel word is B << 24 | G << 16 | R << 8. */
+static inline int xex_video_tiled_index(int x, int y, int pitch)
+{
+ return (((y >> 5) * 32 * pitch + ((x >> 5) << 10)
+ + (x & 3) + ((y & 1) << 2) + (((x & 31) >> 2) << 3) + (((y & 31) >> 1) << 6))
+ ^ ((y & 8) << 2));
+}
+
+static inline uint32_t xex_video_rgb(uint8_t r, uint8_t g, uint8_t b)
+{
+ return ((uint32_t)b << 24) | ((uint32_t)g << 16) | ((uint32_t)r << 8);
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/platform/xex/tests/video/main.cpp b/platform/xex/tests/video/main.cpp
new file mode 100644
index 0000000..62954ea
--- /dev/null
+++ b/platform/xex/tests/video/main.cpp
@@ -0,0 +1,77 @@
+// 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.
+#include <cstdio>
+#include <cstdint>
+
+#include "xex_runtime.h"
+#include "xex_video.h"
+#include "xex_input.h"
+
+int main(int, char **) {
+ if (xex_video_init() != 0) {
+ printf("video: init failed: %s\n", xex_video_error());
+ xex_notify("video init failed");
+ xex_sleep_ms(4000);
+ return 1;
+ }
+
+ int sx = 200, sy = 200;
+ uint32_t colour = xex_video_rgb(255, 0, 77);
+ uint64_t freq = xex_timebase_freq();
+ uint64_t last = xex_timebase(), second = last;
+ int frames = 0, fps = 0;
+ unsigned t = 0;
+
+ for (;;) {
+ xex_pad pad;
+ xex_input_get(0, &pad);
+ if ((pad.buttons & XEX_PAD_BACK) && (pad.buttons & XEX_PAD_START)) break;
+
+ int speed = 6;
+ if ((pad.buttons & XEX_PAD_LEFT) || pad.lx < -12000) sx -= speed;
+ if ((pad.buttons & XEX_PAD_RIGHT) || pad.lx > 12000) sx += speed;
+ if ((pad.buttons & XEX_PAD_UP) || pad.ly > 12000) sy -= speed;
+ if ((pad.buttons & XEX_PAD_DOWN) || pad.ly < -12000) sy += speed;
+ if (pad.buttons & XEX_PAD_A) colour = xex_video_rgb(0, 228, 54);
+ if (pad.buttons & XEX_PAD_B) colour = xex_video_rgb(255, 0, 77);
+ if (pad.buttons & XEX_PAD_X) colour = xex_video_rgb(41, 173, 255);
+ if (pad.buttons & XEX_PAD_Y) colour = xex_video_rgb(255, 236, 39);
+
+ xex_frame f = xex_video_begin();
+ if (sx < 0) sx = 0;
+ if (sy < 0) sy = 0;
+ if (sx > f.window_width - 128) sx = f.window_width - 128;
+ if (sy > f.window_height - 128) sy = f.window_height - 128;
+
+ for (int y = 0; y < f.window_height; y++) {
+ int py = f.window_y + y;
+ for (int x = 0; x < f.window_width; x++) {
+ uint32_t c;
+ if (x >= sx && x < sx + 128 && y >= sy && y < sy + 128) {
+ c = colour;
+ } else if (y < 24 && x < fps * 8) {
+ c = xex_video_rgb(255, 255, 255);
+ } else {
+ c = xex_video_rgb((uint8_t)(x + t), (uint8_t)(y + t / 2), 96);
+ }
+ f.pixels[xex_video_tiled_index(f.window_x + x, py, f.pitch)] = c;
+ }
+ }
+ xex_video_present();
+ t += 2;
+
+ frames++;
+ uint64_t now = xex_timebase();
+ if (now - second >= freq) {
+ fps = frames;
+ printf("video: %d fps, %dx%d window at %d,%d\n", fps, f.window_width, f.window_height, f.window_x, f.window_y);
+ frames = 0;
+ second = now;
+ }
+ last = now;
+ }
+ (void)last;
+ return 0;
+}
diff --git a/platform/xex/tests/video/xex.toml b/platform/xex/tests/video/xex.toml
new file mode 100644
index 0000000..0854722
--- /dev/null
+++ b/platform/xex/tests/video/xex.toml
@@ -0,0 +1,3 @@
+title_id = 0x46383038 # "F808"
+load_address = 0x82000000
+module_flags = ["TITLE"]