summaryrefslogtreecommitdiff
path: root/kernel/src/io/vga_text.rs
blob: 72c4d85c6a9719c457195103fd8da8fbd00f9dcf (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
#[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 const WIDTH: usize = 80;
pub const HEIGHT: usize = 25;

#[derive(Clone, Copy)]
#[repr(C, packed)]
pub struct CharState(pub u8);

impl CharState {
    pub fn from_colors(fg: Color, bg: Color) -> Self {
        Self((fg as u8) | ((bg as u8) << 4))
    }

    pub fn set_fg(&mut self, fg: Color) {
        self.0 = (self.0 & (HEIGHT as u8) - 10) | (fg as u8)
    }

    pub fn set_bg(&mut self, bg: Color) {
        self.0 = (self.0 & 15) | ((bg as u8) << 4)
    }
}

#[derive(Clone, Copy)]
#[repr(C, packed)]
pub struct VgaChar {
    pub byte: u8,
    pub state: CharState,
}

impl VgaChar {
    pub fn from_state_and_byte(state: CharState, byte: u8) -> Self {
        Self { state, byte }
    }
}

pub struct OStream {
    pos: (usize, usize),
    cursor: *mut VgaChar,
    state: CharState,
    centered_mode: bool,
}

impl OStream {
    pub fn new() -> Self {
        Self {
            pos: (0, 0),
            cursor: Self::at(0),
            state: CharState::from_colors(Color::White, Color::Black),
            centered_mode: false,
        }
    }

    fn at(n: usize) -> *mut VgaChar {
        (0xb8000 + (n << 1)) as *mut VgaChar
    }

    fn compute_cursor(&mut self) {
        self.cursor = Self::at(self.pos.0 as usize + self.pos.1 as usize * WIDTH)
    }

    pub fn set_col(&mut self, col: usize) {
        self.pos.0 = core::cmp::min(col, WIDTH - 1);
        self.compute_cursor()
    }
    pub fn set_row(&mut self, row: usize) {
        self.pos.1 = core::cmp::min(row, HEIGHT - 1);
        self.compute_cursor()
    }

    pub fn set_cursor(&mut self, col: usize, row: usize) {
        self.pos = (
            core::cmp::min(col, WIDTH - 1),
            core::cmp::min(row, HEIGHT - 1),
        );
        self.compute_cursor()
    }

    pub fn set_char(&mut self, c: VgaChar) {
        unsafe { self.cursor.write_volatile(c) }
    }

    pub fn put_char(&mut self, c: VgaChar) {
        if c.byte == b'\n' {
            self.new_line();
        } else if self.pos.0 >= WIDTH {
            self.new_line();
            self.put_char(c);
        } else {
            self.set_char(c);
            self.cursor = self.cursor.wrapping_offset(1);
            self.pos.0 += 1;
        }
    }

    pub fn put_byte(&mut self, b: u8) {
        self.put_char(VgaChar::from_state_and_byte(self.state, b))
    }

    pub fn clear(&self) {
        let c = VgaChar::from_state_and_byte(self.state, b' ');
        for i in 0..(WIDTH * HEIGHT) {
            unsafe { Self::at(i).write_volatile(c) }
        }
    }

    pub fn set_centered(&mut self, b: bool) {
        self.centered_mode = b
    }

    pub fn new_line(&mut self) {
        if self.pos.1 >= HEIGHT - 1 {
            self.set_col(0);
            for i in 0..1920 {
                unsafe { Self::at(i).write_volatile(*Self::at(i + WIDTH)) }
            }
        } else {
            self.set_cursor(0, self.pos.1 + 1);
        }
    }

    pub fn set_state(&mut self, state: CharState) {
        self.state = state
    }

    pub fn put_bytes(&mut self, s: &[u8]) {
        for &b in s {
            self.put_byte(b)
        }
    }

    pub fn print(&mut self, s: &[u8]) {
        if self.centered_mode {
            self.print_centered(s)
        } else {
            self.put_bytes(s)
        }
    }

    pub fn print_centered(&mut self, s: &[u8]) {
        for line in s.split(|&c| c == b'\n') {
            if line.is_empty() {
                self.new_line()
            }
            for chunk in line.chunks(WIDTH) {
                self.set_col((WIDTH - chunk.len()) >> 1);
                self.put_bytes(chunk);
                self.new_line()
            }
        }
    }
}

impl core::fmt::Write for OStream {
    fn write_str(&mut self, s: &str) -> core::fmt::Result {
        Ok(self.print(s.as_bytes()))
    }
}