Skip to content

Commit 03b4f9c

Browse files
committed
Clean up framebuffer support.
1 parent aeef59f commit 03b4f9c

1 file changed

Lines changed: 55 additions & 40 deletions

File tree

src/main.rs

Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ enum AppEvent {
5555
KeyDown(Key),
5656
}
5757

58+
/// Our video RAM
59+
struct Framebuffer<const N: usize> {
60+
contents: std::cell::UnsafeCell<[u8; N]>,
61+
}
62+
5863
// ===========================================================================
5964
// Global Variables
6065
// ===========================================================================
@@ -65,46 +70,6 @@ enum AppEvent {
6570
// static mut FRAMEBUFFER: [u8; 307200] = [0u8; 307200];
6671
static FRAMEBUFFER: Framebuffer<{ 640 * 480 }> = Framebuffer::new();
6772

68-
struct Framebuffer<const N: usize> {
69-
contents: std::cell::UnsafeCell<[u8; N]>,
70-
}
71-
72-
impl<const N: usize> Framebuffer<N> {
73-
const fn new() -> Framebuffer<N> {
74-
Framebuffer {
75-
contents: std::cell::UnsafeCell::new([0u8; N]),
76-
}
77-
}
78-
79-
fn write_at(&self, offset: usize, value: u8) {
80-
if offset > std::mem::size_of_val(&self.contents) {
81-
panic!("Out of bounds framebuffer write");
82-
}
83-
unsafe {
84-
let array_ptr = self.contents.get() as *mut u8;
85-
let byte_ptr = array_ptr.add(offset);
86-
byte_ptr.write_volatile(value);
87-
}
88-
}
89-
90-
fn get_at(&self, offset: usize) -> u8 {
91-
if offset > std::mem::size_of_val(&self.contents) {
92-
panic!("Out of bounds framebuffer read");
93-
}
94-
unsafe {
95-
let array_ptr = self.contents.get() as *const u8;
96-
let byte_ptr = array_ptr.add(offset);
97-
byte_ptr.read_volatile()
98-
}
99-
}
100-
101-
fn get_pointer(&self) -> *mut u8 {
102-
self.contents.get() as *mut u8
103-
}
104-
}
105-
106-
unsafe impl<const N: usize> Sync for Framebuffer<N> {}
107-
10873
/// Scale the display to make it readable on a modern monitor
10974
const SCALE_FACTOR: f32 = 2.0;
11075

@@ -1567,6 +1532,56 @@ impl AppState for MyApp {
15671532
}
15681533
}
15691534

1535+
impl<const N: usize> Framebuffer<N> {
1536+
/// Create a new blank Framebuffer.
1537+
///
1538+
/// Everything is zero initialised.
1539+
const fn new() -> Framebuffer<N> {
1540+
Framebuffer {
1541+
contents: std::cell::UnsafeCell::new([0u8; N]),
1542+
}
1543+
}
1544+
1545+
/// Set a byte in the framebuffer.
1546+
///
1547+
/// Panics if you try and write out of bounds.
1548+
///
1549+
/// Uses volatile writes.
1550+
fn write_at(&self, offset: usize, value: u8) {
1551+
if offset > std::mem::size_of_val(&self.contents) {
1552+
panic!("Out of bounds framebuffer write");
1553+
}
1554+
unsafe {
1555+
let array_ptr = self.contents.get() as *mut u8;
1556+
let byte_ptr = array_ptr.add(offset);
1557+
byte_ptr.write_volatile(value);
1558+
}
1559+
}
1560+
1561+
/// Get a byte from the framebuffer.
1562+
///
1563+
/// Panics if you try and read out of bounds.
1564+
///
1565+
/// Uses volatile reads.
1566+
fn get_at(&self, offset: usize) -> u8 {
1567+
if offset > std::mem::size_of_val(&self.contents) {
1568+
panic!("Out of bounds framebuffer read");
1569+
}
1570+
unsafe {
1571+
let array_ptr = self.contents.get() as *const u8;
1572+
let byte_ptr = array_ptr.add(offset);
1573+
byte_ptr.read_volatile()
1574+
}
1575+
}
1576+
1577+
/// Get a pointer to the framebuffer you can give to the OS.
1578+
fn get_pointer(&self) -> *mut u8 {
1579+
self.contents.get() as *mut u8
1580+
}
1581+
}
1582+
1583+
unsafe impl<const N: usize> Sync for Framebuffer<N> {}
1584+
15701585
// ===========================================================================
15711586
// End of File
15721587
// ===========================================================================

0 commit comments

Comments
 (0)