; Copyright 2011 Castle Technology 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.
;

; Assembler gubbins to allow a local copy of the heap manager to be used by
; the testbed application


        GET     Hdr:ListOpts
        GET     Hdr:Macros
        GET     Hdr:System
        GET     Hdr:CPU.Arch
        GET     Hdr:Machine.<Machine>
        GET     Hdr:Heap
        GET     Hdr:Proc
        GET     Hdr:FSNumbers
        GET     Hdr:HighFSI
        GET     Hdr:NewErrors
        GET     Hdr:MsgTrans

; Disable internationalisation
              GBLL International
International SETL {FALSE}

; Indicate we're compiling the testbed
            GBLL HeapTestbed
HeapTestbed SETL {TRUE}

; Heap debugging disabled for now
           GBLL DebugHeaps
DebugHeaps SETL {FALSE}

; Fake zero page workspace

                    ^ 0
IRQsema             # 4
HeapSavedReg_R0     # 4
HeapSavedReg_R1     # 4
HeapSavedReg_R2     # 4
HeapSavedReg_R3     # 4
HeapSavedReg_R4     # 4
HeapSavedReg_R13    # 4
HeapReturnedReg_R0  # 4
HeapReturnedReg_R1  # 4
HeapReturnedReg_R2  # 4
HeapReturnedReg_R3  # 4
HeapReturnedReg_R4  # 4
HeapReturnedReg_R13 # 4
HeapReturnedReg_PSR # 4
ZeroPageSize        * @

; Macros and other bits

        MACRO
        assert  $condition
 [ :LNOT: ($condition)
 ! 1,"Assert failed: $condition"
 ]
        MEND

SVC2632 * SVC32_mode


        AREA    testbeddata, DATA

ZeroPage   % ZeroPageSize

HeapBackgroundError  % 256

        AREA    testbedcode, CODE

; C interface

          EXPORT  CallHeap
CallHeap  ROUT
          ; r0 = _kernel_swi_regs ptr for input/output
          ; Returns error ptr in r0
          Push    "r0,r4-r11,lr"
          LDMIA   r0,{r0-r9}
          SWI     OS_EnterOS ; Call in SVC mode?
          BL      DoCallXOSHeap
          MOVVS   r10,r0
          MOVVC   r10,#0
          SWI     OS_LeaveOS
          Pull    "r11"
          STMIA   r11,{r0-r9}
          MOV     r0,r10
          Pull    "r4-r11,pc"

; Assembler bits for use by heap code

DoCallXOSHeap
          ; Fake an XOS_Heap SWI
          ; Preserve r10-r12 and enter with PSR in lr
          Push    "r10-r12,lr"
          MRS     lr,CPSR
          B       HeapEntry

; Main heap manager code

        GET      ^.^.s.HeapMan

        END