You know it's getting good when I fire up Visio, I no longer know of a test condition that fails:
4-Bit counter (M2 falling edge):
(Basically: In sprite state, counts to 15 clocks before switching to background state on the 16th clock.)
- INC if "Sprite" && (4-bit != 15) && (PA12 = 0)
- CLEAR if (PA12 = 1)
8-bit counter (M2 falling edge):
(Basically: Happens at the transition from background to sprite.)
- RELOAD if "Background" && (PA12 = 1) && "Reset Inactive" && "Reload"
- DEC if "Background" && (PA12 = 1) && "Reset Inactive" && "Don't Reload"
This FINALLY captures the very counter-intuitive fact that we get an extra scanline when there are too many M2 cycles with PA12 = 0. It is having to do with sending a reset. Way back from early on, we knew that the number of M2 cycles with PA12 low, during which the reset was sent, had to be WITHIN
a range, or else we would get the extra scanline. Too many or too few would give an extra scanline. The way I have captured that is to give the reset its own separate state machine.
I can't say I have a great understanding of this. Here is my take of what is going on based on the data dumped out from the simulation.
Say that we are in sprite state, PA12 = 0 for some cycles, but less than 16 of them, so we're still in sprite state. Then we send the reset command, which latches us into reset active state. From this point we can do 1 of 2 things:
- Lots more cycles with PA12 = 0. This will return us to background state. Then the next time that we do a cycle with PA12 = 1, we will return to sprite state, but it will not decrement the 8-bit counter because it is still stuck in reset.
- We have a cycle with PA12 = 1 before exiting sprite state. This clears the reset state EARLIER than the first option, so it is able to decrement the 8-bit counter sooner.
So, there are LOTS of conditions on the transition to "IRQ triggered". Some of these are likely artifacts of tweaking and may not need to be there, or possibly causing undiscovered problems. I need to do some more testing and come up with a plan to retest as I pull things out that might not need to be there. Also, interestingly, my reset active to inactive transition suggests that it may look at PA12 on the rising edge of M2 for that transition. Not sure how I feel about that.