more optimization of the CPU

This commit is contained in:
Luigi Thirty 2017-07-31 17:09:37 -04:00
parent b6a65d1eb7
commit 45e5c3c658
3 changed files with 10 additions and 13 deletions

View File

@ -58,7 +58,6 @@ class AppleI: NSObject {
} }
func runFrame() { func runFrame() {
CPU.sharedInstance.cycles = 0 CPU.sharedInstance.cycles = 0
CPU.sharedInstance.cyclesInBatch = AppleI.CYCLES_PER_BATCH CPU.sharedInstance.cyclesInBatch = AppleI.CYCLES_PER_BATCH
@ -69,17 +68,12 @@ class AppleI: NSObject {
let pixelBase = CVPixelBufferGetBaseAddress(emulatorViewDelegate.pixels!) let pixelBase = CVPixelBufferGetBaseAddress(emulatorViewDelegate.pixels!)
let buf = pixelBase?.assumingMemoryBound(to: BitmapPixelsBE555.PixelData.self) let buf = pixelBase?.assumingMemoryBound(to: BitmapPixelsBE555.PixelData.self)
let startTime = CFAbsoluteTimeGetCurrent()
for (cellNum, character) in terminal.characters.enumerated() { for (cellNum, character) in terminal.characters.enumerated() {
emulatorViewDelegate.putCharacterPixels(buffer: buf, emulatorViewDelegate.putCharacterPixels(buffer: buf,
charPixels: cg.getCharacterPixels(charIndex: character), charPixels: cg.getCharacterPixels(charIndex: character),
pixelPosition: emulatorViewDelegate.getPixelOffset(charCellIndex: cellNum)) pixelPosition: emulatorViewDelegate.getPixelOffset(charCellIndex: cellNum))
} }
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
print("Time elapsed for runFrame: \(timeElapsed) s.")
CVPixelBufferUnlockBaseAddress(emulatorViewDelegate.pixels!, CVPixelBufferLockFlags(rawValue: 0)) CVPixelBufferUnlockBaseAddress(emulatorViewDelegate.pixels!, CVPixelBufferLockFlags(rawValue: 0))
emulatorView.setNeedsDisplay(emulatorView.frame) emulatorView.setNeedsDisplay(emulatorView.frame)

View File

@ -215,9 +215,7 @@ class CPU: NSObject {
self.branch_was_taken = false self.branch_was_taken = false
} }
if(operation!.mnemonic != "JMP" && operation!.mnemonic != "JSR" && operation!.mnemonic != "BRK" && operation!.mnemonic != "RTI") { self.program_counter = UInt16(Int(self.program_counter) + operation!.bytes)
self.program_counter = UInt16(Int(self.program_counter) + operation!.bytes)
}
} }
func outOfCycles() -> Bool { func outOfCycles() -> Bool {
@ -254,6 +252,8 @@ class CPU: NSObject {
} }
func runCyclesBatch() { func runCyclesBatch() {
let startTime = CFAbsoluteTimeGetCurrent()
isRunning = true isRunning = true
while(!outOfCycles() && isRunning) { while(!outOfCycles() && isRunning) {
@ -264,6 +264,9 @@ class CPU: NSObject {
} }
} }
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
print("Time elapsed for runFrame: \(timeElapsed) s.")
} }
} }

View File

@ -586,12 +586,12 @@ class Opcodes: NSObject {
//Misc //Misc
static func JMP(state: CPU, addressingMode: AddressingMode) -> Void { static func JMP(state: CPU, addressingMode: AddressingMode) -> Void {
state.program_counter = getOperandAddressForAddressingMode(state: state, mode: addressingMode) state.program_counter = getOperandAddressForAddressingMode(state: state, mode: addressingMode) - 3
} }
static func JSR(state: CPU, addressingMode: AddressingMode) -> Void { static func JSR(state: CPU, addressingMode: AddressingMode) -> Void {
state.pushWord(data: state.program_counter + 2) state.pushWord(data: state.program_counter + 2)
state.program_counter = state.getOperandWord() state.program_counter = state.getOperandWord() - 3
} }
static func RTS(state: CPU, addressingMode: AddressingMode) -> Void { static func RTS(state: CPU, addressingMode: AddressingMode) -> Void {
@ -600,7 +600,7 @@ class Opcodes: NSObject {
static func RTI(state: CPU, addressingMode: AddressingMode) -> Void { static func RTI(state: CPU, addressingMode: AddressingMode) -> Void {
state.status_register.fromByte(state: state.popByte()) state.status_register.fromByte(state: state.popByte())
state.program_counter = state.popWord() state.program_counter = state.popWord() - 1
state.status_register.brk = false //RTI sets B to 0 state.status_register.brk = false //RTI sets B to 0
} }
@ -610,7 +610,7 @@ class Opcodes: NSObject {
state.pushWord(data: state.program_counter + 2) state.pushWord(data: state.program_counter + 2)
state.pushByte(data: sr.asByte()) state.pushByte(data: sr.asByte())
state.status_register.irq_disable = true //BRK disables interrupts before transferring control state.status_register.irq_disable = true //BRK disables interrupts before transferring control
state.program_counter = state.memoryInterface.readWord(offset: 0xFFFE) state.program_counter = state.memoryInterface.readWord(offset: 0xFFFE) - 1
} }
static func NOP(state: CPU, addressingMode: AddressingMode) -> Void {} static func NOP(state: CPU, addressingMode: AddressingMode) -> Void {}