;SAVE#D:BBKMON2.M65
;
;assembler
;
ASM	INY 
	JSR PULLHEX 	;get hex add
	LDA HCNT 	;if none error
	BEQ ZY1
	CMP #5
	BCC ZZ
ZY	DEY 
ZY1	JMP GOTERR 	;show error
ZZ	LDA HL 	;store add lo
	STA MEM
	LDA HH 	;& hi
	STA MEM+1
CMD	JSR PRADD 	;print add
	JSR GETLIN 	;get input
	JSR CRUNCH 	;crunch it
	LDY #0
	LDA MYBUF,Y
	CMP #EOL 	;RET. ?
	BNE FNM
	JMP INPUT
;
;find mnemonic
;
FNM	LDX #0 	;reset ptr
	STX CNT
	LDY #0
MN2	LDA MYBUF,Y 	;look up
	CMP MNE,X 	;in table
	BNE NOM2 	;1st char
	INX 
	INY 
	LDA MYBUF,Y 	;now check 2nd
	CMP MNE,X
	BNE NOM2
	INX 
	INY 
	LDA MYBUF,Y 	;3rd match?
	CMP MNE,X
	BEQ MAT
NOM2	LDY #0 	;no match
	INC CNT 	;try next item
	LDA CNT 	;in table
	CMP #56 	;only 55 entries
	BNE Z19
	JMP PERROR 	;error!
Z19	CLC 
	ASL A
	ADC CNT 	;x2
	TAX 
	BNE MN2
MAT	LDX CNT 	;get opc offset
	LDA OPC,X 	;get opc
	STA BASEOP 	;keep it
	LDX CNT 	;does mne with
	CPX #8 	;8 modes
	BCC Z21
	JMP TRY3 	;try next set
Z21	LDA #0 	;set ptrs
	STA MODE
	STA LPAR
	STA HL
	STA HH
	LDY #3 	;chk for '('
	LDA MYBUF,Y
	CMP #'(
	BNE Z1
	STA LPAR 	;set flag
Z1	CMP #'# 	;immediate?
	BNE Z2
	LDA #2 	;mode 2
	STA MODE
Z2	LDA MYBUF,Y 	;chk for '$'
	CMP #'$
	BEQ Z3
	INY  	;if we haven't
	CPY #$20 	;found it by
	BNE Z2 	;now-give error
	JMP PERROR
Z3	INY 
	JSR PULLHEX 	;get hex #
	LDA HCNT
	BEQ ZB 	;must have 1-4
	CMP #5 	;digits
	BCC Z7
ZB	JMP PERROR 	;error!
Z7	LDA MODE 	;immed?
	BEQ Z8
	JMP FIGOP2 	;figure opcode
Z8	LDA MYBUF,Y 	;zero/abs?
	CMP #EOL 	;RET?
	BNE Z9
	LDA HH
	BNE Z10 	;3 byter abs
	INC MODE 	;mode flag=1
	JMP FIGOP2 	;calc.opcode
Z10	LDA #3 	;mode =3
	STA MODE
	JMP FIGOP3 	;calc opcode
Z9	CMP #') 	;is it ..)?
	BNE Z11
	LDA #4 	;mode 4 by
	STA MODE 	;default
	JMP FIGOP2 	;figure it
Z11	CMP #', 	;need ','
	BNE Z12
	LDA LPAR 	;left ( ?
	BEQ Z18 	;no
	JMP FIGOP2 	;mode=0 default
Z18	LDA HH
	BNE Z13 	;not x-indexed
	LDA #5
	STA MODE 	;mode must be 5
	JMP FIGOP2 	;x indexed
Z12	JMP PERROR 	;error!
Z13	INY 
	LDA MYBUF,Y 	;is it 'y'
	CMP #'Y
	BNE Z14
	LDA #6 	;mode is 6
	STA MODE 	;abs,y
	JMP FIGOP3 	;find opcode
Z14	CMP #'X 	;is it 'x'?
	BEQ Z15
	JMP PERROR 	;error!
Z15	LDA HH 	;is it 3 byter
	BNE Z16
	LDA #5 	;mode =5
	STA MODE 	;zero x
	JMP FIGOP2 	;calculate
Z16	LDA #7 	;default = 7
	STA MODE
	JMP FIGOP3 	;find opcode
;
;try 1 byters
;
TRY3	LDX CNT 	;x has offset
	CPX #33
	BCS TRY4 	;try next set
	JMP FIGOP1 	;one byter
TRY4	LDX #0 	;next group
	STX HH 	;of basic
	STX HL 	;mnemonics
	STX MODE 	;1st rest ptrs
	LDX CNT
	CPX #48 	;bet 33 & 48?
	BCC MAT3
	CPX #56 	;if over 56
	BCC MAT4 	;show error
	JMP PERROR
MAT4	JMP TRY6
MAT3	TXA 
	SEC 
	SBC #33 	;subt 33 so
	STA CNT 	;cnt= 0-14
	LDA MYBUF+1,Y
	CMP #'A 	;is it Accum?
	BNE NOACC 	;no
	LDA CNT
	CMP #4 	;must be 0-4
	BCC C3
	JMP PERROR 	;error!
C3	TAX 
	LDA ACC,X 	;get opcode
	STA BASEOP 	;figure code
	JMP FIGOP1 	;from table
NOACC	LDX CNT 	;no Accum
	LDA OPC3,X 	;get opcode
	STA BASEOP 	;from table 3
	INY 
	LDA MYBUF,Y
	CMP #'# 	;immediate?
	BNE C4
	INY 
C15	LDA MYBUF,Y 	;is '$' there?
C4	CMP #'$
	BEQ C11
	CPX #10 	;is it jmp()?
	BNE C11
	INY 
	BNE C15
	JMP PERROR 	;error
C11	INY 
	JSR PULLHEX 	;now get hex #
	LDA HCNT
	BEQ C20 	;must have 1-4
	CMP #5 	;digs
	BCC C5
C20	JMP PERROR 	;error
C5	LDX CNT 	;chk 5-8
	CPX #8
	BCC C13
	JMP TRY5 	;try others
C13	LDA MYBUF,Y
	CMP #EOL 	;end yet?
	BNE C6
	LDA HH 	;3 byter?
	BNE C7
	JMP FIGDON2 	;default mode=0
C7	LDA #2 	;absolute mode
	STA MODE
	JMP THOPS 	;three byte
C6	CMP #', 	;',' ?
	BEQ C8
	JMP PERROR 	;if no,error
C8	LDA HH 	;3 byter?
	BNE C9
	INC MODE 	;zero,x
	JMP TWOPS 	;2 byter
C9	LDA #3 	;abs,x
	STA MODE
	JMP THOPS 	;2 byter
TRY5	CPX #9 	;10 or more?
	BCS D1
	LDA HH 	;3 byter?
	BNE D2
	JMP FIGDON2 	;bit-2 byte
D2	LDA #$2C
	STA BASEOP
D3	JMP FIGDON3 	;bit-3 byte
D1	CPX #9 	;=9?
	BEQ D3
	CPX #10 	;=10?
	BNE D4
	LDA MYBUF+3
	CMP #'( 	;left (?
	BNE D3 	;it is jmp
	LDA #$6C 	;must be jmp()
	STA BASEOP
	BNE D3
D4	CPX #13 	;is it type 13?
	BCS E1
	LDA MYBUF+3
	CMP #'# 	;immediate?
	BNE D5
D6	JMP FIGDON2 	;#
D5	LDA BASEOP
	ORA #$04
	STA BASEOP 	;add 4
	LDA HH 	;3 byter?
	BEQ D6 	;zero pg
	LDA BASEOP
	ORA #$08 	;add 8
	STA BASEOP
	JMP FIGDON3 	;do 3 byter
E1	LDA MYBUF+3
	CMP #'#
	BEQ D6 	;immed mode
	LDA MYBUF,Y
	CMP #EOL 	;end yet?
	BNE E2
	LDA HH 	;3 byter?
	BNE E3
	LDX #0
	JMP FIGA2
E3	LDX #2
	JMP FIGA3
E2	CMP #', 	;','?
	BEQ E4
	JMP PERROR 	;error
E4	LDA HH 	;3 byter?
	BNE E5
	LDX #1
	JMP FIGA2
E5	LDX #3 	;fall thru
FIGA3	LDA BASEOP 	;3 byter
	ORA OR5,X 	;modify base
	STA BASEOP 	;opcode
	JMP FIGDON3
FIGA2	LDA BASEOP 	;2 byter
	ORA OR5,X 	;modify base
	STA BASEOP 	;opcode
	JMP FIGDON2
TWOPS	LDX MODE 	;2 byter
	LDA BASEOP 	;diff. type
	ORA OR3,X 	;modify opcode
	STA BASEOP
	JMP FIGDON2
THOPS	LDX MODE 	;3 byter diff.
	LDA BASEOP 	;type
	ORA OR3,X
	STA BASEOP
	JMP FIGDON3
TRY6	LDX #0 	;reset vars
	STX HH
	STX X1
	STX HL
	INY 
	LDA MYBUF+3
	CMP #'$ 	;is it hex?
	BEQ W1
	JMP PERROR 	;error
W1	INY 
	JSR PULLHEX 	;get hex #
	LDA HCNT 	;must have
	BEQ W15 	;1-4 digits
	CMP #5
	BCC W2
W15	JMP PERROR 	;error
W2	LDX #0 	;must be branch
	STX X1 	;by default
	SEC  	;find out
	LDA HL 	;if branch
	SBC MEM 	;back or
	STA HL 	;forward
	LDA HH
	SBC MEM+1
	STA HH
	BCS W3 	;big# c=1
	INC X1
W3	SEC 
	LDA HL
	SBC #2
	STA HL
	BMI W8
	LDA X1
	BEQ W7
W9	JMP PERROR 	;range error
W7	JMP FIGDON2
W8	LDA HL
	CMP #$FE
	BCC W4
	INC X1
W4	LDA X1
	BNE W7
	BEQ W9
;
;error routine
;
PERROR	JSR BEEP 	;tone
	LDA # >ERMES 	;err mess
	LDY # <ERMES
	JSR PRINT 	;print it
	JMP CMD 	;get commd
;
;figure opcode
;
FIGOP2	LDX MODE
	LDA BASEOP
	ORA OR2,X
	STA BASEOP
;
;figuring done-put into
;memory
;
FIGDON2	LDA BASEOP 	;do 2 byter
	LDY #0 	;store opcode
	STA (MEM),Y
	LDA HL 	;store operand
	INY 
	STA (MEM),Y
	LDX #2 	;increase mem
	JMP ADDMEM 	;by 2
;
;figure 3 byter opcode
;
FIGOP3	LDX MODE 	;do 3 byter
	LDA BASEOP
	ORA OR2,X
	STA BASEOP
;
;figuring done-store in mem
;
FIGDON3	LDA BASEOP 	;do opcode
	LDY #0
	STA (MEM),Y
	INY 
	LDA HL 	;do low byte
	STA (MEM),Y
	INY 
	LDA HH 	;now high byte
	STA (MEM),Y
	LDX #3 	;move mem up 3
	JMP ADDMEM
;
;do 1 byter
;
FIGOP1	LDA BASEOP 	;store opcode
	LDY #0
	STA (MEM),Y
	LDX #1 	;inc. mem by 1
	JMP ADDMEM
;
;get up to 4 hex digits
;
PULLHEX	LDX #0 	;reset vars
	STX HL
	STX HH
	STX HCNT
PU1	LDA MYBUF,Y 	;get digit
	LDX #15
H2	CMP HEXDIG,X 	;in table?
	BEQ H3 	;yes
	DEX 
	BPL H2
	BMI H6 	;no
H3	STX X1 	;convert to bin
	ASL HL
	ROL HH 	;x 16
	ASL HL
	ROL HH
	ASL HL
	ROL HH
	ASL HL
	ROL HH
	CLC 
	LDA HL
	ADC X1
	STA HL
	LDA HH
	ADC #0
	STA HH
	INY 
	INC HCNT 	;1 more digit
	JMP PU1
H6	STY Y2
	RTS 
;
;increase mem with x
;
ADDMEM	CLC 
	TXA 
	ADC MEM
	STA MEM
	LDA MEM+1
	ADC #0
	STA MEM+1
	JMP CMD
;
;print address
;
PRADD	LDA #'$ 	;print'$'
	JSR PUTCH
	LDA MEM+1 	;high byte
	LSR A 	;convert to hex
	LSR A
	LSR A
	LSR A
	JSR PUTHX 	;put hex on scrn
	LDA MEM+1
	AND #$0F 	;now low nibble
	JSR PUTHX
	LDA MEM 	;now same
	LSR A 	;for low byte
	LSR A
	LSR A
	LSR A
	JSR PUTHX 	;high nibble
	LDA MEM
	AND #$0F
	JSR PUTHX 	;next low
	LDA #':
	JSR PUTCH
	LDA #32 	;space
	JSR PUTCH
	RTS 
;
;put hex digit on scrn
;
PUTHX	ORA #$30 	;add in bits
	CMP #58 	;add 7?
	BCC PUTCH 	;no
	CLC 
	ADC #7
;
;general put char
;
PUTCH	STX PUTX 	;save x
	LDX #0 	;buff len=0
	STX ICBLL
	STX ICBLH
	LDX #$0B 	;put cmd
	STX ICCOM
	LDX #0 	;iocb 0
	JSR CIOV 	;use Acc.
	BMI JERR 	;error
	LDX PUTX 	;restore x
	RTS  	;done
JERR	PLA  	;pull ret.
	PLA 
	JSR IOERR 	;show err
	JMP INPUT 	;get input
