This repository holds a commented disassembly of Demonia for the MSX (Microids, 1986, cassette). Everything here comes out of the binary and can be generated from it again.
The tape goes in the root, named demonia.cas. It is exactly 58,334 bytes:
sha256 69031ee25099df0703332ca74a64b52e6dd9909e4cfded4c3699be153adc5fc9
The Makefile asks for it by that sha256 and warns if it does not match.
make verify
This reassembles the thirteen listings with pasmo and compares the result
against the original .cas, byte for byte. Here that takes one step more than
a cartridge would: first every listing is checked on its own, and then the
chunks are glued back together in the order the tape carries them, with their
sentinels, their headers and their padding. If it prints OK, the disassembly
has invented nothing: it is the same tape written differently.
make # the whole cycle: extract, trace, list, verify
make listados # regenerates the thirteen src/demonia_*.asm
make sanity # the checks reassembly does NOT cover
make test # the tests
make densidad # how many comments each routine carries
make sobra # the accounting for what the game carries and never uses
make imagenes # redraws the PNGs in docs/imagenes from the tape
make web # regenerates this site
make sanity is the one that watches for what a correct reassembly can hide:
data being read as code. The bytes come out the same either way, because they
do not change; the only thing that changes is what we say about them.
The nineteen tests in tests/ need neither the tape nor make: they read
the thirteen published listings, which is where every byte lives — the defb
rows carry the data with its address, and the instructions carry theirs in the
generated comment.
That was deliberate. A test that skips itself when a file is missing is a place
to hide a check that never actually runs; and besides, what gets published is
the listing, so the listing is what needs watching. If someone hand-edits a
defb, the tests notice. No rigour is lost, because make verify already
proves that assembling those listings gives the tape back exactly.
Getting started · The game · The code · Findings · Open questions
Demonia does not come on a cartridge: it comes on a 58,334-byte tape, and almost everything odd about this game starts there.
A .cas is a list of blocks, each preceded by eight sentinel bytes. There are
thirteen here, and among them three named files. All three are called
DEMON:
| file | type | loads at | size |
|---|---|---|---|
DEMON |
ASCII | — | the BASIC loader |
DEMON |
BIN | 0x8180 |
249 bytes |
DEMON |
BIN | 0x8000 |
24,576 bytes |
Between the two BINs go seven headerless blocks, which cannot be loaded from BASIC because they carry no name: the program in the second file reads them.
The three sharing a name is not an oversight. Line 50 of the BASIC loader is:
BLOAD"CAS:DEMON",R:RUN
Since tape is sequential, the first time that line runs the head is before the
small BIN and finds that one. The ,R starts it; when that program is
done it returns to BASIC, the :RUN restarts the loader and line 50 runs
again. But the tape is now past the seven blocks, so this second time it finds
the big BIN, which is the game.
Two files with the same name, and a RUN to choose between them.
The small BIN is the real loader. At 0x8180 it carries a table of seven
eight-byte entries, each holding three addresses and a sentinel:
defw 0a000h,0a618h,0a600h,0ffffh ; load, end, execute, FFFF
defw 0ad00h,0c07fh,0c050h,0ffffh
defw 0a000h,0b531h,0b468h,0ffffh
defw 0a000h,0bf57h,0bf32h,0ffffh
defw 0a000h,0bbbbh,0bb96h,0ffffh
defw 0afd0h,0bfffh,0afe0h,0ffffh
defw 09000h,0971fh,096f8h,0ffffh
The loop at 0x8208 walks the whole thing and stops at 0x81B8, which is
what the ld de,0x81B8 at 0x8236 says. Eight bytes per entry, from 0x8180
to 0x81B8: seven.
For each chunk the loader takes the load address (bytes 0 and 1) and the end (bytes 2 and 3) out of the table, works out the length by subtracting and adding one, reads the block off the tape and jumps to the execution address (bytes 4 and 5). So each chunk runs the moment it is read, before the next one is.
Notice that four of the seven load at 0xA000 but execute much higher up. That
is because what runs is not the whole chunk but the tail glued to it.
An MSX booted into BASIC sees ROM in pages 0 and 1, and RAM only in pages 2 and
What it does is look for a 64 KB slot and put 26 KB underneath the ROM. The
tail glued to each chunk is always the same idea: switch the slot so the page
in question is RAM, do an LDIR copying the chunk to its final home, and put
the slot back as it was.
That is why the disassembly has thirteen listings and not seven: the chunk has to be disassembled as it lands off the tape and as memory ends up once everything has moved.
| listing | org | bytes |
|---|---|---|
arranque |
0x8180 |
249 |
bajo0000 |
0x0000 |
15,048 |
bajo4000 |
0x4000 |
5,224 |
bajo6000 |
0x6000 |
4,096 |
bajo7900 |
0x7900 |
1,768 |
carga1 |
0xA000 |
1,561 |
carga2 |
0xAD00 |
4,992 |
carga3cola |
0xB468 |
202 |
carga4cola |
0xBF32 |
38 |
carga5cola |
0xBB96 |
38 |
carga6cab |
0xAFD0 |
48 |
carga7cola |
0x96E8 |
56 |
juego |
0x8000 |
24,576 |
The five hidden chunks were checked byte for byte against the RAM of a running openMSX, and also against the RAM snapshots inside a replay recorded on a different machine. Two independent measurements that agree.
whole tape 58,334 bytes
traced code 20,299
named data 37,597
tape wrapper 438
unexplained 0 -> 100.00%
The 438 bytes of wrapper break down like this, and none of it is a mystery:
| what | bytes |
|---|---|
| block sentinels | 104 (13 blocks × 8) |
| file headers | 48 (3 files × 16) |
| BIN header | 12 (2 files × 6) |
| BASIC loader | 256 |
| alignment padding | 18 |
What the tape spends on being a tape is 18 bytes. A block has to start on a multiple of eight, so up to seven padding bytes can follow each one; here only five of the thirteen need any.
The BASIC loader, on the other hand, is nearly empty: its two lines take 93
bytes and the other 163 are the 0x1A the MSX pads the 256-byte chunk with.
64% of the tape's first file is padding.
make verify cannot just reassemble and compare, the way it would for a
cartridge. Here it does two things:
.cas — all thirteen blocks, with their sentinels,
their headers, the BASIC loader and the padding — and checks it gives the
same sha256 as the original tape.If both steps pass, the disassembly has invented nothing.
Getting started · The game · The tape · Findings · Open questions
When the seven chunks have finished moving, the MSX's memory looks like this. That is 50,712 bytes occupied out of 65,536, 77.4% of the map:
| address | bytes | what is there | from which chunk |
|---|---|---|---|
0x0000-0x1F31 |
7,986 | first sprite bank: 242 records of 33 bytes | chunk 4 |
0x1F32-0x3AC7 |
7,062 | second bank: 214 records | chunk 5 |
0x4000-0x5467 |
5,224 | the 26 compressed screen maps | chunk 3 |
0x6000-0x6FFF |
4,096 | the scenery: 2,048 of pattern and 2,048 of colour | chunk 6 |
0x7900-0x7FE7 |
1,768 | the music | chunk 7 |
0x8000-0xDFFF |
24,576 | the whole game | the big BIN |
The first four rows and the fifth are underneath the BASIC ROM: they are
pages 0 and 1, which on a normally booted MSX are not RAM at all. Only the
block at 0x8000 lives where you can see it.
And 0x5480, immediately after the compressed maps, is where the decompressor
leaves the current screen's map: 640 uncompressed tiles.
They sit together at the end of the game block:
| address | size | what it is |
|---|---|---|
0xD000 |
24 × 2 | pointer to each screen's compressed map |
0xD030 |
24 × 4 pointers | each screen's behaviour |
0xD0C0 |
24 × 88 bytes | each screen's record |
The behaviour table is this game's nearest thing to a script: every screen
declares up to four routines, and the ones it does not use are not left at zero
but point at 0xFD9A, the sound vector. It is a cheap way of saying "nothing
to do here" without having to test for anything.
The records were delimited by a marker they all carry at byte 84:
FF <screen number in ASCII> FF. The first lands at 0xD114 and the last at
0xD8FC, always 88 bytes apart; that made it possible to separate them one by
one without assuming anything.
This game modifies itself in several places, and it is worth knowing before reading the listing:
0xABB3 writes into 0xAB1A, which is the operand of a ld c,n, to
switch sprite bank.0x864E and 0x8653 patch two operands, those of 0x8603 and 0x8623.0xC645 writes the return address over the
operand of the jp at 0xC65E.Operands double as variables: the tape loader keeps the pointer to the entry it
is reading in 0x81F8 and the chunk's execution address in 0x81FA, and it
writes both before reading them, so the values shipped on the tape do not
matter.
It is not a linear disassembly. The real control flow is traced from the entry points and only what is reached gets marked as code; the rest stays as data and has to be explained.
src/imagen.entries, why
it is an entry point: a pushed return address, a jump table, a self-modified
operand. A test fails if any of them is left without a justification.D directives delimit the data ranges, each with its name and the
width of its structure.make sanity crosses the two: no range declared as data may come out as
code, and no entry point may land inside a data range.The trace was also validated against the emulator: the program counter was sampled over 200 seconds of real play on a Philips VG-8020 and 1,357 distinct addresses turned up. All 1,357 fall inside what the tracer marks as code, not one outside.
(The reverse does not hold: that sampling takes the PC every 0.5 ms and sees a tiny fraction of what runs, so it says nothing about which code is dead. That is measured a different way, in Findings.)
A complete listing is not much use if it says nothing. The series applies a double yardstick:
make densidad
TOTAL 9790 3230 33.0% 1308 0
Nine thousand seven hundred and ninety instructions, three thousand two hundred and thirty of them carrying a line comment: 33.0% density. And the second figure, which is the expensive one: zero routines below 10%, out of 1,308.
The series bar is 22% density and no weak routine. This is one of the densest of the published disassemblies.