Include file restructure!

- include/ now has subdirs
- standard C headers will be in 'include/c'. Any program can include these
- all other header shall only be used by the kernel
This commit is contained in:
2024-05-07 21:01:50 +02:00
parent 41e0b93f52
commit 7826b9b7d4
20 changed files with 33 additions and 83 deletions
+16
View File
@@ -0,0 +1,16 @@
#ifndef MATH_H
#define MATH_H
// Constants
#define PI 3.141592653589 // Should be good enough
#define E 2.718281828459
// Macros
#define abs(x) ( (x) > 0 ? (x) : -(x) )
#define sign(x) ( (x) == 0 ? 0 : ((x) < 0 ? -1 : 1 ) )
#define floor(x) ( (int)(x) ) // C floors floats when converting to ints
#define round(x) ( (int)((x) + 0.5) ) // Normal rounding
#define ceil(x) ( (int)((x) + 1) ) // Always rounds up
#endif
+8
View File
@@ -0,0 +1,8 @@
#ifndef BOOL_H
#define BOOL_H
#define bool _Bool
#define true 1
#define false 0
#endif
+21
View File
@@ -0,0 +1,21 @@
#ifndef STDINT_H
#define STDINT_H
#define NULL ((void*)0)
// Signed ints
typedef signed char int8_t;
typedef signed short int16_t;
typedef signed int int32_t;
typedef signed long long int64_t;
// Unsigned ints
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
// Other
typedef unsigned int size_t;
#endif
+106
View File
@@ -0,0 +1,106 @@
#ifndef STRING_H
#define STRING_H
#include <stdint.h>
// Source: http://www.strudel.org.uk/itoa/
char* itoa(uint32_t val, uint8_t base) {
static char buf[32] = { 0 };
if (base == 0 || base > 16) {
// TODO: Add errors!!
return NULL;
}
if (val == 0) {
buf[0] = '0';
return buf;
}
int i = 30;
for (; val && i; i--, val /= base) {
buf[i] = "0123456789abcdef"[val % base];
}
return &buf[i + 1];
}
uint32_t atoi(const char* str) {
uint32_t res = 0;
// TODO: Add error checking
while (*str >= '0' && *str <= '9') {
res = res * 10 + (*str++ - '0');
}
return res;
}
int isSpace(char c) {
if (c == ' ' || c == '\t' || c == '\n') return 1;
else return 0;
}
// Some implementations taken from https://en.wikibooks.org/wiki/C_Programming/String_manipulation
char* strcpy(char* str1, const char* str2) {
char* dst = str1;
const char* src = str2;
while ((*dst++ = *src++) != '\0');
return str1;
}
char* strcat(char* str1, const char* str2) {
char* s = str1;
while (*s != '\0') { s++; }
strcpy(s, str2);
return str1;
}
char* strchr(const char* str, char c) {
const char* s = str;
while (*s != '\0' && *s != c) { s++; }
return (*s == c) ? (char*)s : NULL;
}
int strcmp(const char* str1, const char* str2) {
while (*str1 != '\0' && *str1 == *str2) {
str1++;
str2++;
}
uint8_t c1 = (*(uint8_t*)str1);
uint8_t c2 = (*(uint8_t*)str2);
return ((c1 < c2) ? -1 : (c1 > c2));
}
uint32_t strlen(const char* str) {
const char* p = str;
while (*++p != '\0');
return (uint32_t)(p - str);
}
// Memory functions
void* memcpy(void* src, void* dst, uint32_t length) {
for (uint32_t i = 0; i < length; i++) {
*((uint8_t*)dst + i) = *((uint8_t*)src + i);
}
return dst;
}
void* memcpy32(void* src, void* dst, uint32_t length) {
for (uint32_t i = 0; i * 4 < length; i++) {
*((uint32_t*)dst + i) = *((uint32_t*)src + i);
}
return dst;
}
void* memset(void* dst, uint8_t value, uint32_t length) {
for (uint32_t i = 0; i < length; i++) {
*(uint8_t*)(dst + i) = value;
}
return dst;
}
#endif
+10
View File
@@ -0,0 +1,10 @@
#ifndef TIME_H
#define TIME_H
#include <stdint.h>
void sleep(uint32_t ms) {
__asm__ volatile ("int $0x80" : : "a"(0), "b"(ms));
}
#endif