Thursday, March 23, 2017

Peeking into the ZX81s Screen Maze

While thinking about some possibilities of how to approach various projects in my to-do list, I realised that these were going to need some screen reading elements, or rather I'd need to PEEK into the display file to see what was going on. This lead to some re-research into how the ZX81 handles its screen output. While some excellent information on the subject is not hard to find, I figure a poke into this might be of use to others.

What's being displayed by the ZX81 on a monitor at any given time is not exactly a mystery, nor is how it got there. What  can be mystery is how to retrieve that information for (re)usage in an application. Actually that's not much of a puzzle either, though possibly a little ambiguous at first.

The ZX81 stores screen related information in a area of memory known as the "display file" or DFILE for short, this is outlined briefly in chapters 27 of the ZX81 manual. What's not made quite so clear is how to access that area of memory, or how that area is configured at any given moment by the machine. This last part is particularly crucial if your ZX81 has less than 4k of RAM (Not of any real concern these days I suppose, but you never know.)

The relationship between the ZX81 Display File and D_File system variables laid out for easy reference.

First off we need to know where the DFILE resides. The location is not static (unlike the ZX Spectrum's DFILE) and moves up or down depending on the size the BASIC program listing. Thankfully there is a system variable, D_FILE, which stores this starting location. What we need to do is PEEK the address of the D_FILE variable to ascertain the location of the actual DFILE. Note that address is stored in 2 bytes.

PRINT PEEK 16396 + PEEK 16397 * 256

On a freshly booted system the result of  the above should be 16509. You might notice that this is also the starting address for the "Program" memory area, which indicates that there is currently no program stored.

Next we can determine how large the DFILE is by reading the system variable VARS. This is where a non expanded ZX81 will differ from one with a RAM pack attached. The returned value will give you the starting location of the user Variables memory allocation area. Again note that the returned listed values from PEEKing the VARS system variable assume there in no program loaded.

PRINT PEEK 16400 + PEEK 16401 * 256

Variables starting address on a 1k ZX81 (or 2k Timex 1000) is: 16534
On a ZX81 with 4k or more expanded memory: 17302

If we subtract the value of D_FILE from VARS, the byte allocation or size of the DFILE is revealed. What we notice is that on a 1k ZX81 the default size of the DFILE is 25 bytes, and on a 4k plus system the DFILE is allocated 793 bytes. The massive (overstatement) size difference between an expanded and unexpaned machine comes down to how the ZX81 handles the DFILE. On a 2k or less machine the DFILE is expanded as it is required, while on a 4k plus computer the DFILE is always fully extended.

On a 1k or 2k ZX81, each of those 25 bytes contains a CHR$ 118, the Newline Character. One CHR$ 118 marks the start of the DFILE , and when a line is printed to, the DFILE is expanded to a maximum of 32 characters per line, with each line always being terminated by a Newline.

A ZX81 with 4k or more always has a fully expanded DFILE, containing 25 CHR$ 118, marking the start of the DFILE and end of each line, plus 32 * 24 characters, which are initially CHR$ 0, the space character, yielding a 793 byte total.

Putting the above into practice, two programs below demonstrate how to POKE to the screen directly, instead of PRINTing. The key lines in both applications are 80 (highlighted), where the second PEEK address is adjusted for either the expanded or collapsed DFILE.

1) Program For a 1K ZX81. If using an emulator make sure it's set for 1K
20 PRINT AT 2,8;"+++"
30 PRINT TAB 8;"+ +"
40 PRINT TAB 8;"+++"
80 POKE PEEK 16396+PEEK 16397*256+41,CODE A$
90 GOTO 60

2) Program For a 4K and up ZX81. If using an emulator make sure it's set for 4K or more
20 PRINT AT 2,8;"+++"
30 PRINT TAB 8;"+ +"
40 PRINT TAB 8;"+++"
80 POKE PEEK 16396+PEEK 16397*256+109,CODE A$
90 GOTO 60

The programs above have been modified from listings originally published in the September / October 1981 issue of Sync Magazine, which is available from the Internet Archive. It's well worth trawling through the entire Sync back catalogue for all manner of useful insights.

Using the information above you can see how easy it is to read or write to the ZX81s screen, and then take that into more complex programs. For a deeper insights into the ZX81s display, you can't go past Wilf Rigters' extensive write up The ZX81 Video Display System.

Finally, yes you really can do something interesting with this information, the below maze is generated by a program written in C for the Z88DK compiler. The Maze generator relies heavily on being able to read the Screen / DFILE (that and a maze algorithm). Feel free to download The Maze C source and ZX81 P file for perusal.

Output of the ZX81 Maze Program.

Read More

Tuesday, March 07, 2017

RAMming in Some External Retrofits

With the internal updates out of the way in the last 2 Retrofitting articles (Simple Start to Retrofitting a ZX81 and Advancing along with the Retrofitting of a ZX81), it's time to add an external RAM pack. Nothing unusual there, after all the first expansion (and possibly the only expansion) any ZX81 owner attaches to the back of their humble 1k micro is a 16k RAM pack. But this is 2017 and a simple 16k RAM expansion just doesn't cut it, and it certainly didn't cut it back in 2011 either, and so enters the amazing ZXpand, bringing RAM packs and ZX81s into the new millennium.

The Amazing ZXpand


The ZXpand created by Charlie Robson is firstly a 32k RAM expansion, nothing odd there, however it's also an SD card reader, provides user configurable memory mapping, hires graphics modes, a reset switch, pads on the circuit board to add a joystick port and optionally (via an extra add on expansion) the ZXpand can accommodate an AY sound interface with a fully built in joystick port. For the moment I'm happy with the base model sans AY.

In a nut shell the ZXpand does for the ZX81 what the DIVIDE (in all it's incarnations) does for the ZX Spectrum. Coming pre-assembled, the ZXpand plugs right into the back of a ZX81 (or TS1000) and is ready to go. The build quality of the board is exceptional, and there are even little amusing gems of ZX81 lore written on the boards Silk screen layers.

If you need some convincing of the ZXpands merits check out VectrexRoli's typically flamboyant video review on the Youtubes.

The ZXpand is intermittently available on Sell My Retro, normally released in batches. You'll need to watch out for these appearing as they sell very quickly. The AY Sound interfaces seem to go at an even quicker rate.

ZXpand Needs a Case

Now for all it's merits the ZXpand comes without an encasing. The good news is that there are options, firstly if you have an existing Memotech RAM expansion, then you can gut that, make a minor modification, then slide the ZXpand into void.

The Elongated Cube ZXpand Board Enclosure

Alternatively, should the massacre of harmless ancient peripherals disturb your delicate sensibilities, or more likely you find yourself bereft of Memotech devices, then as luck would have it mhudson52 on Sell My Retro, has recently started producing 3D printed enclosures in various form factors. Models include a classic Memotech inspired design or the ever popular elongated cube form factor. Both versions have counterparts that allow for the AY interface. I obtained a rectangular version of the ZXpand Board Enclosure for my purposes.

The Enclosure  is a well designed smart little black case. The ZXpand fits very snugly inside, and the
whole unit nestles perfectly behind a grateful ZX81. Being 3D printed, the board enclosures finish is a little rough, so don't expect molded plastic perfection.

The only minor short coming in the cases design is the slightly diminutive hole at the back for accessing the ZXpands momentary reset / NMI switch. In order to press the switch through the case you require something the width of a knitting needle or a thin tablet stylis.

Wire Soldered to the ZXpands Pads

Adding A Joystick Port 

ZXpand Joystick Solder Pads
Adding a joystick port to the base ZXpand requires a little soldering, a DB9 male connector, and the usual assortment of wires. The location of attachment points / pads are outlined in the ZXpands manual and these can easily be hooked up to a male DB9 solder-able connector. Wiring should follow the standard Atari 2600 joysick pinouts.

As I intended to attach the  joystick port to the ZXpand Enclosure, I figured the best way would be to separate the metal casing on the DB9 connector. This would allow me to attach the connector to the inside of the enclosure and at the same time cover up any defects created when making some necessary holes in the enclosure on the outside.

Atari Joystick Port Pinout
Atari Joystick Port Pinout
DB9, with Metal Surrounds Separated
The two metal halves of the DB9 connector are pressed together, a lip in the bolt / screw holes keeps two halves firmly in place. To separate them you can file away the lip around the bolt holes. Once separated connecting wires from the ZXpand joystick pads can be attached to the back of the DB9 shell.

After some examination and testing, I found the most convenient location on the enclosure to place the DB9 connector was at the bottom right. This placement allows the ZXpand to slide in and out of the case without hindrance from the DB9 connector.

Using the outer metal shell of the connector as a template, the placement of the joystick port can be marked out before cutting into the case. I used a Dremel like tool for cutting into 3D printed case. You need to be careful and take your time, as printed plastic likes to re-melt when heated by friction generating power tools. Regardless of how genteel one is, the process will most likely leave a rough edge that needs to be carefully filed away by hand.

The Back of the ZXpand Enclosure, With Holes for the DB9 Connector and Mounting Bolts Cut Out

Joystick Port Mounted
Mounting the DB9 connector is straight forward, if a little fiddly. The enclosure is a little flexible and the mounting point I chose is rather close to one of the ICs on the ZXpand. For this reason I chose to use plastic bolts and nuts for mounting; I didn't want some accidental short circuiting taking place when inserting a joystick.

As the case is designed to fit the ZXpand snugly, the wires to the joystick port had to be routed via the bottom of the enclosure, not a major issue, as these can still be tucked up inside the case mostly out of sight and definitely out of the way.

Joystick Port as seen from the Rear of the Enclosure.

It should also be noted that there is no standard for Joystick interfaces on the ZX81. So much of the software supported by the ZXpands Joystick port has been adapted by the community. There is a list over at the Sinclair ZX World forum of some of the supported tittles.

Getting to that Reset Button

Fully Sprung Reset Tube
As mentioned earlier, the case design makes is a little challenging to access the reset / NMI switch. With fate on my side and some creative thinking I had the means to bodge up an effective solution to that minor issue.

In my draw of miscellaneous items I found a small aluminium tube with a handy flange on one end. The rim fitted over the momentary switch perfectly and the barrel was the perfect length and width to fit through the reset hole on the ZXpand enclosure.

To complete the switching mechanism, a spring extracted from a donor retractable biro was cut down to size, fitted over the tube, and a perfect rest switch was in place.

Another option for the reset switch length problem would be to de-solder the existing SPST momentary push button, replacing it with a longer stalked equivalent. This one from Altronics would do the job quite nicely, any components vendor should have similar items.

So that's it, a ZX81 fit for use, super charged and in better shape than it was 36 years when rolled off the Sinclair (ok Timex) production line.

ZXpand / Enclosure attached to the ZX81. The Reset Switch is the Perfect Length

For full details of the Retrofit, see See  Part 1 and Part 2 of this guide.

Read More