
	ORG &4009


                
; System variables live here
	db 0			;4009   VERSN   First System Variabl to be saved
	dw 1			;400A   E_PPC   Line Number of line with cursor
	dw VRAM			;400C   D_FILE  Address of start of display file (VRAM)
	dw VRAM+1  		;400E   DF_CC   Address of PRINT position within display file 
	dw BasicVars	;4010   VARS    Address of start of variables area
	dw 0			;4012   DEST    Address of variable being assigned
	dw ProgramEnd 	;4014   E_LINE  Address of start of edit line
	dw ProgramEnd+4 ;4016   CH_ADD  Pointer to next interpreted character
	dw 0			;4018   X_PTR   Address of character preceeding syntax error marker
	dw ProgramEnd+5	;401A   STKBOT  Address of calculator stack
	dw ProgramEnd+5 ;401C   STKEND  Pointer to bottom of Machine Stack / End of Calculator Stack
	db 0			;401E   BERG    Used by Floating Point calcualtor
	dw &405D		;401F   MEM     Address of start of calculator memory area
	db 0			;4021   SPARE1	spare
	db 2  			;4022   DF_SZ   Number of lines in lower part of screen
	dw 0            ;4023   S_TOP   Line number of line at top of screen
	dw &fdbf		;4025   LAST_K	Keyboard can after last tv frame
	db 255			;4027   DB_ST  	Debounce Status of keyboard	
	db 55			;4028   MARGIN  No of black lines above or below screen
	dw BasicLine2	;4029   NXTLIN  Address of next program line to be executed
	dw 0			;402B   OLDPPC  Line number to which CONT/CONTINUE jumps
	db 0			;402D   FLAGX   Various Flags
	dw 0			;402E   STRLEN  Information conserning assignment of strings
	dw &0c8d		;4030   T_ADDR  Address of next item in syntax table
	dw 0			;4032   SEED    Seed for random number generator
	dw 0			;4034   FRAMES  Updated once for every TV frame displayed 
	dw 0			;4036   COORDS  Coordinates of last point PLOTed
	db &bc			;4038   PR_CC   ;Address of LPRINT position (High part assumed 40)
	dw 0			;4039   S_POSN  Co-ordinates of print position
	db 64			;403B   CDFLAG  Flags relating to fast/slow mode (Bit 7=0 means FAST)
	ds 32
	db &76			;403C   PRBUFF  Buffer to store LPRINT output
	ds 30			;405D   MEMBOT  Area which may be used for calculator memory
	dw 0			;407B   SPARE2	spare
	


;;* 1 REM.... */
BasicLine1:  
	db &00,&00                   ;Line number
    dw BasicLine1End-BasicLine1b ;Length of line
BasicLine1b:  
	db &ea                       ;REM Statement

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Our program is in a rem statement!
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


	
	ld a,255
	JR StartDraw

infloop:
	call Player_ReadControls	;Read Keys
	cp 255				;Any keys down?
	jr z,infloop		;See if no keys are pressed

StartDraw:
	push af
		ld a,(PlayerX)	;Back up X
		ld b,a
		ld (PlayerX2),a

		ld a,(PlayerY)	;Back up Y
		ld c,a
		ld (PlayerY2),a

		push bc
			call BlankPlayer
		pop bc
	pop af
	ld d,a
	and %00000001
	jr nz,JoyNotUp		;Jump if UP not presesd
	dec c
	ld a,4
	ld (FrameBase),a
JoyNotUp:
	ld a,d
	and %00000010
	jr nz,JoyNotDown	;Jump if DOWN not presesd
	ld a,4
	ld (FrameBase),a
	inc c
JoyNotDown:
	ld a,d
	and %00000100
	jr nz,JoyNotLeft 	;Jump if LEFT not presesd
	dec b				;Move X Left 
	ld a,2
	ld (FrameBase),a
JoyNotLeft:
	ld a,d
	and %00001000
	jr nz,JoyNotRight	;Jump if RIGHT not presesd
	inc b				;Move X Right
	ld a,0
	ld (FrameBase),a
JoyNotRight: 
	ld a,b
	ld (PlayerX),a		;Update X
	ld a,c
	ld (PlayerY),a		;Update Y
	
	ld a,(FrameNo)
	xor %00000001		;Toggle Frame 
	ld (FrameNo),a
	
;X Boundary Check - if we go <0 we will end up back at &FF
	ld a,b
	cp 30 				;32 bytes wide
	jr c,PlayerPosXOk
	jr PlayerReset 		;Player out of bounds - Reset!
PlayerPosXOk

;Y Boundary Check - only need to check 1 byte
	ld a,c
	cp 21
	jr c,PlayerPosYOk	;Not Out of bounds
	
PlayerReset:
	ld a,(PlayerX2) 	;Reset Xpos	
	ld b,a
	ld (PlayerX),a	

	ld a,(PlayerY2) 	;Reset Ypos
	ld c,a
	ld (PlayerY),a
	
PlayerPosYOk:
	call DrawPlayer		;Draw Player Sprite
	
	ld bc,2000
	call PauseBC		;Wait a bit!
	jp infloop

	
	
PauseBC:
	dec bc
	ld a,b
	or c
	jr nz,PauseBC
	ret

	


BlankPlayer:
	ld hl,BlankBitmap ;Blank Sprite source
	jr DrawSprite
	
DrawPlayer:
	ld hl,Sprites	;Source sprite
	
	ld de,(FrameNo)	
	ld a,d			;D=FrameBase
	add e			;E=FrameNo
	ld e,a
	ld d,0
	sla e			;16 bytes per sprite
	rl d
	sla e
	rl d
	sla e
	rl d
	sla e
	rl d
	add hl,de
	
DrawSprite:
	push hl
		call GetScreenPos ;HL=Vram
		ex de,hl
	pop hl
	
	ld b,4				;H
	ld c,4				;W
ShowSprite:
NextSpriteLine:
	push bc
		push de
			ld b,0		;C=Width
			ldir			
		pop de
		ex de,hl
			ld bc,33
			add hl,bc
		ex de,hl
	pop bc
	djnz NextSpriteLine
	ret
	
Sprites:
	incbin "\ResAll\Sprites\AnimZX81.RAW" 
BlankBitmap:
	ds 16	
	
;Current player pos
PlayerX: db &1
PlayerY: db &1

;Last player pos (For clearing sprite)
PlayerX2: db &1
PlayerY2: db &1

FrameNo:	db 0
FrameBase:	db 0
		

;Destination ram = VRAM+1+ XPOS + (YPOS*33)
		
GetScreenPos:
	ld hl,VRAM+1	;First Screen byte 
	ld d,0
	ld e,b
	add hl,de		;Add Xpos
	
	ld e,c			
	add hl,de		;Add Ypos *1
	
	xor a
	sla e	
	rla				;x2
	sla e
	rla				;x4
	sla e
	rla				;x8
	sla e
	rla				;16
	sla e
	rla				;32
	ld d,a
	add hl,de		;Add Ypos *32
	ret
	

	
	
	;QAOP Space Enter - We'll use these like Joystick directions
Player_ReadControls:
	LD  B,%01111111		;%01111111 B, N, M, Sym, Space
	;set upper address lines - note, low bit is zero
	LD  C,&FE  			;port for lines K1 - K5
	
	ld h,b	;Need l to fill top bit 
	
	ld l,3
JoyInAgain:	
	in a,(c)	
	rra		;F2 - Space, F1- Enter, Right - P
	rl h
	rrc b	;%10111111 H, J, K, L  , Enter
			;%11011111 Y, U, I, O  , P
			;%11101111 6, 7, 8, 9  , 0
	dec l
	jr nz,JoyInAgain
	
	rra			;Left - O
	rl h				
	rrc b	;%11110111 5, 4, 3, 2  , 1
	rrc b	;%11111011 T, R, E, W  , Q
	
	in a,(c)	
	rra
	rl l		;U - Q (for later)
	rrc b	;%11111101 G, F, D, S  , A
	
	in a,(c)	
	rra
	rl h		;Down - A
	
	rr l		;Up   - Q
	rl h

	ld a,h
	ret
	


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

		db &76       ;NEWLINE
BasicLine1End:

;/* 2 RAND USR VAL "16514" */	
BasicLine2:
	db &00,&0a
	dw BasicLine2End-BasicLine2b
BasicLine2b:  db &F9,&D4
	db &1D,&22,&21,&1D,&20     
	db &7E                     
	db &8F,&01,&04,&00,&00     
	db &76                     
BasicLine2End:




;display file defintion
;ZX81 screens
; Display file (ZX81 screen) - low res screen
VRAM:
	db &76		;NewLine
	db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,&76	;NewLine
	db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,&76	;NewLine
	db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,&76	;NewLine
	db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,&76	;NewLine
	db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,&76	;NewL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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,&76	;NewLine
	db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,&76	;NewLine
	db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,&76	;NewLine
	db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,&76	;NewLine
	db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,&76	;NewLine
	db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,&76	;NewLine
BasicVars: 	db 128
ProgramEnd:                              




						
