2024-01-11 17:54:42 +00:00
|
|
|
;
|
|
|
|
; Colin Leroy-Mira, 2024
|
|
|
|
;
|
|
|
|
; int __fastcall__ fgetc (register FILE* f)
|
|
|
|
;
|
|
|
|
|
|
|
|
.export _fgetc
|
2024-01-30 17:11:10 +00:00
|
|
|
.import _read, checkferror
|
|
|
|
.import pusha0, pushax, popptr1, incsp2, returnFFFF
|
2024-01-11 17:54:42 +00:00
|
|
|
.importzp ptr1
|
|
|
|
|
|
|
|
.include "stdio.inc"
|
|
|
|
.include "_file.inc"
|
|
|
|
|
2024-02-01 19:05:57 +00:00
|
|
|
.macpack cpu
|
|
|
|
|
2024-01-11 17:54:42 +00:00
|
|
|
_fgetc:
|
|
|
|
sta ptr1
|
|
|
|
stx ptr1+1
|
|
|
|
jsr pushax ; Backup our ptr
|
|
|
|
|
2024-01-30 17:11:10 +00:00
|
|
|
jsr checkferror
|
2024-01-11 17:54:42 +00:00
|
|
|
bne ret_eof
|
|
|
|
|
2024-02-01 19:05:57 +00:00
|
|
|
.if (.cpu .bitand ::CPU_ISET_65SC02)
|
|
|
|
bit #_FPUSHBACK ; Check for pushed back char
|
|
|
|
beq do_read
|
|
|
|
.else
|
2024-01-30 17:11:10 +00:00
|
|
|
tax
|
2024-01-11 17:54:42 +00:00
|
|
|
and #_FPUSHBACK ; Check for pushed back char
|
|
|
|
beq do_read
|
|
|
|
txa
|
2024-02-01 19:05:57 +00:00
|
|
|
.endif
|
|
|
|
|
2024-01-11 17:54:42 +00:00
|
|
|
and #<(~_FPUSHBACK) ; Reset flag
|
|
|
|
sta (ptr1),y
|
|
|
|
|
|
|
|
.assert _FILE::f_pushback = _FILE::f_flags+1, error
|
|
|
|
iny
|
|
|
|
jsr incsp2 ; Drop our ptr copy
|
|
|
|
lda (ptr1),y ; Return pushed back char
|
|
|
|
ldx #$00
|
|
|
|
rts
|
|
|
|
|
|
|
|
do_read:
|
|
|
|
; Push _read parameters
|
|
|
|
ldy #_FILE::f_fd
|
|
|
|
lda (ptr1),y
|
|
|
|
jsr pusha0
|
|
|
|
|
|
|
|
lda #<c
|
|
|
|
ldx #>c
|
|
|
|
jsr pushax
|
|
|
|
|
|
|
|
lda #$01
|
|
|
|
ldx #$00
|
|
|
|
|
|
|
|
; Read
|
|
|
|
jsr _read
|
|
|
|
|
|
|
|
; Check for errors
|
|
|
|
cmp #$00
|
|
|
|
beq set_feof
|
|
|
|
|
|
|
|
cmp #<(-1)
|
|
|
|
beq set_ferror
|
|
|
|
|
|
|
|
jsr incsp2
|
|
|
|
; Return char
|
|
|
|
ldx #$00
|
|
|
|
lda c
|
|
|
|
rts
|
|
|
|
|
|
|
|
ret_eof:
|
|
|
|
jsr incsp2
|
|
|
|
jmp returnFFFF
|
|
|
|
|
|
|
|
set_ferror:
|
|
|
|
lda #_FERROR
|
|
|
|
bne set_err
|
|
|
|
set_feof:
|
|
|
|
lda #_FEOF
|
|
|
|
set_err:
|
|
|
|
pha
|
|
|
|
jsr popptr1
|
|
|
|
pla
|
|
|
|
ldy #_FILE::f_flags
|
|
|
|
ora (ptr1),y
|
|
|
|
sta (ptr1),y
|
|
|
|
jmp returnFFFF
|
|
|
|
|
|
|
|
.bss
|
|
|
|
|
|
|
|
c: .res 1
|