Project Mehitabel: C Defying UNIX

Last time on Project Mehitabel, we wrote some programs in BBC BASIC and ran them out of the Filer or in the ShellCLI mode that lives outside of the desktop. This time, we’ll make some programs that run as machine code, and run them in the final type of text interface RISC OS provides: the task window. There’s nothing fancy here in terms of code; we’ll just be writing Hello World

What you’re really here for this time is to laugh at my suffering.

The design decisions RISC OS made long ago, and the ways that the community around it addressed those decisions to interoperate with the broader world, all have very ugly sharp corners. We’re going to make things work here, but this will also largely serve as a demonstration as to why what development there is for this platform is normally done with cross-compilers.

Step 1: Prep work

The first thing we need to do is run the !GCC app. (App bundles in RISC OS are directories with exclamation points as the first character in their names. There’s stuff that needs to be in the directory too, but the general mechanism is not unlike app bundles in the UNIX-based macOS versions.) We navigate to it through the Filer first by clicking the SD Card image in the icon bar and then navigating through to the appropriate directory:


As a collection of compilers and ancillary data, of course, gcc is not really an app; running it does nothing obvious and leaves no processes running. What this has done is extended the execution environment so that when we go into command-line mode it will be able to find the programs we need, and those programs will be able to find the data they need. RISC OS is notionally a co-operative multitasking system like Classic Mac OS or Windows 3.1, but there’s a very large amount of shared OS state across all applications. By setting environment variables, every program will see those changes immediately. That will include our command line.

But we aren’t ready to fire up the command line yet. As you may recall from last time, when we were playing with BASIC, we were handed a chunk of 640KB of RAM to work with. This was not a quirk of BASIC; it was the OS that allocated that memory to us. We won’t be able to run gcc in 640KB. To address this, we’ll left-click the little Raspberry Pi logo in the lower right. This calls up the Tasks Window:


This gives us a nice breakdown of memory usage in the system, including the two applications that are already running. (“Pinboard” is the application that manages the desktop icons, and “Snapper” is the screen-capture utility I’ve been using to illustrate these posts.) The green bars are fixed. The red ones are not. We can grab the “Next” bar with the mouse and drag it out to give the next program we start a bunch more memory. 64MB should do.

That done, we then middle-click the Raspberry Pi icon to open its context menu:


Most things in RISC OS can be middle-clicked to pop up a context menu, and you’ll need to do that in most apps to get anything done. As you’ve seen, it’s not like there are menu bars in these windows. As the menu shows, we could also hit Ctrl-F12 to open a new Task Window instead. That makes sense, because F12 alone is what suspends the desktop to put us into a CLI. Task Windows are text-only CLIs that live in a text editor window and are fully integrated with the CLI. If you aren’t working with non-GUI graphics modes, this is usually the way you’re going to interact with command-line applications.

Cynical and/or paranoid readers will notice that Shift Control F12 does nothing at all related to command lines and in fact powers down the system as immediately as possible. Don’t fat-finger that control-key, kids!

Anyway, with the Task Window created, we can dial back down the “Next” bar, and confirm that BASIC is getting allocated way more memory than it was before, and that we can run gcc inside the Task Window:


Everything that we have done to this point has just been so that we can get the compiler to run at all. A lot of Unix ports fit uneasily into RISC OS’s operational model, but this is enough to make you long for the simplicity of EDIT.COM and the Turbo C command-line build tools on DOS.

But that work is done! Let’s write some actual code. This part is also incredibly alien to anyone that’s used pretty much any other GUI-based OS, including that one X11 window manager written and configured in Haskell that doesn’t believe in the desktop metaphor.

Strap in, folks. This is the part that isn’t supposed to be wonky and weird.

Continue reading

Demo Release: Coast to Coast

I recently collaborated with the musician Nick Vivid on a Commodore 64 demo named “Coast to Coast” that was presented at @-Party 2017. It seems to be traditional to do writeups of one’s demos after the fact, but even if it weren’t, I’d be doing one. I’ve held off until the actual first presentation, though.

The Effects Used

The core techniques used here will start out looking pretty straightforward. There’s some text scrolling at the bottom of the screen, and the main part of the screen is consumed by text scrolling down at one pixel per frame. Then the 8 sprites are being used to cover the top of the screen with an additional marquee of scrolling text. (It take all 8 sprites to do this while keeping each letter static within the sprite; X-expanded sprites are 48 pixels wide, and the screen is 320 pixels across. So you need 368 pixels of space to scroll over the screen, but 7 only gives you 336.)

There’s nothing at all exciting about the visuals on this effect if you’ve been following this blog, but if you’ve been following this blog you also know that single-line splitscreen is a bit harder than it looks. Back in 2015 I didn’t really deal with the problem of scrolling the color RAM, either. I don’t address that here, either; I was just careful about my choices of colors.

Things get more fun about 20 seconds in, when I reveal that I wasn’t actually using the technique I just linked:


At this point I begin scrolling graphics up the screen as well as a down-scrolling background. Part of the reason I stuck to the simple sprite text scrolling was to show that I wasn’t doing this the easy way with sprites; this is a purely textual effect. The trick here is that while it looks like I’m scrolling one scanline down each frame, I’m actually using a much simpler technique to scroll up two lines every frame. The downscroll effect is created by combining that with rotating the character graphics down three lines each frame.

Of course, to get the objects that move up to actually move up, I also had to “coarse scroll” the screen up by one character row every four frames. This was handled via the traditional C64 technique of double-buffering the screen, spending the intervening four frames copying over all the characters, and then updating the video-matrix pointer as needed. I had a little jump table that would let me generate the map I was scrolling through a line at a time, and threading values through that jump table turned into a little miniature scripting language. So that was fun.

I did end up being a little more clever than I perhaps needed to be there. I was writing this while Nick Vivid was working on his music, and I didn’t know how expensive the music update would be each frame. I thus ended up restricting my coarse-scroll effects to the bottom line and then a selected (and variable) column of characters on the screen itself. In the end, I think this probably was overkill, but better to do the work and not need it than the other way around.

Speaking of the music, I had very little to do with that at all. He worked on his own with his musicmaking tools, produced a very nice piece, and I asked him if he could export it so that it would run only using RAM over location $3FFF. (The graphics and logic all live completely within the first 16KB, even uncompressed, even at runtime.) That was easily arranged and all I needed to do from there on out was copy it into place and then call into it at appropriate times.

With all the pieces together, all I did after that was run it through the PuCrunch system. This essentially turns a C64 binary into a self-decompressing, self-running archive. The home page has been gone for awhile, and while the code is still out there on the Internet, The Wayback Machine is the only way I can link the original site. PuCrunch was unreasonably effective at compressing this program; it dropped in size from about 12KB to less than 6.

Challenges, Compromises, and Goofs

Graphically, the biggest issue that I had was that I really, really did not want to have to wrangle color memory with things scrolling in three directions at once. I set multicolor text mode once, filled the entire screen with a single color, and then designed everything else around that. This meant that my scrolltext color also had to be in the range 8-15, and also had to be exactly the color 8 above whichever color I used for the text’s per-cell color. I wasn’t allowing myself any slack in the display, so that means (as we saw in the original vertical scrolling articles) that the scroll text’s color values would be shared with the last row of displayed graphical cells. Our change in scroll values would force a re-read, but the badline would come too quickly for us to do any reassignment. So I stuck with solid colors, and red/pink produced a reasonably nice display.

Conceptually the biggest problem that I had was that just scrolling stuff is kind of boring. Since one of the other goals of the demo was to be the invite for SynchroNY 2018, and that particular party takes place on a train that runs from NYC to Montreal, the train theme was set in pretty early. Discussions with Nick Vivid while we were talking about progress sort of developed the idea of it being about traveling in general, and once “remote entry to @-Party” was decided upon as the best venue to release in, that also brought in the notion of travel as a theme. The demo was being written, roughly, between SF and NYC, and about an event that would travel from NYC to Montreal. That’s when I got the idea of running the city names as extra text from, and it wasn’t too hard to hit enough major cities on one route to fill the time the music took up.

Unfortunately, those cities aren’t on an actual train route. If you want to take a train from the West Coast to NYC, you need to start in Los Angeles. I used Interstate 80’s route to select my interstitial cities.

An additional goof, which fortunately was caught before release, was that I ended up bungling the copying of the music code into place, and as a result a full third of it was originally missing. I think the three most devastating bugs that emerged during development and testing all ultimately were single-byte errors.


At the actual compo, the demo placed second, coming in behind an extremely elaborate demo for the Intellivision that is the most impressive work I’ve seen on the platform. Comments on it after the fact can be summarized as “not bad for a first effort”, which is about where it belongs. I’m not using any incredibly advanced techniques here and I’m not particularly talented as a graphician, either—the part of this project I like the most is that with only one extremely arguable exception, I’m getting an “impossible”-looking effect out of this without actually pushing the system’s limits. (The exception is that I do have to compensate for some of the VIC-II’s slightly inconsistent behavior surrounding vertical split-scrolling. I think that can count as “compensating for a quirk” rather than “pushing the envelope.”) This also let me put a lot of the small, independent routines I’d developed over the years into a single coherent program, and that was nice too.


Here is the disk image that was sent to the competition.

Project Mehitabel: The (BBC) Basics

We’ve now got a RISC OS system up and running, and that also means that we’ve got our two most important applications ready: a web browser and Minecraft a text editor and Minesweeper:


Traditionally, one would program a RISC OS system in BBC BASIC, a dialect that originated on the BBC Micro and proceeded to evolve through the Archimedes line. It split off a number of times and has dialects that run on today’s more modern operating systems—the brandy system is the one I’ve found on most Linux machines—and while this isn’t a complete fool’s errand, BBC BASIC is primitive enough that I don’t think it would really be worth the effort to make new software in it for Linux or Windows.

That’s not to say that it’s not an interesting language, though. It’s capable of simply being used as a slight dialect of the early microcomputer BASICs that we’ve seen here before. It’s got its own quirks around file access and graphics, but most importantly, it’s case sensitive. Keywords and functions need to be in all capital letters, and because variables with capital letters often have important meanings to the system, accepted practice is to keep one’s own variables in lower case. We can see a copy of Hammurabi in my edit window up above.

We have several options for actually running the program. The one that is the most familiar to us would be to get into a command mode, start BASIC, and then LOAD and RUN our program. We can suspend RISC OS’s GUI and go into a command-line mode by pressing F12. This puts us at a command prompt not unlike a DOS prompt, from which we may start BASIC and then proceed as usual. Hilariously, as we do this, the GUI begins to scroll off the top of the screen:


Once we’re done, we enter QUIT at the BASIC prompt and return to the GUI by hitting ENTER at a blank line.

We don’t actually have to manually enter BASIC to do this, though. One of the advantages of the file metadata here is that the Hammurabi program has a file type of “BASIC program” (&FFB—RISC OS uses the ampersand to indicate hexadecimal constants) and as such we can simply type Hammurabi at the ShellCLI prompt and be in our program.

We can also run it without leaving the GUI by double-clicking the program in the Filer (what RISC OS calls the Explorer or Finder). Hammurabi isn’t a GUI program, so this still takes over the desktop, but it does it in a somewhat more friendly way:


That window looks like it should be movable around the desktop, but it actually isn’t. The mouse doesn’t do anything useful while you’re running a program in this way. If you really want to properly integrate with the GUI, and you’re a purely-textual application, the right thing to do is to run it in a “task window.” We’ll get to those in a later post, once we start working with C.

But BBC BASIC is actually much more sophisticated than running a reformatted version of the C64 BASIC Hammurabi suggests.

Continue reading

Kicking off Project Mehitabel

Sorry for the radio silence, folks. I’ve both had less time to work on hobby projects of late, and the projects I was working on weren’t the kind that I could write up as they went. Also, I’ve pushed my knowledge of how the systems of my youth worked to the point that it feels like all else is commentary.

That ends up being pretty demotivating for a blog like this. Except for the DOS work, all the projects I’ve written about on this blog have involved 8-bit systems. One of the main reasons for that is that they feel like “lost technology.” The capabilities each has are unique and they are all programmed very differently, even when they use the same chip. DOS lives a bit in that space too, slowly growing out of it as you move into the Windows 9x era. On the other hand, once you start looking at the 32-bit systems—and remember, the 32-bit era begins in 1984 with the Macintosh, followed up over the next three years by machines like the Amiga, the Atari ST, and the Acorn Archimedes—it feels less like you are dealing with unique relics the likes of which the modern world has never seen, and more like dealing with modern systems that are heavily stripped down. That’s a fundamentally less interesting question to me, and boils down to constrained use of the same dev tools we’ve been using in the UNIX world for 40 years or more.

If you’re interested in early GUI programming, these are a gold mine, of course, and the skills you’ve picked up as a systems programmer in the modern world suddenly end up applicable to surprising machines. I can see that being fun too, but without any history with any of these systems, there isn’t much of the same kick to it.

But there’s one fun little wrinkle on this. You know what else is basically a modern system that’s been heavily stripped down?


A Raspberry Pi, that’s what. You can make a very strong case that the Pi is the true inheritor of the Acorn Archimedes and as such the BBC Micro:

  • It’s very British.
  • It was originally intended for primarily educational purposes.
  • In particular, it was intended to be programmed by its end user.
  • It’s based on the same chip series as the Archimedes and its successors.
  • RISC OS, the operating system and GUI that was released alongside the Archimedes and which continued through its successors, was ported to the Raspberry Pi within a year of its launch.

I recently got a Pi 3. Building a RISC OS system and writing some software for it—or porting some software to it—sounds like a fun thing to do. I’ve named this “Project Mehitabel” after the name of Archie’s old companion, and because getting this all to work has been a bit like herding cats.

Continue reading

How to Not Learn Assembly Language in DOS, Part 2

Last time, we talked about writing memory directly and invoking interrupts. This time, we’ll cover writing I/O ports directly and writing our own interrupts.

The Project

Our first draft of the Smoking Clover program relied on the delay() routine in Borland’s DOS extensions to control animation speed, and used BIOS interrupts to rewrite the palette. This produces very inconsistent animation depending on CPU speed. In this article we will reorganize the animation to be governed by IRQ0, the timing interrupt. This also means that, since we’re changing the palettes inside an interrupt, we can’t use interrupts to do it—we’ll need to use the VGA chip’s I/O ports to alter colors. We’ll also need to actually properly program the timer. In the end, we should also have a reusable timer callback system.

Code begins below the break.

Continue reading

How to Not Learn Assembly Language in DOS, Part 1

After a couple of weeks of ZX81 work, let’s jump ten years forward in time and port Bill Gosper’s Smoking Clover effect to DOS and VGA. Basically every description I’ve seen of this, including the one on Wiki, is some variation of the minimal definition in ESR’s old Jargon File:

Many convergent lines are drawn on a color monitor in such a way that every pixel struck has its color incremented…. The color map is then repeatedly rotated.

The latest edition of the Jargon File actually specifies that the lines are drawn such that one endpoint is in the middle of the screen and the others are one pixel apart around the perimeter of a large square. This is more or less what I attempted when I first implemented the effect back in high school—but doing this turns out not to produce the clover effect. You get a more tunnel-like effect instead.

What worked for me was to have my endpoints march, exactly pixel at a time, around a circle much larger than the display screen. This in turn isn’t exactly the same thing as points equally spaced along the perimeter of a circle, either—what I ultimately did was solve x2 + y2 = r2 for successive values of x, in exactly the range where x <= y. I then reflected that line across the X and Y axes as well as the line y=x. This finally produced the result I wanted:


Somewhat unusually for this blog, this program is actually written with no use of assembly language whatsoever. Back when I was exploring the HAT function, I demonstrated how to invoke interrupts and memory-mapped I/O in Turbo Pascal 5.5.

In this article I will bring C up to speed. Like my earlier work here, this will be written using Borland’s Turbo C 2.0.1, still available free of charge from Embarcadero Software.

What We Need

So, aside from a perverse breed of bragging rights, what does assembly language buy us, anyway? Mostly, what it buys us is direct access to the hardware:

  • We can trap to the operating system, firmware, or hypervisor. In DOS we do that with the INT instruction. We’ve used this all over our DOS work here, for everything from changing graphics modes to printing messages to exiting the program.
  • We may directly read and write video memory or other kinds of memory-mapped I/O. In DOS, we’ve used this just to control graphics memory—on the 6502-based systems, it’s how we controlled all our peripherals.
  • We may directly read and write the I/O ports. Not all chips have these—the 6502 and its cousins in particular do not— but they figure heavily in both the Z80 and the x86 line of chips. We didn’t touch the I/O ports on the ZX81, but we’ve used them in DOS to produce nonstandard CGA modes and to program the Adlib and Sound Blaster chips.
  • We may override the operating system’s own interrupt vectors and provide our own. We used this when we coerced digital sound out of the PC speaker.

All of these things are possible within Borland’s HLLs. In this article, we’ll focus on the first two, and leave the last two for another time.

Continue reading

Lights Out: ZX81 Release and Design Notes

So. On to my ZX81 program – an implementation of the Lights Out puzzle. This is one of my go-to simple programs for putting an interactive system through its paces.


As such, this is not the first time I’ve implemented the puzzle—I included C64 implementations on the first Bumbershoot collection back in 2015…


…and quietly included source for a DOS port that was otherwise not distributed…


…and after I’d worked out the basics of linking directly against the Windows layer in Hello World 4 Different Ways I also quietly did a Windows console port based on the DOS edition.


Despite all that, I’ve never really talked about implementing it. So let’s talk a little about the puzzle and how implementation strategies differ when developing for an old home computer compared to a more powerful system.

The general design

The puzzle itself is easily modeled as a 5×5 rectangular array of boolean values, with a move that selects an index and toggles its neighbors in the four cardinal directions, if they exist. A modern implementation would separate the model (this 5×5 array and operations upon it) from the view (the actual display of the puzzle).

As it happens, none of my implementations do this. Because in each case the display is text on the screen, the screen’s display of the puzzle itself is used in place of the 5×5 array. Moves directly manipulate the screen memory and the puzzle state is read by consulting it.

So despite being implemented four ways for four computers using four different instruction sets, all four implementations are broadly similar:

  1. The static parts of the display are drawn. This includes the title, the puzzle board in solved state, and some kind of message area at the bottom of the screen. If we need to do something special to have a screen to draw on, that also happens here.
  2. Generate a random but solvable puzzle.
  3. Read the keyboard and execute the requested move. (Alternately, if the user has requested a reset, go back to step 2, and if the user has requested to quit, proceed to the final step.)
  4. Check to see if the puzzle is solved. If it’s not, go back to step 3.
  5. Congratulate the user on a solved puzzle. Ask if they want another game, and if so go back to step 2.
  6. Clean up the screen and return to the context from which the program was invoked.

Let’s take each of these in turn.

Displaying the board

This is, at the end of the day, a bunch of fancy print statements. Different platforms handle things like colors and the edges of the screen differently, but the simplest approach usually involves blitting things directly into screen memory.

Generating Puzzles

I accomplish this by executing a thousand or so random moves. This is not the most efficient way to produce a guaranteed-solvable Lights-Out puzzle but it does produce a nice visual effect of the puzzle being scrambled. (Since making the same move twice perfectly undoes the move, the optimum way to generate a puzzle is to flip a coin for each of the 25 spots and execute a move on that spot if the coin comes up heads.

Making Moves

All my implementations use roughly the same algorithm for this. Each letter appears on the screen as the center of a notional button to press; I have a routine that computes the location of each letter within the screen memory. I can then examine screen memory at that location to flip the light there. I can also then compute the addresses of its neighbors to the north, south, east, and west. In each implementation the board and screen are designed such that moving off the board hits a point of valid screen memory that does not have a letter in it—this means I don’t need to bounds-check my moves but instead can simply see if there’s a letter at the point of interest.

Determining if the user has won

This is the same algorithm across all implementations. Go through each letter from A through Y, compute the part of screen memory that cell resides in, and examine it to see if it’s on. If it is, then we know the puzzle is yet unsolved. If we make it all the way through the loop, then we know that victory has been achieved.

Platform-Specific decisions

Making the program fit the platform requires more decisions than just the ones above, of course. I’ll run through these in the order I implemented them.

Commodore 64

This was the first implementation. As a result, it matches my description above almost exactly, with no additions or compromises. The board is drawn with character graphics, and the character cell with the letter in it is the only one that changes. If a letter is on or off, it is shifted in and out of inverse video, and then the color memory is independently changed to match. “On” was light red inverse video, and “off” was dark grey on a black background. Most of the rest of the text was in a lighter grey, but not quite reaching full white.

Random numbers are generated by calling out to the BASIC ROM, which is a bit opaque but is also very compact.

This is a reasonably compact program, weighing in at 977 bytes. It could be ported to the VIC-20 simply by altering a few constants.


The character graphics on the IBM PC lend themselves more readily to designing displays like this, and with 80 columns to work in the result here feels the cleanest of all my implementations.

DOS (more properly, the PC BIOS) does not really have a notion of “inverse video,” though. Instead, background and foreground colors are encoded separately alongside each letter. This means that the solution for displaying lights as on or off ends up being rather more elegant, because we need only adjust or inspect these color attributes to make our moves.

The random number generator here is a simple linear congruential generator that lets the x86 chip handle the multiplication.

The DOS version is a .COM file that is the smallest of the implementations overall, at 812 bytes.

Microsoft Windows (Windows 2000 or later)

The core logic here is mostly an adaptation of the DOS implementation. However, working with the console is complicated because there’s no guarantee that the window will have a display large enough to hold the board in it. The Windows Console API addresses this by allowing the developer to allocate and provide their own text buffer. This buffer cannot be reliably accessed directly—there are API functions to do so instead—but it behaves in a manner roughly analogous to the old PC BIOS color text mode. Like the DOS implementation, we only ever read or write color data (“attributes”, as the API calls them) after drawing the initial board.

The Windows Console is also a Unicode environment, so to get access to our box-drawing characters and such all of our strings are represented as UTF-16. Fortunately for us, NASM has a convenient macro for that.

The end result is a reasonably faithful translation the DOS implementation into 32-bit x86 code (notable mainly for shortening the RNG routine from 24 instructions to 10) and replaces all reads of screen memory and syscalls to BIOS or MS-DOS with Windows ABI calls. (I give simpler examples of how to do this in my old article about Hello World four different ways. The end result depends only on kernel32.dll, but the alignment requirements for a Windows executable make this the largest of the programs, weighing in at 5,120 bytes.


The game logic here is largely the same as the other three implementations, but the display logic is almost entirely different. The general C64 trick of relying on inverse video for a letter to represent a light’s status has been kept, but the ZX81 doesn’t have the kind of box-drawing graphics characters that any of my previous platforms used. Instead, it offers sixteen characters which fill in the four quadrants of the character cell in all possible ways, and then a somewhat more restricted dithered-grey.

My solution to the display of the board, then, was simply to not draw any walls at all—an “off” light is a 3×3 grid of inverted spaces with the letter in inverse video in the center, and an “on” light retains a half-character-cell-wide black border but leaves the letter in normal video. I am actually very, very happy with how this looks, and if I had done this before the DOS and Windows versions I’d have seriously considered using this rendering technique instead of what I actually went with.

This does make the actual “flip” operation more expensive, since instead of writing three color bytes (DOS, Windows) or one character byte and one color byte (C64) we need to instead write nine bytes scattered across screen memory. This cost is more than offset by the fact that the initial screen draw is now just a matter of filling the whole screen with inverted spaces.

Filling the whole screen with inverted spaces—in effect, having the program provide white text on a black background—also has the happy side effect of ensuring that every line is its full fixed length long. That makes computing the addresses of letters in screen memory much easier, because it is simply the address (start of display file)+33*row+column+1.

The final size of LIGHTSOUT.P is 903 bytes, but much of that isn’t the program. The raw binary is actually the smallest of all our platforms, at 704 bytes. Some of this might be me getting better at implementing the program with practice, but the far more likely reason is the simplification of the initial board display.

ZX81 compatibility concerns

It took several tries to get the ZX81 build to really work the way I wanted it to. Once the basic implementation had been debugged, my code ran fine in the sz81 emulator, but tests on the more accurate and more configurable EightyOne emulator showed that despite having a core binary size of (at the time) 710 bytes, attempting to load it into a 1K ZX81 would lock the system and attempting to load it into a 2K system would successfully load, but would not run. As long as there was at least 4KB of expansion memory, though, it would run fine.

This is a bit mysterious, as thanks to the screen-memory-as-game-state trick this program should require no more memory than it actually takes to load. But, it turns out, that is the trick: the system does need 2KB to hold both the program and the full display. We use every byte of the screen in making the display thanks to the inverse-video background, but even if we didn’t do that, the puzzle display is more than enough to blow past our limits. So that’s why we were getting the lockup, at least; my original linking program was including a full 793-byte display file at the end of the program, and this was enough to make the load operation blow past all of RAM in the 1K case. We can fix that by replacing it with a 25-byte “compressed” display file instead. At that point it loads in 1KB, but still seems to need 4KB to run. What’s going on? Why 4 instead of 2?

The issue turns out to be in my assumption of getting to have a fully expanded chunk of screen memory even if there’s room for it. If you have less than 3.5KB of RAM, then the Sinclair ROM will re-compress the display file back down to 25 bytes as your program starts. If you have more, then it will re-expand it to the full 793. I was relying on the latter behavior in my board display code.

This happens inside the ROM’s implementation of the CLS command, which starts at location $0A2A. Early on it does indeed check the value of RAMTOP to decide whether or not to do a collapsed or expanded display. Unfortunately, it does the check about halfway through. I solve this by replicating the first half of the routine and then jumping to the expanded display-file case.

I think the accepted practice was actually to use a ROM routine at $0918, which was the machine code implementation of PRINT AT. Subtract your target row from 24 and put that in register B. then subtract your target column from 33 and put that in register C. (Yes, this puts 0, 0 just off the lower-right hand side of the screen. I don’t know either. I think the idea here is to count down how many characters are left in each direction within the screen as a whole.) This routine will move the cursor there, expand the display file as necessary (thus doing a gigantic overlapping memory memory-blit) pretty much up to the top of the machine stack) and then loads location $400e with the memory location of you wanted.

One of the nice things about having an inverse-video screen is that I don’t have to mess with that, so I don’t.

The final issue was more trivial, but annoying nevertheless. I had to drastically shorten my “out of memory” message because there wasn’t enough room left to have a display file that could actually display it. Thus my reasonably grammatical original sentence got crammed down to “2KB+ RAM REQUIRED, SORRY”. So it goes.

But now it’s done!


I’ve collected all four implementations in binary form into one zip file. The source code for each version is in the Github repository as usual.


  1. Logan, I. and O’Hara, F. The Complete Timex TS1000/Sinclair ZX81 ROM Disassembly. Melbourne House, 1982.
  2. Baker, Toni. Mastering Machine Code on Your ZX81. Reston, 1982.