diff --git a/src/devices/bochs.rs b/src/devices/bochs.rs index ebfd2d3..c86931a 100644 --- a/src/devices/bochs.rs +++ b/src/devices/bochs.rs @@ -40,24 +40,25 @@ pub struct BochsDevice { pci_device: PciDevice, physical_address: PhysAddr, virtual_address: VirtAddr, - current_resolution: Resolution, + resolution: Resolution, } impl BochsDevice { - pub fn new() -> 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, - current_resolution: Resolution::default(), + resolution, } } @@ -149,12 +150,7 @@ impl BochsDevice { /// Sets the `BochsDevice` to the given `resolution`. pub fn set_resolution(&mut self, resolution: Resolution) { - self.disable_display(); - self.set_width(resolution.width); - self.set_height(resolution.height); - self.set_bpp(); - self.enable_display(); - self.current_resolution = resolution; + self.resolution = resolution; } fn get_width(&mut self) -> usize { @@ -195,7 +191,7 @@ impl BochsDevice { impl GraphicsWriter for BochsDevice { fn clear_screen(&self, color: u32) { - let screen_size = self.current_resolution.width * self.current_resolution.height; + let screen_size = self.resolution.width * self.resolution.height; let frame_buffer = self.virtual_address.as_mut_ptr::(); for offset in 0..screen_size { unsafe { @@ -225,7 +221,7 @@ impl GraphicsWriter for BochsDevice { } } fn set_pixel(&self, x: usize, y: usize, color: u32) { - let offset = (y * self.current_resolution.width) + x; + let offset = (y * self.resolution.width) + x; unsafe { self.virtual_address .as_mut_ptr::() @@ -236,4 +232,11 @@ impl GraphicsWriter for BochsDevice { 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(); + } } diff --git a/src/writers/graphics_320x200x256.rs b/src/writers/graphics_320x200x256.rs index c9ff664..8d239a4 100644 --- a/src/writers/graphics_320x200x256.rs +++ b/src/writers/graphics_320x200x256.rs @@ -77,16 +77,7 @@ impl GraphicsWriter for Graphics320x200x256 { fn get_frame_buffer(&self) -> *mut u8 { u32::from(VGA.lock().get_frame_buffer()) as *mut u8 } -} - -impl Graphics320x200x256 { - /// Creates a new `Graphics320x200x256`. - pub const fn new() -> Graphics320x200x256 { - Graphics320x200x256 - } - - /// Sets the graphics device to a `VideoMode`. - pub fn set_mode(&self) { + fn set_mode(&mut self) { let mut vga = VGA.lock(); vga.set_video_mode(VideoMode::Mode320x200x256); @@ -95,3 +86,10 @@ impl Graphics320x200x256 { vga.color_palette_registers.load_palette(&DEFAULT_PALETTE); } } + +impl Graphics320x200x256 { + /// Creates a new `Graphics320x200x256`. + pub const fn new() -> Graphics320x200x256 { + Graphics320x200x256 + } +} diff --git a/src/writers/graphics_320x240x256.rs b/src/writers/graphics_320x240x256.rs index ce217ed..8669845 100644 --- a/src/writers/graphics_320x240x256.rs +++ b/src/writers/graphics_320x240x256.rs @@ -87,16 +87,7 @@ impl GraphicsWriter for Graphics320x240x256 { fn get_frame_buffer(&self) -> *mut u8 { u32::from(VGA.lock().get_frame_buffer()) as *mut u8 } -} - -impl Graphics320x240x256 { - /// Creates a new `Graphics320x240x256`. - pub const fn new() -> Graphics320x240x256 { - Graphics320x240x256 - } - - /// Sets the graphics device to a `VideoMode`. - pub fn set_mode(&self) { + fn set_mode(&mut self) { let mut vga = VGA.lock(); vga.set_video_mode(VideoMode::Mode320x240x256); @@ -105,3 +96,10 @@ impl Graphics320x240x256 { vga.color_palette_registers.load_palette(&DEFAULT_PALETTE); } } + +impl Graphics320x240x256 { + /// Creates a new `Graphics320x240x256`. + pub const fn new() -> Graphics320x240x256 { + Graphics320x240x256 + } +} diff --git a/src/writers/graphics_640x480x16.rs b/src/writers/graphics_640x480x16.rs index 668e996..3b558c7 100644 --- a/src/writers/graphics_640x480x16.rs +++ b/src/writers/graphics_640x480x16.rs @@ -89,16 +89,7 @@ impl GraphicsWriter for Graphics640x480x16 { fn get_frame_buffer(&self) -> *mut Color16 { u32::from(VGA.lock().get_frame_buffer()) as *mut Color16 } -} - -impl Graphics640x480x16 { - /// Creates a new `Graphics640x480x16`. - pub const fn new() -> Graphics640x480x16 { - Graphics640x480x16 - } - - /// Sets the graphics device to a `VideoMode`. - pub fn set_mode(&self) { + fn set_mode(&mut self) { let mut vga = VGA.lock(); vga.set_video_mode(VideoMode::Mode640x480x16); @@ -106,6 +97,13 @@ impl Graphics640x480x16 { // so explicitly set it. vga.color_palette_registers.load_palette(&DEFAULT_PALETTE); } +} + +impl Graphics640x480x16 { + /// Creates a new `Graphics640x480x16`. + pub const fn new() -> Graphics640x480x16 { + Graphics640x480x16 + } fn set_write_mode_0(self, color: Color16) { let mut vga = VGA.lock(); diff --git a/src/writers/mod.rs b/src/writers/mod.rs index 768c41d..d4608da 100644 --- a/src/writers/mod.rs +++ b/src/writers/mod.rs @@ -199,4 +199,6 @@ pub trait GraphicsWriter { fn set_pixel(&self, x: usize, y: usize, color: Color); /// Returns the frame buffer for this vga mode. fn get_frame_buffer(&self) -> *mut Color; + /// Sets the graphics device to a `VideoMode`. + fn set_mode(&mut self); }