;References:
;swi - vector_handler_forward
;swi_OS_Module - vector_handler_trap

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

.os_byte_19
 #if compile_GPU == 1
   LDR     R12, graphicsV_claimed
   TEQ     R12, #0			;is our graphicsV driver active?
   BEQ     vector_handler_forward	;NO, pass to OS
   LDRNE   R12, IRQsema			;check IRQsema
   LDRNE   R12, [R12, #0]
   TEQNE   R12, #0			;is an IRQ occuring
   BNE     vector_handler_trap		;YES, exit
   LDR     R1, force_vsync		;NO
   TEQ     R1, #0			;are we adding frames?
   MOVEQ   R1, #2			;NO, use 50hz instead
 #else
   LDR     R12, force_vsync		;NO,
   TEQ     R12, #0			; |  are we adding frames?
   BEQ     vector_handler_forward	; |+ NO, or re-entrant
   MOV     R1, R12			;R1=ticks to wait
 #endif

 ADR     R12, _SPSR			;buffer for C/SPSR (pre_swi_veneer)

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

 #if compile_IOMD == 1 AND JIT_debug == 1
   BL      pre_SWI_veneer		;call SWI that follows
   SWI     XOS_IntOn			;enable IRQ
 #else
   BL      pre_SWI_veneer		;call SWI that follows
   SWI     XOS_IntOn			;enable IRQ
 #endif

 ADD     R1, R1, R0			;add to get exit time
 ._L1					;wait
   #if power_save_processor == 1
     WFI				;wait for interrupt
   #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
 ._SPSR				DBD 4

.os_byte_19_happened	DCD 0
