;*****************
;* Program Tests *
;*****************

; This is a set of programming tests. 

; July 1, 2003

; Lance F. Squire 2002

INCLUDE HVGLIB.H

BALMNU	EQU	0218H		;LINK POINT TO BALLY
				;STANDARD MENU.
B1VEC	EQU	4F00H		;VECTOR BLOCK FOR BALL TEST
B2VEC	EQU	B1VEC+VBOAH+1	;VECTOR BLOCKS FOR MULTI TEST
B3VEC	EQU	B2VEC+VBOAH+1
C1VEC	EQU	B3VEC+VBOAH+1

	ORG	2000H		;Start of Cart memory

	DEFB	55H		;NORMAL (MENUED) CART 'SENTENAL'
	DEFW	NEXT		;NEXT NODE IN MENU CHAIN
	DEFW	LABEL		;ADDRESS OF MENU TEXT
	DEFW	PROG1		;WHERE TO GO IF SELECTED.
NEXT	DEFW	NEXT2		
	DEFW	LABEL2		;MENU TEXT
	DEFW	PROG2
NEXT2	DEFW	BALMNU		;(START OF ONBOARD MENU)
	DEFW	LABEL3
	DEFW	SCRTST

LABEL	DEFB	'BALL TEST',0	;NULL DELIMITED STRINGS
LABEL2	DEFB	'3 BALL TEST',0
LABEL3	DEFB	'SCROLL TEST',0

;*****************
;* Bouncing ball *
;*****************

PROG1	SYSSUK	FILL		;CLEAR SCREEN
	DEFW	4000H		;START OF SCREEN RAM
	DEFW	95*40		;95 LINES (40 BYTES)
	DEFB	0		;ZERO VALUE

	LD	IX,B1VEC	;BEGIN SETUP OF VECTOR BLOCK
	LD	(IX+VBMR),8	;MAGIC: EXPAND
	LD	(IX+VBSTAT),192	;STATUS: ACTIVE+BLANKING
	LD	(IX+VBTIMB),1	;TIMES: 1 (NUMBER OF TIMES DELTA IS ADDED TO POSITION)
	LD	(IX+VBDXL),0	;DELTA (DIRECTION)X LOW ORDER BYTE (FRACTIONS)
	LD	(IX+VBDXH),1	;1 PIXEL PER CALL MOTION (RIGHT)
	LD	(IX+VBXL),0	;INITIAL POSITION LOW ORDER BYTE (FRACTIONS)
	LD	(IX+VBXH),0	;ACTUAL X POSITION ON SCREEN
	LD	(IX+VBXCHK),3	;X LIMIT CHECKING: ON + REVERSING (BOUNCE)
	LD	(IX+VBDYL),0	;DELTA (DIRECTION)Y LOW ORDER BYTE (FRACTION)
	LD	(IX+VBDYH),1	;1 PIXEL PER CALL MOTION (DOWN)
	LD	(IX+VBYL),0	;INITIAL POSSITION LOW ORDER BYTE (FRACTION)
	LD	(IX+VBYH),0	;ACTUAL Y POSITION ON SCREEN
	LD	(IX+VBYCHK),3	;Y LIMIT CHECKING: ON + REVERSING (BOUNCE)
	LD	(IX+VBOAL),0	;OLD ADDRESS: CLEARED FOR NOW
	LD	(IX+VBOAH),0	;END VECTOR BLOCK SETUP

	LD	A,0CH		;COLOURS TO EXPAND TO
	OUT	(XPAND),A	;INTO EXPANSION PORT

LOOP	SYSSUK	VWRITR		;DRAW BALL
	DEFW	B1VEC		;VECTOR BLOCK TO USE
	DEFW	BALL1		;IMAGE TO DRAW

	LD	IX,B1VEC
	LD	(IX+VBOAL),E	;COPY SCREEN ADDRESS TO VECTORBLOCK?
	LD	(IX+VBOAH),D	;

	SYSSUK	PAWS		;PAUSE FOR
	DEFB	1		;ONE CYCLE

	SYSSUK	VBLANK		;ERASE BALL
	DEFW	B1VEC		;VECTOR BLOCK
	DEFB	2		;X SIZE IN BYTES
	DEFB	6		;Y SIZE

	SYSSUK	VECT		;MOVE BALL
	DEFW	B1VEC		;VECTOR BLOCK TO USE
	DEFW	BLMT		;LIMITS TABLE TO USE

	LD	IX,B1VEC
	LD	(IX+VBTIMB),1	;RE ACTIVATE?
	JR	LOOP		;BACK TO DRAW BALL

;*********************
;* Bouncing ball 3 +1*
;*********************

PROG2	SYSSUK	FILL		;CLEAR SCREEN
	DEFW	4000H		;START OF SCREEN RAM
	DEFW	95*40		;95 LINES (40 BYTES)
	DEFB	0		;ZERO VALUE

	LD	IX,B1VEC	;BEGIN SETUP OF VECTOR BLOCK
	LD	(IX+VBMR),40	;MAGIC: EXPAND+XOR
	LD	(IX+VBSTAT),192	;STATUS: ACTIVE+BLANKING
	LD	(IX+VBTIMB),1	;TIMES: 1
	LD	(IX+VBDXL),0	;DELTA (DIRECTION)X LOW ORDER BYTE (FRACTIONS)
	LD	(IX+VBDXH),1	;1 PIXEL PER CALL MOTION (RIGHT)
	LD	(IX+VBXL),0	;INITIAL POSITION LOW ORDER BYTE (FRACTIONS)
	LD	(IX+VBXH),0	;ACTUAL X POSITION ON SCREEN
	LD	(IX+VBXCHK),3	;X LIMIT CHECKING: ON + REVERSING (BOUNCE)
	LD	(IX+VBDYL),0	;DELTA (DIRECTION)Y LOW ORDER BYTE (FRACTION)
	LD	(IX+VBDYH),1	;1 PIXEL PER CALL MOTION (DOWN)
	LD	(IX+VBYL),0	;INITIAL POSSITION LOW ORDER BYTE (FRACTION)
	LD	(IX+VBYH),0	;ACTUAL Y POSITION ON SCREEN
	LD	(IX+VBYCHK),3	;Y LIMIT CHECKING: ON + REVERSING (BOUNCE)
	LD	(IX+VBOAL),0	;OLD ADDRESS: CLEARED FOR NOW
	LD	(IX+VBOAH),0	;END VECTOR BLOCK SETUP

	LD	IX,B2VEC	;BEGIN SETUP OF VECTOR BLOCK
	LD	(IX+VBMR),40	;MAGIC: EXPAND+XOR
	LD	(IX+VBSTAT),192	;STATUS: ACTIVE+BLANKING
	LD	(IX+VBTIMB),1	;TIMES: 1
	LD	(IX+VBDXL),128	;DELTA (DIRECTION)X LOW ORDER BYTE (FRACTIONS)
	LD	(IX+VBDXH),0	;.5? PIXEL PER CALL MOTION (RIGHT)
	LD	(IX+VBXL),0	;INITIAL POSITION LOW ORDER BYTE (FRACTIONS)
	LD	(IX+VBXH),0	;ACTUAL X POSITION ON SCREEN
	LD	(IX+VBXCHK),3	;X LIMIT CHECKING: ON + REVERSING (BOUNCE)
	LD	(IX+VBDYL),192	;DELTA (DIRECTION)Y LOW ORDER BYTE (FRACTION)
	LD	(IX+VBDYH),1	;1.75? PIXEL PER CALL MOTION (DOWN)
	LD	(IX+VBYL),0	;INITIAL POSSITION LOW ORDER BYTE (FRACTION)
	LD	(IX+VBYH),0	;ACTUAL Y POSITION ON SCREEN
	LD	(IX+VBYCHK),3	;Y LIMIT CHECKING: ON + REVERSING (BOUNCE)
	LD	(IX+VBOAL),0	;OLD ADDRESS: CLEARED FOR NOW
	LD	(IX+VBOAH),0	;END VECTOR BLOCK SETUP

	LD	IX,B3VEC	;BEGIN SETUP OF VECTOR BLOCK
	LD	(IX+VBMR),40	;MAGIC: EXPAND+XOR
	LD	(IX+VBSTAT),192	;STATUS: ACTIVE+BLANKING
	LD	(IX+VBTIMB),1	;TIMES: 1
	LD	(IX+VBDXL),0	;DELTA (DIRECTION)X LOW ORDER BYTE (FRACTIONS)
	LD	(IX+VBDXH),2	;2 PIXEL PER CALL MOTION (RIGHT)
	LD	(IX+VBXL),0	;INITIAL POSITION LOW ORDER BYTE (FRACTIONS)
	LD	(IX+VBXH),0	;ACTUAL X POSITION ON SCREEN
	LD	(IX+VBXCHK),3	;X LIMIT CHECKING: ON + REVERSING (BOUNCE)
	LD	(IX+VBDYL),128	;DELTA (DIRECTION)Y LOW ORDER BYTE (FRACTION)
	LD	(IX+VBDYH),1	;1.5? PIXEL PER CALL MOTION (DOWN)
	LD	(IX+VBYL),0	;INITIAL POSSITION LOW ORDER BYTE (FRACTION)
	LD	(IX+VBYH),0	;ACTUAL Y POSITION ON SCREEN
	LD	(IX+VBYCHK),3	;Y LIMIT CHECKING: ON + REVERSING (BOUNCE)
	LD	(IX+VBOAL),0	;OLD ADDRESS: CLEARED FOR NOW
	LD	(IX+VBOAH),0	;END VECTOR BLOCK SETUP

	LD	IX,C1VEC	;BEGIN SETUP OF VECTOR BLOCK
	LD	(IX+VBMR),32	;MAGIC: XOR
	LD	(IX+VBSTAT),192	;STATUS: ACTIVE+BLANKING
	LD	(IX+VBTIMB),1	;TIMES: 1
	LD	(IX+VBDXL),182	;.5 X MOTION
	LD	(IX+VBDXH),0	;
	LD	(IX+VBXL),0	;INITIAL POSITION LOW ORDER BYTE (FRACTIONS)
	LD	(IX+VBXH),0	;ACTUAL X POSITION ON SCREEN
	LD	(IX+VBXCHK),3	;X LIMIT CHECKING: ON + REVERSING (BOUNCE)
	LD	(IX+VBDYL),128	;.5 Y MOTION
	LD	(IX+VBDYH),0	;
	LD	(IX+VBYL),0	;INITIAL POSSITION LOW ORDER BYTE (FRACTION)
	LD	(IX+VBYH),0	;ACTUAL Y POSITION ON SCREEN
	LD	(IX+VBYCHK),3	;Y LIMIT CHECKING: ON + REVERSING (BOUNCE)
	LD	(IX+VBOAL),0	;OLD ADDRESS: CLEARED FOR NOW
	LD	(IX+VBOAH),0	;END VECTOR BLOCK SETUP

;Initialize

	LD	A,0CH		;COLOURS TO EXPAND TO
	OUT	(XPAND),A	;INTO EXPANSION PORT

	SYSSUK	VWRITR		;DRAW BALL
	DEFW	B1VEC		;VECTOR BLOCK TO USE
	DEFW	BALL1		;IMAGE TO DRAW

	LD	A,08H		;COLOURS TO EXPAND TO
	OUT	(XPAND),A	;INTO EXPANSION PORT

	SYSSUK	VWRITR		;DRAW BALL
	DEFW	B2VEC		;VECTOR BLOCK TO USE
	DEFW	BALL1		;IMAGE TO DRAW

	LD	A,04H		;COLOURS TO EXPAND TO
	OUT	(XPAND),A	;INTO EXPANSION PORT

	SYSSUK	VWRITR		;DRAW BALL
	DEFW	B3VEC		;VECTOR BLOCK TO USE
	DEFW	BALL1		;IMAGE TO DRAW

	SYSSUK	VWRITR		;DRAW CRITTER
	DEFW	C1VEC		;VECTOR BLOCK TO USE
	DEFW	CRIT1		;IMAGE TO DRAW

LOOP2	LD	A,0CH		;COLOURS TO EXPAND TO
	OUT	(XPAND),A	;INTO EXPANSION PORT

	SYSSUK	VWRITR		;ERASE BALL 1
	DEFW	B1VEC		;VECTOR BLOCK TO USE
	DEFW	BALL1		;IMAGE TO DRAW

	SYSSUK	VECT		;MOVE BALL 1
	DEFW	B1VEC		;VECTOR BLOCK TO USE
	DEFW	BLMT		;LIMITS TABLE TO USE

;	LD	A,0CH		;COLOURS TO EXPAND TO
;	OUT	(XPAND),A	;INTO EXPANSION PORT

	SYSSUK	VWRITR		;DRAW BALL 1
	DEFW	B1VEC		;VECTOR BLOCK TO USE
	DEFW	BALL1		;IMAGE TO DRAW

	LD	A,08H		;COLOURS TO EXPAND TO
	OUT	(XPAND),A	;INTO EXPANSION PORT

	SYSSUK	VWRITR		;ERASE BALL 2
	DEFW	B2VEC		;VECTOR BLOCK TO USE
	DEFW	BALL1		;IMAGE TO DRAW

	SYSSUK	VECT		;MOVE BALL 2
	DEFW	B2VEC		;VECTOR BLOCK TO USE
	DEFW	BLMT		;LIMITS TABLE TO USE

;	LD	A,08H		;COLOURS TO EXPAND TO
;	OUT	(XPAND),A	;INTO EXPANSION PORT

	SYSSUK	VWRITR		;DRAW BALL 2
	DEFW	B2VEC		;VECTOR BLOCK TO USE
	DEFW	BALL1		;IMAGE TO DRAW

	LD	A,04H		;COLOURS TO EXPAND TO
	OUT	(XPAND),A	;INTO EXPANSION PORT

	SYSSUK	VWRITR		;ERASE BALL 3
	DEFW	B3VEC		;VECTOR BLOCK TO USE
	DEFW	BALL1		;IMAGE TO DRAW

	SYSSUK	VECT		;MOVE BALL 3
	DEFW	B3VEC		;VECTOR BLOCK TO USE
	DEFW	BLMT		;LIMITS TABLE TO USE

;	LD	A,04H		;COLOURS TO EXPAND TO
;	OUT	(XPAND),A	;INTO EXPANSION PORT

	SYSSUK	VWRITR		;DRAW BALL 3
	DEFW	B3VEC		;VECTOR BLOCK TO USE
	DEFW	BALL1		;IMAGE TO DRAW

	SYSSUK	VWRITR		;ERASE CRITTER
	DEFW	C1VEC		;VECTOR BLOCK TO USE
	DEFW	CRIT1		;IMAGE TO DRAW

	SYSSUK	VECT		;MOVE CRITTER
	DEFW	C1VEC		;VECTOR BLOCK TO USE
	DEFW	CLMT		;LIMITS TABLE TO USE

	SYSSUK	VWRITR		;DRAW CRITTER
	DEFW	C1VEC		;VECTOR BLOCK TO USE
	DEFW	CRIT1		;IMAGE TO DRAW

	SYSSUK	PAWS		;PAUSE FOR
	DEFB	1		;ONE CYCLE

	LD	IX,B1VEC
	LD	(IX+VBTIMB),1	;RE ACTIVATE??

	LD	IX,B2VEC
	LD	(IX+VBTIMB),1	;RE ACTIVATE??

	LD	IX,B3VEC
	LD	(IX+VBTIMB),1	;RE ACTIVATE??

	IN    A,(1CH)     ;Get KN(1) Value
	LD    IX,C1VEC    ; Beginning of Vector Block
	LD    (IX+VBTIMB),A    ; Speed of ball is KN(1)

;	LD	IX,C1VEC
;	LD	(IX+VBTIMB),1	;RE ACTIVATE??

	JP	LOOP2		;BACK TO DRAW BALL

;********************
;* BALL IMAGE BLOCK *
;********************

BALL1	DEFB	0	;NO X OFFSET
	DEFB	0	;NO Y OFFSET
	DEFB	1	;1 BYTE WIDE
	DEFB	6	;4 BYTES TALL
	DEFB	01111000B	
	DEFB	11011100B
	DEFB	10111100B	
	DEFB	10111100B
	DEFB	11111100B
	DEFB	01111000B

;***************
;* BALL LIMITS *
;***************

BLMT	DEFB	0	;X LOWER LIMIT (LEFT EDGE)
	DEFB	153	;X UPPER LIMIT (RIGHT EDGE)
			;-8 FOR 8 PIXEL WIDE BALL
	DEFB	0	;Y LOWER LIMIT (TOP EDGE)
	DEFB	89	;Y UPPER LIMIT (BOTTOM EDGE)
			;-6 FOR 6 PIXEL HIGH BALL
;***********
;* CRITTER *
;***********

CRIT1	DEFB	0	;NO X/Y OFFSET
	DEFB	0
	DEFB	2	;2 BYTES WIDE
	DEFB	8	;8 BYTES HIGH
	DEFW	0A00AH
	DEFW	9826H	;3 COLOURS
	DEFW	0AAAAH
	DEFW	0A82AH
	DEFW	2008H
	DEFW	0820H
	DEFW	2008H
	DEFW	0000H

;******************
;* CRITTER LIMITS *
;******************

CLMT	DEFB	0,152	;X LIMITS
	DEFB	0,88	;Y LIMITS

;***************
;* Color Table *
;***************

CLRTAB: DEFB     0F2H	; Color 3 Left  - BLUE WALLS
        DEFB     0A5H	; Color 2 Left  - GREEN MAN
        DEFB     7FH	; Color 1 Left  - YELLOW ROBOTS
        DEFB     00H	; Color 0 Left  - BLACK
        DEFB     00H	; Color 3 Right - Black
        DEFB     5AH	; Color 2 Right - Red
        DEFB     77H	; Color 1 Right - Yellow
        DEFB     72H	; Color 0 Right - Brown

;***************
;* Running man *
;***************

RMAN	DEFB	00011000B
	DEFB	00011000B
	DEFB	00000000B
	DEFB	00111100B
	DEFB	01011100B
	DEFB	01011100B
	DEFB	00111110B
	DEFB	00011000B
	DEFB	00011000B
	DEFB	00010100B
	DEFB	00010010B
	DEFB	11110010B
	DEFB	10000010B
	DEFB	00000010B
	DEFB	00000001B
	
;******************
;* Robot Standing *
;******************

RBS	DEFB	00111100B
	DEFB	01100110B
	DEFB	11111111B
	DEFB	10111101B
	DEFB	10111101B
	DEFB	10111101B
	DEFB	00111100B
	DEFB	00100100B
	DEFB	00100100B
	DEFB	00100100B
	DEFB	01100110B

;Table of room cells
	
TABLE	DEFB	4,9
	DEFB	34,9
	DEFB	64,9
	DEFB	94,9
	DEFB	124,9
	DEFB	4,38
	DEFB	34,38
	DEFB	64,38
	DEFB	94,38
	DEFB	124,38
	DEFB	4,67
	DEFB	34,67
	DEFB	64,67
	DEFB	94,67
	DEFB	124,67
	
; Done message

DONE	DEFB	'DONE!',0
	
;*********************
;* Scroll Right test *
;*********************
;
; Set-up screen

SCRTST	SYSSUK	FILL		; CLEAR SCREEN
	DEFW	4000H
	DEFW	96*40
	DEFB	0

	SYSSUK	COLSET		; SET ALL SCREEN COLOURS
	DEFW	CLRTAB		; USING THIS TABLE.
	
;
; DRAW PLAYER SCORE BLOCK
;
	SYSSUK	CHRDIS		;DISPLAY CHARACTER
	DEFB	0		;X
	DEFB	0		;Y
	DEFB	00001000B	;GREEN ON BLACK
	DEFB	49		;PLAYER 1
	
	LD	A,099h
	LD	(B2VEC),A
	LD	A,10h
	LD	(B2VEC+1),A
	
	LD	IX,FNTSML	;USE SMALL FONT
	SYSSUK	DISNUM		;DISPLAY SCORE
	DEFB	8		;X
	DEFB	1		;Y
	DEFB	8		;GREEN ON BLACK
	DEFB	128+64+4	;0 SUPPRESS+SMALL FONT+4 DIGITS
	DEFW	B2VEC		;ADDRESS OF SCORE
	
;
; DRAW FIELD
;

ROOM	SYSSUK	RECTAN		; DRAW LEFT WALL TOP
	DEFB	4		; X
	DEFB	9		; Y
	DEFB	1		; WIDTH
	DEFB	29		; HEIGHT
	DEFB	255		; BLUE x 4 PIXELS
	
	SYSSUK	RECTAN		; DRAW LEFT WALL BOTTOM
	DEFB	4		; X
	DEFB	67		; Y
	DEFB	1		; WIDTH
	DEFB	29		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS
	
	SYSSUK	RECTAN		; DRAW TOP WALL LEFT
	DEFB	4		; X
	DEFB	9		; Y
	DEFB	60		; WIDTH
	DEFB	1		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS
	
	SYSSUK	RECTAN		; DRAW TOP WALL RIGHT
	DEFB	94		; X
	DEFB	9		; Y
	DEFB	60		; WIDTH
	DEFB	1		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS	
	
	SYSSUK	RECTAN		; DRAW RIGHT WALL TOP
	DEFB	154		; X
	DEFB	9		; Y
	DEFB	1		; WIDTH
	DEFB	29		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS
	
	SYSSUK	RECTAN		; DRAW RIGHT WALL BOTTOM
	DEFB	154		; X
	DEFB	67		; Y
	DEFB	1		; WIDTH
	DEFB	29		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS
	
	SYSSUK	RECTAN		; DRAW BOTTOM WALL LEFT
	DEFB	4		; X
	DEFB	95		; Y
	DEFB	60		; WIDTH
	DEFB	1		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS
	
	SYSSUK	RECTAN		; DRAW BOTTOM WALL RIGHT
	DEFB	94		; X
	DEFB	95		; Y
	DEFB	60		; WIDTH
	DEFB	1		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS
	
;***********************************
;* Now that Room borders are drawn *
;* Create random walls             *
;***********************************

; ROW 1	

	SYSSUK	RANGED		; GET RANDOM NUMBER
	DEFB	(0)		; 0-255
	
	LD	(B1VEC),A	; STORE TEMPORARILY IN RAM
	
	BIT	0,A		; IS THE FIRST BIT SET?
	JP	Z,WAL2		; IF NOT DON'T DRAW WALL
	
	SYSSUK	RECTAN		; DRAW TOP VERTICAL WALL 1 OF 4
	DEFB	34		; X
	DEFB	9		; Y
	DEFB	1		; WIDTH
	DEFB	29		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS

	LD	A,(B1VEC)	; RESTORE A (RECTAN KILLS IT)
	
WAL2	BIT	1,A		; IF SECOND BIT NOT SET...
	JP	Z,WAL3		; DON'T DRAW WALL
	
	SYSSUK	RECTAN		; DRAW TOP VERTICAL WALL 2 OF 4
	DEFB	64		; X
	DEFB	9		; Y
	DEFB	1		; WIDTH
	DEFB	29		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS

	LD	A,(B1VEC)	; RESTORE A
	
WAL3	BIT	2,A		; IF THIRD BIT NOT SET...
	JP	Z,WAL4		; DON'T DRAW WALL
	
	SYSSUK	RECTAN		; DRAW TOP VERTICAL WALL 3 OF 4
	DEFB	94		; X
	DEFB	9		; Y
	DEFB	1		; WIDTH
	DEFB	29		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS

	LD	A,(B1VEC)	; RESTORE A
	
WAL4	BIT	3,A		; IF FORTH BIT NOT SET...
	JP	Z,WAL5		; DON'T DRAW WALL
	
	SYSSUK	RECTAN		; DRAW TOP VERTICAL WALL 4 OF 4
	DEFB	124		; X
	DEFB	9		; Y
	DEFB	1		; WIDTH
	DEFB	29		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS

	LD	A,(B1VEC)	; RESTORE A
	
WAL5	BIT	4,A		; IF FIFTH BIT NOT SET...
	JP	Z,WAL6		; DON'T DRAW WALL
	
	SYSSUK	RECTAN		; DRAW TOP HORIZONTAL WALL 1 OF 5
	DEFB	4		; X
	DEFB	38		; Y
	DEFB	30		; WIDTH
	DEFB	1		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS

	LD	A,(B1VEC)	; RESTORE A

WAL6	BIT	5,A		; IF SIXTH BIT NOT SET...
	JP	Z,WAL7		; DON'T DRAW WALL
	
	SYSSUK	RECTAN		; DRAW TOP HORIZONTAL WALL 2 OF 5
	DEFB	34		; X
	DEFB	38		; Y
	DEFB	30		; WIDTH
	DEFB	1		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS

	LD	A,(B1VEC)	; RESTORE A

WAL7	BIT	6,A		; IF SEVENTH BIT NOT SET...
	JP	Z,WAL8		; DON'T DRAW WALL
	
	SYSSUK	RECTAN		; DRAW TOP HORIZONTAL WALL 3 OF 5
	DEFB	64		; X
	DEFB	38		; Y
	DEFB	30		; WIDTH
	DEFB	1		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS

	LD	A,(B1VEC)	; RESTORE A
	
WAL8	BIT	7,A		; IF 8TH BIT NOT SET...
	JP	Z,R2W1		; DON'T DRAW WALL
	
	SYSSUK	RECTAN		; DRAW TOP HORIZONTAL WALL 2 OF 5
	DEFB	94		; X
	DEFB	38		; Y
	DEFB	30		; WIDTH
	DEFB	1		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS

	LD	A,(B1VEC)	; RESTORE A

; ROW 2

R2W1	SYSSUK	RANGED		; GET RANDOM NUMBER
	DEFB	(0)		; 0-255
	
	LD	(B1VEC),A	; STORE TEMPORARILY IN RAM
	
	BIT	0,A		; IS THE FIRST BIT SET?
	JP	Z,R2W2		; IF NOT DON'T DRAW WALL
	
	SYSSUK	RECTAN		; DRAW MIDDLE VERTICAL WALL 1 OF 4
	DEFB	34		; X
	DEFB	38		; Y
	DEFB	1		; WIDTH
	DEFB	29		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS

	LD	A,(B1VEC)	; RESTORE A (RECTAN KILLS IT)
	
R2W2	BIT	1,A		; IF SECOND BIT NOT SET...
	JP	Z,R2W3		; DON'T DRAW WALL
	
	SYSSUK	RECTAN		; DRAW TOP MIDDLE WALL 2 OF 4
	DEFB	64		; X
	DEFB	38		; Y
	DEFB	1		; WIDTH
	DEFB	29		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS

	LD	A,(B1VEC)	; RESTORE A
	
R2W3	BIT	2,A		; IF THIRD BIT NOT SET...
	JP	Z,R2W4		; DON'T DRAW WALL
	
	SYSSUK	RECTAN		; DRAW TOP MIDDLE WALL 3 OF 4
	DEFB	94		; X
	DEFB	38		; Y
	DEFB	1		; WIDTH
	DEFB	29		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS

	LD	A,(B1VEC)	; RESTORE A
	
R2W4	BIT	3,A		; IF FORTH BIT NOT SET...
	JP	Z,R2W5		; DON'T DRAW WALL
	
	SYSSUK	RECTAN		; DRAW TOP MIDDLE WALL 4 OF 4
	DEFB	124		; X
	DEFB	38		; Y
	DEFB	1		; WIDTH
	DEFB	29		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS

	LD	A,(B1VEC)	; RESTORE A
	
R2W5	BIT	4,A		; IF FIFTH BIT NOT SET...
	JP	Z,R2W6		; DON'T DRAW WALL
	
	SYSSUK	RECTAN		; DRAW LOWER HORIZONTAL WALL 1 OF 5
	DEFB	4		; X
	DEFB	67		; Y
	DEFB	30		; WIDTH
	DEFB	1		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS

	LD	A,(B1VEC)	; RESTORE A

R2W6	BIT	5,A		; IF SIXTH BIT NOT SET...
	JP	Z,R2W7		; DON'T DRAW WALL
	
	SYSSUK	RECTAN		; DRAW LOWER HORIZONTAL WALL 2 OF 5
	DEFB	34		; X
	DEFB	67		; Y
	DEFB	30		; WIDTH
	DEFB	1		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS

	LD	A,(B1VEC)	; RESTORE A

R2W7	BIT	6,A		; IF SEVENTH BIT NOT SET...
	JP	Z,R2W8		; DON'T DRAW WALL
	
	SYSSUK	RECTAN		; DRAW LOWER HORIZONTAL WALL 3 OF 5
	DEFB	64		; X
	DEFB	67		; Y
	DEFB	30		; WIDTH
	DEFB	1		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS

	LD	A,(B1VEC)	; RESTORE A
	
R2W8	BIT	7,A		; IF 8TH BIT NOT SET...
	JP	Z,R3W1		; DON'T DRAW WALL
	
	SYSSUK	RECTAN		; DRAW LOWER HORIZONTAL WALL 2 OF 5
	DEFB	94		; X
	DEFB	67		; Y
	DEFB	30		; WIDTH
	DEFB	1		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS

	LD	A,(B1VEC)	; RESTORE A

; ROW 3

R3W1	SYSSUK	RANGED		; GET RANDOM NUMBER
	DEFB	(0)		; 0-255
	
	LD	(B1VEC),A	; STORE TEMPORARILY IN RAM
	
	BIT	0,A		; IS THE FIRST BIT SET?
	JP	Z,R3W2		; IF NOT DON'T DRAW WALL
	
	SYSSUK	RECTAN		; DRAW BOTTOM VERTICAL WALL 1 OF 4
	DEFB	34		; X
	DEFB	67		; Y
	DEFB	1		; WIDTH
	DEFB	29		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS

	LD	A,(B1VEC)	; RESTORE A (RECTAN KILLS IT)
	
R3W2	BIT	1,A		; IF SECOND BIT NOT SET...
	JP	Z,R3W3		; DON'T DRAW WALL
	
	SYSSUK	RECTAN		; DRAW BOTTOM WALL 2 OF 4
	DEFB	64		; X
	DEFB	67		; Y
	DEFB	1		; WIDTH
	DEFB	29		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS

	LD	A,(B1VEC)	; RESTORE A
	
R3W3	BIT	2,A		; IF THIRD BIT NOT SET...
	JP	Z,R3W4		; DON'T DRAW WALL
	
	SYSSUK	RECTAN		; DRAW BOTTOM WALL 3 OF 4
	DEFB	94		; X
	DEFB	67		; Y
	DEFB	1		; WIDTH
	DEFB	29		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS

	LD	A,(B1VEC)	; RESTORE A
	
R3W4	BIT	3,A		; IF FORTH BIT NOT SET...
	JP	Z,R3W5		; DON'T DRAW WALL
	
	SYSSUK	RECTAN		; DRAW BOTTOM WALL 4 OF 4
	DEFB	124		; X
	DEFB	67		; Y
	DEFB	1		; WIDTH
	DEFB	29		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS

	LD	A,(B1VEC)	; RESTORE A
	
R3W5	BIT	4,A		; IF FIFTH BIT NOT SET...
	JP	Z,R3W6		; DON'T DRAW WALL
	
	SYSSUK	RECTAN		; DRAW TOP LEFT HORIZONTAL WALL 1 OF 2
	DEFB	124		; X
	DEFB	38		; Y
	DEFB	30		; WIDTH
	DEFB	1		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS

	LD	A,(B1VEC)	; RESTORE A

R3W6	BIT	5,A		; IF SIXTH BIT NOT SET...
	JP	Z,DOOR		; DON'T DRAW WALL
	
	SYSSUK	RECTAN		; DRAW LOWER LEFT HORIZONTAL WALL 2 OF 2
	DEFB	124		; X
	DEFB	67		; Y
	DEFB	30		; WIDTH
	DEFB	1		; HEIGHT
	DEFB	255		; BLUE X 4 PIXELS

	LD	A,(B1VEC)	; RESTORE A

DOOR	;As this is a scroll demo, there is no door to draw.

;
;Draw Player
;

DPLYR	SYSSUK	WRIT		;DRAW HERE..
	DEFB	148		;X (4 FLOPPED)
	DEFB	43		;Y
	DEFB	1		;PATTERN WIDTH
	DEFB	15		;PATTERN HEIGHT
	DEFB	8+64		;MAGIC EXPAND+FLOP
	DEFW	RMAN		;PATTERN

;
;Draw Robots
;

	LD	A,6		;5 iterations
RLOOP	DEC	A
	JP	Z,SSCRL
	PUSH	AF
	
	SYSSUK	RANGED		;Rand(30-RobotWidth)
	DEFB	21
	
	LD	(B3VEC),A	;Store X offset
	
	SYSSUK	RANGED		;Rand(29-RobotHeight)
	DEFB	18
	
	LD	(B3VEC+1),A	;Store Y offset
	
AGAIN	SYSSUK	RANGED		;Rand(Room Box)
	DEFB	15
	
	CP	5		;Can't be in box Man is.
	JP	Z,AGAIN		;Pick new location
	
	SLA	A		;x2
	
	LD	C,A
	LD	B,0
	
	LD	IX,TABLE	;Room Block X,Y Table
	
	ADD	IX,BC		;Adjust offset
			
	LD	E,(IX)		;Get Block X
	LD	D,(IX+1)	;Get Block Y
	
	LD	A,(B3VEC)	;Get X offset
	ADD	A,E		;Calculate final X
	LD	E,A		;Ready for SYSTEM WRIT
	
	LD	A,(B3VEC+1)	;Get Y offset
	ADD	A,D		;Calculate final Y
	LD	D,A		;Ready for SYSTEM WRIT
	
	LD	A,4		;COLOURS TO EXPAND TO
	OUT	(XPAND),A	;INTO EXPANSION PORT

	
	LD	A,8+16		;MAGIC EXPAND+OR
	LD	HL,RBS		;Robot Pattern
	LD	B,11		;Robot Height
	LD	C,1		;Robot Width (Bytes)
	SYSTEM	WRIT		;Draw Robot
	
	POP	AF		;Get loop counter
	JP	RLOOP		;Next
	
;*********************************
;* Code to scroll the screen one *
;* pixel right                   *
;*********************************
SSCRL	SYSSUK	PAWS
	DEFB	60

	LD	A,159		;For x=0 to 159
SLOOP	PUSH	AF		;Writ uses A
	
	SYSSUK	WRIT		;WRITE PATTERN TO MAGIC RAM
	DEFB	1		;X CO-ORDINATE ON SCREEN
	DEFB	9		;Y
	DEFB	40		;X PATTERN SIZE (BYTES)
	DEFB	88		;Y PATTERN SIZE
	DEFB	0		;MAGIC: PLOP (STRAIGHT DRAW)
	DEFW	4168H		;SCREEN RAM
	
;	SYSSUK	PAWS		;PAUSE FOR
;	DEFB	2		;ONE CYCLE
	
	POP	AF
	DEC	A		;Next x
	
	JR	NZ,SLOOP	;LOOP
	
	SYSSUK	STRDIS		;Anounce compleation
	DEFB	70		;X
	DEFB	48		;Y
	DEFB	4		;01 ON 00 OFF
	DEFW	DONE

	SYSSUK	PAWS		;PAUSE FOR
	DEFB	30		;30 CYCLE
	
