;	ROBOT, A VDM ROBOT SIMULATION
;
;	FROM BYTE, APRIL 1978
;
SOL	EQU	1
	IF	SOL
; modified for Sol, jtb 11/15/01
VDMPT	EQU	0FEH		;VDM PORT
KBD	EQU	0FCH		;KEYBOARD DATA PORT
STAPT	EQU	0FAH		;KEYBOARD DATA PORT
KDR	EQU	01H		;KEYBOARD STATUS BIT
	ELSE
VDMPT	EQU	0C8H		;VDM PORT
KBD	EQU	01H		;KEYBOARD DATA PORT
KDR	EQU	01H		;KEYBOARD STATUS BIT
	ENDIF
VDM	EQU	0CC00H		;VDM START
SPEED0	EQU	5FFFH
SPEED1	EQU	1FFFH
REBOOT	EQU	0000H		;CP/M warm start
;
	ORG	100H
START	LXI	SP,STACK	;SET UP STACK POINTER
	XRA	A		;CLEAR ACC
	OUT	VDMPT		;INITIALIZE VDM
	MVI	D,14
	LXI	H,VDM		;SET HL TO VDM START
	LXI	B,0A042H
	CALL	LOAD
SCRN	LXI	B,203CH
	CALL	LOAD
	LXI	B,0A004H
	CALL	LOAD
	DCR	D
	CMP	D
	JNZ	SCRN
	LXI	B,0A03EH
	CALL	LOAD
	LXI	H,VDM+220H
	MVI	M,07H		;PUT ROBOT ON SCREEN
GO	CALL	RND
	ANI	02H
	JZ	RC0
	MVI	A,10H
RC0	CALL	REC
DIS	CALL	RND
	STA	DSTAT
DIR	CALL	RND
	MOV	B,A
	LDA	PSTAT
	ANI	10H
	ADD	B
	STA	PSTAT
CHEK	MOV	B,A
	LDA	OSTAT
	XRA	B
	CPI	10H
	JZ	DIR
WAIT	LDA	PSTAT
	ANI	0FH
	CPI	01H
	JZ	SWAIT
LWAIT	LXI	B,SPEED0
	JMP	DEC
SWAIT	LXI	B,SPEED1
DEC	DCX	B
	MOV	A,B
	CPI	00H
	JNZ	DEC
	MVI	M,' '
MOV1	LDA	PSTAT
	CPI	00H
	JZ	UR
	CPI	01H
	JZ	RT
	CPI	02H
	JZ	DR
	CPI	13H
	JZ	DN
	CPI	10H
	JZ	DL
	CPI	11H
	JZ	LFT
	CPI	12H
	JZ	UL
	CPI	03H
	JZ	UP
UR	LXI	D,0FFC2H
	DAD	D
	CALL	BOUNDS
	JMP	NEXT
RT	INX	H
	CALL	BOUNDS
	JMP	NEXT
DR	LXI	D,042H
	DAD	D
	CALL	BOUNDS
	JMP	NEXT
DN	LXI	D,40H
	DAD	D
	CALL	BOUNDS
	JMP	NEXT
DL	LXI	D,3EH
	DAD	D
	CALL	BOUNDS
	JMP	NEXT
LFT	DCX	H
	CALL	BOUNDS
	JMP	NEXT
UL	LXI	D,0FFBEH
	DAD	D
	CALL	BOUNDS
	JMP	NEXT
UP	LXI	D,0FFC0H
	DAD	D
	CALL	BOUNDS
	IF	SOL
NEXT	IN	STAPT		; see if a key is waiting
	ANI	KDR		; test for new key status
	JNZ	NEXTT		; don't look for termination
	IN	KBD
	ELSE
NEXT	IN	KBD
	ENDIF
	CPI	'C'-40H		; look for CTRL-C
	JZ	REBOOT		; go back to monitor
NEXTT	MVI	M,07H
	LDA	DSTAT
	DCR	A
	STA	DSTAT
	CPI	0FFH
	JZ	GO
	JMP	WAIT
BOUNDS	MOV	A,M
	ANI	80H
	RZ
	INX	SP
	INX	SP
	LDA	PSTAT
	MVI	C,10H
	XRA	C
	STA	PSTAT
	JMP	MOV1
LOAD	MOV	M,B
	INX	H
	DCR	C
	CMP	C
	RZ
	JMP	LOAD
REC	PUSH	H
	LXI	H,PSTAT
	MOV	C,M
	MOV	M,A
	INX	H
	MOV	M,C
	POP	H
	RET
RND	PUSH	H
	LXI	H,SH+3
	MVI	B,08H
	MOV	A,M
RTOP	RLC
	RLC
	RLC
	XRA	M
	RAL
	RAL
	DCR	L
	DCR	L
	DCR	L
	MOV	A,M
	RAL
	MOV	M,A
	INR	L
	MOV	A,M
	RAL
	MOV	M,A
	INR	L
	MOV	A,M
	RAL
	MOV	M,A
	INR	L
	MOV	A,M
	RAL
	MOV	M,A
	DCR	B
	JNZ	RTOP
	ANI	03H
	POP	H
	RET
SH	DS	4
PSTAT	DB	1
OSTAT	DB	1
DSTAT	DB	1
STACK	EQU	$+20H
	END

