forked from cc65/cc65
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request cc65#2380 from colinleroy/asm-fgetc
Rewrite fgetc in asm
- Loading branch information
Showing
4 changed files
with
161 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
; | ||
; Colin Leroy-Mira, 2024 | ||
; | ||
; int __fastcall__ fgetc (register FILE* f) | ||
; | ||
|
||
.export _fgetc | ||
.import _read, pusha0, pushax, popptr1, incsp2, returnFFFF | ||
.importzp ptr1 | ||
|
||
.include "stdio.inc" | ||
.include "_file.inc" | ||
|
||
_fgetc: | ||
sta ptr1 | ||
stx ptr1+1 | ||
jsr pushax ; Backup our ptr | ||
|
||
ldy #_FILE::f_flags | ||
lda (ptr1),y | ||
tax | ||
and #_FOPEN ; Check for file open | ||
beq ret_eof | ||
txa | ||
and #(_FERROR|_FEOF); Check for error/eof | ||
bne ret_eof | ||
|
||
txa | ||
and #_FPUSHBACK ; Check for pushed back char | ||
beq do_read | ||
|
||
txa | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
!!DESCRIPTION!! fgets test | ||
!!LICENCE!! Public domain | ||
*/ | ||
|
||
#include "common.h" | ||
|
||
#include <ctype.h> | ||
#include <errno.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
FILE *in, *out; | ||
char buf[32]; | ||
|
||
#define INFILE "cf.in" | ||
|
||
int main(int argc,char **argv) | ||
{ | ||
static char outfile_path[FILENAME_MAX+1]; | ||
|
||
sprintf(outfile_path, "%s.test.out", argv[0]); | ||
|
||
out = fopen(outfile_path, "wb"); | ||
if (out == NULL) { | ||
return EXIT_FAILURE; | ||
} | ||
if (fgets(buf, sizeof(buf), out) != NULL) { | ||
printf("Error, could fgets with write-only file\n"); | ||
return 1; | ||
} | ||
if (!ferror(out)) { | ||
printf("Error: file pointer should be in error state\n"); | ||
} | ||
fclose(out); | ||
|
||
in = fopen(INFILE, "rb"); | ||
if (in == NULL) { | ||
return EXIT_FAILURE; | ||
} | ||
|
||
if (fgets(NULL, 0, in) != NULL) { | ||
printf("Error, could fgets with zero size\n"); | ||
return 1; | ||
} | ||
|
||
/* Test ungetc while we're at it */ | ||
buf[0] = fgetc(in); | ||
ungetc(buf[0], in); | ||
|
||
|
||
while (fgets(buf, sizeof(buf), in) != NULL) | ||
{ | ||
printf("%s",buf); | ||
} | ||
|
||
if (!feof(in)) | ||
{ | ||
printf("We should have EOF!\n"); | ||
} | ||
|
||
fclose(in); | ||
return 0; | ||
} |