MSX Assembly · the code

Every program in the book

The sources and the programs, in folders named exactly as the book names them. Free to use in your own work, commercial or not, under the MIT licence.

Assemble and run a chapter

You need sjasmplus (assembler), openMSX (emulator and debugger) and C-BIOS (a free BIOS for the emulator). Open a terminal in the folder and type:

cd ch05
openmsx -machine MSXBOOK_EU16 -carta border.rom

Check everything

From the top folder of the full download, this assembles every source again and compares the result with the reference bytes:

python3 verify.py
  • First install sjasmplus 1.24.0, openMSX and C-BIOS (chapter 4 does it on Linux, Windows and macOS; on Debian and Ubuntu, sh setup_linux.sh in ch04 does all of it), then run python3 install_machines.py with the folder or the zip of the C-BIOS ROMs: it copies the book's twelve machines where openMSX finds them.
  • A chapter folder holds everything the chapter prints: the sources, the cartridges that were photographed, the generator that wrote the sources, the meas script and its results, the figures. build.sh assembles the chapter again.
  • verify.py assembles every program again and compares it, byte for byte, with the cartridges the book was photographed from. It needs Python 3 and sjasmplus on the PATH.
  • No system ROM is included. The programs run on C-BIOS, a free BIOS that comes with openMSX.

Chapter by chapter

Ch.TitleFolderDownload
Part I · Before the first byte
1A machine made from a standard: what MSX is, and why it is programmed this waych01/
2Numbers a computer understands: bits, bytes, hexadecimal, and two's complementch02/
3Inside the case: the Z80, the VDP, the PSG, the PPI, and the slotsch03/
4Setting up the development environment: sjasmplus, openMSX, and C-BIOSch04/
5Your first program: a coloured border, from source to cartridge, photographedch05/
6Seeing inside the machine: the openMSX debuggerch06/
Part II · The Z80 language
7Registers, flags, and the shape of a Z80 instructionch07/
8Moving data: LD and every addressing modech08/
9Arithmetic: eight-bit and sixteen-bit sums, carries, and BCDch09/
10Logic, bits, shifts, and rotatesch10/
11Making decisions: the flags, JP, JR, DJNZ, and conditional returnsch11/
12Subroutines and the stack: CALL, RET, PUSH, POP, and the shadow registersch12/
13Blocks, tables, and the index registers: LDIR, IX, IY, and lookup tablesch13/
14Counting T-states: the Z80 at 3.58 MHz, and the wait the MSX adds to every M1ch14/
15sjasmplus for the MSX: labels, macros, the cartridge header, and a ROM imagech15/
16The BIOS as a library: the jump table, the system variables, and the hooksch16/
Part III · The VDP: video memory behind a port, tiles, and thirty-two sprites
17Taking the machine: the BIOS interrupt, the H.TIMI hook, and an interrupt of your ownch17/
18How a frame is built: 228 T-states a line, 313 lines in Europe and 262 in Japanch18/
19Talking to the VDP: two ports, eight registers, and sixteen kilobytes you cannot addressch19/
20How fast the VDP listens: access slots, the 29 T-state loop, and what a write too soon destroysch20/
21The screen modes: text, Graphic I, Graphic II, and multicolourch21/
22Graphic II, the screen of the games: three thirds, 768 patterns, and colour in strips of eightch22/
23Colour clash on the MSX: two colours in eight pixels, and how to draw around itch23/
24Tiles and the name table: screens built from 8 × 8 pieces, and a copy in RAMch24/
25Plotting and drawing in Graphic II: pixels and lines through a portch25/
26Hardware sprites I: the attribute table, patterns, size, magnification, and the early clockch26/
27Hardware sprites II: four on a line, the fifth-sprite flag, and flicker by rotationch27/
28Software sprites in the tiles: redrawing patterns to move what the hardware cannotch28/
29Scrolling without hardware help: by tile, by pre-shifted tiles, and what each costsch29/
30Fonts and text: the BIOS character set, a set of your own, and a status linech30/
31Fast transfers to VRAM: unrolled OUTI, the vertical-blank budget, and the blanked screench31/
32Racing the beam on an MSX1: register changes in mid-frame, timed from the interruptch32/
Part IV · Making it a game machine
33The frame loop: the interrupt as a heartbeat, fifty or sixty times a secondch33/
34Reading the keyboard: the PPI, eleven rows, ghosting, and layouts that change by countrych34/
35Joysticks through the PSG: two ports, two buttons, and one standardch35/
36The PSG: three tones, noise, envelopes, and the mixer bits that must not movech36/
37An engine for sound effects and music that runs from the interrupt, at 50 and 60 Hzch37/
38Slots and memory: where the RAM is, how a 32 KB cartridge finds itself, and where things livech38/
39Collisions: tile tests, bounding boxes, and the coincidence flagch39/
40Score, text, and a status line that never flickersch40/
41Random numbers, the R register, and game statesch41/
42The frame budget: fitting a game into 59,736 T-statesch42/
Part V · Two complete games
43The first game I: a single-screen arcade game, from specification to cartridgech43/
44The first game II: sound, score, difficulty, and the finished ROMch44/
45The second game I: a flip-screen world, rooms in ROM, and a mapch45/
46The second game II: enemies, items, music, and the finished ROMch46/
47Where to go next: three projects, sketchedch47/
Part VI · Beyond the 32 KB cartridge
48MegaROMs: mappers, banks, and a 128 KB cartridgech48/
49Samples through the PSG: drums and voices from the volume registersch49/
50Tape and disk: BLOAD, CAS and DSK files, and when a cartridge is not the answerch50/
51One cartridge, many machines: makers, MSX2 and later, 50 and 60 Hz, and the slot zooch51/
52Optimisation: T-states, unrolled loops, code copied to RAM, and self-modifying codech52/
Part VII · The rest of the craft
53Compression: RLE, and a packer for screens and levels, unpacked straight into VRAMch53/
54Title screens, demo mode, and attract modech54/
55Testing on real hardware: real MSX machines, flash cartridges, and the video outputch55/
56Releasing your work: ROM files, cartridges, labels, and the MSX scene todaych56/