;os_byte_19
;----------
;PRM1-652 - implements frame pacing
;
;Exit:
; R1, R2 corrupted

.os_byte_19
 #if compile_GPU == 1
   LDR     R14, IRQsema			;check IRQsema
   LDR     R14, [R14, #0]
   TEQ     R14, #0			;is an IRQ occuring
   EXITSWINE ""				;YES, exit
 #endif
 LDR     R14, force_vsync		;NO,
 TEQ     R14, #0			; |  are we adding frames?
 BEQ     vector_handler_forward		; |+ NO, or re-entrant
 MOV     R1, R14			;R1=ticks to wait

 LDR     R0, _os_byte_19_time		;R0=time we were last called

 BL      pre_SWI_veneer			;call SWI that follows
 SWI     XOS_IntOn			;enable IRQ

 ADD     R1, R1, R0			;add to get exit time
 ._L1					;wait
   #if power_save_processor == 1
     SWI     XPortable_Idle		;generic WFI
   #endif
   SWI     XOS_ReadMonotonicTime	;PRM1-446
   CMP     R0, R1
 BLO     _L1
 STR     R0, _os_byte_19_time		;time to expect next frame

 #if No_Frame_Pacing == 1
   MOV     R0, #19			;preserve R0
   BL      pass_SWI_on
   SWI     XOS_Byte			;PRM1-652 force a physical VSync
 #endif

 MOV     R0, #19			;preserve R0
 STR     R0, os_byte_19_happened	;mark as seen
B       exit_SWI_veneer

 ._os_byte_19_time	DCD 0
.os_byte_19_happened	DCD 0
