From 5e0d151f12e509e4eb801b10a62470e266a9d2d8 Mon Sep 17 00:00:00 2001 From: Andrew Keeton Date: Thu, 6 Nov 2014 18:58:09 -0500 Subject: [PATCH 1/4] Add and_test(). --- src/machine.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/machine.rs b/src/machine.rs index 15e25aa..e322160 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -628,6 +628,35 @@ fn add_with_carry_test() { assert_eq!(machine.registers.status.contains(PS_OVERFLOW), true); } +#[test] +fn and_test() { + let mut machine = Machine::new(); + + machine.registers.accumulator = 0; + machine.and(0xff); + assert_eq!(machine.registers.accumulator, 0); + assert_eq!(machine.registers.status.contains(PS_ZERO), true); + assert_eq!(machine.registers.status.contains(PS_NEGATIVE), false); + + machine.registers.accumulator = 0xff; + machine.and(0); + assert_eq!(machine.registers.accumulator, 0); + assert_eq!(machine.registers.status.contains(PS_ZERO), true); + assert_eq!(machine.registers.status.contains(PS_NEGATIVE), false); + + machine.registers.accumulator = 0xff; + machine.and(0x0f); + assert_eq!(machine.registers.accumulator, 0x0f); + assert_eq!(machine.registers.status.contains(PS_ZERO), false); + assert_eq!(machine.registers.status.contains(PS_NEGATIVE), false); + + machine.registers.accumulator = 0xff; + machine.and(0xf0); + assert_eq!(machine.registers.accumulator, 0xf0); + assert_eq!(machine.registers.status.contains(PS_ZERO), false); + assert_eq!(machine.registers.status.contains(PS_NEGATIVE), true); +} + #[test] fn subtract_with_carry_test() { let mut machine = Machine::new(); From 413d063f8226865aa7dcf76fc2872e2703504139 Mon Sep 17 00:00:00 2001 From: Andrew Keeton Date: Thu, 6 Nov 2014 19:07:54 -0500 Subject: [PATCH 2/4] Add and(). --- src/machine.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/machine.rs b/src/machine.rs index e322160..e7b6cac 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -459,6 +459,11 @@ impl Machine { } } + fn and(&mut self, value: i8) { + let a_after = self.registers.accumulator & value; + self.load_accumulator(a_after); + } + // TODO: Implement binary-coded decimal fn subtract_with_carry(&mut self, value: i8) { if self.registers.status.contains(PS_DECIMAL_MODE) { From 198ddc3131d5be8a8e60364aed782d6318b6d812 Mon Sep 17 00:00:00 2001 From: Andrew Keeton Date: Thu, 6 Nov 2014 19:13:42 -0500 Subject: [PATCH 3/4] Fix and_test(). --- src/machine.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/machine.rs b/src/machine.rs index e7b6cac..374ace4 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -638,26 +638,26 @@ fn and_test() { let mut machine = Machine::new(); machine.registers.accumulator = 0; - machine.and(0xff); + machine.and(-1); assert_eq!(machine.registers.accumulator, 0); assert_eq!(machine.registers.status.contains(PS_ZERO), true); assert_eq!(machine.registers.status.contains(PS_NEGATIVE), false); - machine.registers.accumulator = 0xff; + machine.registers.accumulator = -1; machine.and(0); assert_eq!(machine.registers.accumulator, 0); assert_eq!(machine.registers.status.contains(PS_ZERO), true); assert_eq!(machine.registers.status.contains(PS_NEGATIVE), false); - machine.registers.accumulator = 0xff; + machine.registers.accumulator = -1; machine.and(0x0f); assert_eq!(machine.registers.accumulator, 0x0f); assert_eq!(machine.registers.status.contains(PS_ZERO), false); assert_eq!(machine.registers.status.contains(PS_NEGATIVE), false); - machine.registers.accumulator = 0xff; - machine.and(0xf0); - assert_eq!(machine.registers.accumulator, 0xf0); + machine.registers.accumulator = -1; + machine.and(-128); + assert_eq!(machine.registers.accumulator, -128); assert_eq!(machine.registers.status.contains(PS_ZERO), false); assert_eq!(machine.registers.status.contains(PS_NEGATIVE), true); } From c9f59dca603c1cba6e6a1d019740f3a6fd65a2ab Mon Sep 17 00:00:00 2001 From: Andrew Keeton Date: Thu, 6 Nov 2014 19:17:38 -0500 Subject: [PATCH 4/4] Add AND to execute_instruction. --- src/machine.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/machine.rs b/src/machine.rs index 374ace4..e95411d 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -88,6 +88,14 @@ impl Machine { self.add_with_carry(val); } + (instruction::AND, instruction::UseImmediate(val)) => { + self.and(val as i8); + } + (instruction::AND, instruction::UseAddress(addr)) => { + let val = self.memory.get_byte(addr) as i8; + self.and(val as i8); + } + (instruction::ASL, instruction::UseImplied) => { // Accumulator mode let mut val = self.registers.accumulator as u8;