2020-03-12 12:34:13 -05:00
|
|
|
use crate::{
|
2020-03-15 17:30:28 -05:00
|
|
|
colors::{Color16Bit, DEFAULT_PALETTE},
|
2020-03-21 22:26:59 -05:00
|
|
|
vga::{PlaneMask, Vga, VideoMode, VGA},
|
2020-03-12 12:34:13 -05:00
|
|
|
};
|
2020-03-22 14:11:35 -05:00
|
|
|
use core::convert::TryInto;
|
2020-03-12 12:34:13 -05:00
|
|
|
use spinning_top::SpinlockGuard;
|
|
|
|
|
|
|
|
const WIDTH: usize = 640;
|
|
|
|
const HEIGHT: usize = 480;
|
2020-03-21 22:26:59 -05:00
|
|
|
const ALL_PLANES_SCREEN_SIZE: usize = (WIDTH * HEIGHT) / 4;
|
2020-03-12 12:34:13 -05:00
|
|
|
|
|
|
|
/// A basic interface for interacting with vga graphics mode 640x480x16
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Basic usage:
|
|
|
|
///
|
2020-03-12 12:39:38 -05:00
|
|
|
/// ```no_run
|
2020-03-15 19:46:42 -05:00
|
|
|
/// use vga::writers::Graphics640x480x16;
|
2020-03-12 12:39:38 -05:00
|
|
|
///
|
2020-03-12 12:34:13 -05:00
|
|
|
/// let graphics_mode = Graphics640x480x16::new();
|
2020-03-12 12:39:38 -05:00
|
|
|
///
|
2020-03-12 12:34:13 -05:00
|
|
|
/// graphics_mode.set_mode();
|
|
|
|
/// graphics_mode.clear_screen();
|
|
|
|
/// ```
|
2020-03-12 12:55:27 -05:00
|
|
|
#[derive(Default)]
|
2020-03-12 12:34:13 -05:00
|
|
|
pub struct Graphics640x480x16;
|
|
|
|
|
|
|
|
impl Graphics640x480x16 {
|
|
|
|
/// Creates a new `Graphics640x480x16`.
|
|
|
|
pub fn new() -> Graphics640x480x16 {
|
|
|
|
Graphics640x480x16 {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clears the screen by setting all pixels to `Color16Bit::Black`.
|
|
|
|
pub fn clear_screen(&self) {
|
2020-03-21 22:26:59 -05:00
|
|
|
let (mut vga, frame_buffer) = self.get_frame_buffer();
|
|
|
|
vga.set_plane_mask(PlaneMask::ALL_PLANES);
|
2020-03-21 23:40:40 -05:00
|
|
|
vga.set_graphics_enable_set_reset(PlaneMask::NONE);
|
2020-03-21 22:26:59 -05:00
|
|
|
for offset in 0..ALL_PLANES_SCREEN_SIZE {
|
|
|
|
unsafe {
|
|
|
|
frame_buffer
|
|
|
|
.add(offset)
|
|
|
|
.write_volatile(Color16Bit::Black as u8);
|
2020-03-12 12:34:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the given pixel at `(x, y)` to the given `color`.
|
|
|
|
pub fn set_pixel(&self, x: usize, y: usize, color: Color16Bit) {
|
|
|
|
let (mut vga, frame_buffer) = self.get_frame_buffer();
|
2020-03-12 12:55:27 -05:00
|
|
|
let offset = x / 8 + (WIDTH / 8) * y;
|
2020-03-12 12:34:13 -05:00
|
|
|
|
2020-03-22 14:11:35 -05:00
|
|
|
// Store the current value for masking.
|
|
|
|
let x = x & 7;
|
|
|
|
let mask = 0x80 >> (x & 7);
|
|
|
|
let mut plane_mask = 0x01;
|
2020-03-21 23:40:40 -05:00
|
|
|
|
2020-03-22 14:11:35 -05:00
|
|
|
for plane in 0u8..4u8 {
|
|
|
|
vga.set_read_plane(plane.try_into().unwrap());
|
|
|
|
vga.set_plane_mask(plane.try_into().unwrap());
|
|
|
|
let current_value = unsafe { frame_buffer.add(offset).read_volatile() };
|
|
|
|
let new_value = if plane_mask & color as u8 != 0 {
|
|
|
|
current_value | mask
|
|
|
|
} else {
|
|
|
|
current_value & !mask
|
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
frame_buffer.add(offset).write_volatile(new_value);
|
|
|
|
}
|
|
|
|
plane_mask <<= 1;
|
2020-03-12 12:34:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the graphics device to `VideoMode::Mode640x480x16`.
|
|
|
|
pub fn set_mode(&self) {
|
2020-03-15 17:30:28 -05:00
|
|
|
let mut vga = VGA.lock();
|
|
|
|
vga.set_video_mode(VideoMode::Mode640x480x16);
|
|
|
|
|
|
|
|
// Some bios mess up the palette when switching modes,
|
|
|
|
// so explicitly set it.
|
|
|
|
vga.load_palette(&DEFAULT_PALETTE);
|
2020-03-12 12:34:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the start of the `FrameBuffer` as `*mut u8` as
|
|
|
|
/// well as a lock to the vga driver. This ensures the vga
|
|
|
|
/// driver stays locked while the frame buffer is in use.
|
|
|
|
fn get_frame_buffer(&self) -> (SpinlockGuard<Vga>, *mut u8) {
|
|
|
|
let mut vga = VGA.lock();
|
|
|
|
let frame_buffer = vga.get_frame_buffer();
|
|
|
|
(vga, u32::from(frame_buffer) as *mut u8)
|
|
|
|
}
|
|
|
|
}
|