;; Z80N nextreg and MMU intrinsic rewrites. zcc loads this for -mz80n.

;;
;; ZXN_NEXTREG(reg,val)
;;

%"[ \t]*"0call%"[ \t]*"1_ZXN_NEXTREG_R_%"[A-Fa-f0-9xX]+"2
%"[ \t]*"3jp%"[ \t]*"4_ZXN_NEXTREG_V_%"[A-Fa-f0-9xX]+"5
=
	call	_ZXN_NEXTREG_R_%2
	call	_ZXN_NEXTREG_V_%5
	ret

%"[ \t]*"0call%"[ \t]*"1_ZXN_NEXTREG_R_%"[A-Fa-f0-9xX]+"2
%"[ \t]*"3call%"[ \t]*"4_ZXN_NEXTREG_V_%"[A-Fa-f0-9xX]+"5
=
	nextreg	%2,%5

;;
;; ZXN_NEXTREGA(reg,val)
;;

%"[ \t]*"0jp%"[ \t]*"1_ZXN_NEXTREGA_%"[A-Fa-f0-9xX]+"2
=
	call	_ZXN_NEXTREGA_%2
	ret

%"[ \t]*"0call%"[ \t]*"1_ZXN_NEXTREGA_%"[A-Fa-f0-9xX]+"2
=
	ld	a,l
	nextreg	%2,a

%"[ \t]*"0defc%"[ \t]*"1%"[A-Za-z0-9_]*"2%"[ \t]*"3=%"[ \t]*"4_ZXN_NEXTREGA_%"[A-Fa-f0-9xX]+"5
=
%2:
	call	_ZXN_NEXTREGA_%5
	ret


;;
;; ZXN_READ_REG(reg)
;;

;; zsdcc

%"[ \t]*"0jp%"[ \t]*"1_ZXN_READ_REG_fastcall
=
	call	_ZXN_READ_REG_fastcall
	ret

%"[ \t]*"0defc%"[ \t]*"1%"[A-Za-z0-9_]*"2%"[ \t]*"3=%"[ \t]*"4_ZXN_READ_REG_fastcall
=
%2:
	call	_ZXN_READ_REG_fastcall
	ret

%"[ \t]*"0call%"[ \t]*"1_ZXN_READ_REG_fastcall
=
	ld	bc,0x243b
	out	(c),l
	inc	b
	in	l,(c)

;; sccz80

%"[ \t]*"0jp%"[ \t]*"1ZXN_READ_REG
=
	call	ZXN_READ_REG
	ret

%"[ \t]*"0defc%"[ \t]*"1%"[A-Za-z0-9_]*"2%"[ \t]*"3=%"[ \t]*"4ZXN_READ_REG
=
%2:
	call	ZXN_READ_REG
	ret

%"[ \t]*"0call%"[ \t]*"1ZXN_READ_REG
=
	ld	bc,0x243b
	out	(c),l
	inc	b
	in	l,(c)
	ld	h,0

;;
;; ZXN_WRITE_REG(reg,data)
;;

;; zsdcc

%"[ \t]*"0jp%"[ \t]*"1_ZXN_WRITE_REG_callee
=
	call	_ZXN_WRITE_REG_callee
	ret

%"[ \t]*"0call%"[ \t]*"1_ZXN_WRITE_REG_callee
=
	pop	hl
	ld	bc,0x243b
	out	(c),l
	inc	b
	out	(c),h


;; sccz80

%"[ \t]*"0jp%"[ \t]*"1ZXN_WRITE_REG_callee
=
	call	ZXN_WRITE_REG_callee
	ret

%"[ \t]*"0call%"[ \t]*"1ZXN_WRITE_REG_callee
=
	pop	de
	pop	hl
	ld	bc,0x243b
	out	(c),l
	inc	b
	out	(c),e

;;
;; ZXN_READ_MMUn()
;;

;; zsdcc

%"[ \t]*"0jp%"[ \t]*"1_ZXN_READ_MMU%"[0-9]+"3
=
	call	_ZXN_READ_MMU%3
	ret

%"[ \t]*"0defc%"[ \t]*"1%"[A-Za-z0-9_]*"2%"[ \t]*"3=%"[ \t]*"4_ZXN_READ_MMU%"[0-9]+"5
=
%2:
	call	_ZXN_READ_MMU%5
	ret

%"[ \t]*"0call%"[ \t]*"1_ZXN_READ_MMU%"[0-9]+"3
=
	ld	bc,0x243b
	ld	a,0x50+%3
	out	(c),a
	inc	b
	in	l,(c)

;; sccz80

%"[ \t]*"0jp%"[ \t]*"1ZXN_READ_MMU%"[0-9]+"3
=
	call	ZXN_READ_MMU%3
	ret

%"[ \t]*"0defc%"[ \t]*"1%"[A-Za-z0-9_]*"2%"[ \t]*"3=%"[ \t]*"4ZXN_READ_MMU%"[0-9]+"5
=
%2:
	call	ZXN_READ_MMU%5
	ret

%"[ \t]*"0call%"[ \t]*"1ZXN_READ_MMU%"[0-9]+"3
=
	ld	bc,0x243b
	ld	a,0x50+%3
	out	(c),a
	inc	b
	in	l,(c)
	ld	h,0

;;
;; ZXN_WRITE_MMUn(page)
;;

%"[ \t]*"0jp%"[ \t]*"1_ZXN_WRITE_MMU_%"[0-9]+"2
=
	call	_ZXN_WRITE_MMU_%2
	ret

%"[ \t]*"0call%"[ \t]*"1_ZXN_WRITE_MMU_%"[0-9]+"2
=
	ld	a,l
	mmu%2	a

;;
;; TEMPORARY AREA
;;
;; Real test hardware has had zx next instructions removed
;; as many have smaller fpgas that cannot hold all the logic.
;;
;; Must translate "nextreg" and "mmu" instructions we are
;; using to actual OUTs.  This also affects the C macros
;; used to inline these instructions in zxn.h because zsdcc
;; is being told which registers are unaffected by the macros.
;;
;; This rules file only affects c code.
;;

;;	nextreg	%"[A-Fa-f0-9xX]+"1,%"[A-Fa-f0-9xX]+"2
;;=
;;	ld	bc,0x243b
;;	ld	a,%1
;;	out	(c),a
;;	inc	b
;;	ld	a,%2
;;	out	(c),a

;;	mmu%"[0-9]+"1	a
;;=
;;	ld	bc,0x243b
;;	ld	l,0x50+%1
;;	out	(c),l
;;	inc	b
;;	out	(c),a

	ld	a,l
	ld	l,a
=
	ld	a,l

	ld	l,a
	ld	a,l
=
	ld	l,a

%check 0 <= %2 <= 65535
	ld	hl,_%1
	add	hl,%2
=
	ld	hl,_%1+%2

%title ZXNext register
	ld	hl,%1	;const
	push	hl
	ld	hl,%2	;const
	push	hl
	pop	de
	pop	hl
=
	ld	hl,%1	;const
	ld	de,%2	;const
