GlurkRLE: A ROM-Friendly Compressor Tuned for Graph2Font Screens
In September 2021 the AtariAge programmer glurk released GlurkRLE, a small compression scheme with an 80-byte 6502 decompressor. It is designed for the screen and font data made by Graph2Font (G2F), the Atari graphics converter and editor. He noticed that the .SCR screen files G2F writes contain long runs of the same byte and, even more often, runs of consecutive bytes ($06,$07,$08,$09…). Ordinary RLE handles only the first case. GlurkRLE handles both.
The format
The packed stream is a series of one-byte commands, each followed by its data:
| Command | Meaning |
|---|---|
$00 |
End of stream |
$01–$3F |
Repeat the next byte n times |
$41–$7F |
Copy the next n−$40 bytes literally |
$81–$FF |
Write the next byte n−$80 times, adding 1 each time |
So .BYTE $88,$06 expands to 06 07 08 09 0A 0B 0C 0D. A whole row of a G2F screen can shrink to a couple of bytes.
Results
On one of tebe's G2F screens (a 1,200-byte .SCR file), glurk's hand-encoded version came to 253 bytes, about 79% smaller. A plain RLE packer only reached 994 bytes on the same file. A screen consisting of the sequence $00–$77 repeated ten times went from 1,200 bytes to 21. Fonts compress less well, depending on their contents.
Built for cartridges
glurk wrote the decoder for a cartridge project, so it uses no self-modifying code and can run straight from ROM. It is relocatable and uses five zero-page locations: source pointer, destination pointer and an "incrementer" byte (that last one doesn't have to be in zero page). One path uses the undocumented ASR opcode ($4B, AND followed by LSR) to save a byte and two cycles. The release includes the 6502 decompressor and a compressor written in QB64 BASIC. Another forum member fixed two bugs in that encoder and posted a Perl version as well. Those tools reach 262 bytes on the test screen, a little more than the hand encoding.
Why it matters
Shorter, faster depackers already existed. tebe posted one in the thread, but it saves space by modifying its own code, which can't work from ROM. glurk also hadn't seen another scheme that encodes rising runs. A cartridge game with many G2F screens can use GlurkRLE to save space with a tiny decoder that runs in place. In the thread, ivop pointed out that the same idea can be seen as delta coding plus RLE: rising runs become runs of 1.
Getting it
The decoder source and QB64 encoder were posted as GlurkRLE.zip in the "Graph2Font RLE" thread in AtariAge's 8-bit programming forum, and the improved encoders as rle.zip. For comparison, tebe pointed readers to the Mad Team's RLE Encoder 1.2, a standard RLE packer.
Credits
- AtariAge — Graph2Font RLE — AtariAge forum members
- AtariAge — Graph2Font RLE (format, ROM-safe decoder) — AtariAge forum members
- AtariAge — Graph2Font RLE (encoder release, compression results) — AtariAge forum members
