Run the game and follow one move
The standalone Tic-Tac-Toe app has two-player and computer-opponent modes. Choose a mode with F1 or F3, move the cursor with the arrow keys, and place a mark with Space or Return. The disk image includes the idiomatic implementation as TICTACTOE and the other standalone variant as CLASSIC.
Follow one move in the source. main waits for a PETSCII key, hands it to handleKey, and renders the updated game. handleKey decides whether the key changes modes, moves the cursor, or attempts a move. This keeps drawing out of the move rules.
fn handleKey(game: *Game, key: u8) void {
if (key == c64.key.f1) {
initGame(game, .two_player);
} else if (key == c64.key.f3) {
initGame(game, .ai);
} else if (key == c64.key.cursor_down or key == c64.key.cursor_up or key == c64.key.cursor_right or key == c64.key.cursor_left) {
game.cursor = moveCursor(game.cursor, key);
game.message = 0;
} else if (key == c64.key.space or key == c64.key.return_) {
applyMove(game, game.cursor);
applyAiIfNeeded(game);
}
}
Download the Tic-Tac-Toe disk · Read the complete standalone application
Keep one mutable record for the game
Board contains nine one-byte enum cells. Game owns that board plus the move list, history, mode, and cursor. The entry point takes &game once and passes *Game to helpers that update the same record. A by-value argument would copy its fields. A pointer also lets the renderer borrow &game.board without copying the history or depending on its layout.
State layout
const Board = struct { cells: [9]Cell };
const Game = struct {
board: Board,
moves: [9]u8,
x_history: [10]u16,
o_history: [10]u16,
move_count: u8,
turn: Cell,
cursor: u8,
mode: Mode,
outcome: Outcome,
message: u8,
};
Mutation through a pointer
const board: *Board = &game.board;
const turn = game.turn;
const move_count = game.move_count;
board.cells[index] = turn;
game.moves[move_count] = index;
game.move_count = move_count + 1;
In the complete function, the index must be below nine, the game must still be playing, and the cell must be empty. These checks happen before the write. The moves and history are bounded arrays because the game has a fixed maximum number of moves. Each new snapshot uses word bit masks to hold up to nine positions.
Read applyMove and recordSnapshot · Fixture checking in-place state changes
Render screen codes and colors
The renderer passes a screen pointer at $0400 and a color RAM pointer at $d800 to each cell-drawing call. The same offset selects the character and its color. drawCell writes nine positions for a glyph using the cell and cursor as inputs. Volatile pointers tell the compiler to emit the actual stores.
fn drawBoard(board: *Board, cursor: u8) void {
const screen: [*]volatile u8 = @ptrFromInt(0x0400);
const color: [*]volatile u8 = @ptrFromInt(0xd800);
for (0..9) |i| {
drawCell(screen, color, board.cells[i], i, cursor);
}
}
The app uses a short assembly helper for the border and an assembly badge. The helper reads a byte parameter through @color and writes the VIC-II register at $d020. Typed game code and explicit hardware writes can live in the same program.
fn setBorderAsm(color: u8) void {
asm {
lda @color
sta $d020
}
}
Screen and keyboard chapter · Assembly operands and preservation
Test the rules separately from the display
The repository includes a host fixture for the game's rules. It checks state transitions and outcomes independently of the keyboard and screen. The standalone sample exercises keyboard input and rendering in VICE. For a new game, isolate rules that can be verified from memory and check the hardware-facing behavior separately.
Tic-Tac-Toe rules fixture · Test layers and fixture conventions
Read the hosted ReadyOS variant
Both variants initialize a game, read PETSCII input, and render to the C64 screen. The difference starts at the executable boundary. The standalone PRG loads at $0801 and its BASIC stub calls the generated entry. The hosted PRG loads and enters at $1000. ReadyOS provides the resident shim and transfers control there. The hosted program's exclusive limit $c600 leaves $c600–$c9ff available to the shim.
memory {
// ReadyOS owns this resident region and the application snapshot cache.
region readyos_shim: c64 at 0xc600 size 0x0400;
image prg {
load_addr = 0x1000;
entry_addr = 0x1000;
basic_stub = false;
address_limit = 0xc600;
}
}
Pressing Ctrl-B reaches the shim's launcher vector at $c80c. F2 and F4 look for a loaded application and, when one is found, the selection helper stores its logical token at $c820. The handler then jumps through the switch vector at $c80f. The inline terminal jump lets the handler transfer directly without adding a separate helper call. The shim owns the switch and the host's REU cache.
if (key == 2) {
// Terminal transfer is inline so no ZeroPage call return address is
// left on the hardware stack inherited by the next ReadyOS app.
asm {
jmp $c80c
}
} else if (key == 137) {
const next_found = readyos.selectNextApp();
if (next_found != 0) {
asm {
jmp $c80f
}
}
} else if (key == 138) {
const previous_found = readyos.selectPreviousApp();
if (previous_found != 0) {
asm {
jmp $c80f
}
}
}
To compare the builds, inspect each map's prg.load, prg.entry, and memory region list. The hosted binary needs ReadyOS for the navigation vectors. Test launcher return and application switching under that host. Re-entry at $1000 currently starts a fresh game. The ReadyOS design notes describe the remaining work for resume-aware state and host-managed REU allocation.