Skip to content

Commit

Permalink
fix VGA writer
Browse files Browse the repository at this point in the history
  • Loading branch information
AtsukiTak committed Jun 17, 2019
1 parent 9fa1aa8 commit 944771c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 28 deletions.
22 changes: 22 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#![no_std]
#![no_main]
#![feature(custom_test_frameworks)]
#![test_runner(crate::test_runner)]
#![reexport_test_harness_main = "test_main"]

mod vga;

use core::panic::PanicInfo;
Expand All @@ -8,11 +12,29 @@ use core::panic::PanicInfo;
pub extern "C" fn _start() -> ! {
println!("Hello World!");

#[cfg(test)]
test_main();

loop {}
}

#[test_case]
fn trivial_assertion() {
print!("trivial assertion... ");
assert_eq!(1, 1);
println!("[ok]");
}

#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
println!("{}", info);
loop {}
}

#[cfg(test)]
fn test_runner(tests: &[&dyn Fn()]) {
println!("Running {} tests", tests.len());
for test in tests {
test();
}
}
60 changes: 32 additions & 28 deletions src/vga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,24 @@ macro_rules! println {
($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
}

lazy_static! {
static ref WRITER: Mutex<Writer> =
Mutex::new(Writer::new(ColorCode::new(Color::Yellow, Color::Black)));
}

/// Write a string to the VGA buffer.
#[doc(hidden)]
pub fn _print(args: core::fmt::Arguments) {
use core::fmt::Write;
let mut writer = BottomWriter::new(ColorCode::new(Color::Yellow, Color::Black));
writer.write_fmt(args).unwrap();
WRITER.lock().write_fmt(args).unwrap();
}

struct BottomWriter {
struct Writer {
column_position: usize,
color_code: ColorCode,
}

impl core::fmt::Write for BottomWriter {
impl core::fmt::Write for Writer {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
for byte in s.bytes() {
match byte {
Expand All @@ -38,9 +42,9 @@ impl core::fmt::Write for BottomWriter {
}
}

impl BottomWriter {
fn new(color_code: ColorCode) -> BottomWriter {
BottomWriter {
impl Writer {
fn new(color_code: ColorCode) -> Writer {
Writer {
column_position: 0,
color_code,
}
Expand Down Expand Up @@ -88,21 +92,39 @@ impl BottomWriter {
}
}

const BUF_HEIGHT: usize = 25;
const BUF_WIDTH: usize = 80;

lazy_static! {
static ref VGA_BUFFER: Mutex<&'static mut [[Volatile<ScreenChar>; BUF_WIDTH]; BUF_HEIGHT]> =
Mutex::new(unsafe { &mut *(0xb8000 as *mut _) });
}

/// Write a character to the VGA buffer.
pub fn write_char(screen_char: ScreenChar, row: usize, col: usize) {
VGA_BUFFER.lock()[row][col].write(screen_char);
}

/// Read a character from the VGA buffer.
pub fn read_char(row: usize, col: usize) -> ScreenChar {
VGA_BUFFER.lock()[row][col].read()
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(C)]
struct ScreenChar {
pub struct ScreenChar {
ascii_char: u8,
color_code: ColorCode,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(transparent)]
struct ColorCode(u8);
pub struct ColorCode(u8);

#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
enum Color {
pub enum Color {
Black = 0,
Blue = 1,
Green = 2,
Expand All @@ -126,21 +148,3 @@ impl ColorCode {
ColorCode((foreground as u8) | (background as u8) << 4)
}
}

const BUF_HEIGHT: usize = 25;
const BUF_WIDTH: usize = 80;

lazy_static! {
static ref VGA_BUFFER: Mutex<&'static mut [[Volatile<ScreenChar>; BUF_WIDTH]; BUF_HEIGHT]> =
Mutex::new(unsafe { &mut *(0xb8000 as *mut _) });
}

/// Write a character to the VGA buffer.
fn write_char(screen_char: ScreenChar, row: usize, col: usize) {
VGA_BUFFER.lock()[row][col].write(screen_char);
}

/// Read a character from the VGA buffer.
fn read_char(row: usize, col: usize) -> ScreenChar {
VGA_BUFFER.lock()[row][col].read()
}

0 comments on commit 944771c

Please sign in to comment.