;
;	"FASTLIFE"		08-June-83
;
BDOS	EQU	5
CONIN	EQU	1
CONOUT	EQU	2
DIRECT	EQU	6
PSTRING	EQU	9
INPUT	EQU	0FFh
DJNZ	EQU	010h
JR	EQU	018h
JRNZ	EQU	020h
JRZ	EQU	028h
LDIR	EQU	0B0EDh
RBIT4H	EQU	0A4CBh
TBIT7M	EQU	07ECBh
RBIT7M	EQU	0BECBh
SBIT7M	EQU	0FECBh

BS	EQU	08	;Left
LF	EQU	0Ah	;Down
UP	EQU	0Bh	;Up
RIGHT	EQU	0Ch	;Right
CR	EQU	0Dh
CLRSCR	EQU	01Ah
BUG	EQU	0Dh
XMAX	EQU	60	;Number of columns
YMAX	EQU	32
CRT	EQU	0F000h
	ORG	00100h
FAST:	JMP	FAST00

INSTR:	DB CR,LF,CR,LF,CR,LF
	DB 'FASTLIFE  1.00  -- by Carl Flarity'
	DB CR,LF,CR,LF
	DB 'This  game  involves  cellular  generation  growth.'
	DB CR,LF
	DB 'Each cell is represented by a character on the CRT.'
	DB CR,LF
	DB 'A cell is surrounded by eight other cells.  In each'
	DB CR,LF
	DB 'generation  cells  die  and   grow  simultaneously.'
	DB CR,LF
	DB 'A cell dies from overpopulation if it is surrounded'
	DB CR,LF
	DB 'by more  than three living cells.  It will also die'
	DB CR,LF
	DB 'of   isolation  if  it  has  less  than  two  cells'
	DB CR,LF
	DB 'surrounding  it.   New  cells  are formed in  empty'
	DB CR,LF
	DB 'positions ONLY if it has three cells around it.'
	DB CR,LF,CR,LF
	DB 'Enter your pattern - a line at a time ...'
	DB CR,LF,CR,LF
	DB '   [.]    where you want a live cell'
	DB CR,LF
	DB '   [0]    for an empty  one'
	DB CR,LF
	DB ' [RETURN] at the end of each line'
	DB CR,LF
	DB '   [ESC]  to clear screen and set new pattern'
	DB CR,LF
	DB '   [/]    for each new generation'
	DB CR,LF
	DB '   [\]    for continuous growth'
	DB CR,LF
	DB '   [^C]   to return to CP/M'
	DB CR,LF,CR,LF
	DB 'The  number  pad can  be used to reposition cursor.'
STACK:	DB CR,LF,'$'
EMPTY:	DB 1Ah
ASCII0:	DB	' ','$'		; 0
HOME:	DB	01Eh,'$'
	DB	LF,BS,'$'	; 1
LASTKEY:DB	0
	DB	LF,'$'		; 2
CURSOR:	DW	CRT
	DB	LF,RIGHT,'$$'	; 3
	DB	BS,'$$$'	; 4
	DB	'*',BS,'$$'	; 5
	DB	RIGHT,'$$$'	; 6
	DB	BS,UP,'$$'	; 7
	DB	UP,'$$$'	; 8
	DB	UP,RIGHT,'$$'	; 9
	DB	'*',BS,LF,'$'	; A  :
	DB	CLRSCR,'$$$'	; B  ESC
	DB	'*',BS,BS,'$'	; C  ;
	DB	CR,LF,'$$'	; D  CR
	DB	'*$$$'		; E  .
	DB	'$'		; F  /

PATA:	DB	'AAA',BS,LF,'A',BS,BS,LF,'A$'
PATB:	DB	'B',BS,LF,'B',BS,LF,'B',BS,BS,'B'
	DB	BS,BS,UP,'B$'
PATC:	DB	'CCC$'
PATD:	DB	'DD',LF,BS,BS,BS,'DD$'


FAST00:	LXI	D,INSTR		;Give instructions
	MVI	C,PSTRING
	CALL	BDOS
	LXI	D,HOME		;Home the cursor
	MVI	C,PSTRING
	CALL	BDOS
	JMP	FAST01

CLEAR:	MVI	E,CLRSCR
	MVI	C,CONOUT
	CALL	BDOS
	XRA	A
	STA	LASTKEY

FAST01:	LXI	SP,STACK
	MVI	C,DIRECT	;Wait for another key
	MVI	E,INPUT
	CALL	BDOS
	CPI	3
	JZ	0		;Return to CP/M !
	ORA	A
	JNZ	FAST02
	LDA	LASTKEY
	CPI	'\'
	JZ	FAST04
	JMP	FAST01

FAST02:	STA	LASTKEY
	CPI	'\'
	JZ	FAST04

	ANI	05Fh
	LXI	H,PATA
	CMP	M
	JZ	FAST03		;Pattern 'A' !
	LXI	H,PATB
	CMP	M
	JZ	FAST03		;Pattern 'B' !
	LXI	H,PATC
	CMP	M
	JZ	FAST03
	LXI	H,PATD
	CMP	M
	JZ	FAST03
	
	ANI	0Fh
	PUSH	A
	ADD	A
	ADD	A
	MOV	E,A
	MVI	D,0
	LXI	H,ASCII0
	DAD	D
FAST03:	XCHG
	MVI	C,PSTRING
	CALL	BDOS
	POP	A
	CPI	0Fh
	JNZ	FAST01

FAST04:	LXI	H,COUNT		;Zero out count array
	LXI	D,COUNT+1
	LXI	B,0FFFh
	MVI	M,0
  DW	LDIR

FAST08:	LXI	H,CRT-080h+XMAX	;Find all occupied cells
	MVI	C,YMAX	
FAST10:	MVI	B,XMAX
	LXI	D,080h-XMAX
	DAD	D
	MVI	A,' '

FAST12:	CMP	M
  DB	JRNZ,	FAST14-$-1
FAST13:	INX	H
  DB	DJNZ,	FAST12-$+0FFh
	DCR	C
  DB	JRNZ,	FAST10-$+0FFh
	JMP	FAST32

FAST14	EQU	$
  DW	TBIT7M
	JZ	FAST17
  DW	RBIT7M
	SHLD	CURSOR
FAST15:	CMP	M
  DB	JRZ,	FAST13-$+0FFh

FAST17:	PUSH	H
	LXI	D,COUNT-CRT+1

	MOV	A,B
	DCR	A		;Test for 01
  DB	JRNZ,	FAST19-$-1
	LXI	D,COUNT-CRT+1-XMAX
	DAD	D
	INR	M	;"R"
	LXI	D,XMAX-2
	DAD	D
	INR	M	;L
	MVI	E,080h	;(D=00) 080h
	DAD	D
  DW	RBIT4H
	INR	M	;L & D
	INX	H
	INR	M	;    D
	LXI	D,1-XMAX
	DAD	D
	INR	M	;"R" & D
	MVI	E,00	;(D=0FFh) 0-100h
	DAD	D
	MOV	A,H
	ANI	00Fh
	ORI	COUNT8
	MOV	H,A
	INR	M
	LXI	D,XMAX-1
	DAD	D
  DB	JR,	FAST27-$-1

FAST19:	CPI	XMAX-1
  DB	JRNZ,	FAST26-$-1
	DAD	D
	INR	M	;R
	LXI	D,XMAX-2
	DAD	D
	INR	M	;"L"
	MVI	E,080h	;(D=00) 080h
	DAD	D
  DW	RBIT4H
	INR	M	;"L" & D
	LXI	D,1-XMAX
	DAD	D
	INR	M	;    D
	INX	H
	INR	M	;R & D
	MVI	E,0	;(D=0FFh) 0-100h
	DAD	D
	MOV	A,H
	ANI	00Fh
	ORI	COUNT8
	MOV	H,A
	INR	M	;R & U
	DCX	H
	INR	M	;    U
	LXI	D,XMAX-1
	DAD	D
  DB	JR,	FAST28-$-1

FAST26:	DAD	D
	INR	M	;R
	DCX	H
	DCX	H
	INR	M	;L
	LXI	D,080h
	DAD	D
  DW	RBIT4H
	INR	M	;L & D
	INX	H
	INR	M	;    D
	INX	H
	INR	M	;R & D
	LXI	D,0-100h
	DAD	D
	MOV	A,H
	ANI	00Fh
	ORI	COUNT8
	MOV	H,A
	INR	M	;R & U
	DCX	H
FAST27:	INR	M	;    U
	DCX	H
FAST28:	INR	M	;L & U
	POP	H
	MVI	A,' '

FAST30:	INX	H
	DCR	B
	JNZ	FAST12
	DCR	C
	JNZ	FAST10

FAST32:	LXI	H,CRT-080h+XMAX
	LXI	D,COUNT-080h+XMAX
	MVI	C,YMAX
FAST35:	MVI	B,XMAX
	PUSH	B
	LXI	B,080h-XMAX
	DAD	B
	XCHG
	DAD	B
	XCHG
	POP	B
	
FAST37:	LDAX	D
	CPI	3
	JZ	FAST40	;Let it live!
	MOV	A,M
	CPI	' '
	JZ	FAST50	;Not occupied !
	LDAX	D
	CPI	2
	JZ	FAST40	;Let it live!
	MVI	M,' '	;Kill this one
	JMP	FAST50

FAST40	EQU	$
  DW	SBIT7M
FAST50:	INX	H
	INX	D
	DCR	B
	JNZ	FAST37
	DCR	C
	JNZ	FAST35

	XRA	A
	STA	EMPTY
	LXI	H,CRT-080h+XMAX
	MVI	C,YMAX
FAST60:	MVI	B,XMAX
	LXI	D,080h-XMAX
	DAD	D
FAST70	EQU	$
  DW	TBIT7M
	MVI	A,' '
	JZ	FAST74
	MVI	A,BUG
	STA	EMPTY
FAST74:	MOV	M,A
	INX	H
  DB	DJNZ,	FAST70-$+0FFh
	DCR	C
	JNZ	FAST60
	LDA	EMPTY
	ORA	A
	JZ	CLEAR
	LHLD	CURSOR
  DW	SBIT7M
	JMP	FAST01		;Go for next generation

COUNT	EQU	08000h
COUNT8	EQU	080h
	END
