kernel tools split into kio (for i/o) and kttools (kernel type tools).

in general, kernel libraries will be marked with "k" in the beginning.
This commit is contained in:
lordtet 2025-06-08 01:27:41 -04:00
parent f39627ec51
commit e8a6c514ad
4 changed files with 130 additions and 30 deletions

61
src/kio.c Normal file
View file

@ -0,0 +1,61 @@
#include "kio.h"
#include <stddef.h>
/*
* VARS
*/
int cursor_col = 0;
int cursor_row = 0;
volatile uint16_t* const vga_buffer = (uint16_t*)0xB8000;
uint16_t vga_attributes = 0x0F00;
/*
* FUNCTIONS
*/
void vga_clear() {
for (int col = 0; col < VGA_GRID_COLS; col ++) {
for (int row = 0; row < VGA_GRID_ROWS; row ++) {
//works out to iterating every cell
const size_t index = (VGA_GRID_COLS * row) + col;
//vga buffer looks something like xxxxyyyyzzzzzzzz
//x=bg color
//y=fg color
//c=character to use
//Therefore, to write, we just take our color data and tack on the character to the end.
vga_buffer[index] = vga_attributes | ' '; //blank out
}
}
}
void vga_putc(char c)
{
//Check for some freaky escape character first
if(c == '\n') {
cursor_col = 0;
cursor_row = (cursor_row + 1) % 25;
return;
}
//Calculate where in the vga buffer to put the character
const size_t index = (VGA_GRID_COLS * cursor_row) + cursor_col;
//VGA buffer cell consists of the first half attributes, second half character
vga_buffer[index] = vga_attributes | c;
cursor_col++;
}
void vga_set_attributes(uint8_t attributes) {
vga_attributes = ((uint16_t)attributes) << 8;
}
void vga_print(const char* out)
{
for (int i = 0; out[i] != '\0'; i++)
vga_putc(out[i]);
}
void vga_println(const char* out) {
vga_print(out);
vga_print("\n");
}

52
src/kttools.c Normal file
View file

@ -0,0 +1,52 @@
#include "kttools.h"
#include <stdint.h>
/*
* VARS
*/
//Placeholder.
/*
* FUNCTIONS
*/
//convert digits from int num to buffer buf. in hex. This is 32 bit only for now!
//WARN: integer is written from right to left into the buffer, and will halt if the buffer is too small.
/**
* @brief Convert a hex int to string, up to 32 bits.
*
* Converts int "num" into a null-terminated string, placed into buffer "buf".
* Evaluates from right to left, so left hand digits will be cut off if the buffer is too small
* Should work for non 32-bit integers actually, but is uint32_t for now.
*
* @param num: Value to convert to string
* @param buf: Memory to place the string into
* @param size: Size of the memory buffer
*
* @return No return value
*
*/
void i_to_str(uint32_t num, char* buf, int size) {
//null terminate the string
buf[--size] = '\0';
while(size > 0 && (num) != 0){
int isolated_num = num % 0x10;
if(isolated_num > 9){
isolated_num+=7;
}
buf[--size] = '0' + isolated_num;
num/=0x10;
}
//now shift the whole thing to the left
if(size != 0) {
while(buf[size]!='\0') {
*buf = buf[size];
buf++;
}
*buf = '\0';
}
}

View file

@ -1,43 +1,29 @@
//Our own code, at this point...
//#include <stddef.h>
#include <stdint.h>
#include "kernio.h"
//convert digits from int num to buffer buf. in hex. This is 32 bit only for now!
//WARN: integer is written from right to left into the buffer, and will halt if the buffer is too small.
void i_to_str(uint32_t num, char* buf, int size) {
//null terminate the string
buf[--size] = '\0';
while(size > 0 && (num) != 0){
int isolated_num = num % 0x10;
if(isolated_num > 9){
isolated_num+=7;
}
buf[--size] = '0' + isolated_num;
num/=0x10;
}
//now shift the whole thing to the left
if(size != 0) {
while(buf[size]!='\0') {
*buf = buf[size];
buf++;
}
*buf = '\0';
}
}
#include "kio.h"
#include "kttools.h"
typedef struct {
uint32_t flags;
uint32_t mem_lower;
uint32_t mem_upper;
uint32_t boot_device;
uint32_t cmdline;
uint32_t mods_count;
uint32_t mods_addr;
} multiboot_info_t;
//finally, main.
void kern_main()
void kern_main(uint32_t multiboot_magic, multiboot_info_t* multiboot_info)
{
//wipe the screen
vga_clear();
//IT IS TIME. TO PRINT.
char lol[5];
i_to_str(0xCAFEBABE, lol, 5);
char lol[9];
i_to_str(multiboot_info->mem_upper, lol, 9);
vga_println(lol);
}

View file

@ -36,7 +36,8 @@
start:
//Lets set up the stack. Stack grows downward on x86. We did the work earlier of defining where the top of the stack is, so just tell esp.
mov $stack_top, %esp //set the stack pointer
pushl %ebx
pushl %eax
//To C-land!
call kern_main