Re: [stella] Help (Timing problems?)

Subject: Re: [stella] Help (Timing problems?)
From: Erik Mooney <erik@xxxxxxxxxx>
Date: Mon, 30 Jul 2001 22:17:26 -0400
On Mon, 30 Jul 2001 00:37:18 -0400, you wrote:

>BTW, I was trying to get a fairly optimal way to read diagonals from the 
>joystick - anyone out there see how I can improve this approach or has a 
>different take, I would really like to see...

No real easy way to do it; yours looks okay.

>T64_VBLANK		EQU	44	; are these values correct?
>T64_OSCAN		EQU	34	; 

That first TIM64 interval (rounded up) 38 is scanlines, and the second is
29, so if those plus your kernel plus 3 lines of VSYNC equals 262, you're

>; I believe Andrew Davie wrote the mem clear code. Thanks!
>	LDX #0
>	TXS 
>	TXA 
>	PHA 
>	DEX 
>	BNE	.Clear
>	JSR	ResetGameVars

Well, it's a trivial routine, but that does seem a byte or two more
optimized than the standard version by using PHA instead of STA 0,X.

>	LDA	#T64_VBLANK		; total number of cycles for 37 scan-line VBLANK period
>	STA	TIM64T		;Set the timer for start of screen
>	;-----------------------------------------------
>	; VBlank Starts (37 scanlines, 2812 cycles)
>	; Note: Is the cycle count (scanlines * 76) correct?

Actually, your timer is 44*64 = 2816 cycles, which will expire four cycles
into the 38th scanline.  However, you seem to have another problem.
Unless I'm missing it, you have no loop to wait for the end of VBLANK
(should look like Wait: LDA TIM64T / BNE Wait).  The emulators will
forgive that and start displaying on the PC screen as soon as you write
the zero to VBLANK, but a real TV on a 2600 video signal won't.

>	LDA	Player1X	
>	LDX	#0
>	JSR SetMotionRegsX 	; 1 scan line here
>	LDA	ZColors+1		; Color of player1
>	STA	COLUP0		; Save the color
>	STA	WSYNC	;3		; 2nd here...

Here is your problem.  When the player is too far to the right of the
screen, SetMotionRegsX will end the STA RESP0 instruction too late.  After
that you have 12 cycles of instructions before you WSYNC (RTS takes 6 and
then that LDA/STA 3 each) and that will run onto the next scanline.

Here's a fairly standard positioning routine.

    sta HMCLR
    sta WSYNC           ;begin scanline
    ldx Player0Position ;+3  3
    lda HorzTable,X     ;+4  7
    sta HMP0            ;+3 10
    and #$0F            ;+2 12
    tax                 ;+2 14
P0  dex                 ;+2 16
    bpl P0              ;when branch not taken: +2 (18 + x*5)
    sta RESP0           ;(21 + x*5) NOW!
    sta WSYNC
    sta HMOVE

The routine is compact enough to fit entirely in a single scanline except
for the STA HMCLR, which means you can position both players in
consecutive scanlines with two copies of it (the HMCLR and HMOVE need not
be repeated.)  Typically, you will change the player color each line
during the loop to draw the players, so you need not load/store its color

In general, JSR/RTS at six cycles each are a huge time penalty for the
2600 and should almost always be avoided during your screen kernels.  Use
this inline with no JSR and you'll do much better.  (The traditional
programming rules of modularize and proceduralize go right out the window
here.  :) )

Archives (includes files) at
Unsub & more at

Current Thread