;Entry_ADFRemapVideoMemory
;------------------------
;Remaps video memory to <2000000 to fix games hardcoded for <=RO3.1
;Needs to be ARM600+ and RISCOS 3.5+ compatible
;
;Entry:
; R0 - command line in format "<video mode> <memory to reserve>"
;    - only one parameter indicates do not change mode.  ie "WIMP"
;    - =-1 if called from GOARM3JIT
; R1 - number of parameters
; R12 - pointer to module private word or unset if called internally
;
;Exit:
; R0 - error block if V set, corrupt otherwise

.Entry_ADFRemapVideoMemory
 STMFD   R13!, {R1-R9, R12, R14}
 ADR     R12, VARS

 MOV     R8, R0				;R8=commandline
 MOVS    R9, R1				;R9=no. of parameters
 BNE     _P1				;parameters? YES

 LDR     R1, existing_DataAbort
 TEQ     R1, #0				;is the vector claimed?
 LDMEQFD R13!, {R1-R9, R12, PC}		;NO, exit

 LDR     R2, [R12, #is_screen_remapped - VARS]
 TEQ     R2, #0				;is screen memory remapped?
 BLNE    unmap_screen_memory		;YES, unmap it

 #if JIT_WIMP_support == 1
   LDR     R0, [R12, #domainID - VARS]
   LDR     R0, [R0, #0]
   BL      remove_task_ID		;stop tracking the current task
 #endif

 #if compile_GPU == 1
   MOV     R0, #0
   TEMP    R1
   STR     R0, Cursor_state		;ensure the cursor is off
   LOCK    R1

   BL      release_GraphicsV_Vector
 #endif

 BL      release_DataAbort_Vector

 #if compile_RO == 400
   LDR     R0, MEMC_L1PT
   TEQ     R0, #0				;is there a Section for the old
   LDRNE   R1, L1PT_addr			;..MEMC address range?
   STRNE   R0, [R1, #&3600000 >> (20 - 2)]	;YES, put it back
 #endif

 MOV     R0, #3				;see if task is WIMP based
 SWI     XWimp_ReadSysInfo		;PRM3-217
 TEQ     R0, #1				;=1 if task is at the desktop
 MOVNE   R0, #0
 MOVNE   R1, #28			;go to MODE 28
 SWINE   XOS_ScreenMode			;PRM5a-133
LDMFD   R13!, {R1-R9, R12, PC}


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

 LDR     R1, existing_DataAbort
 TEQ     R1, #0				;is the vector claimed?
 LDMNEFD R13!, {R1-R9, R12, PC}		;YES, exit

 BL      Claim_DataAbort_Vector		;claim DataAbort vector
 LDMVSFD R13!, {R1-R9, R12, PC}		;exit on error

 #if compile_RO == 400
 ;@@@ need to claim this legally
   LDR     R1, L1PT_addr			;take a copy of the MEMC1
   LDR     R0, [R1, #&3600000 >> (20 - 2)]	;..address range in L1PT
   AND     R2, R0, #%11
   TEQ     R2, #%10				;is it a Section?
   TEMP    R2
   STREQ   R0, MEMC_L1PT			;YES, take a copy of it
   MOVEQ   R0, #0				; | blank it
   STREQ   R0, [R1, #&3600000 >> (20 - 2)]	; |+ and update it
   LOCK    R2
 #endif

 TEQ     R9, #2				;*ADFRemapVideoMemory WIMP?
 MVNNE   R0, #0				;YES
 STRNE   R0, [R12, #closest_screen_mode - VARS]	;mark current MODE as unknown
 LDMNEFD R13!, {R1-R9, R12, PC}		; |+ exit

 MOV     R1, R8				;command tail
 MOV     R0, #10 + (1<<30)		;base 10 number in range 0-255
 SWI     XOS_ReadUnsigned		;read the value PRM1-460
 LDMVSFD R13!, {R1-R9, R12, PC}		;exit on error
 MOV     R9, R2				;R9=mode

 MOV     R0, #10 + (1<<29)		;base 10 number with restricted range
 MOV     R2, #480			;max memory we can remap
 SWI     XOS_ReadUnsigned		;read the value PRM1-460
 LDMVSFD R13!, {R1-R9, R12, PC}		;exit on error
 MOV     R3, R2, LSL #10		;R3 = size in bytes
 BL      _set_DA_2			;change the DA size first

 MOV     R0, R9
 SWI     XOS_WriteI + 22		;VDU 22 PRM1-594
 SWI     XOS_WriteC			;VDU 22, <video mode>

 BL      _set_DA_2			;change DA size again just to be sure
 BL      get_screen_start		;get the display screen start

 SUBS    R0, R0, R0			;clear V
LDMFD   R13!, {R1-R9, R12, PC}




;_set_DA_2
;---------
;change the size of the Screen DA
;
;Entry:
; R3 - size in bytes
;
;Exit:
; R0 - corrupt or error if V set

._set_DA_2
 STMFD   R13!, {R1-R3, R14}
 MOV     R0, #DA2_magic_number		;read current screen size
 SWI     XOS_ReadDynamicArea		;PRM1-396

 SUBS    R1, R3, R1			;do we need to change the size?
 MOV     R0, #2				;screen size
 SWINE   XOS_ChangeDynamicArea		;  |+ PRM1-384
 BVS     _set_DA_2_exit			;exit on error

 #if compile_RO == 400
   LDR     R14, os_version
   AND     R14, R14, #&F00 << 8
   CMP     R14, #&400 << 8		;RO4?
   LDREQ   R14, cpu_id
   TEQEQ   R14, #CPUID_StrongARM	;YES, StrongARM?
   LDMEQFD R13!, {R1-R3, PC}		;exit if RO4/SA, DA size will be wrong
 #endif

 MOV     R0, #DA2_magic_number
 SWI     XOS_ReadDynamicArea		;did the command succeed? PRM1-396
 CMP     R1, R3				;is there >= what we need?
 MOVLO   R0, #1<<31			;NO
 ADDLOS  R0, R0, R0			;|+ set V
 ._set_DA_2_exit
 ADRVS   R0, Error_ScreenSize		;produce an error
LDMFD   R13!, {R1-R3, PC}




#if JIT_WIMP_support == 1
;get_domainID_address
;--------------------
;gets the address of domainID in Page Zero from the OS if possible (RO5.17+)

.get_domainID_address
 STMFD   R13!, {R14}

 TEMP    R14
 #if compile_RO > 400 AND JIT_WIMP_support == 1
   LDR     R0, os_version
   MOV     R1, #&517 << 8		;RO 5.17+
   CMP     R0, R1
   BLO     _P1				;NO
   MOV     R0, #6			;request system internal info
   MOV     R1, #0			;request one item
   MOV     R2, #70			;item# for domainID
   SWI     XOS_ReadSysInfo		;see ROOL online documentation
   STR     R2, domainID			;store domainID pointer
   ._P1
 #endif
 MVN     R0, #0
 STR     R0, JIT_task_ID
 LOCK    R14
LDMFD   R13!, {PC}




;get_taskID
;----------
;gets the current taskID

.get_taskID
 STMFD   R13!, {R0, R14}

 LDR     R0, domainID
 LDR     R0, [R0, #0]
 TEMP    R14
 STR     R0, JIT_task_ID		;store so we can check later
 LOCK    R14
LDMFD   R13!, {R0, PC}




;check_task_ID
;-------------
;checks if the current task is one we're handling
;
;Exit:
; EQ if task is supported

.check_task_ID
 STMFD   R13!, {R12, R14}

 LDR     R12, domainID			;address of current task ID
 LDR     R14, JIT_task_ID		;JIT task ID
 LDR     R12, [R12, #0]			;get current task ID
 TEQ     R14, R12			;is this the task we support?
LDMFD   R13!, {R12, PC}




;check_and_return_task_ID
;------------------------
;checks if the current task is one we're handling
;
;Exit:
; EQ if task is supported
; R0 - current task ID

.check_and_return_task_ID
 STR     R14, [R13, #-4]!

 LDR     R0, domainID			;address of current task ID
 LDR     R0, [R0, #0]			;get current task ID
 LDR     R14, JIT_task_ID		;JIT task ID
 TEQ     R14, R0			;is this the task we support?
LDR     PC, [R13], #4




;remove_task_ID
;--------------
;removes a task from the monitored list
;
;Entry:
; R0 - task ID

.remove_task_ID
 STMFD   R13!, {R12, R14}
 ADR     R12, JIT_task_ID
 MVN     R14, #0
 STR     R14, [R12, #0]
LDMFD   R13!, {R12, PC}
#endif
