added volatile type to prevent the compiler from optimizing writings to the buffer away

This commit is contained in:
clizia 2025-08-26 14:31:37 +02:00
parent 1fbe7a483b
commit 237b99941f
2 changed files with 16 additions and 4 deletions

View file

@ -16,3 +16,4 @@ panic = "abort" # disable stack unwinding on panic
[dependencies]
bootloader = "0.9"
volatile = "0.2.6"

View file

@ -1,3 +1,5 @@
use volatile::Volatile;
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
@ -42,7 +44,15 @@ const BUFFER_WIDTH: usize = 80;
#[repr(transparent)]
struct Buffer {
chars: [[ScreenChar; BUFFER_WIDTH]; BUFFER_HEIGHT],
// wrapping the `ScreenChar` in a generic `Volatile`
// type prevents the compiler from optimizing writes
// away since the program only writes to it but never
// reads from it.
// by making it volatile we signal to the compiler
// that our write call have side-effects
// to interact with this type the `write()` and `read()`
// methods must be used now
chars: [[Volatile<ScreenChar>; BUFFER_WIDTH]; BUFFER_HEIGHT],
}
pub struct Writer {
@ -64,10 +74,10 @@ impl Writer {
let col = self.column_position;
let color_code = self.color_code;
self.buffer.chars[row][col] = ScreenChar {
self.buffer.chars[row][col].write(ScreenChar {
ascii_carachter: byte,
color_code,
};
});
self.column_position += 1;
}
}
@ -89,10 +99,11 @@ impl Writer {
}
}
// function to test things: to be removed
pub fn print_something() {
let mut writer = Writer {
column_position: 0,
color_code: ColorCode::new(Color::Yellow, Color::Black),
color_code: ColorCode::new(Color::Pink, Color::Black),
buffer: unsafe { &mut *(0xB8000 as *mut Buffer) },
};