Skip to content

Commit

Permalink
[libc] fix issues with inchar/outchar and fget/fput family
Browse files Browse the repository at this point in the history
  • Loading branch information
mateoconlechuga committed Oct 25, 2024
1 parent ee119ba commit a89abd4
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 33 deletions.
11 changes: 5 additions & 6 deletions src/libc/fputc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ int __attribute__((weak)) fputc(int c, FILE *stream)
{
int ret;

if (stream == NULL ||
stream == stdin)
if (stream == NULL || stream == stdin)
{
ret = EOF;
}
else if (stream == stdout ||
stream == stderr)
else if (stream == stdout || stream == stderr)
{
ret = putchar(c);
putchar(c);
ret = c;
}
else
{
Expand All @@ -30,5 +29,5 @@ int __attribute__((weak)) fputc(int c, FILE *stream)
stream->err = 1;
}

return ti_PutC((char)c, stream->slot);
return ret;
}
5 changes: 0 additions & 5 deletions src/libc/fputs.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,5 @@ int __attribute__((weak)) fputs(const char *__restrict str, FILE *__restrict str
i++;
}

if (fputc('\n', stream) == EOF)
{
return EOF;
}

return 1;
}
13 changes: 1 addition & 12 deletions src/libc/getchar.src
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,5 @@

section .text
public _getchar
_getchar:
call _inchar
ld l,a
rlc l
sbc hl,hl
ld l,a
push hl
call _outchar
pop hl
ret

_getchar := _inchar
extern _inchar
extern _outchar
19 changes: 14 additions & 5 deletions src/crt/inchar.src → src/libc/inchar.src
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,33 @@
weak _inchar
_inchar:
ld iy,$d00080 ; ti.flags
.getchar:
.l:
call $2014C ; ti.GetCSC
or a,a
jr z,.getchar
jr z,.l
sbc hl,hl
ld l,a
ld de,.lut_char
ld de,.lut
add hl,de
ld a,(hl)
or a,a
jr z,_inchar
jr z,.l
sbc hl,hl
ld l,a
push hl
push hl
call _outchar
pop hl
pop hl
ret

.lut_char:
.lut:
db 0,0,0,0,0,0,0,0,0,10 ;
db 0,'WRMH',0,0 ; + - × ÷ ^ undef
db 0,'Z'+ 1,'VQLG',0,0 ; (-) 3 6 9 ) TAN VARS undef
db 0,'ZUPKFC',0 ; . 2 5 8 ( COS PRGM STAT
db ' YTOJEBX',0 ; 0 1 4 7 , SIN APPS XT?n undef
db 'XSNIDA' ; STO LN LOG x2 x-1 MATH
db 0,0,0,0,0,0,0,0,0,0 ;

extern _outchar
2 changes: 1 addition & 1 deletion src/libc/include/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ typedef struct
__BEGIN_DECLS

/* weak user-defined functions */
char inchar(void);
int inchar(void);

void outchar(char character);

Expand Down
5 changes: 1 addition & 4 deletions src/libc/outchar.src
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ _outchar:
ex (sp),hl
push de
ld iy,$d00080 ; ti.flags
ld a,l
cp a,32
jp nc,$207B8 ; ti.PutC
cp a,10
jp z,$0207F0 ; ti.NewLine
ret
jp $207B8 ; ti.PutC
2 changes: 2 additions & 0 deletions src/libc/putchar.src
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ _putchar:
ex (sp),hl
push de
push hl
push hl
call _outchar
pop hl
push hl
ret

extern _outchar

0 comments on commit a89abd4

Please sign in to comment.