Category Archives: retrocoding

4th of July Special: Type-In Programs

One of the other things I’ve been doing lately is fiddling around a bit with old type-in programs. This has been amusing mostly because it’s a major part of how I learned to program in the first place. It instilled, by accident, much of the discipline that modern tutorials like Learn Python the Hard Way hope to instill on purpose: patience, precision in typing and reading, and a certain sense that inexplicable commands we echoed would at some point make sense.

But there’s a certain art that the modern courses lack, because the modern courses are explicitly instructional. Old type-ins needed to do something cool or useful to be worth the effort of typing in. The needed to be simple enough to type in that you either would not make errors, or the errors would become clear quickly. And, of course, they had to be actually correct. I must admit that a distressing number of actual published listings failed this last criterion, and that this is a major part of how I learned to debug.

The gold standard for type-in software on the Commodore 64, now that we can look over the era, is Compute!’s Gazette, collected in full on the Internet Archive. Sufficiently so, in fact, that I consider it to be very nearly out of scope; many programs published here could compete with full commercial offerings, and the BASIC listings were accompanied by a program that would compute a checksum of every line you typed in. It also included a program called “MLX”, which was a program for entering machine code programs as a raw series of bytes, with a checksum byte of every line you typed in. Compute! was very confident indeed in the correctness of what was printed. Those who learned programming techniques from Compute! learned it more from the articles surrounding the programs they typed in rather than the programs themselves.

I didn’t have the Gazette as a kid, though. Scholastic, Inc. published its own computer magazine with type-ins and article about games and hardware called Family Computing and offered subscriptions as part of its book club for elementary school students. Its programs were often simpler, since it was targeting a younger audience with many fewer needs, and also because it supported many different home systems. Most programs had to have six or more editions, so that cut heavily into the size of any individual program. It did not include any automatic typing checkers like Compute! did, though, so it often had to rely on other mechanisms. Usually there weren’t any, but programs with very large sequences of DATA statements often would compute checksums along the way and have the program error out if there were problems. (This is sound practice—Compute! itself used it to make sure that the proofreader program was properly entered.) For the most part, though, you just had to have sharp eyes and fingers as a young typist.

When I was young, I mostly skipped over the programs that were 90% DATA statements by weight. But I’m a faster and more accurate typist these days, and so when this issue caught my eye, I decided to enter in its flagship program:

family_computing_cover_198607.jpg

The flagship program in this issue was a program that would draw a rendering of the Statue of Liberty and then play The Star-Spangled Banner. Unusually for Family Computing, this program actually shared most of its source code across all platforms. (This shared code was a gigantic block of DATA statements that stored vector information about its image of the Statue of Liberty in a platform-independent manner.) Then each platform had its own code for interpreting the vector information and then playing the music. For the C64, this was another enormous block of DATA statements. Most of that was sound data for the SID chip, but there was also a small machine language routine that it used to clear the bitmap display in a reasonable amount of time, POKEd into memory and then SYSed at program start.

Everything else was handled in BASIC, including this particularly impressive little block of code:

   80 X=A:Y=B:GOSUB 3000
   90 F=0:V1=ABS(C-A):V2=ABS(D-B):S1=SGN(C-A):S2=SGN(D-B)
  100 IF X=C AND Y=D THEN 150
  110 F1=F+V1:F2=F-V2
  120 IF ABS(F1)>=ABS(F2) THEN F=F2:X=X+S1:GOTO 140
  130 F=F1:Y=Y+S2
  140 GOSUB 3000:GOTO 100

The subroutine at line 3000 plots a single point on the bitmap display. That detail aside, this handful of lines is a complete implementation of Bresenham’s line algorithm. That is a very impressive feat of compact and effective code.

Well.

“Effective” might be too strong a term. The screen clears nice and fast, but the full display takes over ten minutes to actually draw, and it looks awful:

liberty-bas

To add insult to injury, the rendititon of The Star Spangled Banner only uses one of the SID’s three voices.

Perusing the code for the other platforms, it becomes clear that the IBM PCjr edition was actually the one with the highest production values. It—like all BASICs that supported it—supplemented the vector graphics with three strategically chosen flood fills. It picked better colors for the display—green on grey instead of white on blue. And uniquely amongst the implementations, it provided a three-voice arrangement of the anthem.

There’s nothing here the C64 couldn’t do, if only we had a decent bitmap library and a minimally acceptable 3-voice music driver. Fortunately, we have just that. All I need to do is convert the string-based PCjr music code into numerical commands for my music driver, and implement the flood fill. The rest is just minor details.

The difference flood fill makes, though, is absolutely night and day. The garbled mess we saw above conceals an incredibly effective chiaroscuro-like effect:

liberty-asm

That’s much more like it. Below the fold, I discuss what went into making the port.

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:

coast_to_coast

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.

Reception

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.

Downloads

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

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:

clover_001

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.

lightsout81

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…

lightsout64

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

lightsout86

…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.

lightsout32

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.

MS-DOS

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.

ZX81/TS1000

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!

Downloads

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.

References

  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.

Getting a Decent and Fast PRNG Out of an 8-Bit Chip

Working on this Z80 project has been burning my brain a bit. I’m going to step back a bit and play around specifically with one part of it: the pseudo-random number generator.

Reader alert: There’s a whole lot of grindy code grinding in this post. If that’s not your thing, you can probably skip this article. On the other hand, if you want to see how my thought process works when I’m trying to get a clean and functional assembly language implementation of something, this article is the good stuff. Do as you will.

The usual technique I’ve used for PRNGs is the Linear Congruential Generator, but this usually relies on having a chip that can multiply for you. That’s fine for x86, but it’s much more obnoxious on the 6502 or the Z80. When I was working on the C64, I just called out to the floating point routines for RND, but when I needed an RNG for Galaxy Patrol on the NES I had no choice but to roll my own 32-bit multiplier and go with that.

I have, however, more recently become aware of the Xorshift family of PRNGs, and in particular the investigations by one Brad Forschinger for high-quality constants in the 16-bit space. His winning algorithm, in C, is as follows:

uint16_t rnd_xorshift_32() {
    static uint16_t x=1,y=1;
    uint16_t t=(x^(x1))^(t^(t>>3));
}

He also provides an implementation in 16-bit (self-modifying!) x86 assembly, which weighs in at 23 instructions and 43 bytes.

It’s worth noting that the ARM chip, which is what powers basically every cell phone ever and also every Nintendo handheld from the Game Boy Advance on, is incredibly efficient at encoding this stuff, because nearly every ARM instruction includes the option of shifting arguments on the way in. So, for instance, the line t=x^(x<<5) becomes the two instructions:

        ldrh    r0, [r2]
        eor     r0, r0, r0, lsl #5

Running the C routine through GCC reveals that it gets it down to 12 instructions and 4 bytes of supporting data (x and y), for a total of 52 bytes and code that reads like a straightforward hand-translation. That’s in the less space-efficient ARM instruction encoding, though: when using the more compact THUMB2 encoding the same number of instructions drops to 40 bytes. Not bad at all! It’s not every day you see a compiler emit code that’s more compact than hand-tuned self-modifying assembly code.

(Older ARM chips such as the one powering the Game Boy Advance used a different compact encoding now called THUMB1. GCC can still emit this, with command flags like -marm7tdmi -mfp=softfp -mthumb. Unlike its successor, THUMB1 loses a lot of expressive power, and this means extra instructions are necessary and the routine grows to 44 bytes. Still not bad, but it’s no longer beating the x86 code.)

Meanwhile, I’m now adapting this to the Z80. This turns out to be a superb level of hassle, and working through it has been more of a chore than I’m used to it being. Below the cut, my first attempt of porting the routine to Z80 assembly language:

Continue reading

ZX81: Alternatives

Last time we fired up the ZX81, we managed to create and link a machine language program to print out a message. That was great, but it also looked like this:

zx81hello_rem

This was the accepted practice of the time, but it’s still a bit ugly. Can we do better? Toni Baker’s book Mastering Machine Code on Your ZX81 suggested three other ways to store machine code alongside this one. In the first, you lower the amount of space available to BASIC and then store your machine language program inside the space left over. This protects the program well, but you cannot save it out, nor can you load it directly. A more capable BASIC that can load machine language programs into arbitrary locations—such as its successor the Spectrum, or any of the Apple or Commodore BASICs—can use this technique pretty readily, though. I don’t personally like it because it implies you need a separate loader program to set up memory right and then once the program stops it can be tricky to restart. In the second, you store the program in a string variable and call it out of the program variable space. This combines the disadvantage of an inconsistent program location with the disadvantage of a program that is obliterated if the end user ever types RUN.

That’s not ideal. The final suggestion was to store the machine language program inside the last line of the program and then make that last line disappear by POKEing a $FF into the most significant line number byte, making this line disappear from BASIC but nevertheless maintained as part of the display file. Ms. Baker did not recommend this technique, largely because the code’s location would vary as the BASIC program did, so calling it was a bit of a chore and any routine that wasn’t fully relocatable would fail if the program was modified.

But that’s perfect for us. We’re aiming for a small fixed program that never changes. Our target program would be something like

10 RAND USR 16514

That number is wrong, of course, but we can compute the right one. The BASIC program starts at 16509; two bytes each for the line number and line size, then one each for RAND and USR, then five for the digits of the address, and then six for the floating-point representation, and finally a newline to end the line. We then need a $FF to force the next line number to be invalid, and we can start our machine code right after that. A bit of addition shows that this means our machine code starts at 16528—$090 in hex. A pleasingly round number.

We can get the bytes for that program by typing the line in and saving that one-line program to a .P file. The bytes we need are then easy to extract from a hex dump.

Of course, in order to actually make use of all this, I need to make my own version of the appmake program we used last time. This isn’t difficult; it’s mostly just concatenating stuff together and then computing and loading in a few constants based on the length of the program. That ends up being a hundred lines or so of Python, most of them just arrays of constants. After reassembling our Hello World program with an origin of $4090 instead of $4082, we get a program that runs identically but has a much more pleasing listing:

zx81hello_post

And there you have it. Now I just have to do something more interesting than Hello World.