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