]> git.sur5r.net Git - cc65/blob - libsrc/lynx/clock.s
lseek: Implement additional feedback from PR #723.
[cc65] / libsrc / lynx / clock.s
1 ;
2 ; 2003-04-13, Ullrich von Bassewitz
3 ; 2012-02-06, Greg King
4 ;
5 ; #include <time.h>
6 ;
7 ; typedef unsigned long int clock_t;
8 ; clock_t _clk_tck(void);
9 ; #define CLOCKS_PER_SEC _clk_tck()
10 ; clock_t clock(void);
11 ;
12 ; clk_tck()'s test-values are based on the numbers in "set_tv.s".
13 ; If you change the numbers there, then change them here, too.
14 ;
15
16         .export         _clock, __clk_tck, clock_count
17         .interruptor    update_clock, 2 ; (low priority)
18         .constructor    init_clock
19
20         .import         sreg: zp
21         .include        "lynx.inc"
22
23         .macpack        generic
24
25
26         .proc   _clock
27         php
28         sei                     ; Disable interrupts
29
30 ; Read the clock counter.
31
32         lda     clock_count
33         ldx     clock_count+1
34         ldy     clock_count+2
35
36         plp                     ; Re-enable interrupts
37         sty     sreg
38         stz     sreg+1          ; Promote 24 bits up to 32 bits
39         rts
40         .endproc
41
42 ;-----------------------------------------------------------------------------
43 ; Return the number of clock ticks in one second.
44 ;
45 __clk_tck:
46         ldx     #$00            ; >50, >60, >75
47         ldy     PBKUP
48         lda     #<75
49         cpy     #$20 + 1
50         blt     @ok
51         lda     #<60
52         cpy     #$29 + 1
53         blt     @ok
54         lda     #<50
55 @ok:    stz     sreg            ; return 32 bits
56         stz     sreg+1
57         rts
58
59 ;-----------------------------------------------------------------------------
60 ; This interrupt handler increments a 24-bit counter at every video
61 ; vertical-blanking time.
62 ;
63         .segment        "LOWCODE"
64 update_clock:
65         lda     INTSET
66         and     #%00000100
67         beq     @NotVBlank      ; Not vertical-blank interrupt
68
69         inc     clock_count
70         bne     @L1
71         inc     clock_count+1
72         bne     @L1
73         inc     clock_count+2
74 @L1:    ;clc                    ; General interrupt was not reset
75 @NotVBlank:
76         rts
77
78 ;-----------------------------------------------------------------------------
79 ; Enable the interrupt that update_clock needs.
80 ;
81         .segment        "ONCE"
82 init_clock:
83         lda     #%10000000
84         tsb     VTIMCTLA
85         rts
86
87 ;-----------------------------------------------------------------------------
88 ;
89         .bss
90 clock_count:
91         .res    3