From 8d3d090772ee0cd62cc3ad11239a5c373411ffd5 Mon Sep 17 00:00:00 2001 From: antifallobst Date: Wed, 25 Jan 2023 01:42:04 +0100 Subject: [PATCH] implemented a few utils --- kernel/inc/utils/math.h | 22 +++++++++++++++++++ kernel/inc/utils/memory.h | 25 +++++++++++++++++++++ kernel/inc/utils/string.h | 26 ++++++++++++++++++++++ kernel/src/kmain.c | 2 +- kernel/src/utils/memory.c | 46 +++++++++++++++++++++++++++++++++++++++ kernel/src/utils/string.c | 38 ++++++++++++++++++++++++++++++++ 6 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 kernel/inc/utils/math.h create mode 100644 kernel/inc/utils/memory.h create mode 100644 kernel/inc/utils/string.h create mode 100644 kernel/src/utils/memory.c create mode 100644 kernel/src/utils/string.c diff --git a/kernel/inc/utils/math.h b/kernel/inc/utils/math.h new file mode 100644 index 0000000..d147b2f --- /dev/null +++ b/kernel/inc/utils/math.h @@ -0,0 +1,22 @@ +/* Copyright (C) Antifallobst + * + * NyxOS is free software: + * you can redistribute it and/or modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * NyxOS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. + * If not, see . + */ + +#ifndef NYXOS_MATH_H +#define NYXOS_MATH_H + +#define max(a, b) (a > b ? a : b) +#define min(a, b) (a > b ? b : a) + +#endif //NYXOS_MATH_H diff --git a/kernel/inc/utils/memory.h b/kernel/inc/utils/memory.h new file mode 100644 index 0000000..ee90a97 --- /dev/null +++ b/kernel/inc/utils/memory.h @@ -0,0 +1,25 @@ +/* Copyright (C) Antifallobst + * + * NyxOS is free software: + * you can redistribute it and/or modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * NyxOS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. + * If not, see . + */ + +#ifndef NYXOS_MEMORY_H +#define NYXOS_MEMORY_H + +#include "stdtypes.h" + +void memory_copy (void* source, void* destination, uint32_t num); +void memory_set (void* destination, uint8_t data, uint32_t num); +bool memory_compare (void* a, void* b, uint32_t num); + +#endif //NYXOS_MEMORY_H diff --git a/kernel/inc/utils/string.h b/kernel/inc/utils/string.h new file mode 100644 index 0000000..cc28bb4 --- /dev/null +++ b/kernel/inc/utils/string.h @@ -0,0 +1,26 @@ +/* Copyright (C) Antifallobst + * + * NyxOS is free software: + * you can redistribute it and/or modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * NyxOS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. + * If not, see . + */ + +#ifndef NYXOS_STRING_H +#define NYXOS_STRING_H + +#include "stdtypes.h" + +typedef const char* string_t; + +uint32_t string_length (string_t str); +bool string_compare(string_t a, string_t b); + +#endif //NYXOS_STRING_H diff --git a/kernel/src/kmain.c b/kernel/src/kmain.c index 93a3f4c..df6becb 100644 --- a/kernel/src/kmain.c +++ b/kernel/src/kmain.c @@ -29,7 +29,7 @@ void _start() { struct limine_terminal* terminal = terminal_request.response->terminals[0]; - terminal_request.response->write(terminal, "Hello Limine", 12); + terminal_request.response->write(terminal, "Booting NyxOS", 13); while(true) asm("hlt"); } diff --git a/kernel/src/utils/memory.c b/kernel/src/utils/memory.c new file mode 100644 index 0000000..db54afd --- /dev/null +++ b/kernel/src/utils/memory.c @@ -0,0 +1,46 @@ +/* Copyright (C) Antifallobst + * + * NyxOS is free software: + * you can redistribute it and/or modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * NyxOS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. + * If not, see . + */ + +#include "utils/memory.h" + +void memory_copy (void* source, void* destination, uint32_t num) { + uint8_t* src_8 = (uint8_t*)source; + uint8_t* dst_8 = (uint8_t*)destination; + + for (int i = 0; i < num; i++) { + dst_8[i] = src_8[i]; + } +} + +void memory_set (void* destination, uint8_t data, uint32_t num) { + uint8_t* dst_8 = (uint8_t*)destination; + + for (int i = 0; i < num; i++) { + dst_8[i] = data; + } +} + +bool memory_compare (void* a, void* b, uint32_t num) { + uint8_t* a_8 = (uint8_t*)a; + uint8_t* b_8 = (uint8_t*)b; + + for (int i = 0; i < num; i++) { + if (a_8[i] != b_8[i]) { + return false; + } + } + + return true; +} \ No newline at end of file diff --git a/kernel/src/utils/string.c b/kernel/src/utils/string.c new file mode 100644 index 0000000..41ec19c --- /dev/null +++ b/kernel/src/utils/string.c @@ -0,0 +1,38 @@ +/* Copyright (C) Antifallobst + * + * NyxOS is free software: + * you can redistribute it and/or modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * NyxOS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. + * If not, see . + */ + +#include "utils/string.h" +#include "utils/memory.h" + +uint32_t string_length (string_t str) { + uint32_t n = 0; + char* c = (char*)str; + + while(*c != '\0') { + c++; + n++; + } + + return n; +} + +bool string_compare(string_t a, string_t b) { + uint32_t a_len = string_length(a); + + if (a_len != string_length(b)) { + return false; + } + return memory_compare(a, b, a_len); +} \ No newline at end of file