; macros.asm

;  Copyright 2019-2020 Robin Verhagen-Guest
;
; 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.

VersionPrefix           equ "1."

include "version.asm", 1                                ; Auto-generated by ..\build\cspect.bat or builddot.bat

Border                  macro(Colour)
                        if Colour=0
                          xor a
                        else
                          ld a, Colour
                        endif
                        out (ULA_PORT), a
                        if Colour=0
                          xor a
                        else
                          ld a, Colour*8
                        endif
                        ld (23624), a
mend

Freeze                  macro(Colour1, Colour2)
Loop:                   Border(Colour1)
                        Border(Colour2)
                        jr Loop
mend

CSBreak                 macro()                         ; Intended for CSpect debugging
                        push bc                         ; enabled when the -brk switch is supplied
                        noflow                          ; Mitigate the worst effect of running on real hardware
                        db $DD, $01                     ; On real Z80 or Z80N, this does NOP:LD BC, NNNN
                        nop                             ; so we set safe values for NN
                        nop                             ; and NN,
                        pop bc                          ; then we restore the value of bc we saved earlier
mend

CSExit                  macro()                         ; Intended for CSpect debugging
                        noflow                          ; enabled when the -exit switch is supplied
                        db $DD, $00                     ; This executes as NOP:NOP on real hardware
mend

CpHL                    macro(Register)
                        or a
                        sbc hl, Register
                        add hl, Register
mend

PrintMsg                macro(Address)
                        ld hl, Address
                        call PrintRst16
mend

PrintBuffer             macro(StartAddr, LengthAddr)
                        ld hl, (StartAddr)
                        ld bc, (LengthAddr)
                        call PrintBufferProc
mend

PrintMsgRetToBASIC      macro(MsgAddr)
                        ld hl, MsgAddr
                        call PrintRst16
                        jp Return.ToBasic
mend

ErrorIfCarry            macro(ErrAddr)
                        jp nc, Continue
                        ld hl, ErrAddr
                        jp CheckESPTimeout.HandleError
Continue:
mend

ErrorIfNonZero          macro(ErrAddr)
                        jp z, Continue
                        ld hl, ErrAddr
                        jp CheckESPTimeout.HandleError
Continue:
mend

WriteString             macro(StringAddr, StringLen)
                        ld hl, StringAddr
                        ld bc, StringLen
                        ldir
mend

WriteBuffer             macro(CommandAddrAddr, CommandLenAddr)
                        ld hl, (CommandAddrAddr)
                        ld bc, (CommandLenAddr)
                        ldir
mend

AddHL                   macro (WordValue)               ; Next-only opcode
                        noflow
                        db $ED, $34
                        dw WordValue
mend

MirrorA                 macro()
                        noflow
                        db $ED, $24
mend

FillLDIR                macro(SourceAddr, Size, Value)
                        ld a, Value
                        ld hl, SourceAddr
                        ld (hl), a
                        ld de, SourceAddr+1
                        ld bc, Size-1
                        ldir
mend

NextRegRead             macro(Register)
                        ld bc, Port.NextReg             ; Port.NextReg = $243B
                        ld a, Register
                        call NextRegReadProc
mend

DecodeDecimal           macro(Buffer, DigitCount)
                        ld hl, Buffer
                        dec hl
                        ld (DecodeDecimalProc.DecimalBuffer), hl
                        ld b, DigitCount
                        call DecodeDecimalProc
mend

Rst8                    macro(Command)
                        rst $08
                        noflow
                        db Command
mend

CopyLDIR                macro(SourceAddr, DestAddr, Size)
                        ld hl, SourceAddr
                        ld de, DestAddr
                        ld bc, Size
                        ldir
mend

WaitFrames              macro(FrameCount)
                        ld b, FrameCount
                        call WaitFramesProc
mend

