Saturday, 24 September 2016

It's all in the Modes & Mappings of the Recreated ZX Spectrum Keys

As mentioned last post, the keyboard works perfectly in the standard PS2 mode, it's the emulator 'layer' that has usage issues. If you switch to mode A (the emulator layer) on the keyboard, the key presses register anything but what you'd expect. There seemed to be some rather odd mapping going on, leaving the emulator mode a little useless.

There would seem to be 3 ways in which to adjust the Recreated Spectrum for usage with Emulators, other than the designated but pulled Elite variants.
  1. Patch an Emulator, or wait for support to be added. Apparently the some patches exist for the Fuse Emulator, adding compatibility for the Recreated Spectrum. That's no guarantee that other emulators will support it; I'd wager many gradually will, though you never know.
  2. Rip out the existing control board and build up a new version.
  3. Come up with an external hardware solution to adjust the key codes sent back to the host computer.
I guess option 1 would be the easiest, at least from a general user perspective, though unless your favorite emulator is open source, this could take a while. Option 2 is the most problematic, as to replace the board in a non case destroying way would be difficult (not impossible) to achieve. Frankly I don't want to ruin the look of the Recreated Spectrum with a hatchet job. That leaves option 3 as a pretty good alternative.

The PS2 mode would be the logical mode to tap into for signal / code modification, we need to see what the Spectrum keyboard actually sends out across the USB cable. To do this easily I pulled out an Arduino USB host shield to read the key codes are being sent. The results where interesting. Firstly, the standard PS2 layer (Mode B), was sending exactly what would be expected. With all letters and combinations of 'shift' and 'Alt' key presses being sent in a seemingly standard manner. I found one glaring issue that puts a stop to tapping into the PS2 mode however; the keyboard does not send any codes for the combination of "CAPS SHIFT + 9". What this means to those who have not already guessed, is that you can't set the keyboard to graphics mode.

So this leaves us with Mode 'A', the emulator layer. This is where things get interesting. A PS2 style keyboard will normally send codes stating when a key is pressed, and a similar code indicating that a particular key has been released. The codes the Recreated Spectrum is sending in emulator mode do not follow that pattern. Instead, when you press 'q', for example the keyboard sends the code for 'u' being pressed, however on release the keyboard send the code for 'v' being released. There is not a single key that sends just the expected letter. I've mapped the keys in the table below.


The Recreated ZX Spectrums Mode 'A' Key Mapping

KEY 1 2 3 4 5 6 7 8 9 0
PRESS a c e g I k m o q s
RELEASE b d f h j l n p r t
KEY Q W E R T Y U I O P
PRESS u w y A C E G I K M
RELEASE v x z B D F H J L N
KEY A S D F G H J K L ENTER
PRESS O Q S U W Y 0 2 4 6
RELEASE P R T V X Z 1 3 5 7
KEY CAPS SHIFT Z X C V B N M SYM SHIFT SPACE
PRESS 8 < - [ ; , / { ! %
RELEASE 9 > = ] : . ? } $ ^

As can be seen from the table, the key code order is layed out in a very neat and predictable manner. Also crucially, all key presses are detected in emulator mode, there are no dead keys combination problems such as in the "CAPS SHIFT + 9" issue discovered in PS2 mode. This all makes adding a man in the middle hardware solution relatively easy to implement.

The next step is to write some Arduino code to see just what we can achieve.

Tuesday, 20 September 2016

Half Recreated ZX Spectrum

Curiosity got the better of me, with some help from continuous price drops and forced my hand into the procurement of a Recreated ZX Spectrum. The Elite Systems clone of the original Sinclair Spectrums keyboard was one of the inspirations behind the starting of this blog. Given these reasons (or excuses) it's not surprising that I felt the desire to try one out, a kind of celebration of the year since beginning the AZ15 project.


I'm not in much of a position to pass judgement on the overall look, feel or experiential replication embodied by the Recreated ZX, you see I've never owned a rubber keyed Spectrum (I had a +2a for a while quite recently), in fact I've only ever seen them in the wild twice. The most recent Spectrum encounter was with an extremely beaten up system found resting quietly in a computer store in Delfzijl, up in the north eastern Netherlands.

My only other brush with a rubber Spectrum was half a world away and decades in between, at the Sydney Personal Computer Show in 1983, where Sinclair had a very prominent and striking black pavilion right at the entrance to the exhibition. As a young teenager and ZX81 owner, this encounter was beyond exciting, though sadly owning one of the beasts on display was not in my near future.

Regardless of the above, we can still judge the Recreated on it's functionality as a keyboard. There are good and bad points to this device. The good points first. The keyboard is stunning, I mean really stunning, it does look to me (with my limited experience) look like a Spectrum. Secondly the keyboard works flawlessly, at least for a rubber keyed throwback to times past as a PC keyboard, though certainly not recommended for daily typing. Frankly is quite fun to use the keyboard, gaze at the keyboard and generally fiddle with it for a while.

Despite the attention to detail shown externally and in the immediate PC-keyboard compatibility, the real use for this keyboard is with Spectrum emulators. This is where functionality falls apart. For whatever reason Elite crippled the keyboard by limiting its emulation use to Elites own suite of applications. None of these Elite applications are supported, let alone available due to ongoing legal disputes. So unless Emulator writers decide to include support for the Recreated Spectrum, the device is functionally useless for emulation purposes. 

There is of course always an option another option: Rip out the internals, grab an Adruino or other micro-controller and retrofit for purpose. In fact retrofitting looks quite achievable, I haven't as yet opened the little box up to check it over, but Tero Heikkinen at the Old Machinery blog has and the possibilities are promising. So this is what I think I'll end up trying out, and if so, I'll of course post the details here.


Tuesday, 13 September 2016

The ZX81, Z88dk, Apps & Roman Numerals.

So what do you do with a Raspberry PI / Arduino ZX81 frankensteinian clone beast, that is other than play copious amounts of games?

In a world full of mobile phone apps, it's easy to forget that effectively computers of the ZX81s ilk only ever really ran what we would now consider apps. Refer to any listing in say, the magazine ZX-Computing, one that isn't a game of some dubious nature for examples. The case being, that really very serious software just wont fit into 16k, let alone 1k of RAM. (With all apologies to the masters of machine code and their associated assembly acolytes, who are at this very moment proving my point entirely incorrect.)

Of course all of the above being the case, the answer to the lead question is of to this post, is write an app.

Thanks to the wonders of the post-modern age, apps can quite easily be developed off on a PC some place, then cross complied for injection back into the plastic doorstop that is a ZX81. To give this all a good go, it was off to Z88dk, the C development tool of choice for targeting Z80 based machines.

I'm not about to bore the world with a run down on the inner most workings of Z88dk, not least because never really having used it before, my 2 cents of input here is not going to help anybody. Suffice to say that it's pretty easy to get to grips with, and really the hardest part about it is that you end up primarily programming in C.

Which leads to the point that there is only one really good reason that explains the existence of C++, and that is the annoyance that is C and specifically Cs' painful handling of Strings. The String and C problem being an extra annoyance as the App I ended up making was a conversion of a Roman Numeral Format checking applet, I'd originally written in Java and then moved to C++, an applet that I'd made the Strings do all the heavy lifting in.

Anyway, regardless of any pet C based grievances, Z88dk is brilliant, and in the end I produced a passable piece of ZX81 software.


The "ZX Roman Numeral-izer" is born.

The only real hard and fast rules to Roman Numeral conversion is this: Always add the numbers together from left to right, unless the numeral to the right is of a lower value, in that case subtract that numeral. That's it, see Wikipeadia for a better explanation.

Of course there is a correct, strict and socially acceptable way to format Roman Numerals, and this is precisely what my little ZX81 app is designed to do. It will check your dodgy formatting, make an informed guess at what you meant, spits out an Arabic Number and presents you with the Correct Roman formatting.

For the moment I've left the app a little bit on the gamey side, it's up to you to try and format your Roman Numeral correctly, there is no Arabic Numeral input option. The code behind the app would easily allow for it, I've just not implemented that extra code as yet, (and possibly wont).

You can download ZX Roman Numeral-izer and give it a go in your favourite ZX81 emulator.