Newer
Older
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use core::{fmt, slice};
#[derive(Clone, Copy)]
#[repr(packed)]
pub struct VgaTextBlock {
pub char: u8,
pub color: u8,
}
#[derive(Clone, Copy)]
#[repr(u8)]
pub enum VgaTextColor {
Black = 0,
Blue = 1,
Green = 2,
Cyan = 3,
Red = 4,
Purple = 5,
Brown = 6,
Gray = 7,
DarkGray = 8,
LightBlue = 9,
LightGreen = 10,
LightCyan = 11,
LightRed = 12,
LightPurple = 13,
Yellow = 14,
White = 15,
}
pub struct Vga {
pub blocks: &'static mut [VgaTextBlock],
pub width: usize,
pub height: usize,
pub x: usize,
pub y: usize,
pub bg: VgaTextColor,
pub fg: VgaTextColor,
}
impl Vga {
pub unsafe fn new(ptr: *mut VgaTextBlock, width: usize, height: usize) -> Self {
Self {
blocks: slice::from_raw_parts_mut(
ptr,
width * height
),
width,
height,
x: 0,
y: 0,
bg: VgaTextColor::DarkGray,
fg: VgaTextColor::White,
}
}
}
impl fmt::Write for Vga {
fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> {
for c in s.chars() {
if self.x >= self.width {
self.x = 0;
self.y += 1;
}
while self.y >= self.height {
for y in 1..self.height {
for x in 0..self.width {
let i = y * self.width + x;
let j = i - self.width;
self.blocks[j] = self.blocks[i];
if y + 1 == self.height {
self.blocks[i].char = 0;
}
}
}
self.y -= 1;
}
match c {
'\r' => {
self.x = 0;
},
'\n' => {
self.x = 0;
self.y += 1;
},
_ => {
let i = self.y * self.width + self.x;
if let Some(block) = self.blocks.get_mut(i) {
block.char = c as u8;
block.color =
((self.bg as u8) << 4) |
(self.fg as u8);
}
}
}
self.x += 1;
}
Ok(())
}
}