| 1 |
|
| 2 |
*= $1000 |
| 3 |
|
| 4 |
; Static relocation is easy, just use .logical/.here and copy the code to the |
| 5 |
; already know destination for execution. |
| 6 |
|
| 7 |
ldx #0 |
| 8 |
_lp |
| 9 |
lda inram,x |
| 10 |
sta reloc,x |
| 11 |
inx |
| 12 |
cpx #size(reloc) |
| 13 |
bcc _lp |
| 14 |
|
| 15 |
; Patching it at the destination is simple as well. As all labels point to |
| 16 |
; destination memory it can be directly overwritten. |
| 17 |
|
| 18 |
sta reloc.patchme + 1 |
| 19 |
|
| 20 |
; But what if the code has to be transmitted in some way to somewhere else for |
| 21 |
; execution? Then the only way is to patch it before the transmission in RAM. |
| 22 |
; But all the available labels contain the "wrong" addresses for that and can't |
| 23 |
; be used as-is. |
| 24 |
|
| 25 |
; Normally one calculates the in memory address by adjusting it with the |
| 26 |
; difference: |
| 27 |
|
| 28 |
sta inram + reloc.patchme - reloc + 1 |
| 29 |
|
| 30 |
; When patching many locations it's less error prone to move the calculation to |
| 31 |
; a function: |
| 32 |
|
| 33 |
sta patch(reloc.patchme) + 1 |
| 34 |
rts |
| 35 |
|
| 36 |
; Function to calculate in memory address. May be a good idea to put it inside |
| 37 |
; the "reloc" scope but that's not done now. |
| 38 |
|
| 39 |
patch .sfunction _adr, inram + _adr - reloc |
| 40 |
|
| 41 |
; Example relocated routine, compiled here but needs to execute at $2000 |
| 42 |
|
| 43 |
inram .logical $2000 |
| 44 |
reloc .block ; in separate scope as a reminder |
| 45 |
patchme lda #0 ; that labels inside are relocated |
| 46 |
inc patchme+1 |
| 47 |
.bend |
| 48 |
.here |
| 49 |
|
| 50 |
;----------------------- |
| 51 |
; Not recommended at all but if one is careful it's possible to put all |
| 52 |
; relocated code into a macro and compile it twice just to get direct labels |
| 53 |
; which carry the in memory address: |
| 54 |
|
| 55 |
sta virt.patchme + 1 |
| 56 |
|
| 57 |
; For the virtual double compile hack the code in the macro must be written so |
| 58 |
; that it compiles the same way every time! |
| 59 |
; And it's really easy to get this wrong. For example when relocating to zero |
| 60 |
; page the second in memory address compile might use absolute addressing while |
| 61 |
; the first did use zero page for an opcode. These are different size and so |
| 62 |
; the second compile will have useless shifted addresses from there on. You |
| 63 |
; have been warned. |
| 64 |
|
| 65 |
drvcode .macro |
| 66 |
patchme lda #0 |
| 67 |
inc patchme+1 |
| 68 |
.endm |
| 69 |
|
| 70 |
; Actual in memory code relocated to $0300. |
| 71 |
|
| 72 |
inram2 .logical $300 |
| 73 |
#drvcode |
| 74 |
.here |
| 75 |
|
| 76 |
; Virtual compile a second time to have the right labels for patching |
| 77 |
|
| 78 |
.virtual inram2 |
| 79 |
virt #drvcode |
| 80 |
.endv |
| 81 |
|
| 82 |
; Sanity check, not perfect but better than nothing. Change the relocation from |
| 83 |
; $300 to $30 see it triggered |
| 84 |
|
| 85 |
.cerror size(virt) != size(reloc), "Different size, how?!" |