Added font and primitive logging capacity

This commit is contained in:
Hopeless Tyromancy 2026-02-05 09:47:16 -05:00
parent 0d436e5255
commit 4b81def864
17 changed files with 362 additions and 43 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
obj/
bin/

BIN
bin/os

Binary file not shown.

3
bochs.txt Normal file
View File

@ -0,0 +1,3 @@
vga: extension=vgahw
display_library: x, options="gui_debug"

BIN
image.hdd

Binary file not shown.

View File

@ -1 +1,2 @@
obj/src/int.c.o: src/int.c obj/src/int.c.o: src/int.c src/common.h
src/common.h:

Binary file not shown.

View File

@ -1,2 +1,5 @@
obj/src/main.c.o: src/main.c src/../include/limine.h obj/src/main.c.o: src/main.c src/../include/limine.h src/common.h \
src/print.h
src/../include/limine.h: src/../include/limine.h:
src/common.h:
src/print.h:

Binary file not shown.

52
src/acpi.c Normal file
View File

@ -0,0 +1,52 @@
#include "common.h"
// root system description pointer, points
pstruct acpi_rsdp_header {
u8 signature[8];
u8 checksum;
u8 OEMID[6];
u8 revision;
u32 addr32_depr;
// v2
u32 len;
u64 addr;
u8 extended_checksum;
u8 resv[3];
};
#define RSDT_SIGNATURE "RSDT"
#define XSDT_SIGNATURE "XSDT"
struct acpi_sdt_header {
u8 signature[4];
u32 len;
u8 revision;
u8 checksum;
u8 OEMID[6];
u8 OEM_table_id[8];
u32 OEM_revision;
u32 creator_id;
u32 creator_revision;
};
struct apci_rsdt {
struct acpi_sdt_header header;
u32 addrs[];
};
struct xsdt {
struct acpi_rsdp_header header;
u64 addrs[];
};
bool rsdp_validate(u8* data, size_t len) {
u32 sum_to_zero = 0;
for(size_t i = 0; i < len; i++) {
sum_to_zero += data[i];
}
return (sum_to_zero & 0xFF) == 0;
}

17
src/common.h Normal file
View File

@ -0,0 +1,17 @@
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#define pstruct struct __attribute__((packed))
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;

12
src/etc.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#include "common.h"
void* memcpy(void* restrict dst, const void* src, size_t n);
void* memset(void* s, int c, size_t n);
void* memmove(void* dst, const void* src, size_t n);
int memcmp(const void* s1, const void* s2, size_t n);
static int isprint(int ch) {
return (u8)(ch-' ') <= ('~'-' ');
}

124
src/font.h Normal file
View File

@ -0,0 +1,124 @@
#pragma once
#include "common.h"
/* credit due to blit-fonts
ISC License
Copyright (c) 2018 Andrew Reece
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#define FONT_WIDTH 5
#define FONT_HEIGHT 6
static u32 font[] = {
0x0,
0x8021084,
0x294a,
0x15f52bea,
0x8fa38be,
0x33a22e60,
0x2e94d8a2,
0x1084,
0x10421088,
0x4421082,
0xa23880,
0x471000,
0x4420000,
0x70000,
0xc600000,
0x2222200,
0x1d3ad72e,
0x3e4214c4,
0x3e22222e,
0x1d18320f,
0x210fc888,
0x1d183c3f,
0x1d17844c,
0x222221f,
0x1d18ba2e,
0x210f463e,
0xc6018c0,
0x4401000,
0x10411100,
0xe03800,
0x4441040,
0x802322e,
0x3c1ef62e,
0x231fc544,
0x1f18be2f,
0x3c10862e,
0x1f18c62f,
0x3e10bc3f,
0x210bc3f,
0x1d1c843e,
0x2318fe31,
0x3e42109f,
0xc94211f,
0x23149d31,
0x3e108421,
0x231ad6bb,
0x239cd671,
0x1d18c62e,
0x217c62f,
0x30eac62e,
0x2297c62f,
0x1d141a2e,
0x842109f,
0x1d18c631,
0x8454631,
0x375ad631,
0x22a21151,
0x8421151,
0x3e22221f,
0x1842108c,
0x20820820,
0xc421086,
0x4544,
0x3e000000,
0x82,
0x1c97b000,
0xe949c21,
0x1c10b800,
0x1c94b908,
0x1c549800,
0x2211c4c,
0xe87252e,
0x12949c21,
0xc210040,
0x6210802,
0x12519521,
0xc210842,
0x235aac00,
0x12949c00,
0xc949800,
0x213a526,
0x3087252e,
0x2149800,
0xe837000,
0xc213c42,
0xe94a400,
0x464a400,
0x155ac400,
0x36426c00,
0xe872529,
0x1e223c00,
0x1843188c,
0x8421084,
0xc463086,
0x6d800,
0x3fffffff,
};

View File

@ -1,15 +1,15 @@
#include <stdint.h> #include <stdint.h>
#define pstruct struct __attribute__((packed)) #include "common.h"
pstruct int_desc { pstruct int_desc {
uint16_t addr_low; u16 addr_low;
uint16_t selector; u16 selector;
uint8_t ist; u8 ist;
uint8_t flags; u8 flags;
uint16_t addr_mid; u16 addr_mid;
uint32_t addr_high; u32 addr_high;
uint32_t resv; u32 resv;
}; };
enum INT_DESC_FLAGS { enum INT_DESC_FLAGS {
@ -24,9 +24,9 @@ enum INT_DESC_FLAGS {
struct int_desc idt[256]; struct int_desc idt[256];
void set_idt(uint8_t vect, void* handler, uint8_t dpl) { void set_idt(u8 vect, void* handler, u8 dpl) {
struct int_desc* ent = &idt[vect]; struct int_desc* ent = &idt[vect];
uint64_t addr = (uint64_t) handler; u64 addr = (u64) handler;
ent->addr_low = addr & 0xFFFF; ent->addr_low = addr & 0xFFFF;
ent->addr_mid = (addr >> 16) & 0xFFFF; ent->addr_mid = (addr >> 16) & 0xFFFF;
ent->addr_mid = addr >> 32; ent->addr_mid = addr >> 32;
@ -37,8 +37,8 @@ void set_idt(uint8_t vect, void* handler, uint8_t dpl) {
} }
pstruct idtr { pstruct idtr {
uint16_t limit; u16 limit;
uint64_t base; u64 base;
}; };
#define IDTR_LIMIT (sizeof(struct int_desc)*256 - 1) #define IDTR_LIMIT (sizeof(struct int_desc)*256 - 1)
@ -46,25 +46,25 @@ pstruct idtr {
void load_idt(void* addr) { void load_idt(void* addr) {
struct idtr ir = (struct idtr) { struct idtr ir = (struct idtr) {
.limit = IDTR_LIMIT, .limit = IDTR_LIMIT,
.base = (uint64_t) addr, .base = (u64) addr,
}; };
asm volatile("lidt %0" :: "m"(ir)); asm volatile("lidt %0" :: "m"(ir));
} }
extern void draw(uint8_t vect); extern void draw(u8 vect);
struct cpu_ctx { struct cpu_ctx {
uint64_t r15, r14, r13, r12, r11, r10, r9, r8, rdi, rsi, rdx, rcx, rbx, rax; u64 r15, r14, r13, r12, r11, r10, r9, r8, rdi, rsi, rdx, rcx, rbx, rax;
uint64_t vect; u64 vect;
uint64_t err; u64 err;
uint64_t iret_rip; u64 iret_rip;
uint64_t iret_cs; u64 iret_cs;
uint64_t iret_flags; u64 iret_flags;
uint64_t iret_rsp; u64 iret_rsp;
uint64_t iret_ss; u64 iret_ss;
}; };
struct cpu_ctx* interrupt_dispatch(struct cpu_ctx* ctx) { struct cpu_ctx* interrupt_dispatch(struct cpu_ctx* ctx) {

View File

@ -1,12 +1,16 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include <stdbool.h> #include <stdbool.h>
#include "../include/limine.h"
// credit owed to https://wiki.osdev.org/Limine_Bare_Bones
#include "../include/limine.h"
#include "common.h"
#include "print.h"
// credit owed to https://wiki.osdev.org/Limine_Bare_Bones for the limine setup code
__attribute__((used, section(".limine_requests"))) __attribute__((used, section(".limine_requests")))
static volatile uint64_t limine_base_revision[] = LIMINE_BASE_REVISION(4); static volatile u64 limine_base_revision[] = LIMINE_BASE_REVISION(4);
__attribute__((used, section(".limine_requests"))) __attribute__((used, section(".limine_requests")))
static volatile struct limine_framebuffer_request fb_req = { static volatile struct limine_framebuffer_request fb_req = {
@ -15,14 +19,14 @@ static volatile struct limine_framebuffer_request fb_req = {
}; };
__attribute__((used, section(".limine_requests_start"))) __attribute__((used, section(".limine_requests_start")))
static volatile uint64_t limine_requests_start_marker[] = LIMINE_REQUESTS_START_MARKER; static volatile u64 limine_requests_start_marker[] = LIMINE_REQUESTS_START_MARKER;
__attribute__((used, section(".limine_requests_end"))) __attribute__((used, section(".limine_requests_end")))
static volatile uint64_t limine_requests_end_marker[] = LIMINE_REQUESTS_END_MARKER; static volatile u64 limine_requests_end_marker[] = LIMINE_REQUESTS_END_MARKER;
void* memcpy(void* restrict dst, const void* src, size_t n) { void* memcpy(void* restrict dst, const void* src, size_t n) {
uint8_t* restrict pdst = (uint8_t* restrict)dst; u8* restrict pdst = (u8* restrict)dst;
const uint8_t* restrict psrc = (const uint8_t* restrict)src; const u8* restrict psrc = (const u8* restrict)src;
for(size_t i = 0; i < n; i++) { for(size_t i = 0; i < n; i++) {
pdst[i] = psrc[i]; pdst[i] = psrc[i];
@ -32,18 +36,18 @@ void* memcpy(void* restrict dst, const void* src, size_t n) {
} }
void* memset(void* s, int c, size_t n) { void* memset(void* s, int c, size_t n) {
uint8_t* p = (uint8_t*) s; u8* p = (u8*) s;
for(size_t i = 0; i < n; i++) { for(size_t i = 0; i < n; i++) {
p[i] = (uint8_t)c; p[i] = (u8)c;
} }
return s; return s;
} }
void* memmove(void* dst, const void* src, size_t n) { void* memmove(void* dst, const void* src, size_t n) {
uint8_t* restrict pdst = (uint8_t* restrict)dst; u8* restrict pdst = (u8* restrict)dst;
const uint8_t* restrict psrc = (const uint8_t* restrict)src; const u8* restrict psrc = (const u8* restrict)src;
if(src > dst) { if(src > dst) {
for(size_t i = 0; i < n; i++) { for(size_t i = 0; i < n; i++) {
@ -59,8 +63,8 @@ void* memmove(void* dst, const void* src, size_t n) {
} }
int memcmp(const void* s1, const void* s2, size_t n) { int memcmp(const void* s1, const void* s2, size_t n) {
const uint8_t* p1 = (const uint8_t* )s1; const u8* p1 = (const u8* )s1;
const uint8_t* p2 = (const uint8_t* )s2; const u8* p2 = (const u8* )s2;
for(size_t i = 0; i < n; i++) { for(size_t i = 0; i < n; i++) {
if(p1[i] != p2[i]) { if(p1[i] != p2[i]) {
@ -80,10 +84,10 @@ static void hang() {
struct limine_framebuffer* fb; struct limine_framebuffer* fb;
void draw(uint8_t vect) { void draw(u8 vect) {
for(size_t y = 0; y < fb->height; y++) { for(size_t y = 0; y < fb->height; y++) {
for(size_t x = 0; x < fb->width; x++) { for(size_t x = 0; x < fb->width; x++) {
volatile uint32_t* fb_ptr = fb->address; volatile u32* fb_ptr = fb->address;
fb_ptr[y * fb->width + x] = ( fb_ptr[y * fb->width + x] = (
(x + y) * 0x00010000 | (x + y) * 0x00010000 |
(x ^ y) * 0x00000100 | (x ^ y) * 0x00000100 |
@ -108,10 +112,9 @@ void kmain() {
init_idt(); init_idt();
for(size_t i = 0; ~i & (1<<30); i++) { print_init(fb);
draw(i&0xFF);
}
print("TEST THIS\nMessage for me please Mr Kernel");
hang(); hang();

87
src/print.c Normal file
View File

@ -0,0 +1,87 @@
#include "etc.h"
#include "font.h"
#include "../include/limine.h"
u32 bg = 0x00000000;
u32 fg = 0xFFFFFFFF;
static size_t row;
static size_t col;
static size_t term_width, term_height;
static size_t fb_width, fb_height;
static volatile u32* fb_scr;
#define TAB_STOP 4
// buffer at each edge for readability
#define TERM_WIDTH (FONT_WIDTH+2)
#define TERM_HEIGHT (FONT_HEIGHT+2)
void print_init(struct limine_framebuffer* fb) {
term_width = fb->width / TERM_WIDTH;
term_height = fb->height / TERM_HEIGHT;
fb_width = fb->width;
fb_height = fb->height;
fb_scr = fb->address;
}
static void scrollback() {
memmove((void*)fb_scr, (void*)(fb_scr + fb_width*TERM_HEIGHT), fb_width*(fb_height-1)*sizeof(u32));
}
void put(int ch) {
ch = (unsigned char)ch;
if (!isprint(ch)) {
if (ch == '\n') {
col = 0;
row++;
} else if (ch == '\t') {
size_t target = (col + TAB_STOP) & ~(TAB_STOP - 1);
if (target >= term_width) {
col = 0;
row++;
} else {
while (col < target)
put(' ');
}
}
return;
}
if (col >= term_width) {
col = 0;
row++;
}
if (row >= term_height) {
scrollback();
row = term_height - 1;
}
size_t idx = ch - ' ';
u32 glyph = font[idx];
size_t px = col * TERM_WIDTH;
size_t py = row * TERM_HEIGHT;
for (size_t y = 0; y < TERM_HEIGHT; y++) {
for (size_t x = 0; x < TERM_WIDTH; x++) {
size_t off = (py + y) * fb_width + (px + x);
bool padding = (y == 0 || y == TERM_HEIGHT-1 || x == 0 || x == TERM_WIDTH-1);
fb_scr[off] = ((~glyph & 1) | padding )? bg : (x^y * off + y*term_width);
glyph >>= !padding;
}
}
col++;
}
void print(const char* msg) {
while(*msg) {
put(*(msg++));
}
}

10
src/print.h Normal file
View File

@ -0,0 +1,10 @@
#pragma once
struct limine_framebuffer;
void put(int ch);
void print(const char* msg);
void print_init(struct limine_framebuffer* fb);

5
src/rep.pl Normal file
View File

@ -0,0 +1,5 @@
while(<>) {
s/\buint(..?)_t\b/u\1/g;
s/\bint(..?)_t\b/i\1/g;
print;
}