User Tools

Site Tools


base:making_a_counter

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

base:making_a_counter [2015-04-17 04:32] (current)
Line 1: Line 1:
 +A quite nifty example of how to make an increasing decimal counter with a flexible amount of digits.
 +It's possible to create a counter with up to 255 digits.
  
 +Useful for counting scores in games, for example.
 +
 +<code>
 +; Counter code
 +; [c]2007 Scout/Silicon Ltd.
 +
 +numdigits = 6
 +
 +
 +      *=$c000
 +
 +      ldx #0
 +      txa         ; A=X=0
 +-
 +      sta tellertabel,  ; erase the countertable
 +      inx
 +      cpx #numdigits      ; for the amount of desired digits
 +      bne -
 +
 +loop
 +
 +      ldx #0
 +-
 +      lda tellertabel+1,  ; read the contents of the countertable
 +      clc
 +      adc #$30      ; add 48(decimal) -> in petscii, 48=0, 49=1 etc 
 +      sta $0400,     ; poke them in the upperleft of the screen (address 1024)
 +      inx
 +      cpx #numdigits
 +      bne -
 +
 +      jsr teller      ; do the counter
 +
 +      jmp loop      ; do the loop
 +
 +;----------------------------------------------------------------
 +
 +teller
 +      ldx #numdigits      ; go from back to front in the table
 +-
 +      lda tellertabel,  
 +      cmp #9         ; is the current digit 9?
 +      beq +         ; yes, jump to the + branch   
 +      inc tellertabel,  ; no, keep on counting
 +      rts         ; back to the mainroutine
 ++
 +      lda #0         ; put current digit to 0
 +      sta tellertabel,  ; in the countertable
 +      dex         ; go 1 back in the table
 +      bne -         ; and do the rest of the remaining digits
 +      rts         ; back to the main routine
 +;-------------------------------------------------------------------
 +
 +.align 256
 +tellertabel   ; 256 bytes reserved for the countertable 
 +</code>
base/making_a_counter.txt ยท Last modified: 2015-04-17 04:32 (external edit)