pluto/src/kernel/serial.zig

60 lines
1.4 KiB
Zig
Raw Normal View History

2019-06-16 23:48:32 +01:00
const arch = @import("arch.zig").internals;
const build_options = @import("build_options");
2019-06-16 23:48:32 +01:00
2020-06-04 14:39:37 +01:00
pub const Serial = struct {
/// Function that writes a single byte to the serial stream
pub const Write = fn (byte: u8) void;
write: Write,
///
/// Write a slice of bytes to the serial stream.
///
/// Arguments:
/// str: []const u8 - The bytes to send.
///
pub fn writeBytes(self: *const @This(), bytes: []const u8) void {
for (bytes) |byte| {
self.write(byte);
}
2019-10-08 00:11:50 +01:00
}
2020-06-04 14:39:37 +01:00
};
2019-10-08 00:11:50 +01:00
2019-06-16 23:48:32 +01:00
///
2020-06-04 14:39:37 +01:00
/// Initialise the serial interface. The details of how this is done depends on the architecture.
2019-06-16 23:48:32 +01:00
///
2020-06-09 13:47:06 +01:00
/// Arguments:
/// IN boot_payload: arch.BootPayload - The payload passed to the kernel at boot. How this is used depends on the architecture
///
2020-06-04 14:39:37 +01:00
/// Return: Serial
/// The serial interface constructed by the architecture
2019-06-16 23:48:32 +01:00
///
2020-06-09 13:47:06 +01:00
pub fn init(boot_payload: arch.BootPayload) Serial {
const serial = arch.initSerial(boot_payload);
2020-06-04 14:39:37 +01:00
if (build_options.rt_test) runtimeTests(serial);
return serial;
2019-06-16 23:48:32 +01:00
}
2019-10-08 00:11:50 +01:00
///
/// Run all the runtime tests
///
2020-06-04 14:39:37 +01:00
pub fn runtimeTests(serial: Serial) void {
rt_writeByte(serial);
rt_writeBytes(serial);
2019-10-08 00:11:50 +01:00
}
///
/// Test writing a byte and a new line separately
///
2020-06-04 14:39:37 +01:00
fn rt_writeByte(serial: Serial) void {
serial.write('c');
serial.write('\n');
2019-10-08 00:11:50 +01:00
}
///
/// Test writing a series of bytes
///
2020-06-04 14:39:37 +01:00
fn rt_writeBytes(serial: Serial) void {
serial.writeBytes(&[_]u8{ '1', '2', '3', '\n' });
2019-10-08 00:11:50 +01:00
}