PS/2 to C64 Mouse Adapter using ATmega8 microcontroller

Summary of PS/2 to C64 Mouse Adapter using ATmega8 microcontroller


This article details the creation of [M]ouse, a PS/2 to Commodore 64 adapter allowing standard mice to function as 1351 mice. Based on Levente Hársfalvi's design, the project uses an ATmega8 microcontroller on a custom PCB to translate serial PS/2 data into analog signals compatible with C64 joystick ports. The device supports two-way communication and emulates joystick tilt while handling three-button mouse configurations.

Parts used in the[M]ouse Adapter:

  • ATmega8 microcontroller
  • Filtering capacitors
  • Resistors
  • Connectors
  • Custom homemade PCB
  • 5V voltage regulator
  • Temporarily tapped wires for programming

A pixel artist friend of mine wanted a mouse to try his skills on a real C64. I thought I could help him by making an adapter that would allow a regular PS/2 mouse to be used with a Commodore 64. The most popular and supported C64 mouse is 1351 mouse which uses analog capabilities of joystick control ports to send movement data. Same mouse also has a joystick compatibility mode that emulates joystick tilt in response to mouse movements. A PS/2 mouse uses a serial protocol to send digital packets that contain information about current counter values and status of the buttons. A 1351 mouse sends information in the form that C64 analog inputs can read. [M]ouse, the coupling device, communicates with a PS/2 mouse on one side and presents itself as a 1351 mouse to the other side.
This work is based on the pioneering design by Levente Hársfalvi, Serial mouse interface for Commodore. Although the element base and interfaces supported in his project are different, his brilliant work served as inspiration and basis for the development of [M]ouse.

Hardware

Hardware consists of ATmega8 microcontroller, filtering caps, several resistors and connectors. The schematic is basically an adaptation of Levente’s design to a more modern microcontroller and PS/2 interface. Everything is assembled on a small homemade PCB. Programming pins were only used for fuse bits setting and flashing the bootloader. All firmware programming was done via the serial interface through two temporarily tapped wires.
Hardware consists of ATmega8 microcontroller
The circuit contains an additional 5V regulator because I was worried about implied 50mA power draw limitation on the C64 control port. In reality, this limitation is purely hypothetical and the mice that I tried never sucked too much power anyway so this regulator is not needed.
ATmega8 is clocked by its internal 8MHz RC oscillator. It is expected that the device will be used in room conditions with more or less stable temperature and frequency stability is not going to be an issue. To minimize error rate with a 8MHz oscillator, 19200bps serial speed is chosen for bootloading and debugging.

PS/2 Protocol and Mouse Interface

PS/2 protocol and common mouse negotiation procedures are best described by Adam Chapweske in The PS/2 Mouse/Keyboard Protocol paper. Electrically PS/2 interface needs open-drain lines with pull-ups. ATmega8 doesn’t have open drain outputs (except for TWL pins which can’t be used for PS/2 interfacing). In this project open-drain pins are simulated by dynamically switching between active low and hi-impedance states. The implementation supports two-way communication with a PS/2 device and is interrupt-driven by PS/2 clock on INT0 and by Timer0. Read operations are nonblocking; write operations await completion. The PS/2 software module can be used to interface to any PS/2 device be it a mouse or a keyboard.
Higher-level mouse functions are supported only as much as required to properly boot and configure a regular 3-button mouse.

1351 Mouse

1351 Mouse is said to have analog interface, which is however not entirely true. It’s based on a premise that C64 analog-to-digital converter in fact measures the time it takes to charge a capacitor (supposedly, through a potentiometer in an analog paddle). By detecting the moment of the beginning of a measure cycle. It is possible to quickly charge the capacitor to its maximum value at any given time. How to interpret the measured time is entirely up to C64 software. 1351 mousing software interprets such changes in time as mouse movement.
Here’s an illustration from the original U.S. patent 4,886,941 best describing the workings of the 1351:
U.S. patent 4,886,941
A measurement cycle takes not less than 512 microseconds. First 256 microseconds are spent by SID making sure that the capacitor is discharged. The following 256 microseconds are spent waiting until the capacitor charges to the threshold value Vth. The time when Vth is reached is marked and is used by C64 software as analog input data. Instead of using a large ohmic potentiometer, a 1351 mouse rapidly charges the capacitor at intervals between 320 to 448 microseconds from the beginning of measurement cycle.

If this is a POTX line and a mouse is being moved right. The interval from 0μs to the moment of 0-to-1 transition  will keep increasing until it reaches 448μs. Then it will jump back to 320μs and so on. If a mouse is being moved left, the interval will decrease until it hits 320μs and then it will jump to 448μs. The faster the movement, the faster the change. Similar readings could be obtained from a potentiometer without angular limits: the resistance would go up and then hop back to minimum after 360 degrees. In pure terms it can be said that transition delay is zero_point plus (axis_counter_value modulo 128) microseconds. That’s all there is to the magic 1351 mouse.
For more detail: PS/2 to C64 Mouse Adapter using ATmega8 microcontroller

Quick Solutions to Questions related to[M]ouse Adapter:

  • What is the primary purpose of the [M]ouse adapter?
    To allow a regular PS/2 mouse to be used with a Commodore 64 by translating its digital serial protocol into the analog signals expected by the 1351 mouse interface.
  • Which microcontroller powers this project?
    The project utilizes an ATmega8 microcontroller clocked by its internal 8MHz RC oscillator.
  • How does the circuit handle power supply concerns?
    An additional 5V regulator was included due to worries about the 50mA power draw limit on the C64 control port, though it was found to be unnecessary.
  • How are open-drain outputs simulated on the ATmega8?
    Since the ATmega8 lacks native open-drain outputs for this purpose, they are simulated by dynamically switching between active low and hi-impedance states.
  • What determines the movement direction in the 1351 emulation?
    Movement is determined by the time interval it takes to charge a capacitor; moving right increases the interval up to 448 microseconds, while moving left decreases it to 320 microseconds.
  • How is the firmware programmed onto the device?
    All firmware programming is done via the serial interface using two temporarily tapped wires, avoiding the need for dedicated programming pins.
  • Why was a 19200bps serial speed chosen?
    This speed was selected to minimize error rates when using the ATmega8's internal 8MHz oscillator for bootloading and debugging.
  • Does the project support keyboard input?
    The PS/2 software module can technically interface with any PS/2 device like a keyboard, but higher-level functions were implemented only as required for mouse operation.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter
Scroll to Top