主文件中如果 mod port; 放在 main 函数后面则可以正常进入 文本模式,如果放在 main 函数前则无法进入文本模式 主文件
#![no_std]
#![feature(custom_attribute,asm)]
#![allow(improper_ctypes)]
//mod color;
mod kernel;
use kernel::Color;
use kernel::Size;
use kernel::Console;
use port::UnsafePort;
#[no_mangle]
pub unsafe fn main() {
let mut console = Console::new(Size::new(80, 25), Color::Black, Color::White);
console.println("Hello world!");
loop {
let a = UnsafePort::new(0x60).read();
console.put_char(Color::Black, Color::White, a);
}
}
mod port;
引入的文件
#![allow(dead_code)]
use kernel::unsafe_func::inb;
use kernel::unsafe_func::outb;
use kernel::unsafe_func::inw;
use kernel::unsafe_func::outw;
use kernel::unsafe_func::inl;
use kernel::unsafe_func::outl;
use core::marker::PhantomData;
pub trait InOut {
unsafe fn port_in(port: u32) -> Self;
unsafe fn port_out(port: u32, value: Self);
}
impl InOut for u8 {
unsafe fn port_in(port: u32) -> u8 { inb(port) }
unsafe fn port_out(port: u32, value: u8) { outb(value, port); }
}
impl InOut for u16 {
unsafe fn port_in(port: u32) -> u16 { inw(port) }
unsafe fn port_out(port: u32, value: u16) { outw(value, port); }
}
impl InOut for u32 {
unsafe fn port_in(port: u32) -> u32 { inl(port) }
unsafe fn port_out(port: u32, value: u32) { outl(value, port); }
}
pub struct UnsafePort<T: InOut> {
port: u32,
phantom: PhantomData<T>,
}
impl<T: InOut> UnsafePort<T> {
pub unsafe fn new(port: u32) -> UnsafePort<T> {
UnsafePort { port: port, phantom: PhantomData }
}
pub unsafe fn read(&mut self) -> T {
T::port_in(self.port)
}
pub unsafe fn write(&mut self, value: T) {
T::port_out(self.port, value);
}
}
1
共 1 条评论, 1 页
评论区
写评论GitHub demo 地址 https://github.com/duncup/dissOS