blob: 25234a46cdc202cf99c32d4527d64afc13e33a83 [file] [log] [blame]
kthacker6de67752006-04-17 15:02:26 +00001 .area _CODE
2 .globl _get_file_length
3 .globl _load_file
4;;----------------------------------------------------------------------------
5;; get length of file on disc. Assumption file has a AMSDOS header
6;;
7;; int get_file_length(const char *filename);
8
9_get_file_length::
10 ld hl,#2
11 add hl,sp
12 ld a,(hl)
13 inc hl
14 ld h,(hl)
15 ld l,a
16
Adrien Destuguesdd007ce2020-12-17 21:49:04 +010017 push ix
18
kthacker6de67752006-04-17 15:02:26 +000019 ;; HL = address of null terminated string
20 call count_string_length
21 ld de,#0x0c000 ;; points to unused 2k buffer
22 call 0x0bc77 ;; cas in open
23 push bc ;; BC = length of file
24 call 0x0bc7d ;; cas in abandon
25 pop hl
Adrien Destuguesdd007ce2020-12-17 21:49:04 +010026
27 pop ix
kthacker6de67752006-04-17 15:02:26 +000028 ret
29
30;;---------------------------------------------------------------------
31
32count_string_length:
33 push hl
34 ld b,#0
35csl: ld a,(hl)
36 or a
37 jr z,csl2
38 inc hl
39 inc b
40 jr csl
41csl2:
42 pop hl
43 ret
44
45;;---------------------------------------------------------------------------
46;; void load_file(const char *filename, void *addr)
47
48_load_file::
49 ld hl,#5
50 add hl,sp
51 ld d,(hl)
52 dec hl
53 ld e,(hl)
54 dec hl
55
kthacker6de67752006-04-17 15:02:26 +000056 ld a,(hl)
57 dec hl
58 ld l,(hl)
59 ld h,a
60
Adrien Destuguesdd007ce2020-12-17 21:49:04 +010061 push ix
62
kthacker6de67752006-04-17 15:02:26 +000063 call count_string_length
Adrien Destuguesdd007ce2020-12-17 21:49:04 +010064 push de
kthacker6de67752006-04-17 15:02:26 +000065 ld de,#0x0c000
66 call 0x0bc77 ;; cas in open
67 pop hl ;; load address
68 call 0x0bc83 ;; cas in direct
69 call 0x0bc7a ;; cas in close
Adrien Destuguesdd007ce2020-12-17 21:49:04 +010070
71 pop ix
72
kthacker6de67752006-04-17 15:02:26 +000073 ret
74
75;; void relocate(void *addr,void *base)
76
77;; IX = address of relocate data
78_relocate::
79 ld hl,#5
80 add hl,sp
81 push ix
82 ld b,(hl) ;; base address
83 dec hl
84 ld c,(hl)
85 dec hl
86 ld a,(hl)
87 .db #0x0dd
88 ld h,a
89 dec hl
90 ld a,(hl)
91 .db #0x0dd
92 ld l,a ;; IX is offset of table from start of loaded file
93 add ix,bc ;; relocate IX to give absolute address of table.
94
95 push bc
96 pop hl
97 call relocate_16bit
98 push bc
99 pop hl
100 call relocate_8bitl ;; lower byte
101 push bc
102 pop hl
103 call relocate_8bith ;; upper byte
104 pop ix
105 ret
106
107;;--------------------------------------------------------------------------
108;; Relocate 8-bit values (e.g. where low and high parts of an address
109;; are loaded seperatly into registers)
110;;
111;; IX = list of 16-bit addresses. Each address identifies an 8-bit
112;; value
113;;
114relocate_8bith:
115ld a,0(ix)
116inc ix
117or a
118ret z
119cp #0x0ff
120jr nz,r8bh
121ld e,0(ix)
122inc ix
123ld d,0(ix)
124inc ix
125add hl,de
126jr relocate_8bith
127
128r8bh:
129;; add offset
130add a,l
131ld l,a
132ld a,h
133adc a,#0x0
134ld h,a
135
136;; get low byte of address
137ld e,0(ix)
138inc ix
139
140;; get high byte to relocate
141ld d,(hl)
142ex de,hl
143add hl,bc
144ex de,hl
145ld (hl),d
146jr relocate_8bith
147
148relocate_8bitl:
149ld a,0(ix)
150inc ix
151or a
152ret z
153cp #0x0ff
154jr nz,r8bl
155ld e,0(ix)
156inc ix
157ld d,0(ix)
158inc ix
159add hl,de
160jr relocate_8bitl
161
162r8bl:
163add a,l
164ld l,a
165ld a,h
166adc a,#0x0
167ld h,a
168
169ld e,(hl)
170ld d,#0x0
171ex de,hl
172add hl,bc
173ex de,hl
174ld (hl),e
175jr relocate_8bitl
176
177;;--------------------------------------------------------------------------
178;; Relocate 16-bit values
179;;
180;; Entry conditions:
181;;
182;; IX = list of 16-bit addresses. Each address identifies a 16-bit
183;; value to relocate.
184;;
185;; BC = base address
186;;
187;; NOTE:
188;; - Relocatable 16-bit values come from CALL and JP instructions and
189;; loading a 16-bit register.
190
191relocate_16bit:
192ld a,0(ix) ;; number of items to relocate
193inc ix
194or a
195ret z
196cp #0x0ff
197jr nz,r16
198ld e,0(ix)
199inc ix
200ld d,0(ix)
201inc ix
202add hl,de
203jr relocate_16bit
204
205r16:
206;; add offset
207add a,l
208ld l,a
209ld a,h
210adc a,#0x0
211ld h,a
212
213;; get the 16-bit value
214ld e,(hl)
215inc hl
216ld d,(hl)
217
218
219;; add base address; therefore relocating it.
220ex de,hl
221add hl,bc
222ex de,hl
223;; write relocated value
224ld (hl),d
225dec hl
226ld (hl),e
227jr relocate_16bit
228