;*******************************************************************************
; 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.1, September 24th, 2025
; Copyright (C) 2019 - 2025 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
;-------------------------------------------------------------------------------
; Z65               | Z80
;---------------------------------------
; TSF               | AND     A
; CLA               | XOR     A
;                   |
; coc               | ccf
; clc               | OR      A
; sec               | scf
;                   |
; bra     N         | jr      N
; bcc     N         | jr      nc,N
; bcs     N         | jr      c,N
; beq     N         | jr      z,N
; bne     N         | jr      nz,N
;                   |
; jcc     NN        | jp      nc,NN
; jcs     NN        | jp      c,NN
; jpl     NN        | jp      p,NN
; jmi     NN        | jp      n,NN
; jeq     NN        | jp      z,NN
; jne     NN        | jp      nz,NN
; jpe     NN        | jp      pe,NN
; jpo     NN        | jp      po,NN
;                   |
; djne    NN        | djnz    NN
;                   |
; ccc     NN        | call    nc,NN
; ccs     NN        | call    c,NN
; cpl     NN        | call    p,NN
; cmi     NN        | call    n,NN
; ceq     NN        | call    z,NN
; cne     NN        | call    nz,NN
; cpe     NN        | call    pe,NN
; cpo     NN        | call    po,NN
;                   |
; 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     N         | jr      N
; bcc     N         | jr      nc,N
; bcs     N         | jr      c,N
; beq     N         | jr      z,N
; bne     N         | jr      nz,N
;---------------------------------------
.macro bra
	jr	\1
.endm

.macro bcc
	jr	nc,\1
.endm

.macro bcs
	jr	c,\1
.endm

.macro beq
	jr	z,\1
.endm

.macro bne
	jr	nz,\1
.endm


;-------------------------------------------------------------------------------
; Jumps
;-------------------------------------------------------------------------------
; jcc     NN        | jp      nc,NN
; jcs     NN        | jp      c,NN
; jpl     NN        | jp      p,NN
; jmi     NN        | jp      n,NN
; jeq     NN        | jp      z,NN
; jne     NN        | jp      nz,NN
; jpe     NN        | jp      pe,NN
; jpo     NN        | jp      po,NN
;                   |
; djne    NN        | djnz    NN
;---------------------------------------
.macro jcc
	jp	nc,\1
.endm

.macro jcs
	jp	c,\1
.endm

.macro jpl
	jp	p,\1
.endm

.macro jmi
	jp	m,\1
.endm

.macro jeq
	jp	z,\1
.endm

.macro jne
	jp	nz,\1
.endm

.macro jpe
	jp	pe,\1
.endm

.macro jpo
	jp	po,\1
.endm


.macro djne
	djnz	\1
.endm


;-------------------------------------------------------------------------------
; Calls
;-------------------------------------------------------------------------------
; ccc     NN        | call    nc,NN
; ccs     NN        | call    c,NN
; cpl     NN        | call    p,NN
; cmi     NN        | call    n,NN
; ceq     NN        | call    z,NN
; cne     NN        | call    nz,NN
; cpe     NN        | call    pe,NN
; cpo     NN        | call    po,NN
;---------------------------------------
.macro ccc
	call	nc,\1
.endm

.macro ccs
	call	c,\1
.endm

.macro cpl
	call	p,\1
.endm

.macro cmi
	call	m,\1
.endm

.macro ceq
	call	z,\1
.endm

.macro cne
	call	nz,\1
.endm

.macro cpe
	call	pe,\1
.endm

.macro cpo
	call	po,\1
.endm


;-------------------------------------------------------------------------------
; Returns
;-------------------------------------------------------------------------------
; rcc               | ret     nc
; rcs               | ret     c
; rpl               | ret     p
; rmi               | ret     n
; req               | ret     z
; rne               | ret     nz
; rpe               | ret     pe
; rpo               | ret     po
;---------------------------------------
.macro rcc
	ret	nc
.endm

.macro rcs
	ret	c
.endm

.macro rpl
	ret	p
.endm

.macro rmi
	ret	m
.endm

.macro req
	ret	z
.endm

.macro rne
	ret	nz
.endm

.macro rpe
	ret	pe
.endm

.macro rpo
	ret	po
.endm
