diff options
| author | Tianhao Wang <wth@riseup.net> | 2024-01-30 02:33:26 +0100 |
|---|---|---|
| committer | Tianhao Wang <wth@riseup.net> | 2024-01-30 02:33:26 +0100 |
| commit | 5232a899ca7537a4493a7fbd1bd8e03287ff1e26 (patch) | |
| tree | 87ded44101daa8832ad264996bde74160ab2db93 /src | |
| parent | 553657a0b527ae8ce09ac628da6122fbdd57157b (diff) | |
formatting code, use tab identation -- before it's too late
Diffstat (limited to 'src')
| -rw-r--r-- | src/arch/x86_64/io_port.rs | 37 | ||||
| -rw-r--r-- | src/arch/x86_64/misc.rs | 8 | ||||
| -rw-r--r-- | src/arch/x86_64/mod.rs | 4 | ||||
| -rw-r--r-- | src/arch/x86_64/pic.rs | 1 | ||||
| -rw-r--r-- | src/arch/x86_64/pit.rs | 1 | ||||
| -rw-r--r-- | src/arch/x86_64/plugbox.rs | 1 | ||||
| -rw-r--r-- | src/lib.rs | 26 | ||||
| -rw-r--r-- | src/machine/cgascr.rs | 378 | ||||
| -rw-r--r-- | src/machine/kbd_defs.rs | 56 | ||||
| -rw-r--r-- | src/machine/key.rs | 297 | ||||
| -rw-r--r-- | src/machine/keyctrl.rs | 60 | ||||
| -rw-r--r-- | src/machine/mod.rs | 6 |
12 files changed, 432 insertions, 443 deletions
diff --git a/src/arch/x86_64/io_port.rs b/src/arch/x86_64/io_port.rs index 4f629b8..f5755f4 100644 --- a/src/arch/x86_64/io_port.rs +++ b/src/arch/x86_64/io_port.rs @@ -1,36 +1,29 @@ extern "C" { - fn _inb(port:u16) -> u8; - fn _inw(port:u16) -> u16; - fn _outb(port:u16, val:u8); - fn _outw(port:u16, val:u16); + fn _inb(port: u16) -> u8; + fn _inw(port: u16) -> u16; + fn _outb(port: u16, val: u8); + fn _outw(port: u16, val: u16); } // The port addr is 16-bit wide. // wrappers for in/out[b,w] -// Also I don't feel necessary to have a IO_Port Class give how +// Also I don't feel necessary to have a IO_Port Class give how // trivial it is // TODO perhaps use inline asm, because the code is short -pub fn inw(p:u16) -> u16 { - unsafe{ - _inw(p) - } +pub fn inw(p: u16) -> u16 { + unsafe { _inw(p) } } -pub fn inb(p:u16) -> u8 { - unsafe{ - _inb(p) - } +pub fn inb(p: u16) -> u8 { + unsafe { _inb(p) } } -pub fn outb(p:u16, val:u8){ - unsafe{ - _outb(p,val); - } +pub fn outb(p: u16, val: u8) { + unsafe { + _outb(p, val); + } } -pub fn outw(p:u16, val:u16){ - unsafe{ - _outw(p,val) - } +pub fn outw(p: u16, val: u16) { + unsafe { _outw(p, val) } } - diff --git a/src/arch/x86_64/misc.rs b/src/arch/x86_64/misc.rs index c007d99..8a2c316 100644 --- a/src/arch/x86_64/misc.rs +++ b/src/arch/x86_64/misc.rs @@ -3,7 +3,7 @@ // asm code goes to asm/misc.s extern "C" { - fn _delay(); + fn _delay(); } #[inline(always)] @@ -11,7 +11,7 @@ extern "C" { // delays for several cycles. Used to fill sequantial IO commands // (for devices to react) pub fn delay() { - unsafe { - _delay(); - } + unsafe { + _delay(); + } } diff --git a/src/arch/x86_64/mod.rs b/src/arch/x86_64/mod.rs index f48c7fd..a7f9e89 100644 --- a/src/arch/x86_64/mod.rs +++ b/src/arch/x86_64/mod.rs @@ -1,6 +1,6 @@ +pub mod cpu; pub mod io_port; +pub mod misc; pub mod pic; pub mod pit; pub mod plugbox; -pub mod cpu; -pub mod misc; diff --git a/src/arch/x86_64/pic.rs b/src/arch/x86_64/pic.rs index e69de29..8b13789 100644 --- a/src/arch/x86_64/pic.rs +++ b/src/arch/x86_64/pic.rs @@ -0,0 +1 @@ + diff --git a/src/arch/x86_64/pit.rs b/src/arch/x86_64/pit.rs index e69de29..8b13789 100644 --- a/src/arch/x86_64/pit.rs +++ b/src/arch/x86_64/pit.rs @@ -0,0 +1 @@ + diff --git a/src/arch/x86_64/plugbox.rs b/src/arch/x86_64/plugbox.rs index e69de29..8b13789 100644 --- a/src/arch/x86_64/plugbox.rs +++ b/src/arch/x86_64/plugbox.rs @@ -0,0 +1 @@ + @@ -7,22 +7,22 @@ use machine::cgascr::CGAScreen; #[panic_handler] fn panic(_info: &PanicInfo) -> ! { - loop {} + loop {} } #[no_mangle] pub extern "C" fn _entry() -> ! { - let mut scr = CGAScreen::new(); - scr.show_coners(); - scr.setattr(0x1f); - scr.clear(); - scr.show_coners(); + let mut scr = CGAScreen::new(); + scr.show_coners(); + scr.setattr(0x1f); + scr.clear(); + scr.show_coners(); - scr.print("--RuStuBs--\n"); - scr.print(" _._ _,-'\"\"`-._ ~Meow\n"); - scr.print(" (,-.`._,'( |\\`-/|\n"); - scr.print(" `-.-' \\ )-`( , o o)\n"); - scr.print(" `- \\`_`\"'-\n"); - scr.print("it works!\n"); - loop {} + scr.print("--RuStuBs--\n"); + scr.print(" _._ _,-'\"\"`-._ ~Meow\n"); + scr.print(" (,-.`._,'( |\\`-/|\n"); + scr.print(" `-.-' \\ )-`( , o o)\n"); + scr.print(" `- \\`_`\"'-\n"); + scr.print("it works!\n"); + loop {} } diff --git a/src/machine/cgascr.rs b/src/machine/cgascr.rs index a529b8b..3fa7b95 100644 --- a/src/machine/cgascr.rs +++ b/src/machine/cgascr.rs @@ -1,6 +1,6 @@ use crate::arch::x86_64::io_port::*; use crate::arch::x86_64::misc::*; -use core::{slice,ptr,str}; +use core::{ptr, slice, str}; // I would consider these cga parameters constant. // the scroll() and clear() works with the assumption @@ -10,203 +10,201 @@ use core::{slice,ptr,str}; // holds // For each character, it takes 2 byte in the buffer // (one for char and one for attribute) -// Therefore the MAX_COLS should be a multiple of 4 -const MAX_COLS:usize = 80; -const MAX_ROWS:usize = 25; -const CGA_BUFFER_START:*mut u8 = 0xb8000 as *mut u8; -const CGA_BUFFER_BYTE_SIZE:usize = MAX_COLS * MAX_ROWS *2; - +// Therefore the MAX_COLS should be a multiple of 4 +const MAX_COLS: usize = 80; +const MAX_ROWS: usize = 25; +const CGA_BUFFER_START: *mut u8 = 0xb8000 as *mut u8; +const CGA_BUFFER_BYTE_SIZE: usize = MAX_COLS * MAX_ROWS * 2; // THESE TWO ARE USED TO DO BATCH OPERATIONS ON CGA BUFFER // MEMORY, HOPEFULLY MAKE IT FASTER. // I.E. SETTING 4 CHARACTERS AT ONCE. -const CGA_BUFFER_START_64:*mut u64 = 0xb8000 as *mut u64; -const CGA_BUFFER_QWORD_SIZE:usize = CGA_BUFFER_BYTE_SIZE / 8; -const CGA_BUFFER_QWORDS_PER_ROW:usize = MAX_COLS / 4; +const CGA_BUFFER_START_64: *mut u64 = 0xb8000 as *mut u64; +const CGA_BUFFER_QWORD_SIZE: usize = CGA_BUFFER_BYTE_SIZE / 8; +const CGA_BUFFER_QWORDS_PER_ROW: usize = MAX_COLS / 4; -const IR_PORT:u16 = 0x3d4; -const DR_PORT:u16 = 0x3d5; +const IR_PORT: u16 = 0x3d4; +const DR_PORT: u16 = 0x3d5; #[allow(dead_code)] -pub struct CGAScreen <'a>{ - pub cga_mem:&'a mut [u8], - cursor_r: usize, - cursor_c: usize, - attr: u8 +pub struct CGAScreen<'a> { + pub cga_mem: &'a mut [u8], + cursor_r: usize, + cursor_c: usize, + attr: u8, } #[allow(dead_code)] -impl <'a> CGAScreen<'a>{ - pub fn new() -> Self { - Self {cga_mem: unsafe{slice::from_raw_parts_mut(CGA_BUFFER_START, 2*MAX_COLS*MAX_ROWS)}, - cursor_r:0, - cursor_c:0, - attr:0x0f - } - } - - #[inline(always)] - fn cal_offset(&self,row:usize, col:usize) -> usize{ - col + row*MAX_COLS - } - - pub fn show(&self, row:usize, col:usize, c:char, attr:u8){ - let index = self.cal_offset(row, col); - - unsafe{ - *CGA_BUFFER_START.offset(index as isize * 2) = c as u8; - *CGA_BUFFER_START.offset(index as isize * 2 + 1) = attr; - } - } - - pub fn putchar(&mut self, ch:char){ - if ch == '\n'{ - self.cursor_r += 1; - self.cursor_c = 0; - self._check_scroll(); - }else{ - self.show(self.cursor_r, self.cursor_c,ch,self.attr); - self.cursor_c += 1; - if self.cursor_c>= MAX_COLS { - self.cursor_c = 0; - self.cursor_r += 1; - self._check_scroll(); - } - } - - self.setpos(self.cursor_r, self.cursor_c); - } - - fn _check_scroll(&mut self ){ - if self.cursor_r >= MAX_ROWS { - self.scroll(1); - self.cursor_r -= 1; - } - } - - pub fn scroll(&self, lines: u32) { - // TODO - // sanity check - if lines >= MAX_COLS as u32{ - self.clear(); - } - - if lines == 0 { - return; - } - // behold the magic ... (oh fuck me) - let mut i:usize = lines as usize - 1; - loop{ - if i == MAX_ROWS {break;} - let offset_src = (i*CGA_BUFFER_QWORDS_PER_ROW) as isize; - let offset_dist = offset_src - (lines*CGA_BUFFER_QWORDS_PER_ROW as u32) as isize; - unsafe{ - ptr::copy_nonoverlapping( - CGA_BUFFER_START_64.offset(offset_src), - CGA_BUFFER_START_64.offset(offset_dist), - CGA_BUFFER_QWORDS_PER_ROW); - } - i += 1; - } - - i = MAX_ROWS - lines as usize; - loop{ - if i == MAX_ROWS {break;} - self.clearline(i); - i += 1; - } - // clear the remaining lines: - } - - pub fn clear(&self){ - // remember to swap the endian.. - let b : u8 = self.attr; - let mut base:u64 = (b as u64) << 8; - base += base << 16; - base += base << 32; - - - for i in 0..CGA_BUFFER_QWORD_SIZE{ - unsafe{*CGA_BUFFER_START_64.offset(i as isize) = base} - } - } - - fn clearline(&self, line:usize){ - - let b : u8 = self.attr; - let mut base:u64 = (b as u64) << 8; - base += base << 16; - base += base << 32; - let start_offset_qw:isize = (line as isize) * CGA_BUFFER_QWORDS_PER_ROW as isize; - let end_offset_qw:isize = start_offset_qw + CGA_BUFFER_QWORDS_PER_ROW as isize; - unsafe{ - for i in start_offset_qw..end_offset_qw{ - *CGA_BUFFER_START_64.offset(i) = base; - } - } - - } - - - pub fn setpos(&mut self, row:usize, col:usize){ - // io ports for instruction register and data register - let offset = self.cal_offset(row,col); - // set lower byte - outb(IR_PORT, 15 as u8); - delay(); - outb(DR_PORT, offset as u8); - delay(); - // set higher byte - outb(IR_PORT, 14 as u8); - delay(); - outb(DR_PORT, (offset >> 8) as u8); - delay(); - self.cursor_r = row; - self.cursor_c = col; - } - - pub fn getpos_xy(&self, row:& mut u32, col:& mut u32){ - let offset = Self::getpos_offset(); - *row = offset % MAX_COLS as u32; - *col = offset / MAX_COLS as u32; - } - - #[allow(arithmetic_overflow)] - pub fn getpos_offset() -> u32 { - // read higher byte - outb(IR_PORT, 14 as u8); - let mut offset = inb(DR_PORT); - offset = offset << 8; - // read lower byte - outb(IR_PORT, 15 as u8); - offset += inb(DR_PORT); - offset as u32 - } - - - // Sanity Check of the cgascreen - pub fn test(&self){ - // TODO - - } - - pub fn show_coners(&self){ - self.show(0,0,0xda as char,self.attr); - self.show(0,79,0xbf as char,self.attr); - self.show(24,0,0xc0 as char,self.attr); - self.show(24,79,0xd9 as char,self.attr); - } - - pub fn print(&mut self, s:&str){ - for c in s.bytes(){ - self.putchar(c as char); - } - - } - - pub fn setattr(&mut self, attr:u8){ - self.attr = attr; - - } - +impl<'a> CGAScreen<'a> { + pub fn new() -> Self { + Self { + cga_mem: unsafe { + slice::from_raw_parts_mut(CGA_BUFFER_START, 2 * MAX_COLS * MAX_ROWS) + }, + cursor_r: 0, + cursor_c: 0, + attr: 0x0f, + } + } + + #[inline(always)] + fn cal_offset(&self, row: usize, col: usize) -> usize { + col + row * MAX_COLS + } + + pub fn show(&self, row: usize, col: usize, c: char, attr: u8) { + let index = self.cal_offset(row, col); + + unsafe { + *CGA_BUFFER_START.offset(index as isize * 2) = c as u8; + *CGA_BUFFER_START.offset(index as isize * 2 + 1) = attr; + } + } + + pub fn putchar(&mut self, ch: char) { + if ch == '\n' { + self.cursor_r += 1; + self.cursor_c = 0; + self._check_scroll(); + } else { + self.show(self.cursor_r, self.cursor_c, ch, self.attr); + self.cursor_c += 1; + if self.cursor_c >= MAX_COLS { + self.cursor_c = 0; + self.cursor_r += 1; + self._check_scroll(); + } + } + + self.setpos(self.cursor_r, self.cursor_c); + } + + fn _check_scroll(&mut self) { + if self.cursor_r >= MAX_ROWS { + self.scroll(1); + self.cursor_r -= 1; + } + } + + pub fn scroll(&self, lines: u32) { + // TODO + // sanity check + if lines >= MAX_COLS as u32 { + self.clear(); + } + + if lines == 0 { + return; + } + // behold the magic ... (oh fuck me) + let mut i: usize = lines as usize - 1; + loop { + if i == MAX_ROWS { + break; + } + let offset_src = (i * CGA_BUFFER_QWORDS_PER_ROW) as isize; + let offset_dist = offset_src - (lines * CGA_BUFFER_QWORDS_PER_ROW as u32) as isize; + unsafe { + ptr::copy_nonoverlapping( + CGA_BUFFER_START_64.offset(offset_src), + CGA_BUFFER_START_64.offset(offset_dist), + CGA_BUFFER_QWORDS_PER_ROW, + ); + } + i += 1; + } + + i = MAX_ROWS - lines as usize; + loop { + if i == MAX_ROWS { + break; + } + self.clearline(i); + i += 1; + } + // clear the remaining lines: + } + + pub fn clear(&self) { + // remember to swap the endian.. + let b: u8 = self.attr; + let mut base: u64 = (b as u64) << 8; + base += base << 16; + base += base << 32; + + for i in 0..CGA_BUFFER_QWORD_SIZE { + unsafe { *CGA_BUFFER_START_64.offset(i as isize) = base } + } + } + + fn clearline(&self, line: usize) { + let b: u8 = self.attr; + let mut base: u64 = (b as u64) << 8; + base += base << 16; + base += base << 32; + let start_offset_qw: isize = (line as isize) * CGA_BUFFER_QWORDS_PER_ROW as isize; + let end_offset_qw: isize = start_offset_qw + CGA_BUFFER_QWORDS_PER_ROW as isize; + unsafe { + for i in start_offset_qw..end_offset_qw { + *CGA_BUFFER_START_64.offset(i) = base; + } + } + } + + pub fn setpos(&mut self, row: usize, col: usize) { + // io ports for instruction register and data register + let offset = self.cal_offset(row, col); + // set lower byte + outb(IR_PORT, 15 as u8); + delay(); + outb(DR_PORT, offset as u8); + delay(); + // set higher byte + outb(IR_PORT, 14 as u8); + delay(); + outb(DR_PORT, (offset >> 8) as u8); + delay(); + self.cursor_r = row; + self.cursor_c = col; + } + + pub fn getpos_xy(&self, row: &mut u32, col: &mut u32) { + let offset = Self::getpos_offset(); + *row = offset % MAX_COLS as u32; + *col = offset / MAX_COLS as u32; + } + + #[allow(arithmetic_overflow)] + pub fn getpos_offset() -> u32 { + // read higher byte + outb(IR_PORT, 14 as u8); + let mut offset = inb(DR_PORT); + offset = offset << 8; + // read lower byte + outb(IR_PORT, 15 as u8); + offset += inb(DR_PORT); + offset as u32 + } + + // Sanity Check of the cgascreen + pub fn test(&self) { + // TODO + } + + pub fn show_coners(&self) { + self.show(0, 0, 0xda as char, self.attr); + self.show(0, 79, 0xbf as char, self.attr); + self.show(24, 0, 0xc0 as char, self.attr); + self.show(24, 79, 0xd9 as char, self.attr); + } + + pub fn print(&mut self, s: &str) { + for c in s.bytes() { + self.putchar(c as char); + } + } + + pub fn setattr(&mut self, attr: u8) { + self.attr = attr; + } } diff --git a/src/machine/kbd_defs.rs b/src/machine/kbd_defs.rs index 94026e6..a59d75a 100644 --- a/src/machine/kbd_defs.rs +++ b/src/machine/kbd_defs.rs @@ -2,54 +2,52 @@ use core::ffi::c_uchar; // bit masks for modifier keys pub enum Mbit { - Shift = 0b00000001, - AltLeft = 0b00000010, - AltRight = 0b00000100, - CtrlLeft = 0b00001000, - CtrlRight = 0b00010000, - CapsLock = 0b00100000, - NumLock = 0b01000000, - ScrollLock = 0b10000000, + Shift = 0b00000001, + AltLeft = 0b00000010, + AltRight = 0b00000100, + CtrlLeft = 0b00001000, + CtrlRight = 0b00010000, + CapsLock = 0b00100000, + NumLock = 0b01000000, + ScrollLock = 0b10000000, } // scan codes of a few specific keys pub enum Scan { - F1 = 0x3b, - Del = 0x53, - Up = 72, - Down = 80, - Left = 75, - Right = 77, - Div = 8, + F1 = 0x3b, + Del = 0x53, + Up = 72, + Down = 80, + Left = 75, + Right = 77, + Div = 8, } // Decoding tables ... this shit is so ugly, thanks to rust's strong typing system!!! // Also, this is a german layout keyboard // oh btw, the code translation is done by ChatGPT if it's wrong complain to the AI! const NORMAL_TAB: [c_uchar; 89] = [ - 0, 0, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 225, 39, 8, 0, 113, 119, 101, 114, 116, 122, 117, - 105, 111, 112, 129, 43, 10, 0, 97, 115, 100, 102, 103, 104, 106, 107, 108, 148, 132, 94, 0, 35, - 121, 120, 99, 118, 98, 110, 109, 44, 46, 45, 0, 42, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 45, 0, 0, 0, 43, 0, 0, 0, 0, 0, 0, 0, 60, 0, 0, + 0, 0, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 225, 39, 8, 0, 113, 119, 101, 114, 116, 122, 117, + 105, 111, 112, 129, 43, 10, 0, 97, 115, 100, 102, 103, 104, 106, 107, 108, 148, 132, 94, 0, 35, + 121, 120, 99, 118, 98, 110, 109, 44, 46, 45, 0, 42, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 45, 0, 0, 0, 43, 0, 0, 0, 0, 0, 0, 0, 60, 0, 0, ]; const SHIFT_TAB: [c_uchar; 89] = [ - 0, 0, 33, 34, 21, 36, 37, 38, 47, 40, 41, 61, 63, 96, 0, 0, 81, 87, 69, 82, 84, 90, 85, 73, 79, - 80, 154, 42, 0, 0, 65, 83, 68, 70, 71, 72, 74, 75, 76, 153, 142, 248, 0, 39, 89, 88, 67, 86, - 66, 78, 77, 59, 58, 95, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, + 0, 0, 33, 34, 21, 36, 37, 38, 47, 40, 41, 61, 63, 96, 0, 0, 81, 87, 69, 82, 84, 90, 85, 73, 79, + 80, 154, 42, 0, 0, 65, 83, 68, 70, 71, 72, 74, 75, 76, 153, 142, 248, 0, 39, 89, 88, 67, 86, + 66, 78, 77, 59, 58, 95, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, ]; const ALT_TAB: [c_uchar; 89] = [ - 0, 0, 0, 253, 0, 0, 0, 0, 123, 91, 93, 125, 92, 0, 0, 0, 64, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 126, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 230, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 124, 0, 0 + 0, 0, 0, 253, 0, 0, 0, 0, 123, 91, 93, 125, 92, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 126, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 230, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 124, 0, 0, ]; const ASC_NUM_TAB: [c_uchar; 13] = [55, 56, 57, 45, 52, 53, 54, 43, 49, 50, 51, 48, 44]; -const SCAN_NUM_TAB: [c_uchar; 13] = [8, 9, 10, 53, 5, 6, 7, 27, 2, 3, 4, 11, 51]; +const SCAN_NUM_TAB: [c_uchar; 13] = [8, 9, 10, 53, 5, 6, 7, 27, 2, 3, 4, 11, 51]; // I think constants are more handy than enum for these... // Keyboard controller commands diff --git a/src/machine/key.rs b/src/machine/key.rs index 3464d83..250b38d 100644 --- a/src/machine/key.rs +++ b/src/machine/key.rs @@ -2,166 +2,163 @@ use self::super::kbd_defs::*; use core::convert; pub struct Key { - asc: u8, - scan: u8, - modi: u8, - rawcode: u8, // this field not necessary, remove after testing + asc: u8, + scan: u8, + modi: u8, + rawcode: u8, // this field not necessary, remove after testing } // Not implementing // +operator char() // -impl convert::Into<char> for Key{ - fn into(self) -> char { - self.asc as char - } +impl convert::Into<char> for Key { + fn into(self) -> char { + self.asc as char + } } -impl convert::Into<u8> for Key{ - fn into(self) -> u8 { - self.asc - } +impl convert::Into<u8> for Key { + fn into(self) -> u8 { + self.asc + } } #[allow(dead_code)] impl Key { - pub fn new() -> Self { - Self { - asc: 0, - scan: 0, - modi: 0, - rawcode: 0, - } - } - - pub fn valid(self) -> bool { - self.scan != 0 - } - - pub fn invalidate(&mut self) { - self.scan = 0; - } - - pub fn set_raw(&mut self, code:u8) { - self.rawcode = code; - } - - pub fn get_raw(self) -> u8 { - self.rawcode - } - - // setter and getter for ascii and scancode - pub fn set_ascii(&mut self, ascii: u8) { - self.asc = ascii; - } - pub fn get_ascii(self) -> u8 { - self.asc - } - pub fn set_scancode(&mut self, scancode: u8) { - self.scan = scancode; - } - pub fn get_scancode(self) -> u8 { - self.scan - } - - // reading the state of SHIFT, ALT, CTRL etc. - pub fn shift(&self) -> bool { - self.modi & (Mbit::Shift as u8) != 0 - } - pub fn alt_left(&self) -> bool { - self.modi & (Mbit::AltLeft as u8) != 0 - } - pub fn alt_right(&self) -> bool { - self.modi & (Mbit::AltRight as u8) != 0 - } - pub fn ctrl_left(&self) -> bool { - self.modi & (Mbit::CtrlLeft as u8) != 0 - } - pub fn ctrl_right(&self) -> bool { - self.modi & (Mbit::CtrlRight as u8) != 0 - } - pub fn caps_lock(&self) -> bool { - self.modi & (Mbit::CapsLock as u8) != 0 - } - pub fn num_lock(&self) -> bool { - self.modi & (Mbit::NumLock as u8) != 0 - } - pub fn scroll_lock(&self) -> bool { - self.modi & (Mbit::ScrollLock as u8) != 0 - } - pub fn alt(&self) -> bool { - self.alt_left() | self.alt_right() - } - pub fn ctrl(&self) -> bool { - self.ctrl_left() | self.ctrl_right() - } - - // setting/clearing states of SHIFT, ALT, CTRL etc. - pub fn set_shift(&mut self, pressed: bool) { - self.modi = if pressed { - self.modi | Mbit::Shift as u8 - } else { - self.modi & !(Mbit::Shift as u8) - } - } - - pub fn set_alt_left(&mut self, pressed: bool) { - self.modi = if pressed { - self.modi | Mbit::AltLeft as u8 - } else { - self.modi & !(Mbit::AltLeft as u8) - } - } - - - pub fn set_alt_right(&mut self, pressed: bool) { - self.modi = if pressed { - self.modi | Mbit::AltRight as u8 - } else { - self.modi & !(Mbit::AltRight as u8) - } - } - - pub fn set_ctrl_left(&mut self, pressed: bool) { - self.modi = if pressed { - self.modi | Mbit::CtrlLeft as u8 - } else { - self.modi & !(Mbit::CtrlLeft as u8) - } - } - - pub fn set_ctrl_right(&mut self, pressed: bool) { - self.modi = if pressed { - self.modi | Mbit::CtrlRight as u8 - } else { - self.modi & !(Mbit::CtrlRight as u8) - } - } - - - pub fn set_caps_lock(&mut self, pressed: bool) { - self.modi = if pressed { - self.modi | Mbit::CapsLock as u8 - } else { - self.modi & !(Mbit::CapsLock as u8) - } - } - - - pub fn set_num_lock(&mut self, pressed: bool) { - self.modi = if pressed { - self.modi | Mbit::NumLock as u8 - } else { - self.modi & !(Mbit::NumLock as u8) - } - } - - pub fn set_scroll_lock(&mut self, pressed: bool) { - self.modi = if pressed { - self.modi | Mbit::ScrollLock as u8 - } else { - self.modi & !(Mbit::ScrollLock as u8) - } - } + pub fn new() -> Self { + Self { + asc: 0, + scan: 0, + modi: 0, + rawcode: 0, + } + } + + pub fn valid(self) -> bool { + self.scan != 0 + } + + pub fn invalidate(&mut self) { + self.scan = 0; + } + + pub fn set_raw(&mut self, code: u8) { + self.rawcode = code; + } + + pub fn get_raw(self) -> u8 { + self.rawcode + } + + // setter and getter for ascii and scancode + pub fn set_ascii(&mut self, ascii: u8) { + self.asc = ascii; + } + pub fn get_ascii(self) -> u8 { + self.asc + } + pub fn set_scancode(&mut self, scancode: u8) { + self.scan = scancode; + } + pub fn get_scancode(self) -> u8 { + self.scan + } + + // reading the state of SHIFT, ALT, CTRL etc. + pub fn shift(&self) -> bool { + self.modi & (Mbit::Shift as u8) != 0 + } + pub fn alt_left(&self) -> bool { + self.modi & (Mbit::AltLeft as u8) != 0 + } + pub fn alt_right(&self) -> bool { + self.modi & (Mbit::AltRight as u8) != 0 + } + pub fn ctrl_left(&self) -> bool { + self.modi & (Mbit::CtrlLeft as u8) != 0 + } + pub fn ctrl_right(&self) -> bool { + self.modi & (Mbit::CtrlRight as u8) != 0 + } + pub fn caps_lock(&self) -> bool { + self.modi & (Mbit::CapsLock as u8) != 0 + } + pub fn num_lock(&self) -> bool { + self.modi & (Mbit::NumLock as u8) != 0 + } + pub fn scroll_lock(&self) -> bool { + self.modi & (Mbit::ScrollLock as u8) != 0 + } + pub fn alt(&self) -> bool { + self.alt_left() | self.alt_right() + } + pub fn ctrl(&self) -> bool { + self.ctrl_left() | self.ctrl_right() + } + + // setting/clearing states of SHIFT, ALT, CTRL etc. + pub fn set_shift(&mut self, pressed: bool) { + self.modi = if pressed { + self.modi | Mbit::Shift as u8 + } else { + self.modi & !(Mbit::Shift as u8) + } + } + + pub fn set_alt_left(&mut self, pressed: bool) { + self.modi = if pressed { + self.modi | Mbit::AltLeft as u8 + } else { + self.modi & !(Mbit::AltLeft as u8) + } + } + + pub fn set_alt_right(&mut self, pressed: bool) { + self.modi = if pressed { + self.modi | Mbit::AltRight as u8 + } else { + self.modi & !(Mbit::AltRight as u8) + } + } + + pub fn set_ctrl_left(&mut self, pressed: bool) { + self.modi = if pressed { + self.modi | Mbit::CtrlLeft as u8 + } else { + self.modi & !(Mbit::CtrlLeft as u8) + } + } + + pub fn set_ctrl_right(&mut self, pressed: bool) { + self.modi = if pressed { + self.modi | Mbit::CtrlRight as u8 + } else { + self.modi & !(Mbit::CtrlRight as u8) + } + } + + pub fn set_caps_lock(&mut self, pressed: bool) { + self.modi = if pressed { + self.modi | Mbit::CapsLock as u8 + } else { + self.modi & !(Mbit::CapsLock as u8) + } + } + + pub fn set_num_lock(&mut self, pressed: bool) { + self.modi = if pressed { + self.modi | Mbit::NumLock as u8 + } else { + self.modi & !(Mbit::NumLock as u8) + } + } + + pub fn set_scroll_lock(&mut self, pressed: bool) { + self.modi = if pressed { + self.modi | Mbit::ScrollLock as u8 + } else { + self.modi & !(Mbit::ScrollLock as u8) + } + } } diff --git a/src/machine/keyctrl.rs b/src/machine/keyctrl.rs index 49ac486..14e7aa4 100644 --- a/src/machine/keyctrl.rs +++ b/src/machine/keyctrl.rs @@ -17,37 +17,37 @@ use crate::arch::x86_64::io_port::*; // set_led(char led,bool on) // set_repeat_rate(int speed,int delay) pub struct KeyboardController { - code: u8, - prefix: u8, - gather: Key, - leds: u8, - - // two ports for keyboard controller - ctrl_port: u16, - data_port: u16, - // status register bits + code: u8, + prefix: u8, + gather: Key, + leds: u8, + + // two ports for keyboard controller + ctrl_port: u16, + data_port: u16, + // status register bits } impl KeyboardController { - - pub fn new() -> Self { - Self { - code: 0, - prefix: 9, - gather: Key::new(), - leds: 0, - ctrl_port: 0x64, - data_port: 0x60, - } - } - - pub fn key_hit(&mut self) -> Key { - // for debugging only - let mut invalid:Key = Key::new(); - invalid.set_raw(0xff); - - let status = inb(self.ctrl_port); - - // TODO here - } + pub fn new() -> Self { + Self { + code: 0, + prefix: 9, + gather: Key::new(), + leds: 0, + ctrl_port: 0x64, + data_port: 0x60, + } + } + + pub fn key_hit(&mut self) -> Key { + todo!(); + // for debugging only + let mut invalid: Key = Key::new(); + invalid.set_raw(0xff); + + let status = inb(self.ctrl_port); + return Key::new(); + // TODO here + } } diff --git a/src/machine/mod.rs b/src/machine/mod.rs index f272509..70660ce 100644 --- a/src/machine/mod.rs +++ b/src/machine/mod.rs @@ -1,5 +1,5 @@ pub mod cgascr; -pub mod mem; -pub mod keyctrl; -pub mod key; mod kbd_defs; +pub mod key; +pub mod keyctrl; +pub mod mem; |
