-
Notifications
You must be signed in to change notification settings - Fork 5
/
reloc.S
438 lines (358 loc) · 10.3 KB
/
reloc.S
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
INCLUDE "exec/memory.i"
INCLUDE "lvo/exec_lib.i"
IFND ROM
section .text,code
ENDIF
NUM_ENTRIES EQU 5
; API:
; Z3 ROM access:
; a0: rombase
; d0: offset
; Memory (RAM) access:
; a0: pointer to binary
; d0: 0
public _relocate
_relocate
bsr InitHandle
; fetch file header
bsr RomFetch32
cmp.l #$524e4301,d0 ; Are we compressed?
beq.b .decompress
bra .real_relocate
.decompress
movem.l d1-d7/a0-a6,-(sp)
bsr RomFetch32
; d0 = original length
move.l d0,d3
moveq.l #MEMF_PUBLIC,d1
move.l 4,a6 ; execbase
jsr _LVOAllocMem(a6)
move.l d0,d2
; d2 / d3 = Original Ptr/Size
bsr RomFetch32
add.l #18,d0 ; magic 18 bytes at the beginning
move.l d0,d5
addq.l #3,d0 ; padding
and.b #-4,d0 ; align
moveq.l #MEMF_PUBLIC,d1
jsr _LVOAllocMem(a6)
move.l d0,d4
; d4 / d5 = Compressed Ptr/Size
; Reset ReadHandle
lea ReadHandle(pc),a1
move.l (a1),d0
tst.l 4(a1)
beq .direct
sub.l #48,d0 ; 12 bytes
bra .storeHandle
.direct
sub.l #12,d0 ; 12 bytes
.storeHandle
move.l d0,(a1)
; Copy compressed file to ram.
move.l d4,a0
move.l d5,d1
subq.l #1,d1 ; round down
lsr.l #2,d1
.Copy
bsr RomFetch32
move.l d0,(a0)
addq.l #4,a0
dbra d1,.Copy
move.l d4,a0
move.l d2,a1
bsr _Unpack
move.l d4,a1
move.l d5,d0
addq.l #3,d0 ; padding
and.b #-4,d0 ; align
jsr _LVOFreeMem(a6)
lea ReadHandle(pc),a1
move.l d2,(a1) ; memory location passed in a0
move.l #0,4(a1) ; Set access type: 0 = memory, Z3 otherwise
bsr RomFetch32
bsr .real_relocate
; save return value
move.l d0,d4
move.l d2,a1
move.l d3,d0
jsr _LVOFreeMem(a6)
move.l d4,d0
movem.l (sp)+,d1-d7/a0-a6
rts
.real_relocate
movem.l d1-d7/a0-a6,-(sp)
cmp.l #$3f3,d0 ; We only know hunk_hdr
bne .RelocateFail
; Allocate pHunks array
moveq.l #(8*NUM_ENTRIES),d0
move.l #MEMF_PUBLIC|MEMF_CLEAR,d1
jsr _LVOAllocMem(a6)
lea pHunks(pc),a1
move.l d0,(a1)
; consume the header and remember number of hunks
bsr RomFetch32
bsr RomFetch32
move.l d0,d7 ; table size
subq.l #1,d7
move.l d7,d6 ; for HUNK_END
bsr RomFetch32
bsr RomFetch32
; Go through hunk header and allocate all segments
move.l 4,a6 ; execbase
move.l pHunks(pc),a2 ; segment pointers
lea.l 4*NUM_ENTRIES(a2),a3 ; segment lengths
.AllocateLoop
moveq.l #MEMF_PUBLIC,d1
bsr RomFetch32
lsl.l #2,d0
move.l d0,(a3)+ ; segment length
addq.l #8,d0 ; Add 8 for seglist overhead
jsr _LVOAllocMem(a6)
tst.l d0
beq.s .RelocateFail
addq.l #8,d0 ; Skip seglist overhead
move.l d0,(a2)+ ; segment pointer
dbra d7,.AllocateLoop
move.l pHunks(pc),a5
.HunkLoop
move.l (a5),a0 ; current hunk
bsr RomFetch32
cmp.l #$3e9,d0
beq .HunkCode
cmp.l #$3ea,d0
beq .HunkData
cmp.l #$3eb,d0
beq .HunkBSS
cmp.l #$3ec,d0
beq .HunkReloc32
cmp.l #$3fc,d0
beq .HunkReloc32Short
cmp.l #$3f7,d0
beq .HunkDRel32
cmp.l #$3f0,d0
beq .HunkSymbol
cmp.l #$3f2,d0
beq .HunkEnd
; We don't know this hunk
.RelocateFail
IFD HAVE_ERRNO
; Save hunk number in rErrno as error code
lea _rErrno(pc),a0
move.l d0,(a0)
ENDIF
bsr .RelocateCleanup
moveq.l #0,d0 ; NULL = Failure
movem.l (sp)+,d1-d7/a0-a6
rts
; ---------------------
.HunkData
.HunkCode
bsr RomFetch32
move.l d0,d7
subq.l #1,d7
.CopyHunk
bsr RomFetch32
move.l d0,(a0)+
dbra d7,.CopyHunk
bra .HunkLoop
; ---------------------
.HunkBSS
bsr RomFetch32
move.l d0,d7
subq.l #1,d7
; hunk address in a0
.ClearBSS
move.l #$0,(a0)+
dbra d7,.ClearBSS
bra .HunkLoop
; ---------------------
.HunkReloc32
bsr RomFetch32
tst.l d0 ; if len is zero the reloc32 block is done
beq .HunkLoop
move.l d0,d1 ; len ; number of offsets for a given hunk
subq.l #1,d1 ; for dbra
bsr RomFetch32
move.l d0,d2 ; num ; number of the hunk the offsets are to point into
lsl.l #2,d2 ; *4 ; offset from Hunks
move.l pHunks(pc),a2
add.l d2,a2 ; hunk number num
move.l (a2),d3 ; base address of hunk
.Reloc32Tight
bsr RomFetch32
; get baseaddr + d0, add d3 to the data there.
move.l a0,a4
add.l d0,a4
add.l d3,(a4)
dbra d1,.Reloc32Tight
bra .HunkReloc32
; ---------------------
.HunkDRel32
.HunkReloc32Short
bsr RomFetch16
tst.l d0 ; if len is zero the reloc32 block is done
beq .HunkLoopPrep
move.l d0,d1 ; len ; number of offsets for a given hunk
subq.l #1,d1 ; for dbra
bsr RomFetch16
move.l d0,d2 ; num ; number of the hunk the offsets are to point into
lsl.l #2,d2 ; *4 ; offset from Hunks
move.l pHunks(pc),a2
add.l d2,a2 ; hunk number num
move.l (a2),d3 ; base address of hunk
.Reloc32ShortTight
bsr RomFetch16
; get baseaddr + d0, add d3 to the data there.
move.l a0,a4
add.l d0,a4
add.l d3,(a4)
dbra d1,.Reloc32ShortTight
bra .HunkReloc32Short
.HunkLoopPrep
lea ReadHandle(pc),a2
move.l (a2),d0 ; align pointer to longword
addq.l #2,d0 ; in case we had an odd number
and.l #$fffffffc,d0 ; of relocations
move.l d0,(a2)
bra .HunkLoop
; ---------------------
.HunkSymbol
bsr RomFetch32
tst.l d0
beq .HunkLoop
move.l d0,d7
.SkipSymbol
bsr RomFetch32
dbra d7,.SkipSymbol
bra .HunkSymbol
; ---------------------
.HunkEnd
addq.l #4,a5
dbra d6,.HunkLoop
IFD HAVE_ERRNO
lea _rErrno(pc),a0
move.l #0,(a0)
ENDIF
bsr.s CreateSegList
move.l pHunks(pc),a0
move.l (a0),d2
subq.l #4,d2
bsr.s .RelocateCleanup
move.l d2,d0
movem.l (sp)+,d1-d7/a0-a6
rts
.RelocateCleanup
move.l pHunks(pc),a1
moveq.l #(8*NUM_ENTRIES),d0
jsr _LVOFreeMem(a6)
rts
; ---------------------
; +--------------------+
; | seg length (longs) | <-- = pHunks[x+NUM_ENTRIES]
; +--------------------+ <-- seglist returned
; | bptr to next seg | <-- = pHunks[x+1]
; +--------------------+ <-- pHunks[x] points here
; | segment data (also |
; | first entry point) |
; |/\/\/\/\/\/\/\/\/\/\|
; pHunks -+ Hunk Pointers Hunk Lengths
; | +--+--+--+--+ +--+--+--+--+
; +->| | | | 0| | | | | 0|
; +--+--+--+--+ +--+--+--+--+
CreateSegList:
move.l pHunks(pc),a0
.NextSeg
move.l (a0),a1 ; Hunks[x]
move.l 4*NUM_ENTRIES(a0),d0 ; length of current hunk
addq.l #8,d0 ; add header size
lsr.l #2,d0 ; MKBADDR
move.l d0,-8(a1) ; write seg size
addq.l #4,a0
move.l (a0),d0 ; next hunk
tst.l d0 ; hunk addr zero?
beq .SegListDone ; we're done
subq.l #4,d0 ; else: point to linked list
lsr.l #2,d0 ; MKBADDR
move.l d0,-4(a1) ; write BPTR to next hunk
bra.s .NextSeg
.SegListDone
move.l d0,-4(a1) ; terminate seglist
rts
; ---------------------
; data = RomFetch32At(offset)
; d0 d0
RomFetch32At:
movem.l a0-a1,-(sp)
lea ReadHandle(pc),a1
move.l 8(a1),a0 ; base address
lsl.l #2,d0
add.l d0,a0
move.l a0,(a1) ; base address
movem.l (sp)+,a0-a1
bra RomFetch32
; data = RomFetch32(void)
; d0
RomFetch16
moveq #1,d0
bra.s RomFetch
RomFetch32
moveq #3,d0
RomFetch
movem.l a0-a1/d1-d3,-(sp)
lea ReadHandle(pc),a1
move.l (a1),a0
tst.l 4(a1) ; access type ZorroIII?
bne.s .RomFetchZ3
cmp.b #3,d0
bne.b .RomFetchWord
move.l (a0)+,d0 ; access type is memory
bra.s .RomFetchDone
.RomFetchWord
move.w (a0)+,d0
bra.s .RomFetchDone
.RomFetchZ3 ; access type is ZorroIII
move.l d0,d3
.nextnip
lsl.l #8,d0
move.b (a0),d1
and.l #$f0,d1
move.b 2(a0),d2
and.l #$f0,d2
lsr.w #4,d2
or.b d1,d2
move.b d2,d0
addq #4,a0
dbra d3,.nextnip
.RomFetchDone
move.l a0,(a1)
movem.l (sp)+,a0-a1/d1-d3
rts
InitHandle
; initialize readhandle to beginning of device driver
; ROM_OFFSET needs to be multiplied by 4 because of the
; nibble mapped nature of the AutoConfig ROM.
; ROM_ADDRESS is passed in a0
; ROM_OFFSET is passed in d0
movem.l a0-a1,-(sp)
lsl.l #2,d0
lea ReadHandle(pc),a1
move.l a0,8(a1) ; base address
add.l d0,a0
move.l a0,(a1) ; memory location passed in a0
move.l d0,4(a1) ; Set access type: 0 = memory, Z3 otherwise
movem.l (sp)+,a0-a1
rts
ReadHandle
dc.l 0 ; Current address
dc.l 0 ; 0=memory, otherwise z3
dc.l 0 ; base
pHunks
dc.l 0
IFD HAVE_ERRNO
public _rErrno
_rErrno
dc.l 0
ENDIF
INCLUDE "rnc.S"
CODE