2002-11-23 23:05:00 +00:00
|
|
|
;
|
2004-05-14 20:54:46 +00:00
|
|
|
; Ullrich von Bassewitz, 2002-11-22, 2004-05-14
|
2002-11-23 23:05:00 +00:00
|
|
|
;
|
2003-06-17 20:52:33 +00:00
|
|
|
; size_t __fastcall__ fread (void* buf, size_t size, size_t count, FILE* file);
|
2002-11-23 23:05:00 +00:00
|
|
|
; /* Read from a file */
|
|
|
|
;
|
|
|
|
|
|
|
|
.export _fread
|
|
|
|
|
|
|
|
.import _read
|
2004-05-14 20:54:46 +00:00
|
|
|
.import pusha0, pushax
|
|
|
|
.import incsp4, incsp6
|
|
|
|
.import ldaxysp, ldax0sp
|
|
|
|
.import pushwysp
|
2002-11-23 23:05:00 +00:00
|
|
|
.import tosumulax, tosudivax
|
|
|
|
|
2004-05-14 20:54:46 +00:00
|
|
|
.importzp ptr1, sp
|
2002-11-23 23:05:00 +00:00
|
|
|
|
|
|
|
.include "errno.inc"
|
|
|
|
.include "_file.inc"
|
|
|
|
|
2004-05-14 20:54:46 +00:00
|
|
|
.macpack generic
|
2002-11-23 23:05:00 +00:00
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; Code
|
|
|
|
|
|
|
|
.proc _fread
|
|
|
|
|
2004-05-14 20:54:46 +00:00
|
|
|
; We will place a pointer to the file descriptor into the register bank
|
2002-11-23 23:05:00 +00:00
|
|
|
|
2004-05-14 20:54:46 +00:00
|
|
|
.import regbank: zp
|
|
|
|
file = regbank
|
2002-11-23 23:05:00 +00:00
|
|
|
|
2004-05-14 20:54:46 +00:00
|
|
|
; Save two bytes from the register bank
|
|
|
|
|
|
|
|
ldy file+0
|
|
|
|
sty save
|
|
|
|
ldy file+1
|
|
|
|
sty save+1
|
2002-11-23 23:05:00 +00:00
|
|
|
|
2004-05-14 20:54:46 +00:00
|
|
|
; Save the file pointer into the register bank
|
2002-11-23 23:05:00 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
sta file
|
|
|
|
stx file+1
|
2004-05-14 20:54:46 +00:00
|
|
|
|
|
|
|
; Check if the file is open
|
2002-11-23 23:05:00 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
ldy #_FILE::f_flags
|
|
|
|
lda (file),y
|
|
|
|
and #_FOPEN ; Is the file open?
|
|
|
|
beq @L1 ; Branch if no
|
2002-11-23 23:05:00 +00:00
|
|
|
|
|
|
|
; Check if the stream is in an error state
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
lda (file),y ; get file->f_flags again
|
|
|
|
and #_FERROR
|
|
|
|
beq @L2
|
2004-05-14 20:54:46 +00:00
|
|
|
|
|
|
|
; File not open or in error state
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
@L1: lda #EINVAL
|
2004-05-14 20:54:46 +00:00
|
|
|
jsr __seterrno ; Set __errno, return zero in A
|
|
|
|
tax ; a/x = 0
|
|
|
|
jmp @L99 ; Bail out
|
|
|
|
|
|
|
|
; Remember if we have a pushed back character and reset the flag.
|
|
|
|
|
|
|
|
@L2: tax ; X = 0
|
|
|
|
lda (file),y
|
|
|
|
and #_FPUSHBACK
|
|
|
|
beq @L3
|
|
|
|
lda (file),y
|
|
|
|
and #<~_FPUSHBACK
|
|
|
|
sta (file),y ; file->f_flags &= ~_FPUSHBACK;
|
|
|
|
inx ; X = 1
|
|
|
|
@L3: stx pb
|
2002-11-23 23:05:00 +00:00
|
|
|
|
|
|
|
; Build the stackframe for read()
|
|
|
|
|
2003-12-03 10:15:33 +00:00
|
|
|
ldy #_FILE::f_fd
|
2004-05-14 20:54:46 +00:00
|
|
|
lda (file),y
|
|
|
|
jsr pusha0 ; file->f_fd
|
2002-11-23 23:05:00 +00:00
|
|
|
|
|
|
|
ldy #9
|
|
|
|
jsr pushwysp ; buf
|
|
|
|
|
2003-06-17 20:52:33 +00:00
|
|
|
; Stack is now: buf/size/count/file->fd/buf
|
2002-11-23 23:05:00 +00:00
|
|
|
; Calculate the number of bytes to read: count * size
|
|
|
|
|
|
|
|
ldy #7
|
|
|
|
jsr pushwysp ; count
|
|
|
|
ldy #9
|
|
|
|
jsr ldaxysp ; Get size
|
|
|
|
jsr tosumulax ; count * size -> a/x
|
|
|
|
|
2004-05-14 20:54:46 +00:00
|
|
|
; Check if count is zero.
|
2002-11-23 23:05:00 +00:00
|
|
|
|
|
|
|
cmp #0
|
2004-05-14 20:54:46 +00:00
|
|
|
bne @L4
|
|
|
|
cpx #0
|
|
|
|
bne @L4
|
2002-11-23 23:05:00 +00:00
|
|
|
|
2004-05-14 20:54:46 +00:00
|
|
|
; Count is zero, drop the stack frame just built and return count
|
2002-11-23 23:05:00 +00:00
|
|
|
|
2004-05-14 20:54:46 +00:00
|
|
|
jsr incsp4 ; Drop file->fd/buf
|
|
|
|
jsr ldax0sp ; Get count
|
|
|
|
jmp @L99 ; Bail out
|
|
|
|
|
|
|
|
; Check if we have a buffered char from ungetc
|
|
|
|
|
|
|
|
@L4: ldy pb
|
|
|
|
beq @L6
|
|
|
|
|
|
|
|
; We have a buffered char from ungetc. Save the low byte from count
|
|
|
|
|
|
|
|
pha
|
|
|
|
|
2010-06-03 20:26:33 +00:00
|
|
|
; Copy the buffer pointer into ptr1, and increment the pointer value passed
|
2004-05-14 20:54:46 +00:00
|
|
|
; to read() by one, so read() starts to store data at buf+1.
|
|
|
|
|
|
|
|
ldy #0
|
|
|
|
lda (sp),y
|
|
|
|
sta ptr1
|
|
|
|
add #1
|
|
|
|
sta (sp),y
|
|
|
|
iny
|
|
|
|
lda (sp),y
|
|
|
|
sta ptr1+1
|
|
|
|
adc #0
|
|
|
|
sta (sp),y ; ptr1 = buf++;
|
|
|
|
|
|
|
|
; Get the buffered character and place it as first character into the read
|
|
|
|
; buffer.
|
|
|
|
|
|
|
|
ldy #_FILE::f_pushback
|
|
|
|
lda (file),y
|
|
|
|
ldy #0
|
|
|
|
sta (ptr1),y ; *buf = file->f_pushback;
|
|
|
|
|
|
|
|
; Restore the low byte of count and decrement count by one. This may result
|
|
|
|
; in count being zero, so check for that.
|
|
|
|
|
|
|
|
pla
|
|
|
|
sub #1
|
|
|
|
bcs @L5
|
|
|
|
dex
|
|
|
|
@L5: cmp #0
|
|
|
|
bne @L6
|
|
|
|
cpx #0
|
|
|
|
beq @L8
|
2002-11-23 23:05:00 +00:00
|
|
|
|
|
|
|
; Call read(). This will leave the original 3 params on the stack
|
|
|
|
|
2004-05-14 20:54:46 +00:00
|
|
|
@L6: jsr _read
|
2002-11-23 23:05:00 +00:00
|
|
|
|
|
|
|
; Check for errors in read
|
|
|
|
|
|
|
|
cpx #$FF
|
2004-05-14 20:54:46 +00:00
|
|
|
bne @L8
|
2002-11-23 23:05:00 +00:00
|
|
|
cmp #$FF
|
2004-05-14 20:54:46 +00:00
|
|
|
bne @L8
|
2002-11-23 23:05:00 +00:00
|
|
|
|
2010-06-03 20:26:33 +00:00
|
|
|
; Error in read. Set the stream error flag and bail out. errno has already
|
2022-04-17 14:06:22 +00:00
|
|
|
; been set by read(). On entry to label @L7, X must be zero.
|
2002-11-23 23:05:00 +00:00
|
|
|
|
2004-05-14 20:54:46 +00:00
|
|
|
inx ; X = 0
|
2002-11-23 23:05:00 +00:00
|
|
|
lda #_FERROR
|
2004-05-14 20:54:46 +00:00
|
|
|
@L7: ldy #_FILE::f_flags ; X must be zero here!
|
|
|
|
ora (file),y
|
|
|
|
sta (file),y
|
|
|
|
txa ; a/x = 0
|
|
|
|
beq @L99 ; Return zero
|
2002-11-23 23:05:00 +00:00
|
|
|
|
2004-05-14 20:54:46 +00:00
|
|
|
; Read was ok, account for the pushed back character (if any).
|
2002-11-23 23:05:00 +00:00
|
|
|
|
2004-05-14 20:54:46 +00:00
|
|
|
@L8: add pb
|
|
|
|
bcc @L9
|
|
|
|
inx
|
|
|
|
|
|
|
|
; Check for end of file.
|
|
|
|
|
|
|
|
@L9: cmp #0 ; Zero bytes read?
|
|
|
|
bne @L10
|
2002-11-23 23:05:00 +00:00
|
|
|
cpx #0
|
2004-05-14 20:54:46 +00:00
|
|
|
bne @L10
|
2002-11-23 23:05:00 +00:00
|
|
|
|
|
|
|
; Zero bytes read. Set the EOF flag
|
|
|
|
|
|
|
|
lda #_FEOF
|
2004-05-14 20:54:46 +00:00
|
|
|
bne @L7 ; Set flag and return zero
|
2002-11-23 23:05:00 +00:00
|
|
|
|
|
|
|
; Return the number of items successfully read. Since we've checked for
|
|
|
|
; bytes == 0 above, size cannot be zero here, so the division is safe.
|
|
|
|
|
2004-05-14 20:54:46 +00:00
|
|
|
@L10: jsr pushax ; Push number of bytes read
|
2002-11-23 23:05:00 +00:00
|
|
|
ldy #5
|
|
|
|
jsr ldaxysp ; Get size
|
|
|
|
jsr tosudivax ; bytes / size -> a/x
|
2004-05-14 20:54:46 +00:00
|
|
|
@L99: ldy save ; Restore zp register
|
|
|
|
sty file
|
|
|
|
ldy save+1
|
|
|
|
sty file+1
|
2002-11-23 23:05:00 +00:00
|
|
|
jmp incsp6 ; Drop params, return
|
|
|
|
|
|
|
|
.endproc
|
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; Data
|
|
|
|
|
|
|
|
.bss
|
2013-05-09 11:56:54 +00:00
|
|
|
save: .res 2
|
2004-05-14 20:54:46 +00:00
|
|
|
pb: .res 1
|
2002-11-23 23:05:00 +00:00
|
|
|
|