[cc65] Re: 6502 runtime relocation

From: Jonathan Graham Harston <jgh1arcade.demon.co.uk>
Date: 2005-06-27 01:11:19
> Message-ID: <20050622212448.GB22030@trixie.musoftware.de>
 
Ullrich von Bassewitz <uz@musoftware.de> wrote:
> On Wed, Jun 22, 2005 at 01:58:23PM +0100, Jonathan Graham Harston wrote:
> > until runtime. I've got some code that relocates a 6502 program
> > loaded into the middle of memory to the bottom and jumps to it.
> 
> Probably the most versatile solution would be to generate o65 output and
> write a relocator that runs on arbitrary memory addresses. I don't know
> how your relocator works, but if it disassembles the code, there are
> pieces of code it won't handle correctly.
 
I was thinking something like the following, which is a
generalised version of the specific-case relocator in the Master
MOS 3.50. The program is compiled/assembled to load in the middle
of memory, eg &4000. It is entered at the relocator at the end of
the program code, which finds out the lowest memory address and
copies the code down there, modifying bytes indicated in the
bitmap table.
 
The easiest way of generating the bitmap table is to assembled the
code at two addresses 256 bytes apart and scan through the object
code for bytes that are different.
 
.start
; program code to be relocated
;
.enter
; enter here, to relocator after program code
;
; (src) =start of code to relocate
; (dst) =destination
; (len) =length of code to relocate
; (rel) =relocation table
; offset=page offset
; bitmap=relocation bitmap
;
LDX #start AND 255:LDY #start DIV 256  ; XY=start of loaded code
STX src:STY src+1
LDA #&82:JSR osbyte                    ; Get OSHWM to relocate to
STX dst:STY dst+1                      ; OSHWM is always &xx00
DEY:TYA:PHA:LDA #&FF:PHA               ; Push OSHWM-1 to 'RTS' to
SEC:LDA src+1:SBC dst+1:STA offset     ; Calculate relocation offset
SEC:LDA #enter AND 255:SBC #start AND 255:STA len
LDA #enter DIV 256:SBC #start DIV 256:STA len+1 ; code length
LDX #table AND 255:LDY #table DIV 256  ; Start of relocation table
STX rel:STY rel+1
LDY #0:LDX #1
.loop
DEX:BNE copy
LDA (rel,X):STA bitmap:LDX #7
INC rel:BNE P%+5:INC rel+1
.copy
LDA (src),Y                            ; Fetch byte
ROL bitmap:BCC P%+5:SBC offset         ; Subtract offset if needed
STA (dst),Y                            ; Store byte
INY:BNE P%+7:INC src+1:INC dst+1       ; Update src/dst
LDA len:SEC:SBC #1:STA len             ; len=len-1
LDA len+1:SBC #0:STA len+1
ORA len:BNE                            ; loop until len=0
RTS                                    ; Jump to relocated start
.table
; table of relocation bitmaps
; %76543210 - if bit set, subtract offset when copying
;             b7=first byte, b6=second byte, etc.
 
-- 
J.G.Harston - jgh@arcade.demon.co.uk - mdfs.net/User/JGH
BBC IDE Hard Drive Interface - http://mdfs.net/Info/Comp/BBC/IDE
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Mon Jun 27 02:00:12 2005

This archive was generated by hypermail 2.1.8 : 2005-06-27 02:00:15 CEST