;Entry_xxxxSize
;----------------
;alters the size of the area
;
;Entry:
; R0 - command line in format "<memory to reserve>[K] <error>"
; R1 - number of parameters
; R12 - pointer to module private word
;
;Exit:
; R0 - error block if V set

.Entry_ScreenSize
 STMFD   R13!, {R1-R6, R14}

 #if compile_RO > 311
   LDR     R12, [R12, #0]
   LDR     R3, [R12, #emulate_RISCOS - VARS]
   CMP     R3, #&350 << 8		;don't restrict RO3.50+
   MOVHI   R2, #2048
   MOVLS   R2, #480			;max memory on RO3.11
 #endif
 #if compile_RO == 311
   MOV     R2, #480			;max memory we can remap
 #endif
 BL      MA_readUnsigned		;R2=value

 #if compile_RO < 500
   STMFD   R13!, {R2}			;read current DA2 size
   #if compile_IOMD == 1
     MOV     R0, #DA2_magic_number
   #endif
   #if compile_RO == 311
     MOV     R0, #2
   #endif
   SWI     XOS_ReadDynamicArea		;PRM1-396
   #if compile_RO == 400
     LDRB    R14, [R12, #riscos6 - VARS]
     TEQ     R14, #0				;RISC OS 4/6?
     SUBEQ   R14, R2, R1
     STREQ   R14, [R12, #DA2_delta - VARS]	;fix RO4/6 SA
   #endif
   CMP     R2, R1			;are we shrinking the area?
   BHI     _P1				;NO
					;YES, change video mode
   SWI     XOS_WriteI + 22		;can't use OS_ScreenMode here
   SWI     XOS_WriteI + 0
   MOV     R0, #19			;workaround to avoid Red Squirrel
   SWI     XOS_Byte			;..crashing when changing MODE
   SWI     XOS_Byte			;..directy before setting DA2's size
   SWI     XOS_Byte
   SWI     XOS_Byte
   ._P1
   LDMFD   R13!, {R2}
 #endif

 MOV     R0, #2				;screen size
B       MA_changeDynamicArea


.Entry_SystemSize
 STMFD   R13!, {R1-R6, R14}

 MOV     R2, #2048			;max memory we can remap
 BL      MA_readUnsigned		;R2=value

 MOV     R0, #0				;system heap
B      MA_changeDynamicArea


.Entry_RMASize
 STMFD   R13!, {R1-R6, R14}

 MOV     R2, #4096			;max memory we can remap
 BL      MA_readUnsigned		;R2=value

 #if ZLib_RMASpace > 0
   ADD     R2, R2, #ZLib_RMASpace	;increase RMA for ZLib module
 #endif

 MOV     R0, #1				;RMA
B      MA_changeDynamicArea


.Entry_SpriteSize
 STMFD   R13!, {R1-R6, R14}

 MOV     R2, #4096			;max memory we can remap
 BL      MA_readUnsigned		;R2=value

 MOV     R0, #3				;sprite area
B      MA_changeDynamicArea


.Entry_FontSize
 STMFD   R13!, {R1-R6, R14}

 MOV     R2, #1024			;max memory we can remap
 BL      MA_readUnsigned		;R2=value

 MOV     R0, #4				;font cache
B      MA_changeDynamicArea


.Entry_RAMFSSize
 STMFD   R13!, {R1-R6, R14}

 MOV     R2, #4096			;max memory we can remap
 BL      MA_readUnsigned		;R2=value

 MOV     R0, #5				;RAM FS
B      MA_changeDynamicArea


.Entry_RAMFSFree
 STMFD   R13!, {R1-R6, R14}

 MOV     R2, #4096			;max memory we can remap
 BL      MA_readUnsigned		;R2=value
 MOV     R5, R2				;preserve R2

 MOV     R0, #ASC("0")			;null terminated 0
 MOV     R1, #0				;assume no RAM drive
 STMFD   R13!, {R0, R8}
 MOV     R0, R13
 SWI     XRamFS_FreeSpace		;PRM2-319
 LDMFD   R13!, {R3, R8}
 CMP     R1, R5				;is there enough space?
 BPL     MA_exit			;YES, exit

 MOV     R0, #5				;RAMFS area
 MOV     R2, R5
B       MA_read_and_changeDynamicArea


.Entry_RMAFree
 STMFD   R13!, {R1-R6, R14}

 #if ZLib_RMASpace > 0
   CMN     R0, #1			;are we being called by Boot Floppy?
   MOVEQ   R5, #ZLib_RMASpace		;YES, increase RMA space
   BEQ     _P1
 #endif

 MOV     R2, #4096			;max memory we can remap
 BL      MA_readUnsigned		;R2=value

 #if ZLib_RMASpace > 0
   ADD     R2, R2, #ZLib_RMASpace	;increase RMA for ZLib module
 #endif

 MOV     R5, R2				;preserve R2
 ._P1
 MOV     R0, #Module_DescribeRMA	;PRM1-236
 SWI     XOS_Module			;R2=free
 SUBS    R0, R2, R5			;is there enough space?
 BPL     MA_exit			;YES, exit

 MOV     R0, #1				;RMA
B       MA_read_and_changeDynamicArea


.Entry_SpriteFree
 STMFD   R13!, {R1-R6, R14}

 MOV     R2, #4096			;max memory we can remap
 BL      MA_readUnsigned		;R2=value
 MOV     R6, R2				;preserve R2

 MOV     R0, #3				;sprite area
 SWI     XOS_ReadDynamicArea		;read current size PRM1-396
 TEQ     R1, #0				;is it zero?
 ADDEQ   R1, R6, #16			;YES, create area then
 MOVEQ   R0, #3				;|    sprite area
 SWIEQ   XOS_ChangeDynamicArea		;|+   create area

 MOV     R0, #SpriteOp_ReadArea		;PRM1-793
 SWI     XOS_SpriteOp			;R2=size, R5=used
 SUB     R3, R2, R5			;R3=free space
 SUBS    R5, R6, R3			;is there enough space?
 BLE     MA_exit			;YES, exit

 MOV     R0, #3				;sprite area
B       MA_read_and_changeDynamicArea


.Entry_FontFree
 STMFD   R13!, {R1-R6, R14}

 MOV     R2, #1024			;max memory we can remap
 BL      MA_readUnsigned		;R2=value
 MOV     R5, R2				;preserve R2

 MOV     R0, #0				;PRM3-426
 SWI     XFont_CacheAddr		;R2=size, R3=used
 SUB     R1, R2, R3			;R1=free space
 SUBS    R0, R1, R5			;is there enough space?
 BPL     MA_exit			;YES, exit

 MOV     R0, #4				;font area
B       MA_read_and_changeDynamicArea


.Entry_ADFWimpSlot
 STMFD   R13!, {R1-R6, R14}

 MOV     R3, R1				;R3=number of parameters

 MOV     R6, R0
 MVN     R0, #0
 MVN     R1, #0
 SWI     XWimp_SlotSize			;read current size
 MOV     R5, R0				;R5=current allocation

 MOV     R2, #65536			;max value supported
 MOV     R0, R6
 BL      MA_readUnsigned
 MOV     R4, R2				;R4=<min>

 CMP     R3, #2				;min and max values?
 BLO     _no_max

 MOV     R0, R6
 MOV     R2, #65536			;max value supported
 BL      MA_readUnsigned		;R2=value

 MOV     R0, R2
 MVN     R1, #0
 SWI     XWimp_SlotSize			;try for max
 LDMVCFD R13!, {R1-R6, PC}		;exit if it worked
 SUBS    R0, R0, R0			;clear V

 ._no_max				;R4=value to use
 CMP     R5, R4				;do we need to change the allocation?
 LDMHSFD R13!, {R1-R6, PC}		;NO

 MOV     R0, R4
 MVN     R1, #0
 SWI     XWimp_SlotSize			;try for <min>
LDMFD   R13!, {R1-R6, PC}


;Entry:
; R0 - area to change
; R5 - size to increase by

.MA_read_and_changeDynamicArea
 MOV     R3, R0				;preserve R0
 SWI     XOS_ReadDynamicArea		;PRM1-396
 BVS     MA_exit			;exit on error
 ADD     R5, R5, R1			;R5=current + free requested
 B       MA_changeDynamicArea_P1

;Entry:
; R0 - area to change
; R2 - new size

.MA_changeDynamicArea
 MOV     R5, R2				;preserve R2
 MOV     R3, R0				;preserve R0
 SWI     XOS_ReadDynamicArea		;read current size PRM1-396
 BVS     MA_exit			;exit on error
					;R1=current size
;Entry:
; R1 - current size
; R3 - area to change
; R5 - new size

 .MA_changeDynamicArea_P1
 SUBS    R1, R5, R1			;do we need to change the size?
 LDMEQFD R13!, {R1-R6, PC}		;NO, what we require
 MOV     R0, R3				;preserve R0
 SWI     XOS_ChangeDynamicArea		;  |+ PRM1-384

 MOV     R0, R3				;preserve R0
 SWI     XOS_ReadDynamicArea		;did the command succeed? PRM1-396
 CMP     R1, R5				;clear V, is there >= what we need?
 MOVLO   R0, #1<<31			;NO
 ADDLOS  R0, R0, R0			;|+ set V

.MA_exit				;R6=command line
 LDRVSB  R0, [R6], #1			;error? YES, read the next char
 SUBS    R1, R1, R1			;clear V

 TEQ     R0, #32			;R0=32 if an error occured
 MOVEQ   R0, R6				;R6=ptr to error command
 SWIEQ   XOS_CLI			;PRM1-961
LDMFD   R13!, {R1-R6, PC}


;Entry:
; R0 - command line
; R2 - max value allowed
;
;Exit:
; R2 - value * 1024
; R6 - next value

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

 MOV     R1, R0				;command line
 MOV     R0, #10 + (1<<29)		;assume base 10 with restricted range
 SWI     XOS_ReadUnsigned		;read the value PRM1-460
 LDRVS   R14, [R13], #4			;exit on error
 LDMVSFD R13!, {R1-R6, PC}

 ._L1
   LDRB    R0, [R1], #1			;read the next char
   TEQ     R0, #0			;terminator
   TEQNE   R0, #10			;CR?
   TEQNE   R0, #13			;LF?
   TEQNE   R0, #32			;space?
 BNE     _L1

 SUB     R6, R1, #1			;preserve end of command line
 MOV     R2, R2, LSL #10		;R2 = size in bytes
LDR     PC, [R13], #4
