*      
* PEEK+POKE+WAIT 24-bit patch for the SuperCPU
* Also patches load/save
*
* Vers. 2: Change only LDA (),Y -> LDA [], use $50 $51 $52
*   as 24-bit address. (but also $14 $15 as before).  This
*   should maximize compatibility.
*
*   Note that SYS patch is trivial.
*
* Stephen L. Judd 12/6/97
*
* 1/3/98 Added SYS and LOAD patches.
*
* 12/14/99 -- Added to Jammon
*

LINNUM   = $14            ;Variable
NUM24    = $50            ;New 24-bit dest.
CHRGET   = $73
TXTPTR   = $7A
HIGHPTR  = $AB            ;High byte of end save addr.

*===============================
GETBYTE  = $B79B
GETNUM   = $B7EB
GETCHK   = GETNUM+6
GETADR   = $B7F7
PEEK     = $B80D
POKE     = $B824
WAIT     = $B82D
*===============================

* GETADR patch -> GETADR+7
P1ADR    = GETADR+7
PATCH1   = $99            ;CMP #$91 -> CMP #$99

* GETADR patch -> GETADR+$0D
P2ADR    = GETADR+$0D
PATCH2   = $4C            ;JMP LINFETCH

* PEEK patch -> PEEK+9
P3ADR    = PEEK+11
PATCH3A  = $A7            ;LDA [$50]
PATCH3B  = NUM24

* POKE patch -> POKE+4
P4ADR    = POKE+6
PATCH4A  = $87            ;STA [$50]
PATCH4B  = PATCH3B

* WAIT patch -> WAIT+17
P5ADR    = WAIT+19
PATCH5A  = PATCH3A        ;LDA [$50]
PATCH5B  = PATCH3B
*===============================

INIT     

* Patch BASIC

* Init IEVAL vector

         LDA $F7E5
         CMP #$91         ;Check for PPP
         BNE :SKIP
         LDA $030A        ;Old IEVAL
         STA $0334
         LDA $030B
         STA $0335
:SKIP    
         >>> per,HEXEVAL
         PLA
         STA $030A
         PLA
         STA $030B

* Set up bank 1 patches

         CLC
         DFB $FB          ;xce
         DFB $E2,$30      ;sep #$30
         LDA #$01
         PHA
         DFB $AB          ;PLB -- store to bank 1

* Patch BASIC

         LDA #PATCH1      ;GETADR
         STA P1ADR
         LDA #PATCH2
         STA P2ADR
         >>> per,LINFETCH
         PLA
* LDA #<LINFETCH
         STA P2ADR+1
         PLA
* LDA #>LINFETCH
         STA P2ADR+2
         LDA #PATCH3A     ;PEEK+WAIT
         STA P3ADR
         STA P5ADR
         LDA #PATCH3B
         STA P3ADR+1
         STA P5ADR+1
         STA P4ADR+1      ;POKE
         LDA #PATCH4A
         STA P4ADR

* Patch SYS

         LDA #$DC         ;JMP []
         STA $E144
         LDA #NUM24
         STA $E145

* Get filename+address patch

         >>> per,GETIT
         PLA
* LDA #<GETIT
         STA $E1F7
         PLA
* LDA #>GETIT
         STA $E1F8

* Patch SAVE routine
SPATCH   
         >>> per,SAVE
         PLA
* LDA #<SAVE
         STA $E157
* LDA #>SAVE
         PLA
         STA $E158

         >>> per,SB0
* LDA #<SB0
         PLA
         STA $F606
* LDA #>SB0
         PLA
         STA $F607

         LDA #$04
         STA $F5F5        ;CMP #$04
         LDA #$90         ;BCC ...
         STA $F5F6
         LDA #$A6         ;LDX $B9
         STA $F5F8
         LDA #$B9
         STA $F5F9

         LDA #$AE         ;start address in $AE/F
         STA $F61B
         LDA #$AF
         STA $F620

         LDA #$EA         ;NOP out $C1->$AC copy
         STA $F617
         STA $F618
         STA $F619

         >>> per,CMPSAVE
* LDA #<CMPSAVE
         PLA
         STA $F625
* LDA #>CMPSAVE
         PLA
         STA $F626
         >>> per,INCSAVE
* LDA #<INCSAVE
         PLA
         STA $F63B
* LDA #>INCSAVE
         PLA
         STA $F63C

         LDA #$A7         ;LDA [$AE]
         STA $F629
         LDA #$AE
         STA $F62A

* Patch LOAD routine
LPATCH   
         >>> per,LOAD
* LDA #<LOAD
         PLA
         STA $E16D
         PLA
* LDA #>LOAD
         STA $E16E

         >>> per,SAVEADDR
         PLA
         STA $F4F1
         STA $F39F
         PLA
         STA $F4F2
         STA $F3A0

         LDA #$C7
         STA $F512        ;CMP [$AE]
         LDA #$87
         STA $F51C        ;STA [$AE]
         STA $F7E5
         STA $F7F0
         LDA #$20
         STA $F51E        ;JSR INCSAVE
         STA $F7E7
         >>> per,INCSAVE
         PLA
* LDA #<INCSAVE
         STA $F51F
         STA $F7E8
         PLA
* LDA #>INCSAVE
         STA $F520
         STA $F7E9
         LDA #$EA         ;NOP NOP NOP
         STA $F521
         STA $F522
         STA $F523
         STA $F7EA
         STA $F7EB
         STA $F7EC

         LDA #$B0
         STA $F75B        ;LDA $00B0,Y
         LDA #$B3
         STA $F765        ;sprites -> $B3
         STA $F7FF
         INC $F805        ;PLA/STA etc.
         INC $F808
         INC $F80B

         SEC
         DFB $FB          ;xce
         LDA #$00
         PHA
         DFB $AB          ;PLB -- store to bank 0

* Further LOAD patch

         LDX #F4C1-F4A7-1
:LOOP    LDA F4A7,X
         HEX 9FA7F401     ;STA $01F4A7,X
         DEX
         BPL :LOOP

         RTS

F4A7     HEX 6490         ;STZ $90
         LDA $BA
         CMP #$04
         BCS :CONT
         JMP $F713
:CONT    LDY $B7
         BNE :C2
         JMP $F2CC
:C2      LDX $B9
         BPL :C3          ;Really just want $FF
         INX
         HEX 2C
:C3      HEX 64B0         ;STZ $B0
F4C1     

* Patch EVAL to read in hex values

HEXEVAL  
         JSR CHRGET
         CMP #'$'
         BNE NOPE
         LDA #$C0
         STA $0E          ;128+64 = 24-bit integer
         LDA #00
         STA $61          ;FAC1=0
         STA $0D          ;VALTYP = numeric

LOOP     JSR CHRGET
         BEQ LINRTS
         BCC :NUMBER
         SBC #8           ;A-F will be 10-15
:NUMBER  SBC #47
         CMP #16
         BCC OK           ;Can use e.g. $d0+5 now
         CMP #46-55       ;Check for decimal pt.
         BNE LINRTS
ERROR    LDX #14          ;Illegal quantity
         JMP ($0300)
OK       PHA
         >>> per,SIXTEEN
         PLA
         TAX
         PLA
         TAY
         TXA
* LDA #<SIXTEEN
* LDY #>SIXTEEN
         JSR $BA28        ;Multiply FAC1 by 16
         PLA
         JSR $BD7E        ;Add to FAC1
         JMP LOOP

NOPE     LDX TXTPTR       ;Back up TXTPTR
         BNE :SKIP
         DEC TXTPTR+1
:SKIP    DEC TXTPTR
         JMP ($0334)      ;Normal routine

SIXTEEN  FLO 16           ;16 in 5-byte fp ($85 00 00 00 00)

         TXT 'judd'

*
* Copy bytes of FAC1 into LINNUM and NUM24
*
* On exit, .Y .A = LINNUM+1, LINNUM
*
LINFETCH 
         LDA $63          ;high
         STA NUM24+2
         LDA $64          ;med
         LDY $65          ;low
         STY LINNUM
         STY NUM24
         STA LINNUM+1
         STA NUM24+1
LINRTS   RTS

*
* Patch LOAD+SAVE command to read address if present.
*
SAVE     JSR GETPARMS     ;Get 1st two parms
         BNE LINRTS       ;Exit
         JSR SETNUM       ;$AC is now start address
         JSR GETIT
         JSR CHEK24
         BNE ERROR        ;require another address
         LDY NUM24+1      ;end address
         LDA NUM24+2
         STA HIGHPTR
         PLA
         PLA
         LDA #$AC         ;start address
         JMP $E156+9

LOAD     JSR GETPARMS
         BNE LINRTS
         JSR SETNUM
         PLA              ;old return address
         PLA
         LDA $0A
         JMP $E16C+9      ;Into the load routine

GETIT    JSR $E20E        ;Check for comma
         JSR $AD8A        ;get number
         JSR $B7F7        ;convert to int
         LDX NUM24        ;low byte in .X
         RTS

GETPARMS JSR $E1D4        ;Get 1st two parms
CHEK24   LDY #$FF
         LDA $0E          ;Check for 24-bit hex value
         CMP #$C0
         BNE :RTS
         STY $B9          ;SA -- 24-bit flag
:RTS     RTS

SETNUM   LDX NUM24
         STX $AC          ;SAVE start addresses
         LDY NUM24+1
         STY $AD
         LDA NUM24+2
         STA $B0          ;Always high address
         RTS

*
* SAVE compare and increment routines
*
CMPSAVE  SEC
         LDA $AE
         SBC $AC
         LDA $AF
         SBC $AD
         LDA $B0
         SBC HIGHPTR
         RTS

INCSAVE  INC $AE
         BNE :RTS
         INC $AF
         BNE :RTS
         INC $B0
:RTS     RTS

*
* Patch SAVE variables
*
SB0      INX              ;.X contains SA
         BEQ :SKIP        ;One byte short of a fit into
         DFB $64,$B0      ;SAVE itself.
         DFB $64,HIGHPTR  ;STZ
:SKIP    LDA $C1          ;start address -> $AE
         LDX $AE          ;end address -> $AC
         STA $AE
         STX $AC
         LDA $C2
         LDX $AF
         STA $AF
         STX $AD
         JMP $F3D5        ;continue as normal

*
* Save load address
*
SAVEADDR 
         LDA $AE
         STA $C3
         LDA $AF
         STA $C4
         JMP $F5D2
