-
Notifications
You must be signed in to change notification settings - Fork 3
/
frame_buffer.rs
66 lines (58 loc) · 1.61 KB
/
frame_buffer.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use core::clone::Clone;
use core::marker::Copy;
use io;
const FB_COMMAND_PORT: u16 = 0x3d4;
const FB_DATA_PORT: u16 = 0x3d5;
const FB_HIGH_BYTE_COMMAND: u8 = 14;
const FB_LOW_BYTE_COMMAND: u8 = 15;
pub enum Color {
Black = 0,
Red = 4,
DarkGrey = 8,
LightRed = 12,
Blue = 1,
Magenta = 5,
LightBlue = 9,
LightMagenta = 13,
Green = 2,
Brown = 6,
LightGreen = 10,
LightBrown = 14,
Cyan = 3,
LightGrey = 7,
LightCyan = 11,
White = 15,
}
impl Copy for Color {}
impl Clone for Color {
fn clone(&self) -> Color {
return *self;
}
}
pub fn write_cell(position: usize, character: u8, background: Color, foreground: Color) {
unsafe {
*((0xb8000 + position * 2) as *mut u8) = character;
*((0xb8000 + position * 2 + 1) as *mut u8) = (background as u8) << 4 | foreground as u8;
}
}
pub fn cursor_position() -> usize {
io::outb(FB_COMMAND_PORT, FB_HIGH_BYTE_COMMAND);
let high = io::inb(FB_DATA_PORT);
io::outb(FB_COMMAND_PORT, FB_LOW_BYTE_COMMAND);
let low = io::inb(FB_DATA_PORT);
((high as usize) << 8) | low as usize
}
pub fn move_cursor(position: usize) {
io::outb(FB_COMMAND_PORT, FB_HIGH_BYTE_COMMAND);
io::outb(FB_DATA_PORT, ((position >> 8) & 0x0ff) as u8);
io::outb(FB_COMMAND_PORT, FB_LOW_BYTE_COMMAND);
io::outb(FB_DATA_PORT, (position & 0x0ff) as u8);
}
pub fn write(text: &str, background: Color, foreground: Color) {
let mut position = cursor_position();
for b in text.bytes() {
write_cell(position, b, background, foreground);
position += 1;
move_cursor(position);
}
}