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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
mod graphics_320x200x256;
mod graphics_320x240x256;
mod graphics_640x480x16;
mod text_40x25;
mod text_40x50;
mod text_80x25;
use super::{
colors::{Color16, TextModeColor},
drawing::Point,
registers::CrtcControllerIndex,
vga::{Vga, VGA},
};
use spinning_top::SpinlockGuard;
pub use graphics_320x200x256::Graphics320x200x256;
pub use graphics_320x240x256::Graphics320x240x256;
pub use graphics_640x480x16::Graphics640x480x16;
pub use text_40x25::Text40x25;
pub use text_40x50::Text40x50;
pub use text_80x25::Text80x25;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[repr(C)]
pub struct ScreenCharacter {
character: u8,
color: TextModeColor,
}
impl ScreenCharacter {
pub const fn new(character: u8, color: TextModeColor) -> ScreenCharacter {
ScreenCharacter { character, color }
}
pub fn get_character(self) -> u8 {
self.character
}
pub fn get_color(self) -> TextModeColor {
self.color
}
}
static BLANK_CHARACTER: ScreenCharacter = ScreenCharacter {
character: b' ',
color: TextModeColor::new(Color16::Yellow, Color16::Black),
};
pub trait Screen {
const WIDTH: usize;
const HEIGHT: usize;
const SIZE: usize;
}
pub trait TextWriter: Screen {
fn set_mode(&self);
fn get_frame_buffer(&self) -> (SpinlockGuard<Vga>, *mut ScreenCharacter) {
let mut vga = VGA.lock();
let frame_buffer = vga.get_frame_buffer();
(vga, u32::from(frame_buffer) as *mut ScreenCharacter)
}
fn clear_screen(&self) {
self.fill_screen(BLANK_CHARACTER);
}
fn fill_screen(&self, character: ScreenCharacter) {
let (_vga, frame_buffer) = self.get_frame_buffer();
for i in 0..Self::SIZE {
unsafe {
frame_buffer.add(i).write_volatile(character);
}
}
}
fn disable_cursor(&self) {
let (mut vga, _frame_buffer) = self.get_frame_buffer();
let emulation_mode = vga.get_emulation_mode();
let cursor_start = vga
.crtc_controller_registers
.read(emulation_mode, CrtcControllerIndex::TextCursorStart);
vga.crtc_controller_registers.write(
emulation_mode,
CrtcControllerIndex::TextCursorStart,
cursor_start | 0x20,
);
}
fn enable_cursor(&self) {
let (mut vga, _frame_buffer) = self.get_frame_buffer();
let emulation_mode = vga.get_emulation_mode();
let cursor_start = vga
.crtc_controller_registers
.read(emulation_mode, CrtcControllerIndex::TextCursorStart);
vga.crtc_controller_registers.write(
emulation_mode,
CrtcControllerIndex::TextCursorStart,
cursor_start & 0xDF,
);
}
fn read_character(&self, x: usize, y: usize) -> ScreenCharacter {
let (_vga, frame_buffer) = self.get_frame_buffer();
let offset = Self::WIDTH * y + x;
unsafe { frame_buffer.add(offset).read_volatile() }
}
fn set_cursor(&self, scan_line_start: u8, scan_line_end: u8) {
let (mut vga, _frame_buffer) = self.get_frame_buffer();
let emulation_mode = vga.get_emulation_mode();
let cursor_start = vga
.crtc_controller_registers
.read(emulation_mode, CrtcControllerIndex::TextCursorStart)
& 0xC0;
let cursor_end = vga
.crtc_controller_registers
.read(emulation_mode, CrtcControllerIndex::TextCursorEnd)
& 0xE0;
vga.crtc_controller_registers.write(
emulation_mode,
CrtcControllerIndex::TextCursorStart,
cursor_start | scan_line_start,
);
vga.crtc_controller_registers.write(
emulation_mode,
CrtcControllerIndex::TextCursorEnd,
cursor_end | scan_line_end,
);
}
fn set_cursor_position(&self, x: usize, y: usize) {
let offset = Self::WIDTH * y + x;
let (mut vga, _frame_buffer) = self.get_frame_buffer();
let emulation_mode = vga.get_emulation_mode();
let cursor_start = offset & 0xFF;
let cursor_end = (offset >> 8) & 0xFF;
vga.crtc_controller_registers.write(
emulation_mode,
CrtcControllerIndex::TextCursorLocationLow,
cursor_start as u8,
);
vga.crtc_controller_registers.write(
emulation_mode,
CrtcControllerIndex::TextCursorLocationHigh,
cursor_end as u8,
);
}
fn write_character(&self, x: usize, y: usize, screen_character: ScreenCharacter) {
let (_vga, frame_buffer) = self.get_frame_buffer();
let offset = Self::WIDTH * y + x;
unsafe {
frame_buffer.add(offset).write_volatile(screen_character);
}
}
}
pub trait GraphicsWriter<Color> {
fn clear_screen(&self, color: Color);
fn draw_line(&self, start: Point<isize>, end: Point<isize>, color: Color);
fn draw_character(&self, x: usize, y: usize, character: char, color: Color);
fn set_pixel(&self, x: usize, y: usize, color: Color);
fn set_mode(&self);
fn get_frame_buffer(&self) -> *mut u8 {
u32::from(VGA.lock().get_frame_buffer()) as *mut u8
}
}