2005-04-12 09:12:48 +00:00
|
|
|
;
|
|
|
|
; Oliver Schmidt, 30.12.2004
|
|
|
|
;
|
|
|
|
; int open (const char* name, int flags, ...);
|
|
|
|
;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.export _open, closedirect, freebuffer
|
2013-08-03 17:08:36 +00:00
|
|
|
.export __filetype, __auxtype, __datetime
|
2013-05-09 11:56:54 +00:00
|
|
|
.constructor raisefilelevel
|
|
|
|
.destructor closeallfiles, 5
|
2005-04-12 09:12:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.import pushname, popname, __dos_type
|
|
|
|
.import iobuf_alloc, iobuf_free
|
|
|
|
.import addysp, incsp4, incaxy, pushax, popax
|
2005-04-12 09:12:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.include "zeropage.inc"
|
|
|
|
.include "errno.inc"
|
|
|
|
.include "fcntl.inc"
|
|
|
|
.include "mli.inc"
|
|
|
|
.include "filedes.inc"
|
2005-04-12 09:12:48 +00:00
|
|
|
|
2016-03-06 20:26:22 +00:00
|
|
|
.segment "ONCE"
|
2011-01-29 21:43:26 +00:00
|
|
|
|
|
|
|
raisefilelevel:
|
|
|
|
; Raise file level
|
2013-05-09 11:56:54 +00:00
|
|
|
lda __dos_type
|
|
|
|
beq :+
|
|
|
|
inc LEVEL
|
2011-01-29 21:43:26 +00:00
|
|
|
: rts
|
|
|
|
|
|
|
|
.code
|
|
|
|
|
2005-04-12 09:12:48 +00:00
|
|
|
_open:
|
|
|
|
; Throw away all parameters except name
|
|
|
|
; and flags occupying together 4 bytes
|
|
|
|
dey
|
|
|
|
dey
|
|
|
|
dey
|
|
|
|
dey
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr addysp
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; Start with first fdtab slot
|
2013-05-09 11:56:54 +00:00
|
|
|
ldy #$00
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; Check for free fdtab slot
|
2013-05-09 11:56:54 +00:00
|
|
|
: lda fdtab + FD::REF_NUM,y
|
|
|
|
beq found
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; Advance to next fdtab slot
|
2005-04-20 16:52:18 +00:00
|
|
|
.assert .sizeof(FD) = 4, error
|
2005-04-12 09:12:48 +00:00
|
|
|
iny
|
|
|
|
iny
|
|
|
|
iny
|
|
|
|
iny
|
|
|
|
|
|
|
|
; Check for end of fdtab
|
2013-05-09 11:56:54 +00:00
|
|
|
cpy #MAX_FDS * .sizeof(FD)
|
|
|
|
bcc :-
|
2005-04-12 09:12:48 +00:00
|
|
|
|
2005-08-26 12:09:43 +00:00
|
|
|
; Load errno code
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #EMFILE
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; Cleanup stack
|
2013-05-09 11:56:54 +00:00
|
|
|
errno: jsr incsp4 ; Preserves A
|
2005-04-12 09:12:48 +00:00
|
|
|
|
2010-06-15 20:38:05 +00:00
|
|
|
; Set __errno
|
2022-08-28 19:52:53 +00:00
|
|
|
jmp ___directerrno
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; Save fdtab slot
|
|
|
|
found: tya
|
|
|
|
pha
|
|
|
|
|
|
|
|
; Alloc I/O buffer
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #<(fdtab + FD::BUFFER)
|
|
|
|
ldx #>(fdtab + FD::BUFFER)
|
|
|
|
jsr incaxy
|
|
|
|
jsr pushax
|
|
|
|
lda #$00
|
|
|
|
ldx #>$0100
|
|
|
|
jsr pushax ; Preserves A
|
|
|
|
ldx #>$0400
|
|
|
|
jsr iobuf_alloc
|
|
|
|
tay ; Save errno code
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; Restore fdtab slot
|
|
|
|
pla
|
2013-05-09 11:56:54 +00:00
|
|
|
sta tmp2 ; Save fdtab slot
|
2005-04-12 09:12:48 +00:00
|
|
|
|
2005-08-26 12:09:43 +00:00
|
|
|
; Check for error
|
2013-05-09 11:56:54 +00:00
|
|
|
tya ; Restore errno code
|
|
|
|
bne errno
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; Get and save flags
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr popax
|
|
|
|
sta tmp3
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; Get and push name
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr popax
|
|
|
|
jsr pushname
|
|
|
|
bne oserr1
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; Set pushed name
|
2013-05-09 11:56:54 +00:00
|
|
|
lda sp
|
|
|
|
ldx sp+1
|
|
|
|
sta mliparam + MLI::OPEN::PATHNAME
|
|
|
|
stx mliparam + MLI::OPEN::PATHNAME+1
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; Check for create flag
|
2013-05-09 11:56:54 +00:00
|
|
|
lda tmp3 ; Restore flags
|
|
|
|
and #O_CREAT
|
|
|
|
beq open
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; PATHNAME already set
|
2005-04-20 16:52:18 +00:00
|
|
|
.assert MLI::CREATE::PATHNAME = MLI::OPEN::PATHNAME, error
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; Set all other parameters from template
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #(MLI::CREATE::CREATE_TIME+1) - (MLI::CREATE::PATHNAME+1) - 1
|
|
|
|
: lda CREATE,x
|
|
|
|
sta mliparam + MLI::CREATE::ACCESS,x
|
2005-04-12 09:12:48 +00:00
|
|
|
dex
|
2013-05-09 11:56:54 +00:00
|
|
|
bpl :-
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; Create file
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #CREATE_CALL
|
|
|
|
ldx #CREATE_COUNT
|
|
|
|
jsr callmli
|
|
|
|
bcc open
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; Check for ordinary errors
|
2013-05-09 11:56:54 +00:00
|
|
|
cmp #$47 ; "Duplicate filename"
|
|
|
|
bne oserr2
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; Check for exclusive flag
|
2013-05-09 11:56:54 +00:00
|
|
|
lda tmp3 ; Restore flags
|
|
|
|
and #O_EXCL
|
|
|
|
beq open
|
2005-04-12 09:12:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #$47 ; "Duplicate filename"
|
2005-04-20 16:52:18 +00:00
|
|
|
|
2005-04-12 09:12:48 +00:00
|
|
|
; Cleanup name
|
2013-05-09 11:56:54 +00:00
|
|
|
oserr2: jsr popname ; Preserves A
|
2005-04-12 09:12:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
oserr1: ldy tmp2 ; Restore fdtab slot
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; Cleanup I/O buffer
|
2013-05-09 11:56:54 +00:00
|
|
|
pha ; Save oserror code
|
|
|
|
jsr freebuffer
|
|
|
|
pla ; Restore oserror code
|
2005-04-20 16:52:18 +00:00
|
|
|
|
2022-08-28 19:52:53 +00:00
|
|
|
; Set ___oserror
|
|
|
|
jmp ___mappederrno
|
2005-04-12 09:12:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
open: ldy tmp2 ; Restore fdtab slot
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; Set allocated I/O buffer
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx fdtab + FD::BUFFER+1,y
|
|
|
|
sta mliparam + MLI::OPEN::IO_BUFFER ; A = 0
|
|
|
|
stx mliparam + MLI::OPEN::IO_BUFFER+1
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; Open file
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #OPEN_CALL
|
|
|
|
ldx #OPEN_COUNT
|
|
|
|
jsr callmli
|
|
|
|
bcs oserr2
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; Get and save fd
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx mliparam + MLI::OPEN::REF_NUM
|
|
|
|
stx tmp1 ; Save fd
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; Set flags and check for truncate flag
|
2013-05-09 11:56:54 +00:00
|
|
|
lda tmp3 ; Restore flags
|
|
|
|
sta fdtab + FD::FLAGS,y
|
|
|
|
and #O_TRUNC
|
|
|
|
beq done
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; Set fd and zero size
|
2013-05-09 11:56:54 +00:00
|
|
|
stx mliparam + MLI::EOF::REF_NUM
|
|
|
|
ldx #$02
|
|
|
|
lda #$00
|
|
|
|
: sta mliparam + MLI::EOF::EOF,x
|
2005-04-12 09:12:48 +00:00
|
|
|
dex
|
2013-05-09 11:56:54 +00:00
|
|
|
bpl :-
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; Set file size
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #SET_EOF_CALL
|
|
|
|
ldx #EOF_COUNT
|
|
|
|
jsr callmli
|
|
|
|
bcc done
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; Cleanup file
|
2013-05-09 11:56:54 +00:00
|
|
|
pha ; Save oserror code
|
|
|
|
lda tmp1 ; Restore fd
|
|
|
|
jsr closedirect
|
|
|
|
pla ; Restore oserror code
|
|
|
|
bne oserr2 ; Branch always
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; Store fd
|
2013-05-09 11:56:54 +00:00
|
|
|
done: lda tmp1 ; Restore fd
|
|
|
|
sta fdtab + FD::REF_NUM,y
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; Convert fdtab slot to handle
|
2005-04-20 16:52:18 +00:00
|
|
|
.assert .sizeof(FD) = 4, error
|
2005-04-12 09:12:48 +00:00
|
|
|
tya
|
|
|
|
lsr
|
|
|
|
lsr
|
|
|
|
|
|
|
|
; Cleanup name
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr popname ; Preserves A
|
2005-04-20 16:52:18 +00:00
|
|
|
|
2005-04-12 09:12:48 +00:00
|
|
|
; Return success
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #$00
|
2022-08-28 19:52:53 +00:00
|
|
|
stx ___oserror
|
2005-04-12 09:12:48 +00:00
|
|
|
rts
|
|
|
|
|
|
|
|
freebuffer:
|
|
|
|
; Free I/O buffer
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #$00
|
|
|
|
ldx fdtab + FD::BUFFER+1,y
|
|
|
|
jmp iobuf_free
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
closedirect:
|
|
|
|
; Set fd
|
2013-05-09 11:56:54 +00:00
|
|
|
sta mliparam + MLI::CLOSE::REF_NUM
|
2005-04-12 09:12:48 +00:00
|
|
|
|
|
|
|
; Call close
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #CLOSE_CALL
|
|
|
|
ldx #CLOSE_COUNT
|
|
|
|
jmp callmli
|
2005-04-12 09:12:48 +00:00
|
|
|
|
2011-01-29 21:43:26 +00:00
|
|
|
closeallfiles:
|
|
|
|
; All open files with current level (or higher)
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #$00
|
|
|
|
jsr closedirect
|
2011-01-29 21:43:26 +00:00
|
|
|
|
|
|
|
; Restore original file level
|
2013-05-09 11:56:54 +00:00
|
|
|
lda __dos_type
|
|
|
|
beq :+
|
|
|
|
dec LEVEL
|
2011-01-29 21:43:26 +00:00
|
|
|
: rts
|
|
|
|
|
2011-01-05 16:09:08 +00:00
|
|
|
.data
|
2005-04-12 09:12:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
CREATE: .byte %11000011 ; ACCESS: Standard full access
|
2011-01-05 16:09:08 +00:00
|
|
|
__filetype:
|
2013-05-09 11:56:54 +00:00
|
|
|
.byte $06 ; FILE_TYPE: Standard binary file
|
2011-01-05 16:09:08 +00:00
|
|
|
__auxtype:
|
2013-05-09 11:56:54 +00:00
|
|
|
.word $0000 ; AUX_TYPE: Load address N/A
|
|
|
|
.byte $01 ; STORAGE_TYPE: Standard seedling file
|
2013-08-03 17:08:36 +00:00
|
|
|
__datetime:
|
2013-05-09 11:56:54 +00:00
|
|
|
.word $0000 ; CREATE_DATE: Current date
|
|
|
|
.word $0000 ; CREATE_TIME: Current time
|