1
0
mirror of https://github.com/mre/mos6502.git synced 2024-12-22 12:29:31 +00:00

Make compare() take a variable register.

This commit is contained in:
Andrew Keeton 2014-11-13 20:58:56 -05:00
parent a05a5dfde7
commit 99345e2b1f

View File

@ -189,11 +189,11 @@ impl Machine {
}
(instruction::CMP, instruction::UseImmediate(val)) => {
self.compare(val);
self.compare_with_a_register(val);
}
(instruction::CMP, instruction::UseAddress(addr)) => {
let val = self.memory.get_byte(addr);
self.compare(val);
self.compare_with_a_register(val);
}
(instruction::DEC, instruction::UseAddress(addr)) => {
@ -634,22 +634,20 @@ impl Machine {
// If the C flag is 1, then A (unsigned) >= NUM (unsigned) and BCS will branch
// ...
// The N flag contains most significant bit of the of the subtraction result.
fn compare(&mut self, val: u8) {
let a = self.registers.accumulator;
if a as u8 >= val as u8 {
fn compare(&mut self, r: i8, val: u8) {
if r as u8 >= val as u8 {
self.registers.status.insert(PS_CARRY);
} else {
self.registers.status.remove(PS_CARRY);
}
if a as i8 == val as i8 {
if r as i8 == val as i8 {
self.registers.status.insert(PS_ZERO);
} else {
self.registers.status.remove(PS_ZERO);
}
let diff: i8 = (a as i8) - (val as i8);
let diff: i8 = (r as i8) - (val as i8);
if diff < 0 {
self.registers.status.insert(PS_NEGATIVE);
} else {
@ -657,6 +655,11 @@ impl Machine {
}
}
fn compare_with_a_register(&mut self, val: u8) {
let a = self.registers.accumulator;
self.compare(a, val);
}
fn push_on_stack(&mut self, val: u8) {
let addr = self.registers.stack_pointer.to_address();
self.memory.set_byte(addr, val);
@ -1069,15 +1072,14 @@ fn branch_if_overflow_set_test() {
assert_eq!(machine.registers.program_counter, Address(0xABCD));
}
#[test]
fn compare_test() {
fn compare_test_helper(compare: |&mut Machine, u8|) {
let mut machine = Machine::new();
machine.execute_instruction(
(instruction::LDA, instruction::UseImmediate(127))
);
machine.compare(127);
compare(&mut machine, 127);
assert!( machine.registers.status.contains(PS_ZERO ));
assert!( machine.registers.status.contains(PS_CARRY ));
assert!(!machine.registers.status.contains(PS_NEGATIVE));
@ -1087,7 +1089,7 @@ fn compare_test() {
(instruction::LDA, instruction::UseImmediate(127))
);
machine.compare(1);
compare(&mut machine, 1);
assert!(!machine.registers.status.contains(PS_ZERO ));
assert!( machine.registers.status.contains(PS_CARRY ));
assert!(!machine.registers.status.contains(PS_NEGATIVE));
@ -1097,7 +1099,7 @@ fn compare_test() {
(instruction::LDA, instruction::UseImmediate(1))
);
machine.compare(2);
compare(&mut machine, 2);
assert!(!machine.registers.status.contains(PS_ZERO ));
assert!(!machine.registers.status.contains(PS_CARRY ));
assert!( machine.registers.status.contains(PS_NEGATIVE));
@ -1107,7 +1109,7 @@ fn compare_test() {
(instruction::LDA, instruction::UseImmediate(20))
);
machine.compare(-50);
compare(&mut machine, -50);
assert!(!machine.registers.status.contains(PS_ZERO ));
assert!(!machine.registers.status.contains(PS_CARRY ));
assert!(!machine.registers.status.contains(PS_NEGATIVE));
@ -1117,7 +1119,7 @@ fn compare_test() {
(instruction::LDA, instruction::UseImmediate(1))
);
machine.compare(-1);
compare(&mut machine, -1);
assert!(!machine.registers.status.contains(PS_ZERO ));
assert!(!machine.registers.status.contains(PS_CARRY ));
assert!(!machine.registers.status.contains(PS_NEGATIVE));
@ -1127,8 +1129,17 @@ fn compare_test() {
(instruction::LDA, instruction::UseImmediate(127))
);
machine.compare(-128);
compare(&mut machine, -128);
assert!(!machine.registers.status.contains(PS_ZERO ));
assert!(!machine.registers.status.contains(PS_CARRY ));
assert!( machine.registers.status.contains(PS_NEGATIVE));
}
#[test]
fn compare_with_a_register_test() {
compare_test_helper(
|machine: &mut Machine, val: u8| {
machine.compare_with_a_register(val);
}
);
}