Analysis model: gpt-5.5 xhigh, sol high
Last edited: 2026-08-25

# Demo 1 by The Space Pigs - Technical Dissection

`Demo 1` / `SPDEMO1` is a 1990 MS-DOS demo by The Space Pigs. The Hornet
1990 demo index lists `spdemo1.zip` as `Demo 1 by The Space Pigs`.

Release year: 1990

The demo is a good early example of a one-screen EGA/VGA system rather than a
multipart show. It sets BIOS mode `0Eh`, decodes a three-plane picture, loads
a custom 32-row font, expands two encoded text streams, scrolls by moving the
CRTC start address, draws glyphs across several bitplanes over several frames,
and optionally plays a large PC-speaker sample stream through an `INT 08h`
timer handler.

![Decoded SPDEMO1 BILD.PCM still](images/spdemo1-space-pigs-decoded-bild.png)

The image above is decoded from `BILD.PCM` with the demo's own RLE algorithm.
For web readability each decoded scanline is doubled vertically. It is not an
AI-generated illustration and not a guessed redraw.

## Sources

- Hornet 1990 demo index:
  https://ftp.scene.org/mirrors/hornet/demos/1990/00_index.txt
- Scene.org archive:
  https://ftp.scene.org/mirrors/hornet/demos/1990/spdemo1.zip
- Scene.org file page:
  https://files.scene.org/view/mirrors/hornet/demos/1990/spdemo1.zip

## Archive

The examined archive is `spdemo1.zip` from the Hornet mirror:

```text
spdemo1.zip  166455 bytes
```

Hornet's index places the production in the 1990 demo directory. The ZIP member
timestamps are later archive timestamps (`1993-12-27 14:02`), not treated here
as the original release date.

Archive contents:

```text
BILD.PCM       5782 bytes
FONT32.FNT    23040 bytes
SP.SMP       156466 bytes
SPDEMO1.EXE   17101 bytes
TEXT1.COD      4867 bytes
TEXT2.COD      5486 bytes
```

Hashes:

```text
e1ee28b92f07c81ad77d3cba5f0f4143ebcaea26d4d5666dfcf7844d81587ea8  spdemo1.zip
b19860f43314d3037bb509e0f76db438172c69e63cca10346cda74829f3a0a8d  BILD.PCM
db013fc93a24e625682b2d7310397fe4f69c341a66db36769c3a690a828c93a3  FONT32.FNT
0e775fc6f15c28d09dcd7de65a9ce5c3a35829ab200723b6532c8535f5ae2232  SP.SMP
2f1cf4375ba4801bee5f7f59b2a5a816168fd4d0acc91299c7a0a26f300b71dd  SPDEMO1.EXE
05e11064ffc27940ba43a3938a23acdd83e898a3427bef100059c43b3ad6f0e5  TEXT1.COD
c22614875f94f44b112d910bc4cf960ec485a84fd794ce50d727b8faa9b5895a  TEXT2.COD
```

## Executable Shape

`SPDEMO1.EXE` is an MZ executable:

```text
file size       17101 bytes
declared size   17101 bytes
header size       512 bytes
loaded image    16589 bytes
relocations         0
entry           03F7:0010
stack           47BE:0080
min alloc       17337 paragraphs
max alloc       FFFFh
entry file off  4180h
```

The entry is an EXEPACK-style wrapper. The visible packed stub includes the
usual failure string:

```text
Packed file is corrupt
```

A corrected real-mode unpack gives:

```text
packed blocks decoded       26
packed source range         0000h..3F6Ch
unpacked memory range       0000h..47A7Fh
final far jump              loadseg + 472Ch : 0000h
initial data segment        loadseg + 0014h
initial PSP-like segment    loadseg - 0010h
stack after unpack          loadseg : 0140h
relocation entries          12
```

The `loadseg`-dependent relocation words point mostly at the data and helper
segments used after unpacking. In the notes below, offsets such as `0000h`,
`0225h`, or `0650h` are offsets in the final code segment
`CS = loadseg + 472Ch` unless marked as `DS:...`. The main data segment is
`DS = loadseg + 0014h`.

## Runtime Timeline

This demo is not a fixed multipart sequence. It starts with one prompt, then
runs one continuous screen until Esc is pressed.

Observed under DOSBox-X `2026.01.02` with an automated `n` keypress:

```text
0.00s   DOS process starts and prints SOUND(Y/N)? through BIOS teletype.
1.00s   Automated N key accepted; sound flag is cleared.
1.00s+  BIOS mode 0Eh is set, assets are loaded, CRTC/palette state is changed.
after   Main loop runs continuously until Esc scan code 01h is seen on port 60h.
```

The DOSBox-X dummy-video capture preserved the text prompt, but not a useful
main EGA frame. The still in this article therefore comes from decoding the
picture asset directly with the demo's own image routine.

The front-page card uses that decoded still directly. There is no GIF for this
pass because only one useful visual frame is published.

## Visual/Asset-To-Code Concordance

This page is currently asset-backed rather than runtime-frame-backed: the
published visual is a direct decode of `BILD.PCM`, not a screenshot from a
working capture of the main loop. The article still ties the visible artifact
to the executable because the same restored code path that the demo runs is
what explains the still.

Visible artifact:

```text
images/spdemo1-space-pigs-decoded-bild.png
source asset: BILD.PCM
decoder path: far helper at 0560h
display mode: BIOS 0Eh, planar A000h, three decoded bitplanes
web presentation: each meaningful 100-row decoded line is doubled to 200 rows
```

The startup path that produces that screen is compact:

```text
EXEPACK restore
  -> entry at loadseg+472Ch:0000h
  -> SOUND(Y/N)? prompt
  -> BIOS mode 0Eh
  -> ROM 8x8 font expansion
  -> BILD.PCM three-plane RLE decode
  -> CRTC offset/line-compare setup
  -> FONT32.FNT, TEXT1.COD, TEXT2.COD loading
  -> five-phase large-font renderer and CRTC start-address scroll
  -> optional SP.SMP timer-driven PC-speaker playback
```

The image decoder call is the bridge between archive evidence and the rendered
still. The caller passes `AX=0003h`, so the helper decodes exactly three
bitplanes. The helper writes compressed packets into a work area, switches the
VGA sequencer map mask through `01h`, `02h`, and `04h`, then copies 80-byte
rows into `A000h`. Eighty bytes is one 640-pixel 1bpp planar row, and the copy
loop skips alternate rows. That matches the decoded still's 640-pixel width and
the doubled vertical presentation used on the page.

The rest of the article explains what would animate over that base image once
the main loop is running:

- `FONT32.FNT` supplies the large 32-row glyph banks used by the five-phase
  renderer.
- `TEXT1.COD` is read live through the add/rotate decoder at `007Ah`; the text
  stream advances only after the phase-3 tail write.
- `TEXT2.COD` is expanded into the secondary stream at `DS:5B98h`, then used by
  the smaller wave text routine at `0225h`.
- Plane 3 is reserved for the trail/column routine at `0378h`, which explains
  why the picture loader uses only three planes for the base art.
- `SP.SMP` is not visual evidence, but its load/install path accounts for the
  optional `INT 08h` speaker player.

Evidence boundary: there is no published GIF for this page because the current
DOSBox-X capture did not preserve a useful main EGA frame. The still is a
faithful asset decode, not a guessed runtime screenshot. A future animation
should come from a correct DOSBox/video capture of the running main loop, not
from cropping or restyling this still.

## Startup

The real entry starts by choosing the data and video segments, then prints the
sound prompt one character at a time:

```asm
0000  mov ax, loadseg+0014h
0003  mov ds, ax
0005  mov ax, 0A000h
0008  mov es, ax
000A  xor bx, bx
000D  mov ah, 0Eh
000F  mov al, 'S'       ; repeated int 10h teletype calls
...
0037  mov al, '?'
0039  int 10h
003B  xor ax, ax
003E  int 16h           ; wait for key
```

Only `Y/y/N/n` are accepted. `N` stores zero at `DS:E885h`; `Y` stores one:

```asm
0052  mov byte [E885h], 00h
...
005A  mov byte [E885h], 01h
```

After the prompt it enters graphics mode and runs setup:

```asm
0062  mov ax, 000Eh
0065  int 10h
0067  cld
0068  call 03EDh        ; font/palette/picture/file setup
006B  mov ax, 0001h
006E  cmp byte [E885h], 00h
0075  lcall loadseg+472Ch:0650h   ; start sample player if sound enabled
```

Mode `0Eh` is the BIOS 640x200 16-color planar graphics mode. The demo then
treats `A000h` as an EGA/VGA planar framebuffer.

## Setup Routine

The setup routine begins by making a small expanded copy of the ROM 8x8 font.
It reads 1024 bytes from `F000:FA6E` and writes 2048 bytes into the demo data
segment at `DS:0000h`. Each input bit is expanded into two output bits:

```asm
03EF  mov ax, 0F000h
03F2  mov ds, ax
03F4  mov ax, loadseg+0014h
03F7  mov es, ax
03F9  mov si, 0FA6Eh
03FC  xor di, di
03FF  mov cx, 0400h

0402  lodsb
0404  mov cx, 0008h
0407  xor bx, bx
040A  shl al, 1
040C  lahf
040D  rcl bx, 1
040F  sahf
0410  rcl bx, 1
0412  loop 040Ah
0414  mov ax, bx
0416  xchg ah, al
0418  stosw
041A  loop 0402h
```

That ROM-font table is not the main visible logo font. It is used by the
secondary text routine at `0225h`. The large visual font comes from
`FONT32.FNT`.

Next setup calls the picture decoder:

```asm
0429  mov ax, 0003h      ; three bitplanes
042C  mov bx, 1F40h      ; image work offset parameter
042F  mov cx, 270Fh      ; read size
0432  mov dx, 00A0h      ; stride parameter
0435  lcall loadseg+472Ch:0560h
```

Then it programs palette and CRTC state. It detects VGA/EGA behavior through
`INT 10h AX=1008h`, then selects one of two line-compare values:

```asm
044B  mov dx, 03D4h
044E  mov al, 13h
0450  mov ah, 50h        ; CRTC offset = 80 bytes
0452  out dx, ax
0453  mov al, 07h
0455  mov ah, 01h        ; overflow bits
0457  out dx, ax
...
0466  mov al, 18h
0468  mov ah, 0BEh       ; one branch
046A  out dx, ax

0473  mov dx, 03D4h
...
0480  mov al, 18h
0482  mov ah, 5Fh        ; other branch
0484  out dx, ax
```

The important CRTC choice is the 80-byte screen stride (`index 13h = 50h`) and
the line-compare setup. The image decoder writes every other displayed line,
so the CRTC setup and doubled-line behavior are part of how the image is meant
to be seen.

## File Loading

The unpacked setup opens the external files through DOS `INT 21h`.

The visible filename strings in the packed executable are:

```text
text1.cod
text2.cod
font32.fnt
bild.pcm
sp.smp
```

The main setup loads:

```asm
04B1  mov dx, 5B8Bh
04B4  int 21h            ; open font32.fnt
04BE  mov cx, 5A00h
04C1  mov dx, 8E60h
04C4  int 21h            ; read FONT32.FNT to DS:8E60h

04CE  mov dx, 5B77h
04D1  int 21h            ; open text1.cod
04DC  mov cx, 14ECh
04DF  mov dx, 419Eh
04E2  int 21h            ; read first encoded/data table

04E9  mov dx, 5B81h
04EC  int 21h            ; open text2.cod
04F9  mov cx, 18AAh
04FC  mov dx, 74FCh
04FF  int 21h            ; read second encoded stream
```

`FONT32.FNT` is exactly `5A00h` bytes, matching the font read. The two text
reads request more bytes than the files contain; DOS simply returns the bytes
available. The code checks carry for failure, not the returned byte count.

The second text stream is expanded into `DS:5B98h` by a compact add/rotate
codec:

```asm
0508  mov si, 74FCh
050B  mov di, 5B98h
050E  mov byte [si], 20h

0511  lodsb
0512  add al, [5B97h]
0516  test byte [5B97h], 01h
051B  je 0539h

0528  rol al, 1
052A  rol al, 1
052C  rol al, 1
052E  jmp 053Fh

0539  ror al, 1
053B  ror al, 1
053D  ror al, 1

053F  inc byte [5B97h]
0543  cmp al, 00h
0545  je 055Ah
0547  stosb
0548  mov al, 20h
054A  stosb
054B  jmp 0511h
```

So every decoded byte is followed by a space, making the secondary stream a
wide, sparse stream for the `0225h` text routine. The same stateful
add-and-rotate idea appears again in the live main text fetch at `007Ah`.

## Decoding BILD.PCM

The picture helper at `0560h` switches to a helper segment that contains the
`bild.pcm` filename and private state:

```asm
0561  mov ax, loadseg+0E9Dh
0564  mov ds, ax
0566  mov ax, 0A000h
0569  mov es, ax
056C  mov [3AA6h], ax_from_caller   ; plane count = 3
056F  mov [3AA8h], bx_from_caller   ; 1F40h
0573  mov [3AAAh], cx_from_caller   ; 270Fh
0577  mov [3AACh], dx_from_caller   ; 00A0h
```

It opens the filename at `DS:0000h` and reads the compressed image to
`DS:000Dh`. The inner RLE loop is short and exact:

```asm
05B5  mov cx, [3AA6h]      ; outer plane count

plane_loop:
05B9  push cx
05BA  lodsw                ; AL,AH from compressed stream
05BB  cmp al, ah
05BD  je run_packet

literal_word:
05BF  stosw                ; write the two different bytes
05C0  mov ax, [3AA8h]
05C3  shl ax, 1
05C5  add ax, [3AA8h]
05C9  sub ax, 2            ; limit = BX*3 - 2
05CC  cmp di, ax
05CE  jb 05BAh
05D0  jmp plane_done

run_packet:
05D2  xchg ax, cx
05D3  lodsb                ; run length
05D4  xchg ax, cx
05D5  xor ch, ch
05D7  rep stosb            ; repeat AL
05D9  ...                  ; same DI limit test

plane_done:
05E9  mov dx, 03C4h
05EC  mov al, 02h
05EE  out dx, al
05EF  inc dx
05F0  shl byte [3AA5h], 1  ; map mask 1,2,4
05F4  mov al, [3AA5h]
05F7  out dx, al
05F8  mov di, [3AA8h]
05FC  shl di, 1
05FE  pop cx
05FF  loop plane_loop
```

With the caller's parameters, each plane starts writing at `3E80h` and stops
near `5DBEh`. The three decoded plane passes consume nearly the whole file:

```text
plane 0 compressed end offset 09D1h, output end 5E6Ch
plane 1 compressed end offset 0FBDh, output end 5E0Eh
plane 2 compressed end offset 163Ch, output end 5DBFh
```

After the three planes are decoded into the work area, the helper copies rows
from the work area to the displayed framebuffer:

```asm
0612  mov dx, [3AACh]
0616  sub dx, 50h          ; DX = 50h
0619  mov ax, [3AA8h]
061C  shl ax, 1
061E  sub ax, 2            ; final DI limit near 3E7Eh
0622  mov si, [3AA8h]
0626  shl si, 1            ; SI = 3E80h
0628  mov ax, 0A000h
062B  mov ds, ax
062E  xor di, di

0631  mov cx, 50h
0634  rep movsb            ; copy 80 bytes = 640 pixels at 1 bpp per plane
0636  add di, dx           ; skip another 80 bytes
0638  cmp di, ax_limit
063A  jb 0631h
```

That is why the reconstructed image has 100 meaningful rows doubled to a
640x200 presentation. The image uses only three bitplanes; the fourth plane is
left for other overlay work.

## Main Display Loop

After setup, the main loop starts at `007Ah`. It reads one byte from the live
text stream pointer `DS:E86Bh`, adds the evolving key byte `DS:5B96h`, then
rotates in opposite directions depending on the low bit of that key:

```asm
007A  mov bx, [E86Bh]
007E  mov al, [bx]
0080  add al, [5B96h]
0084  test byte [5B96h], 01h
0089  je decode_even

decode_odd:
0096  rol al, 1
0098  rol al, 1
009A  rol al, 1
009C  jmp decoded

decode_even:
00A7  ror al, 1
00A9  ror al, 1
00AB  ror al, 1

decoded:
00AD  cmp al, 01h
00AF  ja draw_glyph
00B1  jb wrap_stream
```

`AL == 0` wraps the stream back to `DS:41E4h` and seeds the key byte with
`46h`:

```asm
00FA  mov word [E86Bh], 41E4h
0100  mov byte [5B96h], 46h
0105  jmp 007Ah
```

`AL == 1` is treated as a delay/control step. The code forces a space, advances
the stream pointer and key byte, and then runs the background update for
`00F0h` retrace-paced iterations:

```asm
00B3  mov al, 20h
00B5  inc byte [5B96h]
00B9  inc word [E86Bh]
00BD  mov cx, 00F0h

00C0  call 0378h
00C3  call 0225h
00C6  wait_for_retrace
00D3  int 16h AH=01h      ; poll only
00D7  in al, 60h
00D9  cmp al, 01h
00DB  je exit
00DD  loop 00C0h
00DF  jmp 007Ah
```

For normal characters, `AL > 1`, it enters the glyph renderer at `010Ah`.

## Five-Phase Glyph Renderer

The glyph renderer sanitizes the character range, maps it to `FONT32.FNT`, then
draws only one phase per frame. This is the main visual trick: one logical
character is not blitted in one call. It is assembled over five phase states
stored at `DS:E86Fh`.

Character to glyph offset:

```asm
010A  xor ah, ah
010C  cmp al, 5Dh
010E  jae force_space
0110  cmp al, 1Fh
0112  jbe force_space
0114  sub al, 20h
0116  xchg al, ah
0118  shr ax, 1
011A  mov si, ax
011C  add si, 8E60h       ; FONT32.FNT base
0120  mov di, [E86Dh]
0124  add di, 7D50h       ; destination relative to scroll phase
```

The unusual `xchg` plus `shr` effectively turns `(character - 20h)` into a
large, word-aligned offset. The font file is organized so the three later plane
bases are exactly `1E00h` apart.

### Phase 0: Plane 0, 32 Rows

```asm
013D  mov dx, 03C5h
0140  mov al, 01h
0142  out dx, al          ; sequencer map mask = plane 0
0143  xor dx, dx

0145  movsw
0146  movsw               ; 4 bytes for this glyph row
0147  inc dx
0148  cmp dx, 20h
014B  je phase_done
014D  add di, 009Ch       ; 160-byte stride minus 4 bytes just written
0151  jmp 0145h
```

This writes 32 rows. Each row writes four bytes, then advances by `009Ch`, so
the vertical pitch is `00A0h` bytes. The same `00A0h` pitch appears elsewhere
in the demo; it is the doubled-line screen layout.

### Phase 1: Plane 1, 32 Rows

```asm
0153  add si, 1E00h
0157  mov dx, 03C5h
015A  mov al, 02h
015C  out dx, al
015D  dec di
0160  movsw
0161  movsw
0168  add di, 009Ch
```

The `dec di` shifts this plane one byte left. At 8 pixels per planar byte, that
is a coarse horizontal interleave, not a per-pixel shift.

### Phase 2: Plane 2, 32 Rows

```asm
016E  add si, 3C00h
0172  mov dx, 03C5h
0175  mov al, 04h
0177  out dx, al
0178  sub di, 0002h
017D  movsw
017E  movsw
0185  add di, 009Ch
```

Plane 2 uses the third font bank and shifts two bytes left. Together, phases
0-2 give the visible glyph a multi-plane color structure.

### Phase 3: Plane 0 Bottom-Up Tail

```asm
018B  mov dx, 03C5h
018E  mov al, 01h
0190  out dx, al
0191  add di, 1FDDh

0197  movsw
0198  movsw
0199  add si, 0004h
019C  inc dx
019D  cmp dx, 10h
01A0  je phase3_done
01A2  sub di, 00A4h
01A6  jmp 0197h

01A8  inc byte [5B96h]
01AC  inc word [E86Bh]
```

This writes only 16 rows and walks upward by `00A4h` after each four-byte row.
It is a separate tail/cap pass, not a duplicate of phase 0. Only after this
phase does the code advance the live encoded text stream.

### Phase 4: Advance Without Drawing

If `DS:E86Fh` is neither 0, 1, 2, nor 3, control drops to the common update
path without writing a glyph:

```asm
01B0  mov bx, [E86Dh]
01B4  cmp bx, 00A0h
01BA  mov word [E86Dh], 0000h   ; if wrapped
...
01C0  call 0378h
01C3  call 0225h
01C6  wait_for_retrace
01C9  inc byte [E86Fh]
01CD  cmp byte [E86Fh], 05h
01D4  mov byte [E86Fh], 00h     ; after phase 4
```

That gives five display ticks per glyph slot: three colored plane writes, one
short tail write, and one no-write update tick. The text stream advances only
after phase 3, so phase 4 is spacing and motion.

### Five-Tick Write Budget

The phase split also gives an exact foreground workload. Phases 0, 1, and 2
each execute 64 `MOVSW`s and therefore read and write 128 bytes. Phase 3
executes 32 `MOVSW`s, reading and writing 64 bytes, while phase 4 writes no
large-glyph pixels:

```text
phase    selected plane    rows x bytes    VRAM bytes written
  0             0             32 x 4              128
  1             1             32 x 4              128
  2             2             32 x 4              128
  3             0             16 x 4               64
  4             -              0 x 0                0
five-tick total                                      448
```

That is 89.6 foreground bytes per ordinary display tick averaged over a full
glyph slot. It is deliberately small beside the fixed plane-3 pass described
below. Phase 3 additionally skips four source bytes after each four-byte copy,
so it samples alternating four-byte groups while walking its 16-row tail
bottom-up.

## CRTC Start-Address Scroll

The ordinary five-phase glyph path finishes by waiting for vertical retrace
and changing the CRTC start address:

```asm
01DA  in al, 03DAh
01DB  test al, 08h
01DD  je 01DAh
01DF  in al, 03DAh
01E0  test al, 08h
01E2  jne 01DFh

01E4  mov dx, 03D4h
01E7  mov ax, [E869h]
01EA  add ax, bx
01EC  push ax
01ED  mov al, 0Ch
01EF  out dx, ax          ; start high
01F0  pop ax
01F1  xchg al, ah
01F3  mov al, 0Dh
01F5  out dx, ax          ; start low

01F6  inc bx
01F7  cmp bx, 00A0h
01FD  mov bx, 0000h       ; wrap after 160 positions
```

This is why so many drawing routines use a `00A0h` pitch. The screen is a
160-byte ring, and the CRTC start address slides through it one address unit
per ordinary foreground tick. The glyph destination uses `DS:E86Dh`, so the
drawing position is coupled to the current CRTC phase rather than to a static
framebuffer origin.

The `AL == 1` delay path is different. Its `00F0h` iterations call `0378h` and
`0225h` and wait for retrace, but never execute the CRTC writes at `01E4h` or
increment `DS:E86Dh`. The small wave/trail layer therefore keeps animating for
240 ticks while the coarse start-address scroll and five-phase foreground
state remain frozen.

Esc handling is not through BIOS return values. The code polls BIOS only to
service the keyboard state, then reads raw port `60h` and exits when scan code
`01h` appears:

```asm
0200  in al, 60h
0202  cmp al, 01h
0204  je exit
```

## Secondary Text And Wave Routine

The routine at `0225h` maintains a separate table at `DS:0800h`, parses the
expanded stream at `DS:5B98h`, and draws compact 8-row text fragments from the
ROM-font table. Its job is not the same as the big `FONT32.FNT` renderer.

It starts by toggling a one-bit phase and computing the current stream pointer:

```asm
0229  mov di, 0800h
022C  mov bx, 5B98h
022F  xor word [4198h], 0001h
0234  add bx, [4194h]
0238  mov ax, [4198h]
023B  and ax, [419Ah]
023F  add [4194h], ax
```

It does not build a table of 40 single words. It builds forty packed 18-byte
records at `DS:0800h`: one destination word followed immediately by eight
expanded glyph-row words. The source position table begins near `DS:0B70h`
and is shifted by the toggled phase and by `DS:4196h`:

```asm
024A  mov si, 0B70h
024D  sub si, [4198h]
0251  sub si, [4198h]
0255  sub si, [4196h]
0259  add word [4196h], 0002h
025E  cmp word [4196h], 00A0h
0266  mov word [4196h], 0000h    ; wrap
026C  mov dl, 0A0h
026E  mov cx, 0028h              ; 40 entries

0271  lodsw
0272  add si, 0002h
0275  sub ax, 00A0h
0278  sar ax, 1
027A  sar ax, 1
027C  add ax, 002Dh
027F  mul dl                     ; row/offset scale by 160
0281  mov bp, [4198h]
0285  and bp, [419Ah]
0289  sub ax, bp
028B  stosw
```

`LODSW` followed by `add si, 2` advances four source bytes per record, so each
pass samples a 160-byte window as forty interleaved words. The arithmetic maps
the prepared source range into rows:

```text
row = ((signed_position - 160) >> 2) + 45
record.destination = row * 160 - (phase & mode_mask)
```

The complete steady table-window union has an exact minimum of 118 and maximum
of 201, mapping to rows `34..55`. This is a lookup-driven wave, with the byte
multiply by 160 turning the selected row directly into the renderer's address
pitch.

The stream has three control cases:

```asm
0294  cmp al, 02h
0298  mov ax, 1000h
029D  mov bx, 0F12h
02A0  int 10h          ; control 02h: palette register write

02A6  cmp al, 01h
02B3  mov word [419Ah], 0000h
02B9  mov word [419Ch], 00C0h  ; control 01h: timed mode toggle

02C1  cmp al, 00h
02C5  mov word [4194h], 0000h  ; control 00h: stream position reset
```

For printable bytes it computes a 16-byte character offset and then uses a
moving row-offset table at `DS:5B72h`:

```asm
02D3  shl ax, 1
02D5  shl ax, 1
02D7  shl ax, 1
02D9  shl ax, 1       ; char * 16
02DE  mov bx, [5B72h]

02E5  add si, ax
02E9  add si, [bx]
02EB  movsw
02EC  add bx, 0002h
...
0323  movsw           ; eight row writes total
```

Those eight `MOVSW`s land directly after the destination word written at
`028Bh`. A complete pass therefore writes exactly `40 * 9 = 360` words, or 720
bytes, covering `DS:0800h..0ACFh`. There is no pointer table followed by a
separate bitmap: every record is self-contained and has this layout:

```text
+00h  word  plane-3 destination row base
+02h  word  expanded glyph row 0
+04h  word  expanded glyph row 1
 ...
+10h  word  expanded glyph row 7
record size: 12h bytes
```

On an ordinary record, the builder reads two position bytes, one stream byte,
eight two-byte row offsets, and eight two-byte glyph rows: 35 data bytes. A
normal 40-record pass therefore has a 1,400-byte source-read baseline and 720
bytes of record writes, before its small state accesses. Only the final record
recognizes stream controls: the `cmp cx, 1` at `028Fh` lets earlier values
`00h..02h` pass through the normal character sanitizer. A final-slot `02h`
changes a palette register and fetches another byte; `00h` resets the stream
position; `01h` enters the timed distortion hold.

During that hold, the row-offset table pointer bounces between `5B02h` and
`5B62h`:

```asm
0335  mov ax, [5B74h]
0338  rol byte [5B76h], 1
033C  test byte [5B76h], 01h
0343  add [5B72h], ax
0347  cmp word [5B72h], 5B02h
034F  mov word [5B74h], 0010h
0355  cmp word [5B72h], 5B62h
035D  mov word [5B74h], 0FFF0h
```

That moving offset table is the small wave/distortion component. It does not
recompute sine; it slides through seven prepared eight-word profiles. The
initial state is:

```text
DS:5B72h = 5B02h   current eight-row profile
DS:5B74h = 0010h   profile-pointer delta
DS:5B76h = 01h     one-hot rotate counter
```

The byte at `5B76h` rotates once per held tick, so its low bit is set only once
every eight calls. The profile pointer consequently advances by `10h` every
eight ticks, reverses at `5B62h`, and returns to `5B02h` after 96 ticks. The
hold timer at `DS:419Ch` starts at `00C0h` (192), enclosing exactly two of
those 96-tick out-and-back sweeps. While the mode mask is one, there is no
profile motion and the stream position advances by one byte on every other
call; while it is zero, the stream position stays fixed and the distortion
timer runs down.

The position-window counter has its own cadence. Restored initial data gives
`DS:4196h = E53Eh`; each invocation adds two and resets only when the result is
exactly `00A0h`. The first reset is therefore invocation 3,505. Because setup
calls `0225h` once to prime the records, that is visual tick 3,504 after setup.
Subsequent position-table cycles are exactly 80 calls. The long initial range
selects a region filled with `00A0h` position words, which maps all records to
row 45 before the normal wave window arrives.

## Plane-3 Trail Routine

Routine `0378h` selects sequencer map mask `08h`, so it writes only the fourth
plane:

```asm
037B  mov dx, 03C4h
037E  mov al, 02h
0380  out dx, al
0381  inc dx
0382  mov al, 08h
0384  out dx, al
0385  mov ax, 0A000h
0388  mov es, ax
038A  mov si, 0800h
038D  mov cx, 0050h
```

The apparent count of 80 is deceptive. Each iteration executes both `dec cx`
and `loop`, so `CX` enters the body as `80, 78, 76, ... 2`: exactly forty
iterations. That matches the forty 18-byte records made by `0225h`. For each
record it loads the destination word, clears four rows, copies the eight glyph
rows, then clears four more rows:

```asm
0390  lodsw
0391  mov di, ax
0393  sub di, cx
0395  sub di, 004Fh
0398  xor ax, ax

039A  stosw
039B  add di, 009Eh
039F  stosw
03A0  add di, 009Eh
03A4  stosw
03A5  add di, 009Eh
03A9  stosw
03AA  add di, 009Eh

03AE  movsw
03AF  add di, 009Eh
03B3  movsw
...
03CD  add di, 009Eh
03D1  movsw

03D6  stosw
03D7  add di, 009Eh
03DB  stosw
03DC  add di, 009Eh
03E0  stosw
03E1  add di, 009Eh
03E5  stosw
03E6  dec cx
03E7  loop 0390h
```

The `009Eh` increment looks odd until the two-byte writes are counted. A
`MOVSW` or `STOSW` advances `DI` by two; adding `009Eh` after it makes the next
row exactly `00A0h` bytes below. Each record is therefore a 16-pixel-wide,
16-row stencil: four blank rows, eight expanded ROM-font rows, and four blank
rows. Because the base picture occupies only planes 0..2, its set pixels add
plane-3's high color bit without replacing the three-plane background.

The doubly decremented counter also supplies horizontal placement. For record
index `i = 0..39`, with `CX = 80 - 2*i`, the first destination is:

```text
DI = record.destination - CX - 79
   = record.destination - 159 + 2*i
```

Successive two-byte glyph columns are thus separated by exactly two bytes;
the loop count and address bias are one mechanism, not unrelated constants.

The complete consumer pass reads nine words per record (the destination plus
eight glyph rows), or 720 source bytes. It performs sixteen word stores per
record: 320 zero words and 320 copied words over the whole pass, totaling 640
word stores or 1,280 plane-3 VRAM bytes. Combined with the normal producer
baseline, one `0225h`/`0378h` pair accounts for 2,120 data bytes read and 2,000
written, 4,120 bytes of memory traffic before stack, state, instruction-fetch,
control-byte, and port-I/O costs.

Setup calls `0225h` once before loading the external text files, priming blank
records. Every animation tick then calls `0378h` first and `0225h` second.
The renderer consequently consumes the previous tick's complete record set;
the builder never modifies a record while the plane-3 pass is reading it.

## Optional Sample Player

The sound routine is a far-call API at `0650h`:

```asm
AX = 0001h  start playback
AX = 0003h  stop playback
```

The start path uses a helper data segment around `loadseg+1248h`, opens
`sp.smp`, reads the sample in large chunks, saves the old timer vector, installs
its handler, programs the PIT, and enables the PC speaker gate:

```asm
0652  mov dx, loadseg+1248h
0655  mov ds, dx
0657  cmp ax, 0001h
065A  je start
...
065F  mov ax, loadseg+124Ah
0662  mov [0010h], ax      ; current sample segment
066A  in al, 21h
066C  mov [0012h], al      ; saved PIC mask
0673  mov dx, 0000h
0676  int 21h              ; open sp.smp
...
0682  mov cx, 0003h
0696  mov cx, 0FFFFh
0699  int 21h              ; read sample chunks
```

The read loop advances `DS` by `0FFFh` paragraphs after each large read:

```asm
069B  mov ax, ds
069D  add ax, 0FFFh
06A0  pop cx
06A1  mov ds, ax
06A3  loop 0685h
```

When `CX == 1`, it starts the read at offset `9EC2h`, matching the final
partial chunk. The sample file is `156466` bytes, so this segmented read layout
is deliberate.

The timer install:

```asm
06AE  mov ax, 3508h
06B1  int 21h              ; get old INT 08h
06B3  mov [000Bh], bx
06B9  mov [0009h], es

06BF  mov ax, loadseg+4791h
06C2  mov ds, ax
06C4  mov ah, 25h
06C6  mov al, 08h
06C8  mov dx, 00E5h
06CB  int 21h              ; set INT 08h to handler alias

06CF  mov bx, 0048h
06D8  mov al, 36h
06DA  out 43h, al
06DC  mov al, bl
06DE  out 40h, al
06E0  mov al, bh
06E2  out 40h, al          ; PIT divisor 72, about 16.57 kHz

06E5  in al, 61h
06E7  mov [0008h], al
06EA  in al, 61h
06EC  or al, 03h
06EE  out 61h, al          ; enable speaker gate/data
06F0  mov al, 0FEh
06F2  out 21h, al          ; unmask IRQ0 only
06F4  mov al, 0B0h
06F6  out 43h, al          ; prepare PIT channel 2 writes
```

The effective sample tick is `1193182 / 72`, about `16572.0 Hz`.

## Timer ISR Inner Loop

The installed interrupt handler starts at the same code bytes as `0735h` in the
normalized disassembly. It increments a sample offset, handles segment wrapping,
then writes to PIT channel 2:

```asm
0735  push ds
0736  push ax
0737  push bx
0738  pushf
0739  mov ax, loadseg+1248h
073C  mov ds, ax
073E  inc word [000Dh]       ; sample offset
0742  mov bx, [000Dh]
0746  and bx, bx
0748  je segment_wrap

074A  mov ax, [0010h]        ; current sample segment
074D  mov ds, ax
074F  mov al, [bx]
0751  shr al, 1
0753  shr al, 1
0755  out 42h, al
0757  xor al, al
0759  out 42h, al
```

This is not just toggling the PC speaker bit. Each sample byte is shifted down
to a 6-bit value and written as the low byte of PIT channel 2, followed by a
zero high byte. The speaker is being driven by rapid timer reloads.

The handler also mirrors a threshold into port `03F2h`:

```asm
075B  mov al, [bx]
075D  mov bx, dx
075F  mov dx, 03F2h
0762  cmp al, 0C8h
0764  jbe low
0766  mov al, 10h
0768  out dx, al
0769  jmp done_port

low:
076C  mov al, 00h
076E  out dx, al
```

Port `03F2h` is the floppy digital output register on the PC/AT. Here it is
used as a crude threshold side effect, likely visible on real hardware as drive
motor/light activity if a controller is present.

Segment wrapping is explicit:

```asm
077A  mov word [000Dh], 0000h
0780  inc byte [000Fh]
0784  add word [0010h], 0FFFh
078A  cmp byte [000Fh], 03h
078F  je restart_sample
0791  cmp byte [000Fh], 02h
0796  je final_partial
0798  jmp 074Fh

final_partial:
079A  mov ax, 9EC2h
079D  mov [000Dh], ax
07A0  jmp 074Fh

restart_sample:
07A2  mov byte [000Fh], 00h
07A7  mov [0010h], ds
07AC  mov word [000Dh], 012Ch
07B2  jmp 074Fh
```

The sample therefore loops with a nonzero restart offset. The code path is
rough but understandable: keep the interrupt short, stream bytes from segmented
memory, reload PIT channel 2 from each byte, and repair the segment state when
the 16-bit offset wraps.

The stop path restores the saved PIC mask, old `INT 08h` vector, old port
`61h` value, and the normal PIT rate:

```asm
0703  mov al, [0012h]
0706  out 21h, al
070A  mov ax, 0000h
070D  mov es, ax
070F  mov di, 0020h
0712  mov ax, [000Bh]
0715  stosw
0716  mov ax, [0009h]
0719  stosw
071B  mov al, [0008h]
071E  out 61h, al
0721  xor bx, bx
0724  mov al, 36h
0726  out 43h, al
0728  mov al, bl
072A  out 40h, al
072C  mov al, bh
072E  out 40h, al
```

## What The Whole Demo Is Doing

At a high level, `SPDEMO1` is one integrated screen engine:

- `BILD.PCM` supplies the three-plane background art.
- `FONT32.FNT` supplies large 32-row glyph planes for the main message.
- `TEXT1.COD` supplies tables/data used by the secondary text and offset
  system.
- `TEXT2.COD` is decoded into the spaced stream at `DS:5B98h`.
- The main stream at `DS:E86Bh` is fetched through a stateful rotate codec and
  rendered one glyph over five ticks.
- `0225h` writes forty complete 18-byte wave/glyph records and recognizes
  control bytes only in the final slot.
- `0378h` consumes the prior tick's forty records as 16-row plane-3 stencils,
  with exact producer/consumer hand-off and no partial-record overlap.
- The CRTC start address scrolls through a 160-byte ring on ordinary foreground
  ticks and freezes during 240-tick control delays.
- The optional `SP.SMP` path installs a high-rate timer ISR and plays the large
  sample through PIT channel 2 and the PC speaker gate.

The result is not a generic blitter loop. It is a specific early VGA/EGA design:
use planar map masks as independent layers, encode most state as byte/word
tables, let the CRTC do the coarse scrolling, and keep the per-frame work small
enough for a 1990 real-mode PC.

## Follow-Up Notes

The most useful next Space Pigs targets are `Spdemo2`, `Vicky`, `Xmas`,
`Spdemo3`, and their tool/player side material. This analysis only covers
`SPDEMO1`.
