1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-12-11 00:52:13 +00:00
CLK/OSBindings/Mac/Clock SignalTests/EnterpriseDaveTests.mm

103 lines
3.0 KiB
Plaintext
Raw Normal View History

//
// EnterpriseDaveTests.m
// Clock SignalTests
//
// Created by Thomas Harte on 02/07/2021.
// Copyright © 2021 Thomas Harte. All rights reserved.
//
#import <XCTest/XCTest.h>
#include "../../../Machines/Enterprise/Dave.hpp"
#include <memory>
@interface EnterpriseDaveTests : XCTestCase
@end
@implementation EnterpriseDaveTests {
std::unique_ptr<Enterprise::Dave::TimedInterruptSource> _interruptSource;
}
- (void)setUp {
[super setUp];
_interruptSource = std::make_unique<Enterprise::Dave::TimedInterruptSource>();
}
/// Tests that the programmable timer flag toggles and produces interrupts
/// at the rate specified, and that the flag toggles when interrupts are signalled.
2021-07-07 00:47:49 +00:00
- (void)performTestExpectedInterrupts:(double)expectedInterruptsPerSecond mode:(int)mode {
// If a programmable timer mode is requested, synchronise both channels.
if(mode >= 2) {
_interruptSource->write(0xa7, 3);
_interruptSource->run_for(Cycles(2));
}
// Set mode (and disable sync, if it was applied).
_interruptSource->write(0xa7, mode << 5);
int toggles = 0;
int interrupts = 0;
uint8_t dividerState = _interruptSource->get_divider_state() & 1;
int nextSequencePoint = _interruptSource->next_sequence_point().as<int>();
for(int c = 0; c < 250000 * 5; c++) {
// Advance one cycle. Clock is 500,000 Hz.
_interruptSource->run_for(Cycles(2));
2021-07-07 00:28:32 +00:00
--nextSequencePoint;
2021-07-07 00:28:32 +00:00
// Check for a status bit change.
const uint8_t newDividerState = _interruptSource->get_divider_state();
const bool didToggle = (dividerState^newDividerState)&0x1;
dividerState = newDividerState;
2021-07-07 00:28:32 +00:00
toggles += didToggle;
// Check for the relevant interrupt.
const uint8_t newInterrupts = _interruptSource->get_new_interrupts();
2021-07-07 00:28:32 +00:00
if(newInterrupts) {
XCTAssertEqual(nextSequencePoint, 0);
nextSequencePoint = _interruptSource->next_sequence_point().as<int>();
2021-07-07 00:28:32 +00:00
if(newInterrupts & 0x02) {
++interrupts;
XCTAssertTrue(didToggle);
} else {
// Failing that, confirm that the other interrupt happend.
XCTAssertTrue(newInterrupts & 0x08);
}
}
XCTAssertEqual(nextSequencePoint, _interruptSource->next_sequence_point().as<int>(), @"At cycle %d", c);
}
XCTAssertEqual(toggles, int(expectedInterruptsPerSecond * 5.0));
XCTAssertEqual(interrupts, int(expectedInterruptsPerSecond * 5.0));
}
- (void)test1kHzTimer {
2021-07-07 00:47:49 +00:00
[self performTestExpectedInterrupts:1000.0 mode:0];
}
- (void)test50HzTimer {
2021-07-07 00:47:49 +00:00
[self performTestExpectedInterrupts:50.0 mode:1];
}
- (void)testTone0Timer {
// Set tone generator 0 as the interrupt source, with a divider of 137;
// apply sync momentarily.
_interruptSource->write(0, 137);
2021-07-03 03:57:11 +00:00
_interruptSource->write(1, 0);
2021-07-07 00:47:49 +00:00
[self performTestExpectedInterrupts:250000.0/(138.0 * 2.0) mode:2];
}
2021-07-03 03:57:11 +00:00
- (void)testTone1Timer {
// Set tone generator 1 as the interrupt source, with a divider of 961;
// apply sync momentarily.
_interruptSource->write(2, 961 & 0xff);
_interruptSource->write(3, (961 >> 8) & 0xff);
2021-07-07 00:47:49 +00:00
[self performTestExpectedInterrupts:250000.0/(962.0 * 2.0) mode:3];
2021-07-03 03:57:11 +00:00
}
@end