;Compile With Option: "Vasm ZX8"

	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
	



BasicLine1:  
	db &00,&00                   ;Line number 0
    dw BasicLine1End-BasicLine1b ;Length of line
BasicLine1b:  
	db &ea                       ;REM Statement

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

CursorY equ &4036			;These are the PLOT OS vars
CursorX equ &4037

	ld hl,Message			;Address of string
	Call PrintString		;Show String to screen
	
	call newline
		
	call Monitor		;Show Register contents

	call newline
	
	ld hl,&4000			;Address to show
	ld c,32				;Bytes to show
	call Monitor_MemDumpDirect	;Show memory to screen


;Note 4000 will show as 40003, this is because the : in "4000:"	is not 
;remapped by the printchar routine.
	
infloop:	
	jp infloop				;Wait forever
	
Message: db 'Hello World!123',255	;255 terminated
	
		
PrintString:
	ld a,(hl)				;Print a '255' terminated string 
	cp 255
	ret z
	inc hl
	call PrintChar
	jr PrintString
	
	
PrintSpace:
	xor a					;CHR 0 = Space
	jr PrintCharB

PrintChar:
	push af
		cp ' '
		jr z,PrintSpace
		cp '9'+1
		jr nc,PrintNotNum
		add 7				;Fix Numbers
PrintNotNum:
		cp 96
		jr c,PrintUpper
		sub 32				;Convert Lower -> Upper
PrintUpper:	
		sub 27				;offset to ZX81 chars
PrintCharB:
		cp 64				;No Chars over &40
		jr c,PrintCharOK
		ld a,&1B			;Bad chars -> .
PrintCharOK:
		push hl
		push bc
		push de			
			push af
				ld bc,(CursorY)
				call GetScreenPos	;Get VRAM address
			pop af						; (B,C)
			ld (hl),a				;Set Char
			
			ld hl,CursorX
			inc (hl)				;Move across
			ld a,(hl)
			cp 32					;At end of line
			call nc,NewLine			;Yes? then Newline!
		pop de
		pop bc
		pop hl
	pop af
	ret
	
	
newline:
	push hl
		ld hl,CursorY
		inc (hl)			;Ypos down
		inc hl
		ld (hl),0			;Xpos=0
	pop hl
	ret
	
Locate:	
	ld (CursorY),bc			;Set XYPos
	ret
	


;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
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

SimpleBuild equ 1
	include "\SrcALL\Multiplatform_Monitor_RomVer.asm"
	include "\SrcALL\Multiplatform_ShowHex.asm"
	include "\SrcALL\Multiplatform_MonitorMemdump.asm"
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	db &76       ;New Line
BasicLine1End:

BasicLine2:
	db &00,&0a		;Line 10
	dw BasicLine2End-BasicLine2b
BasicLine2b:  
	db &F9,&D4			;RAND USR 
	db &1D,&22,&21,&1D,&20   ;16514 ... 16514=4082
	db &7E                     		;(Start of program)
	db &8F,&01,&04,&00,&00     
	db &76                     
BasicLine2End:


;Display file (ZX81 VRAM)
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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,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
	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:                              




						
