From c424b3134efc68783a3c425d729f3b66a399a0e8 Mon Sep 17 00:00:00 2001 From: Ryan Kennedy Date: Sun, 22 Mar 2020 20:09:22 -0500 Subject: [PATCH] More refactoring --- src/registers/graphics_controller.rs | 64 ++++++++++++++++++++++- src/registers/mod.rs | 2 +- src/vga.rs | 77 ++-------------------------- src/writers/graphics_640x480x16.rs | 6 ++- 4 files changed, 71 insertions(+), 78 deletions(-) diff --git a/src/registers/graphics_controller.rs b/src/registers/graphics_controller.rs index 716cd02..5769ff9 100644 --- a/src/registers/graphics_controller.rs +++ b/src/registers/graphics_controller.rs @@ -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 for ReadPlane { + type Error = &'static str; + + fn try_from(value: u8) -> Result { + 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 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)); diff --git a/src/registers/mod.rs b/src/registers/mod.rs index 5c582c6..32f0a80 100644 --- a/src/registers/mod.rs +++ b/src/registers/mod.rs @@ -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; diff --git a/src/vga.rs b/src/vga.rs index c7a361b..e46ad63 100644 --- a/src/vga.rs +++ b/src/vga.rs @@ -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 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 for ReadPlane { - type Error = &'static str; - - fn try_from(value: u8) -> Result { - 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 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(); diff --git a/src/writers/graphics_640x480x16.rs b/src/writers/graphics_640x480x16.rs index dcace91..ccb2514 100644 --- a/src/writers/graphics_640x480x16.rs +++ b/src/writers/graphics_640x480x16.rs @@ -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() };