add sleep

This commit is contained in:
matthias@rpi 2023-11-09 12:07:37 +01:00
parent 0b1dcf4998
commit db80bd9aa3

41
programs/sleep.s65 Normal file
View File

@ -0,0 +1,41 @@
.ifndef INCLUDE_SLEEP
INCLUDE_SLEEP = 1
;********************************************************************************
; @macro Sleep
; @param time_cs: Time to sleep in centiseconds (10^-2s = 10ms)
; @details
; Interrupts might change the actual time to finish
;********************************************************************************
.macro Sleep time_cs
phx
ldx #time_cs
jsr sleep
plx
.endmacro
;********************************************************************************
; @function sleep
; @param x: Time to sleep in centiseconds (10^-2s = 10ms)
; @details
; Interrupts might change the actual time to finish
; To be exact, time_cs is in units of 0.010244s
;********************************************************************************
.proc sleep
VAR_1 = 0
stz VAR_1
@loop:
.repeat 17
nop ; 2 - i
.endrepeat
inc VAR_1 ; 3 - zp
bne @loop ; 2/3 - r (2 if not taken, but here most of the time its 3)
; reaching this takes 256 * (X * 2(nop) + 3(inc) + 3(bne)) - 1(last bne) cycles
; with X = 17: T1 = (256×(17×2 + 3 +3) 1) × (1/1MHz) = 0,010239s
dex ; 2 - i
bne @loop ; 2/3 - r
; with T1 = for running through VAR_1, one iteration takes
; T1 + (2(dex) + 3(bne)) × (1/1MHz) = 0,010244s
rts
; so T = N * 0,010244s - (1(last bne) + 4(jsr) + 6(rts)) * (1/1MHz)
.endproc
.endif ; guard