Add serial module

This commit is contained in:
Josh Holtrop 2023-09-04 14:56:12 -04:00
parent 93a4065dea
commit e25d28ebc7
3 changed files with 77 additions and 1 deletions

View File

@ -21,6 +21,7 @@ import hulk.pic;
import hulk.acpi;
import hulk.apic;
import hulk.rtc;
import hulk.serial;
extern extern(C) __gshared ubyte _hulk_bss_size;
@ -56,6 +57,7 @@ void hulk_start()
{
cli();
initialize_cpu();
Serial.initialize();
Gdt.initialize();
Idt.initialize();
Fb.initialize(cast(uint *)Hurl.HULK_FRAMEBUFFER,

View File

@ -58,7 +58,7 @@ struct Klog
}
/**
* Write a formatted string and newline to the console.
* Write a formatted string and newline to the kernel log.
*
* @param s Format string.
*/

74
src/hulk/serial.d Normal file
View File

@ -0,0 +1,74 @@
/**
* Serial port access.
*/
module hulk.serial;
import hulk.cpu;
import core.stdc.stdarg;
static import hulk.writef;
struct Serial
{
enum BASE_PORT = 0x3F8;
/**
* Initialize the serial module.
*/
static void initialize()
{
}
/**
* Write a character to the serial port.
*
* @param ch Character to write.
*/
static void write(ubyte ch)
{
if (ch == '\n')
{
out8(BASE_PORT, '\r');
}
out8(BASE_PORT, ch);
}
/**
* Write a formatted string to the serial port.
*
* @param s Format string.
* @param args Variable arguments structure.
*/
public static void writef(string s, va_list args)
{
hulk.writef.writef(s, args, function void(ubyte ch) {
Serial.write(ch);
});
}
/**
* Write a formatted string to the serial port.
*
* @param s Format string.
*/
public static extern (C) void writef(string s, ...)
{
va_list args;
va_start(args, s);
writef(s, args);
va_end(args);
}
/**
* Write a formatted string and newline to the serial port.
*
* @param s Format string.
*/
public static extern (C) void writefln(string s, ...)
{
va_list args;
va_start(args, s);
writef(s, args);
writef("\n", args);
va_end(args);
}
}