]> git.sur5r.net Git - cc65/blob - libsrc/c64/gettime.s
Replaced _systime with clock_gettime.
[cc65] / libsrc / c64 / gettime.s
1 ;
2 ; Stefan Haubenthal, 27.7.2009
3 ; Oliver Schmidt, 14.8.2018
4 ;
5 ; int clock_gettime (clockid_t clk_id, struct timespec *tp);
6 ;
7
8         .include        "time.inc"
9         .include        "c64.inc"
10         .include        "get_tv.inc"
11
12         .constructor    inittime
13         .importzp       sreg, tmp1, tmp2
14         .import         pushax, pusheax, tosmul0ax, steaxspidx, incsp1, return0
15         .import         _get_tv, _get_ostype
16
17
18 ;----------------------------------------------------------------------------
19 .code
20
21 .proc   _clock_gettime
22
23         jsr     pushax
24         jsr     pushax
25
26         lda     CIA1_TODHR
27         bpl     AM
28         and     #%01111111
29         sed
30         clc
31         adc     #$12
32         cld
33 AM:     jsr     BCD2dec
34         sta     TM + tm::tm_hour
35         lda     CIA1_TODMIN
36         jsr     BCD2dec
37         sta     TM + tm::tm_min
38         lda     CIA1_TODSEC
39         jsr     BCD2dec
40         sta     TM + tm::tm_sec
41         lda     #<TM
42         ldx     #>TM
43         jsr     _mktime
44
45         ldy     #timespec::tv_sec
46         jsr     steaxspidx      ; Pops address pushed by 2. pushax
47
48         lda     #<(100 * 1000 * 1000 / $10000)
49         ldx     #>(100 * 1000 * 1000 / $10000)
50         sta     sreg
51         stx     sreg+1
52         lda     #<(100 * 1000 * 1000)
53         ldx     #>(100 * 1000 * 1000)
54         jsr     pusheax
55         lda     CIA1_TOD10
56         ldx     #>$0000
57         jsr     tosmul0ax
58
59         ldy     #timespec::tv_nsec
60         jsr     steaxspidx      ; Pops address pushed by 1. pushax
61
62         jsr     incsp1
63         jmp     return0
64
65 .endproc
66
67 ;----------------------------------------------------------------------------
68 ; dec = (((BCD>>4)*10) + (BCD&0xf))
69
70 .proc   BCD2dec
71
72         tax
73         and     #%00001111
74         sta     tmp1
75         txa
76         and     #%11110000      ; *16
77         lsr                     ; *8
78         sta     tmp2
79         lsr
80         lsr                     ; *2
81         adc     tmp2            ; = *10
82         adc     tmp1
83         rts
84
85 .endproc
86
87 ;----------------------------------------------------------------------------
88 ; Constructor that writes to the 1/10 sec register of the TOD to kick it
89 ; into action. If this is not done, the clock hangs. We will read the register
90 ; and write it again, ignoring a possible change in between.
91 .segment "ONCE"
92
93 .proc   inittime
94
95         lda     CIA1_TOD10
96         sta     CIA1_TOD10
97         jsr     _get_tv
98         cmp     #TV::PAL
99         bne     @60Hz
100         jsr     _get_ostype
101         cmp     #$43
102         beq     @60Hz
103         lda     CIA1_CRA
104         ora     #$80
105         sta     CIA1_CRA
106 @60Hz:  rts
107
108 .endproc
109
110 ;----------------------------------------------------------------------------
111 ; TM struct with date set to 1970-01-01
112 .data
113
114 TM:     .word           0       ; tm_sec
115         .word           0       ; tm_min
116         .word           0       ; tm_hour
117         .word           1       ; tm_mday
118         .word           0       ; tm_mon
119         .word           70      ; tm_year
120         .word           0       ; tm_wday
121         .word           0       ; tm_yday
122         .word           0       ; tm_isdst