]> git.sur5r.net Git - cc65/blob - libsrc/cbm510/gettime.s
Fixed 12 AM/PM handling.
[cc65] / libsrc / cbm510 / gettime.s
1 ;
2 ; Stefan Haubenthal, 2009-07-27
3 ; Ullrich von Bassewitz, 2009-09-24
4 ; Oliver Schmidt, 2018-08-14
5 ;
6 ; int clock_gettime (clockid_t clk_id, struct timespec *tp);
7 ;
8
9         .include        "time.inc"
10         .include        "cbm510.inc"
11         .include        "extzp.inc"
12
13         .import         pushax, pusheax, tosmul0ax, steaxspidx, incsp1
14         .import         sys_bank, restore_bank
15         .importzp       sreg, tmp1, tmp2
16
17
18 ;----------------------------------------------------------------------------
19 .code
20
21 .proc   _clock_gettime
22
23         jsr     sys_bank
24         jsr     pushax
25         jsr     pushax
26
27         ldy     #CIA::TODHR
28         lda     (cia2),y
29         sed
30         tax                     ; Save PM flag
31         and     #%01111111
32         cmp     #$12            ; 12 AM/PM
33         bcc     @L1
34         sbc     #$12
35 @L1:    inx                     ; Get PM flag
36         bpl     @L2
37         clc
38         adc     #$12
39 @L2:    cld
40         jsr     BCD2dec
41         sta     TM + tm::tm_hour
42         ldy     #CIA::TODMIN
43         lda     (cia2),y
44         jsr     BCD2dec
45         sta     TM + tm::tm_min
46         ldy     #CIA::TODSEC
47         lda     (cia2),y
48         jsr     BCD2dec
49         sta     TM + tm::tm_sec
50         lda     #<TM
51         ldx     #>TM
52         jsr     _mktime
53
54         ldy     #timespec::tv_sec
55         jsr     steaxspidx      ; Pops address pushed by 2. pushax
56
57         lda     #<(100 * 1000 * 1000 / $10000)
58         ldx     #>(100 * 1000 * 1000 / $10000)
59         sta     sreg
60         stx     sreg+1
61         lda     #<(100 * 1000 * 1000)
62         ldx     #>(100 * 1000 * 1000)
63         jsr     pusheax
64         ldy     #CIA::TOD10
65         lda     (cia2),y
66         ldx     #>$0000
67         jsr     tosmul0ax
68
69         ldy     #timespec::tv_nsec
70         jsr     steaxspidx      ; Pops address pushed by 1. pushax
71
72         jsr     incsp1
73
74         lda     #0
75         tax
76         jmp     restore_bank
77
78 .endproc
79
80 ;----------------------------------------------------------------------------
81 ; dec = (((BCD>>4)*10) + (BCD&0xf))
82
83 .proc   BCD2dec
84
85         tax
86         and     #%00001111
87         sta     tmp1
88         txa
89         and     #%11110000      ; *16
90         lsr                     ; *8
91         sta     tmp2
92         lsr
93         lsr                     ; *2
94         adc     tmp2            ; = *10
95         adc     tmp1
96         rts
97
98 .endproc
99
100 ;----------------------------------------------------------------------------
101 ; TM struct with date set to 1970-01-01
102 .data
103
104 TM:     .word           0       ; tm_sec
105         .word           0       ; tm_min
106         .word           0       ; tm_hour
107         .word           1       ; tm_mday
108         .word           0       ; tm_mon
109         .word           70      ; tm_year
110         .word           0       ; tm_wday
111         .word           0       ; tm_yday
112         .word           0       ; tm_isdst