; 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.
;
;
; Copyright (C) Acorn Computers Ltd., 1988.
;
       AREA    |RTSK$$Data|, READONLY
        IMPORT  |C$$code$$Base|
        IMPORT  |C$$code$$Limit|

rtskBase
        &       EndRTSK-.
        &       |C$$code$$Base|
        &       |C$$code$$Limit|
        &       CLanguageString
        &       Initialise
 [ SharedLibrary
        &       0       ; compatibility - sharedlibrary here means stub
 |
        &       Finalise
 ]
        &       TrapHandler
        &       UncaughtTrapHandler
        &       EventHandler
        &       UnhandledEventHandler
EndRTSK

        AREA    |C$$code|, CODE, READONLY
        IMPORT  main, WEAK
        EXPORT  |__main|

CLanguageString
        =       "C",0
        ALIGN

; __main is a dummy definition to statisfy references generated by the
; compiler. It should never be called.
|__main|

Initialise
; argument for _armsys_lib_init:
;                   top of stack,
;                   start of code area,
;                   end of code area,
 [ SharedLibrary
; Here, SharedLibrary means stub. In this case, we have to enable Wimp-Slot
; extension by _kernel_alloc, as for compatibility with old binaries it has
; to be off by default.
;
; This byte has moved in version 7 of the library. In fact, in version 7
; it is on by default, so doesn't need to be poked. So we still work with
; version 6, we poke it directly, using its old address. This position
; is in the middle of registerDump on newer libraries, so it is harmless.
        LDR     a1, =StaticData + 0x115 ; was |_kernel_kallocExtendsWS|
        LDR     a2, [sl, #SL_Client_Offset]
        ADD     a1, a1, a2
        MOV     a2, #1
        STRB    a2, [a1]                ; enable new _kernel_alloc behaviour
 ]
        FunctionEntry
        ADD     a1, sp, #4
        LDR     a2, =rtskBase
        LDMIB   a2, {a2, a3}
        BL      |_clib_initialise|
        LDR     a1, =main
        CMP     a1, #0
        ADRNE   a1, RunSubMain
        Return

RunSubMain
; args for _main(): command line
;                   address of main
        FunctionEntry "", makeframe
        BL      |_kernel_command_string|
        LDR     a2, =main
        BL      |_main|
; Doesn't return, but just in case...
        Return  "", fpbased

        END