; ======================================================================
; lib-set-01 / TIMER -- r005 source candidate.
; HD61700 TM sampled twice for a stable count (Chapter 01/Tiny precedent).
; TMR_SAMPLE: returns $8 = stabilized TM sample; clobbers $9.
; TMR_WAIT:   input $10 low byte = number of TM changes (0 returns now).
;             waits for that many observed transitions, not CPU loops.
;             clobbers $8,$9,$22,$23 and flags; calls OFFCHK.
;             $SY must represent one, maintained by platform scaffold.
; No RAM required. Not calibrated to exact wall-clock seconds.
; Not reentrant or interrupt-proof; dedicated Lab qualification pending.
; ======================================================================
TMR_SAMPLE:
        GST     TM,$8
TMR_SAGAIN:
        LD      $9,$8
        GST     TM,$8
        SBC     $8,$9
        JR      NZ,TMR_SAGAIN
        RTN

TMR_WAIT:
        LD      $23,$10
        AN      $23,$23
        RTN     Z
        CAL     TMR_SAMPLE
        LD      $22,$8
TMR_WLOOP:
        CAL     OFFCHK
        CAL     TMR_SAMPLE
        SBC     $8,$22
        JR      Z,TMR_WLOOP
        LD      $22,$8
        SB      $23,$SY
        JR      NZ,TMR_WLOOP
        RTN
