2015-08-28 13:19:52 +00:00
|
|
|
; timer routines
|
|
|
|
;
|
|
|
|
; the timer should be a 16-bit counter that's incremented by about
|
|
|
|
; 1000 units per second. it doesn't have to be particularly accurate.
|
|
|
|
|
2015-09-09 12:04:07 +00:00
|
|
|
.include "atari.inc"
|
2018-02-23 15:36:05 +00:00
|
|
|
.include "../inc/common.inc"
|
2015-08-28 13:19:52 +00:00
|
|
|
|
|
|
|
.export timer_init
|
|
|
|
.export timer_exit
|
|
|
|
.export timer_read
|
|
|
|
.export timer_seconds
|
|
|
|
|
|
|
|
|
|
|
|
.bss
|
|
|
|
|
|
|
|
current_time_value: .res 2
|
|
|
|
current_seconds: .res 1
|
|
|
|
current_jiffies: .res 1
|
2017-09-12 12:14:14 +00:00
|
|
|
timer_freq: .res 1 ; VBLANK frequency: 50 - PAL, 60 - NTSC
|
|
|
|
timer_freq_reciproc:.res 1 ; reciprocal for timer_freq in ms: 20 - PAL, ~17 - NTSC
|
2015-08-28 13:19:52 +00:00
|
|
|
|
|
|
|
.data
|
|
|
|
|
|
|
|
vbichain: .word 0
|
|
|
|
|
|
|
|
|
|
|
|
.code
|
|
|
|
|
2017-09-25 15:06:43 +00:00
|
|
|
; reset timer to 0 and install handler
|
2015-08-28 13:19:52 +00:00
|
|
|
; inputs: none
|
|
|
|
; outputs: none
|
|
|
|
timer_init:
|
|
|
|
lda vbichain+1
|
2017-09-25 15:06:43 +00:00
|
|
|
bne @done
|
2017-09-12 12:14:14 +00:00
|
|
|
lda PAL ; hardware register describing TV system of GTIA
|
|
|
|
and #$0e ; mask out irrelevant bits
|
|
|
|
bne @ntsc
|
|
|
|
; PAL system
|
|
|
|
lda #50
|
|
|
|
sta timer_freq
|
|
|
|
lda #20
|
|
|
|
sta timer_freq_reciproc
|
|
|
|
bne @system_set
|
|
|
|
@ntsc:
|
|
|
|
; NTSC system
|
|
|
|
lda #60
|
|
|
|
sta timer_freq
|
|
|
|
lda #17
|
|
|
|
sta timer_freq_reciproc
|
|
|
|
@system_set:
|
2017-09-25 15:06:43 +00:00
|
|
|
lda #0 ; initialize time variables
|
2015-08-28 13:19:52 +00:00
|
|
|
sta current_time_value
|
|
|
|
sta current_time_value+1
|
|
|
|
sta current_seconds
|
|
|
|
sta current_jiffies
|
2017-09-25 15:06:43 +00:00
|
|
|
ldax VVBLKI ; IMMEDIATE VERTICAL BLANK NMI VECTOR
|
|
|
|
stax vbichain ; save old immediate vector
|
|
|
|
ldy #<timer_vbl_handler
|
|
|
|
ldx #>timer_vbl_handler
|
|
|
|
lda #6 ; STAGE 1 VBI
|
|
|
|
jsr SETVBV ; vector to set VBLANK parameters
|
|
|
|
@done:
|
2015-08-28 13:19:52 +00:00
|
|
|
rts
|
|
|
|
|
|
|
|
timer_exit:
|
|
|
|
lda vbichain+1
|
|
|
|
beq @handler_not_installed
|
|
|
|
ldy vbichain
|
|
|
|
ldx vbichain+1
|
|
|
|
lda #6 ; STAGE 1 VBI
|
2015-09-09 12:04:07 +00:00
|
|
|
jsr SETVBV ; vector to set VBLANK parameters
|
2017-09-12 12:14:14 +00:00
|
|
|
lda #0
|
|
|
|
sta vbichain
|
|
|
|
sta vbichain+1
|
2015-08-28 13:19:52 +00:00
|
|
|
@handler_not_installed:
|
|
|
|
rts
|
|
|
|
|
|
|
|
; read the current timer value
|
|
|
|
; inputs: none
|
|
|
|
; outputs: AX = current timer value (roughly equal to number of milliseconds since the last call to 'timer_init')
|
|
|
|
timer_read:
|
|
|
|
ldax current_time_value
|
|
|
|
rts
|
|
|
|
|
2017-09-24 00:26:40 +00:00
|
|
|
; tick over the current timer value - should be called 50 or 60 times per second, depending on the TV system
|
2015-08-28 13:19:52 +00:00
|
|
|
; inputs: none
|
|
|
|
; outputs: none (all registers preserved, but carry flag can be modified)
|
|
|
|
timer_vbl_handler:
|
|
|
|
pha
|
2017-09-12 12:14:14 +00:00
|
|
|
lda timer_freq_reciproc ; ms per tick
|
2015-08-28 13:19:52 +00:00
|
|
|
clc
|
|
|
|
adc current_time_value
|
|
|
|
sta current_time_value
|
|
|
|
bcc :+
|
|
|
|
inc current_time_value+1
|
|
|
|
: inc current_jiffies
|
|
|
|
lda current_jiffies
|
2017-09-12 12:14:14 +00:00
|
|
|
cmp timer_freq ; full second?
|
2017-09-24 00:26:40 +00:00
|
|
|
bne @done ; no, we're done
|
|
|
|
lda #0 ; yes, increment "seconds" counter
|
2015-08-28 13:19:52 +00:00
|
|
|
sta current_jiffies
|
2017-09-24 00:26:40 +00:00
|
|
|
sed
|
|
|
|
clc
|
|
|
|
lda #$01
|
|
|
|
adc current_seconds
|
|
|
|
cld
|
|
|
|
cmp #$60
|
2015-08-28 13:19:52 +00:00
|
|
|
bne :+
|
|
|
|
lda #$00
|
|
|
|
: sta current_seconds
|
|
|
|
@done:
|
|
|
|
pla
|
2015-09-09 12:04:07 +00:00
|
|
|
jmp SYSVBV ; vector to process immediate VBLANK
|
2015-08-28 13:19:52 +00:00
|
|
|
|
|
|
|
timer_seconds:
|
|
|
|
lda current_seconds
|
|
|
|
rts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;-- LICENSE FOR atrtimer.s --
|
|
|
|
; The contents of this file are subject to the Mozilla Public License
|
|
|
|
; Version 1.1 (the "License"); you may not use this file except in
|
|
|
|
; compliance with the License. You may obtain a copy of the License at
|
|
|
|
; http://www.mozilla.org/MPL/
|
|
|
|
;
|
|
|
|
; Software distributed under the License is distributed on an "AS IS"
|
|
|
|
; basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
|
|
|
; License for the specific language governing rights and limitations
|
|
|
|
; under the License.
|
|
|
|
;
|
|
|
|
; The Original Code is ip65.
|
|
|
|
;
|
|
|
|
; The Initial Developer of the Original Code is Jonno Downes,
|
|
|
|
; jonno@jamtronix.com.
|
|
|
|
; Portions created by the Initial Developer are Copyright (C) 2009
|
|
|
|
; Jonno Downes. All Rights Reserved.
|
|
|
|
; -- LICENSE END --
|