Compare commits

...

53 commits

Author SHA1 Message Date
able 773709c035 Begin work on draw_unicode_char 2022-08-04 03:36:46 -05:00
able 53317e6935 Implement bounds checks 2022-08-02 01:16:47 -05:00
RKennedy9064 2059f09675 Merge pull request #26 from clavierpaul/master
Set color palette to standard 256 VGA palette
2022-02-15 16:06:11 -06:00
Paul Clavier c2ad9a0155 Set color palette to standard 256 VGA palette
This fixes issue 18.
2022-02-15 20:01:44 +00:00
Ryan Kennedy 86fc89f060 Merge branch 'master' of https://github.com/rust-osdev/vga 2021-06-02 18:53:27 -05:00
Ryan Kennedy bd91dae004 Update version 2021-06-02 18:53:17 -05:00
RKennedy9064 a0d5ccfd2c Merge pull request #24 from rust-osdev/update-testing
Update testing
2021-05-24 20:32:52 -05:00
Ryan Kennedy f88a4cfc07 Update testing 2021-05-24 20:22:55 -05:00
RKennedy9064 1c0b29dba2 Update interrupts.rs 2021-05-24 19:27:14 -05:00
RKennedy9064 4c782615c5 Update Cargo.toml 2021-05-24 19:25:17 -05:00
RKennedy9064 46f5302cd1 Merge pull request #23 from ethindp/master
Update dependencies to latest versions to fix E0557
2021-05-19 10:09:00 -05:00
RKennedy9064 9a7efbd3ef Update Cargo.toml 2021-05-19 10:03:30 -05:00
RKennedy9064 d2f3b66381 Update Cargo.toml 2021-05-19 09:55:37 -05:00
Ethin Probst 40e62a9536 Update dependencies to latest versions to fix E0557
Signed-off-by: Ethin Probst <ethindp@protonmail.com>
2021-05-16 12:19:02 -05:00
RKennedy9064 d80449dc8e Merge pull request #22 from rust-osdev/develop
Update config/testing
2021-02-14 21:38:51 -06:00
Ryan Kennedy 8b805d7c3b Update config/testing 2021-02-14 21:31:21 -06:00
RKennedy9064 38cc3a9868 Merge pull request #20 from Pollux3737/master
fix: should now compile
2021-02-14 21:09:20 -06:00
RKennedy9064 c86da4b2ba Merge pull request #21 from rust-osdev/develop
Update workflow
2021-02-14 21:07:47 -06:00
Ryan Kennedy fd0be9b608 Update workflow 2021-02-14 21:06:06 -06:00
Paul Fournier 53395cb9ed fix: should now compile 2021-02-13 22:15:39 +01:00
RKennedy9064 1628ab2c79 Merge pull request #17 from rust-osdev/develop
Version bumps
2020-11-02 15:07:08 -06:00
Ryan Kennedy 3544c8ca79 Updated readme and version 2020-11-02 14:41:39 -06:00
Ryan Kennedy c5d21bd38c Version bumps 2020-10-30 15:11:43 -05:00
RKennedy9064 b1d71e76d8 Merge pull request #15 from rust-osdev/develop
Develop
2020-05-21 17:11:04 -05:00
Ryan Kennedy 3a5e941966 Fixing tests 2020-05-21 17:04:49 -05:00
Ryan Kennedy c711f22e63 Deprecated asm! macro 2020-05-21 16:56:17 -05:00
RKennedy9064 7ff25184ce Update rust.yml
All the cool kids are doing it 2.0
2020-04-08 22:35:51 -05:00
RKennedy9064 135edda3ab Update rust.yml
All the cool kids are doing it
2020-04-08 22:34:16 -05:00
RKennedy9064 4a5598e072 Merge pull request #14 from rust-osdev/develop
Develop
2020-04-05 18:59:12 -05:00
Ryan Kennedy 39ad683665 Changelog and version bump 2020-04-05 18:58:30 -05:00
RKennedy9064 d61a3f783a Merge pull request #13 from rust-osdev/mode-320-240
Mode 320 240
2020-04-05 18:55:53 -05:00
Ryan Kennedy 6861ca509f Comments and cleanup 2020-04-05 18:45:52 -05:00
Ryan Kennedy 41a746fd2c Always specify plane for clear screen 2020-04-05 17:49:31 -05:00
Ryan Kennedy 015c1c6597 Actually make the height 240 2020-04-05 17:17:16 -05:00
Ryan Kennedy 95f550a55e Unused imports 2020-04-04 23:14:09 -05:00
Ryan Kennedy ac80307d29 Probably don't need those locks 2020-04-04 23:08:34 -05:00
Ryan Kennedy 626ad52589 Initial setup for mode 320x240x256 2020-04-04 22:53:36 -05:00
RKennedy9064 9850223040 Fixed trait name 2020-03-31 23:02:48 -05:00
Ryan Kennedy 4f93bab9c8 Changelog and version bump 2020-03-31 22:57:56 -05:00
RKennedy9064 4816d79476 Merge pull request #12 from dbeckwith/dbeckwith.improvements
Small improvements
2020-03-31 22:29:54 -05:00
Daniel Beckwith 98e9fb3d86 Allow any character for fill_screen 2020-03-31 20:33:06 -04:00
Daniel Beckwith 6bc387cdf6 Fix lint 2020-03-31 18:37:42 -04:00
Daniel Beckwith 66b4ccf6d0 Fix lint 2020-03-31 18:35:59 -04:00
Daniel Beckwith 19d41fd146 Make constructors const 2020-03-31 18:29:41 -04:00
Daniel Beckwith 6547019382 Make screen width and height constant 2020-03-31 18:29:32 -04:00
Daniel Beckwith 88ce4a55a1 More trait impls for screen writers 2020-03-31 18:24:29 -04:00
Daniel Beckwith 8eb86d55a1 Add fill_screen 2020-03-31 18:22:55 -04:00
Daniel Beckwith 44c3821d9d More const 2020-03-31 18:20:25 -04:00
Daniel Beckwith cbad0a141e More trait impls 2020-03-31 18:20:22 -04:00
RKennedy9064 3d052e69a5 Merge pull request #10 from rust-osdev/develop
Develop
2020-03-25 20:48:06 -05:00
RKennedy9064 13b10c055b Merge pull request #9 from rust-osdev/develop
Develop
2020-03-24 19:33:02 -05:00
RKennedy9064 a84ca2a535 Merge pull request #6 from rust-osdev/develop
Upated readme
2020-03-19 10:29:28 -05:00
RKennedy9064 9ed8b8bab5 Merge pull request #5 from rust-osdev/develop
Develop
2020-03-19 10:12:57 -05:00
20 changed files with 416 additions and 169 deletions

View file

@ -29,7 +29,7 @@ jobs:
- name: Install Rustup
run: |
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain nightly
echo ::add-path::$HOME/.cargo/bin
echo "$HOME/.cargo/bin" >> $GITHUB_PATH
if: runner.os == 'macOS'
- name: "Print Rust Version"
@ -43,7 +43,7 @@ jobs:
path: binaries
key: ${{ runner.OS }}-binaries
- name: Add binaries/bin to PATH
run: echo ::add-path::$GITHUB_WORKSPACE/binaries/bin
run: echo "$GITHUB_WORKSPACE/binaries/bin" >> $GITHUB_PATH
shell: bash
- name: "Run cargo build"
@ -75,7 +75,7 @@ jobs:
- name: Install Scoop (Windows)
run: |
Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh')
echo ::add-path::$HOME\scoop\shims
echo "$HOME\scoop\shims" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
if: runner.os == 'Windows'
shell: pwsh
- name: Install QEMU (Windows)
@ -100,7 +100,7 @@ jobs:
timeout-minutes: 2
steps:
- uses: actions/checkout@v1
- run: rustup install nightly
- run: rustup toolchain install nightly --profile minimal --component rustfmt
- run: cargo +nightly fmt -- --check
clippy:
@ -109,5 +109,5 @@ jobs:
timeout-minutes: 10
steps:
- uses: actions/checkout@v1
- run: rustup install nightly
- run: rustup toolchain install nightly --profile minimal --component clippy
- run: cargo +nightly clippy -- -D warnings

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
/target
/testing/target
/testing/binaries
Cargo.lock

View file

@ -1,17 +1,12 @@
[package]
name = "vga"
version = "0.2.1"
version = "0.2.7"
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",
]
categories = [
"no-std",
]
keywords = ["vga", "no_std"]
categories = ["no-std"]
license = "MIT/Apache-2.0"
readme = "README.md"
repository = "https://github.com/rust-osdev/vga"
@ -20,11 +15,20 @@ repository = "https://github.com/rust-osdev/vga"
[dependencies]
bitflags = "1.2.1"
conquer-once = { version = "0.2.0", default-features = false }
font8x8 = { version = "0.2.5", default-features = false, features = ["unicode"] }
spinning_top = { version = "0.1.0", features = ["nightly"] }
x86_64 = "0.9.6"
spin = "0.9"
log = "*"
conquer-once = { version = "0.3.2", default-features = false }
font8x8 = { version = "0.3.1", default-features = false, features = [
"unicode",
] }
spinning_top = { version = "0.2.4", features = ["nightly"] }
x86_64 = "0.14.2"
[dependencies.num-traits]
version = "0.2.11"
version = "0.2.14"
default-features = false
[dependencies.ab_glyph]
version = "0.2.15"
default-features = false
features = ["libm"]

View file

@ -1,3 +1,35 @@
# 0.2.6
- Updatex `x86_64` to build with lastest rust nightly.
# 0.2.5
- Updated various out of date crates.
# 0.2.4
- Updated `x86_64` to fix deprecated `asm!` macro.
# 0.2.3
- Added support for 320x240x256 mode via `Graphics320x240x256`.
- Added the ability to easily get a pointer to the modes frame buffer via `GraphicsWriter::get_frame_buffer`.
# 0.2.2
## Breaking
- `Screen::get_width()` now accessed by associated constant `i.e Text80x25::WIDTH`.
- `Screen::get_height()` now accessed by associated constant `i.e Text80x25::HEIGHT`.
- `Screen::get_size()` now accessed by associated constant `i.e Text80x25::SIZE`.
- `Graphics320x200x256::new`, `Graphics640x480x16::new`, `Text40x25::new`, `Text40x50::new`, `Text80x25::new` and `ScreenCharacter::new` are now `const fn`.
- `Graphics320x200x256`, `Graphics640x480x16`, `Text40x25`, `Text40x50`, and `Text80x25` now derive `Copy` and `Clone`.
## Other
- Added `TextWriter::fill_screen(ScreenCharacter)` for convenience.
# 0.2.1
- Added `Graphics320x200x256` mode.

Binary file not shown.

Binary file not shown.

View file

@ -4,7 +4,7 @@
pub const PALETTE_SIZE: usize = 768;
/// Represents a 16 bit color used for vga display.
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum Color16 {
/// Represents the color `Black (0x0)`.
@ -48,7 +48,7 @@ impl From<Color16> for u8 {
}
/// Represents a color for vga text modes.
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct TextModeColor(u8);
@ -69,7 +69,6 @@ impl TextModeColor {
self.0 = foreground as u8;
}
}
/// Represents the default vga 256 color palette.
pub const DEFAULT_PALETTE: [u8; PALETTE_SIZE] = [
0x0, 0x0, 0x0, 0x0, 0x0, 0x2A, 0x0, 0x2A, 0x0, 0x0, 0x2A, 0x2A, 0x2A, 0x0, 0x0, 0x2A, 0x0,

View file

@ -316,7 +316,7 @@ pub const MODE_640X480X16_CONFIGURATION: VgaConfiguration = VgaConfiguration {
],
};
/// Register values for Vga mode 640x480x16 Graphics.
/// Register values for Vga mode 320x200x256 Graphics.
pub const MODE_320X200X256_CONFIGURATION: VgaConfiguration = VgaConfiguration {
// Configuration values acquired from https://www.singlix.com/trdos/archive/vga/Graphics%20in%20pmode.pdf
miscellaneous_output: 0x63,
@ -389,3 +389,77 @@ pub const MODE_320X200X256_CONFIGURATION: VgaConfiguration = VgaConfiguration {
(AttributeControllerIndex::ColorSelect, 0x00),
],
};
/// Register values for Vga mode 320x200x256x Graphics.
pub const MODE_320X240X256_CONFIGURATION: VgaConfiguration = VgaConfiguration {
// Configuration values acquired from https://www.singlix.com/trdos/archive/vga/Graphics%20in%20pmode.pdf
miscellaneous_output: 0x63,
sequencer_registers: &[
(SequencerIndex::SequencerReset, 0x03),
(SequencerIndex::ClockingMode, 0x01),
(SequencerIndex::PlaneMask, 0x0F),
(SequencerIndex::CharacterFont, 0x00),
(SequencerIndex::MemoryMode, 0x06),
],
crtc_controller_registers: &[
(CrtcControllerIndex::HorizontalTotal, 0x5F),
(CrtcControllerIndex::HorizontalDisplayEnableEnd, 0x4F),
(CrtcControllerIndex::HorizontalBlankingStart, 0x50),
(CrtcControllerIndex::HorizontalBlankingEnd, 0x82),
(CrtcControllerIndex::HorizontalSyncStart, 0x54),
(CrtcControllerIndex::HorizontalSyncEnd, 0x80),
(CrtcControllerIndex::VeritcalTotal, 0x0D),
(CrtcControllerIndex::Overflow, 0x3E),
(CrtcControllerIndex::PresetRowScan, 0x00),
(CrtcControllerIndex::MaximumScanLine, 0x41),
(CrtcControllerIndex::TextCursorStart, 0x00),
(CrtcControllerIndex::TextCursorEnd, 0x00),
(CrtcControllerIndex::StartAddressHigh, 0x00),
(CrtcControllerIndex::StartAddressLow, 0x00),
(CrtcControllerIndex::TextCursorLocationHigh, 0x00),
(CrtcControllerIndex::TextCursorLocationLow, 0x00),
(CrtcControllerIndex::VerticalSyncStart, 0xEA),
(CrtcControllerIndex::VerticalSyncEnd, 0xAC),
(CrtcControllerIndex::VerticalDisplayEnableEnd, 0xDF),
(CrtcControllerIndex::Offset, 0x28),
(CrtcControllerIndex::UnderlineLocation, 0x00),
(CrtcControllerIndex::VerticalBlankingStart, 0xE7),
(CrtcControllerIndex::VerticalBlankingEnd, 0x06),
(CrtcControllerIndex::ModeControl, 0xE3),
(CrtcControllerIndex::LineCompare, 0xFF),
],
graphics_controller_registers: &[
(GraphicsControllerIndex::SetReset, 0x00),
(GraphicsControllerIndex::EnableSetReset, 0x00),
(GraphicsControllerIndex::ColorCompare, 0x00),
(GraphicsControllerIndex::DataRotate, 0x00),
(GraphicsControllerIndex::ReadPlaneSelect, 0x00),
(GraphicsControllerIndex::GraphicsMode, 0x40),
(GraphicsControllerIndex::Miscellaneous, 0x05),
(GraphicsControllerIndex::ColorDontCare, 0x0F),
(GraphicsControllerIndex::BitMask, 0xFF),
],
attribute_controller_registers: &[
(AttributeControllerIndex::PaletteRegister0, 0x00),
(AttributeControllerIndex::PaletteRegister1, 0x01),
(AttributeControllerIndex::PaletteRegister2, 0x02),
(AttributeControllerIndex::PaletteRegister3, 0x03),
(AttributeControllerIndex::PaletteRegister4, 0x04),
(AttributeControllerIndex::PaletteRegister5, 0x05),
(AttributeControllerIndex::PaletteRegister6, 0x06),
(AttributeControllerIndex::PaletteRegister7, 0x07),
(AttributeControllerIndex::PaletteRegister8, 0x08),
(AttributeControllerIndex::PaletteRegister9, 0x09),
(AttributeControllerIndex::PaletteRegisterA, 0x0A),
(AttributeControllerIndex::PaletteRegisterB, 0x0B),
(AttributeControllerIndex::PaletteRegisterC, 0x0C),
(AttributeControllerIndex::PaletteRegisterD, 0x0D),
(AttributeControllerIndex::PaletteRegisterE, 0x0E),
(AttributeControllerIndex::PaletteRegisterF, 0x0F),
(AttributeControllerIndex::ModeControl, 0x41),
(AttributeControllerIndex::OverscanColor, 0x00),
(AttributeControllerIndex::MemoryPlaneEnable, 0x0F),
(AttributeControllerIndex::HorizontalPixelPanning, 0x00),
(AttributeControllerIndex::ColorSelect, 0x00),
],
};

View file

@ -7,6 +7,8 @@
#![no_std]
#![warn(missing_docs)]
extern crate alloc;
pub mod colors;
pub mod configurations;
pub mod drawing;

View file

@ -96,7 +96,7 @@ impl SequencerRegisters {
/// Sets the plane mask of the sequencer controller, as specified by `plane_mask`.
pub fn set_plane_mask(&mut self, plane_mask: PlaneMask) {
let original_value = self.read(SequencerIndex::PlaneMask);
let original_value = self.read(SequencerIndex::PlaneMask) & 0xF0;
self.write(
SequencerIndex::PlaneMask,
original_value | u8::from(plane_mask),

View file

@ -2,8 +2,9 @@
use super::{
configurations::{
VgaConfiguration, MODE_320X200X256_CONFIGURATION, MODE_40X25_CONFIGURATION,
MODE_40X50_CONFIGURATION, MODE_640X480X16_CONFIGURATION, MODE_80X25_CONFIGURATION,
VgaConfiguration, MODE_320X200X256_CONFIGURATION, MODE_320X240X256_CONFIGURATION,
MODE_40X25_CONFIGURATION, MODE_40X50_CONFIGURATION, MODE_640X480X16_CONFIGURATION,
MODE_80X25_CONFIGURATION,
},
fonts::VgaFont,
registers::{
@ -59,6 +60,8 @@ pub enum VideoMode {
Mode80x25,
/// Represents graphics mode 320x200x256.
Mode320x200x256,
/// Represents graphics mode 320x240x256.
Mode320x240x256,
/// Represents graphics mode 640x480x16.
Mode640x480x16,
}
@ -101,6 +104,7 @@ impl Vga {
VideoMode::Mode40x50 => self.set_video_mode_40x50(),
VideoMode::Mode80x25 => self.set_video_mode_80x25(),
VideoMode::Mode320x200x256 => self.set_video_mode_320x200x256(),
VideoMode::Mode320x240x256 => self.set_video_mode_320x240x256(),
VideoMode::Mode640x480x16 => self.set_video_mode_640x480x16(),
}
}
@ -275,6 +279,12 @@ impl Vga {
self.most_recent_video_mode = Some(VideoMode::Mode320x200x256);
}
/// Sets the video card to Mode 320x200x256x.
fn set_video_mode_320x240x256(&mut self) {
self.set_registers(&MODE_320X240X256_CONFIGURATION);
self.most_recent_video_mode = Some(VideoMode::Mode320x240x256);
}
/// Sets the video card to Mode 640x480x16.
fn set_video_mode_640x480x16(&mut self) {
self.set_registers(&MODE_640X480X16_CONFIGURATION);

View file

@ -2,10 +2,9 @@ use super::{GraphicsWriter, Screen};
use crate::{
colors::DEFAULT_PALETTE,
drawing::{Bresenham, Point},
vga::{Vga, VideoMode, VGA},
vga::{VideoMode, VGA},
};
use font8x8::UnicodeFonts;
use spinning_top::SpinlockGuard;
const WIDTH: usize = 320;
const HEIGHT: usize = 200;
@ -20,7 +19,7 @@ const SIZE: usize = WIDTH * HEIGHT;
/// ```no_run
/// use vga::colors::Color16;
/// use vga::writers::{Graphics320x200x256, GraphicsWriter};
///
/// let mode = Graphics320x200x256::new();
/// mode.set_mode();
/// mode.clear_screen(0);
@ -33,32 +32,19 @@ const SIZE: usize = WIDTH * HEIGHT;
/// mode.draw_character(118 + offset * 8, 27, character, 255);
/// }
/// ```
#[derive(Default)]
pub struct Graphics320x200x256 {}
#[derive(Debug, Clone, Copy, Default)]
pub struct Graphics320x200x256;
impl Screen for Graphics320x200x256 {
#[inline]
fn get_width(&self) -> usize {
WIDTH
}
#[inline]
fn get_height(&self) -> usize {
HEIGHT
}
#[inline]
fn get_size(&self) -> usize {
SIZE
}
const WIDTH: usize = WIDTH;
const HEIGHT: usize = HEIGHT;
const SIZE: usize = SIZE;
}
impl GraphicsWriter<u8> for Graphics320x200x256 {
fn clear_screen(&self, color: u8) {
for x in 0..WIDTH {
for y in 0..HEIGHT {
self.set_pixel(x, y, color);
}
unsafe {
self.get_frame_buffer().write_bytes(color, Self::SIZE);
}
}
fn draw_line(&self, start: Point<isize>, end: Point<isize>, color: u8) {
@ -67,10 +53,9 @@ impl GraphicsWriter<u8> for Graphics320x200x256 {
}
}
fn set_pixel(&self, x: usize, y: usize, color: u8) {
let (_vga, frame_buffer) = self.get_frame_buffer();
let offset = (y * WIDTH) + x;
unsafe {
frame_buffer.add(offset).write_volatile(color);
self.get_frame_buffer().add(offset).write_volatile(color);
}
}
fn draw_character(&self, x: usize, y: usize, character: char, color: u8) {
@ -101,16 +86,7 @@ impl GraphicsWriter<u8> for Graphics320x200x256 {
impl Graphics320x200x256 {
/// Creates a new `Graphics320x200x256`.
pub fn new() -> Graphics320x200x256 {
Graphics320x200x256 {}
}
/// 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)
pub const fn new() -> Graphics320x200x256 {
Graphics320x200x256
}
}

View file

@ -0,0 +1,102 @@
use super::{GraphicsWriter, Screen};
use crate::{
colors::DEFAULT_PALETTE,
drawing::{Bresenham, Point},
registers::PlaneMask,
vga::{VideoMode, VGA},
};
use font8x8::UnicodeFonts;
const WIDTH: usize = 320;
const HEIGHT: usize = 240;
const SIZE: usize = (WIDTH * HEIGHT) / 4;
/// A basic interface for interacting with vga graphics mode 320x200x256.
///
/// # Examples
///
/// Basic usage:
///
/// ```no_run
/// use vga::colors::Color16;
/// use vga::writers::{Graphics320x240x256, GraphicsWriter};
///
/// let mode = Graphics320x240x256::new();
/// mode.set_mode();
/// mode.clear_screen(0);
/// mode.draw_line((60, 20), (260, 20), 255);
/// mode.draw_line((60, 20), (60, 180), 255);
/// mode.draw_line((60, 180), (260, 180), 255);
/// mode.draw_line((260, 180), (260, 20), 255);
/// mode.draw_line((60, 40), (260, 40), 255);
/// for (offset, character) in "Hello World!".chars().enumerate() {
/// mode.draw_character(118 + offset * 8, 27, character, 255);
/// }
/// ```
#[derive(Debug, Clone, Copy, Default)]
pub struct Graphics320x240x256;
impl Screen for Graphics320x240x256 {
const WIDTH: usize = WIDTH;
const HEIGHT: usize = HEIGHT;
const SIZE: usize = SIZE;
}
impl GraphicsWriter<u8> for Graphics320x240x256 {
fn clear_screen(&self, color: u8) {
let frame_buffer = self.get_frame_buffer();
VGA.lock()
.sequencer_registers
.set_plane_mask(PlaneMask::ALL_PLANES);
unsafe {
frame_buffer.write_bytes(color, Self::SIZE);
}
}
fn draw_line(&self, start: Point<isize>, end: Point<isize>, color: u8) {
for (x, y) in Bresenham::new(start, end) {
self.set_pixel(x as usize, y as usize, color);
}
}
fn set_pixel(&self, x: usize, y: usize, color: u8) {
let frame_buffer = self.get_frame_buffer();
unsafe {
let offset = (WIDTH * y + x) / 4;
let plane_mask = 0x1 << (x & 3);
VGA.lock()
.sequencer_registers
.set_plane_mask(PlaneMask::from_bits(plane_mask).unwrap());
frame_buffer.add(offset).write_volatile(color);
}
}
fn draw_character(&self, x: usize, y: usize, character: char, color: u8) {
let character = match font8x8::BASIC_FONTS.get(character) {
Some(character) => character,
// Default to a filled block if the character isn't found
None => font8x8::unicode::BLOCK_UNICODE[8].byte_array(),
};
for (row, byte) in character.iter().enumerate() {
for bit in 0..8 {
match *byte & 1 << bit {
0 => (),
_ => self.set_pixel(x + bit, y + row, color),
}
}
}
}
fn set_mode(&self) {
let mut vga = VGA.lock();
vga.set_video_mode(VideoMode::Mode320x240x256);
// Some bios mess up the palette when switching modes,
// so explicitly set it.
vga.color_palette_registers.load_palette(&DEFAULT_PALETTE);
}
}
impl Graphics320x240x256 {
/// Creates a new `Graphics320x240x256`.
pub const fn new() -> Graphics320x240x256 {
Graphics320x240x256
}
}

View file

@ -3,15 +3,14 @@ use crate::{
colors::{Color16, DEFAULT_PALETTE},
drawing::{Bresenham, Point},
registers::{PlaneMask, WriteMode},
vga::{Vga, VideoMode, VGA},
vga::{VideoMode, VGA},
};
use core::convert::From;
use font8x8::UnicodeFonts;
use spinning_top::SpinlockGuard;
const WIDTH: usize = 640;
const HEIGHT: usize = 480;
const SIZE: usize = WIDTH * HEIGHT;
const ALL_PLANES_SCREEN_SIZE: usize = (WIDTH * HEIGHT) / 8;
const SIZE: usize = (WIDTH * HEIGHT) / 8;
const WIDTH_IN_BYTES: usize = WIDTH / 8;
/// A basic interface for interacting with vga graphics mode 640x480x16
@ -36,29 +35,21 @@ const WIDTH_IN_BYTES: usize = WIDTH / 8;
/// mode.draw_character(270 + offset * 8, 72, character, Color16::White)
/// }
/// ```
#[derive(Default)]
#[derive(Debug, Clone, Copy, Default)]
pub struct Graphics640x480x16;
impl Screen for Graphics640x480x16 {
fn get_width(&self) -> usize {
WIDTH
}
fn get_height(&self) -> usize {
HEIGHT
}
fn get_size(&self) -> usize {
SIZE
}
const WIDTH: usize = WIDTH;
const HEIGHT: usize = HEIGHT;
const SIZE: usize = SIZE;
}
impl GraphicsWriter<Color16> for Graphics640x480x16 {
fn clear_screen(&self, color: Color16) {
self.set_write_mode_2();
let (_vga, frame_buffer) = self.get_frame_buffer();
for offset in 0..ALL_PLANES_SCREEN_SIZE {
unsafe {
frame_buffer.add(offset).write_volatile(u8::from(color));
}
self.get_frame_buffer()
.write_bytes(u8::from(color), Self::SIZE);
}
}
@ -108,12 +99,12 @@ impl GraphicsWriter<Color16> for Graphics640x480x16 {
impl Graphics640x480x16 {
/// Creates a new `Graphics640x480x16`.
pub fn new() -> Graphics640x480x16 {
Graphics640x480x16 {}
pub const fn new() -> Graphics640x480x16 {
Graphics640x480x16
}
fn set_write_mode_0(&self, color: Color16) {
let (mut vga, _frame_buffer) = self.get_frame_buffer();
fn set_write_mode_0(self, color: Color16) {
let mut vga = VGA.lock();
vga.graphics_controller_registers.write_set_reset(color);
vga.graphics_controller_registers
.write_enable_set_reset(0xF);
@ -121,8 +112,8 @@ impl Graphics640x480x16 {
.set_write_mode(WriteMode::Mode0);
}
fn set_write_mode_2(&self) {
let (mut vga, _frame_buffer) = self.get_frame_buffer();
fn set_write_mode_2(self) {
let mut vga = VGA.lock();
vga.graphics_controller_registers
.set_write_mode(WriteMode::Mode2);
vga.graphics_controller_registers.set_bit_mask(0xFF);
@ -130,24 +121,91 @@ impl Graphics640x480x16 {
.set_plane_mask(PlaneMask::ALL_PLANES);
}
/// 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)
}
#[inline]
fn _set_pixel(&self, x: usize, y: usize, color: Color16) {
let (mut vga, frame_buffer) = self.get_frame_buffer();
fn _set_pixel(self, x: usize, y: usize, color: Color16) {
if x < 640 && y < 480 {
let frame_buffer = self.get_frame_buffer();
let offset = x / 8 + y * WIDTH_IN_BYTES;
let pixel_mask = 0x80 >> (x & 0x07);
vga.graphics_controller_registers.set_bit_mask(pixel_mask);
VGA.lock()
.graphics_controller_registers
.set_bit_mask(pixel_mask);
unsafe {
frame_buffer.add(offset).read_volatile();
frame_buffer.add(offset).write_volatile(u8::from(color));
}
}
}
}
use ab_glyph::{Font, FontRef, Glyph};
use log::trace;
use spin::Lazy;
const FONT_BASIC: Lazy<FontRef> = Lazy::new({
|| FontRef::try_from_slice(include_bytes!("../../assets/fonts/unifont-14.0.01.ttf")).unwrap()
});
const FONT_SUPPLEMENTARY: Lazy<FontRef> = Lazy::new(|| {
FontRef::try_from_slice(include_bytes!(
"../../assets/fonts/unifont_upper-14.0.01.ttf"
))
.unwrap()
});
const FONT_SCALE: f32 = 14.0;
const GLYPH_HEIGHT: f32 = 10.0;
const GLYPH_WIDTH: f32 = 8.0;
impl Graphics640x480x16 {
/// Draw a glyph on the screen at the given position
///
/// # Arguments
/// * `x` - the x position of the glyph
/// * `y` - the y position of the glyph
/// * `glyph` - the glyph to draw
/// * `color` - the color of the glyph
pub fn draw_unicode_char(&self, mut x: usize, mut y: usize, character: char, color: Color16) {
self.set_write_mode_2();
// trace!("Char {}", character);
// if character == '\n' {
// return;
// }
if x == 80 {
x = 0;
y += 1;
}
if character != '\0' {
let real_x = x as f32 * GLYPH_WIDTH;
let real_y = (y as f32 + 1.0) * GLYPH_HEIGHT;
// trace!("REALX{}\nREALY{}", real_x, real_y);
let in_supp_plane = character as u32 > 0xffff;
let plane = match in_supp_plane {
false => FONT_BASIC,
true => FONT_SUPPLEMENTARY,
};
let q_glyph: Glyph = plane
.glyph_id(character)
.with_scale_and_position(FONT_SCALE, ab_glyph::point(real_x, real_y));
if let Some(q) = plane.outline_glyph(q_glyph) {
q.draw(|gx, gy, c| {
if c > 0.1 {
let corner = q.px_bounds().min;
self._set_pixel(
gx as usize + corner.x as usize,
gy as usize + corner.y as usize,
color,
);
}
});
}
}
}
}

View file

@ -1,5 +1,6 @@
//! Writers for common vga modes.
mod graphics_320x200x256;
mod graphics_320x240x256;
mod graphics_640x480x16;
mod text_40x25;
mod text_40x50;
@ -14,13 +15,14 @@ use super::{
use spinning_top::SpinlockGuard;
pub use graphics_320x200x256::Graphics320x200x256;
pub use graphics_320x240x256::Graphics320x240x256;
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)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[repr(C)]
pub struct ScreenCharacter {
character: u8,
@ -29,7 +31,7 @@ pub struct ScreenCharacter {
impl ScreenCharacter {
/// Creates a new `ScreenCharacter` with the specified `character` and `TextModeColor`.
pub fn new(character: u8, color: TextModeColor) -> ScreenCharacter {
pub const fn new(character: u8, color: TextModeColor) -> ScreenCharacter {
ScreenCharacter { character, color }
}
@ -51,12 +53,12 @@ static BLANK_CHARACTER: ScreenCharacter = ScreenCharacter {
/// A helper trait used to interact with various vga screens.
pub trait Screen {
/// Returns the width of the `Screen`.
fn get_width(&self) -> usize;
/// Returns the height of the `Screen`.
fn get_height(&self) -> usize;
/// Returns the size of the `Screen`.
fn get_size(&self) -> usize;
/// The width of the `Screen`.
const WIDTH: usize;
/// The height of the `Screen`.
const HEIGHT: usize;
/// The size (total area) of the `Screen`.
const SIZE: usize;
}
/// A helper trait used to interact with various vga text modes.
@ -78,11 +80,15 @@ pub trait TextWriter: Screen {
/// a background color of `Color16::Black` and a foreground
/// color of `Color16::Yellow`.
fn clear_screen(&self) {
self.fill_screen(BLANK_CHARACTER);
}
/// Fills the screen by setting all cells to the given screen character.
fn fill_screen(&self, character: ScreenCharacter) {
let (_vga, frame_buffer) = self.get_frame_buffer();
let screen_size = self.get_width() * self.get_height();
for i in 0..screen_size {
for i in 0..Self::SIZE {
unsafe {
frame_buffer.add(i).write_volatile(BLANK_CHARACTER);
frame_buffer.add(i).write_volatile(character);
}
}
}
@ -118,7 +124,7 @@ pub trait TextWriter: Screen {
/// Returns the `ScreenCharacter` at the given `(x, y)` position.
fn read_character(&self, x: usize, y: usize) -> ScreenCharacter {
let (_vga, frame_buffer) = self.get_frame_buffer();
let offset = self.get_width() * y + x;
let offset = Self::WIDTH * y + x;
unsafe { frame_buffer.add(offset).read_volatile() }
}
@ -154,7 +160,7 @@ pub trait TextWriter: Screen {
/// 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 offset = Self::WIDTH * y + x;
let (mut vga, _frame_buffer) = self.get_frame_buffer();
let emulation_mode = vga.get_emulation_mode();
let cursor_start = offset & 0xFF;
@ -174,7 +180,7 @@ pub trait TextWriter: Screen {
/// 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;
let offset = Self::WIDTH * y + x;
unsafe {
frame_buffer.add(offset).write_volatile(screen_character);
}
@ -193,4 +199,8 @@ pub trait GraphicsWriter<Color> {
fn set_pixel(&self, x: usize, y: usize, color: Color);
/// Sets the graphics device to a `VideoMode`.
fn set_mode(&self);
/// Returns the frame buffer for this vga mode.
fn get_frame_buffer(&self) -> *mut u8 {
u32::from(VGA.lock().get_frame_buffer()) as *mut u8
}
}

View file

@ -7,6 +7,7 @@ use crate::{
const WIDTH: usize = 40;
const HEIGHT: usize = 25;
const SIZE: usize = WIDTH * HEIGHT;
/// A basic interface for interacting with vga text mode 40x25
///
@ -26,21 +27,13 @@ const HEIGHT: usize = 25;
/// text_mode.clear_screen();
/// text_mode.write_character(0, 0, screen_character);
/// ```
#[derive(Default)]
#[derive(Debug, Clone, Copy, Default)]
pub struct Text40x25;
impl Screen for Text40x25 {
fn get_width(&self) -> usize {
WIDTH
}
fn get_height(&self) -> usize {
HEIGHT
}
fn get_size(&self) -> usize {
WIDTH * HEIGHT
}
const WIDTH: usize = WIDTH;
const HEIGHT: usize = HEIGHT;
const SIZE: usize = SIZE;
}
impl TextWriter for Text40x25 {
@ -58,7 +51,7 @@ impl TextWriter for Text40x25 {
impl Text40x25 {
/// Creates a new `Text40x25`.
pub fn new() -> Text40x25 {
Text40x25 {}
pub const fn new() -> Text40x25 {
Text40x25
}
}

View file

@ -7,6 +7,7 @@ use crate::{
const WIDTH: usize = 40;
const HEIGHT: usize = 50;
const SIZE: usize = WIDTH * HEIGHT;
/// A basic interface for interacting with vga text mode 40x50
///
@ -26,21 +27,13 @@ const HEIGHT: usize = 50;
/// text_mode.clear_screen();
/// text_mode.write_character(0, 0, screen_character);
/// ```
#[derive(Default)]
#[derive(Debug, Clone, Copy, Default)]
pub struct Text40x50;
impl Screen for Text40x50 {
fn get_width(&self) -> usize {
WIDTH
}
fn get_height(&self) -> usize {
HEIGHT
}
fn get_size(&self) -> usize {
WIDTH * HEIGHT
}
const WIDTH: usize = WIDTH;
const HEIGHT: usize = HEIGHT;
const SIZE: usize = SIZE;
}
impl TextWriter for Text40x50 {
@ -58,7 +51,7 @@ impl TextWriter for Text40x50 {
impl Text40x50 {
/// Creates a new `Text40x50`.
pub fn new() -> Text40x50 {
Text40x50 {}
pub const fn new() -> Text40x50 {
Text40x50
}
}

View file

@ -7,6 +7,7 @@ use crate::{
const WIDTH: usize = 80;
const HEIGHT: usize = 25;
const SIZE: usize = WIDTH * HEIGHT;
/// A basic interface for interacting with vga text mode 80x25
///
@ -26,21 +27,13 @@ const HEIGHT: usize = 25;
/// text_mode.clear_screen();
/// text_mode.write_character(0, 0, screen_character);
/// ```
#[derive(Default)]
#[derive(Debug, Clone, Copy, Default)]
pub struct Text80x25;
impl Screen for Text80x25 {
fn get_width(&self) -> usize {
WIDTH
}
fn get_height(&self) -> usize {
HEIGHT
}
fn get_size(&self) -> usize {
WIDTH * HEIGHT
}
const WIDTH: usize = WIDTH;
const HEIGHT: usize = HEIGHT;
const SIZE: usize = SIZE;
}
impl TextWriter for Text80x25 {
@ -57,7 +50,7 @@ impl TextWriter for Text80x25 {
impl Text80x25 {
/// Creates a new `Text80x25`.
pub fn new() -> Text80x25 {
Text80x25 {}
pub const fn new() -> Text80x25 {
Text80x25
}
}

View file

@ -5,13 +5,13 @@ authors = ["Ryan Kennedy <rkennedy9064@gmail.com>"]
edition = "2018"
[dependencies]
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"
bootloader = { version = "0.9.11", features = ["map_physical_memory"] }
conquer-once = { version = "0.3.2", default-features = false }
spinning_top = { version = "0.2.4", features = ["nightly"] }
pic8259 = "0.10.1"
vga = { path = "../" }
uart_16550 = "0.2.4"
x86_64 = "0.9.6"
uart_16550 = "0.2.14"
x86_64 = "0.14.2"
[package.metadata.bootimage]
test-args = [

View file

@ -2,7 +2,7 @@ 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 pic8259::ChainedPics;
use spinning_top::Spinlock;
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame, PageFaultErrorCode};
@ -49,14 +49,14 @@ pub fn init_idt() {
}
extern "x86-interrupt" fn double_fault_handler(
stack_frame: &mut InterruptStackFrame,
stack_frame: InterruptStackFrame,
_error_code: u64,
) -> ! {
panic!("EXCEPTION: DOUBLE FAULT\n{:#?}", stack_frame);
}
extern "x86-interrupt" fn page_fault_handler(
stack_frame: &mut InterruptStackFrame,
stack_frame: InterruptStackFrame,
error_code: PageFaultErrorCode,
) {
use x86_64::registers::control::Cr2;
@ -69,7 +69,7 @@ extern "x86-interrupt" fn page_fault_handler(
}
extern "x86-interrupt" fn segment_not_present(
_stack_frame: &mut InterruptStackFrame,
_stack_frame: InterruptStackFrame,
_error_code: u64,
) {
// For some reason this sometimes gets thrown when running tests in qemu,