Kernel: Add new generic Printer class

kprint is now just a #define to Printer::print<terminal_putc>.

This allows us to use same print formatting for serial output :)
This commit is contained in:
Bananymous
2022-12-10 00:33:03 +02:00
parent ac2ba8bc56
commit 3d3f12bd30
9 changed files with 480 additions and 174 deletions

48
kernel/kernel/Serial.cpp Normal file
View File

@@ -0,0 +1,48 @@
#include <kernel/IO.h>
#include <kernel/kprint.h>
#include <kernel/Serial.h>
#define COM1_PORT 0x3f8
namespace Serial
{
static bool s_initialized = false;
void initialize()
{
IO::outb(COM1_PORT + 1, 0x00); // Disable all interrupts
IO::outb(COM1_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
IO::outb(COM1_PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
IO::outb(COM1_PORT + 1, 0x00); // (hi byte)
IO::outb(COM1_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
IO::outb(COM1_PORT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
IO::outb(COM1_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
IO::outb(COM1_PORT + 4, 0x1E); // Set in loopback mode, test the serial chip
IO::outb(COM1_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(IO::inb(COM1_PORT + 0) != 0xAE) {
kprint("Could not initialize COM1 serial port\n");
return;
}
// If serial is not faulty set it in normal operation mode
// (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled)
IO::outb(COM1_PORT + 4, 0x0F);
s_initialized = true;
}
int is_transmit_empty() {
return IO::inb(COM1_PORT + 5) & 0x20;
}
void serial_putc(char c)
{
if (!s_initialized)
return;
while (is_transmit_empty() == 0);
IO::outb(COM1_PORT, c);
}
}

View File

@@ -1,14 +1,15 @@
#include <kernel/GDT.h>
#include <kernel/IDT.h>
#include <kernel/IO.h>
#include <kernel/Keyboard.h>
#include <kernel/kmalloc.h>
#include <kernel/kprint.h>
#include <kernel/multiboot.h>
#include <kernel/panic.h>
#include <kernel/PIC.h>
#include <kernel/PIT.h>
#include <kernel/Serial.h>
#include <kernel/tty.h>
#include <kernel/kprint.h>
#include <kernel/IO.h>
#include <string.h>
#include <stdlib.h>
@@ -22,6 +23,12 @@ void on_key_press(Keyboard::Key key, uint8_t modifiers, bool pressed)
{
if (pressed)
{
if (key == Keyboard::Key::Escape)
{
kprint("time since boot: {} ms\n", PIT::ms_since_boot());
return;
}
char ascii = Keyboard::key_to_ascii(key, modifiers);
if (ascii)
kprint("{}", ascii);
@@ -36,9 +43,13 @@ void kernel_main(multiboot_info_t* mbi, uint32_t magic)
s_multiboot_info = mbi;
if (magic != 0x2BADB002)
goto halt;
return;
terminal_initialize();
if (mbi->framebuffer.type != 2)
return;
TTY::initialize();
Serial::initialize();
kmalloc_initialize();
@@ -49,11 +60,18 @@ void kernel_main(multiboot_info_t* mbi, uint32_t magic)
PIT::initialize();
Keyboard::initialize(on_key_press);
kprint("Hello from the kernel!\n");
ENABLE_INTERRUPTS();
kprintln("Hello from the kernel!");
dprintln("Hello emulator from kernel!");
int** lol = new int*[10];
for (int i = 0; i < 10; i++)
lol[i] = new int;
kprint("{.2}\n", -12.123f);
kprint("0x{.H}", 0xcafebabe);
halt:
for (;;)
{
asm("hlt");