; 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.
;
;How to save and restore the flags in a macro:
;       MACRO
;       Push    ... ;whatever
;       Push    PC ;save flags (must be pushed last)
;       ... ;macro body
;       Pull    LR ;contains flags
;       TEQP    PC, LR ;restore them
;       Pull    ... ;as above
;       MEND
;---------------------------------------
        MACRO
$lab    TraceError $dbg
$lab
        [       debug :LAND: debug$dbg
        BVC     %FT00
        Push    R0-R1

        TraceK  $dbg, "ERROR: "
        MOV     R1, R0
; R1 -> error block

        LDR     R0, [R1]
; R0 = error number
        TraceX  $dbg, R0

        TraceK  $dbg, ", "
        ADD     R0, R1, #4
; R0 -> error message
        TraceS  $dbg, R0

        TraceNL $dbg
        Pull    R0-R1
00
        ]
        MEND
;---------------------------------------
        MACRO
$lab    Trace   $dbg, $string, $type, $reg
$lab
        TraceK  $dbg, "$string"
        Trace$type $dbg, $reg
        TraceNL $dbg
        MEND
;---------------------------------------
        MACRO
$lab    TraceC  $dbg, $r
$lab
        [       debug :LAND: debug$dbg
        Push    "R0, LR"
        [       :LNOT: ("$r" = "SP")
        MOV     R0, $r
        |
        ADD     R0, SP, #8 ;trace SP as it was on entry
        ]
        TraceK  $dbg, "'"
        BL      trace_char
        TraceK  $dbg, "'"
        Pull    "R0, LR"
        ]
        MEND
;---------------------------------------
        MACRO
$lab    TracePC $dbg, $r
$lab
        [       debug :LAND: debug$dbg
        Push    "R0, LR"
        [       :LNOT: ("$r" = "SP")
        MOV     R0, $r
        |
        ADD     R0, SP, #8 ;trace SP as it was on entry
        ]
        LDRB    R0, [R0]
        BL      trace_char
        Pull    "R0, LR"
        ]
        MEND
;---------------------------------------
        MACRO
$lab    TraceS  $dbg, $r
$lab
        [       debug :LAND: debug$dbg
        Push    "R0, LR"
        [       :LNOT: ("$r" = "SP")
        MOV     R0, $r
        |
        ADD     R0, SP, #8 ;trace SP as it was on entry
        ]
        TraceK  $dbg, "'"
        BL      trace_string
        TraceK  $dbg, "'"
        Pull    "R0, LR"
        ]
        MEND
;---------------------------------------
        MACRO
$lab    TraceK  $dbg, $s
$lab
        [       debug :LAND: debug$dbg
        Push    "R0, LR"
        ADR     R0, %FT00
        BL      trace_string
        B       %FT01
00
        =       "$s", 0
        ALIGN
01
        Pull    "R0, LR"
        ]
        MEND
;---------------------------------------
        MACRO
$lab    TraceD  $dbg, $r
$lab
        [       debug :LAND: debug$dbg
        Push    "R0, LR"
        [       :LNOT: ("$r" = "SP")
        MOV     R0, $r
        |
        ADD     R0, SP, #8 ;trace SP as it was on entry
        ]
        BL      trace_decimal
        Pull    "R0, LR"
        ]
        MEND
;---------------------------------------
        MACRO
$lab    TraceX  $dbg, $r
$lab
        [       debug :LAND: debug$dbg
        Push    "R0, LR"
        [       :LNOT: ("$r" = "SP")
        MOV     R0, $r
        |
        ADD     R0, SP, #8 ;trace SP as it was on entry
        ]
        TraceK  $dbg, "&"
        BL      trace_hex
        Pull    "R0, LR"
        ]
        MEND
;---------------------------------------
        MACRO
$lab    TraceNL $dbg
$lab
        [       debug :LAND: debug$dbg
        Push    LR
        BL      trace_nl
        Pull    LR
        ]
        MEND
;---------------------------------------
        MACRO
$lab    TraceL  $dbg
$lab
        [       debug :LAND: debug$dbg
        Push    "R0, LR"
        ADR     R0, %FT00
        BL      trace_string
        BL      trace_nl
        B       %FT01
00
        =       "$lab", 0
        ALIGN
01
        Pull    "R0, LR"
        ]
        MEND
;---------------------------------------
        MACRO
$lab    TEST_ARROW $c
$lab
        ;Z := $c is an arrow
        TEQ     $c, #:CHR: &88 ;left
        TEQNE   $c, #:CHR: &89 ;right
        TEQNE   $c, #:CHR: &8A ;down
        TEQNE   $c, #:CHR: &8B ;up
      [ :LNOT: Euro
        TEQNE   $c, #:CHR: &80 ;tick
      ]
        TEQNE   $c, #:CHR: &84 ;cross
        MEND
;---------------------------------------
        MACRO
$lab    TEST_TERM $c
$lab
        ;Z := $c is a terminator
        TEQ     $c, #:CHR: 0
        TEQNE   $c, #:CHR: 10
        TEQNE   $c, #:CHR: 13
        MEND

        MACRO
$lab    ReportInformation       $info
        Push    "R0-R5,lr"
        ADR     R0,$info
        LDR     R1,=257         ; new bits + ok
        MOV     R2,#0
        MOV     R3,#0
        MOV     R4,#0
        MOV     R5,#0
        SWI     XWimp_ReportError
        Pull    "R0-R5,lr"
        MEND

;----------------------------------------
; Option introduces and sets an option neatly
;
	MACRO
	Option	$name,$value
      [ :LNOT::DEF:$name
	GBLL	$name
      ]
$name	SETL	$value
	MEND

;----------------------------------------
; OptionD introduces and sets an debug option neatly
;
	MACRO
	OptionD	$name,$value
	Option	debug$name,$value:LAND:debug
	MEND


        END