mirror of
https://github.com/iritmaximus/os.git
synced 2026-10-12 19:13:36 +03:00
moved things around, still ongoing
This commit is contained in:
-169
@@ -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");
|
||||
}
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
]
|
||||
@@ -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
|
||||
@@ -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");
|
||||
}
|
||||
Reference in New Issue
Block a user