;SAVE#D:BBKMON3.M65
;
;disassembler
;
DISA	LDA MYBUF,Y
	CMP #'$ 	;check for hex
	BNE DNOK
	INY 
	JSR PULLHEX 	;get hex add
	LDA HCNT
	BEQ DNOK1 	;no digits
	CMP #5 	;1-4 digits
	BCC DD1
DNOK	DEY  	;show error
DNOK1	JMP GOTERR
DD1	LDA HL 	;get low byte
	STA MEM
	LDA HH 	;& high
	STA MEM+1
	LDA #0 	;turn off
	STA PRFLG 	;print flag
	JSR DCLOSE 	;close iocb 2
	LDY Y2 	;check for
	LDA MYBUF,Y 	;',P'
	CMP #',
	BNE DCMD
	INY 
	LDA MYBUF,Y
	CMP #'P
	BNE DCMD
	STA PRFLG 	;set print flag
;
;open #2 to printer
;
	LDX #$20
	LDA #3 	;open printer
	STA ICCOM,X
	LDA # >PDEV
	STA ICBAH,X
	LDA # <PDEV
	STA ICBAL,X
	LDA #8 	;output
	STA AUX1,X
	JSR CIOV
;
;format output line with spaces
;
DCMD	LDY #32
F1	LDA FORMD,Y
	STA MYBUF,Y
	DEY 
	BPL F1
;
;put address in buffer
;
	LDA MEM+1
	JSR HEX 	;convert to hex
	LDY #1
	LDA HEXB 	;high nibble
	STA MYBUF,Y
	INY 
	LDA HEXS 	;low nibble
	STA MYBUF,Y
	LDA MEM 	;now do low
	JSR HEX 	;byte
	INY 
	LDA HEXB 	;high nibble
	STA MYBUF,Y
	INY 
	LDA HEXS 	;low nibble
	STA MYBUF,Y
;
;get base opcode
;
	LDY #0
	LDA (MEM),Y
	STA BASEOP 	;keep it
	LDX #0
O1	LDA BASEOP 	;check if valid
	CMP DOPC,X 	;from table
	BEQ O2
	INX 
	CPX #151 	;151 opcodes
	BNE O1
	LDA #13 	;13 = not valid
	STA OPT 	;opcode type
	LDX #151 	;151st = ???
	STX INDEX
	JMP O3
O2	STX INDEX 	;keep valid
O3	LDA OFFSET,X 	;index to
	TAX  	;mnemonic table
	LDY #7
O4	LDA MNE,X 	;put mnemonic
	STA MYBUF,Y 	;into buffer
	INX 
	INY 
	CPY #10
	BNE O4
	LDX INDEX 	;now get type
	LDA MTYPE,X 	;of addressing
	STA OPT
	JSR INCMEM 	;increase mem
;
; put opcode into buffer
;
	LDA BASEOP
	JSR HEX 	;convert to hex
	LDA HEXB 	;high 1st
	LDY #19
	STA MYBUF,Y
	INY 
	LDA HEXS 	;low 2nd
	STA MYBUF,Y
;
;get type of addressing
;and jsr to correct
;handler
;
	LDA OPT
	ASL A
	TAX 
	LDA DCOM,X
	STA JPLACE+1
	LDA DCOM+1,X
	STA JPLACE+2
JPLACE	JSR $FFFF
	LDA # >MYBUF 	;now print it
	LDY # <MYBUF
	JSR PRINT
	LDA PRFLG 	;printer also?
	BEQ KEY 	;no
;
;send to printer
;
	LDA #32 	;knock out
	STA MYBUF+28 	;arrows
	STA MYBUF+29
	LDX #$20 	;iocb 2
	LDA #9
	STA ICCOM,X
	LDA # >MYBUF
	STA ICBAH,X
	LDA # <MYBUF
	STA ICBAL,X
	LDA #40 	;40 chars
	STA ICBLL,X
	LDA #0
	STA ICBLH,X
	JSR CIOV
KEY	JSR PAUSE 	;check for pause
	BCS K5 	;exit on RET.
	JMP DCMD
K5	JMP INPUT
;
;1 byte binary to hex
;
HEX	PHA 
	AND #15
	TAX 
	LDA HEXDIG,X
	STA HEXS
	PLA 
	LSR A
	LSR A
	LSR A
	LSR A
	TAX 
	LDA HEXDIG,X
	STA HEXB
	RTS 
;
;put low byte operand into
;output buffer
;
PUTLOW	STY Y3 	;enter w/ Y=pos
	LDY #0
	LDA (MEM),Y
	JSR HEX
	LDY Y3 	;put next to
	LDA HEXB 	;mnemonic
	STA MYBUF,Y
	LDY #22 	;& at right
	STA MYBUF,Y
	LDY Y3
	INY 
	LDA HEXS
	STA MYBUF,Y
	LDY #23
	STA MYBUF,Y 	;fall through
;
;increase mem,mem+1 by 1
;
INCMEM	LDA MEM
	CLC 
	ADC #1
	STA MEM
	LDA MEM+1
	ADC #0
	STA MEM+1
	RTS 
;
;put high byte of operand
;into output buffer
;
PUTHI	STY Y3 	;enter y= pos.
	LDY #0
	LDA (MEM),Y
	JSR HEX 	;convert to hex
	LDY Y3
	LDA HEXB 	;next mnemonic
	STA MYBUF,Y
	LDY #25 	;and at right
	STA MYBUF,Y
	INY 
	LDA HEXS
	STA MYBUF,Y
	LDY Y3
	INY 
	STA MYBUF,Y
	JMP INCMEM 	;inc. mem by 1
;
;handlers for various modes
;of addressing
;
;immediate mode
;
IMMED	LDA #'#
	LDY #10
	STA MYBUF,Y
	INY 
	LDA #'$
	STA MYBUF,Y
	INY 
	JSR PUTLOW
	RTS 
;
;zero page
;
ZERO	LDA #'$
	LDY #11
	STA MYBUF,Y
	INY 
	JSR PUTLOW
	RTS 
;
;zero page,x
;
ZEROX	JSR ZERO
	LDA #',
	LDY #14
	STA MYBUF,Y
	INY 
	LDA #'X
	STA MYBUF,Y
	RTS 
;
;absolute
;
ABSOL	LDA #'$
	LDY #11
	STA MYBUF,Y
	LDY #14
	JSR PUTLOW
	LDY #12
	JSR PUTHI
	RTS 
;
;absoulte,x
;
ABSOLX	JSR ABSOL
	LDA #',
	LDY #16
	STA MYBUF,Y
	INY 
	LDA #'X
	STA MYBUF,Y
	RTS 
;
;absolute,y
;
ABSOLY	JSR ABSOLX
	LDY #17
	LDA #'Y
	STA MYBUF,Y
	RTS 
;
;preindexed with x
;
PREX	JSR LPAREN
	LDY #13
	JSR PUTLOW
	LDY #15
	LDA #',
	STA MYBUF,Y
	LDA #'X
	INY 
	STA MYBUF,Y
	LDA #')
	INY 
	STA MYBUF,Y
	RTS 
;
;post indexed with y
;
PREY	JSR PREX
	LDY #15
	LDA #')
	STA MYBUF,Y
	INY 
	LDA #',
	STA MYBUF,Y
	INY 
	LDA #'Y
	STA MYBUF,Y
	RTS 
;
;accumulator
;
ACCUM	LDA #'A
	LDY #11
	STA MYBUF,Y
	RTS 
;
;relative - branches
;
RELAT	LDA #27 	;escpae
	STA MYBUF+28
	LDA #28
	STA MYBUF+29 	;arrow
	LDY #0
	LDA (MEM),Y
	STA AC1 	;rel branch
	LDY #22 	;save it twice!
	JSR PUTLOW
	LDA MEM
	STA 207
	LDA MEM+1
	STA 208
	LDA AC1
	BMI BSUB
	CLC 
	ADC 207
	STA 207
	LDA 208
	ADC #0
	STA 208
	INC MYBUF+29
BPUT	LDA 207
	JSR HEX
	LDY #14
	LDA HEXB
	STA MYBUF,Y
	INY 
	LDA HEXS
	STA MYBUF,Y
	LDA 208
	JSR HEX
	LDY #12
	LDA HEXB
	STA MYBUF,Y
	INY 
	LDA HEXS
	STA MYBUF,Y
	LDY #11
	LDA #'$
	STA MYBUF,Y
	RTS 
BSUB	LDA #255
	SBC AC1
	CLC 
	ADC #2
	STA AC1
	LDA 207
	SEC 
	SBC AC1
	STA 207
	LDA 208
	SBC #0
	STA 208
	JMP BPUT
;
;implied is real easy
;
IMPLD	RTS 
;
;indirect
;
INDIR	JSR LPAREN
	LDY #15
	JSR PUTLOW
	LDY #13
	JSR PUTHI
	LDY #17
	LDA #')
	STA MYBUF,Y
	RTS 
;
;general put left parenthesis
;in buffer
;
LPAREN	LDY #11
	LDA #'(
	STA MYBUF,Y
	LDA #'$
	INY 
	STA MYBUF,Y
	RTS 
;
;zero page y
;
ZEROY	JSR ZEROX
	LDA #'Y
	STA MYBUF,Y
	RTS 
;
;no instruction ???
;
NOINST	RTS 
;
; binary save
;
;save a portion of memory
;
BSAVE	JSR DCLOSE 	;close iocb
	LDY #1
	LDA MYBUF,Y
	CMP #'D 	;check for
	BEQ SS1 	;D: - error if
SS9	JMP GOTERR 	;not there
SS1	INY 
	LDA MYBUF,Y
	CMP #':
	BNE SS9
SSB	INY 
	LDA MYBUF,Y
	CMP #', 	;find ','
	BEQ GOTCMA
	CPY #16 	;only 16 chars
	BNE SSB
	BEQ SSD 	;give error
GOTCMA	STY PRFLG 	;keep y pos
	INY 
	LDA MYBUF,Y 	;look for $
	CMP #'$
	BNE SS9
	INY 
	JSR PULLHEX
	LDA HCNT
	BEQ SS9 	;1-4 digs
	CMP #5
	BCC SSC
SSD	JMP GOTERR
SSC	LDA HL
	STA MEM
	STA PUTBUF+2
	LDA HH
	STA MEM+1
	STA PUTBUF+3
	LDA MYBUF,Y
	CMP #', 	;another ,
	BNE SSD
	INY 
	LDA MYBUF,Y
	CMP #'$ 	;hex coming up
	BNE SSD
	INY 
	JSR PULLHEX 	;get hex #
	LDA HCNT
	BEQ SSD
	CMP #5
	BCS SSD
	LDA HL 	;save low byte
	STA Y1 	;of hex add
	STA PUTBUF+4
	LDA HH 	;save hi byte
	STA Y2
	STA PUTBUF+5
	STA Y2 	;check if end
	CMP MEM+1 	;> start
	BEQ JJ2 	;chk low bytes
	BCS DOSUB 	;ok to subtract
JJ3	JMP PRADER 	;range error
JJ2	LDA Y1 	;comp low bytes
	CMP MEM
	BCC JJ3
DOSUB	SEC  	;get # bytes
	LDA Y1
	SBC MEM
	STA NBYT
	LDA Y2
	SBC MEM+1
	STA NBYT+1
	INC NBYT 	;add 1 to #
	BNE JJ7 	;of bytes
	INC NBYT+1
;
;open file for output
;
JJ7	LDX #DDEV
	LDA #3 	;open
	STA ICCOM,X
	LDA #EOL
	LDY PRFLG
	STA MYBUF,Y 	;put in eol
	LDA # >MYBUF+1
	STA ICBAH,X
	LDA # <MYBUF+1
	STA ICBAL,X
	LDA #8 	;output
	STA AUX1,X
	LDA #0
	STA AUX2,X
	JSR CIOV
	BPL JJ6
	JSR IOERR 	;I/O error
	JMP DERR 	;close file
;
;now do the save
;
JJ6	LDX #0 	;put headers
GG10	LDA PUTBUF,X 	;& start
	JSR PUTD 	;and end add
	INX 
	CPX #6
	BNE GG10
;
;now put all bytes
;
	LDX #DDEV 	;iocb 2
	LDA NBYT 	;# bytes to
	STA ICBLL,X 	;save
	LDA NBYT+1
	STA ICBLH,X
	LDA #$0B 	;put commd
	STA ICCOM,X
	LDA MEM 	;add where
	STA ICBAL,X 	;save starts
	LDA MEM+1
	STA ICBAH,X
	JSR CIOV
	BPL SVDONE 	;done
JJ4	JSR IOERR 	;error
	JMP DERR 	;close file
SVDONE	JSR DCLOSE 	;close #2
	JMP INPUT
;
;put 1 byte at a time
;
PUTD	STX X1 	;enter with A
	PHA  	;save it
	LDA #$0B 	;put
	LDX #DDEV 	;iocb #2
	STA ICCOM,X
	LDA #0 	;length = 0
	STA ICBLL,X
	STA ICBLH,X
	PLA  	;get byte
	JSR CIOV 	;put it
	BMI GG4 	;error
	LDX X1 	;no error-get x
	RTS 
GG4	STY Y1 	;show error
	PLA 
	PLA 
	JSR IOERR
	JMP INPUT
;
;'address range error'
;
PRADER	LDA # >ADDERR 	;print add
	LDY # <ADDERR 	;range error
	JSR PRINT
	JMP INPUT
;
;binary load
;
BLOAD	JSR DCLOSE 	;close iocb
	LDY #1
	LDA MYBUF,Y 	;check for D
	CMP #'D
	BEQ KK2
KK1	JMP GOTERR 	;show error
KK2	INY 
	LDA MYBUF,Y
	CMP #': 	;check for :
	BNE KK1
	LDA # >MYBUF+1 	;here is
	LDX #DDEV 	;device
	STA ICBAH,X 	;& name
	LDA # <MYBUF+1
	STA ICBAL,X
	LDA #4 	;read
	STA AUX1,X
	LDA #0
	STA AUX2,X
	LDA #3 	;open
	STA ICCOM,X
	JSR CIOV
	BPL KK3
	JSR IOERR 	;show I/O error
	JMP INPUT
KK3	JSR GETB 	;get 1 byte
	CMP #$FF 	;is it header?
	BNE LL1 	;no!
	JSR GETB 	;is 2nd # header
	CMP #$FF
	BEQ LL2
LL1	LDA # >NOTBIN 	;no binary
	LDY # <NOTBIN 	;file mess
	JSR PRINT
	JMP INPUT
LL2	JSR GETB 	;get load add
	STA MEM 	;low byte
	STA FR0
	JSR GETB 	;hi byte
	STA MEM+1
	STA FR0+1
	JSR GETB 	;get end add
	STA Y1 	;low byte
	JSR GETB 	;& hi byte
	STA Y2
	SEC  	;find number
	LDA Y1 	;of bytes to
	SBC MEM 	;load
	STA NBYT 	;save lo
	LDA Y2
	SBC MEM+1
	STA NBYT+1 	;save hi
	INC NBYT 	;increase by 1
	BNE LL6
	INC NBYT+1
LL6	JSR BIN2HEX 	;convert start
	LDY #0 	;add to hex
	LDX #12
LL5	LDA PRNBUF,Y 	;put in mess
	STA LDADR,X 	;buffer
	INX 
	INY 
	CPY #4
	BNE LL5
LL7	LDA # >LDADR 	;print 'loading
	LDY # <LDADR 	;at $0000'
	JSR PRINT
;
;now get all bytes
;
	LDX #DDEV 	;iocb 2
	LDA NBYT 	;# of bytes to
	STA ICBLL,X 	;get
	LDA NBYT+1
	STA ICBLH,X
	LDA #$07 	;get commd
	STA ICCOM,X
	LDA MEM 	;load add low
	STA ICBAL,X
	LDA MEM+1 	;load hi
	STA ICBAH,X
	JSR CIOV
	BPL LDONE
	JSR IOERR 	;show any error
LDONE	JSR DCLOSE 	;close iocb
	JMP INPUT
;
;get 1 byte from file
;
GETB	LDA #$07 	;get commd
	LDX #DDEV 	;iocb 2
	STA ICCOM,X
	LDA #0 	;only 1 at a
	STA ICBLL,X 	;time
	STA ICBLH,X
	JSR CIOV
	BMI KK4 	;if error
	RTS 
KK4	PLA  	;pull return
	PLA 
	JSR IOERR 	;show error
	JMP INPUT
;
;memory dump
;
MDUM	INY 
	JSR PULLHEX 	;get address
	LDA HCNT 	;1 - 4 digits
	BEQ MMA
	CMP #5
	BCC MMB
MMA	DEY  	;sho error
	JMP GOTERR
MMB	LDA HL 	;put address
	STA MEM 	;on screen
	LDA HH
	STA MEM+1
MMC	JSR PRADD
;
;fill buffer with blanks
;
	LDA #32
	LDX #44 	;do 44 spaces
ZLOOP	STA MYBUF,X
	DEX 
	BPL ZLOOP
;
;put 6 ATASCI characters
;in buffer
;
	LDY #0
	LDX #20
MMM	LDA (MEM),Y 	;do chars
	CMP #EOL 	;no ret
	BNE MMN 	;replace with
	LDA #27 	;!	;!!!!!!!! SPECIAL CHARS !!!!!!!!
MMN	STA MYBUF,X
	INX 
	INY 
	CPY #6 	;6 chars
	BNE MMM
	LDA #EOL 	;eol at end
	STA MYBUF,X
MMP	LDY #0 	;now do hex #s
	STY Y2 	;x cntr
MMO	STY Y1 	;y cntr
	LDA (MEM),Y 	;get bin #
	STA FR0 	;chng to hex
	JSR BIN2HEX
	LDY #2 	;get hex bytes
	LDA PRNBUF,Y
	LDX Y2
	STA MYBUF,X 	;store in
	INX  	;my buffer
	INY 
	LDA PRNBUF,Y
	STA MYBUF,X
	INX 
	INX 
	STX Y2
	LDY Y1
	INY 
	CPY #6
	BNE MMO
	LDA #1 	;set display
	STA $02FE 	;flag
	LDA # >MYBUF 	;print a line
	LDY # <MYBUF
	JSR PRINT
	LDA #0
	STA $02FE 	;reset dispflg
	LDA MEM
	CLC 
	ADC #6
	STA MEM 	;add 6 to mem
	LDA MEM+1
	ADC #0
	STA MEM+1
	JSR PAUSE 	;check for
	BCS MMR 	;pause-carry
	JMP MMC 	;set= RETURN
MMR	JMP INPUT
;
;general pause routine
;activated by any
;key press
;
PAUSE	LDA 764 	;check for
	CMP #12 	;key-RETURN
	BNE PPA 	;exits
PPE	LDA #$FF 	;any other
	STA 764 	;key pauses
	SEC 
	RTS 
PPA	CMP #$FF 	;if no key
	BNE PPB 	;no pause
PPD	CLC 
	RTS 
PPB	LDA #$FF 	;reset
	STA 764
PPC	LDA 764 	;if key pressed
	CMP #12 	;during pause
	BEQ PPE 	;either RETURN
	CMP #$FF 	;to abort
	BEQ PPC 	;or any key to
	LDA #$FF 	;continue
	STA 764
	BNE PPD
