Visualizing the GBKJV89 Compression

A couple years ago I went down a rabbit hole trying to see how a grey market video game company made a compression algorithm to fit 4MB of text onto a 1MB Gameboy cartridge - specifically, the King James Version of the Bible. And, before that, how a purpose built computing device did the same thing.

Both of these devices implement algorithms that are either barely documented or had to be discovered via decompilation. They do have some parallels to contemporary algorithms of the time, but are different enough to be considered obscure. With that ind mind, I wanted to see if I could point Claude to my blog posts on the decompiled algorithm for the Gameboy, and the patent for the Franklin device, and see what it would come up with.

Turns out, it did a pretty good job.

Source on GitHub

Live Demonstration

Franklin Algorithm

I also had Claude go read the patent for the device that started this whole journey, and asked it if it could approximate the algorithm. I was very impressed that it was able to come up with something workable, and after a few tweaks, was actually a tiny bit better than the GBKJV algorithm.

The algorithm in the patent relies on storing offsets for the next occurrence of a specific word, and it repeats this process until it reaches the last occurrence of a word, which then points to the lexicon entry for that word. The patent does cover a tradeoff in speed/space for common words - store an offset and lexicon pointer every N occurrences of a word.

So if we start with Genesis 1:

(v) In the beginning God created the heaven and the earth. (v) And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters.

If we look at the worth earth, the first occurrence would store an offset of 5 tokens to get to the next occurrence - the period, a verse separator, "And", and "the". In this short example, the second occurrence of earth would then just point to the lexicon entry.

The patent describes the overall file format, but does not get into the details of the actual encoding of tokens. It describes them as integers, but storing 16 bit or 20 or 24 bit integers would not lead to a sub-1MB file. The algorithm that Claude came up with essentially uses Huffman coding with some lookup tables, an easy algorithm that existed at the time. The patent references a large C code listing in an appendix (presumably available to the public in-person in DC), which may or may not contain a Huffman-like encoder.

If a jump is 5 tokens, and 5 is a very common number of skips, that encodes as a very small value. If it were 52 skips, maybe that is more rare, so that takes up more bits to encode.

The visualization includes lines to show the skips (thanks Claude, I didn't even ask for that).

Visualizing Memory Reads

I had Claude whip up a page that could show what it look to skip ahead to a verse, and to decode the tokens into usable words. Both devices from the late 80's had very slow CPUs and memory access, so even though compression would help to fit data onto the limited memory, one design tradeoff was how many times it would have to scan to different areas of memory. This was even more important on the Gameboy, as it could only work on 32kb chunks of memory at a time (its my understanding that the Franklin device had a 20-bit addressing system, so it could address 1MB of memory at a time).

I've added a book table so at least the devices can seek to the start of a book. They would then have to scan to get to a specific chapter. On the Gameboy, this would mean decompressing every recursive pair, as the chapter and verse makers would be compressed as any other pair of frequently used tokens would. On the Franklin, chapter and verse markers were stored in the clear, but words could be stored as repetitively linked skips that could go as far as the end of the entire stream, meaning reading from earlier in the stream (ie: Genesis) could result in a lot of skips to get to the terminal word token, which could be all the way at the end (Revelations).

Visualizing Search

One of the features I remember from the Franklin device was its built-in stemming algorithm and thesarus. If you typed "love?" into the search, it would stem it and produce "Love, Lover, Lovest, love, love's, loved, lovedst, lovely, lover, lovers, loves, lovest, loveth", and search for those words. If you put a double question mark into the search, it would first expand it using a thesaurus, producing "love, affection, charity, delight", and then run a stemming expansion on all of those words. If you typed in two words, regardless of question marks, it would do an AND query, looking for verses that contained both words. So, having two (or more) double question mark words like "A?? B??", it essentially would look for verses containing at least one of the words from the expansion of A, and at least one of the words from the expansion of B.

We can see that the stream format of the Franklin device was a lot more optimal for searching, using about a quarter of the memory reads as the Gameboy for this example search.

Leave a Reply