Merge pull request #2 from rust-osdev/develop

Develop
This commit is contained in:
RKennedy9064 2020-03-16 19:13:21 -05:00 committed by GitHub
commit 26d13e1380
19 changed files with 736 additions and 239 deletions

View file

@ -4,6 +4,7 @@ on:
push:
branches:
- "master"
- "develop"
tags:
- "*"
schedule:
@ -84,10 +85,10 @@ jobs:
- name: "Print QEMU Version"
run: qemu-system-x86_64 --version
- name: "Build Test Kernel"
run: cargo xbuild
working-directory: 'testing'
working-directory: "testing"
- name: "Run Test Framework"
run: cargo xtest --verbose

View file

@ -1,9 +1,10 @@
[package]
name = "vga"
version = "0.1.0"
version = "0.1.1"
authors = ["Ryan Kennedy <rkennedy9064@gmail.com>"]
edition = "2018"
description = "Support for vga specific functions, data structures, and registers."
documentation = "https://docs.rs/vga"
keywords = [
"vga",
"no_std",

View file

@ -5,15 +5,17 @@ and access to various registers.
Memory addresses `0xA0000 -> 0xBFFFF` must be readable and writeable
this crate to work properly.
**Note: This crate is currently experimental and subject to change since it's in active development.**
## Usage
```rust
use vga::colors::{Color16Bit, TextModeColor};
use vga::Text80x25;
use vga::colors::Color16Bit;
use vga::writers::{ScreenCharacter, TextWriter, Text80x25};
let text_mode = Text80x25::new();
let color = TextModeColor::new(Color16Bit::Yellow, Color16Bit::Black);
let screen_character = ScreenCharacter::new(b'T'Color16Bit::Yellow, Color16Bit::Black);
text_mode.set_mode();
text_mode.clear_screen();
text_mode.write_character(0, 0, b'H', color);
text_mode.write_character(0, 0, screen_character);
```

View file

@ -1,4 +1,7 @@
//! Common color structures used in vga.
//! Common color structures used in vga programming.
/// Represents the size of the vga palette in bytes.
pub const PALETTE_SIZE: usize = 768;
/// Represents a 16 bit color used for vga display.
#[derive(Debug, Clone, Copy)]
@ -50,3 +53,55 @@ impl TextModeColor {
TextModeColor((background as u8) << 4 | (foreground as u8))
}
}
/// Represents the default vga 256 color palette.
pub const DEFAULT_PALETTE: [u8; PALETTE_SIZE] = [
0x0, 0x0, 0x0, 0x0, 0x0, 0x2A, 0x0, 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, 0x3F, 0x3F,
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
];

View file

@ -1,3 +1,5 @@
//! Common video configurations used in vga programming.
use super::registers::{
AttributeControllerIndex, CrtcControllerIndex, GraphicsControllerIndex, SequencerIndex,
};
@ -5,10 +7,15 @@ use super::registers::{
/// Represents a set of vga registers for a given mode.
#[derive(Debug)]
pub struct VgaConfiguration {
/// Represents the configuration value for the miscellaneous output register.
pub miscellaneous_output: u8,
/// Represents the configuration values for the sequencer registers.
pub sequencer_registers: &'static [(SequencerIndex, u8)],
/// Represents the configuration values for the crtc controller registers.
pub crtc_controller_registers: &'static [(CrtcControllerIndex, u8)],
/// Represents the configuration values for the graphics controller registers.
pub graphics_controller_registers: &'static [(GraphicsControllerIndex, u8)],
/// Represents the configuration values for the attribute controller registers.
pub attribute_controller_registers: &'static [(AttributeControllerIndex, u8)],
}
@ -34,8 +41,8 @@ pub const MODE_40X25_CONFIGURATION: VgaConfiguration = VgaConfiguration {
(CrtcControllerIndex::Overflow, 0x1F),
(CrtcControllerIndex::PresetRowScan, 0x00),
(CrtcControllerIndex::MaximumScanLine, 0x4F),
(CrtcControllerIndex::TextCursorStart, 0x0A),
(CrtcControllerIndex::TextCursorEnd, 0x20),
(CrtcControllerIndex::TextCursorStart, 0x0D),
(CrtcControllerIndex::TextCursorEnd, 0x0E),
(CrtcControllerIndex::StartAddressHigh, 0x00),
(CrtcControllerIndex::StartAddressLow, 0x00),
(CrtcControllerIndex::TextCursorLocationHigh, 0x00),
@ -44,7 +51,7 @@ pub const MODE_40X25_CONFIGURATION: VgaConfiguration = VgaConfiguration {
(CrtcControllerIndex::VerticalSyncEnd, 0x8E),
(CrtcControllerIndex::VerticalDisplayEnableEnd, 0x8F),
(CrtcControllerIndex::Offset, 0x14),
(CrtcControllerIndex::UnderlineLocationRegister, 0x1F),
(CrtcControllerIndex::UnderlineLocation, 0x1F),
(CrtcControllerIndex::VerticalBlankingStart, 0x96),
(CrtcControllerIndex::VerticalBlankingEnd, 0xB9),
(CrtcControllerIndex::ModeControl, 0xA3),
@ -108,8 +115,8 @@ pub const MODE_40X50_CONFIGURATION: VgaConfiguration = VgaConfiguration {
(CrtcControllerIndex::Overflow, 0x1F),
(CrtcControllerIndex::PresetRowScan, 0x00),
(CrtcControllerIndex::MaximumScanLine, 0x47),
(CrtcControllerIndex::TextCursorStart, 0x0A),
(CrtcControllerIndex::TextCursorEnd, 0x20),
(CrtcControllerIndex::TextCursorStart, 0x06),
(CrtcControllerIndex::TextCursorEnd, 0x07),
(CrtcControllerIndex::StartAddressHigh, 0x00),
(CrtcControllerIndex::StartAddressLow, 0x00),
(CrtcControllerIndex::TextCursorLocationHigh, 0x04),
@ -118,7 +125,7 @@ pub const MODE_40X50_CONFIGURATION: VgaConfiguration = VgaConfiguration {
(CrtcControllerIndex::VerticalSyncEnd, 0x8E),
(CrtcControllerIndex::VerticalDisplayEnableEnd, 0x8F),
(CrtcControllerIndex::Offset, 0x14),
(CrtcControllerIndex::UnderlineLocationRegister, 0x1F),
(CrtcControllerIndex::UnderlineLocation, 0x1F),
(CrtcControllerIndex::VerticalBlankingStart, 0x96),
(CrtcControllerIndex::VerticalBlankingEnd, 0xB9),
(CrtcControllerIndex::ModeControl, 0xA3),
@ -192,7 +199,7 @@ pub const MODE_80X25_CONFIGURATION: VgaConfiguration = VgaConfiguration {
(CrtcControllerIndex::VerticalSyncEnd, 0x0E),
(CrtcControllerIndex::VerticalDisplayEnableEnd, 0x8F),
(CrtcControllerIndex::Offset, 0x28),
(CrtcControllerIndex::UnderlineLocationRegister, 0x1F),
(CrtcControllerIndex::UnderlineLocation, 0x1F),
(CrtcControllerIndex::VerticalBlankingStart, 0x96),
(CrtcControllerIndex::VerticalBlankingEnd, 0xB9),
(CrtcControllerIndex::ModeControl, 0xA3),
@ -267,7 +274,7 @@ pub const MODE_640X480X16_CONFIGURATION: VgaConfiguration = VgaConfiguration {
(CrtcControllerIndex::VerticalSyncEnd, 0x0C),
(CrtcControllerIndex::VerticalDisplayEnableEnd, 0xDF),
(CrtcControllerIndex::Offset, 0x28),
(CrtcControllerIndex::UnderlineLocationRegister, 0x00),
(CrtcControllerIndex::UnderlineLocation, 0x00),
(CrtcControllerIndex::VerticalBlankingStart, 0xE7),
(CrtcControllerIndex::VerticalBlankingEnd, 0x04),
(CrtcControllerIndex::ModeControl, 0xE3),

View file

@ -1,7 +1,12 @@
//! Common font structures used in vga programming.
/// Represents a font to be used for text mode.
pub struct VgaFont {
/// Represents the number of characters contained in the font.
pub characters: u16,
/// Represents the height of the characters in bytes.
pub character_height: u16,
/// Represents the font data to be loaded in.
pub font_data: &'static [u8],
}

View file

@ -8,11 +8,8 @@
#![warn(missing_docs)]
pub mod colors;
mod configurations;
mod fonts;
mod registers;
pub mod configurations;
pub mod fonts;
pub mod registers;
pub mod vga;
mod writers;
pub use self::vga::VGA;
pub use self::writers::{Graphics640x480x16, Text40x25, Text40x50, Text80x25};
pub mod writers;

View file

@ -1,3 +1,6 @@
//! Common registers used in vga programming.
use crate::colors::PALETTE_SIZE;
use x86_64::instructions::port::{Port, PortReadOnly, PortWriteOnly};
const ST00_READ_ADDRESS: u16 = 0x3C2;
@ -23,10 +26,17 @@ const CRX_INDEX_MDA_ADDRESS: u16 = 0x3B4;
const CRX_DATA_CGA_ADDRESS: u16 = 0x3D5;
const CRX_DATA_MDA_ADDRESS: u16 = 0x3B5;
const COLOR_PALETTE_DATA_ADDRESS: u16 = 0x3C9;
const COLOR_PALETTE_INDEX_READ_ADDRESS: u16 = 0x3C7;
const COLOR_PALETTE_INDEX_WRITE_ADDRESSS: u16 = 0x3C8;
/// Represents a vga emulation mode.
#[derive(Debug, Copy, Clone)]
#[repr(u8)]
pub enum EmulationMode {
/// Represents a monochrome emulation mode.
Mda = 0x0,
/// Respresents a color emulation mode.
Cga = 0x1,
}
@ -40,14 +50,21 @@ impl From<u8> for EmulationMode {
}
}
/// Represents an index for the seqeuncer registers.
#[derive(Debug, Clone, Copy)]
#[repr(u8)]
pub enum SequencerIndex {
/// Represents the `Sequencer Reset` register index.
SequencerReset = 0x0,
/// Represents the `Clocking Mode` register index.
ClockingMode = 0x1,
/// Represents the Plane/Map mask register index.
PlaneMask = 0x2,
/// Represents the `Character Font` register index.
CharacterFont = 0x3,
/// Represents the `Memory Mode` register index.
MemoryMode = 0x4,
/// Represents the `Horizontal Character Counter Reset` register index.
CounterReset = 0x7,
}
@ -57,20 +74,33 @@ impl From<SequencerIndex> for u8 {
}
}
/// Represents an index for the graphics controller registers.
#[derive(Debug, Copy, Clone)]
#[repr(u8)]
pub enum GraphicsControllerIndex {
/// Represents the `Set/Reset` register index.
SetReset = 0x0,
/// Represents the `Enable Set/Reset` register index.
EnableSetReset = 0x1,
/// Represents the `Color Compare` register index.
ColorCompare = 0x2,
/// Represents the `Data Rotate` register index.
DataRotate = 0x3,
/// Represents the `Read Plane Select` register index.
ReadPlaneSelect = 0x4,
/// Represents the `Graphics Mode` register index.
GraphicsMode = 0x5,
/// Represents the `Miscellaneous` register index.
Miscellaneous = 0x6,
/// Represents the `Color Don't Care` register index.
ColorDontCare = 0x7,
/// Represents the `Bit Mask` register index.
BitMask = 0x8,
/// Represents the `Address Mapping` register index.
AddressMapping = 0x10,
/// Represents the `Page Selector` register index.
PageSelector = 0x11,
/// Represents the `Software Flags` register index.
SoftwareFlags = 0x18,
}
@ -80,29 +110,51 @@ impl From<GraphicsControllerIndex> for u8 {
}
}
/// Represents an index for the attribute controller registers.
#[derive(Debug, Copy, Clone)]
#[repr(u8)]
pub enum AttributeControllerIndex {
/// Represents the `Palette 0` register index.
PaletteRegister0 = 0x00,
/// Represents the `Palette 1` register index.
PaletteRegister1 = 0x01,
/// Represents the `Palette 2` register index.
PaletteRegister2 = 0x02,
/// Represents the `Palette 3` register index.
PaletteRegister3 = 0x03,
/// Represents the `Palette 4` register index.
PaletteRegister4 = 0x04,
/// Represents the `Palette 5` register index.
PaletteRegister5 = 0x05,
/// Represents the `Palette 6` register index.
PaletteRegister6 = 0x06,
/// Represents the `Palette 7` register index.
PaletteRegister7 = 0x07,
/// Represents the `Palette 8` register index.
PaletteRegister8 = 0x08,
/// Represents the `Palette 9` register index.
PaletteRegister9 = 0x09,
/// Represents the `Palette A` register index.
PaletteRegisterA = 0x0A,
/// Represents the `Palette B` register index.
PaletteRegisterB = 0x0B,
/// Represents the `Palette C` register index.
PaletteRegisterC = 0x0C,
/// Represents the `Palette D` register index.
PaletteRegisterD = 0x0D,
/// Represents the `Palette E` register index.
PaletteRegisterE = 0x0E,
/// Represents the `Palette F` register index.
PaletteRegisterF = 0x0F,
/// Represents the `Mode Control` register index.
ModeControl = 0x10,
/// Represents the `Overscan Color` register index.
OverscanColor = 0x11,
/// Represents the `Memory Plane Enable` register index.
MemoryPlaneEnable = 0x12,
/// Represents the `Horizontal Pixel Panning` register index.
HorizontalPixelPanning = 0x13,
/// Represents the `Color Select` register index.
ColorSelect = 0x14,
}
@ -112,35 +164,63 @@ impl From<AttributeControllerIndex> for u8 {
}
}
/// Represents an index for the crtc controller registers.
#[derive(Debug, Copy, Clone)]
#[repr(u8)]
pub enum CrtcControllerIndex {
/// Represents the `Horizontal Total` register index.
HorizontalTotal = 0x00,
/// Represents the `Horizontal Display Enable End` register index.
HorizontalDisplayEnableEnd = 0x01,
/// Represents the `Horizontal Blanking Start` register index.
HorizontalBlankingStart = 0x02,
/// Represents the `Horizontal Blanking End` register index.
HorizontalBlankingEnd = 0x03,
/// Represents the `Horizontal Sync Start` register index.
HorizontalSyncStart = 0x04,
/// Represents the `Horizontal Sync End` register index.
HorizontalSyncEnd = 0x05,
/// Represents the `Vertical Total` register index.
VeritcalTotal = 0x06,
/// Represents the `Overflow` register index.
Overflow = 0x07,
/// Represents the `Preset Row Scan` register index.
PresetRowScan = 0x08,
/// Represents the `Maximum Scan Line` register index.
MaximumScanLine = 0x09,
/// Represents the `Text Cursor Start` register index.
TextCursorStart = 0x0A,
/// Represents the `Text Cursor End` register index.
TextCursorEnd = 0x0B,
/// Represents the `Start Address High` register index.
StartAddressHigh = 0x0C,
/// Represents the `Start Address Low` register index.
StartAddressLow = 0x0D,
/// Represents the `Text Cursor Location High` register index.
TextCursorLocationHigh = 0x0E,
/// Represents the `Text Cursor Location Low` register index.
TextCursorLocationLow = 0x0F,
/// Represents the `Vertical Sync Start` register index.
VerticalSyncStart = 0x10,
/// Represents the `Vertical Sync End` register index.
VerticalSyncEnd = 0x11,
/// Represents the `Vertical Display Enable End` register index
VerticalDisplayEnableEnd = 0x12,
/// Represents the `Offset` register index.
Offset = 0x13,
UnderlineLocationRegister = 0x14,
/// Represents the `Underline Location` register index.
UnderlineLocation = 0x14,
/// Represents the `Vertical Blanking Start` register index.
VerticalBlankingStart = 0x15,
/// Represents the `Vertical Blanking End` register index.
VerticalBlankingEnd = 0x16,
/// Represents the `Mode Control` register index.
ModeControl = 0x17,
/// Represents the `Line Compare` register index.
LineCompare = 0x18,
/// Represents the `Memory Read Latch Data` register index.
MemoryReadLatchData = 0x22,
/// Represents the `Toggle State Of Attribute Controller` register index.
ToggleStateOfAttributeController = 0x24,
}
@ -151,7 +231,7 @@ impl From<CrtcControllerIndex> for u8 {
}
#[derive(Debug)]
pub struct GeneralRegisters {
pub(crate) struct GeneralRegisters {
st00_read: PortReadOnly<u8>,
st01_read_cga: PortReadOnly<u8>,
st01_read_mda: PortReadOnly<u8>,
@ -188,7 +268,7 @@ impl GeneralRegisters {
}
#[derive(Debug)]
pub struct SequencerRegisters {
pub(crate) struct SequencerRegisters {
srx_index: Port<u8>,
srx_data: Port<u8>,
}
@ -221,7 +301,7 @@ impl SequencerRegisters {
}
#[derive(Debug)]
pub struct GraphicsControllerRegisters {
pub(crate) struct GraphicsControllerRegisters {
grx_index: Port<u8>,
grx_data: Port<u8>,
}
@ -254,7 +334,7 @@ impl GraphicsControllerRegisters {
}
#[derive(Debug)]
pub struct AttributeControllerRegisters {
pub(crate) struct AttributeControllerRegisters {
arx_index: Port<u8>,
arx_data: Port<u8>,
st01_read_cga: Port<u8>,
@ -271,6 +351,12 @@ impl AttributeControllerRegisters {
}
}
pub fn read(&mut self, emulation_mode: EmulationMode, index: AttributeControllerIndex) -> u8 {
self.toggle_index(emulation_mode);
self.set_index(index);
unsafe { self.arx_data.read() }
}
pub fn write(
&mut self,
emulation_mode: EmulationMode,
@ -334,7 +420,7 @@ impl AttributeControllerRegisters {
}
#[derive(Debug)]
pub struct CrtcControllerRegisters {
pub(crate) struct CrtcControllerRegisters {
crx_index_cga: Port<u8>,
crx_index_mda: Port<u8>,
crx_data_cga: Port<u8>,
@ -383,3 +469,42 @@ impl CrtcControllerRegisters {
}
}
}
#[derive(Debug)]
pub(crate) struct ColorPaletteRegisters {
data_port: Port<u8>,
index_read_port: Port<u8>,
index_write_port: Port<u8>,
}
impl ColorPaletteRegisters {
pub fn new() -> ColorPaletteRegisters {
ColorPaletteRegisters {
data_port: Port::new(COLOR_PALETTE_DATA_ADDRESS),
index_read_port: Port::new(COLOR_PALETTE_INDEX_READ_ADDRESS),
index_write_port: Port::new(COLOR_PALETTE_INDEX_WRITE_ADDRESSS),
}
}
pub fn load_palette(&mut self, palette: &[u8; PALETTE_SIZE]) {
unsafe {
self.index_write_port.write(0);
}
for i in palette.iter() {
unsafe {
self.data_port.write(*i);
}
}
}
pub fn read_palette(&mut self, palette: &mut [u8; PALETTE_SIZE]) {
unsafe {
self.index_read_port.write(0);
}
for byte in palette.iter_mut().take(PALETTE_SIZE) {
unsafe {
*byte = self.data_port.read();
}
}
}
}

View file

@ -1,15 +1,16 @@
//! Provides access to the vga graphics card.
use super::{
colors::PALETTE_SIZE,
configurations::{
VgaConfiguration, MODE_40X25_CONFIGURATION, MODE_40X50_CONFIGURATION,
MODE_640X480X16_CONFIGURATION, MODE_80X25_CONFIGURATION,
},
fonts::{VgaFont, TEXT_8X16_FONT, TEXT_8X8_FONT},
fonts::VgaFont,
registers::{
AttributeControllerRegisters, CrtcControllerIndex, CrtcControllerRegisters, EmulationMode,
GeneralRegisters, GraphicsControllerIndex, GraphicsControllerRegisters, SequencerIndex,
SequencerRegisters,
AttributeControllerIndex, AttributeControllerRegisters, ColorPaletteRegisters,
CrtcControllerIndex, CrtcControllerRegisters, EmulationMode, GeneralRegisters,
GraphicsControllerIndex, GraphicsControllerRegisters, SequencerIndex, SequencerRegisters,
},
};
use conquer_once::spin::Lazy;
@ -90,6 +91,7 @@ pub struct Vga {
graphics_controller_registers: GraphicsControllerRegisters,
attribute_controller_registers: AttributeControllerRegisters,
crtc_controller_registers: CrtcControllerRegisters,
color_palette_registers: ColorPaletteRegisters,
most_recent_video_mode: Option<VideoMode>,
}
@ -101,6 +103,7 @@ impl Vga {
graphics_controller_registers: GraphicsControllerRegisters::new(),
attribute_controller_registers: AttributeControllerRegisters::new(),
crtc_controller_registers: CrtcControllerRegisters::new(),
color_palette_registers: ColorPaletteRegisters::new(),
most_recent_video_mode: None,
}
}
@ -131,21 +134,76 @@ impl Vga {
self.most_recent_video_mode
}
/// `I/O Address Select` Bit 0 `(value & 0x1)` of MSR selects 3Bxh or 3Dxh as the I/O address for the CRT Controller
/// registers, the Feature Control Register (FCR), and Input Status Register 1 (ST01). Presently
/// ignored (whole range is claimed), but will "ignore" 3Bx for color configuration or 3Dx for
/// monochrome.
/// Note that it is typical in AGP chipsets to shadow this bit and properly steer I/O cycles to the
/// proper bus for operation where a MDA exists on another bus such as ISA.
///
/// 0 = Select 3Bxh I/O address `(EmulationMode::Mda)`
///
/// 1 = Select 3Dxh I/O address `(EmulationMode:Cga)`
fn get_emulation_mode(&mut self) -> EmulationMode {
/// Returns the current value of the miscellaneous output register.
pub fn read_msr(&mut self) -> u8 {
self.general_registers.read_msr()
}
/// Returns the current value of the sequencer register, as determined by `index`.
pub fn read_sequencer(&mut self, index: SequencerIndex) -> u8 {
self.sequencer_registers.read(index)
}
/// Returns the current value of the graphics controller register, as determined by `index`.
pub fn read_graphics_controller(&mut self, index: GraphicsControllerIndex) -> u8 {
self.graphics_controller_registers.read(index)
}
/// Returns the current value of the attribute controller register, as determined by `emulation_mode`
/// and `index`.
pub fn read_attribute_controller(
&mut self,
emulation_mode: EmulationMode,
index: AttributeControllerIndex,
) -> u8 {
self.attribute_controller_registers
.read(emulation_mode, index)
}
/// Returns the current value of the crtc controller, as determined by `emulation_mode`
/// and `index`.
pub fn read_crtc_controller(
&mut self,
emulation_mode: EmulationMode,
index: CrtcControllerIndex,
) -> u8 {
self.crtc_controller_registers.read(emulation_mode, index)
}
/// Writes `value` to the crtc controller, as determined by `index`.
pub fn write_crtc_controller(
&mut self,
emulation_mode: EmulationMode,
index: CrtcControllerIndex,
value: u8,
) {
self.crtc_controller_registers
.write(emulation_mode, index, value);
}
/// Returns the current `EmulationMode` as determined by the miscellaneous output register.
pub fn get_emulation_mode(&mut self) -> EmulationMode {
EmulationMode::from(self.general_registers.read_msr() & 0x1)
}
fn load_font(&mut self, vga_font: &VgaFont) {
/// Loads a new palette into the vga, as specified by `palette`.
///
/// Each palette must be `PALETTE_SIZE` bytes long, with every 3
/// bytes representing one color `(R, G, B)`.
pub fn load_palette(&mut self, palette: &[u8; PALETTE_SIZE]) {
self.color_palette_registers.load_palette(palette);
}
/// Reads the current vga palette into `palette`.
///
/// Each palette must be `PALETTE_SIZE` bytes long, with every 3
/// bytes representing one color `(R, G, B)`.
pub fn read_palette(&mut self, palette: &mut [u8; PALETTE_SIZE]) {
self.color_palette_registers.read_palette(palette);
}
/// Loads a vga text mode font as specified by `vga_font`.
pub fn load_font(&mut self, vga_font: &VgaFont) {
// Save registers
let (
plane_mask,
@ -284,21 +342,18 @@ impl Vga {
/// Sets the video card to Mode 40x25.
fn set_video_mode_40x25(&mut self) {
self.set_registers(&MODE_40X25_CONFIGURATION);
self.load_font(&TEXT_8X16_FONT);
self.most_recent_video_mode = Some(VideoMode::Mode40x25);
}
/// Sets the video card to Mode 40x50.
fn set_video_mode_40x50(&mut self) {
self.set_registers(&MODE_40X50_CONFIGURATION);
self.load_font(&TEXT_8X8_FONT);
self.most_recent_video_mode = Some(VideoMode::Mode40x50);
}
/// Sets the video card to Mode 80x25.
fn set_video_mode_80x25(&mut self) {
self.set_registers(&MODE_80X25_CONFIGURATION);
self.load_font(&TEXT_8X16_FONT);
self.most_recent_video_mode = Some(VideoMode::Mode80x25);
}

View file

@ -1,5 +1,5 @@
use crate::{
colors::Color16Bit,
colors::{Color16Bit, DEFAULT_PALETTE},
vga::{Plane, Vga, VideoMode, VGA},
};
use spinning_top::SpinlockGuard;
@ -16,7 +16,7 @@ static PLANES: &[Plane] = &[Plane::Plane0, Plane::Plane1, Plane::Plane2, Plane::
/// Basic usage:
///
/// ```no_run
/// use vga::Graphics640x480x16;
/// use vga::writers::Graphics640x480x16;
///
/// let graphics_mode = Graphics640x480x16::new();
///
@ -34,6 +34,7 @@ impl Graphics640x480x16 {
/// Clears the screen by setting all pixels to `Color16Bit::Black`.
pub fn clear_screen(&self) {
// TODO: Clear the screen by using 4-plane mode instead of slow `set_pixel`.
for x in 0..WIDTH {
for y in 0..HEIGHT {
self.set_pixel(x, y, Color16Bit::Yellow);
@ -72,7 +73,12 @@ impl Graphics640x480x16 {
/// Sets the graphics device to `VideoMode::Mode640x480x16`.
pub fn set_mode(&self) {
VGA.lock().set_video_mode(VideoMode::Mode640x480x16);
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);
}
/// Returns the start of the `FrameBuffer` as `*mut u8` as

View file

@ -1,18 +1,154 @@
//! Writers for common vga modes.
mod graphics_640x480x16;
mod text_40x25;
mod text_40x50;
mod text_80x25;
use super::colors::TextModeColor;
use super::{
colors::{Color16Bit, TextModeColor},
registers::CrtcControllerIndex,
vga::{Vga, VGA},
};
use spinning_top::SpinlockGuard;
pub use graphics_640x480x16::Graphics640x480x16;
pub use text_40x25::Text40x25;
pub use text_40x50::Text40x50;
pub use text_80x25::Text80x25;
/// Represents a `ScreenCharacter` in vga text modes.
#[derive(Debug, Copy, Clone)]
#[repr(C)]
struct ScreenCharacter {
pub struct ScreenCharacter {
character: u8,
color: TextModeColor,
}
impl ScreenCharacter {
/// Creates a new `ScreenCharacter` with the specified `character`
/// and a `TextModeColor` with the specified `foreground` and `background`.
pub fn new(character: u8, foreground: Color16Bit, background: Color16Bit) -> ScreenCharacter {
let color = TextModeColor::new(foreground, background);
ScreenCharacter { character, color }
}
}
static BLANK_CHARACTER: ScreenCharacter = ScreenCharacter {
character: b' ',
color: TextModeColor::new(Color16Bit::Yellow, Color16Bit::Black),
};
/// A helper trait used to interact with various vga text modes.
pub trait TextWriter {
/// Returns the width of the `TextWriter`.
fn get_width(&self) -> usize;
/// Returns the height of the `TextWriter`.
fn get_height(&self) -> usize;
/// Sets the graphics device to a video mode as determined by
/// the `TextWriter` implementation.
fn set_mode(&self);
/// Returns the start of the `FrameBuffer` as `*mut ScreenCharacter`
/// 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 ScreenCharacter) {
let mut vga = VGA.lock();
let frame_buffer = vga.get_frame_buffer();
(vga, u32::from(frame_buffer) as *mut ScreenCharacter)
}
/// Clears the screen by setting all cells to `b' '` with
/// a background color of `Color16Bit::Black` and a foreground
/// color of `Color16Bit::Yellow`.
fn clear_screen(&self) {
let (_vga, frame_buffer) = self.get_frame_buffer();
let screen_size = self.get_width() * self.get_height();
for i in 0..screen_size {
unsafe {
frame_buffer.add(i).write_volatile(BLANK_CHARACTER);
}
}
}
/// Disables the cursor in vga text modes.
fn disable_cursor(&self) {
let (mut vga, _frame_buffer) = self.get_frame_buffer();
let emulation_mode = vga.get_emulation_mode();
let cursor_start =
vga.read_crtc_controller(emulation_mode, CrtcControllerIndex::TextCursorStart);
vga.write_crtc_controller(
emulation_mode,
CrtcControllerIndex::TextCursorStart,
cursor_start | 0x20,
);
}
/// Enables the cursor in vga text modes.
fn enable_cursor(&self) {
let (mut vga, _frame_buffer) = self.get_frame_buffer();
let emulation_mode = vga.get_emulation_mode();
let cursor_start =
vga.read_crtc_controller(emulation_mode, CrtcControllerIndex::TextCursorStart);
vga.write_crtc_controller(
emulation_mode,
CrtcControllerIndex::TextCursorStart,
cursor_start & 0xDF,
);
}
/// Sets the size of the cursor, as specified by `scan_line_start` and `scan_line_end`.
///
/// This field controls the appearance of the text mode cursor by specifying the scan
/// line location within a character cell. The top most scan line is 0, with the bottom
/// determined by `CrtcControllerIndex::MaxiumumScanLine (usually 15)`.
/// If `scan_line_start > scan_line_end`, the cursor isn't drawn.
fn set_cursor(&self, scan_line_start: u8, scan_line_end: u8) {
let (mut vga, _frame_buffer) = self.get_frame_buffer();
let emulation_mode = vga.get_emulation_mode();
let cursor_start =
vga.read_crtc_controller(emulation_mode, CrtcControllerIndex::TextCursorStart) & 0xC0;
let cursor_end =
vga.read_crtc_controller(emulation_mode, CrtcControllerIndex::TextCursorEnd) & 0xE0;
vga.write_crtc_controller(
emulation_mode,
CrtcControllerIndex::TextCursorStart,
cursor_start | scan_line_start,
);
vga.write_crtc_controller(
emulation_mode,
CrtcControllerIndex::TextCursorEnd,
cursor_end | scan_line_end,
);
}
/// Sets the current text cursor to the position specified by
/// `x` and `y`.
fn set_cursor_position(&self, x: usize, y: usize) {
let offset = self.get_width() * y + x;
let (mut vga, _frame_buffer) = self.get_frame_buffer();
let emulation_mode = vga.get_emulation_mode();
let cursor_start = offset & 0xFF;
let cursor_end = (offset >> 8) & 0xFF;
vga.write_crtc_controller(
emulation_mode,
CrtcControllerIndex::TextCursorLocationLow,
cursor_start as u8,
);
vga.write_crtc_controller(
emulation_mode,
CrtcControllerIndex::TextCursorLocationHigh,
cursor_end as u8,
);
}
/// Prints the given `character` and `color` at `(x, y)`.
fn write_character(&self, x: usize, y: usize, screen_character: ScreenCharacter) {
let (_vga, frame_buffer) = self.get_frame_buffer();
let offset = self.get_width() * y + x;
unsafe {
frame_buffer.add(offset).write_volatile(screen_character);
}
}
}

View file

@ -1,18 +1,12 @@
use super::ScreenCharacter;
use super::TextWriter;
use crate::{
colors::{Color16Bit, TextModeColor},
vga::{Vga, VideoMode, VGA},
colors::DEFAULT_PALETTE,
fonts::TEXT_8X16_FONT,
vga::{VideoMode, VGA},
};
use spinning_top::SpinlockGuard;
const WIDTH: usize = 40;
const HEIGHT: usize = 25;
const SCREEN_SIZE: usize = WIDTH * HEIGHT;
static BLANK_CHARACTER: ScreenCharacter = ScreenCharacter {
character: b' ',
color: TextModeColor::new(Color16Bit::Yellow, Color16Bit::Black),
};
/// A basic interface for interacting with vga text mode 40x25
///
@ -21,60 +15,43 @@ static BLANK_CHARACTER: ScreenCharacter = ScreenCharacter {
/// Basic usage:
///
/// ```no_run
/// use vga::Text40x25;
/// use vga::colors::Color16Bit;
/// use vga::writers::{ScreenCharacter, TextWriter, Text40x25};
///
/// let text_mode = Text40x25::new();
/// let screen_character = ScreenCharacter::new(b'T', Color16Bit::Yellow, Color16Bit::Black);
///
/// text_mode.set_mode();
/// text_mode.clear_screen();
/// text_mode.write_character(0, 0, screen_character);
/// ```
#[derive(Default)]
pub struct Text40x25;
impl TextWriter for Text40x25 {
fn get_width(&self) -> usize {
WIDTH
}
fn get_height(&self) -> usize {
HEIGHT
}
/// Sets the graphics device to `VideoMode::Mode40x25`.
fn set_mode(&self) {
let mut vga = VGA.lock();
vga.set_video_mode(VideoMode::Mode40x25);
// Some bios mess up the palette when switching modes,
// so explicitly set it.
vga.load_palette(&DEFAULT_PALETTE);
vga.load_font(&TEXT_8X16_FONT);
}
}
impl Text40x25 {
/// Creates a new `Text40x25`.
pub fn new() -> Text40x25 {
Text40x25 {}
}
/// Clears the screen by setting all cells to `b' '` with
/// a background color of `Color16Bit::Black` and a foreground
/// color of `Color16Bit::Yellow`.
pub fn clear_screen(&self) {
let (_vga, frame_buffer) = self.get_frame_buffer();
for i in 0..SCREEN_SIZE {
unsafe {
frame_buffer.add(i).write_volatile(BLANK_CHARACTER);
}
}
}
/// Prints the given `character` and `color` at `(x, y)`.
///
/// Panics if `x >= 40` or `y >= 25`.
pub fn write_character(&self, x: usize, y: usize, character: u8, color: TextModeColor) {
assert!(x < WIDTH, "x >= {}", WIDTH);
assert!(y < HEIGHT, "y >= {}", HEIGHT);
let (_vga, frame_buffer) = self.get_frame_buffer();
let offset = WIDTH * y + x;
unsafe {
frame_buffer
.add(offset)
.write_volatile(ScreenCharacter { character, color });
}
}
/// Sets the graphics device to `VideoMode::Mode40x25`.
pub fn set_mode(&self) {
VGA.lock().set_video_mode(VideoMode::Mode40x25);
}
/// Returns the start of the `FrameBuffer` as `*mut ScreenCharacter`
/// 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 ScreenCharacter) {
let mut vga = VGA.lock();
let frame_buffer = vga.get_frame_buffer();
(vga, u32::from(frame_buffer) as *mut ScreenCharacter)
}
}

View file

@ -1,18 +1,12 @@
use super::ScreenCharacter;
use super::TextWriter;
use crate::{
colors::{Color16Bit, TextModeColor},
vga::{Vga, VideoMode, VGA},
colors::DEFAULT_PALETTE,
fonts::TEXT_8X8_FONT,
vga::{VideoMode, VGA},
};
use spinning_top::SpinlockGuard;
const WIDTH: usize = 40;
const HEIGHT: usize = 50;
const SCREEN_SIZE: usize = WIDTH * HEIGHT;
static BLANK_CHARACTER: ScreenCharacter = ScreenCharacter {
character: b' ',
color: TextModeColor::new(Color16Bit::Yellow, Color16Bit::Black),
};
/// A basic interface for interacting with vga text mode 40x50
///
@ -21,60 +15,43 @@ static BLANK_CHARACTER: ScreenCharacter = ScreenCharacter {
/// Basic usage:
///
/// ```no_run
/// use vga::Text40x50;
/// use vga::colors::Color16Bit;
/// use vga::writers::{ScreenCharacter, TextWriter, Text40x50};
///
/// let text_mode = Text40x50::new();
/// let screen_character = ScreenCharacter::new(b'T', Color16Bit::Yellow, Color16Bit::Black);
///
/// text_mode.set_mode();
/// text_mode.clear_screen();
/// text_mode.write_character(0, 0, screen_character);
/// ```
#[derive(Default)]
pub struct Text40x50;
impl TextWriter for Text40x50 {
fn get_width(&self) -> usize {
WIDTH
}
fn get_height(&self) -> usize {
HEIGHT
}
/// Sets the graphics device to `VideoMode::Mode40x50`.
fn set_mode(&self) {
let mut vga = VGA.lock();
vga.set_video_mode(VideoMode::Mode40x50);
// Some bios mess up the palette when switching modes,
// so explicitly set it.
vga.load_palette(&DEFAULT_PALETTE);
vga.load_font(&TEXT_8X8_FONT);
}
}
impl Text40x50 {
/// Creates a new `Text40x50`.
pub fn new() -> Text40x50 {
Text40x50 {}
}
/// Clears the screen by setting all cells to `b' '` with
/// a background color of `Color16Bit::Black` and a foreground
/// color of `Color16Bit::Yellow`.
pub fn clear_screen(&self) {
let (_vga, frame_buffer) = self.get_frame_buffer();
for i in 0..SCREEN_SIZE {
unsafe {
frame_buffer.add(i).write_volatile(BLANK_CHARACTER);
}
}
}
/// Prints the given `character` and `color` at `(x, y)`.
///
/// Panics if `x >= 40` or `y >= 50`.
pub fn write_character(&self, x: usize, y: usize, character: u8, color: TextModeColor) {
assert!(x < WIDTH, "x >= {}", WIDTH);
assert!(y < HEIGHT, "y >= {}", HEIGHT);
let (_vga, frame_buffer) = self.get_frame_buffer();
let offset = WIDTH * y + x;
unsafe {
frame_buffer
.add(offset)
.write_volatile(ScreenCharacter { character, color });
}
}
/// Sets the graphics device to `VideoMode::Mode40x50`.
pub fn set_mode(&self) {
VGA.lock().set_video_mode(VideoMode::Mode40x50);
}
/// Returns the start of the `FrameBuffer` as `*mut ScreenCharacter`
/// 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 ScreenCharacter) {
let mut vga = VGA.lock();
let frame_buffer = vga.get_frame_buffer();
(vga, u32::from(frame_buffer) as *mut ScreenCharacter)
}
}

View file

@ -1,18 +1,12 @@
use super::ScreenCharacter;
use super::TextWriter;
use crate::{
colors::{Color16Bit, TextModeColor},
vga::{Vga, VideoMode, VGA},
colors::DEFAULT_PALETTE,
fonts::TEXT_8X16_FONT,
vga::{VideoMode, VGA},
};
use spinning_top::SpinlockGuard;
const WIDTH: usize = 80;
const HEIGHT: usize = 25;
const SCREEN_SIZE: usize = WIDTH * HEIGHT;
static BLANK_CHARACTER: ScreenCharacter = ScreenCharacter {
character: b' ',
color: TextModeColor::new(Color16Bit::Yellow, Color16Bit::Black),
};
/// A basic interface for interacting with vga text mode 80x25
///
@ -21,60 +15,42 @@ static BLANK_CHARACTER: ScreenCharacter = ScreenCharacter {
/// Basic usage:
///
/// ```no_run
/// use vga::Text80x25;
/// use vga::colors::Color16Bit;
/// use vga::writers::{ScreenCharacter, TextWriter, Text80x25};
///
/// let text_mode = Text80x25::new();
/// let screen_character = ScreenCharacter::new(b'T', Color16Bit::Yellow, Color16Bit::Black);
///
/// text_mode.set_mode();
/// text_mode.clear_screen();
/// text_mode.write_character(0, 0, screen_character);
/// ```
#[derive(Default)]
pub struct Text80x25;
impl TextWriter for Text80x25 {
fn get_width(&self) -> usize {
WIDTH
}
fn get_height(&self) -> usize {
HEIGHT
}
fn set_mode(&self) {
let mut vga = VGA.lock();
vga.set_video_mode(VideoMode::Mode80x25);
// Some bios mess up the palette when switching modes,
// so explicitly set it.
vga.load_palette(&DEFAULT_PALETTE);
vga.load_font(&TEXT_8X16_FONT);
}
}
impl Text80x25 {
/// Creates a new `Text80x25`.
pub fn new() -> Text80x25 {
Text80x25 {}
}
/// Clears the screen by setting all cells to `b' '` with
/// a background color of `Color16Bit::Black` and a foreground
/// color of `Color16Bit::Yellow`.
pub fn clear_screen(&self) {
let (_vga, frame_buffer) = self.get_frame_buffer();
for i in 0..SCREEN_SIZE {
unsafe {
frame_buffer.add(i).write_volatile(BLANK_CHARACTER);
}
}
}
/// Prints the given `character` and `color` at `(x, y)`.
///
/// Panics if `x >= 80` or `y >= 25`.
pub fn write_character(&self, x: usize, y: usize, character: u8, color: TextModeColor) {
assert!(x < WIDTH, "x >= {}", WIDTH);
assert!(y < HEIGHT, "y >= {}", HEIGHT);
let (_vga, frame_buffer) = self.get_frame_buffer();
let offset = WIDTH * y + x;
unsafe {
frame_buffer
.add(offset)
.write_volatile(ScreenCharacter { character, color });
}
}
/// Sets the graphics device to `VideoMode::Mode80x25`.
pub fn set_mode(&self) {
VGA.lock().set_video_mode(VideoMode::Mode80x25);
}
/// Returns the start of the `FrameBuffer` as `*mut ScreenCharacter`
/// 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 ScreenCharacter) {
let mut vga = VGA.lock();
let frame_buffer = vga.get_frame_buffer();
(vga, u32::from(frame_buffer) as *mut ScreenCharacter)
}
}

View file

@ -5,9 +5,11 @@ authors = ["Ryan Kennedy <rkennedy9064@gmail.com>"]
edition = "2018"
[dependencies]
bootloader = "0.8.9"
bootloader = { version = "0.9.0", features = ["map_physical_memory"] }
conquer-once = { version = "0.2.0", default-features = false }
spinning_top = { version = "0.1.0", features = ["nightly"] }
pic8259_simple = "0.1.1"
vga = { path = "../" }
uart_16550 = "0.2.4"
x86_64 = "0.9.6"

77
testing/src/interrupts.rs Normal file
View file

@ -0,0 +1,77 @@
use crate::gdt;
use crate::{hlt_loop, serial_print, serial_println};
use conquer_once::spin::Lazy;
use core::convert::Into;
use pic8259_simple::ChainedPics;
use spinning_top::Spinlock;
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame, PageFaultErrorCode};
pub const PIC_1_OFFSET: u8 = 32;
pub const PIC_2_OFFSET: u8 = PIC_1_OFFSET + 8;
#[derive(Debug, Clone, Copy)]
#[repr(u8)]
pub enum InterruptIndex {
Timer = PIC_1_OFFSET,
Keyboard,
}
impl Into<u8> for InterruptIndex {
fn into(self) -> u8 {
self as u8
}
}
impl Into<usize> for InterruptIndex {
fn into(self) -> usize {
self as usize
}
}
pub static PICS: Spinlock<ChainedPics> =
Spinlock::new(unsafe { ChainedPics::new(PIC_1_OFFSET, PIC_2_OFFSET) });
static IDT: Lazy<InterruptDescriptorTable> = Lazy::new(|| {
let mut idt = InterruptDescriptorTable::new();
idt.page_fault.set_handler_fn(page_fault_handler);
idt.segment_not_present.set_handler_fn(segment_not_present);
unsafe {
idt.double_fault
.set_handler_fn(double_fault_handler)
.set_stack_index(gdt::DOUBLE_FAULT_IST_INDEX);
}
idt
});
pub fn init_idt() {
IDT.load();
}
extern "x86-interrupt" fn double_fault_handler(
stack_frame: &mut InterruptStackFrame,
_error_code: u64,
) -> ! {
panic!("EXCEPTION: DOUBLE FAULT\n{:#?}", stack_frame);
}
extern "x86-interrupt" fn page_fault_handler(
stack_frame: &mut InterruptStackFrame,
error_code: PageFaultErrorCode,
) {
use x86_64::registers::control::Cr2;
serial_print!("EXCEPTION: PAGE FAULT");
serial_println!("Accessed Address: {:?}", Cr2::read());
serial_println!("Error Code: {:?}", error_code);
serial_println!("{:#?}", stack_frame);
hlt_loop();
}
extern "x86-interrupt" fn segment_not_present(
_stack_frame: &mut InterruptStackFrame,
_error_code: u64,
) {
// For some reason this sometimes gets thrown when running tests in qemu,
// so leave empty so the tests finish for now.
}

View file

@ -1,4 +1,5 @@
#![cfg_attr(test, no_main)]
#![feature(abi_x86_interrupt)]
#![feature(custom_test_frameworks)]
#![test_runner(crate::test_runner)]
#![reexport_test_harness_main = "test_main"]
@ -6,9 +7,9 @@
use core::panic::PanicInfo;
#[macro_use]
pub mod serial;
pub mod gdt;
pub mod interrupts;
pub mod serial;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
@ -26,6 +27,12 @@ pub fn exit_qemu(exit_code: QemuExitCode) {
}
}
pub fn hlt_loop() -> ! {
loop {
x86_64::instructions::hlt();
}
}
pub fn test_runner(tests: &[&dyn Fn()]) {
serial_println!("Running {} tests", tests.len());
for test in tests {

View file

@ -1,27 +0,0 @@
#![no_std]
#![no_main]
#![feature(custom_test_frameworks)]
#![reexport_test_harness_main = "test_main"]
#![test_runner(testing::test_runner)]
use core::panic::PanicInfo;
use testing::{serial_print, serial_println};
#[no_mangle] // don't mangle the name of this function
pub extern "C" fn _start() -> ! {
test_main();
loop {}
}
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
testing::test_panic_handler(info)
}
#[test_case]
fn basic_boot() {
serial_print!("basic_boot... ");
assert_eq!(0, 0);
serial_println!("[ok]");
}

118
testing/tests/vga.rs Normal file
View file

@ -0,0 +1,118 @@
#![no_std]
#![no_main]
#![feature(custom_test_frameworks)]
#![reexport_test_harness_main = "test_main"]
#![test_runner(testing::test_runner)]
use core::panic::PanicInfo;
use testing::{gdt, interrupts, serial_print, serial_println};
use vga::colors::{DEFAULT_PALETTE, PALETTE_SIZE};
use vga::configurations::{
VgaConfiguration, MODE_40X25_CONFIGURATION, MODE_40X50_CONFIGURATION,
MODE_640X480X16_CONFIGURATION, MODE_80X25_CONFIGURATION,
};
use vga::vga::{Vga, VideoMode, VGA};
#[no_mangle] // don't mangle the name of this function
pub extern "C" fn _start() -> ! {
init();
test_main();
loop {}
}
fn init() {
gdt::init();
interrupts::init_idt();
unsafe { interrupts::PICS.lock().initialize() };
x86_64::instructions::interrupts::enable();
}
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
testing::test_panic_handler(info)
}
#[test_case]
fn set_mode_40x25() {
serial_print!("mode 40x25... ");
let mut vga = VGA.lock();
vga.set_video_mode(VideoMode::Mode40x25);
check_registers(&mut vga, &MODE_40X25_CONFIGURATION);
serial_println!("[ok]");
}
#[test_case]
fn set_mode_40x50() {
serial_print!("mode 40x50... ");
let mut vga = VGA.lock();
vga.set_video_mode(VideoMode::Mode40x50);
check_registers(&mut vga, &MODE_40X50_CONFIGURATION);
serial_println!("[ok]");
}
#[test_case]
fn set_mode_80x25() {
serial_print!("mode 80x25... ");
let mut vga = VGA.lock();
vga.set_video_mode(VideoMode::Mode80x25);
check_registers(&mut vga, &MODE_80X25_CONFIGURATION);
serial_println!("[ok]");
}
#[test_case]
fn set_mode_640x480x16() {
serial_print!("mode 640x480x16... ");
let mut vga = VGA.lock();
vga.set_video_mode(VideoMode::Mode640x480x16);
check_registers(&mut vga, &MODE_640X480X16_CONFIGURATION);
serial_println!("[ok]");
}
#[test_case]
fn load_palette() {
serial_print!("load palette... ");
let mut palette = [0u8; PALETTE_SIZE];
let mut vga = VGA.lock();
vga.load_palette(&DEFAULT_PALETTE);
vga.read_palette(&mut palette);
for i in 0..PALETTE_SIZE {
assert_eq!(palette[i], DEFAULT_PALETTE[i]);
}
serial_println!("[ok]");
}
fn check_registers(vga: &mut Vga, configuration: &VgaConfiguration) {
let emulation_mode = vga.get_emulation_mode();
assert_eq!(vga.read_msr(), configuration.miscellaneous_output);
for (index, value) in configuration.sequencer_registers {
assert_eq!(vga.read_sequencer(*index), *value);
}
for (index, value) in configuration.crtc_controller_registers {
assert_eq!(vga.read_crtc_controller(emulation_mode, *index), *value);
}
for (index, value) in configuration.graphics_controller_registers {
assert_eq!(vga.read_graphics_controller(*index), *value);
}
for (index, value) in configuration.attribute_controller_registers {
assert_eq!(
vga.read_attribute_controller(emulation_mode, *index),
*value
);
}
}