aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2020-08-22 17:38:40 +0200
committerMarvin Borner2020-08-22 17:38:40 +0200
commit424dc57424ee17de77d689443f95d2e1bed72f4a (patch)
tree1ae2f071ee2de984a2f112de0e69208e593ddc51
parentd748a6f54ce7af45afde487936d5a02cb2a91b05 (diff)
Added random number generator
-rw-r--r--apps/wm.c3
-rw-r--r--kernel/config.h14
-rw-r--r--kernel/drivers/timer.c5
-rw-r--r--kernel/features/syscall.c6
-rw-r--r--kernel/inc/timer.h1
-rw-r--r--kernel/main.c4
-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
10 files changed, 64 insertions, 19 deletions
diff --git a/apps/wm.c b/apps/wm.c
index 262270a..f3ac901 100644
--- a/apps/wm.c
+++ b/apps/wm.c
@@ -4,6 +4,7 @@
#include <gui.h>
#include <input.h>
#include <print.h>
+#include <random.h>
#include <sys.h>
#include <vesa.h>
@@ -18,6 +19,8 @@ void onkey(u32 scancode)
int main(int argc, char **argv)
{
+ srand(time());
+
printf("ARGC: %d\n", argc);
printf("[%s loaded]\n", argv[0]);
diff --git a/kernel/config.h b/kernel/config.h
deleted file mode 100644
index 29c0e23..0000000
--- a/kernel/config.h
+++ /dev/null
@@ -1,14 +0,0 @@
-// MIT License, Copyright (c) 2020 Marvin Borner
-// General kernel config
-
-#ifndef CONFIG_H
-#define CONFIG_H
-
-int MELVIX_VERSION = 0;
-
-#define USERNAME "Neo"
-//#define FONT_PATH "/font/ter-p32n.psf"
-#define FONT_PATH "/font/spleen-16x32.psfu"
-#define NETWORK "rtl8139"
-
-#endif
diff --git a/kernel/drivers/timer.c b/kernel/drivers/timer.c
index 0207cc0..79e3cd8 100644
--- a/kernel/drivers/timer.c
+++ b/kernel/drivers/timer.c
@@ -14,6 +14,11 @@ void timer_phase(int hz)
outb(0x40, divisor >> 8);
}
+u32 timer_get()
+{
+ return timer_ticks;
+}
+
// Executed 1000 times per second
void timer_handler()
{
diff --git a/kernel/features/syscall.c b/kernel/features/syscall.c
index 157f10c..c8da824 100644
--- a/kernel/features/syscall.c
+++ b/kernel/features/syscall.c
@@ -10,6 +10,7 @@
#include <proc.h>
#include <str.h>
#include <sys.h>
+#include <timer.h>
void syscall_handler(struct regs *r)
{
@@ -66,6 +67,11 @@ void syscall_handler(struct regs *r)
proc_exit(proc_current(), r->ebx);
break;
}
+ case SYS_TIME: {
+ printf("time\n");
+ r->eax = timer_get();
+ break;
+ }
case SYS_MAP: {
printf("map\n");
event_map(r->ebx, proc_current(), (u32 *)r->ecx);
diff --git a/kernel/inc/timer.h b/kernel/inc/timer.h
index 7c4f077..0f8842b 100644
--- a/kernel/inc/timer.h
+++ b/kernel/inc/timer.h
@@ -5,6 +5,7 @@
#include <def.h>
+u32 timer_get();
void timer_install();
void timer_handler(); // For scheduler
diff --git a/kernel/main.c b/kernel/main.c
index d431cf6..c4b51cf 100644
--- a/kernel/main.c
+++ b/kernel/main.c
@@ -1,15 +1,11 @@
// MIT License, Copyright (c) 2020 Marvin Borner
-#include "config.h"
#include <boot.h>
#include <cpu.h>
-#include <def.h>
#include <fs.h>
-#include <interrupts.h>
#include <keyboard.h>
#include <load.h>
#include <mem.h>
-#include <print.h>
#include <serial.h>
#include <syscall.h>
#include <timer.h>
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;
+}