#include <lynx.h>
#include <lynxlib.h>
#asm
*************************************************
* File - functions
*************************************************
; exported functions,labels:
global _FileEntry
global _LoadFileTo
global _OpenAni
global _LoadAni
global FileReadByte
xref popax,tstax
xref __iodat
;; xref PUCrunch
;;; /// Zeropage
bsszp
FileCurrBlock ds 1
FileBlockByte ds 2
FileDestPtr ds 2
_FileEntry ;; same
FileStartBlock ds 1
FileBlockOffset ds 2
FileExecFlag ds 1
FileDestAddr ds 2
FileFileLen ds 2
;;;a0 = $C0 ; 2
;;; xref Explode_Data
;;;/// normal Memory
bss
;;;/// normal Memory
text
;*******************************************
; _OpenAni(uchar bFileNr)
_OpenAni:
jsr enterfun
ldy #2
jsr ldaxysp
jsr FileOpenFile
ldy #0
jmp exitfun
;*******************************************
; _LoadAni(char *FileAddr)
_LoadAni:
jsr enterfun
ldy #2
jsr ldaxysp
sta FileDestPtr ; lo
stx FileDestPtr+1 ; hi
jsr FileReadByte
tax
jsr FileReadByte
tay
; �bergabe X:Y xor FFFF
jsr FileReadBytes
ldy #0
jmp exitfun
;*******************************************
; void LoadFileTo(uchar bFileNr,char *FileAddr)
; loads the specified directory entry
; into the global structure FileEntry
_LoadFileTo:
jsr enterfun
ldy #2
jsr ldaxysp
sta FileDestPtr ; lo
stx FileDestPtr+1 ; hi
ldy #4
jsr ldaxysp
jsr FileLoadFile
ldy #0
jmp exitfun
;**********************************
; Load directory-entry
; In: Accu = entry number
; X: This is only Called from FileOpenFile,
; where x is anyway overwritten
;**********************************
FileLoadDir: stz FileCurrBlock
jsr FileSelectBlock
tax ; Sichere Nummer
lsr A ; *8, oberes Byte
lsr A
lsr A
lsr A
lsr A
pha
txa
asl A ; *8, unteres Byte
asl A
asl A
clc ; over-read the cart-header
adc #<203 ; 410 ; $380 was old header
eor #$ff
tax
pla ; oberes byte mit CARRY addieren
adc #>203 ; 410 ; $380 was old header
eor #$ff
tay
jsr FileReadOver
ldx #0
ldy #8
ploopLD jsr FileReadByte
sta _FileEntry,x
inx
dey
bne ploopLD
rts
;**********************************
; Open file, i.e. select block, over-read offset, set DestPtr
; In: Accu = file number
; Out: X:Y = file length
;**********************************
FileOpenFile: jsr FileLoadDir
lda FileDestAddr
ora FileDestAddr+1 ; dest == 0?
bne pcont0 ; no =>
lda FileDestPtr
sta FileDestAddr
lda FileDestPtr+1
sta FileDestAddr+1
bra pcont1
pcont0:
lda FileDestAddr
sta FileDestPtr
lda FileDestAddr+1
sta FileDestPtr+1
pcont1: lda FileStartBlock
sta FileCurrBlock ; set startblock
jsr FileSelectBlock
ldx FileBlockOffset
ldy FileBlockOffset+1 ; set offset
jsr FileReadOver
ldx FileFileLen
ldy FileFileLen+1 ; return file length
rts
;**********************************
; load file
; In: Accu = file number
;**********************************
FileLoadFile: jsr FileOpenFile ; set position in cartridge and
; get file length in X:Y
;; lda FileExecFlag
;; cmp #'P'
;; beq l_puc
jsr FileReadBytes ; read the whole file to its DestAddr
;; bra l_end
;;l_puc
;; ldx FileDestPtr ; set destination address
;; ldy FileDestPtr+1
;; jsr PUCrunch ; read the whole file to its DestAddr
;;l_end
ldx FileDestAddr ; return destination address
ldy FileDestAddr+1
rts
;**********************************
; Overread some bytes
; X:Y count (EOR $FFFF)
;**********************************
FileReadOver: inx
bne qcont0
iny
beq pexit
qcont0 jsr FileReadByte
bra FileReadOver
;**********************************
; load to DestPtr
; X:Y count (EOR $ffff)
;**********************************
FileReadBytes: inx
bne qcont1
iny
beq pexit
qcont1: jsr FileReadByte ; one byte from the cartridge
sta (FileDestPtr)
inc FileDestPtr
bne FileReadBytes
inc FileDestPtr+1
bra FileReadBytes
;**********************************
; Read one byte from cartridge
;**********************************
FileReadByte: lda $fcb2
inc FileBlockByte
bne pexit
inc FileBlockByte+1
bne pexit
;**********************************
; Select a block
;**********************************
FileSelectBlock:
pha
phx
phy
lda __iodat
and #$fc
tay
ora #2
tax
lda FileCurrBlock
inc FileCurrBlock
sec
bra SLB2
SLB0: bcc SLB1
stx $fd8b
clc
SLB1: inx
stx $fd87
dex
SLB2: stx $fd87
rol a ; A
sty $fd8b
bne SLB0
lda __iodat
sta $fd8b
stz FileBlockByte
lda #$100-(>1024)
sta FileBlockByte+1
ply
plx
pla
pexit rts
#endasm