1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-07-27 02:29:12 +00:00
CLK/OSBindings/Mac/Clock SignalTests/TimingTests.swift

102 lines
3.9 KiB
Swift
Raw Normal View History

//
// TimingTests.swift
// Clock Signal
//
// Created by Thomas Harte on 13/08/2015.
// Copyright © 2015 Thomas Harte. All rights reserved.
//
import Foundation
import XCTest
class TimingTests: XCTestCase, CSTestMachineJamHandler {
private var endTime: UInt32 = 0
func testImpliedNOP() {
let code: [UInt8] = [0xea, CSTestMachineJamOpcode]
self.runTest(code, expectedRunLength: 2)
}
func testLDA() {
let code: [UInt8] = [
0xa9, 0x00, // [2] LDA #$00
0xa5, 0x00, // [3] LDA $00
0xb5, 0x00, // [4] LDA $00,X
0xad, 0x00, 0x00, // [4] LDA $0000
0xbd, 0x00, 0x00, // [4] LDA $0000, x (no wrap)
0xbd, 0x02, 0x00, // [5] LDA $0002, x (wrap)
0xb9, 0x00, 0x00, // [4] LDA $0000, y (no wrap)
0xb9, 0x10, 0x00, // [5] LDA $0010, y (wrap)
0xa1, 0x44, // [6] LDA ($44, x)
0xb1, 0x00, // [5] LDA ($00), y (no wrap)
0xb1, 0x02, // [6] LDA ($01), y (wrap)
CSTestMachineJamOpcode]
self.runTest(code, expectedRunLength: 48)
}
func testBCS() {
let code: [UInt8] = [
0x18, // [2] CLC
0xb0, 0xff, // [2] BCS -1 (not taken)
0x90, 0x7f, // [3] BCC +7f (taken)
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x90, 0x00, // [3] BCC 0 (taken)
0x90, 0x7f, // [4] BCC +6f (taken, page wrap)
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
CSTestMachineJamOpcode]
self.runTest(code, expectedRunLength: 14)
}
func testSnippet1() {
let code: [UInt8] = [
0x8d, 0x08, 0x00, // [4] STA $0008
0xc6, 0xb4, // [5] DEC $B4
CSTestMachineJamOpcode]
self.runTest(code, expectedRunLength: 9)
}
func runTest(code: [UInt8], expectedRunLength: UInt32) {
let machine = CSTestMachine()
machine.jamHandler = self
let immediateCode = NSData(bytes: code, length: code.count)
machine.setData(immediateCode, atAddress: 0x200)
machine.setValue(0x00, forAddress: 0x0000)
machine.setValue(0x00, forAddress: 0x0001)
machine.setValue(0xff, forAddress: 0x0002)
machine.setValue(0x00, forAddress: 0x0003)
machine.setValue(0x200, forRegister: CSTestMachineRegister.ProgramCounter)
machine.setValue(0xff, forRegister: CSTestMachineRegister.X)
machine.setValue(0xfe, forRegister: CSTestMachineRegister.Y)
self.endTime = 0
while self.endTime == 0 {
machine.runForNumberOfCycles(10)
}
XCTAssert(self.endTime == expectedRunLength, "Took \(self.endTime) cycles to perform")
}
func testMachine(machine: CSTestMachine!, didJamAtAddress address: UInt16) {
if self.endTime == 0 {
self.endTime = machine.timestamp - 7
}
}
}