More refactoring

This commit is contained in:
Ryan Kennedy 2020-03-22 20:09:22 -05:00
parent 15825f7828
commit c424b3134e
4 changed files with 71 additions and 78 deletions

View file

@ -1,6 +1,42 @@
use super::{GRX_DATA_ADDRESS, GRX_INDEX_ADDRESS};
use super::{Color16Bit, PlaneMask, GRX_DATA_ADDRESS, GRX_INDEX_ADDRESS};
use core::convert::TryFrom;
use x86_64::instructions::port::Port;
/// Represents a plane for the `GraphicsControllerIndex::ReadPlaneSelect` register.
#[allow(dead_code)]
#[derive(Debug, Copy, Clone)]
#[repr(u8)]
pub enum ReadPlane {
/// Represents `Plane 0 (0x0)`.
Plane0 = 0x0,
/// Represents `Plane 1 (0x1)`.
Plane1 = 0x1,
/// Represents `Plane 2 (0x2)`.
Plane2 = 0x2,
/// Represents `Plane 3 (0x3)`.
Plane3 = 0x3,
}
impl TryFrom<u8> for ReadPlane {
type Error = &'static str;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(ReadPlane::Plane0),
1 => Ok(ReadPlane::Plane1),
2 => Ok(ReadPlane::Plane2),
3 => Ok(ReadPlane::Plane3),
_ => Err("ReadPlane only accepts values between 0-3!"),
}
}
}
impl From<ReadPlane> for u8 {
fn from(value: ReadPlane) -> u8 {
value as u8
}
}
/// Represents an index for the graphics controller registers.
#[derive(Debug, Copy, Clone)]
#[repr(u8)]
@ -68,6 +104,32 @@ impl GraphicsControllerRegisters {
}
}
/// Sets the read plane of the graphics controller, as specified by `read_plane`.
pub fn write_read_plane(&mut self, read_plane: ReadPlane) {
let read_plane = u8::from(read_plane) & 0x3;
self.write(GraphicsControllerIndex::ReadPlaneSelect, read_plane);
}
/// Sets the value to use for `GraphicsControllerIndex::SetReset`,
/// as spcified by `color`.
pub fn write_set_reset(&mut self, color: Color16Bit) {
let original_value = self.read(GraphicsControllerIndex::SetReset) & 0xF0;
self.write(
GraphicsControllerIndex::SetReset,
original_value | u8::from(color),
);
}
/// Sets which planes are effected by `GraphicsControllerIndex::SetReset`,
/// as specified by `plane_mask`.
pub fn write_enable_set_reset(&mut self, plane_mask: PlaneMask) {
let original_value = self.read(GraphicsControllerIndex::EnableSetReset) & 0xF0;
self.write(
GraphicsControllerIndex::EnableSetReset,
original_value | u8::from(plane_mask),
);
}
fn set_index(&mut self, index: GraphicsControllerIndex) {
unsafe {
self.grx_index.write(u8::from(index));

View file

@ -7,7 +7,7 @@ mod general;
mod graphics_controller;
mod sequencer;
use crate::colors::PALETTE_SIZE;
use crate::colors::{Color16Bit, PALETTE_SIZE};
pub use attribute_controller::{AttributeControllerIndex, AttributeControllerRegisters};
pub use color_palette::ColorPaletteRegisters;

View file

@ -1,21 +1,18 @@
//! Provides access to the vga graphics card.
use super::{
colors::{Color16Bit, PALETTE_SIZE},
configurations::{
VgaConfiguration, MODE_40X25_CONFIGURATION, MODE_40X50_CONFIGURATION,
MODE_640X480X16_CONFIGURATION, MODE_80X25_CONFIGURATION,
},
fonts::VgaFont,
registers::{
AttributeControllerIndex, AttributeControllerRegisters, ColorPaletteRegisters,
CrtcControllerIndex, CrtcControllerRegisters, EmulationMode, GeneralRegisters,
GraphicsControllerIndex, GraphicsControllerRegisters, PlaneMask, SequencerIndex,
SequencerRegisters,
AttributeControllerRegisters, ColorPaletteRegisters, CrtcControllerIndex,
CrtcControllerRegisters, EmulationMode, GeneralRegisters, GraphicsControllerIndex,
GraphicsControllerRegisters, PlaneMask, SequencerIndex, SequencerRegisters,
},
};
use conquer_once::spin::Lazy;
use core::convert::TryFrom;
use spinning_top::Spinlock;
/// Provides mutable access to the vga graphics card.
@ -51,41 +48,6 @@ impl From<FrameBuffer> for u32 {
}
}
/// Represents a plane for the `GraphicsControllerIndex::ReadPlaneSelect` register.
#[allow(dead_code)]
#[derive(Debug, Copy, Clone)]
#[repr(u8)]
pub enum ReadPlane {
/// Represents `Plane 0 (0x0)`.
Plane0 = 0x0,
/// Represents `Plane 1 (0x1)`.
Plane1 = 0x1,
/// Represents `Plane 2 (0x2)`.
Plane2 = 0x2,
/// Represents `Plane 3 (0x3)`.
Plane3 = 0x3,
}
impl TryFrom<u8> for ReadPlane {
type Error = &'static str;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(ReadPlane::Plane0),
1 => Ok(ReadPlane::Plane1),
2 => Ok(ReadPlane::Plane2),
3 => Ok(ReadPlane::Plane3),
_ => Err("ReadPlane only accepts values between 0-3!"),
}
}
}
impl From<ReadPlane> for u8 {
fn from(value: ReadPlane) -> u8 {
value as u8
}
}
/// Represents a specified vga video mode.
#[derive(Debug, Clone, Copy)]
pub enum VideoMode {
@ -245,39 +207,6 @@ impl Vga {
)
}
/// Sets the read plane of the graphics controller, as specified by `read_plane`.
pub fn set_read_plane(&mut self, read_plane: ReadPlane) {
let read_plane = u8::from(read_plane) & 0x3;
self.graphics_controller_registers
.write(GraphicsControllerIndex::ReadPlaneSelect, read_plane);
}
/// Sets the value to use for `GraphicsControllerIndex::SetReset`,
/// as spcified by `color`.
pub fn set_graphics_set_reset(&mut self, color: Color16Bit) {
let original_value = self
.graphics_controller_registers
.read(GraphicsControllerIndex::SetReset)
& 0xF0;
self.graphics_controller_registers.write(
GraphicsControllerIndex::SetReset,
original_value | u8::from(color),
);
}
/// Sets which planes are effected by `GraphicsControllerIndex::SetReset`,
/// as specified by `plane_mask`.
pub fn set_graphics_enable_set_reset(&mut self, plane_mask: PlaneMask) {
let original_value = self
.graphics_controller_registers
.read(GraphicsControllerIndex::EnableSetReset)
& 0xF0;
self.graphics_controller_registers.write(
GraphicsControllerIndex::EnableSetReset,
original_value | u8::from(plane_mask),
);
}
fn set_registers(&mut self, configuration: &VgaConfiguration) {
let emulation_mode = self.get_emulation_mode();

View file

@ -38,7 +38,8 @@ impl Graphics640x480x16 {
let (mut vga, frame_buffer) = self.get_frame_buffer();
vga.sequencer_registers
.set_plane_mask(PlaneMask::ALL_PLANES);
vga.set_graphics_enable_set_reset(PlaneMask::NONE);
vga.graphics_controller_registers
.write_enable_set_reset(PlaneMask::NONE);
for offset in 0..ALL_PLANES_SCREEN_SIZE {
unsafe {
frame_buffer
@ -59,7 +60,8 @@ impl Graphics640x480x16 {
let mut plane_mask = 0x01;
for plane in 0u8..4u8 {
vga.set_read_plane(plane.try_into().unwrap());
vga.graphics_controller_registers
.write_read_plane(plane.try_into().unwrap());
vga.sequencer_registers
.set_plane_mask(plane.try_into().unwrap());
let current_value = unsafe { frame_buffer.add(offset).read_volatile() };