;*********************************************************************       
;  SBC Firmware V5.1, 5-30-05, by Daryl Rictor
;  Modifications to run on T65 Core by Carsten Meyer, cm@ctmagazin.de
;  MINIMALVERSION ohne OpCode-Listing!
;*********************************************************************       
; 
;               
; *************************************************************************
; 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
; *************************************************************************
;
	.IF Mouse
	.ELSE
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         ;
	.ENDIF
	
; 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                     ; 

	.IF Mouse
	.ELSE

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            ;
	.ENDIF
PrintXSP1      JSR   Print1SP          ;
               dex                     ;
PrintXSP       cpx   #$00              ;
               bne   PrintXSP1         ;
               rts                     ;
Print2SP       jsr   Print1SP          ; print 2 SPACES
Print1SP       LDA   #$20              ; print 1 SPACE
               JMP   WriteScreen            ;

	.IF Mouse
	.ELSE
;
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   List_one          ; one line of dissassembly
               CLC                     ;
               LDA   Startaddr         ; inc address pointer to next cmd
               ADC   #8         ;
               STA   Startaddr         ;
               BCC   List3             ;
               INC   Startaddr+1       ;
List3          RTS                     ;

List_one:
               jsr   Print_CR          ; one last CR-LF
	LDA	#8
	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   #$08              ;
               BCC   List5             ;
               JSR   Print2SP          ;
               DEX                     ;
               BEQ   List4             ;---------

               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
               
	.ENDIF	; Mouse
	
;
;     
;
; 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
