From 373c554708f24f7e84e5bd5b2b3463d49d6fcd9e Mon Sep 17 00:00:00 2001 From: Suguru Saito Date: Wed, 15 Dec 2021 01:27:08 +0900 Subject: [PATCH] x1 experimental libbrain --- x1/linkerscript/Makefile | 6 ++- x1/linkerscript/libbrain/Makefile | 23 +++++++++++ x1/linkerscript/libbrain/libbrain.h | 54 +++++++++++++++++++++++++ x1/linkerscript/libbrain/sys_closedir.s | 8 ++++ x1/linkerscript/libbrain/sys_fclose.s | 8 ++++ x1/linkerscript/libbrain/sys_feof.s | 8 ++++ x1/linkerscript/libbrain/sys_ferror.s | 8 ++++ x1/linkerscript/libbrain/sys_fflush.s | 8 ++++ x1/linkerscript/libbrain/sys_fgetc.s | 8 ++++ x1/linkerscript/libbrain/sys_fgets.s | 8 ++++ x1/linkerscript/libbrain/sys_fopen.s | 8 ++++ x1/linkerscript/libbrain/sys_fputc.s | 8 ++++ x1/linkerscript/libbrain/sys_fputs.s | 8 ++++ x1/linkerscript/libbrain/sys_fread.s | 8 ++++ x1/linkerscript/libbrain/sys_free.s | 8 ++++ x1/linkerscript/libbrain/sys_ftell.s | 8 ++++ x1/linkerscript/libbrain/sys_fwrite.s | 8 ++++ x1/linkerscript/libbrain/sys_malloc.s | 8 ++++ x1/linkerscript/libbrain/sys_mkdir.s | 8 ++++ x1/linkerscript/libbrain/sys_opendir.s | 8 ++++ x1/linkerscript/libbrain/sys_printf.s | 8 ++++ x1/linkerscript/libbrain/sys_readdir.s | 8 ++++ x1/linkerscript/libbrain/sys_remove.s | 8 ++++ x1/linkerscript/libbrain/sys_rename.s | 8 ++++ x1/linkerscript/libbrain/sys_stat.s | 8 ++++ x1/linkerscript/main.c | 3 ++ 26 files changed, 260 insertions(+), 2 deletions(-) create mode 100644 x1/linkerscript/libbrain/Makefile create mode 100644 x1/linkerscript/libbrain/libbrain.h create mode 100644 x1/linkerscript/libbrain/sys_closedir.s create mode 100644 x1/linkerscript/libbrain/sys_fclose.s create mode 100644 x1/linkerscript/libbrain/sys_feof.s create mode 100644 x1/linkerscript/libbrain/sys_ferror.s create mode 100644 x1/linkerscript/libbrain/sys_fflush.s create mode 100644 x1/linkerscript/libbrain/sys_fgetc.s create mode 100644 x1/linkerscript/libbrain/sys_fgets.s create mode 100644 x1/linkerscript/libbrain/sys_fopen.s create mode 100644 x1/linkerscript/libbrain/sys_fputc.s create mode 100644 x1/linkerscript/libbrain/sys_fputs.s create mode 100644 x1/linkerscript/libbrain/sys_fread.s create mode 100644 x1/linkerscript/libbrain/sys_free.s create mode 100644 x1/linkerscript/libbrain/sys_ftell.s create mode 100644 x1/linkerscript/libbrain/sys_fwrite.s create mode 100644 x1/linkerscript/libbrain/sys_malloc.s create mode 100644 x1/linkerscript/libbrain/sys_mkdir.s create mode 100644 x1/linkerscript/libbrain/sys_opendir.s create mode 100644 x1/linkerscript/libbrain/sys_printf.s create mode 100644 x1/linkerscript/libbrain/sys_readdir.s create mode 100644 x1/linkerscript/libbrain/sys_remove.s create mode 100644 x1/linkerscript/libbrain/sys_rename.s create mode 100644 x1/linkerscript/libbrain/sys_stat.s diff --git a/x1/linkerscript/Makefile b/x1/linkerscript/Makefile index a107a2c..881d8ed 100644 --- a/x1/linkerscript/Makefile +++ b/x1/linkerscript/Makefile @@ -7,8 +7,10 @@ OBJCOPY := $(CROSS_COMPILE)objcopy TARGET := AppMain OBJS := crt0.o main.o +LIBS := libbrain/libbrain.a + ASFLAGS := -W -CFLAGS := -Wall -ffreestanding -fomit-frame-pointer -Os +CFLAGS := -Wall -ffreestanding -fomit-frame-pointer -Os -Ilibbrain LDFLAGS := -T x1.ld .PHONY: @@ -25,7 +27,7 @@ clean: $(CC) -c $(CFLAGS) $< $(TARGET).elf: $(OBJS) - $(LD) $(LDFLAGS) $^ -o $@ + $(LD) $(LDFLAGS) $^ $(LIBS) -o $@ $(TARGET).bin: $(TARGET).elf $(OBJCOPY) -O binary $< $@ diff --git a/x1/linkerscript/libbrain/Makefile b/x1/linkerscript/libbrain/Makefile new file mode 100644 index 0000000..88aa957 --- /dev/null +++ b/x1/linkerscript/libbrain/Makefile @@ -0,0 +1,23 @@ +CROSS_COMPILE := arm-linux-gnueabihf- +AS := $(CROSS_COMPILE)as +AR := $(CROSS_COMPILE)ar + +TARGET := libbrain.a +OBJS := sys_closedir.o sys_feof.o sys_fflush.o sys_fgets.o sys_fputc.o sys_fread.o sys_ftell.o sys_malloc.o sys_opendir.o sys_readdir.o sys_rename.o sys_fclose.o sys_ferror.o sys_fgetc.o sys_fopen.o sys_fputs.o sys_free.o sys_fwrite.o sys_mkdir.o sys_printf.o sys_remove.o sys_stat.o + +ASFLAGS := -W +ARFLAGS := -crs + +.PHONY: +all: $(TARGET) + +.PHONY: +clean: + -rm -f $(TARGET) *.o + +%.o: %.s + $(AS) $(ASFLAGS) $< -o $@ + +$(TARGET): $(OBJS) + $(AR) $(ARFLAGS) $@ $^ + diff --git a/x1/linkerscript/libbrain/libbrain.h b/x1/linkerscript/libbrain/libbrain.h new file mode 100644 index 0000000..2850b02 --- /dev/null +++ b/x1/linkerscript/libbrain/libbrain.h @@ -0,0 +1,54 @@ +#ifndef H_LIB_BRAIN_H +#define H_LIB_BRAIN_H + +typedef unsigned long size_t; +typedef void FILE; +typedef void* DIR; + +struct stat { + unsigned int flags; + unsigned int st_atim; + unsigned int st_mtim; + unsigned int st_ctim; + size_t st_size; +}; + +struct dirent { + unsigned int type; + size_t size; + size_t filename_length; + char filename[260]; +}; + +enum { + SEEK_SET = 0, + SEEK_CUR, + SEEK_END, +}; + +void *sys_malloc(size_t size); +int sys_free(void *ptr); +FILE *sys_fopen(const char *filename, const char *mode); +int sys_fclose(FILE *stream); +int sys_ftell(FILE *stream); +size_t sys_fread(void *ptr, size_t size, size_t nmemb, FILE *stream); +size_t sys_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); +int sys_fseek(FILE *stream, int offset, int whence); +int sys_feof(FILE *stream); +int sys_fflush(FILE *stream); +int sys_fgetc(FILE *stream); +char *sys_fgets(char *s, int n, FILE *stream); +int sys_fputc(int ch, FILE *stream); +int sys_fputs(const char *str, FILE *stream); +int sys_ferror(FILE *stream); +int sys_remove(const char *filepath); +int sys_rename(const char *oldpath, const char *newpath); +int sys_mkdir(const char *pathname); +int sys_stat(const char *filepath, struct stat *buf); +int sys_closedir(DIR *dirp); +struct dirent *sys_readdir(DIR *dirp); +DIR *sys_opendir(const char *pathname); + +int sys_printf(const char *fmt); + +#endif diff --git a/x1/linkerscript/libbrain/sys_closedir.s b/x1/linkerscript/libbrain/sys_closedir.s new file mode 100644 index 0000000..03ce466 --- /dev/null +++ b/x1/linkerscript/libbrain/sys_closedir.s @@ -0,0 +1,8 @@ +.text +.align 4 + +.global sys_closedir +.type sys_closedir, %function +sys_closedir: + ldr pc, [pc, #-4] + .word 0x6000644c diff --git a/x1/linkerscript/libbrain/sys_fclose.s b/x1/linkerscript/libbrain/sys_fclose.s new file mode 100644 index 0000000..cbdb202 --- /dev/null +++ b/x1/linkerscript/libbrain/sys_fclose.s @@ -0,0 +1,8 @@ +.text +.align 4 + +.global sys_fclose +.type sys_fclose, %function +sys_fclose: + ldr pc, [pc, #-4] + .word 0x60006404 diff --git a/x1/linkerscript/libbrain/sys_feof.s b/x1/linkerscript/libbrain/sys_feof.s new file mode 100644 index 0000000..ca94ebf --- /dev/null +++ b/x1/linkerscript/libbrain/sys_feof.s @@ -0,0 +1,8 @@ +.text +.align 4 + +.global sys_feof +.type sys_feof, %function +sys_feof: + ldr pc, [pc, #-4] + .word 0x60006418 diff --git a/x1/linkerscript/libbrain/sys_ferror.s b/x1/linkerscript/libbrain/sys_ferror.s new file mode 100644 index 0000000..37347dd --- /dev/null +++ b/x1/linkerscript/libbrain/sys_ferror.s @@ -0,0 +1,8 @@ +.text +.align 4 + +.global sys_ferror +.type sys_ferror, %function +sys_ferror: + ldr pc, [pc, #-4] + .word 0x60006430 diff --git a/x1/linkerscript/libbrain/sys_fflush.s b/x1/linkerscript/libbrain/sys_fflush.s new file mode 100644 index 0000000..b126b64 --- /dev/null +++ b/x1/linkerscript/libbrain/sys_fflush.s @@ -0,0 +1,8 @@ +.text +.align 4 + +.global sys_fflush +.type sys_fflush, %function +sys_fflush: + ldr pc, [pc, #-4] + .word 0x6000641c diff --git a/x1/linkerscript/libbrain/sys_fgetc.s b/x1/linkerscript/libbrain/sys_fgetc.s new file mode 100644 index 0000000..6c17648 --- /dev/null +++ b/x1/linkerscript/libbrain/sys_fgetc.s @@ -0,0 +1,8 @@ +.text +.align 4 + +.global sys_fgetc +.type sys_fgetc, %function +sys_fgetc: + ldr pc, [pc, #-4] + .word 0x60006420 diff --git a/x1/linkerscript/libbrain/sys_fgets.s b/x1/linkerscript/libbrain/sys_fgets.s new file mode 100644 index 0000000..a56ee21 --- /dev/null +++ b/x1/linkerscript/libbrain/sys_fgets.s @@ -0,0 +1,8 @@ +.text +.align 4 + +.global sys_fgets +.type sys_fgets, %function +sys_fgets: + ldr pc, [pc, #-4] + .word 0x60006424 diff --git a/x1/linkerscript/libbrain/sys_fopen.s b/x1/linkerscript/libbrain/sys_fopen.s new file mode 100644 index 0000000..0c24a42 --- /dev/null +++ b/x1/linkerscript/libbrain/sys_fopen.s @@ -0,0 +1,8 @@ +.text +.align 4 + +.global sys_fopen +.type sys_fopen, %function +sys_fopen: + ldr pc, [pc, #-4] + .word 0x60006400 diff --git a/x1/linkerscript/libbrain/sys_fputc.s b/x1/linkerscript/libbrain/sys_fputc.s new file mode 100644 index 0000000..12aa856 --- /dev/null +++ b/x1/linkerscript/libbrain/sys_fputc.s @@ -0,0 +1,8 @@ +.text +.align 4 + +.global sys_fputc +.type sys_fputc, %function +sys_fputc: + ldr pc, [pc, #-4] + .word 0x60006428 diff --git a/x1/linkerscript/libbrain/sys_fputs.s b/x1/linkerscript/libbrain/sys_fputs.s new file mode 100644 index 0000000..a885027 --- /dev/null +++ b/x1/linkerscript/libbrain/sys_fputs.s @@ -0,0 +1,8 @@ +.text +.align 4 + +.global sys_fputs +.type sys_fputs, %function +sys_fputs: + ldr pc, [pc, #-4] + .word 0x6000642c diff --git a/x1/linkerscript/libbrain/sys_fread.s b/x1/linkerscript/libbrain/sys_fread.s new file mode 100644 index 0000000..e0f2e5b --- /dev/null +++ b/x1/linkerscript/libbrain/sys_fread.s @@ -0,0 +1,8 @@ +.text +.align 4 + +.global sys_fread +.type sys_fread, %function +sys_fread: + ldr pc, [pc, #-4] + .word 0x6000640c diff --git a/x1/linkerscript/libbrain/sys_free.s b/x1/linkerscript/libbrain/sys_free.s new file mode 100644 index 0000000..ac4a70b --- /dev/null +++ b/x1/linkerscript/libbrain/sys_free.s @@ -0,0 +1,8 @@ +.text +.align 4 + +.global sys_free +.type sys_free, %function +sys_free: + ldr pc, [pc, #-4] + .word 0x600063c8 diff --git a/x1/linkerscript/libbrain/sys_ftell.s b/x1/linkerscript/libbrain/sys_ftell.s new file mode 100644 index 0000000..60cba20 --- /dev/null +++ b/x1/linkerscript/libbrain/sys_ftell.s @@ -0,0 +1,8 @@ +.text +.align 4 + +.global sys_ftell +.type sys_ftell, %function +sys_ftell: + ldr pc, [pc, #-4] + .word 0x60006408 diff --git a/x1/linkerscript/libbrain/sys_fwrite.s b/x1/linkerscript/libbrain/sys_fwrite.s new file mode 100644 index 0000000..7e6ff3a --- /dev/null +++ b/x1/linkerscript/libbrain/sys_fwrite.s @@ -0,0 +1,8 @@ +.text +.align 4 + +.global sys_fwrite +.type sys_fwrite, %function +sys_fwrite: + ldr pc, [pc, #-4] + .word 0x60006410 diff --git a/x1/linkerscript/libbrain/sys_malloc.s b/x1/linkerscript/libbrain/sys_malloc.s new file mode 100644 index 0000000..b820c6e --- /dev/null +++ b/x1/linkerscript/libbrain/sys_malloc.s @@ -0,0 +1,8 @@ +.text +.align 4 + +.global sys_malloc +.type sys_malloc, %function +sys_malloc: + ldr pc, [pc, #-4] + .word 0x600063c4 diff --git a/x1/linkerscript/libbrain/sys_mkdir.s b/x1/linkerscript/libbrain/sys_mkdir.s new file mode 100644 index 0000000..26728c8 --- /dev/null +++ b/x1/linkerscript/libbrain/sys_mkdir.s @@ -0,0 +1,8 @@ +.text +.align 4 + +.global sys_mkdir +.type sys_mkdir, %function +sys_mkdir: + ldr pc, [pc, #-4] + .word 0x6000643c diff --git a/x1/linkerscript/libbrain/sys_opendir.s b/x1/linkerscript/libbrain/sys_opendir.s new file mode 100644 index 0000000..14164d4 --- /dev/null +++ b/x1/linkerscript/libbrain/sys_opendir.s @@ -0,0 +1,8 @@ +.text +.align 4 + +.global sys_opendir +.type sys_opendir, %function +sys_opendir: + ldr pc, [pc, #-4] + .word 0x60006454 diff --git a/x1/linkerscript/libbrain/sys_printf.s b/x1/linkerscript/libbrain/sys_printf.s new file mode 100644 index 0000000..cd0c07c --- /dev/null +++ b/x1/linkerscript/libbrain/sys_printf.s @@ -0,0 +1,8 @@ +.text +.align 4 + +.global sys_printf +.type sys_printf, %function +sys_printf: + ldr pc, [pc, #-4] + .word 0x60092004 diff --git a/x1/linkerscript/libbrain/sys_readdir.s b/x1/linkerscript/libbrain/sys_readdir.s new file mode 100644 index 0000000..cb0eec7 --- /dev/null +++ b/x1/linkerscript/libbrain/sys_readdir.s @@ -0,0 +1,8 @@ +.text +.align 4 + +.global sys_readdir +.type sys_readdir, %function +sys_readdir: + ldr pc, [pc, #-4] + .word 0x60006450 diff --git a/x1/linkerscript/libbrain/sys_remove.s b/x1/linkerscript/libbrain/sys_remove.s new file mode 100644 index 0000000..1a746ba --- /dev/null +++ b/x1/linkerscript/libbrain/sys_remove.s @@ -0,0 +1,8 @@ +.text +.align 4 + +.global sys_remove +.type sys_remove, %function +sys_remove: + ldr pc, [pc, #-4] + .word 0x60006434 diff --git a/x1/linkerscript/libbrain/sys_rename.s b/x1/linkerscript/libbrain/sys_rename.s new file mode 100644 index 0000000..26145be --- /dev/null +++ b/x1/linkerscript/libbrain/sys_rename.s @@ -0,0 +1,8 @@ +.text +.align 4 + +.global sys_rename +.type sys_rename, %function +sys_rename: + ldr pc, [pc, #-4] + .word 0x60006438 diff --git a/x1/linkerscript/libbrain/sys_stat.s b/x1/linkerscript/libbrain/sys_stat.s new file mode 100644 index 0000000..4cbf898 --- /dev/null +++ b/x1/linkerscript/libbrain/sys_stat.s @@ -0,0 +1,8 @@ +.text +.align 4 + +.global sys_stat +.type sys_stat, %function +sys_stat: + ldr pc, [pc, #-4] + .word 0x60006444 diff --git a/x1/linkerscript/main.c b/x1/linkerscript/main.c index 9fbdf5d..7ec6fba 100644 --- a/x1/linkerscript/main.c +++ b/x1/linkerscript/main.c @@ -1,4 +1,7 @@ +#include + int main(void *arg) { + sys_printf("Hello!\n"); return 0; }