
CR:		EQU 215
LF:		EQU 212

; print out 1 character
			ORG	000#060
			JMP	OUTCHAR

; wait for 1 character
			ORG	000#070
			JMP	INCHAR

			ORG 	000#100
SHOOTSTR:	LAI	CR			;DISPLAY A LINEFEED TO
			RST	6		;	INITIALZE DISPLAY;
			LAI	LF		;DISPLAY A LINEFEED TO
			RST	6		;	INITIALZE DISPLAY;

			LLI	\LB\HMESS	;SET ADDRESS POINTERS
			LHI	\HB\HMESS	;	TO HEADING MESSAGE;
			CAL	OUTPUT		;PRINT MESSAGE & RETURN;
			CAL	INPUT		;CALL INPUT LOOPER;

ASTART:		LAI	CR			;SET UP LINEFEED
			RST	6		;DISPLAY ONE LINEFEED,
			LAI	LF		;DISPLAY A LINEFEED TO
			RST	6		;	INITIALZE DISPLAY;
			RST	6		;	THEN A SECOND LINEFEED
			RST	6		;	THEN A THIRD


			LBI	0		;INITIALIZE THE UNIVERSE
			LCI	1		;	TO STARTING PATTERN;
			LLI	\LB\COUNTER	;POINT TO THE ATTEMPT COUNTER
			LHI	\HB\COUNTER	;
			LMB			;THEN CLEAR SHOT COUNTER;
CNTSHOT:		LLI	\LB\COUNTER	;POINT TO THE  ATTEMPT COUNTER
			LHI	\HB\COUNTER	;
			LDM
			IND			;COUNT A SHOT (ANTICIPATORY),
			LMD			;SAVE COUNT FOR LATER
SETCNT:		LEI	012			;LOOP COUNT 10 INTERATIONS
DISLOOP:	DCE				;IS THE LOOP DONE?
			JTZ	WINTEST		;IF SO THEN GO TO WIN TESTING.
			LAE			;IF NOT THEN CONTINUE DISPLAY.
			CPI	6		;IF IS FOURTH CYCLE?
			JTZ	LINFEED		;IF SO THEN NEW LINE NEEDED.
			CPI	3		;IS IT SEVENTH CYCLE?
			JTZ	LINFEED		;IF SO THEN NEW LINE NEEDED
			CPI	5		;IS IT STAR NUMBER 5
			JTZ	FIVTST		;IF SO THEN GO TEST STAR 5
NEDOT:		XRA				;CLEAR THE CARRY (AND A TOO)
			LAB			;MOVE UNIVERSE TO A
			RRC			;ROTATE NEXT PLACE INTO CARRY,
			LBA			;SAVE IT IN 8 FOR A WHILE
PSEUDOT:	JFC	LOADOT			;IF DOT THEN GO OUTPUT DOT;
			LAI	052		;OTHERWISE LOAD A STAR;
			RST	6		;THEN PRINT THE STAR

			JMP	SPCNOW		;BRANCH AROUND THE DOT LOGIC;
LOADOT:		LAI	056			;LOAD A DOT
			RST	6		;THEN PRINT THE DOT

SPCNOW:		LAI	040			;LOAD A SPACE;
			RST	6		;PRINT ONE SPACE;
			RST	6		;THEN PRINT A SECOND

			JMP	DISLOOP		;WALTZ AROUND LOOP ONCE MORE;
LINFEED:	LAI	CR			;LOAD A LINE FEED.
			RST	6		;DISPLAY A LINE FEED
			LAI	LF		;DISPLAY A LINEFEED TO
			RST	6		;	INITIALZE DISPLAY;
			RST	6		;	THEN A SECOND ONE;

			JMP	NEDOT		;BACK TO PRINT NEXT DOT OR STAR.
FIVTST:		XRA				;NO OPERATION INTENDED - LEFTOVER.
			LAC			;GET POSITION 5 STATUS;
			RRC			;PUT STATUS INTO CARRY;
			JMP	PSEUDOT	;REJOIN MIANLINE AFTER RCC;
GOTSTAR:	LAI	CR			;LOAD A LINEFEED;
			RST	6		;HAVE FINISHED UNIVERSE PRINT,
			LAI	LF		;DISPLAY A LINEFEED TO
			RST	6		;	INITIALZE DISPLAY;
			RST	6		;	SO PRINT SEVERAL
			RST	6		;	LINE FEEDS
			RST	6		;	TO SEPARATE

			LLI	\LB\MESS7	;POINT TO THE 'YOUR SHOT'
			LHI	\HB\MESS7	;	MESSAGE;
			CAL	OUTPUT		;THEN GO PRINT IT
			RST	7		;CALL INPUT FOR CHARACTER
			LEA			;SAVE INPUT TEMPORARILY IN E;
			RST	6		;IMMEDIATELY CHEO THE INPUT;
			LAI	CR		;LOAD A LINE FEED
			RST	6		;PRINT THREE LINE FEEDS TO
			LAI	LF		;DISPLAY A LINEFEED TO
			RST	6		;	INITIALZE DISPLAY;
			RST	6		;	SPACE OUT THE RESPONSE
			LAE			;RECOVER INPUT FOR TESTING
			LEI	9D		;LOOP COUNT FOR TABLE SEARCH;
			LLI	\LB\MASKTAB	;SET UP POINTER TO THE 
			LHI	\HB\MASKTAB	;	THE MASK TABLE;
NEXTGRUP:	CPM				;IS INPUT EQUAL TABLE CHARACTER?
			JTZ	FOUND		;IF SO THEN GO ALTER STRUCTER OF
			DCE			;	THE UNIVERSE OTHERWISE JUST
			JTZ	INVAL		;	CHECK END OF LOOP;
			INL			;INCREMENT THE L
			INL			;	REGISTER POINTER
			INL			;	FOUR TIMES TO GET
			INL			;	TO NEXT TABLE ENTRY;
			JMP	NEXTGRUP	;THEN GO TEST NEXT ENTRY;
FOUND:		INL				;POINT TO POSITION MASK
			LAM			;	AND LOAD MASK INTO A;
			CPI	0		;IS IT ZERO?
			JFZ	UNIV2A		;IF NOT THEN FRINGE POSITION;
			LAC			;OTHERWISE THE CENTER POSITION;
			CPI	1		;IS A STAR IN CENTER?
			JFZ	BADFELO		;IF NOT THEN HAVE WRONG MOVE;
			JMP	NEXBYT		;IFSO THEN GO PROCESS STAR
UNIV2A:		LAB				;REST OF UNIVERS TO A;
			NDM			;AND WITH MASK TO ISOLATE STAR;
			JTZ	BADFELO		;IF NOT STAR THEN WRONG MOVE;
NEXBYT:		INL				;POINT TO THE GALAXY MASK;
			LAB			;FETCH UNIVERSE AGAIN
			XRM			;AND COMPLEME NT THE UNIVERSE
			LBA			;	ON A FINE PERFORMANCE
			INL			;POINT TO CERNTER MASK
			LAC			;FETCH CENTER OF UNIVERSE
			XRM			;COMPLEMENT CENTER IF REQUIRED
			LCA			;SAVE CENTER OF UNIVERSE
			JMP	CNTSHOT		;GO DISPLAY A NEW UNIVERSE
INVAL:		CPI	177			;WAS INVALID SHOT A D'DELETE'?
			JFZ	NOTVAL		;IF NOT THEN RECYCLE BAD STAR;
			LLI	\LB\MESS6	;OTHER POINT TO GIVING UP
			LLI	\HB\MESS6	;MESSAGE;
			JMP	PRNTIT		;DISPLAY THEN TEST FOR RESTART
NOTVAL:		LLI	\LB\MESS2	;POINT TO INVALID STAR
			LHI	\HB\MESS2	;NUMBER MESSAGE
OUTMES:		CAL	OUTPUT		;OUTPUT A MESSAGE THEN
			JMP	SETCNT		;GO DISPLAY THE UNIVERSE AGAIN;
WINTEST:	LAB				;MOVE UNIVERSE TO A
			CPI 11111111B	;ARE ALL FRINGE STARS PRESENT?
			JFZ	LOSSTST		;IF NOT SEE IF PLAYER HAS LOST;
			LAC			;FETCH CENTER OF UNIVERSE;
			CPI	0		;IS CENTER OF UNIVERSE EMPTY
			JFZ	GOTSTAR		;IS FULL THEN NOT WIN;
			LLI	\LB\MESS4	;NO STAR! GOT A WIN FOLKS
			LHI	\HB\MESS4	;SO POINT TO WIN MESSAGE;
			CAL OUTPUT		;THEN DISPLAY WIN MESSAGE;
			LEI	060		;BEGIN BINARY TO DECIMAL CONVERSION
			LBE			;	BY SETTING ALL THREE WORKING
			LCE			;	REGISTER TO (ASCII) ZERO;

			LLI	\LB\COUNTER	;POINT TO THE ATTEMPT COUNTER
			LHI	\HB\COUNTER	;
			LDM			;FETCH IT
			DCD			;GET RID OF LAST SHOT;
			LMD			;SAVE IT
			LAD			;MOVE SHOT COUNT TO A FOR TEST;
			CPI	0		;TEST FOR ZERO (NOT NEEDED IN
			JTZ	LSTSIG		;	SHOOTING STARS BUT GENERALLY
						;	USEFUL WITH CONVERSIONS);
			LAI	072		;NEED COMPARE TO ASCII '9'+1;
MOREDEC:	INE				;COUNT UP ONE IN 1.S DIGIT;
			CPE			;IS IT EQUAL TO OVERFLOW CODE?
			JFZ	TALLYHO		;IF NOT THEN TALLY AND CONTINUE
			LEI	060		;ELSE RESET 1'S DIGIT TO ZERO
			INC			;	AND CARRY INTO NEXT DIGIT;
			CPC			;IS IT EQUAL TO OVERFLOW CODE TOO?
			JFZ	TALLYHO		;IF NOT THEN TALLY AND CONTINUE;
			LCI	060		;ELSE RESET MIDDLE DIGIT TO ZERO
			INB			;	AND CARRY INTO M.S. DIGIT;
TALLYHO:	DCD				;DECREMENT SCORE COUNTER FOR TALLY
			JFZ	MOREDEC		;IF NOT ZERO THEN KEEP OPTIONS
			LAB			;FETCH LEADING DIGIT TO A:
			CPI	060		;IS IT (ASCII) ZERO?
			JFZ	THREED		;IF NOT GO DISPLAY THREE DIGITS
			LAC			;FETCH MIDDLE DIGIT TO A;
			CPI	060		;IS IT (ASCII) ZERO TOO?
			JFZ MIDPRINT		;IF NOT GO DISPLAY TWO DIGITS
			JMP	LSTSIG		;IF SO DISPLAY ONLY ONE;
THREED:
			RST	6		;DISPLAY THREE DIGITS, LEFT FIRST;

			LAC			;FETCH MIDDLE DIGIT TO A;
MIDPRINT:
			RST	6		;DISPLAY TOW DIGITS, LEFT FIRST;

LSTSIG:		LAE				;FETCH 1'S DIGIT;
			RST	6		;DISPLAY REMAINING DIGIT;

			LLI	\LB\MESS5	;POINT TO FIRST PART OF YOU WIN;
RECYC:		LHI	\HB\MESS5		;SECOND PART OF MESS5/MESS6 POINTER;
PRNTIT:		CAL	OUTPUT			;DISPLAY THE MESSAGE;
			CAL	INPUT		;FETCH A CHARACTER FOR CONTINUE
			CPI	331		;	QUERY, IS IT 'YES"?
;			JTZ	ASTART		;IF SO THEN CONTINUE GAME;
			JMP	ASTART		;WE HAVE NO MONITOR - ALWAYS START
			
LOSSTST:	CPI	0			;IS FRINGE UNIVERSE ALL BLACK HOLES?
			JFZ	GOTSTAR		;IFNOT THEN CONTINUE GAME;
			LAC			;IF SO THEN TEST CENTER POSITION;
			CPI	0		;IS CENTER ALSO BLACK HOLE?
			JFZ	GOTSTAR		;IF NOT THEN CONTINUE GAME;
			LLI	\LB\MESS3	;ELSE POINT TO LOSS MESSAGE;
			JMP	RECYC		;AND GO PRINT LOSS;
MASKTAB:	DATA 261, 001, 013, 001 ;36 BYTES OF MASK TABLE
			DATA 262, 002, 007, 000
			DATA 263, 004, 026, 001
			DATA 264, 010, 051, 000
			DATA 265, 000, 132, 001
			DATA 266, 020, 224, 000
			DATA 267, 040, 150, 001
			DATA 270, 100, 340, 000
			DATA 271, 200, 320, 001

OUTPUT:		LAM				;FETCH NEXT MESSAGE BYTE.
			CPI	031		;IS IT A DELIMITER?
			RTZ			;RETURN WHEN DELIMITER FOUND.
			RST	6		;OTHERWISE DISPLAY BYTE;

			INL			;POINT TO NEXT BYTE;
			JFZ	OUTPUT		;IS IT PAGE BOUNDARY?
			INH			;IF SO INCREMENT PAGE;
			JMP	OUTPUT		;AND THEN RECYCLE.
INPUT:
			RST	7		;GET NEXT CHARACTER
			LEA			;SAVE IT IN E.

			RST	6		;ECHO ON DISPLAY
			
GETNEXT:
			RST	7		;GET NEXT CHARACTER;
			LCA			;SAVE IT IN E.
			RST	6		;ECH ON DISPLY;
			LAC

			CPI	CR		;WAS IT A LINE FEED?
			JFZ	GETNEXT		;IF NOT CONTINUE SCAN.
			LAE			;IF SO, RESTORE FIRST INPUT;
			RET			;AND THEN RETURN TO CALLER;
BADFELO:	LLI	\LB\MESS1		;POINT TO THE ERROR MESSAGE
			LHI	\HB\MESS1	;	ADMNISHING BAD "STAR';
			JMP	OUTMES		;AND GO DISPLAY ERROR;
			
;			ORG	016#000
MESS1:
			DATA	"YOU CAN NOT SHOOT BLACK HOLES."
			DATA	012, 015
			DATA	"TRY AGIAN!"
			DATA	012, 015, 031

;			ORG	016#077
MESS2:		DATA	"THAT WASNT A VALID STAR NUMBER!"
			DATA	012, 015
			DATA	"TRY AGAIN!"
			DATA	012, 015, 031

;			ORG	016#156
MESS3:		DATA	"YOU LOST THE GAME:"
			DATA	012, 015
			DATA	"WANT TO SHOOT SOME MORE STARS!  "
			DATA	031

;			ORG	016#243
MESS4:		DATA	"YOU WIN! GOOD SHOOTING:"
			DATA	012, 015
			DATA	"YOU FIRED  "
			DATA	031

;			ORG	016#310
MESS5:		DATA	" SHOTS."
			DATA	012, 015
			DATA	"BEST POSSIBLE SCORE IS 11 SHOTS"
			DATA	012, 015
			DATA	"WANT TO SHOOT AGAIN?  "
			DATA	031

;			ORG	017#022
MESS6:		DATA	"YOU GIVE UP TOO EASILY!"
			DATA	012, 015
			DATA	"WANT TO SHOOT SOME MORE STARS? "
			DATA	031

;			ORG	017#114
MESS7:		DATA	"YOUR SHOT? "
			DATA	031

;			ORG	03#000
HMESS:		
			DATA	012, 015
			DATA	"**********S H O O T I N G   S T A R S******"
			DATA	012, 015
			DATA	"           A BRAIN TEASER GAME"
			DATA	012, 015
			DATA	"PRESS ENTER TO PLAY!"
			DATA	031

COUNTER:	DATA	0


;;; HERE IS THE USER DEFINED CHARACTER INPUT TO READ FROM SERIAL PORT
; Returns Character in A,  destroys B, C, and D
; WAIT for start bit
INPORT: EQU 5
OUTPORT: EQU 016
;INCHAR:  ORG     007#200                ;ASCII input routine
INCHAR:
        INP INPORT
        NDA                     ; 5
        JTS INCHAR              ; 9/11
; found - delay 1 1/2 bit times
        LEI     7               ; 8
TIMER:                          ;
        DCE                     ; 5
        JFZ TIMER               ; 9/11

;grab 8 bits
	CAL	DATAIN
	CAL	DATAIN
	CAL	DATAIN
	CAL	DATAIN
	CAL	DATAIN
	CAL	DATAIN
	CAL	DATAIN
	CAL	DATAIN
        LEI     15               ; 8 delay for stop bit
TIMER2:                          ;
        DCE                     ; 5
        JFZ TIMER2               ; 9/11
;
; result goes to a and return
;
        LAD                   ;
        ORI 200               ; set MSB
        RET


; get 1 bit
; MSB of port contains input stream
; read and move to carry
; shift into MS bit position
; and maintain in B register
DATAIN:
        INP INPORT              ; 8
        RAL                     ; 5
        LAD                     ; 5
        RAR                     ; 5
        LDA                     ; 5

; delay 1 bit time
        LEI     3               ; 8
TIMER3:                       ;
        DCE                     ; 5
        JFZ TIMER3            ; 9/11
	LAA
	RET

;OUTCHAR:   ORG     007#300         ;ASCII Output routine
OUTCHAR:
        NDI     177             ; set startbit
        RAL
        OUT     OUTPORT
        RAR
        LAA
        LAA
        LDI     2
        CAL     MORE2
        CAL     BITOUT          ; LSB
        CAL     BITOUT          ; 1
        CAL     BITOUT          ; 2
        CAL     BITOUT          ; 3
        CAL     BITOUT          ; 4
        CAL     BITOUT          ; 5
        CAL     BITOUT          ; 6
        CAL     BITOUT          ; MSB
        LAD
        LAI     001
        OUT     OUTPORT         ; stop bit
        LDI     100
        JMP     MORE2

BITOUT:
        OUT     OUTPORT
        RRC                     ; shift to get next bit
        LDI     3
MORE2:
        LAA                     ; nop to delay 5 states to make timing work
        DCD
        JFZ     MORE2
        LDI     0               ; delay 8 states
        RET
