; print jiffies as seconds, with possible decimal point, to the
; active screen.
; unsigned divide based on Ullrich von Bassewitz's runtime/udiv.s.
; uses FR1 as a temp, rather than any of cc65's ZP storage,
; to avoid clobbering.
; Note: this isn't that much smaller than the C version was, but it
; *does* fix a bug: leading zeroes weren't being printed for fractions
; less than 0.1 sec (e.g. 0.04 came out as 0.4).

 FR1 = $e0
 acc16 = FR1    ; 16 bits
 remainder = FR1+2 ;  8 bits
 temp = FR1+3 ; 16 bits

 _hz = $f0 ; must agree with timers.h!

 .import _scr_act_printnum, _scr_putc_active
 .import _read_rtclok, _a2uint
 .import mulax10

 ; extern void print_jif_sec(unsigned int jiffies);
 .export _print_jif_sec, _calc_ping_time

_calc_ping_time:
 jsr _a2uint
 sta acc16
 stx acc16+1 ; acc16 = timestamp in ping response
 jsr _read_rtclok ; A/X = low 15 bits of RTCLOK
 cpx acc16+1 ; has RTCLOK rolled over?
 bcs @clok_ok
 pha
 txa
 ora #$80
 tax
 pla
@clok_ok:
 sec
 sbc acc16
 pha
 txa
 sbc acc16+1
 tax
 pla ; A/X now holds "now" minus timestamp

_print_jif_sec:
 jsr divbyhz
 lda acc16
 ldx acc16+1
 jsr _scr_act_printnum ; print the seconds (it's OK if it's 0)
 lda remainder
 bne @printfrac
 rts ; if it was an even number of seconds, we're done
@printfrac:
 lda #'.' ; otherwise, we need a decimal point...
 jsr _scr_putc_active
 lda remainder ; remainder is 0..(_hz-1), scale to 100ths of a sec.
 ldx #0
 jsr mulax10 ; convenient cc65 runtime routine...
 jsr mulax10 ; AX now remainder * 100
 sta acc16
 stx acc16+1
 jsr divbyhz ; acc16 = remainder / hz
 lda acc16 ; acc16 range is 0..99 (ignore hi byte)
 cmp #$0a  ; does it need a leading zero?
 bcs @no_0
 lda #'0'
 jsr _scr_putc_active
 lda acc16 ; clobbered by printing, reload
@no_0:
 ldx acc16+1
 jmp _scr_act_printnum ; print it, we're done

; NOTE: if we wanted to, we could round, by checking the high bit
; of remainder and adding one to acc16 before printing. not going
; to bother for now, making it look more accurate won't make it
; really *be* more accurate.

; divide 16-bit a/x by 8-bit _hz.
; result in acc16, remainder in remainder.
divbyhz:
 sty temp
 sta acc16
 stx acc16+1
 lda #0
 ldy #16

@L0:
 asl acc16
 rol acc16+1
 rol a
 bcs @L1

 cmp _hz
 bcc @L2
@L1:
 sbc _hz
 inc acc16

@L2:
 dey
 bne @L0
 sta remainder
 ldy temp
 rts
