Thursday, January 4, 2018

Fixing 1351 support, and planning auto-detection and support of Amiga mouses

In my last post about mouse support for the MEGA65, I had reached the point where it was almost working, but the two POT lines were not behaving independently, and this was all only in my VHDL test harness.

The first step since then was realising overnight that the problem was likely due to my Squid of Doom cable: It was originally for pulling up the two IEC serial lines I had temporarily kidnapped to play the role off the POT lines. I tried to be clever-pants and have the one cable to do both jobs, because I didn't have a spare male DIN 6 connector. That was a bad idea, because the pullups were both connected to each other via a pair of 1K resistors. That is fine for pulling up the IEC lines, where the junction of the resistors is tied to 5V, effectively isolating them. However, for the POT lines, this meant that there was 2K resistance between the two pots, and thus the two pots were working effectively as one, but with a small difference in value between them.  This was simple to solve: Ride over to Jaycar electronics for about the fifth time this week, and spend $1.65 on a new DIN 6 connector, and separate the Squid of Doom into two purpose-specific cables. Here is the revised cable, posing next to a 1351 mouse:

Following that, it was nice and easy to get the 1351 mouse showing separate X and Y axis readings, as indicated by the different lengths of the purple/blue bars:

However, I was still seeing that the paddles were being over sensitive, that is, the full range of values on the paddle port are obtained with only about half the travel.  That is, I get the full range of motion from 0 - 250K ohm, instead of 0 - 500K ohm.  Put another way: at high resistances, the circuit doesn't charge fast enough.  Interestingly, because of the way that the 1351 mouse works, this is not a problem there, because the mouse supplies full current after a period of time that is determined by the value it wants to present.  However, I still want paddles to have full range of movement.

So, we need to adjust our circuit so that 500K Ohm yields 250K Ohms of resistance, or alternatively fiddle with the capacitor value to double the charge time.  I will freely confess that my analogue electronics is quite rusty, and was in fact never that good.  However, my intuition is that halving the capacitor value would have the desired effect, as would putting a 500K resistor between the POT lines and +5V, so that the two resistors in parallel cut the maximum resistance to 250K.  The capacitor solution would be better in that it reduces the component count, rather than increases it, but as it is close to 40C outside, and I have already been to Jaycar once today, but do have some 1M Ohm resistors on hand that I can parallel up to make 500K Ohm, that's the solution I will try.

As for why this fix is required, I can only speculate that the SID chip has ~500K Ohm impedence to +5V on the POT lines internally, since all the documentation says that 2.2nF is an appropriate capacitor size.  But stop the press! I found a scanned copy of the original SID chip data sheet, which clearly says that we should be using 1000pF = 1nF, not 2.2nF, which is also shown in the application note schematic in Appendix C of the datasheet.  The type-written last page talks about 2.2nF for the EXT-IN pin, which might be where the confusion has stemmed from.

So, it looks like I need to ride over to Jaycar again, after all, unless I can find a 1nF capacitor here somewhere.  Or I just trust that the change in capacitor value will fix the problem.  Currently undecided.  I might just grab one whenever I next need to go to Jaycar for some other component for the MEGA65.

 Now, one of the other interesting things that came up, was a suggestion about using Amiga mouses*, since the 1351 is relatively scarce these days.  The Amiga mouse is really quite a simple beast, simply outputting conditioned quadrature signals.  The advantage here, is that it would allow the use of the existing USB to Amiga mouse/joystick adapters, so that people could use all sorts of newer input devices if they want to.  This has appeal to me, as we can allow USB input devices, without having to have a USB port on the MEGA65, which would increase the cost (primarily, because the PCB would have to get bigger to accommodate it, and 8-layer PCBs aren't cheap per square inch.

Whereas using Amiga mouses on the C64 would be very difficult, requiring an external adapter, we can very easily add logic to the FPGA that decodes the quadrature signals into POT-like X and Y movement values (which is also what the Amiga does).  Ideally, we would make the MEGA65 automatically detect these signals, and switch the port into Amiga mouse mode when it saw movement. The only trick is to make this sensitive and selective enough.  

If an Amiga mouse were in motion, it would be easy to detect, and set a flag that tells the MEGA65 that it is an Amiga mouse, and that stays set for several seconds after the last movement. The problem is when it stops moving, and the quadrature wheels happen to be in a plausible position for a non-mouse, how can we tell that it is still an Amiga mouse when a button is pressed? Given the four inputs, there are 16 possible arrangements that we need to consider:

| Left|Right|  Up | Down|                                       |
|  0  |  0  |  0  |  0  | Must be Amiga mouse                   |
|  0  |  0  |  0  |  1  | Must be Amiga mouse                   |
|  0  |  0  |  1  |  0  | Must be Amiga mouse                   |
|  0  |  0  |  1  |  1  | Must be Amiga mouse                   |
|  0  |  1  |  0  |  0  | Must be Amiga mouse                   |
|  0  |  1  |  0  |  1  | Might be Amiga mouse                  |
|  0  |  1  |  1  |  0  | Might be Amiga mouse                  |
|  0  |  1  |  1  |  1  | Might be Amiga mouse                  |
|  1  |  0  |  0  |  0  | Must be Amiga mouse                   |
|  1  |  0  |  0  |  1  | Might be Amiga mouse                  |
|  1  |  0  |  1  |  0  | Might be Amiga mouse                  |
|  1  |  0  |  1  |  1  | Might be Amiga mouse                  |
|  1  |  1  |  0  |  0  | Must be Amiga mouse                   |
|  1  |  1  |  0  |  1  | Might be Amiga mouse                  |
|  1  |  1  |  1  |  0  | Might be Amiga mouse                  |
|  1  |  1  |  1  |  1  | No activity, so no idea               |

In short, whenever both Left and Right or Up and Down are 0, i.e., asserted, then we can assume it must be an Amiga mouse (unless someone has purposely made a very strange joystick that allows these movements), these are labeld "Must be Amiga mouse" above. Then, for most of the rest of the cases, it might be an Amiga mouse. More specifically, if the mouse was previously believed to be an Amiga mouse, then if we see one of the "might be Amiga mouse" values, without first seeing all the lines released ("No activity, so no idea"), then we can assume that it is still an Amiga mouse connected.  So, the only really tricky case, is when all four lines return high, and we can't tell if a device is still plugged in or not. So lets think about how we can deal with this situation.

The right buttons of the Amiga mouse and 1351 use different pins, with the Amiga mouse using one of the POT lines.  This could probably also be detected, as it would appear as one pot having high resistance, and the other zero resistance, which cannot happen with a 1351, and is extremely unlikely to happen on a pair of paddles. Thus, if we see such an event, we can assume we still have an Amiga mouse connected.

Unfortunately, the Amiga mouse, the 1351 and joysticks all use the same pin for the left button, which is also fire on a joystick.  This means that if up, down, left and right have all relaxed, and we see a left button / fire button event, we have no idea at all what device is connected. However, it doesn't matter for precisely that reason! We can just let the Amiga mouse detection timeout, and return to Joystick/1351 mode, and then automatically go back to Amiga mouse mode if it does happen to be an Amiga mouse, and either moves, or presses the right button.

So, in summary, we can automatically detect an Amiga mouse in all the cases where it matters, and can thus make a scheme that allows an Amiga mouse to be used in the MEGA65 with absolutely no user intervention required -- even if the user diabolically keeps switching between an Amiga mouse, a 1351 and joystick to try to confuse it.

* I'm old enough that I still think that mouses is the correct plural for more than one computer mouse.  Two small furry grey things widdling in your cupboard and hunting for cheese, are, of course, a pair of mice.

No comments:

Post a Comment