|
| 1 | +use core::ffi::c_void; |
| 2 | +use core::fmt::{Result, Write}; |
| 3 | +use core::mem::size_of; |
| 4 | +use core::ptr; |
| 5 | + |
| 6 | +use vitasdk_sys::{ |
| 7 | + sceDisplaySetFrameBuf, sceKernelAllocMemBlock, sceKernelFreeMemBlock, sceKernelGetMemBlockBase, |
| 8 | + SceDisplayFrameBuf, SceUID, SCE_DISPLAY_SETBUF_NEXTFRAME, |
| 9 | + SCE_KERNEL_MEMBLOCK_TYPE_USER_CDRAM_RW, |
| 10 | +}; |
| 11 | + |
| 12 | +use super::font::DEBUG_FONT; |
| 13 | + |
| 14 | +const SCREEN_WIDTH: usize = 960; |
| 15 | +const SCREEN_HEIGHT: usize = 544; |
| 16 | +const SCREEN_PIXEL_COUNT: usize = SCREEN_WIDTH * SCREEN_HEIGHT; |
| 17 | +const SCREEN_FB_WIDTH: usize = 960; |
| 18 | +const SCREEN_FB_SIZE: usize = 2 * 1024 * 1024; |
| 19 | +const SCREEN_TAB_SIZE: usize = 4; // Tab size in number of characters |
| 20 | +const SCREEN_TAB_W: usize = DEBUG_FONT.size_w * SCREEN_TAB_SIZE; |
| 21 | + |
| 22 | +const DEFAULT_FG: u32 = 0xFFFFFFFF; |
| 23 | +const DEFAULT_BG: u32 = 0xFF000000; |
| 24 | + |
| 25 | +pub struct DebugScreen { |
| 26 | + framebuffer: Framebuffer, |
| 27 | + coord_x: usize, |
| 28 | + coord_y: usize, |
| 29 | + color_fg: u32, |
| 30 | + color_bg: u32, |
| 31 | +} |
| 32 | + |
| 33 | +pub struct Framebuffer { |
| 34 | + buf: *mut u32, |
| 35 | + block_uid: SceUID, |
| 36 | +} |
| 37 | + |
| 38 | +impl Framebuffer { |
| 39 | + pub fn new() -> Framebuffer { |
| 40 | + // Allocate memory to use as display buffer |
| 41 | + let mut base: *mut c_void = ::core::ptr::null_mut(); |
| 42 | + let block_uid = unsafe { |
| 43 | + let block_uid: SceUID = sceKernelAllocMemBlock( |
| 44 | + b"display\0".as_ptr() as *const i8, |
| 45 | + SCE_KERNEL_MEMBLOCK_TYPE_USER_CDRAM_RW, |
| 46 | + SCREEN_FB_SIZE as u32, |
| 47 | + ::core::ptr::null_mut(), |
| 48 | + ); |
| 49 | + sceKernelGetMemBlockBase(block_uid, &mut base); |
| 50 | + block_uid |
| 51 | + }; |
| 52 | + Framebuffer { |
| 53 | + buf: base as *mut u32, |
| 54 | + block_uid, |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + pub fn set_display(&mut self) { |
| 59 | + // Sets buffer as current display frame |
| 60 | + let frame = SceDisplayFrameBuf { |
| 61 | + size: size_of::<SceDisplayFrameBuf>() as u32, |
| 62 | + base: self.buf as *mut c_void, |
| 63 | + pitch: SCREEN_FB_WIDTH as u32, |
| 64 | + pixelformat: 0, |
| 65 | + width: SCREEN_WIDTH as u32, |
| 66 | + height: SCREEN_HEIGHT as u32, |
| 67 | + }; |
| 68 | + unsafe { |
| 69 | + sceDisplaySetFrameBuf(&frame, SCE_DISPLAY_SETBUF_NEXTFRAME); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + #[allow(unused)] |
| 74 | + pub fn get(&self, index: usize) -> u32 { |
| 75 | + if index > SCREEN_PIXEL_COUNT { |
| 76 | + panic!("Invalid framebuffer index"); |
| 77 | + } |
| 78 | + unsafe { ptr::read_volatile(self.buf.offset(index.try_into().unwrap())) } |
| 79 | + } |
| 80 | + |
| 81 | + pub fn set(&mut self, index: usize, value: u32) { |
| 82 | + if index > SCREEN_PIXEL_COUNT { |
| 83 | + panic!("Invalid framebuffer index"); |
| 84 | + } |
| 85 | + unsafe { ptr::write_volatile(self.buf.offset(index.try_into().unwrap()), value) } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl Drop for Framebuffer { |
| 90 | + fn drop(&mut self) { |
| 91 | + let _error_code = unsafe { sceKernelFreeMemBlock(self.block_uid) }; |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +impl Write for DebugScreen { |
| 96 | + fn write_str(&mut self, s: &str) -> Result { |
| 97 | + self.puts(s.as_bytes()); |
| 98 | + Ok(()) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl DebugScreen { |
| 103 | + pub fn new() -> Self { |
| 104 | + let mut framebuffer = Framebuffer::new(); |
| 105 | + framebuffer.set_display(); |
| 106 | + Self { |
| 107 | + framebuffer, |
| 108 | + coord_x: 0, |
| 109 | + coord_y: 0, |
| 110 | + color_fg: DEFAULT_FG, |
| 111 | + color_bg: DEFAULT_BG, |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + #[allow(unused)] |
| 116 | + fn clear(&mut self, from_h: usize, to_h: usize, from_w: usize, to_w: usize) { |
| 117 | + for h in from_h..to_h { |
| 118 | + for w in from_w..to_w { |
| 119 | + self.framebuffer.set(h * SCREEN_FB_WIDTH + w, self.color_bg); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + fn puts(&mut self, text: &[u8]) { |
| 125 | + let bytes_per_glyph = DEBUG_FONT.width * DEBUG_FONT.height / 8; |
| 126 | + |
| 127 | + for &chr in text.iter() { |
| 128 | + if chr == b'\t' { |
| 129 | + self.coord_x += SCREEN_TAB_W - (self.coord_x % SCREEN_TAB_W); |
| 130 | + continue; |
| 131 | + } |
| 132 | + |
| 133 | + // Go to next line at the end of the current line |
| 134 | + if self.coord_x + DEBUG_FONT.width > SCREEN_WIDTH { |
| 135 | + self.coord_y += DEBUG_FONT.size_h; |
| 136 | + self.coord_x = 0; |
| 137 | + } |
| 138 | + |
| 139 | + // Go to screen top when at the bottom of the screen |
| 140 | + if self.coord_y + DEBUG_FONT.height > SCREEN_HEIGHT { |
| 141 | + self.coord_x = 0; |
| 142 | + self.coord_y = 0; |
| 143 | + } |
| 144 | + |
| 145 | + if chr == b'\n' { |
| 146 | + self.coord_x = 0; |
| 147 | + self.coord_y += DEBUG_FONT.size_h; |
| 148 | + continue; |
| 149 | + } else if chr == b'\r' { |
| 150 | + self.coord_x = 0; |
| 151 | + continue; |
| 152 | + } |
| 153 | + |
| 154 | + let current_offset = self.coord_x + self.coord_y * SCREEN_FB_WIDTH; |
| 155 | + let mut font = |
| 156 | + &DEBUG_FONT.glyphs[(chr - DEBUG_FONT.first) as usize * bytes_per_glyph..]; |
| 157 | + let mut mask = 1 << 7; |
| 158 | + |
| 159 | + for row in 0..DEBUG_FONT.height { |
| 160 | + for col in 0..DEBUG_FONT.width { |
| 161 | + if mask == 0 { |
| 162 | + font = &font[1..]; |
| 163 | + mask = 1 << 7; |
| 164 | + } |
| 165 | + |
| 166 | + self.framebuffer.set( |
| 167 | + current_offset + row * SCREEN_FB_WIDTH + col, |
| 168 | + if font[0] & mask == 0 { |
| 169 | + self.color_bg |
| 170 | + } else { |
| 171 | + self.color_fg |
| 172 | + }, |
| 173 | + ); |
| 174 | + |
| 175 | + mask >>= 1; |
| 176 | + } |
| 177 | + |
| 178 | + #[allow(clippy::reversed_empty_ranges)] |
| 179 | + for col in DEBUG_FONT.width..DEBUG_FONT.size_w { |
| 180 | + self.framebuffer |
| 181 | + .set(current_offset + row * SCREEN_FB_WIDTH + col, self.color_bg) |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + #[allow(clippy::reversed_empty_ranges)] |
| 186 | + for row in DEBUG_FONT.height..DEBUG_FONT.size_h { |
| 187 | + for col in 0..DEBUG_FONT.size_w { |
| 188 | + self.framebuffer |
| 189 | + .set(current_offset + row * SCREEN_FB_WIDTH + col, self.color_bg) |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + self.coord_x += DEBUG_FONT.size_w; |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments