summaryrefslogtreecommitdiff
path: root/kernel/src/io/vga_text.rs
blob: e44d341e46194adc3558e5332b33e9d5d3410c91 (plain)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
use spin::{Mutex, MutexGuard};

pub const fn color_state_from_colors(fg: Color, bg: Color) -> u8 {
    fg as u8 | ((bg as u8) << 4)
}

const DEFAULT_COLOR_STATE: u8 = color_state_from_colors(Color::White, Color::Black);
const DEFAULT_VGA_TEXT_BUFFER: *mut VgaChar = 0xb8000 as *mut VgaChar;

/// A character of Code page 437 with a foreground and background color state.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct VgaChar(u16);

impl VgaChar {
    const NULL: Self = Self(0);

    const fn from_bytes(bytes: [u8; 2]) -> Self {
        Self(u16::from_ne_bytes(bytes))
    }

    pub const fn from_byte(byte: u8) -> Self {
        Self::from_bytes([byte, DEFAULT_COLOR_STATE])
    }

    pub const fn from_byte_with_color_state(byte: u8, color_state: u8) -> Self {
        Self::from_bytes([byte, color_state])
    }

    pub fn set_byte(&mut self, byte: u8) {
        let byte = u16::to_ne_bytes(self.0)[0];
        self.0 = u16::from_ne_bytes([byte, byte]);
    }

    pub fn set_color(&mut self, byte: u8) {
        let color = u16::to_ne_bytes(self.0)[1];
        self.0 = u16::from_ne_bytes([byte, color]);
    }
}

impl From<u8> for VgaChar {
    fn from(byte: u8) -> Self {
        Self::from_byte(byte)
    }
}

#[allow(dead_code)]
#[repr(u8)]
pub enum Color {
    Black = 0,
    Blue = 1,
    Green = 2,
    Cyan = 3,
    Red = 4,
    Magenta = 5,
    Brown = 6,
    LightGray = 7,
    DarkGray = 8,
    LightBlue = 9,
    LightGreen = 10,
    LightCyan = 11,
    LightRed = 12,
    Pink = 13,
    Yellow = 14,
    White = 15,
}

pub struct VgaTerminalController {
    width: usize,
    height: usize,
    area: usize,
    start: *mut VgaChar,
    cursor: Mutex<*mut VgaChar>,
}

unsafe impl Send for VgaTerminalController {}
unsafe impl Sync for VgaTerminalController {}

pub struct VgaTerminalControllerLock<'a> {
    color_state: u8,
    controller: &'a VgaTerminalController,
    lock: MutexGuard<'a, *mut VgaChar>,
}

pub static DEFAULT_VGA_TERMINAL_CONTROLLER: VgaTerminalController = unsafe {
    VgaTerminalController::new(
        core::num::NonZeroUsize::new_unchecked(80),
        core::num::NonZeroUsize::new_unchecked(25),
        DEFAULT_VGA_TEXT_BUFFER,
    )
};

impl VgaTerminalController {
    pub fn lock(&self) -> VgaTerminalControllerLock {
        VgaTerminalControllerLock {
            color_state: DEFAULT_COLOR_STATE,
            controller: self,
            lock: self.cursor.lock(),
        }
    }

    pub const fn width(&self) -> usize {
        self.width
    }

    pub const fn height(&self) -> usize {
        self.height
    }

    pub const fn area(&self) -> usize {
        self.area
    }

    pub const fn address(&self) -> *mut VgaChar {
        self.start
    }

    /// Creates a new VGA terminal controller with a width, height and an memory mapped I/O
    /// address to the VGA text buffer.
    ///
    /// # Safety
    /// The address must be a memory mapped I/O VGA text buffer with the given width and height.
    /// Otherwise UB is to be expected.
    pub const unsafe fn new(
        width: core::num::NonZeroUsize,
        height: core::num::NonZeroUsize,
        addr: *mut VgaChar,
    ) -> Self {
        let area = width.get() * height.get();
        Self {
            width: width.get(),
            height: height.get(),
            area,
            start: addr,
            cursor: Mutex::new(addr),
        }
    }
}

impl<'a> VgaTerminalControllerLock<'a> {
    pub const fn controller(&self) -> &'a VgaTerminalController {
        self.controller
    }

    #[inline]
    const fn start(&self) -> *mut VgaChar {
        self.controller.start
    }

    #[inline]
    const fn area(&self) -> usize {
        self.controller.area
    }

    pub const fn width(&self) -> usize {
        self.controller.width
    }

    pub const fn height(&self) -> usize {
        self.controller.height
    }

    fn move_up(&mut self, lines: usize) {
        let offset = lines * self.controller.width;
        assert!(offset <= self.area());
        unsafe {
            core::intrinsics::volatile_copy_memory(
                self.start(),
                self.start().offset(offset as isize),
                self.area() - offset,
            );
        }
        let pos = self.area() - offset;
        *self.lock = unsafe { self.start().offset(pos as isize) };
        unsafe { core::intrinsics::volatile_set_memory(*self.lock, 0u8, offset) }
    }

    pub fn set_color_state(&mut self, fg: Color, bg: Color) {
        self.color_state = color_state_from_colors(fg, bg);
    }

    pub fn clear(&mut self) {
        unsafe {
            core::intrinsics::volatile_set_memory(*self.lock, 0u8, self.area());
            *self.lock = self.start();
        }
    }

    pub fn get_coord(&self) -> isize {
        unsafe { self.lock.offset_from(self.start()) }
    }

    pub fn set_coord(&mut self, n: usize) {
        assert!(n < self.area());
        *self.lock = unsafe { self.start().offset(n as isize) };
    }

    pub fn put_vga_chars(&mut self, chars: &[VgaChar]) {
        if chars.len() >= self.area() {
            *self.lock = self.start();
            self.put_vga_chars(&chars[chars.len() - self.area()..]);
        } else {
            let end = self.get_coord() + chars.len() as isize;
            if end > self.area() as isize {
                self.move_up(
                    (end + self.controller.width as isize - 1 - self.area() as isize) as usize
                        / self.controller.width,
                );
            }
            unsafe {
                core::intrinsics::volatile_copy_memory(*self.lock, chars.as_ptr(), chars.len());
                *self.lock = self.lock.offset(chars.len() as isize);
            }
        }
    }

    pub fn put_const_byte_str<const N: usize>(&mut self, bytes: [u8; N]) {
        self.put_vga_chars(&bytes.map(|b| VgaChar::from_byte_with_color_state(b, self.color_state)))
    }

    pub fn put_byte_str(&mut self, bytes: &[u8]) {
        for &byte in bytes {
            self.put_vga_chars(&[VgaChar::from_byte_with_color_state(byte, self.color_state)]);
        }
    }

    pub fn put_arguments<'b>(&mut self, args: core::fmt::Arguments<'b>) {
        let _ = core::fmt::write(
            &mut WritableVgaTerminalControllerLock { controller: self },
            args,
        );
    }

    pub fn new_line(&mut self) {
        let line = self.get_coord() / self.controller.width as isize;
        let line = if line + 1 >= self.controller.height as isize {
            self.move_up(1);
            line
        } else {
            line + 1
        };
        *self.lock = unsafe { self.start().offset(line * self.controller.width as isize) };
    }

    pub fn new_lines(&mut self, n: usize) {
        // TODO: optimize
        for _ in 0..n {
            self.new_line()
        }
    }
}

struct WritableVgaTerminalControllerLock<'a, 'b> {
    controller: &'a mut VgaTerminalControllerLock<'b>,
}

impl<'a, 'b> core::fmt::Write for WritableVgaTerminalControllerLock<'a, 'b> {
    fn write_str(&mut self, s: &str) -> core::fmt::Result {
        Ok(self.controller.put_byte_str(s.as_bytes()))
    }
}

#[macro_export]
macro_rules! vga_width {
    () => {
        $crate::io::vga_text::DEFAULT_VGA_TERMINAL_CONTROLLER.width()
    };
}

#[macro_export]
macro_rules! vga_height {
    () => {
        $crate::io::vga_text::DEFAULT_VGA_TERMINAL_CONTROLLER.height()
    };
}

#[macro_export]
macro_rules! vga_lock {
    () => {
        $crate::io::vga_text::DEFAULT_VGA_TERMINAL_CONTROLLER.lock()
    };
}

#[macro_export]
macro_rules! vga_print {
    (fg: $fg:expr, bg: $bg:expr, $($arg:tt)*) => {{
        let mut lock = $crate::vga_lock!();
        lock.set_color_state($fg, $bg);
        lock.put_arguments(core::format_args!($($arg)*));
    }};
    ($($arg:tt)*) => {
        $crate::vga_lock!().put_arguments(core::format_args!($($arg)*))
    };
}

#[macro_export]
macro_rules! vga_println {
    () => { $crate::io::vga_text::DEFAULT_VGA_TERMINAL_CONTROLLER.lock().new_line() };
    (fg: $fg:expr, bg: $bg:expr, $($arg:tt)*) => {{
        let mut lock = $crate::vga_lock!();
        lock.set_color_state($fg, $bg);
        lock.put_arguments(core::format_args!($($arg)*));
        lock.new_line();
    }};
    ($($arg:tt)*) => {{
        let mut lock = $crate::vga_lock!();
        lock.put_arguments(core::format_args!($($arg)*));
        lock.new_line();
    }};
}

#[macro_export]
macro_rules! vga_clear {
    () => {
        $crate::vga_lock!().clear()
    };
    (fg: $fg:expr, bg: $bg:expr) => {{
        let mut lock = $crate::vga_lock!();
        lock.set_color_state($fg, $bg);
        lock.clear();
    }};
}