Skip to content

Commit 0e7f27d

Browse files
64phil-opp
authored andcommitted
Use volatile accesses in VGA code and make font dependency optional (#67)
1 parent 369821f commit 0e7f27d

File tree

4 files changed

+29
-26
lines changed

4 files changed

+29
-26
lines changed

Diff for: Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ fixedvec = "0.2.3"
1818
version = "0.2.4"
1919
default-features = false
2020
features = ["unicode"]
21+
optional = true
2122

2223
[build-dependencies]
2324
llvm-tools = "0.1"
2425

2526
[features]
2627
default = []
27-
vga_320x200 = []
28+
vga_320x200 = ["font8x8"]
2829
recursive_page_table = []
2930
map_physical_memory = []
3031

Diff for: src/printer/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ pub use self::vga_text_80x25::*;
44
#[cfg(feature = "vga_320x200")]
55
pub use self::vga_320x200::*;
66

7+
#[cfg(feature = "vga_320x200")]
78
mod vga_320x200;
9+
10+
#[cfg(not(feature = "vga_320x200"))]
811
mod vga_text_80x25;

Diff for: src/printer/vga_320x200.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
11
use core::fmt::{Result, Write};
2-
use core::slice;
32
use core::sync::atomic::{AtomicUsize, Ordering};
43

54
const VGA_BUFFER: *mut u8 = 0xa0000 as *mut _;
65
const SCREEN_WIDTH: usize = 320;
76
const 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

1212
pub struct Printer;
1313

1414
impl 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
}

Diff for: src/printer/vga_text_80x25.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
use core::fmt::{Result, Write};
2-
use core::slice;
32
use core::sync::atomic::{AtomicUsize, Ordering};
43

54
const VGA_BUFFER: *mut u8 = 0xb8000 as *mut _;
65
const SCREEN_SIZE: usize = 80 * 25;
76

7+
// must not be 0 so that we don't have a .bss section
88
pub static CURRENT_OFFSET: AtomicUsize = AtomicUsize::new(160);
99

1010
pub struct Printer;
1111

1212
impl Printer {
1313
pub fn clear_screen(&mut self) {
14-
let vga_buffer = Self::vga_buffer();
15-
for byte in vga_buffer {
16-
*byte = 0;
14+
for i in 0..SCREEN_SIZE {
15+
unsafe {
16+
VGA_BUFFER.offset(i as isize).write_volatile(0);
17+
}
1718
}
18-
CURRENT_OFFSET.store(0, Ordering::Relaxed);
19-
}
2019

21-
fn vga_buffer() -> &'static mut [u8] {
22-
unsafe { slice::from_raw_parts_mut(VGA_BUFFER, SCREEN_SIZE * 2) }
20+
CURRENT_OFFSET.store(0, Ordering::Relaxed);
2321
}
2422
}
2523

2624
impl Write for Printer {
2725
fn write_str(&mut self, s: &str) -> Result {
28-
let vga_buffer = Self::vga_buffer();
2926
for byte in s.bytes() {
30-
let index = CURRENT_OFFSET.fetch_add(2, Ordering::Relaxed);
31-
vga_buffer[index] = byte;
32-
vga_buffer[index + 1] = 0x4f;
27+
let index = CURRENT_OFFSET.fetch_add(2, Ordering::Relaxed) as isize;
28+
29+
unsafe {
30+
VGA_BUFFER.offset(index).write_volatile(byte);
31+
VGA_BUFFER.offset(index + 1).write_volatile(0x4f);
32+
}
3333
}
3434

3535
Ok(())

0 commit comments

Comments
 (0)