From 2893e75ad15a9a96debaa09f5b98b4664ce5005e Mon Sep 17 00:00:00 2001 From: xamidev <121681048+xamidev@users.noreply.github.com> Date: Sat, 24 Aug 2024 20:30:14 +0200 Subject: [PATCH 1/3] fix: conway, words --- docs/USERS.md | 8 +- src/drivers/serial.c | 2 +- src/kernel/kmain.c | 11 +-- src/libc/string.c | 12 +++ src/libc/string.h | 1 + src/programs/conway.c | 5 +- src/programs/conway.h | 4 +- src/programs/words.c | 171 ++++++++++++++++++++++++++++++++++++++++-- 8 files changed, 196 insertions(+), 18 deletions(-) diff --git a/docs/USERS.md b/docs/USERS.md index 1185b0c..0d503b8 100644 --- a/docs/USERS.md +++ b/docs/USERS.md @@ -43,7 +43,11 @@ Triggers a kernel panic by trying to divide four by zero. #### `words` -Prints ten random words using an arbitrary dictionary that you can expand in `src/programs/words.c`. +Prints random words using an arbitrary dictionary that you can expand in `src/programs/words.c`. + +Options: +- `` will default amount of words to 10 +- `` will set the amount of words to that number #### `primes` @@ -78,7 +82,7 @@ The classic echo command, that outputs your input. Outputs information about the current system (CPU and RAM). Options: -- `nothing` will show basic info about the CPUid and lower/upper memory. +- `` will show basic info about the CPUid and lower/upper memory. - `-v` will output the CPUID, lower/upper memory, and the memory map. #### `conway` diff --git a/src/drivers/serial.c b/src/drivers/serial.c index a9d369f..ffb6cc6 100644 --- a/src/drivers/serial.c +++ b/src/drivers/serial.c @@ -282,5 +282,5 @@ void serial_printf(int errlevel, const char* fmt, ...) } fmt++; } - serial_puts("\n"); + serial_puts("\r\n"); } diff --git a/src/kernel/kmain.c b/src/kernel/kmain.c index 14248bc..8aff6c6 100644 --- a/src/kernel/kmain.c +++ b/src/kernel/kmain.c @@ -31,11 +31,11 @@ void kmain(multiboot2_info *mb_info) tags += ((tag_size + 7) & ~7); } - serial_printf(3, "Framebuffer Address: 0x%x\r", fb_info->framebuffer_addr); - serial_printf(3, "Framebuffer Width: %u\r\n", fb_info->framebuffer_width); - serial_printf(3, "Framebuffer Height: %u\r\n", fb_info->framebuffer_height); - serial_printf(3, "Framebuffer Pitch: %u\r\n", fb_info->framebuffer_pitch); - serial_printf(3, "Framebuffer BPP: %u\r\n", fb_info->framebuffer_bpp); + serial_printf(3, "Framebuffer Address: 0x%x", fb_info->framebuffer_addr); + serial_printf(3, "Framebuffer Width: %u", fb_info->framebuffer_width); + serial_printf(3, "Framebuffer Height: %u", fb_info->framebuffer_height); + serial_printf(3, "Framebuffer Pitch: %u", fb_info->framebuffer_pitch); + serial_printf(3, "Framebuffer BPP: %u", fb_info->framebuffer_bpp); if (fb_info) { framebuffer = (uint32_t *)(uintptr_t) fb_info->framebuffer_addr; @@ -47,6 +47,7 @@ void kmain(multiboot2_info *mb_info) //8x16 font, not padded VGA_WIDTH = width/8; VGA_HEIGHT = height/16; + serial_printf(3, "VGA_WIDTH=%d, VGA_HEIGHT=%d", VGA_WIDTH, VGA_HEIGHT); scanline = width * (bpp/8); } diff --git a/src/libc/string.c b/src/libc/string.c index 681298d..6f079b3 100644 --- a/src/libc/string.c +++ b/src/libc/string.c @@ -74,3 +74,15 @@ char* strtok(char* str, const char* delimiter) return token_start; } + +int atoi(char* str) +{ + int result = 0; + + for (int i=0; str[i] != '\0'; i++) + { + result = result*10 + str[i] - '0'; + } + + return result; +} diff --git a/src/libc/string.h b/src/libc/string.h index b3fc1ce..130c2b6 100644 --- a/src/libc/string.h +++ b/src/libc/string.h @@ -9,5 +9,6 @@ int strlen(const char* str); int strcmp(const char* str1, const char* str2); char* strtok(char* str, const char* delimiter); +int atoi(char* str); #endif diff --git a/src/programs/conway.c b/src/programs/conway.c index 61e4c4a..70e26bc 100644 --- a/src/programs/conway.c +++ b/src/programs/conway.c @@ -13,14 +13,13 @@ void print_grid(const unsigned char grid[X][Y]) { + clear(); for (int i=0; i Date: Sat, 24 Aug 2024 21:50:20 +0200 Subject: [PATCH 2/3] Add: cowsay, splash --- docs/USERS.md | 6 +++- src/kernel/shell.c | 24 ++++++++++++-- src/libc/string.c | 17 ++++++++++ src/libc/string.h | 2 ++ src/programs/cowsay.c | 72 +++++++++++++++++++++++++++++++++++++++++ src/programs/primes.c | 17 ++++++++-- src/programs/programs.h | 2 ++ 7 files changed, 134 insertions(+), 6 deletions(-) create mode 100644 src/programs/cowsay.c diff --git a/docs/USERS.md b/docs/USERS.md index 0d503b8..a43f3bc 100644 --- a/docs/USERS.md +++ b/docs/USERS.md @@ -51,7 +51,11 @@ Options: #### `primes` -Computes prime numbers up to `PRIMES_MAX`, defined in `src/programs/primes.c`. +Computes prime numbers. + +Options: +- `` will default to `PRIMES_MAX` (a million) +- `` will compute primes up to that number #### `rainbow` diff --git a/src/kernel/shell.c b/src/kernel/shell.c index 28b8c2c..d154f1c 100644 --- a/src/kernel/shell.c +++ b/src/kernel/shell.c @@ -7,12 +7,15 @@ #include "../libc/stdio.h" #include "../libc/string.h" #include "../programs/programs.h" +#include "../libc/crypto.h" #include #define BUFFER_SIZE 256 #define MAX_COMMANDS 16 #define MAX_ARGS 64 +// Splash screen: esthetic stuff. + char* ascii_title = "\n" "----------------------------------------------\n" @@ -21,6 +24,22 @@ char* ascii_title = "----------------------------------------------\n" "\n"; +char* motd[] = +{ + "Now in 2D!", + "Supercalifragilisticexpialidocious!", + "Tylko jedno w głowie mam!", +}; +int motd_size = sizeof(motd)/sizeof(motd[0]); + +void splash() +{ // Change that seed to something RTC-related (need RTC driver) + int random = randint(global_ticks); + char* motd_pick = motd[random%motd_size]; + cowsay(motd_pick, red, black); + puts("\n"); +} + typedef void (*command_func_t)(int argc, char *argv[]); typedef struct @@ -66,8 +85,8 @@ int parse_input(char* input, char* argv[], int max_args) } void shell_install() -{ - colorputs(ascii_title, yellow, black); +{ + splash(); register_command("help", program_help); register_command("panic", program_panic); @@ -83,6 +102,7 @@ void shell_install() register_command("conway", program_conway); register_command("rot13", program_rot13); register_command("morse", program_morse); + register_command("cowsay", program_cowsay); for (;;) { diff --git a/src/libc/string.c b/src/libc/string.c index 6f079b3..91f07f1 100644 --- a/src/libc/string.c +++ b/src/libc/string.c @@ -86,3 +86,20 @@ int atoi(char* str) return result; } + +void strcat(char* dest, const char* src) +{ + while (*dest) + { + dest++; + } + + while (*src) + { + *dest = *src; + dest++; + src++; + } + + *dest = '\0'; +} diff --git a/src/libc/string.h b/src/libc/string.h index 130c2b6..496950a 100644 --- a/src/libc/string.h +++ b/src/libc/string.h @@ -10,5 +10,7 @@ int strlen(const char* str); int strcmp(const char* str1, const char* str2); char* strtok(char* str, const char* delimiter); int atoi(char* str); +void strcat(char* dest, const char* src); + #endif diff --git a/src/programs/cowsay.c b/src/programs/cowsay.c new file mode 100644 index 0000000..5b1f5c2 --- /dev/null +++ b/src/programs/cowsay.c @@ -0,0 +1,72 @@ +// Cowsay-like program +// Author: xamidev +// Licensed under the Unlicense. See the repo below. +// https://github.com/xamidev/blankos + +#include "../libc/stdio.h" +#include "../libc/string.h" + +#define MAX_MSG_LEN 128 + +const char* cow = +" \\ ^__^\n" +" \\ (oo)\\_______\n" +" (__)\\ )\\/\\\n" +" ||----w |\n" +" || ||\n"; + +void print_bubble(const char* message) +{ + int len = strlen(message); + puts(" "); + for (int i=0; i\n", message); + + puts(" "); + for (int i=0; i\n", argv[0]); + return; + } + + char message[MAX_MSG_LEN]; + message[0] = '\0'; + + for (int i=1; i #include "../libc/stdio.h" #include "../kernel/system.h" +#include "../libc/string.h" #define PRIMES_MAX 1000000 @@ -16,15 +17,25 @@ bool isPrime(int n) return true; } -void program_primes() +void program_primes(int argc, char* argv[]) { - for (long long x=0; x Date: Sat, 24 Aug 2024 22:18:21 +0200 Subject: [PATCH 3/3] Add: cool splash screen --- src/drivers/rtc.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++ src/drivers/rtc.h | 23 +++++++++++++++++ src/kernel/shell.c | 19 ++++++++++---- 3 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 src/drivers/rtc.c create mode 100644 src/drivers/rtc.h diff --git a/src/drivers/rtc.c b/src/drivers/rtc.c new file mode 100644 index 0000000..92a5beb --- /dev/null +++ b/src/drivers/rtc.c @@ -0,0 +1,64 @@ +// Real-time clock driver implementation for better PRNG +// Author: xamidev +// Licensed under the Unlicense. See the repo below. +// https://github.com/xamidev/blankos + +#include +#include "rtc.h" +#include "../kernel/io.h" +#include "../libc/stdio.h" + +uint8_t rtc_read_register(uint8_t reg) +{ + outb(0x70, reg); + return inb(0x71); +} + +uint8_t bcd_to_bin(uint8_t bcd) +{ + return ((bcd/16)*10) + (bcd%16); +} + +int rtc_is_updating() +{ + outb(0x70, 0x0A); + return (inb(0x71) & 0x80); +} + +void rtc_read_time(rtc_time_t *time) +{ + while (rtc_is_updating()); + + time->seconds = rtc_read_register(0x00); + time->minutes = rtc_read_register(0x02); + time->hours = rtc_read_register(0x04); + time->day = rtc_read_register(0x06); + time->month = rtc_read_register(0x07); + time->year = rtc_read_register(0x08); + + outb(0x70, 0x0B); + uint8_t registerB = inb(0x71); + + if (!(registerB & 0x04)) + { + time->seconds = bcd_to_bin(time->seconds); + time->minutes = bcd_to_bin(time->minutes); + time->hours = bcd_to_bin(time->hours); + time->day = bcd_to_bin(time->day); + time->month = bcd_to_bin(time->month); + time->year = bcd_to_bin(time->year); + } +} + +void print_time(const rtc_time_t *time) +{ + printf("%02d/%02d/%02d %02d:%02d:%02d\n", time->day, time->month, time->year, time->hours, time->minutes, time->seconds); +} + +long time_seed() +{ + rtc_time_t* time = {0}; + rtc_read_time(time); + + return time->day + time->month + time->year + time->hours + time->minutes + time->seconds; +} diff --git a/src/drivers/rtc.h b/src/drivers/rtc.h new file mode 100644 index 0000000..ef2bddf --- /dev/null +++ b/src/drivers/rtc.h @@ -0,0 +1,23 @@ +// Real-time clock driver implementation header for better PRNG +// Author: xamidev +// Licensed under the Unlicense. See the repo below. +// https://github.com/xamidev/blankos + +#ifndef RTC_H +#define RTC_H + +typedef struct +{ + uint8_t seconds; + uint8_t minutes; + uint8_t hours; + uint8_t day; + uint8_t month; + uint8_t year; +} rtc_time_t; + +void rtc_read_time(rtc_time_t *time); +long time_seed(); +void print_time(const rtc_time_t *time); + +#endif diff --git a/src/kernel/shell.c b/src/kernel/shell.c index d154f1c..27804ac 100644 --- a/src/kernel/shell.c +++ b/src/kernel/shell.c @@ -9,6 +9,7 @@ #include "../programs/programs.h" #include "../libc/crypto.h" #include +#include "../drivers/rtc.h" #define BUFFER_SIZE 256 #define MAX_COMMANDS 16 @@ -19,7 +20,7 @@ char* ascii_title = "\n" "----------------------------------------------\n" -"Blank OS version 0.3.71-dev\n" +"Blank OS version 0.3.84-alpha\n" "Author: @xamidev - star the repo for a cookie!\n" "----------------------------------------------\n" "\n"; @@ -28,16 +29,24 @@ char* motd[] = { "Now in 2D!", "Supercalifragilisticexpialidocious!", - "Tylko jedno w głowie mam!", + "Tylko jedno w glowie mam!", }; int motd_size = sizeof(motd)/sizeof(motd[0]); void splash() -{ // Change that seed to something RTC-related (need RTC driver) - int random = randint(global_ticks); +{ + int random = randint(time_seed()); char* motd_pick = motd[random%motd_size]; cowsay(motd_pick, red, black); - puts("\n"); + colorputs(" blankOS 0.3.84-alpha", red, black); + puts("\n"); + + + puts(" Time: "); + rtc_time_t time; + rtc_read_time(&time); + print_time(&time); + puts("\n"); } typedef void (*command_func_t)(int argc, char *argv[]); -- 2.49.1