From e0179b067cfe02f1f534a76cff4295a125336009 Mon Sep 17 00:00:00 2001 From: antifallobst Date: Tue, 2 May 2023 21:23:32 +0200 Subject: [PATCH] implemented a few utils --- inc/nox/math.h | 11 +++++++++ inc/nox/memory.h | 12 +++++++++ inc/nox/stdio.h | 5 ++-- inc/syscall.h | 2 +- src/math.c | 11 +++++++++ src/memory.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ src/stdio.c | 14 ++++++++--- 7 files changed, 112 insertions(+), 7 deletions(-) create mode 100644 inc/nox/math.h create mode 100644 inc/nox/memory.h create mode 100644 src/math.c create mode 100644 src/memory.c diff --git a/inc/nox/math.h b/inc/nox/math.h new file mode 100644 index 0000000..84f62a5 --- /dev/null +++ b/inc/nox/math.h @@ -0,0 +1,11 @@ +// This file is part of noxos and licensed under the MIT open source license + +#ifndef LIBNX_MATH_H +#define LIBNX_MATH_H + +#include "stdtypes.h" + +uint64_t min(uint64_t a, uint64_t b); +uint64_t max(uint64_t a, uint64_t b); + +#endif //LIBNX_MATH_H diff --git a/inc/nox/memory.h b/inc/nox/memory.h new file mode 100644 index 0000000..dbf6360 --- /dev/null +++ b/inc/nox/memory.h @@ -0,0 +1,12 @@ +// This file is part of noxos and licensed under the MIT open source license + +#ifndef LIBNX_MEMORY_H +#define LIBNX_MEMORY_H + +#include "nox/stdtypes.h" + +void memcpy (void* source, void* destination, uint32_t num); +void memset (void* destination, uint8_t data, uint32_t num); +bool memcmp (void* a, void* b, uint32_t num); + +#endif //LIBNX_MEMORY_H diff --git a/inc/nox/stdio.h b/inc/nox/stdio.h index 0c1d547..11993ef 100644 --- a/inc/nox/stdio.h +++ b/inc/nox/stdio.h @@ -15,9 +15,10 @@ enum { file_t fopen (string_t path); void fclose (file_t file); -uint32_t fread (file_t file, uint32_t offset, void* buffer, uint32_t num); -uint32_t fwrite (file_t file, uint32_t offset, void* buffer, uint32_t num); +uint64_t fread (file_t file, uint32_t offset, void* buffer, uint32_t num); +uint64_t fwrite (file_t file, uint32_t offset, void* buffer, uint32_t num); void printf (string_t format); +char getc (); #endif //NOX_LIBC_SYSCALL_H \ No newline at end of file diff --git a/inc/syscall.h b/inc/syscall.h index d880cca..babd326 100644 --- a/inc/syscall.h +++ b/inc/syscall.h @@ -40,6 +40,6 @@ typedef enum { SYSCALL_KERNEL_PANIC = 0xFF01 } syscall_E; -extern status_E syscall_perform(uint64_t id, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4); +extern uint64_t syscall_perform(uint64_t id, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4); #endif //LIBC_SYSCALL_H diff --git a/src/math.c b/src/math.c new file mode 100644 index 0000000..65c26b1 --- /dev/null +++ b/src/math.c @@ -0,0 +1,11 @@ +// This file is part of noxos and licensed under the MIT open source license + +#include "nox/math.h" + +uint64_t min(uint64_t a, uint64_t b) { + return a < b ? a : b; +} + +uint64_t max(uint64_t a, uint64_t b) { + return a > b ? a : b; +} \ No newline at end of file diff --git a/src/memory.c b/src/memory.c new file mode 100644 index 0000000..21b0353 --- /dev/null +++ b/src/memory.c @@ -0,0 +1,64 @@ +// This file is part of noxos and licensed under the MIT open source license + +#include "nox/memory.h" + +void memcpy(void* source, void* destination, uint32_t num) { + uint32_t num_64 = num / 8; + uint64_t* src_64 = (uint64_t*)source; + uint64_t* dst_64 = (uint64_t*)destination; + + for (uint32_t i = 0; i < num_64; i++) { + dst_64[i] = src_64[i]; + } + + uint32_t num_8 = num % 8; + uint8_t* src_8 = &((uint8_t*)source)[num - num_8]; + uint8_t* dst_8 = &((uint8_t*)destination)[num - num_8]; + + for (uint32_t i = 0; i < num_8; i++) { + dst_8[i] = src_8[i]; + } +} + +void memset(void* destination, uint8_t data, uint32_t num) { + uint32_t num_64 = num / 8; + uint64_t* dst_64 = (uint64_t*)destination; + uint64_t data_64 = data; + + data_64 |= (data_64 << 8) | (data_64 << 16) | (data_64 << 24) | (data_64 << 32) | (data_64 << 40) | (data_64 << 48) | (data_64 << 56); + + for (uint32_t i = 0; i < num_64; i++) { + dst_64[i] = data_64; + } + + uint32_t num_8 = num % 8; + uint8_t* dst_8 = &((uint8_t*)destination)[num - num_8]; + + for (uint32_t i = 0; i < num_8; i++) { + dst_8[i] = data; + } +} + +bool memcmp(void* a, void* b, uint32_t num) { + uint32_t num_64 = num / 8; + uint64_t* a_64 = (uint64_t*)a; + uint64_t* b_64 = (uint64_t*)b; + + for (uint32_t i = 0; i < num_64; i++) { + if (a_64[i] != b_64[i]) { + return false; + } + } + + uint32_t num_8 = num % 8; + uint8_t* a_8 = &((uint8_t*)a)[num - num_8]; + uint8_t* b_8 = &((uint8_t*)b)[num - num_8]; + + for (uint32_t i = 0; i < num_8; i++) { + if (a_8[i] != b_8[i]) { + return false; + } + } + + return true; +} \ No newline at end of file diff --git a/src/stdio.c b/src/stdio.c index 7c17540..0770642 100644 --- a/src/stdio.c +++ b/src/stdio.c @@ -13,14 +13,20 @@ void fclose(file_t file) { syscall_perform(SYSCALL_FILES_CLOSE, file, 0, 0, 0); } -uint32_t fread(file_t file, uint32_t offset, void* buffer, uint32_t num) { - syscall_perform(SYSCALL_FILES_READ, file, offset, (uint64_t)buffer, num); +uint64_t fread(file_t file, uint32_t offset, void* buffer, uint32_t num) { + return syscall_perform(SYSCALL_FILES_READ, file, offset, (uint64_t)buffer, num); } -uint32_t fwrite(file_t file, uint32_t offset, void* buffer, uint32_t num) { - syscall_perform(SYSCALL_FILES_WRITE, file, offset, (uint64_t)buffer, num); +uint64_t fwrite(file_t file, uint32_t offset, void* buffer, uint32_t num) { + return syscall_perform(SYSCALL_FILES_WRITE, file, offset, (uint64_t)buffer, num); } void printf(string_t format) { fwrite(STDOUT, 0, (void*)format, strlen(format)); +} + +char getc() { + char chr; + while (fread(STDIN, 0, &chr, 1) == 0); + return chr; } \ No newline at end of file