# Porting guide

How to bring an existing C/C++ program to the console with xexkit. FAKE-08
(PICO-8 emulator, C++ plus Lua) is the worked example; its host is
`platform/xex/source/XexHost.cpp` in the [fake-08-xbox360](https://git.kristoffersson.info/repos/fake-08-xbox360.git) repository.

## 1. Build it

Add xexkit as a submodule and write a Makefile (see `examples/minimal`):

```make
XEXKIT      := $(CURDIR)/xexkit
TITLE       := mygame
TITLE_SRC   := $(wildcard ../../src/*.cpp) $(wildcard ../../lib/*.c) source/MyHost.cpp
TITLE_TOML  := mygame.toml
TITLE_FLAGS := -I../../src -DMY_PLATFORM_XEX
TITLE_C_AS_CXX := 1        # only if C++ exceptions pass through C code (Lua)
all: title
include $(XEXKIT)/mk/xexkit.mk
```

Typical compile problems:

- **Missing POSIX bits:** xexkit has `open`/`read`/`opendir`/`stat` and so on.
  `chdir`, `getcwd`, sockets and threads aren't there yet.
- **`getopt` and `argv`:** `main` gets `argc = 1`, `argv[0] = "game:\default.xex"`.
- **Headers clashing with newlib's types** (for example XAM's networking
  header): declare the few functions you need yourself.
- **Lua and exceptions:** z8lua throws C++ exceptions through its C sources,
  so they must be compiled as C++.

## 2. Platform layer

Most programs already have a platform or host layer (SDL, a console port).
Copy the simplest one and replace:

| Program needs | xexkit |
|---|---|
| a framebuffer | `xex_video_begin()` → draw → `xex_video_present()` |
| vsync | built into `xex_video_begin()` |
| sound | `xex_audio_next()`/`xex_audio_queue()` |
| input | `xex_input_get()` for ports 0-3 |
| timing | `xex_timebase()` / `xex_timebase_freq()`, `xex_sleep_ms()` |
| files | ordinary stdio/POSIX on `game:/`, `hdd:/`, `usb0:/` |
| quitting | return from `main` (back to the dashboard) |

## 3. Video

- Ask for the window size after `xex_video_init()` (`xex_video_begin()`
  returns it). Expect 1920x1080 on a 1080p TV, or 640x480 in Xenia and on
  interlaced outputs.
- Scale by an integer factor yourself for pixel art: 128 x 8 = 1024 fits
  1080. Keep 1:1 pixels; the output is square-pixel at the native size.
- Make the blit fast: see "Drawing fast into write-combined memory" in
  [Video](Video.md). The first FAKE-08 blit (11 ms) dropped Celeste to 41 fps.
- Clear the whole buffer (both buffers, so twice) when the layout changes;
  the two front buffers alternate.
- Draw overlays (FPS counters) **every frame**, for the same reason.

## 4. Audio

- Resample to 48 kHz floats.
- **Fill several times a frame**, before and after the vblank wait. Keep
  5 frames (27 ms) queued; see [Audio](Audio.md).
- If the program pulls audio in its main loop after drawing, as FAKE-08's core
  did, do the filling from the host instead (FAKE-08's `audioTopUp`) and
  tell the core not to.

## 5. Input

Map A to confirm and B to back. Read all four ports for multiplayer.

## 6. Files

- Put the program's data next to the XEX: FAKE-08 uses `game:/roms` for
  carts and `game:/fake08` for settings, saves and its log.
- Make data folders on first start (`mkdir`).
- Fall back to a writable place when `game:` is read-only (Xenia).

## 7. Robustness

- Keep `crash.txt` working: it found FAKE-08's USB bug in one run.
- Log the build id first; see [Pitfalls](Pitfalls.md).
- Never quit by yourself while the Guide shows (`xex_video_guide_visible()`).
- A performance overlay, with emulation, blit and wait times and late frames,
  logged every few seconds, answers "why does it stutter" without guessing.

## 8. Test on the console

Xenia for "does it start", the console for everything else. Run the program
with the performance overlay on, try the Guide, leave through Xbox Home, and
check that the dashboard comes back normally.

## FAKE-08's numbers on a 1080p TV

- **Video:** 1920x1080 native, 8x scaling (1024x1024), 60 fps, no late frames
  in Celeste once the blit was fast.
- **Audio:** queue 5 frames, no underruns while playing.
- **Emulation:** 0.6-6 ms per frame, depending on the cart.
