add lots of CPU instruction tests

This commit is contained in:
Candygoblen123 2024-05-19 15:00:58 -04:00
parent 18ce4bebfb
commit 8c0de8c7a7
No known key found for this signature in database
GPG Key ID: 577DA64EBEF10385

View File

@ -2,103 +2,238 @@ import XCTest
@testable import SwiftNES @testable import SwiftNES
class CPUTests: XCTestCase { class CPUTests: XCTestCase {
func test_0xa9_lda_immediate_load_data() { func test_lda() {
let expect = XCTestExpectation()
let cpu = CPU() let cpu = CPU()
cpu.load([0xa9, 0x05, 0x00]) cpu.load([0xa9, 0x05, 0x00])
cpu.reset() cpu.reset()
cpu.run(onCycle: {}, onComplete: { expect.fulfill() }) cpu.runExpect(self)
wait(for: [expect], timeout: 5.0)
XCTAssertEqual(cpu.register_a, 0x05) XCTAssertEqual(cpu.register_a, 0x05)
XCTAssert(cpu.status.rawValue & 0b0000_0010 == 0b00) XCTAssert(cpu.status.rawValue & 0b0000_0010 == 0b00)
XCTAssert(cpu.status.rawValue & 0b1000_0000 == 0) XCTAssert(cpu.status.rawValue & 0b1000_0000 == 0)
} }
func test_lda_from_memory() { func test_ldy() {
let expect = XCTestExpectation()
let cpu = CPU() let cpu = CPU()
cpu.memWrite(0x10, data: 0x55) cpu.load([0xa0, 0x05, 0x00])
cpu.load([0xa5, 0x10, 0x00])
cpu.reset() cpu.reset()
cpu.run(onCycle: {}, onComplete: { expect.fulfill() }) cpu.runExpect(self)
wait(for: [expect], timeout: 5.0) XCTAssertEqual(cpu.register_y, 0x05)
XCTAssert(cpu.status.rawValue & 0b0000_0010 == 0b00)
XCTAssertEqual(cpu.register_a, 0x55) XCTAssert(cpu.status.rawValue & 0b1000_0000 == 0)
} }
func test_0xa9_lda_zero_flag() { func test_ldx() {
let expect = XCTestExpectation()
let cpu = CPU() let cpu = CPU()
cpu.load([0xa9, 0x00, 0x00]) cpu.load([0xa2, 0x05, 0x00])
cpu.reset() cpu.reset()
cpu.run(onCycle: {}, onComplete: { expect.fulfill() }) cpu.runExpect(self)
XCTAssertEqual(cpu.register_x, 0x05)
wait(for: [expect], timeout: 5.0) XCTAssert(cpu.status.rawValue & 0b0000_0010 == 0b00)
XCTAssert(cpu.status.rawValue & 0b1000_0000 == 0)
XCTAssert(cpu.status.rawValue & 0b0000_0010 == 0b10)
} }
func test_0xa9_lda_neg_flag() { func test_sta() {
let expect = XCTestExpectation()
let cpu = CPU() let cpu = CPU()
cpu.load([0xa9, 0xFF, 0x00]) cpu.load([0x85, 0x10, 0x00])
cpu.reset() cpu.reset()
cpu.run(onCycle: {}, onComplete: { expect.fulfill() }) cpu.register_a = 0x55
cpu.runExpect(self)
wait(for: [expect], timeout: 5.0) XCTAssertEqual(cpu.memRead(0x10), 0x55)
XCTAssert(cpu.status.rawValue & 0b1000_0000 == 0b1000_0000)
} }
func test_0xaa_tax_move_a_to_x() { func test_and() {
let expect = XCTestExpectation()
let cpu = CPU() let cpu = CPU()
cpu.load([0x29, 0b0000_1001, 0x00])
cpu.reset()
cpu.register_a = 0b0000_1010
cpu.runExpect(self)
XCTAssertEqual(cpu.register_a, 0b0000_1000)
}
func test_eor() {
let cpu = CPU()
cpu.load([0x49, 0b0000_1001, 0x00])
cpu.reset()
cpu.register_a = 0b0000_1110
cpu.runExpect(self)
XCTAssertEqual(cpu.register_a, 0b0000_0111)
}
func test_ora() {
let cpu = CPU()
cpu.load([0x09, 0b0000_1001, 0x00])
cpu.reset()
cpu.register_a = 0b0000_1110
cpu.runExpect(self)
XCTAssertEqual(cpu.register_a, 0b0000_1111)
}
func test_tax() {
let cpu = CPU()
cpu.load([0xaa, 0x00]) cpu.load([0xaa, 0x00])
cpu.reset() cpu.reset()
cpu.register_a = 10 cpu.register_a = 10
cpu.run(onCycle: {}, onComplete: { expect.fulfill() }) cpu.runExpect(self)
wait(for: [expect], timeout: 5.0)
XCTAssertEqual(cpu.register_x, 10) XCTAssertEqual(cpu.register_x, 10)
} }
func test_0xe8_inx_incriment_x() { func test_inx() {
let expect = XCTestExpectation()
let cpu = CPU() let cpu = CPU()
cpu.load([0xe8, 0x00]) cpu.load([0xe8, 0x00])
cpu.reset() cpu.reset()
cpu.register_x = 10 cpu.register_x = 10
cpu.run(onCycle: {}, onComplete: { expect.fulfill() }) cpu.runExpect(self)
wait(for: [expect], timeout: 5.0)
XCTAssertEqual(cpu.register_x, 11) XCTAssertEqual(cpu.register_x, 11)
} }
func test_5_ops_working_together() {
let expect = XCTestExpectation()
let cpu = CPU()
cpu.load([0xa9, 0xc0, 0xaa, 0xe8, 0x00])
cpu.reset()
cpu.run(onCycle: {}, onComplete: { expect.fulfill() })
wait(for: [expect], timeout: 5.0)
XCTAssertEqual(cpu.register_x, 0xc1)
}
func test_inx_overflow() { func test_inx_overflow() {
let expect = XCTestExpectation()
let cpu = CPU() let cpu = CPU()
cpu.load([0xe8, 0xe8, 0x00]) cpu.load([0xe8, 0xe8, 0x00])
cpu.reset() cpu.reset()
cpu.register_x = 0xff cpu.register_x = 0xff
cpu.run(onCycle: {}, onComplete: { expect.fulfill() }) cpu.runExpect(self)
wait(for: [expect], timeout: 5.0)
XCTAssertEqual(cpu.register_x, 1) XCTAssertEqual(cpu.register_x, 1)
} }
func test_iny() {
let cpu = CPU()
cpu.load([0xc8, 0x00])
cpu.reset()
cpu.register_y = 10
cpu.runExpect(self)
XCTAssertEqual(cpu.register_y, 11)
}
func test_iny_overflow() {
let cpu = CPU()
cpu.load([0xc8, 0xc8, 0x00])
cpu.reset()
cpu.register_y = 0xff
cpu.runExpect(self)
XCTAssertEqual(cpu.register_y, 1)
}
func test_sbc() {
let cpu = CPU()
cpu.load([0xe9, 2, 0x00])
cpu.reset()
cpu.register_a = 0x10
cpu.runExpect(self)
XCTAssertEqual(cpu.register_a, 0x0d)
}
func test_sbc_underflow() {
let cpu = CPU()
cpu.load([0xe9, 2, 0x00])
cpu.reset()
cpu.register_a = 0x00
cpu.runExpect(self)
XCTAssertEqual(cpu.register_a, 0xfd)
}
func test_adc() {
let cpu = CPU()
cpu.load([0x69, 2, 0x00])
cpu.reset()
cpu.register_a = 0x10
cpu.runExpect(self)
XCTAssertEqual(cpu.register_a, 0x12)
}
func test_adc_overflow() {
let cpu = CPU()
cpu.load([0x69, 2, 0x00])
cpu.reset()
cpu.register_a = 0xff
cpu.runExpect(self)
XCTAssertEqual(cpu.register_a, 0x01)
}
func test_asl_accumulator() {
let cpu = CPU()
cpu.load([0x0a, 0x00])
cpu.reset()
cpu.register_a = 0b0000_0001
cpu.runExpect(self)
XCTAssertEqual(cpu.register_a, 0b0000_0010)
}
func test_asl() {
let cpu = CPU()
cpu.load([0x06, 0x10, 0x00])
cpu.reset()
cpu.memWrite(0x10, data: 0b0000_0001)
cpu.runExpect(self)
XCTAssertEqual(cpu.memRead(0x10), 0b0000_0010)
}
func test_lsr_accumulator() {
let cpu = CPU()
cpu.load([0x4a, 0x00])
cpu.reset()
cpu.register_a = 0b0001_0000
cpu.runExpect(self)
XCTAssertEqual(cpu.register_a, 0b0000_1000)
}
func test_lsr() {
let cpu = CPU()
cpu.load([0x46, 0x10, 0x00])
cpu.reset()
cpu.memWrite(0x10, data: 0b0001_0000)
cpu.runExpect(self)
XCTAssertEqual(cpu.memRead(0x10), 0b0000_1000)
}
func test_rol_accumulator() {
let cpu = CPU()
cpu.load([0x2A, 0x00])
cpu.reset()
cpu.setRegisterA(0b1001_1001) // 0x99
cpu.runExpect(self)
XCTAssertEqual(cpu.register_a, 0b0011_0010)
}
func test_rol() {
let cpu = CPU()
cpu.load([0x26, 0x10, 0x00])
cpu.reset()
cpu.memWrite(0x10, data: 0b1001_1001)
cpu.runExpect(self)
XCTAssertEqual(cpu.memRead(0x10), 0b0011_0010)
}
func test_ror_accumulator() {
let cpu = CPU()
cpu.load([0x6a, 0x00])
cpu.reset()
cpu.setRegisterA(0b1001_1001)
cpu.runExpect(self)
XCTAssertEqual(cpu.register_a, 0b0100_1100)
}
func test_5_ops_working_together() {
let cpu = CPU()
cpu.load([0xa9, 0xc0, 0xaa, 0xe8, 0x00])
cpu.reset()
cpu.runExpect(self)
XCTAssertEqual(cpu.register_x, 0xc1)
}
}
extension XCTestExpectation {
func wait(_ sender: XCTestCase) {
sender.wait(for: [self], timeout: 0.1)
}
}
extension CPU {
func runExpect(_ sender: XCTestCase) {
let expect = XCTestExpectation()
self.run(onCycle: {}, onComplete: { expect.fulfill() })
expect.wait(sender)
}
} }