;
;------------
;DISASSEMBLER
;------------
DISA	CLD  	;no decimal.
	LDA DOSINI 	;save DOS INIT
	STA MYINIT+1
	LDA DOSINI+1
	STA MYINIT+2
	LDA VKEYBD 	;copy keybrd adr
	STA TKEY 	;to hold
	LDA VKEYBD+1
	STA TKEY+1
	LDA VBRK 	;copy VBRK vec
	STA BRKS 	;to hold area
	LDA VBRK+1
	STA BRKS+1
	LDA KEYDEF 	;test 800 or XL?
	ORA KEYDEF+1
	BEQ REINIT 	;have 800
	LDA KEYDEF 	;copy keycode to
	STA K1+1 	;ascii table adr
	LDA KEYDEF+1 	;to my program
	STA K1+2
;
REINIT	CLD 
	LDX #0 	;get 0
	STX EXEC 	;no run.
	STX COLDST
	STX NCHK 	;check keys
	STX NMIEN 	;no interupts.
	STX DTYPE 	;display type
	INX 
	STX IPUT
	STX BBCK
	TSX  	;get stk pntr
	STX USRS 	;save.
	LDA #$E0 	;restore char
	STA CHBAS 	;set
	LDA #$A0 	;set RAMTOP on
	STA RAMTOP 	;my program
	SEI 
	LDA # <BRKER 	;point BRK hand
	STA VBRK 	;to my routine.
	LDA # >BRKER
	STA VBRK+1
	LDA # <MKEYV 	;replace keybrd
	STA VKEYBD 	;interrupt with
	LDA # >MKEYV 	;mine
	STA VKEYBD+1
	LDA # <MYINIT 	;point RESET
	STA DOSINI 	;vec to my prog.
	LDA # >MYINIT
	STA DOSINI+1
	CLI 
	LDX #0
C123	LDA ZPAGE,X 	;now copy the
	STA MYZPG,X 	;first 3 pages
	LDA STACK,X 	;of RAM to my
	STA MYPG1,X 	;buffers.
	LDA PAGE2,X 	;(For TRACER)
	STA MYPG2,X
	INX 
	BNE C123
	JSR OPSCR 	;open screen
	LDA USRPC 	;now disa from
	STA FR0 	;the users PC
	LDA USRPC+1
	STA FR0+1
	JSR TOEDEV
	LDA #$40 	;interupts on.
	STA NMIEN
	JSR FREBRK 	;sho BRK points
	JSR DOREGS 	;and registers.
;
;Wait here for command
;
GETCOM	JSR CLRWIN 	;clear com
	JSR CLRCMD 	;and prompt
	JSR CLRKEY
	JSR SETKEY 	;set keys
	JSR GETKEY 	;get a key
	AND #$7F 	;no inverse
	LDX #17 	;scan 18 coms
GC2	CMP CMDTXT,X 	;is it a com?
	BEQ HAVCOM 	;yes.
	DEX  	;no.
	BPL GC2 	;18 coms yet?
	JSR GOTERR 	;sorry error.
	JMP GETCOM 	;go again.
HAVCOM	TXA 
	ASL A 	;mult * 2
	TAX 
	LDA CMDADR,X 	;get command
	STA CMDJSR+1 	;address from
	LDA CMDADR+1,X 	;the table
	STA CMDJSR+2 	;put in JSR
	JSR CLRERR 	;erase error.
	JSR ERASTAT 	;and status.
	JSR SETKEY
CMDJSR	JSR $FFFF 	;go command!
	JMP GETCOM 	;get next one.
;
;This is the go DOS command
;
DOSCMD	LDA #7 	;kill VBLANK
	LDY # <XITVBV
	LDX # >XITVBV
	JSR SETVBV
	LDA BRKS 	;restore the
	STA VBRK 	;BRK vector.
	LDA BRKS+1
	STA VBRK+1
	LDA MYINIT+1 	;restore RESET
	STA DOSINI 	;vector
	LDA MYINIT+2
	STA DOSINI+1
	SEI 
	LDA TKEY 	;restore key-
	STA VKEYBD 	;board interrupt
	LDA TKEY+1
	STA VKEYBD+1
	CLI 
	PLA  	;pull off RTS
	PLA 
	JMP (DOSVEC) 	;go DOS
;
;Re-init prog after RESET
;
MYINIT	JSR $FFFF 	;changed
	JMP REINIT
;
;Open new screen
;
OPSCR	LDA #0 	;black
	STA COLPF2 	;background
	LDA #10 	;white text
	STA COLPF1
	LDA #146 	;blue borders
	STA COLPF2+2
	JSR CLRSCR 	;clear screen
	LDA # <DLIST 	;install my
	STA SDLSTL 	;Display List
	LDA # >DLIST
	STA SDLSTL+1
	LDA #33 	;screen on
	STA SDMCTL
	LDA #7 	;def. mode
	LDY # <VBI 	;point to my VBI
	LDX # >VBI
	JMP SETVBV 	;set it!
;
;This routine closes IOCB two.
;(Device IOCB.)
;
DCLOSE	LDX #DDEV 	;Device
	LDA #12 	;close command
	STA ICCOM,X 	;store it
	JMP CIOV 	;close it.
;
;Zero FR0
;
ZFR	LDA #0 	;zero floating
	STA FR0 	;point regs.
	STA FR0+1
	RTS 
;
;General use print routine
;
EPRINT	STA IND2 	;address of mem
	STY IND2+1 	;to 'print'.
	JSR CLCLN 	;get line adr
	LDY #0 	;zero loop.
PRL	LDA (IND2),Y 	;get char
	CMP #EOL 	;is EOL?
	BEQ PRD 	;yes. finished.
	JSR ASC2IC 	;no. make icode
	STA (SCR),Y 	;on screen
	INY  	;up loop.
	BNE PRL 	;go again.
PRD	INC ROWCRS 	;up y-pos.
	RTS  	;bye.
;
;Error routine
;
GOTERR	LDA #100 	;nice tone.
	STA AUDF1
	LDA #$AA 	;no distortion
	STA AUDC1
	LDA #0 	;leave it on
	STA RTCLOK 	;for 5 jiffies.
TONE	LDA RTCLOK
	CMP #5
	BNE TONE
	LDX #0 	;turn it off.
	STX AUDC1
	STX AUDF1
	RTS  	;bye.
;
;Fetch address
;
GADR	JSR CLRCMD 	;clear prompt
	JSR CLRWIN 	;and window
	LDA #10 	;a '*'
	STA COMND
	JSR INPUT 	;get INPUT
	BCC GDNM 	;good input
	PLA  	;pull off RTS
	PLA 
	RTS  	;next command
GDNM	LDY #0
GTDR	LDX #0
	STX HXDC 	;signal hex #
	LDA MYBUF,Y 	;first char.
	CMP #'. 	;a '.'?
	BNE GET4 	;no. in hex
	INC HXDC 	;signal dec #
	INY 
	BNE GNUM 	;get decimal
;
;Pull out a 4 byte hex address
;
GET4	JSR ZFR 	;zero fp regs
	STA GDIGS
G4LOOP	LDA MYBUF,Y 	;pull char
	CMP #EOL 	;at end?
	BNE TESTIT 	;no.
	RTS  	;bye.
TESTIT	LDX #$0F 	;do 16 digits.
G4SCAN	CMP HEXDIG,X 	;is it in
	BEQ GOTG4D 	;the Hex table?
	DEX  	;no. try next.
	BPL G4SCAN 	;go again.
G4ERR	PLA  	;Not in table,
	PLA  	;we have an
	JMP GOTERR 	;error. Sorry.
GOTG4D	ASL FR0 	;multiply #
	ROL FR0+1 	;by a factor
	ASL FR0 	;of 16.
	ROL FR0+1
	ASL FR0
	ROL FR0+1
	ASL FR0
	ROL FR0+1
	TXA  	;add digit
	ORA FR0 	;to the
	STA FR0 	;current addr
	INC GDIGS 	;up digs done
	LDA GDIGS 	;get it.
	CMP #5 	;more than 4
	BCS G4ERR 	;yes. error.
	INY  	;do next one
	JMP G4LOOP 	;do again.
;
;Get out a decimal number
;
GNUM	JSR ZFR 	;zero fp regs
	STA GDIGS
DONUM	LDA MYBUF,Y 	;pull char
	CMP #EOL 	;EOL?
	BNE GTEST 	;no.
	LDA #0 	;index to str
	STA CIX 	;is zero
	STY SAVEY
	LDA # <LBUFF 	;point INBUFF
	STA INBUFF 	;to LBUFF
	LDA # >LBUFF
	STA INBUFF+1
	LDX GDIGS 	;install EOL
	LDA #EOL 	;for safety
	STA LBUFF,X
	JSR AFP 	;to fp
	BCS GNERR 	;error?
	JSR FPI 	;to integer
	BCS GNERR 	;error?
	LDY SAVEY 	;restore Y
	RTS  	;bye.
GTEST	LDX #9 	;scan 10 digs.
GNSCAN	CMP HEXDIG,X 	;in table
	BEQ GOTGN 	;have digit
	DEX  	;try next
	BPL GNSCAN
GNERR	PLA  	;error
	PLA  	;bad digit
	JMP GOTERR
GOTGN	INY  	;up char index
	LDX GDIGS 	;save char in
	STA LBUFF,X 	;LBUFF.
	INC GDIGS 	;next digit
	JMP DONUM 	;go again
;
;Show memory contents
;
SHOMEM	LDX #0 	;pos. 2,0
	STX HXDC
	INX 
	STX NBYTE
	JSR ZBUF 	;blank buffer
	LDA ADDR 	;in fp for
	STA FR0 	;hex convert
	LDA ADDR+1
	STA FR0+1
	LDX #0 	;at pos 0
	JSR COPYNUM 	;copy number
	LDA #EOL 	;insert EOL
	STA MYBUF+30
	LDY #0 	;Y = 0
COPY2	STY SAVEY 	;save y
	JSR ZFR 	;Zero FR0
	TAY  	;Y = 0
	LDA (ADDR),Y 	;number
	STA OP 	;make digit
	JSR FNDBRK 	;BRK point?
	LDA OP 	;put op in FR0
	STA FR0
	LDY SAVEY 	;restore index
	CMP #EOL 	;have EOL?
	BEQ GOL 	;yes. leave it.
	STA MYBUF+23,Y 	;save char
GOL	JSR BIN2HEX 	;make it hex
	LDA BSET 	;is this BRK?
	BEQ NN1 	;point? no.
	LDY #1 	;yes. make #
NN0	LDA LBUFF+2,Y 	;inverse
	ORA #$80 	;video.
	STA LBUFF+2,Y
	DEY 
	BPL NN0
NN1	LDY SAVEY 	;insert in buf.
	LDX STRPOS,Y 	;get position
	LDA LBUFF+2 	;first digit
	STA MYBUF,X 	;in buffer
	LDA LBUFF+3 	;second digit
	STA MYBUF+1,X 	;in buff.
	JSR NSET 	;add 1 to addr
	INY  	;up count
	CPY #6 	;done 6?
	BNE COPY2 	;no.
	RTS  	;bye.
;
;Handle Change memory
;
CHGMEM	JSR GADR
CREG	LDA FR0 	;copy fp to
	STA SMEM 	;zpage for
	LDA FR0+1 	;lookup
	STA SMEM+1
	JSR GADR 	;get number
	LDA FR0 	;take LSB
	LDY #0
	STA (SMEM),Y 	;poke it in
	RTS 
;
;Go memory routine
;
GMEM	JSR GADR 	;run address
	PLA 
	PLA 
	LDA FR0 	;copy RUN addr
	STA USRPC 	;for later
	LDA FR0+1
	STA USRPC+1
	SEI  	;kill interrupts
	LDX #0
C321	LDA MYZPG,X 	;copy USER RAM
	STA ZPAGE,X 	;back for the
	LDA MYPG1,X 	;run.
	STA STACK,X
	LDA MYPG2,X
	STA PAGE2,X
	INX 
	BNE C321
	CLI 
	LDX USRS 	;restore user
	TXS  	;registers
	LDA USRP
	PHA 
	LDX USRX 	;restore X
	LDY USRY 	;and Y
	LDA USRA 	;and Acc
	INC EXEC 	;set RUN flag
	PLP  	;restore status
	JMP (USRPC) 	;RUN!
;
;Disassemble Memory
;
DISMEM	JSR GADR 	;get adr
TOEDEV	LDA FR0 	;move in addr
	STA ADDR
	STA TOPADR 	;top line
	LDA FR0+1 	;hi byte
	STA ADDR+1
	STA TOPADR+1
	LDA #0 	;pos.0
	STA ROWCRS
DMEM	JSR FINLIN 	;disa it
	JSR CLCLN 	;screen line
	LDY #30
DM3	LDA MYBUF,Y
	CMP #EOL 	;make EOL space
	BNE CON2
	LDA #32
CON2	JSR ASC2IC 	;make icode
	STA (SCR),Y 	;on screen
	DEY 
	BPL DM3 	;do next
	INC ROWCRS 	;up ypos.
	LDA ROWCRS 	;get it
	CMP #1 	;second line?
	BNE SECLN 	;no.
	LDA ADDR 	;set LN2 pntr
	STA LN2
	LDA ADDR+1
	STA LN2+1
SECLN	LDA ROWCRS 	;done 14 yet?
	CMP #14
	BNE DMEM 	;no.
	RTS  	;yes.
;
;Memory disassembler
;
DISAMEM	LDY #0 	;zero Y
	STY BRAFLG 	;no branch
	STY HXDC 	;in hex
	LDA (ADDR),Y 	;get opcode
	STA OP 	;and save
	JSR FNDBRK 	;BRK point?
	LDA OP 	;get op
	LDY #0 	;zero Y
FNDOP	CMP OPCODE,Y 	;is op in
	BEQ HAVOP 	;the opcode
	INY  	;table?
	CPY #151 	;151 yet?
	BNE FNDOP 	;no.
HAVOP	STY SAVY2 	;save Y
	JSR ZBUF 	;clear buffer
	LDA ADDR 	;copy addr
	STA FR0 	;to FP
	LDA ADDR+1
	STA FR0+1
	LDX #0 	;pos. 0
	JSR COPYNUM 	;copy it
	JSR ZFR 	;clear FP
	LDA OP 	;get op
	STA FR0 	;in FP
	LDX #6 	;pos 8
	JSR COPYNUM 	;copy it
	LDY SAVY2 	;get Y
	LDX OFFSET,Y 	;MNE offset
	LDY #0 	;copy out the
ADMNE	LDA MNE,X 	;Mnemonic and
	STA MYBUF+18,Y 	;put in
	INX  	;buffer.
	INY 
	CPY #3 	;3 chars?
	BNE ADMNE 	;no.
	LDY #1 	;Y = 1
	STY NBYTE 	;1 byte instr
	LDA (ADDR),Y 	;copy next 2
	STA INDR 	;possible
	INY  	;instruction
	LDA (ADDR),Y 	;bytes to temp
	STA INDR+1 	;hold address.
	LDY SAVY2 	;restore Y
	LDA ITYPE,Y 	;get Itype
	ASL A 	;times 2
	TAY  	;in Y
	LDA DCOMT,Y 	;look up the
	STA DJSR+1 	;routine addr
	LDA DCOMT+1,Y 	;an put in
	STA DJSR+2 	;JSR
DJSR	JSR $FFFF 	;go command!
	LDY #39 	;now insert the
AEOL3	LDA MYBUF,Y 	;the EOL
	CMP #32 	;character
	BNE AEOL2
	DEY 
	BPL AEOL3
AEOL2	LDA #EOL
	STA MYBUF+1,Y
	LDA BSET 	;brk point?
	BEQ NSET 	;no.
	JSR INV2 	;inverse it
NSET	LDA ADDR 	;now add the
	CLC  	;number of
	ADC NBYTE 	;byte to the
	STA ADDR 	;adr pointer
	LDA ADDR+1 	;to get next
	ADC #0 	;instr addr.
	STA ADDR+1
	RTS  	;bye.
;
;Inverse Mnemonics
;
INV2	LDY #18 	;start w/ 18th
INVL	LDA MYBUF,Y 	;get char
	EOR #$80 	;flip bit 7
	STA MYBUF,Y 	;store in buff
	INY  	;up index
	CPY #21 	;21st yet?
	BNE INVL 	;not done
	RTS  	;done.
;
;Immediate Mode
;
IMMED	JSR LOBYTE 	;insert LSB
	LDA #'# 	;a '#'
	STA MYBUF+22 	;in buffer
NUMBR	LDA #0 	;zero MSB
	STA INDR+1
NUMBR2	LDA INDR 	;put in FR0
	STA FR0
	LDA INDR+1
	STA FR0+1
NUMBR3	LDX #23 	;put in arg
	JMP COPYNUM
;
;Zero page
;
ZPAG	JSR LOBYTE 	;do LSB
	JMP NUMBR 	;add arg
;
;Zero page,X
;
ZPAGX	JSR LOBYTE 	;LSB
	JSR NUMBR 	;add arg
ADDX2	LDY #1 	;just ,X
ADDX	INX  	;up X
ADDX3	LDA COMMAX,Y 	;copy the
	STA MYBUF+1,X 	;,X into
	DEX  	;the buffer
	DEY 
	BPL ADDX3
	RTS  	;done.
;
;Absolute
;
ABSOL	JSR LOBYTE 	;LSB
	JSR HIBYTE 	;MSB
	JMP NUMBR2 	;add arg
;
;Absolute ,X
;
ABSOLX	JSR ABSOL 	;set up absol
	JMP ADDX2 	;copy in ,X
;
;Absolute ,Y
;
ABSOLY	JSR ABSOL 	;do absol
ACY	LDA #$2C 	;a comma
	STA MYBUF+1,X 	;in buf
	LDA #89 	;a Y
	STA MYBUF+2,X 	;in buf
	RTS  	;done
;
;Indirect X
;
INDX	LDA #0 	;no MSB
	STA INDR+1
	JSR LOBYTE 	;do LSB
	JSR OPENP 	;add open '('
	INX  	;up X
	LDY #2 	;3 bytes
	BNE ADDX 	;copy ',X)'
;
;Indirect Y
;
INDY	LDA #0 	;no MSB
	STA INDR+1
	JSR LOBYTE 	;do LSB
	JSR OPENP 	;left (
	LDY #2 	;copy in the
ADDY	LDA COMMAY,Y 	;'),Y'
	STA MYBUF+1,X
	INX 
	DEY 
	BPL ADDY
	RTS  	;done
;
;Accumulator Mode
;
ACCUM	LDA #65 	;an 'A'
	STA MYBUF+22 	;in buffer
	RTS  	;done
;
;Branch instructions
;
RELA	JSR LOBYTE 	;put LSB
	INC NBYTE 	;make 3 byte
	JSR ZFR 	;zero FR0
	INC BRAFLG 	;have branch.
	LDA #29 	;add arrow
	STA MYBUF+17 	;in buffer.
	LDA INDR 	;get operand
	BPL NOBACK 	;not inverse
	DEC MYBUF+17 	;make up arrow
	AND #$7F 	;mask inverse
	EOR #$7F 	;flip it
	CLC  	;add a 1
	ADC #1
	STA DINS+1 	;save it
	SEC  	;and subtract
	SBC #2 	;two.
	JMP TOFP 	;done
NOBACK	STA DINS+1
TOFP	LDA INDR 	;operand
	BPL FORWARD 	;bran forward
	LDA ADDR 	;now subtract
	SEC  	;from addr
	SBC DINS+1 	;to find out
	STA FR0 	;where this
	LDA ADDR+1 	;branch goes.
	SBC #0
	STA FR0+1
	JMP BACK 	;done.
FORWARD	CLC  	;add # to
	ADC ADDR 	;addr to calc
	STA FR0 	;forward branch
	LDA ADDR+1
	ADC #0
	STA FR0+1
BACK	LDA #2 	;now add a 2
	CLC  	;to skip the
	ADC FR0 	;instruction
	STA FR0 	;address
	STA BRAN 	;and save
	LDA FR0+1 	;the branch
	ADC #0 	;addr for the
	STA FR0+1 	;tracer.
	STA BRAN+1
	JSR NUMBR3 	;add number
	DEC NBYTE 	;make 2 bytes
;
;Implied Mode
;
IMP	RTS  	;do nothing
;
;Indirect instructions
;
INDI	JSR LOBYTE 	;copy in LSB
	JSR HIBYTE 	;and MSB
	JSR OPENP 	;do open (
	LDA #') 	;and add
	STA MYBUF+1,X 	;closing one
	RTS  	;leave.
;
;Zero page ,Y
;
ZPAGY	JSR LOBYTE 	;do LSB
	JSR NUMBR 	;add arg
	JMP ACY 	;copy ,Y
;
;Insert LSB
;
LOBYTE	INC NBYTE 	;up bytes
	JSR ZFR 	;zero FR0
	LDA INDR 	;do lobyte
	STA FR0
	LDX #10 	;at pos. 10
	JMP COPYNUM 	;do it!
;
;Insert MSB
;
HIBYTE	INC NBYTE 	;3 byter
	JSR ZFR 	;no FR0
	LDA INDR+1 	;hibyte
	STA FR0 	;in FP
	LDX #14 	;at pos. 14
	JMP COPYNUM 	;copy it.
;
;Add Open (
;
OPENP	LDA #'( 	;open (
	STA MYBUF+22 	;in buffer
	JMP NUMBR2 	;add number
;
;Handle I/O error
;
IOERR	JSR ZFR 	;save errnum
	STY FR0
	STY ERRNUM
	JSR GOTERR 	;beep!
	JSR ZBUF 	;no buffer
	JSR IFP 	;Int to FP
	JSR FASC 	;con to asc
	LDY #$FF
	LDX #$FF 	;copy to buf
IOLP	INY 
	INX 
	LDA (INBUFF),Y
	STA MYBUF,X
	BPL IOLP
	AND #$7F
	STA MYBUF,X 	;in err buf
	LDA #EOL
	STA MYBUF+1,X
	STX CEND
	LDX #17 	;search the
ERRLP1	LDA ERRS,X 	;errors
	CMP ERRNUM 	;we have in
	BEQ GERR1 	;english
	DEX 
	BPL ERRLP1
	BMI IOL5
GERR1	TXA  	;we have one.
	ASL A 	;* 2
	TAX  	;get address
	LDA ERRTAB+1,X
	STA INDR+1
	LDA ERRTAB,X
	STA INDR
	LDY #$FF 	;and copy it
	LDX CEND 	;to mybuf.
	LDA #',
	STA MYBUF+1,X
	INX 
IOL4	INY 
	INX 
	LDA (INDR),Y
	STA MYBUF+1,X
	BPL IOL4
	AND #$7F
	STA MYBUF+1,X
	LDA #EOL 	;add EOL.
	STA MYBUF+2,X
IOL5	LDA #14 	;on error line
	STA ROWCRS
	JSR CLRERR 	;clear it
IOL3	LDA # <MYBUF 	;and print
	LDY # >MYBUF
	JMP EPRINT
;
;Copy number to string
;
COPYNUM	DEX 
	STX CNTLO 	;save X
	LDA HXDC 	;hex/dec?
	BEQ INHEX 	;have Hex.
	JSR IFP 	;to FP
	JSR FASC 	;to ASC
	LDX CNTLO 	;get index
	LDY #$FF 	;and copy
CNLOOP	INY  	;the number
	INX 
	LDA (INBUFF),Y
	STA MYBUF,X
	BPL CNLOOP
	AND #$7F 	;mask bit 7
	STA MYBUF,X
	RTS  	;leave
INHEX	JSR BIN2HEX 	;make hex
	LDX CNTLO 	;get pos
	INX  	;up it.
	BEQ ALL 	;if 0 do all
	CPX #20 	;is X > 20?
	BCS ARG 	;yes.
SOME	LDA LBUFF+2 	;no. just copy
	STA MYBUF,X 	;the LSB part
	INX  	;of the Hex
	LDA LBUFF+3 	;number.
	STA MYBUF,X
	RTS  	;bye.
ARG	LDA #'$ 	;install a '$'
	STA MYBUF,X
	INX  	;up X
	LDA NBYTE 	;Get # of bytes
	CMP #2 	;is it 2?
	BEQ SOME 	;no.
ALL	LDY #0 	;no. copy it
ALOOP	LDA LBUFF,Y 	;all to buf
	STA MYBUF,X
	INX 
	INY 
	CPY #4 	;done yet?
	BNE ALOOP 	;nope.
	DEX  	;dec X
	RTS  	;so long.
;
;Binary to hexadecimal
;
BIN2HEX	LDY #0 	;buf indx=0
	LDX #1 	;do 2 nums
NXTBYT	STX SAVEX 	;save it
	LDA #240 	;get HI bits
	AND FR0,X
	LSR A 	;divide by 16
	LSR A
	LSR A
	LSR A
	TAX  	;to index
	LDA HEXDIG,X 	;get char
	STA LBUFF,Y 	;and save
	INY  	;up buf index
	LDX SAVEX 	;get X back
	LDA #15 	;now LO bits
	AND FR0,X
	TAX 
	LDA HEXDIG,X 	;get char and
	STA LBUFF,Y 	;put in buffer
	INY  	;up buf indx
	LDX SAVEX 	;get X
	DEX  	;down by one
	BPL NXTBYT 	;done?
	RTS  	;yes. bye.
;
;Fill buffer with blanks
;
ZBUF	LDA #32 	;space
	LDX #39 	;do 128 spaces
ZLOOP	STA MYBUF,X 	;in buf
	DEX  	;down count
	BPL ZLOOP 	;go again
	RTS  	;bye.
;
;Handle a 6502 BRK instruction
;
BRKER	LDA EXEC 	;running?
	BNE SAVREG 	;yes.
	PLA  	;no. just exit.
	RTI 
SAVREG	CLD  	;chill dec
	STX USRX 	;save registers
	STY USRY
	PLA 
	STA USRA
	PLA 
	STA USRP 	;calc BRK point
	PLA 
	SEC 
	SBC #2
	STA USRPC 	;put in program
	PLA  	;counter.
	SBC #0
	STA USRPC+1
	TSX  	;store stck pntr
	STX USRS 	;for user
	CLI 
	JMP SHOBRK 	;bye.
;
;Vertical Blank Interrupt
;
;Update my RTCLOK registers
;in my zero page buffer so
;we can use real-time delay
;loops in the tracer.
;
VBI	INC MYZPG+20 	;jiffies
	BNE VBDN
	INC MYZPG+19 	;'seconds'
	BNE VBDN
	INC MYZPG+18 	;'minutes'
;
;Now we check to see if there
;is an input being taken and
;if so blink the cursor
;
VBDN	LDA IPUT 	;inputting?
	BNE VBOT 	;no.
	LDA SRTIMR 	;key pressed?
	BEQ BLINK? 	;no.
	LDA #63 	;yes. leave
	STA CSHAPE 	;cursor on for
	LDA #60 	;1 second.
	STA BLINK
BLINK?	LDA CSHAPE 	;shape
	DEC BLINK 	;down blink
	BNE VEXIT 	;not 0 leave
	LDY #30 	;1/2 second
	STY BLINK 	;delay
	EOR #63 	;flip shape
	STA CSHAPE 	;and save
VEXIT	LDY CEND 	;get posit
	STA COMLN,Y 	;save cursor
VBOT	JMP XITVBV 	;all done
;
;Show BRK information
;
SHOBRK	PHP  	;get status
	TAX 
	PLA 
	AND #$EF 	;alter it
	PHA 
	PLP  	;put it back.
	LDA #0 	;stop EXEC
	STA EXEC
	JMP REINIT 	;reinit DEBUG+
;
;Keyboard Handler
;
MKEYV	LDA EXEC 	;running?
	BEQ MYKEYV
	LDA KBCODE
	CMP #$9C 	;control ESC?
	BNE MYKEYV 	;no so get key
	STY USRY 	;save Y
	STX USRX 	;save X
	LDA #$FF 	;clear out key
	STA CH
	PLA  	;pull Accum
	STA USRA 	;store it
	PLA  	;get P stat
	STA USRP 	;save it
	PLA  	;pull LSB of PC
	STA USRPC 	;save it
	PLA  	;pull MSB
	STA USRPC+1 	;save it
	TSX  	;get S pntr
	STX USRS 	;save it
	CLI  	;clear interrupt
	JMP SHOBRK 	;show info
MYKEYV	JMP (TKEY)
;
;Show registers
;
DOREGS	LDY #0 	;zero index
DOLOP	STY SAVEY
	JSR ZFR 	;clr FR0
	LDA USRA,Y 	;get reg
	STA FR0 	;in FR0
	JSR BIN2HEX 	;to hex.
	LDY SAVEY 	;reload Y
	LDX DRPOS,Y 	;find posit.
	LDA LBUFF+2 	;get byte
	SEC 
	SBC #32
	STA PCNT,X 	;into buffer
	LDA LBUFF+3 	;next.
	SEC 
	SBC #32
	STA PCNT+1,X
	INY 
	CPY #4 	;done?
	BNE DOLOP 	;no way.
	LDA USRPC 	;show the
	STA FR0 	;program cntr
	LDA USRPC+1
	STA FR0+1
	JSR BIN2HEX
	LDY #3 	;put it on
PCL	LDA LBUFF,Y 	;the screen
	SEC  	;make icode
	SBC #32
	STA PCNT,Y
	DEY 
	BPL PCL
	LDX #17 	;index for PS
	LDY #7 	;do 8 bits.
BITIT	LDA USRP 	;get it
	AND BITS,Y 	;mask it
	BEQ NON
	LDA #17 	;a '1'
	BNE PRON
NON	LDA #16 	;a '0'
PRON	STA PCNT,X 	;on screen
	INX  	;up scr pntr
	DEY  	;dec loop
	BPL BITIT 	;do again.
	LDA #17 	;put on a 1
	STA PCNT+19 	;under unused
	RTS  	;status bit.
;
;Change registers
;
CREGS	LDY #7 	;copy on prompt
CRGL	LDA REGTXT,Y
	STA COMND,Y
	DEY 
	BPL CRGL
REGW	JSR GETKEY 	;get a key
	LDY #4 	;test 5
CRG	CMP REGIS,Y 	;this one?
	BEQ HAVREG 	;yes!
	DEY  	;nope.
	BPL CRG 	;try next
	BMI REGW 	;sorry, again
HAVREG	TYA  	;get address
	ASL A 	;of register
	TAY  	;in memory
	LDA REGTAB,Y 	;and put
	STA FR0 	;in fp regs
	LDA REGTAB+1,Y
	STA FR0+1
	JSR CREG 	;change memory.
	JMP DOREGS 	;show regs.
;
;These are the routines used
;to convert ASC to ICODE and
;ICODE to ASC.  Each uses a
;4 byte table of bit masks
;located at the end of the
;program for the conversion.
;
;Asc to internal
;
ASC2IC	JSR BITER 	;fix up bits
	ORA A2I,X 	;add new mask
	LDX SAVX2 	;restore X
	RTS  	;done!
;
;Internal to Asc
;
IC2ASC	JSR BITER 	;do bits.
	ORA I2A,X 	;add mask.
	LDX SAVX2 	;get X
	RTS  	;and leave
;
;Ready the bits
;
BITER	PHA  	;save old byte
	ROL A 	;roll the bits
	ROL A 	;around 4 times
	ROL A
	ROL A
	AND #3 	;make it index
	STX SAVX2 	;save X
	TAX  	;X = A
	PLA  	;pull old byte
	AND #$9F 	;mask it off
	RTS  	;and leave
;
;Clear screen (3 pages)
;
CLRSCR	LDA #0 	;get a space
	TAX  	;zero X
CLSC	STA MYSCRN,X 	;page 1
	STA MYSCRN+$0100,X 	;page 2
	STA MYSCRN+$0200,X 	;page 3
	INX  	;up loop
	BNE CLSC 	;and loop again
	STA ROWCRS 	;pos 0,0
	RTS  	;all done
;
;Input routine
;
INPUT	LDA #0 	;length = 0
	STA CEND
	STA IPUT 	;signal input
	JSR CLRWIN 	;clear window
	JSR ZBUF 	;and buffer
GETC	JSR GETKEY 	;get a key
	CMP #27 	;ESCape?
	BNE LEG0 	;nope.
	INC IPUT
	SEC  	;signal error
	RTS  	;and leave
LEG0	LDY NCHK 	;check legals?
	BNE GOTCHR 	;nope.
LEG1	LDY #40 	;is it a legal
LEGL	CMP LEGAL,Y 	;char?
	BEQ GOTCHR 	;yup.
	DEY  	;not this one
	BPL LEGL 	;try next
	JSR GOTERR 	;error.
	JMP GETC 	;go again.
GOTCHR	CMP #126 	;Delete?
	BNE NOTDEL 	;no.
	DEC CEND 	;lower length
	BPL TS2 	;<0? no.
	LDA #0 	;make zero.
	STA CEND
	JSR GOTERR 	;give error.
TS2	JSR TST1 	;test length
	LDA CEND 	;get length
	BEQ GETC 	;if not 0 go!
	LDY CEND 	;put in a spc
	CPY #12
	BNE DODEL
	DEY 
DODEL	LDA #0
	STA COMLN,Y 	;erase char
	STA COMLN+1,Y 	;erase cursor
	JMP GETC 	;get next.
NOTDEL	CMP #156 	;Delete Line?
	BEQ INPUT 	;yes.
	LDY CEND
	STA MYBUF,Y
	CMP #EOL 	;EOL?
	BEQ ATEND 	;done!
	JSR ASC2IC 	;make icode
	CPY #12
	BNE SHCH
	DEY 
SHCH	STA COMLN,Y 	;to screen
	INY 
	CPY #13
	BCC TST9
	DEY 
TST9	STY CEND
	JSR TST1 	;length?
	JMP GETC 	;do again.
ATEND	LDA MYBUF
	CMP #EOL
	BEQ INPUT
	LDA #0 	;check 'em
	STA NCHK 	;next time
	STA COMLN,Y 	;and erase cursr
	INC IPUT
	CLC  	;show good input
	RTS  	;bye.
;
;Test input length
;
TST1	LDA CEND 	;get length
	BNE TL2 	;no.
	STA COMLN 	;erase 1st char
	STA COMLN+1 	;and cursor
TL2	RTS  	;bye.
;
;Erase error line
;
CLRERR	LDY #23 	;24 chars.
	LDA #0 	;to clear
CLE	STA ERRLN,Y 	;on line
	DEY 
	BPL CLE 	;loop.
	RTS  	;so long.
;
;Print to device
;
DPRINT	LDA #PUTRC 	;put record
	LDX #DDEV
	STA ICCOM,X 	;CIO command
	LDA # <MYBUF 	;mybuf is
	STA ICBAL,X 	;the buffer
	LDA # >MYBUF 	;hi byte
	STA ICBAH,X
	LDA #40 	;40 bytes max
	STA ICBLL,X
	LDA #0
	STA ICBLH,X
	LDA MYBUF+23 	;if chr is
	BPL PRT 	;>128 invert
	JSR INV2
PRT	JSR CIOV 	;print it.
	BMI STOP 	;on error stop
	RTS 
STOP	PLA 
	PLA 
	JMP DCLOSE
;
;Calculate line address
;
CLCLN	LDA ROWCRS 	;y-pos
	ASL A 	;* 2
	TAX  	;look up the
	LDA SCRN,X 	;screen memory
	STA SCR 	;address in
	LDA SCRN+1,X 	;table and
	STA SCR+1 	;store it.
	RTS  	;bye.
;
;Change display between
;Disassembly and display mem
;
ALTER	LDA DTYPE 	;display type
	EOR #1
	STA DTYPE
	BNE HAVMEM 	;in display
	LDA #1
	STA BBCK
	LDA #33
	BNE ALT2 	;finish
HAVMEM	LDA #6
	STA BBCK
	LDA #48
ALT2	STA PCNT+29
	LDA TOPADR
	STA FR0
	LDA TOPADR+1
	STA FR0+1
	JMP TOEDEV
;
;Find Break point
;
FNDBRK	LDA #0 	;say none found
	STA BSET
	LDY #5 	;try 6 B.P's
SET?	LDA ADDR+1 	;check if
	CMP SETHI,Y 	;MSB is same?
	BNE UPLP 	;not same.
	LDA ADDR 	;match. try LSB
	CMP SETLO,Y
	BNE UPLP 	;no go.
	LDA OPSET,Y 	;match.set yet?
	BEQ UPLP 	;no.
	LDA OLDOP,Y 	;yes. restore
	STA OP 	;old opcode.
	INC BSET 	;say set.
	RTS  	;leave.
UPLP	DEY  	;decrease loop
	BPL SET? 	;done? no.
	RTS  	;yes. leave.
;
;Go to correct DISA routine
;
FINLIN	LDA DTYPE 	;type flag
	BNE FINL2 	;1 = display
	JMP DISAMEM 	;disassemble
FINL2	JMP SHOMEM 	;and display
;
;Set default GETKEY data
;
SETKEY	LDA #0 	;set normal vid
	STA INVS
	LDA #$40 	;and upper case
	STA CAPS
	RTS 
