Programs/Connect4 · 20 November 2026 · build 4,330

Connect 4 for the Psion Organiser II

One 32K Datapak that plays Connect 4 on every Organiser II, from an 8K CM to a 64K LZ64. The game runs as 6303 machine code in a single 4,330-byte array; everything else waits on the pack and is read in when needed. This report shows the screens and menus, how the program is put together, how the engine thinks, and where every byte goes.

4,330 Bthe whole game in RAM, every model
≈1,400 Bleft over on a blank 8K CM
28,444 Bused on the 32K Datapak
285 /spositions searched a second
13,260solved opening-book entries
29 ROMsCM, XP, LA, LZ, LZ64 versions tested
1 · What you see

Screens and menus

The two-line models (CM, XP, LA) draw the whole board in the display's eight custom characters: a block 4 characters wide and 2 high, each cell 2 by 2 dots. Solid is a filled square, Hollow a diagonal pair. The four-line LZ and LZ64 give each column its own character and draw discs 5 by 3 dots. The other 12 characters of each line are the text panel. The screens below are drawn dot for dot from the game's rules, with the same sample position.

Playing

The menu (ON/CLEAR)

15 items, shown 2 or 4 at a time with “>” on the chosen one. UP and DOWN move, a letter jumps to the next item starting with it, EXE takes it, ON/CLEAR goes back. New game asks One player, Two players or Demo; Two players then asks each player for a name of up to 6 letters.

Settings

ItemValues (LEFT / RIGHT)What it does
Level1 … 8, AutoHow hard the Psion plays; Auto moves up after your win and down after your loss
Play1st, 2nd, AltWhether you start, the Psion starts, or you take turns
CoachOff, OnComments on each move from the book and solved endings; shows threat cells; names a drop that misses a four or lets one in or a fork, before it is made (one and two players)
HintsYes, NoSPACE asks for a 600-position search
UndoYes, NoDEL takes back your move and the reply
KeepAsk, All, OffFinished games: ask “Export game?” before a new game starts over one not exported; export every one as its result shows; or neither. The last game is kept in A:C4LAST whatever this says
PaceNat, FastA short pause before the moves the Psion makes without searching (book moves, a win, the one block, every move at levels 1-2), or none
SoundAll, Tick, OffWin tune and buzzes, only the drop tick, or silence
DigitsOff, OnWhether 1–7 also drop the disc
StartsAlt, Lost, WonTwo players: who starts the next game (the one who lost, or who won)
SwapOff, OnTwo players: after Solid's first disc, Hollow may take Solid
TimerOff, 10s, 20s, 30sTwo players: a countdown for each move, shown after the column (“ANN:4 17”); held while the menu is open; at 0 the disc drops in the chosen column
Match1, 3, 5, 10Two players: games needed to win a match
ClockNone, 1m, 3m, 5m, 10mTwo players: a game clock for each side
Demo S / Demo H1 … 8The levels Solid and Hollow play at in the demo

Keys while you play

LEFT RIGHT
choose the next column with room (the landing cell blinks: the ghost disc)
1 … 7
choose that column (U V W O P Q I, the keys that carry the digits)
EXE DOWN
drop the disc (coach on: a bad drop is named first; EXE or DOWN again drops it)
DEL
take back
SPACE
hint: its landing cell blinks fast
MODE
ask the coach
UP
the move list
ON/CLEAR
the menu
any key
while the Psion thinks: it moves now
Result screen
EXE new game (Keep Ask: “Export game?” first, EXE yes, ON no) · E export · ON menu · UP move list · DEL take back · MODE review. The winning four blinks for about 15 s while the last line names the keys in turn, 2 s each; then the line is put back and the screen rests so the Organiser can switch itself off.
Review
LEFT RIGHT a move back or on · UP start · DOWN next marked move, then the end · EXE, ON or MODE back to the game
Set up
LEFT RIGHT a column · S or H add a disc · DEL remove the top one · EXE play on (a move order that reaches it is found) · ON leave it as it was
Help
any key turns the page, ON/CLEAR goes back

Other screens

Coach verdicts: “Coach: win”, “Coach: draw”, “Coach: lost”, “Still a win”, “Good move”, “Win -> draw”, “Win -> loss”, “Draw -> loss”, and for solved endings “Win in 9”, “Loss in 4”, “A draw”; out of the book with no solved ending, “Can't tell”. Exported games are files A:C4G1 … A:C4G99, one record of column digits such as 4435362; the last game finished is A:C4LAST.

2 · How it is built

One resident core, everything else on the pack

Nothing about the game is written in OPL except a few lines that make room for it. Two small OPL procedures check the free RAM and make one integer array; a 75-byte loader poked into the top of that array finds the kernel on the pack, reads it in and starts it. From then on the 6303 kernel runs the game, and every screen, the opening book and the animations are read from the pack into a 671-byte slot only when they are needed.

Starting

  1. C4 (OPL, translated for the CM so every model runs it) checks FREE: the game needs 4,660 bytes, or 4,794 on the first launch, before its save files exist (they take 134). It really starts with 4,652 (tests/m1/needs.js on CM 2.4 and 3.3, 20 Nov), so the check keeps 8 bytes in hand. If there is not enough it says exactly how many bytes are missing.
  2. C4G declares the array (4,330 bytes) and pokes the 75-byte loader into its top from one string, then calls it with USR. C4G stays in RAM all game, and once the loader has run that string is never read again: the kernel keeps its 75 bytes of working variables in it, 196 bytes below the array on every ROM.
  3. The loader finds the kernel's block C4K on C:, B: or A: with the OS service FL$BOPN, reads the kernel, the 65-byte fixer and the fix-up list into the array in one go, and runs the fixer.
  4. The fixer adds the array's address to each of the kernel's absolute addresses, then jumps to the kernel. Every block of code is assembled to run at offset 0 and fixed up like this when it is loaded, because the array can sit anywhere in RAM.
  5. The kernel's start-up code (478 bytes, run once from the overlay slot and then overwritten) reads the model byte, picks the two-line or four-line screen driver, switches an LZ to four lines, and shows the title.
  6. OV_GAME loads the saved game from A:C4SAV and replays it, or sets up a new one. The game loop takes over.

The game loop

flowchart LR
  L[game loop] -->|game over| R[OV_RESULT
count, show] --> TA[OV_HELP
the tally] --> EN[OV_END
keys, blink] L -->|your turn| PA[OV_PANEL
fixed lines] --> PL[OV_PLAY / OV_PLAY2
keys, ghost disc] L -->|Psion's turn| BK[OV_BOOK
book move or
search figures] --> TH[search
+ OV_THINK display] PL -->|command| C[kernel commands] --> L EN -->|command| C TH --> MV[play the move] BK -->|book move| MV MV --> FX[OV_FX
record, fall, make,
show; the end?] --> SV[OV_GAME
save] --> L

Each box after the loop is an overlay read from the pack into the slot (about 40 ms each). An overlay can hand straight on to the next by jumping into the loader, so a chain such as OV_RESULT → OV_END costs no kernel code.

The 20 units on the pack

UnitBytesJob
3 · How it thinks

The engine

The engine is 2,229 bytes of 6303 code and tables, kept in RAM all the time. It was written twice: once in JavaScript as the specification, and once in assembler. A test plays both side by side and requires every state byte, every evaluation, and every search's move, score, depth and position count to be identical.

What it knows about a position

Board
49 bytes: 7 columns of 6 cells plus a stop cell, so a column is never searched past full.
Lines
All 69 possible fours (21 up, 24 across, 24 diagonal). One byte each: Solid's discs in the low half, Hollow's in the high half.
Threats
One byte a cell: how many lines would give Solid, or Hollow, four by playing there.
Score
A running total of open lines, 1, 3 or 9 for one, two or three discs in a line the other side has not touched.
Key
24 bits, changed with each move, made from the cell number with the 6303's multiply (no key table).

A move updates only the lines through its cell (3 to 13, about 6.5 on average). One routine plays both sides: a branch is patched to “always” or “never” to read each line byte with its halves swapped for Hollow.

How it judges a position

The open-line score, plus threat parity. For each column, the lowest threat a side has there counts 16 if it is on that side's good rows (odd rows for Solid, even rows for Hollow, the rows where the other side will be forced to give the cell up) and 4 otherwise. The result is kept between −50 and +50, well away from the scores of won and lost positions.

Levels 1 and 2 add noise drawn from the key, so their mistakes vary from game to game.

How it searches

  1. Iterative deepening. Search one move deep, then two, and so on, until the level's budget of positions runs out or the result is proven. Each pass orders moves from the last.
  2. Alpha-beta on explicit frames. No recursion: 20 frames of 10 bytes hold each depth's window, best move and progress. A won position scores 100 less the number of moves to the win, so it plays the fastest win and resists a loss longest.
  3. Forced moves first. If the opponent can win at once in two places the position is lost; in one place, that is the only move searched. At the root the Psion plays these without a search: a win, a lost position, and (since 19 Nov) the one block, which at level 8 used to cost about 23,000 positions, some 75 s, to find the move every search chose. At Pace Natural these get the book’s 1.5 s pause, so the reply does not look abrupt. A move that lets the opponent win directly above it is scored as the loss it is, without searching it.
  4. A 128-entry table of 4-byte entries (13-bit check, move, score, depth, bound) remembers positions met by another move order. Moves are tried in the order: the table's move, the move that last caused a cut at this depth, then centre out.
  5. Every 64 positions the search calls the thinking display: a key stops it, and the depth, seconds and a bar of tenths of the budget are shown.

The search's table and working space (779 bytes) are laid over the screen driver and the overlay slot, which are idle while it thinks; the driver is read back from the pack afterwards. Speed: about 3,230 processor cycles a position, 285 positions a second at 0.92 MHz, the same on every model.

Levels

LevelPositions≈ timeOtherKept
1depth 2<1 snoise 4896.2%
2depth 2<1 snoise 2496.0%
36002 snoise 1297.6%
49003 snoise 6, book 297.2%
51,5005 sbook 396.7%
63,00010 sbook 497.3%
79,00030 swhole book97.7%
830,000100 swhole book98.5%

“Kept”: the share of its moves that keep the position's true result, against a perfect opponent, from random positions after 8 discs. “Book n”: the Psion's first n moves come from the book.

The opening book

Solved on the PC by our own perfect solver and checked against John Tromp's database of all 8-disc positions. Each entry is 4 bits: the best column, a win (only 20 entries are draws or losses, and they use just 8 column-and-result pairs, which codes 8–15 stand for). Entries are found by the opponent's moves alone (the Psion's are the book's own), with openings in columns 5–7 mirrored, so no positions are stored.

Slots
22,409 × 4 bits = 11,205 bytes on the pack (14,007 at 5 bits until 19 Nov)
Entries
13,260 filled
Depth
the opponent's first 5 moves: the Psion's first 5 or 6 moves are perfect

Exact endings

Every position with 14 or fewer empty cells is solved exactly at level 8, most with 16–18 (“Win in 9”). The Psion's solved results and book values are kept in a 2-bit-a-move history in the save file, which the coach and the review use to mark moves that threw away a win or a draw. The review also marks, anywhere in any game, a move that missed a four, or let in a four or a fork that another move would have stopped (“Allows fork”); DOWN in the review jumps from one marked move to the next. With the coach on, the same check runs on each drop before it is made (since 19 Nov).

4 · Where the bytes go

RAM

The game is the same 4,330-byte array on every model (4,530 on the morning of 20 Nov: then the overlay and command tables moved onto the pack, the empty-board routine used the ROM’s fill, the rarer commands and the end of a game moved into overlays, and the start-up code took over the kernel’s entry). The tight case is the 8K CM, where the Organiser's own system variables and stack already take 1,683 bytes. Drawn to scale:

A blank CM's 8,192 bytes while the game runs

On an XP (16K) or LA (32K) the same array leaves 8–24K free. On the LZ64, files on A: live in separate RAM banks and cost the game nothing.

The array, 4,330 bytes

Kernel and engine, 3,097 bytes

Variables, 351 bytes (276 in the array)

The kernel's working variables (75 bytes) are not in the array: they sit in C4G's copy of the loader string, which is dead once the loader has run. The saved part (settings to checksum, 81 bytes) is written to A:C4SAV straight from where it sits. The tally is not in RAM at all: it lives in A:C4TAL and is read only on the result screen.

Most of the engine’s tables (326 of 341 bytes) list the lines through each cell, read on every move of the search. Working them out instead was built and measured on 20 Nov (same moves as the reference engine): 124 bytes less RAM with the search 21% slower, or 96 bytes with a small range table and 12% slower. Levels count positions, so strength would not change, but level 8 would think about 12 s longer a move. Not done.

One region, three uses

The screen driver and the overlay slot are 957 bytes together. Between searches they hold the driver and one overlay. During a search they hold the engine's table, its working space and the thinking display (which carries its own variables), with 2 bytes to spare:

Overlays against the 671-byte slot

Hatched: the bytes some overlays leave at the top of the slot (set-up's search; the review's tactics; the result's last line, kept for the key names). Since 19 Nov export and import use the screen driver's area as their buffer instead, and read the driver in again after. The slot is as big as the largest overlay with its buffer; set-up fills it exactly.

5 · On the pack

The 32K Datapak

C4K, C4B0 and C4B1 are data blocks disguised as OPL procedures whose code is a single RETURN, so running one by mistake does nothing; each costs 33 bytes of headers.

C4K, the kernel block, 3,950 bytes

The fix-up list is stored as steps between addresses, two to a byte (1–15: on that far and fix the word there; 0: on 15): 261 bytes, where one byte a gap took 485 until 19 Nov. The overlays’ lists are stored the same way, so each loads 6–12 ms faster. The two lengths at the front let the loader read everything in one go.

C4B1, the opening book, 11,205 bytes

Read a byte at a time straight from the pack while the Psion chooses its book move; never copied into RAM.

Files the game makes on A:

A:C4SAV
93 bytes of RAM: the settings and the game, saved after every move
A:C4TAL
41 bytes: wins, draws and losses for each level
A:C4LAST
56 bytes: the last game finished, rewritten in place (Import game: “Last game”)
A:C4Gn
about 40 bytes each: exported games; 33 fit beside the game on a blank CM. The next is refused while C4 can still start again

C4B0, the screens, 12,413 bytes

Solid: the code. Light: its fix-up list (read and applied as it loads, never kept in RAM). The block starts with two tables the kernel kept in RAM until 20 Nov: the command table (34 bytes) and where each unit starts (40); each command and each load now reads its two bytes from the pack. Shared routines such as the menu, the move record and the history are copied into each overlay that needs them, which costs pack space but no RAM.