moved things around, still ongoing

This commit is contained in:
Aaro Koivukangas
2026-04-15 10:39:28 +03:00
parent 5eac60973a
commit b43278ab06
14 changed files with 320 additions and 170 deletions
+2
View File
@@ -7,3 +7,5 @@
sysroot
isodir
c-meaty-skeleton
-169
View File
@@ -1,169 +0,0 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
/* Check if the compiler thinks you are targeting the wrong operating system. */
#if defined(__linux__)
#error "You are not using a cross-compiler, you will most certainly run into trouble"
#endif
/* This tutorial will only work for the 32-bit ix86 targets. */
#if !defined(__i386__)
#error "This tutorial needs to be compiled with a ix86-elf compiler"
#endif
/* Hardware text mode color constants. */
enum vga_color {
VGA_COLOR_BLACK = 0,
VGA_COLOR_BLUE = 1,
VGA_COLOR_GREEN = 2,
VGA_COLOR_CYAN = 3,
VGA_COLOR_RED = 4,
VGA_COLOR_MAGENTA = 5,
VGA_COLOR_BROWN = 6,
VGA_COLOR_LIGHT_GREY = 7,
VGA_COLOR_DARK_GREY = 8,
VGA_COLOR_LIGHT_BLUE = 9,
VGA_COLOR_LIGHT_GREEN = 10,
VGA_COLOR_LIGHT_CYAN = 11,
VGA_COLOR_LIGHT_RED = 12,
VGA_COLOR_LIGHT_MAGENTA = 13,
VGA_COLOR_LIGHT_BROWN = 14,
VGA_COLOR_WHITE = 15,
};
static inline uint8_t vga_entry_color(enum vga_color fg, enum vga_color bg)
{
return fg | bg << 4;
}
static inline uint16_t vga_entry(unsigned char uc, uint8_t color)
{
return (uint16_t) uc | (uint16_t) color << 8;
}
size_t strlen(const char* str)
{
size_t len = 0;
while (str[len])
len++;
return len;
}
static const size_t VGA_WIDTH = 80;
static const size_t VGA_HEIGHT = 25;
size_t terminal_row;
size_t terminal_column;
uint8_t terminal_color;
uint16_t* terminal_buffer;
void terminal_initialize(void)
{
terminal_row = 0;
terminal_column = 0;
terminal_color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK);
terminal_buffer = (uint16_t*) 0xB8000;
for (size_t y = 0; y < VGA_HEIGHT; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = vga_entry(' ', terminal_color);
}
}
}
void terminal_setcolor(uint8_t color)
{
terminal_color = color;
}
void terminal_putentryat(char c, uint8_t color, size_t x, size_t y)
{
const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = vga_entry(c, color);
}
void terminal_clear_row(size_t row)
{
for (size_t i=0; i<VGA_WIDTH; i++)
terminal_putentryat(' ', terminal_color, i, row);
}
void terminal_scroll(void)
{
// row 0 => Hello 1
// row 1 => Hello 2
// row 2 => Hello 3 *
//
// scroll 1
//
// row 0 => Hello 2
// row 1 => Hello 3
// row 2 => *
for (size_t i=0; i<VGA_HEIGHT; i++)
{
for (size_t j=0; j<VGA_WIDTH; j++)
terminal_buffer[i*VGA_WIDTH + j] = terminal_buffer[(i+1)*VGA_WIDTH + j];
}
terminal_clear_row(VGA_HEIGHT-1);
}
void terminal_putchar(char c)
{
if (c == '\n') {
terminal_column = 0;
if (++terminal_row == VGA_HEIGHT)
{
terminal_scroll();
terminal_row--;
}
return;
}
terminal_putentryat(c, terminal_color, terminal_column, terminal_row);
if (++terminal_column == VGA_WIDTH)
{
terminal_column = 0;
if (++terminal_row == VGA_HEIGHT)
{
terminal_scroll();
terminal_row--;
}
}
}
void terminal_write(const char* data, size_t size)
{
for (size_t i = 0; i < size; i++)
terminal_putchar(data[i]);
}
void terminal_writestring(const char* data)
{
terminal_write(data, strlen(data));
}
void terminal_test_scroll()
{
for (size_t i=0; i<126-65; i++)
{
terminal_putchar(i+65);
terminal_writestring(" more chars...\n");
}
}
void kernel_main(void)
{
/* Initialize terminal interface */
terminal_initialize();
/* Newline support is left as an exercise. */
terminal_test_scroll();
terminal_writestring("Hello from the kernel :)\n");
}
+44
View File
@@ -0,0 +1,44 @@
CC=i686-elf-gcc
AS=i686-elf-as
QEMU=qemu-system-i386
ARCH=i386
CFLAGS?=-ffreestanding -O2 -Wall -Wextra
CPPFLAGS?=-Iinclude -Iarch/$(ARCH)/include
LDFLAGS?=
ARCHDIR=arch/i386
KERNELDIR=kernel
ISODIR=isodir
OBJS=$(KERNELDIR)/kernel.o $(ARCHDIR)/tty.o boot.o
.PHONY: clean run clean
.SUFFIXES: .o .c .s
run: deros.iso
$(QEMU) -cdrom deros.iso
.c.o:
$(CC) -MD -c $< -std=gnu99 -o $@ $(CFLAGS) $(CPPFLAGS)
.s.o:
$(CC) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS)
deros.bin: $(OBJS)
$(CC) -T linker.ld -o deros.bin $(CFLAGS) -nostdlib $(OBJS) -lgcc
isodir: deros.bin
mkdir -p $(ISODIR)/boot/grub
cp deros.bin $(ISODIR)/boot
cp grub.cfg $(ISODIR)/boot/grub/grub.cfg
deros.iso: deros.bin isodir
grub-mkrescue -o deros.iso isodir
clean:
rm -f *.o *.bin *.iso *.d
rm -f $(KERNELDIR)/*.o $(KERNELDIR)/*.d
rm -f $(ARCHDIR)/*.o $(ARCHDIR)/*.d
rm -rf isodir
+24
View File
@@ -0,0 +1,24 @@
#ifndef TTY_H
#define TTY_H
#include <stddef.h>
#include <stdint.h>
#include "vga.h"
static size_t TERMINAL_WIDTH = VGA_WIDTH;
static size_t TERMINAL_HEIGHT = VGA_HEIGHT;
static size_t terminal_row;
static size_t terminal_column;
static uint8_t terminal_color;
static uint16_t *terminal_buffer;
void terminal_setcolor(uint8_t color);
void terminal_putentryat(char c, uint8_t color, size_t x, size_t y);
void terminal_clear_row(size_t row);
void terminal_scroll(void);
void terminal_initialize(void);
void terminal_initialize2(void);
#endif
+38
View File
@@ -0,0 +1,38 @@
#ifndef VGA_H
#define VGA_H
#include <stddef.h>
#include <stdint.h>
/* Hardware text mode color constants. */
enum vga_color {
VGA_COLOR_BLACK = 0,
VGA_COLOR_BLUE = 1,
VGA_COLOR_GREEN = 2,
VGA_COLOR_CYAN = 3,
VGA_COLOR_RED = 4,
VGA_COLOR_MAGENTA = 5,
VGA_COLOR_BROWN = 6,
VGA_COLOR_LIGHT_GREY = 7,
VGA_COLOR_DARK_GREY = 8,
VGA_COLOR_LIGHT_BLUE = 9,
VGA_COLOR_LIGHT_GREEN = 10,
VGA_COLOR_LIGHT_CYAN = 11,
VGA_COLOR_LIGHT_RED = 12,
VGA_COLOR_LIGHT_MAGENTA = 13,
VGA_COLOR_LIGHT_BROWN = 14,
VGA_COLOR_WHITE = 15,
};
static const size_t VGA_WIDTH = 80;
static const size_t VGA_HEIGHT = 25;
static inline uint8_t vga_entry_color(enum vga_color fg, enum vga_color bg) {
return fg | bg << 4;
}
static inline uint16_t vga_entry(unsigned char uc, uint8_t color) {
return (uint16_t)uc | (uint16_t)color << 8;
}
#endif
+37
View File
@@ -0,0 +1,37 @@
#include "tty.h"
#include "vga.h"
void terminal_putentryat(char c, uint8_t color, size_t x, size_t y) {
const size_t index = y * TERMINAL_WIDTH + x;
terminal_buffer[index] = vga_entry(c, color);
}
void terminal_setcolor(uint8_t color) { terminal_color = color; }
void terminal_clear_row(size_t row) {
for (size_t i = 0; i < TERMINAL_WIDTH; i++)
terminal_putentryat(' ', terminal_color, i, row);
}
void terminal_scroll(void) {
for (size_t i = 0; i < TERMINAL_HEIGHT; i++) {
for (size_t j = 0; j < TERMINAL_WIDTH; j++)
terminal_buffer[i * TERMINAL_WIDTH + j] =
terminal_buffer[(i + 1) * TERMINAL_WIDTH + j];
}
terminal_clear_row(TERMINAL_HEIGHT - 1);
}
void terminal_initialize(void) {
terminal_row = 0;
terminal_column = 0;
terminal_color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK);
terminal_buffer = (uint16_t *)0xB8000;
for (size_t y = 0; y < TERMINAL_HEIGHT; y++) {
for (size_t x = 0; x < TERMINAL_WIDTH; x++) {
const size_t index = y * TERMINAL_WIDTH + x;
terminal_buffer[index] = vga_entry(' ', terminal_color);
}
}
}
View File
+22
View File
@@ -0,0 +1,22 @@
[
{
"file": "arch/i386/tty.c",
"arguments": [
"i686-elf-gcc",
"-MD",
"-c",
"arch/i386/tty.c",
"-std=gnu99",
"-o",
"arch/i386/tty.o",
"-ffreestanding",
"-O2",
"-Wall",
"-Wextra",
"-Iinclude",
"-Iarch/i386/include"
],
"directory": "/home/martti/git/deros/c/kernel",
"output": "arch/i386/tty.o"
}
]
View File
+12
View File
@@ -0,0 +1,12 @@
#ifndef KERNEL_H
#define KERNEL_H
#include <stddef.h>
#include <stdint.h>
void terminal_initialize(void);
void terminal_write(const char *data, size_t size);
void terminal_writestring(const char *data);
void kernel_main(void);
#endif
+71
View File
@@ -0,0 +1,71 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "tty.h"
/* Check if the compiler thinks you are targeting the wrong operating system. */
#if defined(__linux__)
#error \
"You are not using a cross-compiler, you will most certainly run into trouble"
#endif
/* This tutorial will only work for the 32-bit ix86 targets. */
#if !defined(__i386__)
#error "This tutorial needs to be compiled with a ix86-elf compiler"
#endif
size_t strlen(const char *str) {
size_t len = 0;
while (str[len])
len++;
return len;
}
void terminal_putchar(char c) {
if (c == '\n') {
terminal_column = 0;
if (++terminal_row == TERMINAL_HEIGHT) {
terminal_scroll();
terminal_row--;
}
return;
}
terminal_putentryat(c, terminal_color, terminal_column, terminal_row);
if (++terminal_column == TERMINAL_WIDTH) {
terminal_column = 0;
if (++terminal_row == TERMINAL_HEIGHT) {
terminal_scroll();
terminal_row--;
}
}
}
void terminal_write(const char *data, size_t size) {
for (size_t i = 0; i < size; i++)
terminal_putchar(data[i]);
}
void terminal_writestring(const char *data) {
terminal_write(data, strlen(data));
}
void terminal_test_scroll() {
for (size_t i = 0; i < 126 - 65; i++) {
terminal_putchar(i + 65);
terminal_writestring(" more chars...\n");
}
}
void kernel_main(void) {
/* Initialize terminal interface */
terminal_initialize();
/* Newline support is left as an exercise. */
terminal_test_scroll();
terminal_writestring("Hello from the kernel :)\n");
}
View File
+3 -1
View File
@@ -1,6 +1,8 @@
#![no_std]
#![no_main]
mod vga_buffer;
static HELLO: &[u8] = b"Hello World!";
#[unsafe(no_mangle)]
@@ -12,9 +14,9 @@ pub extern "C" fn _start() -> ! {
*vga_buffer.offset(i as isize * 2 + 1) = 0xb;
}
}
loop {}
}
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
+67
View File
@@ -0,0 +1,67 @@
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum Color {
Black = 0,
Blue = 1,
Green = 2,
Cyan = 3,
Red = 4,
Magenta = 5,
Brown = 6,
LightGray = 7,
DarkGray = 8,
LightBlue = 9,
LightGreen = 10,
LightCyan = 11,
LightRed = 12,
Pink = 13,
Yellow = 14,
White = 15,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(transparent)]
struct ColorCode(u8);
impl ColorCode {
fn new(foreground: Color, background: Color) -> ColorCode {
ColorCode((background as u8) << 4 | (foreground as u8))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(C)]
struct TerminalChar {
ascii_char: u8,
color_code: ColorCode,
}
const BUFFER_HEIGHT: usize = 25;
const BUFFER_WIDTH: usize = 80;
#[repr(transparent)]
struct Buffer {
chars: [[TerminalChar; BUFFER_WIDTH]; BUFFER_HEIGHT],
}
pub struct Writer {
column_position: usize,
color_code: ColorCode,
buffer: &'static mut Buffer,
}
impl Writer {
fn write_byte(&mut self, data: u8) {
if self.column_position + 1 < BUFFER_WIDTH {
let terminal_char = TerminalChar {
ascii_char: data,
color_code: self.color_code,
};
self.buffer.chars[BUFFER_HEIGHT - 1][self.column_position + 1] = terminal_char;
self.column_position += 1;
}
}
}
pub fn write_string(data: &str) {}