Skip to content

Commit

Permalink
Serial io
Browse files Browse the repository at this point in the history
  • Loading branch information
Reavershark committed May 5, 2019
1 parent 9889e32 commit 6e72996
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 18 deletions.
4 changes: 3 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
set -ex
i686-elf-as arch/boot.s -o obj/boot.o
i686-elf-gdc -fno-druntime -Isrc -g -c src/convert.d -o obj/convert.o
i686-elf-gdc -fno-druntime -Isrc -g -c src/serial.d -o obj/serial.o
i686-elf-gdc -fno-druntime -Isrc -g -c src/terminal.d -o obj/terminal.o
i686-elf-gdc -fno-druntime -Isrc -g -c src/io.d -o obj/io.o
i686-elf-gdc -fno-druntime -Isrc -g -c src/main.d -o obj/main.o
i686-elf-ld -T arch/linker.ld obj/*.o -o kernel.bin
qemu-system-i386 -kernel kernel.bin
qemu-system-i386 -kernel kernel.bin $@
29 changes: 29 additions & 0 deletions src/io.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module io;

void outp(T)(ushort port, T d) {
uint data = d;
static if (T.sizeof == 1)
{
asm {
"outb %0, %1"
: : "a"(data),
"Nd"(port);
};
}
else static if (T.sizeof == 4)
{
asm {
"outw %0, %1"
: : "a"(data),
"Nd"(port);
};
}
else static if (T.sizeof == 4)
{
asm {
"outl %0, %1"
: : "a"(data),
"Nd"(port);
};
}
}
43 changes: 26 additions & 17 deletions src/main.d
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
module main;

import terminal;
import serial;

extern(C) void main(uint magic, uint addr) {

auto terminal = Terminal(80, 25);
// Serial COM1
auto serial = SerialDevice(SerialDevice.COMPORT.COM1);
serial.log("Kernel image loaded successfully");
serial.log("Welcome to BetterNamePendingOS!");
serial.log("Printing int '10': ", 10);

terminal.color = terminal.COLOR.GREEN;
terminal.writeln("Terminal initialized");
terminal.writeln("Kernel image loaded successfully");

terminal.color = terminal.COLOR.WHITE;
terminal.writeln();
terminal.writeln("Welcome to BetterNamePendingOS!");
terminal.writeln();
terminal.writeln("Running tests on all availiable modules:");
terminal.writeln();
terminal.writeln("convert.d");
terminal.writeln(" ", "Printing int '10': ", 10);
terminal.writeln(" ", "Printing ubyte '0xdd': ", cast(ubyte)0xdd);
terminal.writeln(":/");

for (;;) {
}
// VGA terminal
auto terminal = Terminal(80, 25);

terminal.color = terminal.COLOR.GREEN;
terminal.writeln("Terminal initialized");
terminal.writeln("Kernel image loaded successfully");

terminal.color = terminal.COLOR.WHITE;
terminal.writeln();
terminal.writeln("Welcome to BetterNamePendingOS!");
terminal.writeln();
terminal.writeln("Running tests on all availiable modules:");
terminal.writeln();
terminal.writeln("convert.d");
terminal.writeln(" ", "Printing int '10': ", 10);
terminal.writeln(" ", "Printing ubyte '0xdd': ", cast(ubyte)0xdd);
terminal.writeln(":/");

for (;;) {
}
}
77 changes: 77 additions & 0 deletions src/serial.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
module serial;

import io : outp;
import convert;

struct SerialDevice
{
public:
enum COMPORT : ushort
{
COM1 = 0x3f8,
COM2 = 0x2F8,
};

COMPORT port = COMPORT.COM1;

private void init() {
outp!ubyte(cast(ushort)(port + 1), cast(ubyte)0x00); // Disable all interrupts
outp!ubyte(cast(ushort)(port + 3), cast(ubyte)0x80); // Enable DLAB (set baud rate divisor)
outp!ubyte(cast(ushort)(port + 0), cast(ubyte)0x03); // Set divisor to 3 (lo byte) 38400 baud
outp!ubyte(cast(ushort)(port + 1), cast(ubyte)0x00); // (hi byte)
outp!ubyte(cast(ushort)(port + 3), cast(ubyte)0x03); // 8 bits, no parity, one stop bit
outp!ubyte(cast(ushort)(port + 2), cast(ubyte)0xC7); // Enable FIFO, clear them, with 14-byte threshold
outp!ubyte(cast(ushort)(port + 4), cast(ubyte)0x0B); // IRQs enabled, RTS/DSR set
}

this(COMPORT port)
{
this.port = port;
init();
}

void log(Args...)(Args args)
{
writeln("[LOG] ", args);
}

void writeln(Args...)(Args args)
{
write(args, '\r', '\n');
}

void write(Args...)(Args args)
{
foreach (arg; args) {
alias T = typeof(arg);
static if (is(T : const char[]))
{
putstr(arg);
}
else static if (is(T : char))
{
putchar(arg);
}
else static if (is(T : int))
{
putstr(convert.itoa(arg, 10));
}
else static if (is(T : ubyte))
{
putstr(convert.itoa(arg, 16));
}
}
}

private void putstr(string str)
{
foreach(c; str)
putchar(c);
}

void putchar(char c)
{
outp(this.port, c);
}
}

0 comments on commit 6e72996

Please sign in to comment.