-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconsole.rs
More file actions
345 lines (317 loc) · 10.4 KB
/
Copy pathconsole.rs
File metadata and controls
345 lines (317 loc) · 10.4 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//! Code for handling a text-buffer.
// -----------------------------------------------------------------------------
// Licence Statement
// -----------------------------------------------------------------------------
// Copyright (c) Jonathan 'theJPster' Pallant and the Neotron Developers, 2023
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/>.
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Sub-modules
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Imports
// -----------------------------------------------------------------------------
use core::sync::atomic::{AtomicPtr, AtomicU16, AtomicU8, Ordering};
use neotron_common_bios::video::{
Attr, Glyph, GlyphAttr, TextBackgroundColour, TextForegroundColour,
};
// -----------------------------------------------------------------------------
// Types
// -----------------------------------------------------------------------------
/// Holds some data necessary to present a text console.
///
/// Used by Core 0 to control writes to a shared text-buffer.
pub struct TextConsole {
current_col: AtomicU16,
current_row: AtomicU16,
text_buffer: AtomicPtr<GlyphAttr>,
current_attr: AtomicU8,
}
impl TextConsole {
/// Create a TextConsole.
///
/// Has no buffer associated with it
pub const fn new() -> TextConsole {
TextConsole {
current_row: AtomicU16::new(0),
current_col: AtomicU16::new(0),
text_buffer: AtomicPtr::new(core::ptr::null_mut()),
// White on Black, with the default palette
current_attr: AtomicU8::new(
Attr::new(
TextForegroundColour::White,
TextBackgroundColour::Black,
false,
)
.0,
),
}
}
/// Update the text buffer we are using.
///
/// Will reset the cursor. The screen is not cleared.
pub unsafe fn set_text_buffer(&self, text_buffer: *mut GlyphAttr) {
self.text_buffer.store(text_buffer, Ordering::Relaxed)
}
/// Place a single Code Page 850 encoded 8-bit character on the screen.
///
/// Adjusts the current row and column automatically. Also understands
/// Carriage Return and New Line bytes.
pub fn write_font_glyph(&self, glyph: Glyph) {
// Load from global state
let mut row = self.current_row.load(Ordering::Relaxed);
let mut col = self.current_col.load(Ordering::Relaxed);
self.write_at(glyph, &mut row, &mut col);
// Push back to global state
self.current_row.store(row, Ordering::Relaxed);
self.current_col.store(col, Ordering::Relaxed);
}
/// Moves the text cursor to the specified row and column.
///
/// If a value is out of bounds, the cursor is not moved in that axis.
pub fn move_to(&self, row: u16, col: u16) {
let info = crate::vga::VIDEO_MODE.get_mode();
let num_rows = info.mode.text_height().unwrap_or(0);
let num_cols = info.mode.text_width().unwrap_or(0);
if row < num_rows {
self.current_row.store(row, Ordering::Relaxed);
}
if col < num_cols {
self.current_col.store(col, Ordering::Relaxed);
}
}
/// Convert a Unicode Scalar Value to a font glyph.
///
/// Zero-width and modifier Unicode Scalar Values (e.g. `U+0301 COMBINING,
/// ACCENT`) are not supported. Normalise your Unicode before calling
/// this function.
fn map_char_to_glyph(input: char) -> Glyph {
// This fixed table only works for the default font. When we support
// changing font, we will need to plug-in a different table for each font.
let index = match input {
'\u{0000}'..='\u{007F}' => input as u8,
'\u{00A0}' => 255, // NBSP
'\u{00A1}' => 173, // ¡
'\u{00A2}' => 189, // ¢
'\u{00A3}' => 156, // £
'\u{00A4}' => 207, // ¤
'\u{00A5}' => 190, // ¥
'\u{00A6}' => 221, // ¦
'\u{00A7}' => 245, // §
'\u{00A8}' => 249, // ¨
'\u{00A9}' => 184, // ©
'\u{00AA}' => 166, // ª
'\u{00AB}' => 174, // «
'\u{00AC}' => 170, // ¬
'\u{00AD}' => 240, // SHY
'\u{00AE}' => 169, // ®
'\u{00AF}' => 238, // ¯
'\u{00B0}' => 248, // °
'\u{00B1}' => 241, // ±
'\u{00B2}' => 253, // ²
'\u{00B3}' => 252, // ³
'\u{00B4}' => 239, // ´
'\u{00B5}' => 230, // µ
'\u{00B6}' => 244, // ¶
'\u{00B7}' => 250, // ·
'\u{00B8}' => 247, // ¸
'\u{00B9}' => 251, // ¹
'\u{00BA}' => 167, // º
'\u{00BB}' => 175, // »
'\u{00BC}' => 172, // ¼
'\u{00BD}' => 171, // ½
'\u{00BE}' => 243, // ¾
'\u{00BF}' => 168, // ¿
'\u{00C0}' => 183, // À
'\u{00C1}' => 181, // Á
'\u{00C2}' => 182, // Â
'\u{00C3}' => 199, // Ã
'\u{00C4}' => 142, // Ä
'\u{00C5}' => 143, // Å
'\u{00C6}' => 146, // Æ
'\u{00C7}' => 128, // Ç
'\u{00C8}' => 212, // È
'\u{00C9}' => 144, // É
'\u{00CA}' => 210, // Ê
'\u{00CB}' => 211, // Ë
'\u{00CC}' => 222, // Ì
'\u{00CD}' => 214, // Í
'\u{00CE}' => 215, // Î
'\u{00CF}' => 216, // Ï
'\u{00D0}' => 209, // Ð
'\u{00D1}' => 165, // Ñ
'\u{00D2}' => 227, // Ò
'\u{00D3}' => 224, // Ó
'\u{00D4}' => 226, // Ô
'\u{00D5}' => 229, // Õ
'\u{00D6}' => 153, // Ö
'\u{00D7}' => 158, // ×
'\u{00D8}' => 157, // Ø
'\u{00D9}' => 235, // Ù
'\u{00DA}' => 233, // Ú
'\u{00DB}' => 234, // Û
'\u{00DC}' => 154, // Ü
'\u{00DD}' => 237, // Ý
'\u{00DE}' => 232, // Þ
'\u{00DF}' => 225, // ß
'\u{00E0}' => 133, // à
'\u{00E1}' => 160, // á
'\u{00E2}' => 131, // â
'\u{00E3}' => 198, // ã
'\u{00E4}' => 132, // ä
'\u{00E5}' => 134, // å
'\u{00E6}' => 145, // æ
'\u{00E7}' => 135, // ç
'\u{00E8}' => 138, // è
'\u{00E9}' => 130, // é
'\u{00EA}' => 136, // ê
'\u{00EB}' => 137, // ë
'\u{00EC}' => 141, // ì
'\u{00ED}' => 161, // í
'\u{00EE}' => 140, // î
'\u{00EF}' => 139, // ï
'\u{00F0}' => 208, // ð
'\u{00F1}' => 164, // ñ
'\u{00F2}' => 149, // ò
'\u{00F3}' => 162, // ó
'\u{00F4}' => 147, // ô
'\u{00F5}' => 228, // õ
'\u{00F6}' => 148, // ö
'\u{00F7}' => 246, // ÷
'\u{00F8}' => 155, // ø
'\u{00F9}' => 151, // ù
'\u{00FA}' => 163, // ú
'\u{00FB}' => 150, // û
'\u{00FC}' => 129, // ü
'\u{00FD}' => 236, // ý
'\u{00FE}' => 231, // þ
'\u{00FF}' => 152, // ÿ
'\u{0131}' => 213, // ı
'\u{0192}' => 159, // ƒ
'\u{2017}' => 242, // ‗
'\u{2500}' => 196, // ─
'\u{2502}' => 179, // │
'\u{250C}' => 218, // ┌
'\u{2510}' => 191, // ┐
'\u{2514}' => 192, // └
'\u{2518}' => 217, // ┘
'\u{251C}' => 195, // ├
'\u{2524}' => 180, // ┤
'\u{252C}' => 194, // ┬
'\u{2534}' => 193, // ┴
'\u{253C}' => 197, // ┼
'\u{2550}' => 205, // ═
'\u{2551}' => 186, // ║
'\u{2554}' => 201, // ╔
'\u{2557}' => 187, // ╗
'\u{255A}' => 200, // ╚
'\u{255D}' => 188, // ╝
'\u{2560}' => 204, // ╠
'\u{2563}' => 185, // ╣
'\u{2566}' => 203, // ╦
'\u{2569}' => 202, // ╩
'\u{256C}' => 206, // ╬
'\u{2580}' => 223, // ▀
'\u{2584}' => 220, // ▄
'\u{2588}' => 219, // █
'\u{2591}' => 176, // ░
'\u{2592}' => 177, // ▒
'\u{2593}' => 178, // ▓
'\u{25A0}' => 254, // ■
_ => b'?',
};
Glyph(index)
}
/// Put a single character at a specified point on screen.
///
/// The character is relative to the current font, but newline and carriage
/// return will be interpreted appropriately. The given `row` and `col` are
/// updated.
fn write_at(&self, glyph: Glyph, row: &mut u16, col: &mut u16) {
let info = crate::vga::VIDEO_MODE.get_mode();
let buffer = info.ptr as *mut GlyphAttr;
let num_rows = info.mode.text_height().unwrap_or(0) as usize;
let num_cols = info.mode.text_width().unwrap_or(0) as usize;
let attr = Attr(self.current_attr.load(Ordering::Relaxed));
if glyph.0 == b'\r' {
*col = 0;
} else if glyph.0 == b'\n' {
*col = 0;
*row += 1;
} else {
let offset = (*col as usize) + (num_cols * (*row as usize));
// Note (safety): This is safe as we bound `col` and `row`
unsafe {
buffer
.add(offset)
.write_volatile(GlyphAttr::new(glyph, attr))
};
*col += 1;
}
if *col == (num_cols as u16) {
*col = 0;
*row += 1;
}
if *row == (num_rows as u16) {
// Stay on last line
*row = (num_rows - 1) as u16;
unsafe { core::ptr::copy(buffer.add(num_cols), buffer, num_cols * (num_rows - 1)) };
for blank_col in 0..num_cols {
let offset = blank_col + (num_cols * (*row as usize));
unsafe {
buffer
.add(offset)
.write_volatile(GlyphAttr::new(Glyph(b' '), attr))
};
}
}
}
/// Store a new attribute to be used for subsequent characters.
pub fn change_attr(&self, attr: Attr) {
let value = attr.0;
self.current_attr.store(value, Ordering::Relaxed);
}
}
impl Default for TextConsole {
fn default() -> Self {
Self::new()
}
}
unsafe impl Sync for TextConsole {}
impl core::fmt::Write for &TextConsole {
/// Allows us to call `writeln!(some_text_console, "hello")`
fn write_str(&mut self, s: &str) -> core::fmt::Result {
// Load from global state
let mut row = self.current_row.load(Ordering::Relaxed);
let mut col = self.current_col.load(Ordering::Relaxed);
for ch in s.chars() {
let b = TextConsole::map_char_to_glyph(ch);
self.write_at(b, &mut row, &mut col);
}
// Push back to global state
self.current_row.store(row, Ordering::Relaxed);
self.current_col.store(col, Ordering::Relaxed);
Ok(())
}
}
// -----------------------------------------------------------------------------
// Static and Const Data
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Functions
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// End of file
// -----------------------------------------------------------------------------