From aa9e74299cdd1fce62ae4168af146805aa99e106 Mon Sep 17 00:00:00 2001 From: greg Date: Wed, 25 Nov 2015 02:02:03 -0800 Subject: [PATCH] Add cursor-moving functionality --- rust_experiments/src/lib.rs | 52 ++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/rust_experiments/src/lib.rs b/rust_experiments/src/lib.rs index 4725348..3386607 100644 --- a/rust_experiments/src/lib.rs +++ b/rust_experiments/src/lib.rs @@ -135,31 +135,42 @@ mod x86_asm { } fn timer_callback(count: u64) { - use vga_buffer::{Color, checkerboard}; - if count % 1000 == 0 { - checkerboard(Color::White); - } - if count % 1000 == 500 { - checkerboard(Color::LightCyan); + use vga_buffer::{Color, print_u32, checkerboard, clear, move_cursor}; + + if count < 10 { + clear(); } - vga_buffer::print_u32(count as u32, 0); + if count < 3000 { + + move_cursor((count % 80) as u8, 2); + + print_u32(count as u32, 20); + + } else { + if count % 1000 == 0 { + checkerboard(Color::White); + } + if count % 1000 == 500 { + checkerboard(Color::LightCyan); + } + move_cursor((count % 80) as u8, 2); + } + + print_u32(count as u32, 0); } #[no_mangle] pub extern fn rust_main() { use vga_buffer::{clear, checkerboard, Color}; - clear(); - checkerboard(Color::Red); + //clear(); + //checkerboard(Color::Red); loop { } } - - - // 123 |3,2,1 mod util { pub fn u32_to_chars(n: u32) -> [u8; 10] { @@ -307,6 +318,23 @@ mod vga_buffer { *(ptr as *mut _) = data; } } + + pub fn move_cursor(x: u8, y: u8) { + use x86_asm::{outb}; + const CURSOR_COMMAND_PORT: u16 = 0x3d4; + const CURSOR_DATA_PORT: u16 = 0x3d5; + + let position: u16 = (y as u16)*80 + (x as u16); + let low_bits = (position & 0xff) as u8; + let high_bits = (position >> 8) as u8; + + unsafe { + outb(CURSOR_COMMAND_PORT, 14); + outb(CURSOR_DATA_PORT, low_bits); + outb(CURSOR_COMMAND_PORT, 15); + outb(CURSOR_DATA_PORT, high_bits); + } + } } #[cfg(not(test))]