blob: ce4bac66424727c402ef246f4eae7d5e66d84630 [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);
PulkoMandy3783e422023-05-02 17:40:17 +02008;; Input: HL points to filename
9;; Output: DE contains file length
kthacker6de67752006-04-17 15:02:26 +000010_get_file_length::
kthacker6de67752006-04-17 15:02:26 +000011
PulkoMandy3783e422023-05-02 17:40:17 +020012 ;; IX is modified by CAS routines
Adrien Destuguesdd007ce2020-12-17 21:49:04 +010013 push ix
14
kthacker6de67752006-04-17 15:02:26 +000015 ;; HL = address of null terminated string
16 call count_string_length
17 ld de,#0x0c000 ;; points to unused 2k buffer
18 call 0x0bc77 ;; cas in open
19 push bc ;; BC = length of file
20 call 0x0bc7d ;; cas in abandon
PulkoMandy3783e422023-05-02 17:40:17 +020021 pop de ;; Now DE = length of file
Adrien Destuguesdd007ce2020-12-17 21:49:04 +010022
23 pop ix
kthacker6de67752006-04-17 15:02:26 +000024 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)
PulkoMandy3783e422023-05-02 17:40:17 +020043;; INPUT: HL = filename, DE = addr
kthacker6de67752006-04-17 15:02:26 +000044_load_file::
Adrien Destuguesdd007ce2020-12-17 21:49:04 +010045 push ix
46
kthacker6de67752006-04-17 15:02:26 +000047 call count_string_length
PulkoMandy3783e422023-05-02 17:40:17 +020048 push de ; Save load address
kthacker6de67752006-04-17 15:02:26 +000049 ld de,#0x0c000
50 call 0x0bc77 ;; cas in open
PulkoMandy3783e422023-05-02 17:40:17 +020051 pop hl ;; load address now in HL
kthacker6de67752006-04-17 15:02:26 +000052 call 0x0bc83 ;; cas in direct
53 call 0x0bc7a ;; cas in close
Adrien Destuguesdd007ce2020-12-17 21:49:04 +010054
55 pop ix
56
kthacker6de67752006-04-17 15:02:26 +000057 ret
58
59;; void relocate(void *addr,void *base)
PulkoMandy3783e422023-05-02 17:40:17 +020060;; INPUT: HL = addr, DE = base
kthacker6de67752006-04-17 15:02:26 +000061;; IX = address of relocate data
62_relocate::
PulkoMandy3783e422023-05-02 17:40:17 +020063
kthacker6de67752006-04-17 15:02:26 +000064 push ix
PulkoMandy3783e422023-05-02 17:40:17 +020065
66 ; Transfer base address to BC
67 PUSH DE
68 POP BC
69
70 ; Transfer addr to IX
71 PUSH HL
72 POP IX
73
kthacker6de67752006-04-17 15:02:26 +000074 add ix,bc ;; relocate IX to give absolute address of table.
75
76 push bc
77 pop hl
78 call relocate_16bit
79 push bc
80 pop hl
81 call relocate_8bitl ;; lower byte
82 push bc
83 pop hl
84 call relocate_8bith ;; upper byte
PulkoMandy3783e422023-05-02 17:40:17 +020085
kthacker6de67752006-04-17 15:02:26 +000086 pop ix
87 ret
88
89;;--------------------------------------------------------------------------
90;; Relocate 8-bit values (e.g. where low and high parts of an address
91;; are loaded seperatly into registers)
92;;
93;; IX = list of 16-bit addresses. Each address identifies an 8-bit
94;; value
95;;
96relocate_8bith:
97ld a,0(ix)
98inc ix
99or a
100ret z
101cp #0x0ff
102jr nz,r8bh
103ld e,0(ix)
104inc ix
105ld d,0(ix)
106inc ix
107add hl,de
108jr relocate_8bith
109
110r8bh:
111;; add offset
112add a,l
113ld l,a
114ld a,h
115adc a,#0x0
116ld h,a
117
118;; get low byte of address
119ld e,0(ix)
120inc ix
121
122;; get high byte to relocate
123ld d,(hl)
124ex de,hl
125add hl,bc
126ex de,hl
127ld (hl),d
128jr relocate_8bith
129
130relocate_8bitl:
131ld a,0(ix)
132inc ix
133or a
134ret z
135cp #0x0ff
136jr nz,r8bl
137ld e,0(ix)
138inc ix
139ld d,0(ix)
140inc ix
141add hl,de
142jr relocate_8bitl
143
144r8bl:
145add a,l
146ld l,a
147ld a,h
148adc a,#0x0
149ld h,a
150
151ld e,(hl)
152ld d,#0x0
153ex de,hl
154add hl,bc
155ex de,hl
156ld (hl),e
157jr relocate_8bitl
158
159;;--------------------------------------------------------------------------
160;; Relocate 16-bit values
161;;
162;; Entry conditions:
163;;
164;; IX = list of 16-bit addresses. Each address identifies a 16-bit
165;; value to relocate.
166;;
167;; BC = base address
168;;
169;; NOTE:
170;; - Relocatable 16-bit values come from CALL and JP instructions and
171;; loading a 16-bit register.
172
173relocate_16bit:
174ld a,0(ix) ;; number of items to relocate
175inc ix
176or a
177ret z
178cp #0x0ff
179jr nz,r16
180ld e,0(ix)
181inc ix
182ld d,0(ix)
183inc ix
184add hl,de
185jr relocate_16bit
186
187r16:
188;; add offset
189add a,l
190ld l,a
191ld a,h
192adc a,#0x0
193ld h,a
194
195;; get the 16-bit value
196ld e,(hl)
197inc hl
198ld d,(hl)
199
200
201;; add base address; therefore relocating it.
202ex de,hl
203add hl,bc
204ex de,hl
205;; write relocated value
206ld (hl),d
207dec hl
208ld (hl),e
209jr relocate_16bit
210