From 32b1fa5f4b0c73c7e03662ef602b239dd58b8ae4 Mon Sep 17 00:00:00 2001 From: Johannes Muenzel Date: Sat, 25 Oct 2014 19:39:36 -0400 Subject: [PATCH] implement BIT --- src/machine.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/machine.rs b/src/machine.rs index 7229413..e83c67d 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -87,6 +87,28 @@ impl Machine { self.add_with_carry(val); }, + (instruction::BIT, instruction::UseAddress(addr)) => { + let a: u8 = self.registers.accumulator as u8; + let m: u8 = self.memory.get_byte(addr); + let res = a & m; + + // The zero flag is set based on the result of the 'and'. + let is_zero = 0 == res; + + // The N flag is set to bit 7 of the byte from memory. + let bit7 = 0 != (0x80 & res); + + // The V flag is set to bit 6 of the byte from memory. + let bit6 = 0 != (0x40 & res); + + self.registers.status.set_with_mask( + PS_ZERO | PS_NEGATIVE | PS_OVERFLOW, + Status::new(StatusArgs { zero: is_zero, + negative: bit7, + overflow: bit6, + ..StatusArgs::none() } )); + } + (instruction::BMI, instruction::UseRelative(rel)) => { let addr = self.registers.program_counter + AddressDiff(rel as i32);