From 1e278eebe3328163bc1f6f9f57874e4689fdc0a4 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 12 Feb 2018 17:49:39 +0100 Subject: [PATCH 1/6] colors at startup --- kernel-rs/src/lib.rs | 36 +++++++++++++++++++++--------------- kernel-rs/src/vga/buffer.rs | 4 ++-- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/kernel-rs/src/lib.rs b/kernel-rs/src/lib.rs index 1f0a7fe5..2ef02d66 100644 --- a/kernel-rs/src/lib.rs +++ b/kernel-rs/src/lib.rs @@ -16,6 +16,9 @@ mod vga; mod context; mod keyboard; +use context::CONTEXT; +use vga::{Color, ColorCode}; + #[allow(dead_code)] mod cpuio; @@ -34,21 +37,24 @@ pub extern fn kmain() -> ! { // use vga::color::Color; // use vga::color::ColorCode; - println!(r#" ,--, "#); - println!(r#" ,--.'| ,----, "#); - println!(r#" ,--, | : .' .' \ "#); - println!(r#",---.'| : ' ,----,' | "#); - println!(r#"; : | | ; | : . ; "#); - println!(r#"| | : _' | ; |.' / "#); - println!(r#": : |.' | `----'/ ; "#); - println!(r#"| ' ' ; : / ; / "#); - println!(r#"\ \ .'. | ; / /-, "#); - println!(r#" `---`: | ' / / /.`| "#); - println!(r#" ' ; |./__; : "#); - println!(r#" | : ;| : .' "#); - println!(r#" ' ,/ ; | .' "#); - println!(r#" '--' `---' "#); - println!(">> Kernel startup..."); + unsafe { CONTEXT.current_term().color_code = ColorCode::new(Color::White, Color::Cyan); } + print!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}\n", + format_args!("{: ^80}", r#" ,--, "#), + format_args!("{: ^80}", r#" ,--.'| ,----, "#), + format_args!("{: ^80}", r#" ,--, | : .' .' \ "#), + format_args!("{: ^80}", r#",---.'| : ' ,----,' | "#), + format_args!("{: ^80}", r#"; : | | ; | : . ; "#), + format_args!("{: ^80}", r#"| | : _' | ; |.' / "#), + format_args!("{: ^80}", r#": : |.' | `----'/ ; "#), + format_args!("{: ^80}", r#"| ' ' ; : / ; / "#), + format_args!("{: ^80}", r#"\ \ .'. | ; / /-, "#), + format_args!("{: ^80}", r#" `---`: | ' / / /.`| "#), + format_args!("{: ^80}", r#" ' ; |./__; : "#), + format_args!("{: ^80}", r#" | : ;| : .' "#), + format_args!("{: ^80}", r#" ' ,/ ; | .' "#), + format_args!("{: ^80}", r#" '--' `---' "#)); + unsafe { CONTEXT.current_term().color_code = ColorCode::new(Color::White, Color::Black); } + print!(">"); loop { keyboard::kbd_callback(); diff --git a/kernel-rs/src/vga/buffer.rs b/kernel-rs/src/vga/buffer.rs index 9ead4ce9..5e9e6312 100644 --- a/kernel-rs/src/vga/buffer.rs +++ b/kernel-rs/src/vga/buffer.rs @@ -31,6 +31,7 @@ macro_rules! println { pub fn print(args: fmt::Arguments) { use core::fmt::Write; unsafe { CONTEXT.current_term().write_fmt(args).unwrap() }; + unsafe { CONTEXT.current_term().flush() }; } @@ -45,7 +46,7 @@ const BUFFER_COLS: usize = 80 * 2; pub struct Writer { pub position: usize, - color_code: ColorCode, + pub color_code: ColorCode, buffer: [u8; BUFFER_ROWS * BUFFER_COLS], } @@ -120,7 +121,6 @@ impl fmt::Write for Writer { for byte in s.bytes() { self.write_byte(byte) } - self.flush(); Ok(()) } } From bd86aae5368993516fccc033d8cc3b82052faeff Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 12 Feb 2018 18:00:15 +0100 Subject: [PATCH 2/6] lol --- kernel-rs/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel-rs/src/lib.rs b/kernel-rs/src/lib.rs index 2ef02d66..c1c05fcc 100644 --- a/kernel-rs/src/lib.rs +++ b/kernel-rs/src/lib.rs @@ -38,7 +38,7 @@ pub extern fn kmain() -> ! { // use vga::color::ColorCode; unsafe { CONTEXT.current_term().color_code = ColorCode::new(Color::White, Color::Cyan); } - print!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}\n", + print!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}", format_args!("{: ^80}", r#" ,--, "#), format_args!("{: ^80}", r#" ,--.'| ,----, "#), format_args!("{: ^80}", r#" ,--, | : .' .' \ "#), From 864d2dca4e8f931f0a8b6a5005b2e14a19d064f4 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 13 Feb 2018 09:38:25 +0100 Subject: [PATCH 3/6] cleanup --- kernel-rs/src/keyboard.rs | 75 +++++++++++++++++++-------------------- kernel-rs/src/lib.rs | 21 +++-------- 2 files changed, 40 insertions(+), 56 deletions(-) diff --git a/kernel-rs/src/keyboard.rs b/kernel-rs/src/keyboard.rs index 37f58647..f874aa8c 100644 --- a/kernel-rs/src/keyboard.rs +++ b/kernel-rs/src/keyboard.rs @@ -82,48 +82,45 @@ pub fn kbd_callback() { static mut CTRL: bool = false; static mut ALT: bool = false; // let terminal_two: vga::terminal::Terminal = vga::Screen::new(); - let control = unsafe { cpuio::inb(0x64) }; + let control = cpuio::inb(0x64); if (control & 1) == 1 { - let scancode = unsafe { cpuio::inb(0x60) }; + let scancode = cpuio::inb(0x60); let (is_release, scancode) = check_key_state(scancode); - //TODO implement logic to translate scancode->ascii unsafe {//TODO remove unsafe - match self::KEYMAP_US.get(scancode as usize) { - Some(b"\0\0") => { - match scancode { - 0x2A | 0x36 => {SHIFT = !is_release}, - 0x38 => {ALT = !is_release; println!("atl")}, - 0x1D => {CTRL = !is_release; println!("ctrl")}, - 0x0F if !is_release => { - CONTEXT.switch_term(); - CONTEXT.current_term().flush(); - }, - _ => {} - } - }, - // Some(b"2@") if ALT => { - // CONTEXT.switch_term(); - // CONTEXT.current_term().flush(); - // }, - // Some(b"1!") if CTRL && !is_release => { - // CONTEXT.switch_term(); - // CONTEXT.current_term().keypress('>' as u8); - // CONTEXT.current_term().flush(); - // }, - Some(ascii) if !is_release => { - let mut terminal = CONTEXT.current_term(); - if SHIFT { - terminal.keypress(ascii[1]); - } - else { - terminal.keypress(ascii[0]); - } - }, - Some(_) => {}, - None =>{}, - // None => println!("nokey ctrl {:x}", control), + match self::KEYMAP_US.get(scancode as usize) { + Some(b"\0\0") => { + match scancode { + 0x2A | 0x36 => {SHIFT = !is_release}, + 0x38 => {ALT = !is_release; println!("atl")}, + 0x1D => {CTRL = !is_release; println!("ctrl")}, + 0x0F if !is_release => { + CONTEXT.switch_term(); + CONTEXT.current_term().flush(); + }, + _ => {} + } + }, + // Some(b"2@") if ALT => { + // CONTEXT.switch_term(); + // CONTEXT.current_term().flush(); + // }, + // Some(b"1!") if CTRL && !is_release => { + // CONTEXT.switch_term(); + // CONTEXT.current_term().keypress('>' as u8); + // CONTEXT.current_term().flush(); + // }, + Some(ascii) if !is_release => { + let mut terminal = CONTEXT.current_term(); + if SHIFT { + terminal.keypress(ascii[1]); + } + else { + terminal.keypress(ascii[0]); + } + }, + Some(_) => {}, + None =>{}, + } } - } - // current_screen.flush(); } } diff --git a/kernel-rs/src/lib.rs b/kernel-rs/src/lib.rs index ce6f1035..3fe7cd67 100644 --- a/kernel-rs/src/lib.rs +++ b/kernel-rs/src/lib.rs @@ -55,10 +55,6 @@ fn shutdown() -> ! { } #[no_mangle] pub extern fn kmain() -> ! { - // use vga::VgaScreen; - // use vga::color::Color; - // use vga::color::ColorCode; - println!(r#" ,--, "#); println!(r#" ,--.'| ,----, "#); println!(r#" ,--, | : .' .' \ "#); @@ -78,15 +74,6 @@ pub extern fn kmain() -> ! { loop { keyboard::kbd_callback(); } - // let control = unsafe { cpuio::inb(0x64) }; - // if (control & 1) == 1 { - // let keycode = unsafe { cpuio::inb(0x60) }; - // match keyboard::KEY_CODE_TO_ASCII.get(keycode as usize) { - // Some(ascii) => print!("{}", *ascii as char), - // None =>{}, - // // None => println!("nokey ctrl {:x}", control), - // } - // } } #[lang = "eh_personality"] #[no_mangle] @@ -96,12 +83,12 @@ pub extern fn eh_personality() { #[lang = "panic_fmt"] #[no_mangle] pub extern fn panic_fmt( - // fmt: core::fmt::Arguments, file: &'static str, line: u32 + fmt: core::fmt::Arguments, file: &'static str, line: u32 ) -> ! { - // println!("PANIC: {}", fmt); - // println!("FILE: {}", file); - // println!("LINE: {}", line); + println!("PANIC: {}", fmt); + println!("FILE: {}", file); + println!("LINE: {}", line); loop {} } From d6423d5a78e080c530d56d98544de6476e848544 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 13 Feb 2018 09:40:26 +0100 Subject: [PATCH 4/6] cleanup --- kernel-rs/src/lib.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/kernel-rs/src/lib.rs b/kernel-rs/src/lib.rs index 3fe7cd67..66a72252 100644 --- a/kernel-rs/src/lib.rs +++ b/kernel-rs/src/lib.rs @@ -11,15 +11,12 @@ extern crate rlibc; #[macro_use] mod vga; -#[allow(dead_code)] -#[macro_use] mod context; mod keyboard; #[allow(dead_code)] mod cpuio; - //TODO implement ACPI to have such functionality /// Reboot the kernel /// From 6598cbe3eb13ec9ad61e4428ffd1a01288d1893d Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 13 Feb 2018 14:35:17 +0100 Subject: [PATCH 5/6] cursor is now the real VGA buffer cursor --- kernel-rs/src/vga/buffer.rs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/kernel-rs/src/vga/buffer.rs b/kernel-rs/src/vga/buffer.rs index 5e9e6312..d5491b1d 100644 --- a/kernel-rs/src/vga/buffer.rs +++ b/kernel-rs/src/vga/buffer.rs @@ -9,6 +9,7 @@ use super::{Color, ColorCode}; use ::context::CONTEXT; +use cpuio; #[derive(Debug, Clone, Copy)] #[repr(C)] @@ -34,13 +35,8 @@ pub fn print(args: fmt::Arguments) { unsafe { CONTEXT.current_term().flush() }; } - 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; @@ -70,8 +66,6 @@ impl Writer { match byte { b'\n' => { - //reset cursor - self.buffer[self.position + 1] = ColorCode::new(Color::White, Color::Black).0; let current_line = self.position / (BUFFER_COLS); self.position = (current_line + 1) * BUFFER_COLS; } @@ -86,10 +80,11 @@ impl Writer { self.scroll(); } - // cursor - self.buffer[self.position] = b' '; - self.buffer[self.position + 1] = ColorCode::new(Color::LightGray, Color::LightGray).0; - + let cursor_position = self.position / 2; + cpuio::outb(14, 0x3D4); + cpuio::outb((cursor_position >> 8) as u8, 0x3D5); + cpuio::outb(15, 0x3D4); + cpuio::outb((cursor_position >> 0) as u8 & 0x00ff, 0x3D5); } pub fn flush(&mut self) { From f6384015d62e30379e449b8abfd3d643097f7c28 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Thu, 15 Feb 2018 11:09:27 +0100 Subject: [PATCH 6/6] starting to convert vga buffer to a terminal --- kernel-rs/src/vga/buffer.rs | 43 ++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/kernel-rs/src/vga/buffer.rs b/kernel-rs/src/vga/buffer.rs index d5491b1d..c8aa0643 100644 --- a/kernel-rs/src/vga/buffer.rs +++ b/kernel-rs/src/vga/buffer.rs @@ -41,48 +41,70 @@ const BUFFER_ROWS: usize = 25; const BUFFER_COLS: usize = 80 * 2; pub struct Writer { - pub position: usize, + pub buffer_pos: usize, pub color_code: ColorCode, buffer: [u8; BUFFER_ROWS * BUFFER_COLS], + command: [u8; 10], + command_len: usize, } impl Writer { pub const fn new() -> Writer { Writer { - position: 0, + buffer_pos: 0, color_code: ColorCode::new(Color::White, Color::Black), buffer: [0; BUFFER_ROWS * BUFFER_COLS], + command: [0; 10], + command_len: 0, } } pub fn keypress(&mut self, ascii: u8) { - self.write_byte(ascii); + match ascii { + b'\n' => { + self.command_len = 0; + self.write_byte(b'\n'); + println!("{:?}", self.command.iter()); + self.write_byte(b'>'); + } + byte => { + if self.command_len >= 10 { return }; + + self.write_byte(byte); + self.command_len += 1; + } + } self.flush(); } pub fn write_byte(&mut self, byte: u8) { - let i = self.position; + let i = self.buffer_pos; match byte { b'\n' => { - let current_line = self.position / (BUFFER_COLS); - self.position = (current_line + 1) * BUFFER_COLS; + let current_line = self.buffer_pos / (BUFFER_COLS); + self.buffer_pos = (current_line + 1) * BUFFER_COLS; } byte => { self.buffer[i] = byte; self.buffer[i + 1] = self.color_code.0; - self.position += 2; + self.buffer_pos += 2; } } - if self.position >= self.buffer.len() { + if self.buffer_pos >= self.buffer.len() { self.scroll(); } + } - let cursor_position = self.position / 2; + fn flush_cursor(&self) + { + let cursor_position = self.buffer_pos / 2; + // 14 awaits the rightmost 8bits cpuio::outb(14, 0x3D4); cpuio::outb((cursor_position >> 8) as u8, 0x3D5); + // 15 awaits the leftmost 8bits cpuio::outb(15, 0x3D4); cpuio::outb((cursor_position >> 0) as u8 & 0x00ff, 0x3D5); } @@ -90,6 +112,7 @@ impl Writer { pub fn flush(&mut self) { let slice = unsafe { core::slice::from_raw_parts_mut(0xb8000 as *mut u8, 4000) }; slice.as_mut().clone_from_slice(&self.buffer); + self.flush_cursor(); } fn scroll(&mut self) { @@ -106,7 +129,7 @@ impl Writer { self.buffer[((BUFFER_ROWS - 1) * BUFFER_COLS) + (col * 2)] = b' '; } - self.position = (BUFFER_ROWS - 1) * BUFFER_COLS; + self.buffer_pos = (BUFFER_ROWS - 1) * BUFFER_COLS; } }