[stella] Why Timers refuse to work the way they should?

Subject: [stella] Why Timers refuse to work the way they should?
From: Rodrigo Silva <stella@xxxxxxxxxxxxx>
Date: Sat, 20 Sep 2003 19:00:14 -0300
Hi again!

This time im stretchin my newbie brain muscles creating (or ate least trying to)
a perfect blank template. Trying to be really careful in every detail, as im
plannng to use this template for all demos, tutorial and exercices from now on
(or at least until the source gets so big i need to break it in modules)


So, after digging hints here and there (like the variables section, 100% extracted from
Andres tutorial), I present you with my 1st attempt of the perfectly clean, standard,
modern template for simple 2600 programs. 73 bytes that do absolutely
nothing :) Please feel absolutely free to make any corrections, suggestions, recomendations, whatever, about style, conventions, optimizations, anything you think is
worth pointing out.


And I already some questions (the main reason for posting a file that doesnt work :)

- Altought things look nice, this code generates 260 scanlines. Why are there missing 2? Replacing the timers for scanline counting loops, i figured there is one scanline missing in vblank section and the other in overscan. The very places im using timers instead of a counter. What am i doing wrong?

- Is there any way to accuratly find out how many time cycles are left in each section? (marked with XXXX till i find out). Every attempt i made using cycle-controled loops gave different or weird results. For example... after finding the maxmum number of cycles for 262 scanlines, fine-tuned by SLEEP n, if i change it to SLEEP n+1, scanlines go up to 280!! Wow, 18 scanlines for a single cycle!

Im completely clueless... and i swear i tried hard before bugging you...

Cya,
Rodrigo
; Copyleft 2003 By Rodrigo E.S. Silva - atari@xxxxxxxxxxxxx
; Under GPL - GNU Public Licence - http://www.fsf.org/copyleft/gpl.html
; You may copy, distribuite, use, edit, sell, whatever, as long as such freedom
; is propagated in evey version and my credit for original work is kept
; "GPL = Freeware that should remain Free"

                   processor 6502      ; Atari 2600 CPU
                   include "vcs.h"     ; TIA Definitions
                   include "macro.h"   ; Useful Functions

;///////////////////////////////////////////////////////////////////////////////

                   SEG.U Variables
                   ORG $80

;/////////////////// Global Variables
GlobalDummy        ds 0

;------------------------------------------------------------------------------
; OVERLAYS!
; These variables are overlays, and should be managed with care
; That is, variables are ALREADY DEFINED, and we're reusing RAM for other
; purposes
; EACH OF THESE ARE VARIABLES (TEMPORARY) USED BY ONE ROUTINE (AND IT'S
; SUBROUTINES) THAT IS, LOCAL VARIABLES.  USE 'EM FREELY, THEY COST NOTHING
; TOTAL SPACE USED BY ANY OVERLAY GROUP SHOULD BE <= SIZE OF 'Overlay'
;------------------------------------------------------------------------------
Overlay            ds 0  ; overlay (share) variables (make sure this is as big
                         ; as the biggest overlay subsection)
;/////////////////// 1st set of Overlay Variables - repeat block as necessary
                   ORG Overlay
ov1_Dummy          ds 0


 ECHO [*-$80]d,"out of 128 RAM bytes used",[$100-*]d,"bytes left"
;///////////////////////////////////////////////////////////////////////////////
                   SEG
                   ORG $F000

Reset
                   CLEAN_START         ; Clear RAM and all TIA registers
                                       ; Now A = X = Y = 0

         ;----------------------------------------------------------------------
         ; Once-only initialization... take your time



         ; End of once-only initialization
         ;----------------------------------------------------------------------

StartOfFrame       lda #%00000010
                   sta VSYNC           ; Starts VSYNC
                   sta WSYNC           ; End of VSYNC Line 1
                   sta WSYNC           ; End of VSYNC Line 2
                   sta WSYNC           ; End of VSYNC Line 3
                   lda #%00000000      ; Get ready to turn off VSYNC
                   sta VSYNC           ; Turn off VSYNC

         	   lda #44	       ;
	           sta TIM64T          ; Start the timer for VBlank

         ;----------------------------------------------------------------------
         ; 37 scanlines of vertical blank... have fun with XXXX cycles


         ; End of fun
         ;----------------------------------------------------------------------

WaitEndOfVBlank    lda INTIM           ; check timer...
	           bne WaitEndOfVBlank ;
                   lda #%00000000      ; Get ready to turn off VBLANK
                   sta WSYNC

         ;----------------------------------------------------------------------
         ; Do 192 scanlines
         ; 1st line has 5 cycles less then the others ,
         ; so blew it off if kernel is too tight

                   sta VBLANK          ; Turns VBLANK off
                   ldx #192            ; scanline counter
                   ;dex                ; uncomment here and below if needed
                   ;sta WSYNC          ; (prematuraly ends 1st scanline, but
                                       ;  guarantees that all the other lines
                                       ;  have exactly the same cycle counting)

Kernel
                   ;--Go kernel, go!

                   ;----------------
                   sta WSYNC
                   dex                 ;
                   bne Kernel          ;

         ;----------------------------------------------------------------------
         ; 30 scanlines of overscan...

                   lda #%01000010      ; Get ready for Blanking
                   sta VBLANK          ; End of screen - enter blanking
                   lda #36             ;
                   sta TIM64T          ; Start Timer for Overscan

         ;----------------------------------------------------------------------
         ; Have more fun with XXXX cycles left for overscan


         ; Sorry pal, time's up - get ready for next frame
         ;----------------------------------------------------------------------

WaitEndOfOverscan  lda INTIM           ; check timer...
                   bne WaitEndOfOverscan
                   sta WSYNC

                   jmp StartOfFrame

;///////////////////////////////////////////////////////////////////////////////
 ECHO [*-$F000+6]d,"out of 4096 ROM bytes used",[$FFF9-*]d,"bytes left"

                   ORG $FFFA
InterruptVectors
                   .word Reset         ; NMI
                   .word Reset         ; RESET
                   .word Reset         ; IRQ

                   END
; EOF
Current Thread