aboutsummaryrefslogtreecommitdiff
path: root/src/machine/cgascr.rs
diff options
context:
space:
mode:
authorTianhao Wang <shrik3@riseup.net>2023-03-14 03:16:54 +0100
committerTianhao Wang <shrik3@riseup.net>2023-03-14 03:16:54 +0100
commit4eae38a6ce9110a155715575adcf12bdf45703cb (patch)
tree407cc8f97eabca6d67cd6e83801d83b3a1542beb /src/machine/cgascr.rs
parentb6a2fe46752ce5198122bd427a36fdd8878fd05a (diff)
CGA cursor location
Diffstat (limited to 'src/machine/cgascr.rs')
-rw-r--r--src/machine/cgascr.rs51
1 files changed, 41 insertions, 10 deletions
diff --git a/src/machine/cgascr.rs b/src/machine/cgascr.rs
index 091c50d..2748e67 100644
--- a/src/machine/cgascr.rs
+++ b/src/machine/cgascr.rs
@@ -1,4 +1,8 @@
-const vga_buffer:*mut u8 = 0xb8000 as *mut u8;
+use crate::arch::x86_64::io_port::*;
+
+const CGA_BUFFER_START:*mut u8 = 0xb8000 as *mut u8;
+const IR_PORT:u16 = 0x3d4;
+const DR_PORT:u16 = 0x3d5;
#[allow(dead_code)]
pub struct CGAScreen{
@@ -13,7 +17,7 @@ impl CGAScreen{
}
- fn get_index(&self,row:u32, col:u32) -> u32{
+ fn cal_offset(&self,row:u32, col:u32) -> u32{
col + row*self.max_cows
}
@@ -21,21 +25,48 @@ impl CGAScreen{
// the memory by address.
// and since it's unsafe, it shouldn't be public
pub fn show(&self, row:u32, col:u32, c:char, attr:u8){
- let index = self.get_index(row, col);
+ let index = self.cal_offset(row, col);
unsafe{
- *vga_buffer.offset(index as isize * 2) = c as u8;
- *vga_buffer.offset(index as isize * 2 + 1) = attr;
+ *CGA_BUFFER_START.offset(index as isize * 2) = c as u8;
+ *CGA_BUFFER_START.offset(index as isize * 2 + 1) = attr;
}
}
- pub fn putchar(&self, ch:char){
-
- }
+ // pub fn putchar(&self, ch:char){
+ //
+ // }
- pub fn setpos(){}
- pub fn getpos(){}
+ pub fn setpos(&self, row:u32, col:u32){
+ // io ports for instruction register and data register
+ let offset = self.cal_offset(row,col);
+
+ // set lower byte
+ outb(IR_PORT, 15 as u8);
+ outb(DR_PORT, offset as u8);
+ // set higher byte
+ outb(IR_PORT, 14 as u8);
+ outb(DR_PORT, (offset >> 8) as u8);
+
+ }
+
+ pub fn getoffset() -> 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
+ }
+
+ pub fn getpos(&self, row:& mut u32, cow:& mut u32){
+ let offset = Self::getoffset();
+ *row = offset % self.max_cows;
+ *cow = offset / self.max_cows;
+ }
// Sanity Check of the cgascreen