I ran across a ZX81 forum post showing a fast way to print a circle in BASIC. It was based on an article for the Atari back in 1983. I was surprised how fast the routine was and wondered how it would in assembly. Long story short, I built a screen saver for the ZX81. Sure, the ZX81 really didn’t need one, but the circles are fun to watch.
# A starting point.
The idea came from an Circles Tutorial post on the Sinclair forums. The article was originally for an Atari 800, another machine I’d programmed on. However, GCHarder had converted the code to ZX81 BASIC. To my surprise, it ran really well.
Now, the article talks about a couple of different ways to draw a circle. The BAISC was based on the potential method, where each quadrant of the circle was plotted using some simple math. The routine ran really well in BASIC, but I was curious. I wanted to see how it ran in machine code.
The problem is the assembly version is written in Atari BASIC, so no easy way to use that. Not that it would matter much. The Atari used a 6502 chip, which is very different from ZX80. However, I didn’t need it. I had that BASIC program. Using it as pseudocode, I rewrote the routine in assembly. To my surprise, it worked great!
# Making the demo.
Of course, creating a circle is fun, but not very interesting. Playing with the routine, I started to get the idea of displaying a bunch of random circles. That was easier than I thought. I built a little routine to randomly chose the center point and radius. To make things easy, the plot routine I used would exit if the coordinates were outside the screen. This acted like a clipping routine without having to alter any code.
Of course, I wanted the routine to run as fast as possible. To that end, I started to play with the plot routine. That routine was already pretty good, but I wanted to reduce the registers used. I remapped the routine to eliminate the memory saves. I also broke out the plot and unplug functions. This allowed me to remove another flag check. Relatively small changes, but it makes things a bit more efficient.
Now, because it is so fast, I had to add in a couple of delays. One between each circle draw. And another after ten circles are displayed. I tried to keep the number of circles down so as not to clutter the display too much. I did adjust the random routine a few times. The first one I used tended not to be very random.
# Adding a touch of color.
Now, not content with just black and white circles, I decided to make one more change. If you run it with the Chroma hardware, the circles will display with some color. I pulled the routines from my Gem Quest game and reworked the plot routine to change color. Again, it isn’t a huge change, but the overall effect is quite nice.
And there you have it. My ZX81 screen saver of a bunch of circles.