Sunday, 28 October 2018

PS/2 keyboard on the C64

(Or, the Pizza Box C64 part III)

After building a desktop case for the C64, I wanted an external keyboard.

Browsing the web I get the impression that people want to convert their existing C64 keyboard to work on a PC rather than the other way round, but still I was sure I had seen a PS/2 adapter for C64. And yes, there is the C=key Keyboard Interface, but it's quite expensive and not always available.

Then I came across Robert VanHazinga's solution at GitHub, which uses Arduino Uno/Due, MT8816 chip and a minimal amount of parts. Although it seemed confusing at first, after getting to know the chip a bit it is really quite clearly documented and can be compiled easily.


As far as I understand it's not really possible or healthy to connect an Arduino directly to the keyboard header pins (the CIA chip in fact). An intermediary chip is recommended.

The MT8816 switch array chip comes in handy, as it can specifically produce a connection matrix (X0-15) out of X-Y coordinates (AX0-AX3 and AY0-AY2). The output is built through setting connections between the "coordinates" and setting the matrix using a combination of DATA and SWITCH. RESET clears the whole array.


First I built everything on a breadboard at one go but it failed to produce other than random characters on screen and no visible reaction from the keyboard. This was still somewhat encouraging.

I slept over it, had a deep breath and put the C64 aside. I connected the PS/2 keyboard alone to the Arduino and powered it from the PC USB. I saw that on power-up the keyboard actually flashed its LEDs, something that had not happened before. So perhaps the problem had been some kind of oversight with the cables I guess.

I added a routine for blinking the internal LED to see if it receives the PS/2 keys, and sure it did. Sadly I had to remove this internal blinker as it shares the PIN13 which is needed by the software.

Then I rebuilt the setup on that and it worked!

It became quickly apparent not everything was perfect. Testing the keys, I saw that the 1st keypress after any reset always somehow fails. Left and up cursor are simulated and don't work that well with key repeat. Some keys such as pause/brk and prtscr/sysrq messed things up, and also result in a freeze.

Generally,  fast typing with shift, such as typing the " can result in a keyboard freeze. Actually, if I press shift, press the key to get the ", and if the shift is released before the other key, it freezes.

In this implementation F12/reset clears the freeze, which is a relief.

I also noted that with Final Cartridge III, the bypassing of the GUI by pressing run/stop while booting won't work - I suppose the Arduino software and/or the keyboard can't be yet online at that point.

On the positive side, typing with normal keys works and there were no random hiccups or system crashes. I started to warm to the idea that this could work well enough and most of the problems might be software-based.

Building the board

Satisfied at this, for the next stage I soldered the connections on a prototyping board, with a kind of Arduino "shield". The main connection between the C64 motherboard is with a hard drive ribbon cable, which has 20 pins wide connectors. With some more thought, the board could have been planted on top of the keyboard header but I felt more comfortable with the ribbon.

I thought about having the Arduino USB connector accessible from outside, but after aligning the chip, keyboard header and the Arduino on the board, I couldn't get it to where I wanted it so I gave up the idea for now.
Some day I will have some of that glow visible outside.
Soldering the headers and sockets was easy, but making about 50 connections with wire was boring. The composition was a bit too tight after all, which made the work more stressful. The learning from here was that when making a board by hand, I should make generous space for easier soldering rather than try to optimize prematurely.

Even then the board was not compact enough to fit inside the corner compartment of my case, but the middle suits just as well.

The first boot with the soldered board was a disappointment, but after carefully cleaning the spaces between connections and checking the cables I started to get characters out again.

Case fitting and cleaning up

There still remained the task of attaching the PS/2 connector to the case. This was done with a round screw-in PS/2 connector shown below.

I attached it to the right side, near the joystick ports. Behind the computer would have been perhaps more appropriate, but there's very little space there and I didn't want to pull any more cables across the motherboard.

Just about ok for the 3mm thickness
Inside the box, I also needed to cut the center wall a bit to make a passage for the ribbon cable.

For now I did not attach the keyboard adapter board, as I may still need to change it a bit.

For once, a part that behaved nicely.
More to do:

-Check if the software could be improved a bit and fix the keymap to suit my needs
-Paint the case, put a logo on it or something

Wednesday, 10 October 2018

Pizza Box C64 part II

A followup for my rough C64 case prototype. I came across ready-made 3mm thick, 400 x 300 mm smooth chipboards, and I got an urge to re-make the box already.

(For any DIY-ist out there, I'd remind this kind of case might be considered an added fire hazard!)

This time I put more attention to thinking how the cover would work. I would want to dismantle and assemble the box as often I want, without damaging or wearing out the actual connecting parts. So, although wooden screws would work for a while, they might loosen up eventually.

For this I revived an idea of using flanged, threaded insert nuts for connecting the cover. The major difficulty in obtaining these is remembering what they are called, and not every shop sells the flanged variety. So: flanged insert nut. (In Finnish: kaulusmuhvi)

These are made from zamac, apparently a material similar to what was used in those "metal" toy cars of my childhood. Live and learn.

After sketching the parts on paper I actually bothered to plan this in LibreCAD to study the detail in the millimeter scale. The 40mm interior height was decided on the availability of material in 20mm thickness.

The wooden blocks attached to the top and bottom parts are multi-purpose. Firstly, they keep the top positioned snugly by themselves, but they also hold the insert nuts, which help keep the top and bottom together and well aligned.

Also, the wooden blocks give strength to the corners. The parts are glued together, and with these chipboards the adhesion is not very strong - I could simply pull a part and off it comes with a layer of chipboard. But I'm hoping to get away with it as the box is meant to withstand weight, not tearing apart.

The weight

The thin material brought some challenges I didn't have with the previous box. The enormous weight of the 1084S monitor (in relation to the flimsiness of the material I use) caused some worry.

It's no problem at the front but as the C64 board is near the back I can't put large blocks there without obstructing the connectors. So I kept the space for the board rather small.

Looking at the results of a very complex weight simulation (see above) I also ditched the idea of using rubber legs and added a rigid structure below the box.

These are yet another set of parts that are simply glued together, but if I add enough stuff and wooden corner blocks they'll stick.

The weight ought to be held in check by the vertical "walls". At the same time this structure helps keep the circuit board from bending, as it has been screwed in with 3M machine screws to the bottom cardboard.

Not-to-scale sketch of the section from the side (circuit board in green)
My first test with the monitor resulted ok, the cover does not seem to bend, not even at the backside where the support is not so consistent. Nice!

About making stuff

It's not that easy to make really accurate objects at home, with limited tools and no workshop facilities. But it's not impossible with at least some ready-made parts and some organizing.

These boards are kind of fluffy, not very ideal for drilling. On the plus side sawing with this saw was quite easy:

Razorsaw S-290
The blade is well suited for cutting thin boards and mdf. The front has been rounded so it becomes easier to make the first trace, then start deepening it. Using different blocks and rulers as guides, it's possible to cut very accurate slices.

Start pulling along the ruler
It may seem an obvious point to labor, but if I have a ready-made board with guaranteed straightedges, it follows at least two corners remain 90 degrees after a cut.

I cut the box sides out of separate 400 x 300 chipboards, so all outer walls are already mostly machine-cut.

Perhaps surprisingly, positioning the SD2IEC took more effort the second time round. I cut the SD card reader opening with a paper knife as I was afraid of using a drill, and this took time. I tried to be more clever with positioning the device but ended up making mistakes I had to repair. It stays put anyhow.

The separate keyboard is still a bit of a fantasy. I now have the chip (MT8816 switch array) that would make an Arduino PS/2 keyboard adapter possible. It's not the only option, though. Until next time!