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