Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/libc/__fileioc_stdio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef __FILEIOC_STDIO_H
#define __FILEIOC_STDIO_H

#include <cdefs.h>
#include <fileioc.h>
#include <stdio.h>
#include <stdbool.h>

/*
* fd mapping:
* STDIN_FILENO = 0
* STDOUT_FILENO = 1
* STDERR_FILENO = 2
* fileioc slot 1 = 3
* fileioc slot 2 = 4
* fileioc slot 3 = 5
* fileioc slot 4 = 6
* fileioc slot 5 = 7
*/

__BEGIN_DECLS

extern FILE _file_streams[FOPEN_MAX];

#define FILEIOC_MIN_FD_SLOT 3
#define FILEIOC_MAX_FD_SLOT (FILEIOC_MIN_FD_SLOT + (FOPEN_MAX - 1))

static inline int fileioc_slot_to_fd(ti_var_t slot)
{
return (int)(slot + (FILEIOC_MIN_FD_SLOT - 1));
}

static inline ti_var_t fd_to_fileioc_slot(int fd)
{
return (ti_var_t)(fd - (FILEIOC_MIN_FD_SLOT - 1));
}

static inline bool is_fd_a_fileioc_slot(int fd)
{
return (fd >= FILEIOC_MIN_FD_SLOT && fd <= FILEIOC_MAX_FD_SLOT);
}

__END_DECLS

#endif /* __FILEIOC_STDIO_H */
5 changes: 1 addition & 4 deletions src/libc/clearerr.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#include <stdio.h>
#include <fileioc.h>

extern FILE _file_streams[FOPEN_MAX];
#include "__fileioc_stdio.h"

void __attribute__((weak)) clearerr(FILE *stream)
{
Expand Down
5 changes: 1 addition & 4 deletions src/libc/fclose.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#include <stdio.h>
#include <fileioc.h>

extern FILE _file_streams[FOPEN_MAX];
#include "__fileioc_stdio.h"

int __attribute__((weak)) fclose(FILE *stream)
{
Expand Down
3 changes: 1 addition & 2 deletions src/libc/feof.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <stdio.h>
#include <fileioc.h>
#include "__fileioc_stdio.h"

int __attribute__((weak)) feof(FILE *stream)
{
Expand Down
3 changes: 1 addition & 2 deletions src/libc/ferror.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <stdio.h>
#include <fileioc.h>
#include "__fileioc_stdio.h"

int __attribute__((weak)) ferror(FILE *stream)
{
Expand Down
3 changes: 1 addition & 2 deletions src/libc/fflush.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <stdio.h>
#include <fileioc.h>
#include "__fileioc_stdio.h"

int __attribute__((weak)) fflush(FILE *stream)
{
Expand Down
3 changes: 1 addition & 2 deletions src/libc/fgetc.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <stdio.h>
#include <fileioc.h>
#include "__fileioc_stdio.h"

int __attribute__((weak)) fgetc(FILE *stream)
{
Expand Down
3 changes: 1 addition & 2 deletions src/libc/fgets.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <stdio.h>
#include <fileioc.h>
#include "__fileioc_stdio.h"

char* __attribute__((weak)) fgets(char *__restrict str, int num, FILE *__restrict stream)
{
Expand Down
28 changes: 28 additions & 0 deletions src/libc/fileno.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "__fileioc_stdio.h"
#include <errno.h>
#include <unistd.h>

int __attribute__((weak)) fileno(FILE *stream)
{
if (stream == NULL)
{
// invalid stream
errno = EBADF;
return -1;
}

if (stream == stdin)
{
return STDIN_FILENO;
}
if (stream == stdout)
{
return STDOUT_FILENO;
}
if (stream == stderr)
{
return STDERR_FILENO;
}

return fileioc_slot_to_fd(stream->slot);
}
6 changes: 1 addition & 5 deletions src/libc/fopen.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#include <fileioc.h>
#include "__fileioc_stdio.h"

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

extern FILE _file_streams[FOPEN_MAX];

FILE* __attribute__((weak)) fopen(const char *__restrict filename, const char *__restrict mode)
{
Expand Down
3 changes: 1 addition & 2 deletions src/libc/fputc.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <stdio.h>
#include <fileioc.h>
#include "__fileioc_stdio.h"

int __attribute__((weak)) fputc(int c, FILE *stream)
{
Expand Down
3 changes: 1 addition & 2 deletions src/libc/fputs.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <stdio.h>
#include <fileioc.h>
#include "__fileioc_stdio.h"

int __attribute__((weak)) fputs(const char *__restrict str, FILE *__restrict stream)
{
Expand Down
3 changes: 1 addition & 2 deletions src/libc/fread.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <stdio.h>
#include <fileioc.h>
#include "__fileioc_stdio.h"

size_t __attribute__((weak)) fread(void *ptr, size_t size, size_t count, FILE *__restrict stream)
{
Expand Down
3 changes: 1 addition & 2 deletions src/libc/fseek.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <stdio.h>
#include <fileioc.h>
#include "__fileioc_stdio.h"

int __attribute__((weak)) fseek(FILE *stream, long int offset, int origin)
{
Expand Down
3 changes: 1 addition & 2 deletions src/libc/ftell.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <stdio.h>
#include <fileioc.h>
#include "__fileioc_stdio.h"
#include <stdint.h>

long int __attribute__((weak)) ftell(FILE *stream)
Expand Down
3 changes: 1 addition & 2 deletions src/libc/fwrite.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <stdio.h>
#include <fileioc.h>
#include "__fileioc_stdio.h"

size_t __attribute__((weak)) fwrite(const void *__restrict ptr, size_t size, size_t count, FILE *__restrict stream)
{
Expand Down
2 changes: 2 additions & 0 deletions src/libc/include/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ int remove(const char *filename);

int rename(const char *old_filename, const char *new_filename);

int fileno(FILE *stream);

/* standard impls */

int getchar(void);
Expand Down
30 changes: 30 additions & 0 deletions src/libc/include/unistd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef _UNISTD_H
#define _UNISTD_H

#include <cdefs.h>

/*
* unsigned int sleep(unsigned int seconds);
* typedef unsigned int useconds_t;
* int usleep(useconds_t usec);
*/
#include <sys/timers.h>

#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2

#ifndef _SSIZE_T_DEFINED
#define _SSIZE_T_DEFINED
typedef __PTRDIFF_TYPE__ ssize_t;
#endif /* _SSIZE_T_DEFINED */

__BEGIN_DECLS

int isatty(int fd);

void swab(const void *__restrict src, void *__restrict dst, ssize_t count);

__END_DECLS

#endif /* _UNISTD_H */
20 changes: 20 additions & 0 deletions src/libc/isatty.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "__fileioc_stdio.h"
#include <unistd.h>
#include <errno.h>

int __attribute__((weak)) isatty(int fd)
{
if (fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO)
{
return 1;
}
if (is_fd_a_fileioc_slot(fd))
{
// not a terminal
errno = ENOTTY;
return 0;
}
// invalid fd
errno = EBADF;
return 0;
}
3 changes: 1 addition & 2 deletions src/libc/remove.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <stdio.h>
#include <fileioc.h>
#include "__fileioc_stdio.h"

int __attribute__((weak)) remove(const char *filename)
{
Expand Down
3 changes: 1 addition & 2 deletions src/libc/rename.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <stdio.h>
#include <fileioc.h>
#include "__fileioc_stdio.h"

__attribute__((__weak__))
int rename(const char *old_filename, const char *new_filename) {
Expand Down
3 changes: 1 addition & 2 deletions src/libc/rewind.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <stdio.h>
#include <fileioc.h>
#include "__fileioc_stdio.h"

void __attribute__((weak)) rewind(FILE *stream)
{
Expand Down
72 changes: 72 additions & 0 deletions src/libc/swab.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
.assume adl=1

.section .text

.global _swab
.type _swab, @function

.if 1

; void swab(const void *__restrict src, void *__restrict dst, ssize_t count)
_swab:
; swab has unspecified behaviour when count is odd:
; - this implementation treats an odd count as if it were count - 1
; - does nothing when count <= 1 or count is negative
ld iy, 0
add iy, sp
sra (iy + 11)
ret m ; do nothing when negative
ld bc, (iy + 9)
rr b
rr c
; BC = count / 2
sbc hl, hl
adc hl, bc
ret z ; count <= 1
ld de, (iy + 6) ; DE = dst
ld hl, (iy + 3) ; HL = src
.L.loop:
ld a, (hl) ; A = src.lo
inc hl
ldi ; dst.lo = src.hi
ld (de), a ; dst.hi = A
inc de
jp pe, .L.loop
ret

.else

; void swab(const void *__restrict src, void *__restrict dst, ssize_t count)
_swab:
; swab has unspecified behaviour when count is odd:
; - this implementation will copy dst[count - 1] to src[count - 1] when count is odd
; - does nothing when count <= 0 or count is negative
ld iy, 0
add iy, sp
sra (iy + 11)
ret m ; do nothing when negative
ld bc, (iy + 9)
rr b
rr c
; BC = count / 2
sbc hl, hl
adc hl, bc
; Carry is set when count is odd
ld de, (iy + 6) ; DE = dst
ld hl, (iy + 3) ; HL = src
jr z, .L.finish
.L.loop:
ld a, (hl) ; A = src.lo
inc hl
ldi ; dst.lo = src.hi
ld (de), a ; dst.hi = A
inc de
jp pe, .L.loop
.L.finish:
ret nc ; even count
; copy one more byte
ld a, (hl)
ld (de), a
ret

.endif
9 changes: 9 additions & 0 deletions src/libc/ungetc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "__fileioc_stdio.h"

int __attribute__((weak)) ungetc(int ch, FILE *stream)
{
// unimplemented
(void)ch;
(void)stream;
return EOF;
}
37 changes: 37 additions & 0 deletions test/standalone/fileno/autotest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"transfer_files":
[
"bin/DEMO.8xp"
],
"target":
{
"name": "DEMO",
"isASM": true
},
"sequence":
[
"action|launch",
"delay|500",
"hashWait|1",
"key|enter",
"delay|300",
"hashWait|2"
],
"hashes":
{
"1":
{
"description": "All tests passed",
"start": "vram_start",
"size": "vram_16_size",
"expected_CRCs": [ "38E2AD5A" ]
},
"2":
{
"description": "Test homescreen cleared",
"start": "vram_start",
"size": "vram_16_size",
"expected_CRCs": [ "FFAF89BA", "101734A5", "9DA19F44", "A32840C8", "349F4775" ]
}
}
}
Loading
Loading