; 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.
;
; > Sources.Main

; *********************************
; ***    C h a n g e   L i s t  ***
; *********************************

; Date       Description
; ----       -----------
; 19-Feb-90     0.01    File created
; 11-Jul-91     0.02    PJC: Destroys Help$Dir on Service_Reset
;

        AREA    |Help$$Code|, CODE, READONLY, PIC

Module_BaseAddr

        GET     AppName.s         ; read ApplicationName

        GET     Hdr:ListOpts
        GET     Hdr:Macros
        GET     Hdr:System
        GET     Hdr:ModHand
        GET     Hdr:Services
        GET     Hdr:Proc
        GET     Hdr:ResourceFS
        GET     VersionASM

; **************** Module code starts here **********************

        ASSERT  (.-Module_BaseAddr) = 0

        DCD     0 ; App_Start    - Module_BaseAddr
        DCD     App_Init     - Module_BaseAddr
        DCD     App_Die      - Module_BaseAddr
        DCD     App_Service  - Module_BaseAddr
        DCD     App_Title    - Module_BaseAddr
        DCD     App_HelpStr  - Module_BaseAddr
        DCD     0
      [ :LNOT: No32bitCode
        DCD     0
        DCD     0
        DCD     0
        DCD     0
        DCD     0
        DCD     App_ModFlag  - Module_BaseAddr
      ]

      [ :DEF: standalone
App_Title       DCB     "!$ApplicationName:CC:Resources",0
      |
App_Title       DCB     "!$ApplicationName", 0
      ]
App_HelpStr     DCB   "!$ApplicationName", 9, 9, "$Module_MajorVersion ($Module_Date)", 0
                ALIGN

; *****************************************************************************
;
; Module flags.  Bit 0 set means module is 32-bit aware and safe
;
      [ :LNOT: No32bitCode
App_ModFlag     DCD     ModuleFlag_32bit
      ]

; *****************************************************************************
;
;       App_Init - Initialisation entry
;

App_Init Entry "r0"

        ADR     R0, resourcefsfiles
        SWI     XResourceFS_RegisterFiles   ; ignore errors (starts on Service_ResourceFSStarting)
        CLRV
        EXIT

; *****************************************************************************
;
;       App_Die - Die entry
;

App_Die Entry "r0"

        ADR     R0, resourcefsfiles
        SWI     XResourceFS_DeregisterFiles
        CLRV

        EXIT                    ; ignore errors (ResourceFS might be dead)

; *****************************************************************************
;
;       App_Service - Main entry point for services
;

;Ursula format
        ASSERT  Service_Reset < Service_ResourceFSStarting
App_ServTab
        DCD     0                                   ;flags
        DCD     App_UService - Module_BaseAddr      ;handler
        DCD     Service_Reset
        DCD     Service_ResourceFSStarting
        DCD     0                                   ;terminator
        DCD     App_ServTab - Module_BaseAddr       ;anchor
App_Service ROUT
        MOV     r0, r0                              ;magic instruction
        TEQ     R1, #Service_Reset
        TEQNE   R1, #Service_ResourceFSStarting
        MOVNE   PC, LR

App_UService
        TEQ     R1, #Service_Reset
        BEQ     svc_clearsysvar                     ; on a reset, clear the var

; In    R2 -> address inside ResourceFS module to call
;       R3 = workspace pointer for module
; Out   R2 called with R0 -> files, R3 -> workspace

svc_resourcefsstarting Entry "R0-R3"

        ADR     R0, resourcefsfiles
        MOV     LR, PC                          ; get return address
        MOV     PC, R2                          ; R2 = address to call

        EXIT

resourcefsfiles
        GET     ResFiles.s
        DCD     0

svc_clearsysvar Entry "R0-R4"

        ADR     R0, sysvarname
        MOV     R1, #0
        MVN     R2, #0                          ; destroy the variable!
        MOV     R3, #0
        MOV     R4, #0
        SWI     XOS_SetVarVal

        EXIT

sysvarname
        DCB     "Help$Dir",0

        END
