2015-07-16 20:40:46 -04:00
|
|
|
//
|
|
|
|
// Atari2600.m
|
2015-07-26 15:25:11 -04:00
|
|
|
// CLK
|
2015-07-16 20:40:46 -04:00
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 14/07/2015.
|
|
|
|
// Copyright © 2015 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2015-07-27 21:15:10 -04:00
|
|
|
#import "CSAtari2600.h"
|
2015-07-16 20:40:46 -04:00
|
|
|
#import "Atari2600.hpp"
|
|
|
|
|
2015-07-23 20:07:35 -04:00
|
|
|
@interface CSAtari2600 (Callbacks)
|
2015-07-31 18:04:33 -04:00
|
|
|
- (void)crtDidEndFrame:(CRTFrame *)frame didDetectVSync:(BOOL)didDetectVSync;
|
2015-07-23 20:07:35 -04:00
|
|
|
@end
|
|
|
|
|
|
|
|
struct Atari2600CRTDelegate: public Outputs::CRT::CRTDelegate {
|
2015-07-27 21:17:53 -04:00
|
|
|
__weak CSAtari2600 *atari;
|
2015-07-31 18:04:33 -04:00
|
|
|
void crt_did_end_frame(Outputs::CRT *crt, CRTFrame *frame, bool did_detect_vsync) { [atari crtDidEndFrame:frame didDetectVSync:did_detect_vsync]; }
|
2015-07-23 20:07:35 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
@implementation CSAtari2600 {
|
|
|
|
Atari2600::Machine _atari2600;
|
|
|
|
Atari2600CRTDelegate _crtDelegate;
|
|
|
|
|
|
|
|
dispatch_queue_t _serialDispatchQueue;
|
2015-07-31 18:04:33 -04:00
|
|
|
|
|
|
|
int _failedVSyncCount;
|
2015-07-23 20:07:35 -04:00
|
|
|
}
|
|
|
|
|
2015-07-31 18:04:33 -04:00
|
|
|
- (void)crtDidEndFrame:(CRTFrame *)frame didDetectVSync:(BOOL)didDetectVSync {
|
|
|
|
|
|
|
|
if(!didDetectVSync)
|
|
|
|
{
|
2015-08-02 20:32:18 -04:00
|
|
|
_failedVSyncCount+=2;
|
2015-07-31 18:04:33 -04:00
|
|
|
|
|
|
|
if(_failedVSyncCount == 60)
|
|
|
|
{
|
|
|
|
_atari2600.switch_region();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-08-02 20:32:18 -04:00
|
|
|
_failedVSyncCount = MAX(_failedVSyncCount - 1, 0);
|
2015-07-31 18:04:33 -04:00
|
|
|
}
|
2015-07-22 20:45:10 -04:00
|
|
|
|
2015-07-23 20:16:48 -04:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2015-07-31 17:47:10 -04:00
|
|
|
BOOL hasReturn = [self.view pushFrame:frame];
|
2015-07-26 15:13:46 -04:00
|
|
|
|
|
|
|
if(hasReturn)
|
2015-07-23 20:45:07 -04:00
|
|
|
dispatch_async(_serialDispatchQueue, ^{
|
|
|
|
_atari2600.get_crt()->return_frame();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2015-07-22 18:15:18 -04:00
|
|
|
|
2015-07-16 20:40:46 -04:00
|
|
|
- (void)runForNumberOfCycles:(int)cycles {
|
2015-07-23 20:07:35 -04:00
|
|
|
dispatch_async(_serialDispatchQueue, ^{
|
|
|
|
_atari2600.run_for_cycles(cycles);
|
|
|
|
});
|
2015-07-16 20:40:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setROM:(NSData *)rom {
|
2015-07-23 20:07:35 -04:00
|
|
|
dispatch_async(_serialDispatchQueue, ^{
|
|
|
|
_atari2600.set_rom(rom.length, (const uint8_t *)rom.bytes);
|
|
|
|
});
|
2015-07-16 20:40:46 -04:00
|
|
|
}
|
|
|
|
|
2015-07-22 18:15:18 -04:00
|
|
|
- (instancetype)init {
|
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
if (self) {
|
2015-07-23 20:07:35 -04:00
|
|
|
_crtDelegate.atari = self;
|
2015-07-22 18:15:18 -04:00
|
|
|
_atari2600.get_crt()->set_delegate(&_crtDelegate);
|
2015-07-23 20:16:48 -04:00
|
|
|
_serialDispatchQueue = dispatch_queue_create("Atari 2600 queue", DISPATCH_QUEUE_SERIAL);
|
2015-07-22 18:15:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-07-16 20:40:46 -04:00
|
|
|
@end
|