cleanup emulation layer to match hardware exactly

This commit is contained in:
bunnie 2025-02-28 16:36:06 +08:00
parent a21dd3572d
commit ebd9b6e823

View file

@ -37,23 +37,6 @@ impl Into<ColorNative> for Mono {
} }
} }
#[repr(usize)]
#[derive(Eq, PartialEq, Copy, Clone)]
enum BufferState {
A = 0,
B = 2048,
}
impl BufferState {
pub fn swap(self) -> Self { if self == BufferState::A { BufferState::B } else { BufferState::A } }
pub fn as_index(&self) -> usize {
match self {
BufferState::A => 0,
BufferState::B => 1,
}
}
}
/// The channel for the backend to communicate back to the main thread that it /// The channel for the backend to communicate back to the main thread that it
/// has claimed. /// has claimed.
pub struct MainThreadToken(mpsc::SyncSender<MinifbThread>); pub struct MainThreadToken(mpsc::SyncSender<MinifbThread>);
@ -111,9 +94,8 @@ struct XousKeyboardHandler {
pub struct Oled128x128 { pub struct Oled128x128 {
native_buffer: Arc<Mutex<Vec<u32>>>, //[u32; WIDTH * HEIGHT], native_buffer: Arc<Mutex<Vec<u32>>>, //[u32; WIDTH * HEIGHT],
// front and back buffers // front and back buffers
buffers: [[u32; COLUMN as usize * ROW as usize]; 2], buffer: [u32; WIDTH as usize * HEIGHT as usize / (core::mem::size_of::<u32>() * 8)],
srfb: [u32; COLUMN as usize * ROW as usize], stash: [u32; WIDTH as usize * HEIGHT as usize / (core::mem::size_of::<u32>() * 8)],
active_buffer: BufferState,
} }
impl<'a> Oled128x128 { impl<'a> Oled128x128 {
@ -132,33 +114,29 @@ impl<'a> Oled128x128 {
Self { Self {
native_buffer, native_buffer,
buffers: [[DARK_COLOUR; COLUMN as usize * ROW as usize]; 2], buffer: [0u32; WIDTH as usize * HEIGHT as usize / (core::mem::size_of::<u32>() * 8)],
srfb: [DARK_COLOUR; COLUMN as usize * ROW as usize], stash: [0u32; WIDTH as usize * HEIGHT as usize / (core::mem::size_of::<u32>() * 8)],
active_buffer: BufferState::A,
} }
} }
pub fn buffer_swap(&mut self) { self.active_buffer = self.active_buffer.swap(); } pub fn buffer_mut(&mut self) -> &mut ux_api::platform::FbRaw { &mut self.buffer }
pub fn buffer_mut(&mut self) -> &mut [u32] { &mut self.buffers[self.active_buffer.as_index()] } pub fn buffer(&self) -> &ux_api::platform::FbRaw { &self.buffer }
pub fn buffer(&self) -> &[u32] { &self.buffers[self.active_buffer.as_index()] }
pub fn screen_size(&self) -> Point { Point::new(WIDTH, LINES) } pub fn screen_size(&self) -> Point { Point::new(WIDTH, LINES) }
pub fn redraw(&mut self) { pub fn redraw(&mut self) { self.draw(); }
self.draw();
self.buffer_swap(); pub fn blit_screen(&mut self, bmp: &[u32]) { self.buffer.copy_from_slice(bmp); }
}
pub fn set_devboot(&mut self, _ena: bool) { pub fn set_devboot(&mut self, _ena: bool) {
unimplemented!("devboot feature does not exist on this platform"); unimplemented!("devboot feature does not exist on this platform");
} }
pub fn stash(&mut self) { self.srfb.copy_from_slice(&self.buffers[self.active_buffer.as_index()]); } pub fn stash(&mut self) { self.stash.copy_from_slice(&self.buffer); }
pub fn pop(&mut self) { pub fn pop(&mut self) {
self.buffers[self.active_buffer.as_index()].copy_from_slice(&self.srfb); self.buffer.copy_from_slice(&self.stash);
self.redraw(); self.redraw();
} }
@ -169,75 +147,65 @@ impl<'a> Oled128x128 {
} }
pub fn init(&mut self) {} pub fn init(&mut self) {}
pub fn blit_screen(&mut self, bmp: &[u32]) {
let buf = self.buffer_mut();
let mut temp = [0u32; COLUMN as usize * ROW as usize];
// this routine will paint the bitmap into temp flipped horizontally
for (i, &word) in bmp.iter().rev().enumerate() {
for bit in 0..32 {
let pixel_index = i * 32 + bit;
let is_white = (word >> bit) & 1 != 0;
temp[pixel_index] = if is_white { LIGHT_COLOUR } else { DARK_COLOUR };
}
}
// this flips it horizontally
for (src_line, dst_line) in temp.chunks(COLUMN as usize).zip(buf.chunks_mut(COLUMN as usize)) {
for (&s, d) in src_line.iter().rev().zip(dst_line.iter_mut()) {
*d = s;
}
}
}
} }
impl FrameBuffer for Oled128x128 { impl FrameBuffer for Oled128x128 {
/// Transfers the back buffer
fn draw(&mut self) { fn draw(&mut self) {
// this must be opposite of what `buffer` / `buffer_mut` returns for (index, pixel) in self.native_buffer.lock().unwrap().iter_mut().enumerate() {
let buffer = self.buffer(); *pixel =
let mut native_buffer = self.native_buffer.lock().unwrap(); if (self.buffer[index / 32] & (1 << (index % 32))) != 0 { DARK_COLOUR } else { LIGHT_COLOUR }
native_buffer.copy_from_slice(&buffer); }
} }
fn clear(&mut self) { self.buffer_mut().fill(DARK_COLOUR); } fn clear(&mut self) { self.buffer_mut().fill(0xFFFF_FFFF); }
fn put_pixel(&mut self, p: Point, on: ColorNative) { fn put_pixel(&mut self, p: Point, on: ColorNative) {
if p.x >= COLUMN || p.y >= ROW || p.x < 0 || p.y < 0 { if p.x >= COLUMN || p.y >= ROW || p.x < 0 || p.y < 0 {
return; return;
} }
let buffer = self.buffer_mut(); let bitnum = (p.x + p.y * COLUMN) as usize;
buffer[(p.x + (ROW - 1 - p.y) * COLUMN) as usize] = on.0 as u32; if on.0 != 0 {
self.buffer[bitnum / 32] |= 1 << (bitnum % 32);
} else {
self.buffer[bitnum / 32] &= !(1 << (bitnum % 32));
}
} }
fn dimensions(&self) -> Point { Point::new(COLUMN, ROW) } fn dimensions(&self) -> Point { Point::new(COLUMN, ROW) }
fn get_pixel(&mut self, p: Point) -> Option<ColorNative> { fn get_pixel(&self, p: Point) -> Option<ColorNative> {
if p.x >= COLUMN || p.y >= ROW || p.x < 0 || p.y < 0 { if p.x >= COLUMN || p.y >= ROW || p.x < 0 || p.y < 0 {
return None; return None;
} }
let buffer = self.buffer(); let bitnum = (p.x + p.y * COLUMN) as usize;
let color = ColorNative(buffer[(p.x + p.y * ROW) as usize] as usize); if self.buffer[bitnum / 32] & 1 << (bitnum % 32) != 0 {
Some(color) Some(Mono::White.into())
} else {
Some(Mono::Black.into())
}
} }
fn xor_pixel(&mut self, p: Point) { fn xor_pixel(&mut self, p: Point) {
if p.x >= COLUMN || p.y >= ROW || p.x < 0 || p.y < 0 { if p.x >= COLUMN || p.y >= ROW || p.x < 0 || p.y < 0 {
return; return;
} }
let buffer = self.buffer_mut(); let bitnum = (p.x + p.y * COLUMN) as usize;
let color: Mono = ColorNative(buffer[(p.x + p.y * ROW) as usize] as usize).into(); let flip: ColorNative = if self.buffer[bitnum / 32] & 1 << (bitnum % 32) != 0 {
let xor_color = if color == Mono::Black { Mono::White } else { Mono::Black }; Mono::Black.into()
let on: ColorNative = xor_color.into(); } else {
buffer[(p.x + (ROW - 1 - p.y) * COLUMN) as usize] = on.0 as u32; Mono::White.into()
};
if flip.0 != 0 {
self.buffer[bitnum / 32] |= 1 << (bitnum % 32);
} else {
self.buffer[bitnum / 32] &= !(1 << (bitnum % 32));
}
} }
/// This is highly unsafe. Don't use it - this is only implemented to provide cross-compatibility /// In this architecture, it's actually totally safe to do this, but the trait
/// with other platforms that require this access. /// is marked unsafe because in some other displays it may require some tomfoolery
unsafe fn raw_mut(&mut self) -> &mut ux_api::platform::FbRaw { /// to get reference types to match up.
let len = self.native_buffer.lock().unwrap().len(); unsafe fn raw_mut(&mut self) -> &mut ux_api::platform::FbRaw { self.buffer_mut() }
core::slice::from_raw_parts_mut(self.native_buffer.lock().unwrap().as_mut_ptr(), len)
}
} }
impl MinifbThread { impl MinifbThread {