A miracle when it works first time!

The place for codemasters or beginners to talk about programming any language for the Spectrum.
User avatar
Cosmium
Microbot
Posts: 154
Joined: Tue Dec 04, 2018 10:20 pm
Location: USA

A miracle when it works first time!

Post by Cosmium »

Following on from the "An impressive way to crash the Speccy?" thread (viewtopic.php?f=6&t=918), I was working on a sprite clipping m/c routine and when it came to test my first attempt, while not exactly crashing, I watched it spectacularly fail as the sprite "exited" the top of the screen :lol:

Image

It occurred to me that not only do assembly routines often fail the first time you run them, they often fail in quite dramatic fashion!

There are those rare (but memorable) occasions when it does work first time but anyone else captured any amusing pseudo-crashes when it doesn't?

BTW, I got it going the next day, though the result seemed a bit boring and pedestrian in comparison :)

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

Re: A miracle when it works first time!

Post by R-Tape »

Heh nice. I've had a few firework displays like that. I'll see if I can find them later. It is usually for the same reason - sometimes I even see the sprite data walking around the attributes, and can safely walk it back up to the display file.

It's not quite the same, but something I keep doing when starting my code is hurriedly typing RANDOMIZE USR 3268 (I think) instead of 32768. I get a flashing dashed line at the screen bottom - I must have done it hundreds of times!
Ralf
Rick Dangerous
Posts: 2279
Joined: Mon Nov 13, 2017 11:59 am
Location: Poland

Re: A miracle when it works first time!

Post by Ralf »

Do you mean by "sprite clipping" a situation where a sprite emerges from a side of a screen so you draw only visible part of it?

If so then it's a troublesome job. If you put clipping into your sprite drawing routine then unfortunately it'll become slower and more complicated.
In many cases you may consider the effect is not worth the cost and just draw full sprites when they appear at the screen edge.

If you use a screen buffer in memory then a common trick is to make the buffer a bit wider than the visible screen. So you never clip sprites when you draw them into the buffer but when you copy buffer to the visible screen then you do it without the edges and the sprites will look like clipped.
AndyC
Dynamite Dan
Posts: 1388
Joined: Mon Nov 13, 2017 5:12 am

Re: A miracle when it works first time!

Post by AndyC »

A third trick is to put part of the edge of the screen as black paper and black ink, so that drawing sprites can "leak" over the edges (as long as you also reserve some memory after the display) and the effect won't be visible on screen. How well that works does depend on the size of your sprites too (or at least the granularity with which you can draw them)
User avatar
Seven.FFF
Manic Miner
Posts: 735
Joined: Sat Nov 25, 2017 10:50 pm
Location: USA

Re: A miracle when it works first time!

Post by Seven.FFF »

My favourite one is working in all-ram mode. Normally we’re used to bugs doing a reset after eventually jumping to some part of the ROM.

In all-ram mode it’s ALL your code, and it has an annoying habit or restarting your program at a random point rather than crashing. Makes debugging it hard sometimes.
Robin Verhagen-Guest
SevenFFF / Threetwosevensixseven / colonel32
NXtel NXTP ESP Update ESP Reset CSpect Plugins
User avatar
Cosmium
Microbot
Posts: 154
Joined: Tue Dec 04, 2018 10:20 pm
Location: USA

Re: A miracle when it works first time!

Post by Cosmium »

Ralf wrote: Thu Aug 29, 2019 5:28 pm Do you mean by "sprite clipping" a situation where a sprite emerges from a side of a screen so you draw only visible part of it?
Yeah!
Ralf wrote: Thu Aug 29, 2019 5:28 pm If so then it's a troublesome job. If you put clipping into your sprite drawing routine then unfortunately it'll become slower and more complicated.
In many cases you may consider the effect is not worth the cost and just draw full sprites when they appear at the screen edge.
Very true, but I thought I'd update my existing "non-clipped" sprite routine to first test for sprites partially off screen (via their coordinates) and add a branch to the new clipping code which adjusts some draw values before returning to the (mainly) unchanged main draw loop. That way there wouldn't be as much impact on the original code's performance.
User avatar
Cosmium
Microbot
Posts: 154
Joined: Tue Dec 04, 2018 10:20 pm
Location: USA

Re: A miracle when it works first time!

Post by Cosmium »

AndyC wrote: Thu Aug 29, 2019 6:12 pm A third trick is to put part of the edge of the screen as black paper and black ink, so that drawing sprites can "leak" over the edges (as long as you also reserve some memory after the display) and the effect won't be visible on screen. How well that works does depend on the size of your sprites too (or at least the granularity with which you can draw them)
Thanks for these tips. This black attribute trick was actually my plan for sprites entering/leaving the left/right sides of the screen. It's better than suddenly fully appearing at the screen's edge yet barely requires any code changes to the render loop :)

And the sprite clipping at the screen top/bottom only required changes to the sprite's source address and number of lines to draw, which was relatively easy to change and hasn't had much impact on performance.
User avatar
Turtle_Quality
Manic Miner
Posts: 502
Joined: Fri Dec 07, 2018 10:19 pm

Re: A miracle when it works first time!

Post by Turtle_Quality »

For vertical clipping , if you use a lookup table to get the address of each row, you point rows 192-255 to somewhere harmless, ROM for instance. That way there is no loss of speed. I assume you hold the y address in a single byte so -1 would be 255.
Definition of loop : see loop
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: A miracle when it works first time!

Post by Ast A. Moore »

Yup, even the release version of Exolon has a similar bug.
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
Microbot
Posts: 154
Joined: Tue Dec 04, 2018 10:20 pm
Location: USA

Re: A miracle when it works first time!

Post by Cosmium »

R-Tape wrote: Thu Aug 29, 2019 7:57 am It's not quite the same, but something I keep doing when starting my code is hurriedly typing RANDOMIZE USR 3268 (I think) instead of 32768. I get a flashing dashed line at the screen bottom - I must have done it hundreds of times!
Yeah, USR 3268 does it. Reminds me of some old ZX81 BASIC laser effect!
User avatar
Cosmium
Microbot
Posts: 154
Joined: Tue Dec 04, 2018 10:20 pm
Location: USA

Re: A miracle when it works first time!

Post by Cosmium »

Turtle_Quality wrote: Thu Aug 29, 2019 9:06 pm For vertical clipping , if you use a lookup table to get the address of each row, you point rows 192-255 to somewhere harmless, ROM for instance. That way there is no loss of speed. I assume you hold the y address in a single byte so -1 would be 255.
Hey, that's an elegant solution!

Though I think it's handy to calculate ahead of time how many lines of the clipped sprite to draw. That way you can avoid drawing more than necessary as you would if you "blindly" wrote data into ROM over the course of the full sprite height.
User avatar
Cosmium
Microbot
Posts: 154
Joined: Tue Dec 04, 2018 10:20 pm
Location: USA

Re: A miracle when it works first time!

Post by Cosmium »

So I finished writing the part where the sprite should gracefully exit the bottom of the screen. Gave it a test and sure enough there were no "it works first time" miracles on display here either. Just an encore fireworks display! :)

Image

Luckily it was an easy fix and a silly mistake (aren't they always?!). I'd incorrectly assumed a register had a certain value in it after copying and pasting code. Ugh :roll:

I wonder what percentage of programming bugs are due to unfounded assumptions..
User avatar
R-Tape
Site Admin
Posts: 6353
Joined: Thu Nov 09, 2017 11:46 am

Re: A miracle when it works first time!

Post by R-Tape »

Ast A. Moore wrote: Thu Aug 29, 2019 10:20 pm Yup, even the release version of Exolon has a similar bug.
And so does Rallybug, or at least the paid-for version does. There's very few opportunities to actually reach the outer limits, but it's happen to me.
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: A miracle when it works first time!

Post by Ast A. Moore »

R-Tape wrote: Sat Aug 31, 2019 6:58 am
Ast A. Moore wrote: Thu Aug 29, 2019 10:20 pm Yup, even the release version of Exolon has a similar bug.
And so does Rallybug, or at least the paid-for version does. There's very few opportunities to actually reach the outer limits, but it's happen to me.
What’s that, you say? A bug in Rallybug? Figures. :lol:
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
Microbot
Posts: 154
Joined: Tue Dec 04, 2018 10:20 pm
Location: USA

Re: A miracle when it works first time!

Post by Cosmium »

Capture of the latest amusing bug while optimising my sprite routine. Think it needs a bit more of work. ;)

Funny how tiny and seemingly innocuous changes in the code can lead to such graphic disaster!

Image
User avatar
Cosmium
Microbot
Posts: 154
Joined: Tue Dec 04, 2018 10:20 pm
Location: USA

Re: A miracle when it works first time!

Post by Cosmium »

Don't think I've seen this quirky one before :?

Check out the copyright year right after this latest programming crash:

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

Re: A miracle when it works first time!

Post by R-Tape »

Is that 1882?! Clive hadn't even started going bald back then.

How is that even possible? Is 'overwrite ROM' enabled in you emulator?
User avatar
Cosmium
Microbot
Posts: 154
Joined: Tue Dec 04, 2018 10:20 pm
Location: USA

Re: A miracle when it works first time!

Post by Cosmium »

How is that even possible? Is 'overwrite ROM' enabled in you emulator?
I had a look at Spin's settings: "Allow ROM editing" and "Allow assembly to ROM" are disabled, and I've since fixed the bug, so I'm not even sure how it happened..

Actually it showed "(c) 1B82...". Wasn't even a number. Weird!
User avatar
R-Tape
Site Admin
Posts: 6353
Joined: Thu Nov 09, 2017 11:46 am

Re: A miracle when it works first time!

Post by R-Tape »

Cosmium wrote: Sat Oct 19, 2019 10:45 pm Actually it showed "(c) 1B82...". Wasn't even a number. Weird!
Hex. Good to know that Sinclair research will outlive the common era!
User avatar
Cosmium
Microbot
Posts: 154
Joined: Tue Dec 04, 2018 10:20 pm
Location: USA

Re: A miracle when it works first time!

Post by Cosmium »

A few days ago, while programming some fixed-point maths routines, I was suddenly rewarded with this festive-themed crash. Just before the (c) message it kinda looks like a full screens-worth of gaudy xmas wrapping paper, if you squint hard enough!


Image
User avatar
Seven.FFF
Manic Miner
Posts: 735
Joined: Sat Nov 25, 2017 10:50 pm
Location: USA

Re: A miracle when it works first time!

Post by Seven.FFF »

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 :)
Robin Verhagen-Guest
SevenFFF / Threetwosevensixseven / colonel32
NXtel NXTP ESP Update ESP Reset CSpect Plugins
User avatar
Cosmium
Microbot
Posts: 154
Joined: Tue Dec 04, 2018 10:20 pm
Location: USA

Re: A miracle when it works first time!

Post by Cosmium »

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:
User avatar
Sokurah
Manic Miner
Posts: 283
Joined: Tue Nov 14, 2017 10:38 am
Contact:

Re: A miracle when it works first time!

Post by Sokurah »

Cosmium wrote: Sat Oct 19, 2019 10:45 pm Actually it showed "(c) 1B82...". Wasn't even a number. Weird!
Sure it is. It's 7042 in decimal ;)
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: 6353
Joined: Thu Nov 09, 2017 11:46 am

Re: A miracle when it works first time!

Post by R-Tape »

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!
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: A miracle when it works first time!

Post by Ast A. Moore »

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 [mention]Cosmium[/mention], 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).
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.
Post Reply