Added some functions, did some reorganizing.

kttools.h/c is a lib for converting types (only currently contains
i_to_str).

Also added a printf for i/o.
This commit is contained in:
lordtet 2025-06-08 16:18:06 -04:00
parent e8a6c514ad
commit 8734cd4da6
6 changed files with 72 additions and 72 deletions

View file

@ -1,8 +1,9 @@
#ifndef KERNIO_H
#define KERNIO_H
#define VGA_GRID_COLS 79
#ifndef KIO_H
#define KIO_H
#define VGA_GRID_COLS 80
#define VGA_GRID_ROWS 25
#include <stdint.h>
#include <stdarg.h>
/*
* CONSTANTS AND VARIABLES
@ -29,7 +30,8 @@ void vga_putc(char c);
void vga_set_attributes(uint8_t attributes);
void vga_print(const char* out);
void vga_println(const char* out);
void vga_printhex(uint32_t out);
void vga_prindec(uint32_t out);
void vga_printf(const char* fmt, ...);
#endif

7
include/kttools.h Normal file
View file

@ -0,0 +1,7 @@
#ifndef KTTOOLS_H
#define KTTOOLS_H
#include <stdint.h>
void i_to_str(uint32_t num, char* buf, int size, int radix);
#endif

View file

@ -1,57 +0,0 @@
#include "kernio.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;
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");
}

View file

@ -1,4 +1,5 @@
#include "kio.h"
#include "kttools.h"
#include <stddef.h>
/*
@ -34,7 +35,8 @@ void vga_putc(char c)
//Check for some freaky escape character first
if(c == '\n') {
cursor_col = 0;
cursor_row = (cursor_row + 1) % 25;
//mod implements wraparound
cursor_row = (cursor_row + 1) % (VGA_GRID_ROWS-1);
return;
}
//Calculate where in the vga buffer to put the character
@ -49,8 +51,7 @@ void vga_set_attributes(uint8_t attributes) {
}
void vga_print(const char* out)
{
void vga_print(const char* out) {
for (int i = 0; out[i] != '\0'; i++)
vga_putc(out[i]);
}
@ -59,3 +60,41 @@ void vga_println(const char* out) {
vga_print(out);
vga_print("\n");
}
void vga_printhex(uint32_t out) {
char buff[9];
i_to_str(out, buff, 9, 0x10);
vga_print(buff);
}
void vga_printdec(uint32_t out) {
char buff[11];
i_to_str(out, buff, 11, 10);
vga_print(buff);
}
void vga_printf(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
while(*fmt) {
if(*fmt == '%') {
fmt++;
switch(*fmt) {
case 'X':
vga_print("0x");
case 'x':
vga_printhex(va_arg(args,uint32_t));
break;
case 'd':
vga_printdec(va_arg(args,uint32_t));
break;
}
} else {
vga_putc(*fmt);
}
fmt++;
}
}

View file

@ -28,16 +28,16 @@
* @return No return value
*
*/
void i_to_str(uint32_t num, char* buf, int size) {
void i_to_str(uint32_t num, char* buf, int size, int radix) {
//null terminate the string
buf[--size] = '\0';
while(size > 0 && (num) != 0){
int isolated_num = num % 0x10;
int isolated_num = num % radix;
if(isolated_num > 9){
isolated_num+=7;
}
buf[--size] = '0' + isolated_num;
num/=0x10;
num/=radix;
}
//now shift the whole thing to the left

View file

@ -20,10 +20,19 @@ void kern_main(uint32_t multiboot_magic, multiboot_info_t* multiboot_info)
//wipe the screen
vga_clear();
//IT IS TIME. TO PRINT.
char lol[9];
i_to_str(multiboot_info->mem_upper, lol, 9);
//We're going to use this buffer as our 8char hex representation for reading mem
vga_printf("Entry eax:%X\n", multiboot_magic);
if(multiboot_magic != 0x2BADB002) {
vga_println("Bootloader not multiboot1 compliant! Needed for mmap, etc. Can't work without it, kthxbye!");
return;
} else {
vga_println("Multiboot detected! Continuing...");
}
vga_printf("MEM_LOWER:%X\n", multiboot_info->mem_lower);
vga_printf("MEM_UPPER:%X\n", multiboot_info->mem_upper);
vga_println(lol);
}