Skip to content

Commit

Permalink
Implement SBC fully, and finalize ADC/SBC
Browse files Browse the repository at this point in the history
Both main arithmetic operations are now complete.
Before turning in for the day, going to try to test/implement the rest
of the basic arithmatic operations so I can finally hit v0.1.1. IT
ONLY TOOK A YEAR.

SBC also has the same hold on decimal mode, going to add a note about
SBC on Issue #1.
  • Loading branch information
Spalynx committed Nov 19, 2018
1 parent e9aa613 commit ffdc6ce
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
19 changes: 14 additions & 5 deletions doc/NES_TODO.org
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ CLOSED: [2018-11-18 Sun 12:37]
CLOSED: [2018-11-18 Sun 12:38]
- State "DONE" from "TODO" [2018-11-18 Sun 12:38]
**** DONE testOP_LDY()
** TODO [20%] v0.1.1 : "Primary Arithmetic Operations"
** TODO [40%] v0.1.1 : "Primary Arithmetic Operations"
*** DONE ADC : "Add with Carry"
CLOSED: [2018-11-19 Mon 09:59]
- State "DONE" from "TODO" [2018-11-19 Mon 09:59]
Expand All @@ -47,10 +47,19 @@ CLOCK: [2018-11-18 Sun 10:51]--[2018-11-18 Sun 12:06] => 1:15
CLOSED: [2018-11-19 Mon 09:58]
- State "WAIT" from [2018-11-19 Mon 09:58] \\
See documentation on issue #1, or src/core/cpu_test.rs::testOP_ADC_decimal().
*** TODO SBC : "Subtract with Borrow"
**** TODO testOP_SBC()
**** TODO testOP_SBC_signed()
**** TODO testOP_SBC_decimal()
*** DONE SBC : "Subtract with Borrow"
CLOSED: [2018-11-19 Mon 11:39]
- State "DONE" from "TODO" [2018-11-19 Mon 11:39]
**** DONE testOP_SBC()
CLOSED: [2018-11-19 Mon 11:38]
- State "DONE" from "TODO" [2018-11-19 Mon 11:38]
**** DONE testOP_SBC_signed()
CLOSED: [2018-11-19 Mon 11:38]
- State "DONE" from "TODO" [2018-11-19 Mon 11:38]
**** WAIT testOP_SBC_decimal()
CLOSED: [2018-11-19 Mon 11:38]
- State "WAIT" from [2018-11-19 Mon 11:38] \\
Same as [[testOP_ADC_decimal()]]...
*** TODO AND : "Bitwise AND with Accumulator"
**** TODO testOP_AND()
*** TODO EOR : "Bitwise Exclusive OR with Accumulator"
Expand Down
12 changes: 10 additions & 2 deletions src/core/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ impl CPU {
//#! Flag OPCODES.
// Why not set_status_old()? Simply: Not fast enough.
// Wondering if I should make /fake/ OPs for other flags.
/// CLC - Clear Carry. Sets carry to false.

/// CLC - Clear Carry. Sets carry to false.
pub fn CLC(&mut self) { self.status = self.status ^ (1);}
/// CLD - Clear Decimal. Sets decimal to false.
pub fn CLD(&mut self) { self.status = self.status ^ (1 << 3); }
Expand Down Expand Up @@ -281,7 +281,15 @@ impl CPU {

/// SBC
/// Subtract with borrow.
pub fn SBC(&self) {}
/// This function implements all of ADC functionality by simply negating the
/// value of the operand.
/// NOTE: Without carry being added, the sent value is off-by-one. Supposedly
/// it is common practice to call SEC() prior to SBC(val).
pub fn SBC <AM: AddressingMode>(&mut self, am: AM){
let b: u8 = am.load(self) as u8;

self.ADC(ImmediateAM{address: b^0xFF});
}

/// AND
/// Bitwise AND with accumulator, takes memory address as parameter,
Expand Down
22 changes: 19 additions & 3 deletions src/core/cpu_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ pub mod cpu_test {

#[test]
fn testOP_ADC_signed (){
//Testing signed arithmetic.
let mut test_cpu = super::CPU::new();

test_cpu.LDA(ImmediateAM{address: 0b01111111u8}); //+127
Expand Down Expand Up @@ -376,14 +375,31 @@ pub mod cpu_test {
}
#[test]
fn testOP_SBC() {
assert!(false);
let mut cpu = super::CPU::new();

cpu.SEC(); //C is set in subtraction.
cpu.memory.set(0x755, 237);
cpu.LDA(AbsoluteAM{address: 0x755}); //Filling register a with first value.
cpu.SBC(ImmediateAM{address: 20}); //Subtracting second value to A.

assert_eq!(cpu.a, 217);
}
#[test]
fn testOP_SBC_signed() {
assert!(false);
let mut cpu = super::CPU::new();

cpu.SEC(); //C is set in subtraction.
cpu.LDA(ImmediateAM{address: 0b00000001u8}); //+1
cpu.SBC(ImmediateAM{address: 0b00000010u8}); //+2

//Tests of this addition:
assert_eq!(cpu.a, 0b11111111u8); // = -1 (254)
assert_eq!(cpu.get_status("C"), false, "SBC-Signed-Borrow Required");
}
#[test]
fn testOP_SBC_decimal() {
///Same hold as testOP_ADC_decimal()...
/// Task state on WAIT.
assert!(false);
}
#[test]
Expand Down

0 comments on commit ffdc6ce

Please sign in to comment.