From 0613849da9a7ab738a2ea3d3bff3d3d3b20371d7 Mon Sep 17 00:00:00 2001 From: Corwin Kuiper Date: Sat, 6 Mar 2021 20:07:04 +0000 Subject: [PATCH] mark creating memory mapped as unsafe --- src/display.rs | 26 +++++++++++--------------- src/interrupt.rs | 4 ++-- src/lib.rs | 4 ++-- src/memory_mapped.rs | 6 +++--- src/mgba.rs | 4 ++-- 5 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/display.rs b/src/display.rs index 498adec..ca42ca7 100644 --- a/src/display.rs +++ b/src/display.rs @@ -5,26 +5,28 @@ use crate::{ use bitflags::bitflags; use core::convert::TryInto; -const DISPLAY_CONTROL: MemoryMapped = MemoryMapped::new(0x0400_0000); -const DISPLAY_STATUS: MemoryMapped = MemoryMapped::new(0x0400_0004); -const VCOUNT: MemoryMapped = MemoryMapped::new(0x0400_0006); +const DISPLAY_CONTROL: MemoryMapped = unsafe { MemoryMapped::new(0x0400_0000) }; +const DISPLAY_STATUS: MemoryMapped = unsafe { MemoryMapped::new(0x0400_0004) }; +const VCOUNT: MemoryMapped = unsafe { MemoryMapped::new(0x0400_0006) }; -const PALETTE_BACKGROUND: MemoryMapped1DArray = MemoryMapped1DArray::new(0x0500_0000); -const PALETTE_SPRITE: MemoryMapped1DArray = MemoryMapped1DArray::new(0x0500_0200); +const PALETTE_BACKGROUND: MemoryMapped1DArray = + unsafe { MemoryMapped1DArray::new(0x0500_0000) }; +const PALETTE_SPRITE: MemoryMapped1DArray = + unsafe { MemoryMapped1DArray::new(0x0500_0200) }; const BITMAP_MODE_3: MemoryMapped2DArray = - MemoryMapped2DArray::new(0x600_0000); + unsafe { MemoryMapped2DArray::new(0x600_0000) }; const BITMAP_PAGE_FRONT_MODE_4: MemoryMapped2DArray< u16, { (WIDTH / 2) as usize }, { HEIGHT as usize }, -> = MemoryMapped2DArray::new(0x600_0000); +> = unsafe { MemoryMapped2DArray::new(0x600_0000) }; const BITMAP_PAGE_BACK_MODE_4: MemoryMapped2DArray< u16, { (WIDTH / 2) as usize }, { HEIGHT as usize }, -> = MemoryMapped2DArray::new(0x600_A000); +> = unsafe { MemoryMapped2DArray::new(0x600_A000) }; pub const WIDTH: i32 = 240; pub const HEIGHT: i32 = 160; @@ -64,14 +66,8 @@ pub struct Display { in_mode: Single, } -impl Default for Display { - fn default() -> Self { - Self::new() - } -} - impl Display { - pub(crate) const fn new() -> Self { + pub(crate) const unsafe fn new() -> Self { Display { in_mode: Single::new(), } diff --git a/src/interrupt.rs b/src/interrupt.rs index 730b191..94f5a63 100644 --- a/src/interrupt.rs +++ b/src/interrupt.rs @@ -17,8 +17,8 @@ pub enum Interrupt { Gamepak, } -const ENABLED_INTERRUPTS: MemoryMapped = MemoryMapped::new(0x04000200); -const INTERRUPTS_ENABLED: MemoryMapped = MemoryMapped::new(0x04000208); +const ENABLED_INTERRUPTS: MemoryMapped = unsafe { MemoryMapped::new(0x04000200) }; +const INTERRUPTS_ENABLED: MemoryMapped = unsafe { MemoryMapped::new(0x04000208) }; pub fn enable(interrupt: Interrupt) { let _interrupt_token = temporary_interrupt_disable(); diff --git a/src/lib.rs b/src/lib.rs index 194beca..e61670a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,7 +24,7 @@ fn panic_implementation(info: &core::panic::PanicInfo) -> ! { loop {} } -static mut GBASINGLE: single::Singleton = single::Singleton::new(Gba::single_new()); +static mut GBASINGLE: single::Singleton = single::Singleton::new(unsafe { Gba::single_new() }); pub struct Gba { pub display: display::Display, @@ -35,7 +35,7 @@ impl Gba { unsafe { GBASINGLE.take() } } - const fn single_new() -> Self { + const unsafe fn single_new() -> Self { Self { display: display::Display::new(), } diff --git a/src/memory_mapped.rs b/src/memory_mapped.rs index 7807754..0c0cf6f 100644 --- a/src/memory_mapped.rs +++ b/src/memory_mapped.rs @@ -3,7 +3,7 @@ pub struct MemoryMapped { } impl MemoryMapped { - pub const fn new(address: usize) -> Self { + pub const unsafe fn new(address: usize) -> Self { MemoryMapped { address: address as *mut T, } @@ -24,7 +24,7 @@ pub struct MemoryMapped1DArray { #[allow(dead_code)] impl MemoryMapped1DArray { - pub const fn new(address: usize) -> Self { + pub const unsafe fn new(address: usize) -> Self { MemoryMapped1DArray { array: address as *mut [T; N], } @@ -42,7 +42,7 @@ pub struct MemoryMapped2DArray { } impl MemoryMapped2DArray { - pub const fn new(address: usize) -> Self { + pub const unsafe fn new(address: usize) -> Self { MemoryMapped2DArray { array: address as *mut [[T; X]; Y], } diff --git a/src/mgba.rs b/src/mgba.rs index 6e5d445..c16cb53 100644 --- a/src/mgba.rs +++ b/src/mgba.rs @@ -12,12 +12,12 @@ pub enum DebugLevel { } const OUTPUT: *mut u8 = 0x04FF_F600 as *mut u8; -const ENABLE: MemoryMapped = MemoryMapped::new(0x04FF_F780); +const ENABLE: MemoryMapped = unsafe { MemoryMapped::new(0x04FF_F780) }; const ENABLE_HANDSHAKE_IN: u16 = 0xC0DE; const ENABLE_HANDSHAKE_OUT: u16 = 0x1DEA; -const DEBUG_LEVEL: MemoryMapped = MemoryMapped::new(0x04FF_F700); +const DEBUG_LEVEL: MemoryMapped = unsafe { MemoryMapped::new(0x04FF_F700) }; const DEBUG_FLAG_CODE: u16 = 0x0100; fn is_running_in_mgba() -> bool {