File: file.c - Tab length: 1 2 4 8 - Lines: on off - No wrap: on off

001: #include <lynx.h>
002: #include <lynxlib.h>
003:  
004: #asm
005: *************************************************
006: * File - functions
007: *************************************************
008: ; exported functions,labels:
009:                 global _FileEntry
010:                 global _LoadFileTo
011:                 global _OpenAni
012:                 global _LoadAni
013:                 global FileReadByte
014:  
015:  
016:                 xref popax,tstax
017:                 xref __iodat
018:                
019: ;;                xref PUCrunch
020:  
021: ;;; /// Zeropage
022:         bsszp
023: FileCurrBlock   ds 1
024: FileBlockByte   ds 2
025: FileDestPtr     ds 2
026:  
027: _FileEntry       ;; same
028: FileStartBlock   ds 1
029: FileBlockOffset  ds 2
030: FileExecFlag    ds 1
031: FileDestAddr    ds 2
032: FileFileLen     ds 2
033:  
034: ;;;a0      = $C0 ; 2
035: ;;;    xref Explode_Data
036:  
037: ;;;/// normal Memory
038:         bss
039: ;;;/// normal Memory
040:         text
041:  
042:  
043: ;*******************************************
044: ; _OpenAni(uchar bFileNr)
045: _OpenAni:
046:     jsr enterfun
047:  
048:     ldy #2
049:     jsr ldaxysp
050:  
051:     jsr FileOpenFile
052:  
053:     ldy #0
054:     jmp exitfun
055:  
056: ;*******************************************
057: ; _LoadAni(char *FileAddr)
058: _LoadAni:
059:     jsr enterfun
060:  
061:     ldy #2
062:     jsr ldaxysp
063:  
064:     sta FileDestPtr                ;  lo
065:     stx FileDestPtr+1              ;  hi
066:  
067:       jsr FileReadByte
068:       tax
069:       jsr FileReadByte
070:       tay
071:  
072:       ; �bergabe X:Y xor FFFF
073:       jsr FileReadBytes
074:    
075:     ldy #0
076:     jmp exitfun
077:  
078:  
079: ;*******************************************
080: ; void LoadFileTo(uchar bFileNr,char *FileAddr)
081: ; loads the specified directory entry
082: ; into the global structure FileEntry
083: _LoadFileTo:
084:     jsr enterfun
085:  
086:     ldy #2
087:     jsr ldaxysp
088:  
089:     sta FileDestPtr                ;  lo
090:     stx FileDestPtr+1              ;  hi
091:  
092:     ldy #4
093:     jsr ldaxysp
094:  
095:     jsr FileLoadFile
096:  
097:     ldy #0
098:     jmp exitfun
099:  
100:  
101: ;**********************************
102: ; Load directory-entry
103: ; In: Accu = entry number
104: ; X: This is only Called from FileOpenFile,
105: ; where x is anyway overwritten
106: ;**********************************
107: FileLoadDir:    stz FileCurrBlock
108:                 jsr FileSelectBlock
109:  
110:                 tax ; Sichere Nummer
111:                 lsr A ; *8, oberes Byte
112:                 lsr A
113:                 lsr A
114:                 lsr A
115:                 lsr A
116:                 pha
117:                 txa
118:                 asl A ; *8, unteres Byte
119:                 asl A
120:                 asl A
121:                 clc             ; over-read the cart-header
122:                 adc #<203   ; 410   ; $380 was old header
123:                 eor #$ff
124:                 tax
125:                 pla ; oberes byte mit CARRY addieren
126:                 adc #>203   ; 410   ; $380 was old header
127:                 eor #$ff
128:                 tay
129:                 jsr FileReadOver
130:  
131:                 ldx #0
132:                 ldy #8
133: ploopLD         jsr FileReadByte
134:                 sta _FileEntry,x
135:                 inx
136:                 dey
137:                 bne ploopLD
138:                 rts
139:  
140: ;**********************************
141: ; Open file, i.e. select block, over-read offset, set DestPtr
142: ; In: Accu = file number
143: ; Out: X:Y = file length
144: ;**********************************
145: FileOpenFile:   jsr FileLoadDir
146:                 lda FileDestAddr
147:                 ora FileDestAddr+1      ; dest == 0?
148:                 bne pcont0              ; no =>
149:  
150:                 lda FileDestPtr
151:                 sta FileDestAddr
152:                 lda FileDestPtr+1
153:                 sta FileDestAddr+1
154:                 bra pcont1
155:  
156: pcont0:
157:                 lda FileDestAddr
158:                 sta FileDestPtr
159:                 lda FileDestAddr+1
160:                 sta FileDestPtr+1
161:  
162: pcont1:         lda FileStartBlock
163:                 sta FileCurrBlock       ; set startblock
164:                 jsr FileSelectBlock
165:  
166:                 ldx FileBlockOffset
167:                 ldy FileBlockOffset+1   ; set offset
168:                 jsr FileReadOver
169:  
170:                 ldx FileFileLen
171:                 ldy FileFileLen+1       ; return file length
172:                 rts
173:  
174: ;**********************************
175: ; load file
176: ; In: Accu = file number
177: ;**********************************
178: FileLoadFile:   jsr FileOpenFile        ; set position in cartridge and
179:                                         ; get file length in X:Y
180: ;;                lda FileExecFlag
181: ;;        cmp #'P'
182: ;;        beq l_puc
183:           jsr FileReadBytes       ; read the whole file to its DestAddr
184: ;;          bra l_end
185: ;;l_puc
186: ;;          ldx FileDestPtr        ; set destination address
187: ;;          ldy FileDestPtr+1
188: ;;          jsr PUCrunch       ; read the whole file to its DestAddr
189: ;;l_end                
190:                 ldx FileDestAddr        ; return destination address
191:                 ldy FileDestAddr+1
192:                 rts
193:  
194:  
195: ;**********************************
196: ; Overread some bytes
197: ; X:Y count (EOR $FFFF)
198: ;**********************************
199: FileReadOver:   inx
200:                 bne qcont0
201:                 iny
202:                 beq pexit
203: qcont0          jsr FileReadByte
204:                 bra FileReadOver
205:  
206: ;**********************************
207: ; load to DestPtr
208: ; X:Y count (EOR $ffff)
209: ;**********************************
210: FileReadBytes:  inx
211:                 bne qcont1
212:                 iny
213:                 beq pexit
214: qcont1:         jsr FileReadByte       ; one byte from the cartridge
215:                 sta (FileDestPtr)
216:                 inc FileDestPtr
217:                 bne FileReadBytes
218:                 inc FileDestPtr+1
219:                 bra FileReadBytes
220:  
221: ;**********************************
222: ; Read one byte from cartridge
223: ;**********************************
224: FileReadByte:   lda $fcb2
225:                 inc FileBlockByte
226:                 bne pexit
227:                 inc FileBlockByte+1
228:                 bne pexit
229:  
230: ;**********************************
231: ; Select a block
232: ;**********************************
233: FileSelectBlock:
234:                 pha
235:                 phx
236:                 phy
237:                 lda __iodat
238:                 and #$fc
239:                 tay
240:                 ora #2
241:                 tax
242:                 lda FileCurrBlock
243:                 inc FileCurrBlock
244:                 sec
245:                 bra SLB2
246:  
247: SLB0:           bcc SLB1
248:                 stx $fd8b
249:                 clc
250:  
251: SLB1:           inx
252:                 stx $fd87
253:                 dex
254:  
255: SLB2:           stx $fd87
256:                 rol a ; A
257:                 sty $fd8b
258:                 bne SLB0
259:  
260:                 lda __iodat
261:                 sta $fd8b
262:                 stz FileBlockByte
263:                 lda #$100-(>1024)
264:                 sta FileBlockByte+1
265:                 ply
266:                 plx
267:                 pla
268:  
269: pexit           rts
270:  
271: #endasm