serial print macro

refactor
pantonshire 5 months ago
parent 988d3d601d
commit e2478d0f3a

@ -1,6 +1,28 @@
use core::{fmt, hint};
use crate::ioport;
use crate::{ioport, spin::Spinlock};
const COM1_PORT: u16 = 0x3f8;
pub static COM1: Spinlock<Option<Com>> = Spinlock::new(None);
#[macro_export]
macro_rules! com1_print {
($($args:tt)*) => ({
crate::com::with_com1_if_available(|com1| {
::core::write!(com1, $($args)*).ok();
});
})
}
#[macro_export]
macro_rules! com1_println {
($($args:tt)*) => ({
crate::com::with_com1_if_available(|com1| {
::core::writeln!(com1, $($args)*).ok();
});
})
}
pub struct Com {
port: u16,
@ -8,6 +30,27 @@ pub struct Com {
pub struct ComError;
pub fn with_com1_if_available<F>(f: F)
where
F: FnOnce(&mut Com),
{
let mut guard = COM1.lock();
if let Some(com1) = guard.as_mut() {
f(com1);
}
}
pub unsafe fn try_com1_init() -> bool {
match unsafe { Com::init(COM1_PORT) } {
Ok(com1) => {
let mut guard = COM1.lock();
*guard = Some(com1);
true
}
_ => false,
}
}
impl Com {
pub unsafe fn init(port: u16) -> Result<Self, ComError> {
const ECHO_BYTE: u8 = 0x5a;
@ -43,9 +86,7 @@ impl Com {
ioport::outb(port + 4, 0x0f);
}
Ok(Self {
port,
})
Ok(Self { port })
}
pub fn poll_has_data(&self) -> bool {
@ -80,13 +121,4 @@ impl fmt::Write for Com {
}
Ok(())
}
fn write_char(&mut self, c: char) -> fmt::Result {
let mut buf = [0u8; 4];
let s = c.encode_utf8(&mut buf);
for b in s.bytes() {
self.write_poll(b);
}
Ok(())
}
}

@ -3,12 +3,17 @@
#![feature(custom_test_frameworks)]
#![test_runner(crate::test_runner)]
mod spin;
mod com;
mod ioport;
mod spin;
mod vga;
mod com;
use core::{arch::{asm, global_asm}, fmt::Write, panic::PanicInfo, ptr, slice};
use core::{
arch::{asm, global_asm},
fmt::{self, Write},
panic::PanicInfo,
ptr, slice,
};
global_asm!(include_str!("asm/trampoline.s"));
@ -22,12 +27,14 @@ fn panic(info: &PanicInfo) -> ! {
hlt()
}
const COM1_PORT: u16 = 0x3f8;
#[unsafe(no_mangle)]
pub extern "C" fn _start() -> ! {
vga::vga_init();
unsafe {
com::try_com1_init();
}
let gdt_ptr = ptr::with_exposed_provenance::<u64>(0xc000);
let gdt_slice = unsafe { slice::from_raw_parts(gdt_ptr, 4) };
@ -37,11 +44,7 @@ pub extern "C" fn _start() -> ! {
vga_println!("{:016x}", gdt_slice[2]);
vga_println!("{:016x}", gdt_slice[3]);
let mut com = unsafe { com::Com::init(COM1_PORT) };
if let Ok(com) = &mut com {
writeln!(com, "hello serial").ok();
}
com1_println!("hello serial!");
hlt()
}

@ -1,4 +1,9 @@
use core::{cell::UnsafeCell, hint, ops::{Deref, DerefMut}, sync::atomic::{AtomicBool, Ordering}};
use core::{
cell::UnsafeCell,
hint,
ops::{Deref, DerefMut},
sync::atomic::{AtomicBool, Ordering},
};
pub struct Spinlock<T> {
data: UnsafeCell<T>,
@ -19,7 +24,8 @@ impl<T> Spinlock<T> {
// observed the `false` value.
// - Store `true`, so nothing else can enter the critical section until we exit it.
// Otherwise, spin until we observe a `false` value.
while self.locked
while self
.locked
.compare_exchange_weak(false, true, Ordering::Acquire, Ordering::Relaxed)
.is_err()
{
@ -57,10 +63,7 @@ impl<T> Spinlock<T> {
}
}
unsafe impl<T> Sync for Spinlock<T>
where
T: Send,
{}
unsafe impl<T> Sync for Spinlock<T> where T: Send {}
pub struct SpinlockGuard<'a, T> {
lock: &'a Spinlock<T>,

@ -38,7 +38,7 @@ pub fn with_vga_buf<F>(f: F)
where
F: FnOnce(&mut VgaBuf),
{
let mut guard = crate::vga::VGA.lock();
let mut guard = VGA.lock();
let vga = guard.as_mut().expect("vga not initialised");
f(vga);
}
@ -51,7 +51,11 @@ pub struct VgaBuf {
impl VgaBuf {
pub unsafe fn new(buf: *mut u16) -> Self {
Self { buf, col: 0, row: 0 }
Self {
buf,
col: 0,
row: 0,
}
}
pub fn vga_write_str(&mut self, s: &str) {
@ -84,8 +88,7 @@ impl VgaBuf {
if col < VGA_WIDTH && row < VGA_HEIGHT {
unsafe {
self.coord_ptr(col, row)
.write_volatile(vga_val);
self.coord_ptr(col, row).write_volatile(vga_val);
}
}
}
@ -100,7 +103,7 @@ impl VgaBuf {
ptr::copy(
self.coord_ptr(0, 1),
self.coord_ptr(0, 0),
VGA_WIDTH * (VGA_HEIGHT - 1)
VGA_WIDTH * (VGA_HEIGHT - 1),
);
for col in 0..VGA_WIDTH {

Loading…
Cancel
Save