agb/src/memory_mapped.rs

57 lines
1.5 KiB
Rust
Raw Normal View History

2021-03-07 04:58:59 +11:00
pub struct MemoryMapped<T> {
address: *mut T,
}
impl<T> MemoryMapped<T> {
2021-03-07 07:07:04 +11:00
pub const unsafe fn new(address: usize) -> Self {
2021-03-07 04:58:59 +11:00
MemoryMapped {
address: address as *mut T,
}
}
pub fn get(&self) -> T {
unsafe { self.address.read_volatile() }
}
pub fn set(&self, val: T) {
unsafe { self.address.write_volatile(val) }
}
}
pub struct MemoryMapped1DArray<T, const N: usize> {
array: *mut [T; N],
}
#[allow(dead_code)]
impl<T, const N: usize> MemoryMapped1DArray<T, N> {
2021-03-07 07:07:04 +11:00
pub const unsafe fn new(address: usize) -> Self {
2021-03-07 04:58:59 +11:00
MemoryMapped1DArray {
array: address as *mut [T; N],
}
}
pub fn get(&self, n: usize) -> T {
unsafe { (&mut (*self.array)[n] as *mut T).read_volatile() }
}
pub fn set(&self, n: usize, val: T) {
unsafe { (&mut (*self.array)[n] as *mut T).write_volatile(val) }
}
}
pub struct MemoryMapped2DArray<T, const X: usize, const Y: usize> {
array: *mut [[T; X]; Y],
}
impl<T, const X: usize, const Y: usize> MemoryMapped2DArray<T, X, Y> {
2021-03-07 07:07:04 +11:00
pub const unsafe fn new(address: usize) -> Self {
2021-03-07 04:58:59 +11:00
MemoryMapped2DArray {
array: address as *mut [[T; X]; Y],
}
}
pub fn get(&self, x: usize, y: usize) -> T {
unsafe { (&mut (*self.array)[y][x] as *mut T).read_volatile() }
}
pub fn set(&self, x: usize, y: usize, val: T) {
unsafe { (&mut (*self.array)[y][x] as *mut T).write_volatile(val) }
}
}