Skip to content

Commit

Permalink
Add FEOSSTDIO module with basic stdio.h functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fincs committed Jul 20, 2011
1 parent 462cc59 commit 6886108
Show file tree
Hide file tree
Showing 25 changed files with 308 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -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
Expand Down
11 changes: 1 addition & 10 deletions kernel/source/apibulk.c
Expand Up @@ -4,12 +4,6 @@
#include <stdlib.h>
#include <string.h>

#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)
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions kernel/source/feos.h
Expand Up @@ -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;
Expand Down
67 changes: 67 additions & 0 deletions kernel/source/feosstdio.c
@@ -0,0 +1,67 @@
#include "feos.h"
#include "fxe.h"
#include <sys/fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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;
1 change: 1 addition & 0 deletions kernel/source/fxe.h
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions kernel/source/main.c
Expand Up @@ -97,6 +97,7 @@ int main()
SystemVectors.swi = (u32) __SWIHandler;
setVectorBase(0);
PrepareUserMode();
FeOS_ModuleListInit();

iprintf(
"FeOS kernel\n"
Expand Down
8 changes: 7 additions & 1 deletion 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;
Expand All @@ -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 --;
Expand Down
2 changes: 2 additions & 0 deletions sdk/Makefile
Expand Up @@ -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
66 changes: 65 additions & 1 deletion sdk/include/stdio.h
Expand Up @@ -7,14 +7,78 @@
#pragma once
#include <stddef.h>
#include <limits.h>
#include <stdarg.h>
#include <feosuser.h>

#ifdef __cplusplus
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
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/libfeos.mk
Expand Up @@ -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

Expand Down
4 changes: 1 addition & 3 deletions sdk/source/FEOSBASE/Makefile
Expand Up @@ -30,9 +30,7 @@ strncmp \
memcpy \
memmove \
memset \
memcmp \
printf \
sprintf
memcmp

SFILES := $(addsuffix .s, $(FUNCS))

Expand Down
48 changes: 48 additions & 0 deletions 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)
10 changes: 10 additions & 0 deletions sdk/source/stdio/fprintf.c
@@ -0,0 +1,10 @@
#include <stdio.h>

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;
}
10 changes: 10 additions & 0 deletions sdk/source/stdio/fscanf.c
@@ -0,0 +1,10 @@
#include <stdio.h>

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;
}
6 changes: 6 additions & 0 deletions sdk/source/stdio/getchar.c
@@ -0,0 +1,6 @@
#include <stdio.h>

int getchar()
{
return fgetc(stdin);
}
6 changes: 6 additions & 0 deletions sdk/source/stdio/gets.c
@@ -0,0 +1,6 @@
#include <stdio.h>

char* gets(char* buf)
{
return fgets(buf, 1024, stdin);
}
10 changes: 10 additions & 0 deletions sdk/source/stdio/printf.c
@@ -0,0 +1,10 @@
#include <stdio.h>

int printf(const char* fmt, ...)
{
va_list v;
va_start(v, fmt);
int rc = vprintf(fmt, v);
va_end(v);
return rc;
}
6 changes: 6 additions & 0 deletions sdk/source/stdio/putchar.c
@@ -0,0 +1,6 @@
#include <stdio.h>

int putchar(int ch)
{
return fputc(ch, stdout);
}
10 changes: 10 additions & 0 deletions sdk/source/stdio/puts.c
@@ -0,0 +1,10 @@
#include <stdio.h>

int puts(const char* str)
{
FILE* f = stdout;

int rc = fputs(str, f);
fputc('\n', f);
return rc;
}
6 changes: 6 additions & 0 deletions sdk/source/stdio/rewind.c
@@ -0,0 +1,6 @@
#include <stdio.h>

void rewind(FILE* f)
{
fseek(f, 0, SEEK_SET);
}

0 comments on commit 6886108

Please sign in to comment.