;
;
; PONG
;
; 08/17/76  20:12
; BY ROBERT A. VAN VALZAH
; EXTENSIVELY MODIFIED
; BY WARD CHRISTENSEN
;
; REPLY TO 'ENTER OPTION':
; S TO PLAY SOLO (WALLS REFLECT
;   NO COMPUTER PADDLE)
; W TO PLAY WARM UP; WALLS STILL
;   REFLECT, BUT YOU PLAY AGAINST
;   THE COMPUTER PADDLE
; P PLAY AGAINST THE COMPUTER
;   SERVE CHANGES EVERY 5, MUST
;   WIN BY 2.
;
; HIT RUBOUT AT ANY TIME TO GO BACK
; TO ENTER OPTION
;
; HIT CONTROL-C TO RETURN TO CP/M
;
VDMST	EQU	0CC00H	;VDM SCREEN STARTING ADDRESS
VDMEP	EQU	VDMST/256+4	;VDM ENDING PAGE (PAGE PAST VDM)
;VDMSCP	EQU	0C8H	;VDM SCROLL PORT
VDMSCP	EQU	0FEH	;VDM SCROLL PORT
LSCOR	EQU	VDMST+8		;LEFT SCORE PLACE ON VDM
RSCOR	EQU	VDMST+54	;RIGHT  "	"	"
CURS	EQU	47H		;VDM CURSOR STORE LOACTION
;
;
	ORG	100H
PONG:	LXI	SP,STACK
	LXI	H,VDMST
	SHLD	CURS
	XRA	A
	OUT	VDMSCP
	LXI	H,RECVR
	MVI	M,-4 AND 255	;RECEIVE LEFT
	LXI	H,OPMSG
	CALL	TYPEN
	CALL	KEYIN
	PUSH	PSW
	CALL	TYPE
	POP	PSW
	STA	OPTN
	CPI	'S'
	JNZ	TRYP
	MVI	A,'I'
	STA	LEFT
	STA	RIGHT
	JMP	OK
SINIT:	LXI	H,LSCOR
	MVI	M,'0'
	INX	H
	MVI	M,'0'
	LXI	H,RSCOR
	MVI	M,'0'
	INX	H
	MVI	M,'0'
	LXI	H,0
	SHLD	SCORE
	RET			;FROM SINIT
;
TRYP	CPI	'P'
	JNZ	TRYW
USEP:	MVI	A,']'
	STA	LEFT
	MVI	A,'['
	STA	RIGHT
	JMP	OK
TRYW:	CPI	'W'
	JZ	USEP
	CPI	'A'	;AUTO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IT
	CPI	'I'
	JZ	VHIT
	CPI	'/'
	JZ	HIHT
	CPI	'\'
	JZ	LOHT
	CPI	']'
	JZ	SCORR
	CPI	'['
	JZ	SCORL
	CPI	' '
	JNZ	REST
	SHLD	CURS
	MOV	A,M
	ORI	1000$0000B
	MOV	M,A
DELY	LXI	B,801H
WAIT	DCX	B
	MOV	A,B
	ORA	C
	JNZ	WAIT
	JMP	MORE
;			SCORING ROUTINES
SCORL	EQU	$
	LDA	OPTN
	CPI	'W'
	JZ	VHIT
	LXI	H,SCORE
	INR	M
	LXI	D,LSCOR+2
	CALL	INRSC
	MOV	B,M
	INX	H	;POINT TO RITHT
	MOV	C,M
;		ROUINNE TO DETERMINE
;		WIN, AND CHANGE OF SERVE
;		ON ENTRY, B= LEFT SCORE,
;		C=RIGHT SCORE
TEST	EQU	$
	LXI	H,0
	MVI	D,3
HOLD:	DCX	H
	MOV	A,H
	ORA	L
	JNZ	HOLD
	DCR	D
	JNZ	HOLD
;		DETERMINE SERVER
	MOV	A,B
	ADD	C
SUB10:	SUI	10
	JM	END10
	JMP	SUB10
END10:	ADI	10
	LXI	D,3CFDH		;'<', -4
	CPI	5
	JC	SETSV
	LXI	D,3E03H		;'>', 4
SETSV:	LXI	H,RECVR
	MOV	M,E
	MOV	A,D
	STA	VDMST+32
;		POINT DIFFERENCE >1?
	MOV	A,B
	SUB	C
	JZ	REST
	CPI	1
	JZ	REST
	CPI	-1 AND 255
	JZ	REST
;		DIFF >1; ANYONE WIN?
	MOV	A,B
	CPI	21
	JNC	TEST2
	MOV	A,C
	CPI	21
	JC	REST
;		GOT A WINNER
TEST2:	MOV	A,B
	SUB	C
	LXI	H,LSCOR
	JNC	LWIN
;		RIGHT WIN
	LXI	H,RSCOR
LWIN:	MVI	B,6
SON:	MOV	A,M
	ORI	1000$0000B
	MOV	M,A
	INX	H
	MOV	A,M
	ORI	1000$0000B
	MOV	M,A
	CALL	SWAIT
	MOV	A,M
	ANI	0111$1111B
	MOV	M,A
	DCX	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	M,A
	LXI	D,128
	DAD	D
	MOV	M,C
	POP	H
	RET
;
;		TOP OR BOTTOM HIT
;
HHIT:	LDA	YVEL
	CMA
	INR	A
	STA	YVEL
	JMP	ADVL
;
;		SIDE HIT
;
VHIT:	LDA	XVEL
	CMA
	INR	A
	STA	XVEL
	JMP	ADVL
;
;		HIGH PADDLE HIT
;
HIHT:	LDA	YVEL
	CMA
	INR	A
	INR	A
HIHT1:	STA	YVEL
	JMP	VHIT	;BOUNCE
;
LOHT:	LDA	YVEL
	CMA
	JMP	HIHT1
;
;		TURN CURSOR OFF
;
OFF:	LHLD	CURS
	MOV	A,M
	ANI	0111$1111B
	MOV	M,A
	RET
;
;	AUTO PADDLE
;
AUTO	LDA	OPTN
	CPI	'S'
	RZ
	LDA	XPOS
	CPI	0C0H	;<-- HOW FAR AWAY COMPUTER SEES
	RC
	LDA	XVEL
	ORA	A
	RM
	LDA	XPOS
	ANI	0000$0100B
	RZ		;<-- COMPUTER MOVES 1/2 BALL SPEED
	LHLD	APADL
	CALL	POFF
;
;		CALCULATE BALL Y POSITION
;
	DAD	H
	DAD	H
	MOV	A,H
	ANI	0000$1111B	;LEAVE ONLY LINE NUMBER
	MOV	H,A
	LDA	YPOS
	RAR
	RAR
	RAR
	ANI	0000$1111B	;LEAVE ONLY LINE NUMBER
	LXI	D,0
	SUB	H
	LHLD	APADL
	JZ	AMOVE
	CPI	1
	JZ	AMOVE
	CPI	-1 AND 255
	JZ	AMOVE
	LXI	D,-64
	ORA	A
	JM	AMOVE
	LXI	D,64
AMOVE:	DAD	D
	SHLD	APADL
	MVI	A,'\'
	MVI	B,'I'
	MVI	C,'/'
	CALL	PDTG
	RET
;
TYPEN:	MOV	A,M
	CPI	13
	RZ
	CALL	TYPE
	INX	H
	JMP	TYPEN
;
;		WAIT SUBROUTINE
;
SWAIT:	PUSH	H
	LXI	H,0
SLOOP:	DCX	H
	MOV	A,H
	ORA	L
	JNZ	SLOOP
	POP	H
	RET
;
;		I/0 ROUTINES
;
CSTS:	PUSH	H!	PUSH	D!	PUSH	B
	CALL	CSTSCP
	POP	B!	POP	D!	POP	H
	RET
;
CSTSCP:	LHLD	1
	INX	H!	INX	H!	INX	H
	PCHL
;
;
KEYIN:	PUSH	H!	PUSH	D!	PUSH	B
	CALL	CICPM
	POP	B!	POP	D!	POP	H
	RET
;
CICPM:	LHLD	1
	LXI	D,6
	DAD	D
	PCHL
;
;
TYPE:	PUSH	H!	PUSH	D!	PUSH	B
	PUSH	PSW
	MVI	C,2
	MOV	E,A
	CALL	5
	POP	PSW
	POP	B!	POP	D!	POP	H
	RET
;
;
;		CONSTATNTS
;
APADL	DW	VDMST+509
PADL	DW	0
XPOS	DB	0
YPOS	DB	0
XVEL	DB	0
YVEL	DB	0
SCORE	DW	0	;LEFT,RIGHT SCORE
OPTN	DB	'W'
OPMSG	DB	'ENTER OPTION (S, W, P)?',13
BOOT	EQU	0	;ADDRESS OF CPM RE-BOOT ROUTINE
	DS	50
STACK	EQU	$
	END	PONG

