Tuesday, August 22, 2017

First steps towards 1351 mouse support for MEGA65 and MEGAstick

Last week I mentioned how we have a student working on the MEGAstick, a solid-state joystick specially designed for the MEGA65, but also backward compatible with the C64 etc.

In response to that, Daniël from the Netherlands kindly offered to help, because it turns out that the 1351 is not as simple as it first seems.  I already knew that it worked by sending mouse position information as analog signals to the paddle (POT) inputs on the joystick ports.  What I didn't know, was that the 1351 uses carnal knowledge about how the SID chip scans the analogue inputs.

The SID drains the POT inputs for 256 clock cycles to pull the voltage down, and then counts for up to 255 clock cycles until the voltage reaches a threshold.  The higher the resistance, the longer it takes, and the higher the reading in the POT registers.  The 1351 uses this to know when the SID chip is about to start counting, and then outputs a 5V pulse for a length of time proportional to the movement of the mouse since the last sample.  This lets the SID read the movement quite accurately, with only a couple of niggley problems, mostly because the 1351 has an internal 1MHz clock which doesn't exactly match the C64's 0.98 - 1.02MHz PAL/NTSC clock speeds.

Daniël used an oscilloscope connected to a real C64 to verify that the POT lines are easy to read from the joystick port in this way, with a clearly identifiable edge when the SID starts draining the lines.


First step, connect analog inputs of C64 joystick port to an oscilloscope.


Then connect the digital oscilloscope to the laptop and take a look ...

 And the result was indeed very nice: The pulse train is very clear, so we should be able to find some way to do this.


What Daniël also worked out is that the voltage peaks at only around 1.2V, which is unfortunately lower than the logic level trigger of an Arduino, which is what we are using for the joystick controller: For that we need about 3V.

Fortunately, there is another possible option, which is to use an analogue comparator in the PIC chip on the Arduino, to compare the POT lines to a ~1.2V reference voltage, and generate an interrupt from that.  This is what Daniël is currently looking into.

Meanwhile, our student is getting ready to work on implementing a continuous auto-calibration mode for when the MEGAstick is acting as a normal digital joystick.

However, what this also told is is that if we want a real 1351 or a MEGAstick to work as a mouse on the MEGA65, we need to add a mechanism that drains the pot lines in a similar way to how this works on the C64.  We will need to tweak the MEGA65 PCB to implement this.