Mastodon

About

Over the last few weeks I have gone down a "1K ZX Chess"-shaped rabbit hole. I'm sure many of you will know of 1K ZX Chess for the Sinclair ZX81, released in 1982, as it fits a (mostly complete) game into a stupidly small space. My three mini-projects have appeared on-line sporadically, but am using this opportunity to try and combine them to a cohesive whole, and shine a light on (what I believe) is the first ever complete breakdown of 1K chess, and how it fits into 1K.


Emulation, on a Dragon 64

First, I wrote a program to emulate the ZX81 on a Dragon 64. Who'd have ever thought that you could emulate one 8-bit machine, on another!?!?! However, it was stupidly slow and required the larger memory of the 64 to even get a 1K game running on it. But it works. After some effort, I might add - it's very Inception-esque trying to debug a ZX81 emulator whilst running on a Dragon emulator. So I also wrote a comparator module that checks every instruction of the C version (for the Dragon) with my reference C++ version (running under SDL.) In my defense, it was a quick hack to see if it was even possible, so I only committed time to a C port of my existing emulator, but the next version will be in 6809 assembler. Code for all versions are under the project Ditzy - a 1K ZX Chess emulated on a Dragon 64, but it's a bit of a mess, tbh, since big chunks were autogenerated and manually patched.


On a Cheap Yellow Display

By this time my CYD had arrived, so I postponed the project for which it had been bought, and ported chess across. Then, because I wanted to show it off, I put it into a portable case. But not just any case.... now I have the game of 1K ZX Chess, quite literally, in the cassette case it came in! (I've included pics of the double case version I also made, which doubles as a nice display/holder, but I prefer the compactness of the smaller one.)

The hardware is nothing more than a CYD (formally a "ESP32-2432S028R", but usually called "Cheap Yellow Display") and a standard USB battery pack.

The software was custom. I pushed the code to Ditzy, mentioned above.

The physical case is tricky, since it's a _very_ tight fit, and the plastic isn't easy to cut without it splintering. But there is nothing more to it than that.

(The pictures also show a 'double cassette' version, where everything fits easier - and turns into it's own display case! )

Other games will involve more work, since I only needed to write one block of code to translate XY touch events into chess board squares. Games with alternate/changing interfaces will need something to map those events to (fake) key presses. You could also use a wifi keyboard, but I've not written the code for that. (Willing to take PRs for it though!)


How it works

And finally, I thought I should look deeply at the original game. Checkmate doesn't get reported in my emulated version, so I needed to compare that behaviour with the original game. The code is 672 bytes long, and comprises 390 instructions, with 24 bytes of lookup tables and 45 bytes of working memory, so it didn't take too long. But how did David get 1K chess into 1K?

That became my goal - find out! I did a complete breakdown of the game by creating a Z80 disassembly of 1K Chess, understanding it, commenting it, and then creating a rebuildable version. (I also found a way to save 3 bytes!)

I won't spoil all the tricks here, but you can imagine my smile when I realised why the look-up movement table for the king was purposefully ordered so it could be re-used for the queen, rooks, bishops, and (er) black pawns!

The complete breakdown and analysis is on github entitled How 1K ZX Chess fits in 1K.


Links

As featured in the Hackaday.com article

1K ZX Chess explained

1K ZX Chess ported to SDL and Dragon 64

Dragon 64 .bin file, suitable for Xroar

Turning 1K Chess into a chess bot

My FOSDEM talk on 1K Chess and chess bots