Commit 20eb3a51 by triazo

Make serial work more reliably

parent 47bb3424
/*
asm.c contains x86 specific functions for executing low level code
*/
#include "asm.h"
// Wrapper for outb
// Stolen from http://wiki.osdev.org/Inline_Assembly/Examples
static inline void outb(uint16_t port, uint8_t val) {
asm volatile ("outb %0, %1" : : "a"(val), "Nd"(port));
}
// Wrapper for inb
static inline uint8_t inb(uint16_t port) {
uint8_t ret;
asm volatile ("inb %1, %0" : "=a"(ret) : "Nd"(port));
return ret;
}
/* /*
asm.h exports archatecture specific functions for hardware interaction asm.h contains x86 specific functions for executing low level code
*/ */
#pragma once
#include <stdint.h> #include <stdint.h>
// Functions for reading and writing to/from x86 IO ports // Wrapper for outb
static inline void outb(uint16_t port, uint8_t val); // Stolen from http://wiki.osdev.org/Inline_Assembly/Examples
static inline uint8_t inb(uint16_t port); inline void outb(uint16_t port, uint8_t val) {
asm volatile ("outb %0, %1" : : "a"(val), "Nd"(port));
}
// Wrapper for inb
inline uint8_t inb(uint16_t port) {
uint8_t ret;
asm volatile ("inb %1, %0" : "=a"(ret) : "Nd"(port));
return ret;
}
...@@ -3,19 +3,19 @@ ...@@ -3,19 +3,19 @@
#endif #endif
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
/* Check if the compiler thinks we are targeting the wrong operating system. */ /* Check if the compiler thinks we are targeting the wrong operating system. */
#if defined(__linux__) #if defined(__linux__)
#error "You are not using a cross-compiler, you will most certainly run into trouble" #error "You are not using a cross-compiler, you will most certainly run into trouble"
#endif #endif
/* This tutorial will only work for the 32-bit ix86 targets. */ /* This tutorial will only work for the 32-bit ix86 targets. */
#if !defined(__i386__) #if !defined(__i386__)
#error "This tutorial needs to be compiled with a ix86-elf compiler" #error "This tutorial needs to be compiled with a ix86-elf compiler"
#endif #endif
#include "serial.h" #include "serial.h"
/* Hardware text mode color constants. */ /* Hardware text mode color constants. */
enum vga_color { enum vga_color {
COLOR_BLACK = 0, COLOR_BLACK = 0,
...@@ -35,32 +35,32 @@ enum vga_color { ...@@ -35,32 +35,32 @@ enum vga_color {
COLOR_LIGHT_BROWN = 14, COLOR_LIGHT_BROWN = 14,
COLOR_WHITE = 15, COLOR_WHITE = 15,
}; };
uint8_t make_color(enum vga_color fg, enum vga_color bg) { uint8_t make_color(enum vga_color fg, enum vga_color bg) {
return fg | bg << 4; return fg | bg << 4;
} }
uint16_t make_vgaentry(char c, uint8_t color) { uint16_t make_vgaentry(char c, uint8_t color) {
uint16_t c16 = c; uint16_t c16 = c;
uint16_t color16 = color; uint16_t color16 = color;
return c16 | color16 << 8; return c16 | color16 << 8;
} }
size_t strlen(const char* str) { size_t strlen(const char* str) {
size_t ret = 0; size_t ret = 0;
while ( str[ret] != 0 ) while ( str[ret] != 0 )
ret++; ret++;
return ret; return ret;
} }
static const size_t VGA_WIDTH = 80; static const size_t VGA_WIDTH = 80;
static const size_t VGA_HEIGHT = 25; static const size_t VGA_HEIGHT = 25;
size_t terminal_row; size_t terminal_row;
size_t terminal_column; size_t terminal_column;
uint8_t terminal_color; uint8_t terminal_color;
uint16_t* terminal_buffer; uint16_t* terminal_buffer;
void terminal_initialize() { void terminal_initialize() {
terminal_row = 0; terminal_row = 0;
terminal_column = 0; terminal_column = 0;
...@@ -73,16 +73,16 @@ void terminal_initialize() { ...@@ -73,16 +73,16 @@ void terminal_initialize() {
} }
} }
} }
void terminal_setcolor(uint8_t color) { void terminal_setcolor(uint8_t color) {
terminal_color = color; terminal_color = color;
} }
void terminal_putentryat(char c, uint8_t color, size_t x, size_t y) { void terminal_putentryat(char c, uint8_t color, size_t x, size_t y) {
const size_t index = y * VGA_WIDTH + x; const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = make_vgaentry(c, color); terminal_buffer[index] = make_vgaentry(c, color);
} }
void terminal_putchar(char c) { void terminal_putchar(char c) {
terminal_putentryat(c, terminal_color, terminal_column, terminal_row); terminal_putentryat(c, terminal_color, terminal_column, terminal_row);
if (++terminal_column == VGA_WIDTH) { if (++terminal_column == VGA_WIDTH) {
...@@ -92,25 +92,26 @@ void terminal_putchar(char c) { ...@@ -92,25 +92,26 @@ void terminal_putchar(char c) {
} }
} }
} }
void terminal_writestring(const char* data) { void terminal_writestring(const char* data) {
size_t datalen = strlen(data); size_t datalen = strlen(data);
for (size_t i = 0; i < datalen; i++) for (size_t i = 0; i < datalen; i++)
terminal_putchar(data[i]); terminal_putchar(data[i]);
} }
#if defined(__cplusplus) #if defined(__cplusplus)
extern "C" /* Use C linkage for kernel_main. */ extern "C" /* Use C linkage for kernel_main. */
#endif #endif
void kernel_main() { void kernel_main() {
/* Initialize terminal interface */ /* Initialize terminal interface */
terminal_initialize(); terminal_initialize();
/* Since there is no support for newlines in terminal_putchar /* Since there is no support for newlines in terminal_putchar
* yet, '\n' will produce some VGA specific character instead. * yet, '\n' will produce some VGA specific character instead.
* This is normal. * This is normal.
*/ */
terminal_writestring("Hello, kernel World!\n");
serial_init(); serial_init();
serial_writestring("Hello, kernel World!\n"); serial_writestring("First Serial Test!\n");
terminal_writestring("Hello, kernel World!\n");
serial_writestring("Second Serial Test\n");
} }
...@@ -6,3 +6,4 @@ all: ...@@ -6,3 +6,4 @@ all:
$C -T linker.ld -o myos.bin -ffreestanding -O2 -nostdlib *.o -lgcc $C -T linker.ld -o myos.bin -ffreestanding -O2 -nostdlib *.o -lgcc
clean: clean:
rm *.o *.bin
...@@ -27,7 +27,7 @@ void serial_writechar(char a) { ...@@ -27,7 +27,7 @@ void serial_writechar(char a) {
} }
void serial_writestring(char* s) { void serial_writestring(char* s) {
while (s) { while (*s) {
serial_writechar(*s); serial_writechar(*s);
s++; s++;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment