We want to add the capability to not only get the time but also set the time, but there's no "setter" for the "getter" time().
The first ones that come into mind are gettimeofday() and settimeofday(). However, they take a struct timezone argument that doesn't make sense - even the man pages says "The use of the timezone structure is obsolete; the tz argument should normally be specified as NULL." And POSIX says "Applications should use the clock_gettime() function instead of the obsolescent gettimeofday() function."
The ...timeofday() functions work with microseconds while the clock_...time() functions work with nanoseconds. Given that we expect our targets to support only 1/10 of seconds the microseconds look preferable at first sight. However, already microseconds require the cc65 data type 'long' so it's not such a relevant difference to nanoseconds. Additionally clock_getres() seems useful.
In order to avoid code duplication clock_gettime() takes over the role of the actual time getter from _systime(). So time() now calls clock_gettime() instead of _systime().
For some reason beyond my understanding _systime() was mentioned in time.h. _systime() worked exactly like e.g. _sysremove() and those _sys...() functions are all considered internal. The only reason I could see would be a performance gain of bypassing the time() wrapper. However, all known _systime() implementations internally called mktime(). And mktime() is implemented in C using an iterative algorithm so I really can't see what would be left to gain here. From that perspective I decided to just remove _systime().
.endstruct
+;------------------------------------------------------------------------------
+; Struct timespec - must match the struct defined in time.h
+
+.struct timespec
+ tv_sec .dword
+ tv_nsec .dword
+.endstruct
+
+
;------------------------------------------------------------------------------
; Exported functions
-.global __systime
+.global _clock_getres
+.global _clock_gettime
+.global _clock_settime
.global _mktime
<tag/Description/The function returns the time since the 1970-01-01 00:00:00
measured in seconds. If the pointer <tt/t/ is not <tt/NULL/, the function
result will also be stored there. If no time is available, <tt/(time_t)-1/ is
-returned and <tt/errno/ is set to <tt/ENOSYS/.
+returned and an error code is stored in <tt/errno/.
<tag/Notes/<itemize>
<item>The function is only available as fastcall function, so it may
only be used in presence of a prototype.
typedef unsigned long time_t;
typedef unsigned long clock_t;
+typedef unsigned char clockid_t;
/* Structure for broken down time */
-struct tm {
+struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_isdst;
};
+/* Structure for seconds and nanoseconds */
+struct timespec {
+ time_t tv_sec;
+ long tv_nsec;
+};
+
/* Timezone representation, default is UTC */
extern struct _timezone {
char daylight; /* True if daylight savings time active */
# define CLK_TCK _clk_tck()
# define CLOCKS_PER_SEC _clk_tck()
#endif
+#define CLOCK_REALTIME 0
-time_t _systime (void);
-/* Similar to time(), but:
-** - Is not ISO C
-** - Does not take the additional pointer
-** - Does not set errno when returning -1
-*/
-
/* ISO C function prototypes */
char* __fastcall__ asctime (const struct tm* timep);
clock_t clock (void);
+/* POSIX function prototypes */
+int __fastcall__ clock_getres (clockid_t clock_id, struct timespec *res);
+int __fastcall__ clock_gettime (clockid_t clock_id, struct timespec *tp);
+int __fastcall__ clock_settime (clockid_t clock_id, const struct timespec *tp);
+
+
+
/* End of time.h */
#endif
--- /dev/null
+;
+; Oliver Schmidt, 14.08.2018
+;
+; int clock_gettime (clockid_t clk_id, struct timespec *tp);
+;
+
+ .import pushax, steaxspidx, incsp1, incsp3, return0
+
+ .include "time.inc"
+ .include "zeropage.inc"
+ .include "errno.inc"
+ .include "mli.inc"
+
+_clock_gettime:
+ jsr pushax
+
+ ; Clear tv_nsec (+ tv_sec)
+ sta ptr1
+ stx ptr1+1
+ lda #$00
+ ldy #.sizeof(timespec)-1
+: sta (ptr1),y
+ dey
+ bpl :-
+
+ ; Update date + time
+ lda #GET_TIME_CALL
+ ldx #GET_TIME_COUNT
+ jsr callmli
+ bcs oserr
+
+ ; Get date
+ lda DATELO+1
+ lsr
+ php ; Save month msb
+ cmp #70 ; Year < 70?
+ bcs :+ ; No, leave alone
+ adc #100 ; Move 19xx to 20xx
+: sta TM + tm::tm_year
+ lda DATELO
+ tax ; Save day
+ plp ; Restore month msb
+ ror
+ lsr
+ lsr
+ lsr
+ lsr
+ beq erange ; [1..12] allows for validity check
+ tay
+ dey ; Move [1..12] to [0..11]
+ sty TM + tm::tm_mon
+ txa ; Restore day
+ and #%00011111
+ sta TM + tm::tm_mday
+
+ ; Get time
+ lda TIMELO+1
+ sta TM + tm::tm_hour
+ lda TIMELO
+ sta TM + tm::tm_min
+
+ ; Make time_t
+ lda #<TM
+ ldx #>TM
+ jsr _mktime
+
+ ; Store tv_sec
+ ldy #timespec::tv_sec
+ jsr steaxspidx
+
+ ; Return success
+ jsr incsp1
+ jmp return0
+
+ ; Load errno code
+erange: lda #ERANGE
+
+ ; Cleanup stack
+ jsr incsp3 ; Preserves A
+
+ ; Set __errno
+ jmp __directerrno
+
+ ; Cleanup stack
+oserr: jsr incsp3 ; Preserves A
+
+ ; Set __oserror
+ jmp __mappederrno
+
+ .bss
+
+TM: .tag tm
+++ /dev/null
-;
-; Oliver Schmidt, 22.08.2006
-;
-; time_t _systime (void);
-; /* Similar to time(), but:
-; ** - Is not ISO C
-; ** - Does not take the additional pointer
-; ** - Does not set errno when returning -1
-; */
-;
-
- .include "time.inc"
- .include "zeropage.inc"
- .include "mli.inc"
-
-__systime:
- ; Update time
- lda #GET_TIME_CALL
- ldx #GET_TIME_COUNT
- jsr callmli
- bcs err
-
- lda DATELO+1
- lsr
- php ; Save month msb
- cmp #70 ; Year < 70?
- bcs :+ ; No, leave alone
- adc #100 ; Move 19xx to 20xx
-: sta TM + tm::tm_year
- lda DATELO
- tax ; Save day
- plp ; Restore month msb
- ror
- lsr
- lsr
- lsr
- lsr
- beq err ; [1..12] allows for validity check
- tay
- dey ; Move [1..12] to [0..11]
- sty TM + tm::tm_mon
- txa ; Restore day
- and #%00011111
- sta TM + tm::tm_mday
-
- lda TIMELO+1
- sta TM + tm::tm_hour
- lda TIMELO
- sta TM + tm::tm_min
-
- lda #<TM
- ldx #>TM
- jmp _mktime
-
-err: lda #$FF
- tax
- sta sreg
- sta sreg+1
- rts ; Return -1
-
- .bss
-
-TM: .tag tm
+++ /dev/null
-;
-; Ullrich von Bassewitz, 12.11.2002
-;
-; time_t _systime (void);
-; /* Similar to time(), but:
-; ** - Is not ISO C
-; ** - Does not take the additional pointer
-; ** - Does not set errno when returning -1
-; */
-;
-
- .export __systime
-
- .importzp sreg
-
-.code
-
-.proc __systime
-
- lda #$FF
- tax
- sta sreg
- sta sreg+1
- rts ; Return -1
-
-.endproc
-
-
+++ /dev/null
-;
-; Ullrich von Bassewitz, 12.11.2002
-;
-; time_t _systime (void);
-; /* Similar to time(), but:
-; ** - Is not ISO C
-; ** - Does not take the additional pointer
-; ** - Does not set errno when returning -1
-; */
-;
-
- .export __systime
-
- .importzp sreg
-
-.code
-
-.proc __systime
-
- lda #$FF
- tax
- sta sreg
- sta sreg+1
- rts ; Return -1
-
-.endproc
-
-
--- /dev/null
+;
+; Stefan Haubenthal, 27.7.2009
+; Oliver Schmidt, 14.8.2018
+;
+; int clock_gettime (clockid_t clk_id, struct timespec *tp);
+;
+
+ .include "time.inc"
+ .include "c128.inc"
+ .include "get_tv.inc"
+
+ .constructor inittime
+ .importzp sreg, tmp1, tmp2
+ .import pushax, pusheax, tosmul0ax, steaxspidx, incsp1, return0
+ .import _get_tv
+
+
+;----------------------------------------------------------------------------
+.code
+
+.proc _clock_gettime
+
+ jsr pushax
+ jsr pushax
+
+ lda CIA1_TODHR
+ bpl AM
+ and #%01111111
+ sed
+ clc
+ adc #$12
+ cld
+AM: jsr BCD2dec
+ sta TM + tm::tm_hour
+ lda CIA1_TODMIN
+ jsr BCD2dec
+ sta TM + tm::tm_min
+ lda CIA1_TODSEC
+ jsr BCD2dec
+ sta TM + tm::tm_sec
+ lda #<TM
+ ldx #>TM
+ jsr _mktime
+
+ ldy #timespec::tv_sec
+ jsr steaxspidx ; Pops address pushed by 2. pushax
+
+ lda #<(100 * 1000 * 1000 / $10000)
+ ldx #>(100 * 1000 * 1000 / $10000)
+ sta sreg
+ stx sreg+1
+ lda #<(100 * 1000 * 1000)
+ ldx #>(100 * 1000 * 1000)
+ jsr pusheax
+ lda CIA1_TOD10
+ ldx #>$0000
+ jsr tosmul0ax
+
+ ldy #timespec::tv_nsec
+ jsr steaxspidx ; Pops address pushed by 1. pushax
+
+ jsr incsp1
+ jmp return0
+
+.endproc
+
+;----------------------------------------------------------------------------
+; dec = (((BCD>>4)*10) + (BCD&0xf))
+
+.proc BCD2dec
+
+ tax
+ and #%00001111
+ sta tmp1
+ txa
+ and #%11110000 ; *16
+ lsr ; *8
+ sta tmp2
+ lsr
+ lsr ; *2
+ adc tmp2 ; = *10
+ adc tmp1
+ rts
+
+.endproc
+
+;----------------------------------------------------------------------------
+; Constructor that writes to the 1/10 sec register of the TOD to kick it
+; into action. If this is not done, the clock hangs. We will read the register
+; and write it again, ignoring a possible change in between.
+.segment "ONCE"
+
+.proc inittime
+
+ lda CIA1_TOD10
+ sta CIA1_TOD10
+ jsr _get_tv
+ cmp #TV::PAL
+ bne @60Hz
+ lda CIA1_CRA
+ ora #$80
+ sta CIA1_CRA
+@60Hz: rts
+
+.endproc
+
+;----------------------------------------------------------------------------
+; TM struct with date set to 1970-01-01
+.data
+
+TM: .word 0 ; tm_sec
+ .word 0 ; tm_min
+ .word 0 ; tm_hour
+ .word 1 ; tm_mday
+ .word 0 ; tm_mon
+ .word 70 ; tm_year
+ .word 0 ; tm_wday
+ .word 0 ; tm_yday
+ .word 0 ; tm_isdst
+++ /dev/null
-;
-; Stefan Haubenthal, 27.7.2009
-;
-; time_t _systime (void);
-; /* Similar to time(), but:
-; ** - Is not ISO C
-; ** - Does not take the additional pointer
-; ** - Does not set errno when returning -1
-; */
-;
-
- .include "time.inc"
- .include "c128.inc"
- .include "get_tv.inc"
-
- .constructor initsystime
- .importzp tmp1, tmp2
- .import _get_tv
-
-
-;----------------------------------------------------------------------------
-.code
-
-.proc __systime
-
- lda CIA1_TODHR
- bpl AM
- and #%01111111
- sed
- clc
- adc #$12
- cld
-AM: jsr BCD2dec
- sta TM + tm::tm_hour
- lda CIA1_TODMIN
- jsr BCD2dec
- sta TM + tm::tm_min
- lda CIA1_TODSEC
- jsr BCD2dec
- sta TM + tm::tm_sec
- lda CIA1_TOD10 ; Dummy read to unfreeze
- lda #<TM
- ldx #>TM
- jmp _mktime
-
-; dec = (((BCD>>4)*10) + (BCD&0xf))
-BCD2dec:tax
- and #%00001111
- sta tmp1
- txa
- and #%11110000 ; *16
- lsr ; *8
- sta tmp2
- lsr
- lsr ; *2
- adc tmp2 ; = *10
- adc tmp1
- rts
-
-.endproc
-
-;----------------------------------------------------------------------------
-; Constructor that writes to the 1/10 sec register of the TOD to kick it
-; into action. If this is not done, the clock hangs. We will read the register
-; and write it again, ignoring a possible change in between.
-.segment "ONCE"
-
-.proc initsystime
-
- lda CIA1_TOD10
- sta CIA1_TOD10
- jsr _get_tv
- cmp #TV::PAL
- bne @60Hz
- lda CIA1_CRA
- ora #$80
- sta CIA1_CRA
-@60Hz: rts
-
-.endproc
-
-;----------------------------------------------------------------------------
-; TM struct with date set to 1970-01-01
-.data
-
-TM: .word 0 ; tm_sec
- .word 0 ; tm_min
- .word 0 ; tm_hour
- .word 1 ; tm_mday
- .word 0 ; tm_mon
- .word 70 ; tm_year
- .word 0 ; tm_wday
- .word 0 ; tm_yday
- .word 0 ; tm_isdst
+++ /dev/null
-;
-; Ullrich von Bassewitz, 12.11.2002
-;
-; time_t _systime (void);
-; /* Similar to time(), but:
-; ** - Is not ISO C
-; ** - Does not take the additional pointer
-; ** - Does not set errno when returning -1
-; */
-;
-
- .export __systime
-
- .importzp sreg
-
-.code
-
-.proc __systime
-
- lda #$FF
- tax
- sta sreg
- sta sreg+1
- rts ; Return -1
-
-.endproc
-
-
--- /dev/null
+;
+; Stefan Haubenthal, 27.7.2009
+; Oliver Schmidt, 14.8.2018
+;
+; int clock_gettime (clockid_t clk_id, struct timespec *tp);
+;
+
+ .include "time.inc"
+ .include "c64.inc"
+ .include "get_tv.inc"
+
+ .constructor inittime
+ .importzp sreg, tmp1, tmp2
+ .import pushax, pusheax, tosmul0ax, steaxspidx, incsp1, return0
+ .import _get_tv, _get_ostype
+
+
+;----------------------------------------------------------------------------
+.code
+
+.proc _clock_gettime
+
+ jsr pushax
+ jsr pushax
+
+ lda CIA1_TODHR
+ bpl AM
+ and #%01111111
+ sed
+ clc
+ adc #$12
+ cld
+AM: jsr BCD2dec
+ sta TM + tm::tm_hour
+ lda CIA1_TODMIN
+ jsr BCD2dec
+ sta TM + tm::tm_min
+ lda CIA1_TODSEC
+ jsr BCD2dec
+ sta TM + tm::tm_sec
+ lda #<TM
+ ldx #>TM
+ jsr _mktime
+
+ ldy #timespec::tv_sec
+ jsr steaxspidx ; Pops address pushed by 2. pushax
+
+ lda #<(100 * 1000 * 1000 / $10000)
+ ldx #>(100 * 1000 * 1000 / $10000)
+ sta sreg
+ stx sreg+1
+ lda #<(100 * 1000 * 1000)
+ ldx #>(100 * 1000 * 1000)
+ jsr pusheax
+ lda CIA1_TOD10
+ ldx #>$0000
+ jsr tosmul0ax
+
+ ldy #timespec::tv_nsec
+ jsr steaxspidx ; Pops address pushed by 1. pushax
+
+ jsr incsp1
+ jmp return0
+
+.endproc
+
+;----------------------------------------------------------------------------
+; dec = (((BCD>>4)*10) + (BCD&0xf))
+
+.proc BCD2dec
+
+ tax
+ and #%00001111
+ sta tmp1
+ txa
+ and #%11110000 ; *16
+ lsr ; *8
+ sta tmp2
+ lsr
+ lsr ; *2
+ adc tmp2 ; = *10
+ adc tmp1
+ rts
+
+.endproc
+
+;----------------------------------------------------------------------------
+; Constructor that writes to the 1/10 sec register of the TOD to kick it
+; into action. If this is not done, the clock hangs. We will read the register
+; and write it again, ignoring a possible change in between.
+.segment "ONCE"
+
+.proc inittime
+
+ lda CIA1_TOD10
+ sta CIA1_TOD10
+ jsr _get_tv
+ cmp #TV::PAL
+ bne @60Hz
+ jsr _get_ostype
+ cmp #$43
+ beq @60Hz
+ lda CIA1_CRA
+ ora #$80
+ sta CIA1_CRA
+@60Hz: rts
+
+.endproc
+
+;----------------------------------------------------------------------------
+; TM struct with date set to 1970-01-01
+.data
+
+TM: .word 0 ; tm_sec
+ .word 0 ; tm_min
+ .word 0 ; tm_hour
+ .word 1 ; tm_mday
+ .word 0 ; tm_mon
+ .word 70 ; tm_year
+ .word 0 ; tm_wday
+ .word 0 ; tm_yday
+ .word 0 ; tm_isdst
+++ /dev/null
-;
-; Stefan Haubenthal, 27.7.2009
-;
-; time_t _systime (void);
-; /* Similar to time(), but:
-; ** - Is not ISO C
-; ** - Does not take the additional pointer
-; ** - Does not set errno when returning -1
-; */
-;
-
- .include "time.inc"
- .include "c64.inc"
- .include "get_tv.inc"
-
- .constructor initsystime
- .importzp tmp1, tmp2
- .import _get_tv, _get_ostype
-
-
-;----------------------------------------------------------------------------
-.code
-
-.proc __systime
-
- lda CIA1_TODHR
- bpl AM
- and #%01111111
- sed
- clc
- adc #$12
- cld
-AM: jsr BCD2dec
- sta TM + tm::tm_hour
- lda CIA1_TODMIN
- jsr BCD2dec
- sta TM + tm::tm_min
- lda CIA1_TODSEC
- jsr BCD2dec
- sta TM + tm::tm_sec
- lda CIA1_TOD10 ; Dummy read to unfreeze
- lda #<TM
- ldx #>TM
- jmp _mktime
-
-; dec = (((BCD>>4)*10) + (BCD&0xf))
-BCD2dec:tax
- and #%00001111
- sta tmp1
- txa
- and #%11110000 ; *16
- lsr ; *8
- sta tmp2
- lsr
- lsr ; *2
- adc tmp2 ; = *10
- adc tmp1
- rts
-
-.endproc
-
-;----------------------------------------------------------------------------
-; Constructor that writes to the 1/10 sec register of the TOD to kick it
-; into action. If this is not done, the clock hangs. We will read the register
-; and write it again, ignoring a possible change in between.
-.segment "ONCE"
-
-.proc initsystime
-
- lda CIA1_TOD10
- sta CIA1_TOD10
- jsr _get_tv
- cmp #TV::PAL
- bne @60Hz
- jsr _get_ostype
- cmp #$43
- beq @60Hz
- lda CIA1_CRA
- ora #$80
- sta CIA1_CRA
-@60Hz: rts
-
-.endproc
-
-;----------------------------------------------------------------------------
-; TM struct with date set to 1970-01-01
-.data
-
-TM: .word 0 ; tm_sec
- .word 0 ; tm_min
- .word 0 ; tm_hour
- .word 1 ; tm_mday
- .word 0 ; tm_mon
- .word 70 ; tm_year
- .word 0 ; tm_wday
- .word 0 ; tm_yday
- .word 0 ; tm_isdst
--- /dev/null
+;
+; Stefan Haubenthal, 2009-07-27
+; Ullrich von Bassewitz, 2009-09-24
+; Oliver Schmidt, 2018-08-14
+;
+; int clock_gettime (clockid_t clk_id, struct timespec *tp);
+;
+
+ .include "time.inc"
+ .include "cbm510.inc"
+ .include "extzp.inc"
+
+ .import pushax, pusheax, tosmul0ax, steaxspidx, incsp1
+ .import sys_bank, restore_bank
+ .importzp sreg, tmp1, tmp2
+
+
+;----------------------------------------------------------------------------
+.code
+
+.proc _clock_gettime
+
+ jsr sys_bank
+ jsr pushax
+ jsr pushax
+
+ ldy #CIA::TODHR
+ lda (cia2),y
+ bpl AM
+ and #%01111111
+ sed
+ clc
+ adc #$12
+ cld
+AM: jsr BCD2dec
+ sta TM + tm::tm_hour
+ ldy #CIA::TODMIN
+ lda (cia2),y
+ jsr BCD2dec
+ sta TM + tm::tm_min
+ ldy #CIA::TODSEC
+ lda (cia2),y
+ jsr BCD2dec
+ sta TM + tm::tm_sec
+ lda #<TM
+ ldx #>TM
+ jsr _mktime
+
+ ldy #timespec::tv_sec
+ jsr steaxspidx ; Pops address pushed by 2. pushax
+
+ lda #<(100 * 1000 * 1000 / $10000)
+ ldx #>(100 * 1000 * 1000 / $10000)
+ sta sreg
+ stx sreg+1
+ lda #<(100 * 1000 * 1000)
+ ldx #>(100 * 1000 * 1000)
+ jsr pusheax
+ ldy #CIA::TOD10
+ lda (cia2),y
+ ldx #>$0000
+ jsr tosmul0ax
+
+ ldy #timespec::tv_nsec
+ jsr steaxspidx ; Pops address pushed by 1. pushax
+
+ jsr incsp1
+
+ lda #0
+ tax
+ jmp restore_bank
+
+.endproc
+
+;----------------------------------------------------------------------------
+; dec = (((BCD>>4)*10) + (BCD&0xf))
+
+.proc BCD2dec
+
+ tax
+ and #%00001111
+ sta tmp1
+ txa
+ and #%11110000 ; *16
+ lsr ; *8
+ sta tmp2
+ lsr
+ lsr ; *2
+ adc tmp2 ; = *10
+ adc tmp1
+ rts
+
+.endproc
+
+;----------------------------------------------------------------------------
+; TM struct with date set to 1970-01-01
+.data
+
+TM: .word 0 ; tm_sec
+ .word 0 ; tm_min
+ .word 0 ; tm_hour
+ .word 1 ; tm_mday
+ .word 0 ; tm_mon
+ .word 70 ; tm_year
+ .word 0 ; tm_wday
+ .word 0 ; tm_yday
+ .word 0 ; tm_isdst
+++ /dev/null
-;
-; Stefan Haubenthal, 2009-07-27
-; Ullrich von Bassewitz, 2009-09-24
-;
-; time_t _systime (void);
-; /* Similar to time(), but:
-; ** - Is not ISO C
-; ** - Does not take the additional pointer
-; ** - Does not set errno when returning -1
-; */
-;
-
- .include "time.inc"
- .include "cbm510.inc"
- .include "extzp.inc"
-
- .import sys_bank, restore_bank
- .importzp tmp1, tmp2
-
-
-;----------------------------------------------------------------------------
-.code
-
-.proc __systime
-
-; Switch to the system bank
-
- jsr sys_bank
-
-; Read the clock
-
- ldy #CIA::TODHR
- lda (cia2),y
- bpl AM
- and #%01111111
- sed
- clc
- adc #$12
- cld
-AM: jsr BCD2dec
- sta TM + tm::tm_hour
- ldy #CIA::TODMIN
- lda (cia2),y
- jsr BCD2dec
- sta TM + tm::tm_min
- ldy #CIA::TODSEC
- lda (cia2),y
- jsr BCD2dec
- sta TM + tm::tm_sec
- ldy #CIA::TOD10
- lda (cia2),y ; Dummy read to unfreeze
-
-; Restore the bank
-
- jsr restore_bank
-
-; Convert to a time
-
- lda #<TM
- ldx #>TM
- jmp _mktime
-
-.endproc
-
-;----------------------------------------------------------------------------
-; dec = (((BCD>>4)*10) + (BCD&0xf))
-
-.proc BCD2dec
-
- tax
- and #%00001111
- sta tmp1
- txa
- and #%11110000 ; *16
- lsr ; *8
- sta tmp2
- lsr
- lsr ; *2
- adc tmp2 ; = *10
- adc tmp1
- rts
-
-.endproc
-
-;----------------------------------------------------------------------------
-; TM struct with date set to 1970-01-01
-.data
-
-TM: .word 0 ; tm_sec
- .word 0 ; tm_min
- .word 0 ; tm_hour
- .word 1 ; tm_mday
- .word 0 ; tm_mon
- .word 70 ; tm_year
- .word 0 ; tm_wday
- .word 0 ; tm_yday
- .word 0 ; tm_isdst
-
--- /dev/null
+;
+; Stefan Haubenthal, 2009-07-27
+; Ullrich von Bassewitz, 2009-09-24
+; Oliver Schmidt, 2018-08-14
+;
+; int clock_gettime (clockid_t clk_id, struct timespec *tp);
+;
+
+ .include "time.inc"
+ .include "cbm610.inc"
+ .include "extzp.inc"
+
+ .import pushax, pusheax, tosmul0ax, steaxspidx, incsp1
+ .import sys_bank, restore_bank
+ .importzp sreg, tmp1, tmp2
+
+
+;----------------------------------------------------------------------------
+.code
+
+.proc _clock_gettime
+
+ jsr sys_bank
+ jsr pushax
+ jsr pushax
+
+ ldy #CIA::TODHR
+ lda (cia),y
+ bpl AM
+ and #%01111111
+ sed
+ clc
+ adc #$12
+ cld
+AM: jsr BCD2dec
+ sta TM + tm::tm_hour
+ ldy #CIA::TODMIN
+ lda (cia),y
+ jsr BCD2dec
+ sta TM + tm::tm_min
+ ldy #CIA::TODSEC
+ lda (cia),y
+ jsr BCD2dec
+ sta TM + tm::tm_sec
+ lda #<TM
+ ldx #>TM
+ jsr _mktime
+
+ ldy #timespec::tv_sec
+ jsr steaxspidx ; Pops address pushed by 2. pushax
+
+ lda #<(100 * 1000 * 1000 / $10000)
+ ldx #>(100 * 1000 * 1000 / $10000)
+ sta sreg
+ stx sreg+1
+ lda #<(100 * 1000 * 1000)
+ ldx #>(100 * 1000 * 1000)
+ jsr pusheax
+ ldy #CIA::TOD10
+ lda (cia),y
+ ldx #>$0000
+ jsr tosmul0ax
+
+ ldy #timespec::tv_nsec
+ jsr steaxspidx ; Pops address pushed by 1. pushax
+
+ jsr incsp1
+
+ lda #0
+ tax
+ jmp restore_bank
+
+.endproc
+
+;----------------------------------------------------------------------------
+; dec = (((BCD>>4)*10) + (BCD&0xf))
+
+.proc BCD2dec
+
+ tax
+ and #%00001111
+ sta tmp1
+ txa
+ and #%11110000 ; *16
+ lsr ; *8
+ sta tmp2
+ lsr
+ lsr ; *2
+ adc tmp2 ; = *10
+ adc tmp1
+ rts
+
+.endproc
+
+;----------------------------------------------------------------------------
+; TM struct with date set to 1970-01-01
+.data
+
+TM: .word 0 ; tm_sec
+ .word 0 ; tm_min
+ .word 0 ; tm_hour
+ .word 1 ; tm_mday
+ .word 0 ; tm_mon
+ .word 70 ; tm_year
+ .word 0 ; tm_wday
+ .word 0 ; tm_yday
+ .word 0 ; tm_isdst
+
+++ /dev/null
-;
-; Stefan Haubenthal, 2009-07-27
-; Ullrich von Bassewitz, 2009-09-24
-;
-; time_t _systime (void);
-; /* Similar to time(), but:
-; ** - Is not ISO C
-; ** - Does not take the additional pointer
-; ** - Does not set errno when returning -1
-; */
-;
-
- .include "time.inc"
- .include "cbm610.inc"
- .include "extzp.inc"
-
- .import sys_bank, restore_bank
- .importzp tmp1, tmp2
-
-
-;----------------------------------------------------------------------------
-.code
-
-.proc __systime
-
-; Switch to the system bank
-
- jsr sys_bank
-
-; Read the clock
-
- ldy #CIA::TODHR
- lda (cia),y
- bpl AM
- and #%01111111
- sed
- clc
- adc #$12
- cld
-AM: jsr BCD2dec
- sta TM + tm::tm_hour
- ldy #CIA::TODMIN
- lda (cia),y
- jsr BCD2dec
- sta TM + tm::tm_min
- ldy #CIA::TODSEC
- lda (cia),y
- jsr BCD2dec
- sta TM + tm::tm_sec
- ldy #CIA::TOD10
- lda (cia),y ; Dummy read to unfreeze
-
-; Restore the bank
-
- jsr restore_bank
-
-; Convert to a time
-
- lda #<TM
- ldx #>TM
- jmp _mktime
-
-.endproc
-
-;----------------------------------------------------------------------------
-; dec = (((BCD>>4)*10) + (BCD&0xf))
-
-.proc BCD2dec
-
- tax
- and #%00001111
- sta tmp1
- txa
- and #%11110000 ; *16
- lsr ; *8
- sta tmp2
- lsr
- lsr ; *2
- adc tmp2 ; = *10
- adc tmp1
- rts
-
-.endproc
-
-;----------------------------------------------------------------------------
-; TM struct with date set to 1970-01-01
-.data
-
-TM: .word 0 ; tm_sec
- .word 0 ; tm_min
- .word 0 ; tm_hour
- .word 1 ; tm_mday
- .word 0 ; tm_mon
- .word 70 ; tm_year
- .word 0 ; tm_wday
- .word 0 ; tm_yday
- .word 0 ; tm_isdst
-
.export _time
- .import __systime
- .importzp ptr1, sreg, tmp1
+ .import decsp1
+ .importzp ptr1, sreg, tmp1, tmp2
- .include "errno.inc"
+ .include "time.inc"
.code
txa
pha ; Save timep
- jsr __systime ; Get the time (machine dependent)
-
+; Get the time (machine dependent)
+
+ jsr decsp1
+ lda #<time
+ ldx #>time
+ jsr _clock_gettime
+ sta tmp2
+ lda time + timespec::tv_sec+2
+ ldx time + timespec::tv_sec+3
+ sta sreg
+ stx sreg+1
+ lda time + timespec::tv_sec
+ ldx time + timespec::tv_sec+1
sta tmp1 ; Save low byte of result
; Restore timep and check if it is NULL
lda tmp1
sta (ptr1),y
-; If the result is less than zero, set ERRNO
+; If the result is != 0, return -1
+
+@L1: lda tmp2
+ beq @L2
-@L1: ldy sreg+1
- bpl @L2
-
- lda #ENOSYS ; Function not implemented
- jsr __seterrno ; Set __errno
+ tax
+ sta sreg
+ sta sreg+1
+ rts
; Reload the low byte of the result and return
.endproc
+; ------------------------------------------------------------------------
+; Data
+.bss
+time: .tag timespec
--- /dev/null
+/*
+** gettime.c
+**
+** Maciej 'YTM/Elysium' Witkowiak, 22.11.2002
+*/
+
+#include <time.h>
+#include <geos.h>
+
+clock_t clock(void)
+{
+ struct tm currentTime;
+
+ currentTime.tm_sec = system_date.s_seconds;
+ currentTime.tm_min = system_date.s_minutes;
+ currentTime.tm_hour = system_date.s_hour;
+ currentTime.tm_mday = system_date.s_day;
+ currentTime.tm_mon = system_date.s_month;
+ currentTime.tm_year = system_date.s_year;
+ if (system_date.s_year < 87) {
+ currentTime.tm_year+=100;
+ }
+ currentTime.tm_isdst = -1;
+
+ return mktime(¤tTime);
+}
+
+int clock_gettime(clockid_t, struct timespec *tp)
+{
+ tp->tv_sec = clock();
+ tp->tv_nsec = 0;
+
+ return 0;
+}
+++ /dev/null
-/*
-** systime.c
-**
-** Maciej 'YTM/Elysium' Witkowiak, 22.11.2002
-*/
-
-#include <time.h>
-#include <geos.h>
-
-time_t _systime(void)
-{
- struct tm currentTime;
-
- currentTime.tm_sec = system_date.s_seconds;
- currentTime.tm_min = system_date.s_minutes;
- currentTime.tm_hour = system_date.s_hour;
- currentTime.tm_mday = system_date.s_day;
- currentTime.tm_mon = system_date.s_month;
- currentTime.tm_year = system_date.s_year;
- if (system_date.s_year < 87) {
- currentTime.tm_year+=100;
- }
- currentTime.tm_isdst = -1;
-
- return mktime(¤tTime);
-}
-
-clock_t clock(void)
-{
- return _systime();
-}
+++ /dev/null
-;
-; Ullrich von Bassewitz, 12.11.2002
-;
-; time_t _systime (void);
-; /* Similar to time(), but:
-; ** - Is not ISO C
-; ** - Does not take the additional pointer
-; ** - Does not set errno when returning -1
-; */
-;
-
- .export __systime
-
- .importzp sreg
-
-.code
-
-.proc __systime
-
- lda #$FF
- tax
- sta sreg
- sta sreg+1
- rts ; Return -1
-
-.endproc
-
-