			  Z80 ASSEMBLER - ZASM VER 1.6
                      	;Compatible BASIC for PC-6001mkII/6601
                      	; by AKIKAWA, Hisashi  2017-2025
                      	
                      	;This software is redistributable under the LGPLv2.1 or any later version.
                      	
                      	
                      	;subroutine entry address
  0000                	BOOT	equ	0000h
  0008                	CHKPAR	equ	0008h
  0010                	ANADAT	equ	0010h
  0018                	JPHK1	equ	0018h
  0020                	CPHLDE	equ	0020h
  0028                	CHKSGN	equ	0028h
  0038                	JPHK2	equ	0038h
  0384                	ERRSTR	equ	0384h
  038B                	ERRIN	equ	038bh
  0390                	OK	equ	0390h
  0442                	EDIT	equ	0442h
  06EA                	CMDEND	equ	06eah
  0709                	INTPRT2	equ	0709h
  0741                	FTOI2	equ	0741h
  0955                	PIEND	equ	0955h
  0A8F                	CHKEXTR	equ	0a8fh		;for PC-6001
  0A9B                	INPT1EX	equ	0a9bh
  0BEE                	TOUPHL	equ	0beeh
  0D16                	ABTOF	equ	0d16h
  0DE3                	INT1INC	equ	0de3h
  0DE4                	INT1ARG	equ	0de4h
  0E78                	IN90H	equ	0e78h
  0E8F                	OUT90H	equ	0e8fh
  0EB0                	INTGRP	equ	0eb0h
  0EB5                	INTKEY	equ	0eb5h
  0EF6                	IKBF	equ	0ef6h
  0F2B                	IKPOP	equ	0f2bh
  0F74                	INTTIM	equ	0f74h
  0FBC                	GETCH	equ	0fbch
  0FC4                	GETC	equ	0fc4h
  103A                	GETKBF	equ	103ah
  1058                	CLRKBF	equ	1058h
  1061                	STICK	equ	1061h
  1075                	PRTC	equ	1075h
  10AA                	PRTCH	equ	10aah
  10EA                	PATCH1	equ	10eah
  116D                	SETCSR	equ	116dh
  1179                	CSRON	equ	1179h
  1181                	CSROFF	equ	1181h
  11B8                	Y2AD	equ	11b8h
  11CD                	XY2AD	equ	11cdh
  11DA                	DELLIN	equ	11dah
  1257                	PRTCHXY	equ	1257h
  1260                	SCRLU60	equ	1260h
  12A9                	SCRLD60	equ	12a9h
  12B5                	PRTFKEY	equ	12b5h
  1390                	CHGMOD	equ	1390h
  13ED                	CHGDSP	equ	13edh
  140C                	CHGACT	equ	140ch
  1478                	AD2GAD	equ	1478h
  14A0                	CGROM	equ	14a0h
  14AF                	PRTCHAD	equ	14afh
  1578                	GXY2SXY	equ	1578h
  15BA                	SXY2AD	equ	15bah
  15C0                	SETATT	equ	15c0h
  15D5                	GETCOLR	equ	15d5h
  18C3                	SETBO	equ	18c3h
  1A1C                	PUTPRT	equ	1a1ch
  1A4F                	CNVKANA	equ	1a4fh
  1A61                	ROPEN	equ	1a61h
  1A70                	GETCMT	equ	1a70h
  1AAA                	RCLOSE	equ	1aaah
  1AB8                	WOPEN	equ	1ab8h
  1ACC                	PUTCMT	equ	1acch
  1AED                	CHK90H	equ	1aedh
  1B06                	WCLOSE	equ	1b06h
  1B2A                	BLNKAST	equ	1b2ah
  1B49                	RLOFF	equ	1b49h
  1B4B                	RLON	equ	1b4bh
  1B54                	OUTB0H	equ	1b54h
  1B60                	PLSTPS	equ	1b60h
  1BB3                	PLSTOP	equ	1bb3h
  1BBE                	SETPSG2	equ	1bbeh
  1BC5                	SETPSG	equ	1bc5h
  1BCD                	BELL	equ	1bcdh
  1CA6                	JOYSTK	equ	1ca6h
  1CB4                	PATCH2	equ	1cb4h
  1D52                	CNSMAIN	equ	1d52h
  1D73                	SETCNSL	equ	1d73h
  1DBB                	SETC3	equ	1dbbh
  1DFB                	CLS	equ	1dfbh
  1E39                	SCRMAIN	equ	1e39h
  1EB3                	PLAY	equ	1eb3h
  204B                	SCALE	equ	204bh
  2286                	JOYTBL	equ	2286h
  2296                	CSRTBL	equ	2296h
  2539                	GETFN	equ	2539h
  254E                	CMTSKP	equ	254eh
  2576                	FOUND	equ	2576h
  2583                	PUTFN	equ	2583h
  259A                	INPOPN	equ	259ah
  25A8                	PRTOPN	equ	25a8h
  25E5                	WAIT	equ	25e5h
  26C7                	PUTDEV	equ	26c7h
  2701                	PUTPRNL	equ	2701h
  272D                	PUTNLX	equ	272dh
  2739                	PUTNL	equ	2739h
  274D                	STOPESC	equ	274dh
  2874                	CTLL	equ	2874h
  28F9                	INPTSCR	equ	28f9h
  2905                	INPT1	equ	2905h
  2D13                	SETGXY	equ	2d13h
  2D47                	PSET	equ	2d47h
  2DE4                	LINEBF	equ	2de4h
  2E1F                	LINE	equ	2e1fh
  2E35                	LINEB	equ	2e35h
  2EF1                	PAPOP	equ	2ef1h
  2EFA                	PAINT	equ	2efah
  30CE                	PUTSINC	equ	30ceh
  30CF                	PUTS	equ	30cfh
  310F                	GC	equ	310fh
  34F9                	RESSTK	equ	34f9h
  3913                	DE2HL4	equ	3913h
  3995                	CHKMOD	equ	3995h
  3A99                	INLNUM	equ	3a99h
  3AA1                	PUTI2	equ	3aa1h
  3AA5                	I2TOA	equ	3aa5h
  3AAC                	FTOA	equ	3aach
  3BAF                	RNDPLS	equ	3bafh
                      	
                      	;BASIC command
  0781                	C_RUN	equ	0781h		;RUN
  07E0                	C_DATA	equ	07e0h		;DATA
  07E2                	C_REM	equ	07e2h		;REM
  087A                	C_LPRT	equ	087ah		;LPRINT
  087E                	C_PRT	equ	087eh		;PRINT
  1CD2                	C_LOCA	equ	1cd2h		;LOCATE
  1CF6                	C_CNSL	equ	1cf6h		;CONSOLE
  1D9B                	C_COLR	equ	1d9bh		;COLOR
  1E04                	C_SCRN	equ	1e04h		;SCREEN
  2236                	F_STCK	equ	2236h		;STICK()
  2256                	F_STRG	equ	2256h		;STRIG()
  22A6                	C_LCPY	equ	22a6h		;LCOPY
  2D37                	C_PRST	equ	2d37h		;PRESET
  2D3C                	C_PSET	equ	2d3ch		;PSET
  35A9                	C_CLR	equ	35a9h		;CLEAR
  4A72                	C_CRCL	equ	4a72h		;CIRCLE
  6B4A                	C_COL66	equ	6b4ah		;COLOR (mode5)
  6B9B                	C_SCR66	equ	6b9bh		;SCREEN (mode5)
  70C9                	C_LIN66	equ	70c9h		;LINE (mode5)
  7548                	C_TALK	equ	7548h		;TALK
                      	
                      	;work area
  FA18                	STOPFLG	equ	0fa18h		;03h=STOP,1bh=ESC
  FA19                	CMTSTAT	equ	0fa19h		;bit1=data received, bit4=error
  FA1A                	INPDEV	equ	0fa1ah		;input device
  FA1D                	CMTBUF	equ	0fa1dh		;CMT data
  FA1E                	ASTSTAT	equ	0fa1eh		;cload asterisk status
  FA1F                	BAUD	equ	0fa1fh		;baud rate (00=600baud, ff=1200baud)
  FA20                	HEIGHT	equ	0fa20h		;Y max+1
                      	
  FA27                	PORTB0H	equ	0fa27h		;port b0h
  FA28                	TMCNT	equ	0fa28h		;time count, fa28-fa2b
  FA2D                	CONSOL4	equ	0fa2dh		;console 4th parameter
  FA2E                	CSRBLNK	equ	0fa2eh		;0=cursor blink off, 1=on
  FA2F                	CSRSTAT	equ	0fa2fh		;0=normal, ff=reversed
  FA30                	GRPWRK	equ	0fa30h		;input graphic character work
  FA31                	GRPFLG	equ	0fa31h		;print graphic character flag
  FA32                	FKEYCNT	equ	0fa32h		;function key counter
  FA51                	RSEED	equ	0fa51h		;random seed, fa51-fa55
  FA57                	PRTPOS	equ	0fa57h		;printer head position (0-origin)
  FA58                	DEVICE	equ	0fa58h		;output device
  FA59                	CMMCNST	equ	0fa59h		;constant for print-comma
  FA5A                	KEYFLG	equ	0fa5ah		;special key flags
  FA5B                	STACK	equ	0fa5bh		;initial stack pointer
  FA5D                	LINENUM	equ	0fa5dh		;current line number
  FA5F                	STARTAD	equ	0fa5fh		;basic program start address
  FA61                	CMDTBL	equ	0fa61h		;command jump table
  FAE5                	FNCTBL	equ	0fae5h		;function jump table
  FB3D                	FKEYTBL	equ	0fb3dh		;function key data
  FB8D                	FKEYAD	equ	0fb8dh		;function key address
  FB8F                	KYBFIN	equ	0fb8fh		;key buffer pointer for input
  FB90                	KYBFOUT	equ	0fb90h		;key buffer pointer for output
  FB92                	KYBFSZ	equ	0fb92h		;key buffer size
  FB93                	KYBFAD	equ	0fb93h		;key buffer address
  FB95                	RSBFIN	equ	0fb95h		;RS-232C buffer in
  FB96                	RSBFOUT	equ	0fb96h		;RS-232C buffer out
  FB98                	RSBFSZ	equ	0fb98h		;RS-232C buffer size
  FB99                	RSBFAD	equ	0fb99h		;RS-232C buffer address
                      	
                      	;PLAY command
  FBA1                	BUFAIN	equ	0fba1h		;channelA buffer pointer for input
  FBA2                	BUFAOUT	equ	0fba2h		;channelA buffer pointer for output
  FBA4                	BUFASZ	equ	0fba4h		;channelA buffer size
  FBA5                	BUFAAD	equ	0fba5h		;channelA buffer address
  FBA7                	BUFBIN	equ	0fba7h		;channelB buffer in
  FBAD                	BUFCIN	equ	0fbadh		;channelC buffer in
                      	
  FBB9                	KEYBUF	equ	0fbb9h		;key buffer, fbb9-fbf8
  FBF9                	RSBUF	equ	0fbf9h		;RS-232C buffer, fbf9-fc38
                      	
                      	;PLAY command
  FC39                	BUFA	equ	0fc39h		;channelA buffer, fc39-fc78
  FC79                	BUFB	equ	0fc79h		;channelB buffer, fc79-fcb8
  FCB9                	BUFC	equ	0fcb9h		;channelC buffer, fcb9-fcf8
  FD14                	CHANNEL	equ	0fd14h		;channel (0-2)
  FD15                	SVBCK	equ	0fd15h		;backup of S/V-value
  FD17                	PLAYLEN	equ	0fd17h		;length
  FD18                	PLAYSTR	equ	0fd18h		;string address
  FD1B                	PLAYST	equ	0fd1bh		;PLAY status
  FD1D                	PLWKA	equ	0fd1dh		;PLAY work for channelA
  0000                	REMAIN	equ	0fd1dh-PLWKA	;remaining time
  0002                	PLLEN	equ	0fd1fh-PLWKA	;string length
  0003                	PLADR	equ	0fd20h-PLWKA	;string address
  000F                	OCTAVE	equ	0fd2ch-PLWKA	;O-value
  0010                	LENGTH	equ	0fd2dh-PLWKA	;L-value
  0011                	TEMPO	equ	0fd2eh-PLWKA	;T-value
  0012                	VOLUME	equ	0fd2fh-PLWKA	;V-value
  0013                	PERIOD	equ	0fd30h-PLWKA	;M-value
  FD42                	PLWKB	equ	0fd42h		;PLAY work for channelB
  FD67                	PLWKC	equ	0fd67h		;PLAY work for channelC
                      	
  FD8C                	PAGES	equ	0fd8ch		;How many pages?
  FD8D                	USREND	equ	0fd8dh		;user area end, fd8d-fd8e
  FD8F                	SCREEN2	equ	0fd8fh		;screen 2nd parameter-1
  FD90                	SCREEN3	equ	0fd90h		;screen 3rd parameter-1
                      	
  FD91                	VRAM	equ	0fd91h		;VRAM address (high)
  FD92                	SCREEN1	equ	0fd92h		;screen 1st parameter-1
  FD93                	COLOR1	equ	0fd93h		;color 1st parameter
  FD94                	COLOR2	equ	0fd94h		;color 2nd parameter
  FD95                	COLOR3	equ	0fd95h		;color 3rd parameter (processed)
  FD96                	M1COLOR	equ	0fd96h		;screen mode 1 color parameters, fd96-fd98
  FD99                	M2COLOR	equ	0fd99h		;screen mode 2 color parameters, fd99-fd9b
  FD9C                	M3COLOR	equ	0fd9ch		;screen mode 3 color parameters, fd9c-fd9e
  FD9F                	M4COLOR	equ	0fd9fh		;screen mode 4 color parameters, fd9f-fda1
  FDA2                	CONSOL1	equ	0fda2h		;console 1st line number+1
  FDA3                	CONSOL2	equ	0fda3h		;console last line number+1
  FDA4                	FSTLIN	equ	0fda4h		;=(CONSOL1)
  FDA5                	LASTLIN	equ	0fda5h		;sc12:(CONSOL2)-(CONSOL3) sc34:(CONSOLE2)
  FDA6                	CONSOL3	equ	0fda6h		;console 3rd parameter
  FDA7                	FKEYSFT	equ	0fda7h		;shift key status for function key display
  FDA8                	CSRY	equ	0fda8h		;cursor Y+1
  FDA9                	CSRX	equ	0fda9h		;cursor X+1
  FDAA                	CSRAD	equ	0fdaah		;cursor address
  FDAC                	WIDTH	equ	0fdach		;X max+1
  FDAE                	GRPX1	equ	0fdaeh		;graphic X, fdae-fdaf
  FDB0                	GRPY1	equ	0fdb0h		;graphic Y, fdb0-fdb1
  FDB2                	VRAMBIT	equ	0fdb2h		;color bit/dot pattern
  FDB3                	VRAMAD	equ	0fdb3h		;VRAM attribute address
  FDB5                	LINEST	equ	0fdb5h		;line connection status, (0=connect) fdb5-fdb7
  FDB9                	PAGE1	equ	0fdb9h		;page1 data, fdb9-fde0
  FDE1                	PAGE2	equ	0fde1h		;page2 data, fde1-fe08
  FE09                	PAGE3	equ	0fe09h		;page3 data, fe09-fe30
  FE31                	PAGE4	equ	0fe31h		;page4 data, fe31-fe58
                      	
  FE59                	INPTXY	equ	0fe59h		;INPUT command initial position
  FE5B                	INPTX	equ	0fe5bh		;INPUT command end position, or length
  FE5C                	INPTPAG	equ	0fe5ch		;INPUT command page
  FE5D                	INSMODE	equ	0fe5dh		;0=not insert mode, ff=insert mode
  FE5E                	STOPSC2	equ	0fe5eh		;screen 2nd parameter -1 in stop
  FE5F                	STOPSC3	equ	0fe5fh		;screen 3rd parameter -1 in stop
  FEAC                	ATTDAT	equ	0feach		;color attribute data
  FEAD                	GRPX2	equ	0feadh		;graphic X, fead-feae
  FEAF                	GRPY2	equ	0feafh		;graphic Y, feaf-feb0
  FEB1                	GRPX3	equ	0feb1h		;graphic work, feb1-feb2
  FEB3                	GRPY3	equ	0feb3h		;graphic work, feb3-feb4
  FEB5                	PAWRK	equ	0feb5h		;paint work, feb5-feb6
  FEB7                	PACNT	equ	0feb7h		;paint work
  FEC5                	BORDERA	equ	0fec5h		;paint border color attribute
  FEC6                	BORDERC	equ	0fec6h		;paint border color code
  FECA                	GMKYBUF	equ	0fecah		;reply to game key query
                      	;TARGET	equ	0fecbh		;target file name, fecb-fed0
  FED1                	FNAME	equ	0fed1h		;loading file name, fed1-fed6
  FED8                	VERIFY	equ	0fed8h		;load/verify flag 00h=load ffh=verify
  FEDA                	INPBUF	equ	0fedah		;input buffer, feda-ff21, (ff22)-(ff23)=00h
  FF25                	ARGTYP	equ	0ff25h		;0=value 1=string
  FF27                	STREND	equ	0ff27h		;strings area end
  FF29                	BASICAD	equ	0ff29h		;basic area start
  FF2D                	STRDSC1	equ	0ff2dh		;temporary string descriptor, ff2d-ff30
  FF31                	STRDSC2	equ	0ff31h		;ff31-ff34
  FF35                	STRDSC3	equ	0ff35h		;ff35-ff38
  FF39                	STRDSC4	equ	0ff39h		;ff39-ff3c
  FF3D                	STRAD	equ	0ff3dh		;string area start address - 1
  FF41                	GCWRK	equ	0ff41h		;garbage collection work
  FF45                	DATALN	equ	0ff45h		;data line number
  FF49                	INPRD	equ	0ff49h		;INPUT(=0) or READ(>0)
  FF4E                	PROGAD	equ	0ff4eh		;program address in interpreting
  FF50                	TMP	equ	0ff50h		;temporary
  FF52                	STOPLN	equ	0ff52h		;line number at stop
  FF54                	STOPAD	equ	0ff54h		;stop address
  FF56                	VARAD	equ	0ff56h		;variable area start address
  FF58                	ARRAD	equ	0ff58h		;array area start address
  FF5A                	FREEAD	equ	0ff5ah		;free area start address
  FF5C                	DATAAD	equ	0ff5ch		;data command address
  FF5E                	FNPAR	equ	0ff5eh		;FN() parameter name
  FF60                	FNARG	equ	0ff60h		;FN() argument value, ff60-ff64
  FF65                	OPRTR	equ	0ff65h		;operator, FAC1-1
  FF66                	FAC1	equ	0ff66h		;floating accumlator 1, ff66-ff6a
  FF6D                	FAC2	equ	0ff6dh		;floating accumlator 2, ff6d-ff71
  FF72                	FAC3	equ	0ff72h		;floating accumlator 3 (string conversion)
  FF8A                	HOOK	equ	0ff8ah		;hook area, ff8a-ffe3
  FF8A                	HOOKPED	equ	0ff8ah		;hook for print end
  FF8D                	HOOKERR	equ	0ff8dh		;hook for error
  FF93                	HOOKEDT	equ	0ff93h		;hook for screen editor
  FF99                	HOOKCLP	equ	0ff99h		;hook for changing link pointer
  FFAE                	HOOKPRT	equ	0ffaeh		;hook for PRINT# command
  FFBA                	HOOKINP	equ	0ffbah		;hook for INPUT# command
  FFC9                	HOOKRES	equ	0ffc9h		;hook for RESSTK
  FFCF                	HOOKPUT	equ	0ffcfh		;hook for PUTDEV
  FFD2                	HOOKLCP	equ	0ffd2h		;hook for LCOPY
  FFD8                	HOOKSTP	equ	0ffd8h		;hook for stop key
  FFDB                	HOOK18H	equ	0ffdbh		;hook for 0018h
  FFE1                	HOOK38H	equ	0ffe1h		;hook for 0038h
  FFE6                	ASPECT	equ	0ffe6h		;aspect ratio for CIRCLE command
                      	
                      	
                      	;constant
  0020                	COLUMNS	equ	32
  0010                	ROWS	equ	16
  0023                	TRACKS	equ	35		;track number=0-34 (1D)
  0046                	CLSTS	equ	TRACKS*2	;cluster number=0-69
                      	
  0080                	END_	equ	080h
  0081                	FOR_	equ	081h
  0083                	DATA_	equ	083h
  0084                	INPUT_	equ	084h
  0088                	GOTO_	equ	088h
  0089                	RUN_	equ	089h
  008B                	RSTR_	equ	08bh
  008C                	GOSUB_	equ	08ch
  008E                	REM_	equ	08eh
  0090                	OUT_	equ	090h
  0093                	DEF_	equ	093h
  0095                	PRINT_	equ	095h
  0097                	LIST_	equ	097h
  009B                	PSET_	equ	09bh
  009F                	SCREEN_	equ	09fh
  00AA                	CMDLAST	equ	0aah		;last command for 60
  00AE                	PUT_	equ	0aeh
  00C2                	TAB_	equ	0c2h
  00C3                	TO_	equ	0c3h
  00C4                	FN_	equ	0c4h
  00C5                	SPC_	equ	0c5h
  00C6                	INKEY_	equ	0c6h
  00C7                	THEN_	equ	0c7h
  00C8                	NOT_	equ	0c8h
  00C9                	STEP_	equ	0c9h
  00CA                	PLUS_	equ	0cah
  00CB                	MINUS_	equ	0cbh
  00CF                	AND_	equ	0cfh
  00D0                	OR_	equ	0d0h
  00CC                	ASTRSK_	equ	0cch
  00D1                	GT_	equ	0d1h
  00D2                	EQ_	equ	0d2h
  00D3                	LT_	equ	0d3h
  00D4                	FNC1ST	equ	0d4h		;1st function
  00D7                	USR_	equ	0d7h
  00EA                	LEFT_	equ	0eah
  00EC                	MID_	equ	0ech
  00EF                	STICK_	equ	0efh
  00F0                	STRIG_	equ	0f0h
  00F1                	FNCLAST	equ	0f1h		;last function for 60
                      	
                      	
                      	;extended basic subroutine entry address
  4274                	DISK	equ	4274h
  42B9                	INIDSK	equ	42b9h
  51F2                	SETFATP	equ	51f2h
  5CFD                	DISK2	equ	5cfdh
                      	
                      	;mkII/6601 subroutine entry address
  016B                	CHGRAM	equ	016bh
  2030                	SCALE2	equ	2030h
  2698                	SETF0H	equ	2698h
  397A                	PUTI1	equ	397ah
  4000                	TALK	equ	4000h
  601C                	CALLINI	equ	601ch
  63E2                	SCRLU66	equ	63e2h
  642F                	SCRLD66	equ	642fh
  65CC                	AD2GAD2	equ	65cch
  69F3                	SETBO66	equ	69f3h
  6B7A                	SETC366	equ	6b7ah
  6B9E                	SCRN66	equ	6b9eh
  70E7                	LINBF66	equ	70e7h
  7122                	LINEB66	equ	7122h
  7161                	PA66POP	equ	7161h
  716A                	PAINT66	equ	716ah
  72BD                	PRTKNJ	equ	72bdh
  7381                	KPRTCH	equ	7381h
  7384                	KPRTCH2	equ	7384h
  78FF                	BLKTRA	equ	78ffh
  7AF9                	LOADHEX	equ	7af9h
                      	
                      	;mkII/6601 work area
  FCF9                	REGF	equ	0fcf9h
  FCFA                	REGA	equ	0fcfah
  FCFB                	REGC	equ	0fcfbh
  FCFC                	REGB	equ	0fcfch
  FCFD                	REGE	equ	0fcfdh
  FCFE                	REGD	equ	0fcfeh
  FCFF                	REGL	equ	0fcffh
  FD00                	REGH	equ	0fd00h
  FD02                	REGPC	equ	0fd02h
  FD04                	REGSP	equ	0fd04h
  FD06                	BRKADR1	equ	0fd06h
  FD08                	BRKSAV1	equ	0fd08h
  FD09                	BRKADR2	equ	0fd09h
  FD0B                	BRKSAV2	equ	0fd0bh
  FD0C                	SPSAVE	equ	0fd0ch
  FD0E                	MONFLG	equ	0fd0eh		;b6=RAM/ROM, b5=printer, b4=echo, b3=CMT/RS-232C, b0=CRT
                      	
  FE60                	ATTDAT2	equ	0fe60h
  FE61                	BORDA2	equ	0fe61h
  FE64                	PORTF0H	equ	0fe64h		;port f0h
  FE65                	MODE	equ	0fe65h		;mode-1
  FE66                	HOOKVOI	equ	0fe66h		;hook for PC-6601 VOICEROM
  FE8D                	READRAM	equ	0fe8dh		;subroutine: read RAM data for mkII/6601
  FEA1                	OUTF0H	equ	0fea1h		;subroutine: output port f0h
  FE93                	LDIRRAM	equ	0fe93h		;subroutine: LDIR in RAM for mkII/6601
  FE98                	CHGBANK	equ	0fe98h		;subroutine: change 0000-7fff bank
  FEA5                	LDDRRAM	equ	0fea5h		;subroutine: LDDR in RAM for mkII/6601
  FEB8                	PAWRK2	equ	0feb8h		;paint work, feb8-feb9
  FEC7                	FKEYLEN	equ	0fec7h
                      	
                      	;work area for FDD
  FB31                	FILES	equ	0fb31h
  FB33                	FATP	equ	0fb33h		;FAT pointer+3
  FB35                	BUFPTBL	equ	0fb35h		;file buffer pointer table
  FB37                	BUF0	equ	0fb37h		;file#0 buffer data address
  FB39                	BUFP	equ	0fb39h		;file buffer pointer
  FB3B                	DRIVES	equ	0fb3bh
  FE70                	RDWRCHK	equ	0fe70h		;read or write or check FD
  FE71                	RMSECT	equ	0fe71h		;remaining sector
  FE72                	TRACK	equ	0fe72h
  FE73                	DRIVE	equ	0fe73h
  FE74                	SECTOR	equ	0fe74h
  FE75                	DRVBIT	equ	0fe75h		;bit4=drvieA,bit5=drvieB, bit6,7,0,1=external
  FE77                	RETRY	equ	0fe77h
  FE78                	NSECT	equ	0fe78h		;sectors to be read/written, 1-4
  FE79                	STATBF	equ	0fe79h		;FDC status, fe79-fe7f
  FFFC                	FATPTBL	equ	0fffch		;FAT pointer table
                      	
                      	
                      	;mkII/6601 constant
  0028                	CLMN66	equ	40
  0014                	ROWS66	equ	20
  00C1                	CLAST66	equ	0c1h		;last command for mkII/66
  00F9                	FLASTEX	equ	0f9h		;last function for extended BASIC
                      	
                      	;FD I/O port
  00B1                	FDCNTL	equ	0b1h		;FD control
  00B2                	FDCINT	equ	0b2h		;FDC interrupt
  00B3                	B2CNTL	equ	0b3h		;port b2 control?
  00D0                	FDBUF	equ	0d0h		;d0-d3
                      	
  00D0                	EFDA	equ	0d0h		;external FDD portA (input)
  00D1                	EFDB	equ	0d1h		;external FDD portB (output)
  00D2                	EFDC	equ	0d2h		;external FDD portC (control)
  00D3                	EFDCBIT	equ	0d3h		;external FDD portC bit set/reset (control)
                      	
  00D4                	MOTORST	equ	0d4h		;FDD motor status
  00D6                	MOTOR	equ	0d6h		;FDD motor on/off
  00DA                	BUFSIZ	equ	0dah		;0e=use port d0, 0d=d0-d1, 0c=d0-d2, 0b=d0-d3
  00DC                	FDCSTAT	equ	0dch		;FDC status register
  00DD                	FDCDATA	equ	0ddh		;FDC data register
  00DE                	ADJUST	equ	0deh		;?
                      	
                      	
                      	;;;;;;;;;;;;;;;;;;;;;;;;;
                      	
                      	;boot
  0000                		org	BOOT
  0000  F3            		di
  0001  C34716        		jp	COLD
                      	
                      	
                      	;check parameter
                      	; compare (hl) and data at (sp)
                      	;input: sp,hl
                      	;output: a=data, hl=data address, z-flag(a=00h or 3ah), c-flag(a=30h-39h)
  0004                	_CHKPAR:ds	CHKPAR-_CHKPAR
  0008                		org	CHKPAR
                      	
  0008  E3            		ex	(sp),hl
  0009  7E            		ld	a,(hl)
  000A  23            		inc	hl
  000B  E3            		ex	(sp),hl
  000C  BE            		cp	(hl)
  000D  C2C203        		jp	nz,SNERR
                      	
                      	
                      	;skip space and analyze a byte
                      	;input: hl=address-1
                      	;output: a=data, hl=data address, z-flag(a=00h or 3ah), c-flag(a=30h-39h)
                      	;destroy: f
  0010                	_ANADAT:ds	ANADAT-_ANADAT
  0010                		org	ANADAT
                      	
  0010  CD583F        		call	SKIPSPINC
  0013  C3E00B        		jp	CHKFIG
                      	
                      	
                      	;jump hook
  0016                	_JPHK1:	ds	JPHK1-_JPHK1
  0018                		org	JPHK1
                      	
  0018  C3DBFF        		jp	HOOK18H
                      	
                      	
                      	;compare hl and de
                      	;input: hl,de
                      	;output: f
                      	;destroy: af
  001B                	_CPHLDE:ds	CPHLDE-_CPHLDE
  0020                		org	CPHLDE
                      	
  0020  7C            		ld	a,h
  0021  92            		sub	d
  0022  C0            		ret	nz
  0023  7D            		ld	a,l
  0024  93            		sub	e
  0025  C9            		ret
                      	
                      	
                      	;check sign of FAC1
                      	;input: FAC1
                      	;output: a(0:FAC1=0 1:FAC1>0 ff:FAC1<0), z(1:FAC1=0)
                      	;destroy: af
  0026                	_CHKSGN:ds	CHKSGN-_CHKSGN
  0028                		org	CHKSGN
  0028  3A6AFF        		ld	a,(FAC1+4)
  002B  B7            		or	a
  002C  C8            		ret	z
  002D  C3FC38        		jp	CHKSGN2
                      	
                      	
                      	;jump hook
  0030                	_JPHK2:	ds	JPHK2-_JPHK2
  0038                		org	JPHK2
                      	
  0038  C3E1FF        		jp	HOOK38H
                      	
                      	
                      	;hot start
  003B                	HOT:
                      	;initialize function key
  003B  217201        		ld	hl,FKEYLST
  003E  113DFB        		ld	de,FKEYTBL
  0041  0E0A          		ld	c,0ah
  0043                	SETFKLP1:
  0043  7E            		ld	a,(hl)
  0044  23            		inc	hl
  0045  E5            		push	hl		;
  0046  CDB525        		call	CNVASC
                      	
  0049  0608          		ld	b,08h
  004B                	SETFKLP2:
  004B  FE41          		cp	'A'
  004D  3806          		jr	c,SETFKC
  004F  FE5B          		cp	'Z'+1
  0051  3002          		jr	nc,SETFKC
  0053  C620          		add	a,'a'-'A'
  0055                	SETFKC:
  0055  05            		dec	b
  0056  12            		ld	(de),a
  0057  13            		inc	de
  0058  23            		inc	hl
  0059  7E            		ld	a,(hl)
  005A  B7            		or	a
  005B  F24B00        		jp	p,SETFKLP2
                      	
  005E  E1            		pop	hl		;
  005F  7E            		ld	a,(hl)
  0060  23            		inc	hl
                      	
  0061                	SETFKLP3:
  0061  12            		ld	(de),a
  0062  13            		inc	de
  0063  AF            		xor	a
  0064  10FB          		djnz	SETFKLP3
                      	
  0066  0D            		dec	c
  0067  20DA          		jr	nz,SETFKLP1
  0069  CDB512        		call	PRTFKEY
                      	
                      	;check external ROM
  006C  210340        		ld	hl,4003h
  006F  CD953F        		call	CHKEXTAB
  0072  200A          		jr	nz,SKIPMENU	;if (4000h)='A' and (4001h)='B'
  0074  CD923F        		call	CHKEXTCD
  0077  3EA1          		ld	a,0a1h		;0000-5fff:BASIC ROM, 6000-7fff:external ROM
  0079  D3F0          		out	(0f0h),a
  007B  CD0040        		call	CHKEXT6000	;check 6000h and select mode
  007E                	SKIPMENU:
  007E  F5            		push	af		;a=mode-1, z=mode 5?
  007F  CD98FE        		call	CHGBANK
                      	
                      	;How Many Pages?
  0082                	PAGELP:
  0082  211D01        		ld	hl,HOWMANY
  0085  CDCF30        		call	PUTS
  0088  CD0529        		call	INPT1
  008B  D7            		rst	ANADAT
  008C  3807          		jr	c,PAGEOK
  008E  218CFD        		ld	hl,PAGES
  0091  5E            		ld	e,(hl)
  0092  57            		ld	d,a		;if return, a=0
  0093  1803          		jr	CHKPAGE
                      	
  0095                	PAGEOK:
  0095  CDCA27        		call	ATOI2
  0098                	CHKPAGE:
  0098  7A            		ld	a,d
  0099  B7            		or	a
  009A  20E6          		jr	nz,PAGELP
                      	
  009C  7B            		ld	a,e
  009D  3D            		dec	a
  009E  218CFD        		ld	hl,PAGES
  00A1  BE            		cp	(hl)
  00A2  30DE          		jr	nc,PAGELP
  00A4  73            		ld	(hl),e
                      	
  00A5  CDBE34        		call	NEW
  00A8  211801        		ld	hl,ENDTBL-1
  00AB  19            		add	hl,de		;d=0
  00AC  66            		ld	h,(hl)
  00AD  2EFF          		ld	l,0ffh
  00AF  228DFD        		ld	(USREND),hl
  00B2  F1            		pop	af		;a=mode-1, z=mode 5?
                      	
  00B3  CA7261        		jp	z,MODE5
  00B6  FE02          		cp	02h
  00B8  D24040        		jp	nc,SETTBL
                      	
  00BB                	MODE12:
  00BB  013200        		ld	bc,50
  00BE  CDD135        		call	CLRMAIN
                      	
                      	;print start message
  00C1                	PRTSYS60:
  00C1  212F01        		ld	hl,SYSNAME
  00C4                	PRTSYS:
  00C4  CDCF30        		call	PUTS
  00C7  CDCC34        		call	CHKFRE
                      	;	ld	hl,002fh	;for compatibility with N6X-BASIC
  00CA  212800        		ld	hl,0028h	;for compatibility with N6X-BASIC
  00CD  19            		add	hl,de
  00CE  CDA13A        		call	PUTI2
  00D1  210D01        		ld	hl,BFREE
  00D4                	PUTSEDIT:
  00D4  CDCF30        		call	PUTS
  00D7  C34204        		jp	EDIT
                      	
                      	
                      	;initialize table
  00DA                	IOTBL93:
  00DA  C0            		db	0c0h		;bit7: mode definition
                      					;bit65:portA,portC-upper=mode 2
                      					;bit4: portA=output
                      					;bit3: portC-upper=output
                      					;bit2: portB,portC-lower=mode 0
                      					;bit1: portB=output
                      					;bit0: portC-lower=output
  00DB  0D            		db	0dh		;enable 8255 INT for writing
  00DC  09            		db	09h		;enable 8255 INT for reading
  00DD  05            		db	05h		;CGROM off
  00DE  03            		db	03h		;CRTC
  00DF  0F            		db	0fh		;nobf=1 (for emulator?)
                      	
  00E0                	IOTBL81:
  00E0  92            		db	92h		;reset error, DTR=1, RxE/TxEN=0
  00E1  92            		db	92h		; set twice for synchronous mode
  00E2  52            		db	52h		;reset, reset error, DTR=1, RxE/TxEN=0
  00E3  4F            		db	4fh		;stop bits=1, parity disable,
                      					; character length=8bits, baud rate factor=64
  00E4  37            		db	37h		;reset error, RTS/DTR/RxE/TxEN=1
                      	
  00E5                	IOTBLF0:
  00E5  11            	F0:	db	11h		;0000-7fff:BASIC ROM
  00E6  DD            	F1:	db	0ddh		;8000-ffff:internal RAM for reading
  00E7  55            	F2:	db	55h		;0000-ffff:internal RAM for writing
  00E8  C2            	F3:	db	0c2h		;wait and interrupt control
                      					; bit7: M1 wait (1=on)
                      					; bit6: ROM wait (1=on)
                      					; bit5: RAM wait (1=on)
                      					; bit4: INT2 interrupt address (1=output)
                      					; bit3: INT1 interrupt address (1=output)
                      					; bit2: timer interrupt (1=disable)
                      					; bit1: INT2 interrupt (1=disable)
                      					; bit0: INT1 interrupt (1=disable)
  00E9  00            	F4:	db	00h		;INT1 address
  00EA  00            	F5:	db	00h		;INT2 address
  00EB  03            	F6:	db	03h		;timer count up
  00EC  06            	F7:	db	06h		;timer interrupt address
  00ED  C3            	F8:	db	0c3h		;CG rom access control
                      					; bit7: wait (0=off, 1=on)
                      					; bit6: read enable (0=disable, 1=enable)
                      					; bit5: bit2 mask (1=mask)
                      					; bit4: bit1 mask (1=mask)
                      					; bit3: bit0 mask (1=mask)
                      					; bit2-0: CGROM address (=A15-A13)
                      	
                      	;interrupt
  00EE                	INTTBL:
  00EE  B50E          		dw	INTKEY		;fa02 normal key
  00F0  590F          		dw	INT232		;fa04 RS-232C
  00F2  740F          		dw	INTTIM		;fa06 2ms timer
  00F4  9A0F          		dw	INTCMT		;fa08 CMT read
  00F6  A70F          		dw	EIRET		;fa0a ?
  00F8  A70F          		dw	EIRET		;fa0c ?
  00FA  A90F          		dw	INTWSTP		;fa0e CMT write stop
  00FC  A90F          		dw	INTRSTP		;fa10 CMT read stop
  00FE  B10F          		dw	INTERR		;fa12 CMT error
  0100  B00E          		dw	INTGRP		;fa14 GRAPH key etc.
  0102  D10E          		dw	INTGAM		;fa16 reply to game key query
                      	
                      	
  0104  00            		db	00h		;fa18 STOPFLG
  0105  00            		db	00h		;fa19 CMTSTAT
  0106  00            		db	00h		;fa1a INPDEV
  0107  0000          		db	00h, 00h
  0109  00            		db	00h		;fa1d CMTBUF
  010A  20            		db	' '		;fa1e ASTSTAT
  010B  FF            		db	0ffh		;fa1f BAUD
  010C  10            		db	ROWS		;fa20 HEIGHT
                      	
                      	
  010D                	BFREE:
  010D  20427974657320		db	" Bytes free", 00h
  0119                	ENDTBL:
  0119  F9DFBF9F      		db	0f9h, 0dfh, 0bfh, 9fh
  011D                	HOWMANY:
  011D  486F77204D616E		db	"How Many Pages", 00h
  012C                	QMARK:
  012C  3F2000        		db	"? ", 00h
  012F                	SYSNAME:
  012F  36309ADE96FD42		db	"60", 9ah, 0deh, 96h, 0fdh, "BASIC", 0dh, 0ah, 00h
                      	
  013D                	PAGEDATA:
  013D  80            		db	80h		;fd91	VRAM address
  013E  00            		db	00h		;fd92	screen 1st parameter-1
  013F  01            		db	01h		;fd93	color 1st parameter
  0140  00            		db	00h		;fd94	color 2nd parameter
  0141  00            		db	00h		;fd95	color 3rd parameter (1,2 -> 0,2)
  0142  010000        		db	01h, 00h, 00h	;fd96	screen mode 1 color parameters
  0145  010000        		db	01h, 00h, 00h	;fd99	screen mode 2 color parameters
  0148  020000        		db	02h, 00h, 00h	;fd9c	screen mode 3 color parameters
  014B  030000        		db	03h, 00h, 00h	;fd9f	screen mode 4 color parameters
  014E  01            		db	01h		;fda2	console 1st line number+1
  014F  10            		db	10h		;fda3	console last line number+1
  0150  01            		db	01h		;fda4	=(fda2)
  0151  0F            		db	0fh		;fda5	sc12:(fda3)-(fda6) sc34:(fda3)
  0152  01            		db	01h		;fda6	console 3rd parameter
  0153  00            		db	00h		;fda7	shift key status for function key disp
  0154  01            		db	01h		;fda8	cursor Y+1
  0155  01            		db	01h		;fda9	cursor X+1
  0156  00C2          		dw	0c200h		;fdaa	cursor address
  0158  20            		db	COLUMNS		;fdac	X max+1
  0159  00            		db	00h		;fdad
  015A  0000          		dw	0000h		;fdae	graphic X
  015C  0000          		dw	0000h		;fdb0	graphic Y
  015E  00            		db	00h		;fdb2
  015F  00            		db	00h		;fdb3
  0160  FF            		db	0ffh		;fdb4	LINEST-1
  0161  FF            		db	0ffh		;fdb5	LINEST
  0162  FF            		db	0ffh		;fdb6
  0163  FF            		db	0ffh		;fdb7
                      	
                      	
                      	;change 0000-7fffh to RAM
                      	;destroy: none
  0164                	_CHGRAM:ds	CHGRAM-_CHGRAM
  016B                		org	CHGRAM
                      	
  016B  F5            		push	af
  016C  3EDD          		ld	a,0ddh		;0000-7fff:internal RAM for reading
  016E  F3            		di
  016F  C3A1FE        		jp	OUTF0H
                      	
                      	
  0172                	FKEYLST:
  0172  9A20          		db	9ah, ' '	;COLOR
  0174  A322          		db	0a3h, 22h	;CLOAD"
  0176  8820          		db	GOTO_, ' '	;GOTO
  0178  9720          		db	97h, ' '	;LIST
  017A  890D          		db	RUN_, 0dh	;RUN
  017C  9F20          		db	9fh, ' '	;SCREEN
  017E  A422          		db	0a4h, 22h	;CSAVE"
  0180  9520          		db	PRINT_, ' '	;PRINT
  0182  A720          		db	0a7h, ' '	;PLAY
  0184  960D          		db	96h, 0dh	;CONT
                      	
                      	
  0186                	OPRTBL:
                      	;c4-c7
                      	;	dw					F_FN,	F_SPC,	F_INKY,	C_THEN
                      	;c8-cf
  0186  100CC2037E3694		dw	O_NOT,	C_STEP,	O_PLS,	O_MNS,	O_MUL,	O_DIV,	O_POW,	O_AND
                      	;d0-d3
  0196  F80B220C      		dw	O_OR,	O_GTEQLT
                      	
  019A                	CMDLST:
                      	;80-8f
  019A  66357306F135E0		dw	C_END,	C_FOR,	C_NEXT,	C_DATA,	C_INPT,	C_DIM,	C_READ,	C_LET
  01AA  A5078107590817		dw	C_GOTO,	C_RUN,	C_IF,	C_RSTR,	C_GSUB,	C_RET,	C_REM,	C_STOP
                      	;90-9f
  01BA  B60D36087A0857		dw	C_OUT,	C_ON,	C_LPRT,	C_DEF,	C_POKE,	C_PRT,	C_CONT,	C_LIST
  01CA  CE05A9359B1D3C		dw	C_LLST,	C_CLR,	C_COLR,	C_PSET,	C_PRST,	C_LINE,	C_PAIN,	C_SCRN
                      	;a0-aa
  01DA  FB1DD21CF61CB5		dw	C_CLS,	C_LOCA,	C_CNSL,	C_CLD,	C_CSV,	C_EXEC,	C_SOUN,	C_PLAY
  01EA  4123A622B034  		dw	C_KEY,	C_LCPY,	C_NEW
                      	
  01F0                	FNCLST:
                      	;d4-df
  01F0  043980391C39CB		dw					F_SGN,	F_INT,	F_ABS,	F_USR
  01F8  2932A80D330D41		dw	F_FRE,	F_INP,	F_LPOS,	F_POS,	F_SQR,	F_RND,	F_LOG,	F_EXP
                      	;e0-ef
  0208  7D3D903D583EEE		dw	F_COS,	F_SIN,	F_TAN,	F_PEEK,	F_LEN,	SNERR,	F_STR,	F_VAL
  0218  AC31B631C031C7		dw	F_ASC,	F_CHR,	F_LEFT,	F_RGT,	F_MID,	F_POIN,	F_CSRL,	F_STCK
                      	;f0-f1
  0228  56226C1E      		dw	F_STRG,	F_TIME
                      	
                      	
  022C                	CMDNAME:
  022C  C54E44C64F52CE		db	'E'+80h,"ND",	'F'+80h,"OR",	'N'+80h,"EXT",	'D'+80h,"ATA"
  023A  C94E505554C449		db	'I'+80h,"NPUT",	'D'+80h,"IM",	'R'+80h,"EAD",	'L'+80h,"ET"
  0249  C74F544FD2554E		db	'G'+80h,"OTO",	'R'+80h,"UN",	'I'+80h,"F",	'R'+80h,"ESTORE"
  0259  C74F535542D245		db	'G'+80h,"OSUB",	'R'+80h,"ETURN",'R'+80h,"EM",	'S'+80h,"TOP"
                      	
  026B  CF5554CF4ECC50		db	'O'+80h,"UT",	'O'+80h,"N",	'L'+80h,"PRINT",'D'+80h,"EF"
  0279  D04F4B45D05249		db	'P'+80h,"OKE",	'P'+80h,"RINT",	'C'+80h,"ONT",	'L'+80h,"IST"
  028A  CC4C495354C34C		db	'L'+80h,"LIST",	'C'+80h,"LEAR",	'C'+80h,"OLOR",	'P'+80h,"SET"
  029D  D05245534554CC		db	'P'+80h,"RESET",'L'+80h,"INE",	'P'+80h,"AINT",	'S'+80h,"CREEN"
                      	
  02B2  C34C53CC4F4341		db	'C'+80h,"LS",	'L'+80h,"OCATE",'C'+80h,"ONSOLE",'C'+80h,"LOAD"
  02C7  C353415645C558		db	'C'+80h,"SAVE",	'E'+80h,"XEC",	'S'+80h,"OUND",	'P'+80h,"LAY"
  02D9  CB4559CC434F50		db	'K'+80h,"EY",	'L'+80h,"COPY",	'N'+80h,"EW"
                      	
  02E4                	FNCNAME:
  02E4  D4414228D44F  		db					'T'+80h,"AB(",	'T'+80h,"O"
  02EA  C64ED3504328C9		db	'F'+80h,"N",	'S'+80h,"PC(",	'I'+80h,"NKEY$",'T'+80h,"HEN"
  02FA  CE4F54D3544550		db	'N'+80h,"OT",	'S'+80h,"TEP",	'+'+80h,	'-'+80h
  0303  AAAFDEC14E44  		db	'*'+80h,	'/'+80h,	'^'+80h,	'A'+80h,"ND"
                      	
  0309  CF52BEBDBC    		db	'O'+80h,"R",	'>'+80h,	'='+80h,	'<'+80h
  030E  D3474EC94E54C1		db	'S'+80h,"GN",	'I'+80h,"NT",	'A'+80h,"BS",	'U'+80h,"SR"
  031A  C65245C94E50CC		db	'F'+80h,"RE",	'I'+80h,"NP",	'L'+80h,"POS",	'P'+80h,"OS"
  0327  D35152D24E44CC		db	'S'+80h,"QR",	'R'+80h,"ND",	'L'+80h,"OG",	'E'+80h,"XP"
                      	
  0333  C34F53D3494ED4		db	'C'+80h,"OS",	'S'+80h,"IN",	'T'+80h,"AN",	'P'+80h,"EEK"
  0340  CC454EC8455824		db	'L'+80h,"EN",	'H'+80h,"EX$",	'S'+80h,"TR$",	'V'+80h,"AL"
  034E  C15343C3485224		db	'A'+80h,"SC",	'C'+80h,"HR$",	'L'+80h,"EFT$",	'R'+80h,"IGHT$"
  0360  CD494424D04F49		db	'M'+80h,"ID$",	'P'+80h,"OINT",	'C'+80h,"SRLIN",'S'+80h,"TICK"
                      	
  0374  D354524947D449		db	'S'+80h,"TRIG",	'T'+80h,"IME",	80h
                      	
                      	
  037E                	_ERRBELL:ds	ERRSTR-1-_ERRBELL
  0383                		org	ERRSTR-1
  0383                	ERRBELL:
  0383  07            		db	07h
                      	
  0384                	_ERRSTR:ds	ERRSTR-_ERRSTR
  0384                		org	ERRSTR
  0384  204572726F7200		db	" Error", 00h
                      	
  038B                	_ERRIN:	ds	ERRIN-_ERRIN
  038B                		org	ERRIN
  038B  20696E2000    		db	" in ", 00h
                      	
  0390                	_OK:
  0390                		ds	OK-_OK
  0390                		org	OK
  0390  4F6B0D0A00    		db	"Ok", 0dh, 0ah, 00h
                      	
  0395                	ERRID:
  0395  4E46534E52474F		db	"NF", "SN", "RG", "OD", "FC", "OV", "OM", "UL"
  03A5  425344442F3049		db	"BS", "DD", "/0", "ID", "TM", "OS", "LS", "ST"
  03B5  434E554654524D		db	"CN", "UF", "TR", "MO", "FD"
                      	
  03BF                	NFERR:
  03BF  1E00          		ld	e,00h
  03C1  01            		db	01h		;ld bc,****
                      	
  03C2                	C_TO:
  03C2                	C_TAB:
  03C2                	F_SPC:
  03C2                	C_THEN:
  03C2                	C_STEP:
                      	
                      	
  03C2                	SNERR:
  03C2  1E02          		ld	e,02h
  03C4  01            		db	01h		;ld bc,****
  03C5                	RGERR:
  03C5  1E04          		ld	e,04h
  03C7  01            		db	01h		;ld bc,****
  03C8                	ODERR:
  03C8  1E06          		ld	e,06h
  03CA  01            		db	01h		;ld bc,****
  03CB                	FCERR:
  03CB                	F_USR:
  03CB  1E08          		ld	e,08h
  03CD  01            		db	01h		;ld bc,****
  03CE                	OVERR:
  03CE  1E0A          		ld	e,0ah
  03D0  01            		db	01h		;ld bc,****
  03D1                	OMERR:
  03D1  1E0C          		ld	e,0ch
  03D3  01            		db	01h		;ld bc,****
  03D4                	ULERR:
  03D4  1E0E          		ld	e,0eh
  03D6  01            		db	01h		;ld bc,****
  03D7                	BSERR:
  03D7  1E10          		ld	e,10h
  03D9  01            		db	01h		;ld bc,****
  03DA                	DDERR:
  03DA  1E12          		ld	e,12h
  03DC  01            		db	01h		;ld bc,****
  03DD                	DIV0ERR:
  03DD  1E14          		ld	e,14h
  03DF  01            		db	01h		;ld bc,****
  03E0                	IDERR:
  03E0  1E16          		ld	e,16h
  03E2  01            		db	01h		;ld bc,****
  03E3                	TMERR:
  03E3  1E18          		ld	e,18h
  03E5  01            		db	01h		;ld bc,****
  03E6                	OSERR:
  03E6  1E1A          		ld	e,1ah
  03E8  01            		db	01h		;ld bc,****
  03E9                	LSERR:
  03E9  1E1C          		ld	e,1ch
  03EB  01            		db	01h		;ld bc,****
  03EC                	STERR:
  03EC  1E1E          		ld	e,1eh
  03EE  01            		db	01h		;ld bc,****
  03EF                	CNERR:
  03EF  1E20          		ld	e,20h
  03F1  01            		db	01h		;ld bc,****
  03F2                	UFERR:
  03F2  1E22          		ld	e,22h
  03F4  01            		db	01h		;ld bc,****
  03F5                	TRERR:
  03F5  1E24          		ld	e,24h
  03F7  01            		db	01h		;ld bc,****
  03F8                	MOERR:
  03F8  1E26          		ld	e,26h
  03FA  01            		db	01h		;ld bc,****
  03FB                	FDERR:
  03FB  1E28          		ld	e,28h
                      	
  03FD                	ERROR:
  03FD  CD8DFF        		call	HOOKERR
  0400  ED7B5BFA      		ld	sp,(STACK)
                      	
                      	;	xor	a
                      	;	ld	(STRDSC2),a
                      	;	ld	(STRDSC3),a
                      	;	ld	(STRDSC4),a
                      	;	dec	a
                      	;	ld	(STOPAD+1),a	;(STOPAD)>=fa00h
                      	
  0404  2131FF        		ld	hl,STRDSC2
  0407  01000C        		ld	bc,0c00h
  040A                	ERRORLP:
  040A  71            		ld	(hl),c
  040B  23            		inc	hl
  040C  10FC          		djnz	ERRORLP
  040E  2254FF        		ld	(STOPAD),hl	;>=fa00h
                      	
  0411  CD3C09        		call	PRTNLXTXT
                      	
                      	;print error message
                      	;input: e=error number
                      	;destroy: af,bc,de,hl
  0414                	PRTERR:
  0414  219503        		ld	hl,ERRID
  0417  7B            		ld	a,e
  0418  FE2A          		cp	2ah
  041A  3E3F          		ld	a,'?'
  041C  3806          		jr	c,PRTERRC
  041E  21E941        		ld	hl,ERRIDEX-2ah
  0421  CD7510        		call	PRTC
  0424                	PRTERRC:
  0424  1600          		ld	d,00h
  0426  19            		add	hl,de
                      	
  0427  0603          		ld	b,03h
  0429                	PRTERRLP:
  0429  CD7510        		call	PRTC
  042C  7E            		ld	a,(hl)
  042D  23            		inc	hl
  042E  10F9          		djnz	PRTERRLP
                      	
  0430  218303        		ld	hl,ERRBELL
  0433  CDCF30        		call	PUTS
                      	
  0436                	PRTLNUM:
  0436  CDB31B        		call	PLSTOP
                      	
  0439  2A5DFA        		ld	hl,(LINENUM)
  043C  7C            		ld	a,h
  043D  A5            		and	l
  043E  3C            		inc	a
  043F  C4993A        		call	nz,INLNUM	;if (LINENUM)<>0ffffh
                      	;	jp	EDIT
                      	
                      	
                      	;program edit and direct command mode
  0442                	_EDIT:	ds	EDIT-_EDIT
  0442                		org	EDIT
                      	
  0442  CD93FF        		call	HOOKEDT
  0445  CD5810        		call	CLRKBF
  0448  CD3F09        		call	PRTNLX		;for graphic screen
  044B  CD3C09        		call	PRTNLXTXT
  044E  219003        		ld	hl,OK
  0451  CDCF30        		call	PUTS
  0454                	EDIT2:
  0454  CD0127        		call	PUTPRNL
  0457  21FFFF        		ld	hl,0ffffh
  045A  225DFA        		ld	(LINENUM),hl
                      	
  045D                	EDITLP:
  045D  CDF928        		call	INPTSCR		;hl=INPBUF-1
  0460  DCB31B        		call	c,PLSTOP
  0463  CD7604        		call	INPTPRG
  0466  28F5          		jr	z,EDITLP
  0468  D20A07        		jp	nc,INTPRT
                      	
                      	;change link pointer
  046B                	CHGLKP:
  046B  CD99FF        		call	HOOKCLP
  046E  CDF934        		call	RESSTK
  0471  CDCD04        		call	CHGLKPMAIN
  0474  18DE          		jr	EDIT2
                      	
                      	
                      	;input program or command
                      	;input: hl=INPBUF-1
                      	;output: z-flag(1=no input), c-flag(1=program changed, 0=command)
                      	;destroy: af,bc,de,hl
  0476                	INPTPRG:
  0476  E5            		push	hl		;hl=INPBUF-1
  0477  CD2E05        		call	CNVIL
  047A  E1            		pop	hl		;hl=INPBUF-1
  047B  D7            		rst	ANADAT		;
  047C  3C            		inc	a
  047D  3D            		dec	a
  047E  D0            		ret	nc		;
                      	
                      	;change program
  047F                	CHGPRG:
  047F  CD6807        		call	GETLN
  0482  E5            		push	hl		;buffer address after line number
  0483  D5            		push	de		;line number
  0484  E5            		push	hl		;buffer address after line number
  0485  CDFB04        		call	SRCHLN
  0488  DCE504        		call	c,DELPRG	;c-flag=1
  048B  E1            		pop	hl		;buffer address after line number
  048C  34            		inc	(hl)
  048D  35            		dec	(hl)
  048E  2007          		jr	nz,INSPRG
  0490  D2D403        		jp	nc,ULERR
  0493  D1            		pop	de		;line number
  0494  E1            		pop	hl		;buffer address after line number
  0495  25            		dec	h		;reset z-flag
  0496  C9            		ret			;c-flag=1, delete line
                      	
                      	;insert program
  0497                	INSPRG:
  0497  AF            		xor	a
  0498  110500        		ld	de,0005h
  049B                	PRGLP:
  049B  1C            		inc	e
  049C  3C            		inc	a
  049D  23            		inc	hl
  049E  34            		inc	(hl)
  049F  35            		dec	(hl)
  04A0  20F9          		jr	nz,PRGLP
                      	
  04A2  2A56FF        		ld	hl,(VARAD)
  04A5  E5            		push	hl
                      	;	or	a
  04A6  ED42          		sbc	hl,bc
  04A8  E5            		push	hl		;slide size
  04A9  09            		add	hl,bc		;old (VARAD)
  04AA  19            		add	hl,de		;old (VARAD)+size
  04AB  CDCF34        		call	CHKSTK
  04AE  2256FF        		ld	(VARAD),hl
  04B1  EB            		ex	de,hl
  04B2  C1            		pop	bc		;slide size
  04B3  E1            		pop	hl		;old (VARAD)
  04B4  2B            		dec	hl
  04B5  1B            		dec	de
  04B6  EDB8          		lddr
                      	
  04B8                	COPYPRG:
  04B8  23            		inc	hl		;address to be inserted in
  04B9  23            		inc	hl
  04BA  13            		inc	de
  04BB  13            		inc	de
  04BC  73            		ld	(hl),e
  04BD  23            		inc	hl
  04BE  72            		ld	(hl),d
  04BF  23            		inc	hl
  04C0  D1            		pop	de		;line number
  04C1  73            		ld	(hl),e
  04C2  23            		inc	hl
  04C3  72            		ld	(hl),d
  04C4  23            		inc	hl
  04C5  EB            		ex	de,hl
  04C6  E1            		pop	hl		;buffer address after line number
                      	
  04C7  4F            		ld	c,a		;b=0
  04C8  EDB0          		ldir
  04CA  0C            		inc	c		;reset z-flag
  04CB  37            		scf
  04CC  C9            		ret
                      	
                      	
                      	;change link pointer (main)
                      	;output: hl=last address
                      	;destroy: af,bc,de
  04CD                	CHGLKPMAIN:
  04CD  ED5B5FFA      		ld	de,(STARTAD)
                      	;skip address and line number
  04D1                	CHGLKPLP:
  04D1  62            		ld	h,d
  04D2  6B            		ld	l,e
  04D3  7E            		ld	a,(hl)
  04D4  23            		inc	hl
  04D5  B6            		or	(hl)
  04D6  C8            		ret	z
                      	
  04D7  010300        		ld	bc,0003h
  04DA  09            		add	hl,bc
                      	
                      	;search for next line
  04DB  AF            		xor	a
  04DC  4F            		ld	c,a		;b=0
  04DD  EDB1          		cpir
                      	
                      	;next line address
  04DF  EB            		ex	de,hl
  04E0  73            		ld	(hl),e
  04E1  23            		inc	hl
  04E2  72            		ld	(hl),d
  04E3  18EC          		jr	CHGLKPLP
                      	
                      	
                      	;input: bc=address to be deleted, hl=next line address
                      	;output: c-flag=1
                      	;destroy: af,de,hl
  04E5                	DELPRG:
  04E5  C5            		push	bc		;address to be deleted
  04E6  EB            		ex	de,hl		;de <- next line address
  04E7  2A56FF        		ld	hl,(VARAD)
  04EA  B7            		or	a
  04EB  ED52          		sbc	hl,de
  04ED  44            		ld	b,h
  04EE  4D            		ld	c,l
  04EF  EB            		ex	de,hl		;hl <- next line address
  04F0  D1            		pop	de		;address to be deleted
  04F1  D5            		push	de
  04F2  EDB0          		ldir
  04F4  ED5356FF      		ld	(VARAD),de
  04F8  C1            		pop	bc
  04F9  37            		scf
  04FA  C9            		ret
                      	
                      	
                      	;search for line number
                      	;input: de=line number
                      	;output: z=1,c=1) bc=address, hl=next line address
                      	;	 z=0,c=0) bc=next line address, hl=next next line address
                      	;	 z=1,c=0) bc=hl=end address
                      	;destroy: af
  04FB                	SRCHLN:
  04FB  2A5DFA        		ld	hl,(LINENUM)
  04FE  E7            		rst	CPHLDE
  04FF  300C          		jr	nc,SRCHSTART	;less than or equal to current line number
  0501  2A4EFF        		ld	hl,(PROGAD)
  0504                	SRCHLP1:
  0504  7E            		ld	a,(hl)
  0505  23            		inc	hl
  0506  B7            		or	a
  0507  20FB          		jr	nz,SRCHLP1
                      	
                      	;for PORTOPIA (mk2)
                      	;	jr	SRCHLP2
  0509  CD1005        		call	SRCHLP2
  050C  D8            		ret	c
                      	
  050D                	SRCHSTART:
  050D  2A5FFA        		ld	hl,(STARTAD)	;search from program start address
                      	
  0510                	SRCHLP2:
  0510  44            		ld	b,h
  0511  4D            		ld	c,l
  0512  7E            		ld	a,(hl)
  0513  23            		inc	hl
  0514  B6            		or	(hl)
  0515  2813          		jr	z,SRCHEND	;program end
  0517  23            		inc	hl
  0518  23            		inc	hl
  0519  7E            		ld	a,(hl)
  051A  2B            		dec	hl
  051B  BA            		cp	d
  051C  2002          		jr	nz,SRCHNZ
  051E  7E            		ld	a,(hl)
  051F  BB            		cp	e
  0520                	SRCHNZ:
  0520  0A            		ld	a,(bc)
  0521  2B            		dec	hl
  0522  66            		ld	h,(hl)
  0523  6F            		ld	l,a
  0524  38EA          		jr	c,SRCHLP2	;(hl,hl-1)<de
  0526  23            		inc	hl
  0527  2001          		jr	nz,SRCHEND	;(hl,hl-1)>de
  0529  37            		scf
  052A                	SRCHEND:
  052A  2B            		dec	hl
  052B  0B            		dec	bc
  052C  2B            		dec	hl
  052D  C9            		ret
                      	
                      	
                      	;convert to intermediate language
                      	;input: hl=INPBUF-1
  052E                	CNVIL:
  052E  54            		ld	d,h
  052F  5D            		ld	e,l
                      	
  0530                	ILLP1:
  0530  13            		inc	de
  0531  1A            		ld	a,(de)
                      	
  0532  B7            		or	a
  0533  FA3005        		jp	m,ILLP1		;skip kana
                      	
  0536  23            		inc	hl
  0537  2827          		jr	z,ILEND1	;00=line end
                      	
  0539  CDE00B        		call	CHKFIG
  053C  3818          		jr	c,ILPUT
                      	
  053E  FE22          		cp	22h		;double quotation mark
  0540  2817          		jr	z,ILDQ
  0542  FE14          		cp	14h
  0544  280C          		jr	z,ILGRP
  0546  FE2A          		cp	'*'
  0548  380C          		jr	c,ILPUT		;00h-29h
  054A  FE3F          		cp	'?'
  054C  203D          		jr	nz,ILCMP
                      	
                      	;question mark
  054E  3695          		ld	(hl),PRINT_	;95h
  0550  18DE          		jr	ILLP1
                      	
                      	;graphic character
  0552                	ILGRP:
  0552  77            		ld	(hl),a
  0553  23            		inc	hl
  0554  13            		inc	de
  0555                	ILPUT2:
  0555  1A            		ld	a,(de)
  0556                	ILPUT:
  0556  77            		ld	(hl),a
  0557  18D7          		jr	ILLP1
                      	
                      	
                      	;double quotation mark
  0559                	ILDQ:
  0559  77            		ld	(hl),a
                      	
                      	;REM
  055A                	ILREM:
  055A  4F            		ld	c,a		;double quotation mark or 0(REM)
  055B  0601          		ld	b,01h		;double quotation counter
                      	
                      	;REM,DATA,""
                      	;loop until (de)=0 or (de)=c
  055D                	ILLP2:
  055D  13            		inc	de
  055E  23            		inc	hl
  055F  1A            		ld	a,(de)
  0560                	ILEND1:
  0560  77            		ld	(hl),a
  0561  B7            		or	a
  0562  280E          		jr	z,ILEND2
  0564  FE22          		cp	22h
  0566  2001          		jr	nz,ILNZ
  0568  04            		inc	b
  0569                	ILNZ:
  0569  CB40          		bit	0,b
  056B  20F0          		jr	nz,ILLP2
  056D  B9            		cp	c
  056E  20ED          		jr	nz,ILLP2
  0570  18BE          		jr	ILLP1
                      	
  0572                	ILEND2:
  0572  23            		inc	hl
  0573  77            		ld	(hl),a		;a=0
  0574  23            		inc	hl
  0575  77            		ld	(hl),a		;a=0
  0576  C9            		ret
                      	
                      	
                      	;found
  0577                	ILFOUND:
  0577  F1            		pop	af		;cancel
  0578  E1            		pop	hl
  0579  70            		ld	(hl),b
  057A  78            		ld	a,b
  057B  D68E          		sub	REM_
  057D  28DB          		jr	z,ILREM
  057F  FEF5          		cp	DATA_-REM_
  0581  20AD          		jr	nz,ILLP1
                      	;DATA
  0583                	ILDAT:
  0583  013A00        		ld	bc,003ah	;b=0, c=':'
  0586  18D5          		jr	ILLP2
                      	
                      	
                      	;not found
  0588                	ILNC:
  0588  E1            		pop	hl
  0589  18CA          		jr	ILPUT2
                      	
                      	
                      	;compare with command string
  058B                	ILCMP:
  058B  CDEF0B        		call	TOUPPER
  058E  12            		ld	(de),a
                      	
  058F  E5            		push	hl
  0590  212C02        		ld	hl,CMDNAME
  0593  0680          		ld	b,80h
                      	
  0595                	ILLP3:
  0595  1A            		ld	a,(de)
  0596  F680          		or	80h
  0598  BE            		cp	(hl)
  0599  2018          		jr	nz,ILLP6
                      	
                      	;1st character matched
  059B  D5            		push	de
  059C                	ILLP4:
  059C  23            		inc	hl
  059D  7E            		ld	a,(hl)
  059E  07            		rlca
  059F  38D6          		jr	c,ILFOUND
                      	
  05A1  78            		ld	a,b
  05A2  FE88          		cp	GOTO_
  05A4                	ILLP5:
  05A4  13            		inc	de
  05A5  1A            		ld	a,(de)
  05A6  2004          		jr	nz,NOGOTO
  05A8  FE20          		cp	' '
  05AA  28F8          		jr	z,ILLP5
  05AC                	NOGOTO:
  05AC  CDEF0B        		call	TOUPPER
  05AF  BE            		cp	(hl)
  05B0  28EA          		jr	z,ILLP4
  05B2  D1            		pop	de
                      	
  05B3                	ILLP6:
  05B3  23            		inc	hl
  05B4  7E            		ld	a,(hl)
  05B5  07            		rlca
  05B6  30FB          		jr	nc,ILLP6
  05B8  04            		inc	b
                      	
  05B9  3A65FE        		ld	a,(MODE)
  05BC  FE02          		cp	02h
  05BE  D27742        		jp	nc,CNVIL66
                      	
  05C1  78            		ld	a,b
  05C2  FEF2          		cp	FNCLAST+1
  05C4  30C2          		jr	nc,ILNC
  05C6  FEAB          		cp	CMDLAST+1
  05C8  20CB          		jr	nz,ILLP3
  05CA  06C2          		ld	b,TAB_
  05CC  18C7          		jr	ILLP3
                      	
                      	
                      	;LLIST command
  05CE                	C_LLST:
  05CE  3E01          		ld	a,01h
  05D0  3258FA        		ld	(DEVICE),a
                      	;	jp	C_LIST
                      	
                      	
                      	;LIST command
  05D3                	C_LIST:
  05D3  F1            		pop	af		;cancel return address
  05D4  110000        		ld	de,0000h
  05D7  CD603F        		call	CHKCLN
  05DA  2814          		jr	z,LISTTOEND
                      	
  05DC  CD6807        		call	GETLN
  05DF  D5            		push	de		;start line number
  05E0  CD603F        		call	CHKCLN
  05E3  281D          		jr	z,LISTSTRT
                      	
  05E5  FECB          		cp	MINUS_
  05E7  C2C203        		jp	nz,SNERR
  05EA  CD5F3F        		call	CHKCLNINC
  05ED  2007          		jr	nz,LISTNZ
  05EF  D1            		pop	de
  05F0                	LISTTOEND:
  05F0  01FAFF        		ld	bc,65530
  05F3  C5            		push	bc
  05F4  180C          		jr	LISTSTRT
                      	
  05F6                	LISTNZ:
  05F6  CD6807        		call	GETLN
  05F9  CD603F        		call	CHKCLN
  05FC  C2C203        		jp	nz,SNERR
                      	
  05FF  E1            		pop	hl
  0600  D5            		push	de		;push end line number
  0601  EB            		ex	de,hl		;de <- start line number
                      	
                      	
                      	;de=start line number, (sp)=end line number
  0602                	LISTSTRT:
  0602  CDFB04        		call	SRCHLN
  0605  60            		ld	h,b
  0606  69            		ld	l,c
                      	
  0607                	LISTLP1:
  0607  CD4D27        		call	STOPESC
                      	
  060A  23            		inc	hl
  060B  7E            		ld	a,(hl)
  060C  23            		inc	hl
  060D  B6            		or	(hl)
  060E  2843          		jr	z,LISTEND2
                      	
  0610  CD3927        		call	PUTNL
                      	
  0613  D1            		pop	de		;end line number
  0614  D5            		push	de
                      	
  0615  23            		inc	hl
  0616  7E            		ld	a,(hl)
  0617  23            		inc	hl
  0618  E5            		push	hl
  0619  66            		ld	h,(hl)
  061A  6F            		ld	l,a
                      	
  061B  13            		inc	de
  061C  E7            		rst	CPHLDE
  061D  3033          		jr	nc,LISTEND
                      	
  061F  CDA13A        		call	PUTI2
  0622  3E20          		ld	a,' '
  0624  CDC726        		call	PUTDEV
                      	
  0627  E1            		pop	hl
                      	
  0628  110000        		ld	de,0000h	;double quotation mark and DATA counter
  062B  4B            		ld	c,e		;REM counter
  062C                	LISTLP3:
  062C  23            		inc	hl
  062D  7E            		ld	a,(hl)
  062E  B7            		or	a
  062F  28D6          		jr	z,LISTLP1
                      	
  0631  FE22          		cp	22h
  0633  2822          		jr	z,LISTDQ
                      	
  0635  CB42          		bit	0,d
  0637  2014          		jr	nz,LISTPUT
                      	
  0639  1C            		inc	e
  063A  1D            		dec	e
  063B  201D          		jr	nz,LISTCHKCLN
                      	
  063D  0C            		inc	c
  063E  0D            		dec	c
  063F  200C          		jr	nz,LISTPUT
                      	
  0641  FE8E          		cp	REM_
  0643  281C          		jr	z,LISTREM
  0645  FE83          		cp	DATA_
  0647  2819          		jr	z,LISTDATA
  0649  B7            		or	a
  064A  FA6306        		jp	m,LISTCMD
  064D                	LISTPUT:
  064D  CDC726        		call	PUTDEV
  0650  18DA          		jr	LISTLP3
                      	
  0652                	LISTEND:
  0652  F1            		pop	af		;cancel stack
  0653                	LISTEND2:
  0653  F1            		pop	af		;cancel stack
  0654  C34204        		jp	EDIT
                      	
  0657                	LISTDQ:
  0657  14            		inc	d
  0658  18F3          		jr	LISTPUT
                      	
  065A                	LISTCHKCLN:
  065A  FE3A          		cp	':'
  065C  20EF          		jr	nz,LISTPUT
  065E  1D            		dec	e
  065F  18EC          		jr	LISTPUT
                      	
  0661                	LISTREM:
  0661  0C            		inc	c
  0662                	LISTDATA:
  0662  1C            		inc	e
  0663                	LISTCMD:
  0663  E5            		push	hl
  0664  CDB525        		call	CNVASC
                      	
  0667                	LISTLP4:
  0667  CDC726        		call	PUTDEV
  066A  23            		inc	hl
  066B  7E            		ld	a,(hl)
  066C  B7            		or	a
  066D  F26706        		jp	p,LISTLP4
  0670  E1            		pop	hl
  0671  18B9          		jr	LISTLP3
                      	
                      	
                      	;FOR command
  0673                	C_FOR:
  0673  F1            		pop	af		;cancel return address
  0674  CD8F33        		call	CHKVAR
  0677  7E            		ld	a,(hl)
  0678  FE28          		cp	'('
  067A  CAC203        		jp	z,SNERR
                      	
  067D  CDA932        		call	GETVAR
  0680  2A4EFF        		ld	hl,(PROGAD)
                      	
  0683  D5            		push	de		;variable address
                      	
  0684  CD0408        		call	VARIN
  0687  CD9B0B        		call	CHKNUM
  068A  7E            		ld	a,(hl)
  068B  FEC3          		cp	TO_
  068D  C2C203        		jp	nz,SNERR
  0690  CD910B        		call	NARGMOINC
  0693  EB            		ex	de,hl
  0694  CD2539        		call	PUSHF1		;TO value
  0697  EB            		ex	de,hl
  0698  CD603F        		call	CHKCLN		;a=(hl)
  069B  224EFF        		ld	(PROGAD),hl
  069E  CC6422        		call	z,SETPLS1	;no change in z-flag
  06A1  2808          		jr	z,STEP1
  06A3  FEC9          		cp	STEP_
  06A5  C2C203        		jp	nz,SNERR
  06A8  CD910B        		call	NARGMOINC
  06AB                	STEP1:
  06AB  CD4339        		call	POPF2		;TO value
  06AE  C1            		pop	bc		;variable address
                      	
  06AF  2A4EFF        		ld	hl,(PROGAD)
  06B2  E5            		push	hl		;program address
  06B3  210200        		ld	hl,0002h
  06B6  39            		add	hl,sp
  06B7                	FORLP:
  06B7  CDD407        		call	SRCHSTK2
  06BA  280F          		jr	z,FOROK		;not found or gosub identifier found
                      	
                      	;check program address
  06BC  111100        		ld	de,0011h
  06BF  19            		add	hl,de
  06C0  5E            		ld	e,(hl)
  06C1  23            		inc	hl
  06C2  56            		ld	d,(hl)
  06C3  23            		inc	hl
  06C4  E3            		ex	(sp),hl
  06C5  E7            		rst	CPHLDE
  06C6  E3            		ex	(sp),hl
  06C7  20EE          		jr	nz,FORLP
                      	
  06C9  F9            		ld	sp,hl
  06CA  D5            		push	de		;program address
                      	
  06CB                	FOROK:
  06CB  2A5DFA        		ld	hl,(LINENUM)
  06CE  E5            		push	hl		;line number
  06CF  CD1A3C        		call	PUSHF2		;TO value
  06D2  CD2539        		call	PUSHF1		;STEP value
  06D5  C5            		push	bc		;variable address
  06D6  3E81          		ld	a,FOR_		;identifier
  06D8  F5            		push	af
  06D9  33            		inc	sp
  06DA  180B          		jr	INTPEND
                      	
                      	
                      	;
                      	;
                      	;
                      	;
  06DC                	_COMMAND:ds	CMDEND-14-_COMMAND
  06DC                		org	CMDEND-14
                      	
                      	;execute command
  06DC                	COMMAND:
  06DC  FEC2          		cp	CLAST66+1	;c2h
  06DE  D2C203        		jp	nc,SNERR
  06E1  1161FA        		ld	de,CMDTBL
  06E4  CD493F        		call	JPTBL
                      	
                      	;intepretation end
  06E7                	INTPEND:
  06E7  2A4EFF        		ld	hl,(PROGAD)
                      	
                      	;command end
  06EA                	_CMDEND:ds	CMDEND-_CMDEND
  06EA                		org	CMDEND
                      	
  06EA  2B            		dec	hl
  06EB  D7            		rst	ANADAT
                      	
                      	;	call	CHKCLN
                      	
  06EC  CA0A07        		jp	z,INTPRT
  06EF  C3C203        		jp	SNERR
                      	
                      	
  06F2                	RUNLP:
  06F2  7E            		ld	a,(hl)
  06F3  23            		inc	hl
  06F4  B6            		or	(hl)
  06F5  CA4204        		jp	z,EDIT
  06F8  23            		inc	hl
  06F9  5E            		ld	e,(hl)
  06FA  23            		inc	hl
  06FB  56            		ld	d,(hl)
  06FC  ED535DFA      		ld	(LINENUM),de
  0700  C30907        		jp	INTPRT2
                      	
                      	
                      	;0709h: used by PORTOPIA
  0703                	_INTPRT2:ds	INTPRT2-_INTPRT2
  0709                		org	INTPRT2
                      	
  0709  23            		inc	hl
                      	
  070A                	INTPRT:
  070A  CD5B27        		call	CHKSTOP
  070D  CA3735        		jp	z,STOP1
  0710  7E            		ld	a,(hl)
  0711  23            		inc	hl
  0712  224EFF        		ld	(PROGAD),hl
  0715  B7            		or	a
  0716  FADC06        		jp	m,COMMAND
  0719  28D7          		jr	z,RUNLP
  071B  FE3A          		cp	':'
  071D  28EB          		jr	z,INTPRT
  071F  FE20          		cp	' '
  0721  28E7          		jr	z,INTPRT
  0723  2B            		dec	hl
  0724  CDFB07        		call	C_LET
  0727  18BE          		jr	INTPEND
                      	
                      	
                      	;continued from C_RUN
                      	;RUN with parameter
  0729                	RUNPAR:
  0729  3807          		jr	c,RUNLN		;with line number
  072B  3A3BFB        		ld	a,(DRIVES)
  072E  B7            		or	a
  072F  C23B57        		jp	nz,RUNDSK
                      	
                      	;RUN with line number
  0732                	RUNLN:
  0732  CDF934        		call	RESSTK
  0735  2A4EFF        		ld	hl,(PROGAD)
  0738  CDA507        		call	C_GOTO
  073B  60            		ld	h,b
  073C  69            		ld	l,c
  073D  23            		inc	hl
  073E  18B2          		jr	RUNLP
                      	
                      	
                      	;convert float to 2-byte integer for USR() [-32768,32767]
                      	;input: FAC1
                      	;output: de
                      	;destroy: af,bc,hl
  0740                	_FTOI2:	ds	FTOI2-_FTOI2
  0741                		org	FTOI2
                      	
  0741  CD9F0C        		call	FTOI
  0744  3A69FF        		ld	a,(FAC1+3)
  0747  AA            		xor	d
  0748  F0            		ret	p
  0749  C3CB03        		jp	FCERR
                      	
                      	
                      	;convert float to 1-byte integer [0,255]
                      	;input: FAC1
                      	;output: e, a(0=ok)
                      	;destroy: f,bc,de,hl
  074C                	FTOI1:
  074C  CD4107        		call	FTOI2
  074F  7A            		ld	a,d
  0750  B7            		or	a
  0751  C8            		ret	z
  0752  C3CB03        		jp	FCERR
                      	
                      	
                      	;convert FAC1 and FAC2 to 2-byte integer
                      	;input: FAC1, FAC2
                      	;output: hl, de
                      	;destroy: af,bc,FAC1,FAC2
  0755                	F12TOI2:
  0755  3A25FF        		ld	a,(ARGTYP)
  0758  B7            		or	a
  0759  C2E303        		jp	nz,TMERR
  075C  CD4107        		call	FTOI2
  075F  D5            		push	de		;
  0760  CD6539        		call	EXFAC
  0763  CD4107        		call	FTOI2
  0766  E1            		pop	hl		;
  0767  C9            		ret
                      	
                      	
                      	;get line number from string (0-65529)
                      	;input: hl=program address
                      	;output: de=line number, hl=next address
                      	;destroy: af,bc
  0768                	GETLN:
  0768  CDCA27        		call	ATOI2
  076B  E5            		push	hl
  076C  21F9FF        		ld	hl,65529
  076F  E7            		rst	CPHLDE
  0770  E1            		pop	hl
  0771  D0            		ret	nc
  0772  119919        		ld	de,6553
  0775                	GETLNLP:
  0775  2B            		dec	hl
  0776  7E            		ld	a,(hl)
  0777  FE20          		cp	' '
  0779  C0            		ret	nz
  077A  18F9          		jr	GETLNLP
                      	
                      	
                      	;RUN command
  077C                	_C_RUN:	ds	C_RUN-_C_RUN
  0781                		org	C_RUN
                      	
  0781  2B            		dec	hl
  0782  D7            		rst	ANADAT
  0783  20A4          		jr	nz,RUNPAR	;with parameter
  0785                	RUN:
  0785  CDF934        		call	RESSTK
  0788                	RUNFIRST:
  0788  2A5FFA        		ld	hl,(STARTAD)
  078B  C3F206        		jp	RUNLP
                      	
                      	
                      	;GOSUB command
  078E                	C_GSUB:
  078E  CDA507        		call	C_GOTO
  0791  2B            		dec	hl
  0792                	GSUBLP:
  0792  CD5F3F        		call	CHKCLNINC
  0795  20FB          		jr	nz,GSUBLP
                      	
  0797  E3            		ex	(sp),hl
  0798  E5            		push	hl
  0799  CDCC34        		call	CHKFRE
  079C  2A5DFA        		ld	hl,(LINENUM)
  079F  E3            		ex	(sp),hl
  07A0  3E8C          		ld	a,GOSUB_
  07A2  F5            		push	af		;gosub identifier
  07A3  33            		inc	sp
  07A4                	JPHL:
  07A4  E9            		jp	(hl)
                      	
                      	
                      	;GOTO command
                      	;output: bc=new address, hl=old address (after line number)
  07A5                	C_GOTO:
  07A5  CD6807        		call	GETLN
  07A8  E5            		push	hl
  07A9  CDFB04        		call	SRCHLN
  07AC  E1            		pop	hl
  07AD  D2D403        		jp	nc,ULERR
  07B0  ED434EFF      		ld	(PROGAD),bc
  07B4  C9            		ret
                      	
                      	
                      	;RETURN command
  07B5                	C_RET:
  07B5  C1            		pop	bc		;return address
  07B6                	RETLP:
  07B6  CDD007        		call	SRCHSTK
  07B9  D2C503        		jp	nc,RGERR	;not found
  07BC  2807          		jr	z,RETOK		;gosub identifier found
  07BE  211300        		ld	hl,0013h
  07C1  39            		add	hl,sp
  07C2  F9            		ld	sp,hl
  07C3  18F1          		jr	RETLP
                      	
  07C5                	RETOK:
  07C5  33            		inc	sp
  07C6  E1            		pop	hl
  07C7  225DFA        		ld	(LINENUM),hl
  07CA  E1            		pop	hl
  07CB  224EFF        		ld	(PROGAD),hl
  07CE  C5            		push	bc		;return address
  07CF  C9            		ret
                      	
                      	
                      	;search stack for for/gosub identifier
                      	;output: c-flag(1=found), z-flag(0=for/1=gosub if found)
                      	;destroy: af,de,hl
  07D0                	SRCHSTK:
  07D0  210200        		ld	hl,0002h
  07D3  39            		add	hl,sp
                      	
                      	;search (hl)
                      	;destroy: af,de
  07D4                	SRCHSTK2:
  07D4  ED5B5BFA      		ld	de,(STACK)
  07D8  E7            		rst	CPHLDE
  07D9  D0            		ret	nc		;(z-flag=1)
  07DA  7E            		ld	a,(hl)
  07DB  FE8C          		cp	GOSUB_
  07DD  37            		scf
  07DE  C9            		ret
                      	
                      	
                      	;DATA command
  07DF                	_C_DATA:ds	C_DATA-_C_DATA
  07E0                		org	C_DATA
                      	
  07E0  013A          		db	01h, ':'	;ld bc,0e3ah/nop
                      	
                      	
                      	;REM command
  07E2                	_C_REM:	ds	C_REM-_C_REM
  07E2                		org	C_REM
                      	
  07E2  0E00          		ld	c,00h
                      	
                      	;input: c
  07E4                	DATREM:
  07E4  0600          		ld	b,00h
  07E6                	DATALP:
  07E6  7E            		ld	a,(hl)
  07E7  B7            		or	a
  07E8  CACB0B        		jp	z,SETPRGAD
                      	
  07EB  CB40          		bit	0,b
  07ED  2004          		jr	nz,DATANZ
  07EF  B9            		cp	c
  07F0  CACB0B        		jp	z,SETPRGAD
  07F3                	DATANZ:
  07F3  23            		inc	hl
  07F4  FE22          		cp	22h		;double quotation mark
  07F6  20EE          		jr	nz,DATALP
  07F8  04            		inc	b
  07F9  18EB          		jr	DATALP
                      	
                      	
                      	;LET command
  07FB                	C_LET:
  07FB  CD8F33        		call	CHKVAR
  07FE  CDA932        		call	GETVAR
  0801  2A4EFF        		ld	hl,(PROGAD)
                      	;	jp	VARIN
                      	
                      	
                      	;put into variable
                      	;input: bc=variable name, hl=program address, de=variable address
                      	;output: hl=next program address
                      	;destroy: FAC1,FAC2,af,bc,de
  0804                	VARIN:
  0804  CD593F        		call	SKIPSP
  0807  FED2          		cp	EQ_		;0d2h
  0809  C2C203        		jp	nz,SNERR
                      	
  080C  D5            		push	de		;variable address
  080D  23            		inc	hl
  080E  CB79          		bit	7,c
  0810  2814          		jr	z,VARNUM
                      	
                      	;string
  0812  CD8126        		call	STRARG
  0815  47            		ld	b,a
  0816  7A            		ld	a,d
  0817  FEFA          		cp	0fah		;fa00h=work area start address
  0819  78            		ld	a,b
  081A  EB            		ex	de,hl
  081B  D44227        		call	nc,MAKESTR
  081E  212DFF        		ld	hl,STRDSC1
  0821  010400        		ld	bc,0004h
  0824  1809          		jr	VARINOK
                      	
  0826                	VARNUM:
  0826  CD920B        		call	NARGMO
  0829  2166FF        		ld	hl,FAC1
  082C  010500        		ld	bc,0005h
  082F                	VARINOK:
  082F  D1            		pop	de		;variable address
  0830  EDB0          		ldir
  0832  2A4EFF        		ld	hl,(PROGAD)
  0835  C9            		ret
                      	
                      	
                      	;ON command
  0836                	C_ON:
  0836  CDE40D        		call	INT1ARG
  0839  7E            		ld	a,(hl)
  083A  57            		ld	d,a		;
  083B  F604          		or	04h
  083D  FE8C          		cp	GOSUB_		;88h(GOTO) or 8ch(GOSUB)
  083F  C2C203        		jp	nz,SNERR
                      	
  0842                	ONLP:
  0842  23            		inc	hl
  0843  1D            		dec	e
  0844  280E          		jr	z,ONZ
  0846  D5            		push	de
  0847  CD6807        		call	GETLN
  084A  224EFF        		ld	(PROGAD),hl
  084D  D1            		pop	de
  084E  7E            		ld	a,(hl)
  084F  FE2C          		cp	','
  0851  28EF          		jr	z,ONLP
  0853  C9            		ret
                      	
  0854                	ONZ:
  0854  F1            		pop	af		;cancel return address
  0855  7A            		ld	a,d		;
  0856  C3DC06        		jp	COMMAND
                      	
                      	
                      	;IF command
  0859                	C_IF:
  0859  CD920B        		call	NARGMO
  085C  7E            		ld	a,(hl)
  085D  FEC7          		cp	THEN_
  085F  2805          		jr	z,IFOK
  0861  FE88          		cp	GOTO_
  0863  C2C203        		jp	nz,SNERR
  0866                	IFOK:
  0866  3A6AFF        		ld	a,(FAC1+4)
  0869  B7            		or	a
  086A  CAE207        		jp	z,C_REM
                      	;	call	SKIPSPINC
                      	;	sub	'0'
                      	;	cp	'9'-'0'+1
                      	
  086D  D7            		rst	ANADAT
                      	
  086E  DAA507        		jp	c,C_GOTO
  0871                	IFEND:
  0871  F1            		pop	af		;cancel return address
  0872  C30A07        		jp	INTPRT		;skip colon check
                      	
                      	
                      	;LPRINT command
  0875                	_C_LPRT:ds	C_LPRT-_C_LPRT
  087A                		org	C_LPRT
                      	
  087A  3E01          		ld	a,01h
  087C  1806          		jr	PRTDEV
                      	
                      	
                      	;PRINT command
  087E                	_C_PRT:	ds	C_PRT-_C_PRT
  087E                		org	C_PRT
                      	
  087E  CDAEFF        		call	HOOKPRT
  0881  CD9F27        		call	DEVNUM
  0884                	PRTDEV:
  0884  3258FA        		ld	(DEVICE),a
  0887  07            		rlca
  0888  DCA825        		call	c,PRTOPN
                      	
  088B                	PRTLP1:
  088B  CD603F        		call	CHKCLN		;a=(hl)
  088E  CA4B09        		jp	z,PRTEOL
  0891                	PRTLP2:
  0891  23            		inc	hl
  0892  FE3B          		cp	';'
  0894  284C          		jr	z,PRTNEXT
  0896  FE2C          		cp	','
  0898  2842          		jr	z,PRTCMM
  089A  FEC5          		cp	SPC_
  089C  287C          		jr	z,SPCTAB
  089E  D6C2          		sub	TAB_
  08A0  2878          		jr	z,SPCTAB
                      	
  08A2  2B            		dec	hl
  08A3  CD4717        		call	ARG
  08A6  3A25FF        		ld	a,(ARGTYP)
  08A9  B7            		or	a
  08AA  2813          		jr	z,PRTNUM
                      	
                      	;string
  08AC  CD8A26        		call	STRARG2
  08AF  2A4EFF        		ld	hl,(PROGAD)
  08B2  B7            		or	a
  08B3  28D6          		jr	z,PRTLP1
  08B5  47            		ld	b,a
  08B6                	PRTSLP:
  08B6  1A            		ld	a,(de)
  08B7  13            		inc	de
  08B8  CDC726        		call	PUTDEV
  08BB  10F9          		djnz	PRTSLP
  08BD  18CC          		jr	PRTLP1
                      	
  08BF                	PRTNUM:
  08BF  E5            		push	hl
  08C0  CDAC3A        		call	FTOA
                      	
  08C3  3AACFD        		ld	a,(WIDTH)
  08C6  C603          		add	a,03h
  08C8  4F            		ld	c,a
  08C9  AF            		xor	a
  08CA  EDB1          		cpir
  08CC  3AA9FD        		ld	a,(CSRX)
  08CF  B9            		cp	c
  08D0  D43927        		call	nc,PUTNL
  08D3  2172FF        		ld	hl,FAC3
  08D6  CDCF30        		call	PUTS
  08D9  E1            		pop	hl
  08DA  18AF          		jr	PRTLP1
                      	
  08DC                	PRTCMM:
  08DC  CDE908        		call	CMMMAIN
  08DF  D43927        		call	nc,PUTNL
                      	
  08E2                	PRTNEXT:
  08E2  CD603F        		call	CHKCLN
  08E5  20AA          		jr	nz,PRTLP2
  08E7  1865          		jr	PRTEND
                      	
  08E9                	CMMMAIN:
  08E9  1E0E          		ld	e,0eh
  08EB  3A58FA        		ld	a,(DEVICE)
  08EE  B7            		or	a
  08EF  280E          		jr	z,CMM0
  08F1  3D            		dec	a
  08F2  201F          		jr	nz,CMM2
                      	
                      	;printer
  08F4                	CMM1:
  08F4  0608          		ld	b,08h
  08F6                	TAB1:
  08F6  3A57FA        		ld	a,(PRTPOS)
  08F9                	TABLP:
  08F9  93            		sub	e
  08FA  380B          		jr	c,PUTSPNEG
  08FC  10FB          		djnz	TABLP
  08FE  C9            		ret
                      	
                      	;CRT
  08FF                	CMM0:
  08FF  0601          		ld	b,01h
  0901                	TAB0:
  0901  3AA9FD        		ld	a,(CSRX)
  0904  3D            		dec	a
  0905  18F2          		jr	TABLP
                      	
  0907                	PUTSPNEG:
  0907  ED44          		neg
  0909                	PUTSP:
  0909  47            		ld	b,a
  090A                	PUTSPLP:
  090A  3E20          		ld	a,' '
  090C  CDC726        		call	PUTDEV
  090F  10F9          		djnz	PUTSPLP
  0911  37            		scf
  0912  C9            		ret
                      	
  0913                	CMM2:
                      	;RS-232C,CMT
                      	;for PC-6001
                      	;	ld	b,0eh
                      	;	jr	PUTSPLP
                      	;for PC-6001mkII/6601/SR
  0913  3E2C          		ld	a,','
  0915  CDC726        		call	PUTDEV
  0918  37            		scf
  0919  C9            		ret
                      	
  091A                	SPCTAB:
  091A  CD1F09        		call	SPCTABMAIN
  091D  18C3          		jr	PRTNEXT
                      	
  091F                	SPCTABMAIN:
  091F  F5            		push	af		;00=tab
  0920  CD890B        		call	FNCNUMR
  0923  E5            		push	hl
  0924  CD4C07        		call	FTOI1
  0927  E1            		pop	hl
  0928  F1            		pop	af
  0929  B7            		or	a
  092A  200B          		jr	nz,PRTSPC
                      	
  092C                	PRTTAB:
  092C  0601          		ld	b,01h
  092E  3A58FA        		ld	a,(DEVICE)
  0931  B7            		or	a
  0932  28CD          		jr	z,TAB0		;0=CRT
  0934  3D            		dec	a
  0935  28BF          		jr	z,TAB1		;1=printer
                      	
  0937                	PRTSPC:
  0937  7B            		ld	a,e
  0938  B7            		or	a
  0939  20CE          		jr	nz,PUTSP
  093B  C9            		ret
                      	
                      	
                      	;change to text screen and print new line in CRT
                      	;if (CSRX)>1 then print new line
  093C                	PRTNLXTXT:
  093C  CD7C13        		call	CHGTXT
  093F                	PRTNLX:
  093F  AF            		xor	a
  0940  3258FA        		ld	(DEVICE),a
  0943  3AA9FD        		ld	a,(CSRX)
  0946  3D            		dec	a
  0947  C8            		ret	z
  0948  C33927        		jp	PUTNL
                      	
                      	
  094B                	PRTEOL:
                      	;	xor	a
                      	;	ld	(GRPFLG),a
  094B  CD3927        		call	PUTNL
                      	
  094E                	PRTEND:
  094E  224EFF        		ld	(PROGAD),hl
                      	
                      	;print# and input# end
                      	;destroy: af,hl
  0951                	_PIEND:	ds	PIEND-_PIEND
  0955                		org	PIEND
                      	
  0955  CD8AFF        		call	HOOKPED
  0958  2158FA        		ld	hl,DEVICE
  095B  7E            		ld	a,(hl)
  095C  3600          		ld	(hl),00h
  095E  07            		rlca
  095F  D0            		ret	nc
  0960  C3061B        		jp	WCLOSE
                      	
                      	
  0963                	REDO:
  0963  3F5265646F2066		db	"?Redo from start", 0dh, 0ah, 00h
                      	
                      	;input: a=input(0)/read(>0), hl=input address
                      	;output: a=length, hl=next address, bc=start address
                      	;destroy: af,bc
  0976                	INPRDSTR:
  0976  3249FF        		ld	(INPRD),a
  0979  0600          		ld	b,00h		;length
  097B  CD593F        		call	SKIPSP
  097E  FE22          		cp	22h
  0980  200E          		jr	nz,IRSNDQ	;no double quotation
                      	
                      	;with double quotation
  0982  23            		inc	hl
  0983  E5            		push	hl
  0984                	IRSLP1:
  0984  7E            		ld	a,(hl)
  0985  B7            		or	a
  0986  281F          		jr	z,IRSEND
  0988  23            		inc	hl
  0989  FE22          		cp	22h
  098B  281A          		jr	z,IRSEND
  098D  04            		inc	b
                      	;	jp	z,LSERR
  098E  18F4          		jr	IRSLP1
                      	
                      	;no double quotation
  0990                	IRSNDQ:
  0990  E5            		push	hl
  0991                	IRSLP2:
  0991  7E            		ld	a,(hl)
  0992  B7            		or	a
  0993  2812          		jr	z,IRSEND
  0995  FE2C          		cp	','
  0997  280E          		jr	z,IRSEND
  0999  FE3A          		cp	':'
  099B  2006          		jr	nz,IRSNZ
  099D  3A49FF        		ld	a,(INPRD)
  09A0  B7            		or	a
  09A1  2004          		jr	nz,IRSEND		;READ
  09A3                	IRSNZ:
  09A3  23            		inc	hl
  09A4  04            		inc	b
                      	;	jp	z,LSERR
  09A5  18EA          		jr	IRSLP2
                      	
  09A7                	IRSEND:
  09A7  78            		ld	a,b
  09A8  C1            		pop	bc
  09A9  C9            		ret
                      	
                      	
                      	;INPUT command
  09AA                	C_INPT:
  09AA  7C            		ld	a,h
  09AB  FEFA          		cp	0fah
  09AD  D2E003        		jp	nc,IDERR
  09B0  2B            		dec	hl
  09B1  2254FF        		ld	(STOPAD),hl
  09B4  23            		inc	hl
                      	
  09B5  CDBAFF        		call	HOOKINP
  09B8  CD9F27        		call	DEVNUM
  09BB  321AFA        		ld	(INPDEV),a
  09BE  3D            		dec	a
  09BF  CACB03        		jp	z,FCERR		;printer
  09C2  3C            		inc	a
  09C3  2022          		jr	nz,INPTSTRT
                      	
                      	;INPUT #0,
  09C5  CD7C13        		call	CHGTXT
  09C8  CD593F        		call	SKIPSP
  09CB  FE22          		cp	22h		;double quotation mark
  09CD  2018          		jr	nz,INPTSTRT
                      	
  09CF                	PROMPT:
  09CF  23            		inc	hl
  09D0  7E            		ld	a,(hl)
  09D1  B7            		or	a
  09D2  CAC203        		jp	z,SNERR
  09D5  FE22          		cp	22h		;double quotation mark
  09D7  2805          		jr	z,PROMPTEND
  09D9  CD7510        		call	PRTC
  09DC  18F1          		jr	PROMPT
                      	
  09DE                	PROMPTEND:
  09DE  CD583F        		call	SKIPSPINC
  09E1  FE3B          		cp	';'
  09E3  C2C203        		jp	nz,SNERR
  09E6  23            		inc	hl
                      	
  09E7                	INPTSTRT:
  09E7  E5            		push	hl		;program address (push for "?Redo from start")
                      	
  09E8                	INPTMAIN:
  09E8  224EFF        		ld	(PROGAD),hl
  09EB  3A1AFA        		ld	a,(INPDEV)
  09EE  B7            		or	a
  09EF  2011          		jr	nz,INPTEXT
                      	
  09F1  CD0529        		call	INPT1
                      	
  09F4  D1            		pop	de		;program address
  09F5  C1            		pop	bc		;return address
  09F6  DA3F35        		jp	c,STOP3
  09F9  C5            		push	bc		;return address
  09FA  D5            		push	de		;program address
                      	
  09FB  3EFF          		ld	a,0ffh
  09FD  3255FF        		ld	(STOPAD+1),a
  0A00  180C          		jr	INPTANA
                      	
                      	;input from external device
                      	;input: a=(INPDEV), hl=INPBUF-1
                      	;destroy: af,bc,de,hl
  0A02                	INPTEXT:
  0A02  07            		rlca
  0A03  F5            		push	af		;c-flag=0:RS-232C, 1=CMT
  0A04  DC9A25        		call	c,INPOPN
  0A07  CD9B0A        		call	INPT1EX
  0A0A  F1            		pop	af
  0A0B  DCAA1A        		call	c,RCLOSE
                      	;	jr	INPTANA
                      	
                      	;analyze input buffer
  0A0E                	INPTANA:
  0A0E  11DAFE        		ld	de,INPBUF
  0A11  2A4EFF        		ld	hl,(PROGAD)
  0A14                	INPTLP:
  0A14  CD8F33        		call	CHKVAR
  0A17  224EFF        		ld	(PROGAD),hl
  0A1A  3ADAFE        		ld	a,(INPBUF)
  0A1D  B7            		or	a
  0A1E  283D          		jr	z,INPTNEXT	;if no input, no change in varible
  0A20  D5            		push	de		;input buffer
  0A21  CDA932        		call	GETVAR
  0A24  E1            		pop	hl		;input buffer
  0A25  CB79          		bit	7,c
  0A27  2005          		jr	nz,INPTSTR
                      	
  0A29                	INPTNUM:
  0A29  CD780B        		call	INPRDNUM
  0A2C  1815          		jr	INPTERR
                      	
  0A2E                	INPTSTR:
  0A2E  AF            		xor	a
  0A2F  CD7609        		call	INPRDSTR
  0A32  E5            		push	hl		;input buffer
  0A33  60            		ld	h,b
  0A34  69            		ld	l,c
  0A35  D5            		push	de		;variable address
  0A36  CD4227        		call	MAKESTR
  0A39  D1            		pop	de		;variable address
  0A3A  212DFF        		ld	hl,STRDSC1
  0A3D  010400        		ld	bc,0004h
  0A40  EDB0          		ldir
  0A42  E1            		pop	hl		;input buffer
                      	
  0A43                	INPTERR:
  0A43  CD603F        		call	CHKCLN		;for compatibility with N6X-BASIC
  0A46  EB            		ex	de,hl
  0A47  2814          		jr	z,INPTNEXT
  0A49  FE2C          		cp	','
  0A4B  2810          		jr	z,INPTNEXT
  0A4D  3A1AFA        		ld	a,(INPDEV)
  0A50  B7            		or	a
  0A51  C2FB03        		jp	nz,FDERR
  0A54  216309        		ld	hl,REDO
  0A57  CDCF30        		call	PUTS
  0A5A  E1            		pop	hl		;program address
  0A5B  188A          		jr	INPTSTRT
                      	
  0A5D                	INPTNEXT:
  0A5D  2A4EFF        		ld	hl,(PROGAD)
  0A60  7E            		ld	a,(hl)
  0A61  FE2C          		cp	','
  0A63  201C          		jr	nz,INPTEND
                      	
                      	;comma
  0A65  23            		inc	hl
  0A66  3ADAFE        		ld	a,(INPBUF)
  0A69  B7            		or	a
  0A6A  28A8          		jr	z,INPTLP
  0A6C  1A            		ld	a,(de)
  0A6D  13            		inc	de
  0A6E  FE2C          		cp	','
  0A70  28A2          		jr	z,INPTLP
  0A72  3A1AFA        		ld	a,(INPDEV)
  0A75  B7            		or	a
  0A76  C2C803        		jp	nz,ODERR
  0A79  3E3F          		ld	a,'?'
  0A7B  CD7510        		call	PRTC
  0A7E  C3E809        		jp	INPTMAIN
                      	
                      	;not comma
  0A81                	INPTEND:
  0A81  F1            		pop	af		;cancel program address
  0A82  EB            		ex	de,hl
                      	;	jr	CHKEXTR
                      	
                      	;Extra ignored check?
                      	;used by EXAS BASIC compiler
                      	;input: hl=input buffer address
                      	;destroy: af,bc,de,hl
  0A83                	_CHKEXTR:ds	CHKEXTR-_CHKEXTR
  0A8F                		org	CHKEXTR
                      	
  0A8F  7E            		ld	a,(hl)
  0A90  B7            		or	a
  0A91  21D00A        		ld	hl,EXTRA
  0A94  C4CF30        		call	nz,PUTS
  0A97  C35509        		jp	PIEND
                      	
                      	
                      	;one line input for external device
                      	;used by EXAS BASIC compiler
                      	;output: hl=INPBUF-1
                      	;destroy: af,bc
  0A9A                	_INPT1EX:ds	INPT1EX-_INPT1EX
  0A9B                		org	INPT1EX
                      	
  0A9B  3A1AFA        		ld	a,(INPDEV)
  0A9E  07            		rlca
  0A9F  F5            		push	af		;c-flag=0:RS-232C, 1=CMT
  0AA0  21DAFE        		ld	hl,INPBUF
  0AA3  0647          		ld	b,71
  0AA5                	INPTEXTLP:
  0AA5  F1            		pop	af		;c-flag=0:RS-232C, 1=CMT
  0AA6  F5            		push	af
  0AA7  3822          		jr	c,INPTCMT
                      	
  0AA9                	INPT232:
  0AA9  E5            		push	hl
  0AAA                	INPT232LP:
  0AAA  3E37          		ld	a,37h
  0AAC  D381          		out	(81h),a
  0AAE  3A18FA        		ld	a,(STOPFLG)
  0AB1  FE03          		cp	03h
  0AB3  CA5127        		jp	z,STOPSP
  0AB6  CD4310        		call	GETRSBF
  0AB9  28EF          		jr	z,INPT232LP
  0ABB  E1            		pop	hl
                      	
  0ABC                	INPTCHKRET:
  0ABC  FE0D          		cp	0dh
  0ABE  2804          		jr	z,INPTEXTEND
  0AC0  77            		ld	(hl),a
  0AC1  23            		inc	hl
  0AC2  10E1          		djnz	INPTEXTLP
                      	
  0AC4                	INPTEXTEND:
  0AC4  F1            		pop	af
  0AC5  3600          		ld	(hl),00h
  0AC7  21D9FE        		ld	hl,INPBUF-1
  0ACA  C9            		ret
                      	
  0ACB                	INPTCMT:
  0ACB  CD8F1A        		call	GETCMTTR
  0ACE  18EC          		jr	INPTCHKRET
                      	
  0AD0                	EXTRA:
  0AD0  3F457874726120		db	"?Extra ignored", 0dh, 0ah, 00h
                      	
                      	
                      	;READ command
  0AE1                	C_READ:
  0AE1  CD8F33        		call	CHKVAR
  0AE4  CDA932        		call	GETVAR
  0AE7  C5            		push	bc		;variable name
  0AE8  D5            		push	de		;variable address
                      	
  0AE9  2A5CFF        		ld	hl,(DATAAD)
  0AEC  ED5B45FF      		ld	de,(DATALN)
  0AF0  7E            		ld	a,(hl)
  0AF1  FE2C          		cp	','
  0AF3  2847          		jr	z,READDATA
  0AF5  FE3A          		cp	':'
  0AF7  2810          		jr	z,READLP3
  0AF9  B7            		or	a
  0AFA  C2C803        		jp	nz,ODERR
                      	
  0AFD                	READLP1:
  0AFD  23            		inc	hl
  0AFE                	READLP2:
  0AFE  4F            		ld	c,a		;always a=0, double quation mark counter
  0AFF  7E            		ld	a,(hl)
  0B00  23            		inc	hl
  0B01  B6            		or	(hl)
  0B02  CAC803        		jp	z,ODERR
  0B05  23            		inc	hl
  0B06  5E            		ld	e,(hl)
  0B07  23            		inc	hl
  0B08  56            		ld	d,(hl)
                      	
  0B09                	READLP3:
  0B09  23            		inc	hl
  0B0A  7E            		ld	a,(hl)
  0B0B  B7            		or	a
  0B0C  28EF          		jr	z,READLP1
  0B0E  FE83          		cp	DATA_
  0B10  282A          		jr	z,READDATA
  0B12  FE22          		cp	22h		;double quotation mark
  0B14  281D          		jr	z,READDQ
  0B16  FE3A          		cp	':'
  0B18  28EF          		jr	z,READLP3
  0B1A  FE20          		cp	' '
  0B1C  28EB          		jr	z,READLP3
  0B1E  D68E          		sub	REM_
  0B20  2814          		jr	z,READREM
                      	
  0B22                	READLP4:
  0B22  23            		inc	hl
  0B23  7E            		ld	a,(hl)
  0B24  B7            		or	a
  0B25  28D6          		jr	z,READLP1
  0B27  CB41          		bit	0,c
  0B29  2004          		jr	nz,SKIPCLN
  0B2B  FE3A          		cp	':'
  0B2D  28DA          		jr	z,READLP3
  0B2F                	SKIPCLN:
  0B2F  FE22          		cp	22h		;double quotation mark
  0B31  20EF          		jr	nz,READLP4
  0B33                	READDQ:
  0B33  0C            		inc	c
  0B34  18EC          		jr	READLP4
                      	
                      	;a=0
  0B36                	READREM:
                      	;	xor	a
  0B36  47            		ld	b,a
  0B37  4F            		ld	c,a
  0B38  EDB1          		cpir
  0B3A  18C2          		jr	READLP2
                      	
  0B3C                	READDATA:
  0B3C  ED5345FF      		ld	(DATALN),de
  0B40  23            		inc	hl
  0B41  D1            		pop	de		;variable address
  0B42  C1            		pop	bc		;variable name
  0B43  CB79          		bit	7,c
  0B45  2005          		jr	nz,READSTR
                      	
  0B47                	READNUM:
  0B47  CD780B        		call	INPRDNUM
  0B4A  180C          		jr	READERR
                      	
                      	;de=variable address
  0B4C                	READSTR:
  0B4C  78            		ld	a,b		;>0
  0B4D  CD7609        		call	INPRDSTR
  0B50  12            		ld	(de),a
  0B51  13            		inc	de
  0B52  13            		inc	de
  0B53  79            		ld	a,c
  0B54  12            		ld	(de),a
  0B55  13            		inc	de
  0B56  78            		ld	a,b
  0B57  12            		ld	(de),a
                      	
                      	;error check
  0B58                	READERR:
  0B58  CD603F        		call	CHKCLN
  0B5B  280D          		jr	z,READNEXT
  0B5D  FE2C          		cp	','
  0B5F  2809          		jr	z,READNEXT
  0B61  2A45FF        		ld	hl,(DATALN)
  0B64  225DFA        		ld	(LINENUM),hl
  0B67  C3C203        		jp	SNERR
                      	
  0B6A                	READNEXT:
  0B6A  225CFF        		ld	(DATAAD),hl
  0B6D  2A4EFF        		ld	hl,(PROGAD)
  0B70  7E            		ld	a,(hl)
  0B71  FE2C          		cp	','
  0B73  C0            		ret	nz
  0B74  23            		inc	hl
  0B75  C3E10A        		jp	C_READ
                      	
                      	
                      	;for INPUT/READ
                      	;input: de=variable adderss, hl=program address
  0B78                	INPRDNUM:
  0B78  D5            		push	de		;variable address
  0B79  CD9B39        		call	ATOF
  0B7C  D1            		pop	de		;variable address
  0B7D  E5            		push	hl		;input buffer/data address
  0B7E  2166FF        		ld	hl,FAC1
  0B81  CD480C        		call	SETF
  0B84  E1            		pop	hl		;input buffer/data address
  0B85  C9            		ret
                      	
                      	
                      	;get a numerical argument for function
                      	;input: hl=program address
                      	;output: FAC1
                      	;destroy: FAC2,af,bc,de,hl
  0B86                	FNCNUM:
  0B86  CDB50B        		call	CHKLPAR
  0B89                	FNCNUMR:
  0B89  CD4717        		call	ARG
  0B8C  CDC40B        		call	CHKRPAR
  0B8F  180A          		jr	CHKNUM
                      	
                      	
                      	;get a numerical argument and check result (?MO Error)
                      	;input: hl=program address-1
                      	;output: FAC1, hl=next address
                      	;destroy: af,bc,de,FAC2
  0B91                	NARGMOINC:
  0B91  23            		inc	hl
                      	
                      	;get a numerical argument and check result (?MO Error)
                      	;input: hl=program address
                      	;output: FAC1, hl=next address
                      	;destroy: af,bc,de,FAC2
  0B92                	NARGMO:
  0B92  CD603F        		call	CHKCLN
  0B95  CAF803        		jp	z,MOERR
                      	;	call	NUMARG
                      	;	ret
                      	
                      	
                      	;get a numerical argument and check result
                      	;input: hl=program address
                      	;output: FAC1, hl=next address
                      	;destroy: af,bc,de,FAC2
  0B98                	NUMARG:
  0B98  CD4717        		call	ARG
                      	;	call	CHKNUM
                      	;	ret
                      	
                      	
                      	;check numeric or not
                      	;output: z-flag (1=ok)
                      	;destroy: af
  0B9B                	CHKNUM:
  0B9B  3A25FF        		ld	a,(ARGTYP)
  0B9E  B7            		or	a
  0B9F  C8            		ret	z
  0BA0  3D            		dec	a
  0BA1                	CHKNERR:
  0BA1  CAE303        		jp	z,TMERR
  0BA4  C3C203        		jp	SNERR
                      	
                      	
                      	;check string argument or not
                      	;output: a=length, hl=string descriptor address, z-flag(1=ok)
                      	;destroy: f
  0BA7                	CHKSTR:
  0BA7  3A25FF        		ld	a,(ARGTYP)
  0BAA  3D            		dec	a
  0BAB  2005          		jr	nz,CHKSERR
  0BAD  2A67FF        		ld	hl,(FAC1+1)
  0BB0  7E            		ld	a,(hl)
  0BB1  C9            		ret
                      	
  0BB2                	CHKSERR:
  0BB2  3C            		inc	a
  0BB3  18EC          		jr	CHKNERR
                      	
                      	
                      	;check (
                      	;input: hl=program address
                      	;output: hl=next address
                      	;destroy: af
  0BB5                	CHKLPAR:
  0BB5  CD593F        		call	SKIPSP
  0BB8  FE28          		cp	'('
  0BBA  C2C203        		jp	nz,SNERR
  0BBD  CD5F3F        		call	CHKCLNINC
  0BC0  C0            		ret	nz
  0BC1  C3F803        		jp	MOERR
                      	
                      	
                      	;check )
                      	;input: hl=program address
                      	;output: hl=next address
                      	;destroy: af
  0BC4                	CHKRPAR:
  0BC4  7E            		ld	a,(hl)
  0BC5  FE29          		cp	')'
  0BC7  C2C203        		jp	nz,SNERR
  0BCA  23            		inc	hl
  0BCB                	SETPRGAD:
  0BCB  224EFF        		ld	(PROGAD),hl
  0BCE  C9            		ret
                      	
                      	
                      	;alphabet, numeral, or katakana? (case ignored)
  0BCF                	ALPNUM2:
  0BCF  FEE0          		cp	0e0h
  0BD1  3F            		ccf
  0BD2  D8            		ret	c
  0BD3  FEA1          		cp	0a1h
  0BD5  D0            		ret	nc
  0BD6  CDEF0B        		call	TOUPPER
                      	;	call	ALPNUM
                      	;	ret
                      	
                      	
                      	;alphabet (capital) or numeral?
                      	;input: a
                      	;output: c-flag(1=yes,0=no)
                      	;destroy: f
  0BD9                	ALPNUM:
  0BD9  FE5B          		cp	'Z'+1
  0BDB  D0            		ret	nc
  0BDC  FE41          		cp	'A'
  0BDE  3F            		ccf
  0BDF  D8            		ret	c
                      	;	jp	CHKFIG
                      	
                      	
                      	;analyze for 0010h
                      	;input: a
                      	;output: z-flag(1=00h or 3ah), c-flag(1=30h-39h)
                      	;destroy: f
  0BE0                	CHKFIG:
  0BE0  B7            		or	a
  0BE1  C8            		ret	z
  0BE2  FE30          		cp	'0'
  0BE4  3F            		ccf
  0BE5  D0            		ret	nc
  0BE6  FE3A          		cp	':'
                      	;	ret	z
                      	;	cp	'9'+1
  0BE8  C9            		ret
                      	
                      	
                      	;convert lowercase letter to uppercase letter
                      	;input: hl=data address
                      	;output: a
                      	;destroy: f
  0BE9                	_TOUPHL:ds	TOUPHL-_TOUPHL
  0BEE  7E            		ld	a,(hl)
                      	
                      	;0bef: used by 256 bytes monitor
                      	;input: a=data
  0BEF                	TOUPPER:
  0BEF  FE7B          		cp	'z'+1
  0BF1  D0            		ret	nc
  0BF2                	TOUPPER2:
  0BF2  FE61          		cp	'a'
  0BF4  D8            		ret	c
  0BF5  D620          		sub	'a'-'A'
  0BF7  C9            		ret
                      	
                      	
                      	;OR operator
  0BF8                	O_OR:
  0BF8  CD5507        		call	F12TOI2
  0BFB  7C            		ld	a,h
  0BFC  B2            		or	d
  0BFD  67            		ld	h,a
  0BFE  7D            		ld	a,l
  0BFF  B3            		or	e
  0C00  6F            		ld	l,a
  0C01  C3180D        		jp	S2TOF
                      	
                      	
                      	;AND operator
  0C04                	O_AND:
  0C04  CD5507        		call	F12TOI2
  0C07  7C            		ld	a,h
  0C08  A2            		and	d
  0C09  67            		ld	h,a
  0C0A  7D            		ld	a,l
  0C0B  A3            		and	e
  0C0C  6F            		ld	l,a
  0C0D  C3180D        		jp	S2TOF
                      	
                      	
                      	;NOT operator
  0C10                	O_NOT:
  0C10  CD9B0B        		call	CHKNUM
  0C13  CD6539        		call	EXFAC
  0C16  CD4107        		call	FTOI2
  0C19  7A            		ld	a,d
  0C1A  2F            		cpl
  0C1B  67            		ld	h,a
  0C1C  7B            		ld	a,e
  0C1D  2F            		cpl
  0C1E  6F            		ld	l,a
  0C1F  C3180D        		jp	S2TOF
                      	
                      	
                      	;>,=,< operator
                      	;bit0: >
                      	;bit1: =
                      	;bit2: <
  0C22                	O_GTEQLT:
  0C22  3A25FF        		ld	a,(ARGTYP)
  0C25  B7            		or	a
  0C26  280D          		jr	z,GTEQLTZ
  0C28  D603          		sub	03h		;string and string
  0C2A  C2E303        		jp	nz,TMERR
  0C2D  3225FF        		ld	(ARGTYP),a	;a=0
  0C30  CD580C        		call	CMPSTR
  0C33  1803          		jr	GTEQLTEND
  0C35                	GTEQLTZ:
  0C35  CD1B3F        		call	CMPF
  0C38                	GTEQLTEND:
  0C38  79            		ld	a,c		;operator
  0C39  2803          		jr	z,CASE_EQ
  0C3B  3002          		jr	nc,CASE_GT
  0C3D                	CASE_LT:
  0C3D  0F            		rrca
  0C3E                	CASE_EQ:
  0C3E  0F            		rrca
  0C3F                	CASE_GT:
  0C3F  0F            		rrca
  0C40  3811          		jr	c,SETTRUE
                      	;	jr	SETFALSE
                      	
  0C42                	SETFALSE:
  0C42                	SETZERO:
  0C42  21AB0E        		ld	hl,ZERO
  0C45                	SETF1:
  0C45  1166FF        		ld	de,FAC1
  0C48                	SETF:
  0C48  010500        		ld	bc,0005h
  0C4B  EDB0          		ldir
  0C4D  C9            		ret
                      	
  0C4E                	SETF2:
  0C4E  116DFF        		ld	de,FAC2
  0C51  18F5          		jr	SETF
                      	
  0C53                	SETTRUE:
  0C53                	SETMNS1:
  0C53  21A60E        		ld	hl,MNSONE
  0C56  18ED          		jr	SETF1
                      	
                      	
  0C58                	CMPSTR:
  0C58  2139FF        		ld	hl,STRDSC4
  0C5B  46            		ld	b,(hl)
  0C5C  3600          		ld	(hl),00h
  0C5E  3A2DFF        		ld	a,(STRDSC1)
  0C61  B8            		cp	b
  0C62  F5            		push	af		;flag
  0C63  3801          		jr	c,CMPSNC
  0C65  78            		ld	a,b
  0C66                	CMPSNC:
  0C66  B7            		or	a
  0C67  2810          		jr	z,CMPSZ
                      	
  0C69  ED5B2FFF      		ld	de,(STRDSC1+2)
  0C6D  2A3BFF        		ld	hl,(STRDSC4+2)
  0C70  47            		ld	b,a
  0C71                	CMPSLP:
  0C71  1A            		ld	a,(de)
  0C72  BE            		cp	(hl)
  0C73  2006          		jr	nz,CMPSNZ
  0C75  13            		inc	de
  0C76  23            		inc	hl
  0C77  10F8          		djnz	CMPSLP
  0C79                	CMPSZ:
  0C79  F1            		pop	af		;flag
  0C7A  C9            		ret
                      	
  0C7B                	CMPSNZ:
  0C7B  E1            		pop	hl		;cancel stack
  0C7C  C9            		ret
                      	
                      	
                      	;input: a=integer (unsigned)
  0C7D                	I1TOFA:
  0C7D  6F            		ld	l,a
                      	
                      	;convert 1- or 2-byte integer to float
                      	;input: hl=integer (unsigned)
                      	;output: FAC1
                      	;destroy: af,b,hl
  0C7E                	I1TOF:
  0C7E  2600          		ld	h,00h
  0C80                	I2TOF:
  0C80  D5            		push	de
  0C81  110000        		ld	de,0000h
  0C84  7C            		ld	a,h
  0C85  B5            		or	l
  0C86  280F          		jr	z,I2TOFZERO
  0C88  0610          		ld	b,10h
  0C8A                	I2TOFLP:
  0C8A  29            		add	hl,hl
  0C8B  3802          		jr	c,I2TOFEND
  0C8D  10FB          		djnz	I2TOFLP
                      	
  0C8F                	I2TOFEND:
  0C8F  CB3C          		srl	h
  0C91  CB1D          		rr	l
  0C93  EB            		ex	de,hl
  0C94                	I2TOFEND2:
  0C94  78            		ld	a,b
  0C95  C680          		add	a,80h
  0C97                	I2TOFZERO:
                      	;	ld	(FAC1),hl
                      	;	ld	(FAC1+2),de
  0C97  326AFF        		ld	(FAC1+4),a
  0C9A  CD0725        		call	SETI4
  0C9D  D1            		pop	de
  0C9E  C9            		ret
                      	
                      	
                      	;convert float to 2-byte integer [-65535,65535]
                      	;(round toward minus infinity)
                      	;input: FAC1
                      	;output: de
                      	;destroy: af,bc,hl
  0C9F                	FTOI:
  0C9F  110000        		ld	de,0000h
  0CA2  3A6AFF        		ld	a,(FAC1+4)
  0CA5  B7            		or	a
  0CA6  C8            		ret	z
  0CA7  D691          		sub	91h
  0CA9  D2CB03        		jp	nc,FCERR
  0CAC  ED44          		neg
  0CAE  47            		ld	b,a
                      	
  0CAF  2A66FF        		ld	hl,(FAC1)
  0CB2  7C            		ld	a,h
  0CB3  B5            		or	l		;fraction
                      	
  0CB4  2A68FF        		ld	hl,(FAC1+2)
  0CB7  4C            		ld	c,h		;sign
  0CB8  CBFC          		set	7,h
  0CBA  05            		dec	b
  0CBB  2809          		jr	z,FTOIZ
  0CBD                	FTOILP:
  0CBD  CB3C          		srl	h
  0CBF  CB1D          		rr	l
  0CC1  3001          		jr	nc,FTOINC
  0CC3  17            		rla			;a>0
  0CC4                	FTOINC:
  0CC4  10F7          		djnz	FTOILP
                      	
  0CC6                	FTOIZ:
  0CC6  EB            		ex	de,hl
  0CC7  CB79          		bit	7,c		;sign
  0CC9  C8            		ret	z
                      	
  0CCA  ED44          		neg			;set c-flag if a>0
  0CCC  ED52          		sbc	hl,de
  0CCE  EB            		ex	de,hl
  0CCF  C9            		ret
                      	
                      	
                      	;get integer part from float
                      	;(round toward zero)
                      	;input: FAC1
                      	;output: FAC1
                      	;destroy: af,b,hl
  0CD0                	GETINT:
  0CD0  3A6AFF        		ld	a,(FAC1+4)
  0CD3  FE81          		cp	81h
  0CD5  DA420C        		jp	c,SETZERO
  0CD8  D6A0          		sub	0a0h
  0CDA  D0            		ret	nc
  0CDB  2166FF        		ld	hl,FAC1
  0CDE                	GETILP1:
  0CDE  C608          		add	a,08h		;
  0CE0  3805          		jr	c,GETIC
  0CE2  3600          		ld	(hl),00h
  0CE4  23            		inc	hl
  0CE5  18F7          		jr	GETILP1
  0CE7                	GETIC:
  0CE7  47            		ld	b,a
  0CE8  04            		inc	b
  0CE9  AF            		xor	a
  0CEA                	GETILP2:
  0CEA  1F            		rra
  0CEB  37            		scf
  0CEC  10FC          		djnz	GETILP2
  0CEE  A6            		and	(hl)
  0CEF  77            		ld	(hl),a
  0CF0  C9            		ret
                      	
                      	
                      	;convert 1-byte signed integer to float
                      	;input: b=integer (signed)
                      	;output: FAC1
                      	;destroy: af,bc,hl
  0CF1                	_S1TOF:	ds	ABTOF-5-_S1TOF
  0D11                		org	ABTOF-5
  0D11                	S1TOF:
  0D11  78            		ld	a,b
  0D12  07            		rlca
  0D13  9F            		sbc	a,a		;a=0(b<80h) or ffh(b>=80h)
  0D14  1800          		jr	ABTOF
                      	
                      	
                      	;convert 2-byte integer to float for USR()
                      	;input: ab
                      	;output: FAC1
                      	;destroy: af,bc,hl
  0D16                	_ABTOF:	ds	ABTOF-_ABTOF
  0D16                		org	ABTOF
                      	
  0D16  67            		ld	h,a
  0D17  68            		ld	l,b
                      	;	jr	S2TOF
                      	
                      	
                      	;convert 2-byte signed integer to float
                      	;input: hl=integer (signed)
                      	;output: FAC1
                      	;destroy: af,b,hl
  0D18                	S2TOF:
  0D18  CB7C          		bit	7,h
  0D1A  CA800C        		jp	z,I2TOF
  0D1D  AF            		xor	a
  0D1E  95            		sub	l
  0D1F  6F            		ld	l,a
  0D20  9C            		sbc	a,h
  0D21  95            		sub	l
  0D22  67            		ld	h,a
  0D23  CD800C        		call	I2TOF
  0D26  1805          		jr	NEGABSNZ
                      	
                      	
                      	;FAC1=-abs(FAC1)
                      	;destroy: af,hl
  0D28                	NEGABS:
  0D28  3A6AFF        		ld	a,(FAC1+4)
  0D2B  B7            		or	a
  0D2C  C8            		ret	z
                      	
                      	;FAC1=-abs(FAC1) without checking zero
                      	;destroy: f,hl
  0D2D                	NEGABSNZ:
  0D2D  2169FF        		ld	hl,FAC1+3
  0D30  CBFE          		set	7,(hl)
  0D32  C9            		ret
                      	
                      	
                      	;LPOS() function
  0D33                	F_LPOS:
  0D33  3A25FF        		ld	a,(ARGTYP)
  0D36  FE02          		cp	02h
  0D38  D2C203        		jp	nc,SNERR
  0D3B  2A57FA        		ld	hl,(PRTPOS)
  0D3E  C3A431        		jp	SETI1
                      	
                      	
                      	;POS() function
  0D41                	F_POS:
  0D41  3A25FF        		ld	a,(ARGTYP)
  0D44  FE02          		cp	02h
  0D46  D2C203        		jp	nc,SNERR
  0D49  2AA9FD        		ld	hl,(CSRX)
  0D4C  2D            		dec	l
  0D4D  C3A431        		jp	SETI1
                      	
                      	
                      	;CSRLINE function
  0D50                	F_CSRL:
  0D50  2AA8FD        		ld	hl,(CSRY)
  0D53  2D            		dec	l
  0D54  C37E0C        		jp	I1TOF
                      	
                      	
                      	;DEF command
  0D57                	C_DEF:
  0D57  ED5B5DFA      		ld	de,(LINENUM)
  0D5B  7A            		ld	a,d
  0D5C  A3            		and	e
  0D5D  3C            		inc	a
  0D5E  CAE003        		jp	z,IDERR
  0D61  CD593F        		call	SKIPSP
  0D64  FEC4          		cp	FN_
  0D66  C2C203        		jp	nz,SNERR
  0D69  23            		inc	hl
  0D6A  CD8F33        		call	CHKVAR
  0D6D  CB79          		bit	7,c
  0D6F  C2E303        		jp	nz,TMERR
  0D72  CBF8          		set	7,b
  0D74  E5            		push	hl		;program address
  0D75  CDC133        		call	SRCHVAR
  0D78  DCEB33        		call	c,MAKEVAR
  0D7B  E1            		pop	hl		;program address
  0D7C  CDB50B        		call	CHKLPAR
  0D7F  CD8F33        		call	CHKVAR
  0D82  CB79          		bit	7,c
  0D84  C2E303        		jp	nz,TMERR
  0D87  CDC40B        		call	CHKRPAR
  0D8A  CD593F        		call	SKIPSP
  0D8D  FED2          		cp	EQ_
  0D8F  C2C203        		jp	nz,SNERR
  0D92  CD583F        		call	SKIPSPINC
                      	
  0D95  EB            		ex	de,hl
  0D96  73            		ld	(hl),e
  0D97  23            		inc	hl
  0D98  72            		ld	(hl),d
  0D99  23            		inc	hl
  0D9A  70            		ld	(hl),b
  0D9B  23            		inc	hl
  0D9C  71            		ld	(hl),c
                      	;	inc	hl
                      	;	ld	(hl),00h
  0D9D  EB            		ex	de,hl
                      	
  0D9E  2B            		dec	hl
  0D9F                	DEFLP:
  0D9F  CD5F3F        		call	CHKCLNINC
  0DA2  20FB          		jr	nz,DEFLP
                      	
  0DA4  224EFF        		ld	(PROGAD),hl
  0DA7  C9            		ret
                      	
                      	
                      	;INP() function
  0DA8                	F_INP:
  0DA8  CD9B0B        		call	CHKNUM
  0DAB  CD4C07        		call	FTOI1
  0DAE  42            		ld	b,d		;d=0
  0DAF  4B            		ld	c,e
  0DB0  ED68          		in	l,(c)
  0DB2  62            		ld	h,d		;d=0
  0DB3  C3800C        		jp	I2TOF
                      	
                      	
                      	;OUT command
  0DB6                	C_OUT:
  0DB6  CD2D0E        		call	INT1ARG2
  0DB9  ED59          		out	(c),e
  0DBB  C9            		ret
                      	
                      	
  0DBC                	MODESTR:
  0DBC  0D0A4D4F444520		db	0dh, 0ah, "MODE ",00h
  0DC4                	PAGESTR:
  0DC4  2050414745530D		db	" PAGES", 0dh, 0ah, 00h
  0DCD                	FILESTR:
  0DCD  2046494C45530D		db	" FILES", 0dh, 0ah, 00h
                      	
                      	
                      	;increment hl and get a 1-byte integer argument [0,255]
  0DD6                	_INT1INC:ds	INT1INC-_INT1INC
  0DE3                		org	INT1INC
                      	
  0DE3  23            		inc	hl
                      	
                      	;get a 1-byte integer argument [0,255]
                      	;input: hl=program address
                      	;output: FAC1,a,de=integer, hl=next address
                      	;destroy: FAC2,f,bc
  0DE4                	_INT1ARG:ds	INT1ARG-_INT1ARG
  0DE4                		org	INT1ARG
                      	
  0DE4  CD140E        		call	INT2ARG
  0DE7  7A            		ld	a,d
  0DE8  B7            		or	a
  0DE9  7B            		ld	a,e
  0DEA  C8            		ret	z
  0DEB  C3CB03        		jp	FCERR
                      	
                      	
                      	;PEEK() function
  0DEE                	F_PEEK:
  0DEE  CD9B0B        		call	CHKNUM
  0DF1  CD9F0C        		call	FTOI
  0DF4  1A            		ld	a,(de)
  0DF5  C37D0C        		jp	I1TOFA
                      	
                      	
                      	;POKE command
  0DF8                	C_POKE:
  0DF8  CD0B0E        		call	INTARG
  0DFB  7E            		ld	a,(hl)
  0DFC  FE2C          		cp	','
  0DFE  C2C203        		jp	nz,SNERR
  0E01  D5            		push	de		;
  0E02  CD910B        		call	NARGMOINC
  0E05  CD4C07        		call	FTOI1
  0E08  E1            		pop	hl		;
  0E09  73            		ld	(hl),e
  0E0A  C9            		ret
                      	
                      	
                      	;get a 2-byte integer argument [-65535,65535]
                      	;input: hl=program address
                      	;output: FAC1,de=integer, hl=next address
                      	;destroy: FAC2,af,bc
  0E0B                	INTARG:
  0E0B  CD920B        		call	NARGMO
  0E0E  E5            		push	hl
  0E0F  CD9F0C        		call	FTOI
  0E12  E1            		pop	hl
  0E13  C9            		ret
                      	
                      	
                      	;get a 2-byte integer argument [-32768,32767]
                      	;input: hl=program address
                      	;output: FAC1,de=integer, hl=next address
                      	;destroy: FAC2,af,bc
  0E14                	INT2ARG:
  0E14  CD920B        		call	NARGMO
  0E17  E5            		push	hl
  0E18  CD4107        		call	FTOI2
  0E1B  E1            		pop	hl
  0E1C  C9            		ret
                      	
                      	
                      	;get two 2-byte integer arguments [-65535,65535]
                      	;input: hl=program address
                      	;output: FAC1, bc=1st, de=2nd, hl=next address
                      	;destroy: af
  0E1D                	INTARG2:
  0E1D  CD0B0E        		call	INTARG
  0E20  7E            		ld	a,(hl)
  0E21  FE2C          		cp	','
  0E23  C2C203        		jp	nz,SNERR
  0E26  23            		inc	hl
  0E27  D5            		push	de		;
  0E28  CD0B0E        		call	INTARG
  0E2B  C1            		pop	bc		;
  0E2C  C9            		ret
                      	
                      	
                      	;get two 1-byte integer arguments [0,255]
                      	;input: hl=program address
                      	;output: FAC1, bc=1st, de=2nd, hl=next address
                      	;destroy: af
  0E2D                	INT1ARG2:
  0E2D  CDE40D        		call	INT1ARG
  0E30  7E            		ld	a,(hl)
  0E31  FE2C          		cp	','
  0E33  C2C203        		jp	nz,SNERR
  0E36  D5            		push	de
  0E37  CDE30D        		call	INT1INC
  0E3A  C1            		pop	bc
  0E3B  C9            		ret
                      	
                      	
                      	;convert character VRAM address to attribute address
                      	;input: hl
                      	;output: hl
                      	;destroy: f (no change in c-flag)
  0E3C                	CHR2ATT:
  0E3C  CB54          		bit	2,h
  0E3E  2802          		jr	z,CHR2ATTZ
  0E40  25            		dec	h
  0E41  25            		dec	h
  0E42                	CHR2ATTZ:
  0E42  25            		dec	h
  0E43  25            		dec	h
  0E44  C9            		ret
                      	
                      	
                      	;get space attribute data for scroll
                      	;output: a
                      	;destroy: f
  0E45                	GETSPA:
  0E45  CD9539        		call	CHKMOD
  0E48  3A92FD        		ld	a,(SCREEN1)
  0E4B  CAEC64        		jp	z,GETSPA66
  0E4E  3D            		dec	a
  0E4F  FA0316        		jp	m,CNVATT1
  0E52  280B          		jr	z,GETSPA2
                      	
  0E54                	GETSPAG:
  0E54  3D            		dec	a
  0E55  3A95FD        		ld	a,(COLOR3)
  0E58  2802          		jr	z,GETSPA3
  0E5A  C650          		add	a,50h
  0E5C                	GETSPA3:
  0E5C  C68C          		add	a,8ch		;screen mode 3=8ch, screen mode 4=dch
  0E5E  C9            		ret
                      	
  0E5F                	GETSPA2:
  0E5F  3A94FD        		ld	a,(COLOR2)
  0E62  B7            		or	a
  0E63  2003          		jr	nz,GETSPA2NZ
  0E65  3A93FD        		ld	a,(COLOR1)
                      	
  0E68                	GETSPA2NZ:
  0E68  FE05          		cp	05h
  0E6A  3A95FD        		ld	a,(COLOR3)
  0E6D  3802          		jr	c,GETSPAC
  0E6F  EE02          		xor	02h
  0E71                	GETSPAC:
  0E71  C660          		add	a,60h
  0E73  C9            		ret
                      	
                      	
                      	;get data from port 90h
                      	;output: a
                      	;destroy: f
  0E74                	_IN90H:	ds	IN90H-_IN90H
  0E78                		org	IN90H
                      	
  0E78  3E0C          		ld	a,0ch
  0E7A  D393          		out	(93h),a		;disable 8255 INT for writing
  0E7C                	IN90HLP:
  0E7C  DB92          		in	a,(92h)
  0E7E  2F            		cpl
  0E7F  E628          		and	28h
  0E81  20F9          		jr	nz,IN90HLP	;wait for ibf=1,intr=1
  0E83  3E0D          		ld	a,0dh
  0E85  D393          		out	(93h),a		;enable 8255 INT for writing
  0E87  DB90          		in	a,(90h)
  0E89  C9            		ret
                      	
                      	
                      	;output to port 90h (send sub CPU command)
                      	;input: a=data
                      	;destroy: none
  0E8A                	_OUT90H:ds	OUT90H-_OUT90H
  0E8F                		org	OUT90H
                      	
  0E8F  F5            		push	af
  0E90  3E08          		ld	a,08h
  0E92  D393          		out	(93h),a		;disable 8255 INT for reading
  0E94                	OUT90HLP:
  0E94  DB92          		in	a,(92h)
  0E96  07            		rlca
  0E97  30FB          		jr	nc,OUT90HLP	;wait for nobf=1
  0E99  3E09          		ld	a,09h
  0E9B  D393          		out	(93h),a		;enable 8255 INT for reading
  0E9D  F1            		pop	af
  0E9E  D390          		out	(90h),a
  0EA0  C9            		ret
                      	
                      	
  0EA1                	PLSONE:
  0EA1  0000000081    		db	00h, 00h, 00h, 00h, 81h
                      	
  0EA6                	MNSONE:
  0EA6  0000008081    		db	00h, 00h, 00h, 80h, 81h
                      	
  0EAB                	ZERO:
  0EAB  0000000000    		db	00h, 00h, 00h, 00h, 00h
                      	
                      	
                      	;interrupt for graphic key
  0EB0                	_INTGRP:ds	INTGRP-_INTGRP
  0EB0                		org	INTGRP
                      	
  0EB0  C5            		push	bc
  0EB1  0601          		ld	b,01h
  0EB3  1803          		jr	KEYCOMMON
                      	
                      	
                      	;interrupt for normal key
  0EB5                	_INTKEY:ds	INTKEY-_INTKEY
  0EB5                		org	INTKEY
                      	
  0EB5  C5            		push	bc
  0EB6  0600          		ld	b,00h
                      	
  0EB8                	KEYCOMMON:
  0EB8  F5            		push	af
  0EB9  CD780E        		call	IN90H
                      	
                      	;0ebch: used by SPACE HARRIER
  0EBC  D5            		push	de
  0EBD  E5            		push	hl
  0EBE  B7            		or	a
  0EBF  286A          		jr	z,IKPOP
  0EC1  CD3B15        		call	CLICK
                      	;	dec	b
                      	;	jr	nz,NORMAL
  0EC4  1015          		djnz	NORMAL
                      	
  0EC6                	GRAPH:
  0EC6  47            		ld	b,a		;;
  0EC7  D6FA          		sub	0fah
  0EC9  2842          		jr	z,STOPKEY
  0ECB  304A          		jr	nc,SPECIAL
  0ECD  3E14          		ld	a,14h
  0ECF  1825          		jr	IKBF
                      	
                      	
                      	;interrupt for reply to key query
  0ED1                	INTGAM:
  0ED1  F5            		push	af
  0ED2  CD780E        		call	IN90H
  0ED5  32CAFE        		ld	(GMKYBUF),a
  0ED8  F1            		pop	af
  0ED9  FB            		ei
  0EDA  C9            		ret
                      	
                      	
                      	;normal key
  0EDB                	NORMAL:
  0EDB  FE03          		cp	03h		;Ctrl-C
  0EDD  2833          		jr	z,STOPKEY2
  0EDF  FE1B          		cp	1bh
  0EE1  282F          		jr	z,STOPKEY2
  0EE3  06EF          		ld	b,0efh		;;Ctrl-T->14h+0efh
  0EE5  FE7F          		cp	7fh
  0EE7  2002          		jr	nz,NOTDELKEY
  0EE9  3E08          		ld	a,08h		;DEL->ctrl-H, not necessary for emulator?
                      	
  0EEB                	NOTDELKEY:
  0EEB  215AFA        		ld	hl,KEYFLG
  0EEE  CB46          		bit	0,(hl)
  0EF0  C4EF0B        		call	nz,TOUPPER
  0EF3  1801          		jr	IKBF
                      	
                      	
                      	;0ef6h: used by PORTOPIA
  0EF5                	_IKBF:ds	IKBF-_IKBF
  0EF6                		org	IKBF
                      	
  0EF6  218FFB        		ld	hl,KYBFIN
  0EF9  4E            		ld	c,(hl)		;
  0EFA  CD360F        		call	PUTKBF
                      	;	jr	c,RESSTOP
  0EFD  FE14          		cp	14h
  0EFF  2025          		jr	nz,RESSTOP
                      	
  0F01  78            		ld	a,b		;;
  0F02  CD360F        		call	PUTKBF
  0F05  301F          		jr	nc,RESSTOP
                      	
  0F07  218FFB        		ld	hl,KYBFIN
  0F0A  71            		ld	(hl),c		;
  0F0B  1819          		jr	RESSTOP
                      	
                      	
  0F0D                	STOPKEY:
  0F0D  CDD8FF        		call	HOOKSTP
  0F10  3E03          		ld	a,03h
  0F12                	STOPKEY2:
  0F12  CD360F        		call	PUTKBF
  0F15  1810          		jr	SETSTOP
                      	
                      	
                      	;fb	CAPS		bit0
                      	;fc	SHIFT+PAGE	bit1
                      	;fd	MODE		bit3
                      	;fe	KANA		bit2
  0F17                	SPECIAL:
  0F17  FE03          		cp	03h		;MODE key
  0F19  2002          		jr	nz,NOTMODE
  0F1B  3E08          		ld	a,08h
  0F1D                	NOTMODE:
  0F1D  215AFA        		ld	hl,KEYFLG
  0F20  AE            		xor	(hl)
  0F21  77            		ld	(hl),a
                      	
  0F22  AF            		xor	a
  0F23  32A7FD        		ld	(FKEYSFT),a
                      	
  0F26                	RESSTOP:
  0F26  AF            		xor	a
  0F27                	SETSTOP:
  0F27  3218FA        		ld	(STOPFLG),a
                      	;	jr	IKPOP
                      	
                      	
                      	;pop register for key interrupt
  0F2A                	_IKPOP:	ds	IKPOP-_IKPOP
  0F2B                		org	IKPOP
                      	
  0F2B  E1            		pop	hl
  0F2C  D1            		pop	de
                      	
                      	;0f2dh: used by SPACE HARRIER
  0F2D  F1            		pop	af
  0F2E  C1            		pop	bc
  0F2F  FB            		ei
  0F30  C9            		ret
                      	
                      	
                      	;put into PLAY command buffer
                      	;input: a=data
                      	;destroy: f,hl
  0F31                	PUTPLBF:
  0F31  CD871C        		call	GETPLAD
  0F34  1803          		jr	PUTBF
                      	
                      	
                      	;put into key buffer
                      	;input: a
                      	;output: c-flag (1=buffer full)
                      	;destroy: f,hl
  0F36                	PUTKBF:
  0F36  218FFB        		ld	hl,KYBFIN
                      	;	call	PUTBF
                      	;	ret
                      	
                      	
                      	;put characer into buffer
                      	;input: a=character, hl=buffer control address
                      	;output: c-flag (1=buffer full)
                      	;destroy: f,hl
  0F39                	PUTBF:
  0F39  F5            		push	af
  0F3A  7E            		ld	a,(hl)		;in
  0F3B  3C            		inc	a
  0F3C  2C            		inc	l		;inc hl
  0F3D  2C            		inc	l		;inc hl
  0F3E  2C            		inc	l		;inc hl
  0F3F  A6            		and	(hl)		;size
  0F40  2D            		dec	l		;dec hl
  0F41  2D            		dec	l		;dec hl
  0F42  BE            		cp	(hl)		;out
  0F43  2811          		jr	z,BFFULL
                      	
  0F45  2D            		dec	l		;dec hl
  0F46  77            		ld	(hl),a		;in
  0F47  2C            		inc	l		;inc hl
  0F48  2C            		inc	l		;inc hl
  0F49  2C            		inc	l		;inc hl
  0F4A  2C            		inc	l		;inc hl
  0F4B  86            		add	a,(hl)		;address
  0F4C  2C            		inc	l		;inc hl
  0F4D  66            		ld	h,(hl)
  0F4E  6F            		ld	l,a
  0F4F  3001          		jr	nc,PTBNC
  0F51  24            		inc	h
  0F52                	PTBNC:
  0F52  F1            		pop	af
  0F53  77            		ld	(hl),a
  0F54  B7            		or	a		;reset c-flag
  0F55  C9            		ret
                      	
  0F56                	BFFULL:
  0F56  F1            		pop	af
  0F57  37            		scf
  0F58  C9            		ret
                      	
                      	
                      	;interrupt for RS-232C
  0F59                	INT232:
  0F59  F5            		push	af
  0F5A  E5            		push	hl
  0F5B  3E17          		ld	a,17h		;RTS=0
  0F5D  D381          		out	(81h),a
                      	
  0F5F  DB80          		in	a,(80h)
  0F61  2195FB        		ld	hl,RSBFIN
  0F64  CD390F        		call	PUTBF
                      	
  0F67  3E0C          		ld	a,0ch		;enable RS-232C interrupt
  0F69  CD8F0E        		call	OUT90H
                      	
  0F6C  3E37          		ld	a,37h		;RTS=1
  0F6E  D381          		out	(81h),a
  0F70  E1            		pop	hl
  0F71  1833          		jr	POPAF
                      	
                      	
                      	;interrupt for timer
  0F73                	_INTTIM:ds	INTTIM-_INTTIM
  0F74                		org	INTTIM
                      	
  0F74  C5            		push	bc
  0F75  F5            		push	af
  0F76  D5            		push	de
  0F77  E5            		push	hl
                      	
  0F78  CDEC1B        		call	PLAYINT
                      	
  0F7B  2128FA        		ld	hl,TMCNT
  0F7E  34            		inc	(hl)
  0F7F  34            		inc	(hl)
  0F80  20A9          		jr	nz,IKPOP
  0F82  2C            		inc	l		;inc hl
  0F83  34            		inc	(hl)
  0F84  2006          		jr	nz,TIMCSR
  0F86  2C            		inc	l
  0F87  34            		inc	(hl)
  0F88  2002          		jr	nz,TIMCSR
  0F8A  2C            		inc	l
  0F8B  34            		inc	(hl)
                      	
  0F8C                	TIMCSR:
                      	;cursor blink
  0F8C  3A92FD        		ld	a,(SCREEN1)
  0F8F  212EFA        		ld	hl,CSRBLNK
  0F92  2F            		cpl
  0F93  0F            		rrca
  0F94  A6            		and	(hl)
  0F95  C48F11        		call	nz,CSRREV	;(SCREEN1)=0or1, (CSRBLNK)=1
  0F98  1891          		jr	IKPOP
                      	
                      	
                      	;interrupt for CMT load
  0F9A                	INTCMT:
  0F9A  F5            		push	af
  0F9B  CD780E        		call	IN90H
  0F9E  321DFA        		ld	(CMTBUF),a
  0FA1  3E02          		ld	a,02h
  0FA3                	SETCMTST:
  0FA3  3219FA        		ld	(CMTSTAT),a
  0FA6                	POPAF:
  0FA6  F1            		pop	af
  0FA7                	EIRET:
  0FA7  FB            		ei
  0FA8  C9            		ret
                      	
                      	
                      	;interrupt for CMT write stop
  0FA9                	INTWSTP:
                      	;interrupt for CMT read stop
  0FA9                	INTRSTP:
  0FA9  F5            		push	af
  0FAA  3E03          		ld	a,03h
  0FAC  3218FA        		ld	(STOPFLG),a
  0FAF  18F5          		jr	POPAF
                      	
                      	
                      	;interrupt for CMT error
  0FB1                	INTERR:
  0FB1  F5            		push	af
  0FB2  3E10          		ld	a,10h
  0FB4  18ED          		jr	SETCMTST
                      	
                      	
                      	;get input character
                      	;output: a, z-flag(1=no input)
                      	;destroy: f
  0FB6                	_GETCH:	ds	GETCH-_GETCH
  0FBC                		org	GETCH
                      	
  0FBC  E5            		push	hl
  0FBD  CDF40F        		call	GETCH2
  0FC0  E1            		pop	hl
  0FC1  C9            		ret
                      	
                      	
                      	;wait for input and get a character
                      	;output: a
                      	;destroy: f
  0FC2                	_GETC:	ds	GETC-_GETC
  0FC4                		org	GETC
                      	
  0FC4  E5            		push	hl
  0FC5                	GETCLP1:
  0FC5  CD7911        		call	CSRON
  0FC8  2600          		ld	h,00h
  0FCA                	GETCLP2:
  0FCA  CD7A30        		call	MODEKEY
  0FCD  24            		inc	h
  0FCE  CCB912        		call	z,PRTFKEY2
  0FD1  CDBC0F        		call	GETCH
  0FD4  28F4          		jr	z,GETCLP2
                      	
  0FD6  CD8111        		call	CSROFF
  0FD9  FEFE          		cp	0feh		;page switching key
  0FDB  2807          		jr	z,GETCPAGE
  0FDD  67            		ld	h,a
  0FDE  CD7C13        		call	CHGTXT
  0FE1  7C            		ld	a,h
  0FE2  E1            		pop	hl
  0FE3  C9            		ret
                      	
  0FE4                	GETCPAGE:
  0FE4  3A8FFD        		ld	a,(SCREEN2)
  0FE7  3C            		inc	a
  0FE8  218CFD        		ld	hl,PAGES
  0FEB  BE            		cp	(hl)
  0FEC  3801          		jr	c,GETCC
  0FEE  AF            		xor	a
  0FEF                	GETCC:
  0FEF  CD8913        		call	CHGSCR
  0FF2  18D1          		jr	GETCLP1
                      	
                      	
  0FF4                	GETCH2:
  0FF4  3A32FA        		ld	a,(FKEYCNT)
  0FF7  B7            		or	a
  0FF8  202E          		jr	nz,GETFKEY
  0FFA  2130FA        		ld	hl,GRPWRK
  0FFD  B6            		or	(hl)		;a=0
  0FFE  2808          		jr	z,GETCHZ	;(hl)=0?
  1000  3600          		ld	(hl),00h
  1002  FEEF          		cp	0efh		;Ctrl-T=14h+0efh
  1004  C8            		ret	z
  1005  C630          		add	a,30h
  1007  C9            		ret			;z-flag=0
                      	
  1008                	GETCHZ:
                      	;(GRPWRK)=0
  1008  CD3A10        		call	GETKBF
  100B  C8            		ret	z
  100C  FE14          		cp	14h
  100E  C0            		ret	nz
                      	
                      	;14h+**
  100F  CD3A10        		call	GETKBF
  1012  FEF0          		cp	0f0h
  1014  3006          		jr	nc,CNVFKEY
  1016  3230FA        		ld	(GRPWRK),a
  1019  3E14          		ld	a,14h
  101B  C9            		ret			;z-flag=0
                      	
                      	;a=f0-f9
  101C                	CNVFKEY:
  101C  21BDFB        		ld	hl,FKEYTBL+80h
                      	;	and	0fh
  101F  87            		add	a,a
  1020  87            		add	a,a
  1021  87            		add	a,a
  1022  85            		add	a,l		;no carry
  1023  6F            		ld	l,a
                      	;	jr	nc,CNVFKNC
                      	;	inc	h
                      	;CNVFKNC:
  1024  3E07          		ld	a,07h
  1026  1804          		jr	GETFKEY2
                      	
                      	;function key
  1028                	GETFKEY:
  1028  2A8DFB        		ld	hl,(FKEYAD)
  102B  3D            		dec	a
  102C                	GETFKEY2:
  102C  3232FA        		ld	(FKEYCNT),a
  102F  7E            		ld	a,(hl)
  1030  23            		inc	hl
  1031  228DFB        		ld	(FKEYAD),hl
  1034  B7            		or	a
  1035  C0            		ret	nz
  1036  3232FA        		ld	(FKEYCNT),a	;=0
  1039  C9            		ret			;z-flag=1: no input
                      	
                      	
                      	;get a character from key buffer
                      	;output: a,z-flag(1=no input)
                      	;destroy: f,hl
  103A                	_GETKBF:ds	GETKBF-_GETKBF
  103A                		org	GETKBF
                      	
  103A  218FFB        		ld	hl,KYBFIN
  103D                	CALLGETBF:
  103D  F3            		di
  103E  CD0B26        		call	GETBF
  1041  FB            		ei
  1042  C9            		ret
                      	
                      	
                      	;get a character from RS-232C buffer
                      	;output: a,z-flag(1=no input)
                      	;destroy: f,hl
  1043                	GETRSBF:
  1043  2195FB        		ld	hl,RSBFIN
  1046  18F5          		jr	CALLGETBF
                      	
                      	
                      	;get from PLAY command buffer
                      	;input: b=channel+1 (1,2,3)
                      	;output: a,z-flag(1=no input)
                      	;destroy: f,hl
  1048                	GETPLBF:
  1048  219BFB        		ld	hl,BUFAIN-6
  104B  78            		ld	a,b
  104C  87            		add	a,a		;*2
  104D  80            		add	a,b		;*3
  104E  87            		add	a,a		;*6
  104F  85            		add	a,l		;no carry
  1050  6F            		ld	l,a
  1051  C30B26        		jp	GETBF
                      	
                      	
                      	;clear key buffer pointer
                      	;destroy: hl,(af,bc,de)
  1054                	_CLRKBF:ds	CLRKBF-_CLRKBF
  1058                		org	CLRKBF
                      	
  1058  210000        		ld	hl,0000h
  105B  228FFB        		ld	(KYBFIN),hl	;(KYBFIN),(KYBFOUT)=0
  105E  C9            		ret
                      	
                      	
                      	;send key-query to sub CPU and get reply
                      	;output: a (space-0-left-right-down-up-stop-shift)
                      	;destroy: f (no change in c-flag)
  105F                	_STICK:	ds	STICK-_STICK
  1061                		org	STICK
                      	
  1061  3EFF          		ld	a,0ffh
  1063  32CAFE        		ld	(GMKYBUF),a
  1066  3E06          		ld	a,06h
  1068  F3            		di
  1069  CD8F0E        		call	OUT90H
  106C  FB            		ei
  106D                	STICKLP:
  106D  3ACAFE        		ld	a,(GMKYBUF)
  1070  3C            		inc	a
  1071  28FA          		jr	z,STICKLP	;wait for bit6=0
  1073  3D            		dec	a
  1074  C9            		ret
                      	
                      	
                      	;print a character in CRT
                      	;input: a=character code
                      	;destroy: none
  1075                	_PRTC:	ds	PRTC-_PRTC
  1075                		org	PRTC
                      	
  1075  F5            		push	af
  1076  E5            		push	hl
                      	
  1077  2A31FA        		ld	hl,(GRPFLG)
  107A  2C            		inc	l
  107B  2D            		dec	l
  107C  200D          		jr	nz,PRTGRP
  107E  FE20          		cp	20h
  1080  3010          		jr	nc,PRTCNC
  1082  D5            		push	de
  1083  C5            		push	bc
  1084  CDFA10        		call	PRTCTL
  1087  C1            		pop	bc
  1088  D1            		pop	de
  1089  180A          		jr	PRTCEND
                      	
  108B                	PRTGRP:
  108B  D630          		sub	30h
  108D  2131FA        		ld	hl,GRPFLG
  1090  3600          		ld	(hl),00h
  1092                	PRTCNC:
  1092  CDAA10        		call	PRTCH
                      	
  1095                	PRTCEND:
  1095  E1            		pop	hl
  1096  F1            		pop	af
  1097  C9            		ret
                      	
                      	
                      	;print a character in graphic mode (screen mode 3,4)
                      	;input: a=character
                      	;destroy: af,hl
  1098                	PRT34:
  1098  2AA8FD        		ld	hl,(CSRY)
  109B  CDD614        		call	PRT34XY
  109E                	CTLRGT:
  109E  2AAAFD        		ld	hl,(CSRAD)
  10A1  23            		inc	hl
  10A2  1813          		jr	CHKNL
                      	
                      	;print a character in CRT (no control code)
                      	;input: a=character code
                      	;destroy: af,hl
  10A4                	_PRTCH:ds	PRTCH-_PRTCH
  10AA                		org	PRTCH
                      	
  10AA  2A92FD        		ld	hl,(SCREEN1)
  10AD  CB4D          		bit	1,l
  10AF  20E7          		jr	nz,PRT34	;screen mode 3,4
                      	
  10B1  2AAAFD        		ld	hl,(CSRAD)
  10B4  CDCA14        		call	PRT12
                      	
                      	;next line?
  10B7                	CHKNL:
  10B7  22AAFD        		ld	(CSRAD),hl
  10BA  21A9FD        		ld	hl,CSRX
  10BD  34            		inc	(hl)
  10BE  3AACFD        		ld	a,(WIDTH)
  10C1  BE            		cp	(hl)
  10C2  D0            		ret	nc
  10C3                	CHKNL2:
  10C3  3601          		ld	(hl),1
                      	
  10C5  3AA5FD        		ld	a,(LASTLIN)
  10C8  2AA2FD        		ld	hl,(CONSOL1)	;l=(CONSOL1)
  10CB  95            		sub	l
  10CC  2802          		jr	z,CTLLF
  10CE  3EFF          		ld	a,0ffh
                      	
  10D0                	CTLLF:
  10D0  3C            		inc	a		;0 or 1
  10D1  2AA8FD        		ld	hl,(CSRY)	;l=y+1,h=x+1
                      	
  10D4  E5            		push	hl
  10D5  D5            		push	de
  10D6  CD5114        		call	SETLINE
  10D9  D1            		pop	de
  10DA  E1            		pop	hl
                      	
  10DB  2C            		inc	l
  10DC  3AA5FD        		ld	a,(LASTLIN)
  10DF  BD            		cp	l
  10E0  D26D11        		jp	nc,SETCSR
                      	
                      	;scroll
  10E3  D5            		push	de
  10E4  54            		ld	d,h		;x+1
  10E5  2AA1FD        		ld	hl,(CONSOL1-1)	;h=(CONSOL1)
  10E8  1801          		jr	SKPPATCH1
                      	
                      	
                      	;10eah: to be patched by iP6/iP6win
  10EA                	_PATCH1:ds	PATCH1-_PATCH1
  10EA                		org	PATCH1
  10EA  00            		db	00h
                      	
  10EB                	SKPPATCH1:
  10EB  6F            		ld	l,a
  10EC  9C            		sbc	a,h		;c-flag=1, (LASTLIN)-(CONSOL1)-1
  10ED  D42915        		call	nc,SCRLUP
                      	
  10F0  62            		ld	h,d		;x+1
  10F1  CDB811        		call	Y2AD
  10F4  CDDA11        		call	DELLIN
  10F7  D1            		pop	de
  10F8  1873          		jr	SETCSR
                      	
                      	
  10FA                	PRTCTL:
  10FA  FE14          		cp	14h
  10FC  2847          		jr	z,CTLGRP
  10FE  FE07          		cp	07h
  1100  CACD1B        		jp	z,BELL
  1103  D60A          		sub	0ah
  1105  28C9          		jr	z,CTLLF
  1107  3D            		dec	a
  1108  283F          		jr	z,CTLHOM	;0bh
  110A  3D            		dec	a
  110B  CA7428        		jp	z,CTLL		;0ch
  110E  2AA8FD        		ld	hl,(CSRY)	;l=y+1,h=x+1
  1111  3D            		dec	a
  1112  2838          		jr	z,CTLCR		;0dh
  1114  D60F          		sub	1ch-0dh
  1116  2886          		jr	z,CTLRGT	;1ch
  1118  3D            		dec	a
  1119  2808          		jr	z,CTLLFT	;1dh
  111B  3D            		dec	a
  111C  2814          		jr	z,CTLUP		;1eh
  111E  3D            		dec	a
  111F  C0            		ret	nz
                      	;	jr	z,CTLDWN	;1fh
                      	
  1120                	CTLDWN:
  1120  2C            		inc	l
  1121  1819          		jr	CTLUPNZ
                      	
  1123                	CTLLFT:
  1123  25            		dec	h
  1124  2047          		jr	nz,SETCSR
  1126  3AA2FD        		ld	a,(CONSOL1)
  1129  BD            		cp	l
  112A  D0            		ret	nc
  112B  2D            		dec	l
  112C  3AACFD        		ld	a,(WIDTH)
  112F  67            		ld	h,a
  1130  183B          		jr	SETCSR
                      	
  1132                	CTLUP:
  1132  3AA2FD        		ld	a,(CONSOL1)
  1135  BD            		cp	l
  1136  C8            		ret	z
  1137  2D            		dec	l
  1138  2002          		jr	nz,CTLUPNZ
  113A  2E01          		ld	l,01h
  113C                	CTLUPNZ:
  113C  3AA5FD        		ld	a,(LASTLIN)
  113F  BD            		cp	l
  1140  302B          		jr	nc,SETCSR
  1142  6F            		ld	l,a
  1143  1828          		jr	SETCSR
                      	
  1145                	CTLGRP:
  1145  3231FA        		ld	(GRPFLG),a	;a=14h
  1148  C9            		ret
                      	
  1149                	CTLHOM:
  1149  2AA2FD        		ld	hl,(CONSOL1)	;l=(CONSOL1)
                      	
  114C                	CTLCR:
  114C  2601          		ld	h,1
  114E  181D          		jr	SETCSR
                      	
                      	
                      	;convert VRAM address to text screen (x,y)
                      	;input: hl=address
                      	;output: h=x+1, l=y+1(0=error)
                      	;destroy: af
  1150                	AD2XY:
  1150  D5            		push	de
  1151  112000        		ld	de,COLUMNS
                      	
  1154  CB54          		bit	2,h
  1156  2804          		jr	z,AD2XYZ
                      	
  1158  1E28          		ld	e,CLMN66
  115A  25            		dec	h
  115B  25            		dec	h
  115C                	AD2XYZ:
  115C  25            		dec	h
  115D  25            		dec	h
                      	
  115E  AF            		xor	a
  115F                	AD2XYLP:
  115F  3C            		inc	a
  1160  ED52          		sbc	hl,de
  1162  CB5C          		bit	3,h
  1164  28F9          		jr	z,AD2XYLP
  1166  19            		add	hl,de
  1167  2C            		inc	l
  1168  65            		ld	h,l
  1169  6F            		ld	l,a
  116A  D1            		pop	de
  116B  C9            		ret
                      	
                      	
                      	;set cursor position
                      	;input: h=x+1, l=y+1
                      	;output: (fda8h)=y+1,(fda9h)=x+1,(fdaa-fdab)=VRAM address
                      	;destroy: none
  116C                	_SETCSR:ds	SETCSR-_SETCSR
  116D                		org	SETCSR
                      	
  116D  E5            		push	hl
  116E  22A8FD        		ld	(CSRY),hl
  1171  CDCD11        		call	XY2AD
  1174  22AAFD        		ld	(CSRAD),hl
  1177  E1            		pop	hl
  1178  C9            		ret
                      	
                      	
                      	;cursor blink on
                      	;destroy: none
  1179                	_CSRON:
  1179                		ds	CSRON-_CSRON
  1179                		org	CSRON
                      	
  1179  F5            		push	af
  117A  3E01          		ld	a,01h
  117C  322EFA        		ld	(CSRBLNK),a
  117F  F1            		pop	af
  1180  C9            		ret
                      	
                      	
                      	;cursor blink off
                      	;destroy: hl
  1181                	_CSROFF:
  1181                		ds	CSROFF-_CSROFF
  1181                		org	CSROFF
                      	
  1181  F5            		push	af
  1182  AF            		xor	a
  1183  322EFA        		ld	(CSRBLNK),a
  1186  3A2FFA        		ld	a,(CSRSTAT)
  1189  B7            		or	a
  118A  C48F11        		call	nz,CSRREV
  118D  F1            		pop	af
  118E  C9            		ret
                      	
                      	
                      	;reverse cursor
                      	;destroy: af,hl
  118F                	CSRREV:
  118F  212FFA        		ld	hl,CSRSTAT
  1192  7E            		ld	a,(hl)
  1193  2F            		cpl
  1194  77            		ld	(hl),a
  1195  2AAAFD        		ld	hl,(CSRAD)
                      	
  1198  CB54          		bit	2,h
  119A  C22266        		jp	nz,CSRREV66
                      	
  119D  25            		dec	h
  119E  25            		dec	h
  119F  7E            		ld	a,(hl)
  11A0  CB77          		bit	6,a		;semi-graphic mode?
  11A2  2004          		jr	nz,CSRSEMI
  11A4  EE01          		xor	01h
  11A6  77            		ld	(hl),a
  11A7  C9            		ret
                      	
  11A8                	CSRSEMI:
  11A8  24            		inc	h
  11A9  24            		inc	h
  11AA  7E            		ld	a,(hl)
  11AB  2F            		cpl
  11AC  77            		ld	(hl),a
  11AD  C9            		ret
                      	
                      	
  11AE                	Y2AD2:
  11AE  3A91FD        		ld	a,(VRAM)
  11B1  C602          		add	a,02h
  11B3  82            		add	a,d
  11B4  57            		ld	d,a
  11B5  F1            		pop	af
  11B6  C9            		ret
                      	
                      	
                      	;get left edge address
                      	;input: l=y+1
                      	;output: de=VRAM address
                      	;destroy: none
  11B7                	_Y2AD:ds	Y2AD-_Y2AD
  11B8                		org	Y2AD
                      	
  11B8  F5            		push	af
  11B9  CD9539        		call	CHKMOD
  11BC  CA0066        		jp	z,Y2AD66
                      	
  11BF  7D            		ld	a,l
  11C0  3D            		dec	a
  11C1  0F            		rrca
  11C2  0F            		rrca
  11C3  0F            		rrca
  11C4  57            		ld	d,a
  11C5  E6E0          		and	0e0h
  11C7  5F            		ld	e,a
  11C8  AA            		xor	d
  11C9  57            		ld	d,a
  11CA  18E2          		jr	Y2AD2
                      	
                      	
                      	;get VRAM character address (screen mode 1,2)
                      	;input: h=x+1, l=y+1
                      	;output: hl=VRAM address
                      	;destroy: none
  11CC                	_XY2AD:	ds	XY2AD-_XY2AD
  11CD                		org	XY2AD
                      	
  11CD  F5            		push	af
  11CE  D5            		push	de
  11CF  CDB811        		call	Y2AD
  11D2  25            		dec	h
  11D3  6C            		ld	l,h
  11D4  2600          		ld	h,00h
  11D6  19            		add	hl,de
  11D7  D1            		pop	de
  11D8  F1            		pop	af
  11D9  C9            		ret
                      	
                      	
                      	;delete line
                      	;input: de=left edge address (for screen mode 1,2)
                      	;destroy: none
  11DA                	_DELLIN:ds	DELLIN-_DELLIN
  11DA                		org	DELLIN
                      	
  11DA  F5            		push	af
  11DB  E5            		push	hl
  11DC  D5            		push	de
  11DD  C5            		push	bc
                      	
  11DE  EB            		ex	de,hl
                      	
  11DF  3A92FD        		ld	a,(SCREEN1)
  11E2  FE02          		cp	02h		;;
  11E4  CB54          		bit	2,h		;;;
  11E6  C26C64        		jp	nz,DELLIN66	;;;
  11E9  3018          		jr	nc,DELG		;;
                      	
  11EB  E5            		push	hl		;
                      	;	ld	bc,COLUMNS
  11EC  010120        		ld	bc,2001h
  11EF  25            		dec	h
  11F0  25            		dec	h
                      	
  11F1                	DELLATT:
  11F1  CD450E        		call	GETSPA
  11F4  C5            		push	bc
  11F5  CD4F12        		call	MEMSET
  11F8  C1            		pop	bc
  11F9  E1            		pop	hl		;
                      	
  11FA                	DELLEND:
  11FA  CD1D16        		call	GETSP
  11FD  CD4F12        		call	MEMSET
  1200  C3AB1B        		jp	POPALL
                      	
                      	
                      	;for graphic mode (screen3,4)
  1203                	DELG:
                      	;	ld	bc,COLUMNS*12
  1203  010280        		ld	bc,8002h
  1206  CD7814        		call	AD2GAD
  1209  18EF          		jr	DELLEND
                      	
                      	
                      	;for graphic mode (screen mode 3,4)
  120B                	SCRLG:
                      	;bc=a*32*12=a*(256+128)
  120B  48            		ld	c,b		;b=0
  120C  47            		ld	b,a
  120D  CB38          		srl	b
  120F  CB19          		rr	c
  1211  80            		add	a,b
  1212  47            		ld	b,a
                      	
  1213  2601          		ld	h,1
  1215  CD7D14        		call	XY2GAD60
  1218  EB            		ex	de,hl
  1219  218001        		ld	hl,COLUMNS*12
  121C  19            		add	hl,de
  121D  F1            		pop	af		;up/down
  121E  3003          		jr	nc,SCRLATT
  1220  EB            		ex	de,hl
  1221  1B            		dec	de
  1222  2B            		dec	hl
                      	
  1223                	SCRLATT:
  1223  CD3913        		call	LDIDR
                      	
  1226                	SCRLATT2:
  1226  C1            		pop	bc
  1227  E1            		pop	hl
                      	
                      	;scroll INPUT prompt position
                      	;z=0: up,h=first,l=last
                      	;z=1: down,h=last,l=first
  1228  E5            		push	hl
                      	
  1229  1EFF          		ld	e,0-01h
  122B  2005          		jr	nz,SCRLIUP
  122D  1E01          		ld	e,01h
  122F  7C            		ld	a,h
  1230  65            		ld	h,l
  1231  6F            		ld	l,a
  1232                	SCRLIUP:
  1232  3AABFD        		ld	a,(CSRAD+1)
  1235  57            		ld	d,a
  1236  3A5CFE        		ld	a,(INPTPAG)
  1239  AA            		xor	d
  123A  E6F0          		and	0f0h
  123C  200E          		jr	nz,SCRLIEND
                      	
  123E  3A59FE        		ld	a,(INPTXY)
  1241  BC            		cp	h
  1242  3808          		jr	c,SCRLIEND
  1244  2C            		inc	l
  1245  BD            		cp	l
  1246  3004          		jr	nc,SCRLIEND
  1248  83            		add	a,e
  1249  3259FE        		ld	(INPTXY),a
  124C                	SCRLIEND:
  124C  E1            		pop	hl
                      	
  124D  D1            		pop	de
  124E  C9            		ret
                      	
                      	
                      	;set memory
                      	;input: a=data, b=inner loops, c=outer loops, hl=start address
                      	;destroy: af,bc,hl
  124F                	MEMSET:
  124F  77            		ld	(hl),a
  1250  23            		inc	hl
  1251  10FC          		djnz	MEMSET
  1253  0D            		dec	c
  1254  20F9          		jr	nz,MEMSET
  1256  C9            		ret
                      	
                      	
                      	;print a character in CRT (no control code, no move in cursor)
                      	;input: a=character code, h=x+1, l=y+1
                      	;destroy: none
  1257                	_PRTCHXY:ds	PRTCHXY-_PRTCHXY
  1257                		org	PRTCHXY
                      	
  1257  E5            		push	hl
  1258  CDCD11        		call	XY2AD
  125B  CDAF14        		call	PRTCHAD
  125E  E1            		pop	hl
  125F  C9            		ret
                      	
                      	
                      	;scroll up
                      	;input: h=first line+1, l=last line+1
                      	;destroy: af
  1260                	_SCRLU60:ds	SCRLU60-_SCRLU60
  1260                		org	SCRLU60
                      	
  1260  7D            		ld	a,l
  1261  94            		sub	h		;a=(LASTLIN)-(CONSOL1) < 16
                      	;	ret	z
                      	;	ret	c
                      	
                      	;c-flag=0:scroll up, c-flag=1:scroll down
  1262                	SCRLUD60:
  1262  D5            		push	de
  1263  E5            		push	hl
  1264  C5            		push	bc
  1265  F5            		push	af		;up/down
                      	
                      	;line connection status
  1266  47            		ld	b,a
  1267  04            		inc	b
  1268  50            		ld	d,b		;>0
  1269                	SCRLUD60LP:
  1269  CD4514        		call	CHKLINE4
  126C  4F            		ld	c,a
  126D  7A            		ld	a,d
  126E  E5            		push	hl
  126F  CD5114        		call	SETLINE
  1272  E1            		pop	hl
  1273  51            		ld	d,c
  1274  2D            		dec	l
  1275  F1            		pop	af		;up/down
  1276  F5            		push	af
  1277  3002          		jr	nc,SCRLUD60NC
  1279  2C            		inc	l
  127A  2C            		inc	l
  127B                	SCRLUD60NC:
  127B  10EC          		djnz	SCRLUD60LP
                      	
  127D  6C            		ld	l,h
  127E  3A92FD        		ld	a,(SCREEN1)
  1281  FE02          		cp	02h
  1283  D1            		pop	de		;d=a,e=up/down
  1284  D5            		push	de
  1285  7A            		ld	a,d
  1286  3083          		jr	nc,SCRLG	;screen mode 3 4
                      	
  1288  CD873F        		call	MUL32
  128B  CDB811        		call	Y2AD
  128E  212000        		ld	hl,COLUMNS
  1291  19            		add	hl,de
  1292  F1            		pop	af		;up/down
  1293  3003          		jr	nc,SCRLNC
  1295  EB            		ex	de,hl
  1296  1B            		dec	de
  1297  2B            		dec	hl
                      	
  1298                	SCRLNC:
  1298  E5            		push	hl
  1299  D5            		push	de
  129A  C5            		push	bc
  129B  CD3913        		call	LDIDR
  129E  C1            		pop	bc
  129F  D1            		pop	de
  12A0  E1            		pop	hl
                      	
                      	;attribute
  12A1  CB8C          		res	1,h
  12A3  CB8A          		res	1,d
  12A5  C32312        		jp	SCRLATT
                      	
                      	
                      	;scroll down
                      	;input: h=last line+1, l=first line+1
                      	;destroy: af
  12A8                	_SCRLD60:ds	SCRLD60-_SCRLD60
  12A9                		org	SCRLD60
                      	
  12A9  7C            		ld	a,h
  12AA  95            		sub	l
                      	;	ret	z
                      	;	ret	c
  12AB  37            		scf			;set c-flag
  12AC  18B4          		jr	SCRLUD60
                      	
                      	
                      	;print function key
                      	;input: none
                      	;output: none
                      	;destroy: af
  12AE                	_PRTFKEY:ds	PRTFKEY-_PRTFKEY
  12B5                		org	PRTFKEY
                      	
  12B5  AF            		xor	a
  12B6  32A7FD        		ld	(FKEYSFT),a
                      	
  12B9                	PRTFKEY2:
  12B9  3A92FD        		ld	a,(SCREEN1)
  12BC  FE02          		cp	02h
  12BE  D0            		ret	nc		;screen mode 3 or 4
                      	
  12BF  E5            		push	hl
  12C0  D5            		push	de
  12C1  C5            		push	bc
                      	
  12C2  2A20FA        		ld	hl,(HEIGHT)
  12C5  CDB811        		call	Y2AD
                      	
  12C8  CD6110        		call	STICK		;bit0=shift
  12CB  2AA6FD        		ld	hl,(CONSOL3)	;(CONSOL3)=0 or 1
  12CE  A5            		and	l
  12CF  07            		rlca
  12D0  B5            		or	l
  12D1  21A7FD        		ld	hl,FKEYSFT
  12D4  BE            		cp	(hl)
  12D5  2859          		jr	z,PFKEND
                      	
  12D7  77            		ld	(hl),a
  12D8  0F            		rrca
  12D9  3059          		jr	nc,DELFK
                      	
  12DB  EB            		ex	de,hl
  12DC  113DFB        		ld	de,FKEYTBL
  12DF  0F            		rrca
  12E0  3002          		jr	nc,PFKSTRT
  12E2  1E65          		ld	e,FKEYTBL+8*5-(FKEYTBL+8*5)/256*256	;de=FKEYTBL+8*5
                      	
  12E4                	PFKSTRT:
  12E4  CD4D13        		call	CHRREV
  12E7  3A8FFD        		ld	a,(SCREEN2)
  12EA  C631          		add	a,'1'
  12EC  CDCA14        		call	PRT12
                      	
  12EF  CD4D13        		call	CHRREV
  12F2  3E20          		ld	a,' '
  12F4  CDCA14        		call	PRT12
                      	
  12F7  0E05          		ld	c,05h
  12F9                	FKEYLP1:
  12F9  CD4D13        		call	CHRREV
  12FC  3AC7FE        		ld	a,(FKEYLEN)
  12FF  47            		ld	b,a
  1300                	FKEYLP2:
  1300  1A            		ld	a,(de)
  1301  B7            		or	a
  1302  280C          		jr	z,FKEYNEXT
  1304  FE20          		cp	' '
  1306  3002          		jr	nc,FKEYNC
  1308  3E20          		ld	a,' '
  130A                	FKEYNC:
  130A  CDCA14        		call	PRT12
  130D  1C            		inc	e		;inc de
  130E  10F0          		djnz	FKEYLP2
                      	
  1310                	FKEYNEXT:
  1310  04            		inc	b
  1311                	FKEYLP3:
  1311  78            		ld	a,b
  1312  3D            		dec	a
  1313  CC4D13        		call	z,CHRREV
  1316  3E20          		ld	a,' '
  1318  CDCA14        		call	PRT12
  131B  1C            		inc	e		;inc de
  131C  10F3          		djnz	FKEYLP3
                      	
                      	;	inc	e		;inc de
                      	;	inc	e		;inc de
                      	
  131E  7B            		ld	a,e
  131F  E6F8          		and	0f8h
  1321  F605          		or	FKEYTBL-FKEYTBL/8*8
  1323  5F            		ld	e,a
                      	
  1324  0D            		dec	c
  1325  20D2          		jr	nz,FKEYLP1
                      	
  1327  CD9539        		call	CHKMOD
  132A  CC9864        		call	z,PFK66
                      	
  132D                	PFKEND2:
  132D  CD6F1D        		call	SETCNSL2
  1330                	PFKEND:
  1330  C1            		pop	bc
  1331  D1            		pop	de
  1332  E1            		pop	hl
  1333  C9            		ret
                      	
  1334                	DELFK:
  1334  CDDA11        		call	DELLIN
  1337  18F4          		jr	PFKEND2
                      	
                      	
                      	;ldir or lddr
                      	;input: bc,de,hl,c-flag(0=ldir,1=lddr)
                      	;output: bc=0,de,hl
                      	;destroy: f
  1339                	LDIDR:
  1339  3803          		jr	c,LDIDRC
  133B  EDB0          		ldir
  133D  C9            		ret
                      	
  133E                	LDIDRC:
  133E  EDB8          		lddr
  1340  C9            		ret
                      	
                      	
                      	;get page data address
                      	;input: a=page-1
                      	;output: hl=address, bc=size
                      	;destroy: af
  1341                	GETPGAD:
  1341  21B9FD        		ld	hl,PAGE1
  1344  012800        		ld	bc,PAGE2-PAGE1
  1347  B7            		or	a
  1348                	PGADLP:
  1348  C8            		ret	z
  1349  09            		add	hl,bc
  134A  3D            		dec	a
  134B  18FB          		jr	PGADLP
                      	
                      	
                      	;reverse character attribute
                      	;destroy: af
  134D                	CHRREV:
  134D  CD9539        		call	CHKMOD
  1350  CA1666        		jp	z,CHRREV66
  1353  3A92FD        		ld	a,(SCREEN1)
  1356  EE03          		xor	03h
  1358  2819          		jr	z,REVEND	;screen mode 4
  135A  0F            		rrca
  135B  3A93FD        		ld	a,(COLOR1)
  135E  380D          		jr	c,REV13
                      	
  1360                	REV2:
  1360  B7            		or	a
  1361  CAC015        		jp	z,SETATT
  1364  FE08          		cp	08h
  1366  3802          		jr	c,REV2C
  1368  3E08          		ld	a,08h
  136A                	REV2C:
  136A  3D            		dec	a
  136B  1806          		jr	REVEND
                      	
  136D                	REV13:
  136D  B7            		or	a
  136E  2803          		jr	z,REVEND
  1370  3D            		dec	a
  1371  E603          		and	03h
  1373                	REVEND:
  1373  EE01          		xor	01h
  1375  3C            		inc	a
  1376  3293FD        		ld	(COLOR1),a
  1379  C3C015        		jp	SETATT
                      	
                      	
                      	;change to text screen
                      	;input: none
                      	;output: none
                      	;destroy: af
  137C                	CHGTXT:
  137C  3A90FD        		ld	a,(SCREEN3)
  137F  CD0C14        		call	CHGACT
  1382  3A92FD        		ld	a,(SCREEN1)
  1385  FE02          		cp	02h
  1387  D8            		ret	c
  1388  AF            		xor	a
  1389                	CHGSCR:
  1389  CD0C14        		call	CHGACT
  138C  185F          		jr	CHGDSP
                      	
                      	
                      	;change screen mode
                      	;input: a=mode-1
                      	;destroy: af,de
  138E                	_CHGMOD:ds	CHGMOD-_CHGMOD
  1390                		org	CHGMOD
                      	
  1390  FE04          		cp	04h
  1392  D2CB03        		jp	nc,FCERR
  1395  E5            		push	hl
  1396  2192FD        		ld	hl,SCREEN1
  1399  5E            		ld	e,(hl)		;
  139A  BB            		cp	e
  139B  284E          		jr	z,CMODEND
  139D  77            		ld	(hl),a
                      	
  139E  C5            		push	bc
  139F  F5            		push	af		;;
                      	
  13A0  FE02          		cp	02h
  13A2  3E20          		ld	a,COLUMNS	;screen mode 1,2,4:width=32
  13A4  2001          		jr	nz,SETWID
  13A6  1F            		rra			;screen mode 3:width=16
  13A7                	SETWID:
  13A7  32ACFD        		ld	(WIDTH),a
                      	
  13AA  2196FD        		ld	hl,M1COLOR
  13AD  010300        		ld	bc,0003h
  13B0  50            		ld	d,b		;b=0
  13B1  19            		add	hl,de		;
  13B2  19            		add	hl,de
  13B3  19            		add	hl,de
  13B4  EB            		ex	de,hl
  13B5  2193FD        		ld	hl,COLOR1
  13B8  EDB0          		ldir
                      	
  13BA  F1            		pop	af		;;
  13BB  2196FD        		ld	hl,M1COLOR
  13BE  50            		ld	d,b		;b=0
  13BF  5F            		ld	e,a
  13C0  19            		add	hl,de
  13C1  19            		add	hl,de
  13C2  19            		add	hl,de
  13C3  1193FD        		ld	de,COLOR1
  13C6  0E03          		ld	c,03h		;b=0
  13C8  EDB0          		ldir
                      	
  13CA  CD450E        		call	GETSPA
  13CD  2A90FD        		ld	hl,(VRAM-1)	;h=(VRAM)
  13D0  68            		ld	l,b		;b=0
  13D1  77            		ld	(hl),a
  13D2  04            		inc	b
  13D3  54            		ld	d,h
  13D4  58            		ld	e,b		;de=hl+1
  13D5  0D            		dec	c		;bc=1ffh
                      	
  13D6  CD9539        		call	CHKMOD
  13D9  CC4561        		call	z,CHGMOD66
                      	
  13DC  EDB0          		ldir
                      	
  13DE  CD6F1D        		call	SETCNSL2
  13E1  0E01          		ld	c,01h
  13E3  3A20FA        		ld	a,(HEIGHT)
  13E6  37            		scf
  13E7  CDDC1D        		call	CLSMAIN
                      	
  13EA  C1            		pop	bc
  13EB                	CMODEND:
  13EB  E1            		pop	hl
  13EC  C9            		ret
                      	
                      	
                      	;change display page
                      	;page1=8000h or c000h	out (0b0h),4 or 0
                      	;page2=e000h		out (0b0h),2
                      	;page3=c000h		out (0b0h),0
                      	;page4=a000h		out (0b0h),6
                      	;input: a=page-1
                      	;destroy: af
  13ED                	_CHGDSP:ds	CHGDSP-_CHGDSP
  13ED                		org	CHGDSP
                      	
  13ED  3290FD        		ld	(SCREEN3),a
                      	
  13F0  E5            		push	hl
  13F1  C5            		push	bc
                      	
  13F2  CD4113        		call	GETPGAD
  13F5  CD9539        		call	CHKMOD		;
  13F8  7E            		ld	a,(hl)
  13F9  CC1F61        		call	z,CHGDSP66
  13FC  07            		rlca
  13FD  07            		rlca
  13FE  07            		rlca
  13FF  07            		rlca
  1400  EE04          		xor	04h
                      	
  1402  0606          		ld	b,00000110b
  1404  CD541B        		call	OUTB0H
                      	
  1407  C1            		pop	bc
  1408  E1            		pop	hl
                      	
  1409  C3B912        		jp	PRTFKEY2
                      	
                      	
                      	;change active page
                      	;input: a=page-1
                      	;destroy: none
  140C                	_CHGACT:ds	CHGACT-_CHGACT
  140C                		org	CHGACT
                      	
  140C  F5            		push	af
  140D  E5            		push	hl
                      	
  140E  218FFD        		ld	hl,SCREEN2
  1411  BE            		cp	(hl)
  1412  281C          		jr	z,ACTEND
                      	
  1414  D5            		push	de
  1415  C5            		push	bc
                      	
  1416  57            		ld	d,a		;
  1417  7E            		ld	a,(hl)
  1418  72            		ld	(hl),d		;
  1419  CD4113        		call	GETPGAD
  141C  7A            		ld	a,d		;
  141D  EB            		ex	de,hl
  141E  2191FD        		ld	hl,VRAM		;top of active page data
  1421  EDB0          		ldir
                      	
  1423  CD4113        		call	GETPGAD
  1426  1191FD        		ld	de,VRAM
  1429  EDB0          		ldir
                      	
  142B  CDB512        		call	PRTFKEY
                      	
  142E  C1            		pop	bc
  142F  D1            		pop	de
                      	
  1430                	ACTEND:
  1430  E1            		pop	hl
  1431  F1            		pop	af
  1432  C9            		ret
                      	
                      	
                      	;check connection to previous character
                      	;input: hl=VRAM address
                      	;output: z-flag(1:connect)
                      	;destroy: af
  1433                	CHKLINE:
  1433  E5            		push	hl
  1434  CD5011        		call	AD2XY
  1437  CD3C14        		call	CHKLINE2
  143A  E1            		pop	hl
  143B  C9            		ret
                      	
                      	
                      	;check connection to previous character
                      	;input: h=x+1, l=y+1
                      	;output: z-flag(1:connect), a(0 or not)
                      	;destroy: f
  143C                	CHKLINE2:
  143C  7C            		ld	a,h
  143D  3D            		dec	a
  143E  2802          		jr	z,CHKLINE3
  1440  AF            		xor	a
  1441  C9            		ret
                      	
                      	
                      	;check connection to previous line
                      	;input: l=y+1
                      	;output: z-flag(1:connect), a(0 or not)
                      	;destroy: f
  1442                	CHKLINE3:
  1442  E5            		push	hl
  1443  2D            		dec	l
                      	;	jr	CHKLMAIN
  1444  3E            		db	3eh		;ld a,
                      	
                      	;check connection to next line
                      	;input: l=y+1
                      	;output: z-flag(1:connect), a(0 or not)
                      	;destroy: f
  1445                	CHKLINE4:
  1445  E5            		push	hl
  1446                	CHKLMAIN:
  1446  CD6214        		call	CALCLINE
  1449  A6            		and	(hl)
  144A  E1            		pop	hl
  144B  C9            		ret
                      	
                      	
                      	;cut connection before cursor line
                      	;output: hl=line status address
                      	;destroy: af,de
  144C                	CUTLINE:
  144C  3AA8FD        		ld	a,(CSRY)	;a>0
                      	
                      	;input: a=y+1
                      	;output: hl=line status address
                      	;destroy: f,de
  144F                	CUTLINE2:
  144F  6F            		ld	l,a
  1450  2D            		dec	l
                      	;	jp	SETLINE
                      	
                      	;set line connection status (connection to next line)
                      	;input: l=y+1, a=data (0=connect or not)
                      	;output: hl=line status address
                      	;destroy: f,de
  1451                	SETLINE:
  1451  57            		ld	d,a
  1452  CD6214        		call	CALCLINE
  1455  14            		inc	d
  1456  15            		dec	d
  1457  2804          		jr	z,SETLINEZ
  1459  B6            		or	(hl)
  145A  77            		ld	(hl),a
  145B  7A            		ld	a,d
  145C  C9            		ret
                      	
  145D                	SETLINEZ:
  145D  2F            		cpl
  145E  A6            		and	(hl)
  145F  77            		ld	(hl),a
  1460  7A            		ld	a,d
  1461  C9            		ret
                      	
                      	
                      	;calculate line status address and bit (connection to next line)
                      	;input: l=y+1
                      	;output: hl=line status address, a=bit
                      	;destroy: f
  1462                	CALCLINE:
  1462  C5            		push	bc
  1463  7D            		ld	a,l
  1464  21B4FD        		ld	hl,LINEST-1
  1467                	CALCLLP1:
  1467  23            		inc	hl
  1468  D608          		sub	08h
  146A  30FB          		jr	nc,CALCLLP1
  146C  C609          		add	a,09h
  146E  47            		ld	b,a
  146F  3E80          		ld	a,80h
  1471                	CALCLLP2:
  1471  07            		rlca
  1472  10FD          		djnz	CALCLLP2
  1474  C1            		pop	bc
  1475  C9            		ret
                      	
                      	
                      	;convert character VRAM address to graphic VRAM address
                      	;(screen mode 1,2 -> screen mode 3,4)
                      	;input: hl=character VRAM address
                      	;output: hl=graphic VRAM address
                      	;destroy: none
  1476                	_AD2GAD:ds	AD2GAD-_AD2GAD
  1478                		org	AD2GAD
                      	
  1478  F5            		push	af
  1479  CD5011        		call	AD2XY
  147C  F1            		pop	af
                      	;	jr	XY2GAD60
                      	
                      	
                      	;get VRAM address (screen mode 3,4)
                      	;input: h=x+1, l=y+1
                      	;output: hl=graphic VRAM address =(VRAM+200h)+y*0180h+x
                      	;destroy: none
  147D                	XY2GAD60:
  147D  F5            		push	af
  147E  25            		dec	h
  147F  2D            		dec	l
  1480  3A92FD        		ld	a,(SCREEN1)
  1483  FE02          		cp	02h
  1485  2002          		jr	nz,XY2GAD60C
  1487  CB24          		sla	h		;screen mode 3
  1489                	XY2GAD60C:
  1489  CB24          		sla	h
  148B  3A91FD        		ld	a,(VRAM)
  148E  C602          		add	a,02h
  1490  85            		add	a,l
  1491  CB3D          		srl	l
  1493  CB1C          		rr	h
  1495  85            		add	a,l
  1496  6C            		ld	l,h
  1497  67            		ld	h,a
  1498  F1            		pop	af
  1499  C9            		ret
                      	
                      	
                      	;get CGROM address (DE=6000h+A*10h)
                      	;input: a=character code
                      	;output: de=address
                      	;destroy: none
  149A                	_CGROM:	ds	CGROM-_CGROM
  14A0                		org	CGROM
                      	
  14A0  F5            		push	af
  14A1  07            		rlca
  14A2  07            		rlca
  14A3  07            		rlca
  14A4  07            		rlca
  14A5  57            		ld	d,a
  14A6  E6F0          		and	0f0h
  14A8  5F            		ld	e,a
  14A9  AA            		xor	d
  14AA  C660          		add	a,60h
  14AC  57            		ld	d,a
  14AD  F1            		pop	af
  14AE  C9            		ret
                      	
                      	;	push	af		;push flag
                      	;	ex	de,hl
                      	;	ld	h,06h
                      	;	ld	l,a
                      	;	add	hl,hl
                      	;	add	hl,hl
                      	;	add	hl,hl
                      	;	add	hl,hl
                      	;	ex	de,hl
                      	;	pop	af		;pop flag
                      	;	ret
                      	
                      	
                      	;print a character in CRT (no control code, no move in cursor)
                      	;input: a=character code, hl=VRAM address
                      	;destroy: none
  14AF                	_PRTCHAD:ds	PRTCHAD-_PRTCHAD
  14AF                		org	PRTCHAD
                      	
  14AF  E5            		push	hl
  14B0  F5            		push	af
  14B1  3A92FD        		ld	a,(SCREEN1)
  14B4  FE02          		cp	02h
  14B6  380A          		jr	c,PRTCHXY12
                      	
                      	;screen mode 3,4
  14B8  CD5011        		call	AD2XY
  14BB  F1            		pop	af
  14BC  F5            		push	af
  14BD  CDD614        		call	PRT34XY
  14C0  1805          		jr	PRTCHADEND
                      	
  14C2                	PRTCHXY12:
  14C2  F1            		pop	af
  14C3  F5            		push	af
  14C4  CDCA14        		call	PRT12
                      	
  14C7                	PRTCHADEND:
  14C7  F1            		pop	af
  14C8  E1            		pop	hl
  14C9  C9            		ret
                      	
                      	
                      	;print a character in text mode (screen mode 1,2)
                      	;input: a=character code, hl=VRAM address
                      	;output: hl=hl+1
                      	;destroy: af
  14CA                	PRT12:
  14CA  E5            		push	hl
  14CB  77            		ld	(hl),a
  14CC  CD3C0E        		call	CHR2ATT
  14CF  CD0316        		call	CNVATT1
  14D2  77            		ld	(hl),a
  14D3  E1            		pop	hl
  14D4  23            		inc	hl
  14D5  C9            		ret
                      	
                      	
                      	;print a character in graphic mode (screen mode 3,4)
                      	;input: a=character code, h=x+1, l=y+1
                      	;destroy af,hl
  14D6                	PRT34XY:
  14D6  D5            		push	de
  14D7  C5            		push	bc
                      	
  14D8  CDA014        		call	CGROM
  14DB  3A93FD        		ld	a,(COLOR1)
  14DE  CDC015        		call	SETATT
                      	
  14E1  CD9539        		call	CHKMOD
  14E4  CA3B66        		jp	z,PRT34XY66
  14E7  CD7D14        		call	XY2GAD60
                      	
  14EA  3E04          		ld	a,04h
  14EC  D393          		out	(93h),a		;CGROM ON
                      	
  14EE  060C          		ld	b,0ch
  14F0                	PRT34XYLP:
  14F0  D5            		push	de
  14F1  3A92FD        		ld	a,(SCREEN1)
  14F4  0F            		rrca			;
  14F5  1A            		ld	a,(de)
  14F6  3005          		jr	nc,CALL3	;
  14F8  CD2015        		call	PRT4
  14FB  1804          		jr	CALL34END
  14FD                	CALL3:
  14FD  CD1015        		call	PRT3
  1500  2B            		dec	hl
                      	
  1501                	CALL34END:
  1501  112000        		ld	de,COLUMNS
  1504  19            		add	hl,de
  1505  D1            		pop	de
  1506  1C            		inc	e		;inc de
  1507  10E7          		djnz	PRT34XYLP
                      	
  1509  3E05          		ld	a,05h
  150B  D393          		out	(93h),a		;CGROM OFF
                      	
  150D  C1            		pop	bc
  150E  D1            		pop	de
  150F  C9            		ret
                      	
  1510                	PRT3:
  1510  5F            		ld	e,a
  1511  CD1515        		call	PRT3HALF
  1514  23            		inc	hl
  1515                	PRT3HALF:
  1515  1604          		ld	d,04h
  1517                	PRT3LP:
  1517  CB03          		rlc	e
  1519  17            		rla
  151A  0F            		rrca
  151B  07            		rlca
  151C  17            		rla
  151D  15            		dec	d
  151E  20F7          		jr	nz,PRT3LP
                      	
  1520                	PRT4:
  1520  4F            		ld	c,a
  1521  3AACFE        		ld	a,(ATTDAT)
  1524  AE            		xor	(hl)
  1525  A1            		and	c
  1526  AE            		xor	(hl)
  1527  77            		ld	(hl),a
  1528  C9            		ret
                      	
                      	
                      	;call SCRLU60 or SCRLU66
                      	;destroy: af
  1529                	SCRLUP:
  1529  CD9539        		call	CHKMOD
  152C  C26012        		jp	nz,SCRLU60
  152F  C3E263        		jp	SCRLU66
                      	
                      	
                      	;call SCRLD60 or SCRLD66
                      	;destroy: af
  1532                	SCRLDW:
  1532  CD9539        		call	CHKMOD
  1535  C2A912        		jp	nz,SCRLD60
  1538  C32F64        		jp	SCRLD66
                      	
                      	
                      	;click sound
                      	;destroy: de
  153B                	CLICK:
  153B  F5            		push	af
  153C  3A2DFA        		ld	a,(CONSOL4)
  153F  B7            		or	a
  1540  2823          		jr	z,SKPCLK
                      	
  1542  C5            		push	bc
                      	
  1543  3E08          		ld	a,08h		;register8=ch.A volume
  1545  1E0F          		ld	e,0fh
  1547  CDBE1B        		call	SETPSG2
  154A  D5            		push	de		;
                      	
  154B  3E01          		ld	a,01h		;register1=ch.A coarse tune
  154D  1E00          		ld	e,00h
  154F  43            		ld	b,e
  1550  4F            		ld	c,a
  1551  CDBE1B        		call	SETPSG2		;;
                      	
  1554  CDE825        		call	WAITLP		;bc=0001h
  1557  3E01          		ld	a,01h		;register1=ch.A coarse tune
  1559  5A            		ld	e,d		;;
  155A  CDC51B        		call	SETPSG
                      	
  155D  3E08          		ld	a,08h		;register8=ch.A volume
  155F  D1            		pop	de		;
  1560  5A            		ld	e,d
  1561  CDC51B        		call	SETPSG
                      	
  1564  C1            		pop	bc
                      	
  1565                	SKPCLK:
  1565  F1            		pop	af
  1566  C9            		ret
                      	
                      	
                      	;graphic cordinates -> screen mode cordinates
                      	;input: bc=graphic X, de=graphic Y
                      	;output: bc=screen mode X, de=screen mode Y, (c-flag=1 for EXAS BASIC compiler)
                      	;destroy: af
  1567                	_GXY2SXY:ds	GXY2SXY-_GXY2SXY
  1578                		org	GXY2SXY
                      	
  1578  E5            		push	hl
  1579  CDF42B        		call	CHKGXY
  157C  E1            		pop	hl
  157D  CD9539        		call	CHKMOD
  1580  3A92FD        		ld	a,(SCREEN1)
  1583  CA8B66        		jp	z,GXY2SXY66
  1586  CB3F          		srl	a
  1588  380F          		jr	c,GXY2SXY24
  158A  2009          		jr	nz,GXY2SXY3
                      	
                      	;screen mode 1: 32x16
  158C                	GXY2SXY1:
  158C  C5            		push	bc
  158D  CDA015        		call	DIV12
  1590  C1            		pop	bc
  1591  CB39          		srl	c
  1593                	SRLC2:
  1593  CB39          		srl	c
                      	
                      	;screen mode 3: 128x192
  1595                	GXY2SXY3:
  1595  CB39          		srl	c
  1597  37            		scf			;for EXAS BASIC compiler
  1598  C9            		ret
                      	
  1599                	GXY2SXY24:
                      	;	scf
  1599  C0            		ret	nz		;screen mode 4: 256x192
                      	
                      	;screen mode 2: 64x48
  159A                	GXY2SXY2:
  159A  CB3B          		srl	e
  159C  CB3B          		srl	e
  159E  18F3          		jr	SRLC2
                      	
                      	
                      	;de=de/12
                      	;input: de<192
                      	;output: de=int(de/12), a=de mod 12
                      	;destroy: f,b=0,l
  15A0                	DIV12:
  15A0  7B            		ld	a,e
  15A1  5A            		ld	e,d		;d=0
  15A2  2E60          		ld	l,01100000b
  15A4  0604          		ld	b,04h
  15A6                	DIV12LP:
  15A6  BD            		cp	l
  15A7  3801          		jr	c,DIV12C
  15A9  95            		sub	l
  15AA                	DIV12C:
  15AA  3F            		ccf
  15AB  CB13          		rl	e
  15AD  CB3D          		srl	l
  15AF  10F5          		djnz	DIV12LP
  15B1  C9            		ret
                      	
                      	
                      	;graphic cordinates -> VARM address
                      	; 60: screen1=+0000h, screen2-4=+0200h
                      	; 66: screen1=+0000h, screen2=+0400h, screen3-4=+2000h
                      	;input: bc=graphic X, de=graphic Y
                      	;output: hl=VRAM address, a=bit
                      	;destroy: af,bc,e
  15B2                	GXY2AD:
  15B2  CD7815        		call	GXY2SXY
  15B5  C3BA15        		jp	SXY2AD
                      	
                      	
                      	;screen mode cordinates -> VARM address
                      	; 60: screen1=+0000h, screen2-4=+0200h
                      	; 66: screen1=+0000h, screen2=+0400h, screen3-4=+2000h
                      	;input: bc=screen mode X, de=screen mode Y
                      	;output: hl=(fdb3-fdb4)=attribute address, a=(fdb2)=bit
                      	;destroy: f
  15B8                	_SXY2AD:ds	SXY2AD-_SXY2AD
  15BA                		org	SXY2AD
                      	
  15BA  CD9539        		call	CHKMOD
  15BD  C3B13F        		jp	SXY2ADMAIN
                      	
                      	
                      	;set attribute data
                      	;input: a=color code
                      	;output: a,(ATTDAT)=attribute, (c-flag=0 for EXAS BASIC compiler)
                      	;destroy: f
  15C0                	_SETATT:ds	SETATT-_SETATT
  15C0                		org	SETATT
                      	
  15C0  F5            		push	af
  15C1  CD9539        		call	CHKMOD
  15C4  CAFE64        		jp	z,SETATT66
  15C7  F1            		pop	af
  15C8  CDD815        		call	CNVATT
  15CB  32ACFE        		ld	(ATTDAT),a
  15CE  C9            		ret
                      	
                      	
                      	;get color code
                      	;input: (fdb3h-fdb4h)=attribute address, (fdb2h)=bit
                      	;output: a=color
                      	;destroy: f,hl
  15CF                	_GETCOLR:ds	GETCOLR-_GETCOLR
  15D5                		org	GETCOLR
                      	
  15D5  C3763E        		jp	GETCOLRMAIN
                      	
                      	
                      	;convert to attribute data (screen mode 1)
                      	; or color code (screen moode 2)
                      	; or bit pattern (screen mode 3,4)
                      	;input: a=color code
                      	;output: a=attribute, (c-flag=0 for EXAS BASIC compiler)
                      	;destroy: f
  15D8                	CNVATT:
  15D8  F5            		push	af		;color
  15D9  3A92FD        		ld	a,(SCREEN1)
  15DC  CB3F          		srl	a
  15DE  2818          		jr	z,ATT12		;screen mode 1 or 2
  15E0  3006          		jr	nc,ATT3		;screen mode 3
                      	
  15E2                	ATT4:
  15E2  F1            		pop	af
  15E3  B7            		or	a
  15E4  C8            		ret	z
  15E5  3EFF          		ld	a,0ffh
  15E7  C9            		ret
                      	
  15E8                	ATT3:
  15E8  F1            		pop	af
  15E9  B7            		or	a
  15EA  C8            		ret	z
                      	
  15EB  C5            		push	bc
                      	;	ld	b,04h
                      	;	cp	b
                      	;	jr	c,ATT3C
                      	;	ld	a,b
                      	;ATT3C:
                      	;	dec	a
                      	;	ld	c,a
                      	;	dec	b		;=3
  15EC  0603          		ld	b,03h
  15EE  3D            		dec	a
  15EF  A0            		and	b
  15F0  4F            		ld	c,a
  15F1                	ATT3LP:
  15F1  0F            		rrca
  15F2  0F            		rrca
  15F3  B1            		or	c
  15F4  10FB          		djnz	ATT3LP
  15F6  C1            		pop	bc
  15F7  C9            		ret
                      	
  15F8                	ATT12:
  15F8  3013          		jr	nc,ATT1
                      	
  15FA                	ATT2:
  15FA  F1            		pop	af
  15FB  FE09          		cp	09h
  15FD  3F            		ccf			;reset c-flag for EXAS BASIC compiler
  15FE  D0            		ret	nc
  15FF  3E08          		ld	a,08h
  1601  B7            		or	a		;reset c-flag for EXAS BASIC compiler
  1602  C9            		ret
                      	
                      	
                      	;get attribue data for screen mode 1
                      	;output: a=attirbute value
                      	;destroy: f
  1603                	CNVATT1:
  1603  3A93FD        		ld	a,(COLOR1)
  1606  F5            		push	af
  1607  CD9539        		call	CHKMOD
  160A  CA3165        		jp	z,ATT661
  160D                	ATT1:
  160D  F1            		pop	af
  160E  B7            		or	a
  160F  2803          		jr	z,ATT1OK
  1611  3D            		dec	a
                      	;	cp	04h
                      	;	jr	c,ATT1OK
                      	;	ld	a,03h
  1612  E603          		and	03h
  1614                	ATT1OK:
  1614  C620          		add	a,20h
  1616  E5            		push	hl
  1617  2195FD        		ld	hl,COLOR3
  161A  AE            		xor	(hl)
  161B  E1            		pop	hl
  161C  C9            		ret
                      	
                      	
                      	;get space character data for scroll
                      	;output: a
                      	;destroy: f
  161D                	GETSP:
  161D  CD9539        		call	CHKMOD
  1620  3A92FD        		ld	a,(SCREEN1)
  1623  CACA64        		jp	z,GETSP66
  1626  3D            		dec	a
  1627  3E20          		ld	a,' '
  1629  F8            		ret	m		;screen mode 1
  162A  3A94FD        		ld	a,(COLOR2)
  162D  20A9          		jr	nz,CNVATT	;screen mode 3,4
                      	
                      	;screen mode 2
  162F                	GETSP2:
  162F  B7            		or	a
  1630  2806          		jr	z,GETSP2Z	;color,0
  1632  3D            		dec	a
  1633  0F            		rrca
  1634  0F            		rrca
  1635  F63F          		or	3fh
  1637  C9            		ret
                      	
  1638                	GETSP2Z:
  1638  3A93FD        		ld	a,(COLOR1)
  163B  B7            		or	a
  163C  C8            		ret	z
  163D  3D            		dec	a
  163E  FE04          		cp	04h
  1640  3802          		jr	c,GETSP2ZC
  1642  D604          		sub	04h
  1644                	GETSP2ZC:
  1644  0F            		rrca
  1645  0F            		rrca
  1646  C9            		ret
                      	
                      	
                      	;cold start (initialize hardware)
  1647                	COLD:
                      	
                      	;8255 (port 93h)
  1647  019306        		ld	bc,0693h
  164A  78            		ld	a,b
  164B  21DA00        		ld	hl,IOTBL93
  164E  EDB3          		otir
                      	
                      	;port c1h
                      	;	ld	a,06h
  1650  D3C1          		out	(0c1h),a	;32x16 text mode
                      	
                      	;8251 (port 81h)
  1652  018105        		ld	bc,0581h
                      	;	ld	hl,IOTBL81
  1655  EDB3          		otir
  1657  3E0C          		ld	a,0ch		;enable RS-232C interrupt
  1659  CD8F0E        		call	OUT90H
                      	
                      	;port f0h-f8h
  165C  01EF09        		ld	bc,09efh
                      	;	ld	hl,IOTBLF0
  165F                	F0LP:
  165F  0C            		inc	c
  1660  EDA3          		outi
  1662  20FB          		jr	nz,F0LP
                      	
                      	;interrupt
                      	;	ld	hl,INTTBL
  1664  1102FA        		ld	de,0fa02h
  1667  0E21          		ld	c,HEIGHT-0fa00h+1	;b=0
                      	
  1669  EDB0          		ldir
  166B  7A            		ld	a,d
  166C  ED47          		ld	i,a
  166E  ED5E          		im	2
                      	
                      	;clear work area
                      	
  1670                	COLDLP1:
  1670  AF            		xor	a
  1671  12            		ld	(de),a
  1672  13            		inc	de
  1673  7A            		ld	a,d
  1674  B3            		or	e
  1675  20F9          		jr	nz,COLDLP1
                      	
                      	;stack pointer
  1677  3100E7        		ld	sp,0e700h	;temporary
                      	
                      	;relay,VRAM,timer
  167A  3C            		inc	a		;a=1
  167B  322DFA        		ld	(CONSOL4),a
                      	;misc
  167E  3E0E          		ld	a,0eh
  1680  3259FA        		ld	(CMMCNST),a	;used by EXAS BASIC compiler
                      	
                      	;hook
  1683  218AFF        		ld	hl,HOOK
  1686  3EC9          		ld	a,0c9h
  1688  1E03          		ld	e,03h		;d=0
  168A  061E          		ld	b,1eh
  168C                	COLDLP2:
  168C  77            		ld	(hl),a
  168D  19            		add	hl,de
  168E  10FC          		djnz	COLDLP2
                      	
                      	;for PC-6601 VOICEROM
  1690  3266FE        		ld	(HOOKVOI),a
                      	
                      	;buffer
  1693  21B9FB        		ld	hl,KEYBUF
  1696  1E40          		ld	e,40h		;d=0
  1698  2293FB        		ld	(KYBFAD),hl
  169B  19            		add	hl,de		;hl=RSBUF
  169C  2299FB        		ld	(RSBFAD),hl
  169F  19            		add	hl,de		;hl=BUFA
  16A0  22A5FB        		ld	(BUFAAD),hl
  16A3  19            		add	hl,de		;hl=BUFB
  16A4  22ABFB        		ld	(BUFAAD+6),hl
  16A7  19            		add	hl,de		;hl=BUFC
  16A8  22B1FB        		ld	(BUFAAD+12),hl
                      	
  16AB  2192FB        		ld	hl,KYBFSZ
  16AE  1E06          		ld	e,06h		;d=0
  16B0  43            		ld	b,e		;e=6
  16B1                	COLDLP3:
  16B1  363F          		ld	(hl),3fh	;KYBFSZ,RSBFSZ,,BUFASZ,BUFBSZ,BUFCSZ
  16B3  19            		add	hl,de
  16B4  10FB          		djnz	COLDLP3
                      	
                      	;command/function jump table
  16B6  2161FA        		ld	hl,CMDTBL
  16B9  0668          		ld	b,0+(FILES-CMDTBL)/2
  16BB                	COLDLP4:
  16BB  36C2          		ld	(hl),SNERR-SNERR/100h*100h
  16BD  23            		inc	hl
  16BE  3603          		ld	(hl),SNERR/100h
  16C0  23            		inc	hl
  16C1  10F8          		djnz	COLDLP4
                      	
                      	;command jump table
  16C3  219A01        		ld	hl,CMDLST
  16C6  1161FA        		ld	de,CMDTBL
  16C9  0E56          		ld	c,2*(CMDLAST-80h+1)	;b=0
  16CB  EDB0          		ldir
                      	
                      	;function jump table
                      	;	ld	hl,FNCLST
  16CD  11E5FA        		ld	de,FNCTBL
  16D0  0E3C          		ld	c,2*(FNCLAST-FNC1ST+1)	;b=0
  16D2  EDB0          		ldir
                      	
                      	;screen
  16D4  1191FD        		ld	de,VRAM		;top of active page data
  16D7  3E05          		ld	a,05h
  16D9  32C7FE        		ld	(FKEYLEN),a
  16DC                	COLDLP5:
  16DC  213D01        		ld	hl,PAGEDATA
  16DF  0E28          		ld	c,PAGE1-VRAM	;b=0
  16E1  EDB0          		ldir
  16E3  3D            		dec	a
  16E4  20F6          		jr	nz,COLDLP5
                      	
                      	;	ld	de,PAGE4+(PAGE4-PAGE3)
  16E6  EB            		ex	de,hl
  16E7  11D8FF        		ld	de,0-(PAGE4-PAGE3)
  16EA  3EA0          		ld	a,0a0h
  16EC                	COLDLP6:
  16EC  19            		add	hl,de
  16ED  77            		ld	(hl),a		;(PAGE4)=a0h, (PAGE3)=c0h, (PAGE2)=e0h
  16EE  C620          		add	a,20h
  16F0  20FA          		jr	nz,COLDLP6
                      	
                      	;display
                      	;	xor	a
  16F2  CD0C14        		call	CHGACT
  16F5  CDED13        		call	CHGDSP
  16F8  CDFB1D        		call	CLS
                      	
                      	;RND()
                      	;	call	SETZERO
  16FB  CDB53B        		call	RNDMNS
                      	
                      	;PLAY
  16FE  CDB31B        		call	PLSTOP		;ei
                      	
                      	;for mode select
  1701  3EFF          		ld	a,0ffh
  1703  324EFF        		ld	(PROGAD),a	;invalid value, not set yet
                      	
                      	;RAM access subroutine
  1706  212260        		ld	hl,READRAM_SRC
  1709  118DFE        		ld	de,READRAM
  170C  011F00        		ld	bc,RAMEND-READRAM_SRC
  170F  EDB0          		ldir
                      	
                      	;RAM size and stack work
  1711  0608          		ld	b,08h
  1713  CDE825        		call	WAITLP
  1716  CDBC0F        		call	GETCH
  1719  2100C4        		ld	hl,0c400h
  171C  0602          		ld	b,02h
  171E  280E          		jr	z,COLD16K
  1720  3D            		dec	a
                      	;	or	02h
  1721  B0            		or	b
  1722  D633          		sub	'4'-1
  1724  2008          		jr	nz,COLD16K
  1726  3C            		inc	a
  1727  3265FE        		ld	(MODE),a	;a=1 (mode=2)
  172A  2684          		ld	h,84h
  172C  0604          		ld	b,04h
  172E                	COLD16K:
  172E  2229FF        		ld	(BASICAD),hl
  1731  2C            		inc	l
  1732  225FFA        		ld	(STARTAD),hl
  1735  2100F9        		ld	hl,0f900h
  1738  225BFA        		ld	(STACK),hl
  173B  78            		ld	a,b
  173C  328CFD        		ld	(PAGES),a
                      	
  173F  3E71          		ld	a,71h		;0000-3fff:BASIC ROM, 4000-7fff:external ROM
  1741  CD9826        		call	SETF0h
                      	
  1744  C33B00        		jp	HOT
                      	
                      	
                      	;analyze an argument
                      	;input: hl=program address
                      	;output: hl=next address, FAC1=numerical value or pointer to string descriptor
                      	;	 (ARGTYP)=0(numeric), 1(string), other(cannot analyze)
                      	;destroy: FAC2,af,bc,de,hl
  1747                	ARG:
  1747  E5            		push	hl
  1748  CD420C        		call	SETZERO		;for first +-
  174B  E1            		pop	hl
  174C  AF            		xor	a
  174D  5F            		ld	e,a
  174E  D5            		push	de		;e=dummy operator
  174F  3D            		dec	a
  1750  3225FF        		ld	(ARGTYP),a	;ff=unknown
  1753  3265FF        		ld	(OPRTR),a
  1756                	ARGLP:
  1756  E5            		push	hl
  1757  CDCC34        		call	CHKFRE
  175A  E1            		pop	hl
  175B                	ARGLP2:
  175B  CD593F        		call	SKIPSP
  175E  47            		ld	b,a		;
  175F  23            		inc	hl
  1760  224EFF        		ld	(PROGAD),hl
                      	
                      	;command, function, operator
  1763  B7            		or	a
  1764  FA3B18        		jp	m,ARGCMD
                      	
                      	;previous argument without operator?
  1767  3A65FF        		ld	a,(OPRTR)
  176A  B7            		or	a
  176B  281F          		jr	z,ARGTAIL
                      	;[A-Z]
  176D  78            		ld	a,b		;
  176E  D641          		sub	'A'
  1770  FE1A          		cp	'Z'-'A'+1
  1772  3847          		jr	c,ARGVAR
                      	;[0-9&.]
  1774  D6EF          		sub	'0'-'A'
  1776  FE0A          		cp	'9'-'0'+1
  1778  78            		ld	a,b		;
  1779  382F          		jr	c,ARGNUM
  177B  FE26          		cp	'&'
  177D  282B          		jr	z,ARGNUM
  177F  FE2E          		cp	'.'
  1781  2827          		jr	z,ARGNUM
                      	;others
  1783  FE22          		cp	22h		;double quotation mark
  1785  CA0D18        		jp	z,ARGSTR
  1788  FE28          		cp	'('
  178A  2808          		jr	z,PARL
  178C                	ARGTAIL:
  178C  2B            		dec	hl
  178D  224EFF        		ld	(PROGAD),hl
  1790  AF            		xor	a
  1791  C3FA18        		jp	OPR
                      	
  1794                	PARL:
  1794  CD603F        		call	CHKCLN
  1797  CAF803        		jp	z,MOERR
  179A  CD4717        		call	ARG
  179D  3A25FF        		ld	a,(ARGTYP)
  17A0  FE02          		cp	02h
  17A2  D2C203        		jp	nc,SNERR
  17A5  CDC40B        		call	CHKRPAR
  17A8  18AC          		jr	ARGLP
                      	
  17AA                	ARGNUM:
  17AA  CDB317        		call	ARGNCHK
  17AD  2B            		dec	hl
  17AE  CD9B39        		call	ATOF
  17B1  18A8          		jr	ARGLP2
                      	
  17B3                	ARGNCHK:
  17B3  AF            		xor	a
  17B4  3265FF        		ld	(OPRTR),a
  17B7  3225FF        		ld	(ARGTYP),a
  17BA  C9            		ret
                      	
  17BB                	ARGVAR:
  17BB  E5            		push	hl		;program address
  17BC  CD9D33        		call	GETNAME
  17BF  3A25FF        		ld	a,(ARGTYP)
  17C2  F5            		push	af		;;
  17C3  3A65FF        		ld	a,(OPRTR)
  17C6  F5            		push	af		;;;
  17C7  EB            		ex	de,hl
  17C8  CD2539        		call	PUSHF1
  17CB  EB            		ex	de,hl
                      	
  17CC  C5            		push	bc		;variable name
  17CD  7E            		ld	a,(hl)
  17CE  FE28          		cp	'('
  17D0  280D          		jr	z,ARGVARR
  17D2  224EFF        		ld	(PROGAD),hl
  17D5  CDC133        		call	SRCHVAR
  17D8  3008          		jr	nc,ARGVNC
  17DA  11AB0E        		ld	de,ZERO
  17DD  1803          		jr	ARGVNC
  17DF                	ARGVARR:
  17DF  CDBF32        		call	GETARR
  17E2                	ARGVNC:
  17E2  C1            		pop	bc		;variable name
  17E3  CD3439        		call	POPF1
  17E6  F1            		pop	af		;;;
  17E7  3225FF        		ld	(ARGTYP),a
  17EA  F1            		pop	af		;;
  17EB  3265FF        		ld	(OPRTR),a
  17EE  E1            		pop	hl		;program address
  17EF  CB79          		bit	7,c
  17F1  2011          		jr	nz,ARGVSTR
  17F3                	ARGVNUM:
  17F3  CDB317        		call	ARGNCHK
  17F6  EB            		ex	de,hl
  17F7  1166FF        		ld	de,FAC1
  17FA  EDA0          		ldi
  17FC                	ARGVLDIR:
  17FC  010400        		ld	bc,0004h
  17FF  EDB0          		ldir
  1801  C34819        		jp	ARGNEXT2
                      	
                      	
                      	;string variable
  1804                	ARGVSTR:
  1804  CD2B18        		call	ARGSCHK
  1807  EB            		ex	de,hl
  1808  112DFF        		ld	de,STRDSC1
  180B  18EF          		jr	ARGVLDIR
                      	
  180D                	ARGSTR:
  180D  CD2B18        		call	ARGSCHK
  1810  222FFF        		ld	(STRDSC1+2),hl
  1813  1E00          		ld	e,00h
  1815                	ARGSLP:
  1815  7E            		ld	a,(hl)
  1816  B7            		or	a
  1817  280B          		jr	z,ARGSOK
  1819  23            		inc	hl
  181A  FE22          		cp	22h		;double quotation mark
  181C  2806          		jr	z,ARGSOK
  181E  1C            		inc	e
  181F  CAE903        		jp	z,LSERR
  1822  18F1          		jr	ARGSLP
  1824                	ARGSOK:
  1824  7B            		ld	a,e
  1825  322DFF        		ld	(STRDSC1),a
  1828  C35B17        		jp	ARGLP2
                      	
                      	;destroy: af,bc
  182B                	ARGSCHK:
  182B  AF            		xor	a
  182C  3265FF        		ld	(OPRTR),a	;a=0
  182F  3C            		inc	a
  1830  3225FF        		ld	(ARGTYP),a	;a=1
  1833  012DFF        		ld	bc,STRDSC1
  1836  ED4367FF      		ld	(FAC1+1),bc
  183A  C9            		ret
                      	
  183B                	ARGCMD:
  183B  3A65FE        		ld	a,(MODE)
  183E  FE02          		cp	02h
  1840  78            		ld	a,b		;
  1841  3005          		jr	nc,ARGCMDEX
                      	
  1843  FEF2          		cp	FNCLAST+1	;0f2h
  1845  D2C203        		jp	nc,SNERR
  1848                	ARGCMDEX:
  1848  FEFA          		cp	FLASTEX+1	;0fah
  184A  D2C203        		jp	nc,SNERR
  184D  D6CA          		sub	PLUS_		;0cah
  184F  FE0A          		cp	FNC1ST-PLUS_
  1851  78            		ld	a,b		;
  1852  DAFA18        		jp	c,OPR		;0cah-0d3h
                      	
  1855  3A65FF        		ld	a,(OPRTR)
  1858  B7            		or	a
  1859  CA8C17        		jp	z,ARGTAIL
                      	
  185C  78            		ld	a,b		;
  185D  FEC8          		cp	NOT_		;0c8h
  185F  CAFA18        		jp	z,OPR
  1862  FE9F          		cp	SCREEN_		;9fh
  1864  280D          		jr	z,FNC
  1866  FEC4          		cp	FN_		;0c4h
  1868  2809          		jr	z,FNC
  186A  FEC6          		cp	INKEY_		;0c6h
  186C  2805          		jr	z,FNC
  186E  FECA          		cp	PLUS_		;0cah
  1870  DA8C17        		jp	c,ARGTAIL
                      	
                      	;	jr	FNC
                      	
                      	;function
  1873                	FNC:
  1873  AF            		xor	a
  1874  3225FF        		ld	(ARGTYP),a
  1877  78            		ld	a,b		;
  1878  FEC6          		cp	INKEY_
  187A  CA6927        		jp	z,F_INKY
  187D  FEC4          		cp	FN_
  187F  CAC73E        		jp	z,F_FN
  1882  FE9F          		cp	SCREEN_
  1884  CAAA3E        		jp	z,F_SCRN
                      	
  1887  CDCD18        		call	CALLFNC
  188A                	FNCRTN:
  188A  2A4EFF        		ld	hl,(PROGAD)
  188D                	CLRSTRD:
  188D  3A25FF        		ld	a,(ARGTYP)
  1890  B7            		or	a
  1891  2003          		jr	nz,CLRSTRDNZ
  1893  322DFF        		ld	(STRDSC1),a	;=0
  1896                	CLRSTRDNZ:
  1896  AF            		xor	a
  1897  3265FF        		ld	(OPRTR),a
  189A  C35617        		jp	ARGLP
                      	
                      	
                      	;set border attribute
                      	;input: a=color code
                      	;destroy: af
  189D                	_SETBO:	ds	SETBO-_SETBO
  18C3                		org	SETBO
                      	
  18C3  32C6FE        		ld	(BORDERC),a
  18C6  CDD815        		call	CNVATT
  18C9  32C5FE        		ld	(BORDERA),a
  18CC  C9            		ret
                      	
                      	
  18CD                	CALLFNC:
  18CD  FEEF          		cp	STICK_
  18CF  2804          		jr	z,STKSTR
  18D1  FEF0          		cp	STRIG_
  18D3  2001          		jr	nz,NOTSTKSTR
  18D5                	STKSTR:
  18D5  2B            		dec	hl		;for STICK(),STRIG()
                      	
  18D6                	NOTSTKSTR:
  18D6  FEEA          		cp	LEFT_
  18D8  300B          		jr	nc,SKIPARG
                      	
                      	;SGN()...CHR$()
  18DA  F5            		push	af
  18DB  CDB50B        		call	CHKLPAR
  18DE  CD4717        		call	ARG
  18E1  CDC40B        		call	CHKRPAR
  18E4  F1            		pop	af
                      	
  18E5                	SKIPARG:
  18E5  113DFA        		ld	de,FNCTBL-(FNC1ST-80h)*2
  18E8  FED7          		cp	USR_
  18EA  C2493F        		jp	nz,JPTBL
                      	
                      	;USR() function
  18ED  E5            		push	hl		;program address
  18EE  CD493F        		call	JPTBL
  18F1  CD9B0B        		call	CHKNUM
  18F4  AF            		xor	a
  18F5  322DFF        		ld	(STRDSC1),a
  18F8  F1            		pop	af		;cancel program address
  18F9  C9            		ret
                      	
                      	
                      	;operator
  18FA                	OPR:
  18FA  FED3          		cp	LT_		;0d3h
  18FC  2001          		jr	nz,OPRNZ
  18FE  3C            		inc	a		;0d3h -> 0d4h
  18FF                	OPRNZ:
  18FF  57            		ld	d,a		;operator
  1900  3A65FF        		ld	a,(OPRTR)
  1903  3C            		inc	a
  1904  FE02          		cp	02h
  1906  D29A19        		jp	nc,OPROPR	;not 00 nor ff
  1909  3A25FF        		ld	a,(ARGTYP)
  190C  3C            		inc	a
  190D  7A            		ld	a,d		;operator
  190E  2010          		jr	nz,NOTHEAD
                      	;first +, for numeric and string
  1910  FECA          		cp	PLUS_
  1912  2834          		jr	z,ARGNEXT2
  1914  FECB          		cp	MINUS_
  1916  2808          		jr	z,NOTHEAD
  1918  FEC8          		cp	NOT_
  191A  2804          		jr	z,NOTHEAD
  191C  B7            		or	a
  191D  C2C203        		jp	nz,SNERR
  1920                	NOTHEAD:
  1920  CDCC19        		call	GETPRIO
  1923  5F            		ld	e,a
  1924  C1            		pop	bc		;c=previous operator
  1925  C5            		push	bc
  1926  79            		ld	a,c
  1927  B7            		or	a
  1928  2868          		jr	z,ARGHEAD
                      	
  192A  CDCC19        		call	GETPRIO
  192D  BB            		cp	e
  192E  301E          		jr	nc,CALLOP
  1930  7A            		ld	a,d		;operator
  1931                	ARGNEXT:
  1931  3265FF        		ld	(OPRTR),a
  1934  3A25FF        		ld	a,(ARGTYP)
  1937  3D            		dec	a
  1938  2805          		jr	z,ARGNEXTS
  193A  CD2539        		call	PUSHF1
  193D  1809          		jr	ARGNEXT2
  193F                	ARGNEXTS:
  193F  CDE819        		call	COPYSTR
  1942  2A65FF        		ld	hl,(FAC1-1)	;l=operator
  1945  CBB5          		res	6,l
  1947  E5            		push	hl
  1948                	ARGNEXT2:
  1948  2A4EFF        		ld	hl,(PROGAD)
  194B  C35617        		jp	ARGLP
                      	
                      	
  194E                	CALLOP:
  194E  3A25FF        		ld	a,(ARGTYP)
  1951  3D            		dec	a
  1952  2827          		jr	z,CALLOPS
                      	
                      	;numeric
  1954  CD5239        		call	CPYFAC
  1957  CD3439        		call	POPF1
                      	
  195A                	CALLOPEND:
  195A  CB71          		bit	6,c		;c=previous operator
  195C  2007          		jr	nz,TYPNUM
  195E  CBF1          		set	6,c
  1960  2125FF        		ld	hl,ARGTYP
  1963  CBCE          		set	1,(hl)
  1965                	TYPNUM:
  1965  D5            		push	de		;;d=following operator
  1966  79            		ld	a,c
  1967  FED2          		cp	GT_+1
  1969  3802          		jr	c,CALLOPOK
                      	;>,=,<
  196B  3ED1          		ld	a,GT_
                      	
  196D                	CALLOPOK:
  196D  11F600        		ld	de,OPRTBL-(0c8h-80h)*2
  1970  CD493F        		call	JPTBL
  1973  AF            		xor	a
  1974  3265FF        		ld	(OPRTR),a
  1977  F1            		pop	af		;;a=following operator
  1978  C3FA18        		jp	OPR
                      	
                      	;string
  197B                	CALLOPS:
  197B  C5            		push	bc		;c=previous operator
  197C  3A2DFF        		ld	a,(STRDSC1)
  197F  2A2FFF        		ld	hl,(STRDSC1+2)
  1982  CDFC19        		call	BACKSTR
  1985  3239FF        		ld	(STRDSC4),a
  1988  223BFF        		ld	(STRDSC4+2),hl
  198B  C1            		pop	bc
  198C  E1            		pop	hl
  198D  2265FF        		ld	(FAC1-1),hl
  1990  18C8          		jr	CALLOPEND
                      	
  1992                	ARGHEAD:
  1992  82            		add	a,d		;d=0?
  1993  209C          		jr	nz,ARGNEXT
  1995  E1            		pop	hl		;cancel dummy operator
  1996  2A4EFF        		ld	hl,(PROGAD)
  1999  C9            		ret
                      	
                      	;operator and operator
  199A                	OPROPR:
  199A  7A            		ld	a,d		;operator
  199B  FEC8          		cp	NOT_		;0c8h
  199D  2892          		jr	z,ARGNEXT
  199F  FECA          		cp	PLUS_		;0cah
  19A1  28A5          		jr	z,ARGNEXT2
  19A3  FECB          		cp	MINUS_		;0cbh
  19A5  281D          		jr	z,OPRMNS
  19A7  FED1          		cp	GT_		;0d1h
  19A9  DAC203        		jp	c,SNERR
                      	
                      	;>,=,<
                      	;bit0: >
                      	;bit1: =
                      	;bit2: <
  19AC  2165FF        		ld	hl,OPRTR
  19AF  7E            		ld	a,(hl)
  19B0  FED1          		cp	GT_
  19B2  DAC203        		jp	c,SNERR
  19B5  B2            		or	d
  19B6  BE            		cp	(hl)
  19B7  CAC203        		jp	z,SNERR
  19BA  77            		ld	(hl),a
                      	
                      	;copy numeric/string bit
  19BB  E607          		and	07h		;<=> bits
  19BD  C1            		pop	bc		;c=previous operator
  19BE  B1            		or	c
  19BF  4F            		ld	c,a
  19C0  C5            		push	bc
  19C1  C34819        		jp	ARGNEXT2
                      	
                      	;-
  19C4                	OPRMNS:
  19C4  CD530C        		call	SETMNS1
  19C7  3ECC          		ld	a,ASTRSK_	;0cch
  19C9  C33119        		jp	ARGNEXT
                      	
                      	
                      	;input: a=operator
                      	;output: a=priority
                      	;destroy: f,hl
  19CC                	GETPRIO:
  19CC  B7            		or	a
  19CD  C8            		ret	z
  19CE  21E719        		ld	hl,PRIO+GT_-NOT_
  19D1  F640          		or	40h		;numeric/string bit
  19D3  D6D1          		sub	GT_		;0d1h
  19D5  3005          		jr	nc,PRIONC	;>,=,<
  19D7  85            		add	a,l
  19D8  6F            		ld	l,a
  19D9  3801          		jr	c,PRIONC
  19DB  25            		dec	h
  19DC                	PRIONC:
  19DC  7E            		ld	a,(hl)
  19DD  C9            		ret
                      	
  19DE                	PRIO:
                      	;operator priority table
                      	;		not   +  -  *  /  ^ and or >=<
  19DE  03000505060607		db	3, 0, 5, 5, 6, 6, 7, 2, 1, 4
                      	
                      	
                      	;check string descriptor and copy
                      	;destroy: af,bc,de,hl
  19E8                	COPYSTR:
  19E8  2139FF        		ld	hl,STRDSC4
  19EB  7E            		ld	a,(hl)
  19EC  B7            		or	a
  19ED  C2EC03        		jp	nz,STERR
  19F0  2B            		dec	hl		;STRDSC3+3
  19F1  113CFF        		ld	de,STRDSC4+3
  19F4  010C00        		ld	bc,000ch
  19F7  EDB8          		lddr			;STRDSC3->STRDSC4, STRDSC2->STRDSC3, STRDSC1->STRDSC2
                      	;	xor	a
  19F9  23            		inc	hl
  19FA  77            		ld	(hl),a		;(STRDSC1)=0
  19FB  C9            		ret
                      	
                      	
                      	;copy back string descriptor
                      	;destroy: f,bc
  19FC                	BACKSTR:
  19FC  E5            		push	hl
  19FD  D5            		push	de
  19FE  2131FF        		ld	hl,STRDSC2
  1A01  112DFF        		ld	de,STRDSC1
  1A04  010C00        		ld	bc,000ch
  1A07  EDB0          		ldir			;STRDSC2->STRDSC1, STRDSC3->STRDSC2, STRDSC4->STRDSC3
  1A09  EB            		ex	de,hl
  1A0A  70            		ld	(hl),b		;(STRDSC4)=0
  1A0B  D1            		pop	de
  1A0C  E1            		pop	hl
  1A0D  C9            		ret
                      	
                      	
                      	;send graphic data to PC-6021
                      	;input: c
                      	;destroy: af
  1A0E                	SENDGRP:
  1A0E  3E80          		ld	a,80h
  1A10                	SENDGLP:
  1A10  CB01          		rlc	c
  1A12  1F            		rra
  1A13  30FB          		jr	nc,SENDGLP
  1A15  1820          		jr	PRINTER
                      	
                      	
                      	;put a character to printer
                      	;input: a
                      	;destroy: none
  1A17                	_PUTPRT:ds	PUTPRT-_PUTPRT
  1A1C                		org	PUTPRT
                      	
  1A1C  F5            		push	af
  1A1D  3A31FA        		ld	a,(GRPFLG)
  1A20  B7            		or	a
  1A21  2806          		jr	z,NOTGRP
  1A23  AF            		xor	a
  1A24                	SETGRP:
  1A24  3231FA        		ld	(GRPFLG),a
  1A27  F1            		pop	af
  1A28  C9            		ret
                      	
  1A29                	NOTGRP:
  1A29  F1            		pop	af
  1A2A  F5            		push	af
  1A2B  FE14          		cp	14h
  1A2D  28F5          		jr	z,SETGRP
  1A2F  CD4F1A        		call	CNVKANA
  1A32  CD371A        		call	PRINTER
  1A35  F1            		pop	af
  1A36  C9            		ret
                      	
                      	
                      	;put data to printer
                      	;input: a
                      	;destroy: af
  1A37                	PRINTER:
  1A37  F5            		push	af
  1A38                	PRINTERLP:
  1A38  DBC0          		in	a,(0c0h)
  1A3A  E602          		and	02h
  1A3C  2005          		jr	nz,PRINTERZ	;ready
  1A3E  CD4D27        		call	STOPESC
  1A41  18F5          		jr	PRINTERLP
                      	
  1A43                	PRINTERZ:
  1A43  F1            		pop	af
  1A44  2F            		cpl
  1A45  D391          		out	(91h),a
  1A47  3E01          		ld	a,01h
  1A49  D393          		out	(93h),a		;strobe (>1us)
  1A4B  AF            		xor	a
  1A4C  D393          		out	(93h),a
  1A4E  C9            		ret
                      	
                      	
                      	;convert hiragana -> katakana
                      	;input: a
                      	;output: a
                      	;destroy: f
  1A4F                	_CNVKANA:ds	CNVKANA-_CNVKANA
  1A4F                		org	CNVKANA
                      	
  1A4F  FE86          		cp	86h
  1A51  D8            		ret	c
  1A52  FEA0          		cp	0a0h
  1A54  3803          		jr	c,HIRAGANA
  1A56  FEE0          		cp	0e0h
  1A58  D8            		ret	c
  1A59                	HIRAGANA:
  1A59  EE20          		xor	20h
  1A5B  C9            		ret
                      	
                      	
                      	;CMT open for verify
                      	;destroy: a
  1A5C                	VRFOPN:
  1A5C  3EFF          		ld	a,0ffh
  1A5E  32D8FE        		ld	(VERIFY),a
                      	;	jr	ROPEN
                      	
                      	
                      	;open for tape read
                      	;destroy: none
  1A61                	_ROPEN:ds	ROPEN-_ROPEN
  1A61                		org	ROPEN
                      	
  1A61  F5            		push	af
  1A62  C5            		push	bc
  1A63  061E          		ld	b,1eh
  1A65  CD121B        		call	OPENCMT
  1A68  CD4B1B        		call	RLON
  1A6B  C1            		pop	bc
  1A6C  F1            		pop	af
  1A6D  C9            		ret
                      	
                      	
                      	;get 1 character from CMT
                      	;output: a=data, z(0=error, 1=no error)
                      	;destroy: f
  1A6E                	_GETCMT:ds	GETCMT-_GETCMT
  1A70                		org	GETCMT
                      	
  1A70  3A18FA        		ld	a,(STOPFLG)
  1A73  FE03          		cp	03h
  1A75  2812          		jr	z,CMTSTP
  1A77  3A19FA        		ld	a,(CMTSTAT)
  1A7A  CB67          		bit	4,a
  1A7C  C0            		ret	nz		;read error, z-flag=0
  1A7D  E602          		and	02h
  1A7F  28EF          		jr	z,GETCMT
  1A81  AF            		xor	a		;set z-flag
  1A82  3219FA        		ld	(CMTSTAT),a
  1A85  3A1DFA        		ld	a,(CMTBUF)
  1A88  C9            		ret
                      	
  1A89                	CMTSTP:
  1A89  CD991A        		call	CHKVRF
  1A8C  C35127        		jp	STOPSP
                      	
                      	
                      	;call GETCMT, if z=0 then ?TR Error
  1A8F                	GETCMTTR:
  1A8F  CD701A        		call	GETCMT
  1A92  C8            		ret	z
  1A93  CD991A        		call	CHKVRF
  1A96  C3F503        		jp	TRERR
                      	
                      	
                      	;check verify or not
  1A99                	CHKVRF:
  1A99  CDAA1A        		call	RCLOSE
  1A9C                	CHKVRF2:
  1A9C  3AD8FE        		ld	a,(VERIFY)
  1A9F  B7            		or	a
  1AA0  CCBE34        		call	z,NEW
  1AA3  C9            		ret
                      	
                      	
                      	;close for tape read
                      	;destroy: none
  1AA4                	_RCLOSE:ds	RCLOSE-_RCLOSE
  1AAA                		org	RCLOSE
                      	
  1AAA  F5            		push	af
  1AAB  C5            		push	bc
  1AAC  3E1A          		ld	a,1ah
  1AAE                	CLOSEEND:
  1AAE  CD8F0E        		call	OUT90H
  1AB1  CD491B        		call	RLOFF
  1AB4  C1            		pop	bc
  1AB5  F1            		pop	af
  1AB6  C9            		ret
                      	
                      	
                      	;open for tape write
                      	;destroy: none
  1AB7                	_WOPEN:	ds	WOPEN-_WOPEN
  1AB8                		org	WOPEN
                      	
  1AB8  F5            		push	af
  1AB9  C5            		push	bc
  1ABA  CD4B1B        		call	RLON
  1ABD  CDD91A        		call	BLANK
  1AC0  063E          		ld	b,3eh
  1AC2  CD121B        		call	OPENCMT
  1AC5  CDD91A        		call	BLANK
  1AC8  C1            		pop	bc
  1AC9  F1            		pop	af
  1ACA  C9            		ret
                      	
                      	
                      	;put 1 character to CMT
                      	;input: a=data
                      	;destroy: none
  1ACB                	_PUTCMT:ds	PUTCMT-_PUTCMT
  1ACC                		org	PUTCMT
                      	
  1ACC  F5            		push	af
  1ACD                	PUTCMT2:
  1ACD  CDDC1A        		call	CHKWSTP
  1AD0  3E38          		ld	a,38h
  1AD2  CD8F0E        		call	OUT90H
  1AD5  F1            		pop	af
  1AD6  C38F0E        		jp	OUT90H
                      	
                      	
                      	;wait and check stop for tape write
                      	;destroy: af,bc
  1AD9                	BLANK:
  1AD9  CDE525        		call	WAIT
                      	;	jr	CHKWSTP
                      	
                      	;check stop for tape write
                      	;destroy: af (when not stop)
  1ADC                	CHKWSTP:
  1ADC  3A18FA        		ld	a,(STOPFLG)
  1ADF  FE03          		cp	03h
  1AE1  C0            		ret	nz
  1AE2  CD491B        		call	RLOFF
  1AE5  C35127        		jp	STOPSP
                      	
                      	
                      	;check 8255 status (ready for output to port 90h)
                      	;output: z(0=ready)
                      	;destroy: af,(b)
  1AE8                	_CHK90H:ds	CHK90H-_CHK90H
  1AED                		org	CHK90H
                      	
  1AED  3E08          		ld	a,08h
  1AEF  D393          		out	(93h),a		;disable 8255 INT for reading
  1AF1  DB92          		in	a,(92h)
  1AF3  E688          		and	88h		;nobf=1,intr=1?
  1AF5  EAF91A        		jp	pe,CHK90PE
  1AF8  BF            		cp	a		;set z-flag
  1AF9                	CHK90PE:
  1AF9  3E09          		ld	a,09h
  1AFB  D393          		out	(93h),a		;enable 8255 INT for reading
  1AFD  C9            		ret
                      	
                      	
                      	;close for tape write
                      	;destroy: none
  1AFE                	_WCLOSE:ds	WCLOSE-_WCLOSE
  1B06                		org	WCLOSE
                      	
  1B06  F5            		push	af
  1B07  C5            		push	bc
  1B08  01B003        		ld	bc,03b0h
  1B0B  CDE825        		call	WAITLP
  1B0E  3E3A          		ld	a,3ah
  1B10  189C          		jr	CLOSEEND
                      	
                      	
                      	;open CMT subroutine
                      	;input: b (1eh=read, 3eh=write)
                      	;destroy: af,b
  1B12                	OPENCMT:
  1B12  3A1FFA        		ld	a,(BAUD)	;00=600baud, others=1200baud
  1B15  FE01          		cp	01h
  1B17  9F            		sbc	a,a		;ff=600baud, 00=1200baud
  1B18  80            		add	a,b
  1B19  CD8F0E        		call	OUT90H		;read=1dh(600baud), 1eh(1200baud)
                      					;write=3dh(600baud), 3eh(1200baud)
  1B1C  E6F8          		and	0f8h
  1B1E  3C            		inc	a		;read=19h, write=39h
                      	;	di
  1B1F  CD8F0E        		call	OUT90H
                      	;	ei
  1B22  AF            		xor	a
  1B23  3219FA        		ld	(CMTSTAT),a
                      	;	ld	(STOPFLG),a
  1B26  C9            		ret
                      	
                      	
                      	;blink asterisk
                      	;destroy: af
  1B27                	_BLNKAST:ds	BLNKAST-_BLNKAST
  1B2A                		org	BLNKAST
                      	
  1B2A  E5            		push	hl
  1B2B  211EFA        		ld	hl,ASTSTAT
  1B2E  7E            		ld	a,(hl)
  1B2F  EE0A          		xor	'*'-' '
  1B31  77            		ld	(hl),a
                      	
  1B32  CD9539        		call	CHKMOD
  1B35  7E            		ld	a,(hl)
                      	
                      	;	ld	hl,(WIDTH-1)	;h=(WIDTH)
                      	;	dec	h
                      	;	ld	l,01h
                      	
                      	;for compatibility with N6X-BASIC
  1B36  21011F        		ld	hl,01h+(COLUMNS-1)*100h
  1B39  2002          		jr	nz,BLNKASTNZ
  1B3B  2627          		ld	h,CLMN66-1
  1B3D                	BLNKASTNZ:
                      	
  1B3D  CD5712        		call	PRTCHXY
  1B40  E1            		pop	hl
  1B41  C9            		ret
                      	
                      	
                      	;CMT relay on/off
                      	;destroy: af,b
  1B42                	_RLOFF:	ds	RLOFF-_RLOFF
  1B49                		org	RLOFF
                      	
  1B49  1801          		jr	RLOFF2
                      	
  1B4B                	_RLON:	ds	RLON-_RLON
  1B4B                		org	RLON
                      	
  1B4B  3E            		db	3eh		;ld a,
  1B4C                	RLOFF2:
  1B4C  AF            		xor	a		;afh
                      	
  1B4D  0608          		ld	b,00001000b	;bit3=CMT relay
  1B4F  C3541B        		jp	OUTB0H
                      	
                      	
                      	;output to port b0h
                      	;input: a=new data, b=change bit
                      	;destroy: af
  1B52                	_OUTB0H:ds	OUTB0H-_OUTB0H
  1B54                		org	OUTB0H
                      	
  1B54  E5            		push	hl
  1B55  2127FA        		ld	hl,PORTB0H
  1B58  AE            		xor	(hl)
  1B59  A0            		and	b
  1B5A  AE            		xor	(hl)
  1B5B  D3B0          		out	(0b0h),a
  1B5D  77            		ld	(hl),a
  1B5E  E1            		pop	hl
  1B5F  C9            		ret
                      	
                      	
                      	;PLAY stop sub
                      	;destroy: none
  1B60                	_PLSTPS:ds	PLSTPS-_PLSTPS
  1B60                		org	PLSTPS
                      	
  1B60  F5            		push	af
  1B61  E5            		push	hl
  1B62  D5            		push	de
  1B63  C5            		push	bc
                      	
  1B64  3E07          		ld	a,07h		;ch.ABC=tone,portAB=in
  1B66  1E38          		ld	e,38h
  1B68  CDC51B        		call	SETPSG
  1B6B  3C            		inc	a		;register8=ch.A volume
  1B6C  1E00          		ld	e,00h
  1B6E  CDC51B        		call	SETPSG
  1B71  3C            		inc	a		;register9=ch.B volume
  1B72  CDC51B        		call	SETPSG
  1B75  3C            		inc	a		;register10=ch.C volume
  1B76  CDC51B        		call	SETPSG
                      	
  1B79  AF            		xor	a
  1B7A  321BFD        		ld	(PLAYST),a
  1B7D  3214FD        		ld	(CHANNEL),a
                      	
  1B80  67            		ld	h,a
  1B81  6F            		ld	l,a
  1B82  22A1FB        		ld	(BUFAIN),hl	;clear BFIN and BFOUT for ch.A
  1B85  22A7FB        		ld	(BUFBIN),hl	;clear BFIN and BFOUT for ch.B
  1B88  22ADFB        		ld	(BUFCIN),hl	;clear BFIN and BFOUT for ch.C
                      	
  1B8B  211DFD        		ld	hl,PLWKA
  1B8E  77            		ld	(hl),a
  1B8F  54            		ld	d,h
  1B90  5D            		ld	e,l
  1B91  13            		inc	de
  1B92  012400        		ld	bc,PLWKB-PLWKA-1
  1B95  EDB0          		ldir
                      	
  1B97  21B91B        		ld	hl,PLWKTBL
  1B9A  112CFD        		ld	de,PLWKA+OCTAVE
  1B9D  0E05          		ld	c,05h		;b=0
  1B9F  EDB0          		ldir
                      	
  1BA1  211DFD        		ld	hl,PLWKA
  1BA4  1142FD        		ld	de,PLWKB
  1BA7  0E4A          		ld	c,PLWKC-PLWKA	;b=0
  1BA9  EDB0          		ldir
                      	
  1BAB                	POPALL:
  1BAB  C1            		pop	bc
  1BAC  D1            		pop	de
  1BAD  E1            		pop	hl
  1BAE  F1            		pop	af
  1BAF  C9            		ret
                      	
                      	
                      	;stop and initialize for PLAY command
                      	;destroy: none
  1BB0                	_PLSTOP:ds	PLSTOP-_PLSTOP
  1BB3                		org	PLSTOP
  1BB3  F3            		di
  1BB4  CD601B        		call	PLSTPS
  1BB7  FB            		ei
  1BB8  C9            		ret
                      	
                      	
  1BB9                	PLWKTBL:
  1BB9  04            		db	4		;O-value
  1BBA  04            		db	4		;L-value
  1BBB  78            		db	120		;T-value
  1BBC  08            		db	8		;V-value
  1BBD  FF            		db	255		;M-value(low)
                      	
                      	
                      	;get PSG resister value and set new value
                      	;input: a=register, e=value
                      	;output: d=old value
                      	;destroy: none
  1BBE                	_SETPSG2:ds	SETPSG2-_SETPSG2
  1BBE                		org	SETPSG2
                      	
  1BBE  F5            		push	af
  1BBF  D3A0          		out	(0a0h),a
  1BC1  DBA2          		in	a,(0a2h)
  1BC3  57            		ld	d,a
  1BC4  F1            		pop	af
                      	;	jp	SETPSG
                      	
                      	
                      	;set PSG register
                      	;input: a=register, e=value
                      	;destroy: none
  1BC5                	_SETPSG:ds	SETPSG-_SETPSG
  1BC5                		org	SETPSG
                      	
  1BC5  F5            		push	af
  1BC6  D3A0          		out	(0a0h),a
  1BC8  7B            		ld	a,e
  1BC9  D3A1          		out	(0a1h),a
  1BCB  F1            		pop	af
  1BCC  C9            		ret
                      	
                      	
                      	;bell
                      	;destroy: af,bc,e
  1BCD                	_BELL:	ds	BELL-_BELL
  1BCD                		org	BELL
                      	
  1BCD  CDB31B        		call	PLSTOP
                      	;tune
  1BD0  AF            		xor	a		;register0=ch.A 8bit fine tune
  1BD1  1E55          		ld	e,55h
  1BD3  CDC51B        		call	SETPSG
  1BD6  5F            		ld	e,a		;a=0
  1BD7  3C            		inc	a		;register1=ch.A 4bit coarse tune
  1BD8  CDC51B        		call	SETPSG
                      	;volume
  1BDB  1E07          		ld	e,07h
                      	;	ld	a,08h		;register8=ch.A volume
  1BDD  83            		add	a,e		;register8=ch.A volume
  1BDE  CDC51B        		call	SETPSG
  1BE1  010004        		ld	bc,0400h
  1BE4  59            		ld	e,c		;c=0
  1BE5  CDE825        		call	WAITLP
                      	;volume
  1BE8  3E08          		ld	a,08h		;register8=ch.A volume
  1BEA  18D9          		jr	SETPSG
                      	
                      	
                      	;play subroutine called by time interrupt
                      	;destroy: af,bc,de,hl
  1BEC                	PLAYINT:
  1BEC  3A1BFD        		ld	a,(PLAYST)
  1BEF  B7            		or	a
  1BF0  C8            		ret	z
  1BF1                	PLIZ:
  1BF1  0F            		rrca
  1BF2  0F            		rrca
  1BF3  0F            		rrca
  1BF4  4F            		ld	c,a
  1BF5  2167FD        		ld	hl,PLWKC
  1BF8  0603          		ld	b,03h
  1BFA                	PLILP:
  1BFA  E5            		push	hl
  1BFB  CB01          		rlc	c
  1BFD  300D          		jr	nc,PLINC
                      	
  1BFF  56            		ld	d,(hl)
  1C00  2C            		inc	l		;inc	hl
  1C01  5E            		ld	e,(hl)
  1C02  1B            		dec	de
  1C03  73            		ld	(hl),e
  1C04  2D            		dec	l		;dec	hl
  1C05  72            		ld	(hl),d
  1C06  7A            		ld	a,d
  1C07  B3            		or	e
  1C08  205D          		jr	nz,PLINEXT
  1C0A  1806          		jr	PLIGET
                      	
  1C0C                	PLINC:
  1C0C  3A1BFD        		ld	a,(PLAYST)
  1C0F  B7            		or	a
  1C10  2055          		jr	nz,PLINEXT	;if (PLAYST)<>0
                      	
  1C12                	PLIGET:
                      	;length
  1C12  CD4810        		call	GETPLBF
                      	;	jr	z,PLINEXT
  1C15  2864          		jr	z,PLIRES
  1C17  FEFF          		cp	0ffh
  1C19  2860          		jr	z,PLIRES
  1C1B  D1            		pop	de
  1C1C  D5            		push	de
  1C1D  12            		ld	(de),a
  1C1E  CD4810        		call	GETPLBF		;no change in de
  1C21  1C            		inc	e		;inc de
  1C22  12            		ld	(de),a
                      	
                      	;tune
  1C23  CD4810        		call	GETPLBF
  1C26  5F            		ld	e,a
  1C27  78            		ld	a,b
  1C28  87            		add	a,a
  1C29  3D            		dec	a		;register1,3,5=4bit coarse tune
  1C2A  CDC51B        		call	SETPSG
  1C2D  CD4810        		call	GETPLBF
  1C30  5F            		ld	e,a
  1C31  78            		ld	a,b
  1C32  3D            		dec	a
  1C33  87            		add	a,a		;register0,2,4=8bit fine tune
  1C34  CDC51B        		call	SETPSG
                      	
                      	;volume or envelope
  1C37  CD4810        		call	GETPLBF
  1C3A  57            		ld	d,a		;
  1C3B  FE10          		cp	10h
  1C3D  3802          		jr	c,PLIVOL
  1C3F  3E10          		ld	a,10h		;envelope
  1C41                	PLIVOL:
  1C41  5F            		ld	e,a
  1C42  78            		ld	a,b
  1C43  C607          		add	a,07h		;register8,9,10=volume
  1C45  CDC51B        		call	SETPSG
  1C48  7A            		ld	a,d		;
  1C49  D610          		sub	10h
  1C4B  3818          		jr	c,PLISET
                      	
                      	;period
  1C4D  57            		ld	d,a		;
  1C4E  CD4810        		call	GETPLBF
  1C51  5F            		ld	e,a
  1C52  3E0B          		ld	a,0bh
  1C54  CDC51B        		call	SETPSG
  1C57  CD4810        		call	GETPLBF
  1C5A  5F            		ld	e,a
  1C5B  3E0C          		ld	a,0ch
  1C5D  CDC51B        		call	SETPSG
                      	
                      	;envelope pattern
  1C60  5A            		ld	e,d		;
  1C61  3C            		inc	a		;register13=envelope pattern
  1C62  CDC51B        		call	SETPSG
                      	
                      	;status
  1C65                	PLISET:
  1C65  CBC1          		set	0,c
                      	
  1C67                	PLINEXT:
  1C67  E1            		pop	hl
  1C68  7D            		ld	a,l
  1C69  C6DB          		add	a,PLWKA-PLWKB	;no carry
  1C6B  6F            		ld	l,a
  1C6C  108C          		djnz	PLILP
                      	
  1C6E  211BFD        		ld	hl,PLAYST
  1C71  7E            		ld	a,(hl)
  1C72  71            		ld	(hl),c
  1C73  B7            		or	a
  1C74  C8            		ret	z		;old (PLAYST)=0
  1C75  79            		ld	a,c
  1C76  B7            		or	a
  1C77  C0            		ret	nz		;new (PLAYST)<>0
  1C78  C3F11B        		jp	PLIZ		;old<>0 and new=0
                      	
                      	;reset
  1C7B                	PLIRES:
  1C7B  CB81          		res	0,c
  1C7D  78            		ld	a,b
  1C7E  C607          		add	a,07h		;register8,9,10=volume
  1C80  1E00          		ld	e,00h
  1C82  CDC51B        		call	SETPSG
  1C85  18E0          		jr	PLINEXT
                      	
                      	
                      	;get play buffer address
                      	;output: hl
                      	;destroy: f
  1C87                	GETPLAD:
  1C87  67            		ld	h,a
  1C88  3A14FD        		ld	a,(CHANNEL)
  1C8B  6F            		ld	l,a
  1C8C  87            		add	a,a		;a*2
  1C8D  85            		add	a,l		;a*3
  1C8E  87            		add	a,a		;a*6
  1C8F  C6A1          		add	a,BUFAIN-BUFAIN/256*256		;no carry
  1C91  6F            		ld	l,a
  1C92  7C            		ld	a,h
  1C93  26FB          		ld	h,BUFAIN/256	;higher byte
  1C95  C9            		ret
                      	
                      	
                      	;joystick
                      	;input: a=1or2
                      	;output: a (0-0-trigger2-trigger1-right-left-down-up)
                      	;destroy: f
  1C96                	_JOYSTK:ds	JOYSTK-_JOYSTK
  1CA6                		org	JOYSTK
                      	
  1CA6  D5            		push	de
  1CA7  0F            		rrca
  1CA8  37            		scf
  1CA9  1F            		rra
  1CAA  5F            		ld	e,a		;c0h or 80h
                      	
  1CAB  3E07          		ld	a,07h		;register7=portABin-out,noise,tone
  1CAD  F3            		di
  1CAE  D3A0          		out	(0a0h),a
  1CB0  DBA2          		in	a,(0a2h)
  1CB2  1802          		jr	SKPPATCH2
                      	
                      	
                      	;1cb4h-1cb5h: to be patched by PC6001V/VX/VW
  1CB4                	_PATCH2:ds	PATCH2-_PATCH2
  1CB4                		org	PATCH2
  1CB4  0000          		db	00h, 00h
                      	
                      	
  1CB6                	SKPPATCH2:
  1CB6  57            		ld	d,a		;
                      	
  1CB7  E6BF          		and	10111111b	;parallel port A=in
  1CB9  F680          		or	10000000b	;parallel port B=out
  1CBB  D3A1          		out	(0a1h),a
                      	
  1CBD  3E0F          		ld	a,0fh		;register15=parallel port B
  1CBF  CDC51B        		call	SETPSG		;c0h or 80h
                      	
  1CC2  3D            		dec	a		;register14=parallel port A
  1CC3  D3A0          		out	(0a0h),a
  1CC5  DBA2          		in	a,(0a2h)
  1CC7  2F            		cpl
  1CC8  5A            		ld	e,d		;
  1CC9  57            		ld	d,a		;;
                      	
  1CCA  3E07          		ld	a,07h
  1CCC  CD861E        		call	SETPSGEI
                      	
  1CCF  7A            		ld	a,d		;;
  1CD0  D1            		pop	de
  1CD1  C9            		ret
                      	
                      	
                      	;LOCATE command
  1CD2                	_C_LOCA:ds	C_LOCA-_C_LOCA
  1CD2                		org	C_LOCA
                      	
  1CD2  CD2D0E        		call	INT1ARG2
  1CD5  61            		ld	h,c
  1CD6  3AACFD        		ld	a,(WIDTH)
  1CD9  3D            		dec	a
  1CDA  BC            		cp	h
  1CDB  DACB03        		jp	c,FCERR
  1CDE  6B            		ld	l,e
  1CDF  3A20FA        		ld	a,(HEIGHT)
  1CE2  3D            		dec	a
  1CE3  BD            		cp	l
  1CE4  DACB03        		jp	c,FCERR
  1CE7  24            		inc	h
  1CE8  2C            		inc	l
  1CE9  C36D11        		jp	SETCSR
                      	
                      	
                      	;CONSOLE command
  1CEC                	_C_CNSL:ds	C_CNSL-_C_CNSL
  1CF6                		org	C_CNSL
                      	
  1CF6  ED4BA2FD      		ld	bc,(CONSOL1)	;c=(CONSOL1),b=(CONSOL2)
  1CFA  C5            		push	bc
                      	
  1CFB  CD6B3F        		call	CHKCMM
  1CFE  2814          		jr	z,CNSLPAR2
                      	
  1D00  CDE40D        		call	INT1ARG
  1D03  3A20FA        		ld	a,(HEIGHT)
  1D06  3D            		dec	a
  1D07  BB            		cp	e
  1D08  3801          		jr	c,CNSLC
  1D0A  7B            		ld	a,e
  1D0B                	CNSLC:
  1D0B  3C            		inc	a
  1D0C  C1            		pop	bc
  1D0D  4F            		ld	c,a
  1D0E  C5            		push	bc
                      	
  1D0F  CD7B3F        		call	CHKCLCM
  1D12  2838          		jr	z,CNSLEND
                      	
  1D14                	CNSLPAR2:
  1D14  CD6B3F        		call	CHKCMM
  1D17  2810          		jr	z,CNSLPAR3
                      	
  1D19  CDE40D        		call	INT1ARG
                      	
  1D1C  C1            		pop	bc
  1D1D  81            		add	a,c
  1D1E  DACB03        		jp	c,FCERR
  1D21  3D            		dec	a
  1D22  47            		ld	b,a
  1D23  C5            		push	bc
                      	
  1D24  CD7B3F        		call	CHKCLCM
  1D27  2823          		jr	z,CNSLEND
                      	
  1D29                	CNSLPAR3:
  1D29  CD6B3F        		call	CHKCMM
  1D2C  2813          		jr	z,CNSLPAR4
                      	
  1D2E  CDE40D        		call	INT1ARG
  1D31  FE02          		cp	02h
  1D33  D2CB03        		jp	nc,FCERR
  1D36  32A6FD        		ld	(CONSOL3),a
                      	
  1D39  CDB912        		call	PRTFKEY2
  1D3C  CD7B3F        		call	CHKCLCM
  1D3F  280B          		jr	z,CNSLEND
                      	
  1D41                	CNSLPAR4:
  1D41  CDE40D        		call	INT1ARG
  1D44  FE02          		cp	02h
  1D46  D2CB03        		jp	nc,FCERR
  1D49  322DFA        		ld	(CONSOL4),a
                      	
  1D4C                	CNSLEND:
  1D4C  D1            		pop	de
  1D4D  7A            		ld	a,d
  1D4E  BB            		cp	e
  1D4F  DACB03        		jp	c,FCERR
                      	;	jr	CNSMAIN
                      	
                      	
                      	;CONSOLE command main part
                      	;used by EXAS BASIC compiler
                      	;destroy: af,de,hl
  1D52                	_CNSMAIN:ds	CNSMAIN-_CNSMAIN
  1D52                		org	CNSMAIN
                      	
  1D52  CD731D        		call	SETCNSL
                      	
  1D55  3AA2FD        		ld	a,(CONSOL1)
  1D58  CD4F14        		call	CUTLINE2
                      	
  1D5B  3AA8FD        		ld	a,(CSRY)
  1D5E  3D            		dec	a
  1D5F  21A5FD        		ld	hl,LASTLIN
  1D62  BE            		cp	(hl)
  1D63  D8            		ret	c
  1D64  6E            		ld	l,(hl)
  1D65  C34C11        		jp	CTLCR
                      	
                      	
                      	;set console parameter
                      	;destroy: af,de,hl
  1D68                	_SETCNSL2:ds	SETCNSL-4-_SETCNSL2
  1D6F                		org	SETCNSL-4
  1D6F                	SETCNSL2:
  1D6F  ED5BA2FD      		ld	de,(CONSOL1)
                      	
                      	;used by MINTMARK
                      	;set console parameter
                      	;input: d=last line+1, e=first line+1
                      	;destroy: af,de,hl
  1D73                	_SETCNSL:ds	SETCNSL-_SETCNSL
  1D73                		org	SETCNSL
                      	;2nd parameter
  1D73  3AA6FD        		ld	a,(CONSOL3)
  1D76  67            		ld	h,a
  1D77  3A20FA        		ld	a,(HEIGHT)
  1D7A  BA            		cp	d
  1D7B  3001          		jr	nc,CNSLNC1
  1D7D  57            		ld	d,a
  1D7E                	CNSLNC1:
                      	
                      	;1st parameter
  1D7E  94            		sub	h
  1D7F  BB            		cp	e
  1D80  3001          		jr	nc,CNSLNC2
  1D82  5F            		ld	e,a
  1D83                	CNSLNC2:
  1D83  6B            		ld	l,e
                      	
                      	;last line
  1D84  67            		ld	h,a		;h=(HEIGHT)-(CONSOL3)
  1D85  BA            		cp	d
  1D86  3F            		ccf
  1D87  3A92FD        		ld	a,(SCREEN1)
  1D8A  1F            		rra
  1D8B  B7            		or	a
  1D8C  2801          		jr	z,CNSLZ
  1D8E  62            		ld	h,d		;if screen mode 3,4 or 2nd<height-3rd
  1D8F                	CNSLZ:
                      	
  1D8F  ED53A2FD      		ld	(CONSOL1),de
  1D93  22A4FD        		ld	(FSTLIN),hl
  1D96  C9            		ret
                      	
                      	
                      	;COLOR command
  1D97                	_C_COLR:ds	C_COLR-_C_COLR
  1D9B                		org	C_COLR
                      	
  1D9B  1193FD        		ld	de,COLOR1
  1D9E  0602          		ld	b,02h
  1DA0                	COLRLP:
  1DA0  CD6B3F        		call	CHKCMM
  1DA3  2810          		jr	z,COLRZ
  1DA5  C5            		push	bc
  1DA6  D5            		push	de
  1DA7  CDE40D        		call	INT1ARG
  1DAA  D1            		pop	de
  1DAB  C1            		pop	bc
  1DAC  12            		ld	(de),a
  1DAD  CD603F        		call	CHKCLN		;a=(hl)
  1DB0  C8            		ret	z
  1DB1  FE2C          		cp	','
  1DB3  C0            		ret	nz
  1DB4  23            		inc	hl
  1DB5                	COLRZ:
  1DB5  13            		inc	de
  1DB6  10E8          		djnz	COLRLP
                      	
  1DB8                	COLRPAR3:
  1DB8  CDE40D        		call	INT1ARG
                      	;	jp	SETC3
                      	
                      	
                      	;set color 3rd parameter
                      	;input: a=3rd color parameter (1 or 2)
                      	;destroy: af,bc,de
  1DBB                	_SETC3:	ds	SETC3-_SETC3
  1DBB                		org	SETC3
                      	
  1DBB  EB            		ex	de,hl
  1DBC  3D            		dec	a
  1DBD  FE02          		cp	02h
  1DBF  D2CB03        		jp	nc,FCERR
  1DC2  87            		add	a,a		;0 or 2
                      	
  1DC3  2195FD        		ld	hl,COLOR3
  1DC6  BE            		cp	(hl)
  1DC7  C8            		ret	z
  1DC8  77            		ld	(hl),a
                      	
  1DC9  2A90FD        		ld	hl,(VRAM-1)
  1DCC  010200        		ld	bc,0002h	;32*16=256*2
  1DCF  68            		ld	l,b		;b=0
  1DD0                	SETC3LP:
  1DD0  7E            		ld	a,(hl)
  1DD1  EE02          		xor	02h
  1DD3  77            		ld	(hl),a
  1DD4  23            		inc	hl
  1DD5  10F9          		djnz	SETC3LP
  1DD7  0D            		dec	c
  1DD8  20F6          		jr	nz,SETC3LP
  1DDA  EB            		ex	de,hl
  1DDB  C9            		ret
                      	
                      	
                      	;clear screen and reset graphic cordinate
                      	;input: c-flag=1: c=first line+1, a=last line-first line+1
                      	;       c-flag=0: using console parameters
                      	;destroy: af,bc,de,hl
  1DDC                	CLSMAIN:
  1DDC  210000        		ld	hl,0000h
  1DDF  22AEFD        		ld	(GRPX1),hl
  1DE2  22B0FD        		ld	(GRPY1),hl
  1DE5  2C            		inc	l
  1DE6  CD4C11        		call	CTLCR		;(0,0)
  1DE9  DA7E28        		jp	c,CLSMAIN2	;if a,c given
  1DEC  C37428        		jp	CTLL
                      	
                      	
                      	;CLS command
                      	;destroy: af,de
  1DEF                	_CLS:	ds	CLS-_CLS
  1DFB                	C_CLS:
  1DFB                		org	CLS
                      	
  1DFB  E5            		push	hl
  1DFC  C5            		push	bc
  1DFD  B7            		or	a		;reset c-flag
  1DFE  CDDC1D        		call	CLSMAIN
  1E01  C1            		pop	bc
  1E02  E1            		pop	hl
  1E03  C9            		ret
                      	
                      	
                      	;SCREEN command
  1E04                	_C_SCRN:ds	C_SCRN-_C_SCRN
  1E04                		org	C_SCRN
                      	
  1E04  AF            		xor	a		;1st parameter=none
  1E05  F5            		push	af
  1E06  ED4B8FFD      		ld	bc,(SCREEN2)
  1E0A  51            		ld	d,c		;2nd parameter-1
  1E0B  58            		ld	e,b		;3rd parameter-1
                      	
  1E0C  CD6B3F        		call	CHKCMM
  1E0F  280C          		jr	z,SCRPAR2
                      	
  1E11  D5            		push	de
  1E12  CDE40D        		call	INT1ARG
  1E15  D1            		pop	de
  1E16  C1            		pop	bc
  1E17  F5            		push	af		;1st parameter
                      	
  1E18  CD7B3F        		call	CHKCLCM
  1E1B  2818          		jr	z,GOSCRMAIN
                      	
  1E1D                	SCRPAR2:
  1E1D  CD6B3F        		call	CHKCMM
  1E20  280C          		jr	z,SCRPAR3
                      	
  1E22  D5            		push	de
  1E23  CDE40D        		call	INT1ARG
  1E26  D1            		pop	de
  1E27  3D            		dec	a
  1E28  57            		ld	d,a		;2nd parameter-1
                      	
  1E29  CD7B3F        		call	CHKCLCM
  1E2C  2807          		jr	z,GOSCRMAIN
                      	
  1E2E                	SCRPAR3:
  1E2E  D5            		push	de
  1E2F  CDE40D        		call	INT1ARG
  1E32  D1            		pop	de
  1E33  3D            		dec	a
  1E34  5F            		ld	e,a		;3rd parameter-1
                      	
  1E35                	GOSCRMAIN:
  1E35  F1            		pop	af
  1E36  3D            		dec	a
  1E37  4F            		ld	c,a		;1st parameter-1
                      	;	jr	SCRMAIN
                      	
                      	
                      	;SCREEN command main part
                      	;used by EXAS BASIC compiler
                      	;input: c=1st paraeter-1(ff=none), d=2nd parameter-1, e=3rd parameter-1
                      	;destroy: af,de
  1E38                	_SCRMAIN:ds	SCRMAIN-_SCRMAIN
  1E39                		org	SCRMAIN
                      	
  1E39  3A8CFD        		ld	a,(PAGES)
  1E3C  3D            		dec	a
  1E3D  BA            		cp	d
  1E3E  DACB03        		jp	c,FCERR
  1E41  BB            		cp	e
  1E42  DACB03        		jp	c,FCERR
                      	
  1E45  79            		ld	a,c
  1E46  3C            		inc	a
  1E47  FE03          		cp	03h
  1E49  380A          		jr	c,SCROK
  1E4B  FE05          		cp	05h
  1E4D  D2CB03        		jp	nc,FCERR
  1E50  7A            		ld	a,d
  1E51  B7            		or	a
  1E52  CACB03        		jp	z,FCERR
                      	
  1E55                	SCROK:
  1E55  7A            		ld	a,d
  1E56  CD0C14        		call	CHGACT
                      	
  1E59  3A90FD        		ld	a,(SCREEN3)
  1E5C  BB            		cp	e
  1E5D  7B            		ld	a,e
  1E5E  C4ED13        		call	nz,CHGDSP
                      	
  1E61  3A92FD        		ld	a,(SCREEN1)
  1E64  B9            		cp	c
  1E65  C8            		ret	z
  1E66  79            		ld	a,c
  1E67  0C            		inc	c
  1E68  C8            		ret	z		;1st parameter=none
  1E69  C39013        		jp	CHGMOD
                      	
                      	
                      	;TIME function
  1E6C                	F_TIME:
  1E6C  2A28FA        		ld	hl,(TMCNT)
  1E6F  ED5B2AFA      		ld	de,(TMCNT+2)
  1E73  C30F25        		jp	I4TOF
                      	
                      	
                      	;SOUND command
  1E76                	C_SOUN:
  1E76  CD603F        		call	CHKCLN
  1E79  CAC203        		jp	z,SNERR
  1E7C  CD2D0E        		call	INT1ARG2
  1E7F  79            		ld	a,c
  1E80  FE10          		cp	10h
  1E82  D2CB03        		jp	nc,FCERR
  1E85  F3            		di
  1E86                	SETPSGEI:
  1E86  CDC51B        		call	SETPSG
  1E89  FB            		ei
  1E8A  C9            		ret
                      	
                      	
                      	;PLAY command
  1E8B                	C_PLAY:
  1E8B  CDB31E        		call	PLAY
  1E8E  224EFF        		ld	(PROGAD),hl
  1E91  C9            		ret
                      	
                      	
                      	;play routine
                      	;input: hl=data address, (z=0)
                      	;output: hl=next address
                      	;destroy: af,bc,de
  1E92                	_PLAY:	ds	PLAY-_PLAY
  1EB3                		org	PLAY
                      	
  1EB3  DDE5          		push	ix
  1EB5  ED5B4EFF      		ld	de,(PROGAD)
  1EB9  D5            		push	de
                      	
  1EBA  AF            		xor	a
  1EBB  3244FD        		ld	(PLWKB+PLLEN),a
  1EBE  3269FD        		ld	(PLWKC+PLLEN),a
                      	
  1EC1  DD211DFD      		ld	ix,PLWKA
  1EC5  0603          		ld	b,03h
  1EC7                	PLAYLP1:
  1EC7  C5            		push	bc
  1EC8  CD8126        		call	STRARG
  1ECB  DD7702        		ld	(ix+PLLEN),a
  1ECE  DD7303        		ld	(ix+PLADR),e
  1ED1  DD7204        		ld	(ix+PLADR+1),d
  1ED4  012500        		ld	bc,PLWKB-PLWKA
  1ED7  DD09          		add	ix,bc
  1ED9  C1            		pop	bc
  1EDA  CD7B3F        		call	CHKCLCM
  1EDD  2802          		jr	z,PLSTR0
  1EDF  10E6          		djnz	PLAYLP1
                      	
                      	;check string length=0?
  1EE1                	PLSTR0:
  1EE1  2169FD        		ld	hl,PLWKC+PLLEN
  1EE4  11DBFF        		ld	de,PLWKB-PLWKC
  1EE7  0603          		ld	b,03h
  1EE9                	PLAYLP2:
  1EE9  7E            		ld	a,(hl)
  1EEA  B7            		or	a
  1EEB  200E          		jr	nz,PLAYNZ1
  1EED  78            		ld	a,b
  1EEE  3D            		dec	a
  1EEF  3214FD        		ld	(CHANNEL),a
  1EF2  3EFF          		ld	a,0ffh
  1EF4  E5            		push	hl
  1EF5  F3            		di
  1EF6  CD310F        		call	PUTPLBF
  1EF9  FB            		ei
  1EFA  E1            		pop	hl
  1EFB                	PLAYNZ1:
  1EFB  19            		add	hl,de
  1EFC  10EB          		djnz	PLAYLP2
                      	
  1EFE                	PLAYLP3:
  1EFE  3A18FA        		ld	a,(STOPFLG)
  1F01  FE03          		cp	03h
  1F03  CCB31B        		call	z,PLSTOP
  1F06  285C          		jr	z,PLAYEND
                      	
  1F08  F3            		di
  1F09  DD2167FD      		ld	ix,PLWKC
  1F0D  010003        		ld	bc,0300h	;c=PLAYLEN check counter
  1F10                	PLAYLP4:
  1F10  78            		ld	a,b
  1F11  3D            		dec	a
  1F12  3214FD        		ld	(CHANNEL),a
  1F15  DD7E02        		ld	a,(ix+PLLEN)
  1F18  B7            		or	a
  1F19  2829          		jr	z,PLAYNEXT
                      	
  1F1B  DD6E03        		ld	l,(ix+PLADR)
  1F1E  DD6604        		ld	h,(ix+PLADR+1)
                      	
  1F21  3217FD        		ld	(PLAYLEN),a
  1F24  2218FD        		ld	(PLAYSTR),hl
  1F27  C5            		push	bc
  1F28  CD6F1F        		call	CNVPLAY
  1F2B  C1            		pop	bc
  1F2C  3A17FD        		ld	a,(PLAYLEN)
  1F2F  DD7702        		ld	(ix+PLLEN),a
  1F32  2A18FD        		ld	hl,(PLAYSTR)
  1F35  DD7503        		ld	(ix+PLADR),l
  1F38  DD7404        		ld	(ix+PLADR+1),h
                      	
  1F3B  0C            		inc	c
  1F3C  B7            		or	a
  1F3D  2005          		jr	nz,PLAYNEXT
  1F3F  0D            		dec	c
  1F40  3D            		dec	a
  1F41  CD310F        		call	PUTPLBF		;a=ffh
  1F44                	PLAYNEXT:
  1F44  11DBFF        		ld	de,PLWKB-PLWKC
  1F47  DD19          		add	ix,de
  1F49  10C5          		djnz	PLAYLP4
                      	
  1F4B  211BFD        		ld	hl,PLAYST
  1F4E  7E            		ld	a,(hl)
  1F4F  B7            		or	a
  1F50  200D          		jr	nz,PLAYNZ2
                      	
  1F52  3C            		inc	a		;=01h
  1F53  321EFD        		ld	(PLWKA+REMAIN+1),a
  1F56  3243FD        		ld	(PLWKB+REMAIN+1),a
  1F59  3268FD        		ld	(PLWKC+REMAIN+1),a
  1F5C  3E07          		ld	a,07h
  1F5E  77            		ld	(hl),a
                      	
  1F5F                	PLAYNZ2:
  1F5F  FB            		ei
  1F60  79            		ld	a,c		;c=PLAYLEN check counter
  1F61  B7            		or	a
  1F62  209A          		jr	nz,PLAYLP3
                      	
  1F64                	PLAYEND:
  1F64  D1            		pop	de
  1F65  2A4EFF        		ld	hl,(PROGAD)
  1F68  ED534EFF      		ld	(PROGAD),de
  1F6C  DDE1          		pop	ix
  1F6E  C9            		ret
                      	
                      	
                      	;convert play data
                      	;input: a=string length, hl=string address, ix=play work
                      	;destroy: af,bc,de,hl
  1F6F                	CNVPLAY:
  1F6F  CD871C        		call	GETPLAD
  1F72  56            		ld	d,(hl)
  1F73  14            		inc	d		;in+1
  1F74  23            		inc	hl
  1F75  7E            		ld	a,(hl)		;out
  1F76  92            		sub	d
  1F77  3004          		jr	nc,CNVPLNC
                      	
  1F79  23            		inc	hl
  1F7A  23            		inc	hl		;size
  1F7B  3C            		inc	a
  1F7C  86            		add	a,(hl)
  1F7D                	CNVPLNC:
  1F7D  FE08          		cp	08h
  1F7F  D8            		ret	c
                      	
  1F80  CD1D20        		call	PLAYGETC
  1F83  C8            		ret	z
  1F84  CD6A20        		call	PLAYINC
  1F87  CDEF0B        		call	TOUPPER
  1F8A  CD8F1F        		call	PLAYCMD
  1F8D  18E0          		jr	CNVPLAY
                      	
                      	
  1F8F                	PLAYCMD:
  1F8F  D641          		sub	'A'
  1F91  FE07          		cp	'G'-'A'+1
  1F93  DAC620        		jp	c,TONE
  1F96  FE0C          		cp	'M'-'A'
  1F98  282F          		jr	z,PLAYM
  1F9A  FE12          		cp	'S'-'A'
  1F9C  283F          		jr	z,PLAYS
  1F9E  FE0B          		cp	'L'-'A'
  1FA0  2847          		jr	z,PLAYL
  1FA2  FE13          		cp	'T'-'A'
  1FA4  2855          		jr	z,PLAYT
  1FA6  FE0E          		cp	'O'-'A'
  1FA8  2861          		jr	z,PLAYO
  1FAA  FE11          		cp	'R'-'A'
  1FAC  CA9520        		jp	z,PLAYR
  1FAF  FE0D          		cp	'N'-'A'
  1FB1  CA7620        		jp	z,PLAYN
  1FB4  FE15          		cp	'V'-'A'
  1FB6  C2CB03        		jp	nz,FCERR
                      	
                      	
  1FB9                	PLAYV:
  1FB9  CD5821        		call	GETNUM1
  1FBC  3802          		jr	c,PLAYVNUM
  1FBE  3E08          		ld	a,08h
  1FC0                	PLAYVNUM:
  1FC0  FE10          		cp	10h
  1FC2  D2CB03        		jp	nc,FCERR
  1FC5                	PUTV:
  1FC5  DD7712        		ld	(ix+VOLUME),a
  1FC8  C9            		ret
                      	
  1FC9                	PLAYM:
  1FC9  CD6321        		call	GETNUM2
  1FCC  3803          		jr	c,PLAYMNUM
  1FCE  11FF00        		ld	de,00ffh
  1FD1                	PLAYMNUM:
  1FD1  7A            		ld	a,d
  1FD2  B3            		or	e
  1FD3  CACB03        		jp	z,FCERR
  1FD6  DD7313        		ld	(ix+PERIOD),e
  1FD9  DD7214        		ld	(ix+PERIOD+1),d
  1FDC  C9            		ret
                      	
  1FDD                	PLAYS:
  1FDD  CD5821        		call	GETNUM1
  1FE0  FE10          		cp	10h
  1FE2  D2CB03        		jp	nc,FCERR
  1FE5  C610          		add	a,10h
  1FE7  18DC          		jr	PUTV
                      	
  1FE9                	PLAYL:
  1FE9  CD5821        		call	GETNUM1
  1FEC  3802          		jr	c,PLAYLNUM
  1FEE  3E04          		ld	a,04h
  1FF0                	PLAYLNUM:
  1FF0  3D            		dec	a
  1FF1  FE40          		cp	40h
  1FF3  D2CB03        		jp	nc,FCERR
  1FF6  3C            		inc	a
  1FF7  DD7710        		ld	(ix+LENGTH),a
  1FFA  C9            		ret
                      	
  1FFB                	PLAYT:
  1FFB  CD5821        		call	GETNUM1
  1FFE  3802          		jr	c,PLAYTNUM
  2000  3E78          		ld	a,120
  2002                	PLAYTNUM:
  2002  FE20          		cp	32
  2004  DACB03        		jp	c,FCERR
  2007  DD7711        		ld	(ix+TEMPO),a
  200A  C9            		ret
                      	
  200B                	PLAYO:
  200B  CD5821        		call	GETNUM1
  200E  3802          		jr	c,PLAYONUM
  2010  3E04          		ld	a,04h
  2012                	PLAYONUM:
  2012  3D            		dec	a
  2013  FE08          		cp	08h
  2015  D2CB03        		jp	nc,FCERR
  2018  3C            		inc	a
  2019  DD770F        		ld	(ix+OCTAVE),a
  201C  C9            		ret
                      	
                      	
                      	;get a character from PLAY string
                      	;output: a=data, hl=address, z-flag(1=no character)
                      	;destroy: f,hl
  201D                	PLAYGETC:
  201D  3A17FD        		ld	a,(PLAYLEN)
  2020  B7            		or	a
  2021  C8            		ret	z		;a=0 if no character
  2022  2A18FD        		ld	hl,(PLAYSTR)
  2025  7E            		ld	a,(hl)
  2026  FE20          		cp	' '
  2028  C0            		ret	nz
  2029  CD6A20        		call	PLAYINC
  202C  18EF          		jr	PLAYGETC
                      	
                      	
                      	;scale data
                      	;address probably equals to 6001mkII/6601 ROM (used by BELUGA)
  202E                	_SCALE2:ds	SCALE2-_SCALE2
  2030                		org	SCALE2
                      	
                      	;		c/b+	c+/d-	d	d+/e-	e/f-	f/e+
  2030  E80E120E480D89		dw	0ee8h,	0e12h,	0d48h,	0c89h,	0bd5h,	0b2bh
                      	;		f+/g-	g	g+/a-	a	a+/b-	b/c-
  203C  8A0AF3096409DD		dw	0a8ah,	09f3h,	0964h,	08ddh,	085eh,	07e6h
                      	
                      	
                      	;scale data
  2048                	_SCALE:	ds	SCALE-_SCALE
  204B                		org	SCALE
                      	
                      	;		c/b+	c+/d-	d	d+/e-	e/f-	f/e+
  204B  E80E120E480D89		dw	0ee8h,	0e12h,	0d48h,	0c89h,	0bd5h,	0b2bh
                      	;		f+/g-	g	g+/a-	a	a+/b-	b/c-
  2057  8A0AF3096409DD		dw	0a8ah,	09f3h,	0964h,	08ddh,	085eh,	07e6h
                      	
                      	
  2063                	TONETBL:
                      	;		a   b   c  d  e  f   g
  2063  12160004080A0E		db	18, 22, 0, 4, 8, 10, 14
                      	
                      	
                      	;increment (PLAYSTR) and decrement (PLAYLEN)
                      	;destroy: f,hl
  206A                	PLAYINC:
  206A  2117FD        		ld	hl,PLAYLEN
  206D  35            		dec	(hl)
  206E  2118FD        		ld	hl,PLAYSTR
  2071  34            		inc	(hl)
  2072  C0            		ret	nz
  2073  2C            		inc	l		;inc hl
  2074  34            		inc	(hl)
  2075  C9            		ret
                      	
                      	
  2076                	PLAYN:
  2076  CD5821        		call	GETNUM1
  2079  D2CB03        		jp	nc,FCERR
  207C  FE61          		cp	97
  207E  D2CB03        		jp	nc,FCERR
  2081  B7            		or	a
  2082  283C          		jr	z,PLAYN0
                      	
  2084  0600          		ld	b,00h
  2086                	PLAYNLP:
  2086  04            		inc	b
  2087  D60C          		sub	0ch
  2089  30FB          		jr	nc,PLAYNLP
  208B  87            		add	a,a
  208C  16FF          		ld	d,0ffh
  208E  5F            		ld	e,a
  208F  216320        		ld	hl,SCALE+24
  2092  19            		add	hl,de
  2093  1856          		jr	GETFREQ
                      	
  2095                	PLAYR:
  2095  AF            		xor	a
  2096  F5            		push	af		;volume
  2097  D5            		push	de		;(tune)
  2098  CD5821        		call	GETNUM1
  209B  3006          		jr	nc,PLAYRNC
  209D  B7            		or	a
  209E  2065          		jr	nz,CHKLEN
  20A0  C3CB03        		jp	FCERR
                      	
  20A3                	PLAYRNC:
  20A3  3E04          		ld	a,04h
  20A5  1863          		jr	CALCLEN
                      	
                      	;accidental mark
  20A7                	PLAYPLS:
  20A7  CD6A20        		call	PLAYINC
  20AA  1C            		inc	e
  20AB  1C            		inc	e
  20AC  7B            		ld	a,e
  20AD  D618          		sub	24
  20AF  3833          		jr	c,NOACC
  20B1  5F            		ld	e,a		;=0
  20B2  1830          		jr	NOACC
                      	
  20B4                	PLAYMNS:
  20B4  CD6A20        		call	PLAYINC
  20B7  1D            		dec	e
  20B8  1D            		dec	e
  20B9  F2E420        		jp	p,NOACC
  20BC  1E16          		ld	e,22
  20BE  1824          		jr	NOACC
                      	
  20C0                	PLAYN0:
                      	;	xor	a
  20C0  F5            		push	af		;volume=0
  20C1  D5            		push	de		;(tune)
                      	
                      	;for compatibility with PC-6001
  20C2  3E50          		ld	a,80
  20C4  1844          		jr	CALCLEN
                      	
                      	
  20C6                	TONE:
  20C6  216320        		ld	hl,TONETBL
  20C9  1600          		ld	d,00h
  20CB  5F            		ld	e,a
  20CC  19            		add	hl,de
  20CD  5E            		ld	e,(hl)
  20CE  CD1D20        		call	PLAYGETC
  20D1  2811          		jr	z,NOACC
  20D3  FE2B          		cp	'+'
  20D5  28D0          		jr	z,PLAYPLS
  20D7  FE23          		cp	'#'
  20D9  28CC          		jr	z,PLAYPLS
  20DB  FE2D          		cp	'-'
  20DD  28D5          		jr	z,PLAYMNS
  20DF  FE3D          		cp	'='
  20E1  CACB03        		jp	z,FCERR
                      	
  20E4                	NOACC:
  20E4  DD460F        		ld	b,(ix+OCTAVE)
  20E7  214B20        		ld	hl,SCALE
                      	;	ld	d,00h
  20EA  19            		add	hl,de		;d=0
  20EB                	GETFREQ:
  20EB  7E            		ld	a,(hl)
  20EC  23            		inc	hl
  20ED  56            		ld	d,(hl)
  20EE  05            		dec	b
  20EF  2805          		jr	z,OCTAVE1
  20F1                	OCTVLP:
  20F1  CB3A          		srl	d
  20F3  1F            		rra
  20F4  10FB          		djnz	OCTVLP
  20F6                	OCTAVE1:
  20F6  5F            		ld	e,a
  20F7  DD7E12        		ld	a,(ix+VOLUME)
                      	
  20FA                	PUSHVOL:
  20FA  F5            		push	af		;volume
  20FB  D5            		push	de		;tune
  20FC  CD5821        		call	GETNUM1
  20FF  B7            		or	a
  2100  2003          		jr	nz,CHKLEN
  2102  DD7E10        		ld	a,(ix+LENGTH)
  2105                	CHKLEN:
  2105  FE41          		cp	65
  2107  D2CB03        		jp	nc,FCERR
                      	
  210A                	CALCLEN:
                      	;60[s/min]/T[/min]/(L/4)/(8192/3993600[Hz])=117000/T/L
  210A  DD4611        		ld	b,(ix+TEMPO)	;T-value
  210D  CDE821        		call	MULINT1		;hl=T*L (<=3fc0h)
                      	;	ld	de,0e4e2h	;117187.5/2 (6601:4MHz)
  2110  1184E4        		ld	de,0e484h	;117000/2
  2113  CD1322        		call	DIVINT2		;bc=(117000/2)/(T*L)*2
  2116  50            		ld	d,b
  2117  59            		ld	e,c
                      	
                      	;dotted note
  2118                	DOTLP:
  2118  CD1D20        		call	PLAYGETC
  211B  FE2E          		cp	'.'
  211D  200C          		jr	nz,NOTDOT
  211F  CD6A20        		call	PLAYINC
  2122  CB38          		srl	b
  2124  CB19          		rr	c
  2126  EB            		ex	de,hl
  2127  09            		add	hl,bc
  2128  EB            		ex	de,hl
  2129  18ED          		jr	DOTLP
  212B                	NOTDOT:
                      	;	di
                      	;for compatibility with PC-6001mkII/6601
  212B  210C00        		ld	hl,000ch
  212E  E7            		rst	CPHLDE
  212F  3801          		jr	c,LENOK
  2131  EB            		ex	de,hl
  2132                	LENOK:
  2132  7A            		ld	a,d		;length
  2133  CD310F        		call	PUTPLBF
  2136  7B            		ld	a,e
  2137  CD310F        		call	PUTPLBF
  213A  D1            		pop	de		;tune
  213B  7A            		ld	a,d
  213C  CD310F        		call	PUTPLBF
  213F  7B            		ld	a,e
  2140  CD310F        		call	PUTPLBF
  2143  F1            		pop	af		;volume
  2144  CD310F        		call	PUTPLBF
  2147  FE10          		cp	10h
  2149  380C          		jr	c,PLCMDEND
  214B  DD7E13        		ld	a,(ix+PERIOD)	;period
  214E  CD310F        		call	PUTPLBF
  2151  DD7E14        		ld	a,(ix+PERIOD+1)
  2154  CD310F        		call	PUTPLBF
  2157                	PLCMDEND:
                      	;	ei
  2157  C9            		ret
                      	
                      	
  2158                	GETNUM1:
                      	;output: a,c-flag(1=number)
                      	;destroy: f,bc,de,hl
  2158  CD6321        		call	GETNUM2
  215B  7B            		ld	a,e
  215C  D0            		ret	nc
  215D  14            		inc	d
  215E  15            		dec	d
  215F  C8            		ret	z		;c-flag=1
  2160  C3CB03        		jp	FCERR
                      	
                      	
  2163                	GETNUM2:
                      	;output: de,c-flag(1=number)
                      	;destroy: af,bc,de
  2163  CD1D20        		call	PLAYGETC	;hl=address if z-flag=0
  2166  110000        		ld	de,0000h
  2169  C8            		ret	z		;c-flag=0
  216A  FE3D          		cp	'='
  216C  282A          		jr	z,GETNUMEQ
  216E  FE3B          		cp	';'
  2170  281F          		jr	z,GETNUMSEMI
  2172  D630          		sub	'0'
  2174  FE0A          		cp	'9'-'0'+1
  2176  D0            		ret	nc		;c-flag=0
  2177  3A17FD        		ld	a,(PLAYLEN)
  217A  47            		ld	b,a
  217B  CDCC27        		call	ATOI2LEN
  217E  78            		ld	a,b
  217F  3217FD        		ld	(PLAYLEN),a
  2182  2218FD        		ld	(PLAYSTR),hl
  2185  CD1D20        		call	PLAYGETC
  2188  2805          		jr	z,GETNUMZ
  218A  FE3B          		cp	';'
  218C  CC6A20        		call	z,PLAYINC
  218F                	GETNUMZ:
  218F  37            		scf			;set c-flag
  2190  C9            		ret
                      	
  2191                	GETNUMSEMI:
  2191  CD6A20        		call	PLAYINC
  2194  AF            		xor	a		;reset c-flag
  2195  57            		ld	d,a
  2196  5F            		ld	e,a
  2197  C9            		ret
                      	
  2198                	GETNUMEQ:
  2198  CD6A20        		call	PLAYINC
  219B  11DAFE        		ld	de,INPBUF
  219E                	GETNUMEQLP:
  219E  CD1D20        		call	PLAYGETC
  21A1  CACB03        		jp	z,FCERR
  21A4  CD6A20        		call	PLAYINC
  21A7  CDF20B        		call	TOUPPER2
  21AA  12            		ld	(de),a
  21AB  13            		inc	de
  21AC  FE3B          		cp	';'
  21AE  20EE          		jr	nz,GETNUMEQLP
                      	
  21B0  21DAFE        		ld	hl,INPBUF
  21B3  46            		ld	b,(hl)
  21B4  78            		ld	a,b
  21B5  D641          		sub	'A'
  21B7  FE1A          		cp	'Z'-'A'+1
  21B9  D2CB03        		jp	nc,FCERR
  21BC  23            		inc	hl
  21BD  CD9D33        		call	GETNAME
  21C0  FE24          		cp	'$'
  21C2  CAE303        		jp	z,TMERR
  21C5  FE28          		cp	'('
  21C7  2811          		jr	z,GETNUMARR
                      	
  21C9  CDC133        		call	SRCHVAR
  21CC  3003          		jr	nc,GETNUMEQNC
  21CE  11AB0E        		ld	de,ZERO
  21D1                	GETNUMEQNC:
  21D1  EB            		ex	de,hl
  21D2  CD450C        		call	SETF1
  21D5  CD9F0C        		call	FTOI
  21D8  37            		scf
  21D9  C9            		ret
                      	
                      	
  21DA                	GETNUMARR:
  21DA  ED5B4EFF      		ld	de,(PROGAD)
  21DE  D5            		push	de
  21DF  CDBF32        		call	GETARR
  21E2  E1            		pop	hl
  21E3  224EFF        		ld	(PROGAD),hl
  21E6  18E9          		jr	GETNUMEQNC
                      	
                      	
                      	;hl=a*b
                      	;input: a,b
                      	;output: hl
                      	;destroy: af,c
  21E8                	MULINT1:
  21E8  210000        		ld	hl,0000h
  21EB  4D            		ld	c,l		;=0
  21EC  1801          		jr	MULI1NZ
  21EE                	MULI1C:
  21EE  09            		add	hl,bc
  21EF                	MULI1NZ:
  21EF  CB38          		srl	b
  21F1  CB19          		rr	c
  21F3  87            		add	a,a
  21F4  38F8          		jr	c,MULI1C
  21F6  20F7          		jr	nz,MULI1NZ
  21F8  C9            		ret
                      	
                      	
                      	;dehl=de*hl
                      	;input: de,hl
                      	;output: dehl
                      	;destroy: af,bc
  21F9                	MULINT2:
  21F9  7C            		ld	a,h
  21FA  4D            		ld	c,l
  21FB  210000        		ld	hl,0000h
  21FE  0610          		ld	b,10h
  2200                	MULI2LP:
  2200  1F            		rra
  2201  CB19          		rr	c
  2203  3001          		jr	nc,MULI2NC
  2205  19            		add	hl,de
  2206                	MULI2NC:
  2206  CB1C          		rr	h
  2208  CB1D          		rr	l
  220A  10F4          		djnz	MULI2LP
  220C  1F            		rra
  220D  CB19          		rr	c
  220F  59            		ld	e,c
  2210  57            		ld	d,a
  2211  EB            		ex	de,hl
  2212  C9            		ret
                      	
                      	
                      	;bc=de/hl*2
                      	;input: de,hl(<8000h)
                      	;output: bc...hl
                      	;destroy: af,de,hl
  2213                	DIVINT2:
  2213  AF            		xor	a
  2214  010002        		ld	bc,0200h	;*2^(b-1)
  2217                	DIVILP1:
  2217  04            		inc	b
  2218  ED6A          		adc	hl,hl		;c-flag=0
  221A  F21722        		jp	p,DIVILP1
  221D  EB            		ex	de,hl
  221E                	DIVILP2:
  221E  B7            		or	a
  221F  ED52          		sbc	hl,de
  2221  3001          		jr	nc,DIVNC
  2223  19            		add	hl,de
  2224                	DIVNC:
  2224  3F            		ccf
  2225  CB11          		rl	c
  2227  17            		rla
  2228  CB3A          		srl	d
  222A  CB1B          		rr	e
  222C  10F0          		djnz	DIVILP2
                      	
  222E  47            		ld	b,a
  222F  C9            		ret
                      	
                      	
                      	;STICK() function
  2230                	_F_STCK:ds	F_STCK-_F_STCK
  2236                		org	F_STCK
                      	
  2236  CD7122        		call	FNCI1
                      	
                      	;2239: used by SNAKE & ALIEN, MAD CHASER
  2239  B7            		or	a
  223A  2808          		jr	z,STCK0
  223C  CDA61C        		call	JOYSTK
  223F  218622        		ld	hl,JOYTBL
  2242  1808          		jr	CNVSTCK
                      	
  2244                	STCK0:
  2244  CD6110        		call	STICK
  2247  0F            		rrca
  2248  0F            		rrca
  2249  219622        		ld	hl,CSRTBL
  224C                	CNVSTCK:
  224C  E60F          		and	0fh
  224E  1600          		ld	d,00h
  2250  5F            		ld	e,a
  2251  19            		add	hl,de
  2252  6E            		ld	l,(hl)
  2253  C37E0C        		jp	I1TOF
                      	
                      	
                      	;STRIG() function
  2256                	_F_STRG:ds	F_STRG-_F_STRG
  2256                		org	F_STRG
                      	
  2256  CD7122        		call	FNCI1
                      	
                      	;2259: used by SNAKE & ALIEN, MAD CHASER
  2259  B7            		or	a
  225A  280E          		jr	z,STRG0
  225C  CDA61C        		call	JOYSTK
  225F  E610          		and	10h
  2261                	STRGEND:
  2261  CA420C        		jp	z,SETZERO
                      	;	jp	SETPLS1
                      	
  2264                	SETPLS1:
  2264  21A10E        		ld	hl,PLSONE
  2267  C3450C        		jp	SETF1
                      	
  226A                	STRG0:
  226A  CD6110        		call	STICK
  226D  E680          		and	80h
  226F  18F0          		jr	STRGEND
                      	
                      	
                      	;get a 1-byte integer for function for stick()/strig()
                      	;input: hl=program address-1
                      	;output: FAC1,a, hl=next address
                      	;destroy: FAC2,f,bc
  2271                	FNCI1:
  2271  23            		inc	hl
  2272  CD860B        		call	FNCNUM
  2275  CD4C07        		call	FTOI1
  2278  83            		add	a,e
  2279  FE03          		cp	03h
  227B  D8            		ret	c
  227C  C3CB03        		jp	FCERR
                      	
                      	
                      	;joystick -> stick()
  227F                	_JOYTBL:ds	JOYTBL-_JOYTBL
  2286                		org	JOYTBL
                      	;		    u   d   ud  l   ul  dl  udl
  2286  00010500070806		db	0,  1,  5,  0,  7,  8,  6,  7
                      	;		r   ur  dr  udr lr  ulr dlr udlr
  228E  03020403000105		db	3,  2,  4,  3,  0,  1,  5,  0
                      	
                      	
                      	;cursor key -> stick()
  2296                	_CSRTBL:ds	CSRTBL-_CSRTBL
  2296                		org	CSRTBL
                      	;		    u   d   ud  r   ur  dr  udr
  2296  00010500030204		db	0,  1,  5,  0,  3,  2,  4,  3
                      	;		l   ul  dl  udl rl url drl udrl
  229E  07080607000105		db	7,  8,  6,  7,  0,  1,  5,  0
                      	
                      	
                      	;LCOPY command
  22A6                	_C_LCPY:ds	C_LCPY-_C_LCPY
  22A6                		org	C_LCPY
                      	
  22A6  CDD2FF        		call	HOOKLCP
  22A9  21DF22        		ld	hl,LCPYHD
  22AC  0605          		ld	b,05h
  22AE                	LCPYLP1:
  22AE  7E            		ld	a,(hl)
  22AF  CD371A        		call	PRINTER
  22B2  23            		inc	hl
  22B3  10F9          		djnz	LCPYLP1
                      	
  22B5  2A90FD        		ld	hl,(VRAM-1)	;h=(VRAM)
  22B8  24            		inc	h
  22B9  24            		inc	h
  22BA  68            		ld	l,b		;b=0
  22BB  3A92FD        		ld	a,(SCREEN1)
  22BE  FE02          		cp	02h
  22C0  3822          		jr	c,LCPYTXT
                      	
  22C2                	LCPYGRP:
  22C2  110018        		ld	de,1800h
  22C5                	LCPYLP2:
  22C5  4E            		ld	c,(hl)
  22C6  CD0E1A        		call	SENDGRP
  22C9  23            		inc	hl
  22CA  1B            		dec	de
  22CB  7A            		ld	a,d
  22CC  B3            		or	e
  22CD  20F6          		jr	nz,LCPYLP2
                      	
  22CF                	LCPYEND:
  22CF  010020        		ld	bc,2000h
  22D2  CDD822        		call	LCPYLP3
                      	
  22D5  010A06        		ld	bc,060ah
  22D8                	LCPYLP3:
  22D8  79            		ld	a,c
  22D9  CD371A        		call	PRINTER
  22DC  10FA          		djnz	LCPYLP3
  22DE  C9            		ret
                      	
                      	;header for lcopy
  22DF                	LCPYHD:
  22DF  20200A1DC1    		db	20h, 20h, 0ah, 1dh, 0c1h
                      	
                      	;lcopy text screen
  22E4                	LCPYTXT:
  22E4  3E04          		ld	a,04h
  22E6  D393          		out	(93h),a		;CGROM ON
  22E8  3A20FA        		ld	a,(HEIGHT)
  22EB  47            		ld	b,a
  22EC                	LCPYTLP1:
  22EC  C5            		push	bc
  22ED  0E0C          		ld	c,0ch
  22EF                	LCPYTLP2:
  22EF  0620          		ld	b,COLUMNS
  22F1                	LCPYTLP3:
  22F1  C5            		push	bc
  22F2  7E            		ld	a,(hl)
  22F3  CDA014        		call	CGROM
  22F6  7B            		ld	a,e
  22F7  C60C          		add	a,0ch
  22F9  91            		sub	c
  22FA  5F            		ld	e,a
  22FB  1A            		ld	a,(de)
                      	
  22FC  57            		ld	d,a		;
  22FD  25            		dec	h
  22FE  25            		dec	h
  22FF  7E            		ld	a,(hl)		;attribute
  2300  24            		inc	h
  2301  24            		inc	h
  2302  CB77          		bit	6,a
  2304  201F          		jr	nz,LCPYSEMI
  2306  0F            		rrca
  2307  7A            		ld	a,d		;
  2308  3001          		jr	nc,NOTREV
  230A  2F            		cpl
  230B                	NOTREV:
  230B  4F            		ld	c,a
  230C  CD0E1A        		call	SENDGRP
  230F  23            		inc	hl
  2310  C1            		pop	bc
  2311  10DE          		djnz	LCPYTLP3
                      	
  2313  0D            		dec	c
  2314  2806          		jr	z,LCPYTZ
  2316  11E0FF        		ld	de,0-COLUMNS
  2319  19            		add	hl,de
  231A  18D3          		jr	LCPYTLP2
                      	
  231C                	LCPYTZ:
  231C  C1            		pop	bc
  231D  10CD          		djnz	LCPYTLP1
                      	
  231F  3E05          		ld	a,05h
  2321  D393          		out	(93h),a		;CGROM OFF
  2323  18AA          		jr	LCPYEND
                      	
  2325                	LCPYSEMI:
                      	;c=	9-12	xx**....
                      	;	5-8	xx..**..
                      	;	1-4	xx....**
                      	
  2325  7E            		ld	a,(hl)
  2326  0D            		dec	c
  2327  CB51          		bit	2,c
  2329  2006          		jr	nz,SEMI58	;c=5-8
  232B  CB59          		bit	3,c
  232D  2804          		jr	z,SEMI14	;c=1-4
                      	
  232F  0F            		rrca
  2330  0F            		rrca
  2331                	SEMI58:
  2331  0F            		rrca
  2332  0F            		rrca
  2333                	SEMI14:
  2333  E603          		and	03h
                      	
                      	;00h->00h, 01h->0fh, 02h->f0h, 03h->ffh
  2335  1F            		rra			;c-flag=0
  2336  3002          		jr	nc,SEMINC
  2338  C61E          		add	a,0fh*2
  233A                	SEMINC:
  233A  1F            		rra			;c-flag=0
  233B  30CE          		jr	nc,NOTREV
  233D  C6F0          		add	a,0f0h
  233F  18CA          		jr	NOTREV
                      	
                      	
                      	;KEY command
  2341                	C_KEY:
  2341  CDE40D        		call	INT1ARG
  2344  3D            		dec	a
  2345  FE0A          		cp	0ah
  2347  D2CB03        		jp	nc,FCERR
  234A  113DFB        		ld	de,FKEYTBL
  234D  87            		add	a,a		;*2
  234E  87            		add	a,a		;*4
  234F  87            		add	a,a		;*8
  2350  83            		add	a,e		;no carry
  2351  5F            		ld	e,a
  2352  7E            		ld	a,(hl)
  2353  FE2C          		cp	','
  2355  C2C203        		jp	nz,SNERR
  2358  D5            		push	de		;
  2359  23            		inc	hl
  235A  CD8126        		call	STRARG
  235D  EB            		ex	de,hl
  235E  D1            		pop	de		;
                      	
  235F  01FF08        		ld	bc,08ffh
  2362  3C            		inc	a
  2363                	KEYLP1:
  2363  3D            		dec	a
  2364  2806          		jr	z,KEYLP2
  2366  EDA0          		ldi
  2368  10F9          		djnz	KEYLP1
  236A  1804          		jr	KEYEND
                      	
  236C                	KEYLP2:
  236C  12            		ld	(de),a		;a=0
  236D  13            		inc	de
  236E  10FC          		djnz	KEYLP2
  2370                	KEYEND:
  2370  C3B512        		jp	PRTFKEY
                      	
                      	
                      	;CSAVE command
  2373                	C_CSV:
  2373  CD8126        		call	STRARG
  2376  B7            		or	a
  2377  CACB03        		jp	z,FCERR
  237A  4F            		ld	c,a
                      	
                      	;write header (d3h*10)
  237B  CDB81A        		call	WOPEN
  237E  3ED3          		ld	a,0d3h
  2380  060A          		ld	b,0ah
  2382                	CSVLP1:
  2382  CDCC1A        		call	PUTCMT
  2385  10FB          		djnz	CSVLP1
                      	
                      	;file name
  2387  0606          		ld	b,06h
                      	
  2389  0C            		inc	c
  238A                	CSVLP2:
  238A  1A            		ld	a,(de)
  238B  0D            		dec	c
  238C  2002          		jr	nz,CSVNZ
  238E  AF            		xor	a
  238F  0C            		inc	c
  2390                	CSVNZ:
  2390  CDCC1A        		call	PUTCMT
  2393  13            		inc	de
  2394  10F4          		djnz	CSVLP2
                      	
                      	;data
  2396                	CSVDATA:
  2396  01B024        		ld	bc,24b0h
  2399  CDE825        		call	WAITLP
                      	
  239C  2A5FFA        		ld	hl,(STARTAD)
  239F  ED5B56FF      		ld	de,(VARAD)
  23A3                	CSVLP3:
  23A3  7E            		ld	a,(hl)
  23A4  CDCC1A        		call	PUTCMT
  23A7  23            		inc	hl
  23A8  E7            		rst	CPHLDE
  23A9  20F8          		jr	nz,CSVLP3
                      	
                      	;footer (00h*9)
  23AB  0609          		ld	b,09h
  23AD                	CSVLP4:
  23AD  CDCC1A        		call	PUTCMT		;a=0
  23B0  10FB          		djnz	CSVLP4
                      	
  23B2  C3061B        		jp	WCLOSE
                      	
                      	
                      	;CLOAD command
  23B5                	C_CLD:
  23B5  F1            		pop	af		;cancel return address
  23B6  CD593F        		call	SKIPSP
  23B9  FE95          		cp	PRINT_
  23BB  0E00          		ld	c,00h		;file name length
  23BD  79            		ld	a,c
  23BE  2002          		jr	nz,CLDNZ1
  23C0  23            		inc	hl
  23C1  2F            		cpl
  23C2                	CLDNZ1:
  23C2  32D8FE        		ld	(VERIFY),a
  23C5  F5            		push	af		;verify?
  23C6  CD603F        		call	CHKCLN
  23C9  2808          		jr	z,NOTARGET
  23CB  CD8126        		call	STRARG
  23CE  B7            		or	a
  23CF  CACB03        		jp	z,FCERR
  23D2  4F            		ld	c,a
  23D3                	NOTARGET:
  23D3  CD611A        		call	ROPEN
                      	
  23D6                	CLDLP1:
  23D6  EB            		ex	de,hl
  23D7  CD3925        		call	GETFN
  23DA  EB            		ex	de,hl
                      	
                      	;compare file name
  23DB  79            		ld	a,c
  23DC  B7            		or	a
  23DD  2815          		jr	z,CLDFND
                      	
  23DF  D5            		push	de		;target
                      	
  23E0  21D1FE        		ld	hl,FNAME
  23E3  0C            		inc	c
  23E4  0606          		ld	b,06h
  23E6                	CLDLP2:
  23E6  1A            		ld	a,(de)
  23E7  0D            		dec	c
  23E8  2002          		jr	nz,CLDNZ2
  23EA  AF            		xor	a
  23EB  0C            		inc	c
  23EC                	CLDNZ2:
  23EC  BE            		cp	(hl)
  23ED  2045          		jr	nz,CLDSKIP
  23EF  23            		inc	hl
  23F0  13            		inc	de
  23F1  10F3          		djnz	CLDLP2
  23F3  D1            		pop	de		;target
                      	
  23F4                	CLDFND:
  23F4  217625        		ld	hl,FOUND
  23F7  CD8325        		call	PUTFN
  23FA  CD9C1A        		call	CHKVRF2
                      	
  23FD  F1            		pop	af		;verify?
  23FE  4F            		ld	c,a		;c=0: not verify
                      	
  23FF  2A5FFA        		ld	hl,(STARTAD)
  2402                	CLDLP3:
  2402  060A          		ld	b,0ah
  2404                	CLDLP4:
  2404  EB            		ex	de,hl
  2405  21FEFF        		ld	hl,0-0002h
  2408  39            		add	hl,sp
  2409  E7            		rst	CPHLDE
  240A  2837          		jr	z,CLDOM		;over memory error
  240C  EB            		ex	de,hl
  240D  CD8F1A        		call	GETCMTTR
  2410  0C            		inc	c
  2411  0D            		dec	c
  2412  2803          		jr	z,CLDZ2		;c=0: not verify
  2414  BE            		cp	(hl)
  2415  2035          		jr	nz,CLDBAD
  2417                	CLDZ2:
  2417  77            		ld	(hl),a
  2418  23            		inc	hl
  2419  B7            		or	a
  241A  20E6          		jr	nz,CLDLP3
  241C  CD2A1B        		call	BLNKAST
  241F  10E3          		djnz	CLDLP4
  2421  CDAA1A        		call	RCLOSE
                      	
  2424  2256FF        		ld	(VARAD),hl
                      	
  2427  AF            		xor	a
  2428  3258FA        		ld	(DEVICE),a
  242B                	OKCHGLKP:
  242B  219003        		ld	hl,OK
  242E  CDCF30        		call	PUTS
  2431  C36B04        		jp	CHGLKP
                      	
  2434                	CLDSKIP:
  2434  217D25        		ld	hl,SKIP
  2437  CD8325        		call	PUTFN
                      	
                      	;check footer (00h*10)
  243A  11000A        		ld	de,0a00h
  243D  CD4E25        		call	CMTSKP
  2440  D1            		pop	de		;target
  2441  1893          		jr	CLDLP1
                      	
  2443                	CLDOM:
  2443  CDAA1A        		call	RCLOSE
  2446  CDBE34        		call	NEW
  2449  C3D103        		jp	OMERR
                      	
  244C                	CLDBAD:
  244C  CDAA1A        		call	RCLOSE
  244F  215524        		ld	hl,BAD
  2452  C3D400        		jp	PUTSEDIT
                      	
  2455                	BAD:
  2455  42616400      		db	"Bad", 00h
                      	
                      	
                      	;add 4-byte integer and 4byte integer
                      	;input: FAC1(integer),FAC2(integer)
                      	;output: FAC1(integer),f
                      	;destroy: de,hl
  2459                	ADDINT4:
  2459  2A66FF        		ld	hl,(FAC1)
  245C  ED5B6DFF      		ld	de,(FAC2)
  2460  19            		add	hl,de
  2461  2266FF        		ld	(FAC1),hl
  2464  2A68FF        		ld	hl,(FAC1+2)
  2467  ED5B6FFF      		ld	de,(FAC2+2)
  246B  ED5A          		adc	hl,de
  246D  2268FF        		ld	(FAC1+2),hl
  2470  C9            		ret
                      	
                      	
                      	;subtract 4-byte integer from 4-byte integer
                      	;input: FAC1(integer),FAC2(integer)
                      	;output: FAC1(integer),f
                      	;destroy: f,de,hl
  2471                	SUBINT4:
  2471  2A66FF        		ld	hl,(FAC1)
  2474  ED5B6DFF      		ld	de,(FAC2)
  2478  B7            		or	a
  2479  ED52          		sbc	hl,de
  247B  2266FF        		ld	(FAC1),hl
  247E  2A68FF        		ld	hl,(FAC1+2)
  2481  ED5B6FFF      		ld	de,(FAC2+2)
  2485  ED52          		sbc	hl,de
  2487  2268FF        		ld	(FAC1+2),hl
  248A  C9            		ret
                      	
                      	
                      	;a=FAC1(int4)/(hl)<=9, FAC1%=(hl), hl+=4
                      	;input: FAC1(integer), hl=division factor (4byte int) address
                      	;output: a=FAC1/(hl4), FAC1=FAC1%(hl4), hl=hl+4, a=[0,9]
                      	;destroy: f,FAC2
  248B                	DIVINT4:
  248B  C5            		push	bc
  248C  D5            		push	de
  248D  CD4E0C        		call	SETF2
  2490  2B            		dec	hl
  2491  E5            		push	hl
  2492  AF            		xor	a
  2493                	DIVINT4LP:
  2493  3C            		inc	a
  2494  CD7124        		call	SUBINT4
  2497  30FA          		jr	nc,DIVINT4LP
  2499  3D            		dec	a
  249A  CD5924        		call	ADDINT4
  249D  E1            		pop	hl
  249E  D1            		pop	de
  249F  C1            		pop	bc
  24A0  C9            		ret
                      	
                      	
                      	;negate 4-byte integer
                      	;input: FAC1(integer)
                      	;output: FAC1(integer), c-flag, z-flag
                      	;destroy: af,hl
  24A1                	NEGINT4:
  24A1  2166FF        		ld	hl,FAC1
  24A4  AF            		xor	a
  24A5  96            		sub	(hl)
  24A6  77            		ld	(hl),a
  24A7  23            		inc	hl
  24A8  3E00          		ld	a,00h
  24AA  9E            		sbc	a,(hl)
  24AB  77            		ld	(hl),a
  24AC  23            		inc	hl
  24AD  3E00          		ld	a,00h
  24AF  9E            		sbc	a,(hl)
  24B0  77            		ld	(hl),a
  24B1  23            		inc	hl
  24B2  3E00          		ld	a,00h
  24B4  9E            		sbc	a,(hl)
  24B5  77            		ld	(hl),a
  24B6  C9            		ret
                      	
                      	
                      	;compare 4-byte integer and 4-byte integer
                      	;input: FAC1(integer), FAC2(integer)
                      	;output: c-flag,z-flag
                      	;destroy: f,de,hl
  24B7                	CMPINT4:
  24B7  2A68FF        		ld	hl,(FAC1+2)
  24BA  ED5B6FFF      		ld	de,(FAC2+2)
  24BE  B7            		or	a
  24BF  ED52          		sbc	hl,de
  24C1  C0            		ret	nz
  24C2  2A66FF        		ld	hl,(FAC1)
  24C5  ED5B6DFF      		ld	de,(FAC2)
  24C9  ED52          		sbc	hl,de
  24CB  C9            		ret
                      	
                      	
                      	;decrement 4-byte integer
                      	;input: FAC1
                      	;output: FAC1
                      	;destroy: af,hl
  24CC                	DECINT4:
  24CC  2A66FF        		ld	hl,(FAC1)
  24CF  7C            		ld	a,h
  24D0  B5            		or	l		;
  24D1  2B            		dec	hl
  24D2  2266FF        		ld	(FAC1),hl
  24D5  C0            		ret	nz		;
  24D6  2A68FF        		ld	hl,(FAC1+2)
  24D9  2B            		dec	hl
  24DA  2268FF        		ld	(FAC1+2),hl
  24DD  C9            		ret
                      	
                      	
                      	;4-byte integer=0?
                      	;output: a(4 bytes OR),z-flag
                      	;destroy: af
  24DE                	CHKINT4:
  24DE  E5            		push	hl
  24DF  2A66FF        		ld	hl,(FAC1)
  24E2  7C            		ld	a,h
  24E3  B5            		or	l
  24E4  2A68FF        		ld	hl,(FAC1+2)
  24E7  B4            		or	h
  24E8  B5            		or	l
  24E9  E1            		pop	hl
  24EA  C9            		ret
                      	
                      	
                      	;convert float to 4-byte integer (>=0)
                      	;(round toward zero)
                      	;input: FAC1
                      	;output: FAC1 (integer)
                      	;destroy: af,b,de,hl
  24EB                	FTOI4:
  24EB  3EA0          		ld	a,81h+1fh
  24ED  216AFF        		ld	hl,FAC1+4
  24F0  96            		sub	(hl)		;
  24F1  47            		ld	b,a
  24F2  2A66FF        		ld	hl,(FAC1)
  24F5  ED5B68FF      		ld	de,(FAC1+2)
  24F9  CBFA          		set	7,d
  24FB  280A          		jr	z,FTOI4END	;
  24FD                	FTOI4LP:
  24FD  CB3A          		srl	d
  24FF  CB1B          		rr	e
  2501  CB1C          		rr	h
  2503  CB1D          		rr	l
  2505  10F6          		djnz	FTOI4LP
  2507                	FTOI4END:
                      	;	jp	SETI4
                      	
                      	;set 4-byte integer in FAC1
                      	;input: de-hl
  2507                	SETI4:
  2507  2266FF        		ld	(FAC1),hl
  250A  ED5368FF      		ld	(FAC1+2),de
  250E  C9            		ret
                      	
                      	
                      	;convert 4-byte integer to float (unsigned)
                      	;input: de-hl
                      	;output: FAC1
                      	;destroy: af,b,hl
  250F                	I4TOF:
  250F  D5            		push	de
  2510  7A            		ld	a,d
  2511  B3            		or	e
  2512  B4            		or	h
  2513  B5            		or	l
  2514  CA970C        		jp	z,I2TOFZERO
                      	
  2517  7A            		ld	a,d
  2518  0620          		ld	b,20h
  251A                	I4TOFLP:
  251A  C680          		add	a,80h
  251C  3806          		jr	c,I4TOFEND
  251E  29            		add	hl,hl
  251F  CB13          		rl	e
  2521  17            		rla
  2522  10F6          		djnz	I4TOFLP
                      	
  2524                	I4TOFEND:
  2524  57            		ld	d,a
  2525  C3940C        		jp	I2TOFEND2
                      	
                      	
                      	;check cload header and get file name
                      	;destroy: af,b,de (if no error)
  2528                	_GETFN:	ds	GETFN-_GETFN
  2539                		org	GETFN
                      	
                      	;check header (d3h*10)
  2539  11D30A        		ld	de,0ad3h
  253C  CD4E25        		call	CMTSKP
                      	;file name
  253F  11D1FE        		ld	de,FNAME
                      	
  2542  0606          		ld	b,06h
  2544                	GETFNLP:
  2544  CD8F1A        		call	GETCMTTR
  2547  12            		ld	(de),a
  2548  13            		inc	de
  2549  10F9          		djnz	GETFNLP
  254B  C9            		ret
                      	
                      	
                      	;skip CMT data
                      	;input: d=count, e=data
                      	;destroy: af,b
  254C                	_CMTSKP:ds	CMTSKP-_CMTSKP
  254E                		org	CMTSKP
                      	
  254E  42            		ld	b,d
  254F                	CMTSKPLP:
  254F  CD8F1A        		call	GETCMTTR
  2552  BB            		cp	e
  2553  20F9          		jr	nz,CMTSKP
  2555  10F8          		djnz	CMTSKPLP
  2557  C9            		ret
                      	
                      	
  2558                	_FOUND:	ds	FOUND-_FOUND
  2576                		org	FOUND
                      	
  2576  466F756E643A00		db	"Found:", 00h
                      	
  257D                	SKIP:
  257D  536B69703A00  		db	"Skip:", 00h
                      	
                      	
                      	;put message and file name in device
                      	;input: hl=message address
                      	;destroy: af,hl,(bc,de,FAC1)
  2583                	_PUTFN:	ds	PUTFN-_PUTFN
  2583                		org	PUTFN
                      	
  2583  CDCF30        		call	PUTS
  2586  21D1FE        		ld	hl,FNAME
  2589  CDCF30        		call	PUTS
  258C  C33927        		jp	PUTNL
                      	
                      	
                      	;CMT open for INPUT #-1
                      	;destroy: af,b
  258F                	_INPOPN:ds	INPOPN-_INPOPN
  259A                		org	INPOPN
                      	
                      	;	ld	a,0ffh
                      	;	ld	(VERIFY),a
                      	;	call	ROPEN
  259A  CD5C1A        		call	VRFOPN
  259D  D5            		push	de
  259E  119C06        		ld	de,069ch
  25A1  CD4E25        		call	CMTSKP
  25A4  D1            		pop	de
  25A5  C9            		ret
                      	
                      	
                      	;CMT open for PRINT #-1
                      	;destroy: a,b
  25A6                	_PRTOPN:ds	PRTOPN-_PRTOPN
  25A8                		org	PRTOPN
                      	
  25A8  CDB81A        		call	WOPEN
  25AB  3E9C          		ld	a,9ch
  25AD  0606          		ld	b,06h
  25AF                	PRTOPNLP:
  25AF  CDCC1A        		call	PUTCMT
  25B2  10FB          		djnz	PRTOPNLP
  25B4  C9            		ret
                      	
                      	
                      	;convert intermediate code to command/function ascii characters
                      	;input: a=code
                      	;output: a=first character, hl=address
                      	;destroy: f,b,hl
  25B5                	CNVASC:
  25B5  212B02        		ld	hl,CMDNAME-1
  25B8  FEAB          		cp	CMDLAST+1
  25BA  3819          		jr	c,CNVASCC
  25BC  218D41        		ld	hl,CMDNAMEEX-1
  25BF  FEC2          		cp	CLAST66+1
  25C1  3004          		jr	nc,CNVFNC
  25C3  D62B          		sub	CMDLAST-7fh
  25C5  180E          		jr	CNVASCC
                      	
                      	;function
  25C7                	CNVFNC:
  25C7  21E302        		ld	hl,FNCNAME-1
  25CA  D642          		sub	TAB_-80h
  25CC  FEB0          		cp	FNCLAST-(TAB_-80h)+1
  25CE  3805          		jr	c,CNVASCC
  25D0  21F541        		ld	hl,FNCNAMEEX-1
  25D3  D630          		sub	FNCLAST+1-TAB_
                      	
  25D5                	CNVASCC:
  25D5  D67F          		sub	7fh
  25D7  47            		ld	b,a
  25D8                	CNVASCLP:
  25D8  23            		inc	hl
  25D9  7E            		ld	a,(hl)
  25DA  D680          		sub	80h
  25DC  38FA          		jr	c,CNVASCLP
  25DE  10F8          		djnz	CNVASCLP
  25E0  C9            		ret
                      	
                      	
                      	;wait for about 3.5s
                      	;destroy: af,bc
  25E1                	_WAIT:	ds	WAIT-_WAIT
  25E5                		org	WAIT
                      	
  25E5  010000        		ld	bc,0000h
                      	;25e8
  25E8                	WAITLP:
  25E8  3A18FA        		ld	a,(STOPFLG)
  25EB  FE03          		cp	03h
  25ED  C8            		ret	z
                      	
  25EE  E3            		ex	(sp),hl		;waste time
  25EF  E3            		ex	(sp),hl		;waste time
  25F0  00            		nop			;waste time
                      	
  25F1  0B            		dec	bc
  25F2  78            		ld	a,b
  25F3  B1            		or	c
  25F4  20F2          		jr	nz,WAITLP
  25F6  C9            		ret
                      	
                      	
                      	;EXEC command
  25F7                	C_EXEC:
  25F7  CD920B        		call	NARGMO
  25FA  CD9F0C        		call	FTOI
                      	
                      	;for compatibility with N60-BASIC
  25FD  2A4EFF        		ld	hl,(PROGAD)
  2600  EB            		ex	de,hl
  2601  7C            		ld	a,h
  2602  B5            		or	l
  2603  ED44          		neg			;set c-flag if address<>0
  2605  D5            		push	de
  2606  CDA407        		call	JPHL
  2609  D1            		pop	de
  260A  C9            		ret
                      	
                      	
                      	;get a character from buffer
                      	;input: hl=buffer map address
                      	;output: a,z-flag(1=no input)
                      	;destroy: f,hl
  260B                	GETBF:
  260B  7E            		ld	a,(hl)		;in
  260C  2C            		inc	l		;inc hl
  260D  BE            		cp	(hl)		;out
  260E  C8            		ret	z
  260F  7E            		ld	a,(hl)
  2610  3C            		inc	a
  2611  2C            		inc	l		;inc hl
  2612  2C            		inc	l		;inc hl
  2613  A6            		and	(hl)		;size
  2614  2D            		dec	l		;dec hl
  2615  2D            		dec	l		;dec hl
  2616  77            		ld	(hl),a		;out
  2617  2C            		inc	l		;inc hl
  2618  2C            		inc	l		;inc hl
  2619  2C            		inc	l		;inc hl
  261A  86            		add	a,(hl)		;hl=(buffer address)+a
  261B  2C            		inc	l		;inc hl, l>0, reset z-flag
  261C  66            		ld	h,(hl)
  261D  6F            		ld	l,a
  261E  3001          		jr	nc,GETBFNC
  2620  24            		inc	h		;h>0, reset z-flag
  2621                	GETBFNC:
  2621  7E            		ld	a,(hl)
  2622  C9            		ret
                      	
                      	
                      	;input: STRDSC1, STRDSC4
                      	;destroy: af,bc,de,hl
  2623                	ADDSTR:
  2623  3A39FF        		ld	a,(STRDSC4)
  2626                	ADDSTR2:
  2626  47            		ld	b,a		;;
  2627  3A2DFF        		ld	a,(STRDSC1)
  262A  4F            		ld	c,a		;
  262B  80            		add	a,b		;c+b
  262C  DAE903        		jp	c,LSERR		;over 255 characters
                      	
  262F  CD5D26        		call	GCCHECK
  2632  223DFF        		ld	(STRAD),hl
  2635  23            		inc	hl
  2636  EB            		ex	de,hl
  2637  2A2FFF        		ld	hl,(STRDSC1+2)
  263A  ED532FFF      		ld	(STRDSC1+2),de
                      	
  263E  78            		ld	a,b		;;
  263F  81            		add	a,c		;
  2640  322DFF        		ld	(STRDSC1),a
  2643  90            		sub	b		;c=0?
                      	
  2644  78            		ld	a,b
  2645  0600          		ld	b,00h
  2647  2802          		jr	z,ADDSZ1
  2649  EDB0          		ldir
  264B                	ADDSZ1:
  264B  B7            		or	a
  264C  2806          		jr	z,ADDSZ2
  264E  4F            		ld	c,a		;b=0
  264F  2A3BFF        		ld	hl,(STRDSC4+2)
  2652  EDB0          		ldir
  2654                	ADDSZ2:
  2654  AF            		xor	a
  2655  3239FF        		ld	(STRDSC4),a
  2658  3C            		inc	a
  2659  3225FF        		ld	(ARGTYP),a	;=1
  265C  C9            		ret
                      	
                      	
                      	;check string area and call GC if necessary
                      	;input: a=new string length
                      	;output: hl=new (STRAD)+1
                      	;destroy: af,de
  265D                	GCCHECK:
  265D  C5            		push	bc
  265E  1600          		ld	d,00h
  2660  5F            		ld	e,a
  2661  D5            		push	de
  2662  CD7526        		call	CHKSAREA
  2665  D1            		pop	de
  2666  300B          		jr	nc,GCCHECKOK
  2668  D5            		push	de
  2669  CD0F31        		call	GC
  266C  D1            		pop	de
  266D  CD7526        		call	CHKSAREA
  2670  DAE603        		jp	c,OSERR
  2673                	GCCHECKOK:
  2673  C1            		pop	bc
  2674  C9            		ret
                      	
                      	;check string area
  2675                	CHKSAREA:
  2675  2A3DFF        		ld	hl,(STRAD)
  2678  B7            		or	a
  2679  ED52          		sbc	hl,de
  267B  ED5B5BFA      		ld	de,(STACK)
  267F  E7            		rst	CPHLDE
  2680  C9            		ret
                      	
                      	
                      	;get a string argument
                      	;input: hl=program address
                      	;output: a=length, de=string address, hl=next address, z-flag(1=ok)
                      	;destroy: f,bc,FAC1,FAC2
  2681                	STRARG:
  2681  CD603F        		call	CHKCLN
  2684  CAF803        		jp	z,MOERR
  2687                	STRARG3:
  2687  CD4717        		call	ARG
  268A                	STRARG2:
  268A  EB            		ex	de,hl
  268B  CDA70B        		call	CHKSTR
                      	;get string pointer
  268E  23            		inc	hl
  268F  23            		inc	hl
  2690  46            		ld	b,(hl)
  2691  23            		inc	hl
  2692  66            		ld	h,(hl)
  2693  68            		ld	l,b
  2694  EB            		ex	de,hl
  2695  C9            		ret
                      	
                      	
                      	;output to port f0h
                      	;input: a=data
                      	;destroy: none
  2696                	_SETF0H:ds	SETF0H-_SETF0H
  2698                		org	SETF0H
                      	
  2698  D3F0          		out	(0f0h),a
  269A  3264FE        		ld	(PORTF0H),a
  269D  C9            		ret
                      	
                      	
                      	;get string,integer argument for LEFT$(),RIGHT$(),MID$()
                      	;input: hl=program address
                      	;output: STRDSC1, a=length, hl=string address, FAC1,de=integer
                      	;destroy: FAC2,f,bc
  269E                	ARGSI1:
  269E  CDB50B        		call	CHKLPAR
  26A1  CD8126        		call	STRARG
  26A4  F5            		push	af
  26A5  322DFF        		ld	(STRDSC1),a	;length
  26A8  ED532FFF      		ld	(STRDSC1+2),de	;string address
  26AC  7E            		ld	a,(hl)
  26AD  FE2C          		cp	','
  26AF  C2C203        		jp	nz,SNERR
  26B2  CDBA26        		call	ARGI1
  26B5  2A2FFF        		ld	hl,(STRDSC1+2)	;string address
  26B8  F1            		pop	af		;length
  26B9  C9            		ret
                      	
                      	
                      	;protect string descriptor and get integer argument
                      	;input: hl=program address
                      	;output: FAC1,de=integer
                      	;destroy: FAC2,af,bc,hl
  26BA                	ARGI1:
  26BA  E5            		push	hl		;program address
  26BB  CDE819        		call	COPYSTR
  26BE  E1            		pop	hl
  26BF  CDE30D        		call	INT1INC
  26C2  C3FC19        		jp	BACKSTR
                      	
                      	
                      	;put a character in device
                      	;input: a=character code, (DEVICE)=0:CRT, 1:printer, 2:RS-232C, 80h-ffh:CMT
                      	; (1,4,5,8,9,c,d...:printer, 2,3,6,7,a,b,e,f...:RS-232C)
                      	;destroy: af
  26C5                	_PUTDEV:ds	PUTDEV-_PUTDEV
  26C7                		org	PUTDEV
                      	
  26C7  CDCFFF        		call	HOOKPUT
  26CA  F5            		push	af
  26CB  3A58FA        		ld	a,(DEVICE)
  26CE  B7            		or	a
  26CF  2856          		jr	z,PUTCRT
  26D1  FACD1A        		jp	m,PUTCMT2
  26D4  E602          		and	02h
  26D6  283A          		jr	z,PUTPRT2
                      	;	jr	nz,PUT232
                      	
                      	;input: a
                      	;output: none
                      	;destroy: af
  26D8                	PUT232:
  26D8  3E17          		ld	a,17h		;RTS=0
  26DA  D381          		out	(81h),a
                      	
  26DC                	PUT232LP:
  26DC  DB81          		in	a,(81h)
  26DE  2F            		cpl
  26DF  E605          		and	05h
  26E1  280C          		jr	z,PUT232Z	;if TxEMPTY=1, TxRDY=1
                      	
  26E3  3A18FA        		ld	a,(STOPFLG)
  26E6  FE03          		cp	03h
  26E8  20F2          		jr	nz,PUT232LP
                      	;stop
  26EA  CDF226        		call	PUT232END
  26ED  1862          		jr	STOPSP
                      	
  26EF                	PUT232Z:
  26EF  F1            		pop	af
  26F0  D380          		out	(80h),a
  26F2                	PUT232END:
  26F2  3E37          		ld	a,37h		;RTS=1
  26F4  D381          		out	(81h),a
  26F6  C9            		ret
                      	
                      	
  26F7                	PUTPRTTAB:
  26F7  3E20          		ld	a,' '
  26F9  CD1727        		call	PUTPRT3		;a<-(PRTPOS)
  26FC  E607          		and	07h
  26FE  20F7          		jr	nz,PUTPRTTAB
  2700  C9            		ret
                      	
                      	
                      	;put new line to printer
                      	;destroy: af
  2701                	_PUTPRNL:ds	PUTPRNL-_PUTPRNL
  2701                		org	PUTPRNL
  2701  3E01          		ld	a,01h
  2703  3258FA        		ld	(DEVICE),a
  2706  3A57FA        		ld	a,(PRTPOS)
  2709  B7            		or	a
  270A  C43927        		call	nz,PUTNL
  270D  AF            		xor	a
  270E  3258FA        		ld	(DEVICE),a
  2711  C9            		ret
                      	
                      	
  2712                	PUTPRT2:
  2712  F1            		pop	af
  2713  FE09          		cp	09h
  2715  28E0          		jr	z,PUTPRTTAB
  2717                	PUTPRT3:
  2717  CD1C1A        		call	PUTPRT
  271A  D60D          		sub	0dh
  271C  2805          		jr	z,PUTPRTZ	;a=0 if CR
  271E  D8            		ret	c
  271F  3A57FA        		ld	a,(PRTPOS)
  2722  3C            		inc	a
  2723                	PUTPRTZ:
  2723  3257FA        		ld	(PRTPOS),a
  2726  C9            		ret
                      	
                      	
  2727                	PUTCRT:
  2727  F1            		pop	af
  2728  C37510        		jp	PRTC
                      	
                      	
                      	;put new line if necessary
                      	;destroy: af
  272B                	_PUTNLX:ds	PUTNLX-_PUTNLX
  272D                		org	PUTNLX
  272D  3A58FA        		ld	a,(DEVICE)
  2730  B7            		or	a
  2731  CA3F09        		jp	z,PRTNLX
  2734  3D            		dec	a
  2735  28CA          		jr	z,PUTPRNL
  2737  1800          		jr	PUTNL
                      	
                      	
                      	;put new line
                      	;destroy: af
  2739                	_PUTNL:	ds	PUTNL-_PUTNL
  2739                		org	PUTNL
                      	
  2739  3E0D          		ld	a,0dh
  273B  CDC726        		call	PUTDEV
  273E  3E0A          		ld	a,0ah
  2740  1885          		jr	PUTDEV
                      	
                      	
                      	;input: a=length, hl=string address
                      	;destroy: af,bc,de,hl
  2742                	MAKESTR:
  2742  322DFF        		ld	(STRDSC1),a
  2745  222FFF        		ld	(STRDSC1+2),hl
  2748  AF            		xor	a
  2749  C32626        		jp	ADDSTR2
                      	
                      	
                      	;STOP and ESC
                      	;destroy: af
  274C                	_STOPESC:ds	STOPESC-_STOPESC
  274D                		org	STOPESC
                      	
  274D  CD5B27        		call	CHKSTOP
  2750  C0            		ret	nz
                      	;	jp	STOPSP
                      	
                      	;reset sp and stop
  2751                	STOPSP:
  2751  ED7B5BFA      		ld	sp,(STACK)
  2755  21FFFF        		ld	hl,0ffffh
  2758  C33C35        		jp	STOP2
                      	
                      	
                      	;check STOP/ESC
                      	;output: z-flag (1=STOP)
                      	;destroy: af
  275B                	CHKSTOP:
  275B  3A18FA        		ld	a,(STOPFLG)
  275E  FE03          		cp	03h
  2760  C8            		ret	z
  2761  FE1B          		cp	1bh
  2763  C0            		ret	nz
  2764  CDBC0F        		call	GETCH		;cancel ESC
  2767  18F2          		jr	CHKSTOP
                      	
                      	
                      	;INKEY$
  2769                	F_INKY:
  2769  218A18        		ld	hl,FNCRTN
  276C  E5            		push	hl
                      	
  276D  CDBC0F        		call	GETCH
  2770  282A          		jr	z,INKYZ
                      	
  2772  FE03          		cp	03h
  2774  2826          		jr	z,INKYZ
  2776  FE1B          		cp	1bh
  2778  2822          		jr	z,INKYZ
                      	
  277A  47            		ld	b,a		;
  277B                	INKYNZ:
  277B  3E01          		ld	a,01h		;new string length=1
  277D  3230FF        		ld	(STRDSC1+3),a	;old string descriptor address < (STRAD)
  2780  CD5D26        		call	GCCHECK
  2783  223DFF        		ld	(STRAD),hl
  2786  23            		inc	hl
  2787  70            		ld	(hl),b		;
  2788  222FFF        		ld	(STRDSC1+2),hl	;new string descriptor address
  278B  3E01          		ld	a,01h
  278D                	INKYLEN:
  278D  322DFF        		ld	(STRDSC1),a
                      	
  2790                	INKYEND:
  2790  212DFF        		ld	hl,STRDSC1
  2793  2267FF        		ld	(FAC1+1),hl
  2796  3E01          		ld	a,01h
  2798  3225FF        		ld	(ARGTYP),a
  279B  C9            		ret
                      	
  279C                	INKYZ:
  279C  AF            		xor	a
  279D  18EE          		jr	INKYLEN
                      	
                      	
                      	;get device number
                      	;in	out
                      	;#0	0	CRT
                      	;#-1	80h	CMT
                      	;#-2	2	RS-232C
                      	;#-3	1	printer
                      	;input: hl=program address
                      	;output: a, hl,(PROGAD)=next address
                      	;destroy: f,bc,de
  279F                	DEVNUM:
  279F  CD593F        		call	SKIPSP
  27A2  FE23          		cp	'#'
  27A4  3E00          		ld	a,00h
  27A6  C0            		ret	nz
                      	
  27A7  23            		inc	hl
  27A8  CD140E        		call	INT2ARG
  27AB  E5            		push	hl
  27AC  1B            		dec	de
  27AD  210400        		ld	hl,0004h
  27B0  19            		add	hl,de
  27B1  D2CB03        		jp	nc,FCERR
  27B4  7D            		ld	a,l
  27B5  3C            		inc	a
  27B6  FE03          		cp	03h
  27B8  3803          		jr	c,DEVNUMC	;#-2,#-3
  27BA  E601          		and	01h
  27BC  0F            		rrca
  27BD                	DEVNUMC:
  27BD  E1            		pop	hl
  27BE  F5            		push	af
  27BF  CD6B3F        		call	CHKCMM
  27C2  C2C203        		jp	nz,SNERR
  27C5  224EFF        		ld	(PROGAD),hl
  27C8  F1            		pop	af
  27C9  C9            		ret
                      	
                      	
                      	;convert string to 2-byte integer (unsigned)
                      	;input: hl=program address
                      	;output: de=integer, hl=next address
                      	;destroy: af,bc
  27CA                	ATOI2:
  27CA  0600          		ld	b,00h
                      	
                      	;input: hl=program address, b=string length
                      	;output: b=remaining length, de=integer, hl=next address
  27CC                	ATOI2LEN:
  27CC  110000        		ld	de,0000h
  27CF                	ATOI2LP:
  27CF  CD593F        		call	SKIPSP
  27D2  D630          		sub	'0'
  27D4  FE0A          		cp	'9'-'0'+1
  27D6  D0            		ret	nc
                      	
  27D7  E5            		push	hl
                      	
  27D8  2166E6        		ld	hl,0-6554
  27DB  19            		add	hl,de
  27DC  3813          		jr	c,ATOI2END
                      	
  27DE  62            		ld	h,d
  27DF  6B            		ld	l,e
  27E0  29            		add	hl,hl
  27E1  29            		add	hl,hl
  27E2  19            		add	hl,de
  27E3  29            		add	hl,hl
                      	
  27E4  85            		add	a,l
  27E5  6F            		ld	l,a
  27E6  3003          		jr	nc,ATOI2NC
  27E8  24            		inc	h
  27E9  2806          		jr	z,ATOI2END
                      	
  27EB                	ATOI2NC:
  27EB  EB            		ex	de,hl
  27EC  E1            		pop	hl
  27ED  23            		inc	hl
  27EE  10DF          		djnz	ATOI2LP
  27F0  C9            		ret
                      	
  27F1                	ATOI2END:
  27F1  E1            		pop	hl
  27F2  C9            		ret
                      	
                      	
                      	;convert string to float (integer part)
                      	;input: hl=address
                      	;output: FAC1, hl=next address
                      	;destroy: FAC2,af,bc,de
  27F3                	ATOIF:
  27F3  E5            		push	hl
  27F4  CD420C        		call	SETZERO
  27F7  E1            		pop	hl
  27F8  CD883A        		call	SKIPSPA
  27FB  C8            		ret	z
                      	
  27FC  0600          		ld	b,00h		;sign for CTOF
  27FE  23            		inc	hl
  27FF  FE2B          		cp	'+'
  2801  280F          		jr	z,ATOIFLP
  2803  FECA          		cp	PLUS_
  2805  280B          		jr	z,ATOIFLP
  2807  04            		inc	b
  2808  FE2D          		cp	'-'
  280A  2806          		jr	z,ATOIFLP
  280C  FECB          		cp	MINUS_
  280E  2802          		jr	z,ATOIFLP
  2810  05            		dec	b
  2811  2B            		dec	hl
  2812                	ATOIFLP:
  2812  CD1828        		call	CTOF
  2815  38FB          		jr	c,ATOIFLP
  2817  C9            		ret
                      	
                      	
                      	;FAC1 = FAC1 * 10 + [0-9]
                      	;input: FAC1, hl=address, b=sign(0=plus, 1=minus)
                      	;output: FAC1, c-flag(1=OK), hl=next address
                      	;destroy: FAC2,af,de
  2818                	CTOF:
  2818  CD883A        		call	SKIPSPA
  281B  C8            		ret	z		;c-flag=0
                      	
  281C  D630          		sub	'0'
  281E  FE0A          		cp	'9'-'0'+1
  2820  D0            		ret	nc
  2821  23            		inc	hl
  2822  E5            		push	hl
                      	
  2823  C5            		push	bc
  2824  F5            		push	af
  2825  CD1F39        		call	ABS
  2828  CD9F37        		call	MULF10
  282B  CD5239        		call	CPYFAC
  282E  F1            		pop	af
  282F  CD7D0C        		call	I1TOFA
  2832  CD9E36        		call	ADDF
  2835  C1            		pop	bc
                      	
  2836  78            		ld	a,b
  2837  0F            		rrca
  2838  DC280D        		call	c,NEGABS
                      	
  283B  E1            		pop	hl
  283C  37            		scf
  283D  C9            		ret
                      	
                      	
                      	;insert a character
                      	;output: hl=VRAM address
                      	;destroy: f,bc,de
  283E                	INSERT:
  283E  F5            		push	af
                      	
  283F  2AAAFD        		ld	hl,(CSRAD)
  2842  CDE128        		call	GETSCRC
  2845  47            		ld	b,a		;
                      	
  2846  23            		inc	hl
  2847  CD3314        		call	CHKLINE
  284A  2045          		jr	nz,INSSCRL
                      	
  284C  CD0316        		call	CNVATT1
  284F  4F            		ld	c,a
                      	
  2850                	INSLP:
  2850  CDE128        		call	GETSCRC
                      	
  2853  E5            		push	hl
  2854  CD3C0E        		call	CHR2ATT
  2857  71            		ld	(hl),c
  2858  E1            		pop	hl
                      	
  2859  70            		ld	(hl),b
  285A  47            		ld	b,a
  285B  23            		inc	hl
  285C  CD3314        		call	CHKLINE
  285F  28EF          		jr	z,INSLP
                      	
  2861  78            		ld	a,b
  2862  FE20          		cp	' '
  2864  202B          		jr	nz,INSSCRL
  2866  F1            		pop	af
  2867  C9            		ret
                      	
  2868                	NOINSTBL:
  2868  020305060B0C0D		db	02h, 03h, 05h, 06h, 0bh, 0ch, 0dh, 15h, 1ch, 1dh, 1eh, 1fh
                      	
                      	
                      	;clear screen using console parameters
                      	;destroy: af,bc,de,hl
  2874                	_CTLL:	ds	CTLL-_CTLL
  2874                		org	CTLL
                      	
  2874  CD4911        		call	CTLHOM
  2877  ED4BA4FD      		ld	bc,(FSTLIN)	;c=(FSTLIN),b=(LASTLIN)
  287B  78            		ld	a,b
  287C  91            		sub	c
  287D  3C            		inc	a
                      	
                      	;input: e=first line+1, a=last line-first line+1
  287E                	CLSMAIN2:
  287E  47            		ld	b,a
  287F                	CLSLP:
  287F  69            		ld	l,c
  2880  CDB811        		call	Y2AD
  2883  CDDA11        		call	DELLIN
  2886  69            		ld	l,c
  2887  78            		ld	a,b		;>0
  2888  CD5114        		call	SETLINE
  288B  0C            		inc	c
  288C  10F1          		djnz	CLSLP
                      	
  288E  C3B512        		jp	PRTFKEY
                      	
                      	
  2891                	INSSCRL:
  2891  E5            		push	hl
                      	
  2892  CD5011        		call	AD2XY
                      	
  2895  3AA5FD        		ld	a,(LASTLIN)
  2898  BD            		cp	l
  2899  381B          		jr	c,INSSCRLU
                      	
  289B  67            		ld	h,a		;(LASTLIN)
  289C  2837          		jr	z,NOSCRLD
  289E  CD3215        		call	SCRLDW
                      	
  28A1                	INSSCRLEND:
  28A1  4D            		ld	c,l		;;
  28A2  2D            		dec	l
  28A3  AF            		xor	a
                      	
  28A4                	INSSCRLEND2:
  28A4  CD5114        		call	SETLINE
  28A7  69            		ld	l,c		;;
  28A8  CDB811        		call	Y2AD
  28AB  CDDA11        		call	DELLIN
  28AE  E1            		pop	hl
                      	
  28AF  78            		ld	a,b
  28B0  CDCA14        		call	PRT12
  28B3  2B            		dec	hl
                      	
  28B4  F1            		pop	af
  28B5  C9            		ret
                      	
  28B6                	INSSCRLU:
  28B6  E1            		pop	hl
  28B7  11E0FF        		ld	de,0-COLUMNS
  28BA  CB54          		bit	2,h
  28BC  2802          		jr	z,INSSCRLU60
  28BE  1ED8          		ld	e,0-CLMN66	;d=0ffh
  28C0                	INSSCRLU60:
  28C0  19            		add	hl,de
  28C1  E5            		push	hl
                      	
  28C2  6F            		ld	l,a		;(LASTLIN)
  28C3  3E1E          		ld	a,1eh		;up
  28C5  CD7510        		call	PRTC
                      	
  28C8  3AA2FD        		ld	a,(CONSOL1)
  28CB  67            		ld	h,a
  28CC  95            		sub	l
  28CD  4D            		ld	c,l
  28CE  280B          		jr	z,NOSCRLU	;a=0
                      	
  28D0  CD2915        		call	SCRLUP
  28D3  18CC          		jr	INSSCRLEND
                      	
  28D5                	NOSCRLD:
  28D5  AF            		xor	a
  28D6  3259FE        		ld	(INPTXY),a	;scroll out
  28D9  18C6          		jr	INSSCRLEND
                      	
  28DB                	NOSCRLU:
                      	;	xor	a
  28DB  3259FE        		ld	(INPTXY),a	;scroll out
  28DE  3C            		inc	a
  28DF  18C3          		jr	INSSCRLEND2
                      	
                      	
                      	;get character from screen
                      	;input: hl
                      	;output: a
                      	;destroy: f
  28E1                	GETSCRC:
  28E1  CD9539        		call	CHKMOD
  28E4  CA2C66        		jp	z,GETSCRC66
  28E7  7E            		ld	a,(hl)
  28E8  25            		dec	h
  28E9  25            		dec	h
  28EA  CB76          		bit	6,(hl)
  28EC  2802          		jr	z,GETSCRCZ
  28EE  3E20          		ld	a,' '
  28F0                	GETSCRCZ:
  28F0  24            		inc	h
  28F1  24            		inc	h
  28F2  C9            		ret
                      	
                      	
                      	;screen input
                      	;output: hl=INPBUF-1, c-flag(1=stop)
                      	;destroy: af,bc,de
  28F3                	_INPTSCR:ds	INPTSCR-_INPTSCR
  28F9                		org	INPTSCR
                      	
  28F9  CD5529        		call	SCREDIT
  28FC  D8            		ret	c
  28FD  3E47          		ld	a,71
  28FF  1811          		jr	COPYIBF
                      	
                      	
                      	;one line input for keyboard
                      	;output: hl=INPBUF-1, c-flag(1=stop)
                      	;destroy: af,bc,de
  2901                	_INPT1:	ds	INPT1-_INPT1
  2905                		org	INPT1
                      	
  2905  212C01        		ld	hl,QMARK
  2908  CDCF30        		call	PUTS
  290B  CD5529        		call	SCREDIT
  290E  D8            		ret	c
  290F  3A5BFE        		ld	a,(INPTX)
                      	;	jr	COPYIBF
                      	
                      	;copy characters to input buffer from VRAM
                      	;input: a=length, hl=start position address
                      	;output: hl=INPBUF-1, c-flag=0
                      	;destroy: af,bc,de
  2912                	COPYIBF:
  2912  11DAFE        		ld	de,INPBUF
  2915  B7            		or	a
  2916  2813          		jr	z,CPIBFZ
  2918  4F            		ld	c,a
  2919  0647          		ld	b,71
  291B                	CPIBFLP1:
  291B  CDE128        		call	GETSCRC
  291E  FE20          		cp	20h
  2920  300F          		jr	nc,CPIBFNC
  2922  EB            		ex	de,hl
  2923  3614          		ld	(hl),14h
  2925  EB            		ex	de,hl
  2926  1006          		djnz	CPIBFGRP
  2928  AF            		xor	a
  2929  1813          		jr	CPIBFNZ
  292B                	CPIBFZ:
  292B  12            		ld	(de),a
  292C  1810          		jr	CPIBFNZ
  292E                	CPIBFGRP:
  292E  13            		inc	de
  292F  C630          		add	a,30h
                      	
  2931                	CPIBFNC:
  2931  12            		ld	(de),a
  2932  13            		inc	de
  2933  23            		inc	hl
  2934  CD3314        		call	CHKLINE
  2937  2005          		jr	nz,CPIBFNZ
  2939  0D            		dec	c
  293A  2802          		jr	z,CPIBFNZ
  293C  10DD          		djnz	CPIBFLP1
                      	
  293E                	CPIBFNZ:
  293E  2B            		dec	hl
  293F  CD5011        		call	AD2XY
  2942  CD6D11        		call	SETCSR
  2945  CD3927        		call	PUTNL
                      	
  2948                	CPIBFLP2:
  2948  1B            		dec	de
  2949  1A            		ld	a,(de)
  294A  FE20          		cp	' '
  294C  28FA          		jr	z,CPIBFLP2
  294E  13            		inc	de
  294F  AF            		xor	a
  2950  12            		ld	(de),a
                      	
  2951  21D9FE        		ld	hl,INPBUF-1
  2954  C9            		ret
                      	
                      	
                      	;screen edit
                      	;output: hl=start position address, c-flag(1=stop)
                      	;destroy: af,bc,de
  2955                	SCREDIT:
  2955  CD4C14        		call	CUTLINE
  2958  2AA8FD        		ld	hl,(CSRY)
  295B  225AFE        		ld	(INPTX-1),hl	;h=end position for INPUT command
  295E  2259FE        		ld	(INPTXY),hl	;initial position for INPUT command
  2961  3AABFD        		ld	a,(CSRAD+1)
  2964  325CFE        		ld	(INPTPAG),a	;page for INPUT command
                      	
  2967                	SEDLP:
  2967  CDC40F        		call	GETC
  296A  CD6F29        		call	SEDMAIN
  296D  18F8          		jr	SEDLP
                      	
                      	
                      	;screen edit main routine
  296F                	SEDMAIN:
  296F  FE20          		cp	20h
  2971  3836          		jr	c,SEDCTL
                      	
  2973  2A5DFE        		ld	hl,(INSMODE)
  2976  2C            		inc	l
  2977  2D            		dec	l
  2978  C43E28        		call	nz,INSERT
                      	
  297B                	SEDPRTC:
  297B  CD7510        		call	PRTC
                      	
  297E                	CHKIPOS2:
  297E  2AAAFD        		ld	hl,(CSRAD)
                      	
                      	;check cursor position for input command
                      	;input: hl=cursor address
                      	;output: hl=cursor XY, de=cursor address
                      	;destroy: af
  2981                	CHKIPOS:
  2981  54            		ld	d,h
  2982  5D            		ld	e,l
  2983  CD5011        		call	AD2XY
  2986  3A59FE        		ld	a,(INPTXY)	;y+1
  2989  BD            		cp	l
  298A  C0            		ret	nz
                      	
  298B  3A5BFE        		ld	a,(INPTX)
  298E  BC            		cp	h
  298F  3004          		jr	nc,IPOSNC
  2991  7C            		ld	a,h
  2992  325BFE        		ld	(INPTX),a
  2995                	IPOSNC:
  2995  3A5AFE        		ld	a,(INPTXY+1)	;x+1
  2998  BC            		cp	h
  2999  D8            		ret	c
                      	
  299A  3A5CFE        		ld	a,(INPTPAG)
  299D  AA            		xor	d
  299E  E6F0          		and	0f0h
  29A0  C0            		ret	nz
                      	
  29A1  2259FE        		ld	(INPTXY),hl
  29A4  C9            		ret
                      	
                      	
  29A5                	DELSTR:
  29A5  1D201D00      		db	1dh, ' ', 1dh, 00h
                      	
                      	
                      	;control code
  29A9                	SEDCTL:
  29A9  216828        		ld	hl,NOINSTBL
  29AC  010C00        		ld	bc,000ch
  29AF  EDB1          		cpir
  29B1  2004          		jr	nz,SEDCTLNZ
  29B3  215DFE        		ld	hl,INSMODE
  29B6  70            		ld	(hl),b		;b=0
  29B7                	SEDCTLNZ:
  29B7  2AAAFD        		ld	hl,(CSRAD)
                      	
  29BA  FE02          		cp	02h
  29BC  CA3F2A        		jp	z,SEDCTLB
  29BF  FE03          		cp	03h
  29C1  CAD92A        		jp	z,SEDSTOP
  29C4  FE05          		cp	05h
  29C6  2865          		jr	z,SEDCTLE
  29C8  FE06          		cp	06h
  29CA  CA612A        		jp	z,SEDCTLF
  29CD  FE07          		cp	07h
  29CF  CA3E2B        		jp	z,SEDBELL
  29D2  FE08          		cp	08h
  29D4  281E          		jr	z,SEDDEL
  29D6  FE09          		cp	09h
  29D8  CAB62A        		jp	z,SEDTAB
  29DB  FE0A          		cp	0ah
  29DD  CA8D2A        		jp	z,SEDCTLJ
  29E0  FE0D          		cp	0dh
  29E2  CAF52A        		jp	z,SEDRET
  29E5  FE15          		cp	15h
  29E7  283F          		jr	z,SEDCTLU
  29E9  FE12          		cp	12h
  29EB  208E          		jr	nz,SEDPRTC	;14h,1ch,1dh,1eh,1fh
                      	
  29ED                	SEDINS:
  29ED  215DFE        		ld	hl,INSMODE
  29F0  7E            		ld	a,(hl)
  29F1  2F            		cpl
  29F2  77            		ld	(hl),a
  29F3  C9            		ret
                      	
                      	
  29F4                	SEDDEL:
  29F4  CD3314        		call	CHKLINE
  29F7  EB            		ex	de,hl
  29F8  21A529        		ld	hl,DELSTR
  29FB  2802          		jr	z,SEDDELZ
  29FD  13            		inc	de
  29FE  23            		inc	hl
  29FF                	SEDDELZ:
  29FF  D5            		push	de		;
  2A00  CDCF30        		call	PUTS
  2A03  CD7E29        		call	CHKIPOS2
  2A06  D1            		pop	de		;
  2A07  62            		ld	h,d
  2A08  6B            		ld	l,e
  2A09  1B            		dec	de
  2A0A  CD0316        		call	CNVATT1
  2A0D  4F            		ld	c,a
  2A0E                	DELLP:
  2A0E  CDE128        		call	GETSCRC
  2A11  12            		ld	(de),a
                      	
  2A12  E5            		push	hl
  2A13  CD3C0E        		call	CHR2ATT
  2A16  71            		ld	(hl),c
  2A17  E1            		pop	hl
                      	
  2A18  13            		inc	de
  2A19  23            		inc	hl
                      	
  2A1A  CD3314        		call	CHKLINE
  2A1D  28EF          		jr	z,DELLP
                      	
  2A1F  2B            		dec	hl
  2A20  3620          		ld	(hl),' '
  2A22  CD3C0E        		call	CHR2ATT
  2A25  71            		ld	(hl),c
                      	
  2A26  C9            		ret
                      	
                      	
  2A27                	CTLULP:
  2A27  2B            		dec	hl
  2A28                	SEDCTLU:
  2A28  CD3314        		call	CHKLINE
  2A2B  28FA          		jr	z,CTLULP
                      	
  2A2D                	SEDCTLE:
  2A2D  E5            		push	hl
  2A2E  CD3C0E        		call	CHR2ATT
  2A31  CD0316        		call	CNVATT1
  2A34  77            		ld	(hl),a
  2A35  E1            		pop	hl
                      	
  2A36  3620          		ld	(hl),' '
  2A38  23            		inc	hl
  2A39  CD3314        		call	CHKLINE
  2A3C  28EF          		jr	z,SEDCTLE
  2A3E  C9            		ret
                      	
                      	
  2A3F                	SEDCTLB:
  2A3F  CD502A        		call	CHKCTLB
  2A42  30FB          		jr	nc,SEDCTLB
  2A44                	CTLBLP:
  2A44  CD502A        		call	CHKCTLB
  2A47  38FB          		jr	c,CTLBLP
  2A49  23            		inc	hl
  2A4A  CD8129        		call	CHKIPOS
  2A4D  C36D11        		jp	SETCSR
                      	
  2A50                	CHKCTLB:
  2A50  2B            		dec	hl
  2A51  54            		ld	d,h
  2A52  5D            		ld	e,l
  2A53  CD5011        		call	AD2XY
  2A56  3AA2FD        		ld	a,(CONSOL1)
  2A59  2C            		inc	l
  2A5A  BD            		cp	l
  2A5B  3821          		jr	c,CTLBC
  2A5D                	CTLBNC:
  2A5D  F1            		pop	af		;cancel return address
  2A5E  C34C11        		jp	CTLCR
                      	
                      	
  2A61                	SEDCTLF:
  2A61  CD722A        		call	CHKCTLF
  2A64  38FB          		jr	c,SEDCTLF
  2A66                	CTLFLP:
  2A66  CD722A        		call	CHKCTLF
  2A69  30FB          		jr	nc,CTLFLP
  2A6B  EB            		ex	de,hl
  2A6C  CD6D11        		call	SETCSR
  2A6F  C37E29        		jp	CHKIPOS2
                      	
  2A72                	CHKCTLF:
  2A72  23            		inc	hl
  2A73  54            		ld	d,h
  2A74  5D            		ld	e,l
  2A75  CD5011        		call	AD2XY
  2A78  3AA5FD        		ld	a,(LASTLIN)
  2A7B  BD            		cp	l
  2A7C  3807          		jr	c,CTLFC
  2A7E                	CTLBC:
  2A7E  EB            		ex	de,hl
  2A7F  CDE128        		call	GETSCRC
  2A82  C3CF0B        		jp	ALPNUM2
                      	
  2A85                	CTLFC:
  2A85  2AABFD        		ld	hl,(WIDTH-1)	;h=(WIDTH)
  2A88  6F            		ld	l,a
                      	
  2A89  F1            		pop	af		;cancel return address
  2A8A  C36D11        		jp	SETCSR
                      	
                      	
  2A8D                	SEDCTLJ:
  2A8D  3A5DFE        		ld	a,(INSMODE)
  2A90  B7            		or	a
  2A91  3E0A          		ld	a,0ah
  2A93  CA7B29        		jp	z,SEDPRTC
                      	
  2A96  2AA8FD        		ld	hl,(CSRY)	;l=y+1, h=x+1
  2A99  3AACFD        		ld	a,(WIDTH)
  2A9C  3C            		inc	a
  2A9D  94            		sub	h
  2A9E  47            		ld	b,a
  2A9F                	CTLJLP:
  2A9F  C5            		push	bc
  2AA0  3E20          		ld	a,' '
  2AA2  CD3E28        		call	INSERT
  2AA5  2AAAFD        		ld	hl,(CSRAD)
  2AA8  77            		ld	(hl),a
  2AA9  C1            		pop	bc
  2AAA  10F3          		djnz	CTLJLP
                      	
  2AAC  CD5011        		call	AD2XY
  2AAF  CD6D11        		call	SETCSR
  2AB2  7C            		ld	a,h		;>0
  2AB3  C35114        		jp	SETLINE
                      	
                      	
  2AB6                	SEDTAB:
  2AB6  3AACFD        		ld	a,(WIDTH)
  2AB9  D607          		sub	07h
  2ABB  47            		ld	b,a
  2ABC  3AA9FD        		ld	a,(CSRX)
  2ABF  90            		sub	b
  2AC0  DE07          		sbc	a,07h
  2AC2  C8            		ret	z
  2AC3  2F            		cpl
  2AC4  E607          		and	07h
  2AC6  3C            		inc	a
  2AC7  47            		ld	b,a
  2AC8  3A5DFE        		ld	a,(INSMODE)
  2ACB  E604          		and	04h
  2ACD  C61C          		add	a,1ch		;1ch(right) or 20h(space)
  2ACF  4F            		ld	c,a
  2AD0                	SEDTABLP:
  2AD0  C5            		push	bc
  2AD1  79            		ld	a,c
  2AD2  CD6F29        		call	SEDMAIN
  2AD5  C1            		pop	bc
  2AD6  10F8          		djnz	SEDTABLP
  2AD8  C9            		ret
                      	
                      	
  2AD9                	SEDSTOP:
  2AD9  E1            		pop	hl		;cancel return address
  2ADA  2AA8FD        		ld	hl,(CSRY)
  2ADD                	STOPLP:
  2ADD  2C            		inc	l
  2ADE  CD4214        		call	CHKLINE3
  2AE1  28FA          		jr	z,STOPLP
  2AE3  2D            		dec	l
  2AE4  CD6D11        		call	SETCSR
  2AE7  CD3927        		call	PUTNL
                      	
  2AEA  21DAFE        		ld	hl,INPBUF
  2AED  AF            		xor	a
  2AEE  3218FA        		ld	(STOPFLG),a
  2AF1  77            		ld	(hl),a
  2AF2  2B            		dec	hl
  2AF3  37            		scf
  2AF4  C9            		ret
                      	
                      	
  2AF5                	SEDRET:
  2AF5  F1            		pop	af		;cancel return address
  2AF6  E5            		push	hl		;(CSRAD)
                      	
  2AF7  3A5CFE        		ld	a,(INPTPAG)
  2AFA  AC            		xor	h		;(CSRAD+1)
  2AFB  E6F0          		and	0f0h
  2AFD  2019          		jr	nz,SETLENMAX	;if other page
                      	
  2AFF  3AA8FD        		ld	a,(CSRY)
  2B02  2A59FE        		ld	hl,(INPTXY)
  2B05  BD            		cp	l		;(INPTXY)
  2B06  2010          		jr	nz,SETLENMAX	;if other line
                      	
  2B08  CD4214        		call	CHKLINE3
  2B0B  280B          		jr	z,SETLENMAX	;if connected to previous line
  2B0D  CD4514        		call	CHKLINE4
  2B10  2806          		jr	z,SETLENMAX	;if connected to next line
                      	
  2B12  3A5BFE        		ld	a,(INPTX)	;end position
  2B15  94            		sub	h		;(INPTXY+1)
  2B16  1802          		jr	SETLEN
                      	
  2B18                	SETLENMAX:
  2B18  3E47          		ld	a,71
  2B1A                	SETLEN:
  2B1A  325BFE        		ld	(INPTX),a	;length
  2B1D  E1            		pop	hl		;(CSRAD)
                      	
  2B1E                	SRETLP:
  2B1E  CD3314        		call	CHKLINE
  2B21  2B            		dec	hl
  2B22  28FA          		jr	z,SRETLP
                      	
  2B24  23            		inc	hl
  2B25  54            		ld	d,h		;
  2B26  CD5011        		call	AD2XY
  2B29  3A5CFE        		ld	a,(INPTPAG)
  2B2C  AA            		xor	d		;
  2B2D  E6F0          		and	0f0h
  2B2F  2009          		jr	nz,SRETNZ	;if different page
                      	
  2B31  ED5B59FE      		ld	de,(INPTXY)	;initial position
  2B35  7D            		ld	a,l
  2B36  BB            		cp	e
  2B37  2001          		jr	nz,SRETNZ	;if different line
  2B39  EB            		ex	de,hl		;if same line
  2B3A                	SRETNZ:
  2B3A  B7            		or	a		;reset c-flag
  2B3B  C3CD11        		jp	XY2AD
                      	
                      	
  2B3E                	SEDBELL:
  2B3E  3A5DFE        		ld	a,(INSMODE)
  2B41  B7            		or	a
  2B42  CACD1B        		jp	z,BELL
  2B45  3E20          		ld	a,' '
  2B47  C36F29        		jp	SEDMAIN
                      	
                      	
                      	;x=x+-1
                      	;input: b(bit7=0:increment, bit7=1:decrement), (GRPX3)
                      	;output: hl,(GRPX3)=X, z-flag(1=overflow)
                      	;destroy: af,de
  2B4A                	INCGX:
  2B4A  2AB1FE        		ld	hl,(GRPX3)
  2B4D  CB78          		bit	7,b
  2B4F  2005          		jr	nz,INCGXNZ
  2B51  23            		inc	hl
  2B52  7C            		ld	a,h
  2B53  B5            		or	l
  2B54  1803          		jr	INCGXEND
  2B56                	INCGXNZ:
  2B56  7C            		ld	a,h
  2B57  B5            		or	l
  2B58  2B            		dec	hl
  2B59                	INCGXEND:
  2B59  22B1FE        		ld	(GRPX3),hl
  2B5C  C9            		ret
                      	
                      	
                      	;y=y+1
                      	;input: (GRPY3)
                      	;output: de=(GRPY3)
                      	;destroy: af
  2B5D                	INCGY:
  2B5D  ED5BB3FE      		ld	de,(GRPY3)
  2B61  13            		inc	de
  2B62  ED53B3FE      		ld	(GRPY3),de
  2B66  C9            		ret
                      	
                      	
                      	;error-=|dx|*2
                      	;input: bc=dx, hl=error
                      	;output: hl=error
                      	;destroy: af
  2B67                	SUB2DX:
  2B67  78            		ld	a,b
  2B68  07            		rlca
  2B69  3805          		jr	c,SUB2DXNZ
                      	;dx>0
                      	;	or	a
  2B6B  ED42          		sbc	hl,bc
  2B6D  ED42          		sbc	hl,bc
  2B6F  C9            		ret
                      	
                      	;dx<0
  2B70                	SUB2DXNZ:
  2B70  09            		add	hl,bc
  2B71  09            		add	hl,bc
  2B72  C9            		ret
                      	
                      	
                      	;x1 <-> x2, y1 <-> y2
                      	;input: bc=x1, de=y1, (GRPX2)=x2, (GRPY2)=y2
                      	;output: (GRPX2)=larger x,(GRPY2)=larger y,(GRPX3)=smaller x,(GRPY3)=smaller y
                      	;destroy: bc,de,hl
  2B73                	SORTXY:
  2B73  F5            		push	af
  2B74  B7            		or	a
  2B75  CDC52B        		call	CHKGXY23
  2B78  3807          		jr	c,SORTXOK
  2B7A  22AFFE        		ld	(GRPY2),hl
  2B7D  ED53B3FE      		ld	(GRPY3),de
  2B81                	SORTXOK:
  2B81  2AADFE        		ld	hl,(GRPX2)
  2B84  ED5BB1FE      		ld	de,(GRPX3)
  2B88  E7            		rst	CPHLDE
  2B89  3817          		jr	c,SORTX
  2B8B  F1            		pop	af
  2B8C  C9            		ret
                      	
                      	
                      	;(x1,y1) <-> (x2,y2)
                      	;input: bc=x1, de=y1, (GRPX2)=x2, (GRPY2)=y2
                      	;output: (GRPX2),(GRPY2)=larger y with x, (GRPX3),(GRPY3)=small y with x
                      	;destroy: bc,de,hl
  2B8D                	SORTY:
  2B8D  F5            		push	af
  2B8E  37            		scf
  2B8F  CDC52B        		call	CHKGXY23
  2B92  3815          		jr	c,NOSORT
  2B94  22AFFE        		ld	(GRPY2),hl
  2B97  ED53B3FE      		ld	(GRPY3),de
  2B9B  2AADFE        		ld	hl,(GRPX2)
  2B9E  ED5BB1FE      		ld	de,(GRPX3)
  2BA2                	SORTX:
  2BA2  ED53ADFE      		ld	(GRPX2),de
  2BA6  22B1FE        		ld	(GRPX3),hl
  2BA9                	NOSORT:
  2BA9  F1            		pop	af
  2BAA  C9            		ret
                      	
                      	
                      	;sort XY and put bc,de,hl
                      	;output: bc=smaller x, de=smaller y, hl=larger y
                      	;destroy: none
  2BAB                	SORTXY2:
  2BAB  CD732B        		call	SORTXY
  2BAE  ED4BB1FE      		ld	bc,(GRPX3)	;smaller x, b=0
  2BB2  ED5BB3FE      		ld	de,(GRPY3)	;smaller y
  2BB6  2AAFFE        		ld	hl,(GRPY2)	;larger y
  2BB9  C9            		ret
                      	
                      	
                      	;push (GRPX2),(GRPY2)
                      	;destroy: af,hl
  2BBA                	PUSHGXY:
  2BBA  F1            		pop	af		;return address
  2BBB  2AADFE        		ld	hl,(GRPX2)
  2BBE  E5            		push	hl
  2BBF  2AAFFE        		ld	hl,(GRPY2)
  2BC2  E5            		push	hl
  2BC3  F5            		push	af		;return address
  2BC4  C9            		ret
                      	
                      	
                      	;input: bc=x1, de=y1, (GRPX2)=x2, (GRPY2)=y2,
                      	;       c-flag(1=convert to screen mode cordinate)
                      	;output: bc=(GRPX2)=x2, de=(GRPY2)=y2, hl=(GRPY3)=y1, (GRPX3)=x1, z,c(y1>y2?)
                      	;destroy: af
  2BC5                	CHKGXY23:
  2BC5  F5            		push	af
  2BC6  DC7815        		call	c,GXY2SXY
  2BC9  F1            		pop	af
  2BCA  F5            		push	af
  2BCB  D4F42B        		call	nc,CHKGXY
  2BCE  F1            		pop	af
  2BCF  ED43B1FE      		ld	(GRPX3),bc
  2BD3  ED53B3FE      		ld	(GRPY3),de
  2BD7  ED4BADFE      		ld	bc,(GRPX2)
  2BDB  ED5BAFFE      		ld	de,(GRPY2)
  2BDF  F5            		push	af
  2BE0  DC7815        		call	c,GXY2SXY
  2BE3  F1            		pop	af
  2BE4  D4F42B        		call	nc,CHKGXY
  2BE7  ED43ADFE      		ld	(GRPX2),bc
  2BEB  ED53AFFE      		ld	(GRPY2),de
                      	
  2BEF  2AB3FE        		ld	hl,(GRPY3)
  2BF2  E7            		rst	CPHLDE
  2BF3  C9            		ret
                      	
                      	
                      	;check and round graphic coordinates (x,y)
                      	;input: bc=x, de=y
                      	;output: bc=x, de=y
                      	;destroy: af,hl
  2BF4                	CHKGXY:
  2BF4  CD9539        		call	CHKMOD
  2BF7  CA9A6E        		jp	z,CHKGXY66
  2BFA  78            		ld	a,b
  2BFB  B7            		or	a
  2BFC  2802          		jr	z,CHKMNS
  2BFE  0EFF          		ld	c,0ffh
  2C00                	CHKMNS:
  2C00  B2            		or	d
  2C01  FACB03        		jp	m,FCERR
                      	
  2C04  3A92FD        		ld	a,(SCREEN1)
  2C07  47            		ld	b,a		;
  2C08  0F            		rrca
  2C09  2F            		cpl
  2C0A  21A6FD        		ld	hl,CONSOL3
  2C0D  A6            		and	(hl)
  2C0E  0F            		rrca
  2C0F  21BF00        		ld	hl,191
  2C12  3003          		jr	nc,NOFKEY
  2C14  21B300        		ld	hl,191-12
  2C17                	NOFKEY:
  2C17  E7            		rst	CPHLDE
  2C18  3001          		jr	nc,CHKYOK
  2C1A  EB            		ex	de,hl
  2C1B                	CHKYOK:
  2C1B  78            		ld	a,b		;
  2C1C  3D            		dec	a
  2C1D  280D          		jr	z,ROUNDY2
  2C1F  F2302C        		jp	p,ROUNDX	;screen mode 3,4
                      	
                      	;round off y for screen mode 1
  2C22                	ROUNDY1:
  2C22  CDA015        		call	DIV12
  2C25  7B            		ld	a,e
  2C26  87            		add	a,a		;*2
  2C27  83            		add	a,e		;*3
  2C28  87            		add	a,a		;*6
  2C29  87            		add	a,a		;*12
  2C2A  1803          		jr	ROUNDY2END
                      	
                      	;round off y for screen mode 2
  2C2C                	ROUNDY2:
  2C2C  7B            		ld	a,e
  2C2D  E6FC          		and	0fch
  2C2F                	ROUNDY2END:
  2C2F  5F            		ld	e,a
                      	
  2C30                	ROUNDX:
  2C30  04            		inc	b		;
  2C31  3EF0          		ld	a,0f0h
  2C33                	ROUNDXLP:
  2C33  37            		scf
  2C34  1F            		rra			;f8,fc,fe,ff
  2C35  10FC          		djnz	ROUNDXLP
                      	
  2C37  A1            		and	c
  2C38  4F            		ld	c,a		;b=0
  2C39  C9            		ret
                      	
                      	
                      	;set color for pset/preset command
  2C3A                	PSCOLR:
  2C3A  C5            		push	bc		;x
  2C3B  D5            		push	de		;y
  2C3C  47            		ld	b,a		;color
  2C3D  7E            		ld	a,(hl)
  2C3E  FE2C          		cp	','
  2C40  78            		ld	a,b		;color
  2C41  CCE30D        		call	z,INT1INC
  2C44  D1            		pop	de		;y
  2C45  C1            		pop	bc		;x
  2C46  C3C015        		jp	SETATT
                      	
                      	
                      	;screen mode 2
  2C49                	PSET2:
  2C49  25            		dec	h
  2C4A  25            		dec	h
  2C4B  3AACFE        		ld	a,(ATTDAT)
  2C4E  5E            		ld	e,(hl)		;
  2C4F  B7            		or	a
  2C50  2004          		jr	nz,PS2NZ1
  2C52  CB73          		bit	6,e		;
  2C54  2004          		jr	nz,PS2NZ2
  2C56                	PS2NZ1:
  2C56  CD680E        		call	GETSPA2NZ
  2C59  77            		ld	(hl),a
  2C5A                	PS2NZ2:
  2C5A  24            		inc	h
  2C5B  24            		inc	h
  2C5C  CB73          		bit	6,e		;
  2C5E  2004          		jr	nz,PS2SEMI	;semi-graphic mode?
  2C60  CD1D16        		call	GETSP
  2C63  77            		ld	(hl),a
                      	
  2C64                	PS2SEMI:
  2C64  7E            		ld	a,(hl)
  2C65  E63F          		and	3fh
  2C67  B2            		or	d
  2C68  4F            		ld	c,a		;;
                      	
  2C69  3AACFE        		ld	a,(ATTDAT)
  2C6C  B7            		or	a
  2C6D  2809          		jr	z,PSET2C0
  2C6F  3D            		dec	a
  2C70  E603          		and	03h
  2C72  0F            		rrca
  2C73  0F            		rrca
  2C74  81            		add	a,c		;;
  2C75  77            		ld	(hl),a
  2C76  E1            		pop	hl
  2C77  C9            		ret
                      	
  2C78                	PSET2C0:
  2C78  7A            		ld	a,d
  2C79  2F            		cpl
  2C7A  A6            		and	(hl)
  2C7B  77            		ld	(hl),a
  2C7C  E1            		pop	hl
  2C7D  C9            		ret
                      	
                      	
                      	;continued from BFG
  2C7E                	BFG2:
  2C7E  3AB1FE        		ld	a,(GRPX3)	;smaller x
  2C81  E6F8          		and	0f8h
  2C83  47            		ld	b,a
  2C84  3AADFE        		ld	a,(GRPX2)	;larger x
  2C87  E6F8          		and	0f8h
  2C89  90            		sub	b		;
  2C8A  0F            		rrca
  2C8B  0F            		rrca
  2C8C  0F            		rrca			;int(larger/8)-int(smaller/8)
  2C8D  47            		ld	b,a		;bytes
  2C8E  2003          		jr	nz,BFGLP3	;a=0?
  2C90  7A            		ld	a,d
  2C91  A3            		and	e
  2C92  57            		ld	d,a
                      	
  2C93                	BFGLP3:
                      	;left part
  2C93  C5            		push	bc
  2C94  3AACFE        		ld	a,(ATTDAT)
  2C97  AE            		xor	(hl)
  2C98  A2            		and	d		;d=left part mask
  2C99  AE            		xor	(hl)
  2C9A  77            		ld	(hl),a
                      	
  2C9B  05            		dec	b		;
  2C9C  FAAF2C        		jp	m,BFGNEXT
                      	
                      	;middle part
  2C9F  E5            		push	hl		;VRAM address
  2CA0  3AACFE        		ld	a,(ATTDAT)
  2CA3  2804          		jr	z,BFGRGT	;
  2CA5                	BFGLP4:
  2CA5  23            		inc	hl
  2CA6  77            		ld	(hl),a
  2CA7  10FC          		djnz	BFGLP4
                      	
                      	;right part
  2CA9                	BFGRGT:
  2CA9  23            		inc	hl
  2CAA  AE            		xor	(hl)
  2CAB  A3            		and	e		;e=right part mask
  2CAC  AE            		xor	(hl)
  2CAD  77            		ld	(hl),a
  2CAE  E1            		pop	hl		;VRAM address
                      	
  2CAF                	BFGNEXT:
  2CAF  012000        		ld	bc,0020h
  2CB2  09            		add	hl,bc
  2CB3  C1            		pop	bc
  2CB4  0D            		dec	c
  2CB5  20DC          		jr	nz,BFGLP3
  2CB7  C3BA2E        		jp	LINEEND2
                      	
                      	
                      	;if |dy/dx|<=1
  2CBA                	SLOPEX:
  2CBA  23            		inc	hl
                      	;	or	a
  2CBB  ED52          		sbc	hl,de		;error=-|dx|
  2CBD  D9            		exx			;bc'=dx, de'=dy, hl'=error
  2CBE                	LINELP2:
  2CBE  ED4BB1FE      		ld	bc,(GRPX3)
  2CC2  ED5BB3FE      		ld	de,(GRPY3)
  2CC6  CD4A2D        		call	PSETSXY
                      	
  2CC9  D9            		exx
  2CCA  19            		add	hl,de		;error+=dy
  2CCB  19            		add	hl,de		;error+=dy
  2CCC  CB7C          		bit	7,h
  2CCE  2008          		jr	nz,ERRMNS
                      	
                      	;if error>=0
  2CD0  CD672B        		call	SUB2DX		;error-=|dx|*2
  2CD3  D9            		exx
  2CD4  CD5D2B        		call	INCGY		;increment Y
  2CD7  D9            		exx
                      	
  2CD8                	ERRMNS:
  2CD8  78            		ld	a,b
  2CD9  D9            		exx
  2CDA  47            		ld	b,a
  2CDB  CD4A2B        		call	INCGX		;increment or decrement X
  2CDE  CAB82E        		jp	z,LINEEND
                      	
  2CE1  EB            		ex	de,hl		;de=X
  2CE2  2AADFE        		ld	hl,(GRPX2)
  2CE5  D9            		exx
  2CE6  CB78          		bit	7,b
  2CE8  D9            		exx
  2CE9  2801          		jr	z,LINEZ
  2CEB  EB            		ex	de,hl		;if dx<0
  2CEC                	LINEZ:
  2CEC  E7            		rst	CPHLDE
  2CED  30CF          		jr	nc,LINELP2
  2CEF  C3B82E        		jp	LINEEND
                      	
                      	
                      	;get [step](x,y) cordinate
                      	;input: hl=program address
                      	;output: bc=graphic X, de=graphic Y,
                      	;	 (GRPX1)=(GRPX2),(GRPY1)=(GRPY2)=0-32767
                      	;	 a=next data, hl=next data address, z(, or not)
                      	;destroy: f
  2CF2                	GETGXY:
  2CF2  CD593F        		call	SKIPSP
  2CF5  FEC9          		cp	STEP_
  2CF7  F5            		push	af		;STEP?
  2CF8  2001          		jr	nz,GETGXYNZ
  2CFA  23            		inc	hl
  2CFB                	GETGXYNZ:
  2CFB  CDB50B        		call	CHKLPAR
  2CFE  CD1D0E        		call	INTARG2		;bc,de
  2D01  CDC40B        		call	CHKRPAR
  2D04  F1            		pop	af		;STEP?
  2D05  CD132D        		call	SETGXY
  2D08  CD593F        		call	SKIPSP
  2D0B  FE2C          		cp	','
  2D0D  C9            		ret
                      	
                      	
                      	;input: bc=x, de=y, z(0=absolute, 1=relative)
                      	;output: bc=x, de=y, (GRPX1)=(GRPX2)=x, (GRPY1)=(GRPY2)=y
                      	;destroy: f
  2D0E                	_SETGXY:ds	SETGXY-_SETGXY
  2D13                		org	SETGXY
                      	
  2D13  E5            		push	hl
  2D14  200B          		jr	nz,SETGXYNZ
  2D16  2AAEFD        		ld	hl,(GRPX1)
  2D19  09            		add	hl,bc
  2D1A  44            		ld	b,h
  2D1B  4D            		ld	c,l
  2D1C  2AB0FD        		ld	hl,(GRPY1)
  2D1F  19            		add	hl,de
  2D20  EB            		ex	de,hl
  2D21                	SETGXYNZ:
  2D21  ED43AEFD      		ld	(GRPX1),bc
  2D25  ED43ADFE      		ld	(GRPX2),bc
  2D29  ED53B0FD      		ld	(GRPY1),de
  2D2D  ED53AFFE      		ld	(GRPY2),de
  2D31  E1            		pop	hl
  2D32  C9            		ret
                      	
                      	
                      	;PRESET command
  2D33                	_C_PRST:ds	C_PRST-_C_PRST
  2D37                		org	C_PRST
                      	
  2D37  3A94FD        		ld	a,(COLOR2)
  2D3A  1803          		jr	PSETRST
                      	
                      	;PSET command
  2D3C                	_C_PSET:ds	C_PSET-_C_PSET
  2D3C                		org	C_PSET
                      	
  2D3C  3A93FD        		ld	a,(COLOR1)
                      	
                      	;PSET and PRESET
  2D3F                	PSETRST:
  2D3F  F5            		push	af
  2D40  CDF22C        		call	GETGXY
  2D43  F1            		pop	af
  2D44  CD3A2C        		call	PSCOLR
                      	;	jp	PSET
                      	
                      	
                      	;input: bc=x1, de=y1, (ATTDAT)=attribute
                      	;destroy: af,bc,de
  2D47                	_PSET:	ds	PSET-_PSET
  2D47                		org	PSET
                      	
  2D47  CD7815        		call	GXY2SXY
                      	
  2D4A                	PSETSXY:
  2D4A  CD9539        		call	CHKMOD
  2D4D  CA596E        		jp	z,PSET66
                      	
  2D50  E5            		push	hl
  2D51  CDBA15        		call	SXY2AD
  2D54  57            		ld	d,a
  2D55  3A92FD        		ld	a,(SCREEN1)
  2D58  3D            		dec	a
  2D59  CA492C        		jp	z,PSET2
  2D5C  3AACFE        		ld	a,(ATTDAT)
  2D5F  FA652D        		jp	m,PSET1
                      	
                      	;screen mode 3,4
  2D62                	PSET34:
  2D62  AE            		xor	(hl)
  2D63  A2            		and	d
  2D64  AE            		xor	(hl)
  2D65                	PSET1:
  2D65  77            		ld	(hl),a
  2D66  E1            		pop	hl
  2D67  C9            		ret
                      	
                      	
                      	;POINT() function
  2D68                	F_POIN:
  2D68  CDF22C        		call	GETGXY
  2D6B  CD733E        		call	POINT
  2D6E  C37D0C        		jp	I1TOFA
                      	
                      	
                      	;prepare for line command
  2D71                	PRELINE:
  2D71  CDC62D        		call	SKIPAT
  2D74  FECB          		cp	MINUS_
  2D76  ED4BAEFD      		ld	bc,(GRPX1)
  2D7A  ED5BB0FD      		ld	de,(GRPY1)
  2D7E  2808          		jr	z,LINE2P
                      	
  2D80                	LINE1P:
  2D80  CDF22C        		call	GETGXY
  2D83  FECB          		cp	MINUS_
  2D85  C2C203        		jp	nz,SNERR
                      	
  2D88                	LINE2P:
  2D88  C5            		push	bc		;x1
  2D89  D5            		push	de		;y1
                      	
  2D8A  23            		inc	hl
  2D8B  CDF22C        		call	GETGXY
  2D8E  ED43ADFE      		ld	(GRPX2),bc	;x2
  2D92  ED53AFFE      		ld	(GRPY2),de	;y2
  2D96  3A93FD        		ld	a,(COLOR1)
  2D99  200F          		jr	nz,NOTBOX
  2D9B  5F            		ld	e,a
  2D9C  D7            		rst	ANADAT
  2D9D  FE2C          		cp	','
  2D9F  280C          		jr	z,CHKBF
                      	
                      	;color
  2DA1  CDE40D        		call	INT1ARG
  2DA4  7E            		ld	a,(hl)
  2DA5  FE2C          		cp	','
  2DA7  2804          		jr	z,CHKBF
  2DA9  7B            		ld	a,e
                      	
  2DAA                	NOTBOX:
  2DAA  B7            		or	a		;reset c-flag
  2DAB  1811          		jr	PRELEND
                      	
                      	;check 'B' and 'F'
  2DAD                	CHKBF:
  2DAD  D7            		rst	ANADAT
  2DAE  FE42          		cp	'B'
  2DB0  C2C203        		jp	nz,SNERR
                      	
  2DB3  D7            		rst	ANADAT
  2DB4  FE46          		cp	'F'
  2DB6  2001          		jr	nz,BOX
  2DB8  23            		inc	hl		;for box fill
  2DB9                	BOX:
  2DB9  224EFF        		ld	(PROGAD),hl
  2DBC  7B            		ld	a,e
  2DBD  37            		scf
                      	
  2DBE                	PRELEND:
  2DBE  D1            		pop	de		;y1
  2DBF  C1            		pop	bc		;x1
                      	
  2DC0  F5            		push	af
  2DC1  CDC015        		call	SETATT
  2DC4  F1            		pop	af
  2DC5  C9            		ret
                      	
                      	
                      	;skip @
  2DC6                	SKIPAT:
  2DC6  CD593F        		call	SKIPSP
  2DC9  FE40          		cp	'@'
  2DCB  C0            		ret	nz
  2DCC  C3583F        		jp	SKIPSPINC
                      	
                      	
                      	;LINE command
  2DCF                	C_LINE:
  2DCF  CD712D        		call	PRELINE
  2DD2  304B          		jr	nc,LINE
  2DD4  205F          		jr	nz,LINEB
  2DD6  180C          		jr	LINEBF
                      	
                      	
                      	;input: bc=x1, de=y1, (GRPX2)=x2, (GRPY2)=y2, (ATTDAT)=attribute
                      	;destroy: af,bc,de
  2DD8                	_LINEBF:ds	LINEBF-_LINEBF
  2DE4                		org	LINEBF
                      	
  2DE4  E5            		push	hl
  2DE5  CDBA2B        		call	PUSHGXY
  2DE8  CDAB2B        		call	SORTXY2
                      	
  2DEB  3A92FD        		ld	a,(SCREEN1)
  2DEE  FE02          		cp	02h
  2DF0  3868          		jr	c,BF12
                      	
                      	;line bf (graphic mode)
  2DF2                	BFG:
  2DF2  3AADFE        		ld	a,(GRPX2)	;larger x
  2DF5  2001          		jr	nz,BF4
  2DF7  3C            		inc	a		;screen mode 3
  2DF8                	BF4:
  2DF8  E607          		and	07h
  2DFA  3C            		inc	a
  2DFB  47            		ld	b,a
  2DFC  AF            		xor	a
  2DFD                	BFGLP1:
  2DFD  37            		scf
  2DFE  1F            		rra			;right part mask
  2DFF  10FC          		djnz	BFGLP1
                      	
                      	;	or	a
  2E01  ED52          		sbc	hl,de
  2E03  2C            		inc	l
  2E04  E5            		push	hl		;l=lines
                      	
  2E05  6F            		ld	l,a		;right part mask
                      	
  2E06  79            		ld	a,c		;smaller x
  2E07  E607          		and	07h
  2E09  47            		ld	b,a
  2E0A  3EFF          		ld	a,0ffh
  2E0C  2804          		jr	z,BFGZ
  2E0E                	BFGLP2:
  2E0E  CB3F          		srl	a
  2E10  10FC          		djnz	BFGLP2
  2E12                	BFGZ:
  2E12  67            		ld	h,a		;left part mask
                      	
  2E13  E5            		push	hl
  2E14  CDB215        		call	GXY2AD		;b=0
  2E17  D1            		pop	de		;d=left part mask, e=right part mask
  2E18  C1            		pop	bc		;c=lines
                      	
  2E19  C37E2C        		jp	BFG2
                      	
                      	
                      	;by Bresenham's line algorithm
                      	;input: bc=x1, de=y1, (GRPX2)=x2, (GRPY2)=y2, (ATTDAT)=attribute
                      	;destroy: af
  2E1C                	_LINE:	ds	LINE-_LINE
  2E1F                		org	LINE
                      	
  2E1F  E5            		push	hl
  2E20  CDBA2B        		call	PUSHGXY
  2E23  D5            		push	de
  2E24  C5            		push	bc
                      	
  2E25  CD8D2B        		call	SORTY
  2E28  ED4BB3FE      		ld	bc,(GRPY3)	;start (smaller y)
  2E2C  2AAFFE        		ld	hl,(GRPY2)	;end (larger y)
  2E2F  B7            		or	a
  2E30  ED42          		sbc	hl,bc		;dy
  2E32  1841          		jr	LINE2
                      	
                      	
                      	;input: bc=x1, de=y1, (GRPX2)=x2, (GRPY2)=y2, (ATTDAT)=attribute
                      	;destroy: af,bc,de
  2E34                	_LINEB:	ds	LINEB-_LINEB
  2E35                		org	LINEB
                      	
  2E35  E5            		push	hl
  2E36  CDBA2B        		call	PUSHGXY
  2E39  CDAB2B        		call	SORTXY2
  2E3C  EB            		ex	de,hl
                      	
                      	;down-up-right-left
  2E3D  C5            		push	bc
  2E3E  CD1F2E        		call	LINE
  2E41  22AFFE        		ld	(GRPY2),hl	;smaller y
  2E44  EB            		ex	de,hl
  2E45  CD1F2E        		call	LINE
  2E48  ED4BADFE      		ld	bc,(GRPX2)	;larger x
  2E4C  EB            		ex	de,hl		;larger y
  2E4D  CD1F2E        		call	LINE
  2E50  C1            		pop	bc		;smaller x
  2E51  ED43ADFE      		ld	(GRPX2),bc
  2E55  CD1F2E        		call	LINE
  2E58  1860          		jr	LINEEND2
                      	
                      	
                      	;line bf (screen mode 1,2)
                      	;hl=larger y, a=screen mode-1
  2E5A                	BF12:
  2E5A  ED53AFFE      		ld	(GRPY2),de
  2E5E  CD1F2E        		call	LINE
  2E61  E7            		rst	CPHLDE
  2E62  2856          		jr	z,LINEEND2
                      	
  2E64  E5            		push	hl
  2E65  3A92FD        		ld	a,(SCREEN1)
  2E68  87            		add	a,a
  2E69  87            		add	a,a
  2E6A  210C00        		ld	hl,000ch
  2E6D  2801          		jr	z,BF12Z
  2E6F  6F            		ld	l,a
  2E70                	BF12Z:
  2E70  19            		add	hl,de		;+12 or +4
  2E71  EB            		ex	de,hl
  2E72  E1            		pop	hl
  2E73  18E5          		jr	BF12
                      	
                      	
  2E75                	LINE2:
  2E75  EB            		ex	de,hl
  2E76  ED4BB1FE      		ld	bc,(GRPX3)	;start (x)
  2E7A  2AADFE        		ld	hl,(GRPX2)	;end (x)
  2E7D  B7            		or	a
  2E7E  ED42          		sbc	hl,bc		;dx
  2E80  44            		ld	b,h
  2E81  4D            		ld	c,l
                      	
  2E82  3806          		jr	c,DXMNS		;if dx<0
                      	;if dx>=0, hl=-hl=-|dx|
  2E84  AF            		xor	a
  2E85  95            		sub	l
  2E86  6F            		ld	l,a
  2E87  9C            		sbc	a,h
  2E88  95            		sub	l
  2E89  67            		ld	h,a
  2E8A                	DXMNS:
  2E8A  2B            		dec	hl
  2E8B  19            		add	hl,de
  2E8C  D2BA2C        		jp	nc,SLOPEX	;if |dy/dx|<=1
                      	
                      	;if |dy/dx|>1
  2E8F  62            		ld	h,d		;error=dy
  2E90  6B            		ld	l,e
                      	
  2E91  D9            		exx			;bc'=dx, de'=dy, hl'=error
  2E92  ED5BB3FE      		ld	de,(GRPY3)
  2E96                	LINELP1:
  2E96  ED4BB1FE      		ld	bc,(GRPX3)
  2E9A  CD4A2D        		call	PSETSXY
                      	
  2E9D  D9            		exx
  2E9E  CD672B        		call	SUB2DX		;error-=|dx|*2
  2EA1  CB7C          		bit	7,h
  2EA3  2809          		jr	z,ERRPLS
                      	
                      	;if error<0
  2EA5  19            		add	hl,de		;error+=dy
  2EA6  19            		add	hl,de		;error+=dy
  2EA7  78            		ld	a,b
  2EA8  D9            		exx
  2EA9  47            		ld	b,a
  2EAA  CD4A2B        		call	INCGX		;increment or decrement X
  2EAD  D9            		exx
                      	;	jr	c,LINEEND
                      	
  2EAE                	ERRPLS:
  2EAE  D9            		exx
  2EAF  CD5D2B        		call	INCGY		;increment Y
  2EB2  2AAFFE        		ld	hl,(GRPY2)
  2EB5  E7            		rst	CPHLDE
  2EB6  30DE          		jr	nc,LINELP1
                      	
  2EB8                	LINEEND:
  2EB8  C1            		pop	bc
  2EB9  D1            		pop	de
                      	
  2EBA                	LINEEND2:
  2EBA  E1            		pop	hl
  2EBB  22AFFE        		ld	(GRPY2),hl
  2EBE  E1            		pop	hl
  2EBF  22ADFE        		ld	(GRPX2),hl
  2EC2  E1            		pop	hl
  2EC3  C9            		ret
                      	
                      	
                      	;PAINT command
  2EC4                	C_PAIN:
  2EC4  CDC62D        		call	SKIPAT
  2EC7  CDF22C        		call	GETGXY
  2ECA  C5            		push	bc		;X
  2ECB  D5            		push	de		;Y
  2ECC  3A93FD        		ld	a,(COLOR1)
  2ECF  CCE30D        		call	z,INT1INC
  2ED2  CDC015        		call	SETATT
  2ED5  7E            		ld	a,(hl)
  2ED6  FE2C          		cp	','
  2ED8  3AC5FE        		ld	a,(BORDERA)
  2EDB  2006          		jr	nz,PANZ
  2EDD  CDE30D        		call	INT1INC
  2EE0  32C6FE        		ld	(BORDERC),a
  2EE3                	PANZ:
  2EE3  5F            		ld	e,a
  2EE4  C3F12E        		jp	PAPOP
                      	
                      	
                      	;paint used by OKHOTSK
                      	;input: e=color code, (sp)=Y, (sp+2)=X
  2EE7                	_PAPOP:ds	PAPOP-_PAPOP
  2EF1                		org	PAPOP
                      	
  2EF1  7B            		ld	a,e
  2EF2  CDC318        		call	SETBO
  2EF5  D1            		pop	de		;Y
  2EF6  C1            		pop	bc		;X
  2EF7  C3FA2E        		jp	PAINT
                      	
                      	
                      	;input: bc=x, de=y, (ATTDAT)=attribute, (BORDERA)=attribute, (BORDERC)=color
                      	;destroy: af,bc,de
  2EFA                	_PAINT:	ds	PAINT-_PAINT
  2EFA                		org	PAINT
                      	
  2EFA  3A92FD        		ld	a,(SCREEN1)
  2EFD  3D            		dec	a
  2EFE  C8            		ret	z		;screen mode 2
  2EFF  E5            		push	hl
  2F00  F5            		push	af
  2F01  CDB215        		call	GXY2AD
  2F04  57            		ld	d,a
  2F05  F1            		pop	af
  2F06  FA0E2F        		jp	m,PAINT1	;screen mode 1
  2F09                	PAMAIN:
  2F09  CD122F        		call	PADWN
  2F0C  E1            		pop	hl
  2F0D  C9            		ret
                      	
  2F0E                	PAINT1:
  2F0E  16FF          		ld	d,0ffh
  2F10  18F7          		jr	PAMAIN
                      	
                      	
  2F12                	PADWN:
  2F12  CDC42F        		call	PALINE
  2F15  C8            		ret	z
  2F16  E5            		push	hl		;address
  2F17  D5            		push	de		;bit
  2F18  ED4BB5FE      		ld	bc,(PAWRK)
  2F1C  C5            		push	bc
  2F1D  CD812F        		call	PAUP2
  2F20  E1            		pop	hl
  2F21  22B5FE        		ld	(PAWRK),hl
  2F24  D1            		pop	de		;bit
  2F25  E1            		pop	hl		;address
                      	
  2F26                	PADWN2:
  2F26  3A92FD        		ld	a,(SCREEN1)
  2F29  B7            		or	a
  2F2A  1E19          		ld	e,19h
  2F2C  2002          		jr	nz,PADWNG
  2F2E  1E01          		ld	e,01h
  2F30                	PADWNG:
  2F30  7C            		ld	a,h
  2F31  E61F          		and	1fh
  2F33  BB            		cp	e
  2F34  3804          		jr	c,PADWNOK
  2F36  7D            		ld	a,l
  2F37  FEE0          		cp	0e0h
  2F39  D0            		ret	nc
  2F3A                	PADWNOK:
  2F3A  012000        		ld	bc,COLUMNS
  2F3D  09            		add	hl,bc
                      	
  2F3E  AF            		xor	a
  2F3F  32B7FE        		ld	(PACNT),a
  2F42                	PADWNLP1:
  2F42  CD1E30        		call	SRCHOK
  2F45  3812          		jr	c,CALLDWN
  2F47  E5            		push	hl		;address
  2F48  21B7FE        		ld	hl,PACNT
  2F4B  34            		inc	(hl)
  2F4C  D5            		push	de		;bit
  2F4D  CDCC34        		call	CHKFRE
  2F50  D1            		pop	de
  2F51  E1            		pop	hl
  2F52  E5            		push	hl
  2F53  D5            		push	de
                      	
                      	;	ld	a,l
                      	;	inc	a
                      	;	and	1fh
                      	;	jr	nz,PADWNNZ
                      	;	bit	0,d
                      	;	jr	nz,CALLDWN
                      	;PADWNNZ:
                      	;	call	INCGXPA
                      	
  2F54  CD3730        		call	SRCHNG
  2F57  30E9          		jr	nc,PADWNLP1
                      	
  2F59                	CALLDWN:
  2F59  3AB7FE        		ld	a,(PACNT)
  2F5C  B7            		or	a
  2F5D  C8            		ret	z
  2F5E                	PADWNLP2:
  2F5E  D1            		pop	de		;bit
  2F5F  E1            		pop	hl		;address
  2F60  3D            		dec	a
  2F61  32B7FE        		ld	(PACNT),a
  2F64  28AC          		jr	z,PADWN
  2F66  F5            		push	af
  2F67  CD122F        		call	PADWN
  2F6A  F1            		pop	af
  2F6B  18F1          		jr	PADWNLP2
                      	
                      	
  2F6D                	PAUP:
  2F6D  CDC42F        		call	PALINE
  2F70  C8            		ret	z
  2F71  E5            		push	hl		;address
  2F72  D5            		push	de		;bit
  2F73  ED4BB5FE      		ld	bc,(PAWRK)
  2F77  C5            		push	bc
  2F78  CD262F        		call	PADWN2
  2F7B  E1            		pop	hl
  2F7C  22B5FE        		ld	(PAWRK),hl
  2F7F  D1            		pop	de		;bit
  2F80  E1            		pop	hl		;address
                      	
  2F81                	PAUP2:
                      	;if (hl - start address) < 0020h
  2F81  3A92FD        		ld	a,(SCREEN1)
  2F84  E602          		and	02h		;0(screen mode 1) or 2(screen mode 3,4)
  2F86  5F            		ld	e,a
  2F87  7C            		ld	a,h
  2F88  E61F          		and	1fh
  2F8A  BB            		cp	e
  2F8B  2004          		jr	nz,PAUPNZ1
  2F8D  7D            		ld	a,l
  2F8E  FE20          		cp	20h
  2F90  D8            		ret	c
  2F91                	PAUPNZ1:
  2F91  01E0FF        		ld	bc,0-COLUMNS
  2F94  09            		add	hl,bc
                      	
  2F95  AF            		xor	a
  2F96  32B7FE        		ld	(PACNT),a
  2F99                	PAUPLP1:
  2F99  CD1E30        		call	SRCHOK
  2F9C  3812          		jr	c,CALLUP
  2F9E  E5            		push	hl		;address
  2F9F  21B7FE        		ld	hl,PACNT
  2FA2  34            		inc	(hl)
  2FA3  D5            		push	de		;bit
  2FA4  CDCC34        		call	CHKFRE
  2FA7  D1            		pop	de
  2FA8  E1            		pop	hl
  2FA9  E5            		push	hl
  2FAA  D5            		push	de
                      	
                      	;	ld	a,l
                      	;	inc	a
                      	;	and	1fh
                      	;	jr	nz,PAUPNZ2
                      	;	bit	0,d
                      	;	jr	nz,CALLUP
                      	;PAUPNZ2:
                      	;	call	INCGXPA
                      	
  2FAB  CD3730        		call	SRCHNG
  2FAE  30E9          		jr	nc,PAUPLP1
                      	
  2FB0                	CALLUP:
  2FB0  3AB7FE        		ld	a,(PACNT)
  2FB3  B7            		or	a
  2FB4  C8            		ret	z
  2FB5                	PAUPLP2:
  2FB5  D1            		pop	de		;bit
  2FB6  E1            		pop	hl		;address
  2FB7  3D            		dec	a
  2FB8  32B7FE        		ld	(PACNT),a
  2FBB  28B0          		jr	z,PAUP
  2FBD  F5            		push	af
  2FBE  CD6D2F        		call	PAUP
  2FC1  F1            		pop	af
  2FC2  18F1          		jr	PAUPLP2
                      	
                      	
                      	;paint 1 line
                      	;input: hl=address, d=bit
                      	;output: hl, d, z-flag(1=not painted)
                      	;destroy: af,bc
  2FC4                	PALINE:
  2FC4  CD4D27        		call	STOPESC
  2FC7  CD0F30        		call	CHKPA
  2FCA  C8            		ret	z
  2FCB  E5            		push	hl		;address
  2FCC  D5            		push	de		;bit
  2FCD  CDF42F        		call	PAINTR
  2FD0  7D            		ld	a,l
  2FD1  E61F          		and	1fh
  2FD3  6F            		ld	l,a
  2FD4  62            		ld	h,d
  2FD5  22B5FE        		ld	(PAWRK),hl	;X_right
  2FD8  D1            		pop	de		;bit
  2FD9  E1            		pop	hl		;address
                      	;	jr	PAINTL		;z-flag=0
                      	
                      	
                      	;paint to left direction
                      	;input: hl=address, d=bit
                      	;output: hl, d, z-flag=0
                      	;destroy: af,bc
  2FDA                	PAINTL:
  2FDA  7D            		ld	a,l
  2FDB  E61F          		and	1fh
  2FDD  2003          		jr	nz,PALOK
  2FDF  CB7A          		bit	7,d
  2FE1  C0            		ret	nz
                      	
  2FE2                	PALOK:
  2FE2  CD0530        		call	DECGXPA
  2FE5  CD0F30        		call	CHKPA
  2FE8  20F0          		jr	nz,PAINTL
                      	;	jp	INCGXPA
                      	
                      	
                      	;increment x for paint
                      	;input: hl=address, d=bit
                      	;output: hl, d, z-flag=0
                      	;destroy: af
  2FEA                	INCGXPA:
  2FEA  CB0A          		rrc	d
  2FEC  E2F12F        		jp	po,INCGXPA4	;screen mode 4
  2FEF  CB0A          		rrc	d		;screen mode 1 or 3
  2FF1                	INCGXPA4:
  2FF1  D0            		ret	nc
  2FF2  23            		inc	hl
  2FF3  C9            		ret
                      	
                      	
                      	;paint to right direction
                      	;input: hl=address, d=bit
                      	;output: hl,d
                      	;destroy: af,bc
  2FF4                	PAINTR:
  2FF4  7D            		ld	a,l
  2FF5  3C            		inc	a
  2FF6  E61F          		and	1fh
  2FF8  2003          		jr	nz,PAROK
  2FFA  CB42          		bit	0,d
  2FFC  C0            		ret	nz
                      	
  2FFD                	PAROK:
  2FFD  CDEA2F        		call	INCGXPA
  3000  CD0F30        		call	CHKPA
  3003  20EF          		jr	nz,PAINTR
                      	;	jp	DECGXPA
                      	
                      	
                      	;decrement x for paint
                      	;input: hl=address, d=bit
                      	;output: hl, d
                      	;destroy: af
  3005                	DECGXPA:
  3005  CB02          		rlc	d
  3007  E20C30        		jp	po,DECGXPA4	;screen mode 4
  300A  CB02          		rlc	d		;screen mode 1 or 3
  300C                	DECGXPA4:
  300C  D0            		ret	nc
  300D  2B            		dec	hl
  300E  C9            		ret
                      	
                      	
                      	;check and paint
                      	;input: hl=address, d=bit
                      	;output: z(1=not painted)
                      	;destroy: af
  300F                	CHKPA:
  300F  3AC5FE        		ld	a,(BORDERA)
  3012  AE            		xor	(hl)
  3013  A2            		and	d
  3014  C8            		ret	z
                      	
  3015  3AACFE        		ld	a,(ATTDAT)
  3018  AE            		xor	(hl)
  3019  A2            		and	d
  301A  AE            		xor	(hl)
  301B  77            		ld	(hl),a
  301C  B2            		or	d		;d>0, reset z-flag
  301D  C9            		ret
                      	
                      	
                      	;search for paintable area
                      	;input: hl=address, d=bit
                      	;output: hl, d, c-flag(1=over,0=OK)
                      	;destroy: af,e
  301E                	SRCHOK:
  301E  CD4930        		call	CMPPAX
  3021  D8            		ret	c
                      	
  3022  5E            		ld	e,(hl)
  3023  3AC5FE        		ld	a,(BORDERA)
  3026  AB            		xor	e
  3027  A2            		and	d
  3028  2806          		jr	z,SRCHOKZ
                      	
  302A  3AACFE        		ld	a,(ATTDAT)
  302D  AB            		xor	e
  302E  A2            		and	d
  302F  C0            		ret	nz		;c-flag=0
                      	
  3030                	SRCHOKZ:
  3030  CD5930        		call	GXLARGE
  3033  20E9          		jr	nz,SRCHOK
  3035  37            		scf
  3036  C9            		ret
                      	
                      	
                      	;search for unpaintable area
                      	;get unpaintable area
                      	;input: hl=address, d=bit
                      	;output: hl, d, c-flag(1=over,0=NG)
                      	;destroy: af,e
  3037                	SRCHNG:
  3037  CD4930        		call	CMPPAX
  303A  D8            		ret	c
                      	
  303B  5E            		ld	e,(hl)
  303C  3AC5FE        		ld	a,(BORDERA)
  303F  AB            		xor	e
  3040  A2            		and	d
  3041  C8            		ret	z		;c-flag=0
                      	
  3042  CD5930        		call	GXLARGE
  3045  20F0          		jr	nz,SRCHNG
  3047  37            		scf
  3048  C9            		ret
                      	
                      	
                      	;compare  X to (PAWRK)
                      	;input: hl=address, d=bit
                      	;output: f
                      	;destroy: af,e
  3049                	CMPPAX:
  3049  7D            		ld	a,l
  304A  E61F          		and	1fh
  304C  5F            		ld	e,a
  304D  3AB5FE        		ld	a,(PAWRK)	;address low (5 bits)
  3050  BB            		cp	e
  3051  C0            		ret	nz
                      	
  3052  3AB6FE        		ld	a,(PAWRK+1)	;bit
  3055  3D            		dec	a
  3056  BA            		cp	d
  3057  3F            		ccf
  3058  C9            		ret
                      	
                      	
                      	;increment x and compare graphic width
                      	;output: z-flag(1=over)
                      	;destroy: af
  3059                	GXLARGE:
  3059  CDEA2F        		call	INCGXPA
  305C  7D            		ld	a,l
  305D  E61F          		and	1fh
  305F  C0            		ret	nz
  3060  7A            		ld	a,d
  3061  2F            		cpl
  3062  E680          		and	80h
  3064  C9            		ret
                      	
                      	
                      	;STR$() function
  3065                	F_STR:
  3065  CD9B0B        		call	CHKNUM
  3068  CDAC3A        		call	FTOA
  306B  E5            		push	hl
  306C  010101        		ld	bc,0101h
  306F  AF            		xor	a
  3070  EDB1          		cpir
  3072  91            		sub	c
  3073  E1            		pop	hl
  3074  CD4227        		call	MAKESTR
  3077  C39027        		jp	INKYEND
                      	
                      	
                      	;MODE key
                      	;destroy: af
  307A                	MODEKEY:
  307A  E5            		push	hl
  307B  D5            		push	de
  307C  C5            		push	bc
                      	
  307D  215AFA        		ld	hl,KEYFLG
  3080  CB5E          		bit	3,(hl)
  3082  2844          		jr	z,MODEEND
  3084  CB9E          		res	3,(hl)
                      	
  3086  2A5DFA        		ld	hl,(LINENUM)
  3089  7C            		ld	a,h
  308A  A5            		and	l
  308B  3C            		inc	a
  308C  203A          		jr	nz,MODEEND	;not direct command mode
                      	
  308E  CD8111        		call	CSROFF
                      	
  3091  21BC0D        		ld	hl,MODESTR
  3094  CDCF30        		call	PUTS
  3097  3A65FE        		ld	a,(MODE)
  309A  F5            		push	af		;
  309B  3C            		inc	a
  309C  CD7A39        		call	PUTI1
  309F  CD3927        		call	PUTNL
                      	
  30A2  3A8CFD        		ld	a,(PAGES)
  30A5  CD7A39        		call	PUTI1
  30A8  21C40D        		ld	hl,PAGESTR
  30AB  CDCF30        		call	PUTS
                      	
  30AE  F1            		pop	af		;
  30AF  FE02          		cp	02h
  30B1  3812          		jr	c,SKPFILES
                      	
  30B3  3A75FE        		ld	a,(DRVBIT)
  30B6  B7            		or	a
  30B7  280C          		jr	z,SKPFILES
                      	
  30B9  3A31FB        		ld	a,(FILES)
  30BC  CD7A39        		call	PUTI1
  30BF  21CD0D        		ld	hl,FILESTR
  30C2  CDCF30        		call	PUTS
                      	
  30C5                	SKPFILES:
  30C5  CD7911        		call	CSRON
                      	
  30C8                	MODEEND:
  30C8  C1            		pop	bc
  30C9  D1            		pop	de
  30CA  E1            		pop	hl
  30CB  C9            		ret
                      	
                      	
                      	;put characters (skip first character) (used by LIZARD)
  30CC                	_PUTSINC:ds	PUTSINC-_PUTSINC
  30CE                		org	PUTSINC
                      	
  30CE  23            		inc	hl
                      	
                      	;put characters
                      	;input: hl,(DEVICE)=0:CRT, 1:printer, 2:RS-232C, 80h-ffh:CMT
                      	;destroy: af,hl,(bc,de,FAC1)
  30CF                	_PUTS:	ds	PUTS-_PUTS
  30CF                		org	PUTS
                      	
  30CF  7E            		ld	a,(hl)
  30D0  B7            		or	a
  30D1  C8            		ret	z
  30D2  FE22          		cp	22h		;double quotation mark
  30D4  C8            		ret	z
  30D5  CDC726        		call	PUTDEV
  30D8  18F4          		jr	PUTSINC
                      	
                      	
                      	;input: bc=candidate of string descriptor address
                      	;       hl=string descriptor address to be checked
                      	;	(GCWRK)=candidate of string data address to be comacted
                      	;	stack=string descriptor address pointing to same data (endmark=0000)
                      	;output: bc=string descriptor address to be compacted
                      	;	 (GCWRK)=string data address to be comacted
                      	;	 hl=next string descriptor address
                      	;	 stack=string descriptor address pointing to same data (endmark=0000)
                      	;destroy: af,de
  30DA                	GETSTR:
  30DA  E5            		push	hl
  30DB  7E            		ld	a,(hl)
  30DC  23            		inc	hl
  30DD  23            		inc	hl
  30DE  5E            		ld	e,(hl)
  30DF  23            		inc	hl
  30E0  56            		ld	d,(hl)
  30E1  23            		inc	hl
                      	
  30E2  B7            		or	a
  30E3  2852          		jr	z,GETNOSTR2	;length=0
                      	
  30E5  E5            		push	hl
  30E6  2A56FF        		ld	hl,(VARAD)
  30E9  E7            		rst	CPHLDE
  30EA  304A          		jr	nc,GETNOSTR	;in program area
  30EC  2A3DFF        		ld	hl,(STRAD)
  30EF  E7            		rst	CPHLDE
  30F0  3844          		jr	c,GETNOSTR	;in work area or have been compacted
  30F2  2A41FF        		ld	hl,(GCWRK)
  30F5  E7            		rst	CPHLDE
  30F6  E1            		pop	hl
  30F7  2811          		jr	z,GETSTRZ
  30F9  303C          		jr	nc,GETNOSTR2	;de < candidate
                      	
                      	;new candidate found
  30FB  ED5341FF      		ld	(GCWRK),de
  30FF  C1            		pop	bc
  3100  D1            		pop	de		;return address
                      	
  3101                	GETSTRLP:
  3101  F1            		pop	af		;pushed address? (a=higher byte)
  3102  F5            		push	af
  3103  D5            		push	de		;return address
  3104  B7            		or	a
  3105  C8            		ret	z		;no data
  3106  D1            		pop	de		;return address
  3107  F1            		pop	af		;cancel
  3108  18F7          		jr	GETSTRLP
                      	
  310A                	GETSTRZ:
  310A  F1            		pop	af
  310B  D1            		pop	de		;return address
  310C  F5            		push	af
  310D  D5            		push	de		;return address
  310E  C9            		ret
                      	
                      	
                      	;garbage collection
                      	;destroy: af,bc,de,hl
  310F                	_GC:	ds	GC-_GC
  310F                		org	GC
                      	
  310F  2A27FF        		ld	hl,(STREND)
  3112  223DFF        		ld	(STRAD),hl
                      	
                      	;	ld	hl,0000h
  3115  2600          		ld	h,00h		;higher byte
  3117                	GCLP1:
  3117  E5            		push	hl
  3118  2241FF        		ld	(GCWRK),hl
  311B  2A56FF        		ld	hl,(VARAD)
                      	
                      	;normal variable
  311E                	GCVAR:
  311E  ED5B58FF      		ld	de,(ARRAD)
  3122  E7            		rst	CPHLDE
  3123  3014          		jr	nc,GCARR
  3125  23            		inc	hl
  3126  7E            		ld	a,(hl)
  3127  07            		rlca
  3128  3806          		jr	c,GCVARS
  312A  110600        		ld	de,0006h
  312D  19            		add	hl,de
  312E  18EE          		jr	GCVAR
  3130                	GCVARS:
  3130  23            		inc	hl
  3131  CDDA30        		call	GETSTR
  3134  18E8          		jr	GCVAR
                      	
                      	
  3136                	GETNOSTR:
  3136  E1            		pop	hl
  3137                	GETNOSTR2:
  3137  F1            		pop	af	;cancel
  3138  C9            		ret
                      	
                      	
                      	;array variable
  3139                	GCARR:
  3139  ED5B5AFF      		ld	de,(FREEAD)
  313D  E7            		rst	CPHLDE
  313E  3029          		jr	nc,GCSDSC
  3140  23            		inc	hl
  3141  7E            		ld	a,(hl)
  3142  23            		inc	hl
  3143  5E            		ld	e,(hl)
  3144  23            		inc	hl
  3145  56            		ld	d,(hl)
  3146  23            		inc	hl
  3147  07            		rlca
  3148  3803          		jr	c,GCARRS
  314A  19            		add	hl,de
  314B  18EC          		jr	GCARR
                      	
  314D                	GCARRS:
  314D  EB            		ex	de,hl
  314E  19            		add	hl,de
  314F  EB            		ex	de,hl
  3150  D5            		push	de
  3151  5E            		ld	e,(hl)		;dimensions
  3152  1600          		ld	d,00h
  3154  23            		inc	hl
  3155  19            		add	hl,de
  3156  19            		add	hl,de
  3157                	GCARRLP:
  3157  D1            		pop	de
  3158  E7            		rst	CPHLDE
  3159  30DE          		jr	nc,GCARR
  315B  ED5350FF      		ld	(TMP),de
  315F  CDDA30        		call	GETSTR
  3162  ED5B50FF      		ld	de,(TMP)
  3166  D5            		push	de
  3167  18EE          		jr	GCARRLP
                      	
                      	;temporary string descriptor
  3169                	GCSDSC:
  3169  212DFF        		ld	hl,STRDSC1
  316C  CDDA30        		call	GETSTR
                      	;	ld	hl,STRDSC2
  316F  CDDA30        		call	GETSTR
                      	;	ld	hl,STRDSC3
  3172  CDDA30        		call	GETSTR
                      	;	ld	hl,STRDSC4
  3175  CDDA30        		call	GETSTR
                      	
  3178  2A41FF        		ld	hl,(GCWRK)
  317B  7C            		ld	a,h
                      	;	or	l
  317C  B7            		or	a		;higher byte
  317D  D1            		pop	de		;cancel
  317E  C8            		ret	z
  317F  D5            		push	de
                      	
                      	;compaction
                      	;input:	bc=string descriptor address
                      	;	hl=string data address to be comacted
                      	
  3180  0A            		ld	a,(bc)
  3181  C5            		push	bc		;
  3182  0600          		ld	b,00h
  3184  4F            		ld	c,a
  3185  09            		add	hl,bc
  3186  2B            		dec	hl
  3187  ED5B3DFF      		ld	de,(STRAD)
  318B  EDB8          		lddr
  318D  ED533DFF      		ld	(STRAD),de
  3191  13            		inc	de
                      	
  3192  E1            		pop	hl		;
  3193                	GCLP2:
  3193  23            		inc	hl
  3194  23            		inc	hl
  3195  73            		ld	(hl),e
  3196  23            		inc	hl
  3197  72            		ld	(hl),d
  3198  E1            		pop	hl
  3199  7C            		ld	a,h
                      	;	or	l
  319A  B7            		or	a		;higher byte
  319B  CA1731        		jp	z,GCLP1
  319E  18F3          		jr	GCLP2
                      	
                      	
                      	;LEN() function
  31A0                	F_LEN:
  31A0  CDA70B        		call	CHKSTR
  31A3                	SETI1A:
  31A3  6F            		ld	l,a
  31A4                	SETI1:
  31A4  CD7E0C        		call	I1TOF
  31A7                	SETTYP0:
  31A7  AF            		xor	a
  31A8  3225FF        		ld	(ARGTYP),a	;0=numeric
  31AB  C9            		ret
                      	
                      	
                      	;ASC() function
  31AC                	F_ASC:
  31AC  CD8A26        		call	STRARG2
  31AF  B7            		or	a
  31B0  CACB03        		jp	z,FCERR
  31B3  1A            		ld	a,(de)
  31B4  18ED          		jr	SETI1A
                      	
                      	
                      	;CHR$() function
  31B6                	F_CHR:
  31B6  CD9B0B        		call	CHKNUM
  31B9  CD4C07        		call	FTOI1
  31BC  43            		ld	b,e
  31BD  C37B27        		jp	INKYNZ
                      	
                      	
                      	;LEFT$() function
  31C0                	F_LEFT:
  31C0  CD9E26        		call	ARGSI1
  31C3  0E00          		ld	c,00h
  31C5  1824          		jr	MID
                      	
                      	
                      	;RIGHT$() function
  31C7                	F_RGT:
  31C7  CD9E26        		call	ARGSI1
  31CA  57            		ld	d,a		;
  31CB  93            		sub	e
  31CC  3001          		jr	nc,RGTNC
  31CE  AF            		xor	a
  31CF                	RGTNC:
  31CF  4F            		ld	c,a
  31D0  5A            		ld	e,d		;
  31D1  7A            		ld	a,d
  31D2  1817          		jr	MID
                      	
                      	
                      	;MID$() function
  31D4                	F_MID:
  31D4  CD9E26        		call	ARGSI1
  31D7  57            		ld	d,a		;length
  31D8  1D            		dec	e
  31D9  D5            		push	de		;d=length, e=2nd parameter-1
  31DA  1C            		inc	e
  31DB  CACB03        		jp	z,FCERR		;if 2nd parameter=0
                      	
  31DE  2A4EFF        		ld	hl,(PROGAD)
  31E1  1EFF          		ld	e,0ffh
  31E3  7E            		ld	a,(hl)
  31E4  FE2C          		cp	','
  31E6  CCBA26        		call	z,ARGI1
  31E9  C1            		pop	bc		;b=length, c=2nd parameter-1
  31EA  78            		ld	a,b
                      	
                      	;input: STRDSC1=string, a=string length, c=2nd parameter-1, e=3rd parameter
  31EB                	MID:
  31EB  91            		sub	c
  31EC  3001          		jr	nc,MIDNC
  31EE  AF            		xor	a
  31EF                	MIDNC:
  31EF  BB            		cp	e
  31F0  3801          		jr	c,MIDC
  31F2  7B            		ld	a,e
  31F3                	MIDC:
  31F3  47            		ld	b,a		;output length
  31F4  CD5D26        		call	GCCHECK
  31F7  78            		ld	a,b		;output length
  31F8  2A2FFF        		ld	hl,(STRDSC1+2)
  31FB  0600          		ld	b,00h
  31FD  09            		add	hl,bc
  31FE  CD4227        		call	MAKESTR
  3201  2A4EFF        		ld	hl,(PROGAD)
  3204  CDC40B        		call	CHKRPAR
  3207  C39027        		jp	INKYEND
                      	
                      	
                      	;VAL() function
  320A                	F_VAL:
  320A  CD8A26        		call	STRARG2
  320D  2A27FF        		ld	hl,(STREND)
  3210  23            		inc	hl
  3211  EB            		ex	de,hl
                      	
  3212  0600          		ld	b,00h
  3214  4F            		ld	c,a
  3215  09            		add	hl,bc
  3216  E7            		rst	CPHLDE
  3217  7E            		ld	a,(hl)
  3218  F5            		push	af
  3219  E5            		push	hl
  321A  2801          		jr	z,VALZ
  321C  AF            		xor	a
  321D                	VALZ:
  321D  77            		ld	(hl),a
                      	;	or	a
  321E  ED42          		sbc	hl,bc
  3220  CD9B39        		call	ATOF
  3223  E1            		pop	hl
  3224  F1            		pop	af
  3225  77            		ld	(hl),a
  3226  C3A731        		jp	SETTYP0
                      	
                      	
                      	;FRE() function
  3229                	F_FRE:
  3229  2125FF        		ld	hl,ARGTYP
  322C  7E            		ld	a,(hl)
  322D  B7            		or	a
  322E  2814          		jr	z,FRENUM
  3230  3D            		dec	a
  3231  C2C203        		jp	nz,SNERR
                      	;string
  3234  77            		ld	(hl),a		;=0
  3235  322DFF        		ld	(STRDSC1),a	;=0
  3238  CD0F31        		call	GC
  323B  2A3DFF        		ld	hl,(STRAD)
  323E  ED5B5BFA      		ld	de,(STACK)
  3242  1808          		jr	CALCFRE
                      	;numeric
  3244                	FRENUM:
  3244  21FAFF        		ld	hl,0-0006h	;for compatibility with N6X-BASIC
  3247  39            		add	hl,sp
  3248  ED5B5AFF      		ld	de,(FREEAD)
  324C                	CALCFRE:
  324C  B7            		or	a
  324D  ED52          		sbc	hl,de
  324F  C3800C        		jp	I2TOF
                      	
                      	
                      	;DIM command
  3252                	C_DIM:
  3252  CD8F33        		call	CHKVAR
                      	
  3255  7E            		ld	a,(hl)
  3256  FE28          		cp	'('
  3258  2044          		jr	nz,NOTARR
  325A  23            		inc	hl
                      	
  325B  E5            		push	hl		;program address
  325C  CD3D34        		call	SRCHARR
  325F  D2DA03        		jp	nc,DDERR
  3262  CD8533        		call	SETARNM
  3265  CD3534        		call	VARSIZE
  3268  E1            		pop	hl		;program address
  3269  D5            		push	de		;array address
  326A  D5            		push	de		;array address
  326B  C5            		push	bc		;element bytes
                      	
  326C  48            		ld	c,b		;b=0
  326D                	DIMLP:
  326D  C5            		push	bc		;dimensions
  326E  CD140E        		call	INT2ARG
  3271  C1            		pop	bc		;dimensions
  3272  CB7A          		bit	7,d
  3274  C2CB03        		jp	nz,FCERR
  3277  0C            		inc	c
  3278  CAD103        		jp	z,OMERR		;over 255 dimensions
  327B  13            		inc	de		;+1
                      	
  327C  E1            		pop	hl		;element bytes
  327D  F1            		pop	af		;array address
  327E  D5            		push	de		;size
  327F  F5            		push	af		;array address
  3280  E5            		push	hl		;element bytes
                      	
  3281  2A4EFF        		ld	hl,(PROGAD)
  3284  7E            		ld	a,(hl)
  3285  23            		inc	hl
  3286  FE2C          		cp	','
  3288  28E3          		jr	z,DIMLP
  328A  FE29          		cp	')'
  328C  C2C203        		jp	nz,SNERR
  328F  CD5934        		call	MAKEARR
                      	
  3292                	DIMNEXT:
  3292  2A4EFF        		ld	hl,(PROGAD)
  3295  2B            		dec	hl
  3296  D7            		rst	ANADAT
  3297  C8            		ret	z
  3298  23            		inc	hl
  3299  FE2C          		cp	','
  329B  C0            		ret	nz
  329C  18B4          		jr	C_DIM
                      	
  329E                	NOTARR:
  329E  224EFF        		ld	(PROGAD),hl
  32A1  CDC133        		call	SRCHVAR
  32A4  DCEB33        		call	c,MAKEVAR
  32A7  18E9          		jr	DIMNEXT
                      	
                      	
                      	;get variable address
                      	;input: bc=variable name, hl=program address
                      	;output: de=variable address, (PROGAD)=next program address
                      	;destroy: FAC1,FAC2,af,hl
  32A9                	GETVAR:
  32A9  C5            		push	bc
  32AA  7E            		ld	a,(hl)
  32AB  FE28          		cp	'('
  32AD  280B          		jr	z,GETVARR
  32AF  224EFF        		ld	(PROGAD),hl
  32B2  CDC133        		call	SRCHVAR
  32B5  DCEB33        		call	c,MAKEVAR
  32B8  C1            		pop	bc
  32B9  C9            		ret
  32BA                	GETVARR:
  32BA  CDBF32        		call	GETARR
  32BD  C1            		pop	bc
  32BE  C9            		ret
                      	
                      	
                      	;get array address
                      	;input: bc=variable name, hl=program address of '('
                      	;output: de=array address
                      	;destroy: af,bc,hl
  32BF                	GETARR:
  32BF  23            		inc	hl
  32C0  224EFF        		ld	(PROGAD),hl
  32C3  C5            		push	bc		;variable name
  32C4  CD3D34        		call	SRCHARR
  32C7  300F          		jr	nc,ARROK
                      	
  32C9  D5            		push	de		;array address
  32CA  2A4EFF        		ld	hl,(PROGAD)
  32CD  E5            		push	hl		;program address
  32CE  CD5333        		call	DIMAUTO
  32D1  E1            		pop	hl		;program address
  32D2  224EFF        		ld	(PROGAD),hl
  32D5  D1            		pop	de		;array address
  32D6  13            		inc	de
  32D7  13            		inc	de
  32D8                	ARROK:
  32D8  EB            		ex	de,hl
  32D9  23            		inc	hl
  32DA  23            		inc	hl
  32DB  0600          		ld	b,00h
  32DD  4E            		ld	c,(hl)		;dimensions
  32DE  09            		add	hl,bc
  32DF  09            		add	hl,bc
  32E0  E5            		push	hl		;array address
  32E1  79            		ld	a,c
  32E2  08            		ex	af,af'		;dimensions
  32E3  F5            		push	af
  32E4  48            		ld	c,b		;=0
  32E5  C5            		push	bc		;pointer
  32E6  E5            		push	hl		;array address
  32E7  210100        		ld	hl,0001h
  32EA  2250FF        		ld	(TMP),hl	;multiplied sizes
                      	
  32ED                	GETALP:
  32ED  2A50FF        		ld	hl,(TMP)
  32F0  E5            		push	hl		;multiplied sizes
  32F1  2A4EFF        		ld	hl,(PROGAD)
  32F4  CD140E        		call	INT2ARG		;get index
  32F7  E1            		pop	hl		;multiplied sizes
  32F8  2250FF        		ld	(TMP),hl
  32FB  E1            		pop	hl		;array address
  32FC  D5            		push	de		;index
  32FD  56            		ld	d,(hl)		;de=size of each dimension
  32FE  2B            		dec	hl
  32FF  5E            		ld	e,(hl)
  3300  2B            		dec	hl
  3301  E3            		ex	(sp),hl		;push array address, pop index
  3302  E7            		rst	CPHLDE
  3303  D2D703        		jp	nc,BSERR
                      	
  3306  D5            		push	de		;size
  3307  ED5B50FF      		ld	de,(TMP)	;multiplied sizes
  330B  CDF921        		call	MULINT2		;hl = multiplied sizes * index
  330E  C1            		pop	bc		;size
  330F  EB            		ex	de,hl
  3310  E1            		pop	hl		;array address
  3311  E3            		ex	(sp),hl		;push array address, pop pointer
  3312  19            		add	hl,de		;pointer += multiplied sizes * index
  3313  E5            		push	hl		;pointer
  3314  2A50FF        		ld	hl,(TMP)	;multiplied sizes
  3317  50            		ld	d,b		;size
  3318  59            		ld	e,c
  3319  CDF921        		call	MULINT2		;hl = multiplied sizes * size
  331C  2250FF        		ld	(TMP),hl
  331F  E1            		pop	hl		;pointer
  3320  E3            		ex	(sp),hl		;push poniter, pop array address
  3321  E5            		push	hl		;array address
                      	
  3322  08            		ex	af,af'		;dimensions
  3323  3D            		dec	a
  3324  47            		ld	b,a
  3325  08            		ex	af,af'
  3326  2A4EFF        		ld	hl,(PROGAD)
  3329  7E            		ld	a,(hl)
  332A  FE2C          		cp	','
  332C  200A          		jr	nz,NOTCMM
  332E  23            		inc	hl
  332F  224EFF        		ld	(PROGAD),hl
  3332  04            		inc	b
                      	;	dec	b
                      	;	jr	nz,GETALP
  3333  10B8          		djnz	GETALP
  3335  C3D703        		jp	BSERR
                      	
  3338                	NOTCMM:
  3338  04            		inc	b
  3339  05            		dec	b
  333A  C2D703        		jp	nz,BSERR
  333D  CDC40B        		call	CHKRPAR
                      	
  3340  D1            		pop	de		;array address
  3341  E1            		pop	hl		;pointer
  3342  F1            		pop	af
  3343  08            		ex	af,af'
  3344  D1            		pop	de		;array address
  3345  C1            		pop	bc		;variable name
  3346  79            		ld	a,c
  3347  44            		ld	b,h
  3348  4D            		ld	c,l
  3349  29            		add	hl,hl		;*2
  334A  29            		add	hl,hl		;*4
  334B  07            		rlca
  334C  3801          		jr	c,NCMMSTR
  334E  09            		add	hl,bc		;*5
  334F                	NCMMSTR:
  334F  19            		add	hl,de
  3350  23            		inc	hl
  3351  EB            		ex	de,hl
  3352  C9            		ret
                      	
                      	
  3353                	DIMAUTO:
  3353  CD8533        		call	SETARNM
  3356  D5            		push	de		;array address
  3357  CD3534        		call	VARSIZE
  335A  D5            		push	de		;array address
  335B  C5            		push	bc		;element bytes
                      	
  335C  2A4EFF        		ld	hl,(PROGAD)
  335F  48            		ld	c,b		;b=0
  3360                	AUTOLP1:
  3360  C5            		push	bc		;dimensions
  3361  CD140E        		call	INT2ARG		;de=size
                      	;	ld	a,d
                      	;	or	a
                      	;	jp	nz,BSERR
                      	;	ld	a,e
                      	;	ld	e,0bh		;de=10+1
                      	;	cp	e
                      	;	jp	nc,BSERR
                      	
  3364  110B00        		ld	de,000bh
                      	
  3367  C1            		pop	bc		;dimensions
  3368  0C            		inc	c
  3369  CB51          		bit	2,c
  336B  C2D703        		jp	nz,BSERR	;over 3 dimensions
                      	
  336E  EB            		ex	de,hl
  336F  D1            		pop	de		;element bytes
  3370  E3            		ex	(sp),hl		;push size, pop array address
  3371  E5            		push	hl		;array address
  3372  D5            		push	de		;element bytes
                      	
                      	
  3373  2A4EFF        		ld	hl,(PROGAD)
  3376  7E            		ld	a,(hl)
  3377  23            		inc	hl
  3378  FE2C          		cp	','
  337A  28E4          		jr	z,AUTOLP1
  337C  FE29          		cp	')'
  337E  C2C203        		jp	nz,SNERR
  3381  CD5934        		call	MAKEARR
  3384  C9            		ret
                      	
                      	
                      	;set array name
                      	;input: bc=name, de=array address
                      	;output: de=array address (next of name)
                      	;destroy: af,hl
  3385                	SETARNM:
  3385  EB            		ex	de,hl
  3386  CDCF34        		call	CHKSTK
  3389  70            		ld	(hl),b
  338A  23            		inc	hl
  338B  71            		ld	(hl),c
  338C  23            		inc	hl
  338D  EB            		ex	de,hl
  338E  C9            		ret
                      	
                      	
                      	;check variable name
                      	;input: hl=program address
                      	;output: a=next character, bc=name, hl=next address
                      	;destroy: f
  338F                	CHKVAR:
  338F  7E            		ld	a,(hl)
  3390  23            		inc	hl
  3391  FE20          		cp	' '
  3393  28FA          		jr	z,CHKVAR
  3395  47            		ld	b,a
  3396  D641          		sub	'A'
  3398  FE1A          		cp	'Z'-'A'+1
  339A  D2C203        		jp	nc,SNERR
                      	;	call	GETNAME
                      	;	ret
                      	
                      	
                      	;get variable name
                      	;input: hl=2nd character address, b=1st character
                      	;output: a=next character, bc=name, hl=next address
                      	;destroy: f
  339D                	GETNAME:
  339D  0E00          		ld	c,00h
  339F  2B            		dec	hl
  33A0                	GETNMLP1:
  33A0  23            		inc	hl
  33A1  7E            		ld	a,(hl)
  33A2  FE20          		cp	' '
  33A4  28FA          		jr	z,GETNMLP1
  33A6  FE24          		cp	'$'
  33A8  2813          		jr	z,NAMESTR
  33AA  CDD90B        		call	ALPNUM
  33AD  D0            		ret	nc
  33AE  4F            		ld	c,a
  33AF                	GETNMLP2:
  33AF  23            		inc	hl
  33B0  7E            		ld	a,(hl)
  33B1  FE20          		cp	' '
  33B3  28FA          		jr	z,GETNMLP2
  33B5  CDD90B        		call	ALPNUM
  33B8  38F5          		jr	c,GETNMLP2
  33BA  FE24          		cp	'$'
  33BC  C0            		ret	nz
  33BD                	NAMESTR:
  33BD  23            		inc	hl
  33BE  CBF9          		set	7,c
  33C0  C9            		ret
                      	
                      	
                      	;search for variable
                      	;input: bc=variable name
                      	;output: de=variable address, c-flag(1=not found)
                      	;destroy: af,hl
  33C1                	SRCHVAR:
  33C1  2A5EFF        		ld	hl,(FNPAR)
  33C4  7D            		ld	a,l
  33C5  B8            		cp	b
  33C6  2006          		jr	nz,SRCHVMAIN
  33C8  7C            		ld	a,h
  33C9  B9            		cp	c
  33CA  1160FF        		ld	de,FNARG
  33CD  C8            		ret	z
                      	
  33CE                	SRCHVMAIN:
  33CE  ED5B56FF      		ld	de,(VARAD)
  33D2                	SRCHVLP:
  33D2  2A58FF        		ld	hl,(ARRAD)
  33D5  2B            		dec	hl
  33D6  E7            		rst	CPHLDE
  33D7  D8            		ret	c		;not found
  33D8  1A            		ld	a,(de)
  33D9  13            		inc	de
  33DA  B8            		cp	b		;
  33DB  1A            		ld	a,(de)
  33DC  13            		inc	de
  33DD  2002          		jr	nz,VARNEXT	;
  33DF  B9            		cp	c
  33E0  C8            		ret	z		;found
  33E1                	VARNEXT:
  33E1  07            		rlca			;c-flag=1 if string
  33E2  3F            		ccf
  33E3  210400        		ld	hl,6-2
  33E6  ED5A          		adc	hl,de
  33E8  EB            		ex	de,hl
  33E9  18E7          		jr	SRCHVLP
                      	
                      	
                      	;make variable
                      	;input: bc=variable name, de=variable address
                      	;output: de=de+2
                      	;destroy: af,bc,hl
  33EB                	MAKEVAR:
  33EB  C5            		push	bc		;variable name
  33EC  D5            		push	de		;variable address
                      	
  33ED  110600        		ld	de,0006h
  33F0  CB79          		bit	7,c
  33F2  2001          		jr	nz,MAKEVSTR	;numeric=7, string=6
  33F4  1C            		inc	e
  33F5                	MAKEVSTR:
  33F5  2A5AFF        		ld	hl,(FREEAD)
  33F8  ED4B58FF      		ld	bc,(ARRAD)
  33FC  B7            		or	a
  33FD  ED42          		sbc	hl,bc
  33FF  44            		ld	b,h
  3400  4D            		ld	c,l
                      	
  3401  2A5AFF        		ld	hl,(FREEAD)
                      	
  3404  19            		add	hl,de
  3405  D5            		push	de		;numeric=7, string=6
  3406  CDCF34        		call	CHKSTK
  3409  D1            		pop	de		;numeric=7, string=6
  340A  225AFF        		ld	(FREEAD),hl
  340D  2A58FF        		ld	hl,(ARRAD)
  3410  19            		add	hl,de
  3411  2258FF        		ld	(ARRAD),hl
                      	
  3414  78            		ld	a,b
  3415  B1            		or	c
  3416  280B          		jr	z,NOLDDR
  3418  2A5AFF        		ld	hl,(FREEAD)
  341B  E5            		push	hl		;
                      	;	or	a
  341C  ED52          		sbc	hl,de
  341E  2B            		dec	hl
  341F  D1            		pop	de		;
  3420  1B            		dec	de
  3421  EDB8          		lddr
  3423                	NOLDDR:
  3423  E1            		pop	hl		;variable address
  3424  C1            		pop	bc		;variable name
  3425  70            		ld	(hl),b
  3426  23            		inc	hl
  3427  71            		ld	(hl),c
  3428  23            		inc	hl
  3429  E5            		push	hl		;variable address
                      	
  342A  EB            		ex	de,hl
  342B  21AB0E        		ld	hl,ZERO
  342E  CD3534        		call	VARSIZE
  3431  EDB0          		ldir
  3433  D1            		pop	de		;variable address
  3434  C9            		ret
                      	
                      	
                      	;get variable element size
                      	;input: bc=variable name
                      	;output: bc=5(numeric) or 4(string)
                      	;destroy: f
  3435                	VARSIZE:
  3435  CB79          		bit	7,c
  3437  010500        		ld	bc,0005h
  343A  C8            		ret	z
  343B  0D            		dec	c
  343C  C9            		ret
                      	
                      	
                      	;search for array variable
                      	;input: bc=variable name
                      	;output: de=array address, c-flag(1=not found)
                      	;destroy: af,hl
  343D                	SRCHARR:
  343D  ED5B58FF      		ld	de,(ARRAD)
  3441                	SRCHALP:
  3441  2A5AFF        		ld	hl,(FREEAD)
  3444  2B            		dec	hl
  3445  E7            		rst	CPHLDE
  3446  D8            		ret	c		;not found
  3447  1A            		ld	a,(de)
  3448  13            		inc	de
  3449  B8            		cp	b		;
  344A  1A            		ld	a,(de)
  344B  13            		inc	de
  344C  2002          		jr	nz,ARRNEXT	;
  344E  B9            		cp	c
  344F  C8            		ret	z		;found
  3450                	ARRNEXT:
  3450  EB            		ex	de,hl
  3451  5E            		ld	e,(hl)
  3452  23            		inc	hl
  3453  56            		ld	d,(hl)
  3454  23            		inc	hl
  3455  19            		add	hl,de
  3456  EB            		ex	de,hl
  3457  18E8          		jr	SRCHALP
                      	
                      	
                      	;make array
                      	;input: c=dimensions, hl=program address
                      	;pushed: size*[1,255], array address, element bytes
                      	;destroy: af,bc,de,hl
  3459                	MAKEARR:
  3459  224EFF        		ld	(PROGAD),hl
  345C  E1            		pop	hl		;return address
  345D  2250FF        		ld	(TMP),hl
  3460  D1            		pop	de		;element bytes
  3461  E1            		pop	hl		;array address
  3462  23            		inc	hl
  3463  23            		inc	hl
  3464  71            		ld	(hl),c		;dimensions
  3465  23            		inc	hl
  3466  EB            		ex	de,hl
                      	
  3467  41            		ld	b,c
  3468                	MKALP:
  3468  E3            		ex	(sp),hl		;push element bytes, pop size
  3469  EB            		ex	de,hl
  346A  73            		ld	(hl),e
  346B  23            		inc	hl
  346C  72            		ld	(hl),d
  346D  23            		inc	hl
  346E  E3            		ex	(sp),hl		;push array address, pop element bytes
  346F  C5            		push	bc		;dimensions
  3470  CDF921        		call	MULINT2		;element bytes *= each dimension size
  3473  C1            		pop	bc		;dimensions
  3474  7A            		ld	a,d
  3475  B3            		or	e
  3476  C2D703        		jp	nz,BSERR	;for compatibility with N60-BASIC
  3479  D1            		pop	de		;array address
  347A  10EC          		djnz	MKALP
                      	
  347C  E5            		push	hl		;element bytes
  347D  EB            		ex	de,hl
  347E  60            		ld	h,b
  347F  69            		ld	l,c
  3480  29            		add	hl,hl
  3481  23            		inc	hl
  3482  19            		add	hl,de
  3483  DAD103        		jp	c,OMERR
                      	
  3486  E5            		push	hl		;dimensions*2 + element bytes + 1
  3487  ED5B5AFF      		ld	de,(FREEAD)
  348B  13            		inc	de		;+2(total bytes)
  348C  13            		inc	de
  348D  13            		inc	de		;+2(name)
  348E  13            		inc	de
  348F  19            		add	hl,de
  3490  DAD103        		jp	c,OMERR
  3493  CDCF34        		call	CHKSTK
  3496  225AFF        		ld	(FREEAD),hl
                      	
  3499  79            		ld	a,c		;dimensions
  349A  D1            		pop	de		;dimensions*2 + element bytes + 1
  349B  E1            		pop	hl		;element bytes
  349C  E3            		ex	(sp),hl		;push element bytes, pop array address
                      	
  349D  73            		ld	(hl),e
  349E  23            		inc	hl
  349F  72            		ld	(hl),d
  34A0  23            		inc	hl
  34A1  23            		inc	hl
  34A2  09            		add	hl,bc
  34A3  09            		add	hl,bc
                      	
  34A4  70            		ld	(hl),b		;b=0
  34A5  54            		ld	d,h
  34A6  5D            		ld	e,l
  34A7  13            		inc	de
  34A8  C1            		pop	bc		;element bytes
  34A9  0B            		dec	bc
  34AA  EDB0          		ldir
  34AC  2A50FF        		ld	hl,(TMP)	;return address
  34AF  E9            		jp	(hl)
                      	
                      	
                      	;NEW command
  34B0                	C_NEW:
  34B0  CD603F        		call	CHKCLN
  34B3  C2C203        		jp	nz,SNERR
  34B6  214204        		ld	hl,EDIT
  34B9  E5            		push	hl
                      	
                      	;destroy: af,bc,de,hl
  34BA                	NEWRESSTK:
  34BA  21F934        		ld	hl,RESSTK
  34BD  E5            		push	hl
                      	;	jp	NEW
                      	
                      	;destroy: af,hl
  34BE                	NEW:
  34BE  2A29FF        		ld	hl,(BASICAD)
  34C1  AF            		xor	a
  34C2  77            		ld	(hl),a
  34C3  23            		inc	hl
  34C4  77            		ld	(hl),a
  34C5  23            		inc	hl
  34C6  77            		ld	(hl),a
  34C7  23            		inc	hl
  34C8  2256FF        		ld	(VARAD),hl
  34CB  C9            		ret
                      	
                      	
                      	;check free size
                      	;output: de=free size
                      	;destroy: f,hl
  34CC                	CHKFRE:
  34CC  2A5AFF        		ld	hl,(FREEAD)
                      	
                      	;check stack size
                      	;hl <= stack - 003bh?
                      	;input: hl=address
                      	;output: de=free size
                      	;destroy: f
  34CF                	CHKSTK:
  34CF  EB            		ex	de,hl
                      	;	ld	hl,0-003bh+1
  34D0  21CDFF        		ld	hl,0-0034h+1
  34D3  39            		add	hl,sp
  34D4  ED52          		sbc	hl,de		;c-flag=1
  34D6  DAD103        		jp	c,OMERR
  34D9  EB            		ex	de,hl
  34DA  C9            		ret
                      	
                      	
                      	;reset stack and variables
                      	;destroy: af,bc,de,hl
  34DB                	_RESSTK:ds	RESSTK-_RESSTK
  34F9                		org	RESSTK
                      	
  34F9  C1            		pop	bc		;return address
  34FA  ED7B5BFA      		ld	sp,(STACK)
  34FE  C5            		push	bc		;return address
  34FF  3EFF          		ld	a,0ffh
  3501  3255FF        		ld	(STOPAD+1),a
  3504  2A56FF        		ld	hl,(VARAD)
  3507  2258FF        		ld	(ARRAD),hl
  350A  225AFF        		ld	(FREEAD),hl
  350D  CDC9FF        		call	HOOKRES
  3510                	RSTRZ:
  3510  2A29FF        		ld	hl,(BASICAD)
  3513  225CFF        		ld	(DATAAD),hl
  3516  C9            		ret
                      	
                      	
                      	;RESTORE command
  3517                	C_RSTR:
  3517  CD603F        		call	CHKCLN
  351A  224EFF        		ld	(PROGAD),hl
  351D  28F1          		jr	z,RSTRZ
  351F  CD6807        		call	GETLN
  3522  224EFF        		ld	(PROGAD),hl
  3525  CDFB04        		call	SRCHLN
  3528  D2D403        		jp	nc,ULERR
  352B  ED435CFF      		ld	(DATAAD),bc
  352F  C9            		ret
                      	
                      	
                      	;STOP command
  3530                	C_STOP:
  3530  CD603F        		call	CHKCLN
  3533  C2C203        		jp	nz,SNERR
  3536  F1            		pop	af		;cancel return address
                      	
  3537                	STOP1:
  3537  7C            		ld	a,h
  3538  FEFA          		cp	0fah
  353A  300F          		jr	nc,STOPNC
  353C                	STOP2:
  353C  2254FF        		ld	(STOPAD),hl
  353F                	STOP3:
  353F  2A8FFD        		ld	hl,(SCREEN2)	;l=(SCREEN2),h=(SCREEN3)
  3542  225EFE        		ld	(STOPSC2),hl	;(STOPSC2)=l,(STOPSC3)=h
  3545  2A5DFA        		ld	hl,(LINENUM)
  3548  2252FF        		ld	(STOPLN),hl
  354B                	STOPNC:
  354B  FB            		ei
  354C  AF            		xor	a
  354D  3218FA        		ld	(STOPFLG),a
                      	
  3550  CD3F09        		call	PRTNLX
  3553  CD7C13        		call	CHGTXT
  3556  215F35        		ld	hl,BREAK
  3559  CDCF30        		call	PUTS
  355C  C33604        		jp	PRTLNUM
                      	
  355F                	BREAK:
  355F  07427265616B00		db	07h, "Break", 00h
                      	
                      	
                      	;END command
  3566                	C_END:
  3566  F1            		pop	af		;cancel stack
  3567  C34204        		jp	EDIT
                      	
                      	
                      	;CONT command
  356A                	C_CONT:
  356A  F1            		pop	af		;cancel return address
                      	
  356B  7C            		ld	a,h
  356C  FEFA          		cp	0fah
  356E  DAEF03        		jp	c,CNERR		;cont in program
                      	
  3571  2A54FF        		ld	hl,(STOPAD)
  3574  7C            		ld	a,h
  3575  FEFA          		cp	0fah
  3577  D2EF03        		jp	nc,CNERR	;stop in direct command mode
                      	
  357A  9F            		sbc	a,a		;c-flag=1
  357B  3255FF        		ld	(STOPAD+1),a	;a=ffh
                      	
  357E  EB            		ex	de,hl
  357F  2A52FF        		ld	hl,(STOPLN)
  3582  225DFA        		ld	(LINENUM),hl
  3585  2A5EFE        		ld	hl,(STOPSC2)	;l=(STOPSC2),h=(STOPSC3)
  3588  7D            		ld	a,l
  3589  CD0C14        		call	CHGACT
  358C  7C            		ld	a,h
  358D  CDED13        		call	CHGDSP
  3590  EB            		ex	de,hl
                      	
  3591  C30A07        		jp	INTPRT
                      	
                      	
                      	;read data from CGROM
                      	;input: de
                      	;output: d
                      	;destroy: a
  3594                	READCGROM:
  3594  3E04          		ld	a,04h
  3596  D393          		out	(93h),a		;CGROM ON
  3598  1A            		ld	a,(de)
  3599  57            		ld	d,a
  359A  3E05          		ld	a,05h
  359C  D393          		out	(93h),a		;CGROM OFF
  359E  C9            		ret
                      	
                      	
                      	;CLEAR command
  359F                	_C_CLR:	ds	C_CLR-_C_CLR
  35A9                		org	C_CLR
                      	
  35A9  CD603F        		call	CHKCLN
  35AC  CAF934        		jp	z,RESSTK
  35AF  CD140E        		call	INT2ARG
  35B2  CB7A          		bit	7,d
  35B4  C2CB03        		jp	nz,FCERR
  35B7  D5            		push	de		;1st parameter
  35B8  ED5B27FF      		ld	de,(STREND)
  35BC  7E            		ld	a,(hl)
  35BD  FE2C          		cp	','
  35BF  200E          		jr	nz,CLRNZ
  35C1  CD910B        		call	NARGMOINC
  35C4  CD9F0C        		call	FTOI
  35C7  1B            		dec	de
  35C8  2A8DFD        		ld	hl,(USREND)
  35CB  E7            		rst	CPHLDE
  35CC  DACB03        		jp	c,FCERR
                      	
  35CF                	CLRNZ:
  35CF  EB            		ex	de,hl
  35D0  C1            		pop	bc		;1st parameter
                      	
                      	;input: bc=1st parameter, hl=2nd parameter-1
  35D1                	CLRMAIN:
  35D1  E5            		push	hl		;2nd parameter-1
  35D2  B7            		or	a
  35D3  ED42          		sbc	hl,bc
                      	;	jp	c,OMERR
  35D5  380A          		jr	c,CLRC
  35D7  EB            		ex	de,hl
  35D8  2A56FF        		ld	hl,(VARAD)
  35DB  013800        		ld	bc,0038h
  35DE  09            		add	hl,bc
  35DF  EB            		ex	de,hl
  35E0  E7            		rst	CPHLDE
  35E1                	CLRC:
  35E1  DAD103        		jp	c,OMERR
  35E4  225BFA        		ld	(STACK),hl
  35E7  E1            		pop	hl		;2nd parameter-1
  35E8  2227FF        		ld	(STREND),hl
  35EB  223DFF        		ld	(STRAD),hl
  35EE  C3F934        		jp	RESSTK
                      	
                      	
                      	;NEXT command
  35F1                	C_NEXT:
  35F1  F1            		pop	af		;cancel return address
  35F2  0600          		ld	b,00h
  35F4                	NEXTLP:
  35F4  CD603F        		call	CHKCLN
  35F7  C48F33        		call	nz,CHKVAR
  35FA  224EFF        		ld	(PROGAD),hl
                      	
  35FD                	NEXTVLP:
  35FD  CDD007        		call	SRCHSTK
  3600  CABF03        		jp	z,NFERR		;not found or gosub identifier found
  3603  33            		inc	sp		;identifier
                      	
  3604  D1            		pop	de		;variable address
                      	
  3605  78            		ld	a,b
  3606  B7            		or	a
  3607  2813          		jr	z,NEXTOK
                      	
  3609  1B            		dec	de
  360A  1A            		ld	a,(de)
  360B  B9            		cp	c
  360C  2007          		jr	nz,NEXTNZ
  360E  1B            		dec	de
  360F  1A            		ld	a,(de)
  3610  13            		inc	de
  3611  13            		inc	de
  3612  B8            		cp	b
  3613  2807          		jr	z,NEXTOK
  3615                	NEXTNZ:
  3615  211000        		ld	hl,0010h	;STEP(6)+TO(6)+line number(2)+program address(2)
  3618  39            		add	hl,sp
  3619  F9            		ld	sp,hl
  361A  18E1          		jr	NEXTVLP
                      	
  361C                	NEXTOK:
  361C  CD3439        		call	POPF1		;STEP value
  361F  CD2539        		call	PUSHF1		;STEP value
                      	
  3622  D5            		push	de		;variable address
  3623  EB            		ex	de,hl
  3624  CD8F36        		call	SETADDF
  3627  D1            		pop	de		;variable address
  3628  D5            		push	de		;variable address
  3629  2166FF        		ld	hl,FAC1
  362C  CD480C        		call	SETF
                      	
  362F  D1            		pop	de		;variable address
                      	
  3630  CD3439        		call	POPF1		;STEP value
  3633  CD4339        		call	POPF2		;TO value
  3636  CD1A3C        		call	PUSHF2		;TO value
  3639  CD2539        		call	PUSHF1		;STEP value
                      	
  363C  D5            		push	de		;variable address
                      	
  363D  EF            		rst	CHKSGN
  363E  EB            		ex	de,hl		;variable address
  363F  CD450C        		call	SETF1		;no change in z,c-flag
  3642  2822          		jr	z,STEP0		;if STEP value = 0
  3644  D46539        		call	nc,EXFAC	;if STEP value > 0
  3647  CD1B3F        		call	CMPF
  364A  381F          		jr	c,NEXTEND	;for-next loop end
                      	
  364C                	NEXTCONT:
  364C  210E00        		ld	hl,000eh	;variable address(2)+STEP(6)+TO(6)
  364F  39            		add	hl,sp
  3650  5E            		ld	e,(hl)
  3651  23            		inc	hl
  3652  56            		ld	d,(hl)
  3653  ED535DFA      		ld	(LINENUM),de
  3657  23            		inc	hl
  3658  5E            		ld	e,(hl)
  3659  23            		inc	hl
  365A  56            		ld	d,(hl)
  365B  ED534EFF      		ld	(PROGAD),de
  365F  3E81          		ld	a,FOR_
  3661  F5            		push	af		;identifier
  3662  33            		inc	sp
  3663  C3E706        		jp	INTPEND
                      	
  3666                	STEP0:
  3666  CD1B3F        		call	CMPF
  3669  20E1          		jr	nz,NEXTCONT
                      	
  366B                	NEXTEND:
  366B  211200        		ld	hl,0012h	;variable address(2)+STEP(6)+TO(6)+line number(2)+program address(2)
  366E  39            		add	hl,sp
  366F  F9            		ld	sp,hl
                      	
  3670  2A4EFF        		ld	hl,(PROGAD)
  3673  7E            		ld	a,(hl)
  3674  FE2C          		cp	','
  3676  C2E706        		jp	nz,INTPEND
  3679  23            		inc	hl
  367A  47            		ld	b,a		;not variable name
  367B  C3F435        		jp	NEXTLP
                      	
                      	
                      	;+ operator
  367E                	O_PLS:
  367E  3A25FF        		ld	a,(ARGTYP)
  3681  B7            		or	a
  3682  281A          		jr	z,ADDF
                      	
  3684                	PLSSTR:
  3684  FE03          		cp	03h		;string and string
  3686  CA2326        		jp	z,ADDSTR
  3689  C3E303        		jp	TMERR
                      	
                      	
                      	;FAC1=FAC1+1
                      	;destroy: FAC2,af,bc,de,hl
  368C                	INCF1:
  368C  21A10E        		ld	hl,PLSONE
                      	
                      	;set FAC2 and add
                      	;input: FAC1, hl=float address
  368F                	SETADDF:
  368F  CD4E0C        		call	SETF2
  3692  180A          		jr	ADDF
                      	
                      	
                      	;- operator
  3694                	O_MNS:
  3694  CD9B0B        		call	CHKNUM
                      	;	call	SUBF
                      	;	ret
                      	
                      	
                      	;FAC1 = FAC1 - FAC2
                      	;input: FAC1,FAC2
                      	;output: FAC1
                      	;destroy: FAC2,af,b,de,hl
  3697                	SUBF:
  3697  2170FF        		ld	hl,FAC2+3
  369A                	CHGSGNADD:
  369A  7E            		ld	a,(hl)
  369B  EE80          		xor	80h
  369D  77            		ld	(hl),a
                      	;	call	ADDF
                      	;	ret
                      	
                      	
                      	;FAC1 = FAC1 + FAC2
                      	;input: FAC1,FAC2
                      	;output: FAC1
                      	;destroy: FAC2,af,bc,de,hl
  369E                	ADDF:
  369E  2A70FF        		ld	hl,(FAC2+3)	;l=(FAC2+3),h=(FAC2+4)
  36A1  7C            		ld	a,h
  36A2  B7            		or	a
  36A3  C8            		ret	z		;FAC2=0
                      	
  36A4  E5            		push	hl
  36A5  CBFD          		set	7,l
  36A7  2270FF        		ld	(FAC2+3),hl
                      	
  36AA  2A69FF        		ld	hl,(FAC1+3)	;l=(FAC1+3),h=(FAC1+4)
  36AD  E5            		push	hl
  36AE  CBFD          		set	7,l
  36B0  2269FF        		ld	(FAC1+3),hl
                      	
                      	;	sub	h
                      	;	jr	c,ADD1GT2	;|FAC1| > |FAC2|
                      	;	jr	nz,ADD1LT2	;|FAC1| < |FAC2|
                      	
  36B3  CD1B3F        		call	CMPF
  36B6  380D          		jr	c,ADD1GT2	;|FAC1| > |FAC2|
                      	
  36B8                	ADD1LT2:
  36B8  CD6539        		call	EXFAC
                      	
  36BB  D1            		pop	de		;e=(FAC1+3),d=(FAC1+4)
  36BC  E1            		pop	hl		;l=(FAC2+3),h=(FAC2+4)
                      	
  36BD  7A            		ld	a,d
  36BE  B7            		or	a
  36BF  2006          		jr	nz,ADDCHKSGN
  36C1  2269FF        		ld	(FAC1+3),hl
  36C4  C9            		ret
                      	
  36C5                	ADD1GT2:
  36C5  E1            		pop	hl		;l=(FAC1+3),h=(FAC1+4)
  36C6  D1            		pop	de		;e=(FAC2+3),d=(FAC2+4)
                      	
  36C7                	ADDCHKSGN:
  36C7  7C            		ld	a,h
  36C8  92            		sub	d
  36C9  47            		ld	b,a		;exponent difference
                      	
  36CA  7D            		ld	a,l
  36CB  F5            		push	af		;sign
                      	
  36CC  AB            		xor	e
  36CD  07            		rlca
  36CE  F5            		push	af		;c-flag=0:same sign
  36CF  3009          		jr	nc,SAMESGN
                      	
  36D1  CD6539        		call	EXFAC
  36D4  CDA124        		call	NEGINT4
  36D7  CD6539        		call	EXFAC
                      	
  36DA                	SAMESGN:
  36DA  0E00          		ld	c,00h		;guard/round/sticky bit
  36DC  2A6DFF        		ld	hl,(FAC2)
  36DF  ED5B6FFF      		ld	de,(FAC2+2)
                      	
  36E3                	ADDFLP1:
  36E3  78            		ld	a,b		;exponent difference
  36E4  D608          		sub	08h
  36E6  3818          		jr	c,EXPLT8
  36E8  47            		ld	b,a
                      	
  36E9  7D            		ld	a,l
  36EA  E63F          		and	3fh
  36EC  2004          		jr	nz,SETSBIT
  36EE  0C            		inc	c
  36EF  0D            		dec	c
  36F0  2802          		jr	z,NOSBIT
  36F2                	SETSBIT:
  36F2  0E20          		ld	c,20h		;set sticky bit
  36F4                	NOSBIT:
  36F4  AD            		xor	l
  36F5  B1            		or	c
  36F6  4F            		ld	c,a
  36F7  6C            		ld	l,h
  36F8  63            		ld	h,e
  36F9  5A            		ld	e,d
  36FA  F1            		pop	af		;same sign?
  36FB  F5            		push	af
  36FC  9F            		sbc	a,a		;00h or ffh
  36FD  57            		ld	d,a
  36FE  18E3          		jr	ADDFLP1
                      	
  3700                	EXPLT8:
  3700  C608          		add	a,08h
  3702  2813          		jr	z,SAMEEXP
                      	
  3704                	ADDFLP2:
  3704  F1            		pop	af		;same sign?
  3705  F5            		push	af
  3706  CB1A          		rr	d
  3708  CB1B          		rr	e
  370A  CB1C          		rr	h
  370C  CB1D          		rr	l
                      	
  370E  79            		ld	a,c
  370F  CB19          		rr	c
  3711  E620          		and	20h		;sticky bit
  3713  B1            		or	c
                      	;	and	0e0h
  3714  4F            		ld	c,a
  3715  10ED          		djnz	ADDFLP2
                      	
  3717                	SAMEEXP:
  3717  226DFF        		ld	(FAC2),hl
  371A  ED536FFF      		ld	(FAC2+2),de
                      	
  371E  EB            		ex	de,hl
                      	
  371F  2A66FF        		ld	hl,(FAC1)
                      	;	ld	de,(FAC2)
  3722  19            		add	hl,de
  3723  2266FF        		ld	(FAC1),hl
  3726  2A68FF        		ld	hl,(FAC1+2)
  3729  ED5B6FFF      		ld	de,(FAC2+2)
  372D  ED5A          		adc	hl,de
  372F  2268FF        		ld	(FAC1+2),hl
                      	
  3732  17            		rla
  3733  E1            		pop	hl		;l-bit0=same sign?
  3734  AD            		xor	l
  3735  0F            		rrca
  3736  301A          		jr	nc,SEARCH1
                      	
  3738  216AFF        		ld	hl,FAC1+4
  373B  34            		inc	(hl)
  373C  CACE03        		jp	z,OVERR
                      	
                      	;	scf			;c-flag=1
  373F  2D            		dec	l		;dec hl,FAC1+3
  3740  CB1E          		rr	(hl)
  3742  2D            		dec	l		;dec hl,FAC1+2
  3743  CB1E          		rr	(hl)
  3745  2D            		dec	l		;dec hl,FAC1+1
  3746  CB1E          		rr	(hl)
  3748  2D            		dec	l		;dec hl,FAC1
  3749  CB1E          		rr	(hl)
                      	
  374B  79            		ld	a,c
  374C  CB19          		rr	c
  374E  E620          		and	20h		;sticky bit
  3750  B1            		or	c
                      	;	and	0e0h
  3751  4F            		ld	c,a
                      	
  3752                	SEARCH1:
  3752  2A66FF        		ld	hl,(FAC1)
  3755  ED5B68FF      		ld	de,(FAC1+2)
                      	;	ld	a,h
                      	;	or	l
                      	;	or	d
                      	;	or	e
                      	;	or	c
                      	;	jp	z,MULFZERO
                      	
  3759                	ADDFLP3:
  3759  CB7A          		bit	7,d
  375B  2014          		jr	nz,CHKROUND
  375D  CB21          		sla	c
  375F  ED6A          		adc	hl,hl
  3761  CB13          		rl	e
  3763  CB12          		rl	d
  3765  3A6AFF        		ld	a,(FAC1+4)
  3768  3D            		dec	a
  3769  326AFF        		ld	(FAC1+4),a
  376C  20EB          		jr	nz,ADDFLP3
  376E  C33338        		jp	MULFZERO	;pop af/jp SETZERO
                      	
                      	;round to the nearest even
  3771                	CHKROUND:
  3771  7D            		ld	a,l
  3772  0F            		rrca
  3773  79            		ld	a,c
  3774  8F            		adc	a,a
  3775  3015          		jr	nc,ADDSGN	;guard bit=0
  3777  2813          		jr	z,ADDSGN	;round bit=0 and sticky bit=0 and l=even
                      	
                      	;round up
  3779  23            		inc	hl
  377A  7C            		ld	a,h
  377B  B5            		or	l
  377C  200E          		jr	nz,ADDSGN
  377E  13            		inc	de
  377F  7A            		ld	a,d
  3780  B3            		or	e
  3781  2009          		jr	nz,ADDSGN
                      	
  3783  216AFF        		ld	hl,FAC1+4
  3786  34            		inc	(hl)
  3787  CACE03        		jp	z,OVERR
                      	;	ld	d,80h
  378A  63            		ld	h,e		;e=0
  378B  6B            		ld	l,e		;e=0
                      	
  378C                	ADDSGN:
  378C  F1            		pop	af		;sign
  378D  AA            		xor	d
  378E  E680          		and	80h
  3790  AA            		xor	d
  3791  57            		ld	d,a
                      	
  3792  C30725        		jp	SETI4
                      	
                      	
                      	;FAC1 = FAC1 * 0.1
                      	;input: FAC1
                      	;output: FAC1
                      	;destroy: FAC2,af,bc,de,hl
  3795                	DIVF10:
  3795  219A37        		ld	hl,EM1
  3798  1808          		jr	SETMULF
                      	
                      	;0.1
  379A                	EM1:
  379A  CDCCCC4C7D    		db	0cdh, 0cch, 0cch, 4ch, 7dh
                      	
                      	
                      	;FAC1 = FAC1 * 10
                      	;input: FAC1
                      	;output: FAC1
                      	;destroy: FAC2,af,bc,de,hl
  379F                	MULF10:
  379F  21A737        		ld	hl,TEN
                      	
                      	;set FAC2 and multiply
                      	;input: FAC1, hl=float address
  37A2                	SETMULF:
  37A2  CD4E0C        		call	SETF2
  37A5  1808          		jr	MULF
                      	
  37A7                	TEN:
  37A7  0000002084    		db	00h, 00h, 00h, 20h, 84h
                      	
                      	
                      	;* operator
  37AC                	O_MUL:
  37AC  CD9B0B        		call	CHKNUM
                      	;	call	MULF
                      	;	ret
                      	
                      	
                      	;FAC1 = FAC1 * FAC2
                      	;input: FAC1,FAC2
                      	;output: FAC1
                      	;destroy: FAC2,af,bc,de,hl
  37AF                	MULF:
                      	
                      	;result=bc-de-de'-hl'
                      	
  37AF  CD4D38        		call	MULDIVSGN
  37B2  CA420C        		jp	z,SETZERO	;FAC2=0
  37B5  D8            		ret	c		;FAC1=0
                      	
  37B6  010000        		ld	bc,0000h
  37B9  50            		ld	d,b
  37BA  59            		ld	e,c
                      	
  37BB  F5            		push	af		;sign
  37BC  D9            		exx
  37BD  E5            		push	hl
  37BE  D5            		push	de
  37BF  C5            		push	bc
                      	
  37C0  2A66FF        		ld	hl,(FAC1)
  37C3  ED5B68FF      		ld	de,(FAC1+2)
                      	;	or	a		;c-flag=0
                      	
  37C7  0620          		ld	b,20h
  37C9                	MULFLP:
                      	;shift result and FAC1
  37C9  D9            		exx
  37CA  CB18          		rr	b
  37CC  CB19          		rr	c
  37CE  CB1A          		rr	d
  37D0  CB1B          		rr	e
  37D2  D9            		exx
  37D3  CB1A          		rr	d
  37D5  CB1B          		rr	e
  37D7  CB1C          		rr	h
  37D9  CB1D          		rr	l
  37DB  300E          		jr	nc,MULFNC1
                      	
                      	;add
  37DD  D9            		exx
  37DE  2A6DFF        		ld	hl,(FAC2)
  37E1  19            		add	hl,de
  37E2  EB            		ex	de,hl
  37E3  2A6FFF        		ld	hl,(FAC2+2)
  37E6  ED4A          		adc	hl,bc
  37E8  44            		ld	b,h
  37E9  4D            		ld	c,l
  37EA  D9            		exx
  37EB                	MULFNC1:
  37EB  10DC          		djnz	MULFLP
                      	
  37ED  D9            		exx
  37EE  216AFF        		ld	hl,FAC1+4
  37F1  300D          		jr	nc,MULFNC2
                      	
                      	;shift result
  37F3  CB18          		rr	b
  37F5  CB19          		rr	c
  37F7  CB1A          		rr	d
  37F9  CB1B          		rr	e
  37FB  D9            		exx
  37FC  CB1A          		rr	d
                      	;	rr	e
                      	;	rr	h
                      	;	rr	l
  37FE  D9            		exx
  37FF  34            		inc	(hl)
                      	
  3800                	MULFNC2:
  3800  35            		dec	(hl)		;;
                      	
                      	;round to the nearest even
  3801  D9            		exx
  3802  7A            		ld	a,d
  3803  8F            		adc	a,a		;check d-bit7
  3804  3041          		jr	nc,NOROUNDUP
  3806  B3            		or	e
  3807  B4            		or	h
  3808  B5            		or	l
  3809  C1            		pop	bc
  380A  D1            		pop	de
  380B  E1            		pop	hl
  380C  D9            		exx
  380D  2004          		jr	nz,ROUNDUP
  380F  7B            		ld	a,e
  3810  0F            		rrca
  3811  300E          		jr	nc,MULFEND
                      	
  3813                	ROUNDUP:
  3813  13            		inc	de
  3814  7A            		ld	a,d
  3815  B3            		or	e
  3816  2009          		jr	nz,MULFEND
  3818  03            		inc	bc
  3819  78            		ld	a,b
  381A  B1            		or	c
  381B  2004          		jr	nz,MULFEND
                      	;	ld	b,80h
  381D  34            		inc	(hl)
  381E  CACE03        		jp	z,OVERR
                      	
  3821                	MULFEND:
  3821  ED5366FF      		ld	(FAC1),de
  3825  ED4368FF      		ld	(FAC1+2),bc
                      	
                      	;exponent
  3829  3A71FF        		ld	a,(FAC2+4)
  382C  86            		add	a,(hl)
  382D  3808          		jr	c,MULFC
  382F  D680          		sub	80h		;;not 81h
  3831  3009          		jr	nc,MULEXP
                      	
  3833                	MULFZERO:
  3833  F1            		pop	af		;cancel
  3834  C3420C        		jp	SETZERO
                      	
  3837                	MULFC:
  3837  D680          		sub	80h		;;not 81h
  3839                	MULFCHKOV:
  3839  D2CE03        		jp	nc,OVERR
  383C                	MULEXP:
  383C  77            		ld	(hl),a		;FAC1+4
  383D  28F4          		jr	z,MULFZERO
  383F  2D            		dec	l		;dec hl,FAC1+3
  3840  F1            		pop	af		;sign
  3841  07            		rlca
  3842  7E            		ld	a,(hl)
  3843  17            		rla
  3844  0F            		rrca
  3845  77            		ld	(hl),a
  3846  C9            		ret
                      	
  3847                	NOROUNDUP:
  3847  C1            		pop	bc
  3848  D1            		pop	de
  3849  E1            		pop	hl
  384A  D9            		exx
  384B  18D4          		jr	MULFEND
                      	
                      	
                      	;check signs and set sign bits of FAC1/2 for MULF/DIVF
                      	;output: a-bit7(xor of FAC1,2 sign), c-flag(1:FAC1=0) z-flag(1:FAC2=0)
                      	;destroy: f,hl
  384D                	MULDIVSGN:
  384D  2171FF        		ld	hl,FAC2+4
  3850  7E            		ld	a,(hl)		;FAC2+4
  3851  B7            		or	a
  3852  C8            		ret	z
  3853  2E6A          		ld	l,FAC1+4-(FAC1+4)/256*256
  3855  7E            		ld	a,(hl)		;FAC1+4
  3856  FE01          		cp	01h
  3858  D8            		ret	c
  3859  2D            		dec	l
  385A  7E            		ld	a,(hl)		;FAC1+3
  385B  CBFE          		set	7,(hl)
  385D  2E70          		ld	l,FAC2+3-(FAC2+3)/256*256
  385F  AE            		xor	(hl)		;FAC2+3, clear c-flag
  3860  CBFE          		set	7,(hl)
  3862  25            		dec	h		;clear z-flag
  3863  C9            		ret
                      	
                      	
                      	;/ operator
  3864                	O_DIV:
  3864  CD9B0B        		call	CHKNUM
                      	;	call	DIVF
                      	;	ret
                      	
                      	
                      	;FAC1 = FAC1 / FAC2
                      	;input: FAC1,FAC2
                      	;output: FAC1
                      	;destroy: FAC2,af,bc,de,hl
  3867                	DIVF:
  3867  CD4D38        		call	MULDIVSGN
  386A  CADD03        		jp	z,DIV0ERR	;FAC2=0
  386D  D8            		ret	c		;FAC1=0
                      	
  386E  210000        		ld	hl,0000h
  3871  54            		ld	d,h
  3872  5D            		ld	e,l
                      	
  3873  F5            		push	af		;sign
  3874  D9            		exx			;
  3875  E5            		push	hl
  3876  D5            		push	de
                      	
  3877  CDB724        		call	CMPINT4
  387A  3007          		jr	nc,DIVFNC
  387C  216AFF        		ld	hl,FAC1+4
  387F  35            		dec	(hl)
  3880  CD3A3F        		call	SLAINT4
                      	
  3883                	DIVFNC:
  3883  0620          		ld	b,20h
  3885                	DIVFLP:
  3885  3805          		jr	c,DIVFC1
  3887  CDB724        		call	CMPINT4
  388A  381A          		jr	c,DIVFC2
  388C                	DIVFC1:
  388C  2A66FF        		ld	hl,(FAC1)
  388F  ED5B6DFF      		ld	de,(FAC2)
  3893  B7            		or	a
  3894  ED52          		sbc	hl,de
  3896  2266FF        		ld	(FAC1),hl
  3899  2A68FF        		ld	hl,(FAC1+2)
  389C  ED5B6FFF      		ld	de,(FAC2+2)
  38A0  ED52          		sbc	hl,de
  38A2  2268FF        		ld	(FAC1+2),hl
  38A5  B7            		or	a		;reset c-flag
  38A6                	DIVFC2:
  38A6  3F            		ccf
  38A7  D9            		exx			;
  38A8  ED6A          		adc	hl,hl
  38AA  CB13          		rl	e
  38AC  CB12          		rl	d
  38AE  D9            		exx			;
                      	
  38AF  CD3A3F        		call	SLAINT4
  38B2  10D1          		djnz	DIVFLP
                      	
  38B4  17            		rla			;store c-flag
  38B5  CDB724        		call	CMPINT4		;no change in a
  38B8  D1            		pop	de
  38B9  E1            		pop	hl
  38BA  D9            		exx
                      	
  38BB  1F            		rra
  38BC  3809          		jr	c,DIVFUP
  38BE  07            		rlca
  38BF  381B          		jr	c,DIVFNOUP
                      	
                      	;round to the nearest even
  38C1  2004          		jr	nz,DIVFUP
  38C3  7D            		ld	a,l
  38C4  0F            		rrca
  38C5  3015          		jr	nc,DIVFNOUP
                      	
                      	;round up
  38C7                	DIVFUP:
  38C7  23            		inc	hl
  38C8  7C            		ld	a,h
  38C9  B5            		or	l
  38CA  2010          		jr	nz,DIVFNOUP
  38CC  13            		inc	de
  38CD  7A            		ld	a,d
  38CE  B3            		or	e
  38CF  200B          		jr	nz,DIVFNOUP
  38D1  216AFF        		ld	hl,FAC1+4
  38D4  34            		inc	(hl)
  38D5  CACE03        		jp	z,OVERR
  38D8  EB            		ex	de,hl		;hl<-0
  38D9  110080        		ld	de,8000h
                      	
  38DC                	DIVFNOUP:
  38DC  2266FF        		ld	(FAC1),hl
  38DF  ED5368FF      		ld	(FAC1+2),de
                      	
  38E3  3A71FF        		ld	a,(FAC2+4)
  38E6  47            		ld	b,a
  38E7  216AFF        		ld	hl,FAC1+4
  38EA  7E            		ld	a,(hl)
  38EB  90            		sub	b
  38EC  3806          		jr	c,DIVF1LT2
                      	
                      	;FAC1>=FAC2
  38EE  C681          		add	a,81h
  38F0  3F            		ccf
  38F1  C33938        		jp	MULFCHKOV
                      	
                      	
                      	;FAC1<FAC2
  38F4                	DIVF1LT2:
  38F4  F23338        		jp	p,MULFZERO
  38F7  C681          		add	a,81h
  38F9  C33C38        		jp	MULEXP
                      	
                      	
                      	;continued from CHKSGN
  38FC                	CHKSGN2:
  38FC  3A69FF        		ld	a,(FAC1+3)
  38FF  07            		rlca
  3900  9F            		sbc	a,a		;00h or ffh
  3901  D8            		ret	c		;ffh
  3902  3C            		inc	a
  3903  C9            		ret			;01h
                      	
                      	
                      	;SGN() function
  3904                	F_SGN:
  3904  CD9B0B        		call	CHKNUM
  3907  EF            		rst	CHKSGN
  3908  C8            		ret	z
  3909  3C            		inc	a
  390A  CA530C        		jp	z,SETMNS1
  390D  C36422        		jp	SETPLS1
                      	
                      	
                      	;(de)->(hl):4
                      	;input: de=source address, hl=destination address
                      	;destroy: b,de,hl
  3910                	_DE2HL4:ds	DE2HL4-_DE2HL4
  3913                		org	DE2HL4
                      	
  3913  0604          		ld	b,04h
  3915                	DE2HL4LP:
  3915  1A            		ld	a,(de)
  3916  77            		ld	(hl),a
  3917  13            		inc	de
  3918  23            		inc	hl
  3919  10FA          		djnz	DE2HL4LP
  391B  C9            		ret
                      	
                      	
                      	;ABS() function
  391C                	F_ABS:
  391C  CD9B0B        		call	CHKNUM
  391F                	ABS:
  391F  2169FF        		ld	hl,FAC1+3
  3922  CBBE          		res	7,(hl)
  3924  C9            		ret
                      	
                      	
                      	;push FAC1
                      	;input: FAC1
                      	;output: sp=sp-6
                      	;destroy: af,hl
  3925                	PUSHF1:
  3925  F1            		pop	af		;return address
  3926  2A69FF        		ld	hl,(FAC1+3)
  3929  E5            		push	hl
  392A  2A67FF        		ld	hl,(FAC1+1)
  392D  E5            		push	hl
  392E  2A65FF        		ld	hl,(FAC1-1)
  3931  E5            		push	hl
  3932  F5            		push	af		;
  3933  C9            		ret
                      	
                      	
                      	;pop FAC1
                      	;input: none
                      	;output: FAC1,sp=sp+6
                      	;destroy: af,hl
  3934                	POPF1:
  3934  F1            		pop	af		;return address
  3935  E1            		pop	hl
  3936  2265FF        		ld	(FAC1-1),hl
  3939  E1            		pop	hl
  393A  2267FF        		ld	(FAC1+1),hl
  393D  E1            		pop	hl
  393E  2269FF        		ld	(FAC1+3),hl
  3941  F5            		push	af		;
  3942  C9            		ret
                      	
                      	
                      	;pop FAC2
                      	;input: none
                      	;output: FAC2,sp=sp+6
                      	;destroy: af,hl
  3943                	POPF2:
  3943  F1            		pop	af		;return address
  3944  E1            		pop	hl
  3945  226CFF        		ld	(FAC2-1),hl
  3948  E1            		pop	hl
  3949  226EFF        		ld	(FAC2+1),hl
  394C  E1            		pop	hl
  394D  2270FF        		ld	(FAC2+3),hl
  3950  F5            		push	af		;
  3951  C9            		ret
                      	
                      	
                      	;copy FAC1 to FAC2
                      	;input: FAC1
                      	;output: FAC2
                      	;destroy: hl
  3952                	CPYFAC:
  3952  2A65FF        		ld	hl,(FAC1-1)
  3955  226CFF        		ld	(FAC2-1),hl
  3958  2A67FF        		ld	hl,(FAC1+1)
  395B  226EFF        		ld	(FAC2+1),hl
  395E  2A69FF        		ld	hl,(FAC1+3)
  3961  2270FF        		ld	(FAC2+3),hl
  3964  C9            		ret
                      	
                      	
                      	;exchange FAC1 and FAC2
                      	;input: FAC1,FAC2
                      	;output: FAC1,FAC2
                      	;destroy: hl
  3965                	EXFAC:
  3965  F5            		push	af
  3966  CD1A3C        		call	PUSHF2
  3969  CD5239        		call	CPYFAC
  396C  CD3439        		call	POPF1
  396F  F1            		pop	af
  3970  C9            		ret
                      	
                      	
                      	;print 1-byte integer in device (unsigned)
                      	;input: a
                      	;destroy: af,bc,de,hl
  3971                	_PUTI1:	ds	PUTI1-_PUTI1
  397A                		org	PUTI1
                      	
  397A  2600          		ld	h,00h
  397C  6F            		ld	l,a
  397D  C3A13A        		jp	PUTI2
                      	
                      	
                      	;INT() function
                      	;(round toward minus infinity)
  3980                	F_INT:
  3980  CD9B0B        		call	CHKNUM
  3983                	INT:
  3983  CD5239        		call	CPYFAC
  3986  CDD00C        		call	GETINT
  3989  CD1B3F        		call	CMPF
  398C  C8            		ret	z
  398D  D8            		ret	c
  398E  21A60E        		ld	hl,MNSONE
  3991  C38F36        		jp	SETADDF
                      	
                      	
                      	;check BASIC mode
                      	;output: a=mode-1, z-flag(1=mode 5)
                      	;destroy: f
  3994                	_CHKMOD:
  3994                		ds	CHKMOD-_CHKMOD
  3995                		org	CHKMOD
                      	
  3995  3A65FE        		ld	a,(MODE)
  3998  FE04          		cp	04h
  399A  C9            		ret
                      	
                      	
                      	;convert string to float
                      	;input: hl=address
                      	;output: FAC1, hl=next address
                      	;destroy: FAC2, af,bc,de
  399B                	ATOF:
  399B  E5            		push	hl
  399C  CD420C        		call	SETZERO
  399F  E1            		pop	hl
  39A0  CD883A        		call	SKIPSPA
  39A3  C8            		ret	z
  39A4  B7            		or	a
  39A5  F8            		ret	m		;reject PLUS_ and MINUS_ in VAL()
  39A6  FE26          		cp	'&'
  39A8  CA3F3A        		jp	z,ATOFHEX
                      	
  39AB  CDF327        		call	ATOIF
                      	
  39AE  CD883A        		call	SKIPSPA
  39B1  C8            		ret	z
  39B2  23            		inc	hl
  39B3  0E00          		ld	c,00h		;after-dot counter
  39B5  FE2E          		cp	'.'
  39B7  280E          		jr	z,ATOFDOT
  39B9  F620          		or	20h
  39BB  FE65          		cp	'e'
  39BD  281D          		jr	z,ATOFEXP
  39BF  2B            		dec	hl
  39C0  C9            		ret
                      	
  39C1                	ATOFLP1:
  39C1  CD1828        		call	CTOF
  39C4  3008          		jr	nc,CHKEXP
  39C6  0C            		inc	c
  39C7                	ATOFDOT:
  39C7  CD883A        		call	SKIPSPA
  39CA  20F5          		jr	nz,ATOFLP1
  39CC  180E          		jr	ATOFEXP
                      	
  39CE                	CHKEXP:
  39CE  CD883A        		call	SKIPSPA
  39D1  2814          		jr	z,ATOFE0
  39D3  F620          		or	20h
  39D5  FE65          		cp	'e'
  39D7  200E          		jr	nz,ATOFE0
  39D9  23            		inc	hl
  39DA  1800          		jr	ATOFEXP
                      	
  39DC                	ATOFEXP:
  39DC  EB            		ex	de,hl
  39DD  CD2539        		call	PUSHF1
  39E0  EB            		ex	de,hl
  39E1  C5            		push	bc		;c=after-dot counter
                      	
  39E2  CDF327        		call	ATOIF
  39E5  180B          		jr	CALCEXP
                      	
  39E7                	ATOFE0:
  39E7  EB            		ex	de,hl
  39E8  CD2539        		call	PUSHF1
  39EB  EB            		ex	de,hl
  39EC  C5            		push	bc		;c=after-dot counter
                      	
  39ED  E5            		push	hl
  39EE  CD420C        		call	SETZERO
  39F1  E1            		pop	hl
                      	
  39F2                	CALCEXP:
  39F2  C1            		pop	bc		;c=after-dot counter
  39F3  E5            		push	hl		;address
  39F4  CD5239        		call	CPYFAC
  39F7  69            		ld	l,c		;c=after-dot counter
  39F8  CD7E0C        		call	I1TOF
  39FB  CD143E        		call	SUBF21
  39FE  D1            		pop	de		;address
                      	
  39FF  CD5239        		call	CPYFAC
  3A02  CD3439        		call	POPF1
  3A05  D5            		push	de		;address
                      	
  3A06  2170FF        		ld	hl,FAC2+3
  3A09  7E            		ld	a,(hl)
  3A0A  F5            		push	af		;E sign
  3A0B  CBBE          		res	7,(hl)
                      	
  3A0D                	ATOFLP2:
  3A0D  3A71FF        		ld	a,(FAC2+4)
  3A10  B7            		or	a
  3A11  2829          		jr	z,ATOFEND	;E0
                      	
  3A13  3A6AFF        		ld	a,(FAC1+4)
  3A16  B7            		or	a
  3A17  2823          		jr	z,ATOFEND	;=0
                      	
  3A19  CD2539        		call	PUSHF1
  3A1C  CD530C        		call	SETMNS1
  3A1F  CD9E36        		call	ADDF
  3A22  CD5239        		call	CPYFAC
  3A25  CD3439        		call	POPF1
                      	
  3A28  C1            		pop	bc		;b=E sign
  3A29  C5            		push	bc
  3A2A  CD1A3C        		call	PUSHF2
  3A2D  78            		ld	a,b
  3A2E  07            		rlca
  3A2F  F5            		push	af
  3A30  D49F37        		call	nc,MULF10
  3A33  F1            		pop	af
  3A34  DC9537        		call	c,DIVF10
  3A37  CD4339        		call	POPF2
                      	
  3A3A  18D1          		jr	ATOFLP2
                      	
  3A3C                	ATOFEND:
  3A3C  F1            		pop	af
  3A3D  E1            		pop	hl		;address
  3A3E  C9            		ret
                      	
  3A3F                	ATOFHEX:
  3A3F  CD483A        		call	HEX
  3A42  EB            		ex	de,hl
  3A43  CD180D        		call	S2TOF
  3A46  EB            		ex	de,hl
  3A47  C9            		ret
                      	
                      	
                      	;convert hexadecimal string to integer
                      	;input: hl="&" address
                      	;output: de=value, hl=next address
                      	;destroy: af,c,de
  3A48                	HEX:
  3A48  CD873A        		call	SKIPSPAINC
  3A4B  CAC203        		jp	z,SNERR
  3A4E  F620          		or	20h
  3A50  D668          		sub	'h'		;
  3A52  C2C203        		jp	nz,SNERR
                      	
  3A55  57            		ld	d,a		;=0
  3A56  5F            		ld	e,a		;=0
  3A57  0E05          		ld	c,05h
  3A59                	HEXLP:
  3A59  D5            		push	de
  3A5A  CD873A        		call	SKIPSPAINC
  3A5D  D1            		pop	de
  3A5E  C8            		ret	z
  3A5F  FE93          		cp	DEF_		;93h
  3A61  281D          		jr	z,HEXDEF
  3A63  D630          		sub	'0'
  3A65  FE0A          		cp	'9'-'0'+1
  3A67  3809          		jr	c,HEX0F
  3A69  F620          		or	20h
  3A6B  D631          		sub	'a'-'0'
  3A6D  FE06          		cp	'f'-'a'+1
  3A6F  D0            		ret	nc		;not hex
  3A70  C60A          		add	a,0ah
  3A72                	HEX0F:
  3A72  0D            		dec	c
  3A73  CACE03        		jp	z,OVERR
  3A76  EB            		ex	de,hl
  3A77  29            		add	hl,hl		;*2
  3A78  29            		add	hl,hl		;*4
  3A79  29            		add	hl,hl		;*8
  3A7A  29            		add	hl,hl		;*16
  3A7B  85            		add	a,l		;no carry
  3A7C  6F            		ld	l,a
  3A7D  EB            		ex	de,hl
  3A7E  18D9          		jr	HEXLP
                      	
  3A80                	HEXDEF:
  3A80  53            		ld	d,e
  3A81  1EDE          		ld	e,0deh
  3A83  3E0F          		ld	a,0fh
  3A85  18EB          		jr	HEX0F
                      	
                      	
                      	;increment hl and skip space and check (STREND) for ATOF
  3A87                	SKIPSPAINC:
  3A87  23            		inc	hl
                      	
                      	;skip space and check (STREND) for ATOF
                      	;input: hl=address
                      	;output: a=data, hl=data address, z-flag(1=no data)
                      	;destroy: f,de
  3A88                	SKIPSPA:
  3A88  ED5B27FF      		ld	de,(STREND)
  3A8C  13            		inc	de
  3A8D                	SKIPSPALP:
  3A8D  E7            		rst	CPHLDE
  3A8E  C8            		ret	z
  3A8F  7E            		ld	a,(hl)
  3A90  B7            		or	a
  3A91  C8            		ret	z
  3A92  FE20          		cp	' '
  3A94  C0            		ret	nz
  3A95  23            		inc	hl
  3A96  18F5          		jr	SKIPSPALP
                      	
                      	
                      	;print "in *****"
                      	;input: hl
                      	;destory: af,bc,de,hl
  3A98                	_INLNUM:
  3A98                		ds	INLNUM-_INLNUM
  3A99                		org	INLNUM
                      	
  3A99  E5            		push	hl		;
  3A9A  218B03        		ld	hl,ERRIN
  3A9D  CDCF30        		call	PUTS
  3AA0  E1            		pop	hl		;
                      	;	jp	PUTI2
                      	
                      	
                      	;print 2-byte integer in device (unsigned)
                      	;input: hl
                      	;destory: af,bc,de,hl
  3AA1                	_PUTI2:	ds	PUTI2-_PUTI2
  3AA1                		org	PUTI2
                      	
  3AA1  11CE30        		ld	de,PUTSINC
  3AA4  D5            		push	de
                      	;	jp	I2TOA
                      	
                      	
                      	;convert 2-byte integer (unsigned) to string
                      	;input: hl
                      	;output: FAC3, hl=FAC3
                      	;destroy: af,bc,de
  3AA5                	_I2TOA:	ds	I2TOA-_I2TOA
  3AA5                		org	I2TOA
                      	
  3AA5  CD800C        		call	I2TOF
  3AA8  C3AC3A        		jp	FTOA
                      	
                      	
                      	;convert float to string
                      	;input: FAC1
                      	;output: FAC3, hl=FAC3
                      	;destroy: FAC1,FAC2,af,bc,de
  3AAB                	_FTOA:	ds	FTOA-_FTOA
  3AAC                		org	FTOA
                      	
  3AAC  2172FF        		ld	hl,FAC3
  3AAF  3620          		ld	(hl),' '
  3AB1  EF            		rst	CHKSGN
  3AB2  CA603B        		jp	z,FTOAZERO	;a=0
  3AB5  3D            		dec	a
  3AB6  2802          		jr	z,FTOAPLS	;if FAC1>0
  3AB8  362D          		ld	(hl),'-'
  3ABA                	FTOAPLS:
  3ABA  CD1F39        		call	ABS
  3ABD  21693B        		ld	hl,E8
  3AC0  CD183F        		call	SETCMPF
  3AC3  3071          		jr	nc,LARGE	;FAC1 >= 1e8
  3AC5  217D3B        		ld	hl,EM2
  3AC8  CD183F        		call	SETCMPF
  3ACB  3856          		jr	c,SMALL		;FAC1 < 1e-2
                      	
  3ACD                	FTOA2:
  3ACD  0609          		ld	b,09h
  3ACF                	FTOALP1:
  3ACF  C5            		push	bc
  3AD0  216E3B        		ld	hl,E7
  3AD3  CD183F        		call	SETCMPF
  3AD6  3007          		jr	nc,FTOAOK
  3AD8  CD9F37        		call	MULF10
  3ADB  C1            		pop	bc
  3ADC  10F1          		djnz	FTOALP1
  3ADE  C5            		push	bc
                      	
  3ADF                	FTOAOK:
  3ADF  3A6AFF        		ld	a,(FAC1+4)
  3AE2  D697          		sub	97h
  3AE4  47            		ld	b,a
  3AE5  3A66FF        		ld	a,(FAC1)
  3AE8                	FTOALP2:
  3AE8  07            		rlca
  3AE9  10FD          		djnz	FTOALP2
  3AEB  DC8C36        		call	c,INCF1		;round up
                      	
  3AEE  CDEB24        		call	FTOI4
                      	
  3AF1  21823B        		ld	hl,INTE7
  3AF4  1173FF        		ld	de,FAC3+1
  3AF7  C1            		pop	bc
  3AF8  48            		ld	c,b		;
  3AF9  05            		dec	b
  3AFA                	FTOALP3:
  3AFA  05            		dec	b
  3AFB  FA073B        		jp	m,FTOADOT
  3AFE  CD8B24        		call	DIVINT4
  3B01  C630          		add	a,'0'
  3B03  12            		ld	(de),a
  3B04  13            		inc	de
  3B05  18F3          		jr	FTOALP3
                      	
  3B07                	FTOADOT:
  3B07  CDDE24        		call	CHKINT4
  3B0A  2812          		jr	z,FTOAEND
                      	
  3B0C  3E2E          		ld	a,'.'
  3B0E  12            		ld	(de),a
  3B0F  13            		inc	de
                      	
  3B10  79            		ld	a,c		;
  3B11  B7            		or	a
  3B12                	FTOALP4:
  3B12  C48B24        		call	nz,DIVINT4
  3B15  C630          		add	a,'0'
  3B17  12            		ld	(de),a
  3B18  13            		inc	de
  3B19  CDDE24        		call	CHKINT4
  3B1C  20F4          		jr	nz,FTOALP4
                      	
  3B1E                	FTOAEND:
  3B1E  12            		ld	(de),a		;a=0
  3B1F  2172FF        		ld	hl,FAC3
  3B22  C9            		ret
                      	
  3B23                	SMALL:
  3B23  012D00        		ld	bc,002dh	;c='-'
  3B26                	SMALLLP:
  3B26  04            		inc	b
  3B27  C5            		push	bc
  3B28  CD9F37        		call	MULF10
  3B2B  21783B        		ld	hl,E0
  3B2E  CD183F        		call	SETCMPF
  3B31  C1            		pop	bc
  3B32  38F2          		jr	c,SMALLLP
  3B34  1811          		jr	LARSMA
                      	
  3B36                	LARGE:
  3B36  012B00        		ld	bc,002bh	;c='+'
  3B39                	LARGELP:
  3B39  04            		inc	b
  3B3A  C5            		push	bc
  3B3B  CD9537        		call	DIVF10
  3B3E  21733B        		ld	hl,E2
  3B41  CD183F        		call	SETCMPF
  3B44  C1            		pop	bc
  3B45  30F2          		jr	nc,LARGELP
                      	
  3B47                	LARSMA:
  3B47  C5            		push	bc		;
  3B48  CDCD3A        		call	FTOA2
                      	
  3B4B                	LARLP2:
  3B4B  23            		inc	hl
  3B4C  7E            		ld	a,(hl)
  3B4D  B7            		or	a
  3B4E  20FB          		jr	nz,LARLP2
                      	
  3B50  3645          		ld	(hl),'E'
  3B52  23            		inc	hl
  3B53  C1            		pop	bc		;
  3B54  78            		ld	a,b
  3B55  71            		ld	(hl),c
  3B56  23            		inc	hl
  3B57  010A2F        		ld	bc,2f0ah	;b='0'-1, c=10
  3B5A                	LARLP3:
  3B5A  04            		inc	b
  3B5B  91            		sub	c
  3B5C  30FC          		jr	nc,LARLP3
  3B5E  81            		add	a,c
  3B5F  70            		ld	(hl),b
                      	
  3B60                	FTOAZERO:
  3B60  23            		inc	hl
  3B61  C630          		add	a,'0'
  3B63  77            		ld	(hl),a
  3B64  23            		inc	hl
  3B65  EB            		ex	de,hl
  3B66  AF            		xor	a
  3B67  18B5          		jr	FTOAEND
                      	
                      	
                      	;99999999.5
  3B69                	E8:
  3B69  F01FBC3E9B    		db	0f0h, 1fh, 0bch, 3eh, 9bh
                      	
                      	;9999999.95
  3B6E                	E7:
  3B6E  F37F961898    		db	0f3h, 7fh, 96h, 18h, 98h
                      	
                      	;9.99999995
  3B73                	E2:
  3B73  F3FFFF1F84    		db	0f3h, 0ffh, 0ffh, 1fh, 84h
                      	
                      	;0.999999995
  3B78                	E0:
  3B78  EBFFFF7F80    		db	0ebh, 0ffh, 0ffh, 7fh, 80h
                      	
                      	;0.00999999995
  3B7D                	EM2:
  3B7D  300AD7237A    		db	30h, 0ah, 0d7h, 23h, 7ah
                      	
  3B82                	INTE7:
  3B82  80969800      		db	80h, 96h, 98h, 00h	;10^7
  3B86  40420F00      		db	40h, 42h, 0fh, 00h	;10^6
  3B8A  A0860100      		db	0a0h,86h, 01h, 00h	;10^5
  3B8E  10270000      		db	10h, 27h, 00h, 00h	;10^4
  3B92  E8030000      		db	0e8h,03h, 00h, 00h	;10^3
  3B96  64000000      		db	64h, 00h, 00h, 00h	;10^2
  3B9A  0A000000      		db	0ah, 00h, 00h, 00h	;10^1
  3B9E  01000000      		db	01h, 00h, 00h, 00h	;10^0
                      	
                      	
                      	;RND() function
  3BA2                	_F_RND:	ds	RNDPLS-9-_F_RND
  3BA6                		org	RNDPLS-9
  3BA6                	F_RND:
  3BA6  CD9B0B        		call	CHKNUM
  3BA9  EF            		rst	CHKSGN
  3BAA  2815          		jr	z,RND0		;rnd(0)
  3BAC  3C            		inc	a
  3BAD  2806          		jr	z,RNDMNS	;rnd(-x)
                      	
                      	;rnd(+x)
  3BAF                	_RNDPLS:ds	RNDPLS-_RNDPLS
  3BAF                		org	RNDPLS
                      	
  3BAF  2151FA        		ld	hl,RSEED
  3BB2  CD450C        		call	SETF1
  3BB5                	RNDMNS:
  3BB5  CDCC24        		call	DECINT4
  3BB8  21D53B        		ld	hl,RNDFCT
  3BBB  CD4E0C        		call	SETF2
  3BBE  CDD93B        		call	MULRND
  3BC1                	RND0:
  3BC1  2A51FA        		ld	hl,(RSEED)
  3BC4  ED5B53FA      		ld	de,(RSEED+2)
  3BC8  CD0F25        		call	I4TOF
  3BCB  216AFF        		ld	hl,FAC1+4
  3BCE  7E            		ld	a,(hl)
  3BCF  B7            		or	a
  3BD0  C8            		ret	z
  3BD1  D620          		sub	20h
  3BD3  77            		ld	(hl),a
  3BD4  C9            		ret
                      	
                      	
  3BD5                	RNDFCT:
  3BD5  65520F00      		db	65h, 52h, 0fh, 00h
                      	
                      	
                      	;RSEED = (FAC1 (integer) * FAC2 (integer)) & ff,ff,ff,ff,ffh
                      	; for RND() function
                      	;input: FAC1,FAC2 (not zero)
                      	;output: RSEED
                      	;destroy: FAC1,af,bc,de,hl
  3BD9                	MULRND:
  3BD9  21AB0E        		ld	hl,ZERO
  3BDC  1151FA        		ld	de,RSEED
  3BDF  CD480C        		call	SETF
  3BE2  AF            		xor	a
  3BE3  326AFF        		ld	(FAC1+4),a
                      	
  3BE6  D9            		exx			;
  3BE7  E5            		push	hl
  3BE8  D5            		push	de
  3BE9  2A6DFF        		ld	hl,(FAC2)
  3BEC  ED5B6FFF      		ld	de,(FAC2+2)
  3BF0  D9            		exx			;
                      	
  3BF1  0E20          		ld	c,20h
  3BF3                	MULRLP1:
  3BF3  D9            		exx			;
  3BF4  CB3A          		srl	d
  3BF6  CB1B          		rr	e
  3BF8  CB1C          		rr	h
  3BFA  CB1D          		rr	l
  3BFC  D9            		exx			;
  3BFD  3010          		jr	nc,MULRNC
                      	
  3BFF  1166FF        		ld	de,FAC1
  3C02  2151FA        		ld	hl,RSEED
  3C05  B7            		or	a
  3C06  0604          		ld	b,04h
  3C08                	MULRLP2:
  3C08  1A            		ld	a,(de)
  3C09  8E            		adc	a,(hl)
  3C0A  77            		ld	(hl),a
  3C0B  1C            		inc	e		;inc de
  3C0C  2C            		inc	l		;inc hl
  3C0D  10F9          		djnz	MULRLP2
                      	
  3C0F                	MULRNC:
  3C0F  CD3A3F        		call	SLAINT4
  3C12  0D            		dec	c
  3C13  20DE          		jr	nz,MULRLP1
                      	
  3C15  D9            		exx			;
  3C16  D1            		pop	de
  3C17  E1            		pop	hl
  3C18  D9            		exx			;
  3C19  C9            		ret
                      	
                      	
                      	;push FAC2
                      	;input: FAC2
                      	;output: sp=sp-6
                      	;destroy: af,hl
  3C1A                	PUSHF2:
  3C1A  F1            		pop	af		;return address
  3C1B  2A70FF        		ld	hl,(FAC2+3)
  3C1E  E5            		push	hl
  3C1F  2A6EFF        		ld	hl,(FAC2+1)
  3C22  E5            		push	hl
  3C23  2A6CFF        		ld	hl,(FAC2-1)
  3C26  E5            		push	hl
  3C27  F5            		push	af		;
  3C28  C9            		ret
                      	
                      	
                      	;EXP() function
                      	;exp(x)=2^n * e^a, n=[x/log2], a=x-n*log2
  3C29                	F_EXP:
  3C29  CD9B0B        		call	CHKNUM
  3C2C                	EXP:
  3C2C  CD2539        		call	PUSHF1		;x
  3C2F  21993C        		ld	hl,LOG2
  3C32  CD4E0C        		call	SETF2
  3C35  CD6738        		call	DIVF		;x/log2
                      	
  3C38  2A69FF        		ld	hl,(FAC1+3)
  3C3B  7C            		ld	a,h		;(FAC1+4)
  3C3C  FE89          		cp	89h
  3C3E  3050          		jr	nc,EXPLARGE	;|x/log2|>=256
                      	
  3C40  CD8339        		call	INT		;n=[x/log2]
  3C43  CD9F0C        		call	FTOI
  3C46  D5            		push	de		;n
  3C47  21993C        		ld	hl,LOG2
  3C4A  CDA237        		call	SETMULF		;n*log2
  3C4D  D1            		pop	de		;n
  3C4E  CD4339        		call	POPF2		;x
  3C51  D5            		push	de		;n
  3C52  CD143E        		call	SUBF21		;a=x-n*log2
                      	
                      	;1+a(1+a/2(1+a/3(1+...(1+a/10))))))))))
  3C55  CD5239        		call	CPYFAC
  3C58  CD6422        		call	SETPLS1
                      	
  3C5B  0E0A          		ld	c,0ah
  3C5D                	EXPLP:
  3C5D  CD1A3C        		call	PUSHF2		;a
  3C60  C5            		push	bc
  3C61  CDAF37        		call	MULF		;a*y
  3C64  CD5239        		call	CPYFAC
  3C67  E1            		pop	hl
  3C68  E5            		push	hl
  3C69  CD7E0C        		call	I1TOF
  3C6C  CD6539        		call	EXFAC
  3C6F  CD6738        		call	DIVF		;a*y/b
  3C72  CD8C36        		call	INCF1		;a*y/b+1
  3C75  C1            		pop	bc
  3C76  CD4339        		call	POPF2		;a
  3C79  0D            		dec	c
  3C7A  20E1          		jr	nz,EXPLP
                      	
  3C7C  D1            		pop	de		;-256<=n<=255
                      	;	ld	a,(FAC1+4)	;=81h?
  3C7D  3E81          		ld	a,81h
  3C7F  83            		add	a,e
  3C80  326AFF        		ld	(FAC1+4),a	;exponent
  3C83  CB7A          		bit	7,d
  3C85  2004          		jr	nz,EXPNZ
  3C87  D0            		ret	nc		;0<=n<=126
  3C88                	EXPOV:
  3C88  C3CE03        		jp	OVERR		;127<=n<=255
  3C8B                	EXPNZ:
  3C8B  3D            		dec	a
  3C8C  F0            		ret	p		;-128<=n<=-1
  3C8D                	EXPZERO:
  3C8D  C3420C        		jp	SETZERO		;-256<=n<=-129
                      	
  3C90                	EXPLARGE:
  3C90  CB7D          		bit	7,l		;(FAC1+3)
  3C92  28F4          		jr	z,EXPOV		;x/log2>=256
  3C94  CD3439        		call	POPF1
  3C97  18F4          		jr	EXPZERO		;x/log2<=-256
                      	
                      	;log(2)
  3C99                	LOG2:
  3C99  F817723180    		db	0f8h, 17h, 72h, 31h, 80h
                      	
                      	
                      	;LOG() function
                      	;log(x)=log(a*2^n)=log(a)+n*log(2)
                      	;a'=(a-1)/(a+1)
                      	;log(a)=2*(a'+a'^3/3+a'^5/5+...)=2a'(1+1/3*a'^2(1+3/5*a'^2(...(1+19/21*a'^2))))))))))
  3C9E                	F_LOG:
  3C9E  CD9B0B        		call	CHKNUM
  3CA1                	LOG:
  3CA1  EF            		rst	CHKSGN
  3CA2  3D            		dec	a
  3CA3  C2CB03        		jp	nz,FCERR	;x<=0
                      	
  3CA6  216AFF        		ld	hl,FAC1+4
  3CA9  7E            		ld	a,(hl)
  3CAA  D681          		sub	81h
  3CAC  F5            		push	af		;n
  3CAD  3681          		ld	(hl),81h	;a
  3CAF  CD2539        		call	PUSHF1		;a
  3CB2  CD8C36        		call	INCF1
  3CB5  CD4339        		call	POPF2		;a
  3CB8  CD2539        		call	PUSHF1		;a+1
  3CBB  CD530C        		call	SETMNS1
  3CBE  CD9E36        		call	ADDF		;a-1
  3CC1  CD4339        		call	POPF2		;a+1
  3CC4  CD6738        		call	DIVF		;a'=(a-1)/(a+1)
  3CC7  CD2539        		call	PUSHF1		;a'
  3CCA  CD5239        		call	CPYFAC
  3CCD  CDAF37        		call	MULF		;a'^2
  3CD0  CD5239        		call	CPYFAC		;a'^2
  3CD3  CD6422        		call	SETPLS1		;y0
                      	
  3CD6  0E12          		ld	c,12h
  3CD8                	LOGLP:
  3CD8  CD1A3C        		call	PUSHF2		;a'^2
  3CDB  C5            		push	bc
  3CDC  CDAF37        		call	MULF		;y*a'^2
  3CDF  CD5239        		call	CPYFAC
  3CE2  E1            		pop	hl		;l=c
  3CE3  E5            		push	hl
  3CE4  2C            		inc	l
  3CE5  CD7E0C        		call	I1TOF
  3CE8  CD6539        		call	EXFAC
  3CEB  CD6738        		call	DIVF		;y*a'^2/(c+1)
  3CEE  CD5239        		call	CPYFAC
  3CF1  E1            		pop	hl
  3CF2  2D            		dec	l
  3CF3  E5            		push	hl
  3CF4  CD7E0C        		call	I1TOF		;c-1
  3CF7  CDAF37        		call	MULF		;y*a'^2*(c-1)/(c+1)
  3CFA  CD8C36        		call	INCF1		;y=1+y*a'^2*(c-1)/(c+1)
  3CFD  C1            		pop	bc
  3CFE  CD4339        		call	POPF2		;a'^2
  3D01  0D            		dec	c
  3D02  20D4          		jr	nz,LOGLP
                      	
  3D04  CD4339        		call	POPF2		;a'
  3D07  CDAF37        		call	MULF		;a'*y
  3D0A  216AFF        		ld	hl,FAC1+4
  3D0D  7E            		ld	a,(hl)
  3D0E  B7            		or	a
  3D0F  2804          		jr	z,LOGZ
  3D11  34            		inc	(hl)		;2*a'*y
  3D12  CACE03        		jp	z,OVERR
  3D15                	LOGZ:
  3D15  C1            		pop	bc		;b=n
  3D16  CD2539        		call	PUSHF1		;2*a'*y
  3D19  CD110D        		call	S1TOF
  3D1C  21993C        		ld	hl,LOG2
  3D1F  CDA237        		call	SETMULF		;n*log(2)
  3D22  CD4339        		call	POPF2		;2*a'*y
  3D25  C39E36        		jp	ADDF		;n*log(2)+2*a'*y
                      	
                      	
                      	;^ operator
  3D28                	O_POW:
  3D28  CD9B0B        		call	CHKNUM
                      	
  3D2B  2A70FF        		ld	hl,(FAC2+3)
  3D2E  7C            		ld	a,h		;(FAC2+4)
  3D2F  B7            		or	a
  3D30  CA6422        		jp	z,SETPLS1	;x^0
                      	
  3D33  EF            		rst	CHKSGN
  3D34  2812          		jr	z,POWZERO
  3D36  3C            		inc	a
  3D37  2815          		jr	z,POWNEG
                      	
                      	;x^y=exp(ylog(x))
  3D39                	POWPOS:
  3D39  CD1A3C        		call	PUSHF2		;y
  3D3C  CDA13C        		call	LOG		;log(x)
  3D3F  CD4339        		call	POPF2		;y
  3D42  CDAF37        		call	MULF		;ylog(x)
  3D45  C32C3C        		jp	EXP
                      	
                      	;0^y
  3D48                	POWZERO:
  3D48  7D            		ld	a,l		;(FAC2+3)
  3D49  07            		rlca
  3D4A  D0            		ret	nc		;0^(positive)
  3D4B  C3DD03        		jp	DIV0ERR		;0^(negative)
                      	
                      	;(negative)^y
  3D4E                	POWNEG:
  3D4E  7C            		ld	a,h		;(FAC2+4)
  3D4F  FEA1          		cp	0a1h
  3D51  D2CB03        		jp	nc,FCERR
                      	
  3D54  CD2539        		call	PUSHF1
  3D57  CD1A3C        		call	PUSHF2
  3D5A  CD3439        		call	POPF1
                      	
  3D5D  CDD00C        		call	GETINT
  3D60  CD1B3F        		call	CMPF
  3D63  C2CB03        		jp	nz,FCERR	;y=not integer
  3D66  CDEB24        		call	FTOI4
  3D69  3A66FF        		ld	a,(FAC1)	;even/odd
  3D6C  47            		ld	b,a
                      	
  3D6D  CD3439        		call	POPF1
  3D70  C5            		push	bc		;
  3D71  CD1F39        		call	ABS
  3D74  CD393D        		call	POWPOS
  3D77  F1            		pop	af		;
  3D78  0F            		rrca
  3D79  D0            		ret	nc		;y=even
  3D7A  C32D0D        		jp	NEGABSNZ	;y=odd
                      	
                      	
                      	;COS() function
                      	;cos(x)=sin(pi/2-|x|)
  3D7D                	F_COS:
  3D7D  CD9B0B        		call	CHKNUM
  3D80                	COS:
  3D80  CD280D        		call	NEGABS
  3D83  218B3D        		ld	hl,PIDIV2
  3D86  CD8F36        		call	SETADDF		;pi/2-|x|
  3D89  1808          		jr	SIN
                      	
                      	;pi/2
  3D8B                	PIDIV2:
  3D8B  A2DA0F4981    		db	0a2h, 0dah, 0fh, 49h, 81h
                      	
                      	
                      	;SIN() function
                      	;x-x^3/3!+x^5/5!-...=x(1-x^2/(2*3)(1-x^2/(4*5)(...(1-x^2/(12*13)))))))))))
  3D90                	F_SIN:
  3D90  CD9B0B        		call	CHKNUM
  3D93                	SIN:
  3D93  CD2539        		call	PUSHF1		;x
  3D96  211A3E        		ld	hl,PI2
  3D99  CD4E0C        		call	SETF2
  3D9C  CD6738        		call	DIVF		;x/2pi
  3D9F  CDD00C        		call	GETINT		;int(x/2pi)
  3DA2  211A3E        		ld	hl,PI2
  3DA5  CDA237        		call	SETMULF		;int(x/2pi)*2pi
  3DA8  CD4339        		call	POPF2		;x
  3DAB  CD143E        		call	SUBF21		;x'=x-int(x/2pi)*2pi
                      	
  3DAE  CDFF3D        		call	SINRANGE	;[-pi,+pi]
  3DB1  CDFF3D        		call	SINRANGE	;[-pi/2,+pi/2]
                      	
  3DB4  CD2539        		call	PUSHF1		;x'
  3DB7  CD5239        		call	CPYFAC
  3DBA  CDAF37        		call	MULF		;x'^2
                      	
  3DBD  CD5239        		call	CPYFAC
  3DC0  CD6422        		call	SETPLS1		;y=1
                      	
  3DC3  060C          		ld	b,0ch
  3DC5                	SINLP:
  3DC5  CD1A3C        		call	PUSHF2		;x'^2
  3DC8  C5            		push	bc
  3DC9  CDAF37        		call	MULF		;x'^2*y
  3DCC  C1            		pop	bc
  3DCD  C5            		push	bc
  3DCE  CD2539        		call	PUSHF1		;x'^2*y
                      	
  3DD1  78            		ld	a,b
  3DD2  04            		inc	b
  3DD3  CDE821        		call	MULINT1		;hl=b(b+1)
  3DD6  CD800C        		call	I2TOF		;hl->FAC1
                      	
  3DD9  CD5239        		call	CPYFAC
  3DDC  CD3439        		call	POPF1		;x'^2*y
  3DDF  CD6738        		call	DIVF		;x'^2/b/(b+1)*y
  3DE2  CD280D        		call	NEGABS		;-x'^2/b/(b+1)*y (y>0)
  3DE5  CD8C36        		call	INCF1		;y=1-x'^2/b/(b+1)*y
                      	
  3DE8  C1            		pop	bc
  3DE9  CD4339        		call	POPF2		;x'^2
  3DEC  05            		dec	b
  3DED  10D6          		djnz	SINLP
                      	
  3DEF  CD4339        		call	POPF2		;x'
  3DF2  CDAF37        		call	MULF
  3DF5  3A6AFF        		ld	a,(FAC1+4)
  3DF8  D681          		sub	81h
  3DFA  C0            		ret	nz
                      	
                      	;if |result|>=1
  3DFB  3266FF        		ld	(FAC1),a	;=0
  3DFE  C9            		ret
                      	
                      	;[-pi/2,+pi/2]
  3DFF                	SINRANGE:
  3DFF  218B3D        		ld	hl,PIDIV2	;pi/2
  3E02  CD183F        		call	SETCMPF
  3E05  3009          		jr	nc,RANGENC
  3E07  2170FF        		ld	hl,FAC2+3
  3E0A  CBFE          		set	7,(hl)		;-pi/2
  3E0C  CD1B3F        		call	CMPF
  3E0F  D0            		ret	nc
  3E10                	RANGENC:
  3E10  2171FF        		ld	hl,FAC2+4
  3E13  34            		inc	(hl)		;+-pi
                      	
                      	;FAC1=FAC2-FAC1
  3E14                	SUBF21:
  3E14  2169FF        		ld	hl,FAC1+3
  3E17  C39A36        		jp	CHGSGNADD
                      	
                      	;pi*2
  3E1A                	PI2:
  3E1A  A2DA0F4983    		db	0a2h, 0dah, 0fh, 49h, 83h
                      	
                      	
                      	;SQR() function
                      	;y_n+1=(y_n + x/y_n)/2
  3E1F                	F_SQR:
  3E1F  CD9B0B        		call	CHKNUM
  3E22  EF            		rst	CHKSGN
  3E23  C8            		ret	z		;sqr(0)=0
  3E24  3C            		inc	a
  3E25  CACB03        		jp	z,FCERR		;sqr(-x)
                      	
  3E28  CD2539        		call	PUSHF1
  3E2B  216AFF        		ld	hl,FAC1+4
  3E2E  7E            		ld	a,(hl)
  3E2F  D681          		sub	81h
  3E31  1F            		rra			;=sra a,c-flag=bit7
  3E32  C681          		add	a,81h
  3E34  77            		ld	(hl),a		;exponent of y_0 = (exponent of x)/2
                      	
  3E35  0605          		ld	b,05h
  3E37                	SQRLP:
  3E37  CD5239        		call	CPYFAC
  3E3A  CD3439        		call	POPF1		;x
  3E3D  CD2539        		call	PUSHF1		;x
  3E40  C5            		push	bc
  3E41  CD1A3C        		call	PUSHF2		;;y
  3E44  CD6738        		call	DIVF		;x/y
  3E47  CD4339        		call	POPF2		;;y
  3E4A  CD9E36        		call	ADDF		;x/y+y
  3E4D  216AFF        		ld	hl,FAC1+4
  3E50  35            		dec	(hl)		;y=(x/y+y)/2
  3E51  C1            		pop	bc
  3E52  10E3          		djnz	SQRLP
  3E54  CD4339        		call	POPF2
  3E57  C9            		ret
                      	
                      	
                      	;TAN() function
                      	;tan(x)=sin(x)/cos(x)
  3E58                	F_TAN:
  3E58  CD9B0B        		call	CHKNUM
  3E5B  CD2539        		call	PUSHF1		;x
  3E5E  CD803D        		call	COS		;cos(x)
  3E61  CD5239        		call	CPYFAC
  3E64  CD3439        		call	POPF1		;x
  3E67  CD1A3C        		call	PUSHF2		;cos(x)
  3E6A  CD933D        		call	SIN		;sin(x)
  3E6D  CD4339        		call	POPF2		;cos(x)
  3E70  C36738        		jp	DIVF		;sin(x)/cos(x)
                      	
                      	
                      	;input: bc=graphic X, de=graphic Y
                      	;output: a=color
                      	;destroy: f,bc,de,hl
  3E73                	POINT:
  3E73  CDB215        		call	GXY2AD
                      	;	jp	GETCOLRMAIN
                      	
                      	;input: (fdb3h-fdb4h)=attribute address, (fdb2h)=bit
                      	;output: a=color
                      	;destroy: f,hl
  3E76                	GETCOLRMAIN:
  3E76  CD9539        		call	CHKMOD
  3E79  2AB3FD        		ld	hl,(VRAMAD)
  3E7C  3A92FD        		ld	a,(SCREEN1)
  3E7F  CAF866        		jp	z,GETCOLR66
  3E82  CB3F          		srl	a
  3E84  3AB2FD        		ld	a,(VRAMBIT)
  3E87  2014          		jr	nz,GETCOLR34
  3E89  3803          		jr	c,GETCOLR2
                      	
                      	;screen mode 1
  3E8B                	GETCOLR1:
  3E8B  A6            		and	(hl)
  3E8C  3C            		inc	a
  3E8D  C9            		ret
                      	
                      	;screen mode 2
  3E8E                	GETCOLR2:
  3E8E  A6            		and	(hl)
  3E8F  C8            		ret	z
                      	
  3E90  7E            		ld	a,(hl)		;bit7-6
  3E91  07            		rlca
  3E92  CB8C          		res	1,h
  3E94  AE            		xor	(hl)		;bit1
  3E95  E681          		and	81h
  3E97  AE            		xor	(hl)
  3E98  07            		rlca
  3E99  E607          		and	07h
  3E9B  3C            		inc	a
  3E9C  C9            		ret
                      	
                      	;screen mode 3
                      	;screen mode 4
  3E9D                	GETCOLR34:
  3E9D  C5            		push	bc
  3E9E  47            		ld	b,a
  3E9F  A6            		and	(hl)
  3EA0  07            		rlca
  3EA1                	GETCOLRLP:
  3EA1  0F            		rrca
  3EA2  CB38          		srl	b
  3EA4  30FB          		jr	nc,GETCOLRLP
  3EA6  C1            		pop	bc
  3EA7  C8            		ret	z		;screen mode 4
  3EA8  3C            		inc	a		;screen mode 3
  3EA9  C9            		ret
                      	
                      	
                      	;SCREEN() function
                      	;not called but jumped
  3EAA                	F_SCRN:
  3EAA  CDB50B        		call	CHKLPAR
  3EAD  CD2D0E        		call	INT1ARG2
  3EB0  CDC40B        		call	CHKRPAR
  3EB3  61            		ld	h,c
  3EB4  6B            		ld	l,e
  3EB5  24            		inc	h
  3EB6  2C            		inc	l
  3EB7  CDCD11        		call	XY2AD
  3EBA  CD9539        		call	CHKMOD
  3EBD  CA3E63        		jp	z,F_SCRN66
  3EC0  6E            		ld	l,(hl)
  3EC1  CD7E0C        		call	I1TOF
  3EC4  C38A18        		jp	FNCRTN
                      	
                      	
                      	;FN() function
                      	;not called but jumped
  3EC7                	F_FN:
  3EC7  CD8F33        		call	CHKVAR
  3ECA  E5            		push	hl		;program address
  3ECB  CBF8          		set	7,b
  3ECD  CDC133        		call	SRCHVAR
  3ED0  DAF203        		jp	c,UFERR
  3ED3  E1            		pop	hl		;program address
  3ED4  D5            		push	de		;function address
  3ED5  CD860B        		call	FNCNUM
  3ED8  C1            		pop	bc		;function address
  3ED9  E5            		push	hl		;program address
                      	
                      	;push old FN() argument
  3EDA  2A63FF        		ld	hl,(FNARG+3)
  3EDD  E5            		push	hl
  3EDE  2A61FF        		ld	hl,(FNARG+1)
  3EE1  E5            		push	hl
  3EE2  3A60FF        		ld	a,(FNARG)
  3EE5  F5            		push	af
  3EE6  2A5EFF        		ld	hl,(FNPAR)
  3EE9  E5            		push	hl		;old FN() parameter
                      	
  3EEA  C5            		push	bc		;function address
  3EEB  2166FF        		ld	hl,FAC1
  3EEE  1160FF        		ld	de,FNARG
  3EF1  CD480C        		call	SETF
  3EF4  E1            		pop	hl		;function address
                      	
  3EF5  5E            		ld	e,(hl)
  3EF6  23            		inc	hl
  3EF7  56            		ld	d,(hl)
  3EF8  23            		inc	hl
  3EF9  7E            		ld	a,(hl)
  3EFA  23            		inc	hl
  3EFB  66            		ld	h,(hl)
  3EFC  6F            		ld	l,a
  3EFD  225EFF        		ld	(FNPAR),hl
                      	
  3F00  EB            		ex	de,hl
  3F01  CD4717        		call	ARG
                      	
  3F04  E1            		pop	hl		;old FN() parameter
  3F05  225EFF        		ld	(FNPAR),hl
                      	
                      	;pop old FN() argument
  3F08  F1            		pop	af
  3F09  3260FF        		ld	(FNARG),a
  3F0C  E1            		pop	hl
  3F0D  2261FF        		ld	(FNARG+1),hl
  3F10  E1            		pop	hl
  3F11  2263FF        		ld	(FNARG+3),hl
  3F14  E1            		pop	hl		;program address
  3F15  C38D18        		jp	CLRSTRD
                      	
                      	
                      	;set FAC2 and compare
                      	;input: FAC1,hl=float address
                      	;output: c-flag, z-flag, FAC2
                      	;destroy: af,bc,de,hl
  3F18                	SETCMPF:
  3F18  CD4E0C        		call	SETF2
                      	;	jp	CMPF
                      	
                      	
                      	;compare FAC2 and FAC1
                      	;input: FAC1,FAC2
                      	;output: c-flag,z-flag
                      	;destroy: af,b,de,hl
  3F1B                	CMPF:
  3F1B  1169FF        		ld	de,FAC1+3
  3F1E  2170FF        		ld	hl,FAC2+3
  3F21  B4            		or	h		;h>0, reset z-flag
  3F22  7E            		ld	a,(hl)
  3F23  07            		rlca
  3F24  1A            		ld	a,(de)
  3F25  380E          		jr	c,CMPFNEG
  3F27  07            		rlca
  3F28  D8            		ret	c
                      	
  3F29                	CMPFPOS:
  3F29  13            		inc	de
  3F2A  23            		inc	hl
  3F2B  0605          		ld	b,05h
  3F2D                	CMPFLP:
  3F2D  1A            		ld	a,(de)
  3F2E  BE            		cp	(hl)
  3F2F  C0            		ret	nz
  3F30  2B            		dec	hl
  3F31  1B            		dec	de
  3F32  10F9          		djnz	CMPFLP
  3F34  C9            		ret
                      	
  3F35                	CMPFNEG:
  3F35  07            		rlca
  3F36  D0            		ret	nc
                      	
                      	;FAC1<0, FAC2<0
  3F37  EB            		ex	de,hl
  3F38  18EF          		jr	CMPFPOS
                      	
                      	
                      	;shift left arithmetic for FAC1
                      	;input: FAC1
                      	;output: FAC1,c-flag
                      	;destroy: f,hl
  3F3A                	SLAINT4:
  3F3A  2166FF        		ld	hl,FAC1
  3F3D  CB26          		sla	(hl)
  3F3F  23            		inc	hl
  3F40  CB16          		rl	(hl)
  3F42  23            		inc	hl
  3F43  CB16          		rl	(hl)
  3F45  23            		inc	hl
  3F46  CB16          		rl	(hl)
  3F48  C9            		ret
                      	
                      	
                      	;jump subroutine in table
                      	;input: de=table address, a=data(00h-7fh or 80h-ffh)
                      	;output: de=jump address, a=(hl)
                      	;destroy: f
  3F49                	JPTBL:
  3F49  EB            		ex	de,hl
  3F4A  87            		add	a,a
  3F4B  85            		add	a,l
  3F4C  6F            		ld	l,a
  3F4D  3001          		jr	nc,JPTBLNC
  3F4F  24            		inc	h
  3F50                	JPTBLNC:
  3F50  7E            		ld	a,(hl)
  3F51  23            		inc	hl
  3F52  66            		ld	h,(hl)
  3F53  6F            		ld	l,a
                      	
  3F54  EB            		ex	de,hl
  3F55  7E            		ld	a,(hl)		;for ALIEN IN MAZE
  3F56  D5            		push	de
  3F57  C9            		ret
                      	
                      	
                      	;skip space
                      	;input: hl=program address-1
                      	;output: a=data, hl=next address
                      	;destroy: f
  3F58                	SKIPSPINC:
  3F58  23            		inc	hl
  3F59                	SKIPSP:
  3F59  7E            		ld	a,(hl)
  3F5A  FE20          		cp	' '
  3F5C  C0            		ret	nz
  3F5D  18F9          		jr	SKIPSPINC
                      	
                      	
                      	;check colon and line end
                      	;input: hl=program address
                      	;output: a=(hl), z-flag(1= 00h or ":")
                      	;destroy: af
  3F5F                	CHKCLNINC:
  3F5F  23            		inc	hl
  3F60                	CHKCLN:
  3F60  7E            		ld	a,(hl)
  3F61  B7            		or	a
  3F62  C8            		ret	z
  3F63  FE3A          		cp	':'
  3F65  C8            		ret	z
  3F66  FE20          		cp	' '
  3F68  C0            		ret	nz
  3F69  18F4          		jr	CHKCLNINC
                      	
                      	
                      	;check comma or argument
                      	;input: hl=program address
                      	;output: a=(hl), z-flag(1=comma), hl=next address
                      	;destroy: f
  3F6B                	CHKCMM:
  3F6B  CD603F        		call	CHKCLN		;a=(hl)
  3F6E  CAF803        		jp	z,MOERR
  3F71  23            		inc	hl
  3F72  FE20          		cp	' '
  3F74  28F5          		jr	z,CHKCMM
  3F76  FE2C          		cp	','
  3F78  C8            		ret	z
  3F79  2B            		dec	hl
  3F7A  C9            		ret
                      	
                      	
                      	;check colon and comma
                      	;input: hl=program address
                      	;output: a=(hl), z-flag(1= 00h or ":" ), hl=hl+1(comma)
                      	;destroy: af
  3F7B                	CHKCLCM:
  3F7B  CD603F        		call	CHKCLN
  3F7E  C8            		ret	z
  3F7F  FE2C          		cp	','
  3F81  C2C203        		jp	nz,SNERR
  3F84  23            		inc	hl
  3F85  B7            		or	a		;reset z-flag
  3F86  C9            		ret
                      	
                      	
                      	;bc=a*32
                      	;input: a(<=16), b=0
                      	;output: bc
                      	;destroy: f
  3F87                	MUL32:
  3F87  87            		add	a,a		;*2
  3F88  87            		add	a,a		;*4
  3F89  87            		add	a,a		;*8
  3F8A  87            		add	a,a		;*16
  3F8B  CB10          		rl	b
  3F8D  87            		add	a,a		;*32
  3F8E  CB10          		rl	b
  3F90  4F            		ld	c,a
  3F91  C9            		ret
                      	
                      	
                      	;check external ROM (4000=CD? 6000=CD?)
                      	;input: hl=4003h or 6003h
                      	;output: a=mode-1, z-flag(0=found, 1=not found)
                      	;destroy: f,bc,de
  3F92                	CHKEXTCD:
  3F92  1643          		ld	d,'C'
  3F94  01            		db	01h		;ld bc,
                      	
                      	;check external ROM (4000=AB? 6000=AB?)
                      	;input: hl=4003h or 6003h
                      	;output: a=mode-1, z-flag(0=found, 1=not found)
                      	;destroy: f,bc,de
  3F95                	CHKEXTAB:
  3F95  1641          		ld	d,'A'
  3F97  5A            		ld	e,d
  3F98  1C            		inc	e		;de="AB" or "CD"
  3F99  E5            		push	hl
  3F9A  46            		ld	b,(hl)
  3F9B  2D            		dec	l
  3F9C  4E            		ld	c,(hl)
  3F9D  2D            		dec	l
  3F9E  7E            		ld	a,(hl)
  3F9F  2D            		dec	l
  3FA0  66            		ld	h,(hl)
  3FA1  6F            		ld	l,a
  3FA2  E7            		rst	CPHLDE
  3FA3  2006          		jr	nz,NOABCD	;"AB" "CD" not found
  3FA5  60            		ld	h,b
  3FA6  69            		ld	l,c
  3FA7  CDA407        		call	JPHL
  3FAA  F6            		db	0f6h		;or afh, reset z-flag
                      	
  3FAB                	NOABCD:
  3FAB  BF            		cp	a		;set z-flag
  3FAC  3A65FE        		ld	a,(MODE)
  3FAF  E1            		pop	hl
  3FB0  C9            		ret
                      	
                      	
                      	;input: bc=screen mode X, de=screen mode Y, z-flag(1=mode5)
  3FB1                	SXY2ADMAIN:
  3FB1  C5            		push	bc
  3FB2  D5            		push	de
  3FB3  3A92FD        		ld	a,(SCREEN1)
  3FB6  CAAC66        		jp	z,SXY2AD66
                      	
  3FB9  67            		ld	h,a		;(SCREEN1)
  3FBA  CB3F          		srl	a
  3FBC  2015          		jr	nz,SXY2AD34
  3FBE  3E03          		ld	a,03h
  3FC0  3027          		jr	nc,SXY2AD1
                      	
                      	;(VRAM)*256+int(de/3)*32+bc/2
                      	;screen mode 2
  3FC2                	SXY2AD2:
  3FC2  EB            		ex	de,hl
  3FC3  29            		add	hl,hl
  3FC4  29            		add	hl,hl
  3FC5  EB            		ex	de,hl
  3FC6  CDA015        		call	DIV12
                      	;a=0,4,8
  3FC9  0F            		rrca
  3FCA  0F            		rrca
                      	;e=0-15,a=0,1,2
  3FCB  CB39          		srl	c
  3FCD  17            		rla
                      	;c=0-31,e=0-15,a=0-5
  3FCE  47            		ld	b,a
  3FCF  3E40          		ld	a,40h
  3FD1  1812          		jr	SXY2ADBIT
                      	
  3FD3                	SXY2AD34:
  3FD3  3804          		jr	c,SXY2AD4
                      	
                      	;(VRAM)*256+de*32+bc/4
                      	;screen mode 3
  3FD5                	SXY2AD3:
  3FD5  3E81          		ld	a,81h
  3FD7  CB21          		sla	c
                      	
                      	;(VRAM)*256+de*32+bc/8
                      	;screen mode 4
  3FD9                	SXY2AD4:
  3FD9  57            		ld	d,a		;81h=screen mode 3=81h, 01h=screen mode 4
  3FDA  79            		ld	a,c
  3FDB  E607          		and	07h
  3FDD  47            		ld	b,a
  3FDE  7A            		ld	a,d		;81h=screen mode 3=81h, 01h=screen mode 4
  3FDF  CB39          		srl	c
  3FE1  CB39          		srl	c
  3FE3  CB39          		srl	c
  3FE5                	SXY2ADBIT:
  3FE5  04            		inc	b
  3FE6                	SXY2ADLP:
  3FE6  0F            		rrca
  3FE7  10FD          		djnz	SXY2ADLP
                      	
                      	;(VRAM)*256+de*32+bc
                      	;screen mode 1
  3FE9                	SXY2AD1:
  3FE9  54            		ld	d,h		;(SCREEN1)
  3FEA  61            		ld	h,c
  3FEB  6B            		ld	l,e
  3FEC  24            		inc	h
  3FED  2C            		inc	l
  3FEE  CDCD11        		call	XY2AD
  3FF1  14            		inc	d		;(SCREEN1)
  3FF2  15            		dec	d		;(SCREEN1)
  3FF3  2002          		jr	nz,SXY2ADNZ
  3FF5  25            		dec	h
  3FF6  25            		dec	h
  3FF7                	SXY2ADNZ:
  3FF7  22B3FD        		ld	(VRAMAD),hl
  3FFA  32B2FD        		ld	(VRAMBIT),a
  3FFD  D1            		pop	de
  3FFE  C1            		pop	bc
  3FFF  C9            		ret
                      	
                      	
                      	;60 ROM end
  4000                	_4000H:	ds	4000h-_4000H
                      	
                      	
                      	;check exteral ROM (6000-) and select mode
                      	;output: z(1=mode 5), a=mode-1(if z=0)
  4000                	CHKEXT6000:
  4000  2165FE        		ld	hl,MODE
  4003  34            		inc	(hl)
  4004  34            		inc	(hl)		;mode1->mode3, mode2->mode4
  4005  210360        		ld	hl,6003h
  4008  CD953F        		call	CHKEXTAB
  400B  C0            		ret	nz		;AB found, skip menu
  400C  CD923F        		call	CHKEXTCD
  400F  3E11          		ld	a,11h		;0000-7fff:BASIC ROM
  4011  CD9826        		call	SETF0H
  4014  CD4A60        		call	SELMOD
  4017  F5            		push	af		;a=mode-1, z=mode 5?
  4018  FE02          		cp	02h
  401A  3822          		jr	c,CHKEXT6END	;mode1,2
  401C  3A3BFB        		ld	a,(DRIVES)
  401F  B7            		or	a
  4020  281C          		jr	z,CHKEXT6END
                      	
  4022                	FILELP:
  4022  214341        		ld	hl,HOWMANYFILES
  4025  CDCF30        		call	PUTS
  4028  CD0529        		call	INPT1
  402B  D7            		rst	ANADAT
  402C  110300        		ld	de,0003h
  402F  3009          		jr	nc,FILES3
  4031  CDCA27        		call	ATOI2
  4034  210F00        		ld	hl,000fh
  4037  E7            		rst	CPHLDE
  4038  38E8          		jr	c,FILELP
                      	
  403A                	FILES3:
  403A  7B            		ld	a,e
  403B  3231FB        		ld	(FILES),a
                      	
  403E                	CHKEXT6END:
  403E  F1            		pop	af		;a=mode-1, z=mode 5?
  403F  C9            		ret
                      	
                      	
                      	;set command/function table for mode3,4,5
  4040                	SETTBL:
                      	
                      	;CLOAD(a3)
  4040  217850        		ld	hl,C_CLDEX
  4043  22A7FA        		ld	(CMDTBL+(0a3h-80h)*2),hl
                      	;CSAVE(a4)
  4046  214E50        		ld	hl,C_CSVEX
  4049  22A9FA        		ld	(CMDTBL+(0a4h-80h)*2),hl
                      	;HEX$(e5)
  404C  213246        		ld	hl,F_HEX
  404F  2207FB        		ld	(FNCTBL+(0e5h-FNC1ST)*2),hl
                      	;PAD(f2)
  4052  216E43        		ld	hl,F_PAD
  4055  2221FB        		ld	(FNCTBL+(0f2h-FNC1ST)*2),hl
                      	
                      	;RENUM(ab)-PUT(ae)
  4058  215241        		ld	hl,CMDLSTEX
  405B  11B7FA        		ld	de,CMDTBL+(0abh-80h)*2
  405E  010800        		ld	bc,0008h
  4061  EDB0          		ldir
                      	
  4063  3A3BFB        		ld	a,(DRIVES)
  4066  B7            		or	a
  4067  2825          		jr	z,SETTBLZ1
                      	
                      	;BLOAD(af)-KILL(bd)
                      	;	ld	hl,CMDLSTEX+(0afh-0abh)*2
                      	;	ld	de,CMDTBL+(0afh-80h)*2
  4069  0E1E          		ld	c,0+(0bdh-0afh+1)*2		;b=0
  406B  EDB0          		ldir
                      	;DSKI$(f3)-MKS$(f9)
                      	;	ld	hl,FNCLSTEX
  406D  1123FB        		ld	de,FNCTBL+(0f3h-FNC1ST)*2
  4070  0E0E          		ld	c,0+(0f9h-0f3h+1)*2		;b=0
  4072  EDB0          		ldir
                      	;END(80)
  4074  216743        		ld	hl,C_ENDEX
  4077  2261FA        		ld	(CMDTBL-(80h-80h)*2),hl
                      	
                      	;function key F2(load),F7(save)
  407A  1145FB        		ld	de,FKEYTBL+8
  407D  2146FB        		ld	hl,FKEYTBL+8+1
  4080  0E06          		ld	c,06h		;b=0
  4082  EDB0          		ldir
  4084  116DFB        		ld	de,FKEYTBL+8*6
  4087  216EFB        		ld	hl,FKEYTBL+8*6+1
  408A  0E06          		ld	c,06h		;b=0
  408C  EDB0          		ldir
                      	
  408E                	SETTBLZ1:
  408E  2A8DFD        		ld	hl,(USREND)
  4091  062E          		ld	b,2eh
  4093                	SETTBLLP1:
  4093  71            		ld	(hl),c		;c=0
  4094  2B            		dec	hl
  4095  10FC          		djnz	SETTBLLP1
  4097  E5            		push	hl
  4098  DDE1          		pop	ix
  409A  DD23          		inc	ix
                      	;	ld	a,(DRIVES)
                      	;	or	a
  409C  2866          		jr	z,SETTBLZ2
                      	
                      	
                      	;disk work
                      	;
                      	;----------------------	(ff3dh)
                      	;string area
                      	;			(fd8dh)
                      	;---------------------
                      	;buffer#m info		9bytes	m=files
                      	;---------------------
                      	;buffer#m data		256bytes
                      	;---------------------
                      	;...
                      	;---------------------	buffer pointer
                      	;buffer#0 info		9 bytes
                      	; +0: file open mode (01h=input, 02h=output, 04h=random, 08h=append, 10h=kill, 80h=binary save)
                      	; +1: first cluster
                      	; +2: cluster
                      	; +3: sector in cluster(0-8)
                      	; +4: drive-1
                      	; +5: end position
                      	; +6: position
                      	; +7: file status (bit1=to next sector, bit3=write)
                      	; +8: characters until line end
                      	;---------------------	(fb37h)
                      	;buffer#0 data		256bytes
                      	;---------------------	(fb35h)
                      	;buffer pointer table	2*(files+1)bytes
                      	;---------------------
                      	;FAT#n info		6bytes	n=drives
                      	;---------------------
                      	;FAT#n copy		70bytes
                      	;---------------------
                      	;empty?			1byte
                      	;---------------------
                      	;...
                      	;---------------------	FAT pointer-3
                      	;FAT#1 info		6bytes
                      	; -3: check sum
                      	; -2: need to over write FAT(ffh) or not(00h)
                      	; -1: drive attribute (=ID sector and 70h)
                      	; +0: last accessed track (ffh=removed)
                      	; +1: temporary
                      	; +2: unused clusters
                      	;---------------------
                      	;FAT#1 copy		70bytes
                      	;---------------------
                      	;empty?			1byte
                      	;---------------------	(fffch)
                      	;FAT pointer table	2*drives bytes
                      	;---------------------
                      	;IX area		46bytes
                      	;---------------------
                      	;BASIC work or VRAM
                      	;(fa00h, e000h, c000h, or a000h)
                      	
                      	
                      	;set FAT info and FAT pointer table
                      	;	ld	a,(DRIVES)
  409E  47            		ld	b,a
  409F  3ED2          		ld	a,0d2h
  40A1  90            		sub	b
  40A2  90            		sub	b
  40A3  6F            		ld	l,a
  40A4  22FCFF        		ld	(FATPTBL),hl	;=end-46-2*drives
  40A7  54            		ld	d,h
  40A8  5D            		ld	e,l
                      	
  40A9                	SETTBLLP2:
  40A9  E5            		push	hl
  40AA  21B6FF        		ld	hl,0-(4dh-3)	;FAT buffer size=70+6+1 bytes
  40AD  19            		add	hl,de
  40AE  36FF          		ld	(hl),0ffh	;+0: last accessed track (ff=no open files)
  40B0  EB            		ex	de,hl
  40B1  E1            		pop	hl
                      	
  40B2  73            		ld	(hl),e
  40B3  23            		inc	hl
  40B4  72            		ld	(hl),d
  40B5  23            		inc	hl
  40B6  1B            		dec	de
  40B7  1B            		dec	de
  40B8  AF            		xor	a
  40B9  12            		ld	(de),a		;-2: not need to overwrite FAT
  40BA  1B            		dec	de
                      	
  40BB  E5            		push	hl
  40BC  D5            		push	de
  40BD  79            		ld	a,c		;start=0
  40BE  0C            		inc	c
  40BF  CDF251        		call	SETFATP
  40C2  CDF753        		call	SETSUM
  40C5  D1            		pop	de
  40C6  E1            		pop	hl
                      	
  40C7  10E0          		djnz	SETTBLLP2
                      	
  40C9  D5            		push	de		;
                      	
                      	;hook
  40CA  212741        		ld	hl,HOOKTBL
  40CD  3EC3          		ld	a,0c3h		;jp
  40CF  0E0E          		ld	c,0+(HOOKTBLEND-HOOKTBL)/2	;b=0
  40D1                	SETTBLLP3:
  40D1  5E            		ld	e,(hl)
  40D2  23            		inc	hl
  40D3  56            		ld	d,(hl)
  40D4  23            		inc	hl
  40D5  12            		ld	(de),a
  40D6  13            		inc	de
  40D7  EDA0          		ldi
  40D9  EDA0          		ldi
  40DB  EAD140        		jp	pe,SETTBLLP3
                      	
                      	;set file buffer pointer table
  40DE  E1            		pop	hl		;
  40DF  3A31FB        		ld	a,(FILES)
  40E2  3C            		inc	a
  40E3  47            		ld	b,a
  40E4  87            		add	a,a		;c-flag=0
  40E5  5F            		ld	e,a
  40E6  51            		ld	d,c		;=0
                      	;	or	a
  40E7  ED52          		sbc	hl,de
  40E9  2235FB        		ld	(BUFPTBL),hl
  40EC  54            		ld	d,h
  40ED  5D            		ld	e,l
  40EE  15            		dec	d		;file buffer data size=256 bytes
  40EF  ED5337FB      		ld	(BUF0),de
  40F3  14            		inc	d
                      	
  40F4                	SETTBLLP4:
  40F4  E5            		push	hl
  40F5  21F7FE        		ld	hl,0-0109h	;file buffer size=256+9bytes
  40F8  19            		add	hl,de
  40F9  71            		ld	(hl),c		;+0: file open mode, c=0
  40FA  EB            		ex	de,hl
  40FB  E1            		pop	hl
                      	
  40FC  73            		ld	(hl),e
  40FD  23            		inc	hl
  40FE  72            		ld	(hl),d
  40FF  23            		inc	hl
  4100  10F2          		djnz	SETTBLLP4
  4102  1B            		dec	de
  4103  EB            		ex	de,hl
                      	
  4104                	SETTBLZ2:
  4104  228DFD        		ld	(USREND),hl
  4107  012C01        		ld	bc,300
  410A  CDD135        		call	CLRMAIN
  410D  CDB512        		call	PRTFKEY
  4110  CD9539        		call	CHKMOD
  4113  C2C100        		jp	nz,PRTSYS60
                      	
                      	;TALK(be)-DELETE(c1)
  4116  218641        		ld	hl,CMDLST66
  4119  11DDFA        		ld	de,CMDTBL+(0beh-80h)*2
  411C  010800        		ld	bc,0+(0c2h-0beh)*2
  411F  EDB0          		ldir
                      	
  4121  211963        		ld	hl,SYSNAME66
  4124  C3C400        		jp	PRTSYS
                      	
                      	
  4127                	HOOKTBL:
  4127  8AFFF656      		dw	HOOKPED, ERRCLRB
  412B  8DFFF656      		dw	HOOKERR, ERRCLRB
  412F  93FFD856      		dw	HOOKEDT, REMOVE
  4133  AEFF3952      		dw	HOOKPRT, FNUMPRT
  4137  BAFF7F52      		dw	HOOKINP, FNUMINP
  413B  CFFFBA5A      		dw	HOOKPUT, PUTDSK
  413F  C9FFCE59      		dw	HOOKRES, CHKROPT
  4143                	HOOKTBLEND:
                      	
                      	
  4143                	HOWMANYFILES:
  4143  486F77204D616E		db	"How Many Files", 00h
                      	
  4152                	CMDLSTEX:
                      	;ab-af
  4152  3944724A914EA3		dw				C_RENM,	C_CRCL,	C_GET,	C_PUT,	C_BLD
                      	;b0-bd
  415C  B85DEA59E55942		dw	C_BSV,	C_FLS,	C_LFLS,	C_LOAD,	C_MRG,	C_NAME,	C_SAVE,	C_FLD
  416C  F458E0585F55AF		dw	C_LSET,	C_RSET,	C_OPEN,	C_CLOS,	C_DSKO,	C_KILL
                      	
  4178                	FNCLSTEX:
                      	;f3-f9
  4178  305C4D5D305D5B		dw				F_DSKI,	F_LOF,	F_LOC,	F_EOF,	F_DSKF
  4182  92598459      		dw	F_CVS,	F_MKS
                      	
  4186                	CMDLST66:
                      	;be-bf
  4186  48756176      		dw							C_TALK,	C_MON
                      	;c0-c1
  418A  0872287F      		dw	C_KANJ,	C_DEL
                      	
  418E                	CMDNAMEEX:
  418E  D2454E554D    		db							'R'+80h,"ENUM"
  4193  C34952434C45C7		db	'C'+80h,"IRCLE",'G'+80h,"ET",	'P'+80h,"UT",	'B'+80h,"LOAD"
                      	
  41A4  C253415645C649		db	'B'+80h,"SAVE",	'F'+80h,"ILES",	'L'+80h,"FILES",'L'+80h,"OAD"
  41B8  CD45524745CE41		db	'M'+80h,"ERGE",	'N'+80h,"AME",	'S'+80h,"AVE",	'F'+80h,"IELD"
                      	
  41CA  CC534554D25345		db	'L'+80h,"SET",	'R'+80h,"SET",	'O'+80h,"PEN",	'C'+80h,"LOSE"
  41DB  C4534B4F24CB49		db	'D'+80h,"SKO$",	'K'+80h,"ILL",	'T'+80h,"ALK",	'M'+80h,"ON"
                      	
  41EB  CB414E4A49C445		db	'K'+80h,"ANJI",	'D'+80h,"ELETE"
                      	
                      	
  41F6                	FNCNAMEEX:
  41F6  D04144C4534B49		db					'P'+80h,"AD",	'D'+80h,"SKI$"
  41FE  CC4F46CC4F43C5		db	'L'+80h,"OF",	'L'+80h,"OC",	'E'+80h,"OF",	'D'+80h,"SKF"
                      	
  420B  C35653CD4B5324		db	'C'+80h,"VS",	'M'+80h,"KS$",	80h
                      	
                      	
                      	;error ID for extended BASIC
  4213                	ERRIDEX:
  4213  464F4945424E  		db	"FO", "IE", "BN"
  4219  4646424D414F49		db	"FF", "BM", "AO", "IO", "FE", "DF", "EF", "NM"
  4229  44534154444E54		db	"DS", "AT", "DN", "TS", "RD", "SP", "CF", "FW"
                      	
                      	
  4239                	FOERR:
  4239  1E2A          		ld	e,2ah
  423B  01            		db	01h		;ld bc,****
  423C                	IEERR:
  423C  1E2C          		ld	e,2ch
  423E  01            		db	01h		;ld bc,****
  423F                	BNERR:
  423F  1E2E          		ld	e,2eh
  4241  01            		db	01h		;ld bc,****
  4242                	FFERR:
  4242  1E30          		ld	e,30h
  4244  01            		db	01h		;ld bc,****
  4245                	BMERR:
  4245  1E32          		ld	e,32h
  4247  01            		db	01h		;ld bc,****
  4248                	AOERR:
  4248  1E34          		ld	e,34h
  424A  01            		db	01h		;ld bc,****
  424B                	IOERR:
  424B  1E36          		ld	e,36h
  424D  01            		db	01h		;ld bc,****
  424E                	FEERR:
  424E  1E38          		ld	e,38h
  4250  01            		db	01h		;ld bc,****
  4251                	DFERR:
  4251  1E3A          		ld	e,3ah
  4253  01            		db	01h		;ld bc,****
  4254                	EFERR:
  4254  1E3C          		ld	e,3ch
  4256  01            		db	01h		;ld bc,****
  4257                	NMERR:
  4257  1E3E          		ld	e,3eh
  4259  01            		db	01h		;ld bc,****
  425A                	DSERR:
  425A  1E40          		ld	e,40h
  425C  01            		db	01h		;ld bc,****
  425D                	ATERR:
  425D  1E42          		ld	e,42h
  425F  01            		db	01h		;ld bc,****
  4260                	DNERR:
  4260  1E44          		ld	e,44h
  4262  01            		db	01h		;ld bc,****
  4263                	TSERR:
  4263  1E46          		ld	e,46h
  4265  01            		db	01h		;ld bc,****
  4266                	RDERR:
  4266  1E48          		ld	e,48h
  4268  01            		db	01h		;ld bc,****
  4269                	SPERR:
  4269  1E4A          		ld	e,4ah
  426B  01            		db	01h		;ld bc,****
  426C                	CFERR:
  426C  1E4C          		ld	e,4ch
  426E  01            		db	01h		;ld bc,****
  426F                	FWERR:
  426F  1E4E          		ld	e,4eh
                      	
  4271  C3FD03        		jp	ERROR
                      	
                      	
                      	;read/write/check disk
                      	;input:
                      	; a=the number of sector, b=track, c=sector, de=address
                      	; ix=work address, (ix+00h)=drive-1, (ix+1bh)=error count
                      	; c-flag=0,z-flag=0: read
                      	; c-flag=0,z-flag=1: check
                      	; c-flag=1: write
                      	;output: c-flag(1=error), (de=de+data size)
                      	;destroy: f,(de)
                      	;destroy: af,(de),hl if error count>=10 (FAT error)
  4274                	_DISK:	ds	DISK-_DISK
  4274                		org	DISK
  4274  C3E642        		jp	DSKMAIN
                      	
                      	
  4277                	CNVIL66:
  4277  78            		ld	a,b
  4278  FEAB          		cp	CMDLAST+1
  427A  2810          		jr	z,IL661
  427C  FEC2          		cp	CLAST66+1
  427E  2812          		jr	z,IL662
  4280  FEF2          		cp	FNCLAST+1
  4282  2816          		jr	z,IL663
  4284  FEFA          		cp	FLASTEX+1
  4286  DA9505        		jp	c,ILLP3
  4289  C38805        		jp	ILNC
                      	
  428C                	IL661:
  428C  218E41        		ld	hl,CMDNAMEEX
  428F  C39505        		jp	ILLP3
  4292                	IL662:
  4292  21E402        		ld	hl,FNCNAME
  4295  06C2          		ld	b,TAB_
  4297  C39505        		jp	ILLP3
  429A                	IL663:
  429A  21F641        		ld	hl,FNCNAMEEX
  429D  C39505        		jp	ILLP3
                      	
                      	
                      	;checck drive number
                      	;input: a=drive-1(0-3)
                      	;output: a(0-1=internal,2-5=external), c-flag(1=error)
                      	;destroy: hl
  42A0                	CHKDRV:
  42A0  213BFB        		ld	hl,DRIVES
  42A3  BE            		cp	(hl)
  42A4  3F            		ccf
  42A5  D8            		ret	c
  42A6  2A75FE        		ld	hl,(DRVBIT)
  42A9  CB65          		bit	4,l		;internal FDD driveA
  42AB  2001          		jr	nz,CHKDRVNZ
  42AD  3C            		inc	a
  42AE                	CHKDRVNZ:
  42AE  B7            		or	a
  42AF  C8            		ret	z
  42B0  CB6D          		bit	5,l		;internal FDD dirveB
  42B2  C0            		ret	nz
  42B3  3C            		inc	a
  42B4  C9            		ret			;c-flag=0
                      	
                      	
                      	;initialize FDD (set DRVBIT)
                      	;output: a=the number of drives, z-flag(1=no drive)
                      	;destroy: f,hl,(de)
  42B5                	_INIDSK:ds	INIDSK-_INIDSK
  42B9                		org	INIDSK
                      	
                      	;for no FDD
  42B9  C5            		push	bc
  42BA  2175FE        		ld	hl,DRVBIT
  42BD  3600          		ld	(hl),00h	;bit4=drvieA,bit5=drvieB, bit6,7,0,1=external
                      	
  42BF  3E11          		ld	a,11h
  42C1  D3F0          		out	(0f0h),a	;0000-7fff:BASIC ROM
  42C3  CD826C        		call	INIDSKINT
  42C6  CD98FE        		call	CHGBANK
                      	
                      	;initialize external FDD
  42C9                	INIDSKEX:
  42C9  CDA250        		call	INIEFD		;FDOFF
  42CC  3001          		jr	nc,INIDSKNC1
  42CE  AF            		xor	a
  42CF                	INIDSKNC1:
  42CF  E6F0          		and	0f0h		;bit7-4: external FDD connection
  42D1  07            		rlca
  42D2  07            		rlca
  42D3  B6            		or	(hl)		;DRVBIT
  42D4  77            		ld	(hl),a
                      	
                      	;count drives
  42D5  0600          		ld	b,00h
  42D7                	INIDSKLP:
  42D7  87            		add	a,a
  42D8  3001          		jr	nc,INIDSKNC2
  42DA  04            		inc	b
  42DB                	INIDSKNC2:
  42DB  20FA          		jr	nz,INIDSKLP
                      	
  42DD  3E04          		ld	a,04h
  42DF  B8            		cp	b
  42E0  3801          		jr	c,INIDSKC
  42E2  78            		ld	a,b
  42E3                	INIDSKC:
  42E3  C1            		pop	bc
  42E4  B7            		or	a		;z-flag=1 if no drives
  42E5  C9            		ret
                      	
                      	
                      	;continued from DISK
  42E6                	DSKMAIN:
  42E6  F5            		push	af
  42E7  E5            		push	hl
  42E8  D5            		push	de
  42E9  C5            		push	bc
                      	
  42EA  F5            		push	af		;
  42EB  3EFF          		ld	a,0ffh
  42ED  3804          		jr	c,SETRWC2	;write=ffh
  42EF  2001          		jr	nz,SETRWC1	;read=00h
  42F1  3C            		inc	a		;check=01h
  42F2                	SETRWC1:
  42F2  3C            		inc	a
  42F3                	SETRWC2:
  42F3  2170FE        		ld	hl,RDWRCHK
  42F6  77            		ld	(hl),a
  42F7  F1            		pop	af		;
  42F8  B7            		or	a
  42F9  283D          		jr	z,DSKMAINZ
  42FB  23            		inc	hl
  42FC  77            		ld	(hl),a		;RMSECT
                      	
  42FD  78            		ld	a,b
  42FE  FE28          		cp	28h
  4300  303A          		jr	nc,DSKERR	;if track>39
  4302  23            		inc	hl
  4303  77            		ld	(hl),a		;TRACK
                      	
  4304  79            		ld	a,c
  4305  FE11          		cp	11h
  4307  3033          		jr	nc,DSKERR	;if sector>16
  4309  3274FE        		ld	(SECTOR),a
                      	
  430C  DD7E00        		ld	a,(ix+00h)	;drive-1
  430F  CDA042        		call	CHKDRV
  4312  3828          		jr	c,DSKERR
  4314  3273FE        		ld	(DRIVE),a
  4317  FE02          		cp	02h
  4319  D2B850        		jp	nc,RDWREFD
                      	
  431C  3E11          		ld	a,11h
  431E  D3F0          		out	(0f0h),a	;0000-7fff:BASIC ROM
  4320  CDB06C        		call	DISKINT
  4323  CD98FE        		call	CHGBANK
  4326  3814          		jr	c,DSKERR
                      	
  4328                	DSKEND:
  4328  CD6243        		call	FDOFF
  432B  C1            		pop	bc
  432C  E1            		pop	hl		;de=address
  432D  3A70FE        		ld	a,(RDWRCHK)
  4330  3D            		dec	a		;01h=check
  4331  2001          		jr	nz,NOCHECK	;de changes (read/write)
  4333  EB            		ex	de,hl		;pop de (check)
  4334                	NOCHECK:
  4334  E1            		pop	hl
  4335  F1            		pop	af
  4336  B7            		or	a		;reset c-flag: ok
  4337  C9            		ret
                      	
  4338                	DSKMAINZ:
  4338  C1            		pop	bc
  4339  D1            		pop	de
  433A  18F8          		jr	NOCHECK
                      	
                      	
  433C                	DSKERR:
  433C  CD6243        		call	FDOFF
  433F  C1            		pop	bc
  4340  D1            		pop	de
  4341                	DSKERR2:
  4341  E1            		pop	hl
  4342  DD341B        		inc	(ix+1bh)	;error count
  4345  DD7E1B        		ld	a,(ix+1bh)
  4348  FE0A          		cp	0ah
  434A  3003          		jr	nc,ERRJMP
  434C  F1            		pop	af
  434D                	ERRFAT:
  434D  37            		scf			;set c-flag: error
  434E  C9            		ret
                      	
  434F                	ERRJMP:
  434F  F1            		pop	af
  4350  F1            		pop	af		;cancel return address for DISK2
  4351  F1            		pop	af		;cancel stack for DISK2
  4352  F1            		pop	af		;cancel stack for DISK2
                      	
  4353  78            		ld	a,b
  4354  FE12          		cp	12h
  4356  2007          		jr	nz,ERRJMPNZ
  4358  79            		ld	a,c
  4359  D60E          		sub	0eh
  435B  FE03          		cp	10h-0eh+1
  435D  38EE          		jr	c,ERRFAT
  435F                	ERRJMPNZ:
  435F  C34B42        		jp	IOERR
                      	
                      	
                      	;set FDD off (change to external FDD)
  4362                	FDOFF:
                      	;destroy: a
  4362  3EF7          		ld	a,0f7h
  4364  D3B1          		out	(FDCNTL),a
  4366  C9            		ret
                      	
                      	
                      	;END command (extended)
  4367                	C_ENDEX:
  4367  F1            		pop	af		;cancel stack
  4368  CDD759        		call	CLOSALL
  436B  C34204        		jp	EDIT
                      	
                      	
                      	;PAD() function
  436E                	F_PAD:
  436E  CDE15C        		call	SETIX
  4371  CD7122        		call	FNCI1		;a=0
  4374  83            		add	a,e
  4375  2811          		jr	z,PAD0		;e=0?
  4377  DD6E2B        		ld	l,(ix+2bh)	;X
  437A  1D            		dec	e
  437B  CA7E0C        		jp	z,I1TOF
  437E  1D            		dec	e
  437F  C2CB03        		jp	nz,FCERR
  4382  DD6E2C        		ld	l,(ix+2ch)	;Y
  4385  C37E0C        		jp	I1TOF
                      	
                      	;PC-6051 and uPD7001C signals
                      	;Dsub2	6051	PSG
                      	;1	/SENSE	A0
                      	;2	EOC	A1
                      	;3	SO	A2
                      	;4	-	A3
                      	;5	5V	-
                      	;6	/SCK	A4(in)/B0(out) (select by B7)
                      	;7	SI	A5(in)/B1(out) (select by B7) A0=X, A3=Y
                      	;8	/CS	B4
                      	;9	GND	-
                      	
                      	;portA(input): 1-1-SI-/SCK-1-SO-EOC-/SENSE
                      	;portB(output):0-0-0-/CS-0-0-SI-/SCK
                      	
  4388                	PAD0:
  4388  CD2044        		call	STARTTP		;SI=0, output:d
  438B  CD1544        		call	IFMODE		;latch SI=0 to check /SENSE
  438E  CD1544        		call	ADCMODE
                      	
                      	;??? need wait to get correct /SENSE
  4391  0640          		ld	b,40h
  4393                	PAD0LP:
  4393  10FE          		djnz	PAD0LP
                      	
  4395  CDD343        		call	GETADC		;check /SENSE, select X
  4398  382C          		jr	c,ENDTP		;if /SENSE=1
                      	
  439A  CD3244        		call	SETSI1
  439D  CDD343        		call	GETADC		;get X, select Y
                      	
  43A0  CD2E44        		call	SETSI0
  43A3  CDD343        		call	GETADC		;get Y, select X
                      	
  43A6  CD3244        		call	SETSI1
  43A9  CDD343        		call	GETADC		;get X, select Y
  43AC  4B            		ld	c,e
                      	
  43AD  CD2E44        		call	SETSI0
  43B0  CDD343        		call	GETADC		;get Y, latch SI
                      	
  43B3  CD1544        		call	ADCMODE
  43B6  CDFC43        		call	CHKSENSE
  43B9  380B          		jr	c,ENDTP		;if /SENSE=1
                      	
  43BB  DD712B        		ld	(ix+2bh),c	;X
  43BE  DD732C        		ld	(ix+2ch),e	;Y
                      	
  43C1  7A            		ld	a,d
  43C2  CD6422        		call	SETPLS1
  43C5  57            		ld	d,a
                      	;	jr	ENDTP
                      	
                      	;end touch panel process
                      	;input: d=PSG register 7 value
                      	;destroy: af,de
  43C6                	ENDTP:
  43C6  3E0F          		ld	a,0fh		;parallel port B
  43C8  1ED1          		ld	e,0d1h		;bit7: pin6,7=input
                      					;bit6: select input=joystick port 1
                      					;bit5: joystick port 1 pin8=low
                      					;bit4: joystick port 2 pin8=/CS=high
                      					;bit3: joystick port 1 pin7=low
                      					;bit2: joystick port 1 pin6=low
                      					;bit1: joystick port 2 pin7=SI=low(X)
                      					;bit0: joystick port 2 pin6=/SCK=high
  43CA  CDC51B        		call	SETPSG
                      	
  43CD  3E07          		ld	a,07h		;set back parallel ports
  43CF  5A            		ld	e,d
  43D0  C3861E        		jp	SETPSGEI
                      	
                      	
                      	;get ADC(uPD7001c) value
                      	;output: e=data, c-flag(1=no touch)
                      	;destroy: af,b
  43D3                	GETADC:
  43D3  CDFC43        		call	CHKSENSE
  43D6  F5            		push	af		;c-flag
                      	
  43D7  3E0F          		ld	a,0fh		;parallel port B
  43D9  D3A0          		out	(0a0h),a
  43DB  DBA2          		in	a,(0a2h)
                      	
  43DD  0608          		ld	b,08h
  43DF                	ADCLP:
  43DF  E6FE          		and	0feh		;portB-bit0=/SCK -> low
  43E1  D3A1          		out	(0a1h),a
                      	
  43E3  3E0E          		ld	a,0eh		;parallel port A
  43E5  D3A0          		out	(0a0h),a
  43E7  DBA2          		in	a,(0a2h)	;portA-bit2=SO
  43E9  0F            		rrca
  43EA  0F            		rrca
  43EB  0F            		rrca
  43EC  CB13          		rl	e
                      	
  43EE  3E0F          		ld	a,0fh		;parallel port B
  43F0  D3A0          		out	(0a0h),a
  43F2  DBA2          		in	a,(0a2h)
  43F4  F601          		or	01h		;portB-bit0=/SCK -> high
  43F6  D3A1          		out	(0a1h),a
  43F8  10E5          		djnz	ADCLP
                      	
  43FA  F1            		pop	af		;c-flag
  43FB  C9            		ret
                      	
                      	
                      	;check touch pad sense signal
                      	;output: c-flag(0=touch, 1=no)
                      	;destroy: af
  43FC                	CHKSENSE:
  43FC  CD0A44        		call	WAITEOC
  43FF  CD1544        		call	IFMODE
  4402  3E0E          		ld	a,0eh		;parallel port A
  4404  D3A0          		out	(0a0h),a
  4406  DBA2          		in	a,(0a2h)
  4408  0F            		rrca			;port B bit0=Dsub-pin1=/SENSE
  4409  C9            		ret
                      	
                      	
                      	;go to ADC mode and wait for EOC=1
                      	;destroy: af
  440A                	WAITEOC:
  440A  3E0E          		ld	a,0eh		;parallel port A
  440C  D3A0          		out	(0a0h),a
  440E                	EOCLP:
  440E  DBA2          		in	a,(0a2h)
  4410  E602          		and	02h		;portA bit1=Dsub-pin2=EOC
  4412  28FA          		jr	z,EOCLP
  4414  C9            		ret
                      	
                      	
                      	;ADC mode -> interface mode
                      	;or
                      	;interface mode -> ADC mode
                      	;destroy: af
  4415                	IFMODE:
  4415                	ADCMODE:
  4415  3E0F          		ld	a,0fh		;parallel port B
  4417  D3A0          		out	(0a0h),a
  4419  DBA2          		in	a,(0a2h)
  441B  EE10          		xor	10h		;portB-bit4=/CS -> high/low
  441D  D3A1          		out	(0a1h),a
  441F  C9            		ret
                      	
                      	
                      	;start touch panel process
                      	;output: d=PSG register 7 value
                      	;destroy: af
  4420                	STARTTP:
  4420  F3            		di
                      	
  4421  3E07          		ld	a,07h
  4423  D3A0          		out	(0a0h),a
  4425  DBA2          		in	a,(0a2h)
  4427  57            		ld	d,a
  4428  E6BF          		and	0bfh		;parallel port A=in
  442A  F680          		or	80h		;parallel port B=out
  442C  D3A1          		out	(0a1h),a
                      	;	jp	SETSI0
                      	
                      	
                      	;set SI=0, /SCK=1, /CS=1(ADC mode)
                      	;destroy: af,e
  442E                	SETSI0:
  442E  1E11          		ld	e,11h		;bit7: pin6,7=output
                      					;bit6: select input=joystick port 2
                      					;bit5: joystick port 1 pin8=low
                      					;bit4: joystick port 2 pin8=/CS=high
                      					;bit3: joystick port 1 pin7=low
                      					;bit2: joystick port 1 pin6=low
                      					;bit1: joystick port 2 pin7=SI=low(X)
                      					;bit0: joystick port 2 pin6=/SCK=high
  4430  1802          		jr	SETSI
                      	
                      	
                      	;set SI=0, /SCK=1, /CS=1(ADC mode)
                      	;destroy: af,e
  4432                	SETSI1:
  4432  1E13          		ld	e,13h
  4434                	SETSI:
  4434  3E0F          		ld	a,0fh		;parallel port B
  4436  C3C51B        		jp	SETPSG
                      	
                      	
                      	;RENUM command
  4439                	C_RENM:
  4439  F1            		pop	af		;cancel return address
  443A  110A00        		ld	de,000ah	;default 1st parameter (new line number)
  443D  42            		ld	b,d		;default 2nd parameter (start line number)
  443E  4A            		ld	c,d
  443F  CD603F        		call	CHKCLN
  4442  2819          		jr	z,RENMCHK	;no parameters
  4444  C5            		push	bc
  4445  FE2C          		cp	','
  4447  C46807        		call	nz,GETLN	;1st parameter (new line number)
  444A  C1            		pop	bc
  444B  CD7B3F        		call	CHKCLCM
  444E  280D          		jr	z,RENMCHK	;no 2nd parameter
                      	
  4450  D5            		push	de		;1st parameter (new line number)
  4451  CD6807        		call	GETLN		;2nd parameter (start line number)
  4454  CD603F        		call	CHKCLN
  4457  C2C203        		jp	nz,SNERR
  445A  42            		ld	b,d		;2nd parameter (start line number)
  445B  4B            		ld	c,e
  445C  D1            		pop	de		;1st parameter (new line number)
                      	
                      	;error check
  445D                	RENMCHK:
  445D  D5            		push	de		;1st parameter (new line number)
  445E  C5            		push	bc		;2nd parameter (start line number)
                      	
  445F  2A5FFA        		ld	hl,(STARTAD)
  4462                	RENMLP1:
  4462  7E            		ld	a,(hl)
  4463  23            		inc	hl
  4464  B6            		or	(hl)
  4465  E5            		push	hl		;
  4466  2831          		jr	z,RENMSTRT
                      	
  4468  23            		inc	hl
  4469  7E            		ld	a,(hl)
  446A  23            		inc	hl
  446B  66            		ld	h,(hl)
  446C  6F            		ld	l,a
                      	;	or	a
  446D  ED42          		sbc	hl,bc		;bc=2nd parameter (start line number)
  446F  3007          		jr	nc,RENMNC
                      	
  4471  09            		add	hl,bc
  4472  E7            		rst	CPHLDE
  4473  D2CB03        		jp	nc,FCERR
  4476  181A          		jr	RENMCHKEND
                      	
  4478                	RENMNC:
  4478  21F9FF        		ld	hl,65529
  447B  E7            		rst	CPHLDE
  447C  DACB03        		jp	c,FCERR
                      	
  447F  E1            		pop	hl		;
  4480  E5            		push	hl		;
  4481  7E            		ld	a,(hl)
  4482  2B            		dec	hl
  4483  6E            		ld	l,(hl)
  4484  67            		ld	h,a
  4485  7E            		ld	a,(hl)
  4486  23            		inc	hl
  4487  B6            		or	(hl)
  4488  280F          		jr	z,RENMSTRT
                      	
  448A  210A00        		ld	hl,000ah
  448D  19            		add	hl,de
  448E  DACB03        		jp	c,FCERR
  4491  EB            		ex	de,hl
                      	
  4492                	RENMCHKEND:
  4492  E1            		pop	hl		;
  4493  7E            		ld	a,(hl)
  4494  2B            		dec	hl
  4495  6E            		ld	l,(hl)
  4496  67            		ld	h,a
  4497  18C9          		jr	RENMLP1
                      	
                      	;search line number
  4499                	RENMSTRT:
  4499  E1            		pop	hl
  449A  2A29FF        		ld	hl,(BASICAD)
  449D                	RENMLP2:
  449D  CDED45        		call	SRCHRNM
  44A0  2858          		jr	z,RNMAD
  44A2                	RNMCMM:
  44A2  E5            		push	hl		;program address before line number
  44A3  CD6807        		call	GETLN
  44A6  C1            		pop	bc		;program address before line number
                      	
                      	;bc=before,hl=after,de=linenum
  44A7  C5            		push	bc		;before
  44A8  D5            		push	de		;line number
                      	
  44A9  EB            		ex	de,hl		;de<-after
  44AA  2A56FF        		ld	hl,(VARAD)
  44AD  B7            		or	a
  44AE  ED52          		sbc	hl,de
  44B0  E5            		push	hl		;slide size
  44B1  210500        		ld	hl,0005h
  44B4  09            		add	hl,bc
  44B5  C1            		pop	bc		;slide size
                      	;	or	a
  44B6  ED52          		sbc	hl,de		;diff=before+5-after
  44B8  300A          		jr	nc,RENMF
                      	
                      	;slide program backward
  44BA                	RENMB:
  44BA  19            		add	hl,de
  44BB  EB            		ex	de,hl		;hl<-after, de<-before+5
  44BC  EDB0          		ldir
  44BE  ED5356FF      		ld	(VARAD),de
  44C2  181E          		jr	RNMBFEND
                      	
                      	;slide program forward
  44C4                	RENMF:
  44C4  ED5B56FF      		ld	de,(VARAD)
  44C8  19            		add	hl,de
  44C9  EB            		ex	de,hl		;de<-new (VARAD)=old (VARAD)+diff
  44CA  E5            		push	hl		;old (VARAD)
                      	
  44CB  21C5FF        		ld	hl,0-003bh
  44CE  39            		add	hl,sp
  44CF  AF            		xor	a
  44D0  ED52          		sbc	hl,de
  44D2  8F            		adc	a,a
  44D3  DD7729        		ld	(ix+29h),a	;1 if over memory 
  44D6  C27845        		jp	nz,RNMDELS
                      	
  44D9  ED5356FF      		ld	(VARAD),de	;new (VARAD)
  44DD  E1            		pop	hl		;old (VARAD)
  44DE  1B            		dec	de
  44DF  2B            		dec	hl
  44E0  EDB8          		lddr
                      	
  44E2                	RNMBFEND:
  44E2  D1            		pop	de		;line number
  44E3  E1            		pop	hl		;before
  44E4  360E          		ld	(hl),0eh
  44E6  23            		inc	hl
  44E7  73            		ld	(hl),e
  44E8  23            		inc	hl
  44E9  72            		ld	(hl),d
  44EA  23            		inc	hl
  44EB  3620          		ld	(hl),' '
  44ED  23            		inc	hl
  44EE  3620          		ld	(hl),' '
                      	
                      	;for on goto/on gosub
  44F0  CD583F        		call	SKIPSPINC
  44F3  FE2C          		cp	','
  44F5  20A6          		jr	nz,RENMLP2
  44F7  23            		inc	hl
  44F8  18A8          		jr	RNMCMM
                      	
                      	
                      	;write line address
  44FA                	RNMAD:
  44FA  CDB945        		call	CHGLKP0E
  44FD  2A29FF        		ld	hl,(BASICAD)
  4500                	RENMLP3:
  4500  CDD545        		call	SRCH0D0E
  4503  3840          		jr	c,RNMCLN
  4505  D5            		push	de		;current line number
  4506  E5            		push	hl		;program address
  4507  23            		inc	hl
  4508  5E            		ld	e,(hl)		;de=line number to be searched for
  4509  23            		inc	hl
  450A  56            		ld	d,(hl)
  450B  CDFB04        		call	SRCHLN
  450E  300A          		jr	nc,RENMUD	;not found
  4510  E1            		pop	hl		;program address
  4511  D1            		pop	de		;current line number
  4512  35            		dec	(hl)		;0eh->0dh
  4513                	RNMADEND:
  4513  23            		inc	hl
  4514  71            		ld	(hl),c
  4515  23            		inc	hl
  4516  70            		ld	(hl),b
  4517  23            		inc	hl
  4518  18E6          		jr	RENMLP3
                      	
                      	;undefined line
  451A                	RENMUD:
  451A  D5            		push	de		;line number to be searched for
  451B  213545        		ld	hl,UNDEF_STR
  451E  CDCF30        		call	PUTS
  4521  E1            		pop	hl		;line number to be searched for
  4522  CDA13A        		call	PUTI2
  4525  E1            		pop	hl		;program address
  4526  E3            		ex	(sp),hl		;current line number
  4527  CD993A        		call	INLNUM
  452A  CD3927        		call	PUTNL
  452D  E1            		pop	hl		;program address
  452E  3E20          		ld	a,' '
  4530  77            		ld	(hl),a
  4531  47            		ld	b,a
  4532  4F            		ld	c,a
  4533  18DE          		jr	RNMADEND
                      	
  4535                	UNDEF_STR:
  4535  556E646566696E		db	"Undefined line ",00h
                      	
                      	
                      	;change line number
  4545                	RNMCLN:
  4545  D1            		pop	de		;2nd parameter (start line number)
  4546  CDFB04        		call	SRCHLN
  4549  03            		inc	bc
  454A  D1            		pop	de		;1st parameter (new line number)
  454B                	RENMLP4:
  454B  60            		ld	h,b
  454C  69            		ld	l,c
  454D  4E            		ld	c,(hl)
  454E  23            		inc	hl
  454F  46            		ld	b,(hl)
  4550  78            		ld	a,b
  4551  B1            		or	c
  4552  280B          		jr	z,RNMWLN
  4554  23            		inc	hl
  4555  73            		ld	(hl),e
  4556  23            		inc	hl
  4557  72            		ld	(hl),d
  4558  210A00        		ld	hl,000ah
  455B  19            		add	hl,de
  455C  EB            		ex	de,hl
  455D  18EC          		jr	RENMLP4
                      	
                      	;write line number (binary)
  455F                	RNMWLN:
  455F  2A29FF        		ld	hl,(BASICAD)
  4562                	RENMLP5:
  4562  CDD545        		call	SRCH0D0E
  4565  3811          		jr	c,RNMDELS
  4567  34            		inc	(hl)		;0dh->0eh
  4568  23            		inc	hl
  4569  5E            		ld	e,(hl)
  456A  23            		inc	hl
  456B  56            		ld	d,(hl)
  456C  2B            		dec	hl
  456D  13            		inc	de
  456E  13            		inc	de
  456F  13            		inc	de
  4570  EB            		ex	de,hl
  4571  EDA0          		ldi
  4573  EDA0          		ldi
  4575  EB            		ex	de,hl
  4576  18EA          		jr	RENMLP5
                      	
                      	
                      	;write line number (ascii) and delete space
  4578                	RNMDELS:
  4578  2A29FF        		ld	hl,(BASICAD)
  457B                	RENMLP6:
  457B  CDD545        		call	SRCH0D0E
  457E  382F          		jr	c,RENMEND
  4580  E5            		push	hl		;
  4581  23            		inc	hl
  4582  7E            		ld	a,(hl)
  4583  23            		inc	hl
  4584  66            		ld	h,(hl)
  4585  6F            		ld	l,a
  4586  CDA53A        		call	I2TOA
  4589  D1            		pop	de		;
                      	
  458A  0605          		ld	b,05h
  458C                	RENMLP7:
  458C  23            		inc	hl		;skip first space for sign
  458D  7E            		ld	a,(hl)
  458E  B7            		or	a
  458F  2807          		jr	z,SLIDE
  4591  12            		ld	(de),a
  4592  13            		inc	de
  4593  10F7          		djnz	RENMLP7
  4595  EB            		ex	de,hl
  4596  18E3          		jr	RENMLP6
                      	
  4598                	SLIDE:
  4598  67            		ld	h,a		;=0
  4599  68            		ld	l,b
  459A  19            		add	hl,de
  459B  ED4B56FF      		ld	bc,(VARAD)
  459F  79            		ld	a,c
  45A0  95            		sub	l
  45A1  4F            		ld	c,a
  45A2  78            		ld	a,b
  45A3  9C            		sbc	a,h
  45A4  47            		ld	b,a
  45A5  D5            		push	de		;
  45A6  EDB0          		ldir
  45A8  ED5356FF      		ld	(VARAD),de
  45AC  E1            		pop	hl		;
  45AD  18CC          		jr	RENMLP6
                      	
  45AF                	RENMEND:
  45AF  DD7E29        		ld	a,(ix+29h)
  45B2  B7            		or	a
  45B3  CA2B24        		jp	z,OKCHGLKP
  45B6  C3D103        		jp	OMERR
                      	
                      	
                      	;change link pointers
  45B9                	CHGLKP0E:
  45B9  2A5FFA        		ld	hl,(STARTAD)
  45BC                	CHGL0ELP1:
  45BC  7E            		ld	a,(hl)
  45BD  23            		inc	hl
  45BE  B6            		or	(hl)
  45BF  C8            		ret	z
  45C0  E5            		push	hl
  45C1                	CHGL0ELP2:
  45C1  23            		inc	hl
  45C2  23            		inc	hl
  45C3  23            		inc	hl
  45C4                	CHGL0ELP3:
  45C4  7E            		ld	a,(hl)
  45C5  23            		inc	hl
  45C6  FE0E          		cp	0eh
  45C8  28F7          		jr	z,CHGL0ELP2
  45CA  B7            		or	a
  45CB  20F7          		jr	nz,CHGL0ELP3
                      	
  45CD  EB            		ex	de,hl
  45CE  E1            		pop	hl
  45CF  72            		ld	(hl),d
  45D0  2B            		dec	hl
  45D1  73            		ld	(hl),e
  45D2  EB            		ex	de,hl
  45D3  18E7          		jr	CHGL0ELP1
                      	
                      	
                      	;search 0dh or 0eh
                      	;input: hl=address
                      	;output: hl=address, de=line number, c-flag(0=found,1=program end)
                      	;destroy: af
  45D5                	SRCH0D0E:
  45D5  7E            		ld	a,(hl)
  45D6  FE0D          		cp	0dh
  45D8  C8            		ret	z
  45D9  FE0E          		cp	0eh
  45DB  C8            		ret	z
  45DC  23            		inc	hl
  45DD  B7            		or	a
  45DE  20F5          		jr	nz,SRCH0D0E
                      	
  45E0  7E            		ld	a,(hl)
  45E1  23            		inc	hl
  45E2  B6            		or	(hl)
  45E3  FE01          		cp	01h
  45E5  D8            		ret	c
  45E6  23            		inc	hl
  45E7  5E            		ld	e,(hl)
  45E8  23            		inc	hl
  45E9  56            		ld	d,(hl)
  45EA  23            		inc	hl
  45EB  18E8          		jr	SRCH0D0E
                      	
                      	
                      	;search line number to be renumbered
                      	;input: hl=address
                      	;output: hl=address, z-flag(1=program end)
                      	;destroy: af,b,de
  45ED                	SRCHRNM:
  45ED  7E            		ld	a,(hl)
  45EE  23            		inc	hl
  45EF  B7            		or	a
  45F0  2823          		jr	z,SRCHRNMZ
  45F2  FE22          		cp	22h		;double quotation
  45F4  2828          		jr	z,RENMDQ
  45F6  FE8E          		cp	REM_
  45F8  2831          		jr	z,RENMREM
  45FA  FE83          		cp	DATA_
  45FC  282B          		jr	z,RENMDATA
                      	
  45FE  EB            		ex	de,hl
  45FF  210F46        		ld	hl,RNMTBL
  4602  010600        		ld	bc,0006h
  4605  EDB1          		cpir
  4607  EB            		ex	de,hl
  4608  20E3          		jr	nz,SRCHRNM
                      	
                      	;command found
  460A  2B            		dec	hl
  460B  D7            		rst	ANADAT
  460C  D8            		ret	c
  460D  18DE          		jr	SRCHRNM
                      	
  460F                	RNMTBL:
  460F  88898B8C97C7  		db	GOTO_, RUN_, RSTR_, GOSUB_, LIST_, THEN_
                      	
  4615                	SRCHRNMZ:
  4615  7E            		ld	a,(hl)
  4616  23            		inc	hl
  4617  B6            		or	(hl)
  4618  C8            		ret	z		;program end
  4619  23            		inc	hl
  461A  23            		inc	hl
  461B  23            		inc	hl
  461C  18CF          		jr	SRCHRNM
                      	
  461E                	RENMDQ:
  461E  7E            		ld	a,(hl)
  461F  23            		inc	hl
  4620  B7            		or	a
  4621  28F2          		jr	z,SRCHRNMZ
  4623  FE22          		cp	22h		;double quotation
  4625  20F7          		jr	nz,RENMDQ
  4627  18C4          		jr	SRCHRNM
                      	
  4629                	RENMDATA:
  4629  013A          		db	01h, ':'	;ld bc,0e3ah/nop
  462B                	RENMREM:
  462B  0E00          		ld	c,00h
  462D  CDE407        		call	DATREM
  4630  18BB          		jr	SRCHRNM
                      	
                      	
                      	;HEX$() function
  4632                	F_HEX:
  4632  CD9B0B        		call	CHKNUM
  4635  CD9F0C        		call	FTOI
                      	
  4638  2172FF        		ld	hl,FAC3
  463B  0E00          		ld	c,00h		;length
  463D  7A            		ld	a,d
  463E  CD5446        		call	CNVHEX2
  4641  7B            		ld	a,e
  4642  CD5446        		call	CNVHEX2
  4645  2003          		jr	nz,HEXNZ	;if c<>0
  4647  3630          		ld	(hl),'0'
  4649  0C            		inc	c
  464A                	HEXNZ:
  464A  79            		ld	a,c
  464B  2172FF        		ld	hl,FAC3
  464E  CD4227        		call	MAKESTR
  4651  C39027        		jp	INKYEND
                      	
                      	
                      	;input: a=data, c=length, hl=address
                      	;output: c=length, z-flag(1:c=0)
                      	;destroy: af,b
  4654                	CNVHEX2:
  4654  47            		ld	b,a
  4655  0F            		rrca
  4656  0F            		rrca
  4657  0F            		rrca
  4658  0F            		rrca
  4659  CD5D46        		call	CNVHEX1
  465C  78            		ld	a,b
                      	;	call	CNVHEX1
                      	;	ret
                      	
  465D                	CNVHEX1:
  465D  E60F          		and	0fh
  465F  2003          		jr	nz,CNVHEXNZ
  4661  0C            		inc	c
  4662  0D            		dec	c
  4663  C8            		ret	z
  4664                	CNVHEXNZ:
  4664  FE0A          		cp	0ah
  4666  DE69          		sbc	a,69h
  4668  27            		daa
  4669  77            		ld	(hl),a
  466A  23            		inc	hl
  466B  0C            		inc	c
  466C  C9            		ret
                      	
                      	
                      	;CIRCLE command work area
  FEAD                	XRAD	equ	GRPX2		;x-radius (2bytes)
  FEAF                	YRAD	equ	GRPY2		;y-radius (2bytes)
  FF65                	DEDX	equ	FAC1-1		;de/dx (6bytes)
  FF6C                	DEDY	equ	FAC2-1		;de/dy (6bytes)
  FF72                	ERR	equ	FAC3		;error (6bytes)
                      	
  FF78                	STARTX	equ	0ff78h
  FF7A                	STARTY	equ	0ff7ah
  FF7C                	ENDX	equ	0ff7ch
  FF7E                	ENDY	equ	0ff7eh
  FF81                	CRCLFLG	equ	0ff81h
                      	
                      	
  466D                	CRCLFDATA:
  466D  77FFFFFF      		db	77h, 0ffh, 0ffh, 0ffh	;end quad.-start quad.=4
  4671  6677EEFF      		db	66h, 77h, 0eeh, 0ffh	;end quad.-start quad.=3
  4675  4466CCEE      		db	44h, 66h, 0cch, 0eeh	;end quad.-start quad.=2
  4679  004488CC      		db	00h, 44h, 88h, 0cch	;end quad.-start quad.=1
  467D  00000088      		db	00h, 00h, 00h, 88h	;end quad.-start quad.=0
                      	
  4681                	CRCLMAIN:
  4681  2AADFE        		ld	hl,(XRAD)
  4684  ED5BE6FF      		ld	de,(ASPECT)
  4688  CDF921        		call	MULINT2
  468B  CD784C        		call	ROUNDI2
  468E  22AFFE        		ld	(YRAD),hl	;yrad=r*a
                      	
  4691                	CRCLMAIN2:
  4691  CD3439        		call	POPF1		;end angle
  4694  CD4339        		call	POPF2		;start angle
                      	
                      	
  4697  2169FF        		ld	hl,FAC1+3
  469A  7E            		ld	a,(hl)
  469B  F5            		push	af		;sign of end angle
  469C  CBBE          		res	7,(hl)
  469E  2170FF        		ld	hl,FAC2+3
  46A1  7E            		ld	a,(hl)
  46A2  F5            		push	af		;sign of start angle
  46A3  CBBE          		res	7,(hl)
                      	
  46A5  CD1A3C        		call	PUSHF2		;end angle
  46A8  CD1B3F        		call	CMPF
                      	
  46AB  9F            		sbc	a,a
  46AC  3C            		inc	a		;start<=end?
  46AD  F5            		push	af
                      	
  46AE  CD5D4B        		call	ANGL2XY
  46B1  227CFF        		ld	(ENDX),hl
  46B4  ED537EFF      		ld	(ENDY),de
                      	
  46B8  F1            		pop	af
  46B9  07            		rlca
  46BA  07            		rlca
  46BB  B0            		or	b
  46BC  57            		ld	d,a		;bit2=start<=end?, bit10=end quad.
                      	
  46BD  CD3439        		call	POPF1		;start angle
  46C0  D5            		push	de
                      	
  46C1  CD5D4B        		call	ANGL2XY
  46C4  2278FF        		ld	(STARTX),hl
  46C7  ED537AFF      		ld	(STARTY),de
                      	
  46CB  F1            		pop	af
  46CC  07            		rlca
  46CD  07            		rlca
  46CE  B0            		or	b
  46CF  3281FF        		ld	(CRCLFLG),a	;bit4=start<=end?, bit32=end quad., bit10=start quad.
                      	
                      	
  46D2  2AADFE        		ld	hl,(XRAD)	;"a"
  46D5  54            		ld	d,h
  46D6  5D            		ld	e,l
  46D7  CDF921        		call	MULINT2		;a*a=aa
  46DA  E5            		push	hl		;aa
  46DB  D5            		push	de
  46DC  CD984C        		call	DEHL4
  46DF  42            		ld	b,d
  46E0  4B            		ld	c,e
  46E1  EB            		ex	de,hl
  46E2  D9            		exx			;bcde'=4aa
                      	
  46E3  2AAFFE        		ld	hl,(YRAD)	;"b"
  46E6  54            		ld	d,h
  46E7  5D            		ld	e,l
  46E8  CDF921        		call	MULINT2		;b*b=bb
  46EB  E5            		push	hl		;bb
  46EC  D5            		push	de
  46ED  CD984C        		call	DEHL4
  46F0  29            		add	hl,hl
  46F1  CB13          		rl	e
  46F3  CB12          		rl	d
  46F5  E5            		push	hl		;8bb
  46F6  D5            		push	de
                      	
                      	;de/dx_ini=8abb
  46F7  EB            		ex	de,hl
  46F8  2AADFE        		ld	hl,(XRAD)
  46FB  CDF921        		call	MULINT2		;a*8bb=8abb (low)
  46FE  2265FF        		ld	(DEDX),hl
  4701  ED5367FF      		ld	(DEDX+2),de
  4705  2AADFE        		ld	hl,(XRAD)
  4708  D1            		pop	de		;8bb (high)
  4709  D5            		push	de
  470A  CDF921        		call	MULINT2		;a*8bb=8abb (high)
  470D  ED4B67FF      		ld	bc,(DEDX+2)
  4711  09            		add	hl,bc
  4712  2267FF        		ld	(DEDX+2),hl
  4715  3001          		jr	nc,CRCLNC1
  4717  13            		inc	de
  4718                	CRCLNC1:
  4718  ED5369FF      		ld	(DEDX+4),de
                      	
                      	;err_ini=bb(1-4a)
  471C  216AFF        		ld	hl,DEDX+5
  471F  CDE149        		call	ERRINI
                      	
                      	;de/dy_ini=-4aa
  4722  D9            		exx
  4723  D5            		push	de		;4aa
                      	;	push	bc
                      	
  4724  216CFF        		ld	hl,DEDY
  4727  CD134A        		call	NEG6
                      	
                      	;	pop	bc
  472A  E1            		pop	hl
  472B  29            		add	hl,hl
  472C  EB            		ex	de,hl
  472D  CB11          		rl	c
  472F  CB10          		rl	b
  4731  D9            		exx			;bcde'=8aa
                      	
  4732  2AADFE        		ld	hl,(XRAD)	;x=x-radius
  4735  22B1FE        		ld	(GRPX3),hl
  4738  210000        		ld	hl,0000h	;y=0
  473B  22B3FE        		ld	(GRPY3),hl
                      	
  473E                	CRCLLP1:
  473E  0601          		ld	b,01h
  4740  CD5348        		call	CRCLPSET
  4743  2AB3FE        		ld	hl,(GRPY3)
  4746  23            		inc	hl
  4747  22B3FE        		ld	(GRPY3),hl
                      	
                      	;de/dy+=8aa
                      	;err+=de/dy
  474A  D9            		exx
  474B  216CFF        		ld	hl,DEDY
  474E  CD9149        		call	ADD6		;exx
                      	
                      	;if err>=0 and x>0 then x=x-1:de/dx-=8bb:err-=de/dx
  4751  07            		rlca
  4752  3815          		jr	c,CRCLC1
  4754  2AB1FE        		ld	hl,(GRPX3)
  4757  7C            		ld	a,h
  4758  B5            		or	l
  4759  280E          		jr	z,CRCLC1
  475B  2B            		dec	hl
  475C  22B1FE        		ld	(GRPX3),hl
                      	
                      	;de/dx-=8bb
                      	;err-=de/dx
  475F  C1            		pop	bc		;8bb
  4760  D1            		pop	de
  4761  D5            		push	de		;8bb
  4762  C5            		push	bc
  4763  2165FF        		ld	hl,DEDX
  4766  CDB949        		call	SUB6
                      	
  4769                	CRCLC1:
                      	;if de/dx>de/dy or (de/dx=0 and y<b) then loop
  4769  1171FF        		ld	de,DEDY+5
  476C  216AFF        		ld	hl,DEDX+5
  476F  CD2A4A        		call	CMP6
  4772  38CA          		jr	c,CRCLLP1
  4774  2165FF        		ld	hl,DEDX
  4777  CD344A        		call	CHKZ6
  477A  200B          		jr	nz,CRCLNZ1
  477C  2AB3FE        		ld	hl,(GRPY3)
  477F  ED5BAFFE      		ld	de,(YRAD)
                      	;	or	a
  4783  ED52          		sbc	hl,de
  4785  38B7          		jr	c,CRCLLP1
                      	
  4787                	CRCLNZ1:
                      	;de/dx_ini=-4bb
  4787  C1            		pop	bc		;8bb
  4788  D1            		pop	de
                      	
  4789  D5            		push	de
  478A  C5            		push	bc
  478B  CB38          		srl	b		;4bb
  478D  CB19          		rr	c
  478F  CB1A          		rr	d
  4791  CB1B          		rr	e
                      	
  4793  2165FF        		ld	hl,DEDX
  4796  CD134A        		call	NEG6
                      	
  4799  C1            		pop	bc		;8bb
  479A  D1            		pop	de
  479B  D9            		exx
                      	
  479C  D5            		push	de		;8aa
  479D  C5            		push	bc
                      	
                      	
                      	;de/dy_ini=8aab
  479E  2AAFFE        		ld	hl,(YRAD)
  47A1  CDF921        		call	MULINT2		;8aa*b=8aab (low)
  47A4  226CFF        		ld	(DEDY),hl
  47A7  ED536EFF      		ld	(DEDY+2),de
  47AB  2AAFFE        		ld	hl,(YRAD)
  47AE  D1            		pop	de		;8aa (high)
  47AF  D5            		push	de
  47B0  CDF921        		call	MULINT2		;8aa*b=8aab (high)
  47B3  ED4B6EFF      		ld	bc,(DEDY+2)
  47B7  09            		add	hl,bc
  47B8  226EFF        		ld	(DEDY+2),hl
  47BB  3001          		jr	nc,CRCLNC4
  47BD  13            		inc	de
  47BE                	CRCLNC4:
  47BE  ED5370FF      		ld	(DEDY+4),de
                      	
                      	;err_ini=aa(1-4b)
  47C2  2171FF        		ld	hl,DEDY+5
  47C5  CDE149        		call	ERRINI
                      	
  47C8  210000        		ld	hl,0000h	;x=0
  47CB  22B1FE        		ld	(GRPX3),hl
  47CE  2AAFFE        		ld	hl,(YRAD)	;y=y-radius
  47D1  22B3FE        		ld	(GRPY3),hl
                      	
  47D4                	CRCLLP2:
  47D4  0602          		ld	b,02h
  47D6  CD5348        		call	CRCLPSET
  47D9  2AB1FE        		ld	hl,(GRPX3)
  47DC  23            		inc	hl
  47DD  22B1FE        		ld	(GRPX3),hl
                      	
                      	;de/dx+=8bb
                      	;err+=de/dx
  47E0  D9            		exx
  47E1  2165FF        		ld	hl,DEDX
  47E4  CD9149        		call	ADD6		;exx
                      	
                      	;if err>=0 and y>0 then y=y-1:de/dy-=8aa:err-=de/dy
  47E7  07            		rlca
  47E8  3815          		jr	c,CRCLC2
  47EA  2AB3FE        		ld	hl,(GRPY3)
  47ED  7C            		ld	a,h
  47EE  B5            		or	l
  47EF  280E          		jr	z,CRCLC2
  47F1  2B            		dec	hl
  47F2  22B3FE        		ld	(GRPY3),hl	;y=y-1
                      	
                      	;de/dy-=8aa
                      	;err-=de/dy
  47F5  C1            		pop	bc		;8aa
  47F6  D1            		pop	de
  47F7  D5            		push	de		;8aa
  47F8  C5            		push	bc
  47F9  216CFF        		ld	hl,DEDY
  47FC  CDB949        		call	SUB6
                      	
  47FF                	CRCLC2:
                      	;if de/dx<de/dy or (de/dy=0 and x<a) then loop
  47FF  116AFF        		ld	de,DEDX+5
  4802  2171FF        		ld	hl,DEDY+5
  4805  CD2A4A        		call	CMP6
  4808  38CA          		jr	c,CRCLLP2
  480A  216CFF        		ld	hl,DEDY
  480D  CD344A        		call	CHKZ6
  4810  200B          		jr	nz,CRCLNZ2
  4812  2AB1FE        		ld	hl,(GRPX3)
  4815  ED5BADFE      		ld	de,(XRAD)
                      	;	or	a
  4819  ED52          		sbc	hl,de
  481B  38B7          		jr	c,CRCLLP2
                      	
  481D                	CRCLNZ2:
  481D  C1            		pop	bc		;8aa
  481E  D1            		pop	de
                      	
                      	
  481F  2A78FF        		ld	hl,(STARTX)
  4822  ED5B7AFF      		ld	de,(STARTY)
  4826  F1            		pop	af		;sign of start angle
  4827  CD3248        		call	CRCLLINE
  482A  2A7CFF        		ld	hl,(ENDX)
  482D  ED5B7EFF      		ld	de,(ENDY)
  4831  F1            		pop	af		;sign of start angle
                      	;	jr	CRCLLINE
                      	
                      	;for circle command
                      	;draw line from center
                      	;input: hl=x-xcenter, de=-(y-ycenter), a=sign of angle(bit7=1:minus)
                      	;destroy: af,bc,de,hl
  4832                	CRCLLINE:
  4832  ED4BAEFD      		ld	bc,(GRPX1)	;xcenter
  4836  09            		add	hl,bc
  4837  22ADFE        		ld	(GRPX2),hl
  483A  2AB0FD        		ld	hl,(GRPY1)	;ycenter
  483D  B7            		or	a
  483E  ED52          		sbc	hl,de
  4840  22AFFE        		ld	(GRPY2),hl
  4843  19            		add	hl,de
  4844  EB            		ex	de,hl
                      	
  4845  07            		rlca
  4846  3808          		jr	c,CRCLLC	;if angle<0
  4848  ED4BADFE      		ld	bc,(GRPX2)
  484C  ED5BAFFE      		ld	de,(GRPY2)
  4850                	CRCLLC:
  4850  C31F2E        		jp	LINE
                      	
                      	
                      	;input: b(1:condition=y, 2:condition=x)
                      	;destroy: af,bc,de,hl,af'
  4853                	CRCLPSET:
  4853  CD4D27        		call	STOPESC
                      	
  4856  2A7AFF        		ld	hl,(STARTY)
  4859  ED5BB3FE      		ld	de,(GRPY3)
  485D  E5            		push	hl
  485E  D5            		push	de
  485F  2A78FF        		ld	hl,(STARTX)
  4862  ED5BB1FE      		ld	de,(GRPX3)
                      	
  4866  3A81FF        		ld	a,(CRCLFLG)
  4869  E603          		and	03h		;bit10=start quad.(3,2,1.0)
  486B  2828          		jr	z,STARTQ4
  486D  EAA648        		jp	pe,STARTQ1
  4870  0F            		rrca
  4871  3812          		jr	c,STARTQ3
                      	
  4873                	STARTQ2:
  4873  100A          		djnz	STARTQ2X
  4875  D1            		pop	de
  4876  E1            		pop	hl
  4877  B7            		or	a
  4878  ED52          		sbc	hl,de
  487A  CB04          		rlc	h
  487C  3F            		ccf			;y<=STARTY?
  487D  1839          		jr	STARTQEND
  487F                	STARTQ2X:
  487F  19            		add	hl,de
  4880  CB04          		rlc	h
  4882  3F            		ccf			;-x<=STARTX?
  4883  1831          		jr	STARTQ1XEND
                      	
  4885                	STARTQ3:
  4885  1008          		djnz	STARTQ3X
  4887  D1            		pop	de
  4888  E1            		pop	hl
  4889  19            		add	hl,de
  488A  CB04          		rlc	h
  488C  3F            		ccf			;-y<=STARTY?
  488D  1829          		jr	STARTQEND
  488F                	STARTQ3X:
  488F  2B            		dec	hl
  4890  19            		add	hl,de
  4891  CB04          		rlc	h		;-x>=STARTX?
  4893  1821          		jr	STARTQ1XEND
                      	
  4895                	STARTQ4:
  4895  1008          		djnz	STARTQ4X
  4897  D1            		pop	de
  4898  E1            		pop	hl
  4899  2B            		dec	hl
  489A  19            		add	hl,de
  489B  CB04          		rlc	h		;-y>=STARTY?
  489D  1819          		jr	STARTQEND
  489F                	STARTQ4X:
  489F  37            		scf
  48A0  ED52          		sbc	hl,de
  48A2  CB04          		rlc	h		;x>=STARTX?
  48A4  1810          		jr	STARTQ1XEND
                      	
                      	;1st quad.
  48A6                	STARTQ1:
  48A6  1009          		djnz	STARTQ1X
  48A8  D1            		pop	de
  48A9  E1            		pop	hl
  48AA  37            		scf
  48AB  ED52          		sbc	hl,de
  48AD  CB04          		rlc	h		;y>=STARTY?
  48AF  1807          		jr	STARTQEND
  48B1                	STARTQ1X:
                      	;	or	a
  48B1  ED52          		sbc	hl,de
  48B3  CB04          		rlc	h
  48B5  3F            		ccf			;x<=STARTX?
                      	
  48B6                	STARTQ1XEND:
  48B6  D1            		pop	de
  48B7  E1            		pop	hl
                      	
  48B8                	STARTQEND:
  48B8  CB11          		rl	c		;start ok?
                      	
  48BA  2A7CFF        		ld	hl,(ENDX)
  48BD  ED5BB1FE      		ld	de,(GRPX3)
  48C1  E5            		push	hl
  48C2  D5            		push	de
  48C3  2A7EFF        		ld	hl,(ENDY)
  48C6  ED5BB3FE      		ld	de,(GRPY3)
                      	
  48CA  3A81FF        		ld	a,(CRCLFLG)
  48CD  E60C          		and	0ch		;bit32=start quad.(3,2,1.0)
  48CF  2828          		jr	z,ENDQ4
  48D1  EA0A49        		jp	pe,ENDQ1
  48D4  E604          		and	04h
  48D6  2011          		jr	nz,ENDQ3
                      	
  48D8                	ENDQ2:
  48D8  1008          		djnz	ENDQ2Y
  48DA  D1            		pop	de
  48DB  E1            		pop	hl
  48DC  2B            		dec	hl
  48DD  19            		add	hl,de
  48DE  CB04          		rlc	h		;-x>=ENDX?
  48E0  183B          		jr	ENDQEND
  48E2                	ENDQ2Y:
  48E2  37            		scf
  48E3  ED52          		sbc	hl,de
  48E5  CB04          		rlc	h		;y>=ENDY?
  48E7  1832          		jr	ENDQ1YEND
                      	
  48E9                	ENDQ3:
  48E9  1008          		djnz	ENDQ3Y
  48EB  D1            		pop	de
  48EC  E1            		pop	hl
  48ED  19            		add	hl,de
  48EE  CB04          		rlc	h
  48F0  3F            		ccf			;-x<=ENDX?
  48F1  182A          		jr	ENDQEND
  48F3                	ENDQ3Y:
  48F3  2B            		dec	hl
  48F4  19            		add	hl,de
  48F5  CB04          		rlc	h		;-y>=ENDY?
  48F7  1822          		jr	ENDQ1YEND
                      	
  48F9                	ENDQ4:
  48F9  1009          		djnz	ENDQ4Y
  48FB  D1            		pop	de
  48FC  E1            		pop	hl
                      	;	or	a
  48FD  ED52          		sbc	hl,de
  48FF  CB14          		rl	h
  4901  3F            		ccf			;x<=ENDX?
  4902  1819          		jr	ENDQEND
  4904                	ENDQ4Y:
  4904  19            		add	hl,de
  4905  CB04          		rlc	h
  4907  3F            		ccf			;-y<=ENDY?
  4908  1811          		jr	ENDQ1YEND
                      	
  490A                	ENDQ1:
  490A  1009          		djnz	ENDQ1Y
  490C  D1            		pop	de
  490D  E1            		pop	hl
  490E  37            		scf
  490F  ED52          		sbc	hl,de
  4911  CB04          		rlc	h		;x>=ENDX?
  4913  1808          		jr	ENDQEND
  4915                	ENDQ1Y:
  4915  B7            		or	a
  4916  ED52          		sbc	hl,de
  4918  CB04          		rlc	h
  491A  3F            		ccf			;y<=ENDY?
  491B                	ENDQ1YEND:
  491B  D1            		pop	de
  491C  E1            		pop	hl
  491D                	ENDQEND:
  491D  CB11          		rl	c		;bit1=start ok, bit0=end ok
                      	
  491F  3A81FF        		ld	a,(CRCLFLG)	;bit4=start<=end?, bit32=end quad., bit10=start quad.
  4922  47            		ld	b,a
  4923  0F            		rrca
  4924  0F            		rrca
  4925  90            		sub	b
  4926  E603          		and	03h		;end quadrant-start quadrant
  4928  87            		add	a,a
  4929  87            		add	a,a
  492A  2004          		jr	nz,CRCLPNZ
  492C  A8            		xor	b
  492D  E6EF          		and	0efh		;copy b-bit4
  492F  A8            		xor	b
  4930                	CRCLPNZ:
  4930  A9            		xor	c
  4931  E6FC          		and	0fch		;copy c-bit1,0
  4933  A9            		xor	c
  4934  5F            		ld	e,a
  4935  1600          		ld	d,00h
  4937  216D46        		ld	hl,CRCLFDATA
  493A  19            		add	hl,de
                      	
  493B  78            		ld	a,b		;(CRCLFLG)
  493C  E603          		and	03h		;start quad.
  493E  47            		ld	b,a
  493F  7E            		ld	a,(hl)
  4940  04            		inc	b
  4941                	CRCLPLP:
  4941  07            		rlca
  4942  10FD          		djnz	CRCLPLP
  4944  08            		ex	af,af'
                      	
  4945  2AAEFD        		ld	hl,(GRPX1)	;xcenter
  4948  ED5BB1FE      		ld	de,(GRPX3)	;x
  494C  B7            		or	a
  494D  ED52          		sbc	hl,de
  494F  E5            		push	hl		;xcenter-x
  4950  19            		add	hl,de
  4951  19            		add	hl,de
  4952  44            		ld	b,h
  4953  4D            		ld	c,l
                      	
  4954  2AB0FD        		ld	hl,(GRPY1)	;ycenter
  4957  ED5BB3FE      		ld	de,(GRPY3)	;y
  495B  B7            		or	a
  495C  ED52          		sbc	hl,de
  495E  E5            		push	hl		;ycenter-y
  495F  19            		add	hl,de
  4960  19            		add	hl,de
  4961  EB            		ex	de,hl
                      	
  4962  D5            		push	de		;ycenter+y
  4963  C5            		push	bc		;xcenter+x
                      	
  4964  08            		ex	af,af'
  4965  67            		ld	h,a
  4966  08            		ex	af,af'
  4967  CB64          		bit	4,h
                      	
  4969  C4472D        		call	nz,PSET		;4th quadrant (xcenter+x,ycenter+y)
                      	
  496C  C1            		pop	bc
  496D  E1            		pop	hl
  496E  D1            		pop	de
  496F  E5            		push	hl
  4970  D5            		push	de
                      	
  4971  08            		ex	af,af'
  4972  67            		ld	h,a
  4973  08            		ex	af,af'
  4974  CB7C          		bit	7,h
                      	
  4976  C4472D        		call	nz,PSET		;1st quadrant (xcenter+x,ycenter-y)
  4979  D1            		pop	de
  497A  E1            		pop	hl
  497B  C1            		pop	bc
  497C  E5            		push	hl
  497D  C5            		push	bc
                      	
  497E  08            		ex	af,af'
  497F  67            		ld	h,a
  4980  08            		ex	af,af'
  4981  CB74          		bit	6,h
                      	
  4983  C4472D        		call	nz,PSET		;2nd quadrant (xcenter-x,ycenter-y)
  4986  C1            		pop	bc
  4987  D1            		pop	de
                      	
  4988  08            		ex	af,af'
  4989  67            		ld	h,a
  498A  08            		ex	af,af'
  498B  CB6C          		bit	5,h
                      	
  498D  C8            		ret	z
  498E  C3472D        		jp	PSET		;3rd quadrant (xcenter-x,ycenter+y)
                      	
                      	
                      	;for circle command
                      	;add 6-byte integer and 4-byte integer
                      	;add 6-byte integer and 6-byte integer
                      	;(hl6)=(hl6)+00bcde, (ERR6)=(ERR6)+(hl6)
                      	;input: bcde, hl=data address
                      	;output: a=(ERR+5), bcde'=bcde
                      	;destroy: f,bc,de,hl,hl'
  4991                	ADD6:
                      	;(hl6)=(hl6)+bcde
  4991  E5            		push	hl
  4992  7E            		ld	a,(hl)
  4993  83            		add	a,e
  4994  77            		ld	(hl),a
  4995  23            		inc	hl
                      	
  4996  7E            		ld	a,(hl)
  4997  8A            		adc	a,d
  4998  77            		ld	(hl),a
  4999  23            		inc	hl
                      	
  499A  7E            		ld	a,(hl)
  499B  89            		adc	a,c
  499C  77            		ld	(hl),a
  499D  23            		inc	hl
                      	
  499E  7E            		ld	a,(hl)
  499F  88            		adc	a,b
  49A0  77            		ld	(hl),a
  49A1  3006          		jr	nc,ADD6NC
                      	
  49A3  23            		inc	hl
  49A4  34            		inc	(hl)
  49A5  2002          		jr	nz,ADD6NC
                      	
  49A7  23            		inc	hl
  49A8  34            		inc	(hl)
  49A9                	ADD6NC:
  49A9  D9            		exx
  49AA  E1            		pop	hl
                      	
                      	;(ERR6)=(ERR6)+(hl6)
  49AB  B7            		or	a
  49AC  1172FF        		ld	de,ERR
  49AF  0606          		ld	b,06h
  49B1                	ADD6LP:
  49B1  1A            		ld	a,(de)
  49B2  8E            		adc	a,(hl)
  49B3  12            		ld	(de),a
  49B4  13            		inc	de
  49B5  23            		inc	hl
  49B6  10F9          		djnz	ADD6LP
  49B8  C9            		ret
                      	
                      	
                      	;for circle command
                      	;subtract 4-byte integer from 6-byte integer
                      	;subtract 6-byte integer from 6-byte integer
                      	;(hl6)=(hl6)-00bcde, (ERR6)=(ERR6)-(hl6)
                      	;input: bcde, hl=data address
                      	;destroy: f,b,de,hl
  49B9                	SUB6:
                      	;(hl6)=(hl6)-bcde
  49B9  E5            		push	hl
  49BA  7E            		ld	a,(hl)
  49BB  93            		sub	e
  49BC  77            		ld	(hl),a
  49BD  23            		inc	hl
                      	
  49BE  7E            		ld	a,(hl)
  49BF  9A            		sbc	a,d
  49C0  77            		ld	(hl),a
  49C1  23            		inc	hl
                      	
  49C2  7E            		ld	a,(hl)
  49C3  99            		sbc	a,c
  49C4  77            		ld	(hl),a
  49C5  23            		inc	hl
                      	
  49C6  7E            		ld	a,(hl)
  49C7  98            		sbc	a,b
  49C8  77            		ld	(hl),a
  49C9  3008          		jr	nc,SUB6NC
                      	
  49CB  23            		inc	hl
  49CC  7E            		ld	a,(hl)
  49CD  35            		dec	(hl)
  49CE  B7            		or	a
  49CF  2002          		jr	nz,SUB6NC
                      	
  49D1  23            		inc	hl
  49D2  35            		dec	(hl)
  49D3                	SUB6NC:
  49D3  E1            		pop	hl
                      	
                      	;(ERR6)=(ERR6)-(hl6)
                      	;	or	a
  49D4  1172FF        		ld	de,ERR
  49D7  0606          		ld	b,06h
  49D9                	SUB6LP:
  49D9  1A            		ld	a,(de)
  49DA  9E            		sbc	a,(hl)
  49DB  12            		ld	(de),a
  49DC  13            		inc	de
  49DD  23            		inc	hl
  49DE  10F9          		djnz	SUB6LP
  49E0  C9            		ret
                      	
                      	
                      	;for circle command
                      	;(ERR6)=4-byte integer-(hl6)/2
                      	;input: hl=data address+5
                      	;pushed: *,*,high,low
                      	;destroy: af,bc,de,hl
  49E1                	ERRINI:
  49E1  1177FF        		ld	de,ERR+5
  49E4  B7            		or	a
  49E5  0606          		ld	b,06h
  49E7                	ERRINILP:
  49E7  7E            		ld	a,(hl)
  49E8  1F            		rra
  49E9  12            		ld	(de),a
  49EA  2B            		dec	hl
  49EB  1B            		dec	de
  49EC  10F9          		djnz	ERRINILP
                      	
  49EE  F1            		pop	af		;return address
  49EF  C1            		pop	bc		;8aa or 8bb
  49F0  E1            		pop	hl
  49F1  D1            		pop	de		;aa or bb
  49F2  E3            		ex	(sp),hl
  49F3  C5            		push	bc
  49F4  F5            		push	af		;return address
                      	
                      	
                      	;(ERR6)=00dehl-(ERR6)
                      	;	push	de		;
                      	;	ld	de,(ERR)
                      	;	xor	a
                      	;	sbc	hl,de
                      	;	ld	(ERR),hl
                      	;	pop	hl		;
                      	;	ld	de,(ERR+2)
                      	;	sbc	hl,de
                      	;	ld	(ERR+2),hl
                      	;	ld	h,a
                      	;	ld	l,a		;=0
                      	;	ld	de,(ERR+4)
                      	;	sbc	hl,de
                      	;	ld	(ERR+4),hl
                      	;	ret
                      	
  49F5  44            		ld	b,h		;
  49F6  7D            		ld	a,l
  49F7  2172FF        		ld	hl,ERR
  49FA  96            		sub	(hl)
  49FB  77            		ld	(hl),a
  49FC  23            		inc	hl
  49FD  78            		ld	a,b		;
  49FE  9E            		sbc	a,(hl)
  49FF  77            		ld	(hl),a
  4A00  23            		inc	hl
  4A01  7B            		ld	a,e
  4A02  9E            		sbc	a,(hl)
  4A03  77            		ld	(hl),a
  4A04  23            		inc	hl
  4A05  7A            		ld	a,d
  4A06  9E            		sbc	a,(hl)
  4A07  77            		ld	(hl),a
  4A08  23            		inc	hl
  4A09  3E00          		ld	a,00h
  4A0B  9E            		sbc	a,(hl)
  4A0C  77            		ld	(hl),a
  4A0D  23            		inc	hl
  4A0E  3E00          		ld	a,00h
  4A10  9E            		sbc	a,(hl)
  4A11  77            		ld	(hl),a
  4A12  C9            		ret
                      	
                      	
                      	;negate 6-byte integer
                      	;(hl6)=-00bcde
                      	;input: bcde, hl=output address
                      	;destroy: af,hl
  4A13                	NEG6:
  4A13  AF            		xor	a
  4A14  93            		sub	e
  4A15  77            		ld	(hl),a
  4A16  23            		inc	hl
  4A17  83            		add	a,e		;=0, no change in c-flag
  4A18  9A            		sbc	a,d
  4A19  77            		ld	(hl),a
  4A1A  23            		inc	hl
  4A1B  3E00          		ld	a,00h
  4A1D  99            		sbc	a,c
  4A1E  77            		ld	(hl),a
  4A1F  23            		inc	hl
  4A20  3E00          		ld	a,00h
  4A22  98            		sbc	a,b
  4A23  77            		ld	(hl),a
  4A24  23            		inc	hl
  4A25  9F            		sbc	a,a
  4A26  77            		ld	(hl),a
  4A27  23            		inc	hl
  4A28  77            		ld	(hl),a
  4A29  C9            		ret
                      	
                      	
                      	;compare 6-byte integer
                      	;input: de,hl=data address+5 (de-hl)
                      	;output: c,z-flag
                      	;destroy: af,b,de,hl
  4A2A                	CMP6:
  4A2A  0606          		ld	b,06h
  4A2C                	CMP6LP:
  4A2C  1A            		ld	a,(de)
  4A2D  BE            		cp	(hl)
  4A2E  C0            		ret	nz
  4A2F  1B            		dec	de
  4A30  2B            		dec	hl
  4A31  10F9          		djnz	CMP6LP
  4A33  C9            		ret
                      	
                      	
                      	;check zero or not for 6-byte integer
                      	;input: hl=data address
                      	;destroy: af,b,hl
  4A34                	CHKZ6:
  4A34  AF            		xor	a
  4A35  0606          		ld	b,06h
  4A37                	CHKZ6LP:
  4A37  B6            		or	(hl)
  4A38  23            		inc	hl
  4A39  10FC          		djnz	CHKZ6LP
  4A3B  C9            		ret
                      	
                      	
                      	;CIRCLE command
  4A3C                	_C_CRCL:ds	C_CRCL-_C_CRCL
  4A72                		org	C_CRCL
                      	
  4A72  3A93FD        		ld	a,(COLOR1)
  4A75  CDC015        		call	SETATT
  4A78  CDC62D        		call	SKIPAT
  4A7B  CDF22C        		call	GETGXY
  4A7E  C2C203        		jp	nz,SNERR
  4A81  23            		inc	hl
  4A82  CD140E        		call	INT2ARG		;radius
  4A85  7A            		ld	a,d
  4A86  07            		rlca
  4A87  DACB03        		jp	c,FCERR		;if radius<0
  4A8A  ED53ADFE      		ld	(XRAD),de
                      	
  4A8E  21D700        		ld	hl,00d7h	;default aspect ratio=215/256
  4A91  22E6FF        		ld	(ASPECT),hl
  4A94  CD420C        		call	SETZERO		;default start angle=0
  4A97  CD2539        		call	PUSHF1
  4A9A  211A3E        		ld	hl,PI2		;default end angle=2pi
  4A9D  CD450C        		call	SETF1
  4AA0  CD2539        		call	PUSHF1
                      	
  4AA3  2A4EFF        		ld	hl,(PROGAD)
  4AA6  CD7B3F        		call	CHKCLCM
  4AA9  CA8146        		jp	z,CRCLMAIN
  4AAC  CD6B3F        		call	CHKCMM
  4AAF  280C          		jr	z,CRCLPAR3
  4AB1  CDE40D        		call	INT1ARG		;color
  4AB4  CDC015        		call	SETATT
                      	
  4AB7  CD7B3F        		call	CHKCLCM
  4ABA  CA8146        		jp	z,CRCLMAIN
                      	
  4ABD                	CRCLPAR3:
  4ABD  CD6B3F        		call	CHKCMM
  4AC0  2818          		jr	z,CRCLPAR4
  4AC2  CD980B        		call	NUMARG		;start angle
                      	
  4AC5  CD4339        		call	POPF2		;default end angle
  4AC8  F1            		pop	af		;cancel default start angle
  4AC9  F1            		pop	af
  4ACA  F1            		pop	af
  4ACB  CD2539        		call	PUSHF1		;new start angle
  4ACE  CD1A3C        		call	PUSHF2		;default end angle
                      	
                      	;	ld	hl,FAC1+3
                      	;	res	7,(hl)
                      	;	ld	hl,PI2
                      	;	call	SETCMPF
                      	;	jp	nc,FCERR	;if angle>=|2pi| error
                      	
  4AD1  2A4EFF        		ld	hl,(PROGAD)
  4AD4  CD7B3F        		call	CHKCLCM
  4AD7  CA8146        		jp	z,CRCLMAIN
                      	
  4ADA                	CRCLPAR4:
  4ADA  CD6B3F        		call	CHKCMM
  4ADD  2812          		jr	z,CRCLPAR5
  4ADF  CD980B        		call	NUMARG		;end angle
  4AE2  F1            		pop	af		;cancel default end angle
  4AE3  F1            		pop	af
  4AE4  F1            		pop	af
  4AE5  CD2539        		call	PUSHF1		;new end angle
                      	
                      	;	ld	hl,FAC1+3
                      	;	res	7,(hl)
                      	;	ld	hl,PI2
                      	;	call	SETCMPF
                      	;	jp	nc,FCERR	;if angle>=|2pi| error
                      	
  4AE8  2A4EFF        		ld	hl,(PROGAD)
  4AEB  CD7B3F        		call	CHKCLCM
  4AEE  CA8146        		jp	z,CRCLMAIN
                      	
  4AF1                	CRCLPAR5:
  4AF1  CD980B        		call	NUMARG		;aspect ratio
  4AF4  2A69FF        		ld	hl,(FAC1+3)
  4AF7  7D            		ld	a,l		;(FAC1+3)
  4AF8  07            		rlca
  4AF9  7C            		ld	a,h		;(FAC1+4)
  4AFA  301A          		jr	nc,ASPPOS
                      	
                      	;aspect ratio < 0
  4AFC                	ASPNEG:
  4AFC  FE89          		cp	89h
  4AFE  D2CB03        		jp	nc,FCERR	;aspect ratio <= -256
  4B01  CD5239        		call	CPYFAC
  4B04  CDD00C        		call	GETINT
  4B07  CD9736        		call	SUBF
  4B0A  CD2D0D        		call	NEGABSNZ
  4B0D  CD8C36        		call	INCF1
                      	;	jr	ASPSMALL
                      	
                      	;0 <= aspect ratio < 1
  4B10                	ASPSMALL:
  4B10  CD474B        		call	SETASP
  4B13  C38146        		jp	CRCLMAIN
                      	
                      	;aspect ratio >= 0
  4B16                	ASPPOS:
  4B16  FE81          		cp	81h
  4B18  38F6          		jr	c,ASPSMALL
                      	
                      	;aspect ratio >= 1
  4B1A                	ASPLARGE:
  4B1A  CD5239        		call	CPYFAC
  4B1D  2AADFE        		ld	hl,(XRAD)
  4B20  22AFFE        		ld	(YRAD),hl
                      	
  4B23  7C            		ld	a,h
  4B24  B5            		or	l
  4B25  CA9146        		jp	z,CRCLMAIN2
                      	
  4B28  CD800C        		call	I2TOF
  4B2B  CD2539        		call	PUSHF1		;y-radius(=r)
  4B2E  CD6738        		call	DIVF		;r/asp
  4B31  CD9F0C        		call	FTOI
  4B34  EB            		ex	de,hl
  4B35  22ADFE        		ld	(XRAD),hl	;xrad=r/asp
  4B38  CD800C        		call	I2TOF
  4B3B  CD4339        		call	POPF2		;y-radius
  4B3E  CD6738        		call	DIVF		;1/asp
  4B41  CD474B        		call	SETASP		;replace aspect ratio by 256/asp
  4B44  C39146        		jp	CRCLMAIN2
                      	
                      	
                      	;set aspect ratio
                      	;input: FAC1=aspect ratio
                      	;output: de=aspect ratio*256 (round)
  4B47                	SETASP:
  4B47  216AFF        		ld	hl,FAC1+4
  4B4A  7E            		ld	a,(hl)
  4B4B  C609          		add	a,09h		;*512
  4B4D  77            		ld	(hl),a
  4B4E  CD9F0C        		call	FTOI
  4B51  CB3A          		srl	d		;/2
  4B53  CB1B          		rr	e
  4B55  3001          		jr	nc,SETASPNC
  4B57  13            		inc	de
  4B58                	SETASPNC:
  4B58  ED53E6FF      		ld	(ASPECT),de
  4B5C  C9            		ret
                      	
                      	
                      	;input: FAC1=angle
                      	;output: hl=x=r*cos (signed), de=y=r*sin (signed), b=quadrant(3,2,1,0)
                      	;destroy: af,c,af',bc',de',hl'
  4B5D                	ANGL2XY:
  4B5D  2AADFE        		ld	hl,(XRAD)
  4B60  ED5BAFFE      		ld	de,(YRAD)
  4B64  B7            		or	a
  4B65  ED52          		sbc	hl,de
  4B67  F5            		push	af		;YRAD>XRAD?
  4B68  3802          		jr	c,ANGL2XYC1
  4B6A  19            		add	hl,de
  4B6B  EB            		ex	de,hl
  4B6C                	ANGL2XYC1:
  4B6C  D5            		push	de		;radius
  4B6D  CD9B4B        		call	CORDIC		;hl=cos, de=sin
  4B70  E3            		ex	(sp),hl		;push cos, pop radius
  4B71  F1            		pop	af		;cos
  4B72  C5            		push	bc		;b=quadrant(4,3,2,1)
  4B73  F5            		push	af		;cos
                      	
  4B74  E5            		push	hl		;radius
  4B75  CD6A4C        		call	MULRCS		;de=r*sin
  4B78  E1            		pop	hl		;radius
  4B79  EB            		ex	de,hl
  4B7A  E3            		ex	(sp),hl		;push r*sin, pop cos
  4B7B  CD6A4C        		call	MULRCS		;de=r*cos
  4B7E  E1            		pop	hl		;r*sin
                      	
  4B7F  C1            		pop	bc		;b=quadrant(4,3,2,1)
  4B80  F1            		pop	af		;YRAD>XRAD?
  4B81  C5            		push	bc		;b=quadrant(4,3,2,1)
  4B82  380C          		jr	c,ANGL2XYC2
                      	
                      	;aspect ratio<=1
  4B84  D5            		push	de		;r*cos
  4B85  ED5BE6FF      		ld	de,(ASPECT)
  4B89  CD754C        		call	MULINT2SR	;hl=r*sin*aspect ratio
  4B8C  EB            		ex	de,hl		;de=r*sin*aspect ratio
  4B8D  E1            		pop	hl		;r*cos
  4B8E  1808          		jr	ANGL2XYEND
                      	
                      	;aspect ratio>1
  4B90                	ANGL2XYC2:
  4B90  E5            		push	hl		;r*sin
  4B91  2AE6FF        		ld	hl,(ASPECT)
  4B94  CD754C        		call	MULINT2SR	;hl=r*cos*(1/aspect ratio)
  4B97  D1            		pop	de		;r*sin
                      	
  4B98                	ANGL2XYEND:
  4B98  C1            		pop	bc		;b=quadrant(4,3,2,1)
  4B99  05            		dec	b		;3,2,1,0
  4B9A  C9            		ret
                      	
                      	
                      	;CORDIC correction factor
                      	;9949/16384=0.6073=cos(atan(1))*cos(atan(1/2))*cos(atan(1/4))*...
  26DD                	CORR	equ	9949
                      	
                      	;CORDIC algorithm
                      	;input: FAC1=angle
                      	;output: hl=cos, de=sin (2bit.14bit), b=quardrant(4,3,2,1)
                      	;destroy: af,c,af',bc',de',hl'
  4B9B                	CORDIC:
  4B9B  CD1F39        		call	ABS
  4B9E  0604          		ld	b,04h
  4BA0                	CRDCLP1:
  4BA0  D9            		exx
  4BA1  218B3D        		ld	hl,PIDIV2
  4BA4  CD183F        		call	SETCMPF
  4BA7  3816          		jr	c,CRDCC1
  4BA9  CD9736        		call	SUBF
  4BAC  D9            		exx
  4BAD  10F1          		djnz	CRDCLP1
                      	
  4BAF  3A6AFF        		ld	a,(FAC1+4)
  4BB2  FE66          		cp	66h
  4BB4  D2CB03        		jp	nc,FCERR
  4BB7  04            		inc	b		;4th quadrant
  4BB8  D9            		exx
  4BB9  218B3D        		ld	hl,PIDIV2
  4BBC  CD450C        		call	SETF1
                      	
  4BBF                	CRDCC1:
  4BBF  D9            		exx
  4BC0  21DD26        		ld	hl,CORR		;initial cos (2bit.14bit)
  4BC3  54            		ld	d,h		;initial sin (2bit.14bit)
  4BC4  5D            		ld	e,l
                      	
  4BC5  C5            		push	bc		;b=quadrant(4,3,2,1)
  4BC6  78            		ld	a,b
  4BC7  FE03          		cp	03h
  4BC9  2808          		jr	z,QUAD2		;2nd quadrant
  4BCB  3009          		jr	nc,QUAD1	;1st quadrant
  4BCD  1123D9        		ld	de,0-CORR
  4BD0  3D            		dec	a
  4BD1  2803          		jr	z,QUAD4		;4th quadrant
                      	;	jr	nz,QUAD3	;3rd quadrant
                      	
  4BD3                	QUAD3:
  4BD3                	QUAD2:
  4BD3  2123D9        		ld	hl,0-CORR
  4BD6                	QUAD4:
  4BD6                	QUAD1:
  4BD6  D9            		exx
                      	
                      	
  4BD7  216AFF        		ld	hl,FAC1+4
  4BDA  7E            		ld	a,(hl)
  4BDB  B7            		or	a
  4BDC  2803          		jr	z,CRDCZ1
  4BDE  C60F          		add	a,0fh		;*32768
  4BE0  77            		ld	(hl),a
                      	
  4BE1                	CRDCZ1:
  4BE1  CD9F0C        		call	FTOI
  4BE4  214A4C        		ld	hl,ATAN
  4BE7  D9            		exx
                      	
  4BE8  AF            		xor	a
  4BE9  08            		ex	af,af'		;sign of residual angle
                      	
  4BEA  010010        		ld	bc,1000h
  4BED                	CRDCLP2:
                      	
                      	;residual angle (1bit.15bit)
  4BED  D9            		exx
  4BEE  4E            		ld	c,(hl)
  4BEF  23            		inc	hl
  4BF0  46            		ld	b,(hl)
  4BF1  23            		inc	hl
  4BF2  EB            		ex	de,hl
  4BF3  08            		ex	af,af'		;sign of residual angle
  4BF4  2004          		jr	nz,CRDCNZ
  4BF6  B7            		or	a
  4BF7  ED42          		sbc	hl,bc
  4BF9  3E            		db	3eh		;ld a,
  4BFA                	CRDCNZ:
  4BFA  09            		add	hl,bc
  4BFB  CB7C          		bit	7,h
  4BFD  08            		ex	af,af'		;sign of residual angle
  4BFE  EB            		ex	de,hl
  4BFF  D9            		exx
                      	
  4C00  0C            		inc	c
  4C01  C5            		push	bc
  4C02  D5            		push	de		;sin
  4C03  E5            		push	hl		;cos
  4C04  41            		ld	b,c
  4C05                	CRDCLP3:
  4C05  CB2C          		sra	h
  4C07  CB1D          		rr	l
  4C09  1F            		rra
  4C0A  CB2A          		sra	d
  4C0C  CB1B          		rr	e
  4C0E  10F5          		djnz	CRDCLP3
                      	
  4C10  3001          		jr	nc,CRDCNC1
  4C12  13            		inc	de
  4C13                	CRDCNC1:
  4C13  07            		rlca
  4C14  3001          		jr	nc,CRDCNC2
  4C16  23            		inc	hl
  4C17                	CRDCNC2:
                      	
  4C17  E3            		ex	(sp),hl
  4C18  08            		ex	af,af'		;sign of residual angle
  4C19  280A          		jr	z,CRDCZ2
                      	
                      	;residual<0
  4C1B  08            		ex	af,af'		;sign of residual angle
  4C1C  19            		add	hl,de		;cos=cos+(sin>>c)
  4C1D  EB            		ex	de,hl
  4C1E  C1            		pop	bc		;cos>>c
  4C1F  E1            		pop	hl		;sin
  4C20  B7            		or	a
  4C21  ED42          		sbc	hl,bc		;sin=sin-(cos>>c)
  4C23  1808          		jr	CRDCLP2END
                      	
  4C25                	CRDCZ2:
                      	;residual>0
  4C25  08            		ex	af,af'		;sign of residual angle
  4C26  B7            		or	a
  4C27  ED52          		sbc	hl,de		;cos=cos-(sin>>c)
  4C29  EB            		ex	de,hl
  4C2A  C1            		pop	bc		;cos>>c
  4C2B  E1            		pop	hl		;sin
  4C2C  09            		add	hl,bc		;sin=sin+(cos>>c)
                      	
  4C2D                	CRDCLP2END:
  4C2D  EB            		ex	de,hl
  4C2E  C1            		pop	bc
  4C2F  10BC          		djnz	CRDCLP2
                      	
                      	;round hl
  4C31  7D            		ld	a,l
  4C32  FE03          		cp	03h
  4C34  3805          		jr	c,CRDCC2
  4C36  FEFE          		cp	0feh
  4C38  3803          		jr	c,CRDCC3
  4C3A  24            		inc	h
  4C3B                	CRDCC2:
  4C3B  2E00          		ld	l,00h
  4C3D                	CRDCC3:
                      	
  4C3D  C1            		pop	bc		;b=quadrant(4,3,2,1)
                      	;round de
  4C3E  7B            		ld	a,e
  4C3F  FE03          		cp	03h
  4C41  3804          		jr	c,CRDCC4
  4C43  FEFE          		cp	0feh
  4C45  D8            		ret	c
  4C46  14            		inc	d
  4C47                	CRDCC4:
  4C47  1E00          		ld	e,00h
  4C49  C9            		ret
                      	
                      	
  4C4A                	ATAN:
  4C4A  8864          		dw	25736	;atan(1)*32768
  4C4C  593B          		dw	15193	;atan(1/2)*32768
  4C4E  5B1F          		dw	8027	;atan(1/4)*32768
  4C50  EB0F          		dw	4075	;atan(1/8)*32768
  4C52  FD07          		dw	2045	;atan(1/16)*32768
  4C54  0004          		dw	1024	;atan(1/32)*32768
  4C56  0002          		dw	512	;atan(1/64)*32768
  4C58  0001          		dw	256	;atan(1/128)*32768
  4C5A  8000          		dw	128	;atan(1/256)*32768
  4C5C  4000          		dw	64	;atan(1/512)*32768
  4C5E  2000          		dw	32	;atan(1/1024)*32768
  4C60  1000          		dw	16	;atan(1/2048)*32768
  4C62  0800          		dw	8	;atan(1/4096)*32768
  4C64  0400          		dw	4	;atan(1/8192)*32768
  4C66  0200          		dw	2	;atan(1/16384)*32768
  4C68  0100          		dw	1	;atan(1/32768)*32768
                      	
                      	
                      	;radius * (cos or sin)
                      	;de=(de*hl)>>18 (round)
                      	;input: de,hl
                      	;output: de
                      	;destroy: af,bc,hl
  4C6A                	MULRCS:
  4C6A  CD7F4C        		call	MULINT2S	;dehl=radius*(cos or sin)
  4C6D  CD984C        		call	DEHL4
  4C70  CB04          		rlc	h
  4C72  D0            		ret	nc
  4C73  13            		inc	de
  4C74  C9            		ret
                      	
                      	
                      	;hl=(de*hl)>>8 (signed, round)
                      	;input: de,hl
                      	;output: hl
                      	;destroy: af,bc,de
  4C75                	MULINT2SR:
  4C75  CD7F4C        		call	MULINT2S
                      	
                      	;hl=(dehl)>>8 (round)
  4C78                	ROUNDI2:
  4C78  CB05          		rlc	l
  4C7A  6C            		ld	l,h
  4C7B  63            		ld	h,e
  4C7C  D0            		ret	nc
  4C7D  23            		inc	hl
  4C7E  C9            		ret
                      	
                      	
                      	;dehl=de*hl (signed)
                      	;input: de,hl
                      	;output: dehl
                      	;destroy: af,bc
  4C7F                	MULINT2S:
  4C7F  E5            		push	hl
  4C80  D5            		push	de
  4C81  CDF921        		call	MULINT2
  4C84  C1            		pop	bc
  4C85  E3            		ex	(sp),hl
  4C86  EB            		ex	de,hl
  4C87  CB78          		bit	7,b
  4C89  2803          		jr	z,MULI2SNC1
  4C8B  B7            		or	a
  4C8C  ED52          		sbc	hl,de
  4C8E                	MULI2SNC1:
  4C8E  CB7A          		bit	7,d
  4C90  2803          		jr	z,MULI2SNC2
  4C92  B7            		or	a
  4C93  ED42          		sbc	hl,bc
  4C95                	MULI2SNC2:
  4C95  EB            		ex	de,hl
  4C96  E1            		pop	hl
  4C97  C9            		ret
                      	
                      	
                      	;dehl=dehl*4
                      	;input: dehl
                      	;output: dehl
                      	;destroy: f
  4C98                	DEHL4:
  4C98  29            		add	hl,hl
  4C99  CB13          		rl	e
  4C9B  CB12          		rl	d
  4C9D  29            		add	hl,hl
  4C9E  CB13          		rl	e
  4CA0  CB12          		rl	d
  4CA2  C9            		ret
                      	
                      	
                      	;PUT command
  4CA3                	C_PUT:
  4CA3  CDDF4E        		call	GETPUTSUB1
  4CA6  285B          		jr	z,PUTAT
                      	
                      	;FDD command
  4CA8  CDE84E        		call	GETPUTSUB2
  4CAB  F5            		push	af		;z-flag
  4CAC  2A39FB        		ld	hl,(BUFP)
  4CAF  010600        		ld	bc,0006h
  4CB2  09            		add	hl,bc
  4CB3  46            		ld	b,(hl)		;+6: position
  4CB4  F1            		pop	af		;z-flag
  4CB5  C5            		push	bc		;b=position
  4CB6  E5            		push	hl		;(BUFP)+6
  4CB7  202F          		jr	nz,PUTNZ	;
                      	
  4CB9  7A            		ld	a,d
  4CBA  B3            		or	e
  4CBB  CA5142        		jp	z,DFERR
                      	
  4CBE  01FBFF        		ld	bc,0-0005h
  4CC1  09            		add	hl,bc
  4CC2  7E            		ld	a,(hl)		;+1: first cluster
  4CC3  CDD85C        		call	C2TRSECT
                      	
  4CC6                	PUTLP1:
  4CC6  1B            		dec	de
  4CC7  7A            		ld	a,d
  4CC8  B3            		or	e
  4CC9  2828          		jr	z,PUTNC
  4CCB  60            		ld	h,b
  4CCC  69            		ld	l,c
  4CCD  CD4953        		call	NEXTSECT
  4CD0  30F4          		jr	nc,PUTLP1
                      	
                      	;b=track, c=sector
  4CD2  44            		ld	b,h
  4CD3  4D            		ld	c,l
  4CD4  79            		ld	a,c
  4CD5  C6F7          		add	a,0f7h
  4CD7  CB10          		rl	b		;cluster
  4CD9  0D            		dec	c		;0-15
  4CDA  CB99          		res	3,c		;0-7
  4CDC  0C            		inc	c		;sector in cluster(1-8)
                      	
  4CDD                	PUTLP2:
  4CDD  78            		ld	a,b
  4CDE  CDCF5B        		call	NEXTSECTW4
  4CE1  1B            		dec	de
  4CE2  7A            		ld	a,d
  4CE3  B3            		or	e
  4CE4  2805          		jr	z,PUTEND
  4CE6  18F5          		jr	PUTLP2
                      	
  4CE8                	PUTNZ:
  4CE8  CDC65B        		call	NEXTSECTW3
  4CEB                	PUTEND:
  4CEB  CB38          		srl	b		;track=cluster/2
  4CED  3004          		jr	nc,PUTNC
  4CEF  79            		ld	a,c
  4CF0  C608          		add	a,08h		;odd cluster: 9-16
  4CF2  4F            		ld	c,a		;sector
  4CF3                	PUTNC:
  4CF3  CDC35C        		call	WRTBUF
  4CF6  E1            		pop	hl		;(BUFP)+6
  4CF7  F1            		pop	af
  4CF8  77            		ld	(hl),a		;+6: position
  4CF9  23            		inc	hl
  4CFA  CB5E          		bit	3,(hl)		;+7: file status, bit3=write
  4CFC  2802          		jr	z,PUTZ
  4CFE  3604          		ld	(hl),04h	;random file
  4D00                	PUTZ:
  4D00  C3EF56        		jp	CLRBUFP
                      	
                      	
                      	;PUT@ command
  4D03                	PUTAT:
  4D03  CDF22C        		call	GETGXY
  4D06  C2C203        		jp	nz,SNERR
  4D09  23            		inc	hl
  4D0A  E5            		push	hl
  4D0B  CD4717        		call	ARG
  4D0E  E1            		pop	hl
  4D0F  3A25FF        		ld	a,(ARGTYP)
  4D12  B7            		or	a
  4D13  2827          		jr	z,PUTATN1
                      	
                      	;string (disk file)
  4D15  08            		ex	af,af'		;z-flag(0=string, 1=numeric)
  4D16  CD5E5F        		call	SETFNAME
  4D19  47            		ld	b,a		;drive-1
  4D1A  CDC853        		call	READFAT
  4D1D  CD3A54        		call	SRCHFILE
  4D20  C24242        		jp	nz,FFERR
  4D23  CB40          		bit	0,b
  4D25  CACB03        		jp	z,FCERR		;if not binary file
  4D28  1601          		ld	d,01h		;file open mode=input
  4D2A  CD0056        		call	FOPEN0
  4D2D  CD7B5B        		call	GETDSK2
  4D30  44            		ld	b,h		;bc=x dot size
  4D31  4D            		ld	c,l
  4D32  CD7B5B        		call	GETDSK2
  4D35  54            		ld	d,h		;de=y dot size
  4D36  5D            		ld	e,l
  4D37  2150FF        		ld	hl,TMP
  4D3A  1818          		jr	PUTATSN1
                      	
                      	;numeric array
  4D3C                	PUTATN1:
  4D3C  08            		ex	af,af'		;z-flag(0=string, 1=numeric)
  4D3D  CD8F33        		call	CHKVAR
  4D40  224EFF        		ld	(PROGAD),hl
  4D43  CD3D34        		call	SRCHARR
  4D46  DACB03        		jp	c,FCERR
  4D49  CD3F50        		call	GETARR0
  4D4C  23            		inc	hl
  4D4D  4E            		ld	c,(hl)
  4D4E  23            		inc	hl
  4D4F  46            		ld	b,(hl)		;bc=x dot size
  4D50  23            		inc	hl
  4D51  5E            		ld	e,(hl)
  4D52  23            		inc	hl
  4D53  56            		ld	d,(hl)		;de=y dot size
                      	
  4D54                	PUTATSN1:
  4D54  E5            		push	hl		;array address
                      	
                      	;check x,y dot sizes
  4D55  D5            		push	de
  4D56  50            		ld	d,b
  4D57  59            		ld	e,c
  4D58  1B            		dec	de
  4D59  21FF00        		ld	hl,256-1
  4D5C  CD9539        		call	CHKMOD
  4D5F  2003          		jr	nz,PUTATNZ1	;if mode1-4
  4D61  213F01        		ld	hl,320-1	;mode5
  4D64                	PUTATNZ1:
  4D64  E7            		rst	CPHLDE
  4D65  DACB03        		jp	c,FCERR
  4D68  D1            		pop	de
                      	
  4D69  1B            		dec	de
  4D6A  21BF00        		ld	hl,192-1
  4D6D  CD9539        		call	CHKMOD
  4D70  2002          		jr	nz,PUTATNZ2	;if mode1-4
  4D72  2EC7          		ld	l,200-1		;h=0, mode5
  4D74                	PUTATNZ2:
  4D74  E7            		rst	CPHLDE
  4D75  DACB03        		jp	c,FCERR
  4D78  13            		inc	de
                      	
                      	;or,and,xor,pset,preset
  4D79  2A4EFF        		ld	hl,(PROGAD)
  4D7C  CD593F        		call	SKIPSP
  4D7F  FE2C          		cp	','
  4D81  2011          		jr	nz,PUTATNZ3
  4D83  CD583F        		call	SKIPSPINC
  4D86  23            		inc	hl
  4D87  FE58          		cp	'X'
  4D89  200B          		jr	nz,PUTATNZ4
  4D8B  CD593F        		call	SKIPSP
  4D8E  23            		inc	hl
  4D8F  FED0          		cp	OR_
  4D91  C2C203        		jp	nz,SNERR
  4D94                	PUTATNZ3:
  4D94  3E58          		ld	a,'X'
  4D96                	PUTATNZ4:
  4D96  224EFF        		ld	(PROGAD),hl
                      	
  4D99  215C4E        		ld	hl,PUTATSET
  4D9C  FE9B          		cp	PSET_		;9bh
  4D9E  2818          		jr	z,PUTATIX
  4DA0  215B4E        		ld	hl,PUTATXOR
  4DA3  FE58          		cp	'X'
  4DA5  2811          		jr	z,PUTATIX
  4DA7  21554E        		ld	hl,PUTATAND
  4DAA  FECF          		cp	AND_		;cfh
  4DAC  280A          		jr	z,PUTATIX
  4DAE  21584E        		ld	hl,PUTATOR
  4DB1  FED0          		cp	OR_		;d0h
  4DB3  2803          		jr	z,PUTATIX
  4DB5  21524E        		ld	hl,PUTATPRESET
  4DB8                	PUTATIX:
  4DB8  CDE15C        		call	SETIX
  4DBB  DD7527        		ld	(ix+27h),l
  4DBE  DD7428        		ld	(ix+28h),h
                      	
  4DC1  D9            		exx
  4DC2  21874E        		ld	hl,PUTTBL-2
  4DC5  CD9539        		call	CHKMOD
  4DC8  3A92FD        		ld	a,(SCREEN1)
  4DCB  2802          		jr	z,PUTATZ1
  4DCD  23            		inc	hl
  4DCE  23            		inc	hl
  4DCF                	PUTATZ1:
  4DCF  B7            		or	a
  4DD0  CACB03        		jp	z,FCERR
  4DD3  4F            		ld	c,a		;(SCREEN1)
  4DD4  87            		add	a,a
  4DD5  1600          		ld	d,00h
  4DD7  5F            		ld	e,a
  4DD8  42            		ld	b,d		;=0
  4DD9  19            		add	hl,de
  4DDA  56            		ld	d,(hl)		;color bit size
  4DDB  23            		inc	hl
  4DDC  5E            		ld	e,(hl)		;mask
  4DDD  E1            		pop	hl		;array address
                      	
                      	;	ld	b,00h
  4DDE  CD9539        		call	CHKMOD
  4DE1  2804          		jr	z,PUTATZ2	;mode5: 1-origin
  4DE3  79            		ld	a,c		;(SCREEN1)
  4DE4  0F            		rrca
  4DE5  3801          		jr	c,PUTATC	;mode1-4,screen2,4: 0-origin
  4DE7                	PUTATZ2:
  4DE7  04            		inc	b		;0=0-origin, 1=1-origin
  4DE8                	PUTATC:
  4DE8  D9            		exx
                      	
  4DE9  C5            		push	bc
  4DEA  D5            		push	de
  4DEB  ED4BAEFD      		ld	bc,(GRPX1)
  4DEF  ED5BB0FD      		ld	de,(GRPY1)
  4DF3  37            		scf
  4DF4  CDC52B        		call	CHKGXY23
  4DF7  D1            		pop	de
  4DF8  C1            		pop	bc
                      	
                      	;	ld	hl,(GRPY1)
                      	;	ld	(GRPY3),hl
  4DF9  19            		add	hl,de		;de=y dot size-1
  4DFA  EB            		ex	de,hl
  4DFB  2AAEFD        		ld	hl,(GRPX1)
  4DFE  09            		add	hl,bc		;bc=x dot size
  4DFF  44            		ld	b,h
  4E00  4D            		ld	c,l
  4E01  D5            		push	de
  4E02  CD7815        		call	GXY2SXY
  4E05  D1            		pop	de
                      	
                      	
  4E06                	PUTATLP1:
  4E06  D9            		exx
  4E07  0E00          		ld	c,00h		;shift count
  4E09  D9            		exx
                      	
  4E0A  D5            		push	de		;y-end
                      	;	ld	hl,(GRPX1)	;x-start
  4E0B  2AADFE        		ld	hl,(GRPX2)
  4E0E  22B1FE        		ld	(GRPX3),hl
                      	
  4E11                	PUTATLP2:
  4E11  C5            		push	bc		;x-end
                      	
  4E12  ED4BB1FE      		ld	bc,(GRPX3)
  4E16  ED5BB3FE      		ld	de,(GRPY3)
  4E1A  C5            		push	bc		;x
  4E1B  D5            		push	de		;y
  4E1C  CDBA15        		call	SXY2AD
  4E1F  CDD515        		call	GETCOLR
                      	
  4E22  D9            		exx
  4E23  90            		sub	b		;0- or 1-origin
  4E24  D9            		exx
  4E25  47            		ld	b,a		;;
                      	
  4E26  D9            		exx
  4E27  79            		ld	a,c
  4E28  92            		sub	d
  4E29  4F            		ld	c,a
  4E2A  7E            		ld	a,(hl)
  4E2B  281C          		jr	z,PUTATZ3
  4E2D  3014          		jr	nc,PUTATNC
  4E2F  23            		inc	hl
  4E30  79            		ld	a,c
  4E31  E607          		and	07h
  4E33  4F            		ld	c,a
                      	
  4E34  08            		ex	af,af'		;z-flag(0=string, 1=numeric)
  4E35  280A          		jr	z,PUTATN2
                      	
                      	;string (disk file)
  4E37  08            		ex	af,af'		;z-flag(0=string, 1=numeric)
  4E38  D5            		push	de
  4E39  CD435B        		call	GETDSK
  4E3C  D1            		pop	de
  4E3D  2B            		dec	hl
  4E3E  77            		ld	(hl),a
  4E3F  1802          		jr	PUTATNC
                      	
                      	;numerica array
  4E41                	PUTATN2:
  4E41  08            		ex	af,af'		;z-flag(0=string, 1=numeric)
  4E42  7E            		ld	a,(hl)
                      	
  4E43                	PUTATNC:
  4E43  C5            		push	bc
  4E44  41            		ld	b,c
  4E45                	PUTATLP3:
  4E45  0F            		rrca
  4E46  10FD          		djnz	PUTATLP3
  4E48  C1            		pop	bc
  4E49                	PUTATZ3:
  4E49  A3            		and	e
  4E4A  D9            		exx
                      	
  4E4B  DD6E27        		ld	l,(ix+27h)
  4E4E  DD6628        		ld	h,(ix+28h)
  4E51  E9            		jp	(hl)
                      	
  4E52                	PUTATPRESET:
  4E52  2F            		cpl
  4E53  1807          		jr	PUTATSET
                      	
  4E55                	PUTATAND:
  4E55  A0            		and	b		;;
  4E56  1804          		jr	PUTATSET
                      	
  4E58                	PUTATOR:
  4E58  B0            		or	b		;;
  4E59  1801          		jr	PUTATSET
                      	
  4E5B                	PUTATXOR:
  4E5B  A8            		xor	b		;;
                      	;	jr	PUTATSET
                      	
  4E5C                	PUTATSET:
  4E5C  D9            		exx
  4E5D  80            		add	a,b		;0- or 1-origin
  4E5E  D9            		exx
  4E5F  CDC015        		call	SETATT
  4E62  D1            		pop	de		;y
  4E63  C1            		pop	bc		;x
  4E64  CD4A2D        		call	PSETSXY
                      	
  4E67  2AB1FE        		ld	hl,(GRPX3)
  4E6A  23            		inc	hl
  4E6B  22B1FE        		ld	(GRPX3),hl
                      	
  4E6E  C1            		pop	bc		;x-end
  4E6F  B7            		or	a
  4E70  ED42          		sbc	hl,bc
  4E72  389D          		jr	c,PUTATLP2
                      	
  4E74  CD5D2B        		call	INCGY
  4E77  EB            		ex	de,hl
  4E78  D1            		pop	de		;y-end
  4E79  B7            		or	a
  4E7A  ED52          		sbc	hl,de
  4E7C  DA064E        		jp	c,PUTATLP1
                      	
  4E7F  08            		ex	af,af'		;z-flag(0=string, 1=numeric)
  4E80  C8            		ret	z
                      	
                      	;string (disk file)
  4E81                	PUTATEND:
  4E81  1E00          		ld	e,00h
  4E83  CD0C56        		call	FCLOSE
  4E86  C3EF56        		jp	CLRBUFP
                      	
                      	
                      	;color bit size and mask
  4E89                	PUTTBL:
  4E89  040F          		db	04h, 0fh	;mode5,screen2
  4E8B  040F          		db	04h, 0fh	;mode5,screen3	mode1-4,screen2
  4E8D  0203          		db	02h, 03h	;mode5,screen4	mode1-4,screen3
  4E8F  0101          		db	01h, 01h	;		mode1-4,screen4
                      	
                      	
                      	;GET command
  4E91                	C_GET:
  4E91  CDDF4E        		call	GETPUTSUB1
  4E94  2871          		jr	z,GETAT
                      	
                      	;FDD command
  4E96  CDE84E        		call	GETPUTSUB2
  4E99  203B          		jr	nz,GETNZ
                      	
  4E9B  1B            		dec	de
  4E9C  7B            		ld	a,e
  4E9D  E607          		and	07h
  4E9F  4F            		ld	c,a
  4EA0  0C            		inc	c		;(record number-1)%8 +1
                      	
  4EA1  7B            		ld	a,e
  4EA2  CB3A          		srl	d
  4EA4  1F            		rra
  4EA5  CB3A          		srl	d
  4EA7  1F            		rra
  4EA8  CB3A          		srl	d
  4EAA  1F            		rra
  4EAB  57            		ld	d,a		;(record number-1)/8
                      	
  4EAC  2A39FB        		ld	hl,(BUFP)
  4EAF  23            		inc	hl
  4EB0  7E            		ld	a,(hl)		;+1: first cluster
  4EB1  14            		inc	d
  4EB2                	GETLP:
  4EB2  47            		ld	b,a
  4EB3  CD1D54        		call	GETFAT
  4EB6  FEC0          		cp	0c0h		;
  4EB8  15            		dec	d
  4EB9  2805          		jr	z,GETZ
  4EBB  D25442        		jp	nc,EFERR	;
  4EBE  18F2          		jr	GETLP
                      	
  4EC0                	GETZ:
  4EC0  3806          		jr	c,GETOK
  4EC2  D6C0          		sub	0c0h
  4EC4  B9            		cp	c
  4EC5  DA5442        		jp	c,EFERR
  4EC8                	GETOK:
  4EC8  CB38          		srl	b		;track=cluster/2
  4ECA  3004          		jr	nc,GETNC	;even cluster: 1-8
  4ECC  79            		ld	a,c
  4ECD  C608          		add	a,08h		;odd cluster: 9-16
  4ECF  4F            		ld	c,a		;sector
  4ED0                	GETNC:
  4ED0  CD005C        		call	RDNEXT2
  4ED3  C3EF56        		jp	CLRBUFP
                      	
  4ED6                	GETNZ:
  4ED6  CDFC5B        		call	RDNEXT
  4ED9  DA5442        		jp	c,EFERR
  4EDC  C3EF56        		jp	CLRBUFP
                      	
                      	
                      	;subroutine for GET and PUT
                      	;input: hl=program address
                      	;output: hl=program address, z-flag(1=get@/put@)
  4EDF                	GETPUTSUB1:
  4EDF  CDC62D        		call	SKIPAT
  4EE2  FEC9          		cp	STEP_
  4EE4  C8            		ret	z
  4EE5  FE28          		cp	'('
  4EE7  C9            		ret
                      	
                      	;subroutine for GET and PUT
                      	;input: hl=program address
                      	;output: hl=program address, de=record number, (BUFP), z-flag(1=with record number)
  4EE8                	GETPUTSUB2:
  4EE8  FE23          		cp	'#'
  4EEA  2001          		jr	nz,GETPUTZ
  4EEC  23            		inc	hl
  4EED                	GETPUTZ:
  4EED  CDE40D        		call	INT1ARG
  4EF0  E5            		push	hl		;program address
  4EF1  CD2552        		call	CHKOPEN
  4EF4  2239FB        		ld	(BUFP),hl
  4EF7  E1            		pop	hl		;program address
  4EF8  7E            		ld	a,(hl)
  4EF9  FE2C          		cp	','
  4EFB  C0            		ret	nz
                      	
  4EFC  23            		inc	hl
  4EFD  CD140E        		call	INT2ARG
  4F00  7A            		ld	a,d
  4F01  B3            		or	e
  4F02  CA5442        		jp	z,EFERR
  4F05  BF            		cp	a		;set z-flag
  4F06  C9            		ret
                      	
                      	
                      	;GET@ command
  4F07                	GETAT:
  4F07  CDF22C        		call	GETGXY
  4F0A  FECB          		cp	MINUS_
  4F0C  C2C203        		jp	nz,SNERR
  4F0F  C5            		push	bc
  4F10  D5            		push	de
  4F11  23            		inc	hl
  4F12  CDF22C        		call	GETGXY
  4F15  C2C203        		jp	nz,SNERR
  4F18  D1            		pop	de
  4F19  C1            		pop	bc
  4F1A  E5            		push	hl
  4F1B  CD732B        		call	SORTXY
  4F1E  E1            		pop	hl
  4F1F  23            		inc	hl
                      	
  4F20  E5            		push	hl
  4F21  CD4717        		call	ARG
  4F24  E1            		pop	hl
  4F25  3A25FF        		ld	a,(ARGTYP)
  4F28  B7            		or	a
  4F29  2818          		jr	z,GETATN1
                      	
                      	;string (disk file)
  4F2B  08            		ex	af,af'		;z-flag(0=string, 1=numeric)
  4F2C  CD5E5F        		call	SETFNAME
  4F2F  2601          		ld	h,01h		;attribute
  4F31  CD395F        		call	SVCOMMON
  4F34  0601          		ld	b,01h		;attribute=binary
  4F36  1602          		ld	d,02h		;file open mode=output
  4F38  CD0056        		call	FOPEN0
  4F3B  1150FF        		ld	de,TMP
  4F3E  01FFFF        		ld	bc,0ffffh
  4F41  1814          		jr	GETATSN1
                      	
                      	;numeric array
  4F43                	GETATN1:
  4F43  08            		ex	af,af'		;z-flag(0=string, 1=numeric)
  4F44  CD8F33        		call	CHKVAR
  4F47  224EFF        		ld	(PROGAD),hl
  4F4A  CD3D34        		call	SRCHARR
  4F4D  DACB03        		jp	c,FCERR
  4F50  CD3F50        		call	GETARR0
  4F53  EB            		ex	de,hl		;
  4F54  0B            		dec	bc
  4F55  0B            		dec	bc
  4F56  0B            		dec	bc
                      	
  4F57                	GETATSN1:
  4F57  C5            		push	bc		;array data size
                      	
  4F58  2AADFE        		ld	hl,(GRPX2)
  4F5B  ED4BB1FE      		ld	bc,(GRPX3)
                      	
  4F5F  3A92FD        		ld	a,(SCREEN1)
  4F62  B7            		or	a
  4F63  CACB03        		jp	z,FCERR		;screen mode 1
                      	;	or	a
  4F66  ED42          		sbc	hl,bc
  4F68  FE02          		cp	02h
  4F6A  2F            		cpl
  4F6B  CE05          		adc	a,05h		;1,2,3->4,2,1
  4F6D  47            		ld	b,a		;x dot size
                      	
  4F6E  85            		add	a,l
  4F6F  6F            		ld	l,a
  4F70  3001          		jr	nc,GETATNC1
  4F72  24            		inc	h
  4F73                	GETATNC1:
                      	
  4F73  CD9539        		call	CHKMOD
  4F76  78            		ld	a,b		;x dot size
  4F77  2005          		jr	nz,GETATNZ1
                      	;mode5
  4F79  FE04          		cp	04h
  4F7B  3001          		jr	nc,GETATNZ1
  4F7D  87            		add	a,a		;2,1->4,2
                      	
  4F7E                	GETATNZ1:
  4F7E  F5            		push	af		;color bit size: (4,2,1/4,4,2)
                      	
  4F7F  08            		ex	af,af'		;z-flag(0=string, 1=numeric)
  4F80  280B          		jr	z,GETATN2
                      	
                      	;string (disk file)
  4F82  08            		ex	af,af'		;z-flag(0=string, 1=numeric)
  4F83  7D            		ld	a,l
  4F84  CDC726        		call	PUTDEV
  4F87  7C            		ld	a,h
  4F88  CDC726        		call	PUTDEV
  4F8B  1807          		jr	GETATSN2
                      	
                      	;numeric array
  4F8D                	GETATN2:
  4F8D  08            		ex	af,af'		;z-flag(0=string, 1=numeric)
  4F8E  EB            		ex	de,hl		;
  4F8F  23            		inc	hl
  4F90  73            		ld	(hl),e
  4F91  23            		inc	hl
  4F92  72            		ld	(hl),d
  4F93  EB            		ex	de,hl		;
                      	
  4F94                	GETATSN2:
  4F94  D5            		push	de		;array address
  4F95  ED4BB1FE      		ld	bc,(GRPX3)
  4F99  ED5BB3FE      		ld	de,(GRPY3)
  4F9D  37            		scf
  4F9E  CDC52B        		call	CHKGXY23	;graphic cordinate
  4FA1  B7            		or	a
  4FA2  EB            		ex	de,hl
  4FA3  ED52          		sbc	hl,de		;(GRPY2)-(GRPY3)
  4FA5  23            		inc	hl
  4FA6  D1            		pop	de		;array address
  4FA7  EB            		ex	de,hl
  4FA8  ED4BB1FE      		ld	bc,(GRPX3)
  4FAC  F1            		pop	af		;color bit size
  4FAD  C5            		push	bc		;smaller x
  4FAE  F5            		push	af		;color bit size
                      	
  4FAF  08            		ex	af,af'		;z-flag(0=string, 1=numeric)
  4FB0  2807          		jr	z,GETATN3
                      	
                      	;string (disk file)
  4FB2  08            		ex	af,af'		;z-flag(0=string, 1=numeric)
  4FB3  7B            		ld	a,e
  4FB4  CDC726        		call	PUTDEV
  4FB7  1804          		jr	GETATSN3
                      	
                      	;numeric array
  4FB9                	GETATN3:
  4FB9  08            		ex	af,af'		;z-flag(0=string, 1=numeric)
  4FBA  23            		inc	hl
  4FBB  73            		ld	(hl),e
  4FBC  23            		inc	hl
                      	
  4FBD                	GETATSN3:
  4FBD  72            		ld	(hl),d
  4FBE  F1            		pop	af		;color bit size
  4FBF  5F            		ld	e,a
  4FC0  1600          		ld	d,00h		;x counter initial value
                      	
  4FC2  E5            		push	hl		;array address
  4FC3  D5            		push	de		;d=x counter, e=color bit size
                      	
  4FC4                	GETATLP1:
  4FC4  ED4BB1FE      		ld	bc,(GRPX3)
  4FC8  ED5BB3FE      		ld	de,(GRPY3)
  4FCC  CDBA15        		call	SXY2AD
  4FCF  CDD515        		call	GETCOLR
  4FD2  4F            		ld	c,a
  4FD3  CD9539        		call	CHKMOD
  4FD6  2808          		jr	z,GETATZ1
  4FD8  3A92FD        		ld	a,(SCREEN1)
  4FDB  FE02          		cp	02h
  4FDD  2801          		jr	z,GETATZ1
  4FDF  0C            		inc	c
  4FE0                	GETATZ1:
  4FE0  0D            		dec	c
  4FE1  D1            		pop	de		;d=x counter, e=color bit size
  4FE2  E1            		pop	hl		;array address
  4FE3  7A            		ld	a,d
  4FE4  93            		sub	e		;counter -= color bit size
  4FE5  57            		ld	d,a
  4FE6  301D          		jr	nc,GETATNC2
                      	
  4FE8  C608          		add	a,08h
  4FEA  57            		ld	d,a
                      	
  4FEB  D9            		exx
  4FEC  E1            		pop	hl		;smaller x
  4FED  C1            		pop	bc		;array data size
  4FEE  0B            		dec	bc
  4FEF  78            		ld	a,b
  4FF0  B1            		or	c
  4FF1  CACB03        		jp	z,FCERR
  4FF4  C5            		push	bc		;array data size
  4FF5  E5            		push	hl		;smaller x
  4FF6  D9            		exx
                      	
  4FF7  08            		ex	af,af'		;z-flag(0=string, 1=numeric)
  4FF8  2807          		jr	z,GETATN4
                      	
                      	;string (disk file)
  4FFA  08            		ex	af,af'		;z-flag(0=string, 1=numeric)
  4FFB  7E            		ld	a,(hl)
  4FFC  CDC726        		call	PUTDEV
  4FFF  1802          		jr	GETATSN4
                      	
                      	;numeric array
  5001                	GETATN4:
  5001  08            		ex	af,af'		;z-flag(0=string, 1=numeric)
  5002  23            		inc	hl
                      	
  5003                	GETATSN4:
  5003  3600          		ld	(hl),00h
                      	
  5005                	GETATNC2:
  5005  79            		ld	a,c		;data
  5006  2804          		jr	z,GETATZ3	;counter - color bit size
  5008  42            		ld	b,d
  5009                	GETATLP2:
  5009  07            		rlca
  500A  10FD          		djnz	GETATLP2
  500C                	GETATZ3:
  500C  B6            		or	(hl)
  500D  77            		ld	(hl),a
  500E  C1            		pop	bc		;smaller x
  500F  C5            		push	bc
  5010  E5            		push	hl
  5011  D5            		push	de
                      	
  5012  2AB1FE        		ld	hl,(GRPX3)
  5015  ED5BADFE      		ld	de,(GRPX2)
  5019  E7            		rst	CPHLDE
  501A  3005          		jr	nc,GETATNC3
  501C  CD4A2B        		call	INCGX
  501F  18A3          		jr	GETATLP1
                      	
  5021                	GETATNC3:
  5021  D1            		pop	de
  5022  1600          		ld	d,00h		;x counter initial value
  5024  D5            		push	de
  5025  ED43B1FE      		ld	(GRPX3),bc	;smaller x
  5029  CD5D2B        		call	INCGY
  502C  2AAFFE        		ld	hl,(GRPY2)
                      	;	ld	de,(GRPY3)
  502F  E7            		rst	CPHLDE
  5030  3092          		jr	nc,GETATLP1
                      	
  5032                	GETATEND:
  5032  D1            		pop	de
  5033  E1            		pop	hl
  5034  C1            		pop	bc
  5035  C1            		pop	bc
  5036  08            		ex	af,af'		;z-flag(0=string, 1=numeric)
  5037  C8            		ret	z
                      	
                      	;string (disk file)
  5038  7E            		ld	a,(hl)
  5039  CDC726        		call	PUTDEV
  503C  C3814E        		jp	PUTATEND
                      	
                      	
                      	;get array(0) address and last size
                      	;input: de=array address
                      	;output: bc=size, hl=array(0) address-1
                      	;destroy: af,de
  503F                	GETARR0:
  503F  EB            		ex	de,hl
  5040  4E            		ld	c,(hl)		;bc=bytes
  5041  23            		inc	hl
  5042  46            		ld	b,(hl)
  5043  23            		inc	hl
  5044  7E            		ld	a,(hl)		;dimensions
                      	
  5045  0B            		dec	bc
  5046                	GETARR0LP:
  5046  23            		inc	hl
  5047  23            		inc	hl
  5048  0B            		dec	bc
  5049  0B            		dec	bc
  504A  3D            		dec	a
  504B  20F9          		jr	nz,GETARR0LP
  504D  C9            		ret
                      	
                      	
                      	;CSAVE command (extended)
  504E                	C_CSVEX:
  504E  CD593F        		call	SKIPSP
  5051  FECC          		cp	ASTRSK_		;cch
  5053  C27323        		jp	nz,C_CSV
  5056  23            		inc	hl
  5057  CD8F33        		call	CHKVAR
  505A  224EFF        		ld	(PROGAD),hl
  505D  CD3D34        		call	SRCHARR
  5060  DACB03        		jp	c,FCERR
  5063  CD3F50        		call	GETARR0
                      	
  5066  C5            		push	bc
  5067  CDA825        		call	PRTOPN
  506A  C1            		pop	bc
  506B                	CSVEXLP:
  506B  23            		inc	hl
  506C  7E            		ld	a,(hl)
  506D  CDCC1A        		call	PUTCMT
  5070  0B            		dec	bc
  5071  78            		ld	a,b
  5072  B1            		or	c
  5073  20F6          		jr	nz,CSVEXLP
  5075  C3061B        		jp	WCLOSE
                      	
                      	
                      	;CLOAD command (extended)
  5078                	C_CLDEX:
  5078  CD593F        		call	SKIPSP
  507B  FECC          		cp	ASTRSK_		;cch
  507D  C2B523        		jp	nz,C_CLD
  5080  23            		inc	hl
  5081  CD8F33        		call	CHKVAR
  5084  224EFF        		ld	(PROGAD),hl
  5087  CD3D34        		call	SRCHARR
  508A  DACB03        		jp	c,FCERR
  508D  CD3F50        		call	GETARR0
                      	
  5090  C5            		push	bc
  5091  CD9A25        		call	INPOPN
  5094  C1            		pop	bc
  5095                	CLDEXLP:
  5095  CD8F1A        		call	GETCMTTR
  5098  23            		inc	hl
  5099  77            		ld	(hl),a
  509A  0B            		dec	bc
  509B  78            		ld	a,b
  509C  B1            		or	c
  509D  20F6          		jr	nz,CLDEXLP
  509F  C3AA1A        		jp	RCLOSE
                      	
                      	
                      	;initialize external FDD
                      	;output: a(if no error, b7-b4=drive connect,b3-b0=disk set), c-flag(1=error)
                      	;destroy: f,bc,de
  50A2                	INIEFD:
  50A2  CD6243        		call	FDOFF
  50A5  3E91          		ld	a,91h		;bit0: portC-lower=input
                      					;bit1: portB=output
                      					;bit2: portB,portC-lower=mode 0
                      					;bit3: portC-upper=output
                      					;bit4: portA=input
                      					;bit65:portA,portC-upper=mode 0
                      					;bit7: mode definition
  50A7  D3D3          		out	(EFDCBIT),a
  50A9  DBD2          		in	a,(EFDC)	;bit0=DAV, bit1=RFD, bit2=DAC
  50AB  2F            		cpl
  50AC  E607          		and	07h
  50AE  37            		scf
  50AF  C8            		ret	z		;error if DAV=RFD=DAC=1
  50B0  AF            		xor	a
  50B1  CD7051        		call	SENDCMD		;FDUINZ command
  50B4  D8            		ret	c
  50B5  C36251        		jp	DRVSTAT
                      	
                      	
                      	;call exernafl FDD read/write routine
  50B8                	RDWREFD:
  50B8  67            		ld	h,a		;drive
  50B9  CD6243        		call	FDOFF
                      	
  50BC  C1            		pop	bc		;b=track, c=sector
  50BD  D1            		pop	de		;de=address
  50BE  C5            		push	bc
                      	
  50BF  3A70FE        		ld	a,(RDWRCHK)
  50C2  3C            		inc	a		;00=read,01=check,ff=write
  50C3  3A71FE        		ld	a,(RMSECT)
  50C6  280A          		jr	z,RDWREFDZ
  50C8  CDD750        		call	RDEFD
                      	
  50CB                	RDWREFDEND:
  50CB  C1            		pop	bc
  50CC  D23443        		jp	nc,NOCHECK
  50CF  C34143        		jp	DSKERR2
                      	
  50D2                	RDWREFDZ:
  50D2  CDFF50        		call	WRTEFD
  50D5  18F4          		jr	RDWREFDEND
                      	
                      	
                      	;read from external FDD
                      	;input: h=drive, a=the number of sector, b=track, c=sector, de=address
                      	;output: c-flag(1=error), de=de+data size(if no error)
                      	;destroy: af,bc,de(if error),hl
  50D7                	RDEFD:
  50D7  3278FE        		ld	(NSECT),a
  50DA  68            		ld	l,b
  50DB  2272FE        		ld	(TRACK),hl	;TRACK,DRIVE
  50DE  79            		ld	a,c
  50DF  3274FE        		ld	(SECTOR),a
                      	
  50E2  EB            		ex	de,hl		;hl<-address
                      	
  50E3  CD2651        		call	RDDSK
  50E6  D8            		ret	c
                      	
  50E7  0600          		ld	b,00h
  50E9                	RDEFDLP:
  50E9  05            		dec	b
  50EA  37            		scf
  50EB  C8            		ret	z		;c-flag=1 if error
  50EC  CD6651        		call	CMDSTAT		;bit0=error status, bit6=buffer status
  50EF  D8            		ret	c
  50F0  0F            		rrca
  50F1  D8            		ret	c
  50F2  E620          		and	20h
  50F4  28F3          		jr	z,RDEFDLP
                      	
  50F6  EB            		ex	de,hl		;de<-address
  50F7  3A70FE        		ld	a,(RDWRCHK)	;00=read,01=check,ff=write
  50FA  B7            		or	a
  50FB  C0            		ret	nz		;check
  50FC  C34851        		jp	TRNBUF		;read
                      	
                      	
                      	;write to external FDD
                      	;input: h=drive, a=the number of sector, b=track, c=sector, de=address
                      	;output: c-flag(1=error), de=de+data size(if no error)
                      	;destroy: af,bc,de(if error)
  50FF                	WRTEFD:
  50FF  3278FE        		ld	(NSECT),a
  5102  68            		ld	l,b
  5103  2272FE        		ld	(TRACK),hl	;TRACK,DRIVE
  5106  79            		ld	a,c
  5107  3274FE        		ld	(SECTOR),a
                      	
  510A  EB            		ex	de,hl		;hl<-address
                      	
  510B  CD2251        		call	WRTDSK
  510E  D8            		ret	c
                      	
  510F  3A78FE        		ld	a,(NSECT)
  5112  4F            		ld	c,a
  5113  0600          		ld	b,00h		;1sector=256bytes
  5115                	WRTEFDLP:
  5115  7E            		ld	a,(hl)
  5116  23            		inc	hl
  5117  CD7651        		call	SENDDATA1
  511A  D8            		ret	c
  511B  10F8          		djnz	WRTEFDLP
  511D  0D            		dec	c
  511E  20F5          		jr	nz,WRTEFDLP
                      	
  5120  EB            		ex	de,hl		;de<-address
  5121  C9            		ret
                      	
                      	
                      	;send write command to external FDD
                      	;input: (NSECT),(DRVIE),(TRACK),(SECTOR)
                      	;destroy: af,de
  5122                	WRTDSK:
  5122  3E01          		ld	a,01h		;WRTDSK command
  5124  1802          		jr	RDWRTDSK
                      	
                      	
                      	;send read/write command to external FDD
                      	;input: (NSECT),(DRVIE),(TRACK),(SECTOR)
                      	;output: c-flag(1=error)
                      	;destroy: af,de
  5126                	RDDSK:
  5126  3E02          		ld	a,02h		;RDDSK command
  5128                	RDWRTDSK:
  5128  CD7051        		call	SENDCMD
  512B  D8            		ret	c
                      	
  512C  3A78FE        		ld	a,(NSECT)
  512F  CD7651        		call	SENDDATA1
  5132  3A73FE        		ld	a,(DRIVE)
  5135  D602          		sub	02h
  5137  CD7651        		call	SENDDATA1
  513A  D8            		ret	c
                      	
  513B  3A72FE        		ld	a,(TRACK)
  513E  CD7651        		call	SENDDATA1
  5141  D8            		ret	c
  5142  3A74FE        		ld	a,(SECTOR)
  5145  C37651        		jp	SENDDATA1
                      	
                      	
                      	;send transfer command to external FDD and receive data
                      	;input: de=address, (NSECT)
                      	;output: de=de+data size(if no error), c-flag(1=error)
                      	;destroy: af,bc,de(if error),hl
  5148                	TRNBUF:
  5148  EB            		ex	de,hl		;hl<-address
  5149  3E03          		ld	a,03h		;TRNBUF command
  514B  CD7051        		call	SENDCMD
  514E  D8            		ret	c
                      	
  514F  3A78FE        		ld	a,(NSECT)
  5152  4F            		ld	c,a
  5153  0600          		ld	b,00h		;1sector=256bytes
  5155                	TRNBUFLP:
  5155  CDB451        		call	RCVDATA1
  5158  D8            		ret	c
  5159  77            		ld	(hl),a
  515A  23            		inc	hl
  515B  10F8          		djnz	TRNBUFLP
  515D  0D            		dec	c
  515E  20F5          		jr	nz,TRNBUFLP
  5160  EB            		ex	de,hl		;de<-address+data size
  5161  C9            		ret			;c-flag=0
                      	
                      	
                      	;send drive status command to external FDD and receive result
                      	;output: a
                      	;destroy: f,de
  5162                	DRVSTAT:
  5162  3E07          		ld	a,07h
  5164  1802          		jr	DRVCMDSTAT
                      	
                      	;send command status command to external FDD and receive result
                      	;output: a, c-flag(1=error)
                      	;destroy: f,de
  5166                	CMDSTAT:
  5166  3E06          		ld	a,06h		;CMDSTAT command
  5168                	DRVCMDSTAT:
  5168  CD7051        		call	SENDCMD
  516B  D8            		ret	c
  516C  CDB451        		call	RCVDATA1
  516F  C9            		ret
                      	
                      	
                      	;send command to external FDD
                      	;input: a
                      	;output: c-flag(1=error)
                      	;destroy: af,de
  5170                	SENDCMD:
  5170  57            		ld	d,a
  5171  3E0F          		ld	a,0fh
  5173  D3D3          		out	(EFDCBIT),a	;set ATN
  5175  7A            		ld	a,d
                      	;	jr	SENDDATA1
                      	
                      	
                      	;send 1 byte to external FDD
                      	;input: a
                      	;output: c-flag(1=error)
                      	;destroy: af,de
  5176                	SENDDATA1:
  5176  F5            		push	af
  5177  110000        		ld	de,0000h
  517A                	SD1LP1:
  517A  1B            		dec	de
  517B  7A            		ld	a,d
  517C  B3            		or	e
  517D  2832          		jr	z,EFDERRPOP
  517F  DBD2          		in	a,(EFDC)
  5181  E602          		and	02h
  5183  28F5          		jr	z,SD1LP1	;wait for RFD=1
                      	
  5185  3E0E          		ld	a,0eh
  5187  D3D3          		out	(EFDCBIT),a	;reset ATN
  5189  F1            		pop	af
  518A  D3D1          		out	(EFDB),a	;set data
  518C  3E09          		ld	a,09h
  518E  D3D3          		out	(EFDCBIT),a	;set DAV
                      	
  5190  110000        		ld	de,0000h
  5193                	SD1LP2:
  5193  1B            		dec	de
  5194  7A            		ld	a,d
  5195  B3            		or	e
  5196  281A          		jr	z,EFDERR
  5198  DBD2          		in	a,(EFDC)
  519A  E604          		and	04h
  519C  28F5          		jr	z,SD1LP2	;wait for DAC=1
                      	
  519E  3E08          		ld	a,08h
  51A0  D3D3          		out	(EFDCBIT),a	;reset DAV
                      	
  51A2  110000        		ld	de,0000h
  51A5                	SD1LP3:
  51A5  1B            		dec	de
  51A6  7A            		ld	a,d
  51A7  B3            		or	e
  51A8  2808          		jr	z,EFDERR
  51AA  DBD2          		in	a,(EFDC)
  51AC  E604          		and	04h
  51AE  20F5          		jr	nz,SD1LP3	;wait for DAC=0
  51B0  C9            		ret			;c-flag=0
                      	
  51B1                	EFDERRPOP:
  51B1  F1            		pop	af
  51B2                	EFDERR:
  51B2  37            		scf
  51B3  C9            		ret
                      	
                      	
                      	;receive 1 byte from external FDD
                      	;output: a=data
                      	;destroy: f,de
  51B4                	RCVDATA1:
  51B4  3E0B          		ld	a,0bh
  51B6  D3D3          		out	(EFDCBIT),a	;set RFD
                      	
  51B8  110000        		ld	de,0000h
  51BB                	RD1LP1:
  51BB  1B            		dec	de
  51BC  7A            		ld	a,d
  51BD  B3            		or	e
  51BE  28F2          		jr	z,EFDERR
  51C0  DBD2          		in	a,(EFDC)
  51C2  0F            		rrca
  51C3  30F6          		jr	nc,RD1LP1	;wait for DAV=1
                      	
  51C5  3E0A          		ld	a,0ah
  51C7  D3D3          		out	(EFDCBIT),a	;reset RFD
  51C9  DBD0          		in	a,(EFDA)	;data
  51CB  F5            		push	af
  51CC  3E0D          		ld	a,0dh
  51CE  D3D3          		out	(EFDCBIT),a	;set DAC
                      	
  51D0  110000        		ld	de,0000h
  51D3                	RD1LP2:
  51D3  1B            		dec	de
  51D4  7A            		ld	a,d
  51D5  B3            		or	e
  51D6  28D9          		jr	z,EFDERRPOP
  51D8  DBD2          		in	a,(EFDC)
  51DA  0F            		rrca
  51DB  38F6          		jr	c,RD1LP2	;wait for DAV=0
                      	
  51DD  3E0C          		ld	a,0ch
  51DF  D3D3          		out	(EFDCBIT),a	;reset DAC
  51E1  F1            		pop	af
                      	
  51E2  B7            		or	a		;c-flag=0
  51E3  C9            		ret
                      	
                      	
                      	;set drive number and FAT/drive pointer
                      	;input: ix, a=drive-1(0-3)
                      	;output: (ix+00h)=drive-1, hl=(0fb33h)=FAT/drive pointer, z-flag(1=no open files)
                      	;destroy: af,de
  51E4                	_SETFATP:ds	SETFATP-_SETFATP
  51F2                		org	SETFATP
                      	
  51F2  213BFB        		ld	hl,DRIVES
  51F5  BE            		cp	(hl)
  51F6  D26042        		jp	nc,DNERR
                      	
  51F9  DD7700        		ld	(ix+00h),a	;drive-1
  51FC  2AFCFF        		ld	hl,(FATPTBL)
  51FF  87            		add	a,a
  5200  85            		add	a,l
  5201  6F            		ld	l,a
  5202  7E            		ld	a,(hl)
  5203  23            		inc	hl
  5204  66            		ld	h,(hl)
  5205  6F            		ld	l,a
  5206  2233FB        		ld	(FATP),hl
  5209  7E            		ld	a,(hl)
  520A  3C            		inc	a		;set z-flag if (hl)=ff (no open files)
  520B  C9            		ret
                      	
                      	
                      	;input: e=file number
                      	;output: a=file open mode, hl=buffer pointer, z-flag(0=open, 1=close)
                      	;destroy: f,de
  520C                	GETBUFP:
  520C  3A31FB        		ld	a,(FILES)
  520F  BB            		cp	e
  5210  DA3F42        		jp	c,BNERR
  5213  2A35FB        		ld	hl,(BUFPTBL)
  5216  1600          		ld	d,00h
  5218  CB23          		sla	e
  521A  19            		add	hl,de
  521B  7E            		ld	a,(hl)
  521C  23            		inc	hl
  521D  66            		ld	h,(hl)
  521E  6F            		ld	l,a
  521F  7E            		ld	a,(hl)		;+0: file open mode
  5220  B7            		or	a
  5221  C9            		ret
                      	
                      	
                      	;for function using file number (EOF,LOC,LOF)
                      	;input: hl=program address
                      	;output: a=file open mode, hl=buffer pointer
                      	;destroy: f,bc,de
  5222                	FNCFNUM:
  5222  CD860B        		call	FNCNUM
                      	;	jr	CHKOPEN
                      	
                      	;check file open
                      	;input: (FAC1)=file number
                      	;output: a=file open mode, hl=buffer pointer
                      	;destroy: f,bc,de
  5225                	CHKOPEN:
  5225  CD4C07        		call	FTOI1
  5228  CD0C52        		call	GETBUFP		;a=file open mode
  522B  CA6C42        		jp	z,CFERR		;not open
  522E  E5            		push	hl
  522F  110400        		ld	de,0004h
  5232  19            		add	hl,de
  5233  7E            		ld	a,(hl)		;+4: drive-1
  5234  CDC853        		call	READFAT
  5237  E1            		pop	hl
  5238  C9            		ret
                      	
                      	
                      	;set file pointer for PRINT# command
                      	;input: hl=program address
                      	;output: hl=next address
                      	;destroy: af,bc,de
  5239                	FNUMPRT:
  5239  0601          		ld	b,01h
                      	;	jr	GETFNUM
                      	
                      	;get file number for PRINT# and INPUT# command
                      	;input: hl=program address, b(1=print#, 2=input#)
                      	;output: hl=next address(>0), # address(<=0)
                      	;destroy: af,bc,de
  523B                	GETFNUM:
  523B  CD593F        		call	SKIPSP
  523E  FE23          		cp	'#'
  5240  3E00          		ld	a,00h
  5242  C0            		ret	nz
                      	
  5243  E5            		push	hl
  5244  C5            		push	bc
  5245  CD910B        		call	NARGMOINC
  5248  EF            		rst	CHKSGN
  5249  C1            		pop	bc
  524A  3D            		dec	a
  524B  2802          		jr	z,FNUMDSK	;>0
  524D  E1            		pop	hl
  524E  C9            		ret
                      	
                      	;for disk file
  524F                	FNUMDSK:
  524F  F1            		pop	af		;cancel return address
  5250  E5            		push	hl
  5251  C5            		push	bc
  5252  CD2552        		call	CHKOPEN
  5255  C1            		pop	bc		;b=1:print#, b=2:input#
  5256  B8            		cp	b
  5257  CA4542        		jp	z,BMERR		;a=1(input mode) and b=1(print#) or a=2(output mode) and b=2(input#)
                      	
  525A  E5            		push	hl
  525B  110700        		ld	de,0007h
  525E  19            		add	hl,de
  525F  7E            		ld	a,(hl)		;+7: file status
  5260  E604          		and	04h
  5262  C26942        		jp	nz,SPERR	;random file
  5265  E1            		pop	hl
  5266  2239FB        		ld	(BUFP),hl
                      	
  5269  110400        		ld	de,0004h
  526C  19            		add	hl,de
  526D  7E            		ld	a,(hl)		;+4: drive-1
  526E  CDE15C        		call	SETIX
  5271  CDF251        		call	SETFATP
                      	
  5274  E1            		pop	hl
  5275  CD6B3F        		call	CHKCMM
  5278  C2C203        		jp	nz,SNERR
  527B  224EFF        		ld	(PROGAD),hl
  527E  C9            		ret
                      	
                      	
                      	;get file number for INPUT# command
                      	;input: hl=program address
                      	;output: a, hl,(PROGAD)=next address
                      	;destroy: f,bc,de
  527F                	FNUMINP:
  527F  0602          		ld	b,02h
  5281  CD3B52        		call	GETFNUM
  5284  ED5B39FB      		ld	de,(BUFP)
  5288  7A            		ld	a,d
  5289  B3            		or	e
  528A  C8            		ret	z
                      	
                      	
                      	;for disk file
  528B  F1            		pop	af		;cancel return address to INPUT
  528C  2A4EFF        		ld	hl,(PROGAD)
                      	
  528F                	INPDSK:
  528F  CD8F33        		call	CHKVAR
  5292  CDA932        		call	GETVAR
  5295  D5            		push	de		;variable address
                      	
  5296                	INPDLP1:
  5296  CD435B        		call	GETDSK
  5299  DA5442        		jp	c,EFERR
  529C  FE20          		cp	' '
  529E  28F6          		jr	z,INPDLP1
                      	
  52A0  21DAFE        		ld	hl,INPBUF
  52A3  0648          		ld	b,48h
                      	
  52A5  CBB1          		res	6,c		;for double quotation check
  52A7  CB79          		bit	7,c
  52A9  280B          		jr	z,INPD1ST	;numeric variable
                      	
                      	;string variable
  52AB  FE22          		cp	22h		;double quotation
  52AD  2007          		jr	nz,INPD1ST
  52AF  CBF1          		set	6,c		;for double quotation check
                      	
  52B1                	INPDLP2:
  52B1  CD435B        		call	GETDSK
  52B4  384A          		jr	c,INPDANA
  52B6                	INPD1ST:
  52B6  B7            		or	a
  52B7  28F8          		jr	z,INPDLP2
                      	
  52B9  CB71          		bit	6,c
  52BB  2012          		jr	nz,INPDDQ	;with double quotation
                      	
                      	;without double quotation (numeric or string variable)
  52BD  FE0D          		cp	0dh
  52BF  2829          		jr	z,INPDCR
  52C1  FE2C          		cp	','
  52C3  283B          		jr	z,INPDANA
                      	
  52C5  CB79          		bit	7,c
  52C7  280C          		jr	z,INPDNUM	;numeric variable
                      	
                      	;string variable
                      	;without double quotation
  52C9  FE0A          		cp	0ah
  52CB  28E4          		jr	z,INPDLP2
  52CD  180A          		jr	INPDST
                      	
                      	;with double quotation
  52CF                	INPDDQ:
  52CF  FE22          		cp	22h		;double quotation
  52D1  282D          		jr	z,INPDANA
  52D3  1804          		jr	INPDST
                      	
                      	;numeric variable
  52D5                	INPDNUM:
  52D5  FE20          		cp	' '
  52D7  2806          		jr	z,INPDSP
                      	;	jr	INPDST
                      	
                      	;store a character
  52D9                	INPDST:
  52D9  77            		ld	(hl),a
  52DA  23            		inc	hl
  52DB  10D4          		djnz	INPDLP2
  52DD  1821          		jr	INPDANA
                      	
                      	;after space
  52DF                	INPDSP:
  52DF  CD435B        		call	GETDSK
  52E2  381C          		jr	c,INPDANA
  52E4  FE20          		cp	' '
  52E6  28F7          		jr	z,INPDSP
  52E8  1809          		jr	INPDCCL
                      	
                      	;after 0dh(CR)
  52EA                	INPDCR:
  52EA  CD435B        		call	GETDSK
  52ED  3811          		jr	c,INPDANA
  52EF  FE0A          		cp	0ah
  52F1  280D          		jr	z,INPDANA
                      	
                      	;cancel 1 character
  52F3                	INPDCCL:
  52F3  E5            		push	hl
  52F4  2A39FB        		ld	hl,(BUFP)
  52F7  110600        		ld	de,0006h
  52FA  19            		add	hl,de
  52FB  35            		dec	(hl)		;+6: position
  52FC  23            		inc	hl		;+7: file status
  52FD  CB8E          		res	1,(hl)		;bit1=to next sector
  52FF  E1            		pop	hl
                      	
                      	;analyze input buffer
  5300                	INPDANA:
  5300  3600          		ld	(hl),00h
  5302  D1            		pop	de		;variable address
  5303  21DAFE        		ld	hl,INPBUF
  5306  CB79          		bit	7,c
  5308  201C          		jr	nz,INPDSTR
                      	
                      	;numeric variable
  530A  D5            		push	de		;variable address
  530B  CD9B39        		call	ATOF
  530E  D1            		pop	de		;variable address
  530F  2166FF        		ld	hl,FAC1
  5312  CD480C        		call	SETF
                      	
  5315                	INPDNEXT:
  5315  2A4EFF        		ld	hl,(PROGAD)
  5318  2B            		dec	hl
  5319  D7            		rst	ANADAT
  531A  FE2C          		cp	','
  531C  C2EF56        		jp	nz,CLRBUFP
  531F  23            		inc	hl
  5320  224EFF        		ld	(PROGAD),hl
  5323  C38F52        		jp	INPDSK
                      	
                      	;string variable
  5326                	INPDSTR:
  5326  E5            		push	hl		;=INPBUF
  5327  0600          		ld	b,00h
  5329                	INPDSLP:
  5329  7E            		ld	a,(hl)
  532A  B7            		or	a
  532B  280B          		jr	z,INPDSZ
  532D  04            		inc	b
  532E  23            		inc	hl
  532F  FE22          		cp	22h		;double quotation
  5331  20F6          		jr	nz,INPDSLP
  5333  CB71          		bit	6,c
  5335  28F2          		jr	z,INPDSLP
  5337  05            		dec	b		;with double quotation
  5338                	INPDSZ:
  5338  E1            		pop	hl		;
  5339  78            		ld	a,b
  533A  D5            		push	de		;variable address
  533B  CD4227        		call	MAKESTR
  533E  D1            		pop	de		;variable address
  533F  212DFF        		ld	hl,STRDSC1
  5342  010400        		ld	bc,0004h
  5345  EDB0          		ldir
  5347  18CC          		jr	INPDNEXT
                      	
                      	
                      	;get track and sector for reading (check next cluster if sector=8 or 16)
                      	;input: b=track, c=sector
                      	;output: b=next track, c=next sector, c-flag(1=no next sector)
                      	;destroy: af
  5349                	NEXTSECT:
  5349  E5            		push	hl
  534A  CD1854        		call	GETFAT2
  534D  F5            		push	af		;FAT value
  534E  79            		ld	a,c
  534F  E607          		and	07h
  5351  2829          		jr	z,NXSCTZ	;sector=8 or 16
                      	
  5353  F1            		pop	af
  5354  E1            		pop	hl
  5355                	NXSCT2NC:
  5355  0C            		inc	c		;1-16
  5356  D6C0          		sub	0c0h		;0-8
  5358  3F            		ccf
  5359  D0            		ret	nc		;continue to next cluster
                      	
                      	;same cluster
  535A  C5            		push	bc
  535B  0D            		dec	c		;0-15
  535C  CB99          		res	3,c		;0-7
  535E  0C            		inc	c		;1-8
  535F  B9            		cp	c
  5360  C1            		pop	bc
  5361  C9            		ret
                      	
                      	
                      	;get track and sector for reading (check next cluster if sector=8 or 16)
                      	;output: b=next track, c=next sector, c-flag(1=no next sector)
                      	;destroy: af,hl
  5362                	NEXTSECT2:
  5362  2A39FB        		ld	hl,(BUFP)
  5365  23            		inc	hl
  5366  23            		inc	hl
  5367  7E            		ld	a,(hl)		;+2: cluster
  5368  47            		ld	b,a
  5369  E5            		push	hl
  536A  CD1D54        		call	GETFAT
  536D  E1            		pop	hl
  536E  23            		inc	hl
  536F  4E            		ld	c,(hl)		;+3: sector in cluster(0-8)
  5370  CB59          		bit	3,c
  5372  200A          		jr	nz,NXSCT2Z	;if sector in cluster=8
                      	
                      	;same cluster
  5374  CB38          		srl	b		;track=cluster/2
  5376  30DD          		jr	nc,NXSCT2NC	;0-7
  5378  CBD9          		set	3,c		;8-15
  537A  18D9          		jr	NXSCT2NC
                      	
                      	
                      	;check next cluster
  537C                	NXSCTZ:
  537C  F1            		pop	af		;FAT value
  537D  E1            		pop	hl
  537E                	NXSCT2Z:
  537E  FEC0          		cp	0c0h
  5380  3F            		ccf
  5381  D8            		ret	c		;no next cluster
                      	
  5382  CDD85C        		call	C2TRSECT
  5385  E5            		push	hl
  5386  CD1D54        		call	GETFAT
  5389  E1            		pop	hl
  538A  FEC0          		cp	0c0h
  538C  37            		scf
  538D  C8            		ret	z		;0 sector in cluster
  538E  B7            		or	a		;reset c-flag
  538F  C9            		ret
                      	
                      	
                      	;get track and sector for writing (search unused cluster if sector=8 or 16)
                      	; and write FAT buffer
                      	;input: b=track, c=sector
                      	;output: a=next cluster, b=next track, c=next sector
                      	;destroy: f
  5390                	NEXTSECTW:
  5390  E5            		push	hl
  5391  D5            		push	de
                      	
  5392  79            		ld	a,c
  5393  C6F7          		add	a,0f7h
  5395  78            		ld	a,b
  5396  8F            		adc	a,a		;cluster=track*2+(1 if sector>=9)
                      	
  5397  5F            		ld	e,a		;old cluster
  5398  2A33FB        		ld	hl,(FATP)
  539B  1600          		ld	d,00h
  539D  1C            		inc	e
  539E  1C            		inc	e
  539F  1C            		inc	e
  53A0  19            		add	hl,de		;(FATP)+3+old cluster
  53A1  5F            		ld	e,a		;old cluster
                      	
  53A2  79            		ld	a,c
  53A3  3D            		dec	a
  53A4  2F            		cpl
  53A5  E607          		and	07h
  53A7  2811          		jr	z,NEWCLST	;if sector=8 or 16
  53A9  79            		ld	a,c
                      	
                      	;same cluster
  53AA                	SAMECLST:
  53AA  E607          		and	07h		;1-7
  53AC  C6C1          		add	a,0c1h		;c2-c8
  53AE  77            		ld	(hl),a
  53AF  0C            		inc	c
  53B0  7B            		ld	a,e		;cluster
  53B1                	NXSCTWEND:
  53B1  CDF753        		call	SETSUM
  53B4  23            		inc	hl
  53B5  36FF          		ld	(hl),0ffh	;-2: need to overwrite FAT
  53B7  D1            		pop	de
  53B8  E1            		pop	hl
  53B9  C9            		ret
                      	
                      	
                      	;new cluster
  53BA                	NEWCLST:
  53BA  7B            		ld	a,e
  53BB  E5            		push	hl		;old FAT buffer
  53BC  CDD054        		call	SRCHCLST
  53BF  36C1          		ld	(hl),0c1h
  53C1  CDD85C        		call	C2TRSECT
  53C4  E1            		pop	hl		;old FAT buffer
  53C5  77            		ld	(hl),a		;new cluster
  53C6  18E9          		jr	NXSCTWEND
                      	
                      	
                      	;read FAT to drive buffer
                      	;input: a=drive-1(0-3)
                      	;output: ix=disk work, (ix+00h), (ix+ibh), ((FATP))=FAT pointer, z-flag(1=no open files)
                      	;destroy: af,bc,de,hl
  53C8                	READFAT:
  53C8  CDE15C        		call	SETIX
  53CB  CDF251        		call	SETFATP
  53CE  F5            		push	af
  53CF  CDFE53        		call	CHKSUM
  53D2  F1            		pop	af
  53D3  C0            		ret	nz		;file open
                      	
  53D4  010E12        		ld	bc,120eh	;track 18, sector 14
  53D7  CD955C        		call	READBUF0
  53DA  ED5B33FB      		ld	de,(FATP)
  53DE  13            		inc	de
  53DF  13            		inc	de
  53E0  13            		inc	de
  53E1  014600        		ld	bc,CLSTS
  53E4  EDB0          		ldir
                      	
  53E6  CDF753        		call	SETSUM
  53E9  BF            		cp	a		;set z-flag
  53EA  C9            		ret
                      	
                      	
                      	;check ID sector
                      	;input: ix=work address, (ix+00h)=drive
                      	;destroy: af,b,de,hl
  53EB                	CHKID:
  53EB  010D12        		ld	bc,120dh	;b=track, c=sector
  53EE  CD955C        		call	READBUF0
  53F1  CB66          		bit	4,(hl)
  53F3  C8            		ret	z
  53F4  C36F42        		jp	FWERR
                      	
                      	
                      	;set FAT check sum
                      	;output: a=check sum value, hl=FAT pointer-3
                      	;destroy: f
  53F7                	SETSUM:
  53F7  C5            		push	bc
  53F8  CD0654        		call	CALCSUM
  53FB  C1            		pop	bc
  53FC  77            		ld	(hl),a
  53FD  C9            		ret
                      	
                      	
                      	;check FAT check sum
                      	;output: a=check sum value, hl=FAT pointer-3
                      	;destroy: f,bc
  53FE                	CHKSUM:
  53FE  CD0654        		call	CALCSUM
  5401  BE            		cp	(hl)
  5402  C8            		ret	z
  5403  C33C42        		jp	IEERR
                      	
                      	
                      	;calculate FAT check sum
                      	;output: a=check sum value, hl=FAT pointer-3
                      	;destroy: f,bc
  5406                	CALCSUM:
  5406  2A33FB        		ld	hl,(FATP)
  5409  23            		inc	hl
  540A  23            		inc	hl
  540B  0646          		ld	b,CLSTS
  540D  3E53          		ld	a,'S'
  540F                	CALCSUMLP:
  540F  23            		inc	hl
  5410  AE            		xor	(hl)
  5411  10FC          		djnz	CALCSUMLP
  5413  01B5FF        		ld	bc,0-CLSTS-5
  5416  09            		add	hl,bc
  5417  C9            		ret
                      	
                      	
                      	;get FAT value
                      	;input: b=track, c=sector
                      	;output: a=FAT value(next cluster, etc.), hl=(FATP)+3+cluster
                      	;destroy: f,hl
  5418                	GETFAT2:
  5418  79            		ld	a,c
  5419  C6F7          		add	a,0f7h		;c-flag=1 if sector>=9
  541B  78            		ld	a,b		;track
  541C  8F            		adc	a,a		;cluster=track*2+(1 if sector>=9)
                      	
                      	;get FAT value
                      	;input: a=cluster
                      	;output: a=FAT value(next cluster, etc.), hl=(FATP)+3+cluster
                      	;destroy: f
  541D                	GETFAT:
  541D  2A33FB        		ld	hl,(FATP)
  5420  3C            		inc	a
  5421  3C            		inc	a
  5422  3C            		inc	a
  5423  85            		add	a,l
  5424  6F            		ld	l,a
  5425  3001          		jr	nc,GETFATNC
  5427  24            		inc	h
  5428                	GETFATNC:
  5428  7E            		ld	a,(hl)		;hl=(FATP)+3+cluster
                      	
  5429  FEFE          		cp	0feh
  542B  D0            		ret	nc
  542C  FEC9          		cp	0c9h
  542E  D25D42        		jp	nc,ATERR
  5431  FEC0          		cp	0c0h
  5433  D0            		ret	nc
  5434  FE46          		cp	CLSTS
  5436  D8            		ret	c
  5437  C35D42        		jp	ATERR
                      	
                      	
                      	;search file
                      	;input: ix, (ix+00h)
                      	;output: z-flag(1=found), a=first cluster, b=attribute,
                      	;        hl=directory address, (ix+01h)=found number(16-1), (ix+02h)=sector
                      	;destroy: f,c,de
  543A                	SRCHFILE:
                      	;read directory
  543A  01010C        		ld	bc,0c01h	;sector 1-12
  543D                	SRCHFLP1:
  543D  C5            		push	bc
  543E  0612          		ld	b,12h		;track 18
  5440  CD955C        		call	READBUF0
  5443  DD360100      		ld	(ix+01h),00h	;not found yet
  5447  DD7102        		ld	(ix+02h),c	;sector
                      	
  544A  0E10          		ld	c,10h
  544C                	SRCHFLP2:
  544C  7E            		ld	a,(hl)
  544D  B7            		or	a
  544E  2003          		jr	nz,SRCHFNZ1
  5450  DD7101        		ld	(ix+01h),c	;killed
  5453                	SRCHFNZ1:
  5453  3C            		inc	a
  5454  2837          		jr	z,SRCHFZ1	;end of directory
                      	
  5456  E5            		push	hl
  5457  DDE5          		push	ix
  5459  0609          		ld	b,09h
  545B                	SRCHFLP3:
  545B  7E            		ld	a,(hl)
  545C  DDBE05        		cp	(ix+05h)	;file name
  545F  201A          		jr	nz,SRCHFNZ2
  5461  23            		inc	hl
  5462  DD23          		inc	ix
  5464  10F5          		djnz	SRCHFLP3
                      	;found
  5466  DDE1          		pop	ix
  5468  F1            		pop	af		;cancel stack
  5469  DD7101        		ld	(ix+01h),c	;16-1
  546C  C1            		pop	bc
  546D  46            		ld	b,(hl)		;attribute
  546E  23            		inc	hl
  546F  7E            		ld	a,(hl)		;cluster
  5470  FEC0          		cp	0c0h
  5472  D25D42        		jp	nc,ATERR
  5475  11F6FF        		ld	de,0-10
  5478  19            		add	hl,de
  5479  BF            		cp	a		;set z-flag
  547A  C9            		ret
                      	
  547B                	SRCHFNZ2:
  547B  DDE1          		pop	ix
  547D  E1            		pop	hl
  547E  111000        		ld	de,0010h
  5481  19            		add	hl,de
  5482  0D            		dec	c
  5483  20C7          		jr	nz,SRCHFLP2
  5485  C1            		pop	bc
  5486  0C            		inc	c
  5487  10B4          		djnz	SRCHFLP1
                      	
                      	;not found
  5489  2A37FB        		ld	hl,(BUF0)
  548C  C5            		push	bc
                      	
  548D                	SRCHFZ1:
  548D  C1            		pop	bc
  548E  DD7E01        		ld	a,(ix+01h)	;killed
  5491  B7            		or	a
  5492  202C          		jr	nz,SRCHFNZ3
  5494  7E            		ld	a,(hl)
  5495  3C            		inc	a
  5496  2836          		jr	z,SRCHFZ3
                      	
                      	;after reading of all direcotry
  5498  01010C        		ld	bc,0c01h	;sector 1-12
  549B                	SRCHFLP4:
  549B  C5            		push	bc
  549C  0612          		ld	b,12h		;track 18
  549E  CD955C        		call	READBUF0
  54A1  DD360100      		ld	(ix+01h),00h	;not found yet
  54A5  DD7102        		ld	(ix+02h),c	;sector
                      	
  54A8  0610          		ld	b,10h
  54AA                	SRCHFLP5:
  54AA  7E            		ld	a,(hl)
  54AB  B7            		or	a
  54AC  280D          		jr	z,SRCHFZ2	;killed
  54AE  111000        		ld	de,0010h
  54B1  19            		add	hl,de
  54B2  10F6          		djnz	SRCHFLP5
  54B4  C1            		pop	bc
  54B5  0C            		inc	c
  54B6  10E3          		djnz	SRCHFLP4
  54B8  C35142        		jp	DFERR
                      	
                      	
  54BB                	SRCHFZ2:
                      	;killed entry (directory end)
  54BB  F1            		pop	af		;cancel stack
  54BC  DD7001        		ld	(ix+01h),b	;16-1
  54BF  78            		ld	a,b
                      	;	jr	SRCHFNZ3
                      	
                      	;killed entry
  54C0                	SRCHFNZ3:
  54C0  2A37FB        		ld	hl,(BUF0)
  54C3  C6EF          		add	a,0efh
  54C5  2F            		cpl			;16-1 -> 0-15
  54C6  07            		rlca
  54C7  07            		rlca
  54C8  07            		rlca
  54C9  07            		rlca
  54CA  1600          		ld	d,00h
  54CC  5F            		ld	e,a
  54CD  19            		add	hl,de
  54CE                	SRCHFZ3:
  54CE  04            		inc	b		;reset z-flag
  54CF  C9            		ret
                      	
                      	
                      	;search unused cluster
                      	;(ex. 23h->22h->26h->21h->27h->20h->...)
                      	;input: a=last used cluster
                      	;output: a=cluster, hl=(FATP)+3+cluster
                      	;destroy: af,bc
  54D0                	SRCHCLST:
  54D0  2A33FB        		ld	hl,(FATP)
  54D3  23            		inc	hl
  54D4  23            		inc	hl
  54D5  0646          		ld	b,CLSTS
  54D7                	SRCHCLP1:
  54D7  23            		inc	hl
  54D8  4E            		ld	c,(hl)
  54D9  0C            		inc	c
  54DA  2805          		jr	z,SRCHCZ1	;ff=unused
  54DC  10F9          		djnz	SRCHCLP1
  54DE  C35142        		jp	DFERR
                      	
  54E1                	SRCHCZ1:
  54E1  E6FE          		and	0feh		;track*2
  54E3  0600          		ld	b,00h
                      	
  54E5                	SRCHCLP2:
  54E5  2A33FB        		ld	hl,(FATP)
  54E8  23            		inc	hl
  54E9  23            		inc	hl
  54EA  23            		inc	hl
                      	
  54EB  4F            		ld	c,a
  54EC  78            		ld	a,b
  54ED  0600          		ld	b,00h
  54EF  09            		add	hl,bc
  54F0  47            		ld	b,a
  54F1  7E            		ld	a,(hl)
  54F2  3C            		inc	a		;
  54F3  79            		ld	a,c
  54F4  C8            		ret	z		;found
                      	
                      	;cluster+=-1,+2,-3,+4,-5,...
  54F5                	SRCHCLP3:
  54F5  04            		inc	b
  54F6  80            		add	a,b
  54F7  CB40          		bit	0,b
  54F9  2804          		jr	z,SRCHCZ2	;if b=even
  54FB  90            		sub	b
  54FC  90            		sub	b
  54FD  38F6          		jr	c,SRCHCLP3
  54FF                	SRCHCZ2:
  54FF  FE46          		cp	CLSTS
  5501  30F2          		jr	nc,SRCHCLP3
  5503  18E0          		jr	SRCHCLP2
                      	
                      	
                      	;NAME command
  5505                	C_NAME:
  5505  CD5E5F        		call	SETFNAME
  5508  F5            		push	af		;drive-1
  5509  CF            		rst	CHKPAR
  550A  41            		db	'A'
  550B  CF            		rst	CHKPAR
  550C  53            		db	'S'
                      	
  550D  DDE5          		push	ix
  550F  0609          		ld	b,09h
  5511                	NAMELP1:
  5511  DD7E05        		ld	a,(ix+05h)	;file name
  5514  DD770E        		ld	(ix+0eh),a
  5517  DD23          		inc	ix
  5519  10F6          		djnz	NAMELP1
  551B  DDE1          		pop	ix
                      	
                      	;	inc	hl
  551D  CD5E5F        		call	SETFNAME
  5520  C1            		pop	bc
  5521  B8            		cp	b
  5522  C26642        		jp	nz,RDERR
  5525  DD7700        		ld	(ix+00h),a	;drive-1
  5528  CDFE53        		call	CHKSUM
  552B  CD3A54        		call	SRCHFILE
  552E  CA4E42        		jp	z,FEERR
                      	
  5531  DDE5          		push	ix
  5533  0609          		ld	b,09h
  5535                	NAMELP2:
  5535  DD7E05        		ld	a,(ix+05h)	;file name
  5538  DD4E0E        		ld	c,(ix+0eh)
  553B  DD770E        		ld	(ix+0eh),a
  553E  DD7105        		ld	(ix+05h),c	;file name
  5541  DD23          		inc	ix
  5543  10F0          		djnz	NAMELP2
  5545  DDE1          		pop	ix
                      	
  5547  CD3A54        		call	SRCHFILE
  554A  C24242        		jp	nz,FFERR
                      	
  554D  DDE5          		push	ix
  554F  0609          		ld	b,09h
  5551                	NAMELP3:
  5551  DD7E0E        		ld	a,(ix+0eh)
  5554  77            		ld	(hl),a
  5555  DD23          		inc	ix
  5557  23            		inc	hl
  5558  10F7          		djnz	NAMELP3
  555A  DDE1          		pop	ix
                      	
  555C  C38A5C        		jp	WRTDIR
                      	
                      	
                      	;OPEN command
  555F                	C_OPEN:
  555F  CD5E5F        		call	SETFNAME
  5562  F5            		push	af		;drive-1
  5563  7E            		ld	a,(hl)
  5564  FE81          		cp	FOR_
  5566  3E04          		ld	a,04h
  5568  201F          		jr	nz,OPENNZ2
  556A  D7            		rst	ANADAT
  556B  FE84          		cp	INPUT_
  556D  2817          		jr	z,OPENINP
  556F  FE90          		cp	OUT_
  5571  2007          		jr	nz,OPENNZ1
  5573  D7            		rst	ANADAT
  5574  CF            		rst	CHKPAR
  5575  AE            		db	PUT_
                      	;for output
  5576  3E02          		ld	a,02h
  5578  180F          		jr	OPENNZ2
                      	
  557A                	OPENNZ1:
  557A  CF            		rst	CHKPAR
  557B  41            		db	'A'
  557C  CF            		rst	CHKPAR
  557D  50            		db	'P'
  557E  CF            		rst	CHKPAR
  557F  50            		db	'P'
  5580  CF            		rst	CHKPAR
  5581  80            		db	END_
                      	;for append
  5582  3E08          		ld	a,08h
  5584  1803          		jr	OPENNZ2
                      	
                      	;for input
  5586                	OPENINP:
  5586  3E01          		ld	a,01h
  5588  23            		inc	hl
  5589                	OPENNZ2:
  5589  F5            		push	af		;file open mode
                      	
  558A  CD593F        		call	SKIPSP
  558D  CF            		rst	CHKPAR
  558E  41            		db	'A'
  558F  CF            		rst	CHKPAR
  5590  53            		db	'S'
  5591  FE23          		cp	'#'
  5593  2001          		jr	nz,OPENNZ3
  5595  23            		inc	hl
  5596                	OPENNZ3:
  5596  CDE40D        		call	INT1ARG		;a,e=file number
  5599  3D            		dec	a
  559A  2131FB        		ld	hl,FILES
  559D  BE            		cp	(hl)
  559E  D23F42        		jp	nc,BNERR
                      	
                      	;de=file number
  55A1  2A35FB        		ld	hl,(BUFPTBL)
  55A4  7B            		ld	a,e
  55A5  87            		add	a,a
  55A6  5F            		ld	e,a
                      	;	ld	d,00h
  55A7  19            		add	hl,de
  55A8  7E            		ld	a,(hl)
  55A9  23            		inc	hl
  55AA  66            		ld	h,(hl)
  55AB  6F            		ld	l,a
  55AC  7E            		ld	a,(hl)
  55AD  B7            		or	a
  55AE  C24842        		jp	nz,AOERR
  55B1  D1            		pop	de		;d=file open mode
  55B2  F1            		pop	af		;drive-1
                      	
  55B3  E5            		push	hl		;file buffer pointer
  55B4  D5            		push	de		;d=file open mode
  55B5  CB4A          		bit	1,d
  55B7  201A          		jr	nz,OPENOUT	;for output
  55B9  CB52          		bit	2,d
  55BB  200B          		jr	nz,OPENRND	;random mode
                      	
                      	;for input
                      	;for append
  55BD  CDC853        		call	READFAT
  55C0  CD3A54        		call	SRCHFILE
  55C3  C24242        		jp	nz,FFERR
  55C6  1812          		jr	OPENEND
                      	
                      	
                      	;random mode
  55C8                	OPENRND:
  55C8  CDC853        		call	READFAT
  55CB  CD3A54        		call	SRCHFILE	;z-flag
  55CE  280A          		jr	z,OPENEND
  55D0  DD7E00        		ld	a,(ix+00h)	;drive-1
                      	
                      	;for output, z-flag=0
  55D3                	OPENOUT:
  55D3  2600          		ld	h,00h		;attribute
  55D5  CD395F        		call	SVCOMMON	;if output mode or random mode not found
  55D8  0600          		ld	b,00h
                      	
                      	;a=cluster
  55DA                	OPENEND:
  55DA  2A33FB        		ld	hl,(FATP)
  55DD  3612          		ld	(hl),12h	;+0: last accessed track
  55DF  D1            		pop	de		;d=file open mode
  55E0  E1            		pop	hl		;file buffer pointer
                      	
                      	;open file
                      	;input: a=cluster, b=attribute, d=file open mode, hl=file buffer pointer
                      	;output: hl=(FATP)
                      	;destroy:af,bc
  55E1                	FOPEN:
  55E1  72            		ld	(hl),d		;+0: file open mode
  55E2  23            		inc	hl
  55E3  77            		ld	(hl),a		;+1: first cluster
  55E4  23            		inc	hl
  55E5  77            		ld	(hl),a		;+2: cluster
  55E6  23            		inc	hl
  55E7  AF            		xor	a
  55E8  77            		ld	(hl),a		;+3: sector in cluster(0-8)
  55E9  CB5A          		bit	3,d
  55EB  2801          		jr	z,FOPENZ
  55ED  34            		inc	(hl)		;if append mode
  55EE                	FOPENZ:
  55EE  23            		inc	hl
  55EF  DD4E00        		ld	c,(ix+00h)	;drive-1
  55F2  71            		ld	(hl),c		;+4: drive-1
  55F3  23            		inc	hl
  55F4  77            		ld	(hl),a		;+5: end position
  55F5  23            		inc	hl
  55F6  77            		ld	(hl),a		;+6: position
  55F7  23            		inc	hl
  55F8  CBC8          		set	1,b
  55FA  70            		ld	(hl),b		;+7: file status=attribute | to next sector
  55FB  23            		inc	hl
  55FC  23            		inc	hl
  55FD  C3825C        		jp	SET0BUF		;[+9,+264]: buffer
                      	
                      	
                      	;open file buffer#0
                      	;input: a=cluster, b=attribute, d=file open mode
                      	;output: hl=(FATP)
                      	;destroy: af,bc,e
  5600                	FOPEN0:
  5600  2A35FB        		ld	hl,(BUFPTBL)
  5603  5E            		ld	e,(hl)
  5604  23            		inc	hl
  5605  66            		ld	h,(hl)
  5606  6B            		ld	l,e
  5607  2239FB        		ld	(BUFP),hl
  560A  18D5          		jr	FOPEN
                      	
                      	
                      	;close file
                      	;input: e=file number
                      	;destroy: af,bc,de,hl
  560C                	FCLOSE:
  560C  CD0C52        		call	GETBUFP
  560F  C8            		ret	z
                      	
  5610  E5            		push	hl
  5611  CDFE53        		call	CHKSUM
  5614  E1            		pop	hl
  5615  E5            		push	hl
  5616  3600          		ld	(hl),00h	;+0: file open mode
  5618  110400        		ld	de,0004h
  561B  19            		add	hl,de
  561C  7E            		ld	a,(hl)		;+4: drive-1
  561D  23            		inc	hl
  561E  23            		inc	hl
  561F  46            		ld	b,(hl)		;+6: position
  5620  23            		inc	hl
  5621  CB5E          		bit	3,(hl)		;+7: file status, bit3=write
  5623  E1            		pop	hl
  5624  285A          		jr	z,CHKCLOSE	;+7: file status, bit3=write
                      	
  5626  CDE15C        		call	SETIX
  5629  E5            		push	hl
  562A  CDF251        		call	SETFATP
  562D  E1            		pop	hl
  562E  23            		inc	hl
  562F  23            		inc	hl		;+2: cluster
  5630  54            		ld	d,h
  5631  5D            		ld	e,l
  5632  78            		ld	a,b		;+6: position
  5633  B7            		or	a
  5634  200E          		jr	nz,FCLNZ
                      	
                      	;position=0
  5636  CD875D        		call	CHKLASTSECT
  5639  2045          		jr	nz,CHKCLOSE
  563B  210700        		ld	hl,0007h
  563E  19            		add	hl,de		;+9
  563F  CDA556        		call	REVFAT
  5642  181D          		jr	WRTFATCHK
                      	
                      	;position>0
  5644                	FCLNZ:
  5644  CD875D        		call	CHKLASTSECT
  5647  D5            		push	de		;+2
  5648  210500        		ld	hl,0005h
  564B  19            		add	hl,de		;+7
  564C  CC895B        		call	z,PUTD1A
  564F  E1            		pop	hl		;+2
  5650  46            		ld	b,(hl)		;+2: cluster
  5651  23            		inc	hl
  5652  7E            		ld	a,(hl)		;+3: sector in cluster
  5653  CB38          		srl	b		;track=cluster/2
  5655  3002          		jr	nc,FCLNC	;even cluster: 1-8
  5657  C608          		add	a,08h		;odd cluster: 9-16
  5659                	FCLNC:
  5659  4F            		ld	c,a		;sector
                      	
  565A  110600        		ld	de,0006h
  565D  19            		add	hl,de		;+9
  565E  CDCA5C        		call	WRTBFMAIN1
                      	;	jr	WRTFATCHK
                      	
                      	;call WRTFAT if necessary
                      	;destroy: af,bc,de,hl
  5661                	WRTFATCHK:
  5661  2A33FB        		ld	hl,(FATP)
  5664  2B            		dec	hl
  5665  2B            		dec	hl
  5666  7E            		ld	a,(hl)
  5667  B7            		or	a
  5668  2816          		jr	z,CHKCLOSE	;-2: not need to overwrite FAT
                      	
                      	;write FAT
                      	;destroy: af,bc,de,hl
  566A                	WRTFAT:
  566A  2A33FB        		ld	hl,(FATP)
  566D  23            		inc	hl
  566E  23            		inc	hl
  566F  23            		inc	hl
  5670  010E12        		ld	bc,120eh	;track 18, sector 14-16
  5673                	WRTFATLP:
  5673  CDCA5C        		call	WRTBFMAIN1
  5676  0C            		inc	c
  5677  79            		ld	a,c
  5678  FE11          		cp	11h
  567A  38F7          		jr	c,WRTFATLP
                      	
  567C  2B            		dec	hl
  567D  2B            		dec	hl
  567E  3600          		ld	(hl),00h	;-2: not need to overwrite FAT
                      	;	jr	CHKCLOSE
                      	
                      	
                      	;check if all files closed
                      	;destroy: af,b,de,hl
  5680                	CHKCLOSE:
  5680  3A31FB        		ld	a,(FILES)
  5683  B7            		or	a
  5684  2808          		jr	z,CHKCLZ
  5686  47            		ld	b,a
  5687                	CHKCLLP:
  5687  58            		ld	e,b
  5688  CD0C52        		call	GETBUFP
  568B  C0            		ret	nz		;open
  568C  10F9          		djnz	CHKCLLP
  568E                	CHKCLZ:
  568E  2A33FB        		ld	hl,(FATP)
  5691  36FF          		ld	(hl),0ffh	;+0: last accessed track (ff=no open files)
  5693  C9            		ret
                      	
                      	
                      	;input: a=first cluster
                      	;destroy: af,hl
  5694                	RELEASEFAT:
  5694  CD1D54        		call	GETFAT
  5697  36FF          		ld	(hl),0ffh	;unused
  5699  FEC0          		cp	0c0h
  569B  38F7          		jr	c,RELEASEFAT
  569D  2A33FB        		ld	hl,(FATP)
  56A0  2B            		dec	hl
  56A1  2B            		dec	hl
  56A2  36FF          		ld	(hl),0ffh	;-2: need to overwrite FAT
  56A4  C9            		ret
                      	
                      	
                      	;revert FAT
                      	;input: hl=buffer pointer+9
                      	;destroy: af,b,de,hl
  56A5                	REVFAT:
  56A5  E5            		push	hl
  56A6  2A33FB        		ld	hl,(FATP)
  56A9  2B            		dec	hl
  56AA  2B            		dec	hl
  56AB  36FF          		ld	(hl),0ffh	;-2: need to overwrite FAT
  56AD  E1            		pop	hl
                      	
  56AE  11F8FF        		ld	de,0-0008h
  56B1  19            		add	hl,de
  56B2  56            		ld	d,(hl)		;+1: first cluster
  56B3  23            		inc	hl
  56B4  5E            		ld	e,(hl)		;+2: cluster
  56B5  7B            		ld	a,e
  56B6  CD1D54        		call	GETFAT
  56B9  3D            		dec	a
  56BA  77            		ld	(hl),a
  56BB  FEC1          		cp	0c1h
  56BD  3016          		jr	nc,REVFATEND
                      	
  56BF  7A            		ld	a,d		;first cluster
  56C0  BB            		cp	e
  56C1  2812          		jr	z,REVFATEND
                      	
  56C3  36FF          		ld	(hl),0ffh
                      	
  56C5  2A33FB        		ld	hl,(FATP)
  56C8  23            		inc	hl
  56C9  23            		inc	hl
                      	
  56CA  0646          		ld	b,CLSTS
  56CC                	REVFATLP:
  56CC  23            		inc	hl
  56CD  7E            		ld	a,(hl)
  56CE  BB            		cp	e
  56CF  2002          		jr	nz,REVFATNZ
  56D1  36C8          		ld	(hl),0c8h
  56D3                	REVFATNZ:
  56D3  10F7          		djnz	REVFATLP
  56D5                	REVFATEND
  56D5  C3F753        		jp	SETSUM
                      	
                      	
                      	;remove disk (from edit hook)
  56D8                	REMOVE:
  56D8  3A3BFB        		ld	a,(DRIVES)
  56DB  B7            		or	a
  56DC  C8            		ret	z
  56DD  2A39FB        		ld	hl,(BUFP)
  56E0  7C            		ld	a,h
  56E1  B5            		or	l
  56E2  CA6156        		jp	z,WRTFATCHK
  56E5  CB4E          		bit	1,(hl)		;file open mode
  56E7  2806          		jr	z,CLRBUFP	;not sequential save
                      	
                      	;for ASCII save
  56E9  CD3927        		call	PUTNL
  56EC  CDD759        		call	CLOSALL
                      	
  56EF                	CLRBUFP:
  56EF  210000        		ld	hl,0000h
  56F2  2239FB        		ld	(BUFP),hl
  56F5  C9            		ret
                      	
                      	;from error hook
  56F6                	ERRCLRB:
  56F6  D5            		push	de
  56F7  CD8056        		call	CHKCLOSE
  56FA  D1            		pop	de
  56FB  18F2          		jr	CLRBUFP
                      	
                      	
                      	;KILL command
  56FD                	C_KILL:
  56FD  CD5E5F        		call	SETFNAME
  5700  CDC853        		call	READFAT
  5703  CDEB53        		call	CHKID
  5706  CD3A54        		call	SRCHFILE
  5709  C24242        		jp	nz,FFERR
  570C  CB60          		bit	4,b
  570E  C26F42        		jp	nz,FWERR
                      	
  5711  E5            		push	hl
  5712  4F            		ld	c,a		;first cluster
  5713  3A31FB        		ld	a,(FILES)
  5716  47            		ld	b,a
  5717                	KILLLP:
  5717  58            		ld	e,b
  5718  CD0C52        		call	GETBUFP
  571B  280F          		jr	z,KILLOK	;close
  571D  23            		inc	hl
  571E  7E            		ld	a,(hl)		;+1:first cluster
  571F  B9            		cp	c
  5720  200A          		jr	nz,KILLOK
  5722  23            		inc	hl
  5723  23            		inc	hl
  5724  23            		inc	hl
  5725  7E            		ld	a,(hl)		;+4: drive-1
  5726  DDBE00        		cp	(ix+00h)	;drive-1
  5729  CA4842        		jp	z,AOERR
  572C                	KILLOK:
  572C  10E9          		djnz	KILLLP
  572E  E1            		pop	hl
  572F  79            		ld	a,c
                      	
  5730  3600          		ld	(hl),00h	;delete mark
  5732  CD9456        		call	RELEASEFAT
  5735  CD6A56        		call	WRTFAT
  5738  C38A5C        		jp	WRTDIR
                      	
                      	
                      	;RUN with disk driver
                      	;continued from RUNPAR
                      	;input: a<>0, c-flag=0
  573B                	RUNDSK:
  573B  F5            		push	af		;c-flag=0: not merge
  573C  DDCB17FE      		set	7,(ix+17h)	;do not close files
  5740  1809          		jr	LOADFN		;a<>0: run program
                      	
                      	;LOAD command
  5742                	C_LOAD:
  5742  F6            		db	0f6h		;or 37h, reset c-flag
                      	
                      	;MERGE command
  5743                	C_MRG:
  5743  37            		scf			;37h
                      	
  5744  C1            		pop	bc		;cancel return address
  5745  F5            		push	af		;c-flag: load/run=0, merge=1
                      	
  5746  DDCB17BE      		res	7,(ix+17h)	;close files
  574A  AF            		xor	a		;default: no R option
                      	
  574B                	LOADFN:
  574B  CD5E5F        		call	SETFNAME
  574E  47            		ld	b,a		;drive-1
  574F  F1            		pop	af		;c-flag: load/run=0, merge=1
  5750  F5            		push	af		;c-flag: load/run=0, merge=1
  5751  381A          		jr	c,LOADMAIN	;if merge
                      	
  5753  7E            		ld	a,(hl)
  5754  FE2C          		cp	','
  5756  2806          		jr	z,LOADR
  5758  DDCB17BE      		res	7,(ix+17h)	;close files
  575C  180F          		jr	LOADMAIN
                      	
  575E                	LOADR:
  575E  CD583F        		call	SKIPSPINC
  5761  FE52          		cp	'R'
  5763  C2C203        		jp	nz,SNERR
  5766  DD7703        		ld	(ix+03h),a	;R option (execution)
  5769  DDCB17FE      		set	7,(ix+17h)	;R option (not close files)
                      	
  576D                	LOADMAIN:
  576D  78            		ld	a,b		;drive-1
  576E  CDC853        		call	READFAT
  5771  CD3A54        		call	SRCHFILE
  5774  C24242        		jp	nz,FFERR
                      	
  5777  4F            		ld	c,a		;cluster
  5778  F1            		pop	af		;c-flag: load/run=0, merge=1
  5779  D4BE34        		call	nc,NEW
  577C  79            		ld	a,c		;cluster
  577D  CB78          		bit	7,b		;attribute
  577F  2855          		jr	z,LOADASC
  5781  DA4542        		jp	c,BMERR		;merge and not ascii
                      	
                      	
                      	;normal format load
                      	;input: a=cluster
  5784  2A5FFA        		ld	hl,(STARTAD)
  5787                	LOADLP:
  5787  CDD85C        		call	C2TRSECT
  578A  EB            		ex	de,hl
  578B  CD1D54        		call	GETFAT
  578E  EB            		ex	de,hl
  578F  F5            		push	af		;FAT value
  5790  FEC9          		cp	0c9h
  5792  D25D42        		jp	nc,ATERR
  5795  D6C0          		sub	0c0h
  5797  2813          		jr	z,LOADZ
  5799  3002          		jr	nc,LOADNC2	;c1-c8
  579B  3E08          		ld	a,08h		;others
  579D                	LOADNC2:
  579D  F5            		push	af
  579E  EB            		ex	de,hl
  579F  2F            		cpl
  57A0  67            		ld	h,a
  57A1  2EC6          		ld	l,0-3bh+1
  57A3  39            		add	hl,sp
  57A4  E7            		rst	CPHLDE
  57A5  3826          		jr	c,LOADOM
  57A7  EB            		ex	de,hl
  57A8  F1            		pop	af
                      	
  57A9  CDAA5C        		call	RDBFMAIN
  57AC                	LOADZ:
  57AC  84            		add	a,h
  57AD  67            		ld	h,a
  57AE  F1            		pop	af		;FAT vaule
  57AF  FEC0          		cp	0c0h
  57B1  38D4          		jr	c,LOADLP
                      	
  57B3                	LOADEND:
  57B3  CDCD04        		call	CHGLKPMAIN
  57B6  23            		inc	hl
  57B7  2256FF        		ld	(VARAD),hl
                      	
  57BA  CDF934        		call	RESSTK		;close all files (if without R option)
  57BD  CD8056        		call	CHKCLOSE
  57C0  CDEF56        		call	CLRBUFP
  57C3  DD7E03        		ld	a,(ix+03h)	;R option?
  57C6  B7            		or	a
  57C7  C28807        		jp	nz,RUNFIRST
  57CA  C34204        		jp	EDIT
                      	
                      	
  57CD                	LOADOM:
  57CD  CDBE34        		call	NEW
  57D0  CDEF56        		call	CLRBUFP
  57D3  C3D103        		jp	OMERR
                      	
                      	;ASCII load
                      	;input: a=cluster, b=attribute
  57D6                	LOADASC:
  57D6  05            		dec	b
  57D7  CA5A42        		jp	z,DSERR
                      	
  57DA  CDD85C        		call	C2TRSECT
  57DD  0600          		ld	b,00h		;attribute=ascii
  57DF  1601          		ld	d,01h		;file open mode=input
  57E1  CD0056        		call	FOPEN0
                      	
  57E4                	LDASCLP1:
  57E4  CD435B        		call	GETDSK
  57E7  38CA          		jr	c,LOADEND
  57E9  FE0A          		cp	0ah
  57EB  28F7          		jr	z,LDASCLP1
  57ED  FE0D          		cp	0dh
  57EF  28F3          		jr	z,LDASCLP1
  57F1  B7            		or	a
  57F2  2806          		jr	z,LDASCZ
  57F4  CDE00B        		call	CHKFIG
  57F7  D25A42        		jp	nc,DSERR
  57FA                	LDASCZ:
  57FA  21DAFE        		ld	hl,INPBUF
  57FD  0648          		ld	b,48h
  57FF  180C          		jr	LDASCPUT
                      	
  5801                	LDASCLP2:
  5801  CD435B        		call	GETDSK
  5804  38AD          		jr	c,LOADEND
  5806  FE0D          		cp	0dh
  5808  2807          		jr	z,LOADCNV
  580A  B7            		or	a
  580B  2804          		jr	z,LOADCNV
  580D                	LDASCPUT:
  580D  77            		ld	(hl),a
  580E  23            		inc	hl
  580F  10F0          		djnz	LDASCLP2
                      	
  5811                	LOADCNV:
  5811  F5            		push	af		;
  5812  3600          		ld	(hl),00h
  5814  21D9FE        		ld	hl,INPBUF-1
  5817  CD7604        		call	INPTPRG
  581A  CDCD04        		call	CHGLKPMAIN
  581D  F1            		pop	af		;
  581E  B7            		or	a
  581F  28D9          		jr	z,LDASCZ
  5821  18C1          		jr	LDASCLP1
                      	
                      	
                      	;SAVE command
  5823                	C_SAVE:
  5823  CD5E5F        		call	SETFNAME
  5826  F5            		push	af		;drive-1
  5827  DD361800      		ld	(ix+18h),00h	;0=not ASCII format
  582B  CD603F        		call	CHKCLN
  582E  280A          		jr	z,SAVEMAIN
  5830  CF            		rst	CHKPAR
  5831  2C            		db	','
  5832  FE41          		cp	'A'
  5834  C2C203        		jp	nz,SNERR
  5837  DD7718        		ld	(ix+18h),a	;not0=ASCII format
                      	
  583A                	SAVEMAIN:
  583A  DD7E18        		ld	a,(ix+18h)	;ASCII save?
  583D  FE01          		cp	01h		;set c-flag if a=0
  583F  9F            		sbc	a,a
  5840  E680          		and	80h		;normal save=80h, ASCII save=00h
  5842  67            		ld	h,a		;attribute
  5843  F1            		pop	af		;drive-1
  5844  E5            		push	hl
  5845  CD395F        		call	SVCOMMON
  5848  E1            		pop	hl
  5849  24            		inc	h
  584A  25            		dec	h
  584B  2843          		jr	z,SAVEASC	;if attribute=0
                      	
                      	;normal save
  584D  2A56FF        		ld	hl,(VARAD)
  5850  ED5B5FFA      		ld	de,(STARTAD)
  5854  B7            		or	a
  5855  ED52          		sbc	hl,de
  5857  EB            		ex	de,hl		;hl <- start address
  5858  1B            		dec	de
  5859  14            		inc	d		;d <- sectors
                      	
  585A                	SAVELP:
  585A  E5            		push	hl
  585B  CD1D54        		call	GETFAT
  585E  7A            		ld	a,d		;sectors
  585F  FE08          		cp	08h
  5861  3802          		jr	c,SAVEC
  5863  3E08          		ld	a,08h
  5865                	SAVEC:
  5865  C6C0          		add	a,0c0h
  5867  77            		ld	(hl),a
  5868  D6C0          		sub	0c0h
  586A  F5            		push	af
  586B  CDF753        		call	SETSUM
  586E  F1            		pop	af
  586F  E1            		pop	hl
                      	
  5870  E5            		push	hl
  5871  D5            		push	de
  5872  CDCC5C        		call	WRTBFMAIN
  5875  D1            		pop	de
  5876  E1            		pop	hl
                      	
  5877  92            		sub	d
  5878  2810          		jr	z,SAVEEND
  587A  ED44          		neg			;d-a
  587C  57            		ld	d,a
                      	
  587D  7C            		ld	a,h
  587E  C608          		add	a,08h
  5880  67            		ld	h,a
                      	
  5881  79            		ld	a,c
  5882  C607          		add	a,07h
  5884  4F            		ld	c,a
  5885  CD9053        		call	NEXTSECTW
  5888  18D0          		jr	SAVELP
                      	
  588A                	SAVEEND:
  588A  CDD759        		call	CLOSALL
  588D  C36A56        		jp	WRTFAT
                      	
                      	
                      	;ASCII save
  5890                	SAVEASC:
  5890  0600          		ld	b,00h		;attribute=ascii
  5892  1602          		ld	d,02h		;file open mode=output
  5894  CD0056        		call	FOPEN0
                      	
  5897  110000        		ld	de,0000h	;start line number
  589A  F1            		pop	af		;cancel return address
  589B  C3F005        		jp	LISTTOEND
                      	
                      	
                      	;set file name in directory for writing
                      	;input: a=drive-1(0-3)
                      	;output: a=cluster, de=directory+9(attribute address), ix=disk work, (FATP)
                      	;destroy: f,bc,hl
  589E                	SETDIR:
  589E  CDC853        		call	READFAT
  58A1  CDEB53        		call	CHKID
  58A4  CD3A54        		call	SRCHFILE
  58A7  E5            		push	hl		;directory address
  58A8  2022          		jr	nz,SETDNZ	;not found
  58AA  CB60          		bit	4,b		;can overwrite?
  58AC  C26F42        		jp	nz,FWERR
                      	
  58AF  F5            		push	af		;first cluster
  58B0  57            		ld	d,a
  58B1  3A31FB        		ld	a,(FILES)
  58B4  B7            		or	a
  58B5  2811          		jr	z,SETDZ2
  58B7  5F            		ld	e,a
  58B8                	SETDLP:
  58B8  D5            		push	de
  58B9  CD0C52        		call	GETBUFP
  58BC  D1            		pop	de
  58BD  2806          		jr	z,SETDZ1	;close
  58BF  23            		inc	hl
  58C0  7E            		ld	a,(hl)		;+1: first cluster
  58C1  BA            		cp	d
  58C2  CA4842        		jp	z,AOERR
  58C5                	SETDZ1:
  58C5  1D            		dec	e
  58C6  20F0          		jr	nz,SETDLP
  58C8                	SETDZ2:
  58C8  F1            		pop	af		;first cluster
                      	
  58C9  CD9456        		call	RELEASEFAT
  58CC                	SETDNZ:
  58CC  3E24          		ld	a,24h
  58CE  CDD054        		call	SRCHCLST
  58D1  D1            		pop	de		;directory address
  58D2  DD            		db	0ddh		;ix prefix
  58D3  44            		ld	b,h		;ld b,ixh
  58D4  DD            		db	0ddh		;ix prefix
  58D5  4D            		ld	c,l		;ld c,ixl
  58D6  210500        		ld	hl,0005h
  58D9  09            		add	hl,bc		;hl=ix+5
  58DA  010900        		ld	bc,0009h
  58DD  EDB0          		ldir
  58DF  C9            		ret
                      	
                      	
                      	;RSET command
  58E0                	C_RSET:
  58E0  CD0E59        		call	RLSETSUB		;
                      	
  58E3                	RSETLP:
  58E3  3007          		jr	nc,RSETNC	;
  58E5  3620          		ld	(hl),' '
  58E7  23            		inc	hl
  58E8  0D            		dec	c
  58E9  B9            		cp	c
  58EA  18F7          		jr	RSETLP
                      	
  58EC                	RSETNC:
  58EC  AF            		xor	a
  58ED  47            		ld	b,a
  58EE  B1            		or	c
  58EF  C8            		ret	z
                      	
  58F0  EB            		ex	de,hl
  58F1  EDB0          		ldir
  58F3  C9            		ret
                      	
                      	
                      	;LSET command
  58F4                	C_LSET:
  58F4  CD0E59        		call	RLSETSUB		;
  58F7  3801          		jr	c,LSETC		;
  58F9  79            		ld	a,c
  58FA                	LSETC:
  58FA  B7            		or	a
  58FB  2807          		jr	z,LSETZ
                      	
  58FD  EB            		ex	de,hl
  58FE  47            		ld	b,a
  58FF                	LSETLP1:
  58FF  EDA0          		ldi
  5901  10FC          		djnz	LSETLP1
  5903  EB            		ex	de,hl
                      	
  5904                	LSETZ:
  5904  79            		ld	a,c
  5905  B7            		or	a
  5906  C8            		ret	z
                      	
  5907  41            		ld	b,c
  5908                	LSETLP2:
  5908  3620          		ld	(hl),' '
  590A  23            		inc	hl
  590B  10FB          		djnz	LSETLP2
  590D  C9            		ret
                      	
                      	
                      	;subroutine for RSET and LSET command
                      	;input: hl=program address
                      	;output: c=left side length,  hl=left side buffer address,
                      	;        a=right side length, de=right side string address
                      	;        c-flag: a-c
                      	;destroy: f,b
  590E                	RLSETSUB:
  590E  CD8F33        		call	CHKVAR
  5911  CDA932        		call	GETVAR
  5914  CB79          		bit	7,c
  5916  CAE303        		jp	z,TMERR
                      	
  5919  2A4EFF        		ld	hl,(PROGAD)
  591C  7E            		ld	a,(hl)
  591D  FED2          		cp	EQ_
  591F  C2C203        		jp	nz,SNERR
                      	
  5922  D5            		push	de		;varible address
  5923  23            		inc	hl
  5924  CD8126        		call	STRARG		;a=length, de=string address
  5927  E1            		pop	hl		;variable address
                      	
  5928  4E            		ld	c,(hl)		;length
  5929  23            		inc	hl
  592A  23            		inc	hl
  592B  46            		ld	b,(hl)
  592C  23            		inc	hl
  592D  66            		ld	h,(hl)
  592E  68            		ld	l,b
                      	
  592F  B9            		cp	c
  5930  C9            		ret
                      	
                      	
                      	;FIELD command
  5931                	C_FLD:
  5931  CD593F        		call	SKIPSP
  5934  FE23          		cp	'#'
  5936  2001          		jr	nz,FLDNZ
  5938  23            		inc	hl
  5939                	FLDNZ:
  5939  CDE40D        		call	INT1ARG
                      	
  593C  3A31FB        		ld	a,(FILES)
  593F  BB            		cp	e
  5940  DA3F42        		jp	c,BNERR
  5943  7B            		ld	a,e
  5944  CD0C52        		call	GETBUFP
  5947  010900        		ld	bc,0009h
  594A  09            		add	hl,bc
  594B  48            		ld	c,b		;total length=0
                      	
  594C                	FLDLP:
  594C  E5            		push	hl		;buffer address
  594D  2A4EFF        		ld	hl,(PROGAD)
  5950  7E            		ld	a,(hl)
  5951  FE2C          		cp	','
  5953  D1            		pop	de		;buffer address
  5954  C0            		ret	nz
  5955  D5            		push	de		;buffer address
  5956  23            		inc	hl
                      	
  5957  C5            		push	bc		;total length
  5958  CDE40D        		call	INT1ARG
  595B  D5            		push	de		;length
  595C  CF            		rst	CHKPAR
  595D  41            		db	'A'
  595E  CF            		rst	CHKPAR
  595F  53            		db	'S'
  5960  CD8F33        		call	CHKVAR
  5963  CB79          		bit	7,c
  5965  CAE303        		jp	z,TMERR
  5968  CDA932        		call	GETVAR
                      	
  596B  C1            		pop	bc		;length
  596C  E1            		pop	hl		;total length
  596D  09            		add	hl,bc
  596E  D5            		push	de		;variable address
  596F  110101        		ld	de,0101h
  5972  E7            		rst	CPHLDE
  5973  D23942        		jp	nc,FOERR
  5976  D1            		pop	de		;variable address
  5977  E3            		ex	(sp),hl		;buffer address
                      	
  5978  EB            		ex	de,hl
  5979  71            		ld	(hl),c
  597A  23            		inc	hl
  597B  23            		inc	hl
  597C  73            		ld	(hl),e
  597D  23            		inc	hl
  597E  72            		ld	(hl),d
                      	
  597F  EB            		ex	de,hl
  5980  09            		add	hl,bc
  5981  C1            		pop	bc		;total length
  5982  18C8          		jr	FLDLP
                      	
                      	
                      	;MKS$() function
  5984                	F_MKS:
  5984  CD860B        		call	FNCNUM
  5987  3E05          		ld	a,05h
  5989  2166FF        		ld	hl,FAC1
  598C  CD4227        		call	MAKESTR
  598F  C39027        		jp	INKYEND
                      	
                      	
                      	;CVS() function
  5992                	F_CVS:
  5992  CDB50B        		call	CHKLPAR
  5995  CD4717        		call	ARG
  5998  CDC40B        		call	CHKRPAR
  599B  CD8A26        		call	STRARG2
  599E  FE05          		cp	05h
  59A0  DACB03        		jp	c,FCERR
                      	
  59A3  EB            		ex	de,hl
  59A4  1166FF        		ld	de,FAC1
  59A7  010500        		ld	bc,0005h
  59AA  EDB0          		ldir
                      	
  59AC  C3A731        		jp	SETTYP0
                      	
                      	
                      	;CLOSE command
  59AF                	C_CLOS:
  59AF  CD603F        		call	CHKCLN
  59B2  2823          		jr	z,CLOSALL
  59B4                	CLOSLP1:
  59B4  FE23          		cp	'#'
  59B6  2001          		jr	nz,CLOSNZ
  59B8  23            		inc	hl
  59B9                	CLOSNZ:
  59B9  CDE40D        		call	INT1ARG
  59BC  E5            		push	hl
  59BD  CD0C56        		call	FCLOSE
  59C0  E1            		pop	hl
  59C1  CD603F        		call	CHKCLN
  59C4  C8            		ret	z
  59C5  7E            		ld	a,(hl)
  59C6  FE2C          		cp	','
  59C8  C0            		ret	nz
  59C9  CD583F        		call	SKIPSPINC
  59CC  18E6          		jr	CLOSLP1
                      	
  59CE                	CHKROPT:
  59CE  DDCB177E      		bit	7,(ix+17h)	;close files?
  59D2  DDCB17BE      		res	7,(ix+17h)
  59D6  C0            		ret	nz		;RUN with R option
                      	
  59D7                	CLOSALL:
  59D7  3A31FB        		ld	a,(FILES)
  59DA  5F            		ld	e,a
  59DB                	CLOSLP2:
  59DB  D5            		push	de
  59DC  CD0C56        		call	FCLOSE
  59DF  D1            		pop	de
  59E0  1D            		dec	e
  59E1  F2DB59        		jp	p,CLOSLP2
  59E4  C9            		ret
                      	
                      	
                      	;LFILES command
  59E5                	C_LFLS:
  59E5  3E01          		ld	a,01h
  59E7  3258FA        		ld	(DEVICE),a
                      	;	jr	C_FLS
                      	
                      	;FILES command
  59EA                	C_FLS:
  59EA  CD603F        		call	CHKCLN
  59ED  3E01          		ld	a,01h
  59EF  C4E40D        		call	nz,INT1ARG
  59F2  3D            		dec	a
  59F3  CDC853        		call	READFAT
  59F6  C24842        		jp	nz,AOERR
                      	
                      	;read directory
  59F9  0E01          		ld	c,01h		;sector 1
  59FB                	FLSLP1:
  59FB  0612          		ld	b,12h		;track 18
  59FD  CD955C        		call	READBUF0
                      	
  5A00  0610          		ld	b,10h
  5A02                	FLSLP2:
  5A02  7E            		ld	a,(hl)
  5A03  3C            		inc	a
  5A04  CA8A5A        		jp	z,FLSEND	;ffh=unused
                      	
  5A07  C5            		push	bc
  5A08  111000        		ld	de,0010h
  5A0B  3D            		dec	a
  5A0C  286C          		jr	z,FLSZ2		;00h=deleted
                      	
                      	;file name
  5A0E  0606          		ld	b,06h
  5A10                	FLSLP3:
  5A10  7E            		ld	a,(hl)
  5A11  CDC726        		call	PUTDEV
  5A14  23            		inc	hl
  5A15  10F9          		djnz	FLSLP3
                      	
  5A17  54            		ld	d,h
  5A18  5D            		ld	e,l
  5A19  13            		inc	de
  5A1A  13            		inc	de
  5A1B  13            		inc	de
  5A1C  1A            		ld	a,(de)		;attribute
  5A1D  B7            		or	a		;
  5A1E  0F            		rrca
  5A1F  3E2E          		ld	a,'.'
  5A21  FA2A5A        		jp	m,FLSM		;
  5A24  3E2A          		ld	a,'*'
  5A26  3802          		jr	c,FLSM
  5A28  3E20          		ld	a,' '
  5A2A                	FLSM:
  5A2A  CDC726        		call	PUTDEV
                      	
                      	;suffix
  5A2D  0603          		ld	b,03h
  5A2F                	FLSLP4:
  5A2F  7E            		ld	a,(hl)
  5A30  CDC726        		call	PUTDEV
  5A33  23            		inc	hl
  5A34  10F9          		djnz	FLSLP4
  5A36  3E20          		ld	a,' '
  5A38  CDC726        		call	PUTDEV
                      	
                      	;clusters
  5A3B  23            		inc	hl
  5A3C  7E            		ld	a,(hl)		;first cluster
  5A3D  E5            		push	hl		;;
  5A3E  50            		ld	d,b		;=0
  5A3F  CD975A        		call	CNTCLST
  5A42  3C            		inc	a
  5A43  CD7A39        		call	PUTI1
  5A46  E1            		pop	hl		;;
                      	
                      	;alignment
  5A47  3AACFD        		ld	a,(WIDTH)
  5A4A  FE20          		cp	COLUMNS
  5A4C  06EF          		ld	b,0efh		;mask
  5A4E  3002          		jr	nc,FLSLP6
  5A50  06FF          		ld	b,0ffh		;mask
                      	
  5A52                	FLSLP6:
  5A52  3A58FA        		ld	a,(DEVICE)
  5A55  0F            		rrca			;
  5A56  3AA9FD        		ld	a,(CSRX)
  5A59  3D            		dec	a
  5A5A  3005          		jr	nc,FLSNC2	;
                      	;printer
  5A5C  3A57FA        		ld	a,(PRTPOS)
  5A5F  060F          		ld	b,0fh		;mask
  5A61                	FLSNC2:
  5A61  A0            		and	b
  5A62  2807          		jr	z,FLSZ1
  5A64  3E20          		ld	a,' '
  5A66  CDC726        		call	PUTDEV
  5A69  18E7          		jr	FLSLP6
                      	
  5A6B                	FLSZ1:
  5A6B  CB68          		bit	5,b
  5A6D  2008          		jr	nz,FLSNZ
                      	;printer
  5A6F  3A57FA        		ld	a,(PRTPOS)
  5A72  FE20          		cp	COLUMNS
  5A74  D43927        		call	nc,PUTNL
                      	
  5A77                	FLSNZ:
  5A77  110600        		ld	de,0006h
  5A7A                	FLSZ2:
  5A7A  19            		add	hl,de
                      	
  5A7B  CD4D27        		call	STOPESC
  5A7E  C1            		pop	bc
  5A7F  05            		dec	b
  5A80  C2025A        		jp	nz,FLSLP2
                      	
  5A83  0C            		inc	c
  5A84  79            		ld	a,c
  5A85  FE0C          		cp	0ch
  5A87  DAFB59        		jp	c,FLSLP1
  5A8A                	FLSEND:
  5A8A  CD3927        		call	PUTNL
                      	
  5A8D  2A33FB        		ld	hl,(FATP)
  5A90  36FF          		ld	(hl),0ffh	;+0: last accessed track (ff=no open files)
                      	
  5A92  AF            		xor	a
  5A93  3258FA        		ld	(DEVICE),a
  5A96  C9            		ret
                      	
                      	
                      	;count clusters
                      	;input: a=first cluster number
                      	;output: a=clusters, c=last sectors
                      	;destroy: f,b,hl
  5A97                	CNTCLST:
  5A97  0EFF          		ld	c,0ffh
  5A99                	CNTCLST2:
  5A99  0600          		ld	b,00h
  5A9B                	CNTCLP:
  5A9B  B9            		cp	c
  5A9C  2819          		jr	z,CNTCLZ
  5A9E  CD1D54        		call	GETFAT
  5AA1  FEC9          		cp	0c9h
  5AA3  D25D42        		jp	nc,ATERR
  5AA6  FEC0          		cp	0c0h
  5AA8  3007          		jr	nc,CNTCNC
  5AAA  FE46          		cp	CLSTS
  5AAC  D25D42        		jp	nc,ATERR
  5AAF  10EA          		djnz	CNTCLP
  5AB1                	CNTCNC:
  5AB1  D6C0          		sub	0c0h
  5AB3  4F            		ld	c,a
  5AB4  AF            		xor	a
  5AB5  90            		sub	b
  5AB6  C9            		ret
                      	
  5AB7                	CNTCLZ:
  5AB7  AF            		xor	a
  5AB8  90            		sub	b
  5AB9  C9            		ret
                      	
                      	
                      	;check disk file buffer and put a character to disk file (from PUTDEV hook)
                      	;input: a=data
                      	;destroy: none(file buffer pointer=0) / af(file buffer pointer<>0)
  5ABA                	PUTDSK:
  5ABA  E5            		push	hl
  5ABB  F5            		push	af
  5ABC  2A39FB        		ld	hl,(BUFP)
  5ABF  7C            		ld	a,h
  5AC0  B5            		or	l
  5AC1  2003          		jr	nz,PUTDMAIN
  5AC3  F1            		pop	af
  5AC4  E1            		pop	hl
  5AC5  C9            		ret
                      	
  5AC6                	PUTDMAIN:
  5AC6  F1            		pop	af
  5AC7  D5            		push	de
  5AC8  C5            		push	bc
  5AC9  F5            		push	af		;data
                      	
  5ACA  56            		ld	d,(hl)		;+0: file open mode
  5ACB  23            		inc	hl
  5ACC  23            		inc	hl
  5ACD  46            		ld	b,(hl)		;+2: cluster
  5ACE  23            		inc	hl
  5ACF  4E            		ld	c,(hl)		;+3: sector in cluster
                      	
  5AD0  23            		inc	hl
  5AD1  23            		inc	hl
  5AD2  23            		inc	hl
  5AD3  E5            		push	hl
  5AD4  23            		inc	hl
  5AD5  CB66          		bit	4,(hl)		;+7: file status: write protect?
  5AD7  C26F42        		jp	nz,FWERR
  5ADA  CB4E          		bit	1,(hl)		;+7: file status: to next sector?
  5ADC  2832          		jr	z,PUTDDATA
                      	
  5ADE  CB5A          		bit	3,d
  5AE0  200E          		jr	nz,PUTDAPP
  5AE2  CB4A          		bit	1,d
  5AE4  2027          		jr	nz,PUTDOUT
                      	
                      	;random mode
  5AE6                	PUTDRND:
  5AE6  CD895B        		call	PUTD1A
  5AE9  CDFC5B        		call	RDNEXT
  5AEC  381F          		jr	c,PUTDNEW
  5AEE  1820          		jr	PUTDDATA
                      	
                      	;append mode
  5AF0                	PUTDAPP:
  5AF0  78            		ld	a,b		;cluster
  5AF1  CD945D        		call	LASTSECT
  5AF4  B7            		or	a
  5AF5  2816          		jr	z,PUTDNEW
                      	
  5AF7  CB38          		srl	b		;track=cluster/2
  5AF9  3002          		jr	nc,PUTDNC1
  5AFB  C608          		add	a,08h
  5AFD                	PUTDNC1:
  5AFD  4F            		ld	c,a
  5AFE  CD005C        		call	RDNEXT2
                      	
  5B01  E1            		pop	hl
  5B02  E5            		push	hl
  5B03  77            		ld	(hl),a		;+6: position
  5B04  B7            		or	a
  5B05  2009          		jr	nz,PUTDDATA
                      	
  5B07  23            		inc	hl
  5B08  23            		inc	hl
  5B09  23            		inc	hl
  5B0A  CD825C        		call	SET0BUF
                      	
                      	;output mode
  5B0D                	PUTDOUT:
                      	
                      	;new sector
  5B0D                	PUTDNEW:
  5B0D  CDC65B        		call	NEXTSECTW3
                      	
  5B10                	PUTDDATA:
  5B10  E1            		pop	hl		;(BUFP)+6
  5B11  F1            		pop	af		;data
  5B12  1600          		ld	d,00h
  5B14  5E            		ld	e,(hl)		;+6: position in buffer
  5B15  34            		inc	(hl)		;z-flag
  5B16  23            		inc	hl
  5B17  CB8E          		res	1,(hl)
  5B19  CBDE          		set	3,(hl)		;+7: file status, bit3=write
  5B1B  23            		inc	hl
  5B1C  23            		inc	hl
  5B1D  19            		add	hl,de		;[+9,+264]: buffer
  5B1E  77            		ld	(hl),a
  5B1F  201C          		jr	nz,PUTDEND	;posision: ff->00?
                      	
                      	;write into disk
  5B21  2A39FB        		ld	hl,(BUFP)
  5B24  23            		inc	hl
  5B25  23            		inc	hl
  5B26  46            		ld	b,(hl)		;+2: cluster
  5B27  23            		inc	hl
  5B28  7E            		ld	a,(hl)		;+3: sector in cluster
  5B29  CB38          		srl	b		;track=cluster/2
  5B2B  3002          		jr	nc,PUTDNC2
  5B2D  C608          		add	a,08h
  5B2F                	PUTDNC2:
  5B2F  4F            		ld	c,a
  5B30  2A33FB        		ld	hl,(FATP)
  5B33  70            		ld	(hl),b		;+0: last accessed track
  5B34  CDC35C        		call	WRTBUF
  5B37  CDFC5B        		call	RDNEXT
  5B3A  DCBC5B        		call	c,NEXTSECTW2
                      	
  5B3D                	PUTDEND:
  5B3D  C1            		pop	bc
  5B3E  D1            		pop	de
  5B3F  E1            		pop	hl
  5B40  E3            		ex	(sp),hl		;cancel return to PUTDEV
  5B41  E1            		pop	hl
  5B42  C9            		ret
                      	
                      	
                      	;get data from disk (sequential read)
                      	;output: a=data, c-flag(1=no data)
                      	;destroy: f,de
  5B43                	GETDSK:
  5B43  E5            		push	hl
  5B44  C5            		push	bc
  5B45  2A39FB        		ld	hl,(BUFP)
  5B48  110700        		ld	de,0007h
  5B4B  19            		add	hl,de		;+7: file status
  5B4C  CB4E          		bit	1,(hl)		;to next sector?
  5B4E  280A          		jr	z,GETDZ
                      	
                      	;bit1=1 (to next sector)
  5B50  E5            		push	hl
  5B51  CD895B        		call	PUTD1A
  5B54  CDFC5B        		call	RDNEXT
  5B57  E1            		pop	hl
  5B58  381E          		jr	c,GETDEND	;no next sector
                      	
                      	;bit1(to next sector)=0
  5B5A                	GETDZ:
  5B5A  54            		ld	d,h
  5B5B  5D            		ld	e,l
  5B5C  2B            		dec	hl
  5B5D  4E            		ld	c,(hl)		;+6: position
  5B5E  34            		inc	(hl)
  5B5F  7E            		ld	a,(hl)
  5B60  23            		inc	hl
  5B61  CB46          		bit	0,(hl)		;+7: file status: binary file?
  5B63  2B            		dec	hl
  5B64  2B            		dec	hl
  5B65  2001          		jr	nz,GETDBIN	;if binary file
  5B67  BE            		cp	(hl)		;+5: end position
  5B68                	GETDBIN:
  5B68  0600          		ld	b,00h
  5B6A  09            		add	hl,bc
  5B6B  23            		inc	hl
  5B6C  23            		inc	hl
  5B6D  23            		inc	hl
  5B6E  23            		inc	hl
                      	
  5B6F  7E            		ld	a,(hl)		;[+9,+264]: buffer
  5B70  F5            		push	af
                      	
  5B71  2003          		jr	nz,GETDNZ	;if binary file or position<>end
                      	
                      	;position=end
  5B73  EB            		ex	de,hl
  5B74  CBCE          		set	1,(hl)		;+7: file status, bit1=to next sector
                      	
  5B76                	GETDNZ:
  5B76  F1            		pop	af
  5B77  B7            		or	a		;reset c-flag
  5B78                	GETDEND:
  5B78  C1            		pop	bc
  5B79  E1            		pop	hl
  5B7A  C9            		ret
                      	
                      	
                      	;get 2 byte from disk (sequential read)
                      	;output: hl=data
                      	;destroy: af,bc,de
  5B7B                	GETDSK2:
  5B7B  CD435B        		call	GETDSK
  5B7E  3804          		jr	c,GETDSK2ERR
  5B80  6F            		ld	l,a
  5B81  CD435B        		call	GETDSK
  5B84                	GETDSK2ERR:
  5B84  DACB03        		jp	c,FCERR
  5B87  67            		ld	h,a
  5B88  C9            		ret
                      	
                      	
                      	;put 1ah to disk and write buffer, if necessary
                      	;input: hl=buffer pointer+7
                      	;destroy: af,bc,de,hl
  5B89                	PUTD1A:
  5B89  CB5E          		bit	3,(hl)		;+7: file status, bit3=write
  5B8B  C8            		ret	z
  5B8C  CB9E          		res	3,(hl)
  5B8E  CB46          		bit	0,(hl)
  5B90  C0            		ret	nz
                      	
  5B91  E5            		push	hl
  5B92  11FCFF        		ld	de,0-0004h
  5B95  19            		add	hl,de
  5B96  7E            		ld	a,(hl)		;+3: sector in cluster
  5B97  2B            		dec	hl
  5B98  46            		ld	b,(hl)		;+2: cluster
  5B99  CB38          		srl	b		;track=cluster/2
  5B9B  3002          		jr	nc,PUTD1ANC
  5B9D  C608          		add	a,08h
  5B9F                	PUTD1ANC:
  5B9F  4F            		ld	c,a		;sector
  5BA0  C5            		push	bc
  5BA1  CD4953        		call	NEXTSECT
  5BA4  C1            		pop	bc
                      	
  5BA5  E1            		pop	hl
  5BA6  3011          		jr	nc,PUTD1AEND
                      	
  5BA8  23            		inc	hl
  5BA9  23            		inc	hl		;+9
  5BAA  C5            		push	bc
  5BAB  CDA55D        		call	ENDPOS
  5BAE  78            		ld	a,b
  5BAF  C1            		pop	bc
  5BB0  DAA556        		jp	c,REVFAT	;no data
                      	
  5BB3  B7            		or	a
  5BB4  2803          		jr	z,PUTD1AEND	;data full
                      	
  5BB6  23            		inc	hl
  5BB7  361A          		ld	(hl),1ah	;[+9,+264]: buffer
                      	
  5BB9                	PUTD1AEND:
  5BB9  C3C35C        		jp	WRTBUF
                      	
                      	
                      	;get track and sector for writing (search unused cluster if sector=8 or 16)
                      	; and write FAT buffer
                      	;output: b=next cluster, c=next sector in cluster
                      	;destroy: af,hl
  5BBC                	NEXTSECTW2:
  5BBC  2A39FB        		ld	hl,(BUFP)
  5BBF  010900        		ld	bc,0009h
  5BC2  09            		add	hl,bc
  5BC3  CD825C        		call	SET0BUF
                      	
  5BC6                	NEXTSECTW3:
  5BC6  2A39FB        		ld	hl,(BUFP)
  5BC9  23            		inc	hl
  5BCA  23            		inc	hl
  5BCB  46            		ld	b,(hl)		;+2: cluster
  5BCC  78            		ld	a,b
  5BCD  23            		inc	hl
  5BCE  4E            		ld	c,(hl)		;+3: sector in cluster(0-8)
                      	
  5BCF                	NEXTSECTW4:
  5BCF  CD1D54        		call	GETFAT
  5BD2  CB59          		bit	3,c
  5BD4  2018          		jr	nz,NEWCLST2	;if sector in cluster=8
                      	;same cluster
  5BD6  34            		inc	(hl)		;FAT
  5BD7  0C            		inc	c
                      	
  5BD8                	NXSCTWEND2:
  5BD8  CDF753        		call	SETSUM
  5BDB  23            		inc	hl
  5BDC  36FF          		ld	(hl),0ffh	;-2: need to overwrite FAT
                      	
  5BDE  2A39FB        		ld	hl,(BUFP)
  5BE1  23            		inc	hl
  5BE2  23            		inc	hl
  5BE3  70            		ld	(hl),b		;+2: cluster
  5BE4  23            		inc	hl
  5BE5  71            		ld	(hl),c		;+3: sector in cluster
  5BE6  23            		inc	hl
  5BE7  23            		inc	hl
  5BE8  3600          		ld	(hl),00h	;+5: end position
  5BEA  23            		inc	hl
  5BEB  3600          		ld	(hl),00h	;+6: position
  5BED  C9            		ret
                      	
                      	
                      	;new cluster
  5BEE                	NEWCLST2:
  5BEE  78            		ld	a,b		;old cluster
  5BEF  E5            		push	hl		;old cluster FAT address
  5BF0  CDD054        		call	SRCHCLST
  5BF3  36C1          		ld	(hl),0c1h
  5BF5  E1            		pop	hl		;old FAT buffer
  5BF6  77            		ld	(hl),a		;new cluster
  5BF7  47            		ld	b,a
  5BF8  0E01          		ld	c,01h		;sector in cluster
  5BFA  18DC          		jr	NXSCTWEND2
                      	
                      	
                      	;read next sector
                      	;output: a=end position, c-flag(1=no next sector)
                      	;destroy: af,bc,de,hl
  5BFC                	RDNEXT:
  5BFC  CD6253        		call	NEXTSECT2
  5BFF  D8            		ret	c
                      	
                      	;input: b=track, c=sector
  5C00                	RDNEXT2:
  5C00  CDA15C        		call	READBUF
  5C03  E5            		push	hl
                      	
  5C04  79            		ld	a,c
  5C05  C6F7          		add	a,0f7h
  5C07  CB10          		rl	b		;cluster=track*2+(1 if sector>=9)
  5C09  E607          		and	07h
  5C0B  3C            		inc	a		;sector in cluster
  5C0C  4F            		ld	c,a
                      	
  5C0D  2A39FB        		ld	hl,(BUFP)
  5C10  23            		inc	hl
  5C11  23            		inc	hl
  5C12  70            		ld	(hl),b		;+2: cluster
  5C13  23            		inc	hl
  5C14  71            		ld	(hl),c		;+3: sector in cluster
  5C15  23            		inc	hl
  5C16  23            		inc	hl
  5C17  AF            		xor	a
  5C18  77            		ld	(hl),a		;+5: end position
  5C19  E5            		push	hl
  5C1A  23            		inc	hl
  5C1B  77            		ld	(hl),a		;+6: position
  5C1C  23            		inc	hl
  5C1D  CB8E          		res	1,(hl)		;+7: file status, bit1=to next sector
                      	
  5C1F  C5            		push	bc
  5C20  CD6253        		call	NEXTSECT2
  5C23  C1            		pop	bc		;c=sector in cluster
                      	
  5C24  D1            		pop	de		;de=buffer pointer+5
  5C25  E1            		pop	hl		;hl=buffer pointer+9
  5C26  3E00          		ld	a,00h
  5C28  D0            		ret	nc
                      	
  5C29  CDA55D        		call	ENDPOS
  5C2C  78            		ld	a,b
  5C2D  12            		ld	(de),a		;+5: end position
                      	
  5C2E  B7            		or	a		;reset c-flag
  5C2F  C9            		ret
                      	
                      	
                      	;DSKI$() function
  5C30                	F_DSKI:
  5C30  BF            		cp	a		;set z-flag
  5C31  CD475C        		call	DSKIOSUB
  5C34  CD955C        		call	READBUF0
  5C37  3EFF          		ld	a,0ffh
  5C39  CD4227        		call	MAKESTR
  5C3C  C39027        		jp	INKYEND
                      	
                      	
                      	;DSKO$ command
  5C3F                	C_DSKO:
  5C3F  AF            		xor	a
  5C40  3C            		inc	a		;reset z-flag
  5C41  CD475C        		call	DSKIOSUB
  5C44  C3BA5C        		jp	WRTBUF0
                      	
                      	
                      	;subroutine for DSKI$ and DSKO$
                      	;input: hl=program address, z-flag(1=DSKI$,0=DSKO$)
  5C47                	DSKIOSUB:
  5C47  F5            		push	af		;z-flag(1=DSKI$,0=DSKO$)
  5C48  CCB50B        		call	z,CHKLPAR
  5C4B  CDE40D        		call	INT1ARG		;a,e=drive(1-4)
  5C4E  D5            		push	de
  5C4F  7E            		ld	a,(hl)
  5C50  FE2C          		cp	','
  5C52  C2C203        		jp	nz,SNERR
  5C55  23            		inc	hl
  5C56  CD2D0E        		call	INT1ARG2	;c=track(0-34),e=sector(1-16)
  5C59  41            		ld	b,c		;b=track
  5C5A  4B            		ld	c,e		;c=sector
  5C5B  D1            		pop	de		;e=drive(1-4)
  5C5C  F1            		pop	af		;z-flag(1=DSKI$,0=DSKO$)
  5C5D  CCC40B        		call	z,CHKRPAR
  5C60  7B            		ld	a,e		;drive(1-4)
  5C61  3D            		dec	a
  5C62  213BFB        		ld	hl,DRIVES
  5C65  BE            		cp	(hl)
  5C66  D26042        		jp	nc,DNERR
                      	
  5C69  F5            		push	af		;drive-1(0-3)
  5C6A  78            		ld	a,b
  5C6B  FE23          		cp	TRACKS		;track(0-34)
  5C6D  D26342        		jp	nc,TSERR
  5C70  79            		ld	a,c
  5C71  3D            		dec	a
  5C72  FE10          		cp	10h		;sector(1-16)
  5C74  D26342        		jp	nc,TSERR
  5C77  F1            		pop	af		;drive-1(0-3)
                      	
  5C78  CDE15C        		call	SETIX
  5C7B  DD7700        		ld	(ix+00h),a	;drive-1(0-3)
  5C7E  C9            		ret
                      	
                      	
                      	;fill buffer0 to 00h
                      	;output: hl=(BUF0)
                      	;destroy: af,b
  5C7F                	SET0BUF0:
  5C7F  2A37FB        		ld	hl,(BUF0)
                      	
                      	;set buffer to 00h
                      	;input: hl=buffer address
                      	;destroy: af,b
  5C82                	SET0BUF:
  5C82  AF            		xor	a
  5C83  47            		ld	b,a
  5C84                	SET0BUFLP:
  5C84  77            		ld	(hl),a
  5C85  23            		inc	hl
  5C86  10FC          		djnz	SET0BUFLP
  5C88  25            		dec	h
  5C89  C9            		ret
                      	
                      	
                      	;write directory in buffer#0
                      	;input: ix, (ix+00h)=drive-1
                      	;output: hl=(BUF0)
                      	;destroy: af,bc,de
  5C8A                	WRTDIR:
  5C8A  0612          		ld	b,12h		;track
  5C8C  DD4E02        		ld	c,(ix+02h)	;sector written in SRCHFILE
  5C8F  CDBA5C        		call	WRTBUF0
  5C92  C37F5C        		jp	SET0BUF0	;hl=(BUF0)
                      	
                      	
                      	;read 1 sector into buffer#0
                      	;input: b=track, c=sector, ix, (ix+00h)=drive-1
                      	;output: hl=(BUF0)
                      	;destroy: af,b,de
  5C95                	READBUF0:
  5C95  2A37FB        		ld	hl,(BUF0)
  5C98  E5            		push	hl
  5C99  CDA85C        		call	RDBFMAIN1
  5C9C  CD8056        		call	CHKCLOSE
  5C9F  E1            		pop	hl
  5CA0  C9            		ret
                      	
                      	
                      	;read 1 sector into file buffer
                      	;input: b=track, c=sector, ix, (ix+00h)=drive-1, (BUFP)=file buffer pointer
                      	;output: hl=buffer pointer+9
                      	;destroy: af,de
  5CA1                	READBUF:
  5CA1  2A39FB        		ld	hl,(BUFP)
                      	
                      	;input: b=track, c=sector, ix, (ix+00h)=drive-1, hl=file buffer pointer
  5CA4                	READBUF2:
  5CA4  110900        		ld	de,0009h
  5CA7  19            		add	hl,de
                      	
                      	;read 1 sector
                      	;input: b=track, c=sector, ix, (ix+00h)=drive-1, hl=address
                      	;destroy: af
  5CA8                	RDBFMAIN1:
  5CA8  3E01          		ld	a,01h		;1 sector
                      	
                      	;read disk with error check and change (FATP)
                      	;input:
                      	; a=the number of sector, b=track, c=sector, hl=address
                      	; ix=work address, (ix+00h)=drive-1
                      	;destroy: f,de
  5CAA                	RDBFMAIN:
  5CAA  E5            		push	hl
  5CAB  54            		ld	d,h
  5CAC  5D            		ld	e,l
  5CAD  B7            		or	a		;reset z- and c-flag for reading
  5CAE  CDFD5C        		call	DISK2
  5CB1  DA5D42        		jp	c,ATERR
  5CB4  2A33FB        		ld	hl,(FATP)
  5CB7  70            		ld	(hl),b		;+0: last accessed track
  5CB8  E1            		pop	hl
  5CB9  C9            		ret
                      	
                      	
                      	;write 1 sector in buffer#0 into disk
                      	;input: b=track, c=sector, ix, (ix+00h)=drive-1
                      	;output: hl=(FATP)
                      	;destroy: af,de
  5CBA                	WRTBUF0:
  5CBA  2A37FB        		ld	hl,(BUF0)
  5CBD  CDCA5C        		call	WRTBFMAIN1
  5CC0  C38056        		jp	CHKCLOSE
                      	
                      	
                      	;write 1 sector in buffer into disk
                      	;input: b=track, c=sector, ix, (ix+00h)=drive-1, (BUFP)=buffer pointer
                      	;output: hl=(FATP)
                      	;destroy: af,de,hl
  5CC3                	WRTBUF:
  5CC3  2A39FB        		ld	hl,(BUFP)
  5CC6  110900        		ld	de,0009h
  5CC9  19            		add	hl,de
                      	
                      	;write 1 sector into disk
                      	;input: b=track, c=sector, ix, (ix+00h)=drive-1, hl=address
                      	;output: hl=(FATP)
                      	;destroy: af,de
  5CCA                	WRTBFMAIN1:
  5CCA  3E01          		ld	a,01h
                      	
                      	;write disk with error check and change (FATP)
                      	;input:
                      	; a=the number of sector, b=track, c=sector, hl=address
                      	; ix=work address, (ix+00h)=drive-1
                      	;output: hl=(FATP)
                      	;destroy: f,de
  5CCC                	WRTBFMAIN:
  5CCC  37            		scf			;set c-flag for writing
  5CCD  CDFD5C        		call	DISK2
  5CD0  DA5D42        		jp	c,ATERR
  5CD3  2A33FB        		ld	hl,(FATP)
  5CD6  70            		ld	(hl),b		;+0: last accessed track
  5CD7  C9            		ret
                      	
                      	
                      	;cluster -> track, sector
                      	;input: a=cluster
                      	;output: b=track, c=sector
                      	;destroy: f
  5CD8                	C2TRSECT:
  5CD8  47            		ld	b,a
  5CD9  CB38          		srl	b		;track=cluster/2
  5CDB  0E01          		ld	c,01h
  5CDD  D0            		ret	nc
  5CDE  0E09          		ld	c,09h
  5CE0  C9            		ret
                      	
                      	
                      	;get ix for disk
                      	;output: ix=disk work address
                      	;destroy: none
  5CE1                	SETIX:
  5CE1  F5            		push	af
  5CE2  DD21D2F9      		ld	ix,0f9d2h
  5CE6  CD9539        		call	CHKMOD
  5CE9  280C          		jr	z,SETIXEND	;mode=5
  5CEB  3A8CFD        		ld	a,(PAGES)
  5CEE  3D            		dec	a
  5CEF  2806          		jr	z,SETIXEND	;pages=1
  5CF1  0F            		rrca			;pages>1
  5CF2  0F            		rrca
  5CF3  0F            		rrca			;--,20,40,60
  5CF4  2F            		cpl			;--,df,bf,9f
  5CF5  DD            		db	0ddh		;ix prefix
  5CF6  67            		ld	h,a		;ld ixh,a
  5CF7                	SETIXEND:
  5CF7  F1            		pop	af
  5CF8  C9            		ret
                      	
                      	
                      	;read/write disk
                      	;input:
                      	; a=the number of sector, b=track, c=sector, hl=address
                      	; ix=work address, (ix+00h)=drive-1
                      	; c-flag=0,z-flag=0: read
                      	; c-flag=0,z-flag=1: check
                      	; c-flag=1: write
                      	;output: c-flag(1=FAT error)
                      	;destroy: f,de
  5CF9                	_DISK2:	ds	DISK2-_DISK2
  5CFD                		org	DISK2
                      	
  5CFD  DD361B00      		ld	(ix+1bh),00h	;error count
  5D01  F5            		push	af
  5D02                	DISK2LP:
  5D02  F1            		pop	af
  5D03  54            		ld	d,h
  5D04  5D            		ld	e,l
  5D05  F5            		push	af
  5D06  E5            		push	hl
  5D07  CD7442        		call	DISK
  5D0A  E1            		pop	hl
  5D0B  38F5          		jr	c,DISK2LP
  5D0D  F1            		pop	af
  5D0E  B7            		or	a
  5D0F  C9            		ret
                      	
                      	
                      	;DSKF() function
  5D10                	F_DSKF:
  5D10  CD860B        		call	FNCNUM
  5D13  CD4C07        		call	FTOI1
  5D16  7B            		ld	a,e
  5D17  3D            		dec	a
  5D18  CDC853        		call	READFAT
  5D1B  2A33FB        		ld	hl,(FATP)
  5D1E  23            		inc	hl
  5D1F  23            		inc	hl
  5D20  23            		inc	hl
                      	
  5D21  010046        		ld	bc,CLSTS*100h+00h
  5D24                	DSKFLP:
  5D24  7E            		ld	a,(hl)
  5D25  3C            		inc	a
  5D26  2001          		jr	nz,DSKFNZ	;ffh=unused
  5D28  0C            		inc	c
  5D29                	DSKFNZ:
  5D29  23            		inc	hl
  5D2A  10F8          		djnz	DSKFLP
                      	
  5D2C  69            		ld	l,c
  5D2D  C37E0C        		jp	I1TOF
                      	
                      	
                      	;LOC() function
  5D30                	F_LOC:
  5D30  CD2252        		call	FNCFNUM
  5D33  CD395D        		call	LOC
  5D36  C3800C        		jp	I2TOF
                      	
                      	;calculate LOC
                      	;input: hl=buffer pointer
                      	;output: hl
                      	;destroy: af,bc
  5D39                	LOC:
  5D39  23            		inc	hl
  5D3A  7E            		ld	a,(hl)		;+1: first cluster
  5D3B  23            		inc	hl
  5D3C  4E            		ld	c,(hl)		;+2: cluster
  5D3D  E5            		push	hl
  5D3E  CD995A        		call	CNTCLST2
  5D41  E1            		pop	hl
  5D42  23            		inc	hl
  5D43  4E            		ld	c,(hl)		;+3: sector in cluster
                      	;	jr	HLA8C
                      	
                      	;hl=a*8+c
                      	;input: a,c
                      	;output: hl
                      	;destroy: b
  5D44                	HLA8C:
  5D44  2600          		ld	h,00h
  5D46  44            		ld	b,h
  5D47  6F            		ld	l,a
  5D48  29            		add	hl,hl		;*2
  5D49  29            		add	hl,hl		;*4
  5D4A  29            		add	hl,hl		;*8
  5D4B  09            		add	hl,bc
  5D4C  C9            		ret
                      	
                      	
                      	;LOF() function
  5D4D                	F_LOF:
  5D4D  CD2252        		call	FNCFNUM
  5D50  23            		inc	hl
  5D51  7E            		ld	a,(hl)		;+1: first cluster
  5D52  CD975A        		call	CNTCLST
  5D55  CD445D        		call	HLA8C
  5D58  C3800C        		jp	I2TOF
                      	
                      	
                      	;EOF() function
  5D5B                	F_EOF:
  5D5B  CD2252        		call	FNCFNUM
  5D5E  CD675D        		call	CHKEOF
  5D61  C2420C        		jp	nz,SETFALSE
  5D64  C3530C        		jp	SETTRUE
                      	
                      	
                      	;check if end of file
                      	;input: hl=buffer pointer
                      	;output: z-flag(1=EOF)
                      	;destroy: af,bc,de,hl
  5D67                	CHKEOF:
  5D67  23            		inc	hl
  5D68  23            		inc	hl
  5D69  46            		ld	b,(hl)		;+2: cluster
  5D6A  23            		inc	hl
  5D6B  23            		inc	hl
  5D6C  23            		inc	hl
  5D6D  7E            		ld	a,(hl)		;+5: end position
  5D6E  23            		inc	hl
  5D6F  B7            		or	a
  5D70  2802          		jr	z,CHKEOFZ	;continue to next sector
  5D72                	EOFEND:
  5D72  BE            		cp	(hl)		;+6: position
  5D73  C9            		ret
                      	
  5D74                	CHKEOFZ:
  5D74  BE            		cp	(hl)		;+6: position
  5D75  C0            		ret	nz
  5D76  E5            		push	hl
  5D77  78            		ld	a,b
  5D78  CD945D        		call	LASTSECT
  5D7B  E1            		pop	hl
  5D7C  B7            		or	a
  5D7D  C8            		ret	z
                      	
  5D7E  2B            		dec	hl
  5D7F  2B            		dec	hl
  5D80  2B            		dec	hl
  5D81  BE            		cp	(hl)		;+3: sector in cluster
  5D82  C0            		ret	nz
                      	
  5D83  2B            		dec	hl
  5D84  78            		ld	a,b
  5D85  BE            		cp	(hl)		;+2: cluster
  5D86  C9            		ret
                      	
                      	
                      	;input: hl=buffer pointer+2
                      	;output: z-flag (1=last sector)
                      	;destroy: af,bc,hl
  5D87                	CHKLASTSECT:
  5D87  7E            		ld	a,(hl)		;+2: cluster
  5D88  E5            		push	hl
  5D89  CD945D        		call	LASTSECT
  5D8C  E1            		pop	hl
  5D8D  23            		inc	hl
  5D8E  BE            		cp	(hl)		;+3: sector in cluster
  5D8F  C0            		ret	nz
  5D90  78            		ld	a,b
  5D91  2B            		dec	hl
  5D92  BE            		cp	(hl)		;+2: cluster
  5D93  C9            		ret
                      	
                      	
                      	;get the last sector of file
                      	;input: a=cluster
                      	;output: a=sector in cluster(0-8), b=cluster, hl=(FATP)+3+cluster
                      	;destroy: f,c
  5D94                	LASTSECT:
  5D94  F5            		push	af		;a=cluster
  5D95  CD1D54        		call	GETFAT
  5D98  C1            		pop	bc		;b=cluster
  5D99  FEC0          		cp	0c0h
  5D9B  38F7          		jr	c,LASTSECT
  5D9D  D6C0          		sub	0c0h
  5D9F  FE09          		cp	09h
  5DA1  D8            		ret	c
  5DA2  C36F42        		jp	FWERR
                      	
                      	
                      	;get the end position in sector
                      	;input: hl=buffer pointer+9
                      	;output: b=position of the last data, hl=address, c-flag(1=no data)
                      	;destroy: af
  5DA5                	ENDPOS:
  5DA5  24            		inc	h
  5DA6  2B            		dec	hl
                      	
  5DA7  0600          		ld	b,00h
  5DA9                	ENDPLP:
  5DA9  7E            		ld	a,(hl)
  5DAA  FE1A          		cp	1ah
  5DAC  2807          		jr	z,ENDPZ
  5DAE  B7            		or	a
  5DAF  C0            		ret	nz
  5DB0  2B            		dec	hl
  5DB1  10F6          		djnz	ENDPLP
  5DB3  37            		scf
  5DB4  C9            		ret
                      	
  5DB5                	ENDPZ:
  5DB5  05            		dec	b
  5DB6  2B            		dec	hl
                      	;	or	a		;reset c-flag
  5DB7  C9            		ret
                      	
                      	
                      	;BSAVE command
  5DB8                	C_BSV:
  5DB8  CD5E5F        		call	SETFNAME
  5DBB  F5            		push	af		;drive-1
  5DBC  CD7B3F        		call	CHKCLCM
  5DBF  CAC203        		jp	z,SNERR
  5DC2  CD1D0E        		call	INTARG2		;bc,de
                      	
  5DC5  F1            		pop	af		;drive-1
  5DC6  D5            		push	de		;size
  5DC7  C5            		push	bc		;start address
  5DC8  2601          		ld	h,01h		;attribute
  5DCA  CD395F        		call	SVCOMMON
  5DCD  D1            		pop	de		;start address
  5DCE  2A37FB        		ld	hl,(BUF0)
  5DD1  73            		ld	(hl),e
  5DD2  23            		inc	hl
  5DD3  72            		ld	(hl),d
                      	
  5DD4  E1            		pop	hl		;size
  5DD5  E5            		push	hl		;size
  5DD6  19            		add	hl,de		;start address + size
  5DD7  D5            		push	de		;start address
  5DD8  EB            		ex	de,hl
  5DD9  2A37FB        		ld	hl,(BUF0)
  5DDC  23            		inc	hl
  5DDD  23            		inc	hl
  5DDE  73            		ld	(hl),e
  5DDF  23            		inc	hl
  5DE0  72            		ld	(hl),d
  5DE1  D1            		pop	de		;start address
  5DE2  E1            		pop	hl		;size
  5DE3  C5            		push	bc		;track, sector
                      	
  5DE4  7C            		ld	a,h
  5DE5  B5            		or	l
  5DE6  2828          		jr	z,BSVZ1
                      	
  5DE8  44            		ld	b,h
  5DE9  4D            		ld	c,l
                      	
  5DEA  2A37FB        		ld	hl,(BUF0)
  5DED  23            		inc	hl
  5DEE  23            		inc	hl
  5DEF  23            		inc	hl
  5DF0  23            		inc	hl
                      	
  5DF1  3EFB          		ld	a,0fbh
                      	
                      	;de=data address, hl=buffer address, bc=total size, a=ldir size-1
  5DF3                	BSVLP1:
  5DF3  C5            		push	bc		;size
  5DF4  04            		inc	b
  5DF5  05            		dec	b
  5DF6  2003          		jr	nz,BSVNZ2
  5DF8  B9            		cp	c
  5DF9  3004          		jr	nc,BSVNC1
  5DFB                	BSVNZ2:
  5DFB  4F            		ld	c,a
  5DFC  0600          		ld	b,00h
  5DFE  03            		inc	bc		;00fch or 0100h
  5DFF                	BSVNC1:
  5DFF  C5            		push	bc		;ldir size
  5E00  EB            		ex	de,hl
  5E01  CD93FE        		call	LDIRRAM
  5E04  EB            		ex	de,hl		;de <- data address, hl <- buffer address
  5E05  C1            		pop	bc		;ldir size
  5E06  E1            		pop	hl		;size
  5E07  AF            		xor	a
  5E08  ED42          		sbc	hl,bc
  5E0A  3002          		jr	nc,BSVNC2
  5E0C  67            		ld	h,a		;hl=0000h
  5E0D  6F            		ld	l,a
  5E0E                	BSVNC2:
  5E0E  C1            		pop	bc		;b=track, c=sector
  5E0F  C5            		push	bc		;b=track, c=sector
  5E10                	BSVZ1:
  5E10  E5            		push	hl		;size
  5E11  D5            		push	de		;data address
  5E12  CDBA5C        		call	WRTBUF0
                      	
                      	;FAT
  5E15  CD1854        		call	GETFAT2
  5E18  79            		ld	a,c		;sector=1-16
  5E19  3D            		dec	a		;0-15
  5E1A  E607          		and	07h		;0-7
  5E1C  C6C1          		add	a,0c1h		;c1-c8
  5E1E  77            		ld	(hl),a
  5E1F  CDF753        		call	SETSUM
  5E22  CD7F5C        		call	SET0BUF0
                      	
                      	;end?
  5E25  D1            		pop	de		;data address
  5E26  C1            		pop	bc		;size
  5E27  78            		ld	a,b
  5E28  B1            		or	c
  5E29  2816          		jr	z,BSVEND
                      	
                      	;next sector
  5E2B  60            		ld	h,b
  5E2C  69            		ld	l,c
  5E2D  C1            		pop	bc		;b=track, c=sector
  5E2E  79            		ld	a,c
  5E2F  C6F7          		add	a,0f7h
  5E31  78            		ld	a,b
  5E32  8F            		adc	a,a		;cluster=track*2+(1 if sector>=9)
  5E33  5F            		ld	e,a
  5E34  CD9053        		call	NEXTSECTW
                      	
  5E37  C5            		push	bc		;b=track, c=sector
  5E38  44            		ld	b,h
  5E39  4D            		ld	c,l
  5E3A  2A37FB        		ld	hl,(BUF0)
  5E3D  3EFF          		ld	a,0ffh
  5E3F  18B2          		jr	BSVLP1
                      	
  5E41                	BSVEND:
  5E41  C1            		pop	bc		;track, sector
  5E42  C36A56        		jp	WRTFAT
                      	
                      	
                      	;BLOAD command
  5E45                	C_BLD:
  5E45  AF            		xor	a
  5E46  CD5E5F        		call	SETFNAME	;default: no R option
  5E49  F5            		push	af		;drive-1
  5E4A  115D42        		ld	de,ATERR
  5E4D  D5            		push	de		;dummy (start address)
                      	
  5E4E  DD361901      		ld	(ix+19h),01h	;1=start address is not specified
                      	
  5E52  CD7B3F        		call	CHKCLCM
  5E55  282B          		jr	z,BLDMAIN
  5E57  CD603F        		call	CHKCLN
  5E5A  CAF803        		jp	z,MOERR
  5E5D  FE52          		cp	'R'
  5E5F  2818          		jr	z,BLDZ
  5E61  CD0B0E        		call	INTARG
  5E64  DD3619FF      		ld	(ix+19h),0ffh	;ff=start address is specified
  5E68  F1            		pop	af		;cancel stack
  5E69  D5            		push	de		;start address
  5E6A  CD7B3F        		call	CHKCLCM
  5E6D  2813          		jr	z,BLDMAIN
  5E6F  CD593F        		call	SKIPSP
  5E72  E6DF          		and	0dfh
  5E74  FE52          		cp	'R'
  5E76  C2C203        		jp	nz,SNERR
  5E79                	BLDZ:
  5E79  DD7703        		ld	(ix+03h),a	;R option
  5E7C  CD5F3F        		call	CHKCLNINC
  5E7F  C2C203        		jp	nz,SNERR
                      	
  5E82                	BLDMAIN:
  5E82  224EFF        		ld	(PROGAD),hl
                      	
  5E85  D1            		pop	de		;start address
  5E86  F1            		pop	af		;drive-1
  5E87  D5            		push	de		;start address
  5E88  CDC853        		call	READFAT
  5E8B  CD3A54        		call	SRCHFILE
  5E8E  C24242        		jp	nz,FFERR
                      	
  5E91  CDD85C        		call	C2TRSECT
  5E94  CD1D54        		call	GETFAT
  5E97  FEC0          		cp	0c0h
  5E99  CA2B5F        		jp	z,BLDEND2
  5E9C  D1            		pop	de		;start address
  5E9D  C5            		push	bc		;b=track, c=sector
  5E9E  F5            		push	af		;FAT value
  5E9F  D5            		push	de		;start address
  5EA0  CD955C        		call	READBUF0	;hl=(BUF0)
                      	
  5EA3  5E            		ld	e,(hl)		;de=first address
  5EA4  23            		inc	hl
  5EA5  56            		ld	d,(hl)
  5EA6  23            		inc	hl
  5EA7  7E            		ld	a,(hl)		;last address+1
  5EA8  23            		inc	hl
  5EA9  93            		sub	e
  5EAA  4F            		ld	c,a
  5EAB  7E            		ld	a,(hl)
  5EAC  23            		inc	hl
  5EAD  9A            		sbc	a,d
  5EAE  47            		ld	b,a		;bc=size
                      	
  5EAF  DD7E19        		ld	a,(ix+19h)
  5EB2  3C            		inc	a
  5EB3  2002          		jr	nz,BLDNZ1	;start address is not specified
  5EB5  D1            		pop	de		;start address
  5EB6  F5            		push	af		;dummy
                      	
  5EB7                	BLDNZ1:
  5EB7  F1            		pop	af		;cancel
  5EB8  F1            		pop	af		;FAT value
  5EB9  D5            		push	de		;start address
  5EBA  F5            		push	af		;FAT value
                      	
  5EBB  78            		ld	a,b
  5EBC  B1            		or	c
  5EBD  CA325F        		jp	z,BLDEND4	;size=0
                      	
  5EC0  C5            		push	bc		;size
                      	
  5EC1  3EFC          		ld	a,100h-4
  5EC3  04            		inc	b
  5EC4  05            		dec	b
  5EC5  2003          		jr	nz,BLDNZ2	;size >= 100h
  5EC7  B9            		cp	c
  5EC8  3003          		jr	nc,BLDNC	;size <= 100h-4
  5ECA                	BLDNZ2:
  5ECA  0600          		ld	b,00h
  5ECC  4F            		ld	c,a
  5ECD                	BLDNC:
  5ECD  C5            		push	bc		;ldir size
  5ECE  EDB0          		ldir
  5ED0  C1            		pop	bc		;ldir size
  5ED1  E1            		pop	hl		;size
  5ED2  B7            		or	a
  5ED3  ED42          		sbc	hl,bc		;size -= ldir size
  5ED5  F1            		pop	af		;FAT value
  5ED6  FEC1          		cp	0c1h
  5ED8  285B          		jr	z,BLDEND5
  5EDA  F1            		pop	af		;start address
  5EDB  C1            		pop	bc		;b=track, c=sector
  5EDC  F5            		push	af		;start address
  5EDD  0C            		inc	c
  5EDE  C5            		push	bc		;b=track, c=sector
                      	
  5EDF  0607          		ld	b,07h		;max sectors
  5EE1                	BLDLP:
  5EE1  24            		inc	h
  5EE2  25            		dec	h
  5EE3  2838          		jr	z,BLDEND1	;size < 100h
                      	
  5EE5  D6C0          		sub	0c0h
  5EE7  3809          		jr	c,BLDC1
  5EE9  80            		add	a,b
  5EEA  D608          		sub	08h
  5EEC  BC            		cp	h
  5EED  3808          		jr	c,BLDC2
  5EEF  7C            		ld	a,h
  5EF0  1805          		jr	BLDC2
  5EF2                	BLDC1:
  5EF2  7C            		ld	a,h
  5EF3  B8            		cp	b
  5EF4  3801          		jr	c,BLDC2
  5EF6  78            		ld	a,b
  5EF7                	BLDC2:
  5EF7  C1            		pop	bc		;b=track, c=sector
                      	
  5EF8  EB            		ex	de,hl
  5EF9  D5            		push	de
  5EFA  CDAA5C        		call	RDBFMAIN
  5EFD  D1            		pop	de
  5EFE  EB            		ex	de,hl
                      	
  5EFF  C5            		push	bc		;b=track, c=sector
  5F00  47            		ld	b,a		;sectors
  5F01  82            		add	a,d
  5F02  57            		ld	d,a		;address += sectors*100h
                      	
  5F03  7C            		ld	a,h
  5F04  90            		sub	b
  5F05  67            		ld	h,a		;size -= sectors*100h
  5F06  B5            		or	l
  5F07  78            		ld	a,b		;sectors
  5F08  C1            		pop	bc		;b=track, c=sector
  5F09  2820          		jr	z,BLDEND2	;size=0
                      	
  5F0B  81            		add	a,c
  5F0C  3D            		dec	a
  5F0D  4F            		ld	c,a		;last accessed sector
  5F0E  CD4953        		call	NEXTSECT
  5F11  3818          		jr	c,BLDEND2
  5F13  C5            		push	bc		;b=track, c=sector
  5F14  E5            		push	hl		;size
  5F15  CD1854        		call	GETFAT2
  5F18  E1            		pop	hl		;size
  5F19  0608          		ld	b,08h		;max sectors
  5F1B  18C4          		jr	BLDLP
                      	
  5F1D                	BLDEND1:
  5F1D  C1            		pop	bc		;b=track, c=sector
  5F1E  7C            		ld	a,h
  5F1F  B5            		or	l
  5F20  2809          		jr	z,BLDEND2	;size=0
  5F22  E5            		push	hl		;size
  5F23  D5            		push	de		;address
  5F24  CD955C        		call	READBUF0	;hl=(BUF0)
  5F27  D1            		pop	de		;address
  5F28  C1            		pop	bc		;size
  5F29  EDB0          		ldir
  5F2B                	BLDEND2:
  5F2B  E1            		pop	hl		;start address
  5F2C                	BLDEND3:
  5F2C  DD7E03        		ld	a,(ix+03h)	;R option?
  5F2F  B7            		or	a
  5F30  C8            		ret	z
  5F31  E9            		jp	(hl)
                      	
  5F32                	BLDEND4:
  5F32  F1            		pop	af		;FAT value
  5F33  18F6          		jr	BLDEND2
                      	
  5F35                	BLDEND5:
  5F35  E1            		pop	hl		;start address
  5F36  C1            		pop	bc		;b=track, c=sector
  5F37  18F3          		jr	BLDEND3
                      	
                      	
                      	;save/bsave common subroutine
                      	;set file name in directory, write directory, get track and sector
                      	;input: a=drive-1(0-3), h=attribute
                      	;output: a=cluster, b=track, c=sector, (FATP)
                      	;destroy: f,de,hl
  5F39                	SVCOMMON:
  5F39  E5            		push	hl
  5F3A  CD9E58        		call	SETDIR
  5F3D  E1            		pop	hl
  5F3E  EB            		ex	de,hl
  5F3F  72            		ld	(hl),d		;attribute
  5F40  23            		inc	hl
  5F41  77            		ld	(hl),a		;cluster
  5F42  F5            		push	af		;cluster
  5F43  CD8A5C        		call	WRTDIR
  5F46  F1            		pop	af		;cluster
                      	
  5F47  2A33FB        		ld	hl,(FATP)
  5F4A  0600          		ld	b,00h
  5F4C  4F            		ld	c,a
  5F4D  0C            		inc	c
  5F4E  0C            		inc	c
  5F4F  0C            		inc	c
  5F50  09            		add	hl,bc
  5F51  36C0          		ld	(hl),0c0h	;(FATP)+3+cluster
                      	
  5F53  F5            		push	af
  5F54  CDF753        		call	SETSUM
  5F57  CD6A56        		call	WRTFAT
  5F5A  F1            		pop	af
                      	
  5F5B  C3D85C        		jp	C2TRSECT
                      	
                      	
                      	;read string and set file name in (ix+5)...(ix+13) and set R option in (ix+3)
                      	;input: a=R option?, hl=program address
                      	;output: a=drive-1(0-3), hl=next address, ix=disk work
                      	;destroy: f,bc,de
  5F5E                	SETFNAME:
  5F5E  CDE15C        		call	SETIX
  5F61  DD7703        		ld	(ix+03h),a	;R option?
  5F64  CD8126        		call	STRARG
  5F67  B7            		or	a
  5F68  CA5742        		jp	z,NMERR
  5F6B  E5            		push	hl
                      	
                      	;get drive number
  5F6C  0600          		ld	b,00h		;default drive
  5F6E  4F            		ld	c,a		;length
  5F6F  FE03          		cp	02h+1
  5F71  381A          		jr	c,SETFC
  5F73  13            		inc	de
  5F74  1A            		ld	a,(de)
  5F75  1B            		dec	de
  5F76  FE3A          		cp	':'
  5F78  2013          		jr	nz,SETFC
  5F7A  1A            		ld	a,(de)
  5F7B  D631          		sub	'1'
  5F7D  FE08          		cp	08h
  5F7F  D26042        		jp	nc,DNERR	;1-8=ok
  5F82  47            		ld	b,a		;drive(0-3)
  5F83  0D            		dec	c
  5F84  0D            		dec	c
  5F85  13            		inc	de
  5F86  13            		inc	de
  5F87  1A            		ld	a,(de)
  5F88  FE2E          		cp	'.'
  5F8A  CA5742        		jp	z,NMERR
                      	
  5F8D                	SETFC:
  5F8D  3A3BFB        		ld	a,(DRIVES)
  5F90  3D            		dec	a
  5F91  FA4842        		jp	m,AOERR
  5F94  B8            		cp	b
  5F95  DA6042        		jp	c,DNERR
  5F98  C5            		push	bc		;b=drive(0-3)
  5F99  2AFCFF        		ld	hl,(FATPTBL)
  5F9C  2ED7          		ld	l,0d7h		;ix+5
                      	
                      	;before dot
  5F9E  0C            		inc	c
  5F9F  0606          		ld	b,06h
  5FA1                	SETFLP1:
  5FA1  0D            		dec	c
  5FA2  2817          		jr	z,SETFLP2
  5FA4  1A            		ld	a,(de)
  5FA5  13            		inc	de
  5FA6  FE2E          		cp	'.'
  5FA8  2810          		jr	z,SETFZ1
  5FAA  77            		ld	(hl),a
  5FAB  23            		inc	hl
  5FAC  10F3          		djnz	SETFLP1
                      	
  5FAE  0D            		dec	c
  5FAF  280F          		jr	z,SETFZ2
  5FB1  1A            		ld	a,(de)
  5FB2  FE2E          		cp	'.'
  5FB4  200A          		jr	nz,SETFZ2
  5FB6  13            		inc	de
  5FB7  0D            		dec	c
  5FB8  1806          		jr	SETFZ2
                      	
  5FBA                	SETFZ1:
  5FBA  0D            		dec	c
  5FBB                	SETFLP2:
  5FBB  3620          		ld	(hl),' '
  5FBD  23            		inc	hl
  5FBE  10FB          		djnz	SETFLP2
                      	
                      	;after dot
  5FC0                	SETFZ2:
  5FC0  0C            		inc	c
  5FC1  0603          		ld	b,03h
  5FC3                	SETFLP3:
  5FC3  0D            		dec	c
  5FC4  280E          		jr	z,SETFLP4
  5FC6  1A            		ld	a,(de)
  5FC7  13            		inc	de
  5FC8  FE2E          		cp	'.'
  5FCA  CA5742        		jp	z,NMERR
  5FCD  77            		ld	(hl),a
  5FCE  23            		inc	hl
  5FCF  10F2          		djnz	SETFLP3
  5FD1  F1            		pop	af		;a=drive-1(0-3)
  5FD2  E1            		pop	hl
  5FD3  C9            		ret
                      	
  5FD4                	SETFLP4:
  5FD4  3620          		ld	(hl),' '
  5FD6  23            		inc	hl
  5FD7  10FB          		djnz	SETFLP4
  5FD9  F1            		pop	af		;a=drive-1(0-3)
  5FDA  E1            		pop	hl
  5FDB  C9            		ret
                      	
                      	
                      	;60EX ROM end
  5FDC                	_6000H:	ds	6000h-_6000H
                      	
                      	
  6000                	_CALLINI:ds	CALLINI-_CALLINI
  601C                		org	CALLINI
                      	
  601C  CDB942        		call	INIDSK
                      	
                      	;for patching by iP6/iP6win/iP6Plus/PC6001VW
  601F  00            		nop
  6020  00            		nop
                      	;
                      	
  6021  C9            		ret
                      	
                      	
                      	;read RAM data
                      	;input: hl
                      	;output: a
                      	;destroy: none
                      	;fe8dh
  6022                	READRAM_SRC:
  6022  CD6B01        		call	CHGRAM
  6025  7E            		ld	a,(hl)
  6026  1805          		jr	CHGBANK_SRC
                      	
                      	
                      	;LDIR in RAM
                      	;input: bc,de,hl
                      	;output: bc=0, de=de+bc, hl=hl+bc
                      	;destroy: f (no changes in szc-flag)
                      	;fe93h
  6028                	LDIRRAM_SRC:
  6028  CD6B01        		call	CHGRAM
  602B  EDB0          		ldir
                      	;	jp	CHGBANK
                      	
                      	
                      	;change bank
                      	;destroy: none
                      	;fe98h: used by HURRY FOX
  602D                	CHGBANK_SRC:
  602D  F5            		push	af
  602E  3A64FE        		ld	a,(PORTF0H)
  6031  D3F0          		out	(0f0h),a
  6033  F1            		pop	af
  6034  FB            		ei
  6035  C9            		ret
                      	
                      	
                      	;fea1h
  6036                	OUTF0H_SRC:
  6036  D3F0          		out	(0f0h),a
  6038  F1            		pop	af
  6039  C9            		ret
                      	
                      	
                      	;LDDR in RAM
                      	;input: bc,de,hl
                      	;output: bc=0, de=de-bc, hl=hl-bc
                      	;destroy: f (no changes in szc-flag)
                      	;fea5h
  603A                	LDDRRAM_SRC:
  603A  CD6B01        		call	CHGRAM
  603D  EDB8          		lddr
  603F  18EC          		jr	CHGBANK_SRC
  6041                	RAMEND:
                      	
                      	
  6041                	BOOTERR:
  6041  3EC9          		ld	a,0c9h		;ret
  6043  328DFF        		ld	(HOOKERR),a
                      	;	ld	sp,0e700h-2*2
  6046  F1            		pop	af		;cancel stack for "call HOOKERR"
  6047  F1            		pop	af		;cancel stack for "call DISK2"
  6048  1853          		jr	SKIPFD
                      	
                      	;print menu and select mode
                      	;output: z(1=mode 5), a=mode-1(if z=0)
  604A                	SELMOD:
  604A  3A4EFF        		ld	a,(PROGAD)
  604D  FE05          		cp	05h
  604F  DADC60        		jp	c,SELECTED
                      	
                      	;FD auto start
  6052  CDB942        		call	INIDSK
  6055  323BFB        		ld	(DRIVES),a
  6058  2843          		jr	z,SKIPFD
                      	
  605A  3E02          		ld	a,02h
  605C  3265FE        		ld	(MODE),a
  605F  328CFD        		ld	(PAGES),a
                      	
  6062  3EC3          		ld	a,0c3h		;jp
  6064  328DFF        		ld	(HOOKERR),a
  6067  214160        		ld	hl,BOOTERR	;temporary for boot
  606A  228EFF        		ld	(HOOKERR+1),hl
                      	
  606D  DD2100C4      		ld	ix,0c400h
  6071  010100        		ld	bc,0001h	;track=0, sector=1
  6074  DD7000        		ld	(ix+00h),b	;drive-1=0
  6077  DD701B        		ld	(ix+1bh),b	;error count=0
  607A  2100F9        		ld	hl,0f900h	;load address
  607D  79            		ld	a,c		;1 sector
  607E                	BOOTSECT:
  607E  B7            		or	a		;reset z- and c-flag for reading
  607F  CDFD5C        		call	DISK2
  6082  3A00F9        		ld	a,(0f900h)
  6085  115953        		ld	de,"SY"
  6088  BA            		cp	d		;'S'
  6089  2012          		jr	nz,SKIPFD
  608B  2A01F9        		ld	hl,(0f901h)
  608E  E7            		rst	CPHLDE
  608F  CC03F9        		call	z,0f903h
                      	
  6092  3E06          		ld	a,06h
  6094  D3C1          		out	(0c1h),a	;32x16 text mode
                      	
  6096  3A4EFF        		ld	a,(PROGAD)
  6099  FE05          		cp	05h
  609B  383F          		jr	c,SELECTED
                      	
                      	;select mode
  609D                	SKIPFD:
  609D  AF            		xor	a
  609E  3265FE        		ld	(MODE),a
                      	
  60A1  CDFB1D        		call	CLS
  60A4  211001        		ld	hl,0110h
  60A7  CD6D11        		call	SETCSR
  60AA  3E20          		ld	a,' '
  60AC  061F          		ld	b,1fh
  60AE                	SLMDLP1:
  60AE  CD7510        		call	PRTC
  60B1  10FB          		djnz	SLMDLP1
                      	
  60B3  214762        		ld	hl,MENU
  60B6  0609          		ld	b,09h
  60B8                	SLMDLP2:
  60B8  E5            		push	hl
  60B9  2602          		ld	h,02h
  60BB  3E0D          		ld	a,0dh
  60BD  90            		sub	b
  60BE  6F            		ld	l,a
  60BF  CD6D11        		call	SETCSR
  60C2  E1            		pop	hl
  60C3  E5            		push	hl
  60C4  C5            		push	bc
  60C5  CDCF30        		call	PUTS
  60C8  C1            		pop	bc
  60C9  E1            		pop	hl
                      	
  60CA                	SLMDLP3:
  60CA  7E            		ld	a,(hl)
  60CB  23            		inc	hl
  60CC  B7            		or	a
  60CD  20FB          		jr	nz,SLMDLP3
  60CF  10E7          		djnz	SLMDLP2
                      	
  60D1                	SLMDLP4:
  60D1  CD3A10        		call	GETKBF
  60D4  28FB          		jr	z,SLMDLP4
  60D6  D631          		sub	'1'
  60D8  FE05          		cp	05h
  60DA  30F5          		jr	nc,SLMDLP4
                      	
  60DC                	SELECTED:
  60DC  FE04          		cp	04h
  60DE  F5            		push	af		;a=mode-1, z=mode 5?
  60DF  2001          		jr	nz,SETMOD
  60E1  3D            		dec	a		;mode 5->4 for "How Many..."
  60E2                	SETMOD:
  60E2  3265FE        		ld	(MODE),a
                      	
  60E5  E601          		and	01h
  60E7  3E04          		ld	a,04h
  60E9  210080        		ld	hl,8000h
  60EC  2003          		jr	nz,RAM32K
  60EE  26C0          		ld	h,0c0h
  60F0  1F            		rra			;c-flag=0
  60F1                	RAM32K:
  60F1  328CFD        		ld	(PAGES),a
  60F4  47            		ld	b,a		;;
                      	
  60F5  7C            		ld	a,h
  60F6  3291FD        		ld	(VRAM),a
  60F9  CBD4          		set	2,h		;h=h+04h
  60FB  2229FF        		ld	(BASICAD),hl	;l=0
  60FE  2C            		inc	l		;inc hl
  60FF  225FFA        		ld	(STARTAD),hl
                      	
  6102                	SLMDLP5:
  6102  78            		ld	a,b		;;
  6103  3D            		dec	a
  6104  CD0C14        		call	CHGACT
  6107  CDFB1D        		call	CLS
  610A  10F6          		djnz	SLMDLP5
                      	
  610C  AF            		xor	a
  610D  CDED13        		call	CHGDSP
                      	
  6110  F1            		pop	af		;a=mode-1, z=mode 5?
  6111  C8            		ret	z		;mode5: (PORTF0H)=11h
                      	
  6112  F5            		push	af
  6113  FE02          		cp	02h
  6115  9F            		sbc	a,a
  6116  E6D0          		and	0d0h
  6118  C6A1          		add	a,0a1h		;71h or a1h
  611A  3264FE        		ld	(PORTF0H),a
  611D  F1            		pop	af
  611E  C9            		ret
                      	
                      	
                      	;continued from CHGDSP for mode 5
  611F                	CHGDSP66:
  611F  F5            		push	af
                      	
  6120  3A8FFD        		ld	a,(SCREEN2)
  6123  47            		ld	b,a
  6124  3A90FD        		ld	a,(SCREEN3)
  6127  B8            		cp	b
  6128  2003          		jr	nz,CHGDSP66NZ
  612A  2191FD        		ld	hl,VRAM
  612D                	CHGDSP66NZ:
  612D  23            		inc	hl
  612E  7E            		ld	a,(hl)		;(SCREEN1)
  612F  FE02          		cp	02h
  6131  3E00          		ld	a,00h
  6133  3804          		jr	c,CHGDSP12
  6135  2004          		jr	nz,CHGDSP4
  6137                	CHGDSP3:
  6137  C604          		add	a,04h
  6139                	CHGDSP12:
  6139  C604          		add	a,04h
  613B                	CHGDSP4:
  613B  D3C1          		out	(0c1h),a
                      	
  613D  3A95FD        		ld	a,(COLOR3)
  6140  D3C0          		out	(0c0h),a
                      	
  6142  F1            		pop	af
  6143  0F            		rrca
  6144  C9            		ret
                      	
                      	
                      	;continued from CHGMOD for mode 5
  6145                	CHGMOD66:
  6145  3A8FFD        		ld	a,(SCREEN2)
  6148  47            		ld	b,a
  6149  3A90FD        		ld	a,(SCREEN3)
  614C  B8            		cp	b
  614D  CCED13        		call	z,CHGDSP
                      	
  6150  3A92FD        		ld	a,(SCREEN1)
  6153  3D            		dec	a
  6154  2003          		jr	nz,CHGMOD66NZ
                      	;screen mode 2
  6156  F680          		or	80h
  6158  77            		ld	(hl),a
                      	
  6159                	CHGMOD66NZ:
  6159  3D            		dec	a
  615A  3E28          		ld	a,CLMN66
  615C  2002          		jr	nz,CHGMOD66OK
  615E  3E14          		ld	a,CLMN66/2		;screen mode 3
  6160                	CHGMOD66OK:
  6160  32ACFD        		ld	(WIDTH),a
  6163  011F03        		ld	bc,CLMN66*ROWS66-1
  6166  C9            		ret
                      	
                      	
  6167                	REGDATA:
  6167  FFAACCBBEEDD34		dw	0aaffh, 0bbcch, 0ddeeh, 1234h
  616F  C3            		db	0c3h
  6170  0000          		dw	0000h
  6172                	REGDATAEND:
                      	
  6172                	MODE5:
  6172  AF            		xor	a
  6173  3291FD        		ld	(VRAM),a
  6176  3C            		inc	a
  6177  3294FD        		ld	(COLOR2),a
  617A  3297FD        		ld	(M1COLOR+1),a
  617D  329AFD        		ld	(M2COLOR+1),a
  6180  329DFD        		ld	(M3COLOR+1),a
  6183  32A0FD        		ld	(M4COLOR+1),a
  6186  3C            		inc	a
  6187  3295FD        		ld	(COLOR3),a
  618A  3298FD        		ld	(M1COLOR+2),a
  618D  329BFD        		ld	(M2COLOR+2),a
  6190  329EFD        		ld	(M3COLOR+2),a
  6193  32A1FD        		ld	(M4COLOR+2),a
  6196  3E10          		ld	a,10h
  6198  3293FD        		ld	(COLOR1),a
  619B  3296FD        		ld	(M1COLOR),a
  619E  3299FD        		ld	(M2COLOR),a
  61A1  329CFD        		ld	(M3COLOR),a
                      	
  61A4  3E28          		ld	a,CLMN66
  61A6  32ACFD        		ld	(WIDTH),a
  61A9  3E14          		ld	a,ROWS66
  61AB  3220FA        		ld	(HEIGHT),a
  61AE  32A3FD        		ld	(CONSOL2),a
  61B1  3D            		dec	a
  61B2  32A5FD        		ld	(LASTLIN),a
  61B5  3E06          		ld	a,06h
  61B7  32C7FE        		ld	(FKEYLEN),a
  61BA  3E04          		ld	a,04h
  61BC  3265FE        		ld	(MODE),a
  61BF  329FFD        		ld	(M4COLOR),a
                      	
                      	;	ld	a,04h
  61C2  2191FD        		ld	hl,VRAM
  61C5  11B9FD        		ld	de,PAGE1
  61C8                	MODE5LP1:
  61C8  012800        		ld	bc,PAGE1-VRAM
  61CB  EDB0          		ldir
  61CD  3D            		dec	a
  61CE  20F8          		jr	nz,MODE5LP1
                      	
  61D0  6F            		ld	l,a		;=0
  61D1  3A8CFD        		ld	a,(PAGES)
                      	
  61D4  2680          		ld	h,80h
  61D6  FE03          		cp	03h
  61D8  3806          		jr	c,SETAD
  61DA  2688          		ld	h,88h
  61DC  2802          		jr	z,SETAD
  61DE  26C8          		ld	h,0c8h
  61E0                	SETAD:
  61E0  2229FF        		ld	(BASICAD),hl
  61E3  2C            		inc	l
  61E4  225FFA        		ld	(STARTAD),hl
                      	
  61E7  21FFF9        		ld	hl,0f9ffh
  61EA  228DFD        		ld	(USREND),hl
                      	
  61ED  21A57C        		ld	hl,C_LS66
  61F0  228FFA        		ld	(0fa8fh),hl
  61F3  21A07C        		ld	hl,C_LLS66
  61F6  2291FA        		ld	(0fa91h),hl
  61F9  214A6B        		ld	hl,C_COL66
  61FC  2295FA        		ld	(0fa95h),hl	;COLOR command
  61FF  21C970        		ld	hl,C_LIN66
  6202  229BFA        		ld	(0fa9bh),hl	;LINE command
  6205  212571        		ld	hl,C_PAI66
  6208  229DFA        		ld	(0fa9dh),hl	;PAINT command
  620B  21A16B        		ld	hl,C_LCP66
  620E  22B3FA        		ld	(0fab3h),hl	;LCOPY command
  6211  213163        		ld	hl,F_PEEK66
  6214  2203FB        		ld	(0fb03h),hl	;PEEK() function
                      	
  6217  F5            		push	af		;a=(PAGES)
  6218  57            		ld	d,a
  6219  1E00          		ld	e,00h
  621B                	MODE5LP2:
  621B  7A            		ld	a,d
  621C  3D            		dec	a
  621D  CD4113        		call	GETPGAD
                      	
  6220  73            		ld	(hl),e
  6221  7B            		ld	a,e
  6222  3291FD        		ld	(VRAM),a
  6225  C640          		add	a,40h
  6227  5F            		ld	e,a
  6228  15            		dec	d
  6229  20F0          		jr	nz,MODE5LP2
  622B  C1            		pop	bc		;b=(PAGES)
                      	
  622C                	MODE5LP3:
  622C  78            		ld	a,b
  622D  3D            		dec	a
  622E  CD0C14        		call	CHGACT
  6231  CDFB1D        		call	CLS
  6234  10F6          		djnz	MODE5LP3
                      	
  6236  3E04          		ld	a,04h		;40x20 text mode
  6238  D3C1          		out	(0c1h),a	;bit3: 0=320x200, 1=160x200
                      					;bit2: 0=graphic, 1=text
                      					;bit1: 0=40x20,   1=32x16
                      	;	ld	a,04h		;relay off,timer on, VRAM=0000h
                      	;	out	(0b0h),a	;bit3=CMT relay: 0=off, 1=on
                      					;bit21=VRAM address:
                      					; 00=c000h(60)/8000h(60m)
                      					; 01=e000h(60)/c000h(60m)
                      					; 10=8000h(60)/0000h(60m)*
                      					; 11=a000h(60)/4000h(60m)
                      					;bit0=timer: 0=on, 1=off
                      	
  623A  0F            		rrca			;2
  623B  D3C0          		out	(0c0h),a	;CSS2=1
                      	
  623D  AF            		xor	a
  623E  CDED13        		call	CHGDSP
                      	
  6241  CDBA34        		call	NEWRESSTK
                      	
  6244  C34040        		jp	SETTBL
                      	
                      	
  6247                	MENU:
  6247  2A2A2050432D36		db	"** PC-6001Mk2/6601 ",9ah,0deh,96h,0fdh,"BASIC **",00h
  6267  0A00          		db	0ah,00h
  6269  313A3630204241		db	"1:60 BASIC          (RAM-16K)",00h
  6287  323A3630204241		db	"2:60 BASIC          (RAM-32K)",00h
  62A5  333A3630204558		db	"3:60 EXTENDED BASIC (RAM-16K)",00h
  62C3  343A3630204558		db	"4:60 EXTENDED BASIC (RAM-32K)",00h
  62E1  353A3636204241		db	"5:66 BASIC          (RAM-64K)",00h
  62FF  0A00          		db	0ah,00h
  6301  20202020202053		db	"      SELECT BASIC MODE",00h
                      	
                      	
  6319                	SYSNAME66:
  6319  36369ADE96FD42		db	"66", 9ah, 0deh, 96h, 0fdh, "BASIC Ver.0.5.2", 0dh, 0ah, 00h
                      	
                      	
                      	;PEEK() function
  6331                	F_PEEK66:
  6331  CD9B0B        		call	CHKNUM
  6334  CD9F0C        		call	FTOI
  6337  EB            		ex	de,hl
  6338  CD8DFE        		call	READRAM
  633B  C37D0C        		jp	I1TOFA
                      	
                      	
                      	;continued from F_SCRN
  633E                	F_SCRN66:
  633E  CD8DFE        		call	READRAM
  6341  CD7D0C        		call	I1TOFA
  6344  C38A18        		jp	FNCRTN
                      	
                      	
                      	;scroll up
                      	;input: h=first line+1, l=last line+1
                      	;destroy: af
  6347                	_SCRLU66:ds	SCRLU66-_SCRLU66
  63E2                		org	SCRLU66
                      	
  63E2  7D            		ld	a,l
  63E3  94            		sub	h		;a=(LASTLIN)-(CONSOL1) < 20
                      	;	ret	z
                      	;	ret	c
                      	
                      	;c-flag=0:scroll up, c-flag=1:scroll down
  63E4                	SCRLUD66:
  63E4  D5            		push	de
  63E5  E5            		push	hl
  63E6  C5            		push	bc
  63E7  F5            		push	af		;up/down
                      	
                      	;line connection status
  63E8  47            		ld	b,a
  63E9  04            		inc	b
  63EA  50            		ld	d,b		;>0
  63EB                	SCRLUD66LP:
  63EB  CD4514        		call	CHKLINE4
  63EE  4F            		ld	c,a
  63EF  7A            		ld	a,d
  63F0  E5            		push	hl
  63F1  CD5114        		call	SETLINE
  63F4  E1            		pop	hl
  63F5  51            		ld	d,c
  63F6  2D            		dec	l
  63F7  F1            		pop	af		;up/down
  63F8  F5            		push	af
  63F9  3002          		jr	nc,SCRLUD66NC
  63FB  2C            		inc	l
  63FC  2C            		inc	l
  63FD                	SCRLUD66NC:
  63FD  10EC          		djnz	SCRLUD66LP
                      	
  63FF  6C            		ld	l,h
  6400  3A92FD        		ld	a,(SCREEN1)
  6403  FE02          		cp	02h
  6405  D1            		pop	de		;d=a,e=up/down
  6406  D5            		push	de
  6407  7A            		ld	a,d
  6408  302A          		jr	nc,SCRLG66	;screen mode 3 4
                      	
  640A  CD8964        		call	MUL40
  640D  CDB811        		call	Y2AD
  6410  212800        		ld	hl,CLMN66
  6413  19            		add	hl,de
  6414  F1            		pop	af		;up/down
  6415  3003          		jr	nc,SCRL2NC
  6417  EB            		ex	de,hl
  6418  1B            		dec	de
  6419  2B            		dec	hl
                      	
  641A                	SCRL2NC:
  641A  E5            		push	hl
  641B  D5            		push	de
  641C  C5            		push	bc
  641D  CD8364        		call	LDIDRRAM
  6420  C1            		pop	bc
  6421  D1            		pop	de
  6422  E1            		pop	hl
                      	
                      	;attribute
  6423  CB94          		res	2,h
  6425  CB92          		res	2,d
  6427  C36664        		jp	SCRLATT66
                      	
                      	
                      	;scroll down
                      	;input: h=last line+1, l=first line+1
                      	;destroy: af
  642A                	_SCRLD66:ds	SCRLD66-_SCRLD66
  642F                		org	SCRLD66
                      	
  642F  7C            		ld	a,h
  6430  95            		sub	l
                      	;	ret	z
                      	;	ret	c
  6431  37            		scf			;set c-flag
  6432  18B0          		jr	SCRLUD66
                      	
                      	
                      	;for graphic mode (screen mode 3,4) for mode 5
  6434                	SCRLG66:
                      	;bc=a*40*10=a*(256+128+16)
                      	
  6434  67            		ld	h,a
  6435  48            		ld	c,b		;b=0
  6436  47            		ld	b,a
  6437  1F            		rra			;c-flag=0
  6438  CB19          		rr	c
  643A  80            		add	a,b
  643B  47            		ld	b,a
                      	
  643C  7C            		ld	a,h
  643D  87            		add	a,a
  643E  87            		add	a,a
  643F  87            		add	a,a
  6440  87            		add	a,a
  6441  3001          		jr	nc,SCRLG66NC1
  6443  04            		inc	b
  6444                	SCRLG66NC1:
  6444  81            		add	a,c
  6445  4F            		ld	c,a
  6446  3001          		jr	nc,SCRLG66NC2
  6448  04            		inc	b
  6449                	SCRLG66NC2:
                      	
  6449  2601          		ld	h,1
  644B  CDD565        		call	XY2GAD66
  644E  EB            		ex	de,hl
  644F  219001        		ld	hl,CLMN66*10
  6452  19            		add	hl,de
  6453  F1            		pop	af		;up/down
  6454  3003          		jr	nc,SCRLG66NC3
  6456  EB            		ex	de,hl
  6457  1B            		dec	de
  6458  2B            		dec	hl
                      	
  6459                	SCRLG66NC3:
  6459  E5            		push	hl
  645A  D5            		push	de
  645B  C5            		push	bc
  645C  CD8364        		call	LDIDRRAM
  645F  C1            		pop	bc
  6460  D1            		pop	de
  6461  E1            		pop	hl
  6462  CBAC          		res	5,h
  6464  CBAA          		res	5,d
                      	
  6466                	SCRLATT66:
  6466  CD8364        		call	LDIDRRAM
  6469  C32612        		jp	SCRLATT2
                      	
                      	
                      	;continued from DELLIN
  646C                	DELLIN66:
  646C  3009          		jr	nc,DEL6634
  646E  E5            		push	hl
  646F  CB94          		res	2,h
                      	;	ld	bc,CLMN66
  6471  010128        		ld	bc,2801h
  6474  C3F111        		jp	DELLATT
                      	
  6477                	DEL6634:
  6477  CDCC65        		call	AD2GAD2
  647A  E5            		push	hl
  647B  CBAC          		res	5,h
                      	;	ld	bc,CLMN66*10
  647D  010290        		ld	bc,9002h
  6480  C3F111        		jp	DELLATT
                      	
                      	
                      	;ldir or lddr in RAM
                      	;input: bc,de,hl,c-flag(0=ldir,1=lddr)
                      	;output: bc=0, de, hl
                      	;destroy: f
  6483                	LDIDRRAM:
  6483  D293FE        		jp	nc,LDIRRAM
  6486  C3A5FE        		jp	LDDRRAM
                      	
                      	
                      	;bc=a*40
                      	;input: a(<=20)
                      	;output: bc
                      	;destory: af,h
  6489                	MUL40:
  6489  47            		ld	b,a
  648A  87            		add	a,a		;*2
  648B  87            		add	a,a		;*4
  648C  80            		add	a,b		;*5
  648D  87            		add	a,a		;*10
  648E  45            		ld	b,l		;;
  648F  2600          		ld	h,00h
  6491  6F            		ld	l,a
  6492  29            		add	hl,hl		;*20
  6493  29            		add	hl,hl		;*40
  6494  4D            		ld	c,l
  6495  68            		ld	l,b		;;
  6496  44            		ld	b,h
  6497  C9            		ret
                      	
                      	
                      	;continued from PRTFKEY for mode 5
  6498                	PFK66:
  6498  CD1666        		call	CHRREV66
                      	
  649B  EB            		ex	de,hl
  649C  3A5AFA        		ld	a,(KEYFLG)	;bit0=caps, bit1=shift+page, bit2=kana
  649F  21BE64        		ld	hl,MODE_LOWER
  64A2  0F            		rrca
  64A3  CB4F          		bit	1,a
  64A5  2002          		jr	nz,PFK66NZ	;if caps lock
  64A7  1F            		rra
  64A8  07            		rlca
  64A9                	PFK66NZ:
  64A9  E603          		and	03h
  64AB  0600          		ld	b,00h
  64AD  4F            		ld	c,a
  64AE  09            		add	hl,bc
  64AF  09            		add	hl,bc
  64B0  09            		add	hl,bc
  64B1  EB            		ex	de,hl
                      	
  64B2  0603          		ld	b,03h
  64B4                	PFK66LP:
  64B4  1A            		ld	a,(de)
  64B5  CDCA14        		call	PRT12
  64B8  13            		inc	de
  64B9  10F9          		djnz	PFK66LP
  64BB  C31666        		jp	CHRREV66
                      	
  64BE                	MODE_LOWER:
  64BE  616263        		db	"abc"
  64C1                	MODE_UPPER:
  64C1  414243        		db	"ABC"
  64C4                	MODE_HIRAGANA:
  64C4  96E520        		db	96h, 0e5h, ' '
  64C7                	MODE_KATAKANA:
  64C7  B6C520        		db	0b6h, 0c5h, ' '
                      	
                      	
                      	;continued from GETSP for mode 5
  64CA                	GETSP66:
  64CA  3D            		dec	a
  64CB  C8            		ret	z
  64CC  3D            		dec	a
  64CD  3E20          		ld	a,' '
  64CF  F8            		ret	m
  64D0  3A94FD        		ld	a,(COLOR2)
  64D3  2807          		jr	z,GETSP663
                      	
                      	;screen mode 4
  64D5                	GETSP664:
  64D5  B7            		or	a
  64D6  C8            		ret	z
  64D7  3D            		dec	a		;1,2,3,4,...->0,1,2,3,...
  64D8  0F            		rrca
  64D9  0F            		rrca
  64DA  9F            		sbc	a,a		;0,1,2,3,...->00,00,ff,ff,00,ff,...
  64DB  C9            		ret
                      	
                      	;screen mode 3
  64DC                	GETSP663:
  64DC  B7            		or	a
  64DD  C8            		ret	z
  64DE  3D            		dec	a
  64DF  E60C          		and	0ch
  64E1                	GETSP663BIT:
  64E1  C5            		push	bc
  64E2  4F            		ld	c,a
  64E3  0603          		ld	b,03h
  64E5                	GETSP663LP:
  64E5  0F            		rrca
  64E6  0F            		rrca
  64E7  B1            		or	c
  64E8  10FB          		djnz	GETSP663LP
  64EA  C1            		pop	bc
  64EB  C9            		ret
                      	
                      	
                      	;continued from GETSPA for mode 5
  64EC                	GETSPA66:
  64EC  CB3F          		srl	a
  64EE  2007          		jr	nz,GETSPA6634
  64F0  3A93FD        		ld	a,(COLOR1)
  64F3  303D          		jr	nc,GETSPA661
  64F5  1842          		jr	GETSPA662
                      	
  64F7                	GETSPA6634:
  64F7  3A94FD        		ld	a,(COLOR2)
  64FA  381D          		jr	c,GETSPA664
  64FC  182A          		jr	GETSPA663
                      	
                      	
                      	;continued from SETATT
  64FE                	SETATT66:
  64FE  F1            		pop	af
  64FF  CD0665        		call	CNVATT66
  6502  32ACFE        		ld	(ATTDAT),a
  6505  C9            		ret
                      	
                      	;convert to attribute data (screen mode 1,2)
                      	; or bit pattern (screen mode 3,4)
                      	;input: a=color code
                      	;output: a=attribute
                      	;destroy: f
  6506                	CNVATT66:
  6506  F5            		push	af
  6507  3A92FD        		ld	a,(SCREEN1)
  650A  CB3F          		srl	a
  650C  2821          		jr	z,ATT6612	;screen mode 1 or 2
  650E  300F          		jr	nc,ATT663	;screen mode 3
                      	
                      	;screen mode 4
  6510                	ATT664:
  6510  F1            		pop	af
  6511  F5            		push	af
  6512  CDD564        		call	GETSP664
  6515  3260FE        		ld	(ATTDAT2),a
  6518  F1            		pop	af
  6519                	GETSPA664:
  6519  B7            		or	a
  651A  C8            		ret	z
  651B  3D            		dec	a
  651C  0F            		rrca
  651D  9F            		sbc	a,a		;0,1,2,3,...->00,ff,00,ff,00,ff,...
  651E  C9            		ret
                      	
                      	;screen mode 3
  651F                	ATT663:
  651F  F1            		pop	af
  6520  F5            		push	af
  6521  CDDC64        		call	GETSP663
  6524  3260FE        		ld	(ATTDAT2),a
  6527  F1            		pop	af
  6528                	GETSPA663:
  6528  B7            		or	a
  6529  C8            		ret	z
  652A  3D            		dec	a
  652B  E603          		and	03h
  652D  18B2          		jr	GETSP663BIT
                      	
                      	;screen mode 1 or 2
  652F                	ATT6612:
  652F  3807          		jr	c,ATT662
                      	
                      	;screen mode 1
  6531                	ATT661:
  6531  F1            		pop	af
  6532                	GETSPA661:
  6532  CD3F65        		call	ATTTXT66
  6535  E67F          		and	7fh		;bit7: semi-graphic flag
  6537  C9            		ret
                      	
                      	;screen mode 2
  6538                	ATT662:
  6538  F1            		pop	af
  6539                	GETSPA662:
  6539  CD3F65        		call	ATTTXT66
  653C  F680          		or	80h		;bit7: semi-graphic flag
  653E  C9            		ret
                      	
                      	
                      	;get text attribute for mode 5
                      	;input: a=color code
                      	;output: a[6:0]=attribute
                      	;destroy: f
  653F                	ATTTXT66:
  653F  E5            		push	hl
  6540  D601          		sub	01h		;if a<>0 then a=a-1
  6542  CE00          		adc	a,00h
  6544  E60F          		and	0fh
  6546  67            		ld	h,a
  6547  3A94FD        		ld	a,(COLOR2)
  654A  3D            		dec	a		;range is not checked, for compatibility with N60m/N66-BASIC
  654B  07            		rlca
  654C  07            		rlca
  654D  07            		rlca
  654E  07            		rlca
  654F  B4            		or	h		;bit6-4: background, bit3-0:foreground
  6550  E1            		pop	hl
  6551  C9            		ret
                      	
                      	
                      	;convert character VRAM address to graphic VRAM address for mode 5
                      	;(screen mode 1,2 -> screen mode 3,4)
                      	;input: hl=character VRAM address
                      	;output: hl=graphic VRAM address
                      	;destroy: none
  6552                	_AD2GAD2:ds	AD2GAD2-_AD2GAD2
  65CC                		org	AD2GAD2
                      	
  65CC  F5            		push	af
  65CD  CD5011        		call	AD2XY
  65D0  F1            		pop	af
  65D1  CDD565        		call	XY2GAD66
  65D4  C9            		ret
                      	
                      	
                      	;get VRAM address (screen mode 3,4) for mode 5
                      	;input: h=x+1, l=y+1
                      	;output: hl=graphic VRAM address =(VRAM+4000h)+y*400+x
                      	;destroy: none
  65D5                	XY2GAD66:
  65D5  F5            		push	af
  65D6  D5            		push	de
  65D7  C5            		push	bc
                      	
  65D8  25            		dec	h
  65D9  2D            		dec	l
  65DA  3A92FD        		ld	a,(SCREEN1)
  65DD  FE02          		cp	02h
  65DF  7C            		ld	a,h
  65E0  2001          		jr	nz,XY2GAD66C
  65E2  87            		add	a,a		;screen mode 3
  65E3                	XY2GAD66C:
  65E3  2600          		ld	h,00h
  65E5  29            		add	hl,hl		;*2
  65E6  29            		add	hl,hl		;*4
  65E7  29            		add	hl,hl		;*8
  65E8  29            		add	hl,hl		;*16
  65E9  54            		ld	d,h
  65EA  5D            		ld	e,l
  65EB  29            		add	hl,hl		;*32
  65EC  29            		add	hl,hl		;*64
  65ED  29            		add	hl,hl		;*128
  65EE  44            		ld	b,h
  65EF  4D            		ld	c,l
  65F0  29            		add	hl,hl		;*256
  65F1  09            		add	hl,bc
  65F2  19            		add	hl,de
                      	
  65F3  85            		add	a,l		;c-flag
  65F4  6F            		ld	l,a
                      	
  65F5  3A91FD        		ld	a,(VRAM)
  65F8  CE20          		adc	a,20h		;c-flag
  65FA  84            		add	a,h
  65FB  67            		ld	h,a
                      	
  65FC  C1            		pop	bc
  65FD  D1            		pop	de
  65FE  F1            		pop	af
  65FF  C9            		ret
                      	
                      	
  6600                	Y2AD66:
  6600  E5            		push	hl
  6601  2600          		ld	h,00h
  6603  2D            		dec	l
  6604  54            		ld	d,h
  6605  5D            		ld	e,l
  6606  29            		add	hl,hl		;Y*2
  6607  29            		add	hl,hl		;Y*4
  6608  19            		add	hl,de		;Y*5
  6609  29            		add	hl,hl		;Y*10
  660A  24            		inc	h		;Y*10+0100h
  660B  29            		add	hl,hl		;Y*20+0200h
  660C  29            		add	hl,hl		;Y*40+0400h
  660D  3A91FD        		ld	a,(VRAM)
  6610  84            		add	a,h
  6611  67            		ld	h,a
  6612  EB            		ex	de,hl
  6613  E1            		pop	hl
  6614  F1            		pop	af
  6615  C9            		ret
                      	
                      	
  6616                	CHRREV66:
  6616  E5            		push	hl
  6617  2A93FD        		ld	hl,(COLOR1)	;l=(COLOR1), h=(COLOR2)
  661A  7C            		ld	a,h
  661B  65            		ld	h,l
  661C  6F            		ld	l,a
  661D  2293FD        		ld	(COLOR1),hl
  6620  E1            		pop	hl
  6621  C9            		ret
                      	
                      	
                      	;continued from CSRREV
  6622                	CSRREV66:
  6622  CB94          		res	2,h
  6624  CD8DFE        		call	READRAM
  6627  2F            		cpl
  6628  EE80          		xor	80h
  662A  77            		ld	(hl),a
  662B  C9            		ret
                      	
                      	
  662C                	GETSCRC66:
  662C  E5            		push	hl
  662D  CD3C0E        		call	CHR2ATT
  6630  CD8DFE        		call	READRAM
  6633  E1            		pop	hl
  6634  07            		rlca
  6635  D28DFE        		jp	nc,READRAM
  6638  3E20          		ld	a,' '
  663A  C9            		ret
                      	
                      	
  663B                	PRT34XY66:
  663B  CDD565        		call	XY2GAD66	;no change in c-flag
                      	
  663E  060A          		ld	b,0ah
  6640                	PRT34XY66LP:
  6640  D5            		push	de
  6641  CD9435        		call	READCGROM
                      	
  6644  3A92FD        		ld	a,(SCREEN1)
  6647  0F            		rrca
  6648  3005          		jr	nc,CALL366
                      	
  664A  CD7066        		call	PRT466
  664D  1804          		jr	CALL3466END
  664F                	CALL366:
  664F  CD5E66        		call	PRT366
  6652  2B            		dec	hl
                      	
  6653                	CALL3466END:
  6653  112800        		ld	de,CLMN66
  6656  19            		add	hl,de
  6657  D1            		pop	de
  6658  1C            		inc	e		;inc de
  6659  10E5          		djnz	PRT34XY66LP
                      	
  665B  C1            		pop	bc
  665C  D1            		pop	de
  665D  C9            		ret
                      	
                      	
  665E                	PRT366:
  665E  CD6366        		call	PRT3HALF66
  6661  23            		inc	hl
  6662  51            		ld	d,c
                      	
  6663                	PRT3HALF66:
  6663  1E04          		ld	e,04h
  6665                	PRT366LP:
  6665  CB02          		rlc	d
  6667  17            		rla
  6668  0F            		rrca
  6669  07            		rlca
  666A  17            		rla
  666B  1D            		dec	e
  666C  20F7          		jr	nz,PRT366LP
  666E  4A            		ld	c,d
  666F  57            		ld	d,a
                      	
                      	
                      	;input: hl=address(+2000h), d=bit
                      	;destroy: af,e
  6670                	PRT466:
  6670  CD8DFE        		call	READRAM
  6673  5F            		ld	e,a
  6674  3A60FE        		ld	a,(ATTDAT2)
  6677  AB            		xor	e
  6678  A2            		and	d
  6679  AB            		xor	e
  667A  77            		ld	(hl),a
                      	
  667B  CBAC          		res	5,h
  667D  CD8DFE        		call	READRAM
  6680  5F            		ld	e,a
  6681  3AACFE        		ld	a,(ATTDAT)
  6684  AB            		xor	e
  6685  A2            		and	d
  6686  AB            		xor	e
  6687  77            		ld	(hl),a
  6688  CBEC          		set	5,h
                      	
  668A  C9            		ret
                      	
                      	
                      	;continued from GXY2SXY
                      	;input: a=screen mode-1, bc=graphic X, de=graphic Y
                      	;output: bc=screen mode X, de=screen mode Y
                      	;destroy: af
  668B                	GXY2SXY66:
  668B  CB3F          		srl	a
  668D  3815          		jr	c,GXY2SXY6624
  668F  200D          		jr	nz,GXY2SXY663
                      	
                      	;screen mode 1: 40x20
  6691                	GXY2SXY661:
  6691  E5            		push	hl
  6692  CD076F        		call	DIV10
  6695  E1            		pop	hl
  6696  CB38          		srl	b
  6698  CB19          		rr	c
  669A                	SRLBC2:
  669A  CB38          		srl	b
  669C  CB19          		rr	c
                      	
                      	;screen mode 3: 160x200
  669E                	GXY2SXY663:
  669E  CB38          		srl	b
  66A0  CB19          		rr	c
  66A2  37            		scf			;for EXAS BASIC compiler
  66A3  C9            		ret
                      	
  66A4                	GXY2SXY6624:
                      	;	scf
  66A4  C0            		ret	nz		;screen mode 4: 320x200
                      	
                      	;screen mode 2: 80x40
  66A5                	GXY2SXY662:
  66A5  E5            		push	hl
  66A6  CD036F        		call	DIV5
  66A9  E1            		pop	hl
  66AA  18EE          		jr	SRLBC2	
                      	
                      	
                      	;continued from SXY2AD
                      	;output: hl=(fdb3-fdb4)=attribute address, a=(fdb2)=bit
                      	;destroy: f
  66AC                	SXY2AD66:
  66AC  CB3F          		srl	a
  66AE  2016          		jr	nz,SXY2AD6634
  66B0  3E70          		ld	a,70h
  66B2  16FC          		ld	d,0-04h
  66B4  302D          		jr	nc,SXY2AD661
                      	
                      	;(VRAM)*256+int(de/2)*40+bc/2
                      	;screen mode 2
  66B6                	SXY2AD662:
  66B6  AF            		xor	a
  66B7  CB3B          		srl	e
  66B9  CB17          		rl	a
  66BB  CB39          		srl	c
  66BD  CB17          		rl	a
  66BF  47            		ld	b,a
  66C0  3E10          		ld	a,10h
  66C2  1600          		ld	d,00h
  66C4  1819          		jr	SXY2ADBIT66
                      	
  66C6                	SXY2AD6634:
  66C6  3806          		jr	c,SXY2AD664
                      	
                      	;(VRAM)*256+int(de/2)*40+bc/4
                      	;screen mode 3
  66C8                	SXY2AD663:
  66C8  3E81          		ld	a,81h
  66CA  CB21          		sla	c
  66CC  CB10          		rl	b
                      	
                      	;(VRAM)*256+de*40+bc/8
                      	;screen mode 4
  66CE                	SXY2AD664:
  66CE  67            		ld	h,a		;81h=screen mode 3=81h, 01h=screen mode 4
  66CF  79            		ld	a,c
  66D0  E607          		and	07h
  66D2  57            		ld	d,a
  66D3  7C            		ld	a,h		;81h=screen mode 3=81h, 01h=screen mode 4
  66D4  CB38          		srl	b
  66D6  CB19          		rr	c
  66D8  CB39          		srl	c
  66DA  CB39          		srl	c
  66DC  42            		ld	b,d
  66DD  161C          		ld	d,20h-04h
  66DF                	SXY2ADBIT66:
  66DF  04            		inc	b
  66E0                	SXY2AD66LP:
  66E0  0F            		rrca
  66E1  10FD          		djnz	SXY2AD66LP
                      	
                      	;(VRAM)*256+de*40+bc/2
                      	;screen mode 1
  66E3                	SXY2AD661:
  66E3  61            		ld	h,c
  66E4  6B            		ld	l,e
  66E5  24            		inc	h
  66E6  2C            		inc	l
  66E7  CDCD11        		call	XY2AD		;+0400h
  66EA  47            		ld	b,a		;
  66EB  7C            		ld	a,h
  66EC  82            		add	a,d		;screen1:-04h, screen2:00h, screen3,4:1ch
  66ED  67            		ld	h,a
  66EE  78            		ld	a,b		;
  66EF  22B3FD        		ld	(VRAMAD),hl
  66F2  32B2FD        		ld	(VRAMBIT),a
  66F5  D1            		pop	de
  66F6  C1            		pop	bc
  66F7  C9            		ret
                      	
                      	
                      	
                      	;continued from GETCOLR
                      	;input: a=screen mode-1, hl=VRAM address
                      	; screen1=+0000h, screen2=+0400h, screen3-4=+2000h
                      	;output: a=color
                      	;destroy: f,hl
  66F8                	GETCOLR66:
  66F8  D5            		push	de
  66F9  CB3F          		srl	a
  66FB  3AB2FD        		ld	a,(VRAMBIT)
  66FE  57            		ld	d,a
  66FF  201B          		jr	nz,GETCOLR6634
                      	
  6701  CD8DFE        		call	READRAM
  6704  3809          		jr	c,GETCOLR662
                      	
                      	;screen mode 1
  6706                	GETCOLR661:
  6706  E670          		and	70h
  6708  0F            		rrca
  6709  0F            		rrca
  670A  0F            		rrca
  670B  0F            		rrca
  670C  3C            		inc	a
  670D  D1            		pop	de
  670E  C9            		ret
                      	
                      	;screen mode 2
  670F                	GETCOLR662:
  670F  A2            		and	d
  6710  28F4          		jr	z,GETCOLR661
  6712  CB94          		res	2,h
  6714  CD8DFE        		call	READRAM
  6717  E60F          		and	0fh
  6719  3C            		inc	a
  671A  D1            		pop	de
  671B  C9            		ret
                      	
                      	;screen mode 3
                      	;screen mode 4
  671C                	GETCOLR6634:
  671C  C5            		push	bc
  671D  9F            		sbc	a,a
  671E  47            		ld	b,a		;0=screen mode 3, ff=screen mode 4
  671F  CD8DFE        		call	READRAM
  6722  A2            		and	d
  6723  07            		rlca
  6724  04            		inc	b
  6725  2801          		jr	z,GETCOLR664
  6727  07            		rlca			;only screen mode 3
  6728                	GETCOLR664:
  6728  5F            		ld	e,a
  6729  CBAC          		res	5,h
  672B  CD8DFE        		call	READRAM
  672E  A2            		and	d
  672F  B3            		or	e
  6730  07            		rlca
  6731                	GETCOLR6634LP:
  6731  0F            		rrca
  6732  CB1A          		rr	d
  6734  30FB          		jr	nc,GETCOLR6634LP
  6736  3C            		inc	a
  6737  C1            		pop	bc
  6738  D1            		pop	de
  6739  C9            		ret
                      	
                      	
                      	;set border attribute used by HURRY FOX
                      	;input: a=color code
                      	;destroy: af,bc
  673A                	_SETBO66:ds	SETBO66-_SETBO66
  69F3                		org	SETBO66
                      	
  69F3  32C6FE        		ld	(BORDERC),a
                      	
  69F6  47            		ld	b,a
  69F7  3AACFE        		ld	a,(ATTDAT)
  69FA  F5            		push	af
  69FB  3A60FE        		ld	a,(ATTDAT2)
  69FE  F5            		push	af
                      	
  69FF  78            		ld	a,b
  6A00  CDC015        		call	SETATT
  6A03  3AACFE        		ld	a,(ATTDAT)
  6A06  32C5FE        		ld	(BORDERA),a
  6A09  3A60FE        		ld	a,(ATTDAT2)
  6A0C  3261FE        		ld	(BORDA2),a
                      	
  6A0F  F1            		pop	af
  6A10  3260FE        		ld	(ATTDAT2),a
  6A13  F1            		pop	af
  6A14  32ACFE        		ld	(ATTDAT),a
                      	
  6A17  C9            		ret
                      	
                      	
                      	;COLOR command
  6A18                	_C_COL66:
  6A18                		ds	C_COL66-_C_COL66
  6B4A                		org	C_COL66
                      	
  6B4A  1193FD        		ld	de,COLOR1
  6B4D  0602          		ld	b,02h
  6B4F                	COLR66LP:
  6B4F  CD6B3F        		call	CHKCMM
  6B52  2810          		jr	z,COLR66Z
  6B54  C5            		push	bc
  6B55  D5            		push	de
  6B56  CDE40D        		call	INT1ARG		;1st and 2nd parameter
  6B59  D1            		pop	de
  6B5A  C1            		pop	bc
  6B5B  12            		ld	(de),a
  6B5C  CD603F        		call	CHKCLN		;a=(hl)
  6B5F  C8            		ret	z
  6B60  FE2C          		cp	','
  6B62  C0            		ret	nz
  6B63  23            		inc	hl
  6B64                	COLR66Z:
  6B64  13            		inc	de
  6B65  10E8          		djnz	COLR66LP
                      	
  6B67  CDE40D        		call	INT1ARG		;3rd parameter
  6B6A  1D            		dec	e
                      	
  6B6B  3A92FD        		ld	a,(SCREEN1)
  6B6E  4F            		ld	c,a
  6B6F  3C            		inc	a
  6B70  E604          		and	04h
  6B72  3C            		inc	a		;a=1 if screen mode=1-3, a=5 if screen mode=4
  6B73  BB            		cp	e
  6B74  DACB03        		jp	c,FCERR
  6B77  7B            		ld	a,e
                      	;	jp	SETC366
                      	
                      	
                      	;set color 3rd parameter for mode 5
                      	;input: a=3rd color parameter-1, c=screen mode-1
                      	;destroy: af
  6B78                	_SETC366:ds	SETC366-_SETC366
  6B7A                		org	SETC366
                      	
  6B7A  CB49          		bit	1,c
  6B7C  2805          		jr	z,C366TXT
  6B7E  CB41          		bit	0,c
  6B80  2002          		jr	nz,C3664
                      	
                      	;screen mode 3: 0,1->2,6
  6B82  87            		add	a,a
                      	
                      	;screen mode 1,2: 0,1->2,0
  6B83                	C366TXT:
  6B83  87            		add	a,a
                      	
                      	;screen mode 4: 0,1,2,3,4,5->2,3,0,1,6,7
  6B84                	C3664:
  6B84  EE02          		xor	02h
                      	
  6B86  D3C0          		out	(0c0h),a
  6B88  3295FD        		ld	(COLOR3),a
  6B8B  C9            		ret
                      	
                      	
                      	;SCREEN command
  6B8C                	_C_SCR66:ds	C_SCR66-_C_SCR66
  6B9B                		org	C_SCR66
  6B9B  00            		nop
  6B9C  00            		nop
  6B9D  00            		nop
                      	
                      	;used by PROGRESS
                      	;input: hl=parameter string address
  6B9E                	_SCRN66:ds	SCRN66-_SCRN66
  6B9E                		org	SCRN66
                      	
  6B9E  C3041E        		jp	C_SCRN
                      	
                      	
                      	;LCOPY command for mode 5
  6BA1                	C_LCP66:
  6BA1  CDD2FF        		call	HOOKLCP
  6BA4  CD593F        		call	SKIPSP
  6BA7  FE32          		cp	'2'
  6BA9  2806          		jr	z,LCPMOD2
  6BAB  FE31          		cp	'1'
  6BAD  3E30          		ld	a,'0'
  6BAF  2001          		jr	nz,LCPMOD
  6BB1                	LCPMOD2:
  6BB1  23            		inc	hl
  6BB2                	LCPMOD:
  6BB2  224EFF        		ld	(PROGAD),hl
  6BB5  C602          		add	a,02h
  6BB7  FE34          		cp	'4'
  6BB9  F5            		push	af		;'2'=copy mode 1, '4'=copy mode 2(z-flag=1)
                      	
                      	;header
  6BBA  3E42          		ld	a,'B'
  6BBC  CD786C        		call	LCP66ESC
  6BBF  CD6E6C        		call	LCP66NL
                      	;body
  6BC2  2A90FD        		ld	hl,(VRAM-1)	;h=(VRAM)
  6BC5  2E27          		ld	l,CLMN66-1
  6BC7  0E28          		ld	c,CLMN66
  6BC9                	LCP66LP1:
  6BC9  3E53          		ld	a,'S'
  6BCB  CD786C        		call	LCP66ESC
  6BCE  3E30          		ld	a,'0'
  6BD0  CD371A        		call	PRINTER
  6BD3  F1            		pop	af		;'2'=copy mode 1, '4'=copy mode 2(z-flag=1)
  6BD4  F5            		push	af		;'2'=copy mode 1, '4'=copy mode 2(z-flag=1)
  6BD5  CD371A        		call	PRINTER
  6BD8  3E30          		ld	a,'0'
  6BDA  CD371A        		call	PRINTER
  6BDD  3E30          		ld	a,'0'
  6BDF  CD371A        		call	PRINTER
  6BE2  3A92FD        		ld	a,(SCREEN1)
  6BE5  FE02          		cp	02h
  6BE7  3839          		jr	c,LCP66TXT		;text mode
                      	
                      	;graphic mode
  6BE9  112800        		ld	de,CLMN66
  6BEC  06C8          		ld	b,200
  6BEE                	LCP66LP2:
  6BEE  CD8DFE        		call	READRAM
  6BF1  CD371A        		call	PRINTER
  6BF4  F1            		pop	af		;'2'=copy mode 1, '4'=copy mode 2(z-flag=1)
  6BF5  F5            		push	af		;'2'=copy mode 1, '4'=copy mode 2(z-flag=1)
  6BF6  200A          		jr	nz,LCP66NZ	;copy mode 1
  6BF8  CBEC          		set	5,h
  6BFA  CD8DFE        		call	READRAM
  6BFD  CD371A        		call	PRINTER
  6C00  CBAC          		res	5,h
  6C02                	LCP66NZ:
  6C02  19            		add	hl,de		;de=CLMN66
  6C03  10E9          		djnz	LCP66LP2
                      	
  6C05  11BFE0        		ld	de,0-CLMN66*200-1
  6C08                	LCP66NEXT:
  6C08  19            		add	hl,de
  6C09  CD4D27        		call	STOPESC
  6C0C  CD6E6C        		call	LCP66NL
  6C0F  0D            		dec	c
  6C10  20B7          		jr	nz,LCP66LP1
                      	
                      	;footer
  6C12  3E41          		ld	a,'A'
  6C14  CD786C        		call	LCP66ESC
  6C17  0608          		ld	b,08h
  6C19                	LCP66LP3:
  6C19  3E0A          		ld	a,0ah
  6C1B  CD371A        		call	PRINTER
  6C1E  10F9          		djnz	LCP66LP3
  6C20  F1            		pop	af
  6C21  C9            		ret
                      	
                      	
                      	;text mode
  6C22                	LCP66TXT:
  6C22  0614          		ld	b,ROWS66
  6C24                	LCP66LP4:
  6C24  CB7E          		bit	7,(hl)		;semi-graphic character?
  6C26  CBD4          		set	2,h		;character address
  6C28  CD8DFE        		call	READRAM		;no change in z-flag
  6C2B  CB94          		res	2,h		;attribute address
  6C2D  CDA014        		call	CGROM		;no change in z-flag
  6C30  2802          		jr	z,LCP66LP5
  6C32  CBE2          		set	4,d		;de+=1000h
                      	
  6C34                	LCP66LP5:
  6C34  F1            		pop	af		;'2'=copy mode 1, '4'=copy mode 2(z-flag=1)
  6C35  F5            		push	af		;'2'=copy mode 1, '4'=copy mode 2(z-flag=1)
  6C36  D5            		push	de
  6C37  CD9435        		call	READCGROM	;no change in z-flag
  6C3A  7A            		ld	a,d
  6C3B  201A          		jr	nz,LCP66TPUT	;copy mode 1
                      	
                      	;copy mode 2
  6C3D  2F            		cpl
  6C3E  CB66          		bit	4,(hl)		;background-red
  6C40  2001          		jr	nz,BACKRNZ
  6C42  AF            		xor	a
  6C43                	BACKRNZ:
  6C43  CB46          		bit	0,(hl)		;foreground-red
  6C45  2801          		jr	z,FORERZ
  6C47  B2            		or	d
  6C48                	FORERZ:
  6C48  CD371A        		call	PRINTER
  6C4B  7A            		ld	a,d
  6C4C  2F            		cpl
  6C4D  CB6E          		bit	5,(hl)		;background-green
  6C4F  2001          		jr	nz,BACKGNZ
  6C51  AF            		xor	a
  6C52                	BACKGNZ:
  6C52  CB4E          		bit	1,(hl)		;foreground-green
  6C54  2801          		jr	z,LCP66TPUT
  6C56  B2            		or	d
                      	
  6C57                	LCP66TPUT:
  6C57  CD371A        		call	PRINTER
  6C5A  D1            		pop	de
  6C5B  1C            		inc	e		;inc de
  6C5C  7B            		ld	a,e
  6C5D  E60F          		and	0fh
  6C5F  FE0A          		cp	0ah
  6C61  20D1          		jr	nz,LCP66LP5
                      	
  6C63  112800        		ld	de,CLMN66
  6C66  19            		add	hl,de
  6C67  10BB          		djnz	LCP66LP4
                      		
  6C69  11DFFC        		ld	de,0-CLMN66*ROWS66-1
  6C6C  189A          		jr	LCP66NEXT
                      	
                      	
                      	;put new line to printer
                      	;destroy: af
  6C6E                	LCP66NL:
  6C6E  3E0D          		ld	a,0dh
  6C70  CD371A        		call	PRINTER
  6C73  3E0A          		ld	a,0ah
  6C75  C3371A        		jp	PRINTER
                      	
                      	
                      	;put ESC+data to printer
                      	;input: a=data
                      	;destroy: af
  6C78                	LCP66ESC:
  6C78  F5            		push	af
  6C79  3E1B          		ld	a,1bh
  6C7B  CD371A        		call	PRINTER
  6C7E  F1            		pop	af
  6C7F  C3371A        		jp	PRINTER
                      	
                      	
                      	
                      	;initialize internal FDD
                      	;input: hl=DRVBIT
                      	;destroy: af,bc,de
  6C82                	INIDSKINT:
                      	;check if 6601 or not
  6C82  CD546E        		call	FDSTBY
  6C85  01D000        		ld	bc,00d0h
  6C88  11FF00        		ld	de,00ffh
  6C8B  ED51          		out	(c),d
  6C8D  04            		inc	b
  6C8E  ED59          		out	(c),e
  6C90  ED78          		in	a,(c)
  6C92  BB            		cp	e
  6C93  C0            		ret	nz		;mkII
  6C94  05            		dec	b
  6C95  ED78          		in	a,(c)
  6C97  BA            		cp	d
  6C98  C0            		ret	nz		;mkII
                      	
  6C99  CD4C6E        		call	MOTORABON
  6C9C  CD816D        		call	SPECIFY
  6C9F                	DRIVEA:
  6C9F  AF            		xor	a		;drive
  6CA0  CD936D        		call	RECALIB
  6CA3  3802          		jr	c,DRIVEB
  6CA5  CBE6          		set	4,(hl)		;DRVBIT
  6CA7                	DRIVEB:
  6CA7  3E01          		ld	a,01h		;drive
  6CA9  CD936D        		call	RECALIB
  6CAC  D8            		ret	c
  6CAD  CBEE          		set	5,(hl)		;DRVBIT
  6CAF  C9            		ret
                      	
                      	
                      	;read/write/check internal FDD 
                      	;output: c-flag(1=error)
                      	;destroy: af,bc,de,hl
  6CB0                	DISKINT:
  6CB0  CD296E        		call	MOTORON
                      	;	ld	a,(DRIVE)
                      	;	call	RECALIB
                      	;	ret	c
  6CB3  CD546E        		call	FDSTBY
  6CB6  CDA46D        		call	SEEK
  6CB9  D8            		ret	c
  6CBA                	DSKLP:
  6CBA  2171FE        		ld	hl,RMSECT
  6CBD  7E            		ld	a,(hl)
  6CBE  B7            		or	a
  6CBF  C8            		ret	z
  6CC0  0604          		ld	b,04h
  6CC2  B8            		cp	b
  6CC3  3001          		jr	nc,GE4SECT	;(RMSECT)>=4
  6CC5  47            		ld	b,a		;(RMSECT)<4
  6CC6                	GE4SECT:
  6CC6  90            		sub	b
  6CC7  77            		ld	(hl),a
  6CC8  78            		ld	a,b
  6CC9  3278FE        		ld	(NSECT),a
  6CCC  3A70FE        		ld	a,(RDWRCHK)
  6CCF  3C            		inc	a		;ffh=write
  6CD0  CC056D        		call	z,WT4SECT	;z-flag is set
  6CD3  C4E06C        		call	nz,RD4SECT
  6CD6  D8            		ret	c
  6CD7  2174FE        		ld	hl,SECTOR
  6CDA  7E            		ld	a,(hl)
  6CDB  C604          		add	a,04h
  6CDD  77            		ld	(hl),a
  6CDE  18DA          		jr	DSKLP
                      	
                      	
                      	;read 4 sectors
                      	;input: de
                      	;output: de=de+data size, c-flag(1=error), z-flag=1
                      	;destroy: af,bc,hl
  6CE0                	RD4SECT:
  6CE0  CD206E        		call	SETBSIZ
  6CE3  CD1B6D        		call	RWDATA
  6CE6  D8            		ret	c		;error
  6CE7  EB            		ex	de,hl
  6CE8  0ED0          		ld	c,FDBUF
  6CEA  3A78FE        		ld	a,(NSECT)
  6CED                	RDATALP:
  6CED  F5            		push	af		;
  6CEE  2B            		dec	hl
  6CEF  24            		inc	h
  6CF0  06FF          		ld	b,0ffh
  6CF2  3A70FE        		ld	a,(RDWRCHK)
  6CF5  B7            		or	a
  6CF6  2005          		jr	nz,CHECK
  6CF8  EDBA          		indr			;b=ff-01
  6CFA  EDAA          		ind			;b=00
  6CFC  24            		inc	h
  6CFD                	CHECK:
  6CFD  23            		inc	hl
  6CFE  0C            		inc	c
  6CFF  F1            		pop	af		;
  6D00  3D            		dec	a
  6D01  20EA          		jr	nz,RDATALP
  6D03  EB            		ex	de,hl
  6D04  C9            		ret
                      	
                      	
                      	;write 4 sectors
                      	;input: de
                      	;output: de=de+data size, c-flag(1=error), z-flag=1
                      	;destroy: af,bc,hl
  6D05                	WT4SECT:
  6D05  CD206E        		call	SETBSIZ
  6D08  EB            		ex	de,hl
  6D09  0ED0          		ld	c,FDBUF
  6D0B  3A78FE        		ld	a,(NSECT)
  6D0E                	WDATALP:
  6D0E  2B            		dec	hl
  6D0F  24            		inc	h
  6D10  0600          		ld	b,00h
  6D12  EDBB          		otdr			;b=ff-00
  6D14  23            		inc	hl
  6D15  24            		inc	h
  6D16  0C            		inc	c
  6D17  3D            		dec	a
  6D18  20F4          		jr	nz,WDATALP
  6D1A  EB            		ex	de,hl
                      	;	call	RWDATA
                      	;	ret
                      	
                      	
                      	;read data or write data
                      	;output: c-flag(1=error), z-flag=1
                      	;destroy: af,b,hl
  6D1B                	RWDATA:
  6D1B  AF            		xor	a
  6D1C  3277FE        		ld	(RETRY),a
  6D1F                	RWDTLP:
  6D1F  CDEC6D        		call	DRVRDY
  6D22  3A70FE        		ld	a,(RDWRCHK)
  6D25  3C            		inc	a		;00=read,01=check,ff=write
  6D26  3E46          		ld	a,46h		;46h=read data command
  6D28  2001          		jr	nz,SETCMD
  6D2A  3D            		dec	a		;45h=write data command
  6D2B                	SETCMD:
  6D2B  CD0E6E        		call	WRTFDC
  6D2E  F3            		di
  6D2F  3A73FE        		ld	a,(DRIVE)	;drvie/head
  6D32  CD0E6E        		call	WRTFDC
  6D35  3A72FE        		ld	a,(TRACK)	;track
  6D38  CD0E6E        		call	WRTFDC
  6D3B  AF            		xor	a		;head
  6D3C  CD0E6E        		call	WRTFDC
  6D3F  3A74FE        		ld	a,(SECTOR)	;sector
  6D42  217D6D        		ld	hl,SETCMDTBL
  6D45  0605          		ld	b,05h
  6D47                	SETCMDLP:
  6D47  CD0E6E        		call	WRTFDC
  6D4A  7E            		ld	a,(hl)
  6D4B  23            		inc	hl
  6D4C  10F9          		djnz	SETCMDLP
                      	
  6D4E  3A70FE        		ld	a,(RDWRCHK)
  6D51  3C            		inc	a		;00=read,01=check,ff=write
  6D52  3EF2          		ld	a,0f2h		;read
  6D54  2002          		jr	nz,SETDMA
  6D56  3EF0          		ld	a,0f0h		;write
  6D58                	SETDMA:
  6D58  D3B1          		out	(FDCNTL),a
  6D5A  FB            		ei
  6D5B  CDF66D        		call	INTRPT
  6D5E  CD546E        		call	FDSTBY
  6D61  2179FE        		ld	hl,STATBF
  6D64  0607          		ld	b,07h
  6D66                	STATLP:
  6D66  CD006E        		call	READFDC
  6D69  77            		ld	(hl),a
  6D6A  23            		inc	hl
  6D6B  10F9          		djnz	STATLP
  6D6D  3A79FE        		ld	a,(STATBF)
  6D70  E6C0          		and	11000000b
  6D72  C8            		ret	z		;NT
  6D73  2177FE        		ld	hl,RETRY
  6D76  34            		inc	(hl)
  6D77  7E            		ld	a,(hl)
  6D78  C6F6          		add	a,0-0ah
  6D7A  30A3          		jr	nc,RWDTLP	;if retry<10
  6D7C  C9            		ret			;c-flag=1,z-flag=1
                      	
  6D7D                	SETCMDTBL:
  6D7D  01            		db	01h		;sector length (128*2^n byte/sector)
  6D7E  10            		db	10h		;EOT (End Of Track)
  6D7F  14            		db	14h		;GPL (GaP Length)
  6D80  FF            		db	0ffh		;DTL (DaTa Length)
                      	
                      	
                      	;FDC specify command
                      	;destroy: af
  6D81                	SPECIFY:
  6D81  CDEC6D        		call	DRVRDY
  6D84  3E03          		ld	a,03h		;specify
  6D86  CD0E6E        		call	WRTFDC
  6D89  3EBF          		ld	a,0bfh		;step rate time=5ms/10ms,
                      					;head unload time=240ms/480ms
  6D8B  CD0E6E        		call	WRTFDC
  6D8E  3E26          		ld	a,26h		;head load time=38ms/76ms, DMA mode
  6D90  C30E6E        		jp	WRTFDC
                      	
                      	
                      	;FDC recalibrate command
                      	;input: a=drive
                      	;output: c-flag(1=error)
                      	;destroy: af,b
  6D93                	RECALIB:
  6D93  F5            		push	af
  6D94  CDEC6D        		call	DRVRDY
  6D97  3E07          		ld	a,07h		;recalibrate command
  6D99  CD0E6E        		call	WRTFDC
  6D9C  F1            		pop	af		;drive/head
  6D9D  CD0E6E        		call	WRTFDC
  6DA0  0600          		ld	b,00h		;track
  6DA2  1815          		jr	SENSE
                      	
                      	
                      	;FDC seek command
                      	;output: c-flag(1=error)
                      	;destroy: af,b
  6DA4                	SEEK:
  6DA4  CDEC6D        		call	DRVRDY
  6DA7  3E0F          		ld	a,0fh		;seek command
  6DA9  CD0E6E        		call	WRTFDC
  6DAC  3A73FE        		ld	a,(DRIVE)	;drive/head
  6DAF  CD0E6E        		call	WRTFDC
  6DB2  3A72FE        		ld	a,(TRACK)	;track
  6DB5  CD0E6E        		call	WRTFDC
  6DB8  47            		ld	b,a		;track
                      	;	jr	SENSE
                      	
                      	
                      	;FDC sense command
                      	;input: b=track
                      	;output: c-flag(1=error)
                      	;destroy: af
  6DB9                	SENSE:
  6DB9  CDF66D        		call	INTRPT
  6DBC  CDE56D        		call	FDCRDY
  6DBF  3E08          		ld	a,08h		;sense interrupt status command
  6DC1  CD0E6E        		call	WRTFDC
  6DC4  CD006E        		call	READFDC		;st0
  6DC7  3279FE        		ld	(STATBF),a
  6DCA  CD006E        		call	READFDC		;track
  6DCD  327AFE        		ld	(STATBF+1),a
                      	;error check
  6DD0  3A79FE        		ld	a,(STATBF)
  6DD3  E6D0          		and	11010000b	;bit6-7:interrupt code, bit4=fault
  6DD5  200C          		jr	nz,SENSEER
  6DD7  3A79FE        		ld	a,(STATBF)
  6DDA  E620          		and	00100000b	;seek End
  6DDC  28DB          		jr	z,SENSE
  6DDE  3A7AFE        		ld	a,(STATBF+1)	;track
  6DE1  B8            		cp	b
  6DE2  C8            		ret	z
  6DE3                	SENSEER:
  6DE3  37            		scf
  6DE4  C9            		ret
                      	
                      	
                      	;wait for FDC ready
                      	;destroy: af
  6DE5                	FDCRDY:
  6DE5  DBDC          		in	a,(FDCSTAT)
  6DE7  E610          		and	00010000b
  6DE9  20FA          		jr	nz,FDCRDY
  6DEB  C9            		ret
                      	
                      	
                      	;wait for FDC and drives ready
                      	;destroy: af
  6DEC                	DRVRDY:
  6DEC  CDE56D        		call	FDCRDY
  6DEF  DBDC          		in	a,(FDCSTAT)
  6DF1  E60F          		and	00001111b
  6DF3  20F7          		jr	nz,DRVRDY
  6DF5  C9            		ret
                      	
                      	
                      	;wait for FDC interrupt
                      	;destroy: af
  6DF6                	INTRPT:
  6DF6  3EFE          		ld	a,0feh
  6DF8  D3B3          		out	(B2CNTL),a	;use b2-bit0?
  6DFA  DBB2          		in	a,(FDCINT)	;bit0=1:interrupt
  6DFC  0F            		rrca
  6DFD  30F7          		jr	nc,INTRPT
  6DFF  C9            		ret
                      	
                      	
                      	;read data from FDC
                      	;destroy: f
                      	;output: a
  6E00                	READFDC:
  6E00  DBDC          		in	a,(FDCSTAT)
                      	;	and	11000000b
                      	;	cp	11000000b	;RQM=1,DIO=1(output)
                      	;	jr	nz,READFDC
  6E02  07            		rlca
  6E03  30FB          		jr	nc,READFDC	;wait for RQM=1
  6E05  07            		rlca
  6E06  3803          		jr	c,READOK	;DIO=1(output)
  6E08  3EFF          		ld	a,0ffh
  6E0A  C9            		ret
  6E0B                	READOK:
  6E0B  DBDD          		in	a,(FDCDATA)
  6E0D  C9            		ret
                      	
                      	
                      	;write data to FDC
                      	;destroy: none
  6E0E                	WRTFDC:
  6E0E  F5            		push	af
  6E0F                	WRTFDCLP:
  6E0F  DBDC          		in	a,(FDCSTAT)
                      	;	and	11000000b
                      	;	cp	10000000b	;RQM=1,DIO=0(input)
                      	;	jr	nz,WRTFDCLP
  6E11  07            		rlca
  6E12  30FB          		jr	nc,WRTFDCLP	;wait for RQM=1
  6E14  07            		rlca
  6E15  3005          		jr	nc,WRTOK	;DIO=0(input)
  6E17  CD006E        		call	READFDC
  6E1A  18F3          		jr	WRTFDCLP
  6E1C                	WRTOK:
  6E1C  F1            		pop	af
  6E1D  D3DD          		out	(FDCDATA),a
  6E1F  C9            		ret
                      	
                      	
                      	;set FD buffer size
                      	;destroy: af
  6E20                	SETBSIZ:
  6E20  3A78FE        		ld	a,(NSECT)
  6E23  D610          		sub	10h
  6E25  2F            		cpl
  6E26  D3DA          		out	(BUFSIZ),a
  6E28  C9            		ret
                      	
                      	
                      	;motor on
                      	;input: a=drive (0 or 1)
                      	;destroy: af
  6E29                	MOTORON:
  6E29  FE01          		cp	01h
  6E2B  3D            		dec	a
  6E2C  DBD4          		in	a,(MOTORST)	;motor status
  6E2E  3808          		jr	c,AON
  6E30  C0            		ret	nz
                      	
  6E31                	BON:
  6E31  CB4F          		bit	1,a
  6E33  C8            		ret	z		;status=on
  6E34  E6FD          		and	0fdh		;driveB on (bit1=0)
  6E36  1805          		jr	ABON
                      	
  6E38                	AON:
  6E38  CB47          		bit	0,a
  6E3A  C8            		ret	z		;status=on
  6E3B  E6FE          		and	0feh		;driveA on (bit0=0)
                      	
  6E3D                	ABON:
  6E3D  D3D6          		out	(MOTOR),a	;motor on
                      	
  6E3F  3E08          		ld	a,08h
  6E41  D3DE          		out	(ADJUST),a	;compensation?
                      	
  6E43  C5            		push	bc
  6E44  010030        		ld	bc,3000h	;about 0.6sec
  6E47  CDE825        		call	WAITLP
  6E4A  C1            		pop	bc
  6E4B  C9            		ret
                      	
                      	;destroy: af
  6E4C                	MOTORABON:
  6E4C  DBD4          		in	a,(MOTORST)	;motor status
  6E4E  E603          		and	03h
  6E50  C8            		ret	z		;A,B=on
  6E51  AF            		xor	a
  6E52  18E9          		jr	ABON
                      	
                      	
                      	;set FDD stand-by mode (change to internal FDD)
                      	;destroy: a
  6E54                	FDSTBY:
  6E54  3EF3          		ld	a,0f3h
  6E56  D3B1          		out	(FDCNTL),a
  6E58  C9            		ret
                      	
                      	
                      	;continued from PSET
  6E59                	PSET66:
  6E59  E5            		push	hl
  6E5A  CDBA15        		call	SXY2AD
  6E5D  57            		ld	d,a
  6E5E  3A92FD        		ld	a,(SCREEN1)
  6E61  3D            		dec	a
  6E62  2815          		jr	z,PSET662
  6E64  F2956E        		jp	p,PSET6634
                      	
                      	;screen mode 1
  6E67                	PSET661:
  6E67  CD8DFE        		call	READRAM
  6E6A  E68F          		and	8fh
  6E6C  5F            		ld	e,a
  6E6D  3AACFE        		ld	a,(ATTDAT)
  6E70  07            		rlca
  6E71  07            		rlca
  6E72  07            		rlca
  6E73  07            		rlca
  6E74  A2            		and	d
  6E75  B3            		or	e
  6E76  77            		ld	(hl),a
  6E77  E1            		pop	hl
  6E78  C9            		ret
                      	
                      	
                      	;screen mode 2
  6E79                	PSET662:
  6E79  CB94          		res	2,h
  6E7B  CD8DFE        		call	READRAM
  6E7E  E680          		and	80h		;
  6E80  3AACFE        		ld	a,(ATTDAT)
  6E83  77            		ld	(hl),a
                      	
  6E84  CBD4          		set	2,h
  6E86  2004          		jr	nz,PS662SEMI
  6E88  CD1D16        		call	GETSP
  6E8B  77            		ld	(hl),a
                      	
  6E8C                	PS662SEMI:
  6E8C  CD8DFE        		call	READRAM
  6E8F  E60F          		and	0fh
  6E91  B2            		or	d
  6E92  77            		ld	(hl),a
  6E93  E1            		pop	hl
  6E94  C9            		ret
                      	
                      	;screen mode 3,4
  6E95                	PSET6634:
  6E95  CD7066        		call	PRT466
  6E98  E1            		pop	hl
  6E99  C9            		ret
                      	
                      	
                      	;check and round graphic coordinates (x,y) for mode 5
                      	;input: bc=x, de=y
                      	;output: bc=x, de=y
                      	;destroy: af,hl
  6E9A                	CHKGXY66:
  6E9A  78            		ld	a,b
  6E9B  B2            		or	d
  6E9C  FACB03        		jp	m,FCERR
  6E9F  213F01        		ld	hl,319
                      	;	or	a
  6EA2  ED42          		sbc	hl,bc
  6EA4  3003          		jr	nc,CHKXOK66
  6EA6  013F01        		ld	bc,319
                      	
  6EA9                	CHKXOK66:
  6EA9  3A92FD        		ld	a,(SCREEN1)
  6EAC  0F            		rrca
  6EAD  2F            		cpl
  6EAE  21A6FD        		ld	hl,CONSOL3
  6EB1  A6            		and	(hl)
  6EB2  0F            		rrca
  6EB3  21C700        		ld	hl,199
  6EB6  3003          		jr	nc,NOFKEY66
  6EB8  21BD00        		ld	hl,199-10
  6EBB                	NOFKEY66:
  6EBB  E7            		rst	CPHLDE
  6EBC  3001          		jr	nc,CHKYOK66
  6EBE  EB            		ex	de,hl
  6EBF                	CHKYOK66:
  6EBF  3A92FD        		ld	a,(SCREEN1)
  6EC2  3D            		dec	a
  6EC3  280D          		jr	z,ROUNDY662
  6EC5  F2DA6E        		jp	p,ROUNDX66	;screen mode 3,4
                      	
                      	;round off y for screen mode 1
  6EC8                	ROUNDY661:
  6EC8  CD076F        		call	DIV10
  6ECB  7B            		ld	a,e
  6ECC  87            		add	a,a		;*2
  6ECD  87            		add	a,a		;*4
  6ECE  83            		add	a,e		;*5
  6ECF  87            		add	a,a		;*10
  6ED0  1807          		jr	ROUNDY662END
                      	
                      	;round off y for screen mode 2
  6ED2                	ROUNDY662:
  6ED2  CD036F        		call	DIV5
                      	
  6ED5  7B            		ld	a,e
  6ED6  87            		add	a,a		;*2
  6ED7  87            		add	a,a		;*4
  6ED8  83            		add	a,e		;*5
                      	
  6ED9                	ROUNDY662END:
  6ED9  5F            		ld	e,a
                      	
  6EDA                	ROUNDX66:
  6EDA  3A92FD        		ld	a,(SCREEN1)
  6EDD  57            		ld	d,a
  6EDE  14            		inc	d
  6EDF  3EF0          		ld	a,0f0h
  6EE1                	ROUNDX66LP:
  6EE1  37            		scf
  6EE2  1F            		rra			;f8,fc,fe,ff
  6EE3  15            		dec	d
  6EE4  20FB          		jr	nz,ROUNDX66LP
                      	
  6EE6  A1            		and	c
  6EE7  4F            		ld	c,a
  6EE8  C9            		ret			;d=0
                      	
                      	
                      	;continued from INCGY
  6EE9                	INCGY66:
  6EE9  3A92FD        		ld	a,(SCREEN1)
  6EEC  3D            		dec	a
  6EED  3E05          		ld	a,05h		;BASIC mode 5, screen mode 2
  6EEF  CAF96E        		jp	z,INCGYZ
  6EF2  3E01          		ld	a,01h		;BASIC mode 5, screen mode 3,4
  6EF4  F2F96E        		jp	p,INCGYZ
  6EF7  3E0A          		ld	a,0ah		;BASIC mode 5, screen mode 1
  6EF9                	INCGYZ:
  6EF9  ED5BB3FE      		ld	de,(GRPY3)
  6EFD  83            		add	a,e
  6EFE  5F            		ld	e,a
  6EFF  32B3FE        		ld	(GRPY3),a	;<256
  6F02  C9            		ret
                      	
                      	
                      	;e=e/10 (or e/5)
                      	;input: e
                      	;output: e=int(e/10), a=e mod 10
                      	;destroy: f,hl
  6F03                	DIV5:
  6F03  2606          		ld	h,6
  6F05  1802          		jr	DIV10OR5
  6F07                	DIV10:
  6F07  2605          		ld	h,5
  6F09                	DIV10OR5:
  6F09  7B            		ld	a,e
  6F0A  1E00          		ld	e,00h
  6F0C  2EA0          		ld	l,10100000b
  6F0E                	DIV10LP:
  6F0E  BD            		cp	l
  6F0F  3801          		jr	c,DIV10C
  6F11  95            		sub	l
  6F12                	DIV10C:
  6F12  3F            		ccf
  6F13  CB13          		rl	e
  6F15  CB3D          		srl	l
  6F17  25            		dec	h
  6F18  20F4          		jr	nz,DIV10LP
  6F1A  C9            		ret
                      	
                      	
  6F1B                	BFG662:
  6F1B  EB            		ex	de,hl
  6F1C  2AADFE        		ld	hl,(GRPX2)	;larger x
  6F1F  ED4BB1FE      		ld	bc,(GRPX3)	;smaller x
  6F23  79            		ld	a,c
  6F24  E6F8          		and	0f8h
  6F26  4F            		ld	c,a
                      	;	or	a
  6F27  ED42          		sbc	hl,bc
                      	
  6F29  7D            		ld	a,l
  6F2A  CB3C          		srl	h
  6F2C  1F            		rra
  6F2D  CB3F          		srl	a
  6F2F  CB3F          		srl	a		;int(larger/8)-int(smaller/8)
                      	
  6F31  EB            		ex	de,hl		;hl=VRAM address
  6F32  D1            		pop	de		;d=left part mask, e=right part mask
  6F33  C1            		pop	bc		;c=lines
                      	
  6F34  47            		ld	b,a
                      	;	or	a
  6F35  2003          		jr	nz,BFG66LP3
                      	
  6F37  7A            		ld	a,d
  6F38  A3            		and	e
  6F39  57            		ld	d,a
                      	
  6F3A                	BFG66LP3:
                      	;left part
  6F3A  C5            		push	bc
  6F3B  D5            		push	de
  6F3C  4B            		ld	c,e		;;
  6F3D  CD7066        		call	PRT466
                      	
  6F40  05            		dec	b		;
  6F41  FA5C6F        		jp	m,BFGNEXT66	;
                      	
                      	;middle part
  6F44  E5            		push	hl		;VRAM address
  6F45  280F          		jr	z,BFGRGT66	;
                      	
  6F47                	BFG66LP4:
  6F47  23            		inc	hl
  6F48  3A60FE        		ld	a,(ATTDAT2)
  6F4B  77            		ld	(hl),a
  6F4C  3AACFE        		ld	a,(ATTDAT)
  6F4F  CBAC          		res	5,h
  6F51  77            		ld	(hl),a
  6F52  CBEC          		set	5,h
  6F54  10F1          		djnz	BFG66LP4
                      	
                      	;right part
  6F56                	BFGRGT66:
  6F56  23            		inc	hl
  6F57  51            		ld	d,c		;;
  6F58  CD7066        		call	PRT466
  6F5B  E1            		pop	hl		;VRAM address
                      	
  6F5C                	BFGNEXT66:
  6F5C  012800        		ld	bc,0028h
  6F5F  09            		add	hl,bc
  6F60  D1            		pop	de
  6F61  C1            		pop	bc
  6F62  0D            		dec	c
  6F63  20D5          		jr	nz,BFG66LP3
  6F65  C3BA2E        		jp	LINEEND2
                      	
                      	
                      	;line bf (screen mode 1,2)
                      	;hl=larger y, a=screen mode-1
  6F68                	BF6612:
  6F68  ED53AFFE      		ld	(GRPY2),de
  6F6C  CD1F2E        		call	LINE
  6F6F  E7            		rst	CPHLDE
  6F70  CABA2E        		jp	z,LINEEND2
                      	
  6F73  E5            		push	hl
  6F74  3A92FD        		ld	a,(SCREEN1)
  6F77  B7            		or	a
  6F78  3E05          		ld	a,05h
  6F7A  210A00        		ld	hl,000ah
  6F7D  2801          		jr	z,BF6612Z
  6F7F  6F            		ld	l,a
  6F80                	BF6612Z:
  6F80  19            		add	hl,de		;+12 or +4
  6F81  EB            		ex	de,hl
  6F82  E1            		pop	hl
  6F83  18E3          		jr	BF6612
                      	
                      	
                      	;LINE command for mode 5
  6F85                	_C_LIN66:ds	C_LIN66-_C_LIN66
  70C9                		org	C_LIN66
                      	
  70C9  CD712D        		call	PRELINE
  70CC  D21F2E        		jp	nc,LINE
  70CF  2051          		jr	nz,LINEB66
  70D1  1814          		jr	LINBF66
                      	
                      	
                      	;input: bc=x1, de=y1, (GRPX2)=x2, (GRPY2)=y2,
                      	; (ATTDAT)=attribute, (ATTDAT2)=attribute2
                      	;destroy: af,bc,de
  70D3                	_LINBF66:ds	LINBF66-_LINBF66
  70E7                		org	LINBF66
                      	
  70E7  E5            		push	hl
  70E8  CDBA2B        		call	PUSHGXY
  70EB  CDAB2B        		call	SORTXY2
                      	
  70EE  3A92FD        		ld	a,(SCREEN1)
  70F1  FE02          		cp	02h
  70F3  DA686F        		jp	c,BF6612
                      	
                      	;line bf (graphic mdoe)
  70F6                	BFG66:
  70F6  3AADFE        		ld	a,(GRPX2)	;larger x lower byte
  70F9  2001          		jr	nz,BF664
  70FB  3C            		inc	a		;screen mode 3
  70FC                	BF664:
  70FC  E607          		and	07h
  70FE  3C            		inc	a
  70FF  47            		ld	b,a
  7100  AF            		xor	a
  7101                	BFG66LP1:
  7101  37            		scf
  7102  1F            		rra			;right part mask
  7103  10FC          		djnz	BFG66LP1
                      	
                      	;	or	a
  7105  ED52          		sbc	hl,de
  7107  2C            		inc	l
  7108  E5            		push	hl		;l=lines
                      	
  7109  6F            		ld	l,a		;right part mask
                      	
  710A  79            		ld	a,c		;smaller x
  710B  E607          		and	07h
  710D  47            		ld	b,a
  710E  3EFF          		ld	a,0ffh
  7110  2804          		jr	z,BFG66Z
  7112                	BFG66LP2:
  7112  CB3F          		srl	a
  7114  10FC          		djnz	BFG66LP2
  7116                	BFG66Z:
  7116  67            		ld	h,a		;left part mask
  7117  ED4BB1FE      		ld	bc,(GRPX3)	;smaller x
                      	
  711B  E5            		push	hl
  711C  CDB215        		call	GXY2AD
                      	
  711F  C31B6F        		jp	BFG662
                      	
                      	
                      	;input: bc=x1, de=y1, (GRPX2)=x2, (GRPY2)=y2,
                      	; (ATTDAT)=attribute1, (ATTDAT2)=attribute2
                      	;destroy: af,bc,de
  7122                	_LINEB66:ds	LINEB66-_LINEB66
  7122                		org	LINEB66
                      	
  7122  C3352E        		jp	LINEB
                      	
                      	
                      	;C_PAI66	equ	714ch
                      	;PAINT command for mode 5
                      	;_C_PAI66:ds	C_PAI66-_C_PAI66
                      	;	org	C_PAI66
  7125                	C_PAI66:
  7125  CDC62D        		call	SKIPAT
  7128  CDF22C        		call	GETGXY
  712B  C5            		push	bc		;X
  712C  D5            		push	de		;Y
  712D  3A93FD        		ld	a,(COLOR1)
  7130  CCE30D        		call	z,INT1INC
  7133  CDC015        		call	SETATT
  7136  7E            		ld	a,(hl)
  7137  FE2C          		cp	','
  7139  3AC6FE        		ld	a,(BORDERC)
  713C  CCE30D        		call	z,INT1INC
  713F  5F            		ld	e,a
                      	
                      	;input: e=color, (sp)=y, (sp+2)=x
                      	;used by PERDU, MINTMARK
  7140                	_PA66POP:ds	PA66POP-_PA66POP
  7161                		org	PA66POP
                      	
  7161  7B            		ld	a,e
  7162  CDF369        		call	SETBO66
  7165  D1            		pop	de		;Y
  7166  C1            		pop	bc		;X
  7167  C36A71        		jp	PAINT66
                      	
                      	
                      	;input: bc=x, de=y, (ATTDAT)=attribute1, (ATTDAT2)=attribute2
                      	; (BORDERA)=attribute, (BORDA2)=attribute2, (BORDERC)=color
                      	;destroy: af,bc,de
  716A                	_PAINT66:ds	PAINT66-_PAINT66
  716A                		org	PAINT66
                      	
  716A  E5            		push	hl
  716B  CD9A6E        		call	CHKGXY66
  716E  E1            		pop	hl
  716F  3A92FD        		ld	a,(SCREEN1)
  7172  3D            		dec	a
  7173  C8            		ret	z		;screen mode 2
  7174  ED43B1FE      		ld	(GRPX3),bc
  7178  ED53B3FE      		ld	(GRPY3),de
  717C  E5            		push	hl
  717D  FA8971        		jp	m,PAINT661	;screen mode 1
  7180  CDB215        		call	GXY2AD
  7183  57            		ld	d,a
  7184                	PAMAIN66:
  7184  CD9071        		call	PADWN66
  7187  E1            		pop	hl
  7188  C9            		ret
                      	
  7189                	PAINT661:
  7189  CDB215        		call	GXY2AD
  718C  16FF          		ld	d,0ffh
  718E  18F4          		jr	PAMAIN66
                      	
                      	
  7190                	PADWN66:
  7190  CDE974        		call	PALINE66
  7193  C8            		ret	z
                      	
  7194  E5            		push	hl		;address
  7195  D5            		push	de		;bit
  7196  ED4BB5FE      		ld	bc,(PAWRK)
  719A  C5            		push	bc
  719B  ED4BB1FE      		ld	bc,(GRPX3)
  719F  C5            		push	bc
  71A0  3AB3FE        		ld	a,(GRPY3)
  71A3  F5            		push	af
                      	
  71A4  CD9374        		call	PAUP662
                      	
  71A7  F1            		pop	af
  71A8  32B3FE        		ld	(GRPY3),a
  71AB  E1            		pop	hl
  71AC  22B1FE        		ld	(GRPX3),hl
  71AF  E1            		pop	hl
  71B0  22B5FE        		ld	(PAWRK),hl
  71B3  D1            		pop	de		;bit
  71B4  E1            		pop	hl		;address
                      	
  71B5                	PADWN662:
  71B5  D5            		push	de
  71B6  CDE96E        		call	INCGY66
  71B9  7B            		ld	a,e
  71BA  D1            		pop	de
  71BB  FEC8          		cp	200
  71BD  D0            		ret	nc
                      	
  71BE  012800        		ld	bc,CLMN66
  71C1  09            		add	hl,bc
  71C2  CD9375        		call	READVRAM
                      	
  71C5  AF            		xor	a
  71C6  32B7FE        		ld	(PACNT),a
                      	
  71C9                	PADWN66LP1:
  71C9  CD3A76        		call	SRCHOK66
  71CC  381C          		jr	c,CALLDWN66
                      	
  71CE  ED4BB1FE      		ld	bc,(GRPX3)
  71D2  C5            		push	bc
  71D3  ED4BB3FE      		ld	bc,(GRPY3)
  71D7  C5            		push	bc
                      	
  71D8  E5            		push	hl
  71D9  D5            		push	de
  71DA  21B7FE        		ld	hl,PACNT
  71DD  34            		inc	(hl)
  71DE  CDCC34        		call	CHKFRE
  71E1  D1            		pop	de
  71E2  E1            		pop	hl
                      	
  71E3  E5            		push	hl		;address
  71E4  D5            		push	de		;bit
                      	
  71E5  CD4976        		call	SRCHNG66
  71E8  30DF          		jr	nc,PADWN66LP1
                      	
  71EA                	CALLDWN66:
  71EA  3AB7FE        		ld	a,(PACNT)
  71ED  B7            		or	a
  71EE  C8            		ret	z
                      	
  71EF                	PADWN66LP2:
  71EF  D1            		pop	de		;bit
  71F0  E1            		pop	hl		;address
  71F1  C1            		pop	bc
  71F2  ED43B3FE      		ld	(GRPY3),bc
  71F6  C1            		pop	bc
  71F7  ED43B1FE      		ld	(GRPX3),bc
                      	
  71FB  3D            		dec	a
  71FC  32B7FE        		ld	(PACNT),a
  71FF  288F          		jr	z,PADWN66
  7201  F5            		push	af
  7202  CD9071        		call	PADWN66
  7205  F1            		pop	af
  7206  18E7          		jr	PADWN66LP2
                      	
                      	
                      	;KANJI command
  7208                	C_KANJ:
  7208  3A92FD        		ld	a,(SCREEN1)
  720B  FE02          		cp	02h
  720D  DACB03        		jp	c,FCERR
                      	
  7210  CDF22C        		call	GETGXY
  7213  C0            		ret	nz
                      	
  7214  ED43ADFE      		ld	(GRPX2),bc
  7218  ED53AFFE      		ld	(GRPY2),de
                      	
  721C  CD583F        		call	SKIPSPINC
  721F  224EFF        		ld	(PROGAD),hl
  7222  FE2C          		cp	','
  7224  3A93FD        		ld	a,(COLOR1)
  7227  C4E40D        		call	nz,INT1ARG
  722A  CDC015        		call	SETATT
  722D  7E            		ld	a,(hl)
  722E  FE2C          		cp	','
  7230  C0            		ret	nz
                      	
  7231                	KANJLP1:
  7231  CD583F        		call	SKIPSPINC
  7234  224EFF        		ld	(PROGAD),hl
  7237  FE2C          		cp	','
  7239  28F6          		jr	z,KANJLP1
                      	
  723B  CD603F        		call	CHKCLN
  723E  C8            		ret	z
                      	
  723F  CD4717        		call	ARG
  7242  3A25FF        		ld	a,(ARGTYP)
  7245  3D            		dec	a
  7246  2816          		jr	z,KANJSTR
  7248  F2C203        		jp	p,SNERR
                      	
  724B                	KANJNUM:
  724B  CD9F0C        		call	FTOI
  724E  EB            		ex	de,hl
  724F  CDBD72        		call	PRTKNJ
  7252                	KNEXT:
  7252  2AB0FD        		ld	hl,(GRPY1)
  7255  22AFFE        		ld	(GRPY2),hl
                      	
  7258  2A4EFF        		ld	hl,(PROGAD)
  725B  2B            		dec	hl
  725C  18D3          		jr	KANJLP1
                      	
                      	;string
  725E                	KANJSTR:
  725E  2A67FF        		ld	hl,(FAC1+1)
  7261  4E            		ld	c,(hl)
                      	
  7262  23            		inc	hl
  7263  23            		inc	hl
  7264  46            		ld	b,(hl)
  7265  23            		inc	hl
  7266  66            		ld	h,(hl)
  7267  68            		ld	l,b
                      	
  7268                	KANJLP2:
  7268  79            		ld	a,c
  7269  B7            		or	a
  726A  28E6          		jr	z,KNEXT
                      	
  726C  7E            		ld	a,(hl)
  726D  FE14          		cp	14h
  726F  2007          		jr	nz,KSTRNZ
  7271  0D            		dec	c
  7272  28DE          		jr	z,KNEXT
  7274  23            		inc	hl
  7275  7E            		ld	a,(hl)
  7276  D630          		sub	30h
  7278                	KSTRNZ:
  7278  E5            		push	hl		;string address
  7279  C5            		push	bc
  727A  CD8173        		call	KPRTCH
  727D  C1            		pop	bc
  727E  E1            		pop	hl		;string address
  727F  23            		inc	hl
  7280  0D            		dec	c
  7281  18E5          		jr	KANJLP2
                      	
                      	
                      	;read kanji ROM data
                      	;input: hl=address
                      	;output: h=left data, l=right data
                      	;destroy: none
  7283                	READKROM:
  7283  F5            		push	af
  7284  C5            		push	bc
  7285  3E03          		ld	a,03h
  7287  D3C3          		out	(0c3h),a	;port c2h direction: bit0,1=output
  7289  3E12          		ld	a,12h
  728B  F3            		di
  728C  D3F0          		out	(0f0h),a	;0000-3fff:voice/kanji ROM
  728E  3E01          		ld	a,01h
  7290  D3C2          		out	(0c2h),a	;kanji left
  7292  46            		ld	b,(hl)
  7293  3E03          		ld	a,03h
  7295  D3C2          		out	(0c2h),a	;kanji right
  7297  6E            		ld	l,(hl)
  7298  3E11          		ld	a,11h
  729A  D3F0          		out	(0f0h),a	;0000-7fff:BASIC ROM
  729C  FB            		ei
  729D  60            		ld	h,b
  729E  C1            		pop	bc
  729F  F1            		pop	af
  72A0  C9            		ret
                      	
                      	
                      	;read extended kanji ROM data
                      	;input: hl=address
                      	;output: h=left data, l=right data
                      	;destroy: none
  72A1                	READEKROM:
  72A1  F5            		push	af
  72A2  C5            		push	bc
  72A3  45            		ld	b,l
  72A4  0EFC          		ld	c,0fch
  72A6  ED61          		out	(c),h
  72A8  AF            		xor	a
  72A9  D3FF          		out	(0ffh),a
  72AB  0C            		inc	c
  72AC  ED60          		in	h,(c)		;c=fdh
  72AE  0C            		inc	c
  72AF  ED68          		in	l,(c)		;c=feh
  72B1  3D            		dec	a
  72B2  D3FF          		out	(0ffh),a	;a=ffh
  72B4  C1            		pop	bc
  72B5  F1            		pop	af
  72B6  C9            		ret
                      	
                      	
                      	;print kanji in CRT
                      	;input: hl=kanji code, (GRPX2)=x, (GRPY2)=y
                      	;output: bc=next X, de=Y
                      	;destroy: none
  72B7                	_PRTKNJ:ds	PRTKNJ-_PRTKNJ
  72BD                		org	PRTKNJ
                      	
  72BD  E5            		push	hl
  72BE  EB            		ex	de,hl
  72BF  210000        		ld	hl,0000h
  72C2  CDA172        		call	READEKROM
  72C5  7C            		ld	a,h
  72C6  B5            		or	l
  72C7  CAF472        		jp	z,KANJEXT	;check external kanji rom
                      	
  72CA  1B            		dec	de
  72CB  EB            		ex	de,hl
  72CC  010004        		ld	bc,0400h
                      	;	or	a
  72CF  ED42          		sbc	hl,bc
  72D1  D2CB03        		jp	nc,FCERR
  72D4  09            		add	hl,bc
                      	
  72D5  29            		add	hl,hl		;*2
  72D6  29            		add	hl,hl		;*4
  72D7  29            		add	hl,hl		;*8
  72D8  29            		add	hl,hl		;*16
                      	
  72D9  0610          		ld	b,10h
  72DB                	PRTKLP:
  72DB  C5            		push	bc
  72DC  E5            		push	hl		;kanji ROM address
  72DD  CD8372        		call	READKROM
  72E0  CDBF73        		call	CALLPRTHL
  72E3  E1            		pop	hl
  72E4  23            		inc	hl		;kanji ROM address
  72E5  C1            		pop	bc
  72E6  10F3          		djnz	PRTKLP
                      	
  72E8                	PRTKEND:
  72E8  ED5BAFFE      		ld	de,(GRPY2)
  72EC  011000        		ld	bc,0010h
  72EF  CDAE73        		call	KPRCHXY
  72F2  E1            		pop	hl
  72F3  C9            		ret
                      	
                      	
                      	;extended kanji ROM
  72F4                	KANJEXT:
  72F4  EB            		ex	de,hl
                      	
  72F5  112000        		ld	de,0020h
  72F8  E7            		rst	CPHLDE
  72F9  DACB03        		jp	c,FCERR
                      	
  72FC  110001        		ld	de,00ffh+1
  72FF  E7            		rst	CPHLDE
  7300  385D          		jr	c,KEXT816
                      	
                      	;	ld	de,01ffh+1
  7302  14            		inc	d
  7303  E7            		rst	CPHLDE
  7304  3847          		jr	c,KEXT88
                      	
  7306  7D            		ld	a,l
  7307  D621          		sub	21h
  7309  FE5E          		cp	7eh-21h+1	;hl=xx21-xx7e: ok
  730B  D2CB03        		jp	nc,FCERR	;hl=xx00-xx20, xx7f-xxff: error
                      	
  730E  7C            		ld	a,h
  730F  FE21          		cp	21h
  7311  DACB03        		jp	c,FCERR
  7314  FE27          		cp	27h
  7316  3811          		jr	c,KEXT1616
                      	
  7318  FE30          		cp	30h
  731A  DACB03        		jp	c,FCERR
  731D  FE4F          		cp	4fh
  731F  D2CB03        		jp	nc,FCERR
                      	
                      	
                      	;hl=3021h-4f7eh
                      	;16x16 character
                      	;JIS level-1 kanji set
                      	;JIS    =0xxaaaaa 0bbccccc
                      	;address=bbaaaaac cccc0000
  7322  7D            		ld	a,l
  7323  AC            		xor	h
  7324  E660          		and	60h
  7326  AC            		xor	h
  7327  1808          		jr	KEXT16162
                      	
                      	
                      	;hl=2120h-277eh
                      	;16x16 character
                      	;JIS    =00100aaa 0bbccccc
                      	;address=00bbaaac cccc0000
  7329                	KEXT1616:
  7329  CBAC          		res	5,h
  732B  7D            		ld	a,l
  732C  E660          		and	60h
  732E  0F            		rrca
  732F  0F            		rrca
  7330  B4            		or	h
  7331                	KEXT16162:
  7331  67            		ld	h,a
                      	
  7332  7D            		ld	a,l
  7333  87            		add	a,a
  7334  87            		add	a,a
  7335  87            		add	a,a
  7336  87            		add	a,a
  7337  6F            		ld	l,a
                      	
  7338  7C            		ld	a,h
  7339  8F            		adc	a,a
  733A  67            		ld	h,a
                      	
  733B  0610          		ld	b,10h
  733D                	KEXTLP1:
  733D  C5            		push	bc
  733E  E5            		push	hl		;kanji ROM address
  733F  CDA172        		call	READEKROM
  7342  CDBF73        		call	CALLPRTHL
  7345  E1            		pop	hl
  7346  23            		inc	hl		;kanji ROM address
  7347  C1            		pop	bc
  7348  10F3          		djnz	KEXTLP1
  734A  C3E872        		jp	PRTKEND
                      	
                      	
                      	;hl=100h-1ffh
                      	;8x8 character
  734D                	KEXT88:
  734D  7D            		ld	a,l
  734E  2AAFFE        		ld	hl,(GRPY2)
  7351  010800        		ld	bc,0008h
  7354  09            		add	hl,bc
  7355  22AFFE        		ld	(GRPY2),hl
  7358  6F            		ld	l,a
  7359  2602          		ld	h,02h
                      	
  735B  0604          		ld	b,04h
  735D  1803          		jr	KEXT8162
                      	
                      	;hl=20h-ffh
                      	;8x16 character
  735F                	KEXT816:
  735F  0608          		ld	b,08h
  7361  29            		add	hl,hl		;*2
  7362                	KEXT8162:
  7362  29            		add	hl,hl		;*4
  7363  29            		add	hl,hl		;*8
  7364                	KEXTLP2:
  7364  C5            		push	bc
  7365  E5            		push	hl		;address
  7366  CDA172        		call	READEKROM
  7369  E5            		push	hl
  736A  2E00          		ld	l,00h
  736C  CDBF73        		call	CALLPRTHL
  736F  E1            		pop	hl
  7370  65            		ld	h,l
  7371  2E00          		ld	l,00h
  7373  CDBF73        		call	CALLPRTHL
  7376  E1            		pop	hl
  7377  23            		inc	hl		;kanji ROM address
  7378  C1            		pop	bc
  7379  10E9          		djnz	KEXTLP2
  737B  C3E872        		jp	PRTKEND
                      	
                      	
                      	;input: a=character code
                      	;output: bc=next X, de=Y
                      	;destroy: af,hl
  737E                	_KPRTCH:ds	KPRTCH-_KPRTCH
  7381                		org	KPRTCH
                      	
  7381  CDA014        		call	CGROM
                      	
                      	;input: de=CGROM address
  7384                	_KPRTCH2:ds	KPRTCH2-_KPRTCH2
  7384                		org	KPRTCH2
                      	
  7384  2AAFFE        		ld	hl,(GRPY2)
  7387  010600        		ld	bc,0006h
  738A  09            		add	hl,bc
  738B  22AFFE        		ld	(GRPY2),hl
                      	
  738E  060A          		ld	b,0ah
  7390                	KPRCHLP:
  7390  C5            		push	bc
  7391  D5            		push	de		;CGROM address
  7392  CD9435        		call	READCGROM
  7395  62            		ld	h,d
  7396  2E00          		ld	l,00h
  7398  CDBF73        		call	CALLPRTHL
  739B  D1            		pop	de
  739C  13            		inc	de
  739D  C1            		pop	bc
  739E  10F0          		djnz	KPRCHLP
                      	
  73A0  2AAFFE        		ld	hl,(GRPY2)
  73A3  11F0FF        		ld	de,0-0010h
  73A6  19            		add	hl,de
  73A7  22AFFE        		ld	(GRPY2),hl
  73AA  EB            		ex	de,hl
                      	
  73AB  010800        		ld	bc,0008h
  73AE                	KPRCHXY:
  73AE  2AADFE        		ld	hl,(GRPX2)
  73B1  09            		add	hl,bc
  73B2  3A92FD        		ld	a,(SCREEN1)
  73B5  0F            		rrca
  73B6  3801          		jr	c,KPRCHC	;screen mode 4
  73B8  09            		add	hl,bc		;screen mode 3
  73B9                	KPRCHC:
  73B9  22ADFE        		ld	(GRPX2),hl
  73BC  44            		ld	b,h
  73BD  4D            		ld	c,l
  73BE  C9            		ret
                      	
                      	
                      	;check Y, call PRTHL, increment Y
                      	;input: h,l=data, (GRPX2),(GRPY2),(ATTDAT),(ATTDAT2)
                      	;destroy: af,bc,de,hl
  73BF                	CALLPRTHL:
  73BF  E5            		push	hl
  73C0  21C700        		ld	hl,199
  73C3  ED5BAFFE      		ld	de,(GRPY2)
  73C7  E7            		rst	CPHLDE
  73C8  E1            		pop	hl
  73C9  D8            		ret	c
  73CA  ED4BADFE      		ld	bc,(GRPX2)
  73CE  CDD773        		call	PRTHL
  73D1  13            		inc	de
  73D2  ED53AFFE      		ld	(GRPY2),de
  73D6  C9            		ret
                      	
                      	
                      	;put 2-byte data in graphic mode (screen mode 3,4)
                      	;input: bc=x, de=y,h,l=data, (ATTDAT),(ATTDAT2)
                      	;output: bc=bc+16or32
                      	;destroy: af,hl
  73D7                	PRTHL:
  73D7  ED43B1FE      		ld	(GRPX3),bc
  73DB  D5            		push	de
  73DC  E5            		push	hl		;
                      	
                      	;check y range
  73DD  CB7A          		bit	7,d
  73DF  C2CB03        		jp	nz,FCERR
  73E2  21C700        		ld	hl,199
  73E5  E7            		rst	CPHLDE
  73E6  3001          		jr	nc,PRTHLYOK
  73E8  EB            		ex	de,hl
  73E9                	PRTHLYOK:
                      	
                      	;check x range
  73E9  CB78          		bit	7,b
  73EB  2805          		jr	z,PRTHLXOK
                      	;	ld	b,00h
  73ED  42            		ld	b,d		;=0
  73EE  79            		ld	a,c
  73EF  E607          		and	07h
  73F1  4F            		ld	c,a
                      	
  73F2                	PRTHLXOK:
  73F2  CDB215        		call	GXY2AD
  73F5  44            		ld	b,h
  73F6  4D            		ld	c,l
  73F7  E1            		pop	hl		;data
                      	
  73F8  1600          		ld	d,00h
  73FA  B7            		or	a
  73FB  EA3274        		jp	pe,PRTHLLP3	;screen mode 3
                      	
                      	;h-l -> d-h-l (left justified)
  73FE                	PRTHLLP1:
  73FE  29            		add	hl,hl
  73FF  CB12          		rl	d
  7401  0F            		rrca
  7402  30FA          		jr	nc,PRTHLLP1
                      	
  7404  7D            		ld	a,l
  7405  F5            		push	af
  7406  E5            		push	hl
  7407  D5            		push	de
                      	
  7408  60            		ld	h,b
  7409  69            		ld	l,c
                      	
  740A  0603          		ld	b,03h
  740C                	PRTHLLP2:
  740C  E5            		push	hl
  740D  2AB1FE        		ld	hl,(GRPX3)	;start x
  7410  114001        		ld	de,320
  7413  E7            		rst	CPHLDE
  7414  F5            		push	af
  7415  110800        		ld	de,0008h
  7418  19            		add	hl,de
  7419  22B1FE        		ld	(GRPX3),hl
  741C  F1            		pop	af
  741D  E1            		pop	hl
  741E  D1            		pop	de
  741F  3004          		jr	nc,PRTHLNC
  7421  CD7066        		call	PRT466
  7424  23            		inc	hl
  7425                	PRTHLNC:
  7425  10E5          		djnz	PRTHLLP2
                      	
  7427  2AB1FE        		ld	hl,(GRPX3)
  742A  11F8FF        		ld	de,0-0008h
  742D  19            		add	hl,de
  742E  44            		ld	b,h
  742F  4D            		ld	c,l
  7430  D1            		pop	de
  7431  C9            		ret
                      	
                      	
                      	;for screen mode 3
                      	
                      	;h-l -> d-h-l (left justified)
  7432                	PRTHLLP3:
  7432  29            		add	hl,hl
  7433  CB12          		rl	d
  7435  0F            		rrca
  7436  0F            		rrca
  7437  30F9          		jr	nc,PRTHLLP3
                      	
                      	;stretch and push d-h-l
  7439  7D            		ld	a,l
  743A  CD6074        		call	STRETCH
  743D  F5            		push	af
  743E  7D            		ld	a,l
  743F  0F            		rrca
  7440  0F            		rrca
  7441  0F            		rrca
  7442  0F            		rrca
  7443  CD6074        		call	STRETCH
  7446  F5            		push	af
                      	
  7447  7C            		ld	a,h
  7448  CD6074        		call	STRETCH
  744B  F5            		push	af
  744C  7C            		ld	a,h
  744D  0F            		rrca
  744E  0F            		rrca
  744F  0F            		rrca
  7450  0F            		rrca
  7451  CD6074        		call	STRETCH
  7454  F5            		push	af
                      	
  7455  7A            		ld	a,d
  7456  CD6074        		call	STRETCH
  7459  F5            		push	af
                      	
  745A  60            		ld	h,b
  745B  69            		ld	l,c
                      	
  745C  0605          		ld	b,05h
  745E  18AC          		jr	PRTHLLP2
                      	
                      	
                      	;76543210 -> 33221100
                      	;input: a
                      	;output: a
                      	;destroy: f
  7460                	STRETCH:
  7460  C5            		push	bc
  7461  4F            		ld	c,a
  7462  0604          		ld	b,04h
  7464                	STRETCHLP:
  7464  CB09          		rrc	c
  7466  1F            		rra
  7467  07            		rlca
  7468  0F            		rrca
  7469  1F            		rra
  746A  10F8          		djnz	STRETCHLP
  746C  C1            		pop	bc
  746D  C9            		ret
                      	
                      	
  746E                	PAUP66:
  746E  CDE974        		call	PALINE66
  7471  C8            		ret	z
                      	
  7472  E5            		push	hl		;address
  7473  D5            		push	de		;bit
  7474  ED4BB5FE      		ld	bc,(PAWRK)
  7478  C5            		push	bc
  7479  ED4BB1FE      		ld	bc,(GRPX3)
  747D  C5            		push	bc
  747E  3AB3FE        		ld	a,(GRPY3)
  7481  F5            		push	af
                      	
  7482  CDB571        		call	PADWN662
                      	
  7485  F1            		pop	af
  7486  32B3FE        		ld	(GRPY3),a
  7489  E1            		pop	hl
  748A  22B1FE        		ld	(GRPX3),hl
  748D  E1            		pop	hl
  748E  22B5FE        		ld	(PAWRK),hl
  7491  D1            		pop	de		;bit
  7492  E1            		pop	hl		;address
                      	
  7493                	PAUP662:
                      	;	ld	a,(GRPY3)
  7493  B7            		or	a
  7494  C8            		ret	z
                      	
  7495  CB6C          		bit	5,h
  7497  2002          		jr	nz,PAUP66NZ
  7499  D609          		sub	09h
  749B                	PAUP66NZ:
  749B  3D            		dec	a
  749C  32B3FE        		ld	(GRPY3),a
  749F  01D8FF        		ld	bc,0-CLMN66
  74A2  09            		add	hl,bc
  74A3  CD9375        		call	READVRAM
                      	
  74A6  AF            		xor	a
  74A7  32B7FE        		ld	(PACNT),a
                      	
  74AA                	PAUP66LP1:
  74AA  CD3A76        		call	SRCHOK66
  74AD  381C          		jr	c,CALLUP66
                      	
  74AF  ED4BB1FE      		ld	bc,(GRPX3)
  74B3  C5            		push	bc
  74B4  ED4BB3FE      		ld	bc,(GRPY3)
  74B8  C5            		push	bc
                      	
  74B9  E5            		push	hl
  74BA  D5            		push	de
  74BB  21B7FE        		ld	hl,PACNT
  74BE  34            		inc	(hl)
  74BF  CDCC34        		call	CHKFRE
  74C2  D1            		pop	de
  74C3  E1            		pop	hl
                      	
  74C4  E5            		push	hl		;address
  74C5  D5            		push	de		;bit
                      	
  74C6  CD4976        		call	SRCHNG66
  74C9  30DF          		jr	nc,PAUP66LP1
                      	
  74CB                	CALLUP66:
  74CB  3AB7FE        		ld	a,(PACNT)
  74CE  B7            		or	a
  74CF  C8            		ret	z
                      	
  74D0                	PAUP66LP2:
  74D0  D1            		pop	de		;bit
  74D1  E1            		pop	hl		;address
  74D2  C1            		pop	bc
  74D3  ED43B3FE      		ld	(GRPY3),bc
  74D7  C1            		pop	bc
  74D8  ED43B1FE      		ld	(GRPX3),bc
                      	
  74DC  3D            		dec	a
  74DD  32B7FE        		ld	(PACNT),a
  74E0  288C          		jr	z,PAUP66
  74E2  F5            		push	af
  74E3  CD6E74        		call	PAUP66
  74E6  F1            		pop	af
  74E7  18E7          		jr	PAUP66LP2
                      	
                      	
  74E9                	PALINE66:
  74E9  CD9375        		call	READVRAM
                      	
  74EC  CD4D27        		call	STOPESC
  74EF  CD1175        		call	CHKPA66
  74F2  C8            		ret	z
                      	
  74F3  E5            		push	hl		;address
  74F4  D5            		push	de		;bit
  74F5  ED4BB1FE      		ld	bc,(GRPX3)
  74F9  C5            		push	bc
                      	
  74FA  CDB475        		call	PAINTR66
  74FD  2AB1FE        		ld	hl,(GRPX3)
  7500  22B5FE        		ld	(PAWRK),hl	;X_right
                      	
  7503  E1            		pop	hl
  7504  22B1FE        		ld	(GRPX3),hl
  7507  D1            		pop	de		;bit
  7508  E1            		pop	hl		;address
                      	
  7509  CD9375        		call	READVRAM
  750C  CD6D75        		call	PAINTL66
                      	
  750F  B2            		or	d		;d>0, reset z-flag
  7510  C9            		ret
                      	
                      	
                      	;check and paint for mode 5
                      	;input: hl=address, d=bit
                      	;output: z(1=not painted)
                      	;destroy: af,e
  7511                	CHKPA66:
  7511  CD2676        		call	CMPBOR66
  7514  C8            		ret	z
                      	
                      	;	ld	bc,(PAWRK2)
                      	
  7515  CB6C          		bit	5,h
  7517  281A          		jr	z,CHKPA661
                      	
  7519  3A60FE        		ld	a,(ATTDAT2)
  751C  A9            		xor	c
  751D  A2            		and	d
  751E  A9            		xor	c
  751F  32B8FE        		ld	(PAWRK2),a
  7522  77            		ld	(hl),a
                      	
  7523  3AACFE        		ld	a,(ATTDAT)
  7526  A8            		xor	b
  7527  A2            		and	d
  7528  A8            		xor	b
  7529  32B9FE        		ld	(PAWRK2+1),a
  752C  CBAC          		res	5,h
  752E  77            		ld	(hl),a
  752F  CBEC          		set	5,h
                      	
  7531  B2            		or	d		;d>0, reset z-flag
  7532  C9            		ret
                      	
                      	;screen mode 1
  7533                	CHKPA661:
  7533  CD8DFE        		call	READRAM
  7536  5F            		ld	e,a
  7537  3AACFE        		ld	a,(ATTDAT)
  753A  07            		rlca
  753B  07            		rlca
  753C  07            		rlca
  753D  07            		rlca
  753E  AB            		xor	e
  753F  E670          		and	70h
  7541  AB            		xor	e
  7542  77            		ld	(hl),a
  7543  B2            		or	d		;d>0, reset z-flag
  7544  C9            		ret
                      	
                      	
                      	;TALK command
  7545                	_C_TALK:ds	C_TALK-_C_TALK
  7548                		org	C_TALK
                      	
                      	;	call	STRARG
  7548  CD603F        		call	CHKCLN
  754B  C8            		ret	z
  754C  CD8726        		call	STRARG3
                      	
  754F  EB            		ex	de,hl		;hl=string address
  7550  5F            		ld	e,a		;e=length
                      	
  7551  3E03          		ld	a,03h
  7553  D3C3          		out	(0c3h),a	;port c2h direction: bit0,1=output
  7555  AF            		xor	a
  7556  D3C2          		out	(0c2h),a	;bit0=0:VOICE ROM
  7558  3E51          		ld	a,51h		;0000-3fff,6000-7fff:BASIC ROM, 4000-5fff:VOICE ROM
  755A  CD9826        		call	SETF0H
  755D  CD0040        		call	TALK
                      	
  7560  F5            		push	af
  7561  3E11          		ld	a,11h		;0000-7fff:BASIC ROM
  7563  CD9826        		call	SETF0H
  7566  F1            		pop	af
                      	
  7567  FE20          		cp	20h
  7569  C8            		ret	z
  756A  C3CB03        		jp	FCERR
                      	
                      	
                      	;paint to left direction for mode 5
                      	;input: hl=address, d=bit
                      	;output: hl,d
                      	;destroy: af,bc
  756D                	PAINTL66:
  756D  ED4BB1FE      		ld	bc,(GRPX3)
  7571  78            		ld	a,b
  7572  B1            		or	c
  7573  C8            		ret	z
  7574  CDBE75        		call	DECGXPA66
  7577  CD1175        		call	CHKPA66
  757A  20F1          		jr	nz,PAINTL66
                      	;	jp	INCGXPA66
                      	
                      	
                      	;increment x for paint for mode 5
                      	;input: hl=address, d=bit
                      	;output: hl, d, bc=(GRPX3)
                      	;destroy: af,bc
  757C                	INCGXPA66:
  757C  ED4BB1FE      		ld	bc,(GRPX3)
  7580  CB6C          		bit	5,h
  7582  2823          		jr	z,INCGXPA661	;screen mode 1
  7584  03            		inc	bc
  7585  CB0A          		rrc	d
  7587  E28D75        		jp	po,INCGXPA664	;screen mode 4
  758A  03            		inc	bc		;screen mode 3
  758B  CB0A          		rrc	d
  758D                	INCGXPA664:
  758D  ED43B1FE      		ld	(GRPX3),bc
  7591  D0            		ret	nc
                      	
  7592  23            		inc	hl
                      	;	jr	READVRAM
                      	
                      	
                      	;read VRAM
                      	;input: hl=address
                      	;output: (PAWRK2),(PAWRK2+1)
                      	;destroy: af
  7593                	READVRAM:
  7593  CB6C          		bit	5,h
  7595  C8            		ret	z
  7596  CD8DFE        		call	READRAM
  7599  32B8FE        		ld	(PAWRK2),a
  759C  CBAC          		res	5,h
  759E  CD8DFE        		call	READRAM
  75A1  CBEC          		set	5,h
  75A3  32B9FE        		ld	(PAWRK2+1),a
  75A6  C9            		ret
                      	
                      	
  75A7                	INCGXPA661:
  75A7  79            		ld	a,c
  75A8  C608          		add	a,08h
  75AA  4F            		ld	c,a
  75AB  3001          		jr	nc,INCGXPA661NC
  75AD  04            		inc	b
  75AE                	INCGXPA661NC:
  75AE  ED43B1FE      		ld	(GRPX3),bc
  75B2  23            		inc	hl
  75B3  C9            		ret
                      	
                      	
                      	;paint to right direction for mode 5
                      	;input: hl=address, d=bit
                      	;output: hl,d
                      	;destroy: af,bc
  75B4                	PAINTR66:
  75B4  CD5876        		call	GXLARGE66
  75B7  2805          		jr	z,DECGXPA66
  75B9  CD1175        		call	CHKPA66
  75BC  20F6          		jr	nz,PAINTR66
                      	;	jp	DECGXPA66
                      	
                      	
                      	;decrement x for paint for mode 5
                      	;input: hl=address, d=bit
                      	;output: hl, d, bc=(GRPX3)
                      	;destroy: af
  75BE                	DECGXPA66:
  75BE  ED4BB1FE      		ld	bc,(GRPX3)
  75C2  CB6C          		bit	5,h
  75C4  2811          		jr	z,DECGXPA661	;screen mode 1
  75C6  0B            		dec	bc
  75C7  CB02          		rlc	d
  75C9  E2CF75        		jp	po,DECGXPA664	;screen mode 4
  75CC  0D            		dec	c		;dec bc, screen mode3
  75CD  CB02          		rlc	d
  75CF                	DECGXPA664:
  75CF  ED43B1FE      		ld	(GRPX3),bc
  75D3  D0            		ret	nc
                      	
  75D4  2B            		dec	hl
  75D5  18BC          		jr	READVRAM
                      	
                      	
  75D7                	DECGXPA661:
  75D7  79            		ld	a,c
  75D8  D608          		sub	08h
  75DA  4F            		ld	c,a
  75DB  3001          		jr	nc,DECGXPA661NC
  75DD  05            		dec	b
  75DE                	DECGXPA661NC:
  75DE  ED43B1FE      		ld	(GRPX3),bc
  75E2  2B            		dec	hl
  75E3  C9            		ret
                      	
                      	
                      	;compare (GRPX3) to (PAWRK)
                      	;output: f
                      	;destroy: f,bc
  75E4                	CMPPAX66:
  75E4  E5            		push	hl
  75E5  2AB5FE        		ld	hl,(PAWRK)
  75E8  ED4BB1FE      		ld	bc,(GRPX3)
  75EC  B7            		or	a
  75ED  ED42          		sbc	hl,bc
  75EF  E1            		pop	hl
  75F0  C9            		ret
                      	
                      	
                      	;compare attribute and border color to VRAM
                      	;output: bc=(PAWRK2), z-flag(1=same), c-flag=0
  75F1                	CMPATT66:
  75F1  CB6C          		bit	5,h
  75F3  2813          		jr	z,CMPATT661
  75F5  ED4BB8FE      		ld	bc,(PAWRK2)
  75F9  3A60FE        		ld	a,(ATTDAT2)
  75FC  A9            		xor	c
  75FD  A2            		and	d
  75FE  202E          		jr	nz,CMPBOR662
  7600  3AACFE        		ld	a,(ATTDAT)
  7603  A8            		xor	b
  7604  A2            		and	d
  7605  C8            		ret	z
  7606  1826          		jr	CMPBOR662
                      	
                      	;screen mode 1
  7608                	CMPATT661:
  7608  CD8DFE        		call	READRAM
  760B  5F            		ld	e,a
  760C  3AACFE        		ld	a,(ATTDAT)
  760F  07            		rlca
  7610  07            		rlca
  7611  07            		rlca
  7612  07            		rlca
  7613  AB            		xor	e
  7614  E670          		and	70h
  7616  C8            		ret	z
                      	
  7617                	CMPBOR661:
  7617  CD8DFE        		call	READRAM
  761A  5F            		ld	e,a
  761B  3AC5FE        		ld	a,(BORDERA)
  761E  07            		rlca
  761F  07            		rlca
  7620  07            		rlca
  7621  07            		rlca
  7622  AB            		xor	e
  7623  E670          		and	70h
  7625  C9            		ret
                      	
                      	
                      	;compare border color to VRAM
                      	;output: bc=(PAWRK2), z-flag(1=same), c-flag=0
  7626                	CMPBOR66:
  7626  CB6C          		bit	5,h
  7628  28ED          		jr	z,CMPBOR661
  762A  ED4BB8FE      		ld	bc,(PAWRK2)
  762E                	CMPBOR662:
  762E  3A61FE        		ld	a,(BORDA2)
  7631  A9            		xor	c
  7632  A2            		and	d
  7633  C0            		ret	nz
                      	
  7634  3AC5FE        		ld	a,(BORDERA)
  7637  A8            		xor	b
  7638  A2            		and	d
  7639  C9            		ret
                      	
                      	
                      	;search for paintable area for mode 5
                      	;input: hl=address, d=bit
                      	;output: hl,d,c-flag(1=over,0=OK)
                      	;destroy: af,bc,e
  763A                	SRCHOK66:
  763A  CDE475        		call	CMPPAX66
  763D  D8            		ret	c
  763E  CDF175        		call	CMPATT66
  7641  C0            		ret	nz
  7642  CD5876        		call	GXLARGE66
  7645  20F3          		jr	nz,SRCHOK66
  7647  37            		scf
  7648  C9            		ret
                      	
                      	
                      	;search for unpaintable area for mode 5
                      	;get unpaintable area
                      	;input: hl=address, d=bit
                      	;output: hl,d,c-flag(1=over,0=NG)
                      	;destroy: af,bc
  7649                	SRCHNG66:
  7649  CDE475        		call	CMPPAX66
  764C  D8            		ret	c
  764D  CD2676        		call	CMPBOR66
  7650  C8            		ret	z
  7651  CD5876        		call	GXLARGE66
  7654  20F3          		jr	nz,SRCHNG66
  7656  37            		scf
  7657  C9            		ret
                      	
                      	
                      	;increment x and compare graphic width (=320)
                      	;input: hl=address, d=bit
                      	;output: hl, d, bc=(GRPX3), z-flag(1=over)
                      	;destroy: af,bc
  7658                	GXLARGE66:
  7658  CD7C75        		call	INCGXPA66
                      	;	ld	bc,(GRPX3)
  765B  05            		dec	b
  765C  C0            		ret	nz
  765D  79            		ld	a,c
  765E  FE40          		cp	320-256
  7660  C9            		ret
                      	
                      	
  7661                	C_MON:
                      	;	pop	af		;cancel return address
                      	;	push	af		;for Dezeni Land
  7661  F5            		push	af		;for Dezeni Land
                      	
  7662  3E41          		ld	a,41h
  7664  320EFD        		ld	(MONFLG),a
  7667  ED7304FD      		ld	(REGSP),sp
  766B  ED730CFD      		ld	(SPSAVE),sp
                      	
  766F  3EC3          		ld	a,0c3h
  7671  32E1FF        		ld	(HOOK38H),a
  7674  21AC77        		ld	hl,MONBRK
  7677  22E2FF        		ld	(HOOK38H+1),hl
  767A  216761        		ld	hl,REGDATA
  767D  11F9FC        		ld	de,REGF
  7680  010B00        		ld	bc,REGDATAEND-REGDATA
  7683  EDB0          		ldir
                      	
  7685  210000        		ld	hl,0000h
  7688  2206FD        		ld	(BRKADR1),hl
  768B  2209FD        		ld	(BRKADR2),hl
                      	
  768E                	MONLP1:
  768E  21FD76        		ld	hl,READY
  7691  CD5A7C        		call	MPUTS
  7694                	MONLP2:
  7694  CD257C        		call	GETPUTC
  7697  21B376        		ld	hl,MONTBL1
  769A  11C376        		ld	de,MONTBL2
  769D  0610          		ld	b,10h
  769F                	MONLP3:
  769F  BE            		cp	(hl)
  76A0  2807          		jr	z,MONCALL
  76A2  23            		inc	hl
  76A3  13            		inc	de
  76A4  13            		inc	de
  76A5  10F8          		djnz	MONLP3
  76A7  1841          		jr	MONERR
                      	
  76A9                	MONCALL:
  76A9  EB            		ex	de,hl
  76AA  5E            		ld	e,(hl)
  76AB  23            		inc	hl
  76AC  56            		ld	d,(hl)
  76AD  EB            		ex	de,hl
  76AE  CDA407        		call	JPHL
  76B1  18DB          		jr	MONLP1
                      	
  76B3                	MONTBL1:
  76B3  4243444546474C		db	'B',  'C',  'D',  'E',  'F',  'G',  'L',  'M'
  76BB  5051525357583F		db	'P',  'Q',  'R',  'S',  'W',  'X',  '?',  03h
  76C3                	MONTBL2:
  76C3  0777F477377864		dw	MONB, MONC, MOND, MONE, MONF, MONG, MONL, MONM
  76D3  137981799E7AB4		dw	MONP, MONQ, MONR, MONS, MONW, MONX, MONQMARK, MONSTOP
                      	
  76E3                	MONQMARK:
  76E3  210EFD        		ld	hl,MONFLG
  76E6  7E            		ld	a,(hl)
  76E7  EE40          		xor	40h
  76E9  77            		ld	(hl),a
                      	;	jr	MONERR
                      	
  76EA                	MONERR:
  76EA  21F676        		ld	hl,MONERRSTR
  76ED  CD5A7C        		call	MPUTS
  76F0  ED7B0CFD      		ld	sp,(SPSAVE)
  76F4  189E          		jr	MONLP2
                      	
  76F6                	MONERRSTR:
  76F6  4552524F52213F		db	"ERROR!?"
                      	
  76FD                	READY:
  76FD  0D0A5245414459		db	0dh, 0ah, "READY", 0dh, 0ah, 00h
                      	
                      	
                      	;MON B command
  7707                	MONB:
  7707  CD707C        		call	MPUTHYP
  770A  CD257C        		call	GETPUTC
  770D  282F          		jr	z,MONBASIC
  770F  FE52          		cp	'R'
  7711  2806          		jr	z,MONBR
  7713  FE47          		cp	'G'
  7715  284F          		jr	z,MONBG
  7717  18D1          		jr	MONERR
                      	
  7719                	MONBR:
  7719  2A5FFA        		ld	hl,(STARTAD)
  771C  54            		ld	d,h
  771D  5D            		ld	e,l
  771E  13            		inc	de
  771F  13            		inc	de
  7720  13            		inc	de
  7721                	MONBRLP1:
  7721  13            		inc	de
  7722  1A            		ld	a,(de)
  7723  B7            		or	a
  7724  20FB          		jr	nz,MONBRLP1
  7726  13            		inc	de
  7727  73            		ld	(hl),e
  7728  23            		inc	hl
  7729  72            		ld	(hl),d
  772A                	MONBRLP2:
  772A  EB            		ex	de,hl		;hl=link pointer address
  772B  5E            		ld	e,(hl)
  772C  23            		inc	hl
  772D  56            		ld	d,(hl)
  772E  7A            		ld	a,d
  772F  B3            		or	e
  7730  20F8          		jr	nz,MONBRLP2
  7732  23            		inc	hl
  7733  2256FF        		ld	(VARAD),hl
  7736  CDF934        		call	RESSTK
  7739  214B77        		ld	hl,REVSTR
  773C  1807          		jr	MONBEND
                      	
  773E                	MONBASIC:
  773E  ED7B5BFA      		ld	sp,(STACK)
  7742  215A77        		ld	hl,AGASTR
  7745                	MONBEND:
  7745  CD5A7C        		call	MPUTS
  7748  C34204        		jp	EDIT
                      	
  774B                	REVSTR:
  774B  0D0A5245564956		db	0dh, 0ah, "REVIVAL AND", 0dh, 0ah
  775A                	AGASTR:
  775A  414741494E2042		db	"AGAIN BASIC", 00h
                      	
  7766                	MONBG:
  7766  CD467C        		call	MGETC
  7769  FE0D          		cp	0dh
  776B  2839          		jr	z,MONBGRUN
  776D  FE2C          		cp	','
  776F  2805          		jr	z,MONBGZ1
  7771  FE20          		cp	' '
  7773  C2EA76        		jp	nz,MONERR
  7776                	MONBGZ1:
  7776  CD707C        		call	MPUTHYP
  7779  CDF27B        		call	GETHEX
  777C  C2EA76        		jp	nz,MONERR
  777F  FE0D          		cp	0dh
  7781  281A          		jr	z,MONBGZ2
                      	
  7783  EB            		ex	de,hl		;
  7784  CD707C        		call	MPUTHYP
  7787  CDF27B        		call	GETHEX
  778A  C2EA76        		jp	nz,MONERR
  778D  FE0D          		cp	0dh
  778F  C2EA76        		jp	nz,MONERR
  7792  EB            		ex	de,hl		;
  7793  2209FD        		ld	(BRKADR2),hl
  7796  7E            		ld	a,(hl)
  7797  320BFD        		ld	(BRKSAV2),a
  779A  36FF          		ld	(hl),0ffh
  779C  EB            		ex	de,hl		;
  779D                	MONBGZ2:
  779D  2206FD        		ld	(BRKADR1),hl
  77A0  7E            		ld	a,(hl)
  77A1  3208FD        		ld	(BRKSAV1),a
  77A4  36FF          		ld	(hl),0ffh
                      	
  77A6                	MONBGRUN:
  77A6  CD3F09        		call	PRTNLX
  77A9  C38507        		jp	RUN
                      	
  77AC                	MONBRK:
  77AC  ED7304FD      		ld	(REGSP),sp
  77B0  F3            		di
  77B1  3102FD        		ld	sp,REGPC
  77B4  E5            		push	hl
  77B5  D5            		push	de
  77B6  C5            		push	bc
  77B7  F5            		push	af
  77B8  ED7B04FD      		ld	sp,(REGSP)
  77BC  FB            		ei
                      	
  77BD  21E677        		ld	hl,BRKSTR
  77C0  CD5A7C        		call	MPUTS
  77C3  E1            		pop	hl
  77C4  2B            		dec	hl
  77C5  2202FD        		ld	(REGPC),hl
  77C8  CDC37B        		call	MPUTHEX4
  77CB  CD637C        		call	MPUTNL
                      	
  77CE  2106FD        		ld	hl,BRKADR1
  77D1  0602          		ld	b,02h
  77D3                	BRKLP:
  77D3  5E            		ld	e,(hl)
  77D4  23            		inc	hl
  77D5  56            		ld	d,(hl)
  77D6  23            		inc	hl
  77D7  7A            		ld	a,d
  77D8  B3            		or	e
  77D9  2802          		jr	z,NOBRK
  77DB  7E            		ld	a,(hl)
  77DC  12            		ld	(de),a
  77DD                	NOBRK:
  77DD  23            		inc	hl
  77DE  10F3          		djnz	BRKLP
                      	
  77E0  CDF934        		call	RESSTK
  77E3  C39476        		jp	MONLP2
                      	
  77E6                	BRKSTR:
  77E6  425245414B2121		db	"BREAK!! ADDR=", 00h
                      	
                      	
                      	;MON C command
  77F4                	MONC:
  77F4  CD637C        		call	MPUTNL
  77F7  210EFD        		ld	hl,MONFLG
  77FA  7E            		ld	a,(hl)
  77FB  CB6F          		bit	5,a
  77FD  2008          		jr	nz,MONCNZ		;printer on?
  77FF  CBEE          		set	5,(hl)
  7801  211F78        		ld	hl,PRTON
  7804  C35A7C        		jp	MPUTS
                      	
  7807                	MONCNZ:
  7807  0F            		rrca
  7808  300B          		jr	nc,MONCNC		;printer only?
  780A  CB86          		res	0,(hl)
  780C  CD8111        		call	CSROFF
  780F  212678        		ld	hl,PRTONLY
  7812  C35A7C        		jp	MPUTS
                      	
  7815                	MONCNC:
  7815  07            		rlca
  7816  EE21          		xor	21h
  7818  77            		ld	(hl),a
  7819  212F78        		ld	hl,PRTOFF
  781C  C35A7C        		jp	MPUTS
                      	
  781F                	PRTON:
  781F  505254204F4E00		db	"PRT ON", 00h
  7826                	PRTONLY:
  7826  505254204F4E4C		db	"PRT ONLY", 00h
  782F                	PRTOFF:
  782F  505254204F4646		db	"PRT OFF", 00h
                      	
                      	
                      	;MON D command
  7837                	MOND:
  7837  CDDB7B        		call	GETHEX2
  783A  EB            		ex	de,hl
                      	
                      	;hl...de
  783B                	MONDLP1:
  783B  CD6C7C        		call	MPUTSP
  783E  CDC37B        		call	MPUTHEX4
  7841  3E3A          		ld	a,':'
  7843  CD727C        		call	MPUTC
                      	
  7846                	MONDLP2:
  7846  CD6C7C        		call	MPUTSP
  7849  3A0EFD        		ld	a,(MONFLG)
  784C  E640          		and	40h
  784E  7E            		ld	a,(hl)
  784F  C48DFE        		call	nz,READRAM
  7852  CDC87B        		call	MPUTHEX2
                      	
  7855  E7            		rst	CPHLDE
  7856  D2637C        		jp	nc,MPUTNL
  7859  23            		inc	hl
  785A  7D            		ld	a,l
  785B  E607          		and	07h
  785D  20E7          		jr	nz,MONDLP2
  785F  CD637C        		call	MPUTNL
  7862  18D7          		jr	MONDLP1
                      	
                      	
                      	;MON E command
  7864                	MONE:
  7864  CD637C        		call	MPUTNL
  7867  210EFD        		ld	hl,MONFLG
  786A  7E            		ld	a,(hl)
  786B  EE10          		xor	10h
  786D  77            		ld	(hl),a
  786E  E610          		and	10h
  7870  217B78        		ld	hl,ECHOON
  7873  2003          		jr	nz,MONENZ
  7875  218378        		ld	hl,ECHOOFF
  7878                	MONENZ:
  7878  C35A7C        		jp	MPUTS
                      	
  787B                	ECHOON:
  787B  4543484F204F4E		db	"ECHO ON", 00h
  7883                	ECHOOFF:
  7883  4543484F204F46		db	"ECHO OFF", 00h
                      	
                      	
                      	;MON F command
  788C                	MONF:
  788C  CDE97B        		call	GETHEX1
  788F  CAEA76        		jp	z,MONERR
  7892  E5            		push	hl		;hl=start
                      	
  7893  CDDB7B        		call	GETHEX2
  7896  4D            		ld	c,l		;c=data
  7897  E1            		pop	hl
  7898                	MONFLP:
  7898  71            		ld	(hl),c
  7899  E7            		rst	CPHLDE
  789A  D0            		ret	nc
  789B  23            		inc	hl
  789C  18FA          		jr	MONFLP
                      	
                      	
                      	;MON G command
  789E                	MONG:
  789E  CDF27B        		call	GETHEX
  78A1  202A          		jr	nz,MONGEND
  78A3  2202FD        		ld	(REGPC),hl
  78A6  FE0D          		cp	0dh
  78A8  2823          		jr	z,MONGEND
  78AA  CD707C        		call	MPUTHYP
  78AD  CDE97B        		call	GETHEX1
  78B0  2206FD        		ld	(BRKADR1),hl
  78B3  7E            		ld	a,(hl)
  78B4  3208FD        		ld	(BRKSAV1),a
  78B7  36FF          		ld	(hl),0ffh
  78B9  2812          		jr	z,MONGEND
  78BB  CD707C        		call	MPUTHYP
  78BE  CDE97B        		call	GETHEX1
  78C1  C2EA76        		jp	nz,MONERR
  78C4  2209FD        		ld	(BRKADR2),hl
  78C7  7E            		ld	a,(hl)
  78C8  320BFD        		ld	(BRKSAV2),a
  78CB  36FF          		ld	(hl),0ffh
                      	
  78CD                	MONGEND:
  78CD  F3            		di
  78CE  31F9FC        		ld	sp,REGF
  78D1  F1            		pop	af
  78D2  C1            		pop	bc
  78D3  D1            		pop	de
  78D4  E1            		pop	hl
  78D5  ED7B04FD      		ld	sp,(REGSP)
  78D9  FB            		ei
  78DA  CD01FD        		call	REGPC-1
  78DD  ED7B5BFA      		ld	sp,(STACK)
  78E1  C34204        		jp	EDIT
                      	
                      	
                      	;MON M command
  78E4                	MONM:
  78E4  CDE97B        		call	GETHEX1
  78E7  CAEA76        		jp	z,MONERR
  78EA  E5            		push	hl
  78EB  CDDB7B        		call	GETHEX2
  78EE  E3            		ex	(sp),hl
  78EF  E7            		rst	CPHLDE
  78F0  3801          		jr	c,MONMC
  78F2  EB            		ex	de,hl
  78F3                	MONMC:
  78F3  42            		ld	b,d
  78F4  4B            		ld	c,e
  78F5  D1            		pop	de
  78F6  C3FF78        		jp	BLKTRA
                      	
                      	
                      	;block transfer
                      	;input: hl=start address, bc=end address, de=destination address
                      	;destroy: af,bc,de,hl
  78F9                	_BLKTRA:ds	BLKTRA-_BLKTRA
  78FF                		org	BLKTRA
                      	
  78FF  79            		ld	a,c
  7900  95            		sub	l
  7901  4F            		ld	c,a
  7902  78            		ld	a,b
  7903  9C            		sbc	a,h
  7904  47            		ld	b,a
  7905  03            		inc	bc
  7906  E7            		rst	CPHLDE
  7907  D293FE        		jp	nc,LDIRRAM
  790A  0B            		dec	bc
  790B  09            		add	hl,bc
  790C  EB            		ex	de,hl
  790D  09            		add	hl,bc
  790E  EB            		ex	de,hl
  790F  03            		inc	bc
  7910  C3A5FE        		jp	LDDRRAM
                      	
                      	
                      	;MON P/W command
  7913                	MONP:
  7913                	MONW:
  7913  D604          		sub	04h		;'P'->4ch, 'W'->53h
  7915  E605          		and	05h		;04h, 01h
  7917  0F            		rrca			;02h,80h
  7918  F5            		push	af
                      	
  7919  CDDB7B        		call	GETHEX2
  791C  E7            		rst	CPHLDE
  791D  DAEA76        		jp	c,MONERR
  7920  EB            		ex	de,hl		;hl=start address
                      	
  7921  F1            		pop	af		;'P'->02h, 'W'->80h
  7922  3258FA        		ld	(DEVICE),a
  7925  DCA825        		call	c,PRTOPN
  7928                	MONWLP1:
  7928  CD3927        		call	PUTNL
  792B  3E3A          		ld	a,':'
  792D  CDC726        		call	PUTDEV
  7930  0610          		ld	b,10h		;16 bytes
  7932  78            		ld	a,b		;length
  7933  CDAF7B        		call	PUTHEX2
  7936  7C            		ld	a,h		;address (high)
  7937  CDAF7B        		call	PUTHEX2
  793A  7D            		ld	a,l		;address (low)
  793B  CDAF7B        		call	PUTHEX2
  793E  AF            		xor	a		;record type
  793F  CDAF7B        		call	PUTHEX2
                      	
  7942  AF            		xor	a
  7943  90            		sub	b
  7944  94            		sub	h
  7945  95            		sub	l
  7946  4F            		ld	c,a		;check sum
  7947                	MONWLP2:
  7947  7E            		ld	a,(hl)
  7948  CDAF7B        		call	PUTHEX2
  794B  79            		ld	a,c
  794C  96            		sub	(hl)
  794D  4F            		ld	c,a		;check sum
  794E  E7            		rst	CPHLDE
  794F  300D          		jr	nc,MONWEND
  7951  23            		inc	hl
  7952  10F3          		djnz	MONWLP2
  7954  79            		ld	a,c
  7955  CDAF7B        		call	PUTHEX2
  7958  18CE          		jr	MONWLP1
                      	
  795A                	MONWLP3:
  795A  AF            		xor	a
  795B  CDAF7B        		call	PUTHEX2
  795E                	MONWEND:
  795E  10FA          		djnz	MONWLP3
                      	
  7960  79            		ld	a,c
  7961  CDAF7B        		call	PUTHEX2
  7964  CD3927        		call	PUTNL
  7967  3E3A          		ld	a,':'
  7969  CDC726        		call	PUTDEV
  796C  0608          		ld	b,08h
  796E                	MONWLP4:
  796E  AF            		xor	a
  796F  CDAF7B        		call	PUTHEX2
  7972  10FA          		djnz	MONWLP4
  7974  3E1A          		ld	a,1ah
  7976  CDC726        		call	PUTDEV
  7979  CD061B        		call	WCLOSE
  797C  AF            		xor	a
  797D  3258FA        		ld	(DEVICE),a
  7980  C9            		ret
                      	
                      	
                      	;MON Q command
  7981                	MONQ:
  7981  CDDB7B        		call	GETHEX2
  7984  EB            		ex	de,hl
                      	
                      	;hl...de
  7985                	MONQLP1:
  7985  CD6C7C        		call	MPUTSP
  7988  CDC37B        		call	MPUTHEX4
  798B  3E3A          		ld	a,':'
  798D  CD727C        		call	MPUTC
  7990  CD6C7C        		call	MPUTSP
                      	
  7993                	MONQLP2:
  7993  3A0EFD        		ld	a,(MONFLG)
  7996  E640          		and	40h
  7998  7E            		ld	a,(hl)
  7999  C48DFE        		call	nz,READRAM
                      	
  799C  FE20          		cp	20h
  799E  3002          		jr	nc,MONQNC
  79A0  3E2E          		ld	a,'.'
  79A2                	MONQNC:
  79A2  CD727C        		call	MPUTC
                      	
  79A5  E7            		rst	CPHLDE
  79A6  D2637C        		jp	nc,MPUTNL
  79A9  23            		inc	hl
  79AA  7D            		ld	a,l
  79AB  E60F          		and	0fh
  79AD  20E4          		jr	nz,MONQLP2
  79AF  CD637C        		call	MPUTNL
  79B2  18D1          		jr	MONQLP1
                      	
                      	
                      	;MON S command
  79B4                	MONS:
  79B4  CDE97B        		call	GETHEX1
  79B7  C8            		ret	z
  79B8                	MONSLP:
  79B8  3A0EFD        		ld	a,(MONFLG)
  79BB  E640          		and	40h
  79BD  7E            		ld	a,(hl)
  79BE  C48DFE        		call	nz,READRAM
  79C1  CDC87B        		call	MPUTHEX2
  79C4  CD707C        		call	MPUTHYP
  79C7  EB            		ex	de,hl
  79C8  CDF27B        		call	GETHEX
  79CB  EB            		ex	de,hl
  79CC  2001          		jr	nz,MONSNZ
  79CE  73            		ld	(hl),e
  79CF                	MONSNZ:
  79CF  FE0D          		cp	0dh
  79D1  C8            		ret	z
  79D2  23            		inc	hl
  79D3  7D            		ld	a,l
  79D4  E60F          		and	0fh
  79D6  20E0          		jr	nz,MONSLP
  79D8  CD637C        		call	MPUTNL
  79DB  CDC37B        		call	MPUTHEX4
  79DE  3E3A          		ld	a,':'
  79E0  CD727C        		call	MPUTC
  79E3  CD6C7C        		call	MPUTSP
  79E6  18D0          		jr	MONSLP
                      	
                      	
                      	;MON X command
  79E8                	MONX:
  79E8  217C7A        		ld	hl,REGTBL
  79EB  11887A        		ld	de,REGWKTBL
  79EE  CD257C        		call	GETPUTC
  79F1  280D          		jr	z,PRTREG
  79F3  060B          		ld	b,0bh
  79F5                	MONXLP:
  79F5  BE            		cp	(hl)
  79F6  283F          		jr	z,SETREG
  79F8  23            		inc	hl
  79F9  13            		inc	de
  79FA  13            		inc	de
  79FB  10F8          		djnz	MONXLP
  79FD  C3EA76        		jp	MONERR
                      	
                      	;hl=REGTBL
                      	;de=REGWKTBL
  7A00                	PRTREG:
  7A00  EB            		ex	de,hl
  7A01  060B          		ld	b,0bh
  7A03                	PRTREGLP:
  7A03  C5            		push	bc
                      	
  7A04  CD6C7C        		call	MPUTSP
  7A07  1A            		ld	a,(de)
  7A08  FE4D          		cp	'M'		;
  7A0A  13            		inc	de
  7A0B  CD727C        		call	MPUTC
  7A0E  3E3D          		ld	a,'='
  7A10  CD727C        		call	MPUTC
                      	
  7A13  4E            		ld	c,(hl)
  7A14  23            		inc	hl
  7A15  46            		ld	b,(hl)
  7A16  23            		inc	hl
                      	
  7A17  E5            		push	hl
  7A18  0A            		ld	a,(bc)
  7A19  6F            		ld	l,a
  7A1A  03            		inc	bc
  7A1B  0A            		ld	a,(bc)
  7A1C  67            		ld	h,a
  7A1D  7D            		ld	a,l
  7A1E  F5            		push	af		;
  7A1F  DCC87B        		call	c,MPUTHEX2	;A,B,C,D,E,F,H,L
  7A22  F1            		pop	af
  7A23  D4C37B        		call	nc,MPUTHEX4	;M,P,S
  7A26  E1            		pop	hl
                      	
  7A27  C1            		pop	bc
  7A28  78            		ld	a,b
  7A29  FE07          		cp	07h
  7A2B  CC637C        		call	z,MPUTNL
  7A2E  78            		ld	a,b
  7A2F  FE03          		cp	03h
  7A31  CC637C        		call	z,MPUTNL
  7A34  10CD          		djnz	PRTREGLP
  7A36  C9            		ret
                      	
                      	;hl=register name table
                      	;de=work area address table
  7A37                	SETREG:
  7A37  EB            		ex	de,hl
  7A38  CD6C7C        		call	MPUTSP
  7A3B  E5            		push	hl
  7A3C                	SETREGLP:
  7A3C  E1            		pop	hl
  7A3D  FE0D          		cp	0dh
  7A3F  C8            		ret	z
  7A40  1A            		ld	a,(de)
  7A41  B7            		or	a
  7A42  C8            		ret	z
  7A43  13            		inc	de
  7A44  FE4D          		cp	'M'		;
                      	
  7A46  4E            		ld	c,(hl)
  7A47  23            		inc	hl
  7A48  46            		ld	b,(hl)
  7A49  23            		inc	hl
                      	
  7A4A  E5            		push	hl
  7A4B  0A            		ld	a,(bc)
  7A4C  6F            		ld	l,a
  7A4D  03            		inc	bc
  7A4E  0A            		ld	a,(bc)
  7A4F  67            		ld	h,a
  7A50  0B            		dec	bc
  7A51  7D            		ld	a,l
                      	
  7A52  C5            		push	bc
  7A53  F5            		push	af		;
  7A54  DCC87B        		call	c,MPUTHEX2	;A,B,C,D,E,F,H,L
  7A57  F1            		pop	af
  7A58  D4C37B        		call	nc,MPUTHEX4	;M,P,S
  7A5B  CD707C        		call	MPUTHYP
  7A5E  CDF27B        		call	GETHEX
  7A61  C1            		pop	bc
  7A62  20D8          		jr	nz,SETREGLP
                      	
  7A64  F5            		push	af
  7A65  7D            		ld	a,l
  7A66  02            		ld	(bc),a
                      	
  7A67  1B            		dec	de
  7A68  1A            		ld	a,(de)
  7A69  13            		inc	de
  7A6A  FE4D          		cp	'M'
  7A6C  3803          		jr	c,SETREGNEXT	;A,B,C,D,E,F,H,L
                      	
                      	;M,P,S
  7A6E  03            		inc	bc
  7A6F  7C            		ld	a,h
  7A70  02            		ld	(bc),a
                      	
  7A71                	SETREGNEXT:
  7A71  F1            		pop	af
  7A72  FE2C          		cp	','
  7A74  28C6          		jr	z,SETREGLP
  7A76  FE20          		cp	' '
  7A78  28C2          		jr	z,SETREGLP
  7A7A  E1            		pop	hl
  7A7B  C9            		ret
                      	
  7A7C                	REGTBL:
  7A7C  41424344454648		db	'A', 'B', 'C', 'D', 'E', 'F', 'H', 'L'
  7A84  4D505300      		db	'M', 'P', 'S', 00h
  7A88                	REGWKTBL:
  7A88  FAFCFCFCFBFCFE		dw	REGA, REGB, REGC, REGD, REGE, REGF, REGH, REGL
  7A98  FFFC02FD04FD  		dw	REGL, REGPC, REGSP
                      	
                      	
                      	;MON L/R command
  7A9E                	MONL:
  7A9E                	MONR:
  7A9E  2F            		cpl
  7A9F  210EFD        		ld	hl,MONFLG
  7AA2  AE            		xor	(hl)
  7AA3  E608          		and	08h		;'L'->00h, 'R'->08h
  7AA5  AE            		xor	(hl)
  7AA6  77            		ld	(hl),a
                      	
  7AA7  EB            		ex	de,hl
  7AA8  CD707C        		call	MPUTHYP
  7AAB  CDE97B        		call	GETHEX1
  7AAE  C2EA76        		jp	nz,MONERR
  7AB1  EB            		ex	de,hl
  7AB2  D5            		push	de		;offset address
  7AB3  C3F97A        		jp	LOADHEX
                      	
                      	
                      	;load Intel HEX data
                      	;used by CORRIDOR
                      	;input: (sp)=offset, (sp+2)=return address, (MONFLG)-b3(1:CMT,0:RS-232C)
  7AB6                	_LOADHEX:ds	LOADHEX-_LOADHEX
  7AF9                		org	LOADHEX
                      	
  7AF9  CD997B        		call	DUMMY		;just return
  7AFC  D1            		pop	de
  7AFD  CB5E          		bit	3,(hl)		;(MONFLG)
  7AFF  C49A25        		call	nz,INPOPN
  7B02                	MONRLP1:
  7B02  CD707B        		call	MGETDEV
  7B05  FE0D          		cp	0dh
  7B07  2040          		jr	nz,MONRERR
  7B09  CD707B        		call	MGETDEV
  7B0C  FE0A          		cp	0ah
  7B0E  2039          		jr	nz,MONRERR
  7B10  CD707B        		call	MGETDEV
  7B13  FE3A          		cp	':'
  7B15  2032          		jr	nz,MONRERR
  7B17  CD4F7B        		call	MDEVHEX2
  7B1A  2823          		jr	z,MONREND
  7B1C  47            		ld	b,a		;length
  7B1D  CD4F7B        		call	MDEVHEX2	;address (high)
  7B20  67            		ld	h,a
  7B21  CD4F7B        		call	MDEVHEX2	;address (low)
  7B24  6F            		ld	l,a
  7B25  19            		add	hl,de
  7B26  80            		add	a,b
  7B27  84            		add	a,h
  7B28  4F            		ld	c,a		;check sum=b+h+l+...
  7B29  CD4F7B        		call	MDEVHEX2	;record type
  7B2C  201B          		jr	nz,MONRERR
  7B2E                	MONRLP2:
  7B2E  CD4F7B        		call	MDEVHEX2
  7B31  77            		ld	(hl),a
  7B32  81            		add	a,c		;check sum
  7B33  4F            		ld	c,a
  7B34  23            		inc	hl
  7B35  10F7          		djnz	MONRLP2
                      	
  7B37  CD4F7B        		call	MDEVHEX2	;check sum
  7B3A  81            		add	a,c
  7B3B  200C          		jr	nz,MONRERR
  7B3D  18C3          		jr	MONRLP1
                      	
  7B3F                	MONREND:
  7B3F  CD707B        		call	MGETDEV
  7B42  FE1A          		cp	1ah
  7B44  20F9          		jr	nz,MONREND
  7B46  C3AA1A        		jp	RCLOSE
                      	
                      	;error for MON-R
  7B49                	MONRERR:
  7B49  CDAA1A        		call	RCLOSE
  7B4C  C3EA76        		jp	MONERR
                      	
                      	
                      	;output: a,z-flag
                      	;destroy: f
  7B4F                	MDEVHEX2:
  7B4F  C5            		push	bc
  7B50  CD5E7B        		call	MDEVAN
  7B53  07            		rlca
  7B54  07            		rlca
  7B55  07            		rlca
  7B56  07            		rlca
  7B57  47            		ld	b,a
  7B58  CD5E7B        		call	MDEVAN
  7B5B  B0            		or	b
  7B5C  C1            		pop	bc
  7B5D  C9            		ret
                      	
                      	;alphabet or numeric
                      	;output: a
                      	;destroy: f
  7B5E                	MDEVAN:
  7B5E  CD707B        		call	MGETDEV
  7B61  D630          		sub	'0'
  7B63  FE0A          		cp	'9'-'0'+1
  7B65  D8            		ret	c
  7B66  D611          		sub	'A'-'0'
  7B68  FE06          		cp	'F'-'A'+1
  7B6A  D2497B        		jp	nc,MONRERR
  7B6D  C60A          		add	a,0ah
  7B6F  C9            		ret
                      	
                      	;output: a
                      	;destroy: none
  7B70                	MGETDEV:
  7B70  3A0EFD        		ld	a,(MONFLG)
  7B73  E608          		and	08h
  7B75  2808          		jr	z,MGETDEVLP	;RS-232C
  7B77  CD701A        		call	GETCMT
  7B7A  C2497B        		jp	nz,MONRERR
  7B7D  180D          		jr	MGETDEVEND
                      	
  7B7F                	MGETDEVLP:
  7B7F  3A18FA        		ld	a,(STOPFLG)
  7B82  FE03          		cp	03h
  7B84  CA9A7B        		jp	z,MONSTOP2
  7B87  CD4310        		call	GETRSBF
  7B8A  28F3          		jr	z,MGETDEVLP
                      	
  7B8C                	MGETDEVEND:
  7B8C  F5            		push	af
  7B8D  3A0EFD        		ld	a,(MONFLG)
  7B90  E610          		and	10h
  7B92  2804          		jr	z,MGETDEVZ
  7B94  F1            		pop	af
  7B95  C3817C        		jp	MPUTC2
                      	
  7B98                	MGETDEVZ:
  7B98  F1            		pop	af
  7B99                	DUMMY:
  7B99  C9            		ret
                      	
  7B9A                	MONSTOP2:
  7B9A  CDBC0F        		call	GETCH
  7B9D                	MONSTOP:
  7B9D  ED7B0CFD      		ld	sp,(SPSAVE)
  7BA1  F5            		push	af		;for Dezeni Land
  7BA2  F5            		push	af		;for Dezeni Land
  7BA3  CDCD1B        		call	BELL
  7BA6  216035        		ld	hl,BREAK+1
  7BA9  CD5A7C        		call	MPUTS
  7BAC  C38E76        		jp	MONLP1
                      	
                      	
                      	;destroy: af
  7BAF                	PUTHEX2:
  7BAF  F5            		push	af
  7BB0  07            		rlca
  7BB1  07            		rlca
  7BB2  07            		rlca
  7BB3  07            		rlca
  7BB4  CDB87B        		call	PUTHEX1
  7BB7  F1            		pop	af
                      	
  7BB8                	PUTHEX1:
  7BB8  E60F          		and	0fh
  7BBA  FE0A          		cp	0ah
  7BBC  DE69          		sbc	a,69h
  7BBE  27            		daa
  7BBF  CDC726        		call	PUTDEV
  7BC2  C9            		ret
                      	
                      	
                      	;input: hl
                      	;destroy: af,b
  7BC3                	MPUTHEX4:
  7BC3  7C            		ld	a,h
  7BC4  CDC87B        		call	MPUTHEX2
  7BC7  7D            		ld	a,l
                      	;	jr	MPUTHEX2
                      	
                      	;input: a
                      	;destroy: af,b
  7BC8                	MPUTHEX2:
  7BC8  47            		ld	b,a
  7BC9  0F            		rrca
  7BCA  0F            		rrca
  7BCB  0F            		rrca
  7BCC  0F            		rrca
  7BCD  CDD17B        		call	MPUTHEX1
  7BD0  78            		ld	a,b
                      	;	jr	MPUTHEX1
                      	
                      	;input: a
                      	;destroy: af
  7BD1                	MPUTHEX1:
  7BD1  E60F          		and	0fh
  7BD3  FE0A          		cp	0ah
  7BD5  DE69          		sbc	a,69h
  7BD7  27            		daa
  7BD8  C3727C        		jp	MPUTC
                      	
                      	
                      	;output: de=start address, hl=end address
  7BDB                	GETHEX2:
  7BDB  CDE97B        		call	GETHEX1
  7BDE  CAEA76        		jp	z,MONERR
  7BE1  EB            		ex	de,hl
  7BE2  CDE97B        		call	GETHEX1
  7BE5  C2EA76        		jp	nz,MONERR
  7BE8  C9            		ret
                      	
                      	;output: hl=address, z(terminator=0dh?)
  7BE9                	GETHEX1:
  7BE9  CDF27B        		call	GETHEX
  7BEC  C2EA76        		jp	nz,MONERR
  7BEF  FE0D          		cp	0dh
  7BF1  C9            		ret
                      	
                      	
                      	;get hex value keyboard input
                      	;output: hl, a(0dh or ',' or ' ' if no error), z-flag(0=no input)
                      	;destroy: af
  7BF2                	GETHEX:
  7BF2  210000        		ld	hl,0000h
  7BF5  CD257C        		call	GETPUTC
  7BF8  2808          		jr	z,GETHEXZ
  7BFA  FE20          		cp	' '
  7BFC  2804          		jr	z,GETHEXZ
  7BFE  FE2C          		cp	','
  7C00  200C          		jr	nz,GETHEXNZ
  7C02                	GETHEXZ:
  7C02  B7            		or	a
  7C03  C9            		ret
                      	
  7C04                	GETHEXLP:
  7C04  CD257C        		call	GETPUTC
  7C07  C8            		ret	z
  7C08  FE2C          		cp	','
  7C0A  C8            		ret	z
  7C0B  FE20          		cp	' '
  7C0D  C8            		ret	z
  7C0E                	GETHEXNZ:
  7C0E  D630          		sub	'0'
  7C10  FE0A          		cp	'9'-'0'+1
  7C12  3809          		jr	c,GETHEX0F
  7C14  D611          		sub	'A'-'0'
  7C16  FE06          		cp	'F'-'A'+1
  7C18  D2EA76        		jp	nc,MONERR
  7C1B  C60A          		add	a,0ah
  7C1D                	GETHEX0F:
  7C1D  29            		add	hl,hl		;*2
  7C1E  29            		add	hl,hl		;*4
  7C1F  29            		add	hl,hl		;*8
  7C20  29            		add	hl,hl		;*16
  7C21  85            		add	a,l
  7C22  6F            		ld	l,a
  7C23  18DF          		jr	GETHEXLP
                      	
                      	;output: a,z-flag(1=return)
                      	;destroy: f
  7C25                	GETPUTC:
  7C25  CD467C        		call	MGETC
  7C28  FE0D          		cp	0dh
  7C2A  2814          		jr	z,GETPUTCRET
  7C2C  FEFE          		cp	0feh		;page switching key
  7C2E  2042          		jr	nz,MPUTC
  7C30  3A8FFD        		ld	a,(SCREEN2)
  7C33  3C            		inc	a
  7C34  218CFD        		ld	hl,PAGES
  7C37  BE            		cp	(hl)
  7C38  3801          		jr	c,GETPUTCC
  7C3A  AF            		xor	a
  7C3B                	GETPUTCC:
  7C3B  CD8913        		call	CHGSCR
  7C3E  18E5          		jr	GETPUTC
                      	
  7C40                	GETPUTCRET:
  7C40  F5            		push	af
  7C41  CD637C        		call	MPUTNL
  7C44  F1            		pop	af
  7C45  C9            		ret
                      	
                      	
                      	;get a character for MON
                      	;output: a,z-flag(1=return)
                      	;destroy: f
  7C46                	MGETC:
  7C46  3A0EFD        		ld	a,(MONFLG)
  7C49  0F            		rrca
  7C4A  DC7911        		call	c,CSRON
  7C4D                	MGETCLP:
  7C4D  CDBC0F        		call	GETCH
  7C50  28FB          		jr	z,MGETCLP
  7C52  E5            		push	hl
  7C53  CD8111        		call	CSROFF
  7C56  E1            		pop	hl
  7C57  C3EF0B        		jp	TOUPPER
                      	
                      	;put characters for MON
                      	;input: hl=string address
                      	;output: hl=00 address
                      	;destroy: af
  7C5A                	MPUTS:
  7C5A  7E            		ld	a,(hl)
  7C5B  B7            		or	a
  7C5C  C8            		ret	z
  7C5D  CD727C        		call	MPUTC
  7C60  23            		inc	hl
  7C61  18F7          		jr	MPUTS
                      	
                      	;destroy:  a
                      	;put new line for MON
  7C63                	MPUTNL:
  7C63  3E0D          		ld	a,0dh
  7C65  CD727C        		call	MPUTC
  7C68  3E0A          		ld	a,0ah
  7C6A  1806          		jr	MPUTC
                      	
                      	;destroy: a
                      	;put a space for MON
  7C6C                	MPUTSP:
  7C6C  3E20          		ld	a,' '
  7C6E  1802          		jr	MPUTC
                      	
                      	;destroy: a
                      	;put a hyphen for MON
  7C70                	MPUTHYP:
  7C70  3E2D          		ld	a,'-'
                      	;	jr	MPUTC
                      	
                      	;put a character for MON
                      	;input: a
                      	;destroy: none
  7C72                	MPUTC:
  7C72  F5            		push	af
  7C73  CDBC0F        		call	GETCH
  7C76  2808          		jr	z,MPUTCZ
  7C78  CD467C        		call	MGETC
  7C7B  FE03          		cp	03h
  7C7D  CA9D7B        		jp	z,MONSTOP
  7C80                	MPUTCZ:
  7C80  F1            		pop	af
                      	
                      	;no stop check
                      	;destroy: none
  7C81                	MPUTC2:
  7C81  F5            		push	af
  7C82  C5            		push	bc
  7C83  47            		ld	b,a		;
  7C84  3A0EFD        		ld	a,(MONFLG)
  7C87  4F            		ld	c,a		;;
  7C88  0F            		rrca
  7C89  78            		ld	a,b		;
  7C8A  DC7510        		call	c,PRTC
  7C8D  79            		ld	a,c		;;
  7C8E  E620          		and	20h
  7C90  3E01          		ld	a,01h
  7C92  3258FA        		ld	(DEVICE),a
  7C95  78            		ld	a,b		;
  7C96  C4C726        		call	nz,PUTDEV
  7C99  AF            		xor	a
  7C9A  3258FA        		ld	(DEVICE),a
  7C9D  C1            		pop	bc
  7C9E  F1            		pop	af
  7C9F  C9            		ret
                      	
                      	
                      	;LLIST command for mode 5
  7CA0                	C_LLS66:
  7CA0  3E01          		ld	a,01h
  7CA2  3258FA        		ld	(DEVICE),a
                      	;	jp	C_LS66
                      	
                      	;LIST command for mode 5
  7CA5                	C_LS66:
  7CA5  CD593F        		call	SKIPSP
  7CA8  FE4C          		cp	'L'
  7CAA  CA3D7E        		jp	z,LISTL
  7CAD  FE56          		cp	'V'
  7CAF  C2D305        		jp	nz,C_LIST
                      	
                      	;LIST V command
  7CB2                	LISTV:
  7CB2  F1            		pop	af		;cancel return address
  7CB3  23            		inc	hl
  7CB4  CD7B3F        		call	CHKCLCM
  7CB7  0600          		ld	b,00h
  7CB9  2814          		jr	z,LSVSTRT
  7CBB  CD593F        		call	SKIPSP
  7CBE  D641          		sub	'A'
  7CC0  FE1A          		cp	'Z'-'A'+1
  7CC2  D2CB03        		jp	nc,FCERR
  7CC5  CD8F33        		call	CHKVAR
  7CC8  7E            		ld	a,(hl)
  7CC9  FE28          		cp	'('
  7CCB  2002          		jr	nz,LSVSTRT
  7CCD  CBF8          		set	7,b
                      	
  7CCF                	LSVSTRT:
  7CCF  219AFF        		ld	hl,0-102
  7CD2  39            		add	hl,sp
  7CD3  22D1FE        		ld	(FNAME),hl
  7CD6  EB            		ex	de,hl
                      	
  7CD7  78            		ld	a,b
  7CD8  B7            		or	a
  7CD9  203A          		jr	nz,WITHVAR	;list v,var
                      	
  7CDB  2A29FF        		ld	hl,(BASICAD)
  7CDE                	LSVLP1:
  7CDE  D5            		push	de
  7CDF  CDEC7D        		call	SRCHNXV
  7CE2  D1            		pop	de
  7CE3  2833          		jr	z,LSVPUT
  7CE5  7E            		ld	a,(hl)
  7CE6  FE28          		cp	'('
  7CE8  2002          		jr	nz,LSVNZ
  7CEA  CBF8          		set	7,b
  7CEC                	LSVNZ:
  7CEC  E5            		push	hl
  7CED  2AD1FE        		ld	hl,(FNAME)
  7CF0                	LSVLP2:
  7CF0  E7            		rst	CPHLDE
  7CF1  280D          		jr	z,LSVZ1		;not found
  7CF3  7E            		ld	a,(hl)
  7CF4  2B            		dec	hl
  7CF5  B8            		cp	b
  7CF6  7E            		ld	a,(hl)
  7CF7  2B            		dec	hl
  7CF8  20F6          		jr	nz,LSVLP2
  7CFA  B9            		cp	c
  7CFB  20F3          		jr	nz,LSVLP2
  7CFD  E1            		pop	hl
  7CFE  18DE          		jr	LSVLP1
                      	
  7D00                	LSVZ1:
  7D00  CD067D        		call	ADDVAR
  7D03  E1            		pop	hl
  7D04  18D8          		jr	LSVLP1
                      	
                      	;add variable name list
  7D06                	ADDVAR:	
  7D06  2A5AFF        		ld	hl,(FREEAD)
  7D09  1B            		dec	de
  7D0A  E7            		rst	CPHLDE
  7D0B  D2D103        		jp	nc,OMERR
  7D0E  62            		ld	h,d
  7D0F  6B            		ld	l,e
  7D10  71            		ld	(hl),c
  7D11  23            		inc	hl
  7D12  70            		ld	(hl),b
  7D13  1B            		dec	de
  7D14  C9            		ret
                      	
  7D15                	WITHVAR:
  7D15  CD067D        		call	ADDVAR
                      	
  7D18                	LSVPUT:
  7D18  2AD1FE        		ld	hl,(FNAME)
  7D1B                	LSVLP3:
  7D1B  E7            		rst	CPHLDE
  7D1C  CA4204        		jp	z,EDIT
  7D1F  46            		ld	b,(hl)
  7D20  2B            		dec	hl
  7D21  4E            		ld	c,(hl)
  7D22  2B            		dec	hl
                      	
  7D23  E5            		push	hl
  7D24  D5            		push	de
  7D25  C5            		push	bc
                      	
  7D26  1605          		ld	d,05h
  7D28  78            		ld	a,b
  7D29  E67F          		and	7fh
  7D2B  CDC726        		call	PUTDEV
  7D2E  79            		ld	a,c
  7D2F  E67F          		and	7fh
  7D31  2804          		jr	z,LSVZ2
  7D33  CDC726        		call	PUTDEV
  7D36  15            		dec	d
  7D37                	LSVZ2:
  7D37  79            		ld	a,c
  7D38  07            		rlca
  7D39  3006          		jr	nc,LSVNC
  7D3B  3E24          		ld	a,'$'
  7D3D  CDC726        		call	PUTDEV
  7D40  15            		dec	d
  7D41                	LSVNC:
  7D41  78            		ld	a,b
  7D42  07            		rlca
  7D43  300C          		jr	nc,LSVLP4
  7D45  3E28          		ld	a,'('
  7D47  CDC726        		call	PUTDEV
  7D4A  3E29          		ld	a,')'
  7D4C  CDC726        		call	PUTDEV
  7D4F  15            		dec	d
  7D50  15            		dec	d
  7D51                	LSVLP4:
  7D51  3E20          		ld	a,' '
  7D53  CDC726        		call	PUTDEV
  7D56  15            		dec	d
  7D57  20F8          		jr	nz,LSVLP4
  7D59  21F87E        		ld	hl,SPHYPSP+1
  7D5C  CDCF30        		call	PUTS
  7D5F  2A29FF        		ld	hl,(BASICAD)
  7D62                	LSVLP5:
  7D62  CD4D27        		call	STOPESC
  7D65  CDEC7D        		call	SRCHNXV
  7D68  281A          		jr	z,LSVNEXTV
  7D6A  E3            		ex	(sp),hl
  7D6B  7C            		ld	a,h
  7D6C  E3            		ex	(sp),hl
  7D6D  B8            		cp	b
  7D6E  20F2          		jr	nz,LSVLP5
  7D70  E3            		ex	(sp),hl
  7D71  7D            		ld	a,l
  7D72  E3            		ex	(sp),hl
  7D73  B9            		cp	c
  7D74  20EC          		jr	nz,LSVLP5
  7D76  E5            		push	hl
  7D77  D5            		push	de
  7D78  CDFF7E        		call	PUTDEC5SP3
                      	
  7D7B  D1            		pop	de
  7D7C  E1            		pop	hl
  7D7D                	LSVLP6:
  7D7D  7E            		ld	a,(hl)
  7D7E  B7            		or	a
  7D7F  28E1          		jr	z,LSVLP5
  7D81  23            		inc	hl
  7D82  18F9          		jr	LSVLP6
                      	
                      	
  7D84                	LSVNEXTV:
  7D84  CD3927        		call	PUTNL
  7D87  C1            		pop	bc
  7D88  D1            		pop	de
  7D89  E1            		pop	hl
  7D8A  188F          		jr	LSVLP3	
                      	
                      	
  7D8C                	LSVHEX:
  7D8C  CD593F        		call	SKIPSP
  7D8F  FE48          		cp	'H'
  7D91  2059          		jr	nz,SRCHNXV
  7D93                	LSVHEXLP:
  7D93  CD583F        		call	SKIPSPINC
  7D96  FE93          		cp	DEF_
  7D98  28F9          		jr	z,LSVHEXLP
  7D9A  D630          		sub	'0'
  7D9C  FE0A          		cp	'9'-'0'+1
  7D9E  38F3          		jr	c,LSVHEXLP
  7DA0  D611          		sub	'A'-'0'
  7DA2  FE06          		cp	'F'-'A'+1
  7DA4  38ED          		jr	c,LSVHEXLP
  7DA6  1844          		jr	SRCHNXV
                      	
  7DA8                	LSVDOT:
  7DA8  0601          		ld	b,01h
  7DAA  1802          		jr	LSVNUMLP
                      	
                      	;numerical value
  7DAC                	LSVNUM:
  7DAC  0602          		ld	b,02h
  7DAE                	LSVNUMLP:
  7DAE  CD593F        		call	SKIPSP
  7DB1  23            		inc	hl
  7DB2  B7            		or	a
  7DB3  2868          		jr	z,SRCHNXVZ
  7DB5  FE45          		cp	'E'
  7DB7  2812          		jr	z,LSVEXP
  7DB9  FE2E          		cp	'.'
  7DBB  280A          		jr	z,LSVDOT2
  7DBD  FE30          		cp	'0'
  7DBF  382D          		jr	c,SRCHNXV2
  7DC1  FE3A          		cp	'9'+1
  7DC3  3029          		jr	nc,SRCHNXV2
  7DC5  18E7          		jr	LSVNUMLP
                      	
  7DC7                	LSVDOT2:
  7DC7  10E5          		djnz	LSVNUMLP
  7DC9  1823          		jr	SRCHNXV2
                      	
  7DCB                	LSVEXP:
  7DCB  CD593F        		call	SKIPSP
  7DCE  FECA          		cp	PLUS_
  7DD0  2804          		jr	z,LSVSIGN
  7DD2  FECB          		cp	MINUS_
  7DD4  200B          		jr	nz,LSVELP
  7DD6                	LSVSIGN:
  7DD6  CD583F        		call	SKIPSPINC
  7DD9  FECA          		cp	PLUS_
  7DDB  28F9          		jr	z,LSVSIGN
  7DDD  FECB          		cp	MINUS_
  7DDF  28F5          		jr	z,LSVSIGN
  7DE1                	LSVELP:
  7DE1  D630          		sub	'0'
  7DE3  FE0A          		cp	'9'-'0'+1
  7DE5  3005          		jr	nc,SRCHNXV
  7DE7  CD583F        		call	SKIPSPINC
  7DEA  18F5          		jr	LSVELP
                      	
                      	
                      	;search next variable
                      	;input: hl=program address, (de=line number)
                      	;output: bc=name, de=line number, hl=next address, z-flag(1=program end)
                      	;destroy: af
  7DEC                	SRCHNXV:
  7DEC  7E            		ld	a,(hl)
  7DED  23            		inc	hl
  7DEE                	SRCHNXV2:
  7DEE  B7            		or	a
  7DEF  282C          		jr	z,SRCHNXVZ
  7DF1  FE22          		cp	22h		;double quotation
  7DF3  2833          		jr	z,LSVDQ
  7DF5  FE8E          		cp	REM_
  7DF7  283A          		jr	z,LSVREM
  7DF9  FE83          		cp	DATA_
  7DFB  283B          		jr	z,LSVDATA
  7DFD  FE26          		cp	'&'
  7DFF  288B          		jr	z,LSVHEX
  7E01  FE2E          		cp	'.'
  7E03  28A3          		jr	z,LSVDOT
  7E05  D630          		sub	'0'
  7E07  FE0A          		cp	'9'-'0'+1
  7E09  38A1          		jr	c,LSVNUM
  7E0B  D611          		sub	'A'-'0'
  7E0D  FE1A          		cp	'Z'-'A'+1
  7E0F  30DB          		jr	nc,SRCHNXV
                      	
                      	;variable found
  7E11  2B            		dec	hl
  7E12  CD8F33        		call	CHKVAR
  7E15  7E            		ld	a,(hl)
  7E16  FE28          		cp	'('
  7E18  C0            		ret	nz
  7E19  CBF8          		set	7,b
  7E1B  B7            		or	a		;reset z-flag
  7E1C  C9            		ret
                      	
                      	;line end
  7E1D                	SRCHNXVZ:
  7E1D  7E            		ld	a,(hl)
  7E1E  23            		inc	hl
  7E1F  B6            		or	(hl)
  7E20  23            		inc	hl
  7E21  C8            		ret	z		;program end
  7E22  5E            		ld	e,(hl)
  7E23  23            		inc	hl
  7E24  56            		ld	d,(hl)
  7E25  23            		inc	hl
  7E26  18C4          		jr	SRCHNXV
                      	
  7E28                	LSVDQ:
  7E28  7E            		ld	a,(hl)
  7E29  23            		inc	hl
  7E2A  B7            		or	a
  7E2B  28F0          		jr	z,SRCHNXVZ
  7E2D  FE22          		cp	22h		;double quotation
  7E2F  20F7          		jr	nz,LSVDQ
  7E31  18B9          		jr	SRCHNXV
                      		
  7E33                	LSVREM:
  7E33  CDE207        		call	C_REM
  7E36  18B4          		jr	SRCHNXV
                      	
  7E38                	LSVDATA:
  7E38  CDE007        		call	C_DATA
  7E3B  18AF          		jr	SRCHNXV
                      	
                      	
                      	;LIST L command
  7E3D                	LISTL:
  7E3D  F1            		pop	af		;cancel return address
  7E3E  CD5F3F        		call	CHKCLNINC
  7E41  01FFFF        		ld	bc,0ffffh
  7E44  280E          		jr	z,LSLSTRT
  7E46  FE2C          		cp	','
  7E48  C2C203        		jp	nz,SNERR
  7E4B  D7            		rst	ANADAT
  7E4C  D2CB03        		jp	nc,FCERR
  7E4F  CD6807        		call	GETLN
  7E52  42            		ld	b,d
  7E53  4B            		ld	c,e
                      	
  7E54                	LSLSTRT:
  7E54  2A5FFA        		ld	hl,(STARTAD)
  7E57                	LSLLP1:
  7E57  7E            		ld	a,(hl)
  7E58  23            		inc	hl
  7E59  B6            		or	(hl)
  7E5A  CA4204        		jp	z,EDIT
  7E5D  E5            		push	hl
  7E5E  23            		inc	hl
  7E5F  5E            		ld	e,(hl)
  7E60  23            		inc	hl
  7E61  56            		ld	d,(hl)
                      	
  7E62  78            		ld	a,b
  7E63  A1            		and	c
  7E64  3C            		inc	a
  7E65  281D          		jr	z,LSLOK
  7E67  60            		ld	h,b
  7E68  69            		ld	l,c
  7E69  E7            		rst	CPHLDE
  7E6A  2818          		jr	z,LSLOK
  7E6C  E1            		pop	hl
  7E6D  7E            		ld	a,(hl)
  7E6E  2B            		dec	hl
  7E6F  6E            		ld	l,(hl)
  7E70  67            		ld	h,a
  7E71  18E4          		jr	LSLLP1
                      	
  7E73                	LSLNEXT:
  7E73  23            		inc	hl
  7E74  7E            		ld	a,(hl)
  7E75  23            		inc	hl
  7E76  B6            		or	(hl)
  7E77  201B          		jr	nz,LSLLP2
  7E79  CD3927        		call	PUTNL
  7E7C  C1            		pop	bc
  7E7D  E1            		pop	hl
  7E7E  7E            		ld	a,(hl)
  7E7F  2B            		dec	hl
  7E80  6E            		ld	l,(hl)
  7E81  67            		ld	h,a
  7E82  18D3          		jr	LSLLP1
                      	
  7E84                	LSLOK:
  7E84  C5            		push	bc
  7E85  D5            		push	de
  7E86  CD077F        		call	PUTDEC5
  7E89  21F77E        		ld	hl,SPHYPSP
  7E8C  CDCF30        		call	PUTS
  7E8F  D1            		pop	de
                      	
  7E90  2A5FFA        		ld	hl,(STARTAD)
  7E93  23            		inc	hl
  7E94                	LSLLP2:
  7E94  23            		inc	hl
  7E95  7E            		ld	a,(hl)
  7E96  23            		inc	hl
  7E97  46            		ld	b,(hl)
  7E98  4F            		ld	c,a
  7E99  23            		inc	hl
                      	
  7E9A                	LSLLP3:
  7E9A  CD4D27        		call	STOPESC
  7E9D  7E            		ld	a,(hl)
  7E9E  B7            		or	a
  7E9F  28D2          		jr	z,LSLNEXT
  7EA1  23            		inc	hl
  7EA2  FE22          		cp	22h		;double quotation
  7EA4  283A          		jr	z,LSLDQ
  7EA6  FE8E          		cp	REM_
  7EA8  2841          		jr	z,LSLREM
  7EAA  FE83          		cp	DATA_
  7EAC  2842          		jr	z,LSLDATA
                      	
  7EAE  FE88          		cp	GOTO_
  7EB0  2814          		jr	z,LSLCHK
  7EB2  FE89          		cp	RUN_
  7EB4  2810          		jr	z,LSLCHK
  7EB6  FE8B          		cp	RSTR_
  7EB8  280C          		jr	z,LSLCHK
  7EBA  FE8C          		cp	GOSUB_
  7EBC  2808          		jr	z,LSLCHK
  7EBE  FEC7          		cp	THEN_
  7EC0  2804          		jr	z,LSLCHK
  7EC2  FE97          		cp	LIST_
  7EC4  20D4          		jr	nz,LSLLP3
                      	
  7EC6                	LSLCHK:
  7EC6  D5            		push	de
  7EC7  C5            		push	bc
  7EC8  CD6807        		call	GETLN
  7ECB  C1            		pop	bc
  7ECC  E3            		ex	(sp),hl
  7ECD  E7            		rst	CPHLDE
  7ECE  E5            		push	hl
  7ECF  C5            		push	bc
  7ED0  50            		ld	d,b
  7ED1  59            		ld	e,c
  7ED2  CCFF7E        		call	z,PUTDEC5SP3
  7ED5  C1            		pop	bc
  7ED6  D1            		pop	de
  7ED7  E1            		pop	hl
  7ED8  7E            		ld	a,(hl)
  7ED9  FE2C          		cp	','
  7EDB  20BD          		jr	nz,LSLLP3
  7EDD  23            		inc	hl
  7EDE  18E6          		jr	LSLCHK
                      	
  7EE0                	LSLDQ:
  7EE0  7E            		ld	a,(hl)
  7EE1  B7            		or	a
  7EE2  288F          		jr	z,LSLNEXT
  7EE4  23            		inc	hl
  7EE5  FE22          		cp	22h		;double quotation
  7EE7  20F7          		jr	nz,LSLDQ
  7EE9  18AF          		jr	LSLLP3
                      	
  7EEB                	LSLREM:
  7EEB  CDE207        		call	C_REM
  7EEE  1883          		jr	LSLNEXT
                      	
  7EF0                	LSLDATA:
  7EF0  C5            		push	bc
  7EF1  CDE007        		call	C_DATA
  7EF4  C1            		pop	bc
  7EF5  18A3          		jr	LSLLP3
                      	
  7EF7                	SPHYPSP:
  7EF7  202D2000      		db	" - ", 00h
  7EFB                	SP3:
  7EFB  20202000      		db	"   ", 00h
                      	
                      	
                      	;put "   " and 5-digit decimal (0-padding)
                      	;input: de
                      	;destroy: af,bc,de,hl
  7EFF                	PUTDEC5SP3:
  7EFF  21FB7E        		ld	hl,SP3
  7F02  D5            		push	de
  7F03  CDCF30        		call	PUTS
  7F06  D1            		pop	de
                      	;	jr	PUTDEC5
                      	
                      	;put 5-digit decimal (0-padding)
                      	;input: de
                      	;destroy: af,bc,de,hl
  7F07                	PUTDEC5:
  7F07  21207F        		ld	hl,PUTD5TBL
  7F0A  0604          		ld	b,04h
  7F0C                	PUTD5LP:
  7F0C  4E            		ld	c,(hl)
  7F0D  23            		inc	hl
  7F0E  7E            		ld	a,(hl)
  7F0F  23            		inc	hl
  7F10  E5            		push	hl
  7F11  69            		ld	l,c
  7F12  67            		ld	h,a
  7F13  E7            		rst	CPHLDE
  7F14  3E30          		ld	a,'0'
  7F16  D4C726        		call	nc,PUTDEV
  7F19  E1            		pop	hl
  7F1A  10F0          		djnz	PUTD5LP
  7F1C  EB            		ex	de,hl
  7F1D  C3A13A        		jp	PUTI2
                      	
  7F20                	PUTD5TBL:
  7F20  0F27E703630009		dw	9999, 999, 99, 9
                      	
                      	
                      	;DELETE command
  7F28                	C_DEL:
  7F28  F1            		pop	af		;cancel return address
  7F29  CD603F        		call	CHKCLN
  7F2C  CACB03        		jp	z,FCERR
                      	
  7F2F  CD6807        		call	GETLN
  7F32  D5            		push	de		;start line number
  7F33  CD603F        		call	CHKCLN
  7F36  281D          		jr	z,DELSTRT
                      	
  7F38  FECB          		cp	MINUS_
  7F3A  C2C203        		jp	nz,SNERR
  7F3D  CD5F3F        		call	CHKCLNINC
  7F40  2007          		jr	nz,DELNZ
  7F42  D1            		pop	de
  7F43  01FAFF        		ld	bc,65530
  7F46  C5            		push	bc
  7F47  180C          		jr	DELSTRT
                      	
  7F49                	DELNZ:
  7F49  CD6807        		call	GETLN
  7F4C  CD603F        		call	CHKCLN
  7F4F  C2C203        		jp	nz,SNERR
                      	
  7F52  E1            		pop	hl
  7F53  D5            		push	de		;push end line number
  7F54  EB            		ex	de,hl		;de <- start line number
                      	
                      	;de=start line number, (sp)=end line number
  7F55                	DELSTRT:
  7F55  CDFB04        		call	SRCHLN
  7F58  D1            		pop	de		;end line number
  7F59  C5            		push	bc		;start address
  7F5A  CDFB04        		call	SRCHLN
  7F5D  3802          		jr	c,DELC
  7F5F  60            		ld	h,b		;end address
  7F60  69            		ld	l,c
  7F61                	DELC:
  7F61  D1            		pop	de		;start address
                      	
  7F62  EB            		ex	de,hl
  7F63  E7            		rst	CPHLDE		;start-end
  7F64  D24204        		jp	nc,EDIT
  7F67  EB            		ex	de,hl
                      	
  7F68  42            		ld	b,d
  7F69  4B            		ld	c,e
  7F6A  C5            		push	bc		;dummy,(bc)=0
  7F6B  CDE504        		call	DELPRG
  7F6E  C32B24        		jp	OKCHGLKP
                      	
                      	
                      	;66 ROM end
  7F71                	_8000H:	ds	8000h-_8000H
                      	
  8000                		end
