Fixed tests and renamed Color16Bit

This commit is contained in:
Ryan Kennedy 2020-03-24 16:17:13 -05:00
parent 36d92e4123
commit 244594c476
11 changed files with 52 additions and 45 deletions

View file

@ -2,7 +2,7 @@
## Breaking ## Breaking
- `ScreenCharacter::new` now takes a `TextModeColor` instead of 2 `Color16Bit`. - `ScreenCharacter::new` now takes a `TextModeColor` instead of 2 `Color16`.
## Other ## Other
- Added `ScreenCharacter::get_character`. - Added `ScreenCharacter::get_character`.

View file

@ -11,11 +11,11 @@ this crate to work properly.
## Usage ## Usage
```rust ```rust
use vga::colors::{Color16Bit, TextModeColor}; use vga::colors::{Color16, TextModeColor};
use vga::writers::{ScreenCharacter, TextWriter, Text80x25}; use vga::writers::{ScreenCharacter, TextWriter, Text80x25};
let text_mode = Text80x25::new(); let text_mode = Text80x25::new();
let color = TextModeColor::new(Color16Bit::Yellow, Color16Bit::Black); let color = TextModeColor::new(Color16::Yellow, Color16::Black);
let screen_character = ScreenCharacter::new(b'T', color); let screen_character = ScreenCharacter::new(b'T', color);
text_mode.set_mode(); text_mode.set_mode();

View file

@ -6,7 +6,7 @@ pub const PALETTE_SIZE: usize = 768;
/// Represents a 16 bit color used for vga display. /// Represents a 16 bit color used for vga display.
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
#[repr(u8)] #[repr(u8)]
pub enum Color16Bit { pub enum Color16 {
/// Represents the color `Black (0x0)`. /// Represents the color `Black (0x0)`.
Black = 0x0, Black = 0x0,
/// Represents the color `Blue (0x1)`. /// Represents the color `Blue (0x1)`.
@ -41,8 +41,8 @@ pub enum Color16Bit {
White = 0xF, White = 0xF,
} }
impl From<Color16Bit> for u8 { impl From<Color16> for u8 {
fn from(value: Color16Bit) -> u8 { fn from(value: Color16) -> u8 {
value as u8 value as u8
} }
} }
@ -55,17 +55,17 @@ pub struct TextModeColor(u8);
impl TextModeColor { impl TextModeColor {
/// Returns a new `TextModeColor` given the specified `foreground` /// Returns a new `TextModeColor` given the specified `foreground`
/// and `background` color. /// and `background` color.
pub const fn new(foreground: Color16Bit, background: Color16Bit) -> TextModeColor { pub const fn new(foreground: Color16, background: Color16) -> TextModeColor {
TextModeColor((background as u8) << 4 | (foreground as u8)) TextModeColor((background as u8) << 4 | (foreground as u8))
} }
/// Sets the background color given the specified `background`; /// Sets the background color given the specified `background`;
pub fn set_background(&mut self, background: Color16Bit) { pub fn set_background(&mut self, background: Color16) {
self.0 = (background as u8) << 4 | (self.0 & 0x0F); self.0 = (background as u8) << 4 | (self.0 & 0x0F);
} }
/// Sets the foreground color given the specified `foreground`. /// Sets the foreground color given the specified `foreground`.
pub fn set_foreground(&mut self, foreground: Color16Bit) { pub fn set_foreground(&mut self, foreground: Color16) {
self.0 = foreground as u8; self.0 = foreground as u8;
} }
} }
@ -128,15 +128,15 @@ mod test {
#[test] #[test]
fn test_set_foreground() { fn test_set_foreground() {
let mut color = TextModeColor::new(Color16Bit::Yellow, Color16Bit::Black); let mut color = TextModeColor::new(Color16::Yellow, Color16::Black);
color.set_foreground(Color16Bit::Red); color.set_foreground(Color16::Red);
assert_eq!(color.0 & 0x0F, Color16Bit::Red as u8); assert_eq!(color.0 & 0x0F, Color16::Red as u8);
} }
#[test] #[test]
fn test_set_background() { fn test_set_background() {
let mut color = TextModeColor::new(Color16Bit::Yellow, Color16Bit::Black); let mut color = TextModeColor::new(Color16::Yellow, Color16::Black);
color.set_background(Color16Bit::DarkGrey); color.set_background(Color16::DarkGrey);
assert_eq!(color.0 >> 4, Color16Bit::DarkGrey as u8); assert_eq!(color.0 >> 4, Color16::DarkGrey as u8);
} }
} }

View file

@ -1,4 +1,4 @@
use super::{Color16Bit, GRX_DATA_ADDRESS, GRX_INDEX_ADDRESS}; use super::{Color16, GRX_DATA_ADDRESS, GRX_INDEX_ADDRESS};
use core::convert::TryFrom; use core::convert::TryFrom;
use x86_64::instructions::port::Port; use x86_64::instructions::port::Port;
@ -169,7 +169,7 @@ impl GraphicsControllerRegisters {
/// Sets the value to use for `GraphicsControllerIndex::SetReset`, /// Sets the value to use for `GraphicsControllerIndex::SetReset`,
/// as spcified by `color`. /// as spcified by `color`.
pub fn write_set_reset(&mut self, color: Color16Bit) { pub fn write_set_reset(&mut self, color: Color16) {
let original_value = self.read(GraphicsControllerIndex::SetReset) & 0xF0; let original_value = self.read(GraphicsControllerIndex::SetReset) & 0xF0;
self.write( self.write(
GraphicsControllerIndex::SetReset, GraphicsControllerIndex::SetReset,

View file

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

View file

@ -1,6 +1,6 @@
use super::GraphicsWriter; use super::GraphicsWriter;
use crate::{ use crate::{
colors::{Color16Bit, DEFAULT_PALETTE}, colors::{Color16, DEFAULT_PALETTE},
drawing::{Bresenham, Point}, drawing::{Bresenham, Point},
registers::{PlaneMask, WriteMode}, registers::{PlaneMask, WriteMode},
vga::{Vga, VideoMode, VGA}, vga::{Vga, VideoMode, VGA},
@ -20,19 +20,19 @@ const WIDTH_IN_BYTES: usize = WIDTH / 8;
/// Basic usage: /// Basic usage:
/// ///
/// ```no_run /// ```no_run
/// use vga::colors::Color16Bit; /// use vga::colors::Color16;
/// use vga::writers::{GraphicsWriter, Graphics640x480x16}; /// use vga::writers::{GraphicsWriter, Graphics640x480x16};
/// ///
/// let graphics_mode = Graphics640x480x16::new(); /// let graphics_mode = Graphics640x480x16::new();
/// ///
/// graphics_mode.set_mode(); /// graphics_mode.set_mode();
/// graphics_mode.clear_screen(Color16Bit::Black); /// graphics_mode.clear_screen(Color16::Black);
/// ``` /// ```
#[derive(Default)] #[derive(Default)]
pub struct Graphics640x480x16; pub struct Graphics640x480x16;
impl GraphicsWriter<Color16Bit> for Graphics640x480x16 { impl GraphicsWriter<Color16> for Graphics640x480x16 {
fn clear_screen(&self, color: Color16Bit) { fn clear_screen(&self, color: Color16) {
self.set_write_mode_2(); self.set_write_mode_2();
let (_vga, frame_buffer) = self.get_frame_buffer(); let (_vga, frame_buffer) = self.get_frame_buffer();
for offset in 0..ALL_PLANES_SCREEN_SIZE { for offset in 0..ALL_PLANES_SCREEN_SIZE {
@ -42,14 +42,14 @@ impl GraphicsWriter<Color16Bit> for Graphics640x480x16 {
} }
} }
fn draw_line(&self, start: Point<isize>, end: Point<isize>, color: Color16Bit) { fn draw_line(&self, start: Point<isize>, end: Point<isize>, color: Color16) {
self.set_write_mode_0(color); self.set_write_mode_0(color);
for (x, y) in Bresenham::new(start, end) { for (x, y) in Bresenham::new(start, end) {
self._set_pixel(x as usize, y as usize, color); self._set_pixel(x as usize, y as usize, color);
} }
} }
fn draw_character(&self, x: usize, y: usize, character: char, color: Color16Bit) { fn draw_character(&self, x: usize, y: usize, character: char, color: Color16) {
self.set_write_mode_2(); self.set_write_mode_2();
let character = match font8x8::BASIC_FONTS.get(character) { let character = match font8x8::BASIC_FONTS.get(character) {
Some(character) => character, Some(character) => character,
@ -67,7 +67,7 @@ impl GraphicsWriter<Color16Bit> for Graphics640x480x16 {
} }
} }
fn set_pixel(&self, x: usize, y: usize, color: Color16Bit) { fn set_pixel(&self, x: usize, y: usize, color: Color16) {
self.set_write_mode_2(); self.set_write_mode_2();
self._set_pixel(x, y, color); self._set_pixel(x, y, color);
} }
@ -91,7 +91,7 @@ impl Graphics640x480x16 {
/// Sets the vga to 'WriteMode::Mode0`. This also sets `GraphicsControllerIndex::SetReset` /// Sets the vga to 'WriteMode::Mode0`. This also sets `GraphicsControllerIndex::SetReset`
/// to the specified `color`, `GraphicsControllerIndex::EnableSetReset` to `0xFF` and /// to the specified `color`, `GraphicsControllerIndex::EnableSetReset` to `0xFF` and
/// `SequencerIndex::PlaneMask` to `PlaneMask::ALL_PLANES`. /// `SequencerIndex::PlaneMask` to `PlaneMask::ALL_PLANES`.
pub fn set_write_mode_0(&self, color: Color16Bit) { pub fn set_write_mode_0(&self, color: Color16) {
let (mut vga, _frame_buffer) = self.get_frame_buffer(); let (mut vga, _frame_buffer) = self.get_frame_buffer();
vga.graphics_controller_registers.write_set_reset(color); vga.graphics_controller_registers.write_set_reset(color);
vga.graphics_controller_registers vga.graphics_controller_registers
@ -121,7 +121,7 @@ impl Graphics640x480x16 {
} }
#[inline] #[inline]
fn _set_pixel(&self, x: usize, y: usize, color: Color16Bit) { fn _set_pixel(&self, x: usize, y: usize, color: Color16) {
let (mut vga, frame_buffer) = self.get_frame_buffer(); let (mut vga, frame_buffer) = self.get_frame_buffer();
let offset = x / 8 + y * WIDTH_IN_BYTES; let offset = x / 8 + y * WIDTH_IN_BYTES;
let pixel_mask = 0x80 >> (x & 0x07); let pixel_mask = 0x80 >> (x & 0x07);

View file

@ -5,7 +5,7 @@ mod text_40x50;
mod text_80x25; mod text_80x25;
use super::{ use super::{
colors::{Color16Bit, TextModeColor}, colors::{Color16, TextModeColor},
drawing::Point, drawing::Point,
registers::CrtcControllerIndex, registers::CrtcControllerIndex,
vga::{Vga, VGA}, vga::{Vga, VGA},
@ -44,7 +44,7 @@ impl ScreenCharacter {
static BLANK_CHARACTER: ScreenCharacter = ScreenCharacter { static BLANK_CHARACTER: ScreenCharacter = ScreenCharacter {
character: b' ', character: b' ',
color: TextModeColor::new(Color16Bit::Yellow, Color16Bit::Black), color: TextModeColor::new(Color16::Yellow, Color16::Black),
}; };
/// A helper trait used to interact with various vga screens. /// A helper trait used to interact with various vga screens.
@ -73,8 +73,8 @@ pub trait TextWriter: Screen {
} }
/// Clears the screen by setting all cells to `b' '` with /// Clears the screen by setting all cells to `b' '` with
/// a background color of `Color16Bit::Black` and a foreground /// a background color of `Color16::Black` and a foreground
/// color of `Color16Bit::Yellow`. /// color of `Color16::Yellow`.
fn clear_screen(&self) { fn clear_screen(&self) {
let (_vga, frame_buffer) = self.get_frame_buffer(); let (_vga, frame_buffer) = self.get_frame_buffer();
let screen_size = self.get_width() * self.get_height(); let screen_size = self.get_width() * self.get_height();

View file

@ -15,11 +15,11 @@ const HEIGHT: usize = 25;
/// Basic usage: /// Basic usage:
/// ///
/// ```no_run /// ```no_run
/// use vga::colors::{Color16Bit, TextModeColor}; /// use vga::colors::{Color16, TextModeColor};
/// use vga::writers::{ScreenCharacter, TextWriter, Text40x25}; /// use vga::writers::{ScreenCharacter, TextWriter, Text40x25};
/// ///
/// let text_mode = Text40x25::new(); /// let text_mode = Text40x25::new();
/// let color = TextModeColor::new(Color16Bit::Yellow, Color16Bit::Black); /// let color = TextModeColor::new(Color16::Yellow, Color16::Black);
/// let screen_character = ScreenCharacter::new(b'T', color); /// let screen_character = ScreenCharacter::new(b'T', color);
/// ///
/// text_mode.set_mode(); /// text_mode.set_mode();

View file

@ -15,11 +15,11 @@ const HEIGHT: usize = 50;
/// Basic usage: /// Basic usage:
/// ///
/// ```no_run /// ```no_run
/// use vga::colors::{Color16Bit, TextModeColor}; /// use vga::colors::{Color16, TextModeColor};
/// use vga::writers::{ScreenCharacter, TextWriter, Text40x50}; /// use vga::writers::{ScreenCharacter, TextWriter, Text40x50};
/// ///
/// let text_mode = Text40x50::new(); /// let text_mode = Text40x50::new();
/// let color = TextModeColor::new(Color16Bit::Yellow, Color16Bit::Black); /// let color = TextModeColor::new(Color16::Yellow, Color16::Black);
/// let screen_character = ScreenCharacter::new(b'T', color); /// let screen_character = ScreenCharacter::new(b'T', color);
/// ///
/// text_mode.set_mode(); /// text_mode.set_mode();

View file

@ -15,11 +15,11 @@ const HEIGHT: usize = 25;
/// Basic usage: /// Basic usage:
/// ///
/// ```no_run /// ```no_run
/// use vga::colors::{Color16Bit, TextModeColor}; /// use vga::colors::{Color16, TextModeColor};
/// use vga::writers::{ScreenCharacter, TextWriter, Text80x25}; /// use vga::writers::{ScreenCharacter, TextWriter, Text80x25};
/// ///
/// let text_mode = Text80x25::new(); /// let text_mode = Text80x25::new();
/// let color = TextModeColor::new(Color16Bit::Yellow, Color16Bit::Black); /// let color = TextModeColor::new(Color16::Yellow, Color16::Black);
/// let screen_character = ScreenCharacter::new(b'T', color); /// let screen_character = ScreenCharacter::new(b'T', color);
/// ///
/// text_mode.set_mode(); /// text_mode.set_mode();

View file

@ -83,8 +83,8 @@ fn load_palette() {
let mut palette = [0u8; PALETTE_SIZE]; let mut palette = [0u8; PALETTE_SIZE];
let mut vga = VGA.lock(); let mut vga = VGA.lock();
vga.load_palette(&DEFAULT_PALETTE); vga.color_palette_registers.load_palette(&DEFAULT_PALETTE);
vga.read_palette(&mut palette); vga.color_palette_registers.read_palette(&mut palette);
for i in 0..PALETTE_SIZE { for i in 0..PALETTE_SIZE {
assert_eq!(palette[i], DEFAULT_PALETTE[i]); assert_eq!(palette[i], DEFAULT_PALETTE[i]);
@ -95,23 +95,30 @@ fn load_palette() {
fn check_registers(vga: &mut Vga, configuration: &VgaConfiguration) { fn check_registers(vga: &mut Vga, configuration: &VgaConfiguration) {
let emulation_mode = vga.get_emulation_mode(); let emulation_mode = vga.get_emulation_mode();
assert_eq!(vga.read_msr(), configuration.miscellaneous_output); assert_eq!(
vga.general_registers.read_msr(),
configuration.miscellaneous_output
);
for (index, value) in configuration.sequencer_registers { for (index, value) in configuration.sequencer_registers {
assert_eq!(vga.read_sequencer(*index), *value); assert_eq!(vga.sequencer_registers.read(*index), *value);
} }
for (index, value) in configuration.crtc_controller_registers { for (index, value) in configuration.crtc_controller_registers {
assert_eq!(vga.read_crtc_controller(emulation_mode, *index), *value); assert_eq!(
vga.crtc_controller_registers.read(emulation_mode, *index),
*value
);
} }
for (index, value) in configuration.graphics_controller_registers { for (index, value) in configuration.graphics_controller_registers {
assert_eq!(vga.read_graphics_controller(*index), *value); assert_eq!(vga.graphics_controller_registers.read(*index), *value);
} }
for (index, value) in configuration.attribute_controller_registers { for (index, value) in configuration.attribute_controller_registers {
assert_eq!( assert_eq!(
vga.read_attribute_controller(emulation_mode, *index), vga.attribute_controller_registers
.read(emulation_mode, *index),
*value *value
); );
} }