; Copyright 1996 Acorn Computers Ltd
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
;     http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.
;
; > s.Tail

;-----------------------------------------------------------------------------------------
; cmpstr.
;
; compare the strings pointed to by r0 and r1 (case insensitive).
cmpstr
       Push     "r0-r2,LR"

01
       LDRB     r14,[r0],#1
       BIC      r14,r14,#&20
       LDRB     r2,[r1],#1
       BIC      r2,r2,#&20
       CMP      r14,r2
       Pull     "r0-r2,PC",NE
       CMP      r14,#0
       BNE      %BT01

       Pull     "r0-r2,PC"

; [ :LNOT: usefp
;;-----------------------------------------------------------------------------------------
;; divrem.
;;
;; divides r9 by r10. result in r9, reminder in r10.
;divrem
;       Push    "LR"

;       Debuga  xx,"Div ",r9,r10

;       CMP     r10,#0
;       MOVEQ   r9,#0
;       Pull   "PC",EQ
;
;       MOV     r14,#0
;01
;       SUBS    r9,r9,r10
;       BCC     %FT02
;       ADD     r14,r14,#1
;       B       %BT01
;02
;       ADD     r10,r9,r10
;       MOV     r9,r14

;       Debug   xx," = ",r9,r10

;       Pull    "PC"
; ]

 [ {FALSE}      ; no longer used
;-----------------------------------------------------------------------------------------
; round.
;
; Rounds r0 to the nearest whole multiple of r1.
round
        Push    "LR"

        ADD     r0,r0,r1,LSR #1     ; Add half of R1
        MOV     r14,#0
01
        SUBS    r0,r0,r1
        BMI     %FT02
        ADD     r14,r14,#1
        B       %BT01
02
        MUL     r0,r1,r14

        Pull    "PC"
 ]
; ----------------------------------------------------------------------------------------------------------------------
;       Release linked lists of files/icons. Note - may be in USER mode or SVC mode - can't use USER stack,
;       though, as it may not be okay. Hence not allowed to use the stack at all.
;    R2 -> pointer to start of list to kill (active_ptr or buffered_ptr)
;        DANGER - CORRUPTS R0-R3
free_list
        MOV     R3, LR
        LDR     r1, [r2,#next_ptr]
        MOV     r0, #0
        STR     r0, [r2,#next_ptr]
; Get next file in the list
01
        MOV     r0, #ModHandReason_Free
        SUBS    r2, r1, #0
        MOVLE   PC, R3
; Free the workspace
        LDR     r1, [r2,#next_ptr]
        SWI     XOS_Module
        B       %BT01

; ----------------------------------------------------------------------------------------------------------------------

copy_r0r4r3_space

        Push    "r3,r4,LR"
01
        LDRB    r14, [r4], #1
        CMP     r14,#32
        MOVLE   R14,#0
        STRB    r14, [r0], #1
        SUBS    r3 , r3,#1
        CMPNE   r14, #0
        BNE     %BT01
        CMP     r3,#0
        STREQB  r3,[r0],#1
        SUB     r0, r0, #1

        Pull    "r3,r4,PC"

; ----------------------------------------------------------------------------------------------------------------------

; MessageTrans routines

LookupError
        Push    "r1-r7,lr"
        BL      open_messages
        Pull    "r1-r7,pc",VS
        MOV     r4, r1
        ADR     r1, message_fblock
        MOV     r2, #0
        MOV     r5, #0
        MOV     r6, #0
        MOV     r7, #0
        SWI     XMessageTrans_ErrorLookup
        Pull    "r1-r7,pc"

message_filename
        DCB     "Free:Messages",0
        ALIGN

open_messages
        Push    "r0-r2,lr"
        LDR     r0, message_fopen
        CMP     r0, #0
        Pull    "r0-r2,pc",NE
        ADR     r0, message_fblock
        ADR     r1, message_filename
        MOV     r2, #0
        SWI     XMessageTrans_OpenFile
        STRVS   r0, [sp]
        STRVC   r0, message_fopen
        Pull    "r0-r2,pc"

Lookup_InPlace  ROUT
        Push    "r1-r7,LR"
        BL      open_messages
        Pull    "r1-r7,PC",VS
        MOV     r1,r0
        ADR     r0,message_fblock
        MOV     r2,#0
        MOV     R4,#0
        MOV     R5,#0
        MOV     R6,#0
        MOV     R7,#0
        SWI     XMessageTrans_Lookup
        MOVVC   r0,r2
        Pull    "r1-r7,PC"

Lookup  ROUT                ; r0 -> token, r2 -> buffer , r3 = buf length ,r4 -> arg
        Push    "r1-r7,LR"
        BL      open_messages
        Pull    "r1-r7,PC",VS
        MOV     r1,r0
        ADR     r0,message_fblock
        MOV     R5,#0
        MOV     R6,#0
        MOV     R7,#0
        SWI     XMessageTrans_Lookup
        MOVVC   r0,r2
        STRVC   r3,[sp,#2*4]
        Pull    "r1-r7,PC"

; ----------------------------------------------------------------------------------------------------------------------



; Neil's debugging routines

      [ debug
        InsertNDRDebugRoutines
      ]

        END