Re: [stella] Playfield using "inx" vs "using SLEEP"

Subject: Re: [stella] Playfield using "inx" vs "using SLEEP"
From: "Andrew Davie" <atari2600@xxxxxxxxxxxxx>
Date: Wed, 24 Aug 2005 09:33:11 -0400
Hi Fernando

Imagine we're the Atari, and we've just completed the section of code you 
understand.  Now we get to the bits you don't understand.  Here's a 
walkthrough of exactly what is happening...  remember, we are mostly drawing 
a single scanline *in synch* with the sweeping of the TV beam across the 
television.  And that this all happens in 76 cycles of 6507 time (228 colour 
clocks of TIA time).  What we're really interested with this sort of code is 
where the TV beam *is* when the code executes.  They are tightly locked, and 
that is the magic to doing interesting things on the '2600.

     sta WSYNC

OK, we've just restarted a brand new line.  The 6507 was halted, but now the 
TIA is drawing a new line, the 6507 is active again.  And we're at cycle 0 
in the sanline....

     lda #%00000000
     sta PF1
     sta PF2

We've just cleared the playfield registers #1 and 2.  That took 8 cycles of 
6507 time -- 24 cycles of TIA time.  So the TV beam is 24/228 of the way 
across its horizontal line draw (but note:  only 160 colour clocks are 
visible -- the first 68 on any line are invisible, the 'horizontal blank'. 
So at the moment, we are still well off the left of the visible screen... 
that starts at TIA cycle 68 (or 6507 cycle 68/3  --> 22.666..)

Picture

     lda #15
     sta COLUPF
     lda #%00010000
     sta PF0

We've just changed the colour of the playfield, and written a pattern (just 
one pixel on) to playfield 0.
Note that the current *horizontal position* of the TV beam can be exactly 
determined, because we have executed *exactly* 2+3+3+2+3+2+3 cycles ( = 18 
cycles of 6507 time since the start of this scanline).  Remember, there are 
76 cycles in each scanline.  We're still operating *before* the TIA starts 
drawing any visible colour clocks.

     SLEEP 2                          ; what?

The above simply does "something" that takes 2 cycles.  It doesn't matter to 
us what that is, all the macro does is ensure that the two cycles (no more, 
no less) are used.  So now we're on cycle #20 in the scanline.  There 
appears to be no particular reason for this;   I would guess that the above 
sleep could be omitted, and the following one changed to 27 with exactly the 
same visual effect.

     lda #0
     sta COLUPF

And we set the playfield colour to black.  So although the timing isn't 
really THAT crucial in this situation, whoever wrote this code wanted the 
playfield colour to be black between cycle #25  (20 +2 + 3)...

     SLEEP 25                        ; for what?
     lda #15
     sta COLUPF

... and cycle #55.
The sleep above guarantees 25 cycles after the colour is changed to black, 
and it takes another 5 cycles to load and store the new colour.  Since we 
started at cycle #25, we end up at #55.

And then...

     lda #%10000000
     sta PF2

A pattern (a single pixel on) is written to playfield 2.  If you go to the 
'Programming for Newbies' tutorial on AtariAge, you should be able to find a 
good timing diagram for the playfield, which clearly shows exactly when you 
can write values to registers for asymmetric playfields and what happens. 
If you don't know, an asymmetric playfield is where you modify the three 
playfield registers as the scanline is drawing so that you get a full 40 
pixels of playfield data, instead of the mirrored or duplicated 20 pixels. 
When you are doing this, you have to wait until the data from a register has 
completely displayed BEFORE you change the data for the 2nd use of that 
register in the scanline display.


     sta WSYNC

The above simply says "we're done with this scanline, halt till the start of 
the next one".  One should note that the playfield colour in this case is 
still #15, so that will remain in effect in the next line until it is set to 
black (which happens on 6507 cycle #25.  In other words, I would expect to 
see colour #15 on the left of the screen up until TIA clock 75 (ie: 8 pixels 
at the left of screen), then black for 90 pixels, then colour #15 again for 
the rest of the line.

     inx
     cpx #176
     bne Picture


And that keeps us going until our line counter reaches 176.

Hope this helps.  As always my timings may be wrong, but you get the idea, I 
hope.

Cheers
A

Archives (includes files) at http://www.biglist.com/lists/stella/archives/
Unsub & more at http://stella.biglist.com

Current Thread