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
// LIBERTYOS: src/vgabuff.rs
//
// This module provides the ability for the kernel to write text to the VGA text-buffer,
// use colored text, provide required features for various macros to function correctly, print lines, create lines, etc.
//
// DEPRECATION STATUS:
// This module has been available since the beginning of the kernel's development, and there are no plans to deprecate this module.


use core::fmt;
use lazy_static::lazy_static;
use spin::Mutex;
use volatile::Volatile;


// This is a globally-available instance of "WRITER", which is used by the print!/println!
// macros. In addition to being used by the aforementioned macros, this instance can be used to print to the VGA text-buffer.
lazy_static!
{
	pub static ref WRITER: Mutex<Writer> = Mutex::new(Writer
	{
		colpos: 0,
		colorcode: ColorCode::new(Color::Red, Color::Black),
		buffer: unsafe
		{
			&mut *(0xb8000 as *mut Buffer)
		},
	});
}

// This provides the kernel with a simple color palette for VGA text.
// LightGray and DarkGray (numbers 7 and 8, respectively) have versions that are
// identical in purpose/functionality. The alternate versions in question use the Britsh form of the word "gray".

#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum Color
{
	Black = 0,
	Blue = 1,
	Green = 2,
	Cyan = 3,
	Red = 4,
	Magenta = 5,
	Brown = 6,

	LightGray = 7,
//	LightGrey = 7,

	DarkGray = 8,
//	DarkGrey = 8,

	LightBlue = 9,
	LightGreen = 10,
	LightCyan = 11,
	LightRed = 12,
	Pink = 13,
	Yellow = 14,
	White = 15,
}


#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(transparent)]
struct ColorCode(u8);
impl ColorCode
{
	// This creates a color-code, with a specific foreground and background colors.
	fn new(foreground: Color, background: Color) -> ColorCode
	{
		ColorCode((background as u8) << 4 | (foreground as u8))
	}
}


// A single screen character, consisting of an ASCII character and a color.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(C)]
struct ScreenChar
{
	asciichar: u8,
	colorcode: ColorCode,
}

// This sets the height of the text-buffer.
const BUFFH: usize = 25;
// This sets the width of the text-buffer.
const BUFFW: usize = 80;


// This structure represents the VGA text-buffer.
#[repr(transparent)]
struct Buffer
{
	 chars: [[Volatile<ScreenChar>; BUFFW]; BUFFH],
}


// This is a type that allows for writing ASCII bytes/strings to an existing "Buffer".
//
// Lines are wrapped when the width exceeds the length of BUFFW.
pub struct Writer
{
	colpos: usize,
	colorcode: ColorCode,
	buffer: &'static mut Buffer,
}

impl Writer
{
	// Write a single ASCII byte to the buffer.
	//
	// Lines are wrapped when the width of the buffer matches BUFFW.
	// Supports the '\n' character.

	pub fn writebyte(&mut self, byte: u8)
	{
		match byte
		{
			b'\n' => self.newln(),
			byte =>
			{
				if self.colpos >= BUFFW
				{
					self.newln();
				}

				let row = BUFFH - 1;
				let col = self.colpos;

				let colorcode = self.colorcode;
				self.buffer.chars[row][col].write(ScreenChar
				{
					asciichar: byte,
					colorcode,
				});
			}
		}
	}

	// Write an ASCII string to the buffer.
	//
	// Lines are wrapped when the width of the buffer matches the BUFFW.
	// Supports using the '\n' character.
	// Does NOT support using non-ASCII charcters (VGA text-buffer can only use ASCII).

	fn write_str(&mut self, s: &str)
	{
		for byte in s.bytes()
		{
			match byte
			{
				// Supported ASCII byte/newline ('\n')
				0x20..=0x7e | b'\n' => self.writebyte(byte),
				// Unsupported characters
				_ => self.writebyte(0xfe),
			}
		}
	}


	// Clears the last row, shifts all lines up by one.

	fn newln(&mut self)
	{
		for row in 1..BUFFH
		{
			for col in 0..BUFFW
			{
				let char = self.buffer.chars[row][col].read();
				self.buffer.chars[row - 1][col].write(char);
			}
		}
		self.clear_row(BUFFH - 1);
		self.colpos = 0;
	}


	// Clears a row by writing a row of blank characters to the buffer.
	fn clear_row(&mut self, row: usize)
	{
		let blank = ScreenChar
		{
			asciichar: b' ',
			colorcode: self.colorcode,
		};
		for col in 0..BUFFW
		{
			self.buffer.chars[row][col].write(blank);
		}
	}
}


impl fmt::Write for Writer
{
	fn write_str(&mut self, s: &str) -> fmt::Result
	{
		self.write_str(s);
		Ok(())
	}
}


// MACROS

// PRINT:
// This macro is almost identical to the print! macro (from the standard library), except this macro prints to the VGA text-buffer.
#[macro_export]
macro_rules! print
{
	($($arg:tt)*) => ($crate::vgabuff::_print(format_args!($($arg)*)));
}


// PRINTLN:
// This macro is almost identical to the println! macro (from the standard library), except this macro prints the VGA text-buffer.
#[macro_export]
macro_rules! println
{
	() => ($crate::print!("\n"));
	($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
}


// This function prints the provided string to the VGA text-buffer, using the WRITE instance.
#[doc(hidden)]
pub fn _print(args: fmt::Arguments)
{
	use core::fmt::Write;
	use x86_64::instructions::interrupts;
	interrupts::without_interrupts(|| { WRITER.lock().write_fmt(args).unwrap(); });
}


// TESTING
//
//
//
// TEST-CASE #1: test_println_simple
//
// This test-case prints a simple message to the VGA buffer.

#[test_case]
fn test_println_simple()
{
	println!("TEST_PRINTLN_SIMPLE OUTPUT");
}


// TEST-CASE #2: test_println_many
//
// This test-case prints many lines to the VGA buffer.

#[test_case]
fn test_println_many()
{
	for _ in 0..200
	{
		println!("TEST_PRINTLN_MANY OUTPUT");
	}
}


// TEST-CASE #3: test_println_output
//
// This test-case verifies that printed text is actually being printed onto the display.

#[test_case]
fn test_println_output()
{
	use core::fmt::Writer;
	use x86_64::instructions::interrupts;

	let s = "[INFO] TEST";
	interrupts::without_interrupts(||
	{
		let mut write = WRITER.lock();
		writeln!(write, "\n{}", s).expect("[ERR] WRITELN FAILURE");
		for (i, c) in s.chars().enumerate()
		{
			let screenchar = write.buffer.chars[BUFFH - 2][i].read();
			assert_eq!(char::from(screenchar.asciichar), c);
		}
	});
}