11use core:: fmt:: { Result , Write } ;
2- use core:: slice;
32use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
43
54const VGA_BUFFER : * mut u8 = 0xa0000 as * mut _ ;
65const SCREEN_WIDTH : usize = 320 ;
76const SCREEN_HEIGHT : usize = 200 ;
87
9- pub static X_POS : AtomicUsize = AtomicUsize :: new ( 1 ) ; // must not be 0 so that we don't have a .bss section
10- pub static Y_POS : AtomicUsize = AtomicUsize :: new ( 1 ) ; // must not be 0 so that we don't have a .bss section
8+ // must not be 0 so that we don't have a .bss section
9+ pub static X_POS : AtomicUsize = AtomicUsize :: new ( 1 ) ;
10+ pub static Y_POS : AtomicUsize = AtomicUsize :: new ( 1 ) ;
1111
1212pub struct Printer ;
1313
1414impl Printer {
1515 pub fn clear_screen ( & mut self ) {
16- let vga_buffer = Self :: vga_buffer ( ) ;
17- for byte in vga_buffer {
18- * byte = 0x00 ;
16+ for i in 0 ..( SCREEN_WIDTH * SCREEN_HEIGHT ) {
17+ unsafe {
18+ VGA_BUFFER . offset ( i as isize ) . write_volatile ( 0 ) ;
19+ }
1920 }
21+
2022 X_POS . store ( 0 , Ordering :: SeqCst ) ;
2123 Y_POS . store ( 0 , Ordering :: SeqCst ) ;
2224 }
2325
24- fn vga_buffer ( ) -> & ' static mut [ u8 ] {
25- unsafe { slice:: from_raw_parts_mut ( VGA_BUFFER , SCREEN_WIDTH * SCREEN_HEIGHT ) }
26- }
27-
2826 fn newline ( & mut self ) {
2927 let y_pos = Y_POS . fetch_add ( 8 , Ordering :: SeqCst ) ;
3028 X_POS . store ( 0 , Ordering :: SeqCst ) ;
@@ -41,8 +39,6 @@ impl Printer {
4139 return ;
4240 }
4341
44- let vga_buffer = Self :: vga_buffer ( ) ;
45-
4642 let x_pos = X_POS . fetch_add ( 8 , Ordering :: SeqCst ) ;
4743 let y_pos = Y_POS . load ( Ordering :: SeqCst ) ;
4844
@@ -57,7 +53,10 @@ impl Printer {
5753 continue ;
5854 }
5955 let color = 0xf ;
60- vga_buffer[ ( y_pos + y) * SCREEN_WIDTH + x_pos + x] = color;
56+ let idx = ( y_pos + y) * SCREEN_WIDTH + x_pos + x;
57+ unsafe {
58+ VGA_BUFFER . offset ( idx as isize ) . write_volatile ( color) ;
59+ }
6160 }
6261 }
6362 }
0 commit comments