rustubs/machine/
serial.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//! serial output through port 3f8 (qemu), stateless, non-blocking, non-locking,
//! not thread safe.

use crate::machine::device_io::IOPort;
use core::{fmt, str};
pub struct SerialWritter {
	port: IOPort,
}

impl SerialWritter {
	pub const fn new(port: u16) -> Self { Self { port: IOPort::new(port) } }

	pub fn putchar(&self, ch: char) { self.port.outb(ch as u8); }

	pub fn print(&self, s: &str) {
		for c in s.bytes() {
			self.putchar(c as char);
		}
	}
}

impl fmt::Write for SerialWritter {
	fn write_str(&mut self, s: &str) -> fmt::Result {
		self.print(s);
		Ok(())
	}
}