;SAVE#D:BBKMON1.M65
	.OPT NO LIST
;
;The ANALOG monitor
;
;by: Bryan Schappel
;and Barry Kolbe 1985
;
;COMMANDS:
;
;?nnn   = dec to hex
;?$nn   = hex to dec
;C$nnn  = change memory
;    C$nnn<mm,mm,mm,...
;A$nnn  = assemble mode
;D$nnn  = disassemble [,P]
;    with printer option
;X      = go to DOS
;F      = disk directory [,Dn]
;Q      = quit
;S      = bin save
;B      = bin load
; XIO commands
; form E>D:filespec
; R>D:filespec1,filespec2
;R      = rename
;E      = erase
;L      = lock
;U      = unlock
;M      = mem dump
;
	.INCLUDE #D:BBKMON4.M65
;
	*= $2000
;
REINIT	JSR $FFFF 	;reinit DOS
	JSR OPSCR 	;open screen
	LDA # <REINIT 	;reset
	STA DOSINI 	;vectors
	LDA # >REINIT
	STA DOSINI+1
	LDA # <START
	STA DOSVEC
	LDA # >START
	STA DOSVEC+1
	LDA # <PROGEND 	;set memlo
	STA MEMLO 	;to end of
	LDA # >PROGEND 	;program
	STA MEMLO+1
	LDA WHICH? 	;where did
	BEQ REEX 	;we enter?
	JSR COPYZ
	JMP BEGIN
REEX	RTS 
;
DOINIT	LDA DOSINI 	;set
	STA REINIT+1 	;various
	LDA DOSINI+1 	;vectors to
	STA REINIT+2 	;interrupt
	LDA DOSVEC 	;SYS RESET
	STA DOSV+1
	LDA DOSVEC+1
	STA DOSV+2
	LDA # <START
	STA DOSVEC
	LDA # >START
	STA DOSVEC+1
	LDA # <REINIT
	STA DOSINI
	LDA # >REINIT
	STA DOSINI+1
	LDA # <PROGEND 	;reset
	STA MEMLO 	;memlo
	LDA # >PROGEND
	STA MEMLO+1
	JSR OPSCR
	LDA $06 	;cart present?
	BNE CRTYES
	JMP BEGIN
CRTYES	JMP ($BFFA) 	;go cart
;
START	CLD  	;save top
	LDX #$7F 	;half of page 0
CZP	LDA $80,X
	STA MYZPG,X
	DEX 
	BPL CZP
	LDA #1 	;set switch to 1
	STA WHICH?
BEGIN	JSR OPSCR 	;open screen
	LDA #$E4 	;make it
	STA $02C6 	;green
	LDA # <REINIT
	STA DOSINI
	LDA # >REINIT
	STA DOSINI+1
INPUT	LDA #0 	;reset dispflg
	STA $02FE
	JSR PRTCR 	;print CR
	JSR PROMPT 	;show prompt
INPT2	JSR GETLIN 	;input text
	JSR CRUNCH
;
;Now find command, if any,
;and go to the routine.
;
	LDX #13 	;scan 12 coms
	LDA MYBUF 	;get command
CMDCMP	CMP CMDTXT,X 	;in comms?
	BEQ GOCOMD 	;yes!
	DEX  	;try next one
	BPL CMDCMP
	BMI GOTR 	;oops! error
GOCOMD	TXA  	;command index
	ASL A 	;set up the
	TAX  	;JMP to command
	LDA CMDADR,X 	;routine
	STA GOCOM+1
	LDA CMDADR+1,X
	STA GOCOM+2
	LDY #1
GOCOM	JMP $FFFF 	;go command!
GOTR	DEY 
GOTERR	JSR NOWAY 	;sorry error
	JMP INPUT 	;get next
;
;This is the go DOS command
;
DOSCMD	LDA MYBUF,Y 	;is next
	CMP #EOL 	;char an EOL
	BNE GOTERR 	;no Error!
	JSR COPYZ
	LDA REINIT+1 	;reset
	STA DOSINI 	;DOS vectors
	LDA REINIT+2
	STA DOSINI+1
	LDA DOSV+1
	STA DOSVEC
	LDA DOSV+2
	STA DOSVEC+1
DOSV	JMP $FFFF 	;go DOS
;
;Quit program
;
QUITP	JSR COPYZ 	;copy Zpage
	LDA #0 	;set switch
	STA WHICH? 	;to zero
	JMP WARMST 	;bye
;
COPYZ	LDX #$7F 	;do 128 bytes
CPZ	LDA MYZPG,X 	;get a byte
	STA $80,X 	;in Zpage
	DEX 
	BPL CPZ 	;do next one
	LDA #$94
	STA $02C6
	RTS  	;bye.
;
;Open new screen
;
OPSCR	LDX #EDEV 	;E: iocb
	LDA #12 	;close iocb 0
	STA ICCOM
	JSR CIOV
	LDX #EDEV 	;Open 'E:'
	LDA # <EADR
	STA ICBAL
	LDA # >EADR
	STA ICBAH
	LDA #3 	;open command
	STA ICCOM
	LDA #12 	;allow I/O
	STA AUX1
	STX AUX2 	;zero
	JMP CIOV 	;CIO
;
;Print a CR
;
PRTCR	LDA # >CR
	LDY # <CR
;
;General use print routine
;
PRINT	STA ICBAH 	;MSB
	STY ICBAL 	;LSB
	LDX #EDEV 	;E:
	LDA #PUTRC 	;put record
	STA ICCOM 	;command.
	LDA #$28 	;40 bytes
	STA ICBLL
	STX ICBLH 	;zero
	JSR CIOV 	;print it
	BMI TERR 	;error
	RTS 
TERR	PLA  	;pull ret
	PLA 
	JSR IOERR
	JMP INPUT
;
;Input a line
;
GETLIN	LDA #GETRC 	;get record
	STA ICCOM
	LDX #EDEV 	;from E:
	STX ICBLH
	LDA #50 	;50 bytes Max
	STA ICBLL
	LDA # <MYBUF 	;point to my
	STA ICBAL 	;I/O buffer
	LDA # >MYBUF
	STA ICBAH
	JSR CIOV 	;get it!
	BMI GETLIN 	;again if error
	RTS  	;bye
;
;INPUT masher
;will return to command proc
;if input is just a CR.
;
CRUNCH	LDX #0 	;first find
FINDBE	LDA MYBUF,X 	;end of input
	CMP #EOL 	;EOL?
	BEQ GOTEND 	;yes!
	INX  	;nope.
	BPL FINDBE 	;try next one
GOTEND	CPX #0 	;just a CR?
	BEQ GOTNN 	;yes.
	STX ENDPTR 	;save length
	LDX #0
SQUISH	LDA MYBUF,X 	;get a char
	CMP #$20 	;a space?
	BEQ DOSQSH 	;yes, smash!
	CMP #'> 	;prompt?
	BNE NOSQSH 	;nope.
DOSQSH	TXA  	;now we will
	TAY  	;move the input
SQSHLP	LDA MYBUF+1,Y 	;back one
	STA MYBUF,Y 	;char in the
	INY  	;buffer.
	CPY ENDPTR 	;done yet?
	BNE SQSHLP 	;nope.
	DEC ENDPTR 	;length down one
	JMP SQUISH 	;and do again
NOSQSH	INX  	;up pointer
	CPX ENDPTR 	;done yet?
	BNE SQUISH 	;nope.
	LDA MYBUF 	;get char.
	CMP #EOL 	;just EOL?
	BEQ GOTNN 	;yes.
SQEX	RTS  	;bye.
GOTNN	PLA  	;pull off RTS
	PLA 
	JMP INPUT 	;and input again
;
;Show the prompt
;
PROMPT	LDA # >BBK
	LDY # <BBK
	JSR PRINT
	JSR PRTCR 	;next line.
	LDA #'> 	;and put a '>'
	JMP PUTCH 	;bye!
;
;Print I/O Buffer
;
PRTIO	LDA # >MYBUF 	;point to
	LDY # <MYBUF 	;MYBUF
	JMP PRINT 	;and print it
;
;Error routine
;
NOWAY	LDA MYBUF,Y 	;get bad char
	CMP #EOL 	;EOL?
	BNE INVIT 	;no.
	STA MYBUF+1,Y 	;move over 1
	LDA #$20 	;get a space
INVIT	ORA #$80 	;make inverse
	STA MYBUF,Y 	;store it
	LDX #0 	;now print
PCER	LDA IOER,X 	;the 'Error>'
	JSR PUTCH 	;prompt
	INX 
	CPX #7
	BNE PCER
	JSR PRTIO 	;print MYBUF
BEEP	LDA #100 	; a nice tone
	STA AUDF1
	LDA #$AA 	;pure.
	STA AUDC1
	LDA #0 	;let it on
	STA RTCLOK 	;for about
TONE	LDA RTCLOK 	;3 jiffies
	CMP #3
	BNE TONE
	LDX #0 	;turn off sound
	STX AUDC1
	RTS  	;bye.
;
;These are the ?nnn, ?$nnn
;handlers.  If there is a $
;in front of the number it
;is assumed to be hex-dec
;otherwise dec-hex.
;
PRDH	LDY #1 	;second char
	LDA MYBUF,Y 	;get it
	CMP #'$ 	;Hex?
	BNE DODEC 	;no. try Dec.
	INY  	;get 1st char
	LDA MYBUF,Y 	;of the number
	CMP #EOL 	;is it a CR?
	BNE NOEOL 	;nope.
	JMP GOTERR 	;yes. error
NOEOL	LDY #2 	;third char.
	JSR PULLHEX 	;get the num.
	LDA HL
	STA FR0
	LDA HH
	STA FR0+1
NUMIN	JSR IFP 	;convert it
	JSR FASC 	;to ascii.
	LDY #$FF 	;set up index
LOOP	INY  	;to copy the
	LDA (INBUFF),Y 	;asc string
	STA PBUF+1,Y 	;to a buffer
	BPL LOOP 	;so it can be
	AND #$7F 	;printed.
	STA PBUF+1,Y 	;last char
	INY  	;end of string
PBFR	LDA #EOL 	;install EOL
	STA PBUF+1,Y 	;in buffer
	LDA # >PBUF 	;and print it.
	LDY # <PBUF
	JSR PRINT
	JMP INPUT 	;get nxt comm.
;
;Decimal to Hex routine
;
DODEC	LDA #1 	;point to 2nd
	STA CIX 	;char in MYBUF
	LDA # <MYBUF 	;for ASC-FP
	STA INBUFF 	;conversion
	LDA # >MYBUF
	STA INBUFF+1
	JSR AFP 	;convert it
	JSR FPI 	;make integer
	JSR BIN2HEX 	;convert to hex
	LDY #1 	;copy it.
	LDA #'$ 	;put in a '$'
	STA PBUF+1
DLOOP	LDA PRNBUF-1,Y 	;move out
	STA PBUF+1,Y 	;the HEX num
	INY  	;to the buffer
	CPY #5 	;done?
	BNE DLOOP 	;not yet.
	JMP PBFR 	;print it.
;
;Handle Change memory
;
CHGMEM	LDA MYBUF,Y
	CMP #'$
	BNE CMERR
	INY 
	JSR PULLHEX
	LDA HCNT 	;any chrs?
	BEQ CMERR 	;no. error
	CMP #5
	BCS CME1
	LDA HL 	;copy fp to
	STA TEMP 	;zpage for
	LDA HH 	;lookup
	STA TEMP+1
	LDA #0 	;zero bytes
	STA NDIG 	;(y index)
	LDA MYBUF,Y
	CMP #$3C 	;'<'?
	BNE CMERR 	;ERROR!
DOCM	INY  	;up char
	JSR PULLHEX 	;get number
	LDA HCNT
	BEQ CMERR
	CMP #3 	;2 digits
	BCS CME1
	LDA HL 	;take LSB
	STY SAVEY
	LDY NDIG 	;get offset
	STA (TEMP),Y 	;poke it in
	INC NDIG 	;nxt loc
	LDY SAVEY
	LDA MYBUF,Y
	CMP #EOL 	;done yet?
	BEQ CMDON 	;yes.
	CMP #$2C 	;','?
	BEQ DOCM 	;not done
CMDON	JMP INPUT
CME1	DEY 
CMERR	JMP GOTERR
;
;Handle DIR command
;
DIREC	LDA MYBUF,Y
	CMP #EOL 	;just EOL?
	BEQ DRV1 	;yes. drive 1
	CMP #$2C 	;','?
	BEQ DRV? 	;yes.
DRVER	JMP GOTERR
DRV1	LDA #'1 	;set drive 1
	STA DIRNAM+1
	BNE GOTDIR 	;get dir
DRV?	INY 
	LDA MYBUF,Y 	;get drive #
	CMP #'1 	;< 1?
	BCC DRVER 	;yes.
	CMP #'5 	;> 4?
	BCS DRVER 	;yes.
	STA DIRNAM+1 	;no use a D#
GOTDIR	JSR DCLOSE 	;close D:
	LDX #DDEV
	LDA #3 	;open D:
	STA ICCOM,X 	;to read
	LDA # <DIRNAM 	;the disk
	STA ICBAL,X 	;directory
	LDA # >DIRNAM
	STA ICBAH,X
	LDA #6 	;gdir com
	STA AUX1,X
	LDA #0
	STA AUX2,X
	JSR CIOV
	BPL NER
	JSR IOERR
	JMP DERR
NER	LDX #DDEV 	;set up CIO to
	LDA #GETRC 	;get records
	STA ICCOM,X
	LDA # <MYBUF
	STA ICBAL,X
	LDA # >MYBUF
	STA ICBAH,X
	TXA 
	STA ICBLL,X
	LDA #0
	STA ICBLH,X
GETDIR	LDX #DDEV 	;get record
	JSR CIOV
	BPL NOERR
DERR	JSR DCLOSE 	;done so close
	JMP INPUT 	;and leave
NOERR	JSR PRTIO 	;print it
	JMP GETDIR 	;go again.
;
;XIO handlers
;
LOCK	LDA #35 	;lock command
	BNE DOXIO
UNLOCK	LDA #36 	;unlock command
	BNE DOXIO
DELETE	LDA #33 	;delete command
	BNE DOXIO
RENAME	LDA #32 	;rename command
;
DOXIO	PHA  	;save command
	JSR DCLOSE 	;close disk
	LDX #DDEV 	;use Dn:
	PLA  	;get command
	STA ICCOM,X 	;store it
	LDA # <[MYBUF+1] 	;point to
	STA ICBAL,X 	;the filename
	LDA # >[MYBUF+1]
	STA ICBAH,X
	JSR CIOV 	;do function
	BPL XGD 	;good one? yes.
	JSR IOERR 	;nope. show err
XGD	JMP INPUT 	;bye.
;
;Close D: device
;
DCLOSE	LDX #DDEV 	;Dn:
	LDA #12 	;close com
	STA ICCOM,X
	JMP CIOV 	;SLAM!
;
;Handle I/O error
;
IOERR	STY FR0 	;save errnum
	LDA #0
	STA FR0+1
	JSR IFP
	JSR FASC 	;con to asc
	LDY #$FF
	LDX #6 	;copy to buf
IOLP	INY 
	INX 
	LDA (INBUFF),Y
	STA IOER,X
	BPL IOLP
	AND #$7F
	STA IOER,X 	;in err buf
	INX 
	LDA #EOL 	;install EOL
	STA IOER,X
	JSR PRTCR
	JSR DCLOSE
	LDA # >IOER 	;print it
	LDY # <IOER
	JSR PRINT
	JMP BEEP
;
;Binary to hexadecimal
;
BIN2HEX	LDY #0 	;buf indx=0
	LDX #1 	;do 2 nums
NXTBYT	STX SAVEX 	;save it
	LDA #$F0 	;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 PRNBUF,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 PRNBUF,Y 	;put in buffer
	INY  	;up buf indx
	LDX SAVEX 	;get X
	DEX  	;down by one
	BPL NXTBYT 	;done?
	RTS  	;yes. bye.
;
;This is the command text and
;addresses of routines
;plus string positions for
;Show command.
;
CMDTXT	.BYTE "?FXCLUERDQASBM"
CMDADR	.WORD PRDH 	;?
	.WORD DIREC 	;F
	.WORD DOSCMD 	;X
	.WORD CHGMEM 	;C
	.WORD LOCK 	;L
	.WORD UNLOCK 	;U
	.WORD DELETE 	;E
	.WORD RENAME 	;R
	.WORD DISA 	;DISA
	.WORD QUITP 	;quit prog
	.WORD ASM 	;ASM
	.WORD BSAVE 	;bin save
	.WORD BLOAD 	;bin load
	.WORD MDUM 	;mem dump
;
;Miscellaneous Text & Dev nams
;
EADR	.BYTE "E:",EOL
DIRNAM	.BYTE "D1:*.*"
CR	.BYTE EOL
IOER	.BYTE "Error>    ",EOL
HEXDIG	.BYTE "0123456789ABCDEF"
PBUF	.BYTE "=     ",EOL
BBK	.BYTE "BBK MONITOR",EOL
WHICH?	.BYTE 0
XSAVE	    1
YSAVE	    1
;
	.INCLUDE #D:BBKMON2.M65
	.INCLUDE #D:BBKMON3.M65
	.INCLUDE #D:BBKMON5.M65
	.OPT LIST
PROGEND	= *
	.OPT NO LIST
	*= $02E2
	.WORD DOINIT
