Skip to content

Commit

Permalink
implement serial and serial println with better qemu
Browse files Browse the repository at this point in the history
  • Loading branch information
jeebuscrossaint committed Jul 6, 2024
1 parent 670619e commit 0369e2b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
20 changes: 19 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ bootloader = "0.9"
volatile = "0.2.6"
spin = "0.5.2"
x86_64 = "0.14.2"
uart_16550 = "0.2.0"

#// DO NOTE THAT IN CARGO AS OF TO MY KNOWLEDGE 2019 AND EVEN NOW IN 2024
#// THERE IS A BUG IN CARGO WHERE THERE ARE "duplicate lang item" ERRORS
Expand All @@ -26,5 +27,8 @@ version = "1.0"
features = ["spin_no_std"]

[package.metadata.bootimage]
test-args = ["-device", "isa-debug-exit,iobase=0xf4,iosize=0x04"]
test-args = [
"-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-serial", "stdio",
"-display", "none"
]
test-success-exit-code = 33 # (0x10 << 1) | 1
19 changes: 16 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,27 @@
#![reexport_test_harness_main = "test_main"]

mod vga_buffer;
mod serial;
use core::panic::PanicInfo;

/// This function is called on panic.
#[cfg(not(test))] // new attribute dropped ayyy
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
println!("{}", info);
loop {}
}

// panic handler in test mode
#[cfg(test)]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
serial_println!("[failed]\n");
serial_println!("Error: {}\n", info);
exit_qemu(QemuExitCode::Failed);
loop {}
}


// from old vga implementation
// static VGA_BUF: &[u8] = b"Hello, world!";
Expand Down Expand Up @@ -58,6 +70,7 @@ pub extern "C" fn _start() -> ! {
// FOR A PROFILE IN THE CARGO.TOML FILE
#[cfg(test)]
fn test_runner(tests: &[&dyn Fn()]) {
serial_println!("Running {} tests", tests.len());
println!("Running {} tests", tests.len());
for test in tests {
test();
Expand All @@ -68,9 +81,9 @@ fn test_runner(tests: &[&dyn Fn()]) {

#[test_case]
fn trivial_assertion() {
print!("trivial assertion... ");
assert_eq!(1, 1);
println!("[ok]");
serial_print!("trivial assertion... ");
assert_eq!(0, 1);
serial_println!("[ok]");
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down
34 changes: 34 additions & 0 deletions src/serial.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use uart_16550::SerialPort;
use spin::Mutex;
use lazy_static::lazy_static;

lazy_static! {
pub static ref SERIAL1: Mutex<SerialPort> = {
let mut serial_port = unsafe { SerialPort::new(0x3F8) };
serial_port.init();
Mutex::new(serial_port)
};
}

#[doc(hidden)]
pub fn _print(args: ::core::fmt::Arguments) {
use core::fmt::Write;
SERIAL1.lock().write_fmt(args).expect("Printing to serial failed");
}

/// Prints to the host through the serial interface.
#[macro_export]
macro_rules! serial_print {
($($arg:tt)*) => {
$crate::serial::_print(format_args!($($arg)*));
};
}

/// Prints to the host through the serial interface, appending a newline.
#[macro_export]
macro_rules! serial_println {
() => ($crate::serial_print!("\n"));
($fmt:expr) => ($crate::serial_print!(concat!($fmt, "\n")));
($fmt:expr, $($arg:tt)*) => ($crate::serial_print!(
concat!($fmt, "\n"), $($arg)*));
}

0 comments on commit 0369e2b

Please sign in to comment.