Subversion Repositories NedoOS

Rev

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