; Copyright 2016 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.
;

; DA handler for AMBControl DA nodes

AMBDAHandler ROUT
        CMP     R0, #DAHandler_ResizePMP
        BNE     UnknownHandlerError
        ; In:
        ;  R0 = Reason code
        ;  R1 = change amount (pages)
        ;  R2 = DA number (ignore)
        ;  R12 -> AMBNode
        ; Out:
        ;  All regs preserved
        Entry   "R1,R2,R12"
      [ AMB_Debug
        LDR     R2, [R12, #AMBNode_handle]
        DebugReg R2, "AMBDAHandler "
        DebugReg R1
      ]
        MOV     R2, R12
        CMP     R1, #0
        LDR     LR, [R2, #AMBNode_DANode+DANode_PMPSize]
        LDR     R12, =ZeroPage+AMBControl_ws
        ADD     R1, R1, LR
        LDR     R12, [R12]
        ADR     R14, %FT10
        BGT     growpages
        BLT     shrinkpages
10
      [ AMB_Debug
        LDR     R2, [R2, #AMBNode_DANode+DANode_PMPSize]
        DebugReg R2, "<AMBDAHandler new size "
      ]
        EXIT


    END