diff --git a/Components/6532/6532.hpp b/Components/6532/6532.hpp index 3df6178d3..0201cfa32 100644 --- a/Components/6532/6532.hpp +++ b/Components/6532/6532.hpp @@ -50,7 +50,7 @@ template class MOS6532 { case 0x07: _timer.writtenShift = _timer.activeShift = (decodedAddress - 0x04) * 3 + (decodedAddress / 0x07); // i.e. 0, 3, 6, 10 _timer.value = ((unsigned int)(value) << _timer.activeShift) | ((1 << _timer.activeShift)-1); - _timer.status &= ~0x40; + _timer.status &= ~0x80; break; } } @@ -89,7 +89,7 @@ template class MOS6532 { case 0x07: { uint8_t value = _timer.status; - _timer.status &= ~0x80; + _timer.status &= ~0x40; return value; } break; @@ -103,7 +103,8 @@ template class MOS6532 { if(_timer.value >= number_of_cycles) { _timer.value -= number_of_cycles; } else { - _timer.value = 0x100 + ((_timer.value - (number_of_cycles / 3)) >> _timer.activeShift); + number_of_cycles -= _timer.value; + _timer.value = 0x100 - number_of_cycles; _timer.activeShift = 0; _timer.status |= 0xc0; } diff --git a/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj b/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj index 06a18303e..efd57e617 100644 --- a/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj +++ b/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj @@ -15,6 +15,8 @@ 4B1414621B58888700E04248 /* KlausDormannTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B1414611B58888700E04248 /* KlausDormannTests.swift */; }; 4B1E85751D170228001EF87D /* Typer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B1E85731D170228001EF87D /* Typer.cpp */; }; 4B1E857C1D174DEC001EF87D /* 6532.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B1E857A1D174DEC001EF87D /* 6532.cpp */; }; + 4B1E857F1D17644D001EF87D /* MOS6532Bridge.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4B1E857E1D17644D001EF87D /* MOS6532Bridge.mm */; }; + 4B1E85811D176468001EF87D /* 6532Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B1E85801D176468001EF87D /* 6532Tests.swift */; }; 4B2409551C45AB05004DA684 /* Speaker.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B2409531C45AB05004DA684 /* Speaker.cpp */; }; 4B2A539F1D117D36003C6002 /* CSAudioQueue.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B2A53911D117D36003C6002 /* CSAudioQueue.m */; }; 4B2A53A01D117D36003C6002 /* CSMachine.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4B2A53961D117D36003C6002 /* CSMachine.mm */; }; @@ -357,6 +359,9 @@ 4B1E85741D170228001EF87D /* Typer.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = Typer.hpp; sourceTree = ""; }; 4B1E857A1D174DEC001EF87D /* 6532.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = 6532.cpp; sourceTree = ""; }; 4B1E857B1D174DEC001EF87D /* 6532.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = 6532.hpp; sourceTree = ""; }; + 4B1E857D1D17644D001EF87D /* MOS6532Bridge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MOS6532Bridge.h; sourceTree = ""; }; + 4B1E857E1D17644D001EF87D /* MOS6532Bridge.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MOS6532Bridge.mm; sourceTree = ""; }; + 4B1E85801D176468001EF87D /* 6532Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 6532Tests.swift; sourceTree = ""; }; 4B2409531C45AB05004DA684 /* Speaker.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Speaker.cpp; path = ../../Outputs/Speaker.cpp; sourceTree = ""; }; 4B2409541C45AB05004DA684 /* Speaker.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = Speaker.hpp; path = ../../Outputs/Speaker.hpp; sourceTree = ""; }; 4B24095A1C45DF85004DA684 /* Stepper.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = Stepper.hpp; sourceTree = ""; }; @@ -1234,11 +1239,14 @@ children = ( 4BB297DF1B587D8200A49093 /* Clock SignalTests-Bridging-Header.h */, 4BC751B41D157EB3006C31D9 /* MOS6522Bridge.h */, + 4B1E857D1D17644D001EF87D /* MOS6532Bridge.h */, 4BB297E21B587D8300A49093 /* TestMachine.h */, + 4B1E857E1D17644D001EF87D /* MOS6532Bridge.mm */, 4BC751B51D157EB3006C31D9 /* MOS6522Bridge.mm */, 4BB297E31B587D8300A49093 /* TestMachine.mm */, 4BB73EB81B587A5100552FC2 /* Info.plist */, 4BC751B11D157E61006C31D9 /* 6522Tests.swift */, + 4B1E85801D176468001EF87D /* 6532Tests.swift */, 4BB73EB61B587A5100552FC2 /* AllSuiteATests.swift */, 4B1414611B58888700E04248 /* KlausDormannTests.swift */, 4B92EAC91B7C112B00246143 /* TimingTests.swift */, @@ -1801,11 +1809,13 @@ 4BC751B61D157EB3006C31D9 /* MOS6522Bridge.mm in Sources */, 4B14145E1B5887AA00E04248 /* CPU6502AllRAM.cpp in Sources */, 4B14145D1B5887A600E04248 /* CPU6502.cpp in Sources */, + 4B1E85811D176468001EF87D /* 6532Tests.swift in Sources */, 4B92EACA1B7C112B00246143 /* TimingTests.swift in Sources */, 4BB73EB71B587A5100552FC2 /* AllSuiteATests.swift in Sources */, 4BC751B21D157E61006C31D9 /* 6522Tests.swift in Sources */, 4B1414621B58888700E04248 /* KlausDormannTests.swift in Sources */, 4BB298F01B587D8400A49093 /* TestMachine.mm in Sources */, + 4B1E857F1D17644D001EF87D /* MOS6532Bridge.mm in Sources */, 4B1414601B58885000E04248 /* WolfgangLorenzTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/OSBindings/Mac/Clock SignalTests/6532Tests.swift b/OSBindings/Mac/Clock SignalTests/6532Tests.swift new file mode 100644 index 000000000..2f29bf4e2 --- /dev/null +++ b/OSBindings/Mac/Clock SignalTests/6532Tests.swift @@ -0,0 +1,61 @@ +// +// 6532Tests.swift +// Clock Signal +// +// Created by Thomas Harte on 19/06/2016. +// Copyright © 2016 Thomas Harte. All rights reserved. +// + +import XCTest +import Foundation + +class MOS6532Tests: XCTestCase { + + private func with6532(action: (MOS6532Bridge) -> ()) { + let bridge = MOS6532Bridge() + action(bridge) + } + + // MARK: Timer tests + func testOneTickTimer() { + with6532 { + // set a count of 128 at single-clock intervals + $0.setValue(128, forRegister:4) + + // run for one clock and the count should now be 127 + $0.runForCycles(1) + XCTAssert($0.valueForRegister(4) == 127, "A single tick should decrease the counter once") + + // run for a further 200 clock counts; timer should reach -73 = 183 + $0.runForCycles(200) + XCTAssert($0.valueForRegister(4) == 183, "Timer should underflow and keep counting") + } + } + + func testEightTickTimer() { + with6532 { + // set a count of 28 at eight-clock intervals + $0.setValue(28, forRegister:5) + + // run for seven clock and the count should still be 28 + $0.runForCycles(7) + XCTAssert($0.valueForRegister(4) == 28, "The timer should remain unchanged for seven clocks") + + // run for a further clock and the count should now be 27 + $0.runForCycles(1) + XCTAssert($0.valueForRegister(4) == 27, "The timer should have decremented once after 8 cycles") + + // run for a further 7 + 27*8 + 5 = 228 clock counts; timer should reach -5 = 0xfb + $0.runForCycles(228) + XCTAssert($0.valueForRegister(4) == 0xfb, "Timer should underflow and start counting at single-clock pace") + + // timer should now resume dividing by eight + $0.runForCycles(7) + XCTAssert($0.valueForRegister(4) == 0xfb, "Timer should remain unchanged for seven cycles") + + // timer should now resume dividing by eight + $0.runForCycles(1) + XCTAssert($0.valueForRegister(4) == 0xfa, "Timer should decrement after eighth cycle") + } + } +} diff --git a/OSBindings/Mac/Clock SignalTests/Clock SignalTests-Bridging-Header.h b/OSBindings/Mac/Clock SignalTests/Clock SignalTests-Bridging-Header.h index 0bcb4a76b..b14444445 100644 --- a/OSBindings/Mac/Clock SignalTests/Clock SignalTests-Bridging-Header.h +++ b/OSBindings/Mac/Clock SignalTests/Clock SignalTests-Bridging-Header.h @@ -4,3 +4,4 @@ #import "TestMachine.h" #import "MOS6522Bridge.h" +#import "MOS6532Bridge.h" diff --git a/OSBindings/Mac/Clock SignalTests/MOS6532Bridge.h b/OSBindings/Mac/Clock SignalTests/MOS6532Bridge.h new file mode 100644 index 000000000..47d0f7ebc --- /dev/null +++ b/OSBindings/Mac/Clock SignalTests/MOS6532Bridge.h @@ -0,0 +1,22 @@ +// +// MOS6532Bridge.h +// Clock Signal +// +// Created by Thomas Harte on 19/06/2016. +// Copyright © 2016 Thomas Harte. All rights reserved. +// + +#import + +@interface MOS6532Bridge : NSObject + +@property (nonatomic, readonly) BOOL irqLine; +@property (nonatomic) uint8_t portBInput; +@property (nonatomic) uint8_t portAInput; + +- (void)setValue:(uint8_t)value forRegister:(NSUInteger)registerNumber; +- (uint8_t)valueForRegister:(NSUInteger)registerNumber; + +- (void)runForCycles:(NSUInteger)numberOfCycles; + +@end diff --git a/OSBindings/Mac/Clock SignalTests/MOS6532Bridge.mm b/OSBindings/Mac/Clock SignalTests/MOS6532Bridge.mm new file mode 100644 index 000000000..c7db4c24f --- /dev/null +++ b/OSBindings/Mac/Clock SignalTests/MOS6532Bridge.mm @@ -0,0 +1,35 @@ +// +// MOS6532Bridge.m +// Clock Signal +// +// Created by Thomas Harte on 19/06/2016. +// Copyright © 2016 Thomas Harte. All rights reserved. +// + +#import "MOS6532Bridge.h" +#include "6532.hpp" + +class VanillaRIOT: public MOS::MOS6532 { +}; + +@implementation MOS6532Bridge +{ + VanillaRIOT _riot; +} + +- (void)setValue:(uint8_t)value forRegister:(NSUInteger)registerNumber +{ + _riot.set_register((int)registerNumber, value); +} + +- (uint8_t)valueForRegister:(NSUInteger)registerNumber +{ + return _riot.get_register((int)registerNumber); +} + +- (void)runForCycles:(NSUInteger)numberOfCycles +{ + _riot.run_for_cycles((int)numberOfCycles); +} + +@end