Add IO and Serial
This commit is contained in:
parent
d49823d5d0
commit
a08a53c7f0
9
moon/Cargo.lock
generated
9
moon/Cargo.lock
generated
@ -5,3 +5,12 @@ version = 3
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "moon"
|
name = "moon"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"spin",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "spin"
|
||||||
|
version = "0.5.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
|
||||||
|
@ -16,3 +16,4 @@ strip = "debuginfo"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
spin = "0.5.2"
|
34
moon/src/arch/io.rs
Normal file
34
moon/src/arch/io.rs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#[cfg(target_arch = "x86_64")]
|
||||||
|
pub use super::x86_64::io::*;
|
||||||
|
|
||||||
|
pub mod serial {
|
||||||
|
#[cfg(target_arch = "x86_64")]
|
||||||
|
pub use super::super::x86_64::io::*;
|
||||||
|
|
||||||
|
pub fn write(s: &str) -> () {
|
||||||
|
for byte in s.bytes() {
|
||||||
|
match byte {
|
||||||
|
0x20..=0x7e | b'\n' => serial::putchar(byte),
|
||||||
|
_ => serial::putchar(0xfe),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
use core::fmt;
|
||||||
|
|
||||||
|
pub struct Serial
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Write for Serial {
|
||||||
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
||||||
|
serial::write(s);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
use spin::Mutex;
|
||||||
|
|
||||||
|
pub static SERIAL: Mutex<Serial> = Mutex::new(Serial {});
|
@ -1,4 +1,5 @@
|
|||||||
pub mod cpu;
|
pub mod cpu;
|
||||||
|
pub mod io;
|
||||||
|
|
||||||
#[cfg(target_arch = "x86_64")]
|
#[cfg(target_arch = "x86_64")]
|
||||||
mod x86_64;
|
mod x86_64;
|
35
moon/src/arch/x86_64/io.rs
Normal file
35
moon/src/arch/x86_64/io.rs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
#![cfg(target_arch = "x86_64")]
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
pub const COM1: u16 = 0x3f8;
|
||||||
|
|
||||||
|
use core::arch::asm;
|
||||||
|
|
||||||
|
pub unsafe fn outportb(port: u16, value: u8) -> () {
|
||||||
|
asm!("out dx, al", in("al") value, in("dx") port);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub unsafe fn inportb(port: u16) -> u8 {
|
||||||
|
let value: u8;
|
||||||
|
asm!("in al, dx", out("al") value, in("dx") port);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub unsafe fn io_delay() -> () {
|
||||||
|
outportb(0x80,0);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod serial {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
unsafe fn wait() -> () {
|
||||||
|
while (inportb(COM1 + 5) & 0x20) == 0 {
|
||||||
|
asm!("pause");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn putchar(c: u8) -> () {
|
||||||
|
unsafe { wait(); }
|
||||||
|
unsafe { outportb(COM1, c); }
|
||||||
|
}
|
||||||
|
}
|
@ -1 +1,3 @@
|
|||||||
|
#![cfg(target_arch = "x86_64")]
|
||||||
pub mod cpu;
|
pub mod cpu;
|
||||||
|
pub mod io;
|
19
moon/src/log.rs
Normal file
19
moon/src/log.rs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
use crate::arch::io::SERIAL;
|
||||||
|
use core::fmt;
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! print {
|
||||||
|
($($arg:tt)*) => ($crate::log::_print(format_args!($($arg)*)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! println {
|
||||||
|
() => ($crate::print!("\n"));
|
||||||
|
($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub fn _print(args: fmt::Arguments) {
|
||||||
|
use core::fmt::Write;
|
||||||
|
SERIAL.lock().write_fmt(args).unwrap();
|
||||||
|
}
|
@ -9,6 +9,7 @@ mod panic;
|
|||||||
mod util;
|
mod util;
|
||||||
mod init;
|
mod init;
|
||||||
mod arch;
|
mod arch;
|
||||||
|
mod log;
|
||||||
|
|
||||||
use arch::cpu;
|
use arch::cpu;
|
||||||
|
|
||||||
@ -22,6 +23,8 @@ pub extern "C" fn _start() -> ! {
|
|||||||
|
|
||||||
video::clear(Color::White);
|
video::clear(Color::White);
|
||||||
|
|
||||||
|
println!("Hello, world!");
|
||||||
|
|
||||||
cpu::halt();
|
cpu::halt();
|
||||||
|
|
||||||
}
|
}
|
@ -1,6 +1,9 @@
|
|||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
|
use crate::println;
|
||||||
|
|
||||||
#[panic_handler]
|
#[panic_handler]
|
||||||
fn panic(_info: &PanicInfo) -> ! {
|
fn panic(info: &PanicInfo) -> ! {
|
||||||
|
println!("Kernel {}", info);
|
||||||
loop {}
|
loop {}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user