From e4fb5946dd9dd148ce0e8f19d5a0a178b4450f12 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Mon, 17 May 2021 18:44:42 +0200 Subject: [PATCH] optimize cx16 sys.wait and sys.waitvsync to use WAI instruction --- compiler/res/prog8lib/cx16/syslib.p8 | 38 ++++++++++++++-------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/compiler/res/prog8lib/cx16/syslib.p8 b/compiler/res/prog8lib/cx16/syslib.p8 index 698e6a616..03c4e3979 100644 --- a/compiler/res/prog8lib/cx16/syslib.p8 +++ b/compiler/res/prog8lib/cx16/syslib.p8 @@ -759,28 +759,28 @@ sys { }} } - sub wait(uword jiffies) { - ; --- wait approximately the given number of jiffies (1/60th seconds) - repeat jiffies { - ubyte jiff = lsb(c64.RDTIM16()) - while jiff==lsb(c64.RDTIM16()) { - ; wait until 1 jiffy has passed - } - } + asmsub wait(uword jiffies @AY) { + ; --- wait approximately the given number of jiffies (1/60th seconds) (N or N+1) + ; note: regular system vsync irq handler must be running, and no nother irqs + %asm {{ +- wai ; wait for irq (assume it was vsync) + cmp #0 + bne + + dey ++ dec a + bne - + cpy #0 + bne - + rts + }} } - asmsub waitvsync() clobbers(A, X, Y) { - ; --- busy wait till the next vsync has occurred (approximately), without depending on custom irq handling. - ; note: system vsync irq handler has to be active for this routine to work. - ; note 2: a more accurate way to wait for vsync is to set up a vsync irq handler instead. + inline asmsub waitvsync() { + ; --- suspend execution until the next vsync has occurred, without depending on custom irq handling. + ; note: system vsync irq handler has to be active for this routine to work (and no other irqs). + ; note: a more accurate way to wait for vsync is to set up a vsync irq handler instead. %asm {{ - jsr c64.RDTIM - sta _mod + 1 - inc _mod + 1 -_loop jsr c64.RDTIM -_mod cmp #255 ; modified - bne _loop - rts + wai }} }