Rev 1878 | Rev 1950 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log
Rev | Author | Line No. | Line |
---|---|---|---|
1797 | galstaff | 1 | memorystreamloadfile |
2 | ;de = file name |
||
3 | ;out: zf=1 if successful, zf=0 otherwise |
||
1945 | galstaff | 4 | ;configurable params: |
5 | ; .pagestoload <= MEMORYSTREAMMAXPAGES |
||
6 | ; .errormask = 0xff to require loading the entire file into memory, 0x00 if only [pagestoload] needed |
||
1797 | galstaff | 7 | call openstream_file |
8 | or a |
||
9 | ret nz |
||
10 | ld hl,memorystreampages |
||
11 | ld (memorystreampageaddr),hl |
||
12 | ld hl,0 |
||
13 | ld de,hl |
||
14 | ld c,l |
||
1945 | galstaff | 15 | .pagestoload=$+1 |
1878 | galstaff | 16 | ld b,MEMORYSTREAMMAXPAGES |
1797 | galstaff | 17 | .loadloop |
18 | push bc |
||
19 | push de |
||
20 | push hl |
||
21 | OS_NEWPAGE |
||
22 | or a |
||
23 | jr z,.pageallocated |
||
24 | pop hl |
||
25 | pop de |
||
26 | pop bc |
||
27 | jr .breakloop |
||
28 | .pageallocated |
||
29 | ld hl,(memorystreampageaddr) |
||
30 | ld (hl),e |
||
31 | inc hl |
||
32 | ld (memorystreampageaddr),hl |
||
33 | ld a,e |
||
34 | SETPG8000 |
||
35 | ld de,0x8000 |
||
36 | ld hl,0x4000 |
||
37 | call readstream_file |
||
38 | ex (sp),hl |
||
39 | pop bc |
||
40 | add hl,bc |
||
41 | pop de |
||
42 | jr nc,$+3 |
||
43 | inc e |
||
44 | ld a,b |
||
45 | pop bc |
||
46 | inc c |
||
47 | and 0x40 |
||
48 | jr z,.breakloop |
||
49 | djnz .loadloop |
||
1870 | galstaff | 50 | .errormask=$+1 |
1945 | galstaff | 51 | and 255 |
1797 | galstaff | 52 | .breakloop |
53 | push af |
||
54 | ld (memorystreamsize+0),hl |
||
55 | ld (memorystreamsize+2),de |
||
56 | ld a,c |
||
57 | ld (memorystreampagecount),a |
||
58 | call closestream_file |
||
59 | pop af |
||
60 | ret z |
||
61 | jp memorystreamfree |
||
62 | |||
63 | memorystreamallocate |
||
64 | ;dehl = buffer size |
||
65 | ;out: zf=1 if successful, zf=0 otherwise |
||
66 | ld (memorystreamsize+0),hl |
||
67 | ld (memorystreamsize+2),de |
||
68 | ld a,e |
||
69 | ld de,0x3fff |
||
70 | add hl,de |
||
71 | ld c,0 |
||
72 | adc a,c |
||
73 | sla h |
||
74 | rla |
||
75 | sla h |
||
76 | rla |
||
77 | ld b,a |
||
78 | ld a,MEMORYSTREAMMAXPAGES |
||
79 | cp b |
||
80 | ret c |
||
81 | ld hl,memorystreampages |
||
82 | .loop |
||
83 | push bc |
||
84 | push hl |
||
85 | OS_NEWPAGE |
||
86 | pop hl |
||
87 | pop bc |
||
88 | or a |
||
89 | jr z,.pageallocated |
||
90 | ld a,c |
||
91 | ld (memorystreampagecount),a |
||
92 | jp memorystreamfree |
||
93 | |||
94 | .pageallocated |
||
95 | ld (hl),e |
||
96 | inc hl |
||
97 | inc c |
||
98 | djnz .loop |
||
99 | ld a,c |
||
100 | ld (memorystreampagecount),a |
||
101 | xor a |
||
102 | ret |
||
103 | |||
104 | memorystreamfree |
||
105 | ;out: zf=0 so that this function can be used to return error condition |
||
106 | memorystreampagecount=$+1 |
||
107 | ld a,0 |
||
108 | or a |
||
109 | ret z |
||
110 | ld b,a |
||
111 | ld hl,memorystreampages |
||
112 | .pagefreeloop |
||
113 | push bc |
||
114 | push hl |
||
115 | ld e,(hl) |
||
116 | OS_DELPAGE |
||
117 | pop hl |
||
118 | pop bc |
||
119 | inc hl |
||
120 | djnz .pagefreeloop |
||
121 | inc b |
||
122 | ret |
||
123 | |||
124 | memorystreamstart |
||
125 | ld hl,0xffff |
||
126 | ld (memorystreamcurrentaddr),hl |
||
127 | ld hl,memorystreampages |
||
128 | ld (memorystreampageaddr),hl |
||
129 | ret |
||
130 | |||
131 | memorystreamnextpage |
||
132 | memorystreampageaddr=$+1 |
||
133 | ld hl,0 |
||
134 | push af |
||
135 | ld a,(hl) |
||
136 | inc hl |
||
137 | ld (memorystreamcurrentpage),a |
||
138 | ld (memorystreampageaddr),hl |
||
139 | push bc |
||
140 | SETPG8000 |
||
141 | pop bc |
||
142 | pop af |
||
143 | ld hl,0x8000 |
||
144 | ret |
||
145 | |||
146 | memorystreamskip |
||
147 | ;b = byte count |
||
148 | ld hl,(memorystreamcurrentaddr) |
||
149 | .loop |
||
150 | bit 6,h |
||
151 | call nz,memorystreamnextpage |
||
152 | inc hl |
||
153 | djnz .loop |
||
154 | ld (memorystreamcurrentaddr),hl |
||
155 | ret |
||
156 | |||
157 | macro memory_stream_write_byte src |
||
158 | bit 6,h |
||
159 | call nz,memorystreamnextpage |
||
160 | ld (hl),src |
||
161 | inc hl |
||
162 | endm |
||
163 | |||
164 | macro memory_stream_read_byte dest |
||
165 | bit 6,h |
||
166 | call nz,memorystreamnextpage |
||
167 | ld dest,(hl) |
||
168 | inc hl |
||
169 | endm |
||
170 | |||
171 | macro memory_stream_read_1 dst |
||
172 | ld hl,(memorystreamcurrentaddr) |
||
173 | memory_stream_read_byte dst |
||
174 | ld (memorystreamcurrentaddr),hl |
||
175 | endm |
||
176 | |||
177 | macro memory_stream_read_2 dst1,dst2 |
||
178 | ld hl,(memorystreamcurrentaddr) |
||
179 | memory_stream_read_byte dst1 |
||
180 | memory_stream_read_byte dst2 |
||
181 | ld (memorystreamcurrentaddr),hl |
||
182 | endm |
||
183 | |||
184 | macro memory_stream_read_3 dst1,dst2,dst3 |
||
185 | ld hl,(memorystreamcurrentaddr) |
||
186 | memory_stream_read_byte dst1 |
||
187 | memory_stream_read_byte dst2 |
||
188 | memory_stream_read_byte dst3 |
||
189 | ld (memorystreamcurrentaddr),hl |
||
190 | endm |
||
191 | |||
192 | memorystreamread1 |
||
193 | ;out: a = byte |
||
194 | memory_stream_read_1 a |
||
195 | ret |
||
196 | |||
197 | memorystreamread2 |
||
198 | ;out: de = word |
||
199 | memory_stream_read_2 e,d |
||
200 | ret |
||
201 | |||
202 | memorystreamread3 |
||
203 | ;out: c = byte0, e = byte1, d = byte2 |
||
204 | memory_stream_read_3 c,e,d |
||
205 | ret |
||
206 | |||
207 | memorystreamread4 |
||
208 | ;out: adbc = dword |
||
209 | memorystreamcurrentaddr=$+1 |
||
210 | ld hl,0 |
||
211 | memory_stream_read_byte c |
||
212 | memory_stream_read_byte b |
||
213 | memory_stream_read_byte d |
||
214 | memory_stream_read_byte a |
||
215 | ld (memorystreamcurrentaddr),hl |
||
216 | ret |
||
217 | |||
218 | memorystreamread |
||
219 | ;bc = number of bytes |
||
220 | ;de = dest addr |
||
221 | ld a,c |
||
222 | dec bc |
||
223 | inc b |
||
224 | ld c,b |
||
225 | ld b,a |
||
226 | ld hl,(memorystreamcurrentaddr) |
||
227 | .readloop |
||
228 | memory_stream_read_byte a |
||
229 | ld (de),a |
||
230 | inc de |
||
231 | djnz .readloop |
||
232 | dec c |
||
233 | jr nz,.readloop |
||
234 | ld (memorystreamcurrentaddr),hl |
||
235 | ret |
||
236 | |||
237 | memorystreamwrite |
||
238 | ;bc = number of bytes |
||
239 | ;de = src addr |
||
240 | ld a,c |
||
241 | dec bc |
||
242 | inc b |
||
243 | ld c,b |
||
244 | ld b,a |
||
245 | ld hl,(memorystreamcurrentaddr) |
||
246 | .writeloop |
||
247 | ld a,(de) |
||
248 | memory_stream_write_byte a |
||
249 | inc de |
||
250 | djnz .writeloop |
||
251 | dec c |
||
252 | jr nz,.writeloop |
||
253 | ld (memorystreamcurrentaddr),hl |
||
254 | ret |
||
255 | |||
256 | memorystreamseek |
||
257 | ;dehl = absolute position |
||
258 | ;out: hl = read address |
||
259 | ld a,e |
||
260 | ld b,h |
||
261 | sla b |
||
262 | rla |
||
263 | sla b |
||
264 | rla |
||
265 | add a,memorystreampages%256 |
||
266 | ld e,a |
||
267 | adc a,memorystreampages/256 |
||
268 | sub e |
||
269 | ld d,a |
||
270 | ld a,(de) |
||
271 | ld (memorystreamcurrentpage),a |
||
272 | inc de |
||
273 | ld (memorystreampageaddr),de |
||
274 | SETPG8000 |
||
275 | res 6,h |
||
276 | set 7,h |
||
277 | ld (memorystreamcurrentaddr),hl |
||
278 | ret |
||
279 | |||
280 | memorystreamgetpos |
||
281 | ;out: dehl = absolute position |
||
282 | ld hl,(memorystreampageaddr) |
||
283 | ld de,-memorystreampages-1 |
||
284 | add hl,de |
||
285 | ex de,hl |
||
286 | ld hl,(memorystreamcurrentaddr) |
||
287 | res 7,h |
||
288 | bit 6,h |
||
289 | jr z,$+6 |
||
290 | inc de |
||
291 | ld hl,0 |
||
292 | xor a |
||
293 | rr e |
||
294 | rra |
||
295 | rr e |
||
296 | rra |
||
297 | or h |
||
298 | ld h,a |
||
299 | ret |
||
300 | |||
301 | memorystreamsize |
||
302 | ds 4 |
||
303 | memorystreampages |
||
304 | ds MEMORYSTREAMMAXPAGES |
||
305 | memorystreamcurrentpage |
||
306 | ds 1 |