Syncboot/kernel/utils.c
Jenny Curle e4fcbb20b4 Implement Serial Console messaging.
Intended to be used for debugging, but it's useful.
2019-04-07 15:34:15 +01:00

104 lines
1.7 KiB
C

#include <kernel/utils.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
size_t strlen(const char* string) {
size_t size = 0;
while (string[size])
size++;
return size;
}
unsigned char inb(unsigned short port) {
unsigned char ptr;
asm volatile("inb %1, %0" : "=a" (ptr) : "dN" (port));
return ptr;
}
void outb(unsigned short port, unsigned char data) {
asm volatile("outb %1, %0" : : "dN" (port), "a" (data));
}
void memcpy(void* dest, void* src, size_t n) {
char* src_c = (char*)src;
char* dest_c = (char*)dest;
for(size_t i = 0; i < n; i++) {
dest_c[i] = src_c[i];
}
}
void memset(void* src, int chr, size_t n) {
unsigned char* ptr = src;
while(n--) {
*ptr++ = (unsigned char) chr;
}
}
void int_to_ascii(int num, char* string) {
size_t i = 0; //Counter.
//TODO: Convert this to a for-loop?
int32_t calc = 0;
bool negative = false;
if(num == 0) {
string[0] = '0';
string[1] = '\0';
return;
}
if(num < 0) {
negative = true;
num = -num;
}
while(num != 0) {
calc = num % 10;
num = num / 10;
calc += 48;
string[i] = calc;
i++;
}
if(negative) {
string[i] = '-';
i++;
}
for(size_t j = 0; j < i/2; j++) {
calc = string[j];
string[j] = string[i-j-1];
string[j-i-1] = calc;
}
}
void int_to_hex(int num, char* string) {
empty_string(string);
uint8_t i = 8;
uint8_t temp = 0;
while(i != 0) {
i--;
temp = num & 0xF;
num = num >> 4;
if(temp >= 10) {
temp += 7;
}
temp += 48;
string[i] = temp;
}
}
void empty_string(char* string) {
size_t len = strlen(string);
for(size_t i = 0; i < len + 1; i++) {
string[i] = '\0';
}
}