summaryrefslogtreecommitdiff
path: root/userspace/syscalls.c
diff options
context:
space:
mode:
authorMichal Idziorek <m.i@gmx.at>2014-10-21 00:13:40 +0200
committerMichal Idziorek <m.i@gmx.at>2014-10-21 00:13:40 +0200
commitc81e5044a5d609a42407a5e9b0725d22e33cf5fa (patch)
treebcdd3ed322f64c8bde316bfaf065fe2248947ef1 /userspace/syscalls.c
parent37ceff93572bba6defd916884999c496108220ee (diff)
ported Brainfuck interpreter.
Diffstat (limited to 'userspace/syscalls.c')
-rw-r--r--userspace/syscalls.c84
1 files changed, 68 insertions, 16 deletions
diff --git a/userspace/syscalls.c b/userspace/syscalls.c
index 6547879..d8a463d 100644
--- a/userspace/syscalls.c
+++ b/userspace/syscalls.c
@@ -1,39 +1,79 @@
-
//printf needs following syscalls: sbrk write close fstat isatty lseek read
//
#include <sys/stat.h>
+#include <string.h>
+
+static int preread;
+static int alloc;
+easywrite(char *c);
+
+void syscalls_init()
+{
+ preread=0;
+ alloc=0xff0000;
+}
int close(int file)
{
- return -1;
+ easywrite("syscall: close\n");
+ return -1;
}
int fstat(int file, struct stat *st)
{
+// easywrite("syscall: fstat\n");
st->st_mode = S_IFCHR;
return 0;
}
-int isatty(int file) {
+int isatty(int file)
+{
+ easywrite("syscall: isatty\n");
return 1;
}
-int lseek(int file, int ptr, int dir) {
- return 0;
+int lseek(int file, int ptr, int dir)
+{
+// printf("syscall: lseek %i %i %i",file,ptr,dir);
+ if(dir==0)preread=ptr;
+ else{ puts("other modes unsupported sorry"); while(1);}
+ return preread;
}
-int read(int file, char *ptr, int len) {
- return 0;
+
+
+int read(int file, char *ptr, int len)
+{
+// easywrite("syscall: read\n");
+
+ int i;
+ char buf[]="++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.";
+
+ if(preread>=strlen(buf)){
+ easywrite("syscall: read = EOF\n");
+ while(1);
+ return 0; //EOF
+ }
+
+ for(i=0;i<len;i++)
+ {
+ if(preread>=strlen(buf))return i;
+ ptr[i]=buf[preread++];
+ }
+
+ return len;
}
- int open(const char *name, int flags, int mode) {
- return -1;
- }
+int open(const char *name, int flags, int mode)
+{
+ easywrite("syscall: open\n");
+ return 99;
+ return -1;
+}
int write(int file, char *ptr, int len)
{
-
int todo;
for (todo = 0; todo < len; todo++)
{
@@ -58,28 +98,40 @@ int write(int file, char *ptr, int len)
return len;
}
+
+
caddr_t sbrk(int incr)
{
+ easywrite("syscall: sbrk!!\n");
+ int oldalloc=alloc;
+ alloc+=incr;
+ return oldalloc;
+ /*
-// extern char _end; /* Defined by the linker */
- char _end;
+ extern char end;
+// char _end;
static char *heap_end;
char *prev_heap_end;
if (heap_end == 0) {
- heap_end = &_end;
+ heap_end = &end;
}
prev_heap_end = heap_end;
- /*
+
if (heap_end + incr > stack_ptr) {
write (1, "Heap and stack collision\n", 25);
abort ();
}
- */
heap_end += incr;
return (caddr_t) prev_heap_end;
+ */
}
//
+// helpers
+easywrite(char *c)
+{
+ write(1,c,strlen(c));
+}