add bitarray implementation

This commit is contained in:
Corwin Kuiper 2021-06-04 18:31:28 +01:00
parent 9286378a36
commit 8bf59e0f20
2 changed files with 25 additions and 0 deletions

24
agb/src/bitarray.rs Normal file
View file

@ -0,0 +1,24 @@
pub struct Bitarray<const N: usize> {
a: [u32; N],
}
impl<const N: usize> Bitarray<N> {
pub fn new() -> Self {
Bitarray { a: [0; N] }
}
pub fn get(&self, index: usize) -> Option<bool> {
if index < N * 32 {
Some((self.a[index / N] >> (N % 32) & 1) != 0)
} else {
None
}
}
pub fn set(&mut self, index: usize, value: bool) {
let value = value as u32;
let mask = 1 << (N % 32);
let value_mask = value << (N % 32);
self.a[index / N] = self.a[index / N] & !mask | value_mask
}
}

View file

@ -20,6 +20,7 @@ pub mod input;
/// Implements sound output.
pub mod sound;
mod bitarray;
mod interrupt;
mod memory_mapped;
/// Implements logging to the mgba emulator.