Saturday, October 14, 2006

Waiting for the right moment!

The Spectrum is an interesting beast because it basically has no hardware and you need to do everything in software. When rendering the best way to do things is to render behind the raster that is sending everything to the TV screen.

The only synchronisation that is available is with the vertical sync of the raster, but this leaves a lot of time (in terms of machine cycles) before the start of the screen, where we want to start rendering to allow us to draw just behind the raster, if we render before hand then we will see flicker, just behind and the display will be rock solid (which is what we are aiming for).

Fortunately on the Spectrum there is a little known way that this can be done, once again a technique which was used on Cobra (a technical masterpiece), it uses the floating bus on the Spectrum (see for an explanation). The code is relatively simple

SyncToScreen: ld bc,#3f28
STS_lp: ld a,c
in a,(#ff)
cp b
jr nc,STS_lp

This routine basically waits until an attribute is being output from the ULA of the Spectrum, which if it is started during the border region of the display, then it will wait for the first line of the screen... exactly what we need!

So now our armoury of techniques is filling up I really need to get on and write some real code...

Sadly, the floating bus technique doesn't work on all Spectrums... The +2A and +3 don't support it along with some of the clone machines...
Thanks james, I have to admit I am only looking to target the 48K Spectrum.

I have a strategy though, later on I'll rejig the code to allow the use of a vertical sync without wasting any time waiting for no reason.
I have a quick question... The Commodore 128 has a 4Mhz Z80 inside it as well as a 2Mhz 6502. If you used the Z80 to write the game, would the basic code be any quicker do you think?
I think that all depends on how fast the communication is between the 2 processors, not much point if the Z80 cannot access hardware and takes ages to send results over...

So the answer is maybe... tell us more...
Not sure on any of its limitations. I know the VIC is 2mhz, so the Z80 willprobably drop down to 2 when dealing with the VIC, but thats hardly ever!

I'd need to get my C128 out and try some tests I guess.

Unfortunately the C128's Z80 only runs at 2 MHz, and the two processors can't work simultaneously either. The Z80 is essentially useless for anything but running CP/M.

You can get a fair boost by switching the 8502 to 2 MHz mode in the vertical borders though. And it has a few other neat features too like doublebuffering of the color ram, and the ability to move the zeropage/stack freely in memory (allowing you to copy memory at 6 cycles per byte with unrolled LDA/PHA loops :).

Mmm...Id read it was 4 - oh well. Still, with its 16bit registers, it might be better for some stuff. Although I accept that a 2mhz 6502 is pretty nippty anyway...

Moving Zero Page is handy - juts like the SNES.
The chip itself certainly supports 4 MHz operation, supposedly the Commodore engineers just couldn't (or wouldn't, for the conspiracy theorists listening) get it to run that fast.

After some Googling I still couldn't find any reliable answers though. Most sources seem to agree that it's crippled, just not on how badly.

I'm too lazy to test things on my own C128 though..

I have a 128D in the loft... next time I get it out, I'll have a bash :)
You'll probably have to restort to reading emulator sources to figure out how things work (although admittedly that's still easier to read than most of the documentation around).

I just played a bit with the C128's fast mode myself. 5,000 extra cycles per frame is pretty nice for a five minute fix (could've gained more if I had bothered to add another raster interrupt) :)

VICE doesn't appear to emulate the 2 MHz mode though, so I guess I'll be the only one gaining anything from it..

I think it's commonly agreed on that the C128's Z80 can't do anything faster than the 6502. In the late 90s I read some discussions on this and all the ppl involved came to the same conclusion.

Aren't there a few games that support the C128 2Mhz mode? If I'm not mistaken Andrew Braybrook did this. And I remember some demos using it, too.

Ok, back to Spectrum :) The 48k one is the one without the AY soundchip, right? Too bad :( But those AY-carts (sound boxes) seem to be pretty common in the Spectrum demo scene and maybe you can optionally support them.

I've listened too some interesting SID conversions and they weren't too bad. That way you could use Luca's music in case there's enough time left.

Z80 in C128 runs at 4 MHz for two cycles (500 ns) but is then stalled by VIC-II for the next 500 ns, so the effective speed is 2 MHz.

Andrew Braybrook used 2 MHz mode in Alleykat, Heavy Metal Paradroid and Morpheus. I'm not sure about Uridium +, Intensity and Gribbly's Special Day out but it's easy to find out with VICE. Paradroid Competition Edition and anything before that didn't use 2 MHz.

There are several demos using it, check out pretty much any Mandelbrot/Julia set part.

Does it still get stalled in the borders when the vic isn't doing anything?

How quick is it to kick in and out of Z80 mode?
VIC-II is never really idle (open bottom border and change $3fff to verify it reads memory constantly) so Z80 halting is tied to ΓΈ2 clock directly. That means it is 2 MHz even if you turn off VIC-II display.

If you have your memory setup ready then switching processor requires only resetting (Z80 active) or setting (8500 active) bit 0 of $d505.

Thats not too bad. For 16bit maths, Z80 will probably still win because of the 16bit registers and because it has more of them!

Not great.... but not terrible.
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?