Pages

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.


MT8816

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.


Breadboarding

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

No comments:

Post a Comment