Cosmium wrote: ↑Fri Dec 13, 2019 12:23 am
Those are the hardest to find and fix aren't they? And when you finally trace it back to the root of the bug, there's usually some incorrect assumption made (like in this case) causing that head-slapping moment of "oh of course"!
They can be pretty fun to figure out though, even if they do waste time. For instance, I had a right corker of a bug in
Postie's Peril at one point, where at a specific point on a specific screen, while the character was walking right, the inventory list would appear for no reason. No crashes, just the inventory popping up out of nowhere, at this precise spot. This bug occurred
nowhere else in the game.
It turned out that the problem was in ZAD's keyboard driver. You see, I had written a custom interrupt routine to record each key that was depressed, 50 times a second, so that during the game loop, the main program could handle the inputs in its own time without a short key-press falling between the cracks, since without the interrupt, the keyboard would only be polled at the speed of the game (which can be adjusted to as low as 8 fps).
At the end of each game loop, the main program would reset the temporary keyboard matrix for the next loop, so that released keys were not carried over into the next loop. This reset was performed using an LDIR operation (you can probably see where this is going).
It turned out, that at this very specific spot, the timings aligned so that the interrupt was triggered during the keyboard reset routine. The new data is read into the matrix, the interrupt returns, and the LDIR starts to copy keyboard data down the matrix, making it look like other keys have been pressed. Incredibly, it so happened that the HL register pointed to the data for the keyboard row controlling movement at this specific spot. It got copied to the rest of the keyboard matrix, which, to the input handler, made it look like one of the inventory modes had been activated
The fix? Disable interrupts during the matrix reset. Why I didn't do this to start with is beyond me, but it wasted a good three evenings' worth of coding.
Oh well.
EDIT: Changed wording slightly for readability