From 3a0752f734f4370a05e45958499ddd9c3a4b3919 Mon Sep 17 00:00:00 2001 From: Christian Cunningham Date: Mon, 22 Aug 2022 21:39:16 -0700 Subject: Modularize --- src/kernel.rs | 57 +++++++++---------------------------------------- src/qemu/mod.rs | 31 +++++++++++++++++++++++++++ src/tests.rs | 43 +++++++++++++++++++++++++++++++++++++ src/vga/.buffer.rs.swp | Bin 20480 -> 0 bytes 4 files changed, 84 insertions(+), 47 deletions(-) create mode 100644 src/qemu/mod.rs create mode 100644 src/tests.rs delete mode 100644 src/vga/.buffer.rs.swp (limited to 'src') diff --git a/src/kernel.rs b/src/kernel.rs index cf594cd..514596d 100644 --- a/src/kernel.rs +++ b/src/kernel.rs @@ -9,28 +9,17 @@ #![test_runner(crate::test_runner)] #![reexport_test_harness_main = "test_main"] +mod qemu; +mod serial; mod sync; +mod tests; mod vga; -mod serial; -use vga::*; +use qemu::*; use serial::*; +use tests::*; +use vga::*; use core::panic::PanicInfo; -pub trait Testable { - fn run(&self) -> (); -} - -impl Testable for T -where - T: Fn(), -{ - fn run(&self) { - serial_print!("{}...\t", core::any::type_name::()); - self(); - serial_println!("[ok]"); - } -} - /// This function is called on panic. #[cfg(test)] #[panic_handler] @@ -49,11 +38,15 @@ fn panic(info: &PanicInfo) -> ! { loop {} } +/// # Initialization +/// +/// Provides serial and VGA initialization. fn kernel_init() { WRITER.init(); SERIAL1.init(); } +/// # x86_64 Kernel #[no_mangle] pub extern "C" fn _start() -> ! { kernel_init(); @@ -70,33 +63,3 @@ pub extern "C" fn _start() -> ! { loop {} } - -#[cfg(test)] -fn test_runner(tests: &[&dyn Testable]) { - serial_println!("Running {} tests", tests.len()); - for test in tests { - test.run(); - } - exit_qemu(QemuExitCode::Success); -} - -#[test_case] -fn trivial_assertion() { - assert_eq!(1, 1); -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -#[repr(u32)] -pub enum QemuExitCode { - Success = 0x10, - Failed = 0x11, -} - -pub fn exit_qemu(exit_code: QemuExitCode) { - use x86_64::instructions::port::Port; - - unsafe { - let mut port = Port::new(0xf4); - port.write(exit_code as u32); - } -} diff --git a/src/qemu/mod.rs b/src/qemu/mod.rs new file mode 100644 index 0000000..4c1ab37 --- /dev/null +++ b/src/qemu/mod.rs @@ -0,0 +1,31 @@ +//! # QEMU Crate +//! +//! Provide QEMU bindings + +/// # QEMU Exit Codes +/// +/// Provides QEMU exit codes +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(u32)] +pub enum QemuExitCode { + /// # Success + /// + /// Exit QEMU successfully + Success = 0x10, + /// # Failure + /// + /// Exit QEMU with error + Failed = 0x11, +} + +/// # Exit QEMU +/// +/// Exit QEMU with an exit code +pub fn exit_qemu(exit_code: QemuExitCode) { + use x86_64::instructions::port::Port; + + unsafe { + let mut port = Port::new(0xf4); + port.write(exit_code as u32); + } +} diff --git a/src/tests.rs b/src/tests.rs new file mode 100644 index 0000000..e26c87a --- /dev/null +++ b/src/tests.rs @@ -0,0 +1,43 @@ +use crate::{serial_print,serial_println}; +use crate::qemu::*; + +/// # Testable trait +/// +/// Trait for test functions +pub trait Testable { + fn run(&self) -> (); +} + +impl Testable for T +where + T: Fn(), +{ + /// # Default run + /// + /// Run each test and print results to output + fn run(&self) { + serial_print!("{}...\t", core::any::type_name::()); + self(); + serial_println!("[ok]"); + } +} + +/// # Run tests +/// +/// Run each of the tests +#[cfg(test)] +pub fn test_runner(tests: &[&dyn Testable]) { + serial_println!("Running {} tests", tests.len()); + for test in tests { + test.run(); + } + exit_qemu(QemuExitCode::Success); +} + +/// # Trivial test +/// +/// This test ought to succeed no matter what +#[test_case] +fn trivial_assertion() { + assert_eq!(1, 1); +} diff --git a/src/vga/.buffer.rs.swp b/src/vga/.buffer.rs.swp deleted file mode 100644 index 012b7b1..0000000 Binary files a/src/vga/.buffer.rs.swp and /dev/null differ -- cgit v1.2.1