From 52280a9646a74fb673ac53db2f285ffdb03e5f04 Mon Sep 17 00:00:00 2001 From: Alex Weisberger Date: Tue, 7 Oct 2014 23:52:38 -0400 Subject: [PATCH 1/6] DEX instruction and test --- src/machine.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/machine.rs b/src/machine.rs index 48a7d4e..e42a771 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -90,6 +90,10 @@ impl Machine { self.add_with_carry(val); }, + (instruction::DEX, instruction::UseImplied) => { + self.dec_x(); + } + (instruction::LDA, instruction::UseImmediate(val)) => { log!(log::DEBUG, "load A immediate: {}", val); self.load_accumulator(val as i8); @@ -199,6 +203,26 @@ impl Machine { log!(log::DEBUG, "accumulator: {}", self.registers.accumulator); } + + pub fn dec_x(&mut self) { + let x_before = self.registers.index_x; + let value = 1i8; + let x_after = x_before - value; + self.registers.index_x = x_before - 1; + + let is_negative = x_after < 0; + let is_zero = x_after == 0; + + let mask = ps_negative | ps_zero; + self.registers.status.set_with_mask( + mask, + Status::new(StatusArgs { + negative: is_negative, + zero: is_zero, + ..StatusArgs::none() + }) + ); + } } impl std::fmt::Show for Machine { @@ -281,3 +305,22 @@ 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 dec_x_test() { + let mut machine = Machine::new(); + + machine.dec_x(); + assert_eq!(machine.registers.index_x, -1); + assert_eq!(machine.registers.status.contains(ps_carry), false); + assert_eq!(machine.registers.status.contains(ps_zero), false); + assert_eq!(machine.registers.status.contains(ps_negative), true); + assert_eq!(machine.registers.status.contains(ps_overflow), false); + + machine.dec_x(); + assert_eq!(machine.registers.index_x, -2); + assert_eq!(machine.registers.status.contains(ps_carry), false); + assert_eq!(machine.registers.status.contains(ps_zero), false); + assert_eq!(machine.registers.status.contains(ps_negative), true); + assert_eq!(machine.registers.status.contains(ps_overflow), false); +} From 702129ddeb3970352c9876645f3e7f6c004b6e4b Mon Sep 17 00:00:00 2001 From: Alex Weisberger Date: Tue, 7 Oct 2014 23:56:27 -0400 Subject: [PATCH 2/6] beef up test --- src/machine.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/machine.rs b/src/machine.rs index e42a771..92c5354 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -323,4 +323,30 @@ fn dec_x_test() { assert_eq!(machine.registers.status.contains(ps_zero), false); assert_eq!(machine.registers.status.contains(ps_negative), true); assert_eq!(machine.registers.status.contains(ps_overflow), false); + + machine.load_x_register(5); + machine.dec_x(); + assert_eq!(machine.registers.index_x, 4); + assert_eq!(machine.registers.status.contains(ps_carry), false); + 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); + + machine.dec_x(); + machine.dec_x(); + machine.dec_x(); + machine.dec_x(); + + assert_eq!(machine.registers.index_x, 0); + assert_eq!(machine.registers.status.contains(ps_carry), false); + assert_eq!(machine.registers.status.contains(ps_zero), true); + assert_eq!(machine.registers.status.contains(ps_negative), false); + assert_eq!(machine.registers.status.contains(ps_overflow), false); + + machine.dec_x(); + assert_eq!(machine.registers.index_x, -1); + assert_eq!(machine.registers.status.contains(ps_carry), false); + assert_eq!(machine.registers.status.contains(ps_zero), false); + assert_eq!(machine.registers.status.contains(ps_negative), true); + assert_eq!(machine.registers.status.contains(ps_overflow), false); } From d132f040f2a73ffcadecdb0e4b10f86f752bc32c Mon Sep 17 00:00:00 2001 From: Alex Weisberger Date: Tue, 7 Oct 2014 23:52:38 -0400 Subject: [PATCH 3/6] DEX instruction and test --- src/machine.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/machine.rs b/src/machine.rs index df23a32..14781d6 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -90,6 +90,10 @@ impl Machine { self.add_with_carry(val); }, + (instruction::DEX, instruction::UseImplied) => { + self.dec_x(); + } + (instruction::LDA, instruction::UseImmediate(val)) => { log!(log::DEBUG, "load A immediate: {}", val); self.load_accumulator(val as i8); @@ -199,6 +203,26 @@ impl Machine { log!(log::DEBUG, "accumulator: {}", self.registers.accumulator); } + + pub fn dec_x(&mut self) { + let x_before = self.registers.index_x; + let value = 1i8; + let x_after = x_before - value; + self.registers.index_x = x_before - 1; + + let is_negative = x_after < 0; + let is_zero = x_after == 0; + + let mask = ps_negative | ps_zero; + self.registers.status.set_with_mask( + mask, + Status::new(StatusArgs { + negative: is_negative, + zero: is_zero, + ..StatusArgs::none() + }) + ); + } } impl std::fmt::Show for Machine { @@ -281,3 +305,22 @@ 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 dec_x_test() { + let mut machine = Machine::new(); + + machine.dec_x(); + assert_eq!(machine.registers.index_x, -1); + assert_eq!(machine.registers.status.contains(ps_carry), false); + assert_eq!(machine.registers.status.contains(ps_zero), false); + assert_eq!(machine.registers.status.contains(ps_negative), true); + assert_eq!(machine.registers.status.contains(ps_overflow), false); + + machine.dec_x(); + assert_eq!(machine.registers.index_x, -2); + assert_eq!(machine.registers.status.contains(ps_carry), false); + assert_eq!(machine.registers.status.contains(ps_zero), false); + assert_eq!(machine.registers.status.contains(ps_negative), true); + assert_eq!(machine.registers.status.contains(ps_overflow), false); +} From 8fd38026aa5baade978bfea11dbb0247220ccf41 Mon Sep 17 00:00:00 2001 From: Alex Weisberger Date: Tue, 7 Oct 2014 23:56:27 -0400 Subject: [PATCH 4/6] beef up test --- src/machine.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/machine.rs b/src/machine.rs index 14781d6..ab0f2f0 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -323,4 +323,30 @@ fn dec_x_test() { assert_eq!(machine.registers.status.contains(ps_zero), false); assert_eq!(machine.registers.status.contains(ps_negative), true); assert_eq!(machine.registers.status.contains(ps_overflow), false); + + machine.load_x_register(5); + machine.dec_x(); + assert_eq!(machine.registers.index_x, 4); + assert_eq!(machine.registers.status.contains(ps_carry), false); + 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); + + machine.dec_x(); + machine.dec_x(); + machine.dec_x(); + machine.dec_x(); + + assert_eq!(machine.registers.index_x, 0); + assert_eq!(machine.registers.status.contains(ps_carry), false); + assert_eq!(machine.registers.status.contains(ps_zero), true); + assert_eq!(machine.registers.status.contains(ps_negative), false); + assert_eq!(machine.registers.status.contains(ps_overflow), false); + + machine.dec_x(); + assert_eq!(machine.registers.index_x, -1); + assert_eq!(machine.registers.status.contains(ps_carry), false); + assert_eq!(machine.registers.status.contains(ps_zero), false); + assert_eq!(machine.registers.status.contains(ps_negative), true); + assert_eq!(machine.registers.status.contains(ps_overflow), false); } From dd1d52ae2408cc02c8371c4cb16e9f3b0a3f6e97 Mon Sep 17 00:00:00 2001 From: Alex Weisberger Date: Thu, 16 Oct 2014 20:05:41 -0400 Subject: [PATCH 5/6] Rebase onto master to pick up compiler errors and use load_x_register to simplify impl of dec_x. Because load_x_register sets the zero and negative status flags already --- src/machine.rs | 59 +++++++++++++++++++------------------------------- 1 file changed, 22 insertions(+), 37 deletions(-) diff --git a/src/machine.rs b/src/machine.rs index ab0f2f0..0ee94c4 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -205,23 +205,8 @@ impl Machine { } pub fn dec_x(&mut self) { - let x_before = self.registers.index_x; - let value = 1i8; - let x_after = x_before - value; - self.registers.index_x = x_before - 1; - - let is_negative = x_after < 0; - let is_zero = x_after == 0; - - let mask = ps_negative | ps_zero; - self.registers.status.set_with_mask( - mask, - Status::new(StatusArgs { - negative: is_negative, - zero: is_zero, - ..StatusArgs::none() - }) - ); + let val = self.registers.index_x; + self.load_x_register(val - 1); } } @@ -312,25 +297,25 @@ fn dec_x_test() { machine.dec_x(); assert_eq!(machine.registers.index_x, -1); - assert_eq!(machine.registers.status.contains(ps_carry), false); - assert_eq!(machine.registers.status.contains(ps_zero), false); - assert_eq!(machine.registers.status.contains(ps_negative), true); - assert_eq!(machine.registers.status.contains(ps_overflow), false); + assert_eq!(machine.registers.status.contains(PS_CARRY), false); + assert_eq!(machine.registers.status.contains(PS_ZERO), false); + assert_eq!(machine.registers.status.contains(PS_NEGATIVE), true); + assert_eq!(machine.registers.status.contains(PS_OVERFLOW), false); machine.dec_x(); assert_eq!(machine.registers.index_x, -2); - assert_eq!(machine.registers.status.contains(ps_carry), false); - assert_eq!(machine.registers.status.contains(ps_zero), false); - assert_eq!(machine.registers.status.contains(ps_negative), true); - assert_eq!(machine.registers.status.contains(ps_overflow), false); + assert_eq!(machine.registers.status.contains(PS_CARRY), false); + assert_eq!(machine.registers.status.contains(PS_ZERO), false); + assert_eq!(machine.registers.status.contains(PS_NEGATIVE), true); + assert_eq!(machine.registers.status.contains(PS_OVERFLOW), false); machine.load_x_register(5); machine.dec_x(); assert_eq!(machine.registers.index_x, 4); - assert_eq!(machine.registers.status.contains(ps_carry), false); - 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); + assert_eq!(machine.registers.status.contains(PS_CARRY), false); + 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); machine.dec_x(); machine.dec_x(); @@ -338,15 +323,15 @@ fn dec_x_test() { machine.dec_x(); assert_eq!(machine.registers.index_x, 0); - assert_eq!(machine.registers.status.contains(ps_carry), false); - assert_eq!(machine.registers.status.contains(ps_zero), true); - assert_eq!(machine.registers.status.contains(ps_negative), false); - assert_eq!(machine.registers.status.contains(ps_overflow), false); + assert_eq!(machine.registers.status.contains(PS_CARRY), false); + assert_eq!(machine.registers.status.contains(PS_ZERO), true); + assert_eq!(machine.registers.status.contains(PS_NEGATIVE), false); + assert_eq!(machine.registers.status.contains(PS_OVERFLOW), false); machine.dec_x(); assert_eq!(machine.registers.index_x, -1); - assert_eq!(machine.registers.status.contains(ps_carry), false); - assert_eq!(machine.registers.status.contains(ps_zero), false); - assert_eq!(machine.registers.status.contains(ps_negative), true); - assert_eq!(machine.registers.status.contains(ps_overflow), false); + assert_eq!(machine.registers.status.contains(PS_CARRY), false); + assert_eq!(machine.registers.status.contains(PS_ZERO), false); + assert_eq!(machine.registers.status.contains(PS_NEGATIVE), true); + assert_eq!(machine.registers.status.contains(PS_OVERFLOW), false); } From 446c5ca3afa8ea4ffe415d421369a16ce92d64b8 Mon Sep 17 00:00:00 2001 From: Alex Weisberger Date: Thu, 16 Oct 2014 20:18:03 -0400 Subject: [PATCH 6/6] Remove duplicate test from weird merge pull idk --- src/machine.rs | 45 --------------------------------------------- 1 file changed, 45 deletions(-) diff --git a/src/machine.rs b/src/machine.rs index 2bc581d..0ee94c4 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -335,48 +335,3 @@ fn dec_x_test() { assert_eq!(machine.registers.status.contains(PS_NEGATIVE), true); assert_eq!(machine.registers.status.contains(PS_OVERFLOW), false); } - -#[test] -fn dec_x_test() { - let mut machine = Machine::new(); - - machine.dec_x(); - assert_eq!(machine.registers.index_x, -1); - assert_eq!(machine.registers.status.contains(ps_carry), false); - assert_eq!(machine.registers.status.contains(ps_zero), false); - assert_eq!(machine.registers.status.contains(ps_negative), true); - assert_eq!(machine.registers.status.contains(ps_overflow), false); - - machine.dec_x(); - assert_eq!(machine.registers.index_x, -2); - assert_eq!(machine.registers.status.contains(ps_carry), false); - assert_eq!(machine.registers.status.contains(ps_zero), false); - assert_eq!(machine.registers.status.contains(ps_negative), true); - assert_eq!(machine.registers.status.contains(ps_overflow), false); - - machine.load_x_register(5); - machine.dec_x(); - assert_eq!(machine.registers.index_x, 4); - assert_eq!(machine.registers.status.contains(ps_carry), false); - 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); - - machine.dec_x(); - machine.dec_x(); - machine.dec_x(); - machine.dec_x(); - - assert_eq!(machine.registers.index_x, 0); - assert_eq!(machine.registers.status.contains(ps_carry), false); - assert_eq!(machine.registers.status.contains(ps_zero), true); - assert_eq!(machine.registers.status.contains(ps_negative), false); - assert_eq!(machine.registers.status.contains(ps_overflow), false); - - machine.dec_x(); - assert_eq!(machine.registers.index_x, -1); - assert_eq!(machine.registers.status.contains(ps_carry), false); - assert_eq!(machine.registers.status.contains(ps_zero), false); - assert_eq!(machine.registers.status.contains(ps_negative), true); - assert_eq!(machine.registers.status.contains(ps_overflow), false); -}