From 71d15f9d0884a5dd65d5942645b16cab95a933ca Mon Sep 17 00:00:00 2001 From: Andrew Keeton Date: Mon, 13 Oct 2014 19:39:17 -0400 Subject: [PATCH 1/5] Add BMI implementation. Untested --- src/machine.rs | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/machine.rs b/src/machine.rs index df23a32..e226d96 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -48,7 +48,7 @@ impl Machine { memory: Memory::new() } } - + pub fn reset(&mut self) { *self = Machine::new(); } @@ -90,6 +90,12 @@ impl Machine { self.add_with_carry(val); }, + (instruction::BMI, instruction::UseRelative(rel)) => { + let addr = self.registers.program_counter + rel; + log!(log::DEBUG, "branch if minus relative. address: {}", addr); + self.branch_if_minus(addr); + }, + (instruction::LDA, instruction::UseImmediate(val)) => { log!(log::DEBUG, "load A immediate: {}", val); self.load_accumulator(val as i8); @@ -199,6 +205,12 @@ impl Machine { log!(log::DEBUG, "accumulator: {}", self.registers.accumulator); } + + pub fn branch_if_minus(&mut self, addr: Address) { + if (self.registers.status.contains(PS_NEGATIVE)) { + self.registers.program_counter = addr; + } + } } impl std::fmt::Show for Machine { @@ -210,7 +222,6 @@ impl std::fmt::Show for Machine { #[test] fn add_with_carry_test() { - let mut machine = Machine::new(); machine.add_with_carry(1); @@ -233,7 +244,7 @@ fn add_with_carry_test() { assert_eq!(machine.registers.status.contains(PS_ZERO), false); assert_eq!(machine.registers.status.contains(PS_NEGATIVE), false); assert_eq!(machine.registers.status.contains(PS_OVERFLOW), false); - + let mut machine = Machine::new(); machine.add_with_carry(127); @@ -281,3 +292,26 @@ fn add_with_carry_test() { assert_eq!(machine.registers.status.contains(PS_NEGATIVE), true); assert_eq!(machine.registers.status.contains(PS_OVERFLOW), true); } + +#[test] +fn branch_if_minus_test() { + { + let mut machine = Machine::new(); + let registers_before = machine.registers; + + machine.branch_if_minus(Address(0xABCD)); + assert_eq!(machine.registers, registers_before); + assert_eq!(machine.registers.program_counter, Address(0)); + } + + { + let mut machine = Machine::new(); + + machine.registers.status.set_with_mask(PS_NEGATIVE, PS_NEGATIVE); + let registers_before = machine.registers; + + machine.branch_if_minus(Address(0xABCD)); + assert_eq!(machine.registers, registers_before); + assert_eq!(machine.registers.program_counter, Address(0xABCD)); + } +} From d70915ad0f72479468e2795e3f33b8030ad574b6 Mon Sep 17 00:00:00 2001 From: Andrew Keeton Date: Thu, 16 Oct 2014 19:49:44 -0400 Subject: [PATCH 2/5] Compile tests. --- src/address.rs | 2 ++ src/registers.rs | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/address.rs b/src/address.rs index e2f2bac..6b4b334 100644 --- a/src/address.rs +++ b/src/address.rs @@ -37,6 +37,8 @@ pub struct Address(pub u16); impl Add for Address { fn add(&self, &AddressDiff(rhs): &AddressDiff) -> Address { let &Address(lhs) = self; + + // TODO akeeton: Do a checked cast. Address(((lhs as i32) + rhs) as u16) } } diff --git a/src/registers.rs b/src/registers.rs index bdbc684..ff5752a 100644 --- a/src/registers.rs +++ b/src/registers.rs @@ -54,6 +54,7 @@ impl StatusArgs { } pub bitflags! { +#[deriving(Show)] flags Status: u8 { const PS_NEGATIVE = 0b10000000, const PS_OVERFLOW = 0b01000000, @@ -112,7 +113,7 @@ impl Status { } } -#[deriving(PartialEq, Eq, PartialOrd, Ord)] +#[deriving(PartialEq, Eq, PartialOrd, Ord, Show)] pub struct StackPointer(pub u8); impl StackPointer { @@ -122,6 +123,7 @@ impl StackPointer { } } +#[deriving(PartialEq, Eq, Show)] pub struct Registers { pub accumulator: i8, pub index_x: i8, From 8759b15d9ef286d5f0bcbd250a57a1b6c86ccffb Mon Sep 17 00:00:00 2001 From: Andrew Keeton Date: Thu, 16 Oct 2014 20:00:53 -0400 Subject: [PATCH 3/5] branch_fi_minus_test() passes. --- src/machine.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/machine.rs b/src/machine.rs index e226d96..c22b77f 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -311,7 +311,7 @@ fn branch_if_minus_test() { let registers_before = machine.registers; machine.branch_if_minus(Address(0xABCD)); - assert_eq!(machine.registers, registers_before); + assert_eq!(machine.registers.status, registers_before.status); assert_eq!(machine.registers.program_counter, Address(0xABCD)); } } From 96f5f15d820ee18792c3360e884c7751fb2d0637 Mon Sep 17 00:00:00 2001 From: Andrew Keeton Date: Thu, 16 Oct 2014 22:02:18 -0400 Subject: [PATCH 4/5] Fix build. --- src/machine.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/machine.rs b/src/machine.rs index c22b77f..21574cf 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -91,7 +91,7 @@ impl Machine { }, (instruction::BMI, instruction::UseRelative(rel)) => { - let addr = self.registers.program_counter + rel; + let addr = self.registers.program_counter + AddressDiff(rel as i32); log!(log::DEBUG, "branch if minus relative. address: {}", addr); self.branch_if_minus(addr); }, @@ -207,7 +207,7 @@ impl Machine { } pub fn branch_if_minus(&mut self, addr: Address) { - if (self.registers.status.contains(PS_NEGATIVE)) { + if self.registers.status.contains(PS_NEGATIVE) { self.registers.program_counter = addr; } } From 2f25c490da9a9a9a4b276ecf6bd8f6ee690d2679 Mon Sep 17 00:00:00 2001 From: Andrew Keeton Date: Thu, 16 Oct 2014 22:12:43 -0400 Subject: [PATCH 5/5] Fix build. --- src/machine.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/machine.rs b/src/machine.rs index 21574cf..8518211 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -29,7 +29,7 @@ use log; use std; -use address::{AddressDiff}; +use address::{Address, AddressDiff}; use instruction; use instruction::{DecodedInstr}; use memory::Memory;