From: Jacob Casper Date: Tue, 16 Jan 2024 00:10:23 +0000 (-0600) Subject: Implement addressing on Word structs X-Git-Url: https://git.jacobcasper.com/?a=commitdiff_plain;h=08768a25e0f327d8cd22d2830895af1f4dfed3b3;p=mix.git Implement addressing on Word structs This is more useful than taking 2 MixBits and more easily keeps the sign of the Word in scope. --- diff --git a/src/main.rs b/src/main.rs index 727b3f6..9108572 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,16 +40,27 @@ impl Word { // right-hand part of the field. fn field_specification(&self, l: usize, r: usize) -> Word { let mut sign = false; + let mut l_clamp = l; if l == 0 { sign = self.sign; + } else { + l_clamp = l - 1; } let r_clamp = r % 5; let mut bytes = [MixBit::default(); 5]; - for n in l..r_clamp { + for n in l_clamp..r_clamp { bytes[n] = self.bytes[n]; } Word { sign, bytes } } + + fn address(&self) -> i16 { + let magnitude = ((self.bytes[0].value() as i16) << 6) | self.bytes[1].value() as i16; + match self.sign { + true => magnitude, + false => -magnitude, + } + } } // There are nine registers in MIX. @@ -108,8 +119,8 @@ impl Machine { fn main() { let fs = (Word { sign: true, - bytes: [MixBit { v: 255 }; 5], + bytes: [MixBit { v: 63 }; 5], }) - .field_specification(0, 2); - println!("{:?}", address(fs.bytes[0], fs.bytes[1])); + .field_specification(1, 2); + println!("{:?}", fs.address()); }