Debian Codegraph @

examples/runtime_relocation.asm

raw
1
2 ; This example demonstrates how to do runtime relocation of code to an unknown
3 ; address. To simplify matters the granuality is per-page but can be extended
4 ; to per byte.
5
6 ; The idea is to compile the code twice to different addresses and then check
7 ; which bytes differ by the shift amount. Those will be the ones which need
8 ; relocation.
9
10 ; For this to work the code should not change it's structure (or size) when
11 ; compiled multiple times but that's usually not difficult.
12
13 * = $0801 ; C64 BASIC header
14 .word (+), 2023
15 .null $9e, format("%4d", start)
16 + .word 0
17
18 start
19 lda #>$1000 ; the destination isn't known at compile time
20 jsr relocate ; so this routine does the relocation
21 jmp $1000 ; jump to the relocated code
22
23 ; To not repeat it twice the code to be relocated is put in this macro. It doesn't
24 ; do anything useful but at least has 3 instruction which need relocation.
25
26 relocated_code .macro
27 ldx #0
28 lp lda counter ; this will need relocation
29 sta $400,x
30 inx
31 dec counter ; this will need relocation
32 beq end
33 jmp lp ; this will need relocation
34 end rts
35
36 counter .byte 10
37 .endm
38
39 ; Compile twice. For the second time a page further up in memory. And so all
40 ; high bytes will be different by one.
41
42 code1 .logical $100 ; compile to $100. Not zero page or else
43 #relocated_code ; there's a risk of using zeropage
44 .here ; addressing modes for data access!
45
46 code2 .virtual $200 ; virtual only as the result doesn't
47 #relocated_code ; need saving in the image
48 .endv
49
50 ; Now let's compare the two compilations and collect what needs to be relocated.
51
52 ; This function takes two lists (the two versions of code) and returns the
53 ; offsets of differences. It makes sure that the difference is exactly 1 as that
54 ; was the amount of pages it was shifted in memory. If it goes wrong check
55 ; the listing file at the reported address.
56
57 list_differences .function _code1, _code2
58 .cerror len(_code1) != len(_code2), "Both should have the same size!"
59 _differences := [] ; start with empty list
60 .for _i in range(len(_code1))
61 .continueif _code1[_i] == _code2[_i]; same?
62 .cwarn <(_code2[_i] - _code1[_i]) != 1, "Can't relocate at ", code1 + _i
63 _differences ..= [_i] ; add offset to list
64 .next
65 .endf _differences
66
67 ; This table holds the offsets which need relocation. For this simple short
68 ; example byte offsets are sufficient.
69
70 relocation_table .byte list_differences(code1[::], code2[::])
71
72 ; Please note that for now the bytes of the two codes are put in lists using
73 ; [::] which may change in the future. That's why the function does not rely on
74 ; being able to access code bytes through indexing.
75
76 ; This routine copies the relocated code and fixes its instructions up.
77 ; Destination page is given in the accumulator.
78
79 relocate .proc
80 dstp = $fb ; a zeropage pointer used for copying
81
82 sta dstp+1 ; code will go to $xx00
83 ldy #0
84 sty dstp
85 _lp
86 lda code1,y
87 sta (dstp),y ; do actual copy to destination
88 iny
89 cpy #size(code1)
90 blt _lp
91
92 ldx #0
93 _lp2 ; this loop fixes instruction address bytes
94 ldy relocation_table,x ; who's offsets were collected earlier
95 lda (dstp),y
96 clc
97 adc dstp+1 ; add high byte of destination address
98 sec
99 sbc #>$100 ; one less as it was compiled to $100!
100 sta (dstp),y
101 inx
102 cpx #size(relocation_table)
103 blt _lp2
104 rts
105 .pend
106