2024-05-10 22:06:45 -05:00
|
|
|
import XCTest
|
|
|
|
@testable import SwiftNES
|
|
|
|
|
|
|
|
class CPUTests: XCTestCase {
|
|
|
|
func test_0xa9_lda_immediate_load_data() {
|
2024-05-17 21:51:00 -05:00
|
|
|
let expect = XCTestExpectation()
|
2024-05-10 22:06:45 -05:00
|
|
|
let cpu = CPU()
|
2024-05-17 21:51:00 -05:00
|
|
|
cpu.load([0xa9, 0x05, 0x00])
|
|
|
|
cpu.reset()
|
|
|
|
cpu.run(onCycle: {}, onComplete: { expect.fulfill() })
|
|
|
|
|
|
|
|
wait(for: [expect], timeout: 5.0)
|
2024-05-10 22:06:45 -05:00
|
|
|
|
|
|
|
XCTAssertEqual(cpu.register_a, 0x05)
|
2024-05-11 22:30:17 -05:00
|
|
|
XCTAssert(cpu.status.rawValue & 0b0000_0010 == 0b00)
|
|
|
|
XCTAssert(cpu.status.rawValue & 0b1000_0000 == 0)
|
2024-05-10 22:06:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func test_lda_from_memory() {
|
2024-05-17 21:51:00 -05:00
|
|
|
let expect = XCTestExpectation()
|
2024-05-10 22:06:45 -05:00
|
|
|
let cpu = CPU()
|
2024-05-11 22:30:17 -05:00
|
|
|
cpu.memWrite(0x10, data: 0x55)
|
2024-05-10 22:06:45 -05:00
|
|
|
|
2024-05-17 21:51:00 -05:00
|
|
|
cpu.load([0xa5, 0x10, 0x00])
|
|
|
|
cpu.reset()
|
|
|
|
cpu.run(onCycle: {}, onComplete: { expect.fulfill() })
|
|
|
|
|
|
|
|
wait(for: [expect], timeout: 5.0)
|
2024-05-10 22:06:45 -05:00
|
|
|
|
|
|
|
XCTAssertEqual(cpu.register_a, 0x55)
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_0xa9_lda_zero_flag() {
|
2024-05-17 21:51:00 -05:00
|
|
|
let expect = XCTestExpectation()
|
2024-05-10 22:06:45 -05:00
|
|
|
let cpu = CPU()
|
2024-05-17 21:51:00 -05:00
|
|
|
cpu.load([0xa9, 0x00, 0x00])
|
|
|
|
cpu.reset()
|
|
|
|
cpu.run(onCycle: {}, onComplete: { expect.fulfill() })
|
|
|
|
|
|
|
|
wait(for: [expect], timeout: 5.0)
|
|
|
|
|
2024-05-11 22:30:17 -05:00
|
|
|
XCTAssert(cpu.status.rawValue & 0b0000_0010 == 0b10)
|
2024-05-10 22:06:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func test_0xa9_lda_neg_flag() {
|
2024-05-17 21:51:00 -05:00
|
|
|
let expect = XCTestExpectation()
|
2024-05-10 22:06:45 -05:00
|
|
|
let cpu = CPU()
|
2024-05-17 21:51:00 -05:00
|
|
|
cpu.load([0xa9, 0xFF, 0x00])
|
|
|
|
cpu.reset()
|
|
|
|
cpu.run(onCycle: {}, onComplete: { expect.fulfill() })
|
|
|
|
|
|
|
|
wait(for: [expect], timeout: 5.0)
|
2024-05-11 22:30:17 -05:00
|
|
|
XCTAssert(cpu.status.rawValue & 0b1000_0000 == 0b1000_0000)
|
2024-05-10 22:06:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func test_0xaa_tax_move_a_to_x() {
|
2024-05-17 21:51:00 -05:00
|
|
|
let expect = XCTestExpectation()
|
2024-05-10 22:06:45 -05:00
|
|
|
let cpu = CPU()
|
|
|
|
|
|
|
|
cpu.load([0xaa, 0x00])
|
|
|
|
cpu.reset()
|
|
|
|
cpu.register_a = 10
|
2024-05-17 21:51:00 -05:00
|
|
|
cpu.run(onCycle: {}, onComplete: { expect.fulfill() })
|
|
|
|
wait(for: [expect], timeout: 5.0)
|
|
|
|
|
2024-05-10 22:06:45 -05:00
|
|
|
XCTAssertEqual(cpu.register_x, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_0xe8_inx_incriment_x() {
|
2024-05-17 21:51:00 -05:00
|
|
|
let expect = XCTestExpectation()
|
2024-05-10 22:06:45 -05:00
|
|
|
let cpu = CPU()
|
|
|
|
|
|
|
|
cpu.load([0xe8, 0x00])
|
|
|
|
cpu.reset()
|
|
|
|
cpu.register_x = 10
|
2024-05-17 21:51:00 -05:00
|
|
|
cpu.run(onCycle: {}, onComplete: { expect.fulfill() })
|
|
|
|
wait(for: [expect], timeout: 5.0)
|
2024-05-10 22:06:45 -05:00
|
|
|
|
|
|
|
XCTAssertEqual(cpu.register_x, 11)
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_5_ops_working_together() {
|
2024-05-17 21:51:00 -05:00
|
|
|
let expect = XCTestExpectation()
|
2024-05-10 22:06:45 -05:00
|
|
|
let cpu = CPU()
|
2024-05-17 21:51:00 -05:00
|
|
|
cpu.load([0xa9, 0xc0, 0xaa, 0xe8, 0x00])
|
|
|
|
cpu.reset()
|
|
|
|
cpu.run(onCycle: {}, onComplete: { expect.fulfill() })
|
|
|
|
wait(for: [expect], timeout: 5.0)
|
2024-05-10 22:06:45 -05:00
|
|
|
|
|
|
|
XCTAssertEqual(cpu.register_x, 0xc1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_inx_overflow() {
|
2024-05-17 21:51:00 -05:00
|
|
|
let expect = XCTestExpectation()
|
2024-05-10 22:06:45 -05:00
|
|
|
let cpu = CPU()
|
|
|
|
|
|
|
|
cpu.load([0xe8, 0xe8, 0x00])
|
|
|
|
cpu.reset()
|
|
|
|
cpu.register_x = 0xff
|
2024-05-17 21:51:00 -05:00
|
|
|
cpu.run(onCycle: {}, onComplete: { expect.fulfill() })
|
|
|
|
wait(for: [expect], timeout: 5.0)
|
2024-05-10 22:06:45 -05:00
|
|
|
XCTAssertEqual(cpu.register_x, 1)
|
|
|
|
}
|
|
|
|
}
|