Revision History

January 15, 2017: Added info about existing similar work in the intro section

January 11, 2017: Added info about the first demo clip

January 8, 2017: Added section with reference material, including similar projects

January 4, 2017: Initial version



Author: Adam Podstawczyński. Feel free to use any materials in this tutorial, please send me an email if you find it useful :)



key64tapper – Commodore 64 Keyboard Emulator

This project emulates the keyboard of Commodore 64. It allows one to send a string of characters, or interactively type in characters, from a PC/Mac/Linux computer, and have it displayed on the C64 screen.

The project is just another take on microcontroller + crosspoint switch + keyboard combination. When I built it, I was convinced nobody had had that idea before, but when I announced it I was made aware that I was not the first! Similar project are listed in the Reference Material section.

How it Works

A Python script on the computer accepts text to type in various forms (e.g. interactively typed-in, from command line, redirected from other command). The script maps the text to keys or key combinations, and then as another step maps those to 6-bit binary codes. Then the codes are passed via the serial interface (here USB) to the Arduino microcontroller. A listening program on the Arduino accepts the codes and sends them as 6-bit addresses to the write-only memory of an 8x8 crosspoint switch (MT8808), along with 1-bit data. A 6-bit address + 1-bit data are equivalent to "a specific key on" or "a specific key off" command. The command is latched in the crosspoint switch using a strobe signal which results in a particular column pin being shorted with a particular row pin, i.e. effectively pressing a specific key on C64. In case of key combinations, 2 or more such address/data "words" are latched. The Restore key which is activated by connecting to ground needs a separate handling using a transistor.

Possible Uses

Test the C64 keyboard interface from PC

Build a key macro and "play" it on C64 for demonstration purposes

Quickly input a long BASIC listing, e.g. type it in favorite editor, then send to C64

Use your favorite keyboard as C64 input (via interactive mode)

Features / Work in Progress

Entirely hardware-based emulation (i.e. it behaves as if someone was typing on the physical keyboard of a C64) ( done )

) Any key combinations supported, including combinations with Restore key (Restore key implemented on hardware side as a separate address line and switched with a transistor, but not supported by code yet)

Multiple ways of sending keys to press: raw keys ( done ), pre-typed text in batch mode ( done ), pipe from a different command ( work in progress ), or interactive typing on a keyboard ( done )

), pre-typed text in batch mode ( ), pipe from a different command ( ), or interactive typing on a keyboard ( ) Potential macro functionality possible, e.g. timed keystrokes, delays, loops ( work in progress )

) Platform-independent script on computer side (Python script tested on a Mac, should work on Windows/Linux) ( done )

) Concepts of momentary pressing a key ( done ) and holding a key ( work in progress )

Demo

Pre-typed Text Passed as Command Line Option

(This is an older version without the Restore transistor. Also, I mistyped mt8808 as mt8088 in the clip.)

Interactive Mode

Hardware

What You Need

A computer. This was tested on a Mac, but any Linux/Windows with Python installation should work too.

Any working Commodore 64 main board. Bare board is fine, no need for a case or keyboard.

Any modern Arduino board. I used Arduino Mega 2560, but this is an overkill -- the total of 10 digital output pins are needed, and the compiled code doesn't take up much memory (currently ~5KB), so I believe even the tiny Arduino Nano should do.

MT8808ae crosspoint switch in DIP package (PLCC package only if you want to use surface mounting).

An NPN transistor. I used BC547B.

A 10kΩ resistor. 1kΩ should work too.

A breadboard. Of course, you can build and use a PCB instead if you wish.

Single row pin male/female connectors to make the connections neater (but connecting wires directly works too).

Approx. 35 patch wires, including 20 with male-female connectors and 15 with male-male connectors. If the male-female and male-male are packaged as ribbon/tape cables, that's even better; otherwise we end up with a cobweb of wires.

A USB cable for connecting the Arduino to the PC.

Connections

Make the following connections. If you want to use different digital pins on the Arduino, remember to adjust the numbers in the code later.

Software

The Python script and Arduino code are currently incomplete, and have been written quickly to prove the concept. Use at your risk and expect many issues: download from github. More solid version will need some time to complete -- stay tuned.

Reference Material

Similar Projects

After completing the first working prototype and announcing it on the Lemon C64 forum, I was advised of similar projects undertaken in the past (see below). All of them, however, are meant for connecting a PS/2 keyboard to C64 (either directly or via Arduino). The idea behind the project described here is to give control over C64 keyboard to the computer which opens more possibilities.

C64 PS2-USB Keyboard -- connecting a keyboard (not a computer) to Arduino; their idea is to put the entire keyboard mapping code to Arduino memory which is a different approach (in the project described here Arduino simply passes the signals and takes care of timings while all mapping is done on computer).

http://cbm.ficicilar.name.tr/uncorrected/basicv2/basv2.html -- another project for using a PC keyboard directly with C64 (no Arduino)

A hardware module for the same purpose as the above 2 projects (connecting a keyboard to C64)

A Lemon Forum thread on the above projects

A forum64.de discussion on projects similar to the above

Key/Pin/Signal Mapping