fix tests

This commit is contained in:
Candygoblen123 2024-05-17 22:51:00 -04:00
parent 5bdfebdf13
commit 18ce4bebfb
No known key found for this signature in database
GPG Key ID: 577DA64EBEF10385
3 changed files with 54 additions and 18 deletions

View File

@ -103,18 +103,20 @@ class CPU {
} }
func run() { func run() {
run {} run(onCycle: {}, onComplete: {})
} }
func run(callback: @escaping () -> ()) { func run(onCycle: @escaping () -> (), onComplete: @escaping () -> ()) {
let opcodes = OPCODES_MAP let opcodes = OPCODES_MAP
Timer.scheduledTimer(withTimeInterval: 0.00007, repeats: true) { [self] timer in Timer.scheduledTimer(withTimeInterval: 0.00007, repeats: true) { [self] timer in
processOpcodes(callback: callback, opcodes: opcodes, timer: timer) processOpcodes(onCycle: onCycle, opcodes: opcodes, timer: timer) {
onComplete()
}
} }
} }
func processOpcodes(callback: () -> (), opcodes: [UInt8: OpCode], timer: Timer) { func processOpcodes(onCycle: () -> (), opcodes: [UInt8: OpCode], timer: Timer, onComplete: () -> ()) {
callback() onCycle()
let code = memRead(programCounter) let code = memRead(programCounter)
programCounter += 1 programCounter += 1
@ -278,9 +280,7 @@ class CPU {
/// BRK /// BRK
case 0x00: case 0x00:
timer.invalidate() timer.invalidate()
SDL_DestroyWindow(window) onComplete()
SDL_Quit()
exit(0)
default: fatalError("TODO!") default: fatalError("TODO!")
} }

View File

@ -124,7 +124,7 @@ cpu.reset()
var screenState = [UInt8](repeating: 0, count: 32 * 3 * 32) var screenState = [UInt8](repeating: 0, count: 32 * 3 * 32)
var rng = SystemRandomNumberGenerator() var rng = SystemRandomNumberGenerator()
cpu.run() { cpu.run(onCycle: {
handleUserInput(cpu, event: &event) handleUserInput(cpu, event: &event)
cpu.memWrite(0xfe, data: UInt8.random(in: 1...16, using: &rng)) cpu.memWrite(0xfe, data: UInt8.random(in: 1...16, using: &rng))
@ -133,7 +133,11 @@ cpu.run() {
SDL_RenderCopy(canvas, texture, nil, nil) SDL_RenderCopy(canvas, texture, nil, nil)
SDL_RenderPresent(canvas) SDL_RenderPresent(canvas)
} }
} }, onComplete: {
SDL_DestroyWindow(window)
SDL_Quit()
exit(0)
})
// Infinite loop otherwise the program will exit prematurely // Infinite loop otherwise the program will exit prematurely
RunLoop.main.run() RunLoop.main.run()

View File

@ -3,8 +3,13 @@ import XCTest
class CPUTests: XCTestCase { class CPUTests: XCTestCase {
func test_0xa9_lda_immediate_load_data() { func test_0xa9_lda_immediate_load_data() {
let expect = XCTestExpectation()
let cpu = CPU() let cpu = CPU()
cpu.loadAndRun([0xa9, 0x05, 0x00]) cpu.load([0xa9, 0x05, 0x00])
cpu.reset()
cpu.run(onCycle: {}, onComplete: { expect.fulfill() })
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)
@ -12,61 +17,88 @@ class CPUTests: XCTestCase {
} }
func test_lda_from_memory() { func test_lda_from_memory() {
let expect = XCTestExpectation()
let cpu = CPU() let cpu = CPU()
cpu.memWrite(0x10, data: 0x55) cpu.memWrite(0x10, data: 0x55)
cpu.loadAndRun([0xa5, 0x10, 0x00]) cpu.load([0xa5, 0x10, 0x00])
cpu.reset()
cpu.run(onCycle: {}, onComplete: { expect.fulfill() })
wait(for: [expect], timeout: 5.0)
XCTAssertEqual(cpu.register_a, 0x55) XCTAssertEqual(cpu.register_a, 0x55)
} }
func test_0xa9_lda_zero_flag() { func test_0xa9_lda_zero_flag() {
let expect = XCTestExpectation()
let cpu = CPU() let cpu = CPU()
cpu.loadAndRun([0xa9, 0x00, 0x00]) cpu.load([0xa9, 0x00, 0x00])
cpu.reset()
cpu.run(onCycle: {}, onComplete: { expect.fulfill() })
wait(for: [expect], timeout: 5.0)
XCTAssert(cpu.status.rawValue & 0b0000_0010 == 0b10) XCTAssert(cpu.status.rawValue & 0b0000_0010 == 0b10)
} }
func test_0xa9_lda_neg_flag() { func test_0xa9_lda_neg_flag() {
let expect = XCTestExpectation()
let cpu = CPU() let cpu = CPU()
cpu.loadAndRun([0xa9, 0xFF, 0x00]) cpu.load([0xa9, 0xFF, 0x00])
cpu.reset()
cpu.run(onCycle: {}, onComplete: { expect.fulfill() })
wait(for: [expect], timeout: 5.0)
XCTAssert(cpu.status.rawValue & 0b1000_0000 == 0b1000_0000) XCTAssert(cpu.status.rawValue & 0b1000_0000 == 0b1000_0000)
} }
func test_0xaa_tax_move_a_to_x() { func test_0xaa_tax_move_a_to_x() {
let expect = XCTestExpectation()
let cpu = CPU() 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() cpu.run(onCycle: {}, onComplete: { expect.fulfill() })
wait(for: [expect], timeout: 5.0)
XCTAssertEqual(cpu.register_x, 10) XCTAssertEqual(cpu.register_x, 10)
} }
func test_0xe8_inx_incriment_x() { func test_0xe8_inx_incriment_x() {
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() cpu.run(onCycle: {}, onComplete: { expect.fulfill() })
wait(for: [expect], timeout: 5.0)
XCTAssertEqual(cpu.register_x, 11) XCTAssertEqual(cpu.register_x, 11)
} }
func test_5_ops_working_together() { func test_5_ops_working_together() {
let expect = XCTestExpectation()
let cpu = CPU() let cpu = CPU()
cpu.loadAndRun([0xa9, 0xc0, 0xaa, 0xe8, 0x00]) 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) 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() cpu.run(onCycle: {}, onComplete: { expect.fulfill() })
wait(for: [expect], timeout: 5.0)
XCTAssertEqual(cpu.register_x, 1) XCTAssertEqual(cpu.register_x, 1)
} }
} }