45 lines
1.0 KiB
Zig
45 lines
1.0 KiB
Zig
const std = @import("std");
|
|
|
|
const cpu = @import("cpu.zig");
|
|
const types = @import("types.zig");
|
|
|
|
pub const Serial = struct {
|
|
const Self = @This();
|
|
|
|
ioport: u16,
|
|
|
|
pub fn init(ioport: u16) Self {
|
|
return .{
|
|
.ioport = ioport,
|
|
};
|
|
}
|
|
|
|
pub fn outStream(self: *const Self) OutStream {
|
|
return .{
|
|
.serial = self,
|
|
.stream = OutStream.Stream{ .writeFn = OutStream.writeFn },
|
|
};
|
|
}
|
|
|
|
pub fn put(self: *const Self, byte: u8) void {
|
|
cpu.outb(self.ioport, byte);
|
|
}
|
|
|
|
pub fn write(self: *const Self, string: []const u8) void {
|
|
for (string) |c|
|
|
self.put(c);
|
|
}
|
|
|
|
const OutStream = struct {
|
|
pub const Stream = std.io.OutStream(types.NoError);
|
|
|
|
serial: Self,
|
|
stream: Stream,
|
|
|
|
fn writeFn(out_stream: *Stream, string: []const u8) error{}!void {
|
|
const self = @fieldParentPtr(OutStream, "stream", out_stream);
|
|
self.serial.write(string);
|
|
}
|
|
};
|
|
};
|