Compare commits

...

13 Commits

Author SHA1 Message Date
Ryan Kennedy 8b117b0222 Some rectangle stuff 2020-04-16 23:32:20 -05:00
Ryan Kennedy 3d2c6dd257 Bringing it back 2020-04-16 21:33:17 -05:00
Ryan Kennedy dae60ddf6b Be able to use a trait object 2020-04-16 20:52:07 -05:00
Ryan Kennedy ed2debf7d6 GraphicsWriter changes 2020-04-16 17:49:18 -05:00
Ryan Kennedy 64d5af344b Make get_frame_buffer more generic 2020-04-16 15:47:46 -05:00
Ryan Kennedy 4be5c807a6 Remove some options 2020-04-13 23:57:33 -05:00
Ryan Kennedy c8f38272d3 Add some rectangles 2020-04-13 03:14:03 -05:00
Ryan Kennedy 08a66bd134 Basic lines and rectangles and pixels 2020-04-13 02:50:02 -05:00
Ryan Kennedy ee36e4bea9 Only support 32bpp 2020-04-12 20:03:56 -05:00
Ryan Kennedy 26eb8aee9e Allow virtual address to be changed 2020-04-12 18:48:33 -05:00
Ryan Kennedy 11f9a46e54 Added physical and virtual addresses 2020-04-10 22:45:26 -05:00
Ryan Kennedy f09c69315d Setting some colors 2020-04-09 19:45:45 -05:00
Ryan Kennedy 0676e39214 Initial setup/testing 2020-04-06 22:29:29 -05:00
9 changed files with 495 additions and 13 deletions

245
src/devices/bochs.rs Normal file
View File

@ -0,0 +1,245 @@
use super::pci::{find_pci_device, PciDevice};
use crate::{
drawing::{Bresenham, Point, Rectangle},
writers::GraphicsWriter,
};
use font8x8::UnicodeFonts;
use x86_64::{instructions::port::Port, PhysAddr, VirtAddr};
const BOCHS_ID: u32 = 0x1111_1234;
const BOCHS_INDEX_PORT_ADDRESS: u16 = 0x01CE;
const BOCHS_DATA_PORT_ADDRESS: u16 = 0x01CF;
const VBE_DISPI_INDEX_XRES: u16 = 0x01;
const VBE_DISPI_INDEX_YRES: u16 = 0x02;
const VBE_DISPI_INDEX_BPP: u16 = 0x03;
const VBE_DISPI_INDEX_ENABLE: u16 = 0x04;
const VBE_DISPI_DISABLED: u16 = 0x00;
const VBE_DISPI_ENABLED: u16 = 0x01;
const VBE_DISPI_GETCAPS: u16 = 0x02;
const VBE_DISPI_LFB_ENABLED: u16 = 0x40;
const VBE_DISPI_BPP_32: u16 = 0x20;
#[derive(Debug, Copy, Clone, Default)]
pub struct Resolution {
width: usize,
height: usize,
}
impl Resolution {
pub fn new(width: usize, height: usize) -> Resolution {
Resolution { width, height }
}
}
#[derive(Debug)]
pub struct BochsDevice {
index_port: Port<u16>,
data_port: Port<u16>,
pci_device: PciDevice,
physical_address: PhysAddr,
virtual_address: VirtAddr,
resolution: Resolution,
}
impl BochsDevice {
pub fn new(width: usize, height: usize) -> BochsDevice {
let pci_device = find_pci_device(BOCHS_ID).expect("no bochs device found");
let index_port = Port::new(BOCHS_INDEX_PORT_ADDRESS);
let data_port = Port::new(BOCHS_DATA_PORT_ADDRESS);
let base_address = pci_device.base_addresses[0] & 0xFFFF_FFF0;
let physical_address = PhysAddr::new(base_address as u64);
let virtual_address = VirtAddr::new(base_address as u64);
let resolution = Resolution::new(width, height);
BochsDevice {
pci_device,
index_port,
data_port,
physical_address,
virtual_address,
resolution,
}
}
/// The physical address the frame buffer is mapped to.
pub fn physical_address(&self) -> PhysAddr {
self.physical_address
}
/// The virtual address that's written to for graphics operations.
///
/// **Note:** This address is set to the `physical_address` of the frame buffer
/// by default. If you map the `physical_address` to a different location, `virtual_address`
/// must be set using `set_virtual_address`.
pub fn virtual_address(&self) -> VirtAddr {
self.virtual_address
}
/// Sets the `virtual_address` that's written to for graphics operations.
pub fn set_virtual_address(&mut self, virtual_address: VirtAddr) {
self.virtual_address = virtual_address;
}
/// Returns the max capabilities supported by the `BochsDevice`.
pub fn capabilities(&mut self) -> Resolution {
unsafe {
// Save original value of VBE_DISPI_INDEX_ENABLE
self.index_port.write(VBE_DISPI_INDEX_ENABLE);
let original_value = self.data_port.read();
self.data_port.write(VBE_DISPI_GETCAPS);
// Read max width
self.index_port.write(VBE_DISPI_INDEX_XRES);
let width = self.data_port.read() as usize;
// Read max height
self.index_port.write(VBE_DISPI_INDEX_YRES);
let height = self.data_port.read() as usize;
// Restore original VBE_DISPI_INDEX_ENABLE
self.index_port.write(VBE_DISPI_INDEX_ENABLE);
self.data_port.write(original_value);
Resolution { width, height }
}
}
fn disable_display(&mut self) {
unsafe {
self.index_port.write(VBE_DISPI_INDEX_ENABLE);
self.data_port.write(VBE_DISPI_DISABLED);
}
}
fn enable_display(&mut self) {
unsafe {
self.index_port.write(VBE_DISPI_INDEX_ENABLE);
self.data_port
.write(VBE_DISPI_ENABLED | VBE_DISPI_LFB_ENABLED);
}
}
/// Returns the current resolution the `BochsDevice` is set to.
pub fn current_resolution(&mut self) -> Resolution {
let width = self.get_width();
let height = self.get_height();
Resolution { width, height }
}
/// Sets the `BochsDevice` to the given `resolution`.
pub fn set_resolution(&mut self, resolution: Resolution) {
self.resolution = resolution;
}
fn get_width(&mut self) -> usize {
unsafe {
self.index_port.write(VBE_DISPI_INDEX_XRES);
self.data_port.read() as usize
}
}
fn get_height(&mut self) -> usize {
unsafe {
self.index_port.write(VBE_DISPI_INDEX_YRES);
self.data_port.read() as usize
}
}
fn set_width(&mut self, width: usize) {
unsafe {
self.index_port.write(VBE_DISPI_INDEX_XRES);
self.data_port.write(width as u16);
}
}
fn set_height(&mut self, height: usize) {
unsafe {
self.index_port.write(VBE_DISPI_INDEX_YRES);
self.data_port.write(height as u16);
}
}
fn set_bpp(&mut self) {
unsafe {
self.index_port.write(VBE_DISPI_INDEX_BPP);
self.data_port.write(VBE_DISPI_BPP_32);
}
}
}
impl GraphicsWriter<u32> for BochsDevice {
fn clear_screen(&self, color: u32) {
let screen_size = self.resolution.width * self.resolution.height;
let frame_buffer = self.virtual_address.as_mut_ptr::<u32>();
for offset in 0..screen_size {
unsafe {
frame_buffer.add(offset).write_volatile(color);
}
}
}
fn draw_character(&self, x: usize, y: usize, character: char, color: u32) {
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 draw_rectangle(&self, rectangle: &Rectangle, color: u32) {
let p1 = (rectangle.x as isize, rectangle.y as isize);
let p2 = (rectangle.x as isize, rectangle.bottom() as isize);
let p3 = (rectangle.right() as isize, rectangle.bottom() as isize);
let p4 = (rectangle.right() as isize, rectangle.y as isize);
self.draw_line(p1, p2, color);
self.draw_line(p2, p3, color);
self.draw_line(p3, p4, color);
self.draw_line(p4, p1, color);
}
fn draw_filled_rectangle(&self, rectangle: &Rectangle, color: u32) {
for y in rectangle.y..rectangle.bottom() {
for x in rectangle.x..rectangle.right() {
self.set_pixel(x as usize, y as usize, color);
}
}
}
fn draw_line(&self, start: Point<isize>, end: Point<isize>, color: u32) {
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: u32) {
let offset = (y * self.resolution.width) + x;
unsafe {
self.virtual_address
.as_mut_ptr::<u32>()
.add(offset)
.write_volatile(color);
}
}
fn get_frame_buffer(&self) -> *mut u32 {
self.virtual_address.as_mut_ptr()
}
fn set_mode(&mut self) {
self.disable_display();
self.set_width(self.resolution.width);
self.set_height(self.resolution.height);
self.set_bpp();
self.enable_display();
}
}

4
src/devices/mod.rs Normal file
View File

@ -0,0 +1,4 @@
pub mod bochs;
mod pci;
pub use bochs::{BochsDevice, Resolution};

126
src/devices/pci.rs Normal file
View File

@ -0,0 +1,126 @@
use x86_64::instructions::port::Port;
const BUSSES: u32 = 256;
const SLOTS: u32 = 32;
const FUNCTIONS: u32 = 8;
const CONFIG_ADDRESS: u16 = 0xCF8;
const DATA_ADDRESS: u16 = 0xCFC;
#[derive(Debug, Copy, Clone)]
pub(crate) struct PciDevice {
vendor_id: u16,
device_id: u16,
command: u16,
status: u16,
revision_id: u8,
prog_if: u8,
sub_class: u8,
base_class: u8,
cache_line_size: u8,
latency_timer: u8,
header_type: u8,
bist: u8,
pub(crate) base_addresses: [u32; 6],
cis: u32,
sub_vendor_id: u16,
sub_system_id: u16,
rom_base_address: u32,
reserved_2: [u32; 2],
interrupt_line: u8,
interrupt_pin: u8,
minimum_grant: u8,
maximum_latency: u8,
}
pub(crate) fn find_pci_device(device_id: u32) -> Option<PciDevice> {
for bus in 0..BUSSES {
for slot in 0..SLOTS {
for function in 0..FUNCTIONS {
let address = (bus << 16) | (slot << 11) | (function << 8) | 0x8000_0000;
if read_offset(address, 0) == device_id {
return Some(read_device(address));
}
}
}
}
None
}
fn read_device(address: u32) -> PciDevice {
let (device_id, vendor_id) = read_2_words(address, 0x00);
let (status, command) = read_2_words(address, 0x04);
let (base_class, sub_class, prog_if, revision_id) = read_4_bytes(address, 0x08);
let (bist, header_type, latency_timer, cache_line_size) = read_4_bytes(address, 0x0C);
let mut base_addresses = [0u32; 6];
base_addresses[0] = read_offset(address, 0x10);
base_addresses[1] = read_offset(address, 0x14);
base_addresses[2] = read_offset(address, 0x18);
base_addresses[3] = read_offset(address, 0x1C);
base_addresses[4] = read_offset(address, 0x20);
base_addresses[5] = read_offset(address, 0x24);
let cis = read_offset(address, 0x28);
let (sub_system_id, sub_vendor_id) = read_2_words(address, 0x2C);
let rom_base_address = read_offset(address, 0x30);
let mut reserved_2 = [0u32; 2];
reserved_2[0] = read_offset(address, 0x34);
reserved_2[1] = read_offset(address, 0x38);
let (maximum_latency, minimum_grant, interrupt_pin, interrupt_line) =
read_4_bytes(address, 0x3C);
PciDevice {
vendor_id,
device_id,
status,
command,
base_class,
sub_class,
prog_if,
revision_id,
bist,
header_type,
latency_timer,
cache_line_size,
base_addresses,
cis,
sub_system_id,
sub_vendor_id,
rom_base_address,
reserved_2,
maximum_latency,
minimum_grant,
interrupt_pin,
interrupt_line,
}
}
fn read_2_words(address: u32, offset: u32) -> (u16, u16) {
let value = read_offset(address, offset);
let high_word = (value >> 16) as u16;
let low_word = value as u16;
(high_word, low_word)
}
fn read_4_bytes(address: u32, offset: u32) -> (u8, u8, u8, u8) {
let value = read_offset(address, offset);
let high_word_high_byte = (value >> 24) as u8;
let high_word_low_byte = (value >> 16) as u8;
let low_word_high_byte = (value >> 8) as u8;
let low_wird_low_byte = value as u8;
(
high_word_high_byte,
high_word_low_byte,
low_word_high_byte,
low_wird_low_byte,
)
}
fn read_offset(mut address: u32, offset: u32) -> u32 {
let mut config_address = Port::new(CONFIG_ADDRESS);
let mut data_address = Port::new(DATA_ADDRESS);
address |= offset & 0xFC;
unsafe {
config_address.write(address);
data_address.read()
}
}

View File

@ -11,6 +11,33 @@ use octant::Octant;
/// A point in 2D space.
pub type Point<T> = (T, T);
#[derive(Debug, Copy, Clone)]
pub struct Rectangle {
pub x: usize,
pub y: usize,
pub width: usize,
pub height: usize,
}
impl Rectangle {
pub fn new(x: usize, y: usize, width: usize, height: usize) -> Rectangle {
Rectangle {
x,
y,
width,
height,
}
}
pub fn bottom(&self) -> usize {
self.y + self.height
}
pub fn right(&self) -> usize {
self.x + self.width
}
}
pub(crate) trait SignedNum: Signed + Ord + Copy + NumCast + NumAssignOps {
fn cast<T: NumCast>(value: T) -> Self {
NumCast::from(value).unwrap()

View File

@ -9,6 +9,7 @@
pub mod colors;
pub mod configurations;
pub mod devices;
pub mod drawing;
pub mod fonts;
pub mod registers;

View File

@ -1,7 +1,7 @@
use super::{GraphicsWriter, Screen};
use crate::{
colors::DEFAULT_PALETTE,
drawing::{Bresenham, Point},
drawing::{Bresenham, Point, Rectangle},
vga::{VideoMode, VGA},
};
use font8x8::UnicodeFonts;
@ -47,17 +47,20 @@ impl GraphicsWriter<u8> for Graphics320x200x256 {
self.get_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 offset = (y * WIDTH) + x;
unsafe {
self.get_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,
@ -74,7 +77,31 @@ impl GraphicsWriter<u8> for Graphics320x200x256 {
}
}
}
fn set_mode(&self) {
fn draw_rectangle(&self, rectangle: &Rectangle, color: u8) {
let p1 = (rectangle.x as isize, rectangle.y as isize);
let p2 = (rectangle.x as isize, rectangle.bottom() as isize);
let p3 = (rectangle.right() as isize, rectangle.bottom() as isize);
let p4 = (rectangle.right() as isize, rectangle.y as isize);
self.draw_line(p1, p2, color);
self.draw_line(p2, p3, color);
self.draw_line(p3, p4, color);
self.draw_line(p4, p1, color);
}
fn draw_filled_rectangle(&self, rectangle: &Rectangle, color: u8) {
for y in rectangle.y..rectangle.bottom() {
for x in rectangle.x..rectangle.right() {
self.set_pixel(x as usize, y as usize, color);
}
}
}
fn get_frame_buffer(&self) -> *mut u8 {
u32::from(VGA.lock().get_frame_buffer()) as *mut u8
}
fn set_mode(&mut self) {
let mut vga = VGA.lock();
vga.set_video_mode(VideoMode::Mode320x200x256);

View File

@ -1,7 +1,7 @@
use super::{GraphicsWriter, Screen};
use crate::{
colors::DEFAULT_PALETTE,
drawing::{Bresenham, Point},
drawing::{Bresenham, Point, Rectangle},
registers::PlaneMask,
vga::{VideoMode, VGA},
};
@ -52,11 +52,13 @@ impl GraphicsWriter<u8> for Graphics320x240x256 {
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 {
@ -68,6 +70,7 @@ impl GraphicsWriter<u8> for Graphics320x240x256 {
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,
@ -84,7 +87,31 @@ impl GraphicsWriter<u8> for Graphics320x240x256 {
}
}
}
fn set_mode(&self) {
fn draw_rectangle(&self, rectangle: &Rectangle, color: u8) {
let p1 = (rectangle.x as isize, rectangle.y as isize);
let p2 = (rectangle.x as isize, rectangle.bottom() as isize);
let p3 = (rectangle.right() as isize, rectangle.bottom() as isize);
let p4 = (rectangle.right() as isize, rectangle.y as isize);
self.draw_line(p1, p2, color);
self.draw_line(p2, p3, color);
self.draw_line(p3, p4, color);
self.draw_line(p4, p1, color);
}
fn draw_filled_rectangle(&self, rectangle: &Rectangle, color: u8) {
for y in rectangle.y..rectangle.bottom() {
for x in rectangle.x..rectangle.right() {
self.set_pixel(x as usize, y as usize, color);
}
}
}
fn get_frame_buffer(&self) -> *mut u8 {
u32::from(VGA.lock().get_frame_buffer()) as *mut u8
}
fn set_mode(&mut self) {
let mut vga = VGA.lock();
vga.set_video_mode(VideoMode::Mode320x240x256);

View File

@ -1,7 +1,7 @@
use super::{GraphicsWriter, Screen};
use crate::{
colors::{Color16, DEFAULT_PALETTE},
drawing::{Bresenham, Point},
drawing::{Bresenham, Point, Rectangle},
registers::{PlaneMask, WriteMode},
vga::{VideoMode, VGA},
};
@ -77,6 +77,25 @@ impl GraphicsWriter<Color16> for Graphics640x480x16 {
}
}
fn draw_rectangle(&self, rectangle: &Rectangle, color: Color16) {
let p1 = (rectangle.x as isize, rectangle.y as isize);
let p2 = (rectangle.x as isize, rectangle.bottom() as isize);
let p3 = (rectangle.right() as isize, rectangle.bottom() as isize);
let p4 = (rectangle.right() as isize, rectangle.y as isize);
self.draw_line(p1, p2, color);
self.draw_line(p2, p3, color);
self.draw_line(p3, p4, color);
self.draw_line(p4, p1, color);
}
fn draw_filled_rectangle(&self, rectangle: &Rectangle, color: Color16) {
for y in rectangle.y..rectangle.bottom() {
for x in rectangle.x..rectangle.right() {
self.set_pixel(x as usize, y as usize, color);
}
}
}
/// **Note:** This method is provided for convenience, but has terrible
/// performance since it needs to ensure the correct `WriteMode` per pixel
/// drawn. If you need to draw more then one pixel, consider using a method
@ -86,7 +105,11 @@ impl GraphicsWriter<Color16> for Graphics640x480x16 {
self._set_pixel(x, y, color);
}
fn set_mode(&self) {
fn get_frame_buffer(&self) -> *mut Color16 {
u32::from(VGA.lock().get_frame_buffer()) as *mut Color16
}
fn set_mode(&mut self) {
let mut vga = VGA.lock();
vga.set_video_mode(VideoMode::Mode640x480x16);
@ -130,7 +153,7 @@ impl Graphics640x480x16 {
.set_bit_mask(pixel_mask);
unsafe {
frame_buffer.add(offset).read_volatile();
frame_buffer.add(offset).write_volatile(u8::from(color));
frame_buffer.add(offset).write_volatile(color);
}
}
}

View File

@ -8,7 +8,7 @@ mod text_80x25;
use super::{
colors::{Color16, TextModeColor},
drawing::Point,
drawing::{Point, Rectangle},
registers::CrtcControllerIndex,
vga::{Vga, VGA},
};
@ -195,12 +195,14 @@ pub trait GraphicsWriter<Color> {
fn draw_line(&self, start: Point<isize>, end: Point<isize>, color: Color);
/// Draws a character at the given `(x, y)` coordinant to the specified `color`.
fn draw_character(&self, x: usize, y: usize, character: char, color: Color);
/// Draws a rectangle using the given `rectangle` and `color`.
fn draw_rectangle(&self, rectangle: &Rectangle, color: Color);
/// Draws a filled rectangle using the given `rectangle` and `color`.
fn draw_filled_rectangle(&self, rectangle: &Rectangle, color: Color);
/// Sets the given pixel at `(x, y)` to the given `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
}
fn get_frame_buffer(&self) -> *mut Color;
/// Sets the graphics device to a `VideoMode`.
fn set_mode(&mut self);
}