fix tests

This commit is contained in:
2024-05-17 22:51:00 -04:00
parent 5bdfebdf13
commit 18ce4bebfb
3 changed files with 54 additions and 18 deletions

View File

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

View File

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