]> git.sur5r.net Git - cc65/commitdiff
Replaced _systime with clock_gettime.
authorOliver Schmidt <ol.sc@web.de>
Wed, 15 Aug 2018 13:59:11 +0000 (15:59 +0200)
committerOliver Schmidt <ol.sc@web.de>
Wed, 15 Aug 2018 14:06:44 +0000 (16:06 +0200)
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().

20 files changed:
asminc/time.inc
doc/funcref.sgml
include/time.h
libsrc/apple2/gettime.s [new file with mode: 0644]
libsrc/apple2/systime.s [deleted file]
libsrc/atari/systime.s [deleted file]
libsrc/atmos/systime.s [deleted file]
libsrc/c128/gettime.s [new file with mode: 0644]
libsrc/c128/systime.s [deleted file]
libsrc/c16/systime.s [deleted file]
libsrc/c64/gettime.s [new file with mode: 0644]
libsrc/c64/systime.s [deleted file]
libsrc/cbm510/gettime.s [new file with mode: 0644]
libsrc/cbm510/systime.s [deleted file]
libsrc/cbm610/gettime.s [new file with mode: 0644]
libsrc/cbm610/systime.s [deleted file]
libsrc/common/time.s
libsrc/geos-common/system/gettime.c [new file with mode: 0644]
libsrc/geos-common/system/systime.c [deleted file]
libsrc/plus4/systime.s [deleted file]

index 16858471d10811c81b8deb9b9bf5ea43274208f6..5783232c7fd5b5abaf0786a489bbe2898c1beefb 100644 (file)
 .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
 
 
index edb510e374c77996790c9e525eb558edc64550aa..247689b8ad6035380b5a82eb00394c01768cd644 100644 (file)
@@ -7375,7 +7375,7 @@ be used in presence of a prototype.
 <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.
index 22e24a56e0ac8d5d44be23070c893d4da3387aeb..4ee658da994789c2354b6f5da526c2e57efd5cbb 100644 (file)
@@ -52,9 +52,10 @@ typedef unsigned size_t;
 
 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;
@@ -66,6 +67,12 @@ struct tm {
     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 */
@@ -116,16 +123,10 @@ extern clock_t _clk_tck (void);
 #  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);
@@ -138,6 +139,13 @@ time_t __fastcall__ time (time_t* t);
 
 
 
+/* 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
diff --git a/libsrc/apple2/gettime.s b/libsrc/apple2/gettime.s
new file mode 100644 (file)
index 0000000..6bb5415
--- /dev/null
@@ -0,0 +1,92 @@
+;
+; 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
diff --git a/libsrc/apple2/systime.s b/libsrc/apple2/systime.s
deleted file mode 100644 (file)
index 9bf4fe9..0000000
+++ /dev/null
@@ -1,63 +0,0 @@
-;
-; 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
diff --git a/libsrc/atari/systime.s b/libsrc/atari/systime.s
deleted file mode 100644 (file)
index 273e394..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-;
-; 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
-
-
diff --git a/libsrc/atmos/systime.s b/libsrc/atmos/systime.s
deleted file mode 100644 (file)
index 273e394..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-;
-; 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
-
-
diff --git a/libsrc/c128/gettime.s b/libsrc/c128/gettime.s
new file mode 100644 (file)
index 0000000..b597891
--- /dev/null
@@ -0,0 +1,119 @@
+;
+; 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
diff --git a/libsrc/c128/systime.s b/libsrc/c128/systime.s
deleted file mode 100644 (file)
index b2a7f87..0000000
+++ /dev/null
@@ -1,94 +0,0 @@
-;
-; 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
diff --git a/libsrc/c16/systime.s b/libsrc/c16/systime.s
deleted file mode 100644 (file)
index 273e394..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-;
-; 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
-
-
diff --git a/libsrc/c64/gettime.s b/libsrc/c64/gettime.s
new file mode 100644 (file)
index 0000000..61c5115
--- /dev/null
@@ -0,0 +1,122 @@
+;
+; 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
diff --git a/libsrc/c64/systime.s b/libsrc/c64/systime.s
deleted file mode 100644 (file)
index c28ace3..0000000
+++ /dev/null
@@ -1,97 +0,0 @@
-;
-; 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
diff --git a/libsrc/cbm510/gettime.s b/libsrc/cbm510/gettime.s
new file mode 100644 (file)
index 0000000..3ad162e
--- /dev/null
@@ -0,0 +1,107 @@
+;
+; 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
diff --git a/libsrc/cbm510/systime.s b/libsrc/cbm510/systime.s
deleted file mode 100644 (file)
index daac36d..0000000
+++ /dev/null
@@ -1,98 +0,0 @@
-;
-; 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
-
diff --git a/libsrc/cbm610/gettime.s b/libsrc/cbm610/gettime.s
new file mode 100644 (file)
index 0000000..3022621
--- /dev/null
@@ -0,0 +1,108 @@
+;
+; 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
+
diff --git a/libsrc/cbm610/systime.s b/libsrc/cbm610/systime.s
deleted file mode 100644 (file)
index be2bedf..0000000
+++ /dev/null
@@ -1,98 +0,0 @@
-;
-; 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
-
index 140d47e2c344b6164bc5ef1ee4a97848a250820c..9b297d4d350bbef25e42ed2771f6876bfa48c78b 100644 (file)
@@ -6,10 +6,10 @@
 
         .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
 
@@ -63,4 +76,8 @@
 
 .endproc
 
+; ------------------------------------------------------------------------
+; Data
 
+.bss
+time:   .tag    timespec
diff --git a/libsrc/geos-common/system/gettime.c b/libsrc/geos-common/system/gettime.c
new file mode 100644 (file)
index 0000000..d695787
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+** 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(&currentTime);
+}
+
+int clock_gettime(clockid_t, struct timespec *tp)
+{
+    tp->tv_sec = clock();
+    tp->tv_nsec = 0;
+
+    return 0;
+}
diff --git a/libsrc/geos-common/system/systime.c b/libsrc/geos-common/system/systime.c
deleted file mode 100644 (file)
index 5eb87fe..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
-** 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(&currentTime);
-}
-
-clock_t clock(void)
-{
-    return _systime();
-}
diff --git a/libsrc/plus4/systime.s b/libsrc/plus4/systime.s
deleted file mode 100644 (file)
index 273e394..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-;
-; 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
-
-