aboutsummaryrefslogtreecommitdiff
path: root/libc
diff options
context:
space:
mode:
Diffstat (limited to 'libc')
-rw-r--r--libc/Makefile3
-rw-r--r--libc/inc/random.h12
-rw-r--r--libc/inc/sys.h2
-rw-r--r--libc/random.c33
4 files changed, 49 insertions, 1 deletions
diff --git a/libc/Makefile b/libc/Makefile
index d57b9c4..3458256 100644
--- a/libc/Makefile
+++ b/libc/Makefile
@@ -9,7 +9,8 @@ COBJS = str.o \
serial.o \
cpu.o \
sys.o \
- list.o
+ list.o \
+ random.o
CC = ../cross/opt/bin/i686-elf-gcc
LD = ../cross/opt/bin/i686-elf-ld
AR = ../cross/opt/bin/i686-elf-ar
diff --git a/libc/inc/random.h b/libc/inc/random.h
new file mode 100644
index 0000000..50c849b
--- /dev/null
+++ b/libc/inc/random.h
@@ -0,0 +1,12 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#ifndef RANDOM_H
+#define RANDOM_H
+
+#include <def.h>
+
+void srand(u32 seed);
+u32 rand();
+char *randstr(u32 size);
+
+#endif
diff --git a/libc/inc/sys.h b/libc/inc/sys.h
index 2014da9..bb5dec2 100644
--- a/libc/inc/sys.h
+++ b/libc/inc/sys.h
@@ -12,6 +12,7 @@ enum sys {
SYS_WRITE,
SYS_EXEC,
SYS_EXIT,
+ SYS_TIME,
SYS_MAP,
SYS_UNMAP,
SYS_RESOLVE
@@ -42,6 +43,7 @@ int sysv(enum sys num, ...);
while (1) { \
} \
}
+#define time() sys0(SYS_TIME)
#define event_map(id, func) sys2(SYS_MAP, (int)id, (int)func)
#define event_unmap(id, func) sys2(SYS_UNMAP, (int)id, (int)func)
#define event_resolve() sys0(SYS_RESOLVE) // TODO: Find method making event_resolve obsolete
diff --git a/libc/random.c b/libc/random.c
new file mode 100644
index 0000000..7ad8a67
--- /dev/null
+++ b/libc/random.c
@@ -0,0 +1,33 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#include <def.h>
+#include <mem.h>
+#include <random.h>
+
+static u32 g_seed = 0;
+
+void srand(u32 seed)
+{
+ g_seed = seed;
+}
+
+u32 rand()
+{
+ g_seed = (214013 * g_seed + 2531011);
+ return (g_seed >> 16) & 0x7FFF;
+}
+
+char *randstr(u32 size)
+{
+ char *buf = malloc(size + 1);
+ const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+
+ size--;
+ for (u32 i = 0; i < size; i++) {
+ int key = rand() % (sizeof(charset) - 1);
+ buf[i] = charset[key];
+ }
+ buf[size] = '\0';
+
+ return buf;
+}