; ***********************************
; *                                 *
; *      Z80 MACRO DEFINITIONS      *
; *      =====================      *
; *                                 *
; ***********************************

;
;Constants
;
DEFCONST        (B,C,D,E,H,L,GAP,A,BC,DE,HL,SP,
                AF,IX,IY,I,R,NZ,Z,NC,PO,PE,P,
                M,OFF,ON,ADD,ADC,SUB,SBC,AND,OR,
                XOR,CP,RLC,RL,RRC,RR,SLA,SRA,SRL,
                SLI,RLCA,RRCA,RLA,RRA,BIT,RES,SET,
                INC,DEC,DAA,CPL,SCF,CCF).

;
;Register SETS
;
DEFSET  _SS     = (B,C,D,E,H,L,GAP,A).
DEFSET  _SDD    = (BC,DE,HL,SP).
DEFSET  _SQQ    = (BC,DE,HL,AF).
DEFSET  _SPP    = (BC,DE,IX,SP).
DEFSET  _SRR    = (BC,DE,IY,SP).
DEFSET  _SII    = (HL,IX,IY).
DEFSET  _SXX    = (IX,IY).
DEFSET  _SBD    = (BC,DE).
DEFSET  _SIR    = (I,R).

;
;Condition SETS
;
DEFSET  _SCE    = (NZ,Z,NC,C,PO,PE,P,M).
DEFSET  _SCC    = (NZ,Z,NC,C).
DEFSET  _SOO    = (OFF,ON).

;
;Instruction SETS
;
DEFSET  _SAR    = (ADD,ADC,SUB,SBC,AND,XOR,OR,CP).
DEFSET  _SROT   = (RLC,RRC,RL,RR,SLA,SRA,SLI,SRL).
DEFSET  _SROTA  = (RLCA,RRCA,RLA,RRA).
DEFSET  _SBIT   = (BIT,RES,SET).
DEFSET  _SID    = (INC,DEC).
DEFSET  _SGC    = (DAA,CPL,SCF,CCF).
DEFSET  _SADS   = (ADD,ADC,GAP,SBC).

;
;Number SETS
;
DEFSET  _SNNNN  = 0 TO 0FFFFH.
DEFSET  _SNN    = -256 TO 255.
DEFSET  _SDISP  = -128 TO 127.
DEFSET  _SZTO2  = 0 TO 2.
DEFSET  _SZTO7  = 0 TO 7.


;
;
;Pseudo op macro definitions

DEFMAC ("ORG*",_SNNNN)
        $: = #0
        $2: = $2 & 1111101111111111B
END.

DEFMAC  ("LOAD*",_SNNNN)
        $1: = #0
        $2: = $2 | [$2 & 0000001000000000B]<<1
END.

DEFMAC  ("LIST*",_SOO)
       	$2: = [$2 & 1111111111110111B] + #0<<3
END.

DEFMAC  ("DS*",_SNNNN)

        ORG $+#0
        LOAD $1+#0
END.

DEFMAC  ("DW*",_SNNNN)
        DB #0 & 0FFH
        DB #0 >> 8
END.

DEFMAC  ("DD*",_SNNNN)
        IF [#0<-128]&[#0>127]
        DB 256
        ELSE
        DB #0
        END
END.

;
;
;Standard Z80 instruction set
;============================
;
;1) 8 Bit load group
;===================

DEFMAC  ("LD*,*",_SS,_SS)

        DB 40H + #0<<3 + #1
END.

DEFMAC  (/"LD*,*",_SS,_SNN)

        DB 06H + #0<<3, #1
END.

DEFMAC  ("LD*,(*)",_SS,HL)

        DB 46H + #0<<3
END.

DEFMAC  ("LD(*),*",HL,_SS)

        DB 70H + #1
END.

DEFMAC  (/"LD(*),*",HL,_SNN)

        DB 36H, #1
END.

DEFMAC  ("LD*,(**)",_SS,_SXX,_SDISP)

        DB 0DDH + #1<<5, 46H + #0<<3, #2
END.

DEFMAC  ("LD(**),*",_SXX,_SDISP,_SS)

        DB 0DDH + #0<<5, 70H + #2, #1
END.

DEFMAC  (/"LD(**),*",_SXX,_SDISP,_SNN)

        DB 0DDH + #0<<5, 36H, #1, #2
END.

DEFMAC  (/"LD*,(*)",A,_SBD)

        DB 0AH + #1<<4
END.

DEFMAC  (/"LD(*),*",_SBD,A)

        DB 02H + #0<<4
END.

DEFMAC  (/"LD*,*",A,_SIR)

        DB 0EDH, 57H + #1<<3
END.

DEFMAC  (/"LD*,*",_SIR,A)

        DB 0EDH, 047H + #0<<3
END.

DEFMAC  (/"LD*,(*)",A,_SNNNN)

        DB 3AH
        DW #1
END.

DEFMAC  (/"LD(*),*",_SNNNN,A)

        DB 32H
        DW #0
END.

DEFMAC  ("LDI")

        DB 0EDH,0A0H
END.

DEFMAC  ("LDIR")

        DB 0EDH,0B0H
END.

DEFMAC  ("LDD")

        DB 0EDH,0A8H
END.

DEFMAC  ("LDDR")

        DB 0EDH,0B8H
END.


;
;
; 2) 16 Bit load group
; ====================
;

DEFMAC  (/"LD*,*",_SDD,_SNNNN)

        DB 1 + #0<<4
        DW #1
END.

DEFMAC  (/"LD*,*",_SXX,_SNNNN)

        DB 0DDH + #0<<5, 21H
        DW #1
END.

DEFMAC  (/"LD*,(*)",_SDD,_SNNNN)

        IF #0=2         ;HL is a special case
        DB 2AH
        ELSE
        DB 0EDH,4BH + #0<<4
        END
        DW #1
END.

DEFMAC  (/"LD(*),*",_SNNNN,_SDD)

        IF #1=2
        DB 22H
        ELSE
        DB 0EDH, 43H + #1<<4
        END
        DW #0
END.

DEFMAC  (/"LD*,(*)",_SXX,_SNNNN)

        DB 0DDH + #0<<5, 2AH
        DW #1
END.

DEFMAC  (/"LD(*),*",_SNNNN,_SXX)

        DB 0DDH + #1<<5, 22H
        DW #0
END.

DEFMAC  (/"LD*,*",SP,_SII)

        IF #1
        DB 0BDH + #1<<5
        END
        DB 0F9H
END.

DEFMAC  ("PUSH*",_SQQ)

        DB 0C5H + #0<<4
END.

DEFMAC  ("POP*",_SQQ)

        DB 0C1H + #0<<4
END.

DEFMAC  (/"PUSH*",_SXX)

        DB 0DDH + #0<<5, 0E5H
END.

DEFMAC  (/"POP*",_SXX)

        DB 0DDH + #0<<5, 0E1H
END.

DEFMAC  ("EX*,*",DE,HL)

        DB 0EBH
END.

DEFMAC  (/"EX*,*",AF,AF)

        DB 08H
END.

DEFMAC  ("EX(*),*",SP,_SII)

        IF #1
        DB 0BDH + #1<<5
        END
        DB 0E3H
END.

DEFMAC  ("EXX")

        DB 0D9H
END.


;
;
; 3) 8 Bit arithmetic group
; =========================
;

DEFMAC  ("**",_SAR,_SS)

        DB 80H + #0<<3 +#1
END.

DEFMAC  ("**,*",_SADS,A,_SS)

        DB 80H + #0<<3 + #2
END.

DEFMAC  ("*(*)",_SAR,HL)

        DB 086H + #0<<3
END.

DEFMAC  ("**,(*)",_SADS,A,HL)

        DB 086H + #0<<3
END.

DEFMAC  (/"**",_SAR,_SNN)

        DB 0C6H + #0<<3, #1
END.

DEFMAC  (/"**,*",_SADS,A,_SNN)

        DB 0C6H + #0<<3, #2
END.

DEFMAC  ("*(**)",_SAR,_SXX,_SDISP)

        DB 0DDH + #1<<5, 86H + #0<<3, #2
END.

DEFMAC  ("**,(**)",_SADS,A,_SXX,_SDISP)

        DB 0DDH + #2<<5, 86H + #0<<3, #3
END.

DEFMAC  (/"**",_SID,_SS)

        DB 4 + #0 + #1<<3
END.

DEFMAC  (/"*(*)",_SID,HL)

        DB 34H + #0
END.

DEFMAC  (/"*(**)",_SID,_SXX,_SDISP)

        DB 0DDH + #1<<5, 34H + #0, #2
END.

DEFMAC  ("CPI")

        DB 0EDH,0A1H
END.

DEFMAC  ("CPIR")

        DB 0EDH,0B1H
END.

DEFMAC  ("CPD")

        DB 0EDH,0A9H
END.

DEFMAC  ("CPDR")

        DB 0EDH,0B9H
END.

;
;
; 4) General purpose arithmetic and CPU control
;

DEFMAC  ("*",_SGC)

        DB 27H + #0<<3
END.

DEFMAC  ("NOP")

        DB 0
END.

DEFMAC  ("NEG")

        DB 0EDH,44H
END.

DEFMAC  ("HALT")

        DB 76H
END.

DEFMAC  ("DI")

        DB 0F3H
END.

DEFMAC  ("EI")

        DB 0FBH
END.

DEFMAC  ("IM*",_SZTO2)

        DB 0EDH
        IF #0
        DB 4EH + #0<<3
        ELSE
        DB 46H
        END
END.


;
;
; 5) 16 Bit arithmetic group
; ==========================
;

DEFMAC  (/"**,*",ADD,HL,_SDD)

        DB 9 + #2<<4
END.

DEFMAC  (/"**,*",ADD,IX,_SPP)

        DB 0DDH, 9 + #2<<4
END.

DEFMAC  (/"**,*",ADD,IY,_SRR)

        DB 0FDH, 9 + #2<<4
END.

DEFMAC  (/"**,*",ADC,HL,_SDD)

        DB 0EDH, 4AH + #2<<4
END.

DEFMAC  (/"**,*",SBC,HL,_SDD)

        DB 0EDH, 42H + #2<<4
END.

DEFMAC  (/"**",_SID,_SDD)

        DB 3 + #0<<3 + #1<<4
END.

DEFMAC  (/"**",_SID,_SXX)

        DB 0DDH + #1<<5, 23H + #0<<3
END.


;
;
; 6) Rotate and shift group
; =========================
;

DEFMAC  (/"*",_SROTA)

        DB 7 + #0<<3
END.

DEFMAC  (/"**",_SROT,_SS)

        DB 0CBH, #0<<3 + #1
END.

DEFMAC  (/"*(*)",_SROT,HL)

        DB 0CBH, #0<<3 + 6
END.

DEFMAC  (/"*(**)",_SROT,_SXX,_SDISP)

        DB 0DDH + #1<<5, 0CBH, #2, #0<<3 + 6
END.

DEFMAC  ("RLD")

        DB 0EDH, 6FH
END.

DEFMAC  ("RRD")

        DB 0EDH, 067H
END.


;
;
; 7) Bit test and set group
; =========================
;

DEFMAC  (/"**,*",_SBIT,_SZTO7,_SS)

        DB 0CBH, 40H + #0<<6 + #1<<3 + #2
END.

DEFMAC  (/"**,(*)",_SBIT,_SZTO7,HL)

        DB 0CBH, 46H + #0<<6 + #1<<3
END.

DEFMAC  (/"**,(**)",_SBIT,_SZTO7,_SXX,_SDISP)

        DB 0DDH + #2<<5, 0CBH, #3, 46H + #0<<6 + #1<<3
END.


;
;
; 8) Jump and call group
; ======================
;

DEFMAC  ("JP*,*",_SCE,_SNNNN)

        DB 0C2H + #0<<3
        DW #1
END.

DEFMAC  ("CALL*,*",_SCE,_SNNNN)

        DB 0C4H + #0<<3
        DW #1
END.

DEFMAC  ("RST*",_SNN)
        DB 0C7H + #0
END.

DEFMAC  ("JP*",_SNNNN)

        DB 0C3H
        DW #0
END.

DEFMAC  ("CALL*",_SNNNN)

        DB 0CDH
        DW #0
END.

DEFMAC  ("JR*",_SNNNN)

        DB 18H
        DD #0 - [$ + 1]
END.

DEFMAC  ("JR*,*",_SCC,_SNNNN)

        DB 20H + #0<<3
        DD #1 - [$ + 1]
END.


DEFMAC  ("JP(*)",_SII)

        IF #0
        DB 0BDH + #0<<5
        END
        DB 0E9H
END.

DEFMAC  ("DJNZ*",_SNNNN)

        DB 10H
        DD #0 - [$ + 1]
END.


;
;
; 9) Return group
; ===============
;

DEFMAC  ("RET")

        DB 0C9H
END.

DEFMAC  ("RET*",_SCE)

        DB 0C0H + #0<<3
END.

DEFMAC  ("RETI")

        DB 0EDH, 4DH
END.

DEFMAC  ("RETN")

        DB 0EDH, 045H
END.


;
;
; 10) Input / Output group
; ========================
;

DEFMAC  ("IN*,(*)",A,_SNN)

        DB 0DBH, #1
END.

DEFMAC  ("OUT(*),*",_SNN,A)

        DB 0D3H, #0
END.

DEFMAC  (/"IN*,(*)",_SS,C)

        DB 0EDH, 40H + #0<<3
END.

DEFMAC  (/"OUT(*),*",C,_SS)

        DB 0EDH, 41H + #1<<3
END.

DEFMAC  ("INI")


        DB 0EDH, 0A2H
END.

DEFMAC  ("INIR")

        DB 0EDH, 0B2H
END.

DEFMAC  ("OUTI")

        DB 0EDH, 0A3H
END.

DEFMAC  ("OTIR")

        DB 0EDH, 0B3H
END.

DEFMAC  ("IND")

        DB 0EDH, 0AAH
END.

DEFMAC  ("INDR")

        DB 0EDH, 0BAH
END.

DEFMAC  ("OUTD")

        DB 0EDH, 0ABH
END.

DEFMAC  ("OTDR")

        DB 0EDH, 0BBH
END.

        END

