Add serial and serial printing for debugging

This commit is contained in:
g1n 2022-02-09 18:20:45 +02:00
parent 43d9165486
commit a4a7626010
Signed by: g1n
GPG Key ID: 8D352193D65D4E2C
6 changed files with 74 additions and 3 deletions

View File

@ -5,7 +5,8 @@
#include <stdlib.h>
void early_kernel_main() {
terminal_initialize();
serial_init();
terminal_init();
idt_init();
paging_init();
}

13
src/include/serial.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef SERIAL_H
#define SERIAL_H
#include <stdarg.h>
#define PORT 0x3f8 // COM1
int serial_init();
int dbg_putchar(int c);
int vdbgf(const char *restrict format, va_list ap);
int dbgf(const char *restrict format, ...);
#endif

View File

@ -28,7 +28,7 @@ enum vga_color {
#define VGA_WIDTH 80
#define VGA_HEIGHT 25
void terminal_initialize(void);
void terminal_init(void);
void terminal_setcolor(uint8_t color);
void terminal_putentryat(char c, uint8_t color, size_t x, size_t y);
void terminal_putchar(char c);

View File

@ -1,7 +1,9 @@
#include <vga.h>
#include <stdio.h>
#include <stdlib.h>
#include <serial.h>
void kernel_main() {
kprintf("Hello, World!\n");
dbgf("Hello serial World!\n");
}

55
src/serial.c Normal file
View File

@ -0,0 +1,55 @@
#include <serial.h>
#include <asm.h>
#include <stdio.h>
int serial_init() {
outb(PORT + 1, 0x00); // Disable all interrupts
outb(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
outb(PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
outb(PORT + 1, 0x00); // (hi byte)
outb(PORT + 3, 0x03); // 8 bits, no parity, one stop bit
outb(PORT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
outb(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
outb(PORT + 4, 0x1E); // Set in loopback mode, test the serial chip
outb(PORT + 0, 0xAE); // Test serial chip (send byte 0xAE and check if serial returns same byte)
// Check if serial is faulty (i.e: not same byte as sent)
if(inb(PORT + 0) != 0xAE) {
return 1;
}
// If serial is not faulty set it in normal operation mode
// (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled)
outb(PORT + 4, 0x0F);
return 0;
}
int is_transmit_empty() {
return inb(PORT + 5) & 0x20;
}
int dbg_putchar(int c) {
while (is_transmit_empty() == 0);
outb(PORT, c);
return (unsigned char)c;
}
int vdbgf(const char *restrict format, va_list ap) {
char s[1024] = ""; // FIXME
int size = kvsprintf(s, format, ap);
int i = 0;
while (i < size) {
dbg_putchar(s[i]);
i++;
}
return size;
}
int dbgf(const char *restrict format, ...) {
int size;
va_list ap;
va_start(ap, format);
size = vdbgf(format, ap);
va_end(ap);
return size;
}

View File

@ -25,7 +25,7 @@ static inline uint16_t vga_entry(unsigned char uc, int color) {
return (uint16_t) uc | (uint16_t) color << 8;
}
void terminal_initialize(void) {
void terminal_init(void) {
terminal_row = 0;
terminal_column = 0;
terminal_color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK);