1
0
mirror of https://github.com/cc65/cc65.git synced 2024-12-25 17:29:50 +00:00
cc65/libsrc/common/time.s
Oliver Schmidt 842c151edd 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().
2018-08-15 16:06:44 +02:00

84 lines
1.5 KiB
ArmAsm

;
; Ullrich von Bassewitz, 12.11.2002
;
; time_t __fastcall__ time (time_t* timep);
;
.export _time
.import decsp1
.importzp ptr1, sreg, tmp1, tmp2
.include "time.inc"
.code
.proc _time
pha
txa
pha ; Save timep
; 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
pla
sta ptr1+1
pla
sta ptr1 ; Restore timep
ora ptr1+1 ; timep == 0?
beq @L1
; timep is not NULL, store the result there
ldy #3
lda sreg+1
sta (ptr1),y
dey
lda sreg
sta (ptr1),y
dey
txa
sta (ptr1),y
dey
lda tmp1
sta (ptr1),y
; If the result is != 0, return -1
@L1: lda tmp2
beq @L2
tax
sta sreg
sta sreg+1
rts
; Reload the low byte of the result and return
@L2: lda tmp1
rts
.endproc
; ------------------------------------------------------------------------
; Data
.bss
time: .tag timespec