Double buffering on the Spectrum

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Nomad
Manic Miner
Posts: 600
Joined: Thu Dec 28, 2017 12:38 pm

Re: Double buffering on the Spectrum

Post by Nomad »

It is amazing that even years later stuff like this is coming out. It makes you wonder how many other techniques are just waiting to be rediscovered/discovered.

I guess that is why its important for there to be a true fpga implementation of a spectrum for stuff like this in the future. Otherwise you will have emulators that will not implement stuff that was possible on the original hardware just that nobody thought was possible or even knew existed.
Wall_Axe
Manic Miner
Posts: 492
Joined: Mon Nov 13, 2017 11:13 pm

Re: Double buffering on the Spectrum

Post by Wall_Axe »

does the floating bus trick basically allow you to know where the raster beam is at any time?

so if the static part of the game (e.g. a logo) is on the bottom of the screen..you wait until the beam is on the top of the logo before updating the game above?
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: Double buffering on the Spectrum

Post by Ast A. Moore »

Nomad wrote: Sun Apr 15, 2018 5:01 pm I guess that is why its important for there to be a true fpga implementation of a spectrum for stuff like this in the future. Otherwise you will have emulators that will not implement stuff that was possible on the original hardware just that nobody thought was possible or even knew existed.
You hit the nail on the head. It’s a vicious circle: emulator authors primarily focus on adding “features,” leaving the actual emulation at the “good enough” stage. Modern-day Spectrum developers then code for those emulators, rather than the actual Spectrum. Rinse and repeat. This applies to hardware emulators, too, by the way. In essence, they’re little more than Spectrum clones, except that most of them don’t even use the Z80.
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
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: Double buffering on the Spectrum

Post by Ast A. Moore »

Wall_Axe wrote: Sun Apr 15, 2018 6:00 pm does the floating bus trick basically allow you to know where the raster beam is at any time?

so if the static part of the game (e.g. a logo) is on the bottom of the screen..you wait until the beam is on the top of the logo before updating the game above?
Well, yes and no. The trick takes advantage of the fact that you can fetch the value that the ULA is currently reading from contended memory. If that value happens to be the bitmap or attribute byte of the display file, you can then proceed to treat this information as you see fit.

Catching a bitmap byte is typically of little use—you risk running into too many false positives. An attribute byte is a whole different ballgame. You can fill an area of the screen with a unique attribute byte and wait for that byte to appear on the data bus.

So, technically, you’re not so much following the beam as the data that is being fetched by the ULA. That’s the good part. The downside is that the only piece of information available to you is the value that the ULA reads. Unless the value itself is unique, you can’t know for sure where the beam is exactly.
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.
Wall_Axe
Manic Miner
Posts: 492
Joined: Mon Nov 13, 2017 11:13 pm

Re: Double buffering on the Spectrum

Post by Wall_Axe »

so you put a color(attribute) near the bottom of the screen so you know where the beam is?

so my logo could have a line of magenta and no magenta used in the game?
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: Double buffering on the Spectrum

Post by Ast A. Moore »

Wall_Axe wrote: Sun Apr 15, 2018 7:07 pm so you put a color(attribute) near the bottom of the screen so you know where the beam is?

so my logo could have a line of magenta and no magenta used in the game?
Yes, except it’s the full attribute byte, not just a particular color. You can have, say, a non-bright magenta paper and black ink as your sync zone, but are free to use bright magenta paper and black ink anywhere else on the screen. Or use bright black on black (which is all but invisible). Or set Bit 7 (flashing) and use the same colors for paper and ink.
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
fenderjaguar
Drutt
Posts: 23
Joined: Wed Mar 07, 2018 11:48 am

Re: Double buffering on the Spectrum

Post by fenderjaguar »

Even when I was a kid, I remember playing batman the movie and thinking how no sync on 50hz with all that tearing (at 12.5 or 25 fps, not sure which) was like eye cancer. Especially on the batwing level, I nearly had a seizure :lol:
User avatar
djnzx48
Manic Miner
Posts: 729
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Double buffering on the Spectrum

Post by djnzx48 »

I read that floating bus thread a while ago and I'm still not entirely sure on how it works. Could someone give a quick rundown from the programmer's perspective on how it differs from the standard floating bus and how you would use it in a game?
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: Double buffering on the Spectrum

Post by Ast A. Moore »

djnzx48 wrote: Mon Apr 16, 2018 9:57 am I read that floating bus thread a while ago and I'm still not entirely sure on how it works. Could someone give a quick rundown from the programmer's perspective on how it differs from the standard floating bus and how you would use it in a game?
I think I did just that a couple of posts back. Also, what do you mean by “the standard floating bus”?

To reiterate (what I have already reiterated, I believe):

1. You create a wait loop that reads from a particular port, expecting a particular value (say, a screen attribute byte).
2. When the value read from the port matches the expected value, you exit the loop.

The time you exit the loop will roughly correspond to (plus some delay, naturally) the time the ULA was reading that value from the display fly, which will in turn correspond to the electron beam passing that area of the “physical” screen.

EDIT: I think I know what you mean by “the standard floating bus”—the floating bus on the 48K/128K/+2 as opposed to the +2A/+3. If there’s enough interest in the subject, I’ll do a separate writeup, something along the lines of “The Definitive Guide to Using the Floating Bus” with an actual example and make a separate post about it.
Last edited by Ast A. Moore on Mon Apr 16, 2018 10:24 am, edited 1 time in total.
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
djnzx48
Manic Miner
Posts: 729
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Double buffering on the Spectrum

Post by djnzx48 »

Sorry, I was meaning to ask how the +2A/+3 floating bus differs from on the Sinclair machines.
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: Double buffering on the Spectrum

Post by Ast A. Moore »

djnzx48 wrote: Mon Apr 16, 2018 10:21 am Sorry, I was meaning to ask how the +2A/+3 floating bus differs from on the Sinclair machines.
Yup. Got it. See my edit above . . . uh . . . the previous page, as of now. ;)
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
Ersh
Manic Miner
Posts: 480
Joined: Mon Nov 13, 2017 1:06 pm

Re: Double buffering on the Spectrum

Post by Ersh »

Ast A. Moore wrote: Mon Apr 16, 2018 10:15 am... If there’s enough interest in the subject, I’ll do a separate writeup, something along the lines of “The Definitive Guide to Using the Floating Bus” with an actual example and make a separate post about it.
Please do. :)
User avatar
utz
Microbot
Posts: 114
Joined: Wed Nov 15, 2017 9:04 am
Contact:

Re: Double buffering on the Spectrum

Post by utz »

Ersh wrote: Mon Apr 16, 2018 10:50 am
Ast A. Moore wrote: Mon Apr 16, 2018 10:15 am... If there’s enough interest in the subject, I’ll do a separate writeup, something along the lines of “The Definitive Guide to Using the Floating Bus” with an actual example and make a separate post about it.
Please do. :)
Here's some more interest ;)
Wall_Axe
Manic Miner
Posts: 492
Joined: Mon Nov 13, 2017 11:13 pm

Re: Double buffering on the Spectrum

Post by Wall_Axe »

if you put the still stuff like score and logo in the top third of the screen
and simply used interrupt mode 2,
to use the time the raster takes to traverse the first third of the screen..
to draw the sprites on the bottom two thirds

how is that better/worse than...

putting the still stuff in the bottom third of the screen
waiting for floating bus to indicate raster is two thirds of the way down
drawing sprites in first two thirds of screen


it seems like you still get the same amount of time to draw stuff?

does the floating bus tell you when the raster is in the border? that would give more time to draw sprites
spectron
Drutt
Posts: 25
Joined: Thu Mar 29, 2018 3:27 pm

Re: Double buffering on the Spectrum

Post by spectron »

As already mentioned, there is no definitive answer.

Being pedantic I'd like to leave the double buffering term for the 128k ability to swap memory whereas I'd always call the term of writing off screen then copying to the visible screen as back buffering. But that's a moot point.

Most solid 3d stuff wrote to aback buffer (starstrike2 for example) and so did games like Elite. I used it to just draw the top 3d section for the conversion of Accolade's Gunboat and from memory used the stack to copy the data and also rearranged the off screen buffer to a more sensible layout with each line following the other in memory.

When doing the 100m and hurdles in Summer games, the code blitted the bottom sprites straight after the vblank then the top sprites later once the beam had gone down the screen. It did initially cause issues because the code was written in top memory but then the other coder combining all the events together assembled it into contended memory so everything started to flicker
Wall_Axe
Manic Miner
Posts: 492
Joined: Mon Nov 13, 2017 11:13 pm

Re: Double buffering on the Spectrum

Post by Wall_Axe »

" used the stack to copy the data and also rearranged the off screen buffer to a more sensible layout with each line following the other in memory."

this does sound like the best way for a lot of games
AndyC
Dynamite Dan
Posts: 1388
Joined: Mon Nov 13, 2017 5:12 am

Re: Double buffering on the Spectrum

Post by AndyC »

spectron wrote: Mon Apr 16, 2018 9:00 pm Being pedantic I'd like to leave the double buffering term for the 128k ability to swap memory whereas I'd always call the term of writing off screen then copying to the visible screen as back buffering. But that's a moot point.
Strictly speaking, double buffering is any technique which uses a single back buffer to draw in that is then used to update the screen (with triple buffering being the same but with two buffers and the main screen). The "proper" term for a double or triple buffer which uses a hardware switch to swap the buffers over is page flipping.
User avatar
MonkZy
Manic Miner
Posts: 278
Joined: Thu Feb 08, 2018 1:01 pm

Re: Double buffering on the Spectrum

Post by MonkZy »

Thank you all for this incredibly illuminating thread.

I have successfully coded a working sinus-scroller, although currently it flickers rather badly. My method could be described as placing 28 sprites along a pre-calculated sine wave (i used BASIC to create a lookup table which I copied into my source code). Each character/sprite memory address is calculated real time and the character is bit-shifted into position a line at a time, no lookup-tables. After a HALT I clear all the characters (by ld'ing 0's into RAM over each character position) and start over. I am starting to think I need some serious optimisation or possibly more lookup tables to achieve a true 50hz scroller. I most certainly will need to work out the floating bus method of sync'ing. Using HALT you waste the time the raster takes to fill the bottom border, and you are forced to place your scrolling text at the bottom of the screen.

I will continue, armed with more knowledge after reading this thread.
dfzx
Manic Miner
Posts: 673
Joined: Mon Nov 13, 2017 6:55 pm
Location: New Forest, UK
Contact:

Re: Double buffering on the Spectrum

Post by dfzx »

Wall_Axe wrote: Mon Apr 16, 2018 7:52 pm if you put the still stuff like score and logo in the top third of the screen
and simply used interrupt mode 2,
to use the time the raster takes to traverse the first third of the screen..
to draw the sprites on the bottom two thirds

how is that better/worse than...

putting the still stuff in the bottom third of the screen
waiting for floating bus to indicate raster is two thirds of the way down
drawing sprites in first two thirds of screen


it seems like you still get the same amount of time to draw stuff?

does the floating bus tell you when the raster is in the border? that would give more time to draw sprites
My understanding, which might stand correction :) :

using IM2 tells you when the raster is at the start of the top border, so from there you get the time it takes to draw the top border, plus the time it takes to draw your static stuff at the top of the display.

Using the floating bus, you get the time it takes to draw your static stuff at the bottom of the display, plus the lower border, plus the top border.

So floating bus gives you more time, and has the advantage that your play area is at the top of the screen, which is normally aesthetically better.
Derek Fountain, author of the ZX Spectrum C Programmer's Getting Started Guide and various open source games, hardware and other projects, including an IF1 and ZX Microdrive emulator.
giorgio.denunzio
Drutt
Posts: 1
Joined: Tue Apr 17, 2018 9:11 am

Re: Double buffering on the Spectrum

Post by giorgio.denunzio »

Hi all!
I've just registered to this interesting Forum, and I'have not read the whole thread yet, so perhaps the information I am giving you is of little interest, but in these days there has been a discussion on the floating-bus subject at https://www.facebook.com/groups/z80asm/ ... 271378839/
I am conducting some tests, at present on the ZX48k (emulated, by now). In case you are interested, please take a look. On the other hand I'll read with attention this thread, which looks really interesting.
Thanks
Giorgio
Ralf
Rick Dangerous
Posts: 2279
Joined: Mon Nov 13, 2017 11:59 am
Location: Poland

Re: Double buffering on the Spectrum

Post by Ralf »

Hi Giorgio! Welcome to the forums!

This discussion about floating bus is certainly very interesting.

I would have one advice to beginner programmers however. Don't mess with with floating bus ;) It's a rather advanced trick. Joffa games were generally a piece of quite sophisticated code.

A "normal", typical game did it this way:

1) It keeps a screen buffer somewhere in memory
2) It draws to this buffer as fast as it can but usually longer than one frame
3) When it's ready it copies the buffer to screen by series of LDI commands without any synchronisation with screen refresh

Copying buffer to screen may alone take more than one frame so some artifacts on screen may happen. And the final framerate was around 12.5 fls or so.

It all may sound very rough and crude but in practice it worked and the result was acceptable.
Nomad
Manic Miner
Posts: 600
Joined: Thu Dec 28, 2017 12:38 pm

Re: Double buffering on the Spectrum

Post by Nomad »

Did Joffa Smith ever talk about or write on how he wrote his games?
Ralf
Rick Dangerous
Posts: 2279
Joined: Mon Nov 13, 2017 11:59 am
Location: Poland

Re: Double buffering on the Spectrum

Post by Ralf »

Did Joffa Smith ever talk about or write on how he wrote his games?
He made some comments here and there but not very detailed.
In my opinion he was generally a very modest man. He would probably say you that all his tricks were nothing special and not worth to get excited about.
Nomad
Manic Miner
Posts: 600
Joined: Thu Dec 28, 2017 12:38 pm

Re: Double buffering on the Spectrum

Post by Nomad »

Kind of like an anti Ian Weatherburn then (personality wise). :lol:

One of the most frustrating things trying to find out about N.O.M.A.D was the main developer had left nothing behind about how he wrote the game. It was weird that for a guy that was so sure of his own genius that he didn't talk about his process more before he became An Hero after Canvas folded.
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: Double buffering on the Spectrum

Post by Ast A. Moore »

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