monistode_binutils/
address.rs1use bitvec::vec::BitVec;
2
3#[derive(Debug, Clone, Copy)]
4pub struct Address(pub usize); impl std::ops::Add<usize> for Address {
7 type Output = Address;
8 fn add(self, rhs: usize) -> Self::Output {
9 Address(self.0 + rhs)
10 }
11}
12
13impl std::ops::Sub<Address> for Address {
14 type Output = i64;
15
16 fn sub(self, rhs: Address) -> Self::Output {
17 self.0 as i64 - rhs.0 as i64
18 }
19}
20
21pub trait AddressIndexable<T> {
22 fn index(&self, index: Address) -> T;
23 fn write(&mut self, index: Address, value: T);
24}
25
26impl AddressIndexable<u16> for BitVec {
27 fn index(&self, index: Address) -> u16 {
28 let mut result = 0u16;
29 for i in 0..16 {
30 result <<= 1;
31 if index.0 + i < self.len() && self[index.0 + i] {
32 result |= 1;
33 }
34 }
35 result
36 }
37
38 fn write(&mut self, index: Address, value: u16) {
39 let mut value = value;
40 for i in 0..16 {
41 if index.0 + i < self.len() {
42 self.set(index.0 + 15 - i, value & 1 == 1);
43 value >>= 1;
44 }
45 }
46 }
47}