Printing Banners on the Sinclair ZX Spectrum (with a receipt printer)
Published: 2023-08-21
This blog post is also available in video form! Watch it on YouTube
I wanna show you this banner printer I made! It's not a very complicated system, except for one really specific part. Which of course means that's the part I want to talk about.

Before we get there though, let's have a look at the setup.
The Setup
There is:
- a Sinclair ZX Spectrum, from 1982.
- a cassette deck, of course.
- a Kempston Centronics interface, for connecting to a printer, also 80s era.
- And finally, a modern thermal printer.

Now, that last one is quite an anachronism, I know. So this isn't quite "how to print banners like it's 1982", but Sinclair's printer for this thing is incredibly slow, and it's hard to get the paper for it now. Also I don't have one anyway. So for now we get this.
I would love to see this work on a Sinclair printer, and it would be possible to do it, but actually the interesting part of making this work was interfacing with this modern printer fast enough to print banners at the printer's maximum speed.
Okay, so why was this hard? Sounds like an easy project right? just print the right sequence for each letter. What is this mysterious complicated thing I mentioned? well, let me show you version one of this system.
A simple, big, slow solution
So I want to print the letter 'A'. maybe I'll do that with some LPRINT commands, and use spaces and hashes to make empty pixels and filled pixels.
I just have to have a subroutine that looks like this:
935 LPRINT " # "
936 LPRINT " #### "
937 LPRINT " ####### "
938 LPRINT " ######## "
939 LPRINT " ######### "
940 LPRINT " ## ##### "
941 LPRINT " ## ## "
942 LPRINT " ## ##### "
943 LPRINT " ######### "
944 LPRINT " ######## "
945 LPRINT " ####### "
946 LPRINT " #### "
947 LPRINT " # "
948 RETURN
Now I need another one, for the letter B, and another one for each other letter I want to be able to print, and all the punctuation, etc.
I did start manually writing these, but quickly realised that was a silly idea, and generated all the code for this from a font I had on my laptop. This works fine, except, it took nearly three whole minutes to load the resulting program from the tape. It's a loooong program.
Making it shorter, better, faster, stronger
So, the program takes a long time to load. It's slow to print, and also the banners are not particularly readable from a distance. The contrast between the filled and unfilled pixels is not great. We can solve both of these problems at once.
There's a lot of repetition in this file. Lots of lines that start with LPRINT, lots of long runs of spaces and long runs of hashes. It's clearly very compressible. In particular, the only things we really care about here are the image data, and the boundaries between the images.
The current solution uses an entire byte for each pixel, but we only have two colours anyway, so we want to reduce this to just one bit per pixel. So in this section, for the letter 'A', we've used 369 bytes to represent the image, but at one bit per pixel, tightly packed, the bitmap would only be 35 bytes long.
Clearly this first approach is very wasteful and we should switch to a packed bitmap format. However, switching to this tightly packed format produces another problem. Previously, the image data was encoded exactly how the printer was expecting it. It was already a stream of characters that just get sent to the printer as-is. Now we need to do an extra step before we can send it. We need to convert the bitmap into a stream of printable characters.
We're gonna have to do some bit manipulation on the data, to extract each pixel bit by bit and generate the characters on the fly. Unfortunately, Sinclair BASIC doesn't have bitwise operations. They can be constructed from other operations that are present, but it's much faster to just use machine code. Most of the program is still BASIC though, just the expanding of the font data will be done with machine code.
Since I was going to this much effort, I also decided to add some compression.
Take a look at this letter L. It's three identical lines, and then seven identical lines.
1067 LPRINT " ############## "
1068 LPRINT " ############## "
1069 LPRINT " ############## "
1070 LPRINT " ## "
1071 LPRINT " ## "
1072 LPRINT " ## "
1073 LPRINT " ## "
1074 LPRINT " ## "
1075 LPRINT " ## "
1076 LPRINT " ## "
1077 RETURN
It would be nice to have a way to say "repeat this line this number of times" and save repeating the same few bytes over and over. Turns out there's a really neat way to do this.
The width of the printer output in the mode that I'm using to print these banners is 21 characters. That means that I need 21 pixels per line. A byte has 8 bits, and therefore 8 pixels, so I need three bytes before I have enough bits for an entire line. That's 24 bits. So I have three bits spare before the end of the third byte after I've encoded the first line of pixels.
I could start the next line in those three bits, if I wanted to, but actually I chose to keep all the lines aligned to 3 bytes, and to use those three spare bits for something else.
Two of these bits are going to encode a number. This number is in the range 1 to 4, and it's the number of times to print the line. Of course, a two bit binary number normally has the range 0 to 3, and so I add one during decompression to get a number in the range 1 to 4. The last bit tells us if this is the last line of the bitmap. This lets us know where to stop printing each character.
first byte
---------------------------
| 7..0 |
| bitmap |
---------------------------
second byte
---------------------------
| 7..0 |
| bitmap |
---------------------------
third byte
---------------------------
| 7..3 | 2 | 1..0 |
| bitmap |stp| reps |
---------------------------
So the decompression routine builds a string representing the current line, calculates the number of times to repeat it, then sends that string to the printer the right number of times. Finally it checks if it has reached the end of the bitmap. If it has then it is finished printing this character, if not, it does the same thing again on the next three bytes.
One huge advantage of this approach is that I can use any pair of characters I want for the light and dark pixels with no extra cost. Previously I could only use characters that I could type on the spectrum keyboard. This printer has support for a character that is just a filled rectangle, it's perfect for this purpose. I use that character, and the space character, and get a much better contrast than before.

This loads soooo much faster too. We're down from 160 seconds of load time, to just 40 seconds. And now, since the font data is just a bitmap somewhere in memory, rather than a bunch of subroutines in a BASIC program, I can easily load other data in its place. So, naturally, I converted all of the fonts on my laptop into this format, and I loaded them all on to one big cassette tape.
It's a 90 minute cassette, and I did my best to fill it up, but only ended up with about 70 minutes of font data. I bet there's not many other people in the world that have 70 minutes worth of font data stored on a compact cassette.

I should have filtered through the fonts I had on my laptop, because actually lots of them don't even have the characters I care about and just rendered placeholders instead. But I had space to spare on the tape anyway, so it's not really a problem.
I also added in a name badge printing thing, because why not? And now I have possibly the silliest banner printing system there has ever been.