;*******************************************************************************
; Z65 Linguistic Helpers                                                       *
;*******************************************************************************
; A set of translation macros which allow Zilog Z80 software to be written in a
; manner which more closely resembles 6502, 6800, and 68000 syntax.
;
; Designed for use with WLA-DX.
;-------------------------------------------------------------------------------
; Version 1.0, November 12th, 2019
; Copyright (C) 2019 Osman Celimli
;
; This software is provided 'as-is', without any express or implied
; warranty.  In no event will the authors be held liable for any damages
; arising from the use of this software.
;
; Permission is granted to anyone to use this software for any purpose,
; including commercial applications, and to alter it and redistribute it
; freely, subject to the following restrictions:
;
; 1. The origin of this software must not be misrepresented; you must not
;    claim that you wrote the original software. If you use this software
;    in a product, an acknowledgment in the product documentation would be
;    appreciated but is not required.
; 2. Altered source versions must be plainly marked as such, and must not be
;    misrepresented as being the original software.
; 3. This notice may not be removed or altered from any source distribution.
;
;
;---------------------------------------
; Mnemonic Mangler
;=======================================
; TSF --> AND A
; CLA --> XOR A
;
; coc --> ccf
; clc --> OR A
; sec --> scf
;
; bra [DISP] --> jr [DISP]
; bcc [DISP] --> jr nc,[DISP]
; bcs [DISP] --> jr c,[DISP]
; beq [DISP] --> jr z,[DISP]
; bne [DISP] --> jr nz,[DISP]
;
; jcc [ADDR] --> jp nc,[ADDR]
; jcs [ADDR] --> jp c,[ADDR]
; jpl [ADDR] --> jp p,[ADDR]
; jmi [ADDR] --> jp n,[ADDR]
; jeq [ADDR] --> jp z,[ADDR]
; jne [ADDR] --> jp nz,[ADDR]
; jpe [ADDR] --> jp pe,[ADDR]
; jpo [ADDR] --> jp po,[ADDR]
;
; djne [ADDR] --> djnz [ADDR]
;
; ccc [ADDR] --> call nc,[ADDR]
; ccs [ADDR] --> call c,[ADDR]
; cpl [ADDR] --> call p,[ADDR]
; cmi [ADDR] --> call n,[ADDR]
; ceq [ADDR] --> call z,[ADDR]
; cne [ADDR] --> call nz,[ADDR]
; cpe [ADDR] --> call pe,[ADDR]
; cpo [ADDR] --> call po,[ADDR]
;
; rcc --> ret nc
; rcs --> ret c
; rpl --> ret p
; rmi --> ret n
; req --> ret z
; rne --> ret nz
; rpe --> ret pe
; rpo --> ret po
;
;
;---------------------------------------
; Tips
;=======================================
; - Loads and Stores
;   \- Loads won't alter flags unless from the Interrupt Page Address Register.
;    - Register to Register moves won't alter flags either.
;    - Dummy arithmetic (AND A, OR A, ...) does, use "TSF" for this.
;
; - Subtracts (SUB, SBC, CP, ...)
;   \- Carry Flag is _SET_ to perform a borrow, or _CLEAR_ if there's no borrow.
;    - Carry Flag is _SET_ if A < N, and _CLEAR_ if A >= N.
;    - This is opposite of how the 6502's subtracts and compares behave.
;
; - Braches (bra, bcc, bcs, ...)
;   \- Taken == 12 Cycles
;    - Skipped == 7 Cycles
;
;   \- No bmi or bpl (--!!)
;
; - Conditional Jumps (jcc, jcs, ...)
;   \- Always 10 Cycles
;    - Favorable over branches if condition is usually true.
;
; - Conditional Calls (ccc, ccs, ...)
;   \- Taken == 17 Cycles
;    - Skipped == 10 Cycles
;
; - Conditional Returns (rcc, rcs, ...)
;   \- Taken == 11 Cycles
;    - Skipped == 5 Cycles
;

; TSF --> AND A
; CLA --> XOR A
;---------------------------------------
.macro TSF
	AND A
.endm

.macro CLA
	XOR A
.endm


; coc --> ccf
; clc --> OR A
; sec --> scf
;---------------------------------------
.macro coc
	ccf
.endm

.macro clc
	OR A
.endm

.macro sec
	scf
.endm




; Branches
;-------------------------------------------------------------------------------
; bra [DISP] --> jr [DISP]
;---------------------------------------
.macro bra
	jr \1
.endm


; bcc [DISP] --> jr nc,[DISP]
; bcs [DISP] --> jr c,[DISP]
;---------------------------------------
.macro bcc
	jr nc,\1
.endm

.macro bcs
	jr c,\1
.endm


; beq [DISP] --> jr z,[DISP]
; bne [DISP] --> jr nz,[DISP]
;---------------------------------------
.macro beq
	jr z,\1
.endm

.macro bne
	jr nz,\1
.endm




; Jumps
;-------------------------------------------------------------------------------
; jcc [ADDR] --> jp nc,[ADDR]
; jcs [ADDR] --> jp c,[ADDR]
;---------------------------------------
.macro jcc
	jp nc,\1
.endm

.macro jcs
	jp c,\1
.endm


; jpl [ADDR] --> jp p,[ADDR]
; jmi [ADDR] --> jp n,[ADDR]
;---------------------------------------
.macro jpl
	jp p,\1
.endm

.macro jmi
	jp m,\1
.endm


; jeq [ADDR] --> jp z,[ADDR]
; jne [ADDR] --> jp nz,[ADDR]
;---------------------------------------
.macro jeq
	jp z,\1
.endm

.macro jne
	jp nz,\1
.endm


; jpe [ADDR] --> jp pe,[ADDR]
; jpo [ADDR] --> jp po,[ADDR]
;---------------------------------------
.macro jpe
	jp pe,\1
.endm

.macro jpo
	jp po,\1
.endm


; djne [ADDR] --> djnz [ADDR]
;---------------------------------------
.macro djne
	djnz \1
.endm




; Calls
;-------------------------------------------------------------------------------
; ccc [ADDR] --> call nc,[ADDR]
; ccs [ADDR] --> call c,[ADDR]
;---------------------------------------
.macro ccc
	call nc,\1
.endm

.macro ccs
	call c,\1
.endm


; cpl [ADDR] --> call p,[ADDR]
; cmi [ADDR] --> call n,[ADDR]
;---------------------------------------
.macro cpl
	call p,\1
.endm

.macro cmi
	call m,\1
.endm


; ceq [ADDR] --> call z,[ADDR]
; cne [ADDR] --> call nz,[ADDR]
;---------------------------------------
.macro ceq
	call z,\1
.endm

.macro cne
	call nz,\1
.endm


; cpe [ADDR] --> call pe,[ADDR]
; cpo [ADDR] --> call po,[ADDR]
;---------------------------------------
.macro cpe
	call pe,\1
.endm

.macro cpo
	call po,\1
.endm




; Returns
;-------------------------------------------------------------------------------
; rcc --> ret nc
; rcs --> ret c
;---------------------------------------
.macro rcc
	ret nc
.endm

.macro rcs
	ret c
.endm


; rpl --> ret p
; rmi --> ret n
;---------------------------------------
.macro rpl
	ret p
.endm

.macro rmi
	ret m
.endm


; req --> ret z
; rne --> ret nz
;---------------------------------------
.macro req
	ret z
.endm

.macro rne
	ret nz
.endm


; rpe --> ret pe
; rpo --> ret po
;---------------------------------------
.macro rpe
	ret pe
.endm

.macro rpo
	ret po
.endm
