Files
vulcan/src/word.rs
T

61 lines
1.4 KiB
Rust
Raw Normal View History

2022-02-18 21:02:17 -06:00
// 128k, the amount of memory in a standard Vulcan machine
pub const MEM_SIZE: u32 = 128 * 1024;
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
2022-02-18 22:32:37 -06:00
pub struct Word(u32);
2022-02-18 21:02:17 -06:00
2022-02-18 22:32:37 -06:00
impl From<u32> for Word {
2022-02-18 21:02:17 -06:00
fn from(a: u32) -> Self { Self(a & 0xffffff) }
}
2022-02-20 12:58:52 -06:00
impl From<Word> for u32 {
fn from(word: Word) -> Self { word.0 }
2022-02-18 21:02:17 -06:00
}
2022-02-18 22:32:37 -06:00
impl std::ops::Add<i32> for Word {
type Output = Word;
2022-02-18 21:02:17 -06:00
fn add(self, rhs: i32) -> Self::Output {
2022-02-18 22:32:37 -06:00
Word::from((self.0 as i32).overflowing_add(rhs).0 as u32)
2022-02-18 21:02:17 -06:00
}
}
2022-02-18 22:32:37 -06:00
impl std::ops::Sub<i32> for Word {
type Output = Word;
2022-02-18 21:02:17 -06:00
fn sub(self, rhs: i32) -> Self::Output { self + -rhs }
}
2022-02-19 11:13:27 -06:00
impl std::ops::Sub<Word> for Word {
type Output = Word;
fn sub(self, rhs: Word) -> Self::Output { Word(self.0 - rhs.0) }
}
2022-02-18 22:32:37 -06:00
impl std::ops::SubAssign<i32> for Word {
2022-02-18 21:02:17 -06:00
fn sub_assign(&mut self, rhs: i32) { *self = *self - rhs; }
}
2022-02-18 22:32:37 -06:00
impl std::ops::AddAssign<i32> for Word {
2022-02-18 21:02:17 -06:00
fn add_assign(&mut self, rhs: i32) { *self = *self + rhs; }
}
#[test]
fn test_address_truncation() {
2022-02-18 22:32:37 -06:00
let a: Word = 0x11223344.into();
2022-02-18 21:02:17 -06:00
assert_eq!(a, 0x00223344.into());
}
#[test]
fn test_address_overflows() {
2022-02-18 22:32:37 -06:00
let a = Word::from(0xfffffa);
assert_eq!(a + 10, Word(4));
2022-02-18 21:02:17 -06:00
2022-02-18 22:32:37 -06:00
let b = Word::from(3);
assert_eq!(b - 10, Word(0xfffff9));
2022-02-18 21:02:17 -06:00
2022-02-18 22:32:37 -06:00
let mut c = Word::from(5);
2022-02-18 21:02:17 -06:00
c += 3;
2022-02-18 22:32:37 -06:00
assert_eq!(c, Word(8));
2022-02-18 21:02:17 -06:00
2022-02-18 22:32:37 -06:00
let mut d = Word::from(5);
2022-02-18 21:02:17 -06:00
d -= 3;
2022-02-18 22:32:37 -06:00
assert_eq!(d, Word(2));
2022-02-18 21:02:17 -06:00
}