From cbad0a141e5f9b25e7eec590ca5f90632180ede6 Mon Sep 17 00:00:00 2001 From: Daniel Beckwith Date: Tue, 31 Mar 2020 18:20:22 -0400 Subject: [PATCH 1/9] More trait impls --- src/colors.rs | 4 ++-- src/writers/mod.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/colors.rs b/src/colors.rs index 11ad75c..85c8707 100644 --- a/src/colors.rs +++ b/src/colors.rs @@ -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 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); diff --git a/src/writers/mod.rs b/src/writers/mod.rs index c6c6efa..a129de3 100644 --- a/src/writers/mod.rs +++ b/src/writers/mod.rs @@ -20,7 +20,7 @@ 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, From 44c3821d9de51c58ac8b64cee368b28890e18f9e Mon Sep 17 00:00:00 2001 From: Daniel Beckwith Date: Tue, 31 Mar 2020 18:20:25 -0400 Subject: [PATCH 2/9] More const --- src/writers/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/writers/mod.rs b/src/writers/mod.rs index a129de3..ed66771 100644 --- a/src/writers/mod.rs +++ b/src/writers/mod.rs @@ -29,7 +29,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 } } From 8eb86d55a1d8a6fb39b734cd41903de70a6a6076 Mon Sep 17 00:00:00 2001 From: Daniel Beckwith Date: Tue, 31 Mar 2020 18:22:55 -0400 Subject: [PATCH 3/9] Add fill_screen --- src/writers/mod.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/writers/mod.rs b/src/writers/mod.rs index ed66771..499b031 100644 --- a/src/writers/mod.rs +++ b/src/writers/mod.rs @@ -87,6 +87,21 @@ pub trait TextWriter: Screen { } } + /// Fills the screen by setting all cells to `b' '` with the given color. + fn fill_screen(&self, color: TextModeColor) { + let (_vga, frame_buffer) = self.get_frame_buffer(); + let character = ScreenCharacter { + character: b' ', + color, + }; + let screen_size = self.get_width() * self.get_height(); + for i in 0..screen_size { + unsafe { + frame_buffer.add(i).write_volatile(character); + } + } + } + /// Disables the cursor in vga text modes. fn disable_cursor(&self) { let (mut vga, _frame_buffer) = self.get_frame_buffer(); From 88ce4a55a107982c7a041b6dc4c6c5159183c032 Mon Sep 17 00:00:00 2001 From: Daniel Beckwith Date: Tue, 31 Mar 2020 18:24:29 -0400 Subject: [PATCH 4/9] More trait impls for screen writers --- src/writers/graphics_320x200x256.rs | 4 ++-- src/writers/graphics_640x480x16.rs | 2 +- src/writers/text_40x25.rs | 2 +- src/writers/text_40x50.rs | 2 +- src/writers/text_80x25.rs | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/writers/graphics_320x200x256.rs b/src/writers/graphics_320x200x256.rs index 93e6f2f..fd582f4 100644 --- a/src/writers/graphics_320x200x256.rs +++ b/src/writers/graphics_320x200x256.rs @@ -33,8 +33,8 @@ 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] diff --git a/src/writers/graphics_640x480x16.rs b/src/writers/graphics_640x480x16.rs index 8000b6b..e287ce8 100644 --- a/src/writers/graphics_640x480x16.rs +++ b/src/writers/graphics_640x480x16.rs @@ -36,7 +36,7 @@ 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 { diff --git a/src/writers/text_40x25.rs b/src/writers/text_40x25.rs index 452d2d8..2ead79f 100644 --- a/src/writers/text_40x25.rs +++ b/src/writers/text_40x25.rs @@ -26,7 +26,7 @@ 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 { diff --git a/src/writers/text_40x50.rs b/src/writers/text_40x50.rs index 62b057b..f327905 100644 --- a/src/writers/text_40x50.rs +++ b/src/writers/text_40x50.rs @@ -26,7 +26,7 @@ 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 { diff --git a/src/writers/text_80x25.rs b/src/writers/text_80x25.rs index 72a4354..94aef4f 100644 --- a/src/writers/text_80x25.rs +++ b/src/writers/text_80x25.rs @@ -26,7 +26,7 @@ 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 { From 6547019382e2fe881c48c41c42f8c673488591f0 Mon Sep 17 00:00:00 2001 From: Daniel Beckwith Date: Tue, 31 Mar 2020 18:29:32 -0400 Subject: [PATCH 5/9] Make screen width and height constant --- src/writers/graphics_320x200x256.rs | 17 +++-------------- src/writers/graphics_640x480x16.rs | 12 +++--------- src/writers/mod.rs | 24 +++++++++++------------- src/writers/text_40x25.rs | 15 ++++----------- src/writers/text_40x50.rs | 15 ++++----------- src/writers/text_80x25.rs | 15 ++++----------- 6 files changed, 29 insertions(+), 69 deletions(-) diff --git a/src/writers/graphics_320x200x256.rs b/src/writers/graphics_320x200x256.rs index fd582f4..35ed689 100644 --- a/src/writers/graphics_320x200x256.rs +++ b/src/writers/graphics_320x200x256.rs @@ -37,20 +37,9 @@ const SIZE: usize = WIDTH * HEIGHT; 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 for Graphics320x200x256 { diff --git a/src/writers/graphics_640x480x16.rs b/src/writers/graphics_640x480x16.rs index e287ce8..6a35125 100644 --- a/src/writers/graphics_640x480x16.rs +++ b/src/writers/graphics_640x480x16.rs @@ -40,15 +40,9 @@ const WIDTH_IN_BYTES: usize = WIDTH / 8; 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 for Graphics640x480x16 { diff --git a/src/writers/mod.rs b/src/writers/mod.rs index 499b031..1759a29 100644 --- a/src/writers/mod.rs +++ b/src/writers/mod.rs @@ -51,12 +51,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. @@ -79,8 +79,7 @@ pub trait TextWriter: Screen { /// color of `Color16::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 { + for i in 0..Self::SIZE { unsafe { frame_buffer.add(i).write_volatile(BLANK_CHARACTER); } @@ -94,8 +93,7 @@ pub trait TextWriter: Screen { character: b' ', color, }; - 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(character); } @@ -133,7 +131,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() } } @@ -169,7 +167,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; @@ -189,7 +187,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); } diff --git a/src/writers/text_40x25.rs b/src/writers/text_40x25.rs index 2ead79f..91e547f 100644 --- a/src/writers/text_40x25.rs +++ b/src/writers/text_40x25.rs @@ -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 /// @@ -30,17 +31,9 @@ const HEIGHT: usize = 25; 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 { diff --git a/src/writers/text_40x50.rs b/src/writers/text_40x50.rs index f327905..b1a8a68 100644 --- a/src/writers/text_40x50.rs +++ b/src/writers/text_40x50.rs @@ -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 /// @@ -30,17 +31,9 @@ const HEIGHT: usize = 50; 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 { diff --git a/src/writers/text_80x25.rs b/src/writers/text_80x25.rs index 94aef4f..19811e2 100644 --- a/src/writers/text_80x25.rs +++ b/src/writers/text_80x25.rs @@ -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 /// @@ -30,17 +31,9 @@ const HEIGHT: usize = 25; 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 { From 19d41fd146177930feaddb577f89728ed09bde28 Mon Sep 17 00:00:00 2001 From: Daniel Beckwith Date: Tue, 31 Mar 2020 18:29:41 -0400 Subject: [PATCH 6/9] Make constructors const --- src/writers/graphics_320x200x256.rs | 4 ++-- src/writers/graphics_640x480x16.rs | 4 ++-- src/writers/text_40x25.rs | 4 ++-- src/writers/text_40x50.rs | 4 ++-- src/writers/text_80x25.rs | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/writers/graphics_320x200x256.rs b/src/writers/graphics_320x200x256.rs index 35ed689..6f55831 100644 --- a/src/writers/graphics_320x200x256.rs +++ b/src/writers/graphics_320x200x256.rs @@ -90,8 +90,8 @@ impl GraphicsWriter for Graphics320x200x256 { impl Graphics320x200x256 { /// Creates a new `Graphics320x200x256`. - pub fn new() -> Graphics320x200x256 { - Graphics320x200x256 {} + pub const fn new() -> Graphics320x200x256 { + Graphics320x200x256 } /// Returns the start of the `FrameBuffer` as `*mut u8` as diff --git a/src/writers/graphics_640x480x16.rs b/src/writers/graphics_640x480x16.rs index 6a35125..4cc8a45 100644 --- a/src/writers/graphics_640x480x16.rs +++ b/src/writers/graphics_640x480x16.rs @@ -102,8 +102,8 @@ impl GraphicsWriter 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) { diff --git a/src/writers/text_40x25.rs b/src/writers/text_40x25.rs index 91e547f..6c3e758 100644 --- a/src/writers/text_40x25.rs +++ b/src/writers/text_40x25.rs @@ -51,7 +51,7 @@ impl TextWriter for Text40x25 { impl Text40x25 { /// Creates a new `Text40x25`. - pub fn new() -> Text40x25 { - Text40x25 {} + pub const fn new() -> Text40x25 { + Text40x25 } } diff --git a/src/writers/text_40x50.rs b/src/writers/text_40x50.rs index b1a8a68..e63c248 100644 --- a/src/writers/text_40x50.rs +++ b/src/writers/text_40x50.rs @@ -51,7 +51,7 @@ impl TextWriter for Text40x50 { impl Text40x50 { /// Creates a new `Text40x50`. - pub fn new() -> Text40x50 { - Text40x50 {} + pub const fn new() -> Text40x50 { + Text40x50 } } diff --git a/src/writers/text_80x25.rs b/src/writers/text_80x25.rs index 19811e2..72403d0 100644 --- a/src/writers/text_80x25.rs +++ b/src/writers/text_80x25.rs @@ -50,7 +50,7 @@ impl TextWriter for Text80x25 { impl Text80x25 { /// Creates a new `Text80x25`. - pub fn new() -> Text80x25 { - Text80x25 {} + pub const fn new() -> Text80x25 { + Text80x25 } } From 66b4ccf6d028652e98f7a270f9d6174064b1e92e Mon Sep 17 00:00:00 2001 From: Daniel Beckwith Date: Tue, 31 Mar 2020 18:35:59 -0400 Subject: [PATCH 7/9] Fix lint --- src/writers/graphics_320x200x256.rs | 2 +- src/writers/graphics_640x480x16.rs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/writers/graphics_320x200x256.rs b/src/writers/graphics_320x200x256.rs index 6f55831..ba31144 100644 --- a/src/writers/graphics_320x200x256.rs +++ b/src/writers/graphics_320x200x256.rs @@ -97,7 +97,7 @@ impl 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, *mut u8) { + fn get_frame_buffer(self) -> (SpinlockGuard, *mut u8) { let mut vga = VGA.lock(); let frame_buffer = vga.get_frame_buffer(); (vga, u32::from(frame_buffer) as *mut u8) diff --git a/src/writers/graphics_640x480x16.rs b/src/writers/graphics_640x480x16.rs index 4cc8a45..3af89c7 100644 --- a/src/writers/graphics_640x480x16.rs +++ b/src/writers/graphics_640x480x16.rs @@ -106,7 +106,7 @@ impl Graphics640x480x16 { Graphics640x480x16 } - fn set_write_mode_0(&self, color: Color16) { + fn set_write_mode_0(self, color: Color16) { let (mut vga, _frame_buffer) = self.get_frame_buffer(); vga.graphics_controller_registers.write_set_reset(color); vga.graphics_controller_registers @@ -115,7 +115,7 @@ impl Graphics640x480x16 { .set_write_mode(WriteMode::Mode0); } - fn set_write_mode_2(&self) { + fn set_write_mode_2(self) { let (mut vga, _frame_buffer) = self.get_frame_buffer(); vga.graphics_controller_registers .set_write_mode(WriteMode::Mode2); @@ -127,14 +127,14 @@ impl Graphics640x480x16 { /// 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, *mut u8) { + fn get_frame_buffer(self) -> (SpinlockGuard, *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) { + fn _set_pixel(self, x: usize, y: usize, color: Color16) { let (mut vga, frame_buffer) = self.get_frame_buffer(); let offset = x / 8 + y * WIDTH_IN_BYTES; let pixel_mask = 0x80 >> (x & 0x07); From 6bc387cdf633b2d4df7eda4612bb843f13dfc6bc Mon Sep 17 00:00:00 2001 From: Daniel Beckwith Date: Tue, 31 Mar 2020 18:37:42 -0400 Subject: [PATCH 8/9] Fix lint --- src/writers/graphics_320x200x256.rs | 2 +- src/writers/graphics_640x480x16.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/writers/graphics_320x200x256.rs b/src/writers/graphics_320x200x256.rs index ba31144..e2c8cc0 100644 --- a/src/writers/graphics_320x200x256.rs +++ b/src/writers/graphics_320x200x256.rs @@ -97,7 +97,7 @@ impl 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, *mut u8) { + fn get_frame_buffer(self) -> (SpinlockGuard<'static, Vga>, *mut u8) { let mut vga = VGA.lock(); let frame_buffer = vga.get_frame_buffer(); (vga, u32::from(frame_buffer) as *mut u8) diff --git a/src/writers/graphics_640x480x16.rs b/src/writers/graphics_640x480x16.rs index 3af89c7..43a42b0 100644 --- a/src/writers/graphics_640x480x16.rs +++ b/src/writers/graphics_640x480x16.rs @@ -127,7 +127,7 @@ impl Graphics640x480x16 { /// 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, *mut u8) { + fn get_frame_buffer(self) -> (SpinlockGuard<'static, Vga>, *mut u8) { let mut vga = VGA.lock(); let frame_buffer = vga.get_frame_buffer(); (vga, u32::from(frame_buffer) as *mut u8) From 98e9fb3d86be6ce30f036c84952c92de3cd08f6c Mon Sep 17 00:00:00 2001 From: Daniel Beckwith Date: Tue, 31 Mar 2020 20:33:06 -0400 Subject: [PATCH 9/9] Allow any character for fill_screen --- src/writers/mod.rs | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/writers/mod.rs b/src/writers/mod.rs index 1759a29..d47a2a8 100644 --- a/src/writers/mod.rs +++ b/src/writers/mod.rs @@ -78,21 +78,12 @@ pub trait TextWriter: Screen { /// a background color of `Color16::Black` and a foreground /// color of `Color16::Yellow`. fn clear_screen(&self) { - let (_vga, frame_buffer) = self.get_frame_buffer(); - for i in 0..Self::SIZE { - unsafe { - frame_buffer.add(i).write_volatile(BLANK_CHARACTER); - } - } + self.fill_screen(BLANK_CHARACTER); } - /// Fills the screen by setting all cells to `b' '` with the given color. - fn fill_screen(&self, color: TextModeColor) { + /// 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 character = ScreenCharacter { - character: b' ', - color, - }; for i in 0..Self::SIZE { unsafe { frame_buffer.add(i).write_volatile(character);