strat9_kernel/arch/x86_64/vga/
debug_overlay.rs1use super::panic_screen::{
28 panic_pack_rgb, PANIC_FB_ADDR, PANIC_FB_FORMAT, PANIC_FB_PITCH, PANIC_FONT_8X16,
29};
30
31static VGA_DEBUG_COL: core::sync::atomic::AtomicUsize = core::sync::atomic::AtomicUsize::new(0);
36static VGA_DEBUG_ROW: core::sync::atomic::AtomicUsize = core::sync::atomic::AtomicUsize::new(0);
38
39static VGA_DEBUG_MAX_COL: core::sync::atomic::AtomicUsize = core::sync::atomic::AtomicUsize::new(0);
41static VGA_DEBUG_MAX_ROW: core::sync::atomic::AtomicUsize = core::sync::atomic::AtomicUsize::new(0);
43
44static VGA_DEBUG_LOCK: spin::Mutex<()> = spin::Mutex::new(());
46
47pub fn vga_debug_init(fb_width: usize, fb_height: usize) {
50 let cols = fb_width / 8; let rows = fb_height / 16; let status_rows = 1; let hud_px = super::boot_progress::band_height(fb_height, 16);
55 let hud_rows = hud_px.div_ceil(16);
56 VGA_DEBUG_MAX_COL.store(cols, core::sync::atomic::Ordering::Relaxed);
57 VGA_DEBUG_MAX_ROW.store(
58 rows.saturating_sub(status_rows + hud_rows),
59 core::sync::atomic::Ordering::Relaxed,
60 );
61 VGA_DEBUG_COL.store(0, core::sync::atomic::Ordering::Relaxed);
62 VGA_DEBUG_ROW.store(0, core::sync::atomic::Ordering::Relaxed);
63}
64
65fn vga_debug_scroll() {
70 let fb_addr = PANIC_FB_ADDR.load(core::sync::atomic::Ordering::Acquire);
71 if fb_addr == 0 {
72 return;
73 }
74 let pitch = PANIC_FB_PITCH.load(core::sync::atomic::Ordering::Relaxed) as usize;
75 let max_row = VGA_DEBUG_MAX_ROW.load(core::sync::atomic::Ordering::Relaxed);
76 let fmt = PANIC_FB_FORMAT.load(core::sync::atomic::Ordering::Relaxed);
77 let bpp = (fmt >> 32) as u16;
78 let bpp_bytes = (bpp / 8) as usize;
79 if bpp_bytes < 3 || pitch == 0 || max_row <= 1 {
80 return;
81 }
82
83 let fb = fb_addr as *mut u8;
84 let line_bytes = 16 * pitch; let total = (max_row - 1) * line_bytes; let bg = panic_pack_rgb(fmt, 0x12, 0x16, 0x1E);
87
88 unsafe {
89 super::fastfb::copy_forward(fb, fb.add(line_bytes), total);
94 let clear = fb.add((max_row - 1) * line_bytes);
96 let line_pixels = if bpp_bytes > 0 { pitch / bpp_bytes } else { 0 };
97 vga_debug_fill_line(clear, pitch, line_pixels, bpp_bytes, bg);
98 super::fastfb::fence();
99 }
100}
101
102unsafe fn vga_debug_fill_line(
104 base: *mut u8,
105 pitch: usize,
106 fill_pixels: usize,
107 bpp_bytes: usize,
108 color: u32,
109) {
110 for row in 0..16 {
111 super::fastfb::fill_pixels(base.add(row * pitch), bpp_bytes, color, fill_pixels);
112 }
113}
114
115unsafe fn vga_debug_clear_cell(
117 fb: *mut u8,
118 pitch: usize,
119 x: usize,
120 y: usize,
121 bpp_bytes: usize,
122 bg: u32,
123) {
124 for row in 0..16 {
125 super::fastfb::fill_pixels(fb.add((y + row) * pitch + x * bpp_bytes), bpp_bytes, bg, 8);
126 }
127}
128
129unsafe fn vga_debug_draw_glyph(
134 fb: *mut u8,
135 pitch: usize,
136 x: usize,
137 y: usize,
138 ch: u8,
139 fg: u32,
140 bg: u32,
141 bpp_bytes: usize,
142) {
143 vga_debug_clear_cell(fb, pitch, x, y, bpp_bytes, bg);
145
146 let idx = if ch < 0x20 || ch > 0x7E {
147 0usize
148 } else {
149 (ch - 0x20) as usize
150 };
151 let gs = idx * 16;
152
153 for row in 0..16 {
156 let mask = PANIC_FONT_8X16[gs + row];
157 if mask == 0 {
158 continue;
159 }
160 let mut colors = [bg; 8];
161 for col in 0..8 {
162 if (mask >> (7 - col)) & 1 != 0 {
163 colors[col] = fg;
164 }
165 }
166 super::fastfb::store_row(
167 fb.add((y + row) * pitch + x * bpp_bytes),
168 bpp_bytes,
169 &colors,
170 );
171 }
172}
173
174pub fn vga_debug_write(s: &str) {
183 let fb_addr = PANIC_FB_ADDR.load(core::sync::atomic::Ordering::Acquire);
184 if fb_addr == 0 {
185 return;
186 }
187 let Some(_guard) = VGA_DEBUG_LOCK.try_lock() else {
189 return;
190 };
191
192 let pitch = PANIC_FB_PITCH.load(core::sync::atomic::Ordering::Relaxed) as usize;
193 let max_col = VGA_DEBUG_MAX_COL.load(core::sync::atomic::Ordering::Relaxed);
194 let max_row = VGA_DEBUG_MAX_ROW.load(core::sync::atomic::Ordering::Relaxed);
195 let fmt = PANIC_FB_FORMAT.load(core::sync::atomic::Ordering::Relaxed);
196 let bpp = (fmt >> 32) as u16;
197 let bpp_bytes = (bpp / 8) as usize;
198 if max_col == 0 || max_row == 0 || pitch == 0 || bpp_bytes < 3 {
199 return;
200 }
201
202 let fb = fb_addr as *mut u8;
203
204 let fg = panic_pack_rgb(fmt, 0xE2, 0xE8, 0xF0);
206 let bg = panic_pack_rgb(fmt, 0x12, 0x16, 0x1E);
207
208 let mut col = VGA_DEBUG_COL.load(core::sync::atomic::Ordering::Relaxed);
209 let mut row = VGA_DEBUG_ROW.load(core::sync::atomic::Ordering::Relaxed);
210
211 for &byte in s.as_bytes() {
212 match byte {
213 b'\n' => {
214 col = 0;
215 row += 1;
216 if row >= max_row {
217 vga_debug_scroll();
218 row = max_row - 1;
219 }
220 }
221 b'\r' => {
222 col = 0;
223 }
224 b'\t' => {
225 col = (col + 8) & !7;
226 if col >= max_col {
227 col = 0;
228 row += 1;
229 }
230 if row >= max_row {
231 vga_debug_scroll();
232 row = max_row - 1;
233 }
234 }
235 _ => {
236 if col >= max_col {
237 col = 0;
238 row += 1;
239 if row >= max_row {
240 vga_debug_scroll();
241 row = max_row - 1;
242 }
243 }
244 unsafe {
246 vga_debug_draw_glyph(fb, pitch, col * 8, row * 16, byte, fg, bg, bpp_bytes);
247 }
248 col += 1;
249 }
250 }
251 }
252
253 VGA_DEBUG_COL.store(col, core::sync::atomic::Ordering::Relaxed);
254 VGA_DEBUG_ROW.store(row, core::sync::atomic::Ordering::Relaxed);
255}
256
257pub fn vga_debug_writeln(s: &str) {
259 let mut buf: [u8; 512] = [0u8; 512];
260 let max = buf.len() - 1;
261 let len = if s.len() > max {
262 let mut truncated = max;
264 let bytes = s.as_bytes();
265 while truncated > 0 && (bytes[truncated] & 0xC0) == 0x80 {
266 truncated -= 1;
267 }
268 truncated
269 } else {
270 s.len()
271 };
272 buf[..len].copy_from_slice(&s.as_bytes()[..len]);
273 buf[len] = b'\n';
274 if let Ok(valid) = core::str::from_utf8(&buf[..len + 1]) {
275 vga_debug_write(valid);
276 }
277}