2016-07-09 19:44:55 +00:00
|
|
|
//
|
|
|
|
// C1540Tests.swift
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 09/07/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
|
|
|
|
|
|
|
class C1540Tests: XCTestCase {
|
|
|
|
|
2016-09-16 02:12:12 +00:00
|
|
|
fileprivate func with1540(_ action: (C1540Bridge) -> ()) {
|
2016-07-09 19:47:53 +00:00
|
|
|
let bridge = C1540Bridge()
|
|
|
|
|
2016-09-16 02:12:12 +00:00
|
|
|
if let path = Bundle.main.path(forResource: "1541", ofType: "bin", inDirectory: "ROMImages/Commodore1540") {
|
|
|
|
let data = try? Data(contentsOf: URL(fileURLWithPath: path))
|
2016-07-09 19:47:53 +00:00
|
|
|
bridge.setROM(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
action(bridge)
|
|
|
|
}
|
|
|
|
|
2016-09-16 02:12:12 +00:00
|
|
|
fileprivate func transmit(_ c1540: C1540Bridge, value: Int) {
|
2016-07-09 21:39:51 +00:00
|
|
|
var shiftedValue = value
|
2016-07-09 21:51:04 +00:00
|
|
|
|
|
|
|
c1540.dataLine = true
|
2016-09-16 02:12:12 +00:00
|
|
|
c1540.run(forCycles: 256)
|
2016-07-09 21:51:04 +00:00
|
|
|
XCTAssert(c1540.dataLine == false, "Listener should have taken data line low for start of transmission")
|
|
|
|
|
|
|
|
c1540.clockLine = true
|
2016-09-16 02:12:12 +00:00
|
|
|
c1540.run(forCycles: 256) // this isn't time limited on real hardware
|
2016-07-09 21:51:04 +00:00
|
|
|
XCTAssert(c1540.dataLine == true, "Listener should have let data line go high again")
|
|
|
|
|
2016-07-09 22:01:04 +00:00
|
|
|
// set up for byte transfer
|
|
|
|
c1540.clockLine = false
|
|
|
|
c1540.dataLine = true
|
2016-09-16 02:12:12 +00:00
|
|
|
c1540.run(forCycles: 40)
|
2016-07-09 21:51:04 +00:00
|
|
|
|
2016-07-09 22:01:04 +00:00
|
|
|
// transmit bits
|
|
|
|
for _ in 0..<8 {
|
2016-07-09 21:39:51 +00:00
|
|
|
// load data line
|
2016-07-09 22:06:49 +00:00
|
|
|
c1540.dataLine = (shiftedValue & 1) == 1
|
2016-07-09 21:39:51 +00:00
|
|
|
shiftedValue >>= 1
|
|
|
|
|
|
|
|
// toggle clock
|
2016-07-09 22:01:04 +00:00
|
|
|
c1540.clockLine = true
|
2016-09-16 02:12:12 +00:00
|
|
|
c1540.run(forCycles: 40)
|
2016-07-09 21:39:51 +00:00
|
|
|
c1540.clockLine = false
|
2016-09-16 02:12:12 +00:00
|
|
|
c1540.run(forCycles: 40)
|
2016-07-09 21:39:51 +00:00
|
|
|
}
|
2016-07-09 21:51:04 +00:00
|
|
|
|
|
|
|
// check for acknowledgment
|
|
|
|
c1540.dataLine = true
|
2016-09-16 02:12:12 +00:00
|
|
|
c1540.run(forCycles: 1000)
|
2016-07-09 21:51:04 +00:00
|
|
|
XCTAssert(c1540.dataLine == false, "Listener should have acknowledged byte")
|
2016-07-09 21:39:51 +00:00
|
|
|
}
|
|
|
|
|
2016-07-09 19:47:53 +00:00
|
|
|
// MARK: EOI
|
|
|
|
|
2016-07-09 21:22:10 +00:00
|
|
|
func testTransmission() {
|
2016-07-09 19:47:53 +00:00
|
|
|
with1540 {
|
2016-07-09 21:22:10 +00:00
|
|
|
// allow some booting time
|
2016-09-16 02:12:12 +00:00
|
|
|
$0.run(forCycles: 2000000)
|
2016-07-09 21:22:10 +00:00
|
|
|
|
2016-07-09 21:39:51 +00:00
|
|
|
// I want to be talker, so hold attention and clock low with data high
|
2016-07-09 21:22:10 +00:00
|
|
|
$0.clockLine = false
|
|
|
|
$0.attentionLine = false
|
2016-07-09 21:39:51 +00:00
|
|
|
$0.dataLine = true
|
2016-07-09 21:22:10 +00:00
|
|
|
|
2016-07-09 21:39:51 +00:00
|
|
|
// proceed 1 ms and check that the 1540 pulled the data line low
|
2016-09-16 02:12:12 +00:00
|
|
|
$0.run(forCycles: 1000)
|
2016-07-09 21:22:10 +00:00
|
|
|
XCTAssert($0.dataLine == false, "Listener should have taken data line low")
|
2016-07-09 21:39:51 +00:00
|
|
|
|
|
|
|
// transmit LISTEN #8
|
|
|
|
self.transmit($0, value: 0x28)
|
2016-07-09 19:47:53 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-09 19:44:55 +00:00
|
|
|
}
|