;Entry_EmulateRISCOS
;-------------------
;Switch behaviour to a particular version of RISCOS:
; 2.00		OS_Byte 129,0,255 returns &A1
; 2.00..3.11	OS_ReadSysInfo return R0=0 R1=0 R2=0
;		OS_ClaimProcessorVector returns an error
; 2.00..3.80	OS_SpriteOp 36 changes R7 to 0 if it's -1
;		OS_SpriteOp 36 changes R6 to 0 if it's -1
; 2.00..4.99	OS_Byte 14 does nothing when the event already disabled
; 2.01		OS_Byte 129,0,255 returns &A2
; 3.00		OS_Byte 129,0,255 returns &A3
; 3.10/3.11	OS_Byte 129,0,255 returns &A4
;		OS_Word 21,0 returns after the next VSync
;		(JIT) OS_Memory returns an error
;		IRQv instruction at &18 is changed to B &xxx if host
;		  RISCOS < 5.00 and not running on a StrongARM
;		to 4.99 Sound_Configure,1,&D0,&30 is issued
;		Sound_Configure will use the previous sample period if R2=0 and
;		  VIDC1 Sound Frequency Register has been altered directly
;		OS_SpriteOp 3 turns the cursor off (*ScreenLoad)
;		OS_Memory return SWI not known
;		Sound_Mode returns SWI not known
; 3.50		OS_Byte 129,0,255 returns &A5
;		to 4.99 (JIT) OS_Memory 9,&300 returns &3500000 if host RISCOS >= 5.00
;		Sound_Mode returns 8bit only
; 3.60		OS_Byte 129,0,255 returns &A6
; 3.70		OS_Byte 129,0,255 returns &A7
; 3.80		OS_Byte 129,0,255 returns &A8
; 3.50..3.80	OS_ReadSysInfo return R0=&1010100 R1=1 R2=0
;
;Entry:
; R0 - Command tail
; R12 = pointer to module private word

.Entry_EmulateRISCOS
 STMFD   R13!, {R0-R4, R11, R14}
 LDR     R12, [R12, #0]

 TEQ     R1, #1				;is there a parameter?
 BEQ     _read_version			;YES

 MVN     R0, #0
 STR     R0, [R12, #emulate_RISCOS - VARS]	;switch to current OS behaviour

 #if compile_RO == 400
   MOV     R1, #0
   LDR     R0, [R12, #IRQ_vector - VARS]
   TEQ     R0, #0
   STRNE   R1, [R12, #IRQ_vector - VARS]
   STRNE   R0, [R1, #&18]
 #endif

 #if compile_RO > 400
 ; ref. www.riscosopen.org/wiki/documentation/show/Sound_Tuning
   MOV     R0, #&4F545541			;"AUTO"
   MOV     R1, #2				;Enable auto tuning
   SWI     XSound_Tuning
 #endif

 #if JIT_WIMP_support == 1
   LDR     R0, [R12, #jit_code - VARS]
   TEQ     R0, #0				;is the JIT running?
   MOVEQ   R0, #0				;NO, reset the task ID
   STREQ   R0, [R12, #JIT_task_ID - VARS]
 #endif

 LDMFD   R13!, {R0-R4, R11, PC}

 ._read_version
 MOV     R11, #0
 MOV     R3, #5				;read "X.YY"
 ._L1
   LDRB    R2, [R0], #1			;get byte
   CMP     R2, #ASC(".")		;ignore "."
   SUBHI   R2, R2, #ASC("0")		;NO, convert to BCD value
   ORRHI   R11, R2, R11, LSL #4
   SUBHI   R3, R3, #1
 BHS     _L1
					;end up as XYY00
 ._L2
   SUBS    R3, R3, #1
   MOVPL   R11, R11, LSL #4
 BPL     _L2

 STR     R11, [R12, #emulate_RISCOS - VARS]

 CMP     R11, #&50000			;RISCOS 5.00 or above?
 LDMHSFD R13!, {R0-R4, R11, PC}		;YES, exit

 #if JIT_WIMP_support == 1
   BL      get_taskID			;get current task
 #endif

 #if compile_RO > 450
					;set default sound settings
   MOV     R0, #1			;no. channels
   MOV     R1, #&D0			;RISCOS 3.1 default sample length
   MOV     R2, #&30			;RISCOS 3.1 default sample period
   MOV     R3, #0			;channel handler
   MOV     R4, #0			;scheduler
   SWI     XSound_Configure		;PRM4-18
 #endif

 #if compile_RO > 400
  ; ref. www.riscosopen.org/wiki/documentation/show/Sound_Tuning
   MOV     R0, #&4F545541		;"AUTO"
   MOV     R1, #1			;Disable auto tuning
   SWI     XSound_Tuning
 #endif

 #if compile_RO == 400
					;3.11 EAE00CE7 B &38033BC
					;3.50 E59FF31C LDR PC, &33C (038039CC)
					;3.71 E59FF39C LDR PC, &3BC (03803EC0)
					;4.02 E59FF400 LDR PC, &420 (038008A0)
					;5.21 E59FF2B8 LDR PC, &2D8 (FC012300)
   MOV     R1, #0
   LDR     R4, [R1, #&18]		;copy IRQ vector
   AND     R3, R4, #&FF << 24		;mask instruction
   TEQ     R3, #&E5			;is it LDR PC?
   BNE     _skip_IRQ_change		;NO, leave as is
   STR     R4, [R12, #IRQ_vector - VARS]	;store for later reset

   AND     R4, R4, #&FFF		;R4=offset to address
   ADD     R5, R4, #4
   MOV     R5, R5, LSR #2
   ORR     R5, R5, #&EA000000

   SWI     XOS_IntOff
   STR     R5, [R1, #&18]		;change IRQv to B &340 (RO3.5)
   LDR     R5, _LDR_PC_minus_4
   STR     R5, [R4, #&24]		;store LDR PC, &33C at &340 (RO3.5)
   SWI     XOS_IntOn

   ._skip_IRQ_change
 #endif

LDMFD   R13!, {R0-R4, R11, PC}


#if compile_RO == 400
 ._LDR_PC_minus_4	LDR     PC, [PC, #-12]
#endif