# xexkit

Build native Xbox 360 `.xex` titles with GCC, **without the Microsoft XDK**,
and run them from Aurora/FSD on an RGH/JTAG console like any other app.

xexkit is a small C/C++ runtime and build setup:

- **Toolchain:** GCC 16, newlib and libstdc++ from the
  [`free60/libxenon`](https://hub.docker.com/r/free60/libxenon) docker image.
  libxenon itself is not linked; titles run under the retail kernel and
  dashboard.
- **ELF to XEX:** `xex-link` from [xenon-forge](https://github.com/landaire/xenon-forge).
- **Runtime:** start-up with C++ constructors and exceptions, files, time,
  heap, video with the Guide, audio, controllers and crash reports, all on the
  kernel's own API.

It is a general toolkit for any title: games, emulators, apps. It began as
the Xbox 360 port of [FAKE-08](https://github.com/jtothebell/fake-08), a
PICO-8 emulator, and every behaviour it relies on was checked on real
hardware. The [wiki](docs/wiki/Home.md) (in `docs/wiki/`) records what was learned and why the
code looks the way it does.

## What a title gets

| Header | |
|---|---|
| `xex_video.h` | Takes the GPU like Direct3D does: two tiled 32 bpp front buffers at the output's resolution, shown 1:1, flipped at vertical blank. The Guide shows over the title. |
| `xex_audio.h` | An XAudio render-driver client: 48 kHz, 256-sample frames, 6 channels of floats. |
| `xex_input.h` | Four controllers through `XamInputGetState`. |
| `xex_runtime.h` | Guide notifications, time base, sleeping, device mounts. |
| newlib / libstdc++ | `fopen`, iostreams, `opendir`, `stat`, `mkdir`, `remove`, `gettimeofday`, `clock`, `malloc` (96 MB heap), C++17 exceptions, RTTI. |

Paths: `game:/` is the XEX's folder; `hdd:/` is the hard disk (`Hdd1:`);
`usb0:`-`usb2:` are USB drives. A CPU fault writes `game:/crash.txt` (registers
and stack) before the title goes back to the dashboard.

## Quick start

You need Docker and an RGH/JTAG console with Aurora (or FSD) and FTP.

```sh
git clone https://git.kristoffersson.info/repos/xexkit.git
cd xexkit

# 1. once: build xex-link (a static binary in tools/bin)
docker run --rm --user $(id -u):$(id -g) -e HOME=/tmp \
  -e CARGO_HOME=/usr/local/cargo -e RUSTUP_HOME=/usr/local/rustup \
  -v $PWD:/build_dir rust:latest /bin/sh -e /build_dir/tools/build-xex-link.sh

# 2. the tests and the example
docker run --rm --user $(id -u):$(id -g) -e HOME=/tmp -v $PWD:/src -w /src \
  free60/libxenon:latest bash -lc make
```

This writes `build/tests/<name>/default.xex` and
`examples/minimal/build/minimal/default.xex`. Copy a `default.xex` into a
folder of its own on the console (for example `Hdd1:\Apps\MyTitle\`), add the
folder to Aurora and start it.

## Your own title

Copy `examples/minimal/` and list your sources:

```make
XEXKIT     := ../xexkit          # where xexkit is (a submodule works well)
TITLE      := mygame
TITLE_SRC  := src/main.cpp src/game.cpp src/lib.c
TITLE_TOML := xex.toml           # title id and flags for xex-link
all: title
include $(XEXKIT)/mk/xexkit.mk
```

`make` gives `build/mygame/default.xex`, with `app.elf` next to it for
`xenon-addr2line`. See [mk/xexkit.mk](mk/xexkit.mk) for the options, and the
wiki's [Porting guide](docs/wiki/Porting-Guide.md) for a whole port.

## Hardware tests

| Test | Checks |
|---|---|
| `tests/hello` | constructors, exceptions, `std::regex`, an 896 KB deep stack, notifications |
| `tests/fs` | time, `clock()`, directory listing, read/write/seek/stat/mkdir/remove on every device; writes `fs-test-report.txt` |
| `tests/video` | flips at 60 fps, the Guide, every controller input (logged to `video-log.txt`) |
| `tests/audio` | a click and a flash together every second; the number of frames queued ahead (logged to `audio-log.txt`) |
| `tests/fault` | stores to address 0: `crash.txt` must appear and the console go back to the dashboard |

The tests also start in [Xenia Canary](https://github.com/xenia-canary/xenia-canary),
but Xenia has no display controller and differs from the console in several
ways; see the wiki's [Xenia vs. hardware](docs/wiki/Xenia-vs-Hardware.md) page.

## Layout

```
mk/xexkit.mk        build rules a title includes
runtime/            the runtime (C, assembly), its headers and link script
sdk/                kernel/XAM headers and import thunks from xenon-forge
tools/              build-xex-link.sh (xex-link with a 1 MB main stack)
tests/              hardware tests
examples/minimal/   a title to start from
```

## Improving xexkit

xexkit is meant for any title, not only the port it came from, and to get
better over time. [ROADMAP.md](ROADMAP.md) lists what's missing.
[CHANGELOG.md](CHANGELOG.md) lists what each version brought; ports pin a
version tag or a commit.

When you change or add something:

- **Check it on a console.** Xenia only shows that a title starts. Add or
  extend a test in `tests/` that logs what happened to `game:/`, so the next
  person can run the same check.
- **Write down what you found** in `docs/wiki/`: what the console did, what
  you compared it with (Xenia, libxenon, xsdk, a measurement) and why the code
  does what it does. Facts nobody has checked go in as "unverified".
- **Keep the API stable.** New functions are fine. A change that breaks
  existing titles gets a note in the changelog and a new minor version.
- **Keep it free of XDK material.** Use public sources and your own hardware
  results only, and say in comments where a fact comes from.

## Credits and licences

- **[xenon-forge](https://github.com/landaire/xenon-forge)** (MIT/Apache-2.0):
  `sdk/`, `xex-link`, and the GPU ring and flip code that `runtime/video.c`
  ports to C. Its licences are in `sdk/`.
- **[xsdk](https://github.com/jessenic/xsdk)** (MIT): its hardware notes
  showed how a title's picture reaches the screen (the LUT and the 0x60EC
  commit), the Guide, the audio frame format, the vectored exception handler,
  the 49.875 MHz time base and the FAT32 directory-listing bug. The code
  comments say where.
- **[Xenia](https://github.com/xenia-project/xenia)** (BSD): kernel export
  ordinals and structure layouts.
- **[libxenon](https://github.com/Free60Project/libxenon)**: display register
  names, the tiled framebuffer layout, and the LUT autofill.

The rest of xexkit is under the MIT licence ([LICENSE](LICENSE)). No Microsoft
XDK code or headers are used.
