	TITLE	'6545 KEYBOARD, EDITOR, AND SCREEN HANDLER'
	SUBTTL	' '
	LIST	F,-M,L,R
	SPACE	4,10
***	Copyright 1984 ATARI.  Unauthorized reproduction, adaptation,
*	distribution, performance or display of this computer program
*	or the associated audiovisual work is strictly prohibited.
	SPACE	4,10
***	6545 Keyboard, Editor and Screen Handler
*
*	NOTES

*	The sections of the 6545 Keyboard, Editor and Screen Handler
*	appear in the following order:
*
*	Equates and Definitions
*
*		System Symbol Equates
*		System Address Equates
*		Miscellaneous Address Equates
*		Macro Definitions
*
*	Code and Data
*
	LIST	F,-M,L,R
	SUBTTL	'System Symbol Equates'
	SPACE	4,10


**	Identification Equates


IDREV	EQU	$02	;identification revision number
IDDAY	EQU	$10	;identification day
IDMON	EQU	$05	;identification month
IDYEAR	EQU	$83	;identification year
IDCPU	EQU	$02	;identification CPU series
IDPN1	EQU	'B'	;identification part number field 1
IDPN2	EQU	'B'	;identification part number field 2
IDPN3	EQU	$00	;identification part number field 3
IDPN4	EQU	$00	;identification part number field 4
IDPN5	EQU	$01	;identification part number field 5
	SPACE	4,10
**	Configuration Equates
*
*	NOTES
*		Problem: last byte of HATABS (as defined by MAXDEV)
*		overlaps first power-up validation byte.


MAXDEV	EQU	33	;offset to last possible entry of HATABS
IOCBSZ	EQU	16	;length of IOCB

SEIOCB	EQU	0*IOCBSZ	;screen editor IOCB index
MAXIOC	EQU	8*IOCBSZ	;first invalid IOCB index

LEDGE	EQU	0	;left edge
REDGE	EQU	79	;right edge

	SPACE	4,10
**	IOCB Command Code Equates


OPEN	EQU	$03	;open
GETREC	EQU	$05	;get record
GETCHR	EQU	$07	;get character(s)
PUTREC	EQU	$09	;put record
PUTCHR	EQU	$0B	;put character(s)
CLOSE	EQU	$0C	;close
STATIS	EQU	$0D	;status
SPECIL	EQU	$0E	;special
	SPACE	4,10
**	Special Entry Command Equates


;	Screen Commands

FORTY	EQU	40	;standard screen
EIGHTY	EQU	80	;80 column screen
	SPACE	4,10
**	Device Code Equates


SCREDT	EQU	'E'	;screen editor
	SPACE	4,10
**	Character and Key Code Equates


CLS	EQU	$7D	;clear screen
EOL	EQU	$9B	;end of line (RETURN)

	SPACE	4,10
**	Status Code Equates


SUCCES	EQU	1	;successful operation

BRKABT	EQU	128	;BREAK key abort
NONDEV	EQU	130	;nonexistent device error
NVALID	EQU	132	;invalid command error
CRSROR	EQU	141	;cursor overrange error
BADMOD	EQU	145	;bad screen mode number error
FNCNOT	EQU	146	;function not implemented in handler error
SCRMEM	EQU	147	;insufficient memory for screen mode error

	SUBTTL	'System Address Equates'
	SPACE	4,10
**	Page Zero Address Equates


LNFLG	EQU	$0000	;1-byte LNBUG flag (0 = not LNBUG)
NGFLAG	EQU	$0001	;1-byte memory status (0 = failure)

;	Not Cleared

CASINI	EQU	$0002	;2-byte cassette program initialization address
RAMLO	EQU	$0004	;2-byte RAM address for memory test
TRAMSZ	EQU	$0006	;1-byte RAM size temporary
CMCMD	EQU	$0007	;1-byte command communications

;	Cleared upon Coldstart Only

WARMST	EQU	$0008	;1-byte warmstart flag (0 = coldstart)
BOOT?	EQU	$0009	;1-byte successful boot flags
DOSVEC	EQU	$000A	;2-byte disk program start vector
DOSINI	EQU	$000C	;2-byte disk program initialization address
APPMHI	EQU	$000E	;2-byte applications memory high limit

;	Cleared upon Coldstart or Warmstart

INTZBS	EQU	$0010	;first page zero location to clear

POKMSK	EQU	$0010	;1-byte IRQEN shadow
BRKKEY	EQU	$0011	;1-byte BREAK key flag (0 = no BREAK)
RTCLOK	EQU	$0012	;3-byte real time clock (16 millisecond units)
BUFADR	EQU	$0015	;2-byte disk interface buffer address
ICCOMT	EQU	$0017	;1-byte CIO command table index
DSKFMS	EQU	$0018	;2-byte DOS File Management System pointer
DSKUTL	EQU	$001A	;2-byte DOS utility pointer
ABUFPT	EQU	$001C	;4-byte ACMI buffer pointer area

ZIOCB	EQU	$0020	;address of page zero IOCB
IOCBAS	EQU	$0020	;16-byte page zero IOCB
ICHIDZ	EQU	$0020	;1-byte handler ID ($FF = IOCB free)
ICDNOZ	EQU	$0021	;1-byte device number
ICCOMZ	EQU	$0022	;1-byte command code
ICSTAZ	EQU	$0023	;1-byte status of last action
ICBALZ	EQU	$0024	;1-byte low buffer address
ICBAHZ	EQU	$0025	;1-byte high buffer address
ICPTLZ	EQU	$0026	;1-byte low PUT-BYTE routine address-1
ICPTHZ	EQU	$0027	;1-byte high PUT-BYTE routine address-1
ICBLLZ	EQU	$0028	;1-byte low buffer length
ICBLHZ	EQU	$0029	;1-byte high buffer length
ICAX1Z	EQU	$002A	;1-byte first auxiliary information
ICAX2Z	EQU	$002B	;1-byte second auxiliary information
ICSPRZ	EQU	$002C	;4-byte spares

ENTVEC	EQU	$002C	;2-byte (not used)
ICIDNO	EQU	$002E	;1-byte IOCB index (IOCB number times IOCBSZ)
CIOCHR	EQU	$002F	;1-byte character for current CIO operation

STATUS	EQU	$0030	;1-byte SIO operation status
CHKSUM	EQU	$0031	;1-byte checksum (single byte sum with carry)
BUFRLO	EQU	$0032	;1-byte low data buffer address
BUFRHI	EQU	$0033	;1-byte high data buffer address
BFENLO	EQU	$0034	;1-byte low data buffer end address
BFENHI	EQU	$0035	;1-byte high data buffer end address
LTEMP	EQU	$0036	;2-byte relocating loader temporary
BUFRFL	EQU	$0038	;1-byte data buffer full flag (0 = not full)
RECVDN	EQU	$0039	;1-byte receive-frame done flag (0 = not done)
XMTDON	EQU	$003A	;1-byte transmit-frame done flag (0 = not done)
CHKSNT	EQU	$003B	;1-byte checksum sent flag (0 = not sent)
NOCKSM	EQU	$003C	;1-byte no checksum follows data flag (0 = does)
BPTR	EQU	$003D	;1-byte cassette buffer pointer
FTYPE	EQU	$003E	;1-byte cassette IRG type (neg. = continuous)
FEOF	EQU	$003F	;1-byte cassette EOF flag (0 = no EOF)
FREQ	EQU	$0040	;1-byte cassette beep counter
SOUNDR	EQU	$0041	;1-byte noisy I/O flag (0 = quiet)

CRITIC	EQU	$0042	;1-byte critical section flag (0 = not critical)

FMSZPG	EQU	$0043	;7-byte reserved for DOS File Management System

ZCHAIN	EQU	$004A	;2-byte handler linkage chain pointer
DSTAT	EQU	$004C	;1-byte display status
ATRACT	EQU	$004D	;1-byte attract-mode timer and flag
DRKMSK	EQU	$004E	;1-byte attract-mode dark (luminance) mask
COLRSH	EQU	$004F	;1-byte attract-mode color shift
TMPCHR	EQU	$0050	;1-byte	temporary character
HOLD1	EQU	$0051	;1-byte	temporary
LMARGN	EQU	$0052	;1-byte text column left margin
RMARGN	EQU	$0053	;1-byte text column right margin
ROWCRS	EQU	$0054	;1-byte cursor row
COLCRS	EQU	$0055	;2-byte cursor column
DINDEX	EQU	$0057	;1-byte display mode
SAVMSC	EQU	$0058	;2-byte saved memory scan counter
OLDROW	EQU	$005A	;1-byte prior row
OLDCOL	EQU	$005B	;2-byte prior column
OLDCHR	EQU	$005D	;1-byte saved character under cursor
OLDADR	EQU	$005E	;2-byte saved cursor memory address
FKDEF	EQU	$0060	;2-byte function key definition table address
PALNTS	EQU	$0062	;1-byte PAL/NTSC indicator (0 = NTSC, PAL = 1)
LOGCOL	EQU	$0063	;1-byte logical line cursor column
ADRESS	EQU	$0064	;2-byte temporary address

MLTTMP	EQU	$0066	;1-byte temporary
OPNTMP	EQU	$0066	;1-byte open temporary
TOADR	EQU	$0066	;2-byte destination address

SAVADR	EQU	$0068	;2-byte saved address
FRMADR	EQU	$0068	;2-byte source address

RAMTOP	EQU	$006A	;1-byte RAM size
BUFCNT	EQU	$006B	;1-byte buffer count (logical line size)
BUFSTR	EQU	$006C	;2-byte buffer start pointer
BITMSK	EQU	$006E	;1-byte bit mask for bit map operation
SHFAMT	EQU	$006F	;1-byte shift amount for pixel justification
ROWAC	EQU	$0070	;2-byte draw working row
COLAC	EQU	$0072	;2-byte draw working column
ENDPT	EQU	$0074	;2-byte end point
DELTAR	EQU	$0076	;1-byte row difference
DELTAC	EQU	$0077	;2-byte column difference
KEYDEF	EQU	$0079	;2-byte key definition table address
SWPFLG	EQU	$007B	;1-byte split screen swap flag (0 = not swapped)
HOLDCH	EQU	$007C	;1-byte temporary character
INSDAT	EQU	$007D	;1-byte temporary
COUNTR	EQU	$007E	;2-byte draw iteration count

;	Reserved for Application and Floating Point Package

;	EQU	$0080	;128 bytes reserved for application and FPP
	SPACE	4,10
**	Floating Point Package Page Zero Address Equates


FR0	EQU	$00D4	;6-byte register 0
FR0M	EQU	$00D5	;5-byte register 0 mantissa
QTEMP	EQU	$00D9	;1-byte temporary

FRE	EQU	$00DA	;6-byte (internal) register E

FR1	EQU	$00E0	;6-byte register 1
FR1M	EQU	$00E1	;5-byte register 1 mantissa

FR2	EQU	$00E6	;6-byte (internal) register 2

FRX	EQU	$00EC	;1-byte temporary

EEXP	EQU	$00ED	;1-byte value of exponent

FRSIGN	EQU	$00EE	;1-byte floating point sign
NSIGN	EQU	$00EE	;1-byte sign of number

PLYCNT	EQU	$00EF	;1-byte polynomial degree
ESIGN	EQU	$00EF	;1-byte sign of exponent

SGNFLG	EQU	$00F0	;1-byte sign flag
FCHFLG	EQU	$00F0	;1-byte first character flag

XFMFLG	EQU	$00F1	;1-byte transform flag
DIGRT	EQU	$00F1	;1-byte number of digits after decimal point

CIX	EQU	$00F2	;1-byte current input index
INBUFF	EQU	$00F3	;2-byte line input buffer

ZTEMP1	EQU	$00F5	;2-byte temporary
ZTEMP4	EQU	$00F7	;2-byte temporary
ZTEMP3	EQU	$00F9	;2-byte temporary

FLPTR	EQU	$00FC	;2-byte floating point number pointer
FPTR2	EQU	$00FE	;2-byte floating point number pointer
	SPACE	4,10
**	Page One (Stack) Address Equates


;	EQU	$0100	;256-byte stack
	SPACE	4,10
**	Page Two Address Equates


INTABS	EQU	$0200	;42-byte interrupt handler table

VDSLST	EQU	$0200	;2-byte display list NMI vector
VPRCED	EQU	$0202	;2-byte serial I/O proceed line IRQ vector
VINTER	EQU	$0204	;2-byte serial I/O interrupt line IRQ vector
VBREAK	EQU	$0206	;2-byte BRK instruction IRQ vector
VKEYBD	EQU	$0208	;2-byte keyboard IRQ vector
VSERIN	EQU	$020A	;2-byte serial input ready IRQ vector
VSEROR	EQU	$020C	;2-byte serial output ready IRQ vector
VSEROC	EQU	$020E	;2-byte serial output complete IRQ vector
VTIMR1	EQU	$0210	;2-byte POKEY timer 1 IRQ vector
VTIMR2	EQU	$0212	;2-byte POKEY timer 2 IRQ vector
VTIMR4	EQU	$0214	;2-byte POKEY timer 4 IRQ vector
VIMIRQ	EQU	$0216	;2-byte immediate IRQ vector
CDTMV1	EQU	$0218	;2-byte countdown timer 1 value
CDTMV2	EQU	$021A	;2-byte countdown timer 2 value
CDTMV3	EQU	$021C	;2-byte countdown timer 3 value
CDTMV4	EQU	$021E	;2-byte countdown timer 4 value
CDTMV5	EQU	$0220	;2-byte countdown timer 5 value
VVBLKI	EQU	$0222	;2-byte immediate VBLANK NMI vector
VVBLKD	EQU	$0224	;2-byte deferred VBLANK NMI vector
CDTMA1	EQU	$0226	;2-byte countdown timer 1 vector
CDTMA2	EQU	$0228	;2-byte countdown timer 2 vector

CDTMF3	EQU	$022A	;1-byte countdown timer 3 flag (0 = expired)
SRTIMR	EQU	$022B	;1-byte software key repeat timer
CDTMF4	EQU	$022C	;1-byte countdown timer 4 flag (0 = expired)
INTEMP	EQU	$022D	;1-byte temporary
CDTMF5	EQU	$022E	;1-byte countdown timer 5 flag (0 = expired)
SDMCTL	EQU	$022F	;1-byte DMACTL shadow
SDLSTL	EQU	$0230	;1-byte DLISTL shadow
SDLSTH	EQU	$0231	;1-byte DLISTH shadow
SSKCTL	EQU	$0232	;1-byte SKCTL shadow
LCOUNT	EQU	$0233	;1-byte relocating loader record length counter
LPENH	EQU	$0234	;1-byte light pen horizontal value
LPENV	EQU	$0235	;1-byte light pen vertical value
BRKKY	EQU	$0236	;2-byte BREAK key vector
VPIRQ	EQU	$0238	;2-byte parallel device IRQ vector
CDEVIC	EQU	$023A	;1-byte command frame device ID
CCOMND	EQU	$023B	;1-byte command frame command
CAUX1	EQU	$023C	;1-byte command auxiliary 1
CAUX2	EQU	$023D	;1-byte command auxiliary 2

TEMP	EQU	$023E	;1-byte temporary

	ASSERT	low TEMP<>$FF	;may not be the last word on a page

ERRFLG	EQU	$023F	;1-byte I/O error flag (0 = no error)

	ASSERT	low ERRFLG<>$FF	;may not be the last word on a page

DFLAGS	EQU	$0240	;1-byte disk flags from sector 1
DBSECT	EQU	$0241	;1-byte disk boot sector count
BOOTAD	EQU	$0242	;2-byte disk boot memory address
COLDST	EQU	$0244	;1-byte coldstart flag (0 = complete)
RECLEN	EQU	$0245	;1-byte relocating loader record length
DSKTIM	EQU	$0246	;1-byte disk format timeout
PDVMSK	EQU	$0247	;1-byte parallel device selection mask
SHPDVS	EQU	$0248	;1-byte PDVS (parallel device selection) shadow
PDIMSK	EQU	$0249	;1-byte parallel device IRQ selection mask
RELADR	EQU	$024A	;2-byte relocating loader relative address
PPTMPA	EQU	$024C	;1-byte parallel device handler temporary
PPTMPX	EQU	$024D	;1-byte parallel device handler temporary

;	EQU	$024E	;6 bytes reserved for Atari

;	EQU	$0254	;23 bytes reserved for Atari

CHSALT	EQU	$026B	;1-byte character set alternate
VSFLAG	EQU	$026C	;1-byte fine vertical scroll count
KEYDIS	EQU	$026D	;1-byte keyboard disable
FINE	EQU	$026E	;1-byte fine scrolling mode
GPRIOR	EQU	$026F	;1-byte PRIOR shadow

PADDL0	EQU	$0270	;1-byte potentiometer 0
PADDL1	EQU	$0271	;1-byte potentiometer 1
PADDL2	EQU	$0272	;1-byte potentiometer 2
PADDL3	EQU	$0273	;1-byte potentiometer 3
PADDL4	EQU	$0274	;1-byte potentiometer 4
PADDL5	EQU	$0275	;1-byte potentiometer 5
PADDL6	EQU	$0276	;1-byte potentiometer 6
PADDL7	EQU	$0277	;1-byte potentiometer 7

STICK0	EQU	$0278	;1-byte joystick 0
STICK1	EQU	$0279	;1-byte joystick 1
STICK2	EQU	$027A	;1-byte joystick 2
STICK3	EQU	$027B	;1-byte joystick 3

PTRIG0	EQU	$027C	;1-byte paddle trigger 0
PTRIG1	EQU	$027D	;1-byte paddle trigger 1
PTRIG2	EQU	$027E	;1-byte paddle trigger 2
PTRIG3	EQU	$027F	;1-byte paddle trigger 3
PTRIG4	EQU	$0280	;1-byte paddle trigger 4
PTRIG5	EQU	$0281	;1-byte paddle trigger 5
PTRIG6	EQU	$0282	;1-byte paddle trigger 6
PTRIG7	EQU	$0283	;1-byte paddle trigger 7

STRIG0	EQU	$0284	;1-byte joystick trigger 0
STRIG1	EQU	$0285	;1-byte joystick trigger 1
STRIG2	EQU	$0286	;1-byte joystick trigger 2
STRIG3	EQU	$0287	;1-byte joystick trigger 3

HIBYTE	EQU	$0288	;1-byte relocating loader high byte value
WMODE	EQU	$0289	;1-byte cassette WRITE mode ($80 = writing)
BLIM	EQU	$028A	;1-byte cassette buffer limit
IMASK	EQU	$028B	;1-byte (not used)
JVECK	EQU	$028C	;2-byte jump vector or temporary
NEWADR	EQU	$028E	;2-byte relocating address
TXTROW	EQU	$0290	;1-byte split screen text cursor row
TXTCOL	EQU	$0291	;2-byte split screen text cursor column
TINDEX	EQU	$0293	;1-byte split scree text mode
TXTMSC	EQU	$0294	;2-byte split screen memory scan counter
TXTOLD	EQU	$0296	;6-byte OLDROW, OLDCOL, OLDCHR, OLDADR for text
CRETRY	EQU	$029C	;1-byte number of command frame retries
HOLD3	EQU	$029D	;1-byte temporary
SUBTMP	EQU	$029E	;1-byte temporary
HOLD2	EQU	$029F	;1-byte (not used)
DMASK	EQU	$02A0	;1-byte display (pixel location) mask
TMPLBT	EQU	$02A1	;1-byte (not used)
ESCFLG	EQU	$02A2	;1-byte escape flag ($80 = ESC detected)

** 	TABMAP	EQU	$02A3	;15-byte (120-bit) tab stop bit map

LOGMAP	EQU	$02B2	;3-byte (24-bit) logical line map
INVFLG	EQU	$02B6	;1-byte inverse video flag ($80 = inverse)
FILFLG	EQU	$02B7	;1-byte right fill flag (0 = no fill)
TMPROW	EQU	$02B8	;1-byte temporary row
TMPCOL	EQU	$02B9	;2-byte temporary column
SCRFLG	EQU	$02BB	;1-byte scroll occurence flag (0 = not occurred)
HOLD4	EQU	$02BC	;1-byte temporary
DRETRY	EQU	$02BD	;1-byte number of device retries
SHFLOK	EQU	$02BE	;1-byte shift/control lock flags
BOTSCR	EQU	$02BF	;1-byte screen bottom (24 = normal, 4 = split)

PCOLR0	EQU	$02C0	;1-byte player-missle 0 color/luminance
PCOLR1	EQU	$02C1	;1-byte player-missle 1 color/luminance
PCOLR2	EQU	$02C2	;1-byte player-missle 2 color/luminance
PCOLR3	EQU	$02C3	;1-byte player-missle 3 color/luminance

COLOR0	EQU	$02C4	;1-byte playfield 0 color/luminance
COLOR1	EQU	$02C5	;1-byte playfield 1 color/luminance
COLOR2	EQU	$02C6	;1-byte playfield 2 color/luminance
COLOR3	EQU	$02C7	;1-byte playfield 3 color/luminance

COLOR4	EQU	$02C8	;1-byte background color/luminance

PARMBL	EQU	$02C9	;6-byte relocating loader parameter block
RUNADR	EQU	$02C9	;2-byte run address
HIUSED	EQU	$02CB	;2-byte highest non-zero page address
ZHIUSE	EQU	$02CD	;2-byte highest zero page address

OLDPAR	EQU	$02CF	;6-byte relocating loader parameter block
GBYTEA	EQU	$02CF	;2-byte GET-BYTE routine address
LOADAD	EQU	$02D1	;2-byte non-zero page load address
ZLOADA	EQU	$02D3	;2-byte zero page load address

DSCTLN	EQU	$02D5	;2-byte disk sector length
ACMISR	EQU	$02D7	;2-byte ACMI interrupt service routine address
KRPDEL	EQU	$02D9	;1-byte auto-repeat delay
KEYREP	EQU	$02DA	;1-byte auto-repeat rate
NOCLIK	EQU	$02DB	;1-byte key click disable
HELPFG	EQU	$02DC	;1-byte HELP key flag (0 = no HELP)
DMASAV	EQU	$02DD	;1-byte SDMCTL save/restore
PBPNT	EQU	$02DE	;1-byte printer buffer pointer
PBUFSZ	EQU	$02DF	;1-byte printer buffer size

;	EQU	$02E0	;4 bytes reserved for DOS

RAMSIZ	EQU	$02E4	;1-byte high RAM size
MEMTOP	EQU	$02E5	;2-byte top of available user memory
MEMLO	EQU	$02E7	;2-byte bottom of available user memory
HNDLOD	EQU	$02E9	;1-byte user load flag (0 = no handler load)
DVSTAT	EQU	$02EA	;4-byte device status buffer
CBAUDL	EQU	$02EE	;1-byte low cassette baud rate
CBAUDH	EQU	$02EF	;1-byte high cassette baud rate
CRSINH	EQU	$02F0	;1-byte cursor inhibit (0 = cursor on)
KEYDEL	EQU	$02F1	;1-byte key debounce delay timer
CH1	EQU	$02F2	;1-byte prior keyboard character
CHACT	EQU	$02F3	;1-byte CHACTL shadow
CHBAS	EQU	$02F4	;1-byte CHBASE shadow

NEWROW	EQU	$02F5	;1-byte draw destination row
NEWCOL	EQU	$02F6	;2-byte draw destination column
ROWINC	EQU	$02F8	;1-byte draw row increment
COLINC	EQU	$02F9	;1-byte draw column increment

CHAR	EQU	$02FA	;1-byte internal character
ATACHR	EQU	$02FB	;1-byte ATASCII character or plot point
CH	EQU	$02FC	;1-byte keyboard code (buffer)
FILDAT	EQU	$02FD	;1-byte right fill data
DSPFLG	EQU	$02FE	;1-byte control character display flag (0 = no)
SSFLAG	EQU	$02FF	;1-byte start/stop flag (0 = not stopped)
	SPACE	4,10
**	Page Three Address Equates


DCB	EQU	$0300	;12-byte device control block
DDEVIC	EQU	$0300	;1-byte unit 1 bus ID
DUNIT	EQU	$0301	;1-byte unit number
DCOMND	EQU	$0302	;1-byte bus command
DSTATS	EQU	$0303	;1-byte command type/status return
DBUFLO	EQU	$0304	;1-byte low data buffer address
DBUFHI	EQU	$0305	;1-byte high data buffer address
DTIMLO	EQU	$0306	;1-byte timeout (seconds)
DUNUSE	EQU	$0307	;1-byte (not used)
DBYTLO	EQU	$0308	;1-byte low number of bytes to transfer
DBYTHI	EQU	$0309	;1-byte high number of bytes to transfer
DAUX1	EQU	$030A	;1-byte first command auxiliary
DAUX2	EQU	$030B	;1-byte second command auxiliary

TIMER1	EQU	$030C	;2-byte initial baud rate timer value
JMPERS	EQU	$030E	;1-byte jumper options
CASFLG	EQU	$030F	;1-byte cassette I/O flag (0 = not cassette I/O)
TIMER2	EQU	$0310	;2-byte final baud rate timer value
TEMP1	EQU	$0312	;2-byte temporary
TEMP2	EQU	$0313	;1-byte temporary
PTIMOT	EQU	$0314	;1-byte printer timeout
TEMP3	EQU	$0315	;1-byte temporary
SAVIO	EQU	$0316	;1-byte saved serial data input indicator
TIMFLG	EQU	$0317	;1-byte timeout flag (0 = timeout)
STACKP	EQU	$0318	;1-byte SIO saved stack pointer
TSTAT	EQU	$0319	;1-byte temporary status

HATABS	EQU	$031A	;35-byte handler address table

PUPBT1	EQU	$033D	;1-byte power-up validation byte 1
PUPBT2	EQU	$033E	;1-byte power-up validation byte 2
PUPBT3	EQU	$033F	;1-byte power-up validation byte 3

IOCB	EQU	$0340	;128-byte I/O control blocks area
ICHID	EQU	$0340	;1-byte handler ID ($FF = free)
ICDNO	EQU	$0341	;1-byte device number
ICCOM	EQU	$0342	;1-byte command code
ICSTA	EQU	$0343	;1-byte status of last action
ICBAL	EQU	$0344	;1-byte low buffer address
ICBAH	EQU	$0345	;1-byte high buffer address
ICPTL	EQU	$0346	;1-byte low PUT-BYTE routine address-1
ICPTH	EQU	$0347	;1-byte high PUT-BYTE routine address-1
ICBLL	EQU	$0348	;1-byte low buffer length
ICBLH	EQU	$0349	;1-byte high buffer length
ICAX1	EQU	$034A	;1-byte first auxiliary information
ICAX2	EQU	$034B	;1-byte second auxiliary information
ICSPR	EQU	$034C	;4-byte work area

PRNBUF	EQU	$03C0	;40-byte printer buffer
SUPERF	EQU	$03E8	;1-byte editor super function flag (0 = not)
CKEY	EQU	$03E9	;1-byte cassette boot request flag (0 = not)
CASSBT	EQU	$03EA	;1-byte cassette boot flag (0 = not)
CARTCK	EQU	$03EB	;1-byte cartridge equivalence checksum
DERRF	EQU	$03EC	;1-byte screen OPEN error flag (0 = not)

;	Remainder of Page Three Not Cleared upon Reset

ACMVAR	EQU	$03ED	;11 bytes reserved for ACMI
BASICF	EQU	$03F8	;1-byte BASIC switch flag (0 = BASIC enabled)
MINTLK	EQU	$03F9	;1-byte ACMI module interlock
GINTLK	EQU	$03FA	;1-byte cartridge interlock
CHLINK	EQU	$03FB	;2-byte loaded handler chain link
CASBUF	EQU	$03FD	;3-byte first 3 bytes of cassette buffer
	SPACE	4,10
**	Page Four Address Equates


;	EQU	$0400	;128-byte remainder of cassette buffer

;	Reserved for Application

USAREA	EQU	$0480	;128 bytes reserved for application
	SPACE	4,10
**	CTIA/GTIA Address Equates
CONSOL	EQU	$D01F	;console switches and speaker
	SPACE	4,10
**	PBI Address Equates


PBI	EQU	$D100	;parallel bus interface area

;	Read Addresses

PDVI	EQU	$D1FF	;parallel device IRQ status

;	Write Addresses

PDVS	EQU	$D1FF	;parallel device select
	SPACE	4,10
**	ANTIC Address Equates


ANTIC	EQU	$D400	;ANTIC area

;	Read Addresses

VCOUNT	EQU	$D40B	;vertical line counter
PENH	EQU	$D40C	;light pen horizontal position
PENV	EQU	$D40D	;light pen vertical position
NMIST	EQU	$D40F	;NMI interrupt status

;	Write Addresses

DMACTL	EQU	$D400	;DMA control
CHACTL	EQU	$D401	;character control
DLISTL	EQU	$D402	;low display list address
DLISTH	EQU	$D403	;high disply list address
HSCROL	EQU	$D404	;horizontal scroll
VSCROL	EQU	$D405	;vertical scroll
PMBASE	EQU	$D407	;player-missle base address
CHBASE	EQU	$D409	;character base address
WSYNC	EQU	$D40A	;wait for HBLANK synchronization
NMIEN	EQU	$D40E	;NMI enable
NMIRES	EQU	$D40F	;NMI interrupt status reset
	SPACE	4,10
**	PBI RAM Address Equates


PBIRAM	EQU	$D600	;parallel bus interface RAM area

	SPACE	4,10
**	Parallel Device Address Equates


PDID1	EQU	$D803	;parallel device ID 1
PDIOV	EQU	$D805	;parallel device I/O vector
PDIRQV	EQU	$D808	;parallel device IRQ vector
PDID2	EQU	$D80B	;parallel device ID 2
PDVV	EQU	$D80D	;parallel device vector table
	SPACE	4,10
**	Device Handler Vector Table Address Equates

* Redefined in 6545 Handler

EDITRV	EQU	$E400	;editor handler vector table

SCRENV	EQU	$E410	;screen handler vector table
KEYBDV	EQU	$E420	;keyboard handler vector table
PRINTV	EQU	$E430	;printer handler vector table
CASETV	EQU	$E440	;cassette handler vector table
	SPACE	4,10
**	Generic Parallel Device Handler Vector Table Address Equates


GPDVV	EQU	$E48F	;generic parallel device handler vector table
	LIST	F,-M,L,R
	SUBTTL	'6545 Handler Data Tables'
	SPACE	4,10
**	PDROM	-	Parallel Device ROM Data Table

	ORG	$D800	; This table must be at $D800.

        DW	$0000	; (Optional) ROM Checksum (low byte, high byte)

	DB	$00	; (Optional) Revision Number

	DB	$80	; (Mandatory) ID Number 1
			; Value = 80H

	DB	$00	; (Optional) Name or Type

	JMP	DHSV31	; (Mandatory) Low-level I/O Vector
			; Value = JMP address (low byte, high byte)

	JMP	DHSV31	; (Mandatory) IRQ Handler Vector
			; Value = JMP address (low byte, high byte)

	DB	$91	; (Mandatory) ID Number 2
			; Value = 91H

	DB	$00	; (Optional) Device Name
			; Value = Device Name in ASCII


; 	Device Vector Table for K:, E: and S:

; 	Each non-Initialization command will call the Device Handler
; 	Selector routine.

;	PDVV = $D80D


	DW	DHSV0-1 ; (Mandatory) Device Handler Open Vector
			; Value = address-1 (low byte, high byte)

	DW	DHSV0-1	; (Mandatory) Device Handler Close Vector
			; Value = address-1 (low byte, high byte)

	DW	DHSV0-1 ; (Mandatory) Device Handler Get-Byte Vector
			; Value = address-1 (low byte, high byte)

	DW	DHSV-1 	; (Mandatory) Device Handler Put-Byte Vector
			; Value = address-1 (low byte, high byte)

	DW	DHSV0-1 ; (Mandatory) Device Handler Status Vector
			; Value = address-1 (low byte, high byte)

	DW	DHSV0-1 ; (Mandatory) Device Handler Special Vector
			; Value = address-1 (low byte, high byte)

	JMP	PDSIN   ; (Mandatory) Initialization Vector
			; Value = JMP address (low byte, high byte)

	DW	$00	; (Optional) Not Used
			; Value = 0

	SPACE	4,10
**	CDT	-	6545 Controller Data Table
	SPACE	4,10
; 6545 Register Data Table

; Reference: Synertek SY6545 Detailed System Implementations, Page 12.

	SPACE	4,10
			; -	Address Register
			; -	Status Register
HTOT	DB	$71	; R0	Horizontal Total (N-1)
HDISP	DB	$50	; R1	Horizonatal Displayed
HSYNC	DB	$5A	; R2	Horizontal Sync Position
HVSW	DB	$0C	; R3	Horizontal and Vertical Sync Widths
VTOT	DB	$1F	; R4	Vertical Total (N-1)
VTADJ	DB	$06	; R5	Vertical Total Adjust
VDISP	DB	$19	; R6	Vertical Displayed
VSPOS	DB	$1C	; R7	Vertical Sync Position
MODEC	DB	$00	; R8	Mode Control
SCANL   DB	$07	; R9	Scan Lines (N-1)
CURS	DB	$20	; R10	Cursor Start (changed from $00). Use a software cursor.
CURE	DB	$00	; R11	Cursor End (changed from $07)
START	DW	$0000	; R12	Start (Hi byte, Lo byte)
CUR	DW	$0000	; R13	Cursor (Hi byte, Lo byte)
LPEN	DW	$0000	; R14	LPEN (Hi byte, Lo byte)
UA	DW	$0000	; R15	Update Address (Hi byte, Lo byte)
			; R31	Dummy Location

		SPACE	4,10
**	Table of Command Vector Offsets
*
TCVO	DB	0	;3 - open
	DB	0	;
	DB	4	;5 - get record
	DB	0	;
	DB	4	;7 - get byte(s)
	DB	0	;
	DB	6	;9 - put record
	DB	0	;
	DB	6	;11 - put byte(s)
	DB	2	;12 - close
	DB	8	;13 - status
	DB	10	;14 - special
           	SPACE	4,10
**	6545 Controller Board Equates

AD6545	=	$D1FA	; 6545 Address

SA6545	=	$D1FC	; 6545 Screen Ram Address Latch
			; Top 5 bits of data written into this register
			; Select the address of the 2K screen buffer
	EJECT
	SPACE	4,10
**      Device Handler Vector Tables
	SPACE	4,10
**	EDITRV - Editor Handler Vector Table

PEDTRV

	DW	EOP-1	;	perform editor OPEN
	DW	SST-1	;	perform editor CLOSE
	DW	EGB-1	;	perform editor GET-BYTE
	DW	EPB-1	;	perform editor PUT-BYTE
	DW	SST-1	;	perform editor STATUS (screen STATUS)
	DW	ESP-1	;	perform editor SPECIAL	
;	JMP	SST   	;	parallel handler return
;	DB	0	;	reserved

	EJECT
	SPACE	4,10
**********************************************************
**	DHS - Device Handler Selector Vector		**
**********************************************************
*	
*	Branches to the correct device vector table
*
*	Check the Device Type in the originating IOCB
*	 The x register contains the index to the originating IOCB.
*	 ICHID,X contains the index into HATABS
*	Branch to the corresponding handler vector
*	Set the C flag to indicate if the request was handled.
*	Entry - PPTMPA = A
*		PPTMPX = X
*	PROBLEM - BASIC CALLS PUT-BYTE DIRECTLY WITHOUT USING
*		  THE CIO INTERFACE.

;	Calculate command vector offset.
;	Note - Put-Byte has a special vector because Basic calls
;	the Put-byte routine without using CIO, and ICCOMT is not
;	initialized in that case.


DHSV    LDY	#$06		;Command Vector Offset for Put-Char ($0B) or Put-Record ($09) command.
	LDA	#'E'		;Always the editor.
	BNE	DHSV01		;always


DHSV0   LDY	ICCOMT		;NON PUT-BYTE COMMANDS
	LDA	TCVO-3,Y	;Table of vector offsets for commands
	TAY			;Table value-Offset for COMMAND VECTOR

	LDX	ICHIDZ		;Get the Handler ID (index)
	LDA	HATABS,X	;Device Name


DHSV01  LDX	#0
	STX	CRITIC		;Turn on the Vblank/IRQ K: debounce

	CMP	#'E'
	BNE	DHSV3

	LDA	#HIGH [DHSV4-1]
	PHA
	LDA	#LOW  [DHSV4-1]
	PHA

	LDA	PEDTRV+1,Y	;High routine address - 1
	PHA
	LDA	PEDTRV,Y        ;Low routine address - 1
        PHA
	JMP	DHSV5	        ;Perform Command, Return to DHSV4



;	Request not handled.
;	Return to Generic Parallel Bus Device Handler.


DHSV3	PLA			;Discard the good exit address.
	PLA

DHSV31  LDX	#1		;Turn off Keyboard debounce
	STX	CRITIC

       	CLC			;Request not handled.
	RTS     		;Return to Generic Parallel Device Handler.


;	Request handled.
;	Return to Generic Parallel Bus Device Handler.

DHSV4   LDX	#1
	STX	CRITIC

	SEC	                ;Request Handled.
	RTS 			;Return to Generic Parallel Device Handler.


;	Restore the registers and call the command procedure

DHSV5	LDA	PPTMPA   	;A contains data for put-byte
	LDX	PPTMPX          ;x contains iocb index
	LDY	#FNCNOT         ;y contains #$92 (NOT IMPLMENTED)
	RTS                     ;call the command procedure (address on stack)


	SUBTTL	'Device Handler Commands'
	SPACE	4,10
******************************************************************************************
**	PDSIN	- PARALLEL DEVICE EDITOR INITIALIZATION COMMAND	**
******************************************************************************************


;	Set PDVMSK
;	Load the 6545 device number n from the Parallel Device Shadow
;	Register and store this value in PDVMSK to indicate that
;	device number n exists on the parallel bus and to enable
;	Device handler requests or low-level IO requests.

PDSIN	LDA	SHPDVS		
	ORA	PDVMSK
	STA	PDVMSK

;	Set PDIMSK
;	Used to enable Parallel Device IRQ processing.
;	Not Applicable.



;	Search HATABS for Device Name and Insert Address of
;	the Generic Parallel Device Handler.

SERCH	LDY	#MAXDEV		;Offset to last possible entrty.

SERCH1	CMP	HATABS,Y
	BEQ	SERCH3		;If device found.

	DEY
	DEY
	DEY
	BPL	SERCH1		;Check next entry.

;	Return to the generic parallel device handler with an error condition.

SERCH2	LDY	#NONDEV
	RTS

;	Insert Generic Parallel Device Handler Address in Hatabs.

SERCH3	LDA	#LOW GPDVV	;Insert low byte.
	STA	HATABS+1,Y

	LDA	#HIGH GPDVV     ;Insert high byte.
	STA	HATABS+1,Y

	RTS


	EJECT
	SPACE	4,10
**	COC - Complete OPEN Command
*
*	ENTRY	JSR	COC
*		A = mode
*
*	LOC	6545 Handler
*

COC	=	*	;entry

;	1.0 Check mode.
;	If its not mode 0, then generate an error.

	STA	DINDEX	;save mode
;	2.0 Initialize for OPEN.

COC1
	LDA	#0
	STA	SDMCTL		;turn off DMA
	STA	DINDEX
	STA	SAVMSC
	STA	LMARGN

	LDA	#SUCCES
	STA	DSTAT		;clear status


;	Set initial tab stops.

	LDY	#$E2		;allocate 30 bytes
	STY	ADRESS		;below end of RAM
	LDY	RAMTOP		;for 240 bit
	DEY
	STY	ADRESS+1	;tab stop table

	LDY	#29		;offset to last byte of bit map
	LDA	#$01		;tab stop every 8 characters

COC3	STA	(ADRESS),Y	;set tab stop
	DEY
	BPL	COC3		;if not done


;	3.0  Allocate 6545 Screen Ram, 2K = 8 pages.

	LDA	RAMTOP		;(high) RAM size
	SEC
	SBC	#$08		;8 pages total
	STA	SAVMSC+1
	TAX

;	Load the 6545 screen address register

	STA	SA6545		;load 6545 screen address register

;	4.0 Modify the Handler database variables.

	LDA	#$FF
	STA	MEMTOP		;update top of memory

	DEX	
	STX	MEMTOP+1

;	Clear screen.

	JSR	CSC		;clear screen


;	7.0 Setup the 6545 Hardware Registers

COC19
	LDY	#$13		; Loop counter
BACK1	LDA	HTOT,Y		; Get value from table
	STY	AD6545		; 6545 Address = Register Number
	STA	AD6545+1	; 6545 Register = Data
	DEY
	BPL	BACK1

;	8.0 Exit
	
	JMP	SEC		;set exit conditions, return

	EJECT
	SPACE	4,10
************************************************
**	SGB - Perform Screen GET-BYTE COMMAND **
************************************************
*
*	ENTRY	JSR	SGB
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


SGB	=	*		;entry
	JSR	CCR		;check cursor range
	JSR	GDC		;get data under cursor
	JSR	CIA		;convert internal character to ATASCII
	JSR	SZA		;set zero data and advance cursor
	JMP	SST		;perform screen STATUS, return

	EJECT
	SPACE	4,10
**	GDC - Get Data under Cursor
*
*	ENTRY	JSR	GDC
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83
*		2. Modified 5/7/84
*

GDC	=	*		;entry
	JSR	CCA		;convert cursor row/column to address
	LDA	(ADRESS),Y

	STA	CHAR
	CMP	#0		;restore flags
	RTS			;return


	EJECT
	SPACE	4,10
**	CEL - Check End of Line
*
*	ENTRY	JSR	CEL
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


CEL	=	*		;entry
	LDA	ATACHR
	CMP	#EOL
	BNE	CEL1		;if not EOL

	JSR	RWS		;return with scrolling
	JMP	SEC		;set exit conditions, return

CEL1	JSR	PLO		;plot point
	JSR	SEA		;set EOL data and advance cursor
	JMP	SEC		;set exit conditions, return

	EJECT
	SPACE	4,10
**	PLO - Plot Point
*
*	ENTRY	JSR	PLO
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


PLO	=	*		;entry

;	Wait for start/stop flag clear.

PLO0	LDA	SSFLAG		;start/stop flag
	BNE	PLO0		;if start/stop flag non-zero, loop

;	Save cursor row/column.

	LDX	#2		;offset to last byte

PLO1	LDA	ROWCRS,X	;byte of cursor row/column
	STA	OLDROW,X	;save byte of cursor row/column
	DEX
	BPL	PLO1		;if not done

;	Convert ATASCII character to internal.

	LDA	ATACHR		;character
	TAY			;character
	ROL	A
	ROL	A
	ROL	A
	ROL	A
	AND	#3
	TAX			;index into TAIC
	TYA			;character
	AND	#$9F		;strip off column address
	ORA	TAIC,X		;or in new column address
;	JMP	SPQ		;display, return

	EJECT
	SPACE	4,10
**	SPQ - Display
*
*	ENTRY	JSR	SPQ
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83
*		2. Modified. 5/7/84


SPQ	=	*		;entry

	STA	CHAR

;	Convert cursor row/column to address.

	JSR	CCA		;convert cursor row/column to address

;	Shift up to proper position.

	LDA	CHAR

	STA	(ADRESS),Y	;update data
	RTS			;return
	
	EJECT
	SPACE	4,10
**	SEC - Set Exit Conditions
*
*	ENTRY	JSR	SEC
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


SEC	=	*		;entry
	JSR	GDC		;get data under cursor
	STA	OLDCHR

	LDX	CRSINH		;cursor inhibit flag
	BNE	SST		;if cursor inhibited

	EOR	#$80		;complement most significant bit
	JSR	SPQ		;display
;	JMP	SST		;perform screen status, return

	EJECT
	SPACE	4,10
**************************************************
**	SST - Perform Editor   STATUS COMMAND	**
**		      Editor   CLOSE  COMMAND   **
**************************************************
*
*	ENTRY	JSR	SST
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


SST	=	*

 	LDY	DSTAT		;status
SST1    LDA	#SUCCES		;indicate success
 	STA	DSTAT		;status
 	LDA	ATACHR		;data
;	JMP	ESP		;return


	EJECT
	SPACE	4,10
**************************************************
**	ESP - Perform Editor   SPECIAL COMMAND	**
**	              Keyboard SPECIAL COMMAND	**
**		      Screen   SPECIAL COMMAND  **
**************************************************
*
*	ESP does nothing.
*
*	ENTRY	JSR	ESP
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83

ESP	=	*

 	RTS			;Return.

	EJECT
	SUBTTL	'EGB - PERFORM EDITOR GET-BYTE COMMAND'
	SPACE	4,10
**************************************************
**	EGB - Perform Editor GET-BYTE COMMAND	**
**************************************************
*
*	ENTRY	JSR	EGB
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


EGB	=	*	;entry

;	Initialize.

	JSR	CCR		;check cursor range for editor
	LDA	BUFCNT		;buffer count
	BNE	EGB4		;if something in the buffer

;	Get line.

	LDA	ROWCRS		;cursor row
	STA	BUFSTR		;buffer start pointer
	LDA	COLCRS		;low cursor column
	STA	BUFSTR+1	;high buffer start pointer

EGB1	JSR	IRE		;perform keyboard GET-BYTE
	STY	DSTAT		;status
	LDA	ATACHR		;ATASCII character
	CMP	#EOL
	BEQ	EGB3		;if EOL

	JSR	PCH		;process character
	LDA	LOGCOL		;logical column
	CMP	#230		;column near column 230
	BNE	EGB2		;if not near column 230, no beep

	JSR	BEL		;beep

EGB2	JMP	EGB1		;process next character

;	Process EOL.

EGB3	JSR	ROD		;restore old data under cursor
	JSR	CBC		;compute buffer count
	LDA	BUFSTR		;buffer start pointer
	STA	ROWCRS		;cursor row
	LDA	BUFSTR+1	;high buffer start pointer
	STA	COLCRS		;low cursor column

;	Check buffer count.

EGB4	LDA	BUFCNT		;buffer count
	BEQ	EGB6		;if buffer count zero

;	Decrement and check buffer count.

EGB5	DEC	BUFCNT		;decrement buffer count
	BEQ	EGB6		;if buffer count zero

;	Check status.

	LDA	DSTAT		;status
	BMI	EGB5		;if error, continue decrementing.

;	Perfrom GET-BYTE.

	JSR	SGB		;perform screen GET-BYTE
	STA	ATACHR		;ATASCII character
	JMP	SST		;return

;	Exit.

EGB6	JSR	RWS		;return with scrolling
	LDA	#EOL
	STA	ATACHR		;ATASCII character
	JMP	SEC		;set exit conditions

	EJECT
	SPACE	4,10
**	IRA - Invoke Routine Pointed to by ADRESS
*
*	ENTRY	JSR	IRA
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


IRA	=	*		;entry
	JMP	(ADRESS)	;execute, return
	
IRE	=	*
	LDA	$E425		;Get address for Keyboard Get Byte
	PHA			;Push on stack
	LDA	$E424
	PHA
	RTS			;and execute

	EJECT
	SPACE	4,10
**************************************************
**	EPB - Perform Editor PUT-BYTE COMMAND	**
**************************************************
*
*	ENTRY	JSR	EBP
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


EPB	=	*		;entry
	STA	ATACHR		;ATASCII character
	JSR	CCR		;check cursor range for editor
	LDA	#0
	STA	SUPERF		;clear super function flag
;	JMP	PCH		;process character, return

	EJECT
	SPACE	4,10
**	PCH - Process Character
*
*	PCH displays the character or processes control characters and
*	super functions (shifted function keys).
*
*	ENTRY	JSR	PCH
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


PCH	=	*		;entry
	JSR	ROD		;restore old data under cursor
	JSR	CCC		;check for control character
	BEQ	PCH2		;if control character

;	Display character.

PCH1	ASL	ESCFLG		;escape flag
	JSR	CEL		;check EOL
	JMP	SST		;return

;	Process control character.

PCH2	LDA	DSPFLG		;display flag
	ORA	ESCFLG		;escape flag
	BNE	PCH1		;if dislay or escape, display character

;	Continue.

	ASL	ESCFLG
	INX

;	Check for super function.

	LDA	SUPERF
	BEQ	PCH3		;if not super function

;	Adjust for super function.

	TXA
	CLC
	ADC	#TSFR-TCCR-3
	TAX			;adjusted offset

;	Process control character or super function.

PCH3	LDA	TCCR,X		;low routine address
	STA	ADRESS
	LDA	TCCR+1,X	;high routine address
	STA	ADRESS+1
	JSR	IRA		;invoke routine pointed to by ADRESS
	JMP	SEC		;set exit conditions, return


	EJECT
	SUBTTL	'Editor GET-BYTE Control Character and Super Function Routines.'
	SPACE	4,10
**	ESC - Escape, Control Character Routine
*
*	ENTRY	JSR	ESC
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


ESC	=	*		;entry

 	LDA	#$80		;indicate escape detected
 	STA	ESCFLG		;escape flag
	RTS			;return
		
	EJECT
	SPACE	4,10
**	CUP - Move Cursor Up, Control Character Routine
*
*	ENTRY	JSR	CUP
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


CUP	=	*		;entry
	DEC	ROWCRS		;decrement cursor row
	BPL	CUP2		;if row positive

	LDX	#23		;screen bottom - 1

CUP1	STX	ROWCRS		;update cursor row

CUP2	JMP	SBS		;set buffer start and logical column, return

	EJECT
	SPACE	4,10
**	CDN - Move Cursor Down, Control Character Routine
*
*	ENTRY	JSR	CDN
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


CDN	=	*		;entry
	INC	ROWCRS		;increment cursor row
	LDA	ROWCRS		;cursor row
	CMP	#24		;screen bottom
	BCC	CUP2		;if at bottom, set buffer start, return

	LDX	#0
	BEQ	CUP1		;update cursor row, return
	
	EJECT
	SPACE	4,10
**	CLF - Move Cursor Left, Control Character Routine
*
*	ENTRY	JSR	CLF
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


CLF	=	*		;entry
	DEC	COLCRS		;decrement low cursor column
	LDA	COLCRS		;low cursor column
	BMI	CRM		;if negative, move cursor to margin, return

	CMP	LMARGN		;left margin
	BCS	SCC1		;if at left margin, set logical column, return

;	JMP	CRM		;move cursor to right margin, return

	EJECT
	SPACE	4,10
**	CRM - Move Cursor to Right Margin, Super Function Routine
*
*	ENTRY	JSR	CRM
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


CRM	=	*		;entry
	LDA	RMARGN		;right margin
;	JMP	SCC		;set cursor column, return

	EJECT
	SPACE	4,10
**	SCC - Set Cursor Column
*
*	ENTRY	JSR	SCC
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


SCC	=	*		;entry
	STA	COLCRS		;set low cursor column

SCC1	JMP	SLC		;set logical column, return

	EJECT
	SPACE	4,10
**	CRT - Move Cursor Right, Control Character Routine
*
*	ENTRY	JSR	CRT
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


CRT	=	*		;entry
	INC	COLCRS		;increment low cursor column
	LDA	COLCRS		;low cursor column
	CMP	RMARGN		;right margin
	BCC	SCC1		;if before right margin, process, return

	BEQ	SCC1		;if at right margin

;	JMP	CLM		;move cursor to left margin, return

	EJECT
	SPACE	4,10
**	CLM - Move Cursor to Left Margin, Super Function Routine
*
*	ENTRY	JSR	CLM
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


CLM	=	*		;entry
	LDA	LMARGN		;left margin
	JMP	SCC		;set cursor column, return

	EJECT
	SPACE	4,10
**	CSC - Clear Screen, Control Character Routine
*
*	ENTRY	JSR	CSC
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


CSC	=	*		;entry

;	Set memory scan counter address.

	JSR	SMS		;set memory scan counter address

;	Clear address.

	LDA	#00
	STA	ADRESS
	LDY	#128		;don't erase tab stop table
	LDX	RAMTOP		;get top of RAM

CSC1	DEX			;less 1 page
	STX	ADRESS+1

CSC11	DEY			;the backwards approach
	STA	(ADRESS),Y
	BNE	CSC11           ;256 byte loop
	CPX	SAVMSC+1	;work down to bottom of screen RAM
	BNE	CSC1		;if not done with 2K-128

;	Clean up logical line bit map.

	LDY	#03		;set all bits to 1
	LDA	#$FF

CSC2	STA	LOGMAP,Y	;in all 4 bytes (3 are used)
	DEY
	BPL	CSC2		;if not done

;	Exit.

;	JMP	CHM		;move cursor home, return

	EJECT
	SPACE	4,10
**	CHM - Move Cursor Home, Super Function Routine
*
*	ENTRY	JSR	CHM
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83
*		2. Modified. 5/7/84


CHM	=	*		;entry

	JSR	SCL		;set cursor at left edge
	STA	LOGCOL		;logical column
	STA	BUFSTR+1	;high buffer start
	LDA	#0
	STA	ROWCRS		;cursor row
	STA	BUFSTR		;low buffer start pointer
	RTS			;return
	EJECT
	SPACE	4,10
**	BSP - Backspace, Control Character Routine
*
*	ENTRY	JSR	BSP
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


BSP	=	*		;entry
	LDA	LOGCOL		;logical column
	CMP	LMARGN		;left margin
	BEQ	BSP3		;if at left margin

	LDA	COLCRS		;low cursor column
	CMP	LMARGN		;left margin
	BNE	BSP1		;if not at left margin

	JSR	DWQ		;see if line should be deleted

BSP1	JSR	CLF		;move cursor left
	LDA	COLCRS		;low cursor column
	CMP	RMARGN		;right margin
	BNE	BSP2		;if not at right margin

	LDA	ROWCRS		;cursor row
	BEQ	BSP2		;if row zero

	JSR	CUP		;move cursor up

BSP2	LDA	#' '
	STA	ATACHR		;ATASCII character
	JSR	PLO		;plot point

BSP3	JMP	SLC		;set logical column, return

	EJECT
	SPACE	4,10
**	TAB - Tab, Control Character Routine
*
*	ENTRY	JSR	TAB
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


TAB	=	*		;entry

TAB1	JSR	CRT		;move cursor right
	LDA	COLCRS		;low cursor column
	CMP	LMARGN		;left margin
	BNE	TAB2		;if not at left margin

	JSR	RET		;return
	JSR	BLG		;get bit from logical line bit map
	BCS	TAB3		;if end of logical line

;	Check for tab stop.

TAB2	JSR	BMIT		;get tab table entry
	LDA	(ADRESS),Y	;for this column
	AND	BITMSK		;check for tab stop
	BEQ	TAB1		;don't stop here

;	Set logical column.

TAB3	JMP	SLC		;set logical column, return

	EJECT
	SPACE	4,10
**	STB - Set Tab, Control Character Routine
*
*	ENTRY	JSR	STB
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


STB	=	*		;entry
	JSR	BMIT		;get address of tab table
	LDA	(ADRESS),Y	;get proper byte
	ORA	BITMSK		;set corresponding bit
	STA	(ADRESS),Y
	RTS
	EJECT
	SPACE	4,10
**	CTB - Clear Tab, Control Character Routine
*
*	ENTRY	JSR	CTB
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


CTB	=	*	;entry
	JSR	BMIT		;get address of tab table
	LDA	BITMSK
	EOR	#$FF		;generate clear mask
	AND	(ADRESS),Y
	STA	(ADRESS),Y	;update table
	RTS
	EJECT
	SPACE	4,10
**	ICH - Insert Character, Control Character Routine
*
*	ENTRY	JSR	ICH
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


ICH	=	*		;entry
	JSR	SRC		;save row and column
	JSR	GDC		;get data under cursor
	STA	INSDAT
	LDA	#0
	STA	SCRFLG

ICH1	JSR	SPQ		;store data
	LDA	LOGCOL		;logical column
	PHA			;save logical column
	JSR	ACC		;advance cursor column
	PLA			;saved logical column
	CMP	LOGCOL		;logical column
	BCS	ICH2		;if saved logical column >= logical column, exit

	LDA	INSDAT
	PHA
	JSR	GDC		;get data under cursor
	STA	INSDAT
	PLA
	JMP	ICH1		;continue

;	Exit.

ICH2	JSR	RRC		;restore row and column

ICH3	DEC	SCRFLG
	BMI	ICH4		;if scroll occurred

	DEC	ROWCRS		;decrement cursor row
	BNE	ICH3		;continue

ICH4	JMP	SLC		;set logical column, return

	EJECT
	SPACE	4,10
**	DCH - Delete Character, Control Character Routine
*
*	ENTRY	JSR	DCH
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


DCH	=	*		;entry

;	Save row and column.

	JSR	SRC		;save row and column

;	Get data to the right of cursor.

DCH1	JSR	CCA		;convert cursor row/column to address
	LDA	ADRESS
	STA	SAVADR		;save address
	LDA	ADRESS+1
	STA	SAVADR+1
	LDA	LOGCOL		;logical column
	PHA			;save logical column
	JSR	SZA		;set zero data and advance cursor
	PLA			;saved logical column
	CMP	LOGCOL		;logical column
	BCS	DCH2		;if saved logical column >= logical column, exit

	LDA	ROWCRS		;cursor row
	CMP	#24		;screen bottom
	BCS	DCH2		;if row off screen, exit

	JSR	GDC		;get data under cursor
	LDY	#0
	STA	(SAVADR),Y	;put data in previous position
	BEQ	DCH1		;continue

DCH2	LDY	#0
	TYA
	STA	(SAVADR),Y	;clear last position
	JSR	DQQ		;try to delete a line
	JSR	RRC		;restore row and column
	JMP	SLC		;set logical column, return

	EJECT
	SPACE	4,10
**	ILN - Insert Line, Control Character Routine
*
*	ENTRY	JSR	ILN
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


ILN	=	*		;entry
	SEC
;	JMP	ILN1

	EJECT
	SPACE	4,10
**	ILN1 - Insert Line
*
*	ENTRY	JSR	ILN1
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


ILN1	=	*		;entry
	JSR	ELL		;extend logical line
	LDA	LMARGN		;left margin
	STA	COLCRS		;low cursor column
	JSR	CCA		;convert cursor row/column to address
	JSR	MLN		;move line
	JSR	CLN		;clear current line
	JMP	SLC		;set logical column, return

	EJECT
	SPACE	4,10
**	DLN - Delete Line, Control Character Routine
*
*	ENTRY	JSR	DLN
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


DLN	=	*		;entry
	JSR	SLC		;set logical column
	LDY	HOLD1
	STY	ROWCRS		;cursor row
;	JMP	DLN1

	EJECT
	SPACE	4,10
**	DLN1 - Delete Line
*
*	ENTRY	JSR	DLN1
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


DLN1	=	*		;entry

	LDX	ROWCRS		;cursor row

DLN2	INX
	TXA
	JSR	BLGX		;get next bit
	DEX
	TXA
	JSR	BMP		;update current bit
	INX
	CPX	#24
	BNE	DLN2		;if not done

	LDA	LOGMAP+2
	ORA	#1		;set least significant bit
	STA	LOGMAP+2	;update logical line bit map
	LDA	#0		;delete line of data
	STA	COLCRS		;low cursor column
	JSR	CCA		;convert cursor row/column to address
	JSR	SSD		;scroll screen for delete

;	Check for new logical line.

	JSR	BLG		;get bit from logical line bit map
	BCC	DLN1		;if not new logical line

;	Move cursor to left margin.

	JMP	CLM		;move cursor to left margin, return

	EJECT
	SPACE	4,10
**	BEL - Sound Bell, Control Character Routine
*
*	ENTRY	JSR	BEL
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


BEL	=	*		;entry
	LDY	#$20

BEL1	JSR	SKC		;sound key click
	DEY
	BPL	BEL1		;if not done

	RTS			;return

	EJECT
	SPACE	4,10
**	CBT - Move Cursor to Bottom, Super Function Routine
*
*	ENTRY	JSR	CBT
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


CBT	=	*		;entry
	JSR	CHM		;move cursor home
	JMP	CUP		;move cursor up, return

	EJECT
	SPACE	4,10
**	CCA - Convert Cursor Row/Column to Address
*
*	ENTRY	JSR	CCA
*
*	MODS
*		L. Winner	06/01/82
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83
*		2. Modified 5/7/84


CCA	=	*		;entry

;  	Addr = Savmsc + (2 * 2 * Rowcrs + Rowcrs) * 2 * 2 * 2 * 2 + Colcrs

	LDX	#$00
	STX	ADRESS+1	;clear high address
	LDA	ROWCRS		;cursor row position

;	Calculate (2 * 2 * Rowcrs + Rowcrs).

	ASL	A		;2 times row position
	ROL	ADRESS+1

	ASL	A		;4 time row position
	ROL	ADRESS+1

	ADC	ROWCRS		;add to get 5 times row position
	STA	ADRESS
	BCC	CCA1
	INC	ADRESS+1

;	Calculate (Address * 2 * 2 * 2 * 2).

CCA1	LDX	#$04  		;left shift count for 80 columns
CCA2	ASL	ADRESS		;ADRESS = ADRESS * 2
	ROL	ADRESS+1	
	DEX
	BNE	CCA2

;	Calculate (Address + Colcrs).

	CLC
	LDA	COLCRS		;low cursor column
	ADC	ADRESS		;add address
	BCC	CCA5		;if no carry

	INC	ADRESS+1	;adjust high address

;	Calculate (Address + Savmsc).

CCA5	CLC
	ADC	SAVMSC		;add saved memory scan counter
	STA	ADRESS		;update address
	STA	OLDADR		;save address

	LDA	ADRESS+1
	ADC	SAVMSC+1
	STA	ADRESS+1
	STA	OLDADR+1

	LDY	#0

CCA6	RTS			;return
	EJECT
	SPACE	4,10
**	SZA - Set Zero Data and Advance Cursor Column
*
*	ENTRY	JSR	SZA
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


SZA	=	*		;entry
	LDA	#0
	BEQ	SDA		;set data and advance cursor

**	SEA - Set EOL Data and Advance Cursor Column
*
*	ENTRY	JSR	SEA
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


SEA	=	*		;entry
	LDA	#EOL		;special case eliminator
;	JMP	SDA		;set data and advance cursor, return

**	SDA - Set Data and Advance Cusor Column
*
*	ENTRY	JSR	SDA
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


SDA	=	*		;entry
	STA	INSDAT		;set data
;	JMP	ACC		;advance cursor column, return

	EJECT
	SPACE	4,10
**	ACC - Advance Cursor Column
*
*	ENTRY	JSR	ACC
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83
*		2. Modified. 5/8/84


ACC	=	*		;entry

	INC	LOGCOL		;increment logical column
	INC	COLCRS		;increment low cursor column

	LDA	COLCRS		;low cursor column
	LDX	DINDEX		;mode

	CPX	#0		;if not mode 0, exit.
	BNE	CCA6		;CCA6 is an RTS.

	CMP	#80     	;Mode column count is always 80
	BEQ	ACC2		;if equal, process EOL

	CMP	RMARGN		;right margin
	BEQ	CCA6		;if at right margin, exit
	BCC	CCA6		;if before right margin, exit
ACC2	LDA	LOGCOL
	CMP	#161
	BCC	ACC4
	LDA	INSDAT
	BEQ	RET
	JSR	RWS
	JMP	SLC
ACC4	JSR	RET
	JSR	BLG
	BCC	ACC5
	LDA	INSDAT
	BEQ	ACC5
	CLC
	JSR	ILN1

ACC5	JMP	SLC		;exit


	EJECT
	SPACE	4,10
**	RWS - Return with Scrolling
*
*	ENTRY	JSR	RWS
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


RWS	=	*		;entry
	LDA	#EOL		;select scrolling
	STA	INSDAT
;	JMP	RET		;return

	EJECT
	SPACE	4,10
**	RET - Return
*
*	ENTRY	JSR	RET
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83
*		2. Modified. 5/7/84

RET	=	*		;entry
	JSR	SCL		;set cursor at left edge
	INC	ROWCRS		;increment cursor row
	LDX	DINDEX
	LDY	#24		;assume 24 lines

	TYA	     		;mode row count
     	CMP	ROWCRS		;cursor row
	BNE	RET5

	STY	HOLD3
	TXA			;mode
	BNE	RET5		;if mode not 0, do not scroll

	LDA	INSDAT
	BEQ	RET5		;if zero, do not scroll

;	If EOL, roll in a 0.

	CMP	#EOL		;to extend bottom logical line
	BEQ	RET3		;if EOL

	CLC

RET3	JSR	SCR
	INC	SCRFLG
	DEC	BUFSTR
	BPL	RET4

	INC	BUFSTR

RET4	DEC	HOLD3
	LDA	LOGMAP
	SEC			;indicate for partial line
	BPL	RET3		;if partial logical line

	LDA	HOLD3
	STA	ROWCRS		;cursor row

RET5	JMP	SLC		;set logical column, return

	EJECT
	SPACE	4,10
**	CCR - Check Cursor Range
*
*	ENTRY	JSR	CCR
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


CCR	=	*		;entry
	LDA	#79
	CMP	RMARGN		;right margin
	BCS	CCR1		;if 79 >= right margin
	STA	RMARGN		;set right margin

CCR1	LDA	ROWCRS		;cursor row
	CMP	#24		;maximum row
	BCS	CCR5		;if row >= max then error

	LDA	COLCRS	  	;cursor column
	CMP	#80		;maximum column
	BCS	CCR5		;if count > column position, error

	LDA	#SUCCES		;success indicator
	STA	DSTAT		;indicate success
	LDA	#BRKABT		;assume BREAK abort
	LDX	BRKKEY		;BREAK key status
	STA	BRKKEY		;clear BREAK key status
	BEQ	CCR6		;if BREAK

	RTS			;return

;	Process range error.

CCR5	JSR	CHM		;move cursor home
	LDA	#CRSROR		;indicate cursor overrange

;	Exit.

CCR6	STA	DSTAT		;status
	PLA			;clean stack for return to CIO
	PLA

	JMP	SST		;return (to CIO)

	EJECT
	SPACE	4,10
**	ROD - Restore Old Data under Cursor
*
*	ENTRY	JSR	ROD
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


ROD	=	*		;entry
 	LDY	#0
 	LDA	OLDADR+1
 	BEQ	ROD1		;if page zero

 	LDA	OLDCHR		;old data
 	STA	(OLDADR),Y

ROD1	RTS			;return

	EJECT
	SPACE	4,10
**	BIT MAP ROUTINES         **


BMIT	=	*		;entry

	LDA	LOGCOL
	LDY	#$E2		;get address of tab table
	STY	ADRESS
	LDY	RAMTOP
	DEY
	STY	ADRESS+1

BMIL	PHP			;entry for line table lookup
	PHA			;save for divide
	AND	#07		;logical column modulo 8
	TAY			;offset to bit mask
	LDA	TBTM,X		;get bit mask
	STA	BITMSK		;and save

	PLA			;get old value
	LSR	A               ;divide by 8
	LSR	A
	LSR	A
	TAY			;and save byte offset
	PLP			;restore carry status
	RTS			;return


**	Put Bit

BMP	JSR	BMIL		;get table offset and bitmask
	LDA	BITMSK		;initialize for bit mask operation
	BCC	BMC		;go clear bit

	ORA	LOGMAP,Y	;set bit
	STA	LOGMAP,Y	;update bit map
	RTS			;return


**	Clear Bit

BMC	EOR	#$FF		;compliment mask
	AND	LOGMAP,Y	;clear bit
	STA	LOGMAP,Y	;update bit map
	RTS			;return


**	Get Bit

BLG	LDA	ROWCRS		;cursor row
	
BLGX	JSR	BMIL		;initialize for bit mask operation

	CLC
	LDA	LOGMAP,Y
	AND	BITMSK		;get bit status
	BEQ	BMG1		;bit was clear
       	SEC			;bit was set
BMG1	RTS			;return


	EJECT
	SPACE	4,10
**	CIA - Convert Internal Character to ATASCII
*
*	ENTRY	JSR	CIA
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83
*		2. 6545
*		   R. L. Blotcky 05/08/84


CIA	=	*		;entry

;	Initialize.

	LDA	CHAR

;	Convert internal character to ATASCII.

	ROL	A
	ROL	A
	ROL	A
	ROL	A
	AND	#3
	TAX
	LDA	CHAR		;character
	AND	#$9F		;strip off cloumn address
	ORA	TIAC,X		;or in new column address

	STA	ATACHR	;ATASCII character

CIA3	RTS			;return

	EJECT
	SPACE	4,10
**	MLN - Move Line
*
*	ENTRY	JSR	MLN
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


MLN	=	*		;entry

;	Initialize for 80 character lines.

	LDX	RAMTOP		;(high) RAM size
	DEX			;decrement (high) RAM size
	STX	TOADR+1		;high destination address
	DEX			;128+80+80
	STX	FRMADR+1	;high source address

	LDA	#low [$0000-32]	;low RAM size - 288
	STA	FRMADR		;low source address
	LDA	#low [$0000-208]	;low RAM size - 160
	STA	TOADR		;low destination address

	LDX	ROWCRS		;cursor row

;	Check for completion.

MLN1	INX
	CPX	#24		;screen bottom
	BEQ	CIA3		;if done, return

;	Move line of 80 characters.

	LDY	#79		;offset to last byte
MLN2	LDA	(FRMADR),Y	;byte of source
	STA	(TOADR),Y	;byte of destination
	DEY
	BPL	MLN2		;if not done

;	Adjust source and destination addresses.

	SEC
	LDA	FRMADR		;source address
	STA	TOADR		;update destination address
	SBC	#low 80		;subtract 80
	STA	FRMADR		;update source address

	LDA	FRMADR+1
	STA	TOADR+1
	SBC	#high 80
	STA	FRMADR+1

	JMP	MLN1		;continue

	EJECT
	SPACE	4,10
**	ELL - Extend Logical Line
*
*	ENTRY	JSR	ELL
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


ELL	=	*		;entry
	PHP			;save bit
	LDX	#22		;if row=23 the saved bit will fix it

ELL1	TXA
	JSR	BLGX		;get current bit
	INX			;next position (row)
	TXA
	JSR	BMP		;put bit in next place in bit map
	DEX
	DEX
	BMI	ELL2		;for row=0

	CPY	ROWCRS		;cursor row
	BCS	ELL1

ELL2	LDA	ROWCRS		;cursor row
	PLP			;restore current line status
	JMP	BMP		;put bit in bit map, return

	EJECT
	SPACE	4,10
**	CLN - Clear Line
*
*	ENTRY	JSR	CLN
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


CLN	=	*		;entry
	LDA	LMARGN		;left margin
	STA	COLCRS		;low cursor column
	JSR	CCA		;convert cursor row/column to address
	SEC
	LDA	RMARGN		;right margin
	SBC	LMARGN		;subtract left margin
	TAY			;screen width
	LDA	#0

CLN1	STA	(ADRESS),Y
	DEY
	BPL	CLN1		;if not done

	RTS			;return

	EJECT
	SPACE	4,10
**	SCR - Scroll
*
*	ENTRY	JSR	SCR
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


SCR	=	*	;entry

;	Initialize.

	ROL	LOGMAP+2	;scroll line table "up"
	ROL	LOGMAP+1
	ROL	LOGMAP

	JSR	SMS		;set memory scan counter address
;	JMP	SSD		;scroll screen for delete, return

	EJECT
	SPACE	4,10
**	SSD - Scroll Screen for Delete
*
*	ENTRY	JSR	SSD
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


SSD	=	*		;entry

;	Initialize.

	LDA	#128
	LDX	ADRESS+1        ;high address

;	Calculate number of bytes to move.

SSD1	INX   			;high address
	CPX	RAMTOP
	BEQ	SSD2		;if at RAMTOP

	SEC
	SBC	#$10
	JMP	SSD1		;continue

SSD2	ADC	#79		;(CLC and ADC #80)
	BNE	SSD3		;if byte count non-zero

	LDX	ADRESS+1
	INX
	CPX	RAMTOP
	BEQ	SSD6		;if at RAMTOP

	CLC
	ADC	#$10

;	Adjust address.

SSD3	TAY			;number of bytes
	STA	COUNTR
	SEC
	LDA	ADRESS
	SBC	COUNTR		;subtract
	STA	ADRESS		;update low address
	BCS	SSD4		;if no borrow

	DEC	ADRESS+1	;adjust high address

;	Move data down.

SSD4	LDA	ADRESS
	CLC
	ADC	#80
	STA	COUNTR		;address + 80
	LDA	ADRESS+1
	ADC	#0
	STA	COUNTR+1

SSD5	LDA	(COUNTR),Y	;byte to move
	STA	(ADRESS),Y	;move byte
	INY
	BNE	SSD5		;if not done (256-16 times)

	LDY	#256-240
	LDA	ADRESS
	CMP	#$30            ;80 columns
	BEQ	SSD6		;if all done

	CLC
	ADC	#240
	STA	ADRESS		;update low address
	BCC	SSD4		;if no carry

	INC	ADRESS+1	;adjust high address
	BNE	SSD4		;continue

;	Clear last line.

SSD6	LDX	RAMTOP
	DEX
	STX	COUNTR+1
	LDX	#$30
	STX	COUNTR
	LDA	#0
	LDY	#79

SSD7	STA	(COUNTR),Y	;clear byte of last line
	DEY
	BPL	SSD7		;if not done

;	JMP	SLC		;set logical column, return

	EJECT
	SPACE	4,10
**	SLC - Set Logical Column
*
*	ENTRY	JSR	SLC
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


SLC	=	*		;entry

;	Initialize.

	LDA	#0
	STA	LOGCOL		;initialize logical column
	LDA	ROWCRS		;cursor row
	STA	HOLD1		;working row

;	Search for beginning of line.

SLC1	LDA	HOLD1		;add in row component
	JSR	BLGX
	BCS	SLC2		;if beginning of line found

	LDA	LOGCOL		;logical column
	CLC
	ADC	#80		;add number of characters per line
	STA	LOGCOL		;update logical column
	DEC	HOLD1		;decrement working row
	JMP	SLC1		;continue

;	Add in cursor column.

SLC2	CLC
	LDA	LOGCOL		;logical column
	ADC	COLCRS		;add low cursor cloumn
	STA	LOGCOL		;update logical column
	RTS			;return
	
	EJECT
	SPACE	4,10
**	CBC - Compute Buffer Count
*
*	CBC computes the buffer count as the number of bytes from the
*	buffer start to the end of the logical line (with trailing
*	spaces removed).
*
*	ENTRY	JSR	CBC
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83
*		2. Modified. 5/7/84

CBC	=	*	;entry

;	Initialize.

	JSR	SRC		;save row and column
	LDA	LOGCOL		;logical column
	PHA			;save logical column
	LDA	BUFSTR		;start of buffer
	STA	ROWCRS		;cursor row
	LDA	BUFSTR+1
	STA	COLCRS		;low cursor column
	LDA	#1
	STA	BUFCNT		;initialize buffer count

;	Determine last line on screen.

CBC1	LDX	#23		;normal last line on screen

;	Check for cursor on last line of screen.

	CPX	ROWCRS		;cursor row
	BNE	CBC3		;if cursor on last line

	LDA	COLCRS		;low cursor column
	CMP	RMARGN		;right margin
	BNE	CBC3		;if not at right margin

	INC	BUFCNT		;fake SEA to avoid scrolling
	JMP	CBC4

CBC3	JSR	SZA		;set zero data and advance cursor
	INC	BUFCNT
	LDA	LOGCOL		;logical column
	CMP	LMARGN		;left margin
	BNE	CBC1		;if not yet at left margin

	DEC	ROWCRS		;decrement cursor row
	JSR	CLF		;move cursor left

CBC4	JSR	GDC		;get data under cursor
	BNE	CBC6		;if non-zero, quit

	DEC	BUFCNT		;decrement counter
	LDA	LOGCOL		;logical column
	CMP	LMARGN		;left margin
	BEQ	CBC6		;if beginning of logical line, exit

	JSR	CLF		;move cursor left
	LDA	COLCRS		;low cursor column
	CMP	RMARGN		;right margin
	BNE	CBC5		;if cursor column not right margin

	DEC	ROWCRS		;decrement cursor row

CBC5	LDA	BUFCNT
	BNE	CBC4		;if BUFCNT non-zero, continue

CBC6	PLA			;saved logical column
	STA	LOGCOL		;restore logical column
	JMP	RRC		;restore row and column, return

	EJECT
	SPACE	4,10
**	SBS - Set Buffer Start and Logical Column
*
*	ENTRY	JSR	SBS
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


SBS	=	*		;entry
	JSR	SLC		;set logical column
	LDA	HOLD1
	STA	BUFSTR
	LDA	LMARGN		;left margin
	STA	BUFSTR+1

SBS1	RTS			;return

	EJECT
	SPACE	4,10
**	DQQ - Delete Line
*
*	ENTRY	JSR	DQQ
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


DQQ	=	*		;entry
	LDA	LOGCOL		;logical column
	CMP	LMARGN		;left margin
	BNE	DQQ1		;if not at left margin

	DEC	ROWCRS		;decrement cursor row

DQQ1	JSR	SLC		;set logical column
;	JMP	DWQ

	EJECT
	SPACE	4,10
**	DWQ - Delete Line
*
*	ENTRY	JSR	DWQ
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


DWQ	=	*		;entry

;	Check for left margin.

	LDA	LOGCOL		;logical column
	CMP	LMARGN		;left margin
	BEQ	SBS1		;if at left margin, return

	JSR	CCA		;convert cursor row/column to address
	LDA	RMARGN		;right margin
	SEC
	SBC	LMARGN		;subtract left margin
	TAY			;offset to last byte

DWQ1	LDA	(ADRESS),Y
	BNE	SBS1

	DEY
	BPL	DWQ1		;if not done

	JMP	DLN1		;delete line, return

	EJECT
	SPACE	4,10
**	CCC - Check for Control Character
*
*	ENTRY	JSR	CCC
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


CCC	=	*      		;os entry point

 	LDX	#TCCRL-3	;offset to last entry

CCC1	LDA	TCCR,X		;control character
 	CMP	ATACHR		;ATASCII character
 	BEQ	CCC2		;if character found, exit

 	DEX
 	DEX
 	DEX
 	BPL	CCC1		;if not done, continue search

CCC2	RTS			;return
	

	EJECT
	SPACE	4,10
**	SRC - Save Row and Column
*
*	ENTRY	JSR	SRC
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83

SRC	=	*		;entry

 	LDX	#2		;offset to last byte

SRC1	LDA	ROWCRS,X	;byte of cursor row/column
 	STA	TMPROW,X	;save byte of cursor row/column
 	DEX
 	BPL	SRC1		;if not done

 	RTS			;return

	EJECT
	SPACE	4,10
**	RRC - Restore Row and Column
*
*	ENTRY	JSR	RRC
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


RRC	=	*		;entry

 	LDX	#2		;offset to last byte

RRC1	LDA	TMPROW,X	;byte of saved cursor row/column
 	STA	ROWCRS,X	;byte of cursor row/column
 	DEX
 	BPL	RRC1		;if not done

 	RTS			;return

	EJECT
	SPACE	4,10
**	SKC - Sound Key Click
*
*	ENTRY	JSR	SKC
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


SKC	=	*

;	Initialize.

 	LDX	#2*63		;2 times trip count
 	PHA			;save A

;	Turn loudspeaker on.

SKC1	STX	CONSOL

;	Wait for VBLANK (loudspeaker off).

 	LDA	VCOUNT		;vertical line counter

SKC2	CMP	VCOUNT		;current vertical line counter
 	BEQ	SKC2		;if vertical line not changed

;	Decrement and check trip count.

 	DEX
 	DEX
 	BPL	SKC1		;if not done

;	Exit.

 	PLA			;restore A
 	RTS			;return

	EJECT
	SPACE	4,10
**	SCL - Set Cursor at Left Edge
*
*	ENTRY	JSR	SCL
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83
*		2. Modified. 5/7/84

SCL	=	*		;entry

;	Set Colcrs to Lmargn.

     	LDA	LMARGN		;use left margin
     	STA	COLCRS		;set low cursor column
	RTS			;return

	EJECT
	SPACE	4,10
**	SMS - Set Memory Scan Counter Address
*
*	ENTRY	JSR	SMS
*
*	MODS
*		Original Author Unknown
*		1. Bring closer to Coding Standard (object unchanged).
*		   R. K. Nordin	11/01/83


SMS	=	*		;entry
 	LDA	SAVMSC		;saved low memory scan counter
 	STA	ADRESS		;set low address
 	LDA	SAVMSC+1	;saved high memory scan counter
 	STA	ADRESS+1	;set high address
 	RTS			;return
	EJECT
	SUBTTL	'TABLES'
	SPACE	4,10
**	TCCR - Table of Control Character Routines
*
*	Each entry is 3 bytes.  The first byte is the control
*	character; the second and third bytes are the address of
*	the routine which processes the control character.


TCCR	DB	$1B
	DW	ESC		;escape

	DB	$1C
	DW	CUP		;move cursor up

	DB	$1D
	DW	CDN		;move cursor down

	DB	$1E
	DW	CLF		;move cursor left

	DB	$1F
	DW	CRT		;move  cursor right

	DB	$7D
	DW	CSC		;clear screen

	DB	$7E
	DW	BSP		;backspace

	DB	$7F
	DW	TAB		;tab

	DB	$9B
	DW	RWS		;return with scrolling

	DB	$9C
	DW	DLN		;delete line

	DB	$9D
	DW	ILN		;insert line

	DB	$9E
	DW	CTB		;clear tab

	DB	$9F
	DW	STB		;set tab

	DB	$FD
	DW	BEL		;sound bell

	DB	$FE
	DW	DCH		;delete character

	DB	$FF
	DW	ICH		;insert character

TCCRL	=	*-TCCR		;length
	SPACE	4,10
**	TSFR - Table of Super Function (Shifted Function Key) Routines
*
*	Each entry is 3 bytes.  The first byte is the super function
*	character; the second and third bytes are the address of the
*	routine which processes the super function.


TSFR	DB	$1C
	DW	CHM		;move cursor home

	DB	$1D
	DW	CBT		;move cursor to bottom

	DB	$1E
	DW	CLM		;move cursor to left margin

	DB	$1F
	DW	CRM		;move cursor to right margin
	SPACE	4,10
**	TAIC - Table of ATASCII to Internal Conversion Constants

TAIC	DB	$40
	DB	$00
	DB	$20
	DB	$60
	SPACE	4,10
**	TIAC - Table of Internal to ATASCII Conversion Constants

TIAC	DB	$20
	DB	$40
	DB	$00
	DB	$60
	SPACE	4,10
**	TBTM - Table of Bit Masks

TBTM	DB	$80
	DB	$40
	DB	$20
	DB	$10
	DB	$08
	DB	$04
	DB	$02
	DB	$01
