implemented a few utils

This commit is contained in:
antifallobst 2023-05-02 21:23:32 +02:00
parent 810e322d55
commit e0179b067c
7 changed files with 112 additions and 7 deletions

11
inc/nox/math.h Normal file
View File

@ -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

12
inc/nox/memory.h Normal file
View File

@ -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

View File

@ -15,9 +15,10 @@ enum {
file_t fopen (string_t path); file_t fopen (string_t path);
void fclose (file_t file); void fclose (file_t file);
uint32_t fread (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);
uint32_t fwrite (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); void printf (string_t format);
char getc ();
#endif //NOX_LIBC_SYSCALL_H #endif //NOX_LIBC_SYSCALL_H

View File

@ -40,6 +40,6 @@ typedef enum {
SYSCALL_KERNEL_PANIC = 0xFF01 SYSCALL_KERNEL_PANIC = 0xFF01
} syscall_E; } 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 #endif //LIBC_SYSCALL_H

11
src/math.c Normal file
View File

@ -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;
}

64
src/memory.c Normal file
View File

@ -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;
}

View File

@ -13,14 +13,20 @@ void fclose(file_t file) {
syscall_perform(SYSCALL_FILES_CLOSE, file, 0, 0, 0); syscall_perform(SYSCALL_FILES_CLOSE, file, 0, 0, 0);
} }
uint32_t fread(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) {
syscall_perform(SYSCALL_FILES_READ, file, offset, (uint64_t)buffer, 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) { uint64_t fwrite(file_t file, uint32_t offset, void* buffer, uint32_t num) {
syscall_perform(SYSCALL_FILES_WRITE, file, offset, (uint64_t)buffer, num); return syscall_perform(SYSCALL_FILES_WRITE, file, offset, (uint64_t)buffer, num);
} }
void printf(string_t format) { void printf(string_t format) {
fwrite(STDOUT, 0, (void*)format, strlen(format)); fwrite(STDOUT, 0, (void*)format, strlen(format));
}
char getc() {
char chr;
while (fread(STDIN, 0, &chr, 1) == 0);
return chr;
} }