From 68861080e7dec5df2a0d739a7daf77b7270cdf2b Mon Sep 17 00:00:00 2001 From: fincs Date: Wed, 20 Jul 2011 18:29:06 +0200 Subject: [PATCH] Add FEOSSTDIO module with basic stdio.h functions --- .gitignore | 2 + kernel/source/apibulk.c | 11 +----- kernel/source/feos.h | 6 +++ kernel/source/feosstdio.c | 67 ++++++++++++++++++++++++++++++++++ kernel/source/fxe.h | 1 + kernel/source/main.c | 1 + kernel/source/modulelist.arm.c | 8 +++- sdk/Makefile | 2 + sdk/include/stdio.h | 66 ++++++++++++++++++++++++++++++++- sdk/libfeos.mk | 2 +- sdk/source/FEOSBASE/Makefile | 4 +- sdk/source/FEOSSTDIO/Makefile | 48 ++++++++++++++++++++++++ sdk/source/stdio/fprintf.c | 10 +++++ sdk/source/stdio/fscanf.c | 10 +++++ sdk/source/stdio/getchar.c | 6 +++ sdk/source/stdio/gets.c | 6 +++ sdk/source/stdio/printf.c | 10 +++++ sdk/source/stdio/putchar.c | 6 +++ sdk/source/stdio/puts.c | 10 +++++ sdk/source/stdio/rewind.c | 6 +++ sdk/source/stdio/scanf.c | 10 +++++ sdk/source/stdio/sprintf.c | 10 +++++ sdk/source/stdio/sscanf.c | 10 +++++ sdk/source/stdio/vprintf.c | 6 +++ sdk/source/stdio/vscanf.c | 6 +++ 25 files changed, 308 insertions(+), 16 deletions(-) create mode 100644 kernel/source/feosstdio.c create mode 100644 sdk/source/FEOSSTDIO/Makefile create mode 100644 sdk/source/stdio/fprintf.c create mode 100644 sdk/source/stdio/fscanf.c create mode 100644 sdk/source/stdio/getchar.c create mode 100644 sdk/source/stdio/gets.c create mode 100644 sdk/source/stdio/printf.c create mode 100644 sdk/source/stdio/putchar.c create mode 100644 sdk/source/stdio/puts.c create mode 100644 sdk/source/stdio/rewind.c create mode 100644 sdk/source/stdio/scanf.c create mode 100644 sdk/source/stdio/sprintf.c create mode 100644 sdk/source/stdio/sscanf.c create mode 100644 sdk/source/stdio/vprintf.c create mode 100644 sdk/source/stdio/vscanf.c diff --git a/.gitignore b/.gitignore index 5dbc965..d11406a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ build/ /sdk/bin/fxe2tool /sdk/source/FEOSBASE/* !/sdk/source/FEOSBASE/Makefile +/sdk/source/FEOSSTDIO/* +!/sdk/source/FEOSSTDIO/Makefile *.exe *.nds *.elf diff --git a/kernel/source/apibulk.c b/kernel/source/apibulk.c index b2f501b..33117bd 100644 --- a/kernel/source/apibulk.c +++ b/kernel/source/apibulk.c @@ -4,12 +4,6 @@ #include #include -#define BEGIN_TABLE(_NAME_) static const fxe2_export_t _exp_##_NAME_##_tbl[] = { -#define ADD_FUNC(FUNC) {{(word_t)#FUNC}, {(word_t)FUNC}} -#define ADD_FUNC_ALIAS(FUNC, NAME) {{(word_t)#NAME}, {(word_t)FUNC}} -#define END_TABLE(_NAME_) }; -#define MAKE_EXPORTSTRUCT(_NAME_) { sizeof(_exp_##_NAME_##_tbl) / sizeof(fxe2_export_t), (fxe2_export_t*) _exp_##_NAME_##_tbl } - void FeOS_swi_DebugPrint(const char*); void* FeOS_FindSymbol(instance_t hinst, const char* sym) @@ -51,10 +45,7 @@ BEGIN_TABLE(FEOSBASE) ADD_FUNC(memcpy), ADD_FUNC(memmove), ADD_FUNC(memset), - ADD_FUNC(memcmp), - - ADD_FUNC_ALIAS(iprintf, printf), - ADD_FUNC_ALIAS(siprintf, sprintf) + ADD_FUNC(memcmp) END_TABLE(FEOSBASE) extern void* _inst_FEOSBASE; diff --git a/kernel/source/feos.h b/kernel/source/feos.h index 848bbc2..28d56be 100644 --- a/kernel/source/feos.h +++ b/kernel/source/feos.h @@ -5,6 +5,12 @@ #define FeOS_AllocStack(a) __builtin_alloca(a) +#define BEGIN_TABLE(_NAME_) static const fxe2_export_t _exp_##_NAME_##_tbl[] = { +#define ADD_FUNC(FUNC) {{(word_t)#FUNC}, {(word_t)FUNC}} +#define ADD_FUNC_ALIAS(FUNC, NAME) {{(word_t)#NAME}, {(word_t)FUNC}} +#define END_TABLE(_NAME_) }; +#define MAKE_EXPORTSTRUCT(_NAME_) { sizeof(_exp_##_NAME_##_tbl) / sizeof(fxe2_export_t), (fxe2_export_t*) _exp_##_NAME_##_tbl } + typedef unsigned int word_t; typedef unsigned short hword_t; typedef unsigned char byte_t; diff --git a/kernel/source/feosstdio.c b/kernel/source/feosstdio.c new file mode 100644 index 0000000..fec5bcf --- /dev/null +++ b/kernel/source/feosstdio.c @@ -0,0 +1,67 @@ +#include "feos.h" +#include "fxe.h" +#include +#include +#include +#include +#include + +FILE* FeOS_GetStdin() +{ + return stdin; +} + +FILE* FeOS_GetStdout() +{ + return stdout; +} + +FILE* FeOS_GetStderr() +{ + return stderr; +} + +BEGIN_TABLE(FEOSSTDIO) + ADD_FUNC(FeOS_GetStdin), + ADD_FUNC(FeOS_GetStdout), + ADD_FUNC(FeOS_GetStderr), + + // Basic I/O + ADD_FUNC(fopen), + ADD_FUNC(freopen), + ADD_FUNC(fclose), + ADD_FUNC(fwrite), + ADD_FUNC(fread), + ADD_FUNC(feof), + ADD_FUNC(fseek), + ADD_FUNC(ftell), + ADD_FUNC(fflush), + ADD_FUNC(ferror), + + // Formatting + ADD_FUNC_ALIAS(vfiprintf, vfprintf), + ADD_FUNC_ALIAS(vsiprintf, vsprintf), + ADD_FUNC_ALIAS(vfiscanf, vfscanf), + ADD_FUNC_ALIAS(vsiscanf, vsscanf), + + // Strings and chars + ADD_FUNC(fgetc), ADD_FUNC(fputc), + ADD_FUNC(fgets), ADD_FUNC(fputs) +END_TABLE(FEOSSTDIO) + +extern void* _inst_FEOSSTDIO; + +fxe_runtime_header _header_FEOSSTDIO = +{ + &_inst_FEOSSTDIO, // hThis + "FEOSSTDIO", // name + 1, // refcount + -1, // file + NULL, // entrypoint + MAKE_EXPORTSTRUCT(FEOSSTDIO), // exp + { 0, NULL }, // imp + NULL, // next + NULL // prev +}; + +void* _inst_FEOSSTDIO = &_header_FEOSSTDIO; diff --git a/kernel/source/fxe.h b/kernel/source/fxe.h index d0c024c..0fabe84 100644 --- a/kernel/source/fxe.h +++ b/kernel/source/fxe.h @@ -118,6 +118,7 @@ int ResolveImports(fxe2_import_t* imptbl, int count); void FreeImports(fxe2_import_t* imptbl, int count); void* FindInTbl(const fxe_inmem_exports* exphdr, const char* name); +void FeOS_ModuleListInit(); void FeOS_ModuleListAdd(fxe_runtime_header* pModule); void FeOS_ModuleListRemove(fxe_runtime_header* pModule); int FeOS_ModuleListCount(); diff --git a/kernel/source/main.c b/kernel/source/main.c index c78c8e7..ec27627 100644 --- a/kernel/source/main.c +++ b/kernel/source/main.c @@ -97,6 +97,7 @@ int main() SystemVectors.swi = (u32) __SWIHandler; setVectorBase(0); PrepareUserMode(); + FeOS_ModuleListInit(); iprintf( "FeOS kernel\n" diff --git a/kernel/source/modulelist.arm.c b/kernel/source/modulelist.arm.c index 3b59808..36c44e3 100644 --- a/kernel/source/modulelist.arm.c +++ b/kernel/source/modulelist.arm.c @@ -1,11 +1,17 @@ #include "fxe.h" extern fxe_runtime_header _header_FEOSBASE; +extern fxe_runtime_header _header_FEOSSTDIO; static fxe_runtime_header* mListHead = &_header_FEOSBASE; static fxe_runtime_header* mListTail = &_header_FEOSBASE; static int nmodules = 1; +void FeOS_ModuleListInit() +{ + FeOS_ModuleListAdd(&_header_FEOSSTDIO); +} + void FeOS_ModuleListAdd(fxe_runtime_header* pModule) { mListTail->next = pModule; @@ -16,7 +22,7 @@ void FeOS_ModuleListAdd(fxe_runtime_header* pModule) void FeOS_ModuleListRemove(fxe_runtime_header* pModule) { - if (pModule == mListHead) return; // thwart attempts at doing evil + if (pModule->file == -1) return; // thwart attempts at doing evil pModule->prev->next = pModule->next; nmodules --; diff --git a/sdk/Makefile b/sdk/Makefile index e842b0c..33fa423 100644 --- a/sdk/Makefile +++ b/sdk/Makefile @@ -5,9 +5,11 @@ all: @make -C bin/fxe2tool_src @make -C source/FEOSBASE + @make -C source/FEOSSTDIO @make -f libfeos.mk clean: @make -C bin/fxe2tool_src clean @make -C source/FEOSBASE clean + @make -C source/FEOSSTDIO clean @make -f libfeos.mk clean diff --git a/sdk/include/stdio.h b/sdk/include/stdio.h index eb4ff7c..0689ce0 100644 --- a/sdk/include/stdio.h +++ b/sdk/include/stdio.h @@ -7,6 +7,7 @@ #pragma once #include #include +#include #include #ifdef __cplusplus @@ -14,7 +15,70 @@ extern "C" { #endif -int printf(const char*, ...) __attribute__ ((format (printf, 1, 2))); +typedef struct +{ +} FILE; + +FILE* FeOS_GetStdin(); +FILE* FeOS_GetStdout(); +FILE* FeOS_GetStderr(); + +#define stdin FeOS_GetStdin() +#define stdout FeOS_GetStdout() +#define stderr FeOS_GetStderr() + +#define SEEK_SET 0 +#define SEEK_CUR 1 +#define SEEK_END 2 + +#define EOF (-1) +#define FOPEN_MAX 20 +#define FILENAME_MAX 1024 + +FILE* fopen(const char*, const char*); +FILE* freopen(const char*, const char*, FILE*); +int fclose(FILE*); + +size_t fread(void*, size_t, size_t, FILE*); +size_t fwrite(const void*, size_t, size_t, FILE*); +int feof(FILE*); + +int fseek(FILE*, int, int); +int ftell(FILE*); +void rewind(FILE*); + +int fflush(FILE*); +int ferror(FILE*); + +int vfprintf(FILE*, const char*, va_list); +int vsprintf(char*, const char*, va_list); +int vfscanf(FILE*, const char*, va_list); +int vsscanf(const char*, const char*, va_list); + +int vprintf(const char*, va_list); +int vscanf(const char*, va_list); + +int printf(const char*, ...) __attribute__ ((format (__printf__, 1, 2))); +int fprintf(FILE*, const char*, ...) __attribute__ ((format (__printf__, 2, 3))); +int sprintf(char*, const char*, ...) __attribute__ ((format (__printf__, 2, 3))); + +int scanf(const char*, ...) __attribute__ ((format (__scanf__, 1, 2))); +int fscanf(FILE*, const char*, ...) __attribute__ ((format (__scanf__, 2, 3))); +int sscanf(const char*, const char*, ...) __attribute__ ((format (__scanf__, 2, 3))); + +int fgetc(FILE*); +int fputc(int, FILE*); +char* fgets(char*, int, FILE*); +int fputs(const char*, FILE*); + +#define getc fgetc +#define putc fputc + +int getchar(); +char* gets(char*); + +int putchar(int); +int puts(const char*); #ifdef __cplusplus } diff --git a/sdk/libfeos.mk b/sdk/libfeos.mk index 1ef0a2b..7964ed8 100644 --- a/sdk/libfeos.mk +++ b/sdk/libfeos.mk @@ -25,7 +25,7 @@ FEOSMK = $(FEOSSDK)/mk #--------------------------------------------------------------------------------- TARGET := feos BUILD := build -SOURCES := source source/FEOSBASE +SOURCES := source source/FEOSBASE source/FEOSSTDIO source/stdio DATA := data INCLUDES := include diff --git a/sdk/source/FEOSBASE/Makefile b/sdk/source/FEOSBASE/Makefile index 9a8993a..ba7b85b 100644 --- a/sdk/source/FEOSBASE/Makefile +++ b/sdk/source/FEOSBASE/Makefile @@ -30,9 +30,7 @@ strncmp \ memcpy \ memmove \ memset \ -memcmp \ -printf \ -sprintf +memcmp SFILES := $(addsuffix .s, $(FUNCS)) diff --git a/sdk/source/FEOSSTDIO/Makefile b/sdk/source/FEOSSTDIO/Makefile new file mode 100644 index 0000000..0b40263 --- /dev/null +++ b/sdk/source/FEOSSTDIO/Makefile @@ -0,0 +1,48 @@ +.SUFFIXES: + +MODULE := $(shell basename $(CURDIR)) + +FUNCS := \ +FeOS_GetStdin \ +FeOS_GetStdout \ +FeOS_GetStderr \ +fopen \ +freopen \ +fclose \ +fwrite \ +fread \ +feof \ +fseek \ +ftell \ +fflush \ +ferror \ +vfprintf \ +vsprintf \ +vfscanf \ +vsscanf \ +fgetc \ +fputc \ +fgets \ +fputs + +SFILES := $(addsuffix .s, $(FUNCS)) + +.PHONY: all clean + +all: $(SFILES) + +%.s: + @echo $@ + @printf ".section .imp.%b, \"ax\", %%progbits\n" $(MODULE) > $@ + @printf ".global __imp_%b\n" $(@:.s=) >> $@ + @printf ".hidden __imp_%b\n" $(@:.s=) >> $@ + @printf ".global %b\n" $(@:.s=) >> $@ + @printf ".hidden %b\n" $(@:.s=) >> $@ + @printf "%b:\n" $(@:.s=) >> $@ + @printf "\tldr r12, [pc]\n" >> $@ + @printf "\tbx r12\n" >> $@ + @printf "__imp_%b:\n" $(@:.s=) >> $@ + @printf "\t.word 0" >> $@ + +clean: + @rm -fr $(SFILES) diff --git a/sdk/source/stdio/fprintf.c b/sdk/source/stdio/fprintf.c new file mode 100644 index 0000000..507743d --- /dev/null +++ b/sdk/source/stdio/fprintf.c @@ -0,0 +1,10 @@ +#include + +int fprintf(FILE* f, const char* fmt, ...) +{ + va_list v; + va_start(v, fmt); + int rc = vfprintf(f, fmt, v); + va_end(v); + return rc; +} diff --git a/sdk/source/stdio/fscanf.c b/sdk/source/stdio/fscanf.c new file mode 100644 index 0000000..62190e2 --- /dev/null +++ b/sdk/source/stdio/fscanf.c @@ -0,0 +1,10 @@ +#include + +int fscanf(FILE* f, const char* fmt, ...) +{ + va_list v; + va_start(v, fmt); + int rc = vfscanf(f, fmt, v); + va_end(v); + return rc; +} diff --git a/sdk/source/stdio/getchar.c b/sdk/source/stdio/getchar.c new file mode 100644 index 0000000..b5eb7c6 --- /dev/null +++ b/sdk/source/stdio/getchar.c @@ -0,0 +1,6 @@ +#include + +int getchar() +{ + return fgetc(stdin); +} diff --git a/sdk/source/stdio/gets.c b/sdk/source/stdio/gets.c new file mode 100644 index 0000000..7da726c --- /dev/null +++ b/sdk/source/stdio/gets.c @@ -0,0 +1,6 @@ +#include + +char* gets(char* buf) +{ + return fgets(buf, 1024, stdin); +} diff --git a/sdk/source/stdio/printf.c b/sdk/source/stdio/printf.c new file mode 100644 index 0000000..d9a75e7 --- /dev/null +++ b/sdk/source/stdio/printf.c @@ -0,0 +1,10 @@ +#include + +int printf(const char* fmt, ...) +{ + va_list v; + va_start(v, fmt); + int rc = vprintf(fmt, v); + va_end(v); + return rc; +} diff --git a/sdk/source/stdio/putchar.c b/sdk/source/stdio/putchar.c new file mode 100644 index 0000000..bbfeb8c --- /dev/null +++ b/sdk/source/stdio/putchar.c @@ -0,0 +1,6 @@ +#include + +int putchar(int ch) +{ + return fputc(ch, stdout); +} diff --git a/sdk/source/stdio/puts.c b/sdk/source/stdio/puts.c new file mode 100644 index 0000000..2148f5b --- /dev/null +++ b/sdk/source/stdio/puts.c @@ -0,0 +1,10 @@ +#include + +int puts(const char* str) +{ + FILE* f = stdout; + + int rc = fputs(str, f); + fputc('\n', f); + return rc; +} diff --git a/sdk/source/stdio/rewind.c b/sdk/source/stdio/rewind.c new file mode 100644 index 0000000..08f369f --- /dev/null +++ b/sdk/source/stdio/rewind.c @@ -0,0 +1,6 @@ +#include + +void rewind(FILE* f) +{ + fseek(f, 0, SEEK_SET); +} diff --git a/sdk/source/stdio/scanf.c b/sdk/source/stdio/scanf.c new file mode 100644 index 0000000..f7c8c0b --- /dev/null +++ b/sdk/source/stdio/scanf.c @@ -0,0 +1,10 @@ +#include + +int scanf(const char* fmt, ...) +{ + va_list v; + va_start(v, fmt); + int rc = vscanf(fmt, v); + va_end(v); + return rc; +} diff --git a/sdk/source/stdio/sprintf.c b/sdk/source/stdio/sprintf.c new file mode 100644 index 0000000..218a60a --- /dev/null +++ b/sdk/source/stdio/sprintf.c @@ -0,0 +1,10 @@ +#include + +int sprintf(char* buf, const char* fmt, ...) +{ + va_list v; + va_start(v, fmt); + int rc = vsprintf(buf, fmt, v); + va_end(v); + return rc; +} diff --git a/sdk/source/stdio/sscanf.c b/sdk/source/stdio/sscanf.c new file mode 100644 index 0000000..976b200 --- /dev/null +++ b/sdk/source/stdio/sscanf.c @@ -0,0 +1,10 @@ +#include + +int sscanf(const char* buf, const char* fmt, ...) +{ + va_list v; + va_start(v, fmt); + int rc = vsscanf(buf, fmt, v); + va_end(v); + return rc; +} diff --git a/sdk/source/stdio/vprintf.c b/sdk/source/stdio/vprintf.c new file mode 100644 index 0000000..6cd1a5d --- /dev/null +++ b/sdk/source/stdio/vprintf.c @@ -0,0 +1,6 @@ +#include + +int vprintf(const char* fmt, va_list v) +{ + return vfprintf(stdout, fmt, v); +} diff --git a/sdk/source/stdio/vscanf.c b/sdk/source/stdio/vscanf.c new file mode 100644 index 0000000..70cfb86 --- /dev/null +++ b/sdk/source/stdio/vscanf.c @@ -0,0 +1,6 @@ +#include + +int vscanf(const char* fmt, va_list v) +{ + return vfscanf(stdin, fmt, v); +}