diff --git a/kernel-rs/src/context.rs b/kernel-rs/src/context.rs index cb61b08f..06ca9bb4 100644 --- a/kernel-rs/src/context.rs +++ b/kernel-rs/src/context.rs @@ -2,30 +2,19 @@ extern crate core; use vga; +pub static mut CONTEXT: Context = Context { + current_term: 0, + vga1: vga::Writer::new(), + vga2: vga::Writer::new(), +}; pub struct Context { pub current_term: u8, - pub vga1: vga::Writer<&'static mut [u8]>, - pub vga2: vga::Writer<&'static mut [u8]>, + pub vga1: vga::Writer, + pub vga2: vga::Writer, } impl Context { - pub fn new() -> Context { - let slice1 = unsafe { - core::slice::from_raw_parts_mut(0xb8000 as *mut u8, 4000) - }; - - let slice2 = unsafe { - core::slice::from_raw_parts_mut(0xb8000 as *mut u8, 4000) - }; - - Context { - current_term: 0, - vga1: vga::Writer::new(slice1), - vga2: vga::Writer::new(slice2), - } - } - pub fn switch_term(&mut self) { self.current_term = { if self.current_term == 0 { 1 } @@ -33,7 +22,7 @@ impl Context { }; } - pub fn current_term(&mut self) -> &mut vga::Writer<&'static mut [u8]>{ + pub fn current_term(&mut self) -> &mut vga::Writer{ if self.current_term == 0 { &mut self.vga1 } else { diff --git a/kernel-rs/src/keyboard.rs b/kernel-rs/src/keyboard.rs index 20a737c2..834c9ada 100644 --- a/kernel-rs/src/keyboard.rs +++ b/kernel-rs/src/keyboard.rs @@ -1,7 +1,7 @@ extern crate core; use cpuio; -use context; +use context::CONTEXT; // use vga::color::{Color, ColorCode}; const MAX_KEYS: usize = 59; @@ -77,7 +77,7 @@ fn check_key_state(key: u8) -> (bool, usize) { } } -pub fn kbd_callback(context: &mut context::Context) { +pub fn kbd_callback() { static mut SHIFT: bool = false; static mut CTRL: bool = false; static mut ALT: bool = false; @@ -95,28 +95,28 @@ pub fn kbd_callback(context: &mut context::Context) { 0x38 => {ALT = !is_release}, 0x1D => {CTRL = !is_release}, 0x0F => { - context.current_term().keypress('T' as u8); - context.switch_term(); - context.current_term().keypress('T' as u8); - context.current_term().flush(); - context.current_term().keypress('T' as u8); - context.current_term().keypress('T' as u8); - context.current_term().keypress('T' as u8); + CONTEXT.current_term().keypress('T' as u8); + CONTEXT.switch_term(); + CONTEXT.current_term().keypress('T' as u8); + CONTEXT.current_term().flush(); + CONTEXT.current_term().keypress('T' as u8); + CONTEXT.current_term().keypress('T' as u8); + CONTEXT.current_term().keypress('T' as u8); }, _ => {} } }, // Some(b"2@") if ALT => { - // context.switch_term(); - // context.current_term().flush(); + // CONTEXT.switch_term(); + // CONTEXT.current_term().flush(); // }, Some(b"1!") if CTRL => { - context.switch_term(); - context.current_term().keypress('>' as u8); - context.current_term().flush(); + CONTEXT.switch_term(); + CONTEXT.current_term().keypress('>' as u8); + CONTEXT.current_term().flush(); }, Some(ascii) if !is_release => { - let mut terminal = context.current_term(); + let mut terminal = CONTEXT.current_term(); if SHIFT { terminal.keypress(ascii[1]); } diff --git a/kernel-rs/src/lib.rs b/kernel-rs/src/lib.rs index 74432ce1..e85c053b 100644 --- a/kernel-rs/src/lib.rs +++ b/kernel-rs/src/lib.rs @@ -53,10 +53,8 @@ pub extern fn kmain() -> ! { // WRITER.lock().color_code = ColorCode::new(Color::White, Color::Black); // println!(">> Kernel startup..."); - let mut context = context::Context::new(); - loop { - keyboard::kbd_callback(&mut context); + keyboard::kbd_callback(); } } diff --git a/kernel-rs/src/vga/buffer.rs b/kernel-rs/src/vga/buffer.rs index 7461d9ca..eb946ea9 100644 --- a/kernel-rs/src/vga/buffer.rs +++ b/kernel-rs/src/vga/buffer.rs @@ -16,20 +16,41 @@ struct ScreenChar { color_code: ColorCode, } +// macro_rules! print { +// ($($arg:tt)*) => ({ +// $crate::vga_buffer::print(format_args!($($arg)*)); +// }); +// } + +// macro_rules! println { +// ($fmt:expr) => (print!(concat!($fmt, "\n"))); +// ($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*)); +// } + +// pub fn print(args: fmt::Arguments) { +// use core::fmt::Write; +// context.current_screen().write_fmt(args).unwrap(); +// } + + +extern crate core; + +// pub const unsafe fn vga_slice() -> &'static [u8] { +// unsafe { core::slice::from_raw_parts_mut(0xb8000 as *mut u8, 4000) } +// } + const BUFFER_ROWS: usize = 25; const BUFFER_COLS: usize = 80 * 2; -pub struct Writer> { +pub struct Writer { pub position: usize, color_code: ColorCode, - slice: T, buffer: [u8; BUFFER_ROWS * BUFFER_COLS], } -impl> Writer { - pub fn new(slice: T) -> Writer { +impl Writer { + pub const fn new() -> Writer { Writer { - slice, position: 0, color_code: ColorCode::new(Color::White, Color::Black), buffer: [0; BUFFER_ROWS * BUFFER_COLS], @@ -70,7 +91,8 @@ impl> Writer { } pub fn flush(&mut self) { - self.slice.as_mut().clone_from_slice(&self.buffer); + let slice = unsafe { core::slice::from_raw_parts_mut(0xb8000 as *mut u8, 4000) }; + slice.as_mut().clone_from_slice(&self.buffer); } fn scroll(&mut self) { @@ -92,7 +114,7 @@ impl> Writer { } use core::fmt; -impl> fmt::Write for Writer { +impl fmt::Write for Writer { fn write_str(&mut self, s: &str) -> ::core::fmt::Result { for byte in s.bytes() { self.write_byte(byte)