;
;	REGISTER DEFINITIONS
;
	.MACRO	Z80INI
	.DSABL	REG		; CANCEL PDP-11 REGISTER DEFINITIONS
	B = 0
	C = 1
	D = 2
	E = 3
	H = 4
	L = 5
	M = 6
	STK = 6
	PSW=6
	A = 7
	.ENDM
;
;
;
;	MACROS FOR ASSEMBLING 8080 CODE ON A PDP-11
;
;	B. Z. LEDERMAN
;
	.NLIST	MD,CND		; DO NOT LIST EXPANSIONS
;
;
	.MACRO	HOBYTE	ADDR
	.BYTE	<<<ADDR> & 177400> / 400>
	.ENDM
	.MACRO	LOBYTE	ADDR
	.BYTE	<<ADDR> & 377>
	.ENDM
;
	.MACRO	AINST	W,Y	; ONE BYTE INSTRUCTIONS
	.MACRO	W		; INSERT NAME FOR THE TABLE MACRO
	.NLIST	SRC		; DO NOT LIST THE EXPANSION
	.BYTE	Y		; ASSEMBLE THE ASSOCIATED DATA
	.LIST	SRC		; TURN THE LISTER ON TO LIST CODE
	.ENDM			; END OF TABLE MACRO
	.ENDM	AINST		; END OF ONE BYTE MACRO
;
AINST	NOP	0
AINST	RLC	7
AINST	RRC	17
AINST	RAL	27
AINST	RAR	37
AINST	RIM	40		; 8085
AINST	DAA	47
AINST	CMA	57
AINST	SIM	60		; 8085 CORRECTED 5-24-83 C.B.
AINST	STC	67
AINST	CMC	77
AINST	HLT	166
AINST	XTHL	343
AINST	PCHL	351
AINST	XCHG	353
AINST	RP	360
AINST	DI	363
AINST	SPHL	371
AINST	EI	373
AINST	XRET	311
AINST	EXX	331
;
	.MACRO	BINST	W,Y	; TWO BYTE INSTRUCTIONS
	.MACRO	W	X
	.NLIST	SRC
	.BYTE	Y		; ASSEMBLE BINARY
	LOBYTE	<X>
	.LIST	SRC
	.ENDM
	.ENDM	BINST
;
BINST	ADI	306
BINST	ACI	316
BINST	OUT	323
BINST	SUI	326
BINST	IN	333
BINST	SBI	336
BINST	ANI	346
BINST	XRI	356
BINST	ORI	366
BINST	CPI	376
;
;
	.MACRO	CINST	W,Y		; THREE BYTE INSTRUCTIONS
	.MACRO	W	X
	.NLIST	SRC
	.BYTE	Y			;MAY BE INVERSE ORDER
	LOBYTE	<X>
	HOBYTE	<X>
	.LIST	SRC
	.ENDM
	.ENDM	CINST
;
CINST	SHLD	42
CINST	LHLD	52
CINST	STA	62
CINST	LDA	72
AINST	RNZ	300
CINST	JNZ	302
CINST	JMP	303
CINST	CNZ	304
AINST	RZ	310
CINST	JZ	312
CINST	CZ	314
CINST	XCALL	315
AINST	RNC	320
CINST	JNC	322
CINST	CNC	324
AINST	RC	330
CINST	JC	332
CINST	CC	334
AINST	RPO	340
CINST	JPO	342
CINST	CPO	344
AINST	RPE	350
CINST	JPE	352
CINST	CPE	354
AINST	RP	360
CINST	JP	362
CINST	CP	364
AINST	RM	370
CINST	JM	372
CINST	CM	374
;
;
	.MACRO	DINST	W,Y	; SINGLE BYTE, VALUE IN CENTER
	.MACRO	W,X
	.NLIST	SRC
	.BYTE	<Y!<<X&7>*10>>
	.LIST	SRC
	.ENDM
	.ENDM	DINST
;
DINST	STAX	2
DINST	INX	3
DINST	INR	4
DINST	DCR	5
DINST	DAD	11
DINST	LDAX	12
DINST	DCX	13
DINST	POP	301
DINST	PUSH	305
DINST	RST	307
;
;
	.MACRO	EINST	W,Y	; SINGLE BYTE, VALUE AT RIGHT
	.MACRO	W,X
	.NLIST	SRC
	.BYTE	<Y!<X&7>>
	.LIST	SRC
	.ENDM
	.ENDM	EINST
;
EINST	ADD	200
EINST	ADC	210
EINST	SUB	220
EINST	SBB	230
EINST	AND	240
EINST	XRA	250
EINST	ORA	260
EINST	CMP	270
;
;
	.MACRO	MOV	Y,Z	; MOVE REGISTER TO REGISTER
	.NLIST	SRC
	.BYTE	<100!<<Y&7>*10>!<Z&7>>
	.LIST	SRC
	.ENDM
;
;
	.MACRO	LXI	Y,Z
	.NLIST	SRC
	.BYTE	<1!<<Y&7>*10>>	; CHECK ORDER
	LOBYTE	<Z>
	HOBYTE	<Z>
	.LIST	SRC
	.ENDM
;
;
	.MACRO	MVI	Y,Z
	.NLIST	SRC
	.BYTE	<6!<<Y&7>*10>>
	LOBYTE	<Z>
	.LIST	SRC
	.ENDM
;
	.LIST	MEB,MD		; ENABLE LISTINGS IN PROGRAM
	.LIST
	.MACRO	RETI
	.NLIST	SRC
	.BYTE	355,115
	.LIST	SRC
	.ENDM
	.MACRO	RETN
	.NLIST	SRC
	.BYTE	355,105
	.LIST	SRC
	.ENDM
	.MACRO	BIT	X,Y
	.NLIST	SRC
	.BYTE	313, <<<X & 7> * 10> + <Y & 7>+100>
	.LIST	SRC
	.ENDM
;
;
;
;
	.MACRO	ICAL,	?XYZ
	MOV	E, M
	INX	H
	MOV	D, M	
	XCHG
	LXI	D, XYZ
	PUSH	D
	PCHL
XYZ:
	.ENDM
	.MACRO	DTREPT	STR, ?ABB, ?ABC,  ?XYZ, ?XYY
	DTREPC=DTREPC+1
	XCALL REGSAV
	.NCHR	NUM, <STR>
	.NARG	PRMS
	.IF	DIF ABC Y
	FNCCAL	CHKV40
	JM	<XYZ + NUM + 3>
	.ENDC
	LXI	H, XYZ
	LDA	DOTCNT
	CMP	0
	MVI	A, <NUM + 1>
	JZ	XYY
	ADI	2
	LXI	D, -2
	DAD	D
XYY:
	.IF	IDN   ABB, Y
	ADI	2
	.ENDC
	FNCCAL	DEPSTR	<DTOTQ, H, A>
	MVI	A, 0
	STA	DOTCNT
	JMP	<XYZ + NUM + 3>
	.BYTE	12, 15
XYZ:
	.ASCII	/STR/
	.ASCII 	/ /
	.BYTE	12, 15
	XCALL	RGRST1
	.ENDM	

	.MACRO	WAIT
	XCALL	TSKWT
	.ENDM	
	;
;
;
;
	dvars=0
	dpars=0
	DTREPC=0
	GET16=0
	GET8=0
	PSHREG=0
	EXP=0
	PSHLRG=0
	PSHNAM=0
	DEFCNT=0
	CALCNT=0
	ICLCNT=0
	PTVCNT=0
	L16CNT=0
	L8CNT=0
;
;
;
;
	.macro	chkreg
	.iif	eq	regasn, .error ; register name error
	.endm
;
;
;
	.macro	getv16	rp, var
	GET16=GET16+1
	regasn=0
	.irp	regnam, <B, D, H>
	.if	IDN	<rp> <regnam>
	.byte	335		; LD reg+1, (IX+var)
	.byte	100+<<regnam+1>*8.>+6
	.byte	var
	.byte	335		; LD reg, (IX+var+1)
	.byte	100+<regnam*8.>+6
	.byte	var+1
	regasn=1
	.mexit
	.endc
	.endr
	chkreg
	.endm
;
;
	.macro	getr16	rp, var
	GET16=GET16+1
	regasn=0
	.irp	regnam, <B, D, H>
	.if	IDN	<rp> <regnam>
	.byte	375		; LD reg+1, (IY+var)
	.byte	100+<<regnam+1>*8.>+6
	.byte	var
	.byte	375		; LD reg, (IY+var+1)
	.byte	100+<regnam*8.>+6
	.byte	var+1
	regasn=1
	.mexit
	.endc
	.endr
	chkreg
	.endm
;
;	
	.macro	getp16	rp, var
	GET16=GET16+1
	regasn=0
	.irp	regnam, <B, D, H>
	.if	IDN	<rp> <regnam>
	.byte	335		; LD reg+1, (IX+var)
	.byte	100+<<regnam+1>*8.>+6
	.byte	var
	.byte	335		; LD reg, (IX+var+1)
	.byte	100+<regnam*8.>+6
	.byte	var+1
	regasn=1
	.mexit
	.endc
	.endr
	chkreg
	.endm
;
;
;
	.macro	getv8	rp, var
	GET8=GET8+1
	regasn=0
	.irp	regnam, <A, B, C, D, E, H, L>
	.if	IDN	<rp> <regnam>
	.byte	335		; LD reg, (IX+var)
	.byte	100+<regnam*8.>+6
	.byte	var
	regasn=1
	.mexit
	.endc
	.endr
	chkreg
	.endm
;
;
	.macro	getr8	rp, var
	GET8=GET8+1
	regasn=0
	.irp	regnam, <A, B, C, D, E, H, L>
	.if	IDN	<rp> <regnam>
	.byte	375		; LD reg, (IY+var)
	.byte	100+<regnam*8.>+6
	.byte	var
	regasn=1
	.mexit
	.endc
	.endr
	chkreg
	.endm
;
;
;
;
	.macro	getp8	rp, var
	GET8=GET8+1
	regasn=0
	.irp	regnam, <A, B, C, D, E, H, L>
	.if	IDN	<rp> <regnam>
	.byte	335		; LD reg, (IX+var)
	.byte	100+<regnam*8.>+6
	.byte	var
	regasn=1
	.mexit
	.endc
	.endr
	chkreg
	.endm
;
;
;
;
	.macro	putv16	rp, var
	GET16=GET16+1
	chkvar	var
	regasn=0
	.irp	regnam, <B, D, H>
	.if	IDN	<rp> <regnam>
	.byte	335
	.byte	160+rp+1
	.byte	var
	.byte	335
	.byte	160+rp
	.byte	var+1
	regasn=1
	.mexit
	.endc
	.endr
	chkreg
	.endm
;
;
;
;
	.macro	putv8	rp, var
	GET8=GET8+1
	chkvar	var
	regasn=0
	.irp	regnam, <A, B, C, D, E, H, L>
	.if	idn	<rp> <regnam>
	.byte	335
	.byte	160+rp
	.byte	var
	regasn=1
	.mexit
	.endc
	.endr
	chkreg
	.endm
;
;
;
;
;
	.macro	exppar	param
	regpar=0
	EXP=EXP+1
	.irp	reg, <PSW, B, D, H>
		.if	idn	<reg> <param>
		PSHREG=PSHREG+1
		push	reg
		regpar=1
		.mexit
		.endc
	.endr
	.if	eq	regpar
		.irp	loreg, <A C E L>
			.if 	idn	<loreg> <param>
			PSHLRG=PSHLRG+1
			PUSH	H
			MOV	L, loreg
			XTHL
			regpar=1
			.mexit
			.endc
		.endr
	.endc
	.if	eq	regpar
	PSHNAM=PSHNAM+1
	RST 	3
;	PUSH	H
;	LXI	H, <param>
;	XTHL
	.endc
	.endm
;
;
;
	.macro	endfnc
	RST	2
;	.byte	335, 371		; ld sp, ix
;	.byte	375,  41
;	.byte	0,     0
;	.byte	375,  71
;	XRET
	dpars=0
	dvars=0
	.DSABL	LSB
	.endm
;
;
;
	.macro	chkvar	var
	.iif	ndf	var	.error ; no such name
	.iif	ge	var,	.error ; write to param area
	.endm
;
;
;
	.macro	fncdef, fncnam, pars, vars
	DEFCNT=DEFCNT+1
	.iif	ne	dvars,	.error ; nested function or process def
	.iif	ne	dpars,	.error ; nested function or process def
fncnam::
	dpars=2
	.irp	parnam, <pars>
	dpars=dpars+2
	parnam=dpars
	.endr
	.irp	varnam, <vars>
	dvars=dvars-2
	varnam=dvars
	.endr
	.BYTE	375, 21		;LD IY, DVARS
	LOBYTE	dvars
	HOBYTE	dvars
	.byte	375, 71		;ADD IY, SP
	.byte	375, 371	;LD SP, IY
	.ENABL	LSB
	.endm
;
;
;
	.macro	prcdef, fncnam, pars, vars
	DEFCNT=DEFCNT+1
	.iif	ne	dvars,	.error ; nested function or process def
	.iif	ne	dpars,	.error ; nested function or process def
fncnam:
	dpars=2
	.irp	parnam, <pars>
	dpars=dpars+2
	parnam=dpars
	.endr
	.irp	varnam, <vars>
	dvars=dvars-2
	varnam=dvars
	.endr
	.BYTE	375, 21		;LD IY, DVARS
	LOBYTE	dvars
	HOBYTE	dvars
	.byte	375, 71		;ADD IY, SP
	.byte	375, 371	;LD SP, IY
	.ENABL	LSB
	.endm
;
;
;
;
	.macro	endprc
	dvars=0
	dpars=0
	LDA	CURTSK
	FNCCAL	TSKKIL	A
	WAIT
	.endm
;
;
;
	.macro	fnccal	func	params	reslts
	CALCNT=CALCNT+1
	.irp	parnam, <params>
	exppar	<parnam>
	.endr
	resix=-2
	.irp	res, <reslts>
	res = resix
	resix = resix-2
	.endr
	RST	1
;	.byte	335, 345	;PUSH IX
;	.byte	335,  41	;LD IX, 0
;	.byte	  0,   0
;	.byte	335,  71	;ADD IX, SP
;	XCALL	func
	LOBYTE	FUNC
	HOBYTE	FUNC
;	.byte	335, 341	;POP IX
	.endm

	.macro	ifncal	func	params	reslts ?retadr
	ICLCNT=ICLCNT+1
	.irp	parnam, <params>
	exppar	<parnam>
	.endr
	resix=-2
	.irp	res, <reslts>
	res = resix
	resix = resix-2
	.endr
	.byte	335, 345	;PUSH IX
	.byte	335,  41	;LD IX, 0
	.byte	  0,   0
	.byte	335,  71	;ADD IX, SP
	PUSH	D
	MOV	E, M
	INX	H
	MOV	D, M
	XCHG
	POP 	D
	PUSH	H
	LXI	H, retadr
	XTHL
	PCHL
retadr:
	.byte	335, 341	;POP IX
	.endm

;
;
;
	.MACRO	ptov	var,	 param
	PTVCNT=PTVCNT+1
	chkvar	var
	PUSH 	PSW
	.byte	335, 176		; LD A, (IX+param)
	.byte	param
	.byte	335, 167		; LD (IX+var), A
	.byte	var
	.byte	335, 176		; LD A, (IX+param+1)
	.byte	param+1
	.byte	335, 167		; LD (IX+var+1), A
	.byte	var+1
	POP	PSW
	.endm
;
;
;
;
	.macro	ldv16	var, const
	L16CNT=L16CNT+1
	chkvar	var
	.byte	335, 36			; LD (IX+var), lobyte of const
	LOBYTE	const
	.byte	335, 36			; LD (IX+var+1), hobyte of const
	HOBYTE	const
	.endm
;
;
;
	.macro	ldv8	var, const
	L8CNT=L8CNT+1
	chkvar	var
	.byte	335, 36			; LD (IX+var), lobyte of const
	LOBYTE	const
	.endm
;
;
