10 REM > Patch035
   20 REM Patch ARMCoPro MOS v0.35
   30 :
   40 DIM mcode% 128*1024
   50 OSCLI"LOAD U033035v0 "+STR$~mcode%
   60 length%=&2A8:REM Offset to length to copy to flash
   70 base%=&72C  :REM Offset to start of data to copy to flash
   80 oldlen%=mcode%!length%
   90 :
  100 FOR P=0 TO 1
  110   P%=length%:O%=P%+mcode%
  120   [OPT P*3+4
  130   EQUD newend
  140   ]
  150   P%=&0124:O%=P%+mcode%+base%
  160   [OPT P*3+4
  170   BL      CheckExtension     ; R0 contains startup ack. byte
  180                              ; Overwrites TEQ R0,#&80 opcode
  190   ]
  200   P%=&0168:O%=P%+mcode%+base%
  210   [OPT P*3+4
  220   EQUS "M "                  ; Simple visual clue
  230   ]
  240   P%=&14A4:O%=P%+mcode%+base%
  250   [OPT P*3+4
  260   LDMIA R13,{R2-R5}          ; All OSFILEs return all registers
  270   MOV R0,R12                 ; All OSFILEs return result
  280   ]
  290   P%=oldlen%:O%=P%+mcode%+base%
  300   [OPT P*3+4
  310   .CheckExtension
  320   MOV R3,R0:MOV R4,R14       :\ Save Tube response byte and return address
  330                              :\ Needs to return checking for #&80
  340   
  350   \ Check for * pressed and skip everything
  360   \ ---------------------------------------
  370   MOV R0,#&7A:SWI "OS_Byte"  :\ Check keys pressed
  380   CMP R1,#&5B:CMPNE R1,#&48  :\ Either '*' key pressed?
  390   BEQ ReturnCore             :\ Abort if pressed
  400   :
  410   MOV   R2,#&C8000000        :\ Base of ROM
  420   ADD   R2,R2,#&20000        :\ Start of expansion space
  430   :
  440   LDRB  R0,[R2,#7]           ; Get Copyright offset
  450   ADD   R1,R2,R0             ; R1=>copyright string
  460   LDRB  R0,[R1],#1           ; Check for copyright string
  470   CMP   R0,#0
  480   LDRB  R0,[R1],#1
  490   CMPEQ R0,#ASC"("
  500   LDRB  R0,[R1],#1
  510   CMPEQ R0,#ASC"C"
  520   LDRB  R0,[R1],#1
  530   CMPEQ R0,#ASC")"
  540   BNE   ReturnCore           ; No ROM header, return to core code
  550   LDRB  R0,[R2,#6]           ; Get ROM type byte
  560   AND   R0,R0,#&4F
  570   CMP   R0,#&4D              ; ARM language code?
  580   BNE   ReturnCore           ; Not ARM code, return to core code
  590   LDR   R0,[R2,#0]           ; Get entry address - assumes word aligned
  600   BIC   R0,R0,#&FF000000     ; Drop 6502 JMP opcode from top byte
  610   ADD   R2,R2,R0             ; Add to entry point
  620   STMFD R13!,{R3,R4}         ; Save Tube byte, return address
  630   MOV   R12,#&1900
  640   LDR   R1,[R12,#780]        ; R1=base of code to enter
  650   MOV   R0,R3                ; R0=&80 if code to be entered
  660   MOV   R14,PC:MOV PC,R2     ; Call extension code
  670   LDMFD R13!,{R0,R4}         ; Get Tube byte back and return
  680   .ReturnCore
  690   MOV   R0,R3                ; R0=Tube byte
  700   \\ TEQ   R0,#&80              ; Code to be entered?
  710   TEQ   R0,R0                ; Set EQ
  720   MOV   PC,R4                ; Return to core code
  730   .newend
  740 ]NEXT
  750 OSCLI"SAVE U033035 "+STR$~mcode%+" "+STR$~O%+" 8000 8000"