Skip to content

Commit

Permalink
Add writing to screen and print function
Browse files Browse the repository at this point in the history
  • Loading branch information
ravinperera00 committed Nov 27, 2023
1 parent 8bf1818 commit 4eb6ad9
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/
bin/
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
FILES = ./build/kernel.asm.o
FILES = ./build/kernel.asm.o ./build/kernel.o
INCLUDES = -I./src
FLAGS = -g -ffreestanding -falign-jumps -falign-functions -falign-labels -falign-loops -fstrength-reduce -fomit-frame-pointer -finline-functions -Wno-unused-function -fno-builtin -Werror -Wno-unused-label -Wno-cpp -Wno-unused-parameter -nostdlib -nostartfiles -nodefaultlibs -Wall -O0 -Iinc

all: ./bin/boot.bin ./bin/kernel.bin
rm -rf ./bin/os.bin
Expand All @@ -8,12 +10,15 @@ all: ./bin/boot.bin ./bin/kernel.bin

./bin/kernel.bin: $(FILES)
i686-elf-ld -g -relocatable $(FILES) -o ./build/kernelfull.o
i686-elf-gcc -T ./src/linker.ld -o ./bin/kernel.bin -ffreestanding -O0 -nostdlib ./build/kernelfull.o
i686-elf-gcc $(FLAGS) -T ./src/linker.ld -o ./bin/kernel.bin -ffreestanding -O0 -nostdlib ./build/kernelfull.o

./bin/boot.bin: ./src/boot/boot.asm
nasm -f bin ./src/boot/boot.asm -o ./bin/boot.bin
./build/kernel.asm.o: ./src/kernel.asm
nasm -f elf -g ./src/kernel.asm -o ./build/kernel.asm.o
./build/kernel.o: ./src/kernel.c
i686-elf-gcc $(INCLUDES) $(FLAGS) -std=gnu99 -c ./src/kernel.c -o ./build/kernel.o

clean:
rm -rf ./bin/boot.bin
rm -rf ./bin/kernel.bin
Expand Down
3 changes: 3 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#/bin/bash
./build.sh
qemu-system-x86_64 -hda bin/os.bin
3 changes: 2 additions & 1 deletion src/kernel.asm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BITS 32
global _start

extern kernel_main
CODE_SEG equ 0x08
DATA_SEG equ 0x10

Expand All @@ -19,6 +19,7 @@ _start:
or al, 2
out 0x92, al

call kernel_main
jmp $

times 512-($-$$) db 0
71 changes: 71 additions & 0 deletions src/kernel.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "kernel.h"
#include <stdint.h>
#include <stddef.h>

uint16_t *video_mem = 0;
uint16_t terminal_row = 0;
uint16_t terminal_col = 0;

uint16_t terminal_make_char(char c, char color)
{
return (color << 8) | c;
}

void terminal_putchar(int x, int y, char c, char color)
{
video_mem[(y * VGA_WIDTH + x)] = terminal_make_char(c, color);
}

void terminal_writechar(char c, char color)
{
if (c == '\n')
{
terminal_row += 1;
terminal_col = 0;
return;
}
terminal_putchar(terminal_col, terminal_row, c, color);
terminal_col += 1;
if (terminal_col >= VGA_WIDTH)
{
terminal_col = 0;
terminal_row += 1;
}
}

void terminal_initialize()
{
video_mem = (uint16_t *)(0xB8000);
for (int y = 0; y < VGA_HEIGHT; y++)
{
for (int x = 0; x < VGA_WIDTH; x++)
{
terminal_putchar(x, y, ' ', 0);
}
}
}

size_t strlen(const char *str)
{
size_t len = 0;
while (str[len])
{
len++;
}

return len;
}

void print(const char *str)
{
size_t len = strlen(str);
for (int i = 0; i < len; i++)
{
terminal_writechar(str[i], 15);
}
}
void kernel_main()
{
terminal_initialize();
print("Hello World!\nThis is fun");
}
9 changes: 9 additions & 0 deletions src/kernel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef KERNEL_H
#define KERNEL_H

#define VGA_WIDTH 80
#define VGA_HEIGHT 80

void kernel_main();

#endif

0 comments on commit 4eb6ad9

Please sign in to comment.