; 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.
;
; > DDVMacros

; ********************
; ***  Change List ***
; ********************

; 01-Feb-91 DDV Created with SETD
; 05-Mar-91 DDV Added CheckSpaceOnStack
; 18-Mar-91 DDV Added CheckSpaceOnStackReg
; 20-Mar-91 DDV Added ToMODE / RestoreMODE
; 15-Oct-91 DDV Added 'addl' + new condition code flags
; 16-Oct-91 DDV Added 'Branch' + reordered the list so alphabetical

; label addl                    <reg>,<reg2>,<value>,<cc>
; label Branch                  <base>,<offset>
; label CheckSpaceOnStack       <space>,<routine>,<temp>
; label CheckSpaceonStackReg    <reg>,<routine>,<temp>
; label RestoreMODE             <save PSR>
; label SETD                    <true/fale>
; label ToMODE                  <mode>,<save PSR>,<temp>,<bic>,<orr>

;..............................................................................

        MACRO
$l      SETD    $s
        GBLL    debug$l
debug$l SETL    $s :LAND: debug
        MEND

;..............................................................................

        MACRO
$l      ToMODE  $mode, $register, $temp, $bic, $orr
$l      MOV     $register,PC            ;Temp copy
        BIC     $temp,$register,#2_11   ;Clear out mode bits
      [ $mode<>0
        ORR     $temp,$temp,#$mode      ;ORR in new mode if <>0
      ]
      [ $bic<>""
        BIC     $temp,$temp,#$bic       ;BIC if required
      ]
      [ $orr<>""
        ORR     $temp,$temp,#$orr       ;ORR if required
      ]
        TEQP    $temp,#0                ;Enforce change
        NOP                             ;..account for pipe-lining
        MEND

;..............................................................................

        MACRO
$l      RestoreMODE $register
$l      TEQP    PC,$register            ;Switch back to the original mode
        NOP                             ;..account for pipeling
        MEND

;..............................................................................

        MACRO
$l      CheckSpaceOnStack $v,$d,$t
$l      MOV     $t,SP,LSR #15           ;Stack base on 32K boundary
        SUB     $t,SP,$t,LSL #15        ;Get the amount of space left in the stack
        CMP     $t,#$v                  ;Is there enough?
        BMI     $d                      ;If not then branch to failure routine
        MEND

;..............................................................................

        MACRO
$l      CheckSpaceOnStackReg $r,$d,$t
$l      MOV     $t,SP,LSR #15           ;Stack base on 32K boundary
        SUB     $t,SP,$t,LSL #15        ;Get the amount of space left in the stack
        CMP     $t,$r                   ;Is there enough?
        BMI     $d                      ;If not then branch to failure routine
        MEND

;..............................................................................

        MACRO
$l      addl    $reg,$reg2,$var,$cc
        LCLA    count
        LCLA    varcopy
        LCLA    value
        LCLA    doneone
varcopy SETA    $var
count   SETA    0
doneone SETA    0
        WHILE   varcopy >0
      [ varcopy :AND:3 =0
varcopy SETA    varcopy :SHR: 2
count   SETA    count +2
      |
value   SETA    (varcopy :AND: 255) :SHL: (count)
      [ &$value>0
       [ doneone=1
        ADD$cc  $reg,$reg,#&$value
       |
        ADD$cc  $reg,$reg2,#&$value
doneone SETA    1
       ]
      ]
varcopy SETA    varcopy :SHR: 8
count   SETA    count +8
      ]
        WEND
        MEND

;..............................................................................

        MACRO
$l      Branch  $base,$offset
$l      MOV     LR,PC
        LDR     PC,[$base,#$offset]
        MEND

;..............................................................................

        END

