|
| 1 | +use bootloader_api::info::{FrameBuffer, FrameBufferInfo, PixelFormat}; |
| 2 | +use core::fmt; |
| 3 | +use noto_sans_mono_bitmap::{RasterizedChar, get_raster}; |
| 4 | +use spin::Mutex; |
| 5 | + |
| 6 | +use crate::{framebuffer::font_constants::BACKUP_CHAR, userspace}; |
| 7 | + |
| 8 | +pub static WRITER: Mutex<Option<Writer>> = Mutex::new(None); |
| 9 | + |
| 10 | +const LINE_SPACING: usize = 2; |
| 11 | +const LETTER_SPACING: usize = 0; |
| 12 | +const BORDER_PADDING: usize = 1; |
| 13 | + |
| 14 | +mod font_constants { |
| 15 | + use noto_sans_mono_bitmap::{FontWeight, RasterHeight, get_raster_width}; |
| 16 | + |
| 17 | + pub const CHAR_RASTER_HEIGHT: RasterHeight = RasterHeight::Size16; |
| 18 | + |
| 19 | + pub const CHAR_RASTER_WIDTH: usize = get_raster_width(FontWeight::Regular, CHAR_RASTER_HEIGHT); |
| 20 | + |
| 21 | + pub const BACKUP_CHAR: char = '�'; |
| 22 | + |
| 23 | + pub const FONT_WEIGHT: FontWeight = FontWeight::Regular; |
| 24 | +} |
| 25 | + |
| 26 | +fn get_char_raster(character: char) -> RasterizedChar { |
| 27 | + fn get(character: char) -> Option<RasterizedChar> { |
| 28 | + get_raster( |
| 29 | + character, |
| 30 | + font_constants::FONT_WEIGHT, |
| 31 | + font_constants::CHAR_RASTER_HEIGHT, |
| 32 | + ) |
| 33 | + } |
| 34 | + |
| 35 | + get(character).unwrap_or_else(|| get(BACKUP_CHAR).expect("Backup char not found")) |
| 36 | +} |
| 37 | + |
| 38 | +pub fn init(framebuffer: FrameBuffer) { |
| 39 | + let mut writer = Writer { |
| 40 | + info: framebuffer.info(), |
| 41 | + buffer: framebuffer, |
| 42 | + x_position: 0, |
| 43 | + y_position: 0, |
| 44 | + }; |
| 45 | + writer.clear(); |
| 46 | + |
| 47 | + let mut global_writer = WRITER.try_lock().unwrap(); |
| 48 | + assert!(global_writer.is_none(), "Global writer must be None"); |
| 49 | + |
| 50 | + *global_writer = Some(writer); |
| 51 | +} |
| 52 | + |
| 53 | +pub struct Writer { |
| 54 | + buffer: FrameBuffer, |
| 55 | + info: FrameBufferInfo, |
| 56 | + x_position: usize, |
| 57 | + y_position: usize, |
| 58 | +} |
| 59 | + |
| 60 | +impl Writer { |
| 61 | + fn write_string(&mut self, str: &str) { |
| 62 | + for character in str.chars() { |
| 63 | + self.write_char(character); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + fn write_char(&mut self, character: char) { |
| 68 | + match character { |
| 69 | + '\n' => self.new_line(), |
| 70 | + '\r' => self.carriage_return(), |
| 71 | + character => { |
| 72 | + let updated_x_position = self.x_position + font_constants::CHAR_RASTER_WIDTH; |
| 73 | + |
| 74 | + if updated_x_position >= self.width() { |
| 75 | + self.new_line(); |
| 76 | + } |
| 77 | + |
| 78 | + let updated_y_position = |
| 79 | + self.y_position + font_constants::CHAR_RASTER_HEIGHT.val() + BORDER_PADDING; |
| 80 | + |
| 81 | + while updated_y_position >= self.height() { |
| 82 | + self.shift_lines_up(); |
| 83 | + } |
| 84 | + |
| 85 | + self.write_rendered_char(get_char_raster(character)); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + fn write_rendered_char(&mut self, rendered_char: RasterizedChar) { |
| 91 | + for (y, row) in rendered_char.raster().iter().enumerate() { |
| 92 | + for (x, byte) in row.iter().enumerate() { |
| 93 | + self.write_pixel(self.x_position + x, self.y_position + y, *byte); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + self.x_position += rendered_char.width() + LETTER_SPACING; |
| 98 | + } |
| 99 | + |
| 100 | + fn write_pixel(&mut self, x: usize, y: usize, intensity: u8) { |
| 101 | + let pixel_offset = y * self.info.stride + x; |
| 102 | + |
| 103 | + let color = match self.info.pixel_format { |
| 104 | + PixelFormat::Rgb => [intensity, intensity, intensity / 2, 0], |
| 105 | + PixelFormat::Bgr => [intensity / 2, intensity, intensity, 0], |
| 106 | + PixelFormat::U8 => [if intensity > 200 { 0xf } else { 0 }, 0, 0, 0], |
| 107 | + other => { |
| 108 | + self.info.pixel_format = PixelFormat::Rgb; |
| 109 | + panic!("pixel format {:?} not supported", other); |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + let bytes_per_pixel = self.info.bytes_per_pixel; |
| 114 | + let byte_offset = pixel_offset * bytes_per_pixel; |
| 115 | + |
| 116 | + unsafe { |
| 117 | + core::arch::asm!("mov r8, r9", in("r9") byte_offset); |
| 118 | + } |
| 119 | + |
| 120 | + self.buffer.buffer_mut()[byte_offset..(byte_offset + bytes_per_pixel)] |
| 121 | + .copy_from_slice(&color[..bytes_per_pixel]); |
| 122 | + |
| 123 | + // let _ = unsafe { ptr::read_volatile(&self.buffer.buffer_mut()[byte_offset]) }; |
| 124 | + } |
| 125 | + |
| 126 | + fn new_line(&mut self) { |
| 127 | + self.y_position += font_constants::CHAR_RASTER_HEIGHT.val() + LINE_SPACING; |
| 128 | + self.carriage_return(); |
| 129 | + } |
| 130 | + |
| 131 | + fn carriage_return(&mut self) { |
| 132 | + self.x_position = BORDER_PADDING; |
| 133 | + } |
| 134 | + |
| 135 | + pub fn clear(&mut self) { |
| 136 | + self.x_position = BORDER_PADDING; |
| 137 | + self.y_position = BORDER_PADDING; |
| 138 | + self.buffer.buffer_mut().fill(0); |
| 139 | + } |
| 140 | + |
| 141 | + fn shift_lines_up(&mut self) { |
| 142 | + let offset = self.info.stride * self.info.bytes_per_pixel * 8; |
| 143 | + |
| 144 | + self.buffer.buffer_mut().copy_within(offset.., 0); |
| 145 | + self.y_position += 8; |
| 146 | + } |
| 147 | + |
| 148 | + fn width(&self) -> usize { |
| 149 | + self.info.width |
| 150 | + } |
| 151 | + |
| 152 | + fn height(&self) -> usize { |
| 153 | + self.info.height |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +impl fmt::Write for Writer { |
| 158 | + fn write_str(&mut self, s: &str) -> fmt::Result { |
| 159 | + self.write_string(s); |
| 160 | + Ok(()) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +#[macro_export] |
| 165 | +macro_rules! print { |
| 166 | + ($($arg:tt)*) => ($crate::framebuffer::_print(format_args!($($arg)*))); |
| 167 | +} |
| 168 | + |
| 169 | +#[macro_export] |
| 170 | +macro_rules! println { |
| 171 | + () => ($crate::print("\n")); |
| 172 | + ($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*))); |
| 173 | +} |
| 174 | + |
| 175 | +#[doc(hidden)] |
| 176 | +pub fn _print(args: fmt::Arguments) { |
| 177 | + use core::fmt::Write; |
| 178 | + use x86_64::instructions::interrupts; |
| 179 | + |
| 180 | + let f = || { |
| 181 | + WRITER.lock().as_mut().unwrap().write_fmt(args).unwrap(); |
| 182 | + }; |
| 183 | + |
| 184 | + if !userspace::is_user_ring() { |
| 185 | + interrupts::without_interrupts(f); |
| 186 | + } else { |
| 187 | + f(); |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +#[test_case] |
| 192 | +fn test_println_simple() { |
| 193 | + println!("test_println_simple output"); |
| 194 | +} |
| 195 | + |
| 196 | +#[test_case] |
| 197 | +fn test_println_many() { |
| 198 | + for _ in 0..200 { |
| 199 | + println!("test_println_many output"); |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +#[test_case] |
| 204 | +fn test_println_output() { |
| 205 | + use core::fmt::Write; |
| 206 | + use x86_64::instructions::interrupts; |
| 207 | + |
| 208 | + let s = "tatakae"; |
| 209 | + |
| 210 | + interrupts::without_interrupts(|| { |
| 211 | + let mut writer = WRITER.lock(); |
| 212 | + writeln!(writer, "\n{}", s).expect("writeln failed"); |
| 213 | + for (i, c) in s.chars().enumerate() { |
| 214 | + let screen_char = writer.buffer.chars[BUFFER_HEIGHT - 2][i].read(); |
| 215 | + assert_eq!(char::from(screen_char.ascii_character), c); |
| 216 | + } |
| 217 | + }); |
| 218 | +} |
0 commit comments