zxGammon

Source

zxGammon is written in Z80 assembly. Everything needed to build it is here: the program's source, and the files it includes — the neural networks' weights, the pictures and the tables — as they are.

zxgammon-source.zip

Building

With the open-source assembler sjasmplus (1.20 or later), in a folder holding all of these files:

sjasmplus zxgammon.asm

That writes zxgammon.sna, a snapshot to open in an emulator, and zxgammon.tap, a tape to load with LOAD "". Your tape will be identical, byte for byte, to the zxgammon.tap you can download from the front page.

The program loads at $5E00 and starts there: the game, then from $8000 the engine that chooses Gnubby's moves. It uses $D700–$F3FF to work in, the cube's table for matches is at $F400, and the stack is at the top of memory. Interrupts stay off: the keyboard is read directly, and the engine reads its weights off the stack pointer.

The program

FileWhat it isBytes
game.asmThe game: its memory map, what Gnubby says, its pictures and tables; includes the rest.7834
play.asmThe title and menu, games and matches, the player's and Gnubby's turns, the doubling cube, the scores.28458
screen.asmThe board, checkers, dice, cube, Gnubby, the scores and what he says, drawn only where they change.24612
input.asmThe keyboard, the beeper and the dice.4248
log.asmThe game as it is played, a record at a time in memory, for whatever reads it from outside (this site catches each and writes the games out as a match file).3611
ram.asmThe engine's work area.5035
gen.asmThe legal moves from a position, each position once.11130
encode.asmPositions turned about, totals, and other helpers.1408
features.asmGNU Backgammon's 25 contact features of a position, in integer arithmetic.24866
net.asmThe neural networks, computed without multiplication: only the inputs that change are added.11729
choose.asmChoosing a move: the filter, the contact (or race) net, and a look a ply ahead over the opponent's 21 rolls; and the same look ahead for the cube.12777
data.asmThe networks' layouts, small tables, and the weights' files.1330
zxgammon.asmThe file to assemble: places the program, the loading screen and the cube's table in memory, and writes the snapshot and the tape (with its BASIC loader).1942

Included files

Binary files, read in whole by incbin.

FileWhat it isBytes
checkerA checker, 16 by 16; then with each count from 1 to 15 cut out of it.512
contact.b1The hidden units' biases, 16 bits each.96
contact.b2The output's bias, 24 bits.3
contact.hThe net's hidden units.1
contact.w1The net's first layer: a row of weights (one a hidden unit) for each input, stored plus 128.12912
contact.w2The output's weights, one a hidden unit.48
cubeThe doubling cube's faces: 64 (at 1), 2, 4, ... 64, 128.256
diceThe six faces of a die, 16 by 16.192
filter.b1The move filter's bias.2
filter.w1The move filter's weights, one a board input, stored plus 128.202
fontThe characters, 32 to 95, 8 by 8.512
mascotGnubby, 32 by 32: calm, happy, sad and thinking.512
moneyrecThe cube's thresholds in money play.10
ptnumThe points' numbers, 1 to 24, in small digits.384
race.b1The hidden units' biases, 16 bits each.16
race.b2The output's bias, 24 bits.3
race.hThe net's hidden units.1
race.w1The net's first layer: a row of weights (one a hidden unit) for each input, stored plus 128.1752
race.w2The output's weights, one a hidden unit.8
reply.b1The hidden units' biases, 16 bits each.16
reply.b2The output's bias, 24 bits.3
reply.hThe net's hidden units.1
reply.w1The net's first layer: a row of weights (one a hidden unit) for each input, stored plus 128.1752
reply.w2The output's weights, one a hidden unit.8
sigThe sigmoid, as a table (its upper half).129
triangleA point, 80 lines from its base.160
loading.scrThe loading screen, as the Spectrum's screen memory.6912
mettab.binThe cube's thresholds in a match, by the score and the cube, from GNU Backgammon's match equity table.1260

Licence

zxGammon is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. It is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

The emulator on the front page is part of this site's source too: js/z80.js, js/spectrum.js and js/main.js.