jankos/stlio.c

101 lines
2.2 KiB
C

#include "types.h"
#include "kbd.h"
#include "stlio.h"
#include "pit.h"
extern char kbd_key;
extern u8 kbd_state;
static u8 COLOR_FG = Green;
static u8 COLOR_BG = White;
#define write_char(c) write_cell_fb(c, COLOR_FG, COLOR_BG)
// We are assuming null-terminated strings here
u32 strlen(const char* buffer) {
u32 i = 0;
char c = buffer[i];
while(c != '\0') {
i++;
c = buffer[i];
}
return i;
}
u32 write(const char* buffer, const u32 size) {
u32 i;
for(i = 0; i < size; i++) {
// cheesy but whatever
if(buffer[i] == '\n') {
frame_buffer_newline();
}
else {
write_char(buffer[i]);
}
}
return i;
}
// this is here to get around gcc's optimizations
static void __char_set(u8* dest, u8 desire) {
*dest = desire;
}
u32 read(char* buffer, u32 size) {
// try the timing thing again after this
u32 bytes = 0;
u8 state = 0;
while(bytes < size) {
// All of these calls to __char_set is to avoid gcc from optimizing things away and ruining intended behavior
// Basically gcc will tread all assignments here as one time deals so we have to cram that operation into a routine
// by itself, that way the compiler won't try to optimize and do the assignment once. instead it does the call
// every iteration
__char_set(&state, kbd_state);
if(pit_timer_ticks - kbd_time < 0x20 && state == KBD_RELEASE) {
__char_set(&(buffer[bytes]), (u8)kbd_key);
bytes++;
__char_set(&kbd_state, KBD_WAITING); // reset the kbd_state since we've now used it
putch(kbd_key);
}
pit_timer_wait(1);
}
buffer[size-1] = '\0';
return bytes;
}
void printf(const char* fmt) {
// Variadic fuller version of print on seperate branch but its nowhere near stable/ready/working
u32 size = strlen(fmt);
for(u32 i = 0; i < size;i++) {
if(fmt[i] == '\n') {
frame_buffer_newline();
}
else {
write_char(fmt[i]);
}
}
}
void printhex(u32 num) {
// Prints our hex version of whatever is given
const char _chars[] = "0123456789abcdef";
u32 idx = 0; // used as hash into the _chars mapping
// iterate over each nibble
printf("0x");
for(u32 i = 0 ;i < 8;i++) {
idx = (num << (i * 4)) & 0xf0000000;
idx = idx >> 28;
putch(_chars[idx]);
}
}
void putch(const char c) {
if(c == '\n') {
frame_buffer_newline();
}
else {
write_char(c);
}
}