A miracle when it works first time!

The place for codemasters or beginners to talk about programming any language for the Spectrum.
User avatar
Seven.FFF
Manic Miner
Posts: 341
Joined: Sat Nov 25, 2017 10:50 pm
Location: USA

Re: A miracle when it works first time!

Post by Seven.FFF » Fri Dec 06, 2019 4:02 pm

The wrapping paper is a classic runaway stack. Rogue bytes get executed as code, and it goes into a loop pushing pairs of bytes all the way down memory from the head of the stack to the bottom of memory, passing through the attributes and then the pixels as it goes :)
1 x
Robin Verhagen-Guest
SevenFFF / Threetwosevensixseven / colonel32
seven-fff.com/blog

User avatar
Cosmium
Berk
Posts: 42
Joined: Tue Dec 04, 2018 10:20 pm
Location: USA

Re: A miracle when it works first time!

Post by Cosmium » Wed Dec 11, 2019 3:53 am

Seven.FFF wrote:
Fri Dec 06, 2019 4:02 pm
The wrapping paper is a classic runaway stack. Rogue bytes get executed as code, and it goes into a loop pushing pairs of bytes all the way down memory from the head of the stack to the bottom of memory, passing through the attributes and then the pixels as it goes :)
I bet you're right! I fixed the bug a while ago, but I do remember there was a lot of tricky stack handling going on in a loop, and I ended up simplifying by storing to memory locations instead. But I must say the 'wrapping paper' crash did feel like a timely festive treat when it happened :lol:
1 x

User avatar
Sokurah
Microbot
Posts: 115
Joined: Tue Nov 14, 2017 10:38 am

Re: A miracle when it works first time!

Post by Sokurah » Wed Dec 11, 2019 11:14 am

Cosmium wrote:
Sat Oct 19, 2019 9:45 pm
Actually it showed "(c) 1B82...". Wasn't even a number. Weird!
Sure it is. It's 7042 in decimal ;)
0 x
Website: Tardis Remakes / Mostly remakes of Arcade and ZX Spectrum games.
My games for the Spectrum: Dingo, The Speccies, The Speccies 2, Vallation & Sqij.
Twitter: Sokurah

User avatar
R-Tape
Site Admin
Posts: 2654
Joined: Thu Nov 09, 2017 11:46 am

Re: A miracle when it works first time!

Post by R-Tape » Wed Dec 11, 2019 12:46 pm

I had a nightmare of a bug, that I solved last night. A compressed screen was decompressing into a right mess, and it wasn't always the same screen, or the same way. It turned out that I'd pasted a bit of code and forgot that it loaded a zero into an absolute memory address high in memory. So as the code built up, I got different effects in different places each time. Even though it was kind of fun to track it down, I should never have made that mistake in the first place. Argh!
0 x

User avatar
Ast A. Moore
Dynamite Dan
Posts: 1309
Joined: Mon Nov 13, 2017 3:16 pm

Re: A miracle when it works first time!

Post by Ast A. Moore » Thu Dec 12, 2019 5:53 pm

Oh, I just realized that Quadron didn’t run on the +2A/+3, so I developed a bugfix for it. I only tested the demo version, so @Cosmium, if you’re interested, PM me for details. I believe my fix should make it compatible with all Spectrums in all modes (48 and 128K).
0 x
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.

User avatar
Cosmium
Berk
Posts: 42
Joined: Tue Dec 04, 2018 10:20 pm
Location: USA

Re: A miracle when it works first time!

Post by Cosmium » Fri Dec 13, 2019 12:16 am

Ast A. Moore wrote:
Thu Dec 12, 2019 5:53 pm
Oh, I just realized that Quadron didn’t run on the +2A/+3, so I developed a bugfix for it. I only tested the demo version, so @Cosmium, if you’re interested, PM me for details. I believe my fix should make it compatible with all Spectrums in all modes (48 and 128K).
Nice! PM'd :)
0 x

User avatar
Cosmium
Berk
Posts: 42
Joined: Tue Dec 04, 2018 10:20 pm
Location: USA

Re: A miracle when it works first time!

Post by Cosmium » Fri Dec 13, 2019 12:23 am

R-Tape wrote:
Wed Dec 11, 2019 12:46 pm
I got different effects in different places each time.
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"!
1 x

User avatar
PROSM
Microbot
Posts: 117
Joined: Fri Nov 17, 2017 7:18 pm
Location: Sunderland, England
Contact:

Re: A miracle when it works first time!

Post by PROSM » Fri Dec 13, 2019 8:36 pm

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. :lol:

EDIT: Changed wording slightly for readability
0 x

User avatar
djnzx48
Manic Miner
Posts: 582
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: A miracle when it works first time!

Post by djnzx48 » Sat Dec 14, 2019 3:58 am

How did the interrupt interfere with the LDIR? Did the interrupt routine overwrite the first byte of the keyboard buffer, which then propagated to the rest of the buffer?

Semi-related, this article shows one inventive way to achieve atomic operations on a single-processor system.
1 x

User avatar
PROSM
Microbot
Posts: 117
Joined: Fri Nov 17, 2017 7:18 pm
Location: Sunderland, England
Contact:

Re: A miracle when it works first time!

Post by PROSM » Sat Dec 14, 2019 8:11 am

djnzx48 wrote:
Sat Dec 14, 2019 3:58 am
How did the interrupt interfere with the LDIR? Did the interrupt routine overwrite the first byte of the keyboard buffer, which then propagated to the rest of the buffer?
It did overwrite the entire keyboard buffer, but only the 5th byte had any keys depressed (this contains the 6-0 row in the buffer, which is used for movement in ZAD games). After the interrupt returned, the LDIR continued, and it happened to contain the 5th byte's address, so the 6-0 row's inputs were duplicated across the rest of the buffer (which happened to be the right hand side of the keyboard).

It was bizarre how the timings aligned just so, because if there had been any other address in HL, the bug would've manifested itself as a complete loss of control of the player character.
0 x

Post Reply