68 lines
1.9 KiB
C
68 lines
1.9 KiB
C
|
|
// Reworked sample code from OSDev! Stripped down some of the extra stuff to leave as an exercise.
|
||
|
|
//Headers provided by GCC :). No need for libs! We don't even have those yet.
|
||
|
|
#include <stddef.h>
|
||
|
|
#include <stdint.h>
|
||
|
|
|
||
|
|
|
||
|
|
//VGA buffer is the easiest way to write to the screen at this stage. it's just a massive array with two datapoints: what color, and what character?
|
||
|
|
//array starts at the absolute address 0xB8000
|
||
|
|
volatile uint16_t* vga_buffer = (uint16_t*)0xB8000;
|
||
|
|
//the VGA buffer is 80x25 to represent the screen.
|
||
|
|
const int GRID_COLS = 80;
|
||
|
|
const int GRID_ROWS = 25;
|
||
|
|
|
||
|
|
|
||
|
|
//grid is top left origin. This is our cursor!
|
||
|
|
int cursor_col = 0;
|
||
|
|
int cursor_row = 0;
|
||
|
|
uint16_t term_color = 0x0F00; // Black background, White foreground
|
||
|
|
|
||
|
|
|
||
|
|
//wipe the screen
|
||
|
|
void term_clear() {
|
||
|
|
for (int col = 0; col < GRID_COLS; col ++) {
|
||
|
|
for (int row = 0; row < GRID_ROWS; row ++) {
|
||
|
|
//works out to iterating every cell
|
||
|
|
const size_t index = (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] = term_color | ' '; //blank out
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
//
|
||
|
|
void term_printc(char c)
|
||
|
|
{
|
||
|
|
const size_t index = (GRID_COLS * cursor_row) + cursor_col; //where am i puttin it
|
||
|
|
vga_buffer[index] = term_color | c; //put it there by putting color+char into that spot in the array
|
||
|
|
cursor_col++; //next time put it in the next spot.
|
||
|
|
}
|
||
|
|
|
||
|
|
//print a string!
|
||
|
|
void term_println(const char* out)
|
||
|
|
{
|
||
|
|
for (int i = 0; out[i] != '\0'; i++)
|
||
|
|
term_printc(out[i]);
|
||
|
|
//go to the next line for a println func.
|
||
|
|
cursor_col = 0;
|
||
|
|
cursor_row++;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
//finally, main.
|
||
|
|
void kern_main()
|
||
|
|
{
|
||
|
|
|
||
|
|
//wipe the screen
|
||
|
|
term_clear();
|
||
|
|
|
||
|
|
//IT IS TIME. TO PRINT.
|
||
|
|
term_println("hello my chungus world");
|
||
|
|
term_println(":100:");
|
||
|
|
}
|
||
|
|
|