;*********************************************************************       
;  SBC Firmware V5.1, 5-30-05, by Daryl Rictor
;  Modifications to run on T65 Core by Carsten Meyer, cm@ctmagazin.de
;*********************************************************************       
;

;#####################################################################
;Simulationsumgebung für Debugging
SimuSA		= 0	; läuft auch im Simulator Stand alone wenn <>0
	.IF SimuSA
	
	*=$F000
	.START MonitorBoot
	.OPT	Proc65c02, CaseInsensitive
	.ROM_AREA $F000, $FFFF
	
IO_AREA	= $8000		; set I/O area for this monitor

ACIAsimwr	= IO_AREA+$01	; simulated ACIA write port
ACIAsimrd	= IO_AREA+$04	; simulated ACIA read port
Reset = MonitorBoot

WriteScreen
	STA ACIAsimwr	; save byte to simulated ACIA
	RTS

; byte in from simulated ACIA
GetKey
	LDA ACIAsimrd	; get byte from simulated ACIA
	BEQ LAB_nobyw	; branch if no byte waiting
	SEC		; flag byte received
	RTS
LAB_nobyw
	CLC		; flag no byte received
	RTS
WaitKey
	;JSR GetKey
	LDA ACIAsimrd	; get byte from simulated ACIA
	BEQ WaitKey	; branch if no byte waiting
	RTS	
	.endif	
;#####################################################################

;               
; *************************************************************************
; kernal commands
; *************************************************************************
; PrintRegCR   - subroutine prints a CR, the register contents, CR, then returns
; PrintReg     - same as PrintRegCR without leading CR
; Print2Byte   - prints AAXX hex digits
; Print1Byte   - prints AA hex digits
; PrintDig     - prints A hex nibble (low 4 bits)
; Print_CR     - prints a CR (ASCII 13) and LF (ASCII 10)
; PrintXSP     - prints # of spaces in X Reg
; Print2SP     - prints 2 spaces
; Print1SP     - prints 1 space
; Input_assem  - Alternate input prompt for Assembler
; Input        - print <CR> and prompt then get a line of input, store at buffer
; Waitkey      - get one byte from input port, waits for input
; Getkey       - Checks for an input character (no waiting)
; Output       - send one byte to the output port
; Bell         - send ctrl-g (bell) to output port
; Delay        - delay loop
; *************************************************************************
;


RegData        .byte" PC=  A=  X=  Y=  S=  P= (NVRBDIZC)="
;
PrintReg       Jsr   Print_CR          ; Lead with a CR
               ldx   #$ff              ;
               ldy   #$ff              ;
Printreg1      iny                     ;
               lda   Regdata,y         ;
               jsr   WriteScreen          ;
               cmp   #$3D              ; "="
               bne   Printreg1         ;
Printreg2      inx                     ;
               cpx   #$07              ;
               beq   Printreg3         ; done with first 6
               lda   PCH,x             ;  
               jsr   Print1Byte        ;
               cpx   #$00              ;
               bne   Printreg1         ;
               JMP   Printreg2         ;
Printreg3      dex                     ;
               lda   PCH,x             ; get Preg
               ldx   #$08              ; 
Printreg4      rol                     ;
               tay                     ;
               lda   #$31              ;
               BCS   Printreg5         ;
               SEC
               SBC #1			; DEC Substitut
               CLC                     ;
Printreg5      jsr   WriteScreen            ;
               tya                     ;
               dex                     ;
               bne   Printreg4         ;
; fall into the print CR routine
Print_CR       PHA                     ; Save Acc
               LDA   #$0D              ; "cr"
               JSR   WriteScreen            ; send it
               LDA   #$0A              ; "lf"
               JSR   WriteScreen            ; send it
               PLA                     ; Restore Acc
               RTS                     ; 

Print2Byte     JSR   Print1Byte        ;  prints AAXX hex digits
               TXA                     ;
Print1Byte     PHA                     ;  prints AA hex digits
               LSR                     ;  MOVE UPPER NIBBLE TO LOWER
               LSR                     ;
               LSR                     ;
               LSR                     ;
               JSR   PrintDig          ;
               PLA                     ;
PrintDig       sty ytemp               ;  prints A hex nibble (low 4 bits)
               AND   #$0F              ;
               TAY                     ;
               LDA   Hexdigdata,Y      ;
               ldy ytemp                     ;
               jmp   WriteScreen            ;
PrintXSP1      JSR   Print1SP          ;
               dex                     ;
PrintXSP       cpx   #$00              ;
               bne   PrintXSP1         ;
               rts                     ;
Print2SP       jsr   Print1SP          ; print 2 SPACES
Print1SP       LDA   #$20              ; print 1 SPACE
               JMP   WriteScreen            ;
;
Input          lda   #'*'              ; Monitor Prompt "*"
               sta   Prompt            ; save prompt chr 
Input1         jsr   Print_CR          ; New Line
               lda   Prompt            ; get prompt
               jsr   WriteScreen            ; Print Prompt
               ldy   #$ff              ; pointer
InputWait      JSR   Waitkey         ; get a character
               cmp   #$20              ; is ctrl char?
               BPL   InputSave         ; no, echo chr 
               cmp   #$0d              ; cr
               Beq   InputDone         ; done
               cmp   #$1B              ; esc
               beq   Input1            ; cancel and new line
               cmp   #$08              ; bs
               beq   backspace         ;
               cmp   #$02              ; Ctrl-B
               bne   InputWait         ; Ignore other codes
               
               brk                     ; Force a keyboard Break cmd
backspace      cpy   #$ff              ;
               beq   InputWait         ; nothing to do
               dey                     ; remove last char
               Lda   #$08              ; backup one space
               jsr   WriteScreen          ;
               Lda   #$20              ; Print space (destructive BS)
               jsr   WriteScreen          ;
               Lda   #$08              ; backup one space
               jsr   WriteScreen          ;
               JMP   InputWait         ; ready for next key
InputSave      CMP   #$61              ; ucase
               BCC   InputSave1        ;
               SBC   #$20              ;
InputSave1     INY                     ;
               BMI   InputTooLong      ; get next char (up to 127)
               STA   Buffer,y          ;
               JSR   WriteScreen          ; OutputCharacter
               JMP   InputWait         ;
InputDone      INY                     ;
InputTooLong   LDA   #$0d              ; force CR at end of 128 characters 
               sta   Buffer,y          ;
               JSR   WriteScreen          ;
               lda   #$0a              ; lf Char   
               JSR   WriteScreen          ;
               RTS
;
MonExit:
bell           RTS
;

;*************************************************************************
;     
;  Monitor Program 
;
;*************************************************************************
Break:		; BRK Handler (JMP Break)
	STA ACC		; für Monitor
	LDA #5
	STA Color
	STX Xreg	; save X for monitor
	STY Yreg	; save Y	
	pla		; 
	STA Preg	; save P status
	PLA		; PCL
	STA PCL
	PLA		; PCH
	STA PCH
	TAX
	LDA PCL		; PCL
	SEC		; 2 subtrahieren für echte BRK-Adresse
	SBC #$02	;
	STA PCL		; backup to BRK cmd
	BCS Brk2	;
	DEX		;
Brk2:	STX PCH		; save PC
	LDA irqa	; geretteter Acc
	TSX		; get stack pointer
	STX SPtr	; save stack pointer
	JSR PrintReg	; dump register contents
	PLP
	PHP
	JMP Monitor	; start the monitor

MonitorBoot    
	LDA #5
	STA Color
               JSR   Version           ;
Monitor        LDX   #$FF              ; 
               TXS                     ;  Init the stack
               JSR   input             ;  line input
               LDA   #$00              ;
               TAY                     ;  set to 1st character in line
               sta   LineCnt           ;  normal list vs range list 
Mon01          STA   Memchr            ;
Mon02          LDX #0
	       STX   Hexdigits         ;  holds parsed hex
               STX   Hexdigits+1       ;
               JSR   ParseHexDig       ;  Get any Hex chars
               LDX   #CmdCount         ;  get # of cmds currently used
Mon08          CMP   CmdAscii,X        ;  is non hex cmd chr?
               BEQ   Mon09             ;  yes x= cmd number
               DEX                     ;
               BPL   Mon08             ;
               JMP   Monitor           ;  no
Mon09
	stx xtemp2	;  save command
	sty ytemp2	;  Save input line pointer
	TXA		;
	ASL		;  ptr * 2
	TAX
	JSR   Mon10	;  Execute cmd
	LDY ytemp2	;
	ldx xtemp2	;
               BEQ   Monitor           ;  done
               LDA   Cmdseccode,X      ;  
               BMI   Mon02             ;
               JMP   Mon01             ;
Mon10
	LDA Cmdjmptbl,X
	STA Cmdptr
	INX
	LDA Cmdjmptbl,X
	STA Cmdptr_H
	JMP (Cmdptr)		;6502 für JMP (Cmdjmptbl,X)     ;
	
;--------------- Routines used by the Monitor commands ----------------------

ParseHexDig    PHA
	LDA #0
	STA   Hexdigcnt         ;  cntr
	PLA
               JMP   ParseHex05        ;
ParseHex03     TXA                     ;  parse hex dig
               LDX   #$04              ;  
ParseHex04     ASL   Hexdigits         ;
               ROL   Hexdigits+1       ;
               DEX                     ;
               BNE   ParseHex04        ;
	PHA
	ORA Hexdigits 
	STA Hexdigits
	PLA	; was "TSB   Hexdigits"
               DEC   Hexdigcnt         ;
ParseHex05     LDA   buffer,Y          ;
               LDX   #$0F              ;   is hex chr?
               INY                     ;
ParseHex07     CMP   Hexdigdata,X      ;
               BEQ   ParseHex03        ;   yes
               DEX                     ;
               BPL   ParseHex07        ;
               RTS                     ; Stored in HexDigits if HexDigCnt <> 0
;
Help_cmd       lda   #<Helptxt         ;  lower byte - Menu of Commands
               sta   addrptr           ;
               lda   #>Helptxt         ;  upper byte
               sta   addrptr+1         ;
               JMP   Help_cmd3         ;
Help_Cmd4      cmp   #$7e              ;  "~"
               beq   Help_Cmd1         ;
               jsr   WriteScreen            ;
               JMP   Help_cmd2         ;
Help_cmd1      jsr   Print_CR          ;     
Help_cmd2      jsr   Inc_addrptr       ;
Help_cmd3      ldy #0
	LDA   (addrptr),y              ;  was "LDA   (addrptr)"
               bne   Help_cmd4         ;
               rts                     ;
Version        jsr   Print_CR          ; 
               ldx   #$FF              ; set txt pointer
               lda   #$0d              ; 
PortReadyMsg   inx                     ;
               JSR   WriteScreen          ; put character to Port
               lda   porttxt,x         ; get message text
               bne   PortReadyMsg      ; 
               rts                     ;
;
Excute_cmd     jsr   exe1              ;
               ldx   #$FF              ; reset stack
               txs                     ;
               jmp   Monitor           ;
exe1           
               JMP   (Hexdigits)       ;
;
DOT_cmd        LDX   Hexdigits         ; move address to addrptr
               LDA   Hexdigits+1       ;
               STX   Addrptr           ;
               STA   Addrptr+1         ;
               inc   LineCnt           ; range list command
               RTS                     ;
;
CR_cmd         CPY   #$01              ;
               BNE   SP_cmd            ;
               LDA   Addrptr           ; CR alone - move addrptr to hexdigits
               ORA   #$0F              ;  to simulate entering an address
               STA   Hexdigits         ; *** change 07 to 0f for 16 byte/line
               LDA   Addrptr+1         ;
               STA   Hexdigits+1       ;
	JMP   SP_cmd2           ;
SP_cmd         LDA   Hexdigcnt         ; Space command entry
               BEQ   SP_cmd5           ; any digits to process? no - done
               LDX   Memchr            ; yes - is sec cmd code 0 ? yes - 
               BEQ   SP_cmd1           ; yes - 
               DEX                     ; Is sec cmd = 1?       
               BEQ   SP_cmd3           ;       yes - is sec cmd code 1 ?
               LDA   Hexdigits         ;             no - ":" cmd processed
	PHX
	LDX #0
	STA   (Addrptr,x)              ;  was "STA   (addrptr)"
	PLX
               JMP   Inc_addrptr       ; set to next address and return
SP_cmd1        JSR   DOT_cmd           ; sec dig = 0  move address to addrptr
               JMP   SP_cmd3           ;
SP_cmd2        LDA   Addrptr           ; CR cmd entry 

;              BIT   #$0F              ; *** changed 07 to 0F for 16 bytes/line
               AND   #$0F
               BEQ   SP_cmd3           ; if 16, print new line
               cpy   #$00              ; if TXT cmd, don"t print the - or spaces between chrs
               beq   TXT_cmd1          ;
;              BIT   #$07              ; if 8, print -
               LDA   Addrptr
               AND   #$07
               BEQ   SP_cmd33          ;
               JMP   SP_cmd4           ; else print next byte
SP_cmd3        JSR   Print_CR          ; "." cmd - display address and data 
               JSR   Getkey        ; see if brk requested
               bcs   SP_brk            ; if so, stop 
               LDA   Addrptr+1         ; print address
               LDX   Addrptr           ;
               JSR   Print2Byte        ;
SP_cmd33       LDA   #$20              ; " " print 1 - 16 bytes of data
               JSR   WriteScreen            ;
               LDA   #$2D              ; "-"
               JSR   WriteScreen            ;
SP_cmd4        LDA   #$20              ; " " 
               JSR   WriteScreen            ;
               cpy   #$00              ;
               BEQ   TXT_Cmd1          ;
	PHX
	LDX #0
	LDA   (Addrptr,X)              ;  was "LDA   (addrptr)"
	PLX
               JSR   Print1Byte        ;
SP_cmd44       SEC                     ;  checks if range done
               LDA   Addrptr           ;
               SBC   Hexdigits         ;
               LDA   Addrptr+1         ;
               SBC   Hexdigits+1       ;
               jsr   Inc_addrptr       ;
               BCC   SP_cmd2           ; loop until range done
SP_brk:
	LDA #0         
	STA   Memchr            ; reset sec cmd code
SP_cmd5   
	LDX #0
	RTS                     ; done or no digits to process
	
TXT_Cmd
	sty ytemp2                     ;
               ldy   #$00              ;
               jsr   SP_cmd            ;
	ldy ytemp2                    ;
               RTS                     ;
TXT_cmd1
	PHX
	LDX #0	
	LDA   (Addrptr,X)         ;
	PLX
               AND   #$7F              ;
               CMP   #$7F              ;
               BEQ   TXT_Cmd2          ;
               CMP   #$20              ; " "
               BCS   TXT_Cmd3          ;
TXT_Cmd2       LDA   #$2E              ; "." use "." if not printable char
TXT_Cmd3       JSR   WriteScreen          ;
               JMP   SP_cmd44          ;
;
Inc_addrptr    INC   Addrptr           ;  increments addrptr
               BNE   Inc_addr1         ;
               INC   Addrptr+1         ;
Inc_addr1      RTS                     ;
;
Insert_cmd     lda   Linecnt           ;  "I" cmd code
               beq   Insert_3          ; abort if no . cmd entered
               sec                     ;
               lda   Hexdigits         ;
               sbc   addrptr           ;
               tax                     ;
               lda   Hexdigits+1       ;
               sbc   addrptr+1         ;
               tay                     ;
               bcc   Insert_3          ;
               clc                     ;
               txa                     ;
               adc   memptr            ;
               sta   hexdigits         ;
               tya                     ;
               adc   memptr+1          ;
               STA   hexdigits+1 
Insert_0       ;
	PHX
	LDX #0
	LDA   (memptr,x)          ;
	STA   (Hexdigits,x)       ;
 	PLX
              lda   #$FF              ;
               DEC   Hexdigits         ;  
               cmp   Hexdigits         ;  
               BNE   Insert_1          ;
               DEC   Hexdigits+1       ;
Insert_1       dec   Memptr            ;  
               cmp   Memptr            ;
               bne   Insert_2          ;
               dec   Memptr+1          ;
Insert_2       SEC                     ;  
               LDA   memptr            ;
               SBC   Addrptr           ;
               LDA   memptr+1          ;
               SBC   Addrptr+1         ;
               bcc   Insert_3          ;
               jsr   Getkey        ; see if brk requested
               bcc   Insert_0          ; if so, stop List
Insert_3       RTS                     ;
;
Move_cmd       lda   Linecnt           ; *** any changes to this routine affect EEPROM_WR too!!!
               bne   Move_cmd3         ; abort if no . cmd was used
Move_brk       RTS                     ;
Move_cmd1      INC   Addrptr           ;  increments addrptr
               BNE   Move_cmd2         ;
               INC   Addrptr+1         ;
Move_cmd2      inc   Hexdigits         ;  "M" cmd code
               bne   Move_cmd3         ;
               inc   Hexdigits+1       ;
Move_cmd3      SEC                     ;  checks if range done
               LDA   Memptr            ;
               SBC   Addrptr           ;
               LDA   Memptr+1          ;
               SBC   Addrptr+1         ;
               BCC   Move_brk          ;  exit if range done
               jsr   Getkey        ; see if brk requested
               bcs   Move_brk          ; 
               
	PHX
	LDX #0
	LDA   (Addrptr,x)	; LDA   (Addrptr) ;  Moves one byte
	STA   (Hexdigits,x)	; STA   (Hexdigits)
 	PLX

               JMP   Move_cmd1         ; (zapped after move from eeprom_wr)
;
Dest_cmd       LDX   Hexdigits         ;  ">" cmd code
               LDA   Hexdigits+1       ;
               STX   Memptr            ;  move address to memptr
               STA   Memptr+1          ;
               RTS                     ;  
                                       ;
LIST_cmd       lda   LineCnt           ;  Check for normal/range
               beq   List_cmd_1        ;  0 = normal  1=range 
               LDA   Addrptr           ;  Dissassemble range of code
               LDX   Addrptr+1         ;  move addrptr to startaddr
               STA   Startaddr         ;
               STX   Startaddr+1       ;
List_range     sec                     ;
               lda   Startaddr         ;
               sbc   Addrptr           ;
               lda   Startaddr+1       ; 
               sbc   Addrptr+1         ;
               bcc   List_done         ;
               jsr   List_Line         ;  list one line
               jsr   Getkey            ;  see if brk requested
               bcs   List_done         ;  if so, stop List
               SEC                     ;  checks if range done
               LDA   Hexdigits         ;
               SBC   Startaddr         ;
               LDA   Hexdigits+1       ;
               SBC   Startaddr+1       ;
               BCS   List_range        ;  if not, loop until done
	PHA
	LDA #0
	STA   LineCnt           ; was "STZ LineCnt"
	PLA
List_done      RTS                     ;
List_cmd_1     LDA   Hexdigcnt         ; Dissassemble one page of cmds
               BEQ   List1             ; followed with more pages 
               LDX   Hexdigits         ;
               LDA   Hexdigits+1       ;
               STX   Startaddr         ;
               STA   Startaddr+1       ;
List1          LDA   #$10              ; one page of dissassembly
               STA   Linecnt           ;
List2          JSR   List_line         ;
               DEC   Linecnt           ;
               BNE   List2             ;
               RTS                     ;
List_line      JSR   Print_CR          ; 
               JSR   List_one          ; one line of dissassembly
               CLC                     ;
               LDA   Startaddr         ; inc address pointer to next cmd
               ADC   Hexdigcnt         ;
               STA   Startaddr         ;
               BCC   List3             ;
               INC   Startaddr+1       ;
List3          RTS                     ;
List_one       
	LDX #0		; was "LDA (Startaddr)"
	LDA   (Startaddr,X)       ; Dissassemble one CMD from Startaddr
               TAX                     ; Initialize List Cmd pointers
               LDA   OPCtxtidx,X       ;
               STA   OPCtxtptr         ;
               LDA   OPCaddmode,X      ;
               AND   #$0F              ; mask out pseudo-modes
               STA   Modejmp           ;
               TAX                     ;
               LDA   ModeByteCnt,X     ;
               STA   Hexdigcnt         ;
               LDA   Startaddr+1       ;
               LDX   Startaddr         ;
               JSR   Print2Byte        ; print address 
               LDA   #$2D              ;  "-"
               JSR   WriteScreen            ;
               JSR   Print2SP          ; print "  "
               LDX   #$01              ; ---------
List4          LDY   #$00              ; print up to 3 ascii chars...
List5          CPY   Hexdigcnt         ;  two spaces...
               BCS   List6             ;  up to three hex chars...
               LDA   (Startaddr),Y     ;  two spaces
               CPX   #$00              ;
               BNE   List8             ;
               JSR   Print1Byte        ;
               JMP   List7             ;
List6          CPX   #$00              ;
               BNE   List7             ;
               JSR   Print2SP          ;
List7          LDA   #$20              ; " "
List8          AND   #$7F              ;
               CMP   #$7F              ;
               BEQ   List9             ;
               CMP   #$20              ; " "
               BCS   List10            ;
List9          LDA   #$2E              ; "." use "." if not printable char
List10         JSR   WriteScreen            ;
               INY                     ;
               CPY   #$03              ;
               BCC   List5             ;
               JSR   Print2SP          ;
               DEX                     ;
               BEQ   List4             ;---------
               LDA   OPCtxtptr         ; get opcode text
               ASL                     ;
               ADC   OPCtxtptr         ;
               TAX                     ;
               LDY   #$FD              ;
List11         LDA   OPCtxtData,X      ;
               JSR   WriteScreen            ; print opcode text
               INX                     ;
               INY                     ;
               BNE   List11            ;
               LDA   OPCtxtptr         ;
               CMP   #$42              ; 4chr opcodes start
               BMI   List12		   ;
               CMP   #$46              ; the .xx cmds
               BPL   List12            ; 
               
	PHX	; LDA(startaddr)
	LDX #0	
	LDA (Startaddr,X)       ; Dissassemble one CMD from Startaddr
	PLX
               lsr
               lsr
               lsr
               lsr
               AND   #$07              ; strip last 3 bits
               ora   #$30              ; add CHR '0'
               jsr   WriteScreen            ; print it
               lda   #$20              ; " "
               jsr   WriteScreen            ; 
               jmp   List13            ;
List12         JSR   Print2SP          ;
List13         LDA   Modejmp           ; setup to print operand
               ASL                     ;
               TAX                     ;
	LDA ModeJmpTbl,X
	STA Cmdptr
	INX
	LDA ModeJmpTbl,X
	STA Cmdptr+1
	jmp (Cmdptr)            

IMM_mode       LDA   #$23              ; print #$HH
               JSR   WriteScreen            ;
ZP_mode        LDA   #$24              ; print $HH
               JSR   WriteScreen            ;
               LDY   #$01              ;
Byte_mode      LDA   (Startaddr),Y     ;
               JMP   Print1Byte        ;
ZP_X_mode      JSR   ZP_mode           ; print $HH,X
X_mode         LDA   #$2C              ; print ,X
               JSR   WriteScreen            ;
               LDA   #$58              ; 
               JMP   WriteScreen            ;
ZP_Y_mode      JSR   ZP_mode           ; print $HH,Y
Y_mode         LDA   #$2C              ; Print ,Y
               JSR   WriteScreen            ;
               LDA   #$59              ; 
               JMP   WriteScreen            ;
INDZP_mode     JSR   IND0_mode         ; Print ($HH)
               JSR   ZP_mode           ;
IND1_mode      LDA   #$29              ; Print )
               JMP   WriteScreen            ;
INDZP_X_mode   JSR   IND0_mode         ; Print ($HH,X)
               JSR   ZP_mode           ;
               JSR   X_mode            ;
               JMP   IND1_mode         ;
INDZP_Y_mode   JSR   INDZP_mode        ; Print ($HH),Y
               JMP   Y_mode            ;
ABS_mode       LDA   #$24              ; Print $HHHH
               JSR   WriteScreen            ;
               LDY   #$02              ;
               JSR   Byte_mode         ;
               DEY                     ;
               JMP   Byte_mode         ;
ABS_X_mode     JSR   ABS_mode          ; Print $HHHH,X
               JMP   X_mode            ;
ABS_Y_mode     JSR   ABS_mode          ; Print $HHHH,Y
               JMP   Y_mode            ;
INDABS_mode    JSR   IND0_mode         ; Print ($HHHH)
               JSR   ABS_mode          ;
               JMP   IND1_mode         ;
INDABSX_mode   JSR   IND0_mode         ; Print ($HHHH,X)
               JSR   ABS_mode          ;
               JSR   X_mode            ;
               JMP   IND1_mode         ;
IMPLIED_mode   RTS                     ; Implied/Accumulator mode 
IND0_mode      LDA   #$28              ; Print (
               JMP   WriteScreen            ;
BBREL_mode     JSR   ZP_mode		   ;
		   LDA   #$2C              ; Print ,
               JSR   WriteScreen            ;
               LDA   #$24              ; Print $
               JSR   WriteScreen            ;
               LDY   #$02		   ;
               LDA   (Startaddr),Y     ;
               STA   Memchr            ;
               CLC                     ;
               LDA   Startaddr         ;
               ADC   #$03              ;
               JMP   REL_mode0         ;
REL_mode       LDA   #$24              ; Print $HHHH as Relative Branch
               JSR   WriteScreen            ;
               LDY   #$01              ;
               LDA   (Startaddr),Y     ;
               STA   Memchr            ;
               CLC                     ;
               LDA   Startaddr         ;
               ADC   #$02              ;
REL_mode0      TAX                     ;
               LDA   Startaddr+1       ;
               ADC   #$00              ;
               TAY                     ;
               CLC                     ;
               TXA                     ;
               ADC   Memchr            ;
               TAX                     ;
               TYA                     ;
               LDY   Memchr            ;
               BPL   Rel_mode1         ;
               
	DEA
;	STX xtemp
;	TAX
;	DEX
;	TXA
;	LDX xtemp	; Ersatz für DEA
	
Rel_mode1      ADC   #$00              ;
               JMP   Print2Byte        ;

;
Opcode_List    ldy   #$49              ; Number of Opcodes (64)
               ldx   #$00              ; pointer to characters
Opcode_List1   txa                     ; 
               and   #$0F              ; Print CR after each 16 opcodes 
               bne   Opcode_List2      ; not divisible by 16
               jsr   Print_CR          ;
Opcode_List2   lda   OPCtxtData,x      ; get opcode chr data
               jsr   WriteScreen            ; print 1st char
               inx                     ;
               lda   OPCtxtData,x      ; 
               jsr   WriteScreen            ; print 2nd char
               inx                     ;
               lda   OPCtxtData,x      ;
               jsr   WriteScreen            ; print 3rd char
               inx                     ;
Opcode_List3   lda   #$20              ; print space
               jsr   WriteScreen            ;
               dey                     ;
               bne   Opcode_List1      ; 
               jsr   Print_CR          ; one last CR-LF
               rts                     ;
;
;-----------DATA TABLES ------------------------------------------------
;
Hexdigdata     .byte "0123456789ABCDEF";hex char table 
;     
CmdCount       =13                     ; number of commands to scan for
CmdAscii       .byte $0D               ; 0 enter    cmd codes
               .byte $20               ; 1 SPACE
               .byte $2E               ; 2 .
               .byte $3A               ; 3 :
               .byte $3E               ; 4 >  
               .byte $3f               ; 5 ? - Help
               .byte $47               ; 7 g - Go
               .byte $49               ; 8 i - Insert
               .byte $4C               ; 9 l - List
               .byte $4D               ; A m - Move
               .byte $51               ; B q - Query memory (text dump)
               .byte $52               ; C r - Registers
               .byte 'X'               ; D x - Cold Start Basic
               .byte $56               ;10 v - Version
;     
Cmdjmptbl      .word CR_cmd            ; 0   enter   cmd jmp table
               .word SP_cmd            ; 1   space
               .word DOT_cmd           ; 2    .
               .word DOT_cmd           ; 3    :
               .word Dest_cmd          ; 4    >  
               .word Help_Cmd          ; 5    ?
               .word Excute_cmd        ; 7    g
               .word Insert_Cmd        ; 8    i
               .word LIST_cmd          ; 9    l
               .word Move_cmd          ; A    m
               .word TXT_cmd           ; B    q
               .word Printreg          ; C    r
               .word Reset             ; D    x
               .word Version           ;10    v
;     
Cmdseccode     .byte $00               ; 0   enter       secondary command table
               .byte $FF               ; 1   sp
               .byte $01               ; 2   .
               .byte $02               ; 3   :
               .byte $00               ; 4   > 
               .byte $00               ; 5   ?
               .byte $00               ; 7   g
               .byte $00               ; 8   i
               .byte $00               ; 9   l
               .byte $00               ; A   m
               .byte $00               ; B   q
               .byte $00               ; C   r
               .byte $00               ; D   @
               .byte $00               ;10   v
;
;     
OPCtxtidx      .byte $0B               ;0   operand text index
               .byte $23               ;1
               .byte $49               ;2
               .byte $49               ;3
               .byte $3B               ;4
               .byte $23               ;5
               .byte $02               ;6
               .byte $44               ;7
               .byte $25               ;8
               .byte $23               ;9
               .byte $02               ;A
               .byte $49               ;B
               .byte $3B               ;C
               .byte $23               ;D
               .byte $02               ;E
               .byte $42               ;F
               .byte $09               ;10
               .byte $23               ;11
               .byte $23               ;12
               .byte $49               ;13
               .byte $3A               ;14
               .byte $23               ;15
               .byte $02               ;16
               .byte $44               ;17
               .byte $0E               ;18
               .byte $23               ;19
               .byte $19               ;1A
               .byte $49               ;1B
               .byte $3A               ;1C
               .byte $23               ;1D
               .byte $02               ;1E
               .byte $42               ;1F
               .byte $1D               ;20
               .byte $01               ;21
               .byte $49               ;22
               .byte $49               ;23
               .byte $06               ;24
               .byte $01               ;25
               .byte $2C               ;26
               .byte $44               ;27
               .byte $29               ;28
               .byte $01               ;29
               .byte $2C               ;2A
               .byte $49               ;2B
               .byte $06               ;2C
               .byte $01               ;2D
               .byte $2C               ;2E
               .byte $42               ;2F
               .byte $07               ;30
               .byte $01               ;31
               .byte $01               ;32
               .byte $49               ;33
               .byte $06               ;34
               .byte $01               ;35
               .byte $2C               ;36
               .byte $44               ;37
               .byte $31               ;38
               .byte $01               ;39
               .byte $15               ;3A
               .byte $49               ;3B
               .byte $06               ;3C
               .byte $01               ;3D
               .byte $2C               ;3E
               .byte $42               ;3F
               .byte $2E               ;40
               .byte $18               ;41
               .byte $49               ;42
               .byte $49               ;43
               .byte $49               ;44
               .byte $18               ;45
               .byte $21               ;46
               .byte $44               ;47
               .byte $24               ;48
               .byte $18               ;49
               .byte $21               ;4A
               .byte $49               ;4B
               .byte $1C               ;4C
               .byte $18               ;4D
               .byte $21               ;4E
               .byte $42               ;4F
               .byte $0C               ;50
               .byte $18               ;51
               .byte $18               ;52
               .byte $49               ;53
               .byte $49               ;54
               .byte $18               ;55
               .byte $21               ;56
               .byte $44               ;57
               .byte $10               ;58
               .byte $18               ;59
               .byte $27               ;5A
               .byte $49               ;5B
               .byte $49               ;5C
               .byte $18               ;5D
               .byte $21               ;5E
               .byte $42               ;5F
               .byte $2F               ;60
               .byte $00               ;61
               .byte $49               ;62
               .byte $49               ;63
               .byte $37               ;64
               .byte $00               ;65
               .byte $2D               ;66
               .byte $44               ;67
               .byte $28               ;68
               .byte $00               ;69
               .byte $2D               ;6A
               .byte $49               ;6B
               .byte $1C               ;6C
               .byte $00               ;6D
               .byte $2D               ;6E
               .byte $42               ;6F
               .byte $0D               ;70
               .byte $00               ;71
               .byte $00               ;72
               .byte $49               ;73
               .byte $37               ;74
               .byte $00               ;75
               .byte $2D               ;76
               .byte $44               ;77
               .byte $33               ;78
               .byte $00               ;79
               .byte $2B               ;7A
               .byte $49               ;7B
               .byte $1C               ;7C
               .byte $00               ;7D
               .byte $2D               ;7E
               .byte $42               ;7F
               .byte $0A               ;80
               .byte $34               ;81
               .byte $49               ;82
               .byte $49               ;83
               .byte $36               ;84
               .byte $34               ;85
               .byte $35               ;86
               .byte $45               ;87
               .byte $17               ;88
               .byte $06               ;89
               .byte $3D               ;8A
               .byte $49               ;8B
               .byte $36               ;8C
               .byte $34               ;8D
               .byte $35               ;8E
               .byte $43               ;8F
               .byte $03               ;90
               .byte $34               ;91
               .byte $34               ;92
               .byte $49               ;93
               .byte $36               ;94
               .byte $34               ;95
               .byte $35               ;96
               .byte $45               ;97
               .byte $3F               ;98
               .byte $34               ;99
               .byte $3E               ;9A
               .byte $49               ;9B
               .byte $37               ;9C
               .byte $34               ;9D
               .byte $37               ;9E
               .byte $43               ;9F
               .byte $20               ;A0
               .byte $1E               ;A1
               .byte $1F               ;A2
               .byte $49               ;A3
               .byte $20               ;A4
               .byte $1E               ;A5
               .byte $1F               ;A6
               .byte $45               ;A7
               .byte $39               ;A8
               .byte $1E               ;A9
               .byte $38               ;AA
               .byte $49               ;AB
               .byte $20               ;AC
               .byte $1E               ;AD
               .byte $1F               ;AE
               .byte $43               ;AF
               .byte $04               ;B0
               .byte $1E               ;B1
               .byte $1E               ;B2
               .byte $49               ;B3
               .byte $20               ;B4
               .byte $1E               ;B5
               .byte $1F               ;B6
               .byte $45               ;B7
               .byte $11               ;B8
               .byte $1E               ;B9
               .byte $3C               ;BA
               .byte $49               ;BB
               .byte $20               ;BC
               .byte $1E               ;BD
               .byte $1F               ;BE
               .byte $43               ;BF
               .byte $14               ;C0
               .byte $12               ;C1
               .byte $49               ;C2
               .byte $49               ;C3
               .byte $14               ;C4
               .byte $12               ;C5
               .byte $15               ;C6
               .byte $45               ;C7
               .byte $1B               ;C8
               .byte $12               ;C9
               .byte $16               ;CA
               .byte $40               ;CB
               .byte $14               ;CC
               .byte $12               ;CD
               .byte $15               ;CE
               .byte $43               ;CF
               .byte $08               ;D0
               .byte $12               ;D1
               .byte $12               ;D2
               .byte $49               ;D3
               .byte $49               ;D4
               .byte $12               ;D5
               .byte $15               ;D6
               .byte $45               ;D7
               .byte $0F               ;D8
               .byte $12               ;D9
               .byte $26               ;DA
               .byte $41               ;DB
               .byte $49               ;DC
               .byte $12               ;DD
               .byte $15               ;DE
               .byte $43               ;DF
               .byte $13               ;E0
               .byte $30               ;E1
               .byte $49               ;E2
               .byte $49               ;E3
               .byte $13               ;E4
               .byte $30               ;E5
               .byte $19               ;E6
               .byte $45               ;E7
               .byte $1A               ;E8
               .byte $30               ;E9
               .byte $22               ;EA
               .byte $49               ;EB
               .byte $13               ;EC
               .byte $30               ;ED
               .byte $19               ;EE
               .byte $43               ;EF
               .byte $05               ;F0
               .byte $30               ;F1
               .byte $30               ;F2
               .byte $49               ;F3
               .byte $49               ;F4
               .byte $30               ;F5
               .byte $19               ;F6
               .byte $45               ;F7
               .byte $32               ;F8
               .byte $30               ;F9
               .byte $2A               ;FA
               .byte $49               ;FB
               .byte $49               ;FC
               .byte $30               ;FD
               .byte $19               ;FE
               .byte $43               ;FF
;     
OPCaddmode     .byte $03               ;0   opcode address mode
               .byte $04               ;1
               .byte $03               ;2
               .byte $03               ;3
               .byte $02               ;4
               .byte $02               ;5
               .byte $02               ;6
               .byte $0F               ;7
               .byte $03               ;8
               .byte $00               ;9
               .byte $03               ;A
               .byte $03               ;B
               .byte $01               ;C
               .byte $01               ;D
               .byte $01               ;E
               .byte $0E               ;F
               .byte $0A               ;10
               .byte $05               ;11
               .byte $0D               ;12
               .byte $03               ;13
               .byte $02               ;14
               .byte $06               ;15
               .byte $06               ;16
               .byte $1F               ;17
               .byte $03               ;18
               .byte $09               ;19
               .byte $03               ;1A
               .byte $03               ;1B
               .byte $01               ;1C
               .byte $08               ;1D
               .byte $08               ;1E
               .byte $1E               ;1F
               .byte $01               ;20
               .byte $04               ;21
               .byte $03               ;22
               .byte $03               ;23
               .byte $02               ;24
               .byte $02               ;25
               .byte $02               ;26
               .byte $2F               ;27
               .byte $03               ;28
               .byte $00               ;29
               .byte $03               ;2A
               .byte $03               ;2B
               .byte $01               ;2C
               .byte $01               ;2D
               .byte $01               ;2E
               .byte $2E               ;2F
               .byte $0A               ;30
               .byte $05               ;31
               .byte $0D               ;32
               .byte $03               ;33
               .byte $06               ;34
               .byte $06               ;35
               .byte $06               ;36
               .byte $3F               ;37
               .byte $03               ;38
               .byte $09               ;39
               .byte $03               ;3A
               .byte $03               ;3B
               .byte $08               ;3C
               .byte $08               ;3D
               .byte $08               ;3E
               .byte $3E               ;3F
               .byte $03               ;40
               .byte $04               ;41
               .byte $03               ;42
               .byte $03               ;43
               .byte $03               ;44
               .byte $02               ;45
               .byte $02               ;46
               .byte $4F               ;47
               .byte $03               ;48
               .byte $00               ;49
               .byte $03               ;4A
               .byte $03               ;4B
               .byte $01               ;4C
               .byte $01               ;4D
               .byte $01               ;4E
               .byte $4E               ;4F
               .byte $0A               ;50
               .byte $05               ;51
               .byte $0D               ;52
               .byte $03               ;53
               .byte $03               ;54
               .byte $06               ;55
               .byte $06               ;56
               .byte $5F               ;57
               .byte $03               ;58
               .byte $09               ;59
               .byte $03               ;5A
               .byte $03               ;5B
               .byte $03               ;5C
               .byte $08               ;5D
               .byte $08               ;5E
               .byte $5E               ;5F
               .byte $03               ;60
               .byte $04               ;61
               .byte $03               ;62
               .byte $03               ;63
               .byte $02               ;64
               .byte $02               ;65
               .byte $02               ;66
               .byte $6F               ;67
               .byte $03               ;68
               .byte $00               ;69
               .byte $03               ;6A
               .byte $03               ;6B
               .byte $0B               ;6C
               .byte $01               ;6D
               .byte $01               ;6E
               .byte $6E               ;6F
               .byte $0A               ;70
               .byte $05               ;71
               .byte $0D               ;72
               .byte $03               ;73
               .byte $06               ;74
               .byte $06               ;75
               .byte $06               ;76
               .byte $7F               ;77
               .byte $03               ;78
               .byte $09               ;79
               .byte $03               ;7A
               .byte $03               ;7B
               .byte $0C               ;7C
               .byte $08               ;7D
               .byte $08               ;7E
               .byte $7E               ;7F
               .byte $0A               ;80
               .byte $04               ;81
               .byte $03               ;82
               .byte $03               ;83
               .byte $02               ;84
               .byte $02               ;85
               .byte $02               ;86
               .byte $0F               ;87
               .byte $03               ;88
               .byte $00               ;89
               .byte $03               ;8A
               .byte $03               ;8B
               .byte $01               ;8C
               .byte $01               ;8D
               .byte $01               ;8E
               .byte $0E               ;8F
               .byte $0A               ;90
               .byte $05               ;91
               .byte $0D               ;92
               .byte $03               ;93
               .byte $06               ;94
               .byte $06               ;95
               .byte $07               ;96
               .byte $1F               ;97
               .byte $03               ;98
               .byte $09               ;99
               .byte $03               ;9A
               .byte $03               ;9B
               .byte $01               ;9C
               .byte $08               ;9D
               .byte $08               ;9E
               .byte $1E               ;9F
               .byte $00               ;A0
               .byte $04               ;A1    changed from 0d to 04
               .byte $00               ;A2
               .byte $03               ;A3
               .byte $02               ;A4
               .byte $02               ;A5
               .byte $02               ;A6
               .byte $2F               ;A7
               .byte $03               ;A8
               .byte $00               ;A9
               .byte $03               ;AA
               .byte $03               ;AB
               .byte $01               ;AC
               .byte $01               ;AD
               .byte $01               ;AE
               .byte $2E               ;AF
               .byte $0A               ;B0
               .byte $05               ;B1
               .byte $0D               ;B2     
               .byte $03               ;B3
               .byte $06               ;B4
               .byte $06               ;B5
               .byte $07               ;B6
               .byte $3F               ;B7
               .byte $03               ;B8
               .byte $09               ;B9
               .byte $03               ;BA
               .byte $03               ;BB
               .byte $08               ;BC
               .byte $08               ;BD
               .byte $09               ;BE
               .byte $3E               ;BF
               .byte $00               ;C0
               .byte $04               ;C1
               .byte $03               ;C2
               .byte $03               ;C3
               .byte $02               ;C4
               .byte $02               ;C5
               .byte $02               ;C6
               .byte $4F               ;C7
               .byte $03               ;C8
               .byte $00               ;C9
               .byte $03               ;CA
               .byte $03               ;CB
               .byte $01               ;CC
               .byte $01               ;CD
               .byte $01               ;CE
               .byte $4E               ;CF
               .byte $0A               ;D0
               .byte $05               ;D1
               .byte $0D               ;D2
               .byte $03               ;D3
               .byte $03               ;D4
               .byte $06               ;D5
               .byte $06               ;D6
               .byte $5F               ;D7
               .byte $03               ;D8
               .byte $09               ;D9
               .byte $03               ;DA
               .byte $03               ;DB
               .byte $03               ;DC
               .byte $08               ;DD
               .byte $08               ;DE
               .byte $5E               ;DF
               .byte $00               ;E0
               .byte $04               ;E1
               .byte $03               ;E2
               .byte $03               ;E3
               .byte $02               ;E4
               .byte $02               ;E5
               .byte $02               ;E6
               .byte $6F               ;E7
               .byte $03               ;E8
               .byte $00               ;E9
               .byte $03               ;EA
               .byte $03               ;EB
               .byte $01               ;EC
               .byte $01               ;ED
               .byte $01               ;EE
               .byte $6E               ;EF
               .byte $0A               ;F0
               .byte $05               ;F1
               .byte $0D               ;F2
               .byte $03               ;F3
               .byte $03               ;F4
               .byte $06               ;F5
               .byte $06               ;F6
               .byte $7F               ;F7
               .byte $03               ;F8
               .byte $09               ;F9
               .byte $03               ;FA
               .byte $03               ;FB
               .byte $03               ;FC
               .byte $08               ;FD
               .byte $08               ;FE
               .byte $7E               ;FF
;     
;
ModeByteCnt    .byte $02               ;0  opcode mode byte count
               .byte $03               ;1
               .byte $02               ;2
               .byte $01               ;3
               .byte $02               ;4
               .byte $02               ;5
               .byte $02               ;6
               .byte $02               ;7
               .byte $03               ;8
               .byte $03               ;9
               .byte $02               ;A
               .byte $03               ;B
               .byte $03               ;C
               .byte $02               ;D
               .byte $03               ;E
               .byte $02               ;F 
;     
;
ModeJmpTbl     .word IMM_mode          ;0  Operand print table
               .word ABS_mode          ;1
               .word ZP_mode           ;2
               .word IMPLIED_mode      ;3
               .word INDZP_X_mode      ;4
               .word INDZP_Y_mode      ;5
               .word ZP_X_mode         ;6
               .word ZP_Y_mode         ;7
               .word ABS_X_mode        ;8
               .word ABS_Y_mode        ;9
               .word REL_mode          ;a
               .word INDABS_mode       ;b
               .word INDABSX_mode      ;c
               .word INDZP_mode        ;d
               .word BBREL_mode        ;e
               .word ZP_mode           ;f  dup of ZP for RMB,SMB cmds
;
;              
OPCtxtData     .byte "ADC"             ;0
               .byte "AND"             ;1
               .byte "ASL"             ;2
               .byte "BCC"             ;3
               .byte "BCS"             ;4
               .byte "BEQ"             ;5
               .byte "BIT"             ;6
               .byte "BMI"             ;7
               .byte "BNE"             ;8
               .byte "BPL"             ;9
               .byte "BRA"             ;A
               .byte "BRK"             ;B
               .byte "BVC"             ;C
               .byte "BVS"             ;D
               .byte "CLC"             ;E
               .byte "CLD"             ;F
               .byte "CLI"             ;10
               .byte "CLV"             ;11
               .byte "CMP"             ;12
               .byte "CPX"             ;13
               .byte "CPY"             ;14
               .byte "DEC"             ;15
               .byte "DEX"             ;16
               .byte "DEY"             ;17
               .byte "EOR"             ;18
               .byte "INC"             ;19
               .byte "INX"             ;1A
               .byte "INY"             ;1B
               .byte "JMP"             ;1C
               .byte "JSR"             ;1D
               .byte "LDA"             ;1E
               .byte "LDX"             ;1F
               .byte "LDY"             ;20
               .byte "LSR"             ;21
               .byte "NOP"             ;22
               .byte "ORA"             ;23
               .byte "PHA"             ;24
               .byte "PHP"             ;25
               .byte "PHX"             ;26
               .byte "PHY"             ;27
               .byte "PLA"             ;28
               .byte "PLP"             ;29
               .byte "PLX"             ;2A
               .byte "PLY"             ;2B
               .byte "ROL"             ;2C
               .byte "ROR"             ;2D
               .byte "RTI"             ;2E
               .byte "RTS"             ;2F
               .byte "SBC"             ;30
               .byte "SEC"             ;31
               .byte "SED"             ;32
               .byte "SEI"             ;33
               .byte "STA"             ;34
               .byte "STX"             ;35
               .byte "STY"             ;36
               .byte "STZ"             ;37
               .byte "TAX"             ;38
               .byte "TAY"             ;39
               .byte "TRB"             ;3A
               .byte "TSB"             ;3B
               .byte "TSX"             ;3C
               .byte "TXA"             ;3D
               .byte "TXS"             ;3E
               .byte "TYA"             ;3F
               .byte "WAI"             ;40
               .byte "STP"             ;41
               .byte "BBR"             ;42 4Byte Opcodes
               .byte "BBS"             ;43
               .byte "RMB"             ;44
               .byte "SMB"             ;45
               .byte ".DB"             ;46 define 1 byte for assembler
               .byte ".DW"             ;47 define 1 word for assembler
               .byte ".DS"             ;48 define a string block for assembler
               .byte "???"             ;49 for invalid opcode
;
; HELP text in main_cm.asm!
;
; *** VERSION Notes ***
; 3.5 added the text dump command, "q"
; 4.0 reorganized structure, added RAM vectors for chrin, scan_in, and chrout
; 4.1 fixed set time routine so 20-23 is correct    
; 4.2 RST, IRQ, NMI, BRK all jmp ind to 02xx page to allow user prog to control
; 4.3 added status register bits to printreg routine
; 4.4 refined set time to reduce unneeded sec"s and branches, disp time added CR,
;     and added zeromem to the reset routine, ensuring a reset starts fresh every time!
;     continued to re-organize - moved monitor"s brk handler into mon area.
; 4.5 nop out the jsr Getkey in the eeprom write routine to prevent BRK"s
; 4.6 added version printout when entering assember to show ? prompt
; 4.7 added Lee Davison's Enhanced Basic to ROM Image 
; 4.9 Added all of the WDC opcodes to the disassembler and mini-assembler
; 5.0 Added TAB key support to the input routine, expands tabs to spaces
; 5.1 Added jump table at the start of the monitor to commonly used routines
;
;end of file
