From 5d8b8256596fb58ac33ff91b1aeeb48b6ae8d650 Mon Sep 17 00:00:00 2001 From: apio Date: Mon, 3 Oct 2022 21:24:38 +0200 Subject: [PATCH] Add a few example apps/demos in C, and make the kernel choose a random one to demonstrate every boot --- apps/Makefile | 2 +- apps/src/art.c | 18 ++++++++++++++++++ apps/src/fib.c | 34 ++++++++++++++++++++++++++++++++++ apps/src/leap.c | 38 ++++++++++++++++++++++++++++++++++++++ apps/src/memeater.c | 17 +++++++++++++++++ kernel/src/main.cpp | 26 +++++++++++++++++++++++++- 6 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 apps/src/art.c create mode 100644 apps/src/fib.c create mode 100644 apps/src/leap.c create mode 100644 apps/src/memeater.c diff --git a/apps/Makefile b/apps/Makefile index a4814f10..ad6eb044 100644 --- a/apps/Makefile +++ b/apps/Makefile @@ -1,4 +1,4 @@ -APPS := init +APPS := init fib leap art memeater APPS_DIR := $(LUNA_ROOT)/apps APPS_SRC := $(APPS_DIR)/src diff --git a/apps/src/art.c b/apps/src/art.c new file mode 100644 index 00000000..28c47432 --- /dev/null +++ b/apps/src/art.c @@ -0,0 +1,18 @@ +#include +#include +#include + +int main() +{ + sleep(1); + + syscall(SYS_paint, 0, 0, 400, 300, 0x000000FF); + + sleep(1); + + printf("Hello, colorful world!\n"); + + sleep(1); + + printf("Press any key to restart."); +} \ No newline at end of file diff --git a/apps/src/fib.c b/apps/src/fib.c new file mode 100644 index 00000000..c32fa917 --- /dev/null +++ b/apps/src/fib.c @@ -0,0 +1,34 @@ +#include +#include +#include + +void fib_next(unsigned long int* a, unsigned long int* b) +{ + unsigned long int _a = *a; + unsigned long int _b = *b; + *a = *b; + *b = _a + _b; +} + +int main() +{ + unsigned long int fib_a = 1; + unsigned long int fib_b = 1; + + printf("Calculating the 50 first Fibonacci numbers...\n"); + + sleep(2); + + printf("%lu\n", fib_a); + + msleep(500); + + for (int i = 0; i < 49; i++) + { + printf("%lu\n", fib_b); + fib_next(&fib_a, &fib_b); + msleep(500); + } + + printf("\nDone, press any key to restart.\n"); +} \ No newline at end of file diff --git a/apps/src/leap.c b/apps/src/leap.c new file mode 100644 index 00000000..9b5c986d --- /dev/null +++ b/apps/src/leap.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include + +unsigned long random_year() // Return a year from 1000 to 2500. +{ + unsigned long result = syscall(SYS_rand); + return (result % 1500) + 1000; +} + +int is_leap(unsigned long year) +{ + return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0); +} + +int main() +{ + printf("Welcome to the Luna Leap Year Program!\n\n"); + + sleep(1); + + printf("Choosing a random year between 1000 and 2500... "); + + msleep(500); + + unsigned long year = random_year(); + + printf("%lu!\n\n", year); + + sleep(1); + + printf("%lu is %s\n", year, is_leap(year) ? "a leap year!" : "not a leap year :("); + + msleep(500); + + printf("Press any key to restart.\n"); +} \ No newline at end of file diff --git a/apps/src/memeater.c b/apps/src/memeater.c new file mode 100644 index 00000000..875d319b --- /dev/null +++ b/apps/src/memeater.c @@ -0,0 +1,17 @@ +#include +#include +#include + +#define CHUNK 4194304 // 4 MB + +int main() +{ + printf("Welcome to memeater! This little program is designed to eat up all the memory on your system.\n"); + sleep(1); + void* allocated = malloc(CHUNK); + do { + printf("Allocating 4 MB of memory... %lx\n", (unsigned long)allocated); + sleep(1); + } while ((allocated = malloc(CHUNK))); + printf("Out of memory.\n"); +} \ No newline at end of file diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index 10393bef..d2185c75 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -124,7 +124,31 @@ extern "C" void _start() } }); - Scheduler::load_user_task("bin/init"); + int demo = Mersenne::get() % 5; + + switch (demo) + { + case 0: + kinfoln("Loading demo: example init program"); + Scheduler::load_user_task("bin/init"); + break; + case 1: + kinfoln("Loading demo: first 50 fibonacci numbers"); + Scheduler::load_user_task("bin/fib"); + break; + case 2: + kinfoln("Loading demo: leap year calculator"); + Scheduler::load_user_task("bin/leap"); + break; + case 3: + kinfoln("Loading demo: painting program"); + Scheduler::load_user_task("bin/art"); + break; + case 4: + kinfoln("Loading demo: memory eating program"); + Scheduler::load_user_task("bin/memeater"); + break; + } kinfoln("Prepared scheduler tasks");