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.
Friday, 5 January 2018
Working on paddles and mouse support
We want the joystick ports of the MEGA65 to be fully functional, and that means working POT lines, as much so that we can use the 1351 proportional mouse, that uses them to signal to the computer.
As Daniel has previously described here, this requires a system where every 512 1MHz CPU cycles:
1. The POT lines are connected to GND for 256 cycles.
2. The POT lines are disconnected from GND, and then the 256 cycles are counted until the POT line reaches a logical high level. This time is the reading on the pot.
The 1351 mouse detects this cycle, and then drives a pulse starting a certain time into the sequence based on the value it wants to communicate to the computer. Thus it is important that we replicate this scheme accurately.
As the current M65 r1 prototype PCBs lack POT lines on the joystick ports, we will temporarily use two of the IEC serial port lines for testing. This is because the IEC serial lines are controlled in a way that is the same as we need to be able to control the POT lines when we are pretending to be the SID chip, that is, we need to be able to pull a line to ground, and then to be able to release it to float, and that can be read in that state. It also happens that my IEC pull-up cable that I made to work around the lack of pull-ups on the r1 PCB gives me easy access to those lines already, and indeed, the lack of pull-ups means I can float those lines nice and easily.
So here is my plan:
1. Make a quick and dirty set of paddles, so that I have something to test with, before graduating to the 1351 mouse. This just consists of a pair of 500K Ohm variable resistors wired to the POTX and POTY lines on the joystick port, with the other side of the resistors tied to 5V. What I made to meet this need is in no way intended to be used to play games, and instead is totally minimal:
2. Wire up a shim cable for the joystick port, that routes the POTX and POTY lines to the IEC lines, and adds the 2.2nano Farad capacitors to ground. See the photos below of my construction of this cable:
First, connect GND, +5V and the digital lines straight through:
Second, add the capacitors (the small green jobs on the connector to the left), and then connect those lines to the exposed IEC data and clock signals on my existing IEC pull-up shim cable (you can also see the floppy power connector I added earlier in the day, as I started working on making the internal 3.5" floppy drive work -- more on that in another post):
Third, put cages on the ends of the cable:
Right, so now we have the Squid of Doom(tm), which if we plug the IEC cable in to the computer, and the joystick connector that is part of the pass through into the joystick port, we in theory have the electrical interface that will allow us to read the analog inputs.
3. Test!
The process for testing this, is to have the FPGA every 512 micro seconds pull the POT lines to ground for 256 cycles. To do this, we enable output on the IEC lines, to pull them to ground. Then, we disable the IEC outputs, and monitor the inputs for 255 micro seconds, and not at which point in time the signal is interpreted as a digital 1. That duration is the value returned.
While this is being cobbled together using existing signals, for the next revision of the main board, the whole arrangement will require only five lines to be able to monitor all four analog inputs simultaneously: 4x input lines for reading, and one output signal that causes all four lines to be pulled to ground. This will mean that it will be possible to have very simple code to read these lines on the MEGA65, as we will be able to hide the accuracy errors that arise from the analogue multiplexor on the C64, that multiplexes the four analogue inputs from the two joysticks to the two digitisers in the SID chip.
For initial testing, I will extend my test harness VHDL programme, so that it pulls the IEC lines down, and does the counting, and uses the value to draw bars of length proportional to the analogue signal. One of the reasons for trying this with my home-made "paddles", is that the 1351 mouse is a bit more sophisticated, and tries to sample the POT lines to work out when the SID is pulling them to ground. Thus I want to remove that complexity initially, incase the 1351 mouse doesn't recognise this initially. Also, with the variable resisters, it is easier to test the full range of values, and make sure that I have everything correct.
So, I plugged my Squid of Doom in, powered everything up, and I can see the bars, and if I turn the paddle all the way in one direction, then the bar is really short, and if I turn it the otherway, it is really long. So everything is there at a fundamental level. However, note that I said "is" really long, not "becomes" really long. I am seeing a rather digital response here, with the threshold somewhere below 250 Ohms, rather short of the 500K ohm full-swing of a paddle. Here is the two displays: It is either full, or empty, but not smoothly moving between (the purple/teal bar at the bottom is the one to watch):
Hmmm.. Poking around to find the cause, I found one bug in my test harness: I was assuming a 50MHz input clock instead of 100MHz, so the scan rate was twice as fast as it should be, and thus the time for the pot lines to rise was only half what it should be. That said, on the oscilloscope I am seeing what otherwise looks like nice curves, although they are still rather too low at the high-resistance end of the pots:
Low resistance is fairly square:
Medium resistance is curvier:
And high resistance is almost flat:
This says to me that we probably need to have a shunt resistor added in there, so that the total resistance is lower. But first, we need to find out why we are reading only a rather digital signal. I have one of the inputs routed to an LED after it is digitised, and that LED varies in brightness quite smoothly, as you would expect from the variable duty-cycle digital reading of the signal.
So this means I have probably stuffed up my counter that works out what the pot value should be. This was indeed the case: I had the outpot enable line inverted, so that I was counting when the line was being pulled to ground, rather than in the gap. I can now pan smoothly across much of the range, although it is still not perfect. The two pot lines are partly coupled for a start. This could be that I have some touching wires somewhere, or some other minor horror to sort out. I'll take a look at that in the morning. However, I thought I would get my 1351 mouse out, just to see if this is working well enough to be able to convince the mouse to talk to us, and the answer is a definite yes, I can move the mouse around, and see the (still annoyingly coupled) pot lines respond.
My gut feeling for the coupling problem is that the two capacitors are touching, as they were quite close before I put the backshell on the DB9. Whatever the cause, once I have this sorted out, I will add the POT reading code to the MEGA65 VHDL, and make an option to route it to the IEC serial lines as I have done in the test harness, and then try reading it from the MEGA65 itself, perhaps using the 1351 test program or some other simple mouse-supporting program.
As Daniel has previously described here, this requires a system where every 512 1MHz CPU cycles:
1. The POT lines are connected to GND for 256 cycles.
2. The POT lines are disconnected from GND, and then the 256 cycles are counted until the POT line reaches a logical high level. This time is the reading on the pot.
The 1351 mouse detects this cycle, and then drives a pulse starting a certain time into the sequence based on the value it wants to communicate to the computer. Thus it is important that we replicate this scheme accurately.
As the current M65 r1 prototype PCBs lack POT lines on the joystick ports, we will temporarily use two of the IEC serial port lines for testing. This is because the IEC serial lines are controlled in a way that is the same as we need to be able to control the POT lines when we are pretending to be the SID chip, that is, we need to be able to pull a line to ground, and then to be able to release it to float, and that can be read in that state. It also happens that my IEC pull-up cable that I made to work around the lack of pull-ups on the r1 PCB gives me easy access to those lines already, and indeed, the lack of pull-ups means I can float those lines nice and easily.
So here is my plan:
1. Make a quick and dirty set of paddles, so that I have something to test with, before graduating to the 1351 mouse. This just consists of a pair of 500K Ohm variable resistors wired to the POTX and POTY lines on the joystick port, with the other side of the resistors tied to 5V. What I made to meet this need is in no way intended to be used to play games, and instead is totally minimal:
2. Wire up a shim cable for the joystick port, that routes the POTX and POTY lines to the IEC lines, and adds the 2.2nano Farad capacitors to ground. See the photos below of my construction of this cable:
First, connect GND, +5V and the digital lines straight through:
Second, add the capacitors (the small green jobs on the connector to the left), and then connect those lines to the exposed IEC data and clock signals on my existing IEC pull-up shim cable (you can also see the floppy power connector I added earlier in the day, as I started working on making the internal 3.5" floppy drive work -- more on that in another post):
Third, put cages on the ends of the cable:
Right, so now we have the Squid of Doom(tm), which if we plug the IEC cable in to the computer, and the joystick connector that is part of the pass through into the joystick port, we in theory have the electrical interface that will allow us to read the analog inputs.
3. Test!
The process for testing this, is to have the FPGA every 512 micro seconds pull the POT lines to ground for 256 cycles. To do this, we enable output on the IEC lines, to pull them to ground. Then, we disable the IEC outputs, and monitor the inputs for 255 micro seconds, and not at which point in time the signal is interpreted as a digital 1. That duration is the value returned.
While this is being cobbled together using existing signals, for the next revision of the main board, the whole arrangement will require only five lines to be able to monitor all four analog inputs simultaneously: 4x input lines for reading, and one output signal that causes all four lines to be pulled to ground. This will mean that it will be possible to have very simple code to read these lines on the MEGA65, as we will be able to hide the accuracy errors that arise from the analogue multiplexor on the C64, that multiplexes the four analogue inputs from the two joysticks to the two digitisers in the SID chip.
For initial testing, I will extend my test harness VHDL programme, so that it pulls the IEC lines down, and does the counting, and uses the value to draw bars of length proportional to the analogue signal. One of the reasons for trying this with my home-made "paddles", is that the 1351 mouse is a bit more sophisticated, and tries to sample the POT lines to work out when the SID is pulling them to ground. Thus I want to remove that complexity initially, incase the 1351 mouse doesn't recognise this initially. Also, with the variable resisters, it is easier to test the full range of values, and make sure that I have everything correct.
So, I plugged my Squid of Doom in, powered everything up, and I can see the bars, and if I turn the paddle all the way in one direction, then the bar is really short, and if I turn it the otherway, it is really long. So everything is there at a fundamental level. However, note that I said "is" really long, not "becomes" really long. I am seeing a rather digital response here, with the threshold somewhere below 250 Ohms, rather short of the 500K ohm full-swing of a paddle. Here is the two displays: It is either full, or empty, but not smoothly moving between (the purple/teal bar at the bottom is the one to watch):
Hmmm.. Poking around to find the cause, I found one bug in my test harness: I was assuming a 50MHz input clock instead of 100MHz, so the scan rate was twice as fast as it should be, and thus the time for the pot lines to rise was only half what it should be. That said, on the oscilloscope I am seeing what otherwise looks like nice curves, although they are still rather too low at the high-resistance end of the pots:
Low resistance is fairly square:
Medium resistance is curvier:
And high resistance is almost flat:
This says to me that we probably need to have a shunt resistor added in there, so that the total resistance is lower. But first, we need to find out why we are reading only a rather digital signal. I have one of the inputs routed to an LED after it is digitised, and that LED varies in brightness quite smoothly, as you would expect from the variable duty-cycle digital reading of the signal.
So this means I have probably stuffed up my counter that works out what the pot value should be. This was indeed the case: I had the outpot enable line inverted, so that I was counting when the line was being pulled to ground, rather than in the gap. I can now pan smoothly across much of the range, although it is still not perfect. The two pot lines are partly coupled for a start. This could be that I have some touching wires somewhere, or some other minor horror to sort out. I'll take a look at that in the morning. However, I thought I would get my 1351 mouse out, just to see if this is working well enough to be able to convince the mouse to talk to us, and the answer is a definite yes, I can move the mouse around, and see the (still annoyingly coupled) pot lines respond.
My gut feeling for the coupling problem is that the two capacitors are touching, as they were quite close before I put the backshell on the DB9. Whatever the cause, once I have this sorted out, I will add the POT reading code to the MEGA65 VHDL, and make an option to route it to the IEC serial lines as I have done in the test harness, and then try reading it from the MEGA65 itself, perhaps using the 1351 test program or some other simple mouse-supporting program.
Thursday, 4 January 2018
Hooking up the 3.5" internal floppy drive
Today I finally got around to testing the floppy drive interface, that is, the interface for the internal 3.5" floppy drive. Making it read and write 1581 disks will take some time, as we have to write the floppy controller from scratch. Rather, my goal was to make sure that the hardware interface we have built works correctly.
For simplicity and cost effectiveness, we are using standard PC floppy drives, as these are the most likely to be available for the longest time into the future, and are indeed already about the only drives that are easily available now. This isn't a problem for compatibility, precisely because we are building the floppy controller in the FPGA, and so can avoid the compatibility problems that have arisen in the past between different types of drives.
The floppy interface uses a 34 pin cable which is capable of talking to two floppy drives. All the odd numbered pins are ground, and two of the odd ones are reserved, reducing the number of important signals to 15, which are enough to do all sorts of things. However, for those of us who are more interested in reading and writing data, rather than playing classical music, then this is what matters:
2 - Density select (720K / 1.44MB -- this means we will be able to read HD disks eventually)
8 - Index hole sense
10 - Motor enable, drive A
12 - Drive select, drive B
14 - Drive select, drive A
16 - Motor enable, drive B
18 - Step direction
20 - Step track
22 - Write data
24 - Write gate (write enable)
26 - Track 0 sense
28 - Write protect sense
30 - Read data
32 - Head select
34 - Disk change sense flag
The principle of operation for reading is surprisingly simple:
1. All signals are active low, i.e., ground them to activate them.
2. To talk to a drive, pull the appropriate drive select low. Turn the motor on, pull the motor enable line for the matching drive low, then start reading data from the read data line.
3. To step between tracks, use the step direction and step track lines, noting that you have to step twice to move a whole track. Many modern drives move in whole tracks, while older drives would step a half-track at a time (this is why you have to step twice per track, even on newer drives).
4. You can tell when you are on track 0, by waiting for pin 26 to go low.
5. You can tell when the index hole has passed, by watching for pin 8 to pulse low.
For writing, it is much the same, you just pull pin 24 low when you actually want the write head to do its work. To write good data there are some tricks, such as write precompensation, to work around the fact that magnetic domains tend to move after you write them, but the general theory is the same.
Finally, PCs support two drives on the one cable, without having to change the jumpers on the drives. This works by twisting the section of cable that connects pins 10 to 16 -- this is why those pins are laid out in a palandromic order.
This means that for each floppy drive there are 8 signals that the computer has to provide, 6 of which are common between the two drives, so a total of 10 output signals. In addition, there are the five shared input signals.
I modified my VHDL test harness to allow me to control the floppy interface using my VGA + joystick interface. This is a nice way to test this on the actual MEGA65 PCB, without spending lots of time resynthesising VHDL designs all the time.
The first steps were remarkably easy: Pulling the motor or drive select lines low caused the LED to turn on on the drive. Stepping in and out tracks was also very straight forward. Where I got stuck for a bit, however, was reading the inputs. This was partly just from some clumsy typing and thinking in my VHDL harness writing. However, once that was sorted out, I noticed two problems:
1. The drive didn't like doing much when the drive select for the other drive was active. This probably varies among drives, but it was nice to discover it early rather than in production. This was easily solved by pulling up the drive B drive select.
2. The inputs all need pull-ups on them, which we didn't have. Again, I added some temporary ones (well, actually a single temporary one jammed into the drive B connector on the floppy cable, which I moved around as I tested things). This worked just fine, and I was able to observe the track 0, index and write-protect sense lines. Read data I wasn't able to see doing anything, as I was only game to put a blank disk in a floppy drive that I was controlling completely using only a joystick, and that was very easy to accidentally turn on the write head without realising it.
The last line to test was the disk change sense line. My make-shift pull-up arrangement and just the comedy of errors that often happens when testing made this process a little more drawn out than intended. However, in the end, I was able to confirm that this line goes low when a disk is ejected, and remains low until such time as the drive head is stepped in any direction.
What is not quite visible in this image is that the floppy drive is being powered from a joystick port, because the floppy cable has no 5V line, and the revision 1 PCBs don't have a power connector header for the floppy. Older 3.5" drives also needed 12V, but not this one. We will likely provide a connector with both voltages in the production boards, just in case someone needs to use an ancient drive.
So, the current state of play is that, with the exception of the read and write lines, the floppy interface is known to be fully functional. As the read and write lines are connected in the same manner as the other lines, we have no reason to doubt that they will also work. We do need to add pull-ups on the input lines, and should also connect the two drive B signals, both to avoid problems, and also to allow people to add a second internal floppy drive if they wish.
For simplicity and cost effectiveness, we are using standard PC floppy drives, as these are the most likely to be available for the longest time into the future, and are indeed already about the only drives that are easily available now. This isn't a problem for compatibility, precisely because we are building the floppy controller in the FPGA, and so can avoid the compatibility problems that have arisen in the past between different types of drives.
The floppy interface uses a 34 pin cable which is capable of talking to two floppy drives. All the odd numbered pins are ground, and two of the odd ones are reserved, reducing the number of important signals to 15, which are enough to do all sorts of things. However, for those of us who are more interested in reading and writing data, rather than playing classical music, then this is what matters:
2 - Density select (720K / 1.44MB -- this means we will be able to read HD disks eventually)
8 - Index hole sense
10 - Motor enable, drive A
12 - Drive select, drive B
14 - Drive select, drive A
16 - Motor enable, drive B
18 - Step direction
20 - Step track
22 - Write data
24 - Write gate (write enable)
26 - Track 0 sense
28 - Write protect sense
30 - Read data
32 - Head select
34 - Disk change sense flag
The principle of operation for reading is surprisingly simple:
1. All signals are active low, i.e., ground them to activate them.
2. To talk to a drive, pull the appropriate drive select low. Turn the motor on, pull the motor enable line for the matching drive low, then start reading data from the read data line.
3. To step between tracks, use the step direction and step track lines, noting that you have to step twice to move a whole track. Many modern drives move in whole tracks, while older drives would step a half-track at a time (this is why you have to step twice per track, even on newer drives).
4. You can tell when you are on track 0, by waiting for pin 26 to go low.
5. You can tell when the index hole has passed, by watching for pin 8 to pulse low.
For writing, it is much the same, you just pull pin 24 low when you actually want the write head to do its work. To write good data there are some tricks, such as write precompensation, to work around the fact that magnetic domains tend to move after you write them, but the general theory is the same.
Finally, PCs support two drives on the one cable, without having to change the jumpers on the drives. This works by twisting the section of cable that connects pins 10 to 16 -- this is why those pins are laid out in a palandromic order.
This means that for each floppy drive there are 8 signals that the computer has to provide, 6 of which are common between the two drives, so a total of 10 output signals. In addition, there are the five shared input signals.
I modified my VHDL test harness to allow me to control the floppy interface using my VGA + joystick interface. This is a nice way to test this on the actual MEGA65 PCB, without spending lots of time resynthesising VHDL designs all the time.
The first steps were remarkably easy: Pulling the motor or drive select lines low caused the LED to turn on on the drive. Stepping in and out tracks was also very straight forward. Where I got stuck for a bit, however, was reading the inputs. This was partly just from some clumsy typing and thinking in my VHDL harness writing. However, once that was sorted out, I noticed two problems:
1. The drive didn't like doing much when the drive select for the other drive was active. This probably varies among drives, but it was nice to discover it early rather than in production. This was easily solved by pulling up the drive B drive select.
2. The inputs all need pull-ups on them, which we didn't have. Again, I added some temporary ones (well, actually a single temporary one jammed into the drive B connector on the floppy cable, which I moved around as I tested things). This worked just fine, and I was able to observe the track 0, index and write-protect sense lines. Read data I wasn't able to see doing anything, as I was only game to put a blank disk in a floppy drive that I was controlling completely using only a joystick, and that was very easy to accidentally turn on the write head without realising it.
The last line to test was the disk change sense line. My make-shift pull-up arrangement and just the comedy of errors that often happens when testing made this process a little more drawn out than intended. However, in the end, I was able to confirm that this line goes low when a disk is ejected, and remains low until such time as the drive head is stepped in any direction.
What is not quite visible in this image is that the floppy drive is being powered from a joystick port, because the floppy cable has no 5V line, and the revision 1 PCBs don't have a power connector header for the floppy. Older 3.5" drives also needed 12V, but not this one. We will likely provide a connector with both voltages in the production boards, just in case someone needs to use an ancient drive.
So, the current state of play is that, with the exception of the read and write lines, the floppy interface is known to be fully functional. As the read and write lines are connected in the same manner as the other lines, we have no reason to doubt that they will also work. We do need to add pull-ups on the input lines, and should also connect the two drive B signals, both to avoid problems, and also to allow people to add a second internal floppy drive if they wish.
Wednesday, 3 January 2018
[M]Ultimax and other Cartridges on the MEGA65
Now that we have one cartridge working on the MEGA65, I thought I would go through my collection of cartridges, and see which ones work, and try to get non-working ones to be working ones.
The bulk of the cartridges I have are the official Commodore released ones, plus a few odds and sods. Most had been sitting in my shed or someone else's shed (and in one case, it looks like in their sand dune) for some time. So the first test of them all was not particularly successful, as the contacts were all so dirty that most of them didn't work. 15 minutes with some isopropyl alcohol and some cotton swabs fixed that problem, however.
So the current status of the cartridges is as follows:
Radar Rat Race (x2): These cartridges use the Ultimax (or is it Multimax?) mode of the C64. This means that the KERNAL ROM is replaced by the cartridge ROM, and that most memory is not available. It also means that the cartridge ROM needs to be visible to the VIC-II at $3000,$7000,$B000 and $F000, so that it can be used to contain character sets etc for games. This is a bit of a problem for the MEGA65, because the VIC-IV can't even talk to the cartridge port, and its pixel clock is 100x faster than the cartridge clock (although we can increase that on the MEGA65, but that's for a future post). However, we have an easy work around: Kickstart can copy the 4KB of ROM into those locations in RAM from the Hypervisor, before exiting to Ultimax mode where those portions of RAM are no longer visible to the CPU (actually, they are currently still visible on the MEGA65, but this will get fixed in time). In the process of testing this, and trying these cartridges I also found a few bugs in the memory mapping for Ultimax mode, which didn't surprise me, as it hadn't been previously tested. However, after all that, the cartridge happily loads.
Number Nabber Shape Grabber
Wizard of Wor, which I remember being one of the most popular in our circle when I was young.
Pinball Spectacular, which we all complained bitterly was hardly a pin ball game when you controlled it with paddles, and that it was more like a breakout/arkanoid with spectacularly average graphics, but anyway.
Dragonsden, which I had never heard of before, but looks like it has the potential to be a bit of fun for the kids.
International Soccer, which we already know works:
The cartridges that don't currently work:
KINDERCOMP from Spinnaker Software (because it is too big to fit in the hole in my current prototype case. I will file the hole out bigger later.
Epyx Fastload. This one just doesn't seem to do anything. It might be that I have some signalling wrong.
Action Replay VI. This signals that it has ROMs, but I think it is presenting it's onboard 8KB RAM instead of the ROM at $8000. The IO area does decode correctly, however, which is nice. The main problem is that the ROM doesn't appear at $E000. Again, this might be a problem of me not having the various signals set correctly, or the timing being wrong in some way.
Super Games. This one seems to still have a problem, as it doesn't start, and the area where the signature byte should be doesn't reliably read. Here is the sort of thing I am seeing with it when I repeatedly read it:
m7018000
:7018000 4C 4C 0A 4C C3 4C CD 38 4C 00 4C 80 4C 4C 85 4C
.m7018000
:7018000 4C 4C 4C 80 4C 4C CD 4C 4C 00 4C 4C A0 4C 4C FB
.m7018000
:7018000 0A 80 4C 80 C3 4C CD 4C 30 4C 4C 80 4C 00 4C FB
.m7018000
:7018000 0A 4C 4C 80 4C 4C CD 4C 30 4C A9 80 4C 00 85 4C
.m7018000
:7018000 4C 80 0A 4C C3 C2 4C 38 30 4C A9 4C A0 4C 4C FB
.m7018000
:7018000 0A 80 4C 80 4C C2 4C 38 4C 00 4C 80 4C 00 4C FB
.m7018000
:7018000 4C 4C 0A 4C 4C C2 4C 4C 30 4C 4C 80 4C 4C 85 4C
.m7018000
:7018000 4C 80 0A 4C C3 C2 4C 38 4C 00 4C 4C A0 4C 85 4C
.m7018000
:7018000 0A 4C 4C 80 4C 4C CD 4C 4C 00 4C 4C A0 4C 4C FB
.m7018000
:7018000 0A 80 4C 80 4C C2 4C 38 4C 00 4C 80 4C 00 4C FB
It might be a timing thing, or it might be a crusty old cartridge thing. This one does tellingly have some masking tape wrapped around it, suggesting it has been in for surgery in the past:
[ Picture of taped up cartridge]
Opening it up, it has two ROMs and a couple of glue logic chips to select which ROM is visible. Even though it contains International Soccer as one of the three games, it isn't an Ultimax-mode cartridge. Instead, it just maps 8KB at $8000, but has the means to bank between, presumably, four different banks in the two ROMs using the 74LS175 (quad flip-flop) and 74LS139 (2 to 4 line de-multiplexor). I am guessing that the function here is to latch some of the bits written to $DFxx (via /IO2 feeding the flip-flop) to use for the upper address bit on the ROMs, and use the 2-4 decoder to select the correct ROM chip's output-enable line. All very nice and simple. However, something is unstable, which I need to investigate.
Part of me suspects that it is a faulty 74LS chip, and the other part of me wonders if it isn't a timing issue on the cartridge port, although given how sloppy the C64's timing was originally, and how I am switching all signals within 20ns of one another, I am not convinced that this is that likely. That said, when I stuck the oscilloscope on all the pins of the 74LS chips, nothing was unstable. What was interesting, is that the 1MHz clock appears to be one of the inputs to the de-multiplexor. This does have implications, because currently my expansion port controller tries to cheat a bit, and will make accesses on both sides of the clock, instead of just on the "CPU" side. However, it seems that this is a bad idea, after all, which on reflection, I should have realised at the time. So I will need to fix that. Tellingly about half of the bytes read in the unstable reads are identical ($4C), suggesting that something is wrong 1/2 of the time. This fix might also help the fast loader cartridges, although I suspect there is more to their problems, especially for the Action Replay.
So, the current summary is not too surprising: Almost all the Commodore issued cartridges work, with the exception of one that has revealed a clock problem that will be easy to fix. The two fast load cartridges also have problems, which I am not over surprised about, as they are more complex. But those problems should be fixable. Finally, there is one that just doesn't physically fit at the moment. I'll file the hole in my make-shift "case" to make room for it, and retest it.
The bulk of the cartridges I have are the official Commodore released ones, plus a few odds and sods. Most had been sitting in my shed or someone else's shed (and in one case, it looks like in their sand dune) for some time. So the first test of them all was not particularly successful, as the contacts were all so dirty that most of them didn't work. 15 minutes with some isopropyl alcohol and some cotton swabs fixed that problem, however.
So the current status of the cartridges is as follows:
Radar Rat Race (x2): These cartridges use the Ultimax (or is it Multimax?) mode of the C64. This means that the KERNAL ROM is replaced by the cartridge ROM, and that most memory is not available. It also means that the cartridge ROM needs to be visible to the VIC-II at $3000,$7000,$B000 and $F000, so that it can be used to contain character sets etc for games. This is a bit of a problem for the MEGA65, because the VIC-IV can't even talk to the cartridge port, and its pixel clock is 100x faster than the cartridge clock (although we can increase that on the MEGA65, but that's for a future post). However, we have an easy work around: Kickstart can copy the 4KB of ROM into those locations in RAM from the Hypervisor, before exiting to Ultimax mode where those portions of RAM are no longer visible to the CPU (actually, they are currently still visible on the MEGA65, but this will get fixed in time). In the process of testing this, and trying these cartridges I also found a few bugs in the memory mapping for Ultimax mode, which didn't surprise me, as it hadn't been previously tested. However, after all that, the cartridge happily loads.
Number Nabber Shape Grabber
Wizard of Wor, which I remember being one of the most popular in our circle when I was young.
Pinball Spectacular, which we all complained bitterly was hardly a pin ball game when you controlled it with paddles, and that it was more like a breakout/arkanoid with spectacularly average graphics, but anyway.
Dragonsden, which I had never heard of before, but looks like it has the potential to be a bit of fun for the kids.
International Soccer, which we already know works:
The cartridges that don't currently work:
KINDERCOMP from Spinnaker Software (because it is too big to fit in the hole in my current prototype case. I will file the hole out bigger later.
Epyx Fastload. This one just doesn't seem to do anything. It might be that I have some signalling wrong.
Action Replay VI. This signals that it has ROMs, but I think it is presenting it's onboard 8KB RAM instead of the ROM at $8000. The IO area does decode correctly, however, which is nice. The main problem is that the ROM doesn't appear at $E000. Again, this might be a problem of me not having the various signals set correctly, or the timing being wrong in some way.
Super Games. This one seems to still have a problem, as it doesn't start, and the area where the signature byte should be doesn't reliably read. Here is the sort of thing I am seeing with it when I repeatedly read it:
m7018000
:7018000 4C 4C 0A 4C C3 4C CD 38 4C 00 4C 80 4C 4C 85 4C
.m7018000
:7018000 4C 4C 4C 80 4C 4C CD 4C 4C 00 4C 4C A0 4C 4C FB
.m7018000
:7018000 0A 80 4C 80 C3 4C CD 4C 30 4C 4C 80 4C 00 4C FB
.m7018000
:7018000 0A 4C 4C 80 4C 4C CD 4C 30 4C A9 80 4C 00 85 4C
.m7018000
:7018000 4C 80 0A 4C C3 C2 4C 38 30 4C A9 4C A0 4C 4C FB
.m7018000
:7018000 0A 80 4C 80 4C C2 4C 38 4C 00 4C 80 4C 00 4C FB
.m7018000
:7018000 4C 4C 0A 4C 4C C2 4C 4C 30 4C 4C 80 4C 4C 85 4C
.m7018000
:7018000 4C 80 0A 4C C3 C2 4C 38 4C 00 4C 4C A0 4C 85 4C
.m7018000
:7018000 0A 4C 4C 80 4C 4C CD 4C 4C 00 4C 4C A0 4C 4C FB
.m7018000
:7018000 0A 80 4C 80 4C C2 4C 38 4C 00 4C 80 4C 00 4C FB
It might be a timing thing, or it might be a crusty old cartridge thing. This one does tellingly have some masking tape wrapped around it, suggesting it has been in for surgery in the past:
[ Picture of taped up cartridge]
Opening it up, it has two ROMs and a couple of glue logic chips to select which ROM is visible. Even though it contains International Soccer as one of the three games, it isn't an Ultimax-mode cartridge. Instead, it just maps 8KB at $8000, but has the means to bank between, presumably, four different banks in the two ROMs using the 74LS175 (quad flip-flop) and 74LS139 (2 to 4 line de-multiplexor). I am guessing that the function here is to latch some of the bits written to $DFxx (via /IO2 feeding the flip-flop) to use for the upper address bit on the ROMs, and use the 2-4 decoder to select the correct ROM chip's output-enable line. All very nice and simple. However, something is unstable, which I need to investigate.
Part of me suspects that it is a faulty 74LS chip, and the other part of me wonders if it isn't a timing issue on the cartridge port, although given how sloppy the C64's timing was originally, and how I am switching all signals within 20ns of one another, I am not convinced that this is that likely. That said, when I stuck the oscilloscope on all the pins of the 74LS chips, nothing was unstable. What was interesting, is that the 1MHz clock appears to be one of the inputs to the de-multiplexor. This does have implications, because currently my expansion port controller tries to cheat a bit, and will make accesses on both sides of the clock, instead of just on the "CPU" side. However, it seems that this is a bad idea, after all, which on reflection, I should have realised at the time. So I will need to fix that. Tellingly about half of the bytes read in the unstable reads are identical ($4C), suggesting that something is wrong 1/2 of the time. This fix might also help the fast loader cartridges, although I suspect there is more to their problems, especially for the Action Replay.
So, the current summary is not too surprising: Almost all the Commodore issued cartridges work, with the exception of one that has revealed a clock problem that will be easy to fix. The two fast load cartridges also have problems, which I am not over surprised about, as they are more complex. But those problems should be fixable. Finally, there is one that just doesn't physically fit at the moment. I'll file the hole in my make-shift "case" to make room for it, and retest it.
MEGA65 Boots GEOS ... From a serially attached 1571
Following on from our recent success in getting the MEGA65 to work with real disk drives (my half-dead 1581 not withstanding), Falk, one of our developers, decided to give it a bit of a stress test: Could the MEGA65 handle the timing sensitive fast-loader that GEOS uses?
Well, the answer is in the video below:
There really isn't much more to say, except that I know I have a couple of sprite rendering bugs to sort out (on today's list, all going well).
Well, the answer is in the video below:
There really isn't much more to say, except that I know I have a couple of sprite rendering bugs to sort out (on today's list, all going well).
Tuesday, 2 January 2018
The detective work in getting International Soccer running on the MEGA65, direct from cartridge
This blog post was written as I went through the process of getting International Soccer working on cartridge on the MEGA65. It is written in the present tense, as I went about solving each problem along the way, so that you can get a sense of some of what is involved in making and debugging the MEGA65. The whole process took about 4 days, mostly due to the time it takes the synthesise (compile) the FPGA program. But anyway, here it is.
My current challenge is to see if the expansion port works electrically, and what changes we need to make to it, if any.
(Note that the MEGA65 has a C64-compatible 44-pin cartridge port, not a C65-compatible 50-pin cartridge port. We believe that improved compatibility with C64 accessories is more important that compatibility with non-existant C65 cartridges. There are a few other ways that we have worked to improve C64 compatibility in the MEGA65, such as ensuring that RMW instructions retain their old side-effects that are often relied upon for clearing VIC-II interrupts -- but that's a topic for other blog posts.)
We already know that we accidentally made /EXROM and /GAME bidirectional instead of inputs, but that is only a minor inconvenience, as we can work around that by scanning them as inputs periodically (their direction is switched together with a bunch of other bidirectional signals on the bus, otherwise we could leave them set as inputs the whole time).
A while back I implemented an entire cartridge port framework for the MEGA65, and tested it with a dummy cartridge implemented in VHDL, with both ROMs and I/O area expansions. So I know that, in principle at least, it should work. But let's just go over the theory of how a cartridge works on the C64 first. We will stick with simple cartridges, and only C64 mode cartridges, not C128 ones or C65 ones, partly because that covers most cartridges, and partly because the MEGA65 will support only C64 and MEGA65 cartridges.
The cartridge signals what resources it has to the C64 via the /EXROM and /GAME input lines. These are internally pulled up in the C64, and cartridges connect them to ground if they want to activate them. You can read more about their effects here, but a quick summary is:
/EXROM = 5V, /GAME = 5V gives the normal C64 memory map, as when no cartridge is connected.
/EXROM = 5V, /GAME = 0V gives "Ultimax" mode, where most of internal RAM is banked out, and $8000-$9FFF and $E000-$FFFF are provided by the cartridge. Mostly of interest only if you want to make a cartridge that replaces the KERNAL in a C64.
/EXROM = GND, /GAME = 5V gives a single 8KB cartridge rom at $8000-$9FFF
If both /EXROM and /GAME = 0V, then $8000-$BFFF is a 16KB ROM provided by the cartridge.
In most cases, the contents of the CPU IO port at $01 controls which of these ROMs are actually visible, versus the RAM underneath. The signals from $01 and from /EXROM and /GAME, together with the upper bits off the address that the C64 is trying to access at any point in time go to the PLA chip in the C64, which works out which chip-select lines get activated, and hence, what memory the C64 sees at that loccation at that time.
Now, for cartridges, the external ROMs also have chip-select lines, /ROML and /ROMH, which are driven to 0V by the PLA in the C64 when trying to access the ROM.
For cartridges that provide I/O expansions at $DExx or $DFxx, the process is similar, except that the C64 doesn't need to be told, it simply pulls the /IO1 or /IO2 line to ground, if either of those areas are being accessed.
All up, this makes for surprisingly simple electronics in a cartridge.
The MEGA65 supports all those signals, and also allows the Hypervisor to disable a connected cartridge, by refusing to take the /GAME and /EXROM signals into account, and thus never activating the chip-select lines for them.
For convenience, and as part of the mechanism for supporting much larger capacity cartridges on the MEGA65 (a topic that will be covered in more detail another day), the full 64KB of address space exposed by the cartridge is able to be accessed at $7FFxxxx. Accesses to $8000-$BFFF in that space will activate /ROML, while those to $E000-$FFFF will activate /ROMH, and of course $DExx and $DFxx accesses will activate /IO1 and /IO2.
This makes for convenient cartridge testing, independent of the memory map, because we can simply issue an access request to the cartridge port. Trying this, I found that there appeared to be activity when accessing $DExx and $DFxx. So far so good. However, the address decoding for $DExx and $DFxx is somewhat special, as it appears in every memory bank when IO is visible. However, a quick check revealed that it also attempts to access the cartridge port when I use addresses $7FFDExx and $7FFDFxx. This is expected.
So at this point, I know that the CPU can delegate memory accesses to the cartridge port, but not whether anything sensible happens there. Also, critically for cartridges, I don't know whether the /ROML and /ROMH accesses are also being sent to the cartridge port. Attempting to access $7FF8xxx or $7FFExxx should reveal that -- and indeed, this works also.
So now we know that all of the cartridge chip select lines are being driven. Quick checks revealed that R/_W is floating high to read, and that the 1MHz phi2 clock. The DMA line is floating high, indicating that the bus is not unavailable due to DMA. That leaves just the BA signal outstanding -- which is also floating high, which tells the bus that the VIC-II doesn't need the bus for anything (this is in fact always high on the MEGA65, because the VIC-IV doesn't use the cartridge port for anything).
Thus, all the conditions are ripe for a cartridge to be visible on the expansion port, and for us to be able to read the data from a ROM or IO bank in it. Time to insert a cartridge. In thtis case, an exemplar of the classic "International Soccer"
Quick initial attempts to read memory locations $7FF8000, $7FFA000, $7FFDE00, $7FFDF00 and $7FFE00, however, showed no signs of data. At this point, I don't know if the cartridge is not presenting data, or whether the MEGA65 is not taking note of the read data. Sticking the oscilloscope onto one of the data lines, and accessing $7FFE000 and above, I can see signs of data:
That we are seeing varying data indicates that we are able to drive the address lines correctly, something which I was also able to confirm using the oscilloscope.
There is something a bit odd, however, in that the data lines are resting low in many cases, rather than floating high. I need to investigate the cause of this. It might be that we need pull-ups on the data lines, or it might be that something in the cartridge is continuing to drive the lines. Removing the cartridge has the lines continuing to rest low, which means it must be the lack of pull-up resistors.
This in fact points to two problems that we need to solve: First, the lack of pull-ups, and second, if the lines are sitting low, and we see data being read from the cartridge, why on earth we are not seeing the data being read by the CPU? Is it a timing/logic error, or are the data lines not able to be read for some reason? Again, in the absence of pull-ups, the data being read should be all zeroes, but what I am seeing is all ones.
One challenge to finding and fixing these bugs is that it takes several hours to resynthesise the FPGA program, even if making just a trivial change. Thus, it can be helpful to have a minimal FPGA program that allows us to probe some of these pins, and see what we can see there. This allows iteration taking only a few minutes a time, instead of several hours.
Thus my next step is to see whether I can read from the data lines on the bus, so that I know whether the problem is there, or whether it is in the logic of my expansion port controller in VHDL. To make my life as easy as possible, this program generates a simple VGA frame, and displays incoming signals as white or black squares for 1 or 0. It also allows setting upto 256 different signals, some of which I bound to the cartridge port control and data lines. You then just use a joystick to move around and toggle the lines.
The display of me using it to read a byte from an International Soccer cartridge can be seen below:
The top white and green row is the cartridge port control signals. White = 1, green = 0. They are, from left to right:
cart_ctrl_dir - Sets the direction of R/W, /ROMH, /IO1, /GAME, /EXROM, /IO2, /ROML and /BA to be output if set.
cart_data_dir - Sets the direction of the data lines to output if set.
cart_haddr_dir - Sets the direction of the upper address lines to output if set.
cart_laddr_dir - Sets the direction of the lower address lines to output if set.
cart_phi2 - Directly sets the phi2 clock line.
cart_dotclock - Directly sets the dotclock line.
cart_reset - Directly sets the /RESET line.
cart_nmi - Reads the /NMI line.
cart_irq - Reads the /IRQ line.
cart_dma - Reads the /DMA line.
cart_exrom - Reads/writes the /EXROM line.
cart_game - Read/writes the /GAME line.
cart_ba - Reads/writes the /BA line.
cart_rw - Reads/writes the R/_W line.
cart_roml - Reads/writes the /ROML line.
cart_romh - Reads/writes the /ROMH line.
cart_io1 - Reads/writes the /IO1 line.
cart_io2 - Reads/writes the /IO2 line.
cart_data_en - If 0, connects the data lines between FPGA and the cartridge slot.
cart_addr_en - If 0, connects the address lines between FPGA and the cartridge slot.
Below this line are a couple of white blocks. They just mark where /ROML and /ROMH are in the line above, for my convenience. Then there is a blank and unused line. The far left blocks on this line if set (which they are not) would tri-state the address and data lines, respectively.
The fourth line has the address bits (bit 15 is on the far left) in white=1 and green=0, a gap and then the data bits (bit 7 on the left). These are the bits that the FPGA is outputting (or wishes to output, if the _en and _dir lines are not set correctly). The fifth line, in red=1 and black=0, are the values read from the cartridge port, if it is connected. More precisely, they read back whatever the FPGA has on those pins, whether connected or not.
With this VHDL test setup working, I was able to quite quickly establish that the problem was real - I couldn't drive or read the real pins at all. This turned out to be because I had not connected the _en lines. After fixing that, I was able to obtain the picture above, where it can be seen that memory location $FFFA has the value of $9F on this cartridge, when /ROML is active low. This corresponds to address $9FFA
Better, once I had corrected the _en bug in the MEGA65 VHDL, I could now read the contents of the ROM at $8000:
.M7ff8000
:7FF8000 27 80 A8 80 C3 C2 CD 38 30 00 40 C0 40 C0 40 C0
:7FF8010 40 C0 40 C0 40 C0 40 C0 00 80 80 10 10 10 10 20
:7FF8020 20 20 20 40 40 40 40 20 84 FF 20 87 FF A9 2F 85
:7FF8030 00 A9 00 8D 0E DC 8D 1A D0 A9 7F 8D 0D DC AD 0D
:7FF8040 DC AD 19 D0 8D 19 D0 A9 25 8D FE FF A9 91 8D FF
:7FF8050 FF A9 9C 8D FA FF A9 80 8D FB FF A9 00 85 6F A9
:7FF8060 80 85 70 A0 00 B1 6F 91 6F C8 D0 F9 E6 70 A5 70
Further hunting around in the $7FFxxxx address space, I can see that this cartridge has 16KB of ROM at $8000, and 8KB at $E000, and indeed, my test program was reading the correct value:
.m7ff9ff0
:7FF9FF0 85 75 68 85 73 68 85 72 4C FE 9F 68 68 68 A9 00
That is, $9F is indeed at location $9FFA in the ROM.
So, this is a really nice significant step forward, in that I can correctly access the memory from the cartridge.
The next trick is to get it enabled in the computer's memory map. The Hypervisor on the MEGA65 has the ability to disable cartridges in software, so that they don't affect the C64 mode memory map (although they continue to appear at $7FFxxxx). This is the default mode in the current version of Kickstart. To enable the cartridge, one just needs to set bit 0 of $D67D from within the Hypervisor (if you try to touch this register from user land, it instead causes a trap to the Hypervisor).
The only trouble is, when I set this bit, the cartridge didn't suddenly get mapped into memory. I also realised that I hadn't added any means to examine what the CPU thinks the /EXROM and /GAME lines are set to. So, I have added these in, and resynthesised the design. As sometimes happens with VHDL for me, although it is deeply unsatisfying, the act of putting instrumentation on a signal seems to suddenly make it work.
Anyway, these signals are now available to the CPU, and when the Hypervisor enables cartridges, the ROM suddenly maps at $8000-$BFFF as it should, and I can trace it single-step through its startup process. However, it wouldn't start the game. Tracing further, I found that it copies the ROM to the RAM underneath, which is not uncommon for cartridges to do. Seeing this, I checked to make sure that I had this implemented for cartridge ROMs, but apparently not. It works for the standard ROMs, but I had the cartridge memory space mapping for both reads and writes. Again, a quick fix once I knew what the problem was, but again, I have to resynthesise to test again, which probably means waiting until morning. But now I am getting excited -- everything looks like this cartridge should work when I try it with this fix. We will see whether my excitement is misplaced or not.
Hmmm... Well, that managed to get the cartridge copied into RAM. Now, however the cartridge is getting stuck in a funny little loop:
$8211 98 TYA
$8214 91 1D STA ($1D),Y
$8216 11 1D ORA ($1D),Y
$8218 C9 CE CMP #$CE
$821A D0 F7 BNE $8211
The vector at $1D points to $0000, the data-direction register for the 6510, and Y=0. Thus, this has the effect of ... well, to be honest, I am not quite sure what it is supposed to be achieving.
So I started tracing again from reset.
The cartridge entry point is at $8027, which calls some KERNAL routines and begins to setup memory, and copying the cartridge to RAM as described above, it then switches out the ROM, and continues running the cartridge from the RAM beneath at $8074:
$8074 A9 05 LDA #$05
$8076 85 01 STA $01 ; Bank out cartridge ROM
$8078 58 CLI
$8079 A0 18 LDY #$18
$807B A9 00 LDA #$00
$807D 99 00 D4 STA $D400,Y ; Clear SID registers
$8080 88 DEY
$8081 10 FA BPL $807D
$8083 A9 8F LDA #$8F
$8085 8D 18 D4 STA $D418 ; Set SID volume
$8088 20 43 AC JSR $AC43
$808D 20 40 AC JSR $AC40
All is fine until it gets to these two instructions. The trouble being that $AC43 appears to be in the middle of an instruction. At first I thought that maybe the copy loop had failed for some reason, but comparing the ROM and RAM sections of memory, and also double checking that this matches the memory actually mapped by the CPU there, it all looks the same:
.mac40
:000AC40 69 40 85 87 A9 00 85 3C 85 3F B9 A1 C1 85 3A B9
.m701ac40
:701AC40 69 40 85 87 A9 00 85 3C 85 3F B9 A1 C1 85 3A B9
.dac40
:777AC40 69 40 85 87 A9 00 85 3C 85 3F B9 A1 C1 85 3A B9
.
The sensible disassembly of this would seem to be:
$AC40 69 40 ORA #$40
$AC42 85 87 STA $87
In which case there is no reason to ever JSR into $AC43, which would in fact be an illegal opcode (SAX $A9, i.e., store the and of X and A into location $A9 if you are curious). This doesn't seem to make any sense to me.
So then I wondered if setting $01 to $05 might just bank out the cartridge ROM, and leave C64 BASIC visible. $AC43 is at least the beginning of an instruction in the C64 BASIC, but not the start of a routine. Indeed, it is in the middle of the GET command, which seems an unlikely choice here. Also, $AC40 is not the start of an instruction in the C64 BASIC ROM. But more to the point, after consulting the C64 memory banking table, it is not possible to bank the BASIC ROM in, while /EXROM and /GAME are both low.
So at this point, I am beginning to wonder if the cartridge isn't faulty, and that it is supposed to have a different byte sequence at $8088. So I downloaded a copy of International Soccer, which I know is based off of at least some cartridge version, and took a look. Not surprisingly, the loadable version has the ROM to RAM copy routine replaced by NOP instructions. Then in the process of saving it, I spotted something important: $AC40 is completely different in that version to what I was seeing. Then, finally the penny dropped: The MEGA65 was seeing the first 8KB of the cartridge ROM twice, instead of all 16KB, because I should be asserting /ROMH for the second 8KB, instead of /ROML again. Time for another synthesis run ... Now I am less excited, perhaps because it is almost 4AM (I got up early, not went to bed late), when part of me says I should be more excited, since I found and fixed what seems like it should be the last barrier to the cartridge mapping properly, and indeed automatically on power-up.
So, that fix did indeed fix the mapping of the 2nd 8KB of the cartridge ROM. However, it still doesn't work. It now makes the JSR to $AC43, without trouble. However, then jumps to $AF6C which is an RTI instruction. On the disk version of the game, there is something completely different there. Comparing the two, I am seeing a lot of repetative data from the cartridge, but not from the disk version.
At this point, I am left wondering if this cartridge is working correctly, or if it has a hardware fault, or indeed, if my cartridge port bus has poor timing, that is causing it to read incorrect data, or perhaps that my signalling is so bad in some way, that it is harming the cartridge, e.g., through cross-driving data lines.
As I don't have an easy way to hook up an actual C64 here, I don't have too many options except to try a different cartridge. For (the as it turned out unfounded) fear that the problem might be the MEGA65 toasting cartridges, the only one I had a double of was Radar Rat Race, so I tried that, but that turns out to be an Multimax-mode cartridges, and trying that revealed some fun bugs with my Multimax Mode setup for cartridges. Bugs hopefully fixed, and once again, off synthesising.
So now I was back to the International Soccer cartridge. I was really beginning to think that it was a MEGA65 logic problem, but the repeated blocks I was seeing had raised a suspicion in me that it could be a faulty ROM in the cartridge. So I opened the cartridge up, and was very happy to find this:
First up, we can see that this is a cartridge made on Commodore's multi-purpose PCB, with the three traces on the left that allow reconfiguration between different cartridge types, and has spots for two 8KB ROMs, plus one small RAM, so that it can be used for Multimax cartridges. What isn't quite so obvious above, is that one of the ROMs was looking a bit short on solder on one side, and it looks like a couple of pins were bridged by a solder thread:
On the reverse side, it was obvious that someone at some point had tried (in vain) to desolder one of the ROMs:
That someone is quite possibly a much younger me, who wanted to make a custom cartridge, although I don't specifically remember. The empty socket for the RAM is also just sitting there, not soldered down at all. All most curious.
Anyway, for the task at hand, I needed to re-solder those dry pins, so that all the address lines would work again, and thus, hopefully, fix the repeated blocks problem.
That I did, and then boldly put the cartridge back together, and inserted it into the MEGA65, and started it up. Finally, after tracking down such a diverse variety of problems, I was greeted with scenes like this:
Finally! So, we can now say that the MEGA65 can use (at least some) real C64 cartridges.
Here is a short video I made, showing the cartridge being inserted, and the MEGA65 powering up from cold to run the cartridge. These r1 PCBs have a larger FPGA that takes longer to configure on power up, so the power-on process will likely be a shade quicker on the final machines. Nonetheless, it is already very fast, and feels like a real 8-bit computer, which of course is the point.
My current challenge is to see if the expansion port works electrically, and what changes we need to make to it, if any.
(Note that the MEGA65 has a C64-compatible 44-pin cartridge port, not a C65-compatible 50-pin cartridge port. We believe that improved compatibility with C64 accessories is more important that compatibility with non-existant C65 cartridges. There are a few other ways that we have worked to improve C64 compatibility in the MEGA65, such as ensuring that RMW instructions retain their old side-effects that are often relied upon for clearing VIC-II interrupts -- but that's a topic for other blog posts.)
We already know that we accidentally made /EXROM and /GAME bidirectional instead of inputs, but that is only a minor inconvenience, as we can work around that by scanning them as inputs periodically (their direction is switched together with a bunch of other bidirectional signals on the bus, otherwise we could leave them set as inputs the whole time).
A while back I implemented an entire cartridge port framework for the MEGA65, and tested it with a dummy cartridge implemented in VHDL, with both ROMs and I/O area expansions. So I know that, in principle at least, it should work. But let's just go over the theory of how a cartridge works on the C64 first. We will stick with simple cartridges, and only C64 mode cartridges, not C128 ones or C65 ones, partly because that covers most cartridges, and partly because the MEGA65 will support only C64 and MEGA65 cartridges.
The cartridge signals what resources it has to the C64 via the /EXROM and /GAME input lines. These are internally pulled up in the C64, and cartridges connect them to ground if they want to activate them. You can read more about their effects here, but a quick summary is:
/EXROM = 5V, /GAME = 5V gives the normal C64 memory map, as when no cartridge is connected.
/EXROM = 5V, /GAME = 0V gives "Ultimax" mode, where most of internal RAM is banked out, and $8000-$9FFF and $E000-$FFFF are provided by the cartridge. Mostly of interest only if you want to make a cartridge that replaces the KERNAL in a C64.
/EXROM = GND, /GAME = 5V gives a single 8KB cartridge rom at $8000-$9FFF
If both /EXROM and /GAME = 0V, then $8000-$BFFF is a 16KB ROM provided by the cartridge.
In most cases, the contents of the CPU IO port at $01 controls which of these ROMs are actually visible, versus the RAM underneath. The signals from $01 and from /EXROM and /GAME, together with the upper bits off the address that the C64 is trying to access at any point in time go to the PLA chip in the C64, which works out which chip-select lines get activated, and hence, what memory the C64 sees at that loccation at that time.
Now, for cartridges, the external ROMs also have chip-select lines, /ROML and /ROMH, which are driven to 0V by the PLA in the C64 when trying to access the ROM.
For cartridges that provide I/O expansions at $DExx or $DFxx, the process is similar, except that the C64 doesn't need to be told, it simply pulls the /IO1 or /IO2 line to ground, if either of those areas are being accessed.
All up, this makes for surprisingly simple electronics in a cartridge.
The MEGA65 supports all those signals, and also allows the Hypervisor to disable a connected cartridge, by refusing to take the /GAME and /EXROM signals into account, and thus never activating the chip-select lines for them.
For convenience, and as part of the mechanism for supporting much larger capacity cartridges on the MEGA65 (a topic that will be covered in more detail another day), the full 64KB of address space exposed by the cartridge is able to be accessed at $7FFxxxx. Accesses to $8000-$BFFF in that space will activate /ROML, while those to $E000-$FFFF will activate /ROMH, and of course $DExx and $DFxx accesses will activate /IO1 and /IO2.
This makes for convenient cartridge testing, independent of the memory map, because we can simply issue an access request to the cartridge port. Trying this, I found that there appeared to be activity when accessing $DExx and $DFxx. So far so good. However, the address decoding for $DExx and $DFxx is somewhat special, as it appears in every memory bank when IO is visible. However, a quick check revealed that it also attempts to access the cartridge port when I use addresses $7FFDExx and $7FFDFxx. This is expected.
So at this point, I know that the CPU can delegate memory accesses to the cartridge port, but not whether anything sensible happens there. Also, critically for cartridges, I don't know whether the /ROML and /ROMH accesses are also being sent to the cartridge port. Attempting to access $7FF8xxx or $7FFExxx should reveal that -- and indeed, this works also.
So now we know that all of the cartridge chip select lines are being driven. Quick checks revealed that R/_W is floating high to read, and that the 1MHz phi2 clock. The DMA line is floating high, indicating that the bus is not unavailable due to DMA. That leaves just the BA signal outstanding -- which is also floating high, which tells the bus that the VIC-II doesn't need the bus for anything (this is in fact always high on the MEGA65, because the VIC-IV doesn't use the cartridge port for anything).
Thus, all the conditions are ripe for a cartridge to be visible on the expansion port, and for us to be able to read the data from a ROM or IO bank in it. Time to insert a cartridge. In thtis case, an exemplar of the classic "International Soccer"
Quick initial attempts to read memory locations $7FF8000, $7FFA000, $7FFDE00, $7FFDF00 and $7FFE00, however, showed no signs of data. At this point, I don't know if the cartridge is not presenting data, or whether the MEGA65 is not taking note of the read data. Sticking the oscilloscope onto one of the data lines, and accessing $7FFE000 and above, I can see signs of data:
That we are seeing varying data indicates that we are able to drive the address lines correctly, something which I was also able to confirm using the oscilloscope.
There is something a bit odd, however, in that the data lines are resting low in many cases, rather than floating high. I need to investigate the cause of this. It might be that we need pull-ups on the data lines, or it might be that something in the cartridge is continuing to drive the lines. Removing the cartridge has the lines continuing to rest low, which means it must be the lack of pull-up resistors.
This in fact points to two problems that we need to solve: First, the lack of pull-ups, and second, if the lines are sitting low, and we see data being read from the cartridge, why on earth we are not seeing the data being read by the CPU? Is it a timing/logic error, or are the data lines not able to be read for some reason? Again, in the absence of pull-ups, the data being read should be all zeroes, but what I am seeing is all ones.
One challenge to finding and fixing these bugs is that it takes several hours to resynthesise the FPGA program, even if making just a trivial change. Thus, it can be helpful to have a minimal FPGA program that allows us to probe some of these pins, and see what we can see there. This allows iteration taking only a few minutes a time, instead of several hours.
Thus my next step is to see whether I can read from the data lines on the bus, so that I know whether the problem is there, or whether it is in the logic of my expansion port controller in VHDL. To make my life as easy as possible, this program generates a simple VGA frame, and displays incoming signals as white or black squares for 1 or 0. It also allows setting upto 256 different signals, some of which I bound to the cartridge port control and data lines. You then just use a joystick to move around and toggle the lines.
The display of me using it to read a byte from an International Soccer cartridge can be seen below:
The top white and green row is the cartridge port control signals. White = 1, green = 0. They are, from left to right:
cart_ctrl_dir - Sets the direction of R/W, /ROMH, /IO1, /GAME, /EXROM, /IO2, /ROML and /BA to be output if set.
cart_data_dir - Sets the direction of the data lines to output if set.
cart_haddr_dir - Sets the direction of the upper address lines to output if set.
cart_laddr_dir - Sets the direction of the lower address lines to output if set.
cart_phi2 - Directly sets the phi2 clock line.
cart_dotclock - Directly sets the dotclock line.
cart_reset - Directly sets the /RESET line.
cart_nmi - Reads the /NMI line.
cart_irq - Reads the /IRQ line.
cart_dma - Reads the /DMA line.
cart_exrom - Reads/writes the /EXROM line.
cart_game - Read/writes the /GAME line.
cart_ba - Reads/writes the /BA line.
cart_rw - Reads/writes the R/_W line.
cart_roml - Reads/writes the /ROML line.
cart_romh - Reads/writes the /ROMH line.
cart_io1 - Reads/writes the /IO1 line.
cart_io2 - Reads/writes the /IO2 line.
cart_data_en - If 0, connects the data lines between FPGA and the cartridge slot.
cart_addr_en - If 0, connects the address lines between FPGA and the cartridge slot.
Below this line are a couple of white blocks. They just mark where /ROML and /ROMH are in the line above, for my convenience. Then there is a blank and unused line. The far left blocks on this line if set (which they are not) would tri-state the address and data lines, respectively.
The fourth line has the address bits (bit 15 is on the far left) in white=1 and green=0, a gap and then the data bits (bit 7 on the left). These are the bits that the FPGA is outputting (or wishes to output, if the _en and _dir lines are not set correctly). The fifth line, in red=1 and black=0, are the values read from the cartridge port, if it is connected. More precisely, they read back whatever the FPGA has on those pins, whether connected or not.
With this VHDL test setup working, I was able to quite quickly establish that the problem was real - I couldn't drive or read the real pins at all. This turned out to be because I had not connected the _en lines. After fixing that, I was able to obtain the picture above, where it can be seen that memory location $FFFA has the value of $9F on this cartridge, when /ROML is active low. This corresponds to address $9FFA
Better, once I had corrected the _en bug in the MEGA65 VHDL, I could now read the contents of the ROM at $8000:
.M7ff8000
:7FF8000 27 80 A8 80 C3 C2 CD 38 30 00 40 C0 40 C0 40 C0
:7FF8010 40 C0 40 C0 40 C0 40 C0 00 80 80 10 10 10 10 20
:7FF8020 20 20 20 40 40 40 40 20 84 FF 20 87 FF A9 2F 85
:7FF8030 00 A9 00 8D 0E DC 8D 1A D0 A9 7F 8D 0D DC AD 0D
:7FF8040 DC AD 19 D0 8D 19 D0 A9 25 8D FE FF A9 91 8D FF
:7FF8050 FF A9 9C 8D FA FF A9 80 8D FB FF A9 00 85 6F A9
:7FF8060 80 85 70 A0 00 B1 6F 91 6F C8 D0 F9 E6 70 A5 70
Further hunting around in the $7FFxxxx address space, I can see that this cartridge has 16KB of ROM at $8000, and 8KB at $E000, and indeed, my test program was reading the correct value:
.m7ff9ff0
:7FF9FF0 85 75 68 85 73 68 85 72 4C FE 9F 68 68 68 A9 00
That is, $9F is indeed at location $9FFA in the ROM.
So, this is a really nice significant step forward, in that I can correctly access the memory from the cartridge.
The next trick is to get it enabled in the computer's memory map. The Hypervisor on the MEGA65 has the ability to disable cartridges in software, so that they don't affect the C64 mode memory map (although they continue to appear at $7FFxxxx). This is the default mode in the current version of Kickstart. To enable the cartridge, one just needs to set bit 0 of $D67D from within the Hypervisor (if you try to touch this register from user land, it instead causes a trap to the Hypervisor).
The only trouble is, when I set this bit, the cartridge didn't suddenly get mapped into memory. I also realised that I hadn't added any means to examine what the CPU thinks the /EXROM and /GAME lines are set to. So, I have added these in, and resynthesised the design. As sometimes happens with VHDL for me, although it is deeply unsatisfying, the act of putting instrumentation on a signal seems to suddenly make it work.
Anyway, these signals are now available to the CPU, and when the Hypervisor enables cartridges, the ROM suddenly maps at $8000-$BFFF as it should, and I can trace it single-step through its startup process. However, it wouldn't start the game. Tracing further, I found that it copies the ROM to the RAM underneath, which is not uncommon for cartridges to do. Seeing this, I checked to make sure that I had this implemented for cartridge ROMs, but apparently not. It works for the standard ROMs, but I had the cartridge memory space mapping for both reads and writes. Again, a quick fix once I knew what the problem was, but again, I have to resynthesise to test again, which probably means waiting until morning. But now I am getting excited -- everything looks like this cartridge should work when I try it with this fix. We will see whether my excitement is misplaced or not.
Hmmm... Well, that managed to get the cartridge copied into RAM. Now, however the cartridge is getting stuck in a funny little loop:
$8211 98 TYA
$8214 91 1D STA ($1D),Y
$8216 11 1D ORA ($1D),Y
$8218 C9 CE CMP #$CE
$821A D0 F7 BNE $8211
The vector at $1D points to $0000, the data-direction register for the 6510, and Y=0. Thus, this has the effect of ... well, to be honest, I am not quite sure what it is supposed to be achieving.
So I started tracing again from reset.
The cartridge entry point is at $8027, which calls some KERNAL routines and begins to setup memory, and copying the cartridge to RAM as described above, it then switches out the ROM, and continues running the cartridge from the RAM beneath at $8074:
$8074 A9 05 LDA #$05
$8076 85 01 STA $01 ; Bank out cartridge ROM
$8078 58 CLI
$8079 A0 18 LDY #$18
$807B A9 00 LDA #$00
$807D 99 00 D4 STA $D400,Y ; Clear SID registers
$8080 88 DEY
$8081 10 FA BPL $807D
$8083 A9 8F LDA #$8F
$8085 8D 18 D4 STA $D418 ; Set SID volume
$8088 20 43 AC JSR $AC43
$808D 20 40 AC JSR $AC40
All is fine until it gets to these two instructions. The trouble being that $AC43 appears to be in the middle of an instruction. At first I thought that maybe the copy loop had failed for some reason, but comparing the ROM and RAM sections of memory, and also double checking that this matches the memory actually mapped by the CPU there, it all looks the same:
.mac40
:000AC40 69 40 85 87 A9 00 85 3C 85 3F B9 A1 C1 85 3A B9
.m701ac40
:701AC40 69 40 85 87 A9 00 85 3C 85 3F B9 A1 C1 85 3A B9
.dac40
:777AC40 69 40 85 87 A9 00 85 3C 85 3F B9 A1 C1 85 3A B9
.
The sensible disassembly of this would seem to be:
$AC40 69 40 ORA #$40
$AC42 85 87 STA $87
In which case there is no reason to ever JSR into $AC43, which would in fact be an illegal opcode (SAX $A9, i.e., store the and of X and A into location $A9 if you are curious). This doesn't seem to make any sense to me.
So then I wondered if setting $01 to $05 might just bank out the cartridge ROM, and leave C64 BASIC visible. $AC43 is at least the beginning of an instruction in the C64 BASIC, but not the start of a routine. Indeed, it is in the middle of the GET command, which seems an unlikely choice here. Also, $AC40 is not the start of an instruction in the C64 BASIC ROM. But more to the point, after consulting the C64 memory banking table, it is not possible to bank the BASIC ROM in, while /EXROM and /GAME are both low.
So at this point, I am beginning to wonder if the cartridge isn't faulty, and that it is supposed to have a different byte sequence at $8088. So I downloaded a copy of International Soccer, which I know is based off of at least some cartridge version, and took a look. Not surprisingly, the loadable version has the ROM to RAM copy routine replaced by NOP instructions. Then in the process of saving it, I spotted something important: $AC40 is completely different in that version to what I was seeing. Then, finally the penny dropped: The MEGA65 was seeing the first 8KB of the cartridge ROM twice, instead of all 16KB, because I should be asserting /ROMH for the second 8KB, instead of /ROML again. Time for another synthesis run ... Now I am less excited, perhaps because it is almost 4AM (I got up early, not went to bed late), when part of me says I should be more excited, since I found and fixed what seems like it should be the last barrier to the cartridge mapping properly, and indeed automatically on power-up.
So, that fix did indeed fix the mapping of the 2nd 8KB of the cartridge ROM. However, it still doesn't work. It now makes the JSR to $AC43, without trouble. However, then jumps to $AF6C which is an RTI instruction. On the disk version of the game, there is something completely different there. Comparing the two, I am seeing a lot of repetative data from the cartridge, but not from the disk version.
At this point, I am left wondering if this cartridge is working correctly, or if it has a hardware fault, or indeed, if my cartridge port bus has poor timing, that is causing it to read incorrect data, or perhaps that my signalling is so bad in some way, that it is harming the cartridge, e.g., through cross-driving data lines.
As I don't have an easy way to hook up an actual C64 here, I don't have too many options except to try a different cartridge. For (the as it turned out unfounded) fear that the problem might be the MEGA65 toasting cartridges, the only one I had a double of was Radar Rat Race, so I tried that, but that turns out to be an Multimax-mode cartridges, and trying that revealed some fun bugs with my Multimax Mode setup for cartridges. Bugs hopefully fixed, and once again, off synthesising.
So now I was back to the International Soccer cartridge. I was really beginning to think that it was a MEGA65 logic problem, but the repeated blocks I was seeing had raised a suspicion in me that it could be a faulty ROM in the cartridge. So I opened the cartridge up, and was very happy to find this:
First up, we can see that this is a cartridge made on Commodore's multi-purpose PCB, with the three traces on the left that allow reconfiguration between different cartridge types, and has spots for two 8KB ROMs, plus one small RAM, so that it can be used for Multimax cartridges. What isn't quite so obvious above, is that one of the ROMs was looking a bit short on solder on one side, and it looks like a couple of pins were bridged by a solder thread:
On the reverse side, it was obvious that someone at some point had tried (in vain) to desolder one of the ROMs:
That someone is quite possibly a much younger me, who wanted to make a custom cartridge, although I don't specifically remember. The empty socket for the RAM is also just sitting there, not soldered down at all. All most curious.
Anyway, for the task at hand, I needed to re-solder those dry pins, so that all the address lines would work again, and thus, hopefully, fix the repeated blocks problem.
That I did, and then boldly put the cartridge back together, and inserted it into the MEGA65, and started it up. Finally, after tracking down such a diverse variety of problems, I was greeted with scenes like this:
Finally! So, we can now say that the MEGA65 can use (at least some) real C64 cartridges.
Here is a short video I made, showing the cartridge being inserted, and the MEGA65 powering up from cold to run the cartridge. These r1 PCBs have a larger FPGA that takes longer to configure on power up, so the power-on process will likely be a shade quicker on the final machines. Nonetheless, it is already very fast, and feels like a real 8-bit computer, which of course is the point.
Monday, 1 January 2018
Fixing a few rendering glitches and re-testing enhanced sprite modes.
While I continue to work on some of the missing hardware items, such as cartridge support, I find I have quite a bit of time waiting for synthesis runs to complete for the FPGA. For the MEGA65 PCB these can take anywhere between 1 and 10 hours. So, I am using some of that latent time to work on fixing some rendering glitches that have snuck in with the whole move to 800x600 video modes.
Exhibit A: Bitmap mode glitch. Initially I suspected that the problem here was that each 8x8 block was rotated by one physical pixel -- and that may well have been the case, but after attempting to fix that, a glitch remained. This is really annoying, because it is super ugly. First step to fixing a problem is to fully understand it. So I switched the MEGA65 to bitmap mode, and drew some dots, and set different background colours on the 8x8 blocks, so that I could see what was going on:
The other is that expanded sprites seem to be a little too narrow. This results in some annoying artifacts, e.g., in Boulder Dash, where the black bar beneath the core is actually made with a bar of expanded sprites. Here is a quick shot of the problem taken as my son was coincidentally playing Boulder Dash as I wrote this post.
The little vertical bars beneath the score line are the problem we are talking about here. Looking closer on the screen, I can see that these are one 320x200 pixel wide, i.e., half an expanded sprite pixel. This makes me suspect that the odd/even toggle for working out which half of an expanded sprite pixel is being drawn might be the problem. Let's test that theory by drawing a simple sprite, expanding it, and seeing if any of the pixels are only 1/2 width.
First, unexpanded:
This revealed something I hadn't previously noticed, but am not surprised to see: The bottom physical pixel has been shaved off. Since sprites are effectively expanded vertically the whole time on the MEGA65, they also have an odd/even toggle. This reinforces my suspicion that those toggles are possibly to blame.
Now expanded in both X and Y directions:
We can see that the left half a pixel has been trimmed when the sprite is expanded, and the bottom half of the last pixel is always missing, whether expanded or not.
Looking through the code, I can see that the odd/even toggle in the X direction doesn't get reset at the end of a sprite. This is quite easy to fix, by resetting it each time a sprite starts drawing on a raster line. I've written a fix for this now, as well, which will also have to wait for synthesis to see if it has fixed the problem.
The missing row (or rows of pixels) on the bottom is not quite so straight forward. Here unexpanded, we are seeing one physical row of pixels is missing. When expanded, it is difficult to tell if we are seeing only two physical rows of the four we should be getting, or whether we are getting three. The difference is important, as each result would suggest different root causes. If two, then the sprite expander is probably to blame. If three, then it is the double-scanning of the screen that is probably to blame.
To answer this question, I switched back to text mode, and drew some characters on the screen that would let me determine exactly how high that bottom pixel block is:
Here we can see a couple of very interesting things:
First, the sprite is mis-aligned with the text area by one physical 800x600 pixel vertically, rather than on a VIC-II pixel boundary. That will have to get fixed, as well. But more importantly for now, we can see three whole pixels in the grey box, plus the 1 phsyical pixel = 1/4 of a vertically expanded sprite pixel, an the lower sprite pixel block, just above the A, ends exactly on the bottom of the grey box. That is, we have 1/4 + 1 + 1 + 1 + x = 4 pixels. Thus x must be 3/4 of a pixel high, and must therefore be missing only the bottom physical pixel.
This means that it is our scan-doubling that is the problem. Looking at the code, the end of sprite is determined based on reaching the last pixel in the sprite. However, since multiple physical rasters may have the same VIC-II raster number, we should instead check for the first raster after the end of the sprite, and stop drawing there, instead. Again, once identified, the (hopeful) fix, is quite simple, and will be thrown into the next synthesis run.
While I am here working on sprites, it also gives me the opportunity to re-test some of the MEGA65 enhanced sprite modes, to make sure that those still work.
We have:
Super-hires sprites (enabled for all by bit for in $D054), which also doubles the sprite X resolution. We will be using these in the special MEGA65 version of GEOS, so that the mouse-pointer is also hi-res, and can point at any pixel. If you use it with multi-colour mode, it effectively gives you 320x200 4-colour sprites. Yup, super-hires sprites still seems to work.
Extended height mode (enabled with one bit per sprite in $D055, and the height /of the sprites set by $D056, up to 255 pixels high). Yup, that seems to work.
Extended width mode (64 pixels wide, instead of the usual 24, enabled per sprite by bits of $D057). Yup, that too. So far so good. This mode will also be useful with the super-hires mode and multi-colour mode, to allow sprites to be 32x21 (or 32 x extended sprite height) hi-res pixels in size, but with three colours.
Bitplane mode sprites (sprite modifies one of the bits in output colour, instead of setting colour, enabled by upper bits in $D049 and $D04B). Yup, that, too.
And finally, horizontal tiling mode, where the same 24 or 64 bits of sprite data are repeated until the edge of the screen is reached. Yup, that works, and also confirms that bitplane mode sprites (for better or for worse) currently ignore the presence of the border.
I thought I had also implemented 16-colour sprite mode, where 4 bits are used together for each pixel (so they will be either 6 or 16 pixels wide, depending on whether extended width mode is used), but apparently not. I might have to revisit that, as it won't be hard to add, but will certainly make the 8 sprites we have go further, and will also make it possible to have interesting colourful backgrounds when used with tile mode.
I'd be interested to hear how people think they might use these enhanced sprite modes, or whether there is something obvious that I have missed.
But for now, it is the impatient wait while these changes synthesise, and hopefully, fix those annoying display glitches.
Subscribe to:
Posts (Atom)

































