Replaced _systime with clock_gettime.

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().
This commit is contained in:
Oliver Schmidt
2018-08-15 15:59:11 +02:00
parent 55a07c1dcd
commit 842c151edd
15 changed files with 295 additions and 270 deletions

View File

@@ -1,27 +1,27 @@
;
; Stefan Haubenthal, 27.7.2009
; Oliver Schmidt, 14.8.2018
;
; 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
; */
; int clock_gettime (clockid_t clk_id, struct timespec *tp);
;
.include "time.inc"
.include "c64.inc"
.include "get_tv.inc"
.constructor initsystime
.importzp tmp1, tmp2
.constructor inittime
.importzp sreg, tmp1, tmp2
.import pushax, pusheax, tosmul0ax, steaxspidx, incsp1, return0
.import _get_tv, _get_ostype
;----------------------------------------------------------------------------
.code
.proc __systime
.proc _clock_gettime
jsr pushax
jsr pushax
lda CIA1_TODHR
bpl AM
@@ -38,13 +38,38 @@ AM: jsr BCD2dec
lda CIA1_TODSEC
jsr BCD2dec
sta TM + tm::tm_sec
lda CIA1_TOD10 ; Dummy read to unfreeze
lda #<TM
ldx #>TM
jmp _mktime
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))
BCD2dec:tax
.proc BCD2dec
tax
and #%00001111
sta tmp1
txa
@@ -65,7 +90,7 @@ BCD2dec:tax
; and write it again, ignoring a possible change in between.
.segment "ONCE"
.proc initsystime
.proc inittime
lda CIA1_TOD10
sta CIA1_TOD10