;Entry_EmulateRISCOS
;-------------------
;Switch behaviour to a particular version of RISCOS:
; Arthur	softFont loaded at 36C0
;  to 4.99	Sound_Configure,1,&D0,&30 is issued
; 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
;		softFont loaded at 3700
; 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
;		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

 BL      remove_this_taskID_if_no_JIT	;stop tracking this task
 LDMFD   R13!, {R0-R4, R11, PC}


 ._read_version
 MOV     R11, #0
 LDRB    R2, [R0]			;check for Arthur
 BIC     R2, R2, #32
 TEQ     R2, #ASC("A")			;is it Arthur?
 BEQ     _Arthur			;YES, version=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

 ._Arthur
 STR     R11, [R12, #emulate_RISCOS - VARS]
 BL      load_softFont			;load softFont if JIT running

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

 BL      get_taskID			;need to track the taskID
 #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
   LDR     R0, _AUTO			;"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
#if compile_RO > 400
 ._AUTO			DCB     "AUTO"
#endif



;load_softFont
;-------------
;builds the softFont table in Page Zero

;Entry
; R12 - pointer to VARS

.load_softFont
 STMFD   R13!, {R0-R3, R14}
 BL      check_JIT_and_taskID		;is the JIT running
 LDMNEFD R13!, {R0-R3, PC}		;NO

 LDR     R14, [R12, #jit_code - VARS]
 LDR     R0, [R12, #emulate_RISCOS - VARS]
 CMP     R0, #0				;is it Arthur?
 MOVEQ   R1, #&36C0			;YES
 MOVNE   R1, #&3700			;NO

 MOV     R0, #0				;clear whole area
 MOV     R2, #&36C0
 ADD     R2, R2, R14
 MOV     R3, #&740
 ._L1
   STR     R0, [R2], #4
   SUBS    R3, R3, #4
 BNE     _L1

 MOV     R2, #32			;build font table
 ADD     R1, R1, R14
 MOV     R0, #10
 ._L2
   LDRB    R3, [R1, #-1]!
   STRB    R2, [R1]
   SWI     XOS_Word			;PRM1-702
   STRB    R3, [R1], #9
   ADD     R2, R2, #1
   TEQ     R2, #127
   ADDEQ   R2, R2, #1
   ADDEQ   R1, R1, #9
   TEQ     R2, #256
 BNE      _L2
LDMFD   R13!, {R0-R3, PC}
