-
Notifications
You must be signed in to change notification settings - Fork 13
/
GBAEmulatorBridge.mm
586 lines (446 loc) · 12.8 KB
/
GBAEmulatorBridge.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
//
// GBAEmulatorBridge.m
// GBADeltaCore
//
// Created by Riley Testut on 6/3/16.
// Copyright © 2016 Riley Testut. All rights reserved.
//
#import "GBAEmulatorBridge.h"
#import "GBASoundDriver.h"
#import <CoreMotion/CoreMotion.h>
// VBA-M
#include "System.h"
#include "gba/Sound.h"
#include "gba/GBA.h"
#include "gba/Cheats.h"
#include "gba/RTC.h"
#include "Util.h"
#include <sys/time.h>
// DeltaCore
#import <GBADeltaCore/GBADeltaCore.h>
#import <DeltaCore/DeltaCore.h>
#import <DeltaCore/DeltaCore-Swift.h>
#if STATIC_LIBRARY
#import "GBADeltaCore-Swift.h"
#import "GBATypes.h"
#else
#import <GBADeltaCore/GBADeltaCore-Swift.h>
#endif
// Required vars, used by the emulator core
//
int systemRedShift = 19;
int systemGreenShift = 11;
int systemBlueShift = 3;
int systemColorDepth = 32;
int systemVerbose;
int systemSaveUpdateCounter = 0;
int systemFrameSkip;
uint32_t systemColorMap32[0x10000];
uint16_t systemColorMap16[0x10000];
uint16_t systemGbPalette[24];
int emulating;
int RGB_LOW_BITS_MASK;
@interface GBAEmulatorBridge ()
@property (nonatomic, copy, nullable, readwrite) NSURL *gameURL;
@property (assign, nonatomic, getter=isFrameReady) BOOL frameReady;
@property (nonatomic) uint32_t activatedInputs;
@property (strong, nonatomic, readonly) CMMotionManager *motionManager;
@end
@implementation GBAEmulatorBridge
@synthesize audioRenderer = _audioRenderer;
@synthesize videoRenderer = _videoRenderer;
@synthesize saveUpdateHandler = _saveUpdateHandler;
+ (instancetype)sharedBridge
{
static GBAEmulatorBridge *_emulatorBridge = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_emulatorBridge = [[self alloc] init];
});
return _emulatorBridge;
}
- (instancetype)init
{
self = [super init];
if (self)
{
_motionManager = [[CMMotionManager alloc] init];
}
return self;
}
#pragma mark - Emulation -
- (void)startWithGameURL:(NSURL *)URL
{
self.gameURL = URL;
NSData *data = [NSData dataWithContentsOfURL:URL];
if (!CPULoadRomData((const char *)data.bytes, (int)data.length))
{
return;
}
utilUpdateSystemColorMaps(NO);
utilGBAFindSave((int)data.length);
// Update per-game settings after utilGBAFindSave determines defaults.
[self updateGameSettings];
soundInit();
soundSetSampleRate(32768); // 44100 chirps
soundReset();
CPUInit(0, false);
GBASystem.emuReset();
emulating = 1;
}
- (void)stop
{
GBASystem.emuCleanUp();
soundShutdown();
emulating = 0;
[self deactivateGyroscope];
}
- (void)pause
{
emulating = 0;
[self deactivateGyroscope];
}
- (void)resume
{
emulating = 1;
}
- (void)runFrameAndProcessVideo:(BOOL)processVideo
{
self.frameReady = NO;
while (![self isFrameReady])
{
GBASystem.emuMain(GBASystem.emuCount);
}
}
#pragma mark - Settings -
- (void)updateGameSettings
{
NSString *gameID = [NSString stringWithFormat:@"%c%c%c%c", rom[0xac], rom[0xad], rom[0xae], rom[0xaf]];
NSLog(@"VBA-M: GameID in ROM is: %@", gameID);
// Set defaults
// Use underscores to prevent shadowing of global variables
BOOL _enableRTC = NO;
BOOL _enableMirroring = NO;
BOOL _useBIOS = NO;
int _cpuSaveType = 0;
int _flashSize = 0x10000;
// Read in vba-over.ini and break it into an array of strings
NSString *iniPath = [GBAEmulatorBridge.gbaResources pathForResource:@"vba-over" ofType:@"ini"];
NSString *iniString = [NSString stringWithContentsOfFile:iniPath encoding:NSUTF8StringEncoding error:NULL];
NSArray *settings = [iniString componentsSeparatedByString:@"\n"];
BOOL matchFound = NO;
NSMutableDictionary *overridesFound = [[NSMutableDictionary alloc] init];
NSString *temp;
// Check if vba-over.ini has per-game settings for our gameID
for (NSString *s in settings)
{
temp = nil;
if ([s hasPrefix:@"["])
{
NSScanner *scanner = [NSScanner scannerWithString:s];
[scanner scanString:@"[" intoString:nil];
[scanner scanUpToString:@"]" intoString:&temp];
if ([temp caseInsensitiveCompare:gameID] == NSOrderedSame)
{
matchFound = YES;
}
continue;
}
else if (matchFound && [s hasPrefix:@"saveType="])
{
NSScanner *scanner = [NSScanner scannerWithString:s];
[scanner scanString:@"saveType=" intoString:nil];
[scanner scanUpToString:@"\n" intoString:&temp];
_cpuSaveType = [temp intValue];
[overridesFound setObject:temp forKey:@"CPU saveType"];
continue;
}
else if (matchFound && [s hasPrefix:@"rtcEnabled="])
{
NSScanner *scanner = [NSScanner scannerWithString:s];
[scanner scanString:@"rtcEnabled=" intoString:nil];
[scanner scanUpToString:@"\n" intoString:&temp];
_enableRTC = [temp boolValue];
[overridesFound setObject:temp forKey:@"rtcEnabled"];
continue;
}
else if (matchFound && [s hasPrefix:@"flashSize="])
{
NSScanner *scanner = [NSScanner scannerWithString:s];
[scanner scanString:@"flashSize=" intoString:nil];
[scanner scanUpToString:@"\n" intoString:&temp];
_flashSize = [temp intValue];
[overridesFound setObject:temp forKey:@"flashSize"];
continue;
}
else if (matchFound && [s hasPrefix:@"mirroringEnabled="])
{
NSScanner *scanner = [NSScanner scannerWithString:s];
[scanner scanString:@"mirroringEnabled=" intoString:nil];
[scanner scanUpToString:@"\n" intoString:&temp];
_enableMirroring = [temp boolValue];
[overridesFound setObject:temp forKey:@"mirroringEnabled"];
continue;
}
else if (matchFound && [s hasPrefix:@"useBios="])
{
NSScanner *scanner = [NSScanner scannerWithString:s];
[scanner scanString:@"useBios=" intoString:nil];
[scanner scanUpToString:@"\n" intoString:&temp];
_useBIOS = [temp boolValue];
[overridesFound setObject:temp forKey:@"useBios"];
continue;
}
else if (matchFound)
break;
}
// Some hacked games use the RealTimeClock even when the game they're based off of doesn't (ex: Pokemon Liquid Crystal), so we always have it enabled.
rtcEnable(true);
if (!matchFound)
{
// Only update remaining settings if we found a match.
return;
}
NSLog(@"VBA: overrides found: %@", overridesFound);
// Apply settings
mirroringEnable = _enableMirroring;
doMirroring(mirroringEnable);
cpuSaveType = _cpuSaveType;
// Delta doesn't use BIOS files.
// useBios = _useBIOS;
if (_flashSize == 0x10000 || _flashSize == 0x20000)
{
flashSetSize(_flashSize);
}
}
#pragma mark - Inputs -
- (void)activateInput:(NSInteger)gameInput value:(double)value
{
self.activatedInputs |= (uint32_t)gameInput;
}
- (void)deactivateInput:(NSInteger)gameInput
{
self.activatedInputs &= ~((uint32_t)gameInput);
}
- (void)resetInputs
{
self.activatedInputs = 0;
}
#pragma mark - Game Saves -
- (void)saveGameSaveToURL:(NSURL *)URL
{
GBASystem.emuWriteBattery(URL.fileSystemRepresentation);
}
- (void)loadGameSaveFromURL:(NSURL *)URL
{
GBASystem.emuReadBattery(URL.fileSystemRepresentation);
}
#pragma mark - Save States -
- (void)saveSaveStateToURL:(NSURL *)URL
{
GBASystem.emuWriteState(URL.fileSystemRepresentation);
}
- (void)loadSaveStateFromURL:(NSURL *)URL
{
GBASystem.emuReadState(URL.fileSystemRepresentation);
}
#pragma mark - Cheats -
- (BOOL)addCheatCode:(NSString *)cheatCode type:(NSString *)type
{
NSArray<NSString *> *codes = [cheatCode componentsSeparatedByString:@"\n"];
for (NSString *code in codes)
{
NSMutableCharacterSet *legalCharactersSet = [NSMutableCharacterSet hexadecimalCharacterSet];
[legalCharactersSet addCharactersInString:@" "];
if ([code rangeOfCharacterFromSet:[legalCharactersSet invertedSet]].location != NSNotFound)
{
return NO;
}
if ([type isEqualToString:CheatTypeActionReplay] || [type isEqualToString:CheatTypeGameShark])
{
NSString *sanitizedCode = [code stringByReplacingOccurrencesOfString:@" " withString:@""];
if (sanitizedCode.length != 16)
{
return NO;
}
cheatsAddGSACode([sanitizedCode UTF8String], "code", true);
}
else if ([type isEqualToString:CheatTypeCodeBreaker])
{
if (code.length != 13)
{
return NO;
}
cheatsAddCBACode([code UTF8String], "code");
}
}
return YES;
}
- (void)resetCheats
{
cheatsDeleteAll(true);
}
- (void)updateCheats
{
}
#pragma mark - Gyroscope -
- (void)activateGyroscope
{
if ([self.motionManager isGyroActive] || ![self.motionManager isGyroAvailable])
{
return;
}
[self.motionManager startGyroUpdates];
[[NSNotificationCenter defaultCenter] postNotificationName:GBADidActivateGyroNotification object:self];
}
- (void)deactivateGyroscope
{
if (![self.motionManager isGyroActive])
{
return;
}
[self.motionManager stopGyroUpdates];
[[NSNotificationCenter defaultCenter] postNotificationName:GBADidDeactivateGyroNotification object:self];
}
#pragma mark - Getters/Setters -
- (NSTimeInterval)frameDuration
{
return (1.0 / 60.0);
}
@end
#pragma mark - VBA-M -
void systemMessage(int _iId, const char * _csFormat, ...)
{
NSLog(@"VBA-M: %s", _csFormat);
}
void systemDrawScreen()
{
// Get rid of the first line and the last row
for (int y = 0; y < 160; y++)
{
memcpy([GBAEmulatorBridge sharedBridge].videoRenderer.videoBuffer + y * 240 * 4, pix + (y + 1) * (240 + 1) * 4, 240 * 4);
}
[[GBAEmulatorBridge sharedBridge].videoRenderer processFrame];
[[GBAEmulatorBridge sharedBridge] setFrameReady:YES];
}
bool systemReadJoypads()
{
return true;
}
uint32_t systemReadJoypad(int joy)
{
return [GBAEmulatorBridge sharedBridge].activatedInputs;
}
void systemShowSpeed(int _iSpeed)
{
}
void system10Frames(int _iRate)
{
if (systemSaveUpdateCounter > 0)
{
systemSaveUpdateCounter--;
if (systemSaveUpdateCounter <= SYSTEM_SAVE_NOT_UPDATED)
{
if (GBAEmulatorBridge.sharedBridge.saveUpdateHandler)
{
GBAEmulatorBridge.sharedBridge.saveUpdateHandler();
}
systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
}
}
}
void systemFrame()
{
}
void systemSetTitle(const char * _csTitle)
{
}
void systemScreenCapture(int _iNum)
{
}
uint32_t systemGetClock()
{
timeval time;
gettimeofday(&time, NULL);
double milliseconds = (time.tv_sec * 1000.0) + (time.tv_usec / 1000.0);
return milliseconds;
}
SoundDriver *systemSoundInit()
{
soundShutdown();
auto driver = new GBASoundDriver;
return driver;
}
void systemUpdateMotionSensor()
{
}
uint8_t systemGetSensorDarkness()
{
return 0;
}
int systemGetSensorX()
{
return 0;
}
int systemGetSensorY()
{
return 0;
}
int systemGetSensorZ()
{
if (![GBAEmulatorBridge.sharedBridge.motionManager isGyroActive])
{
[GBAEmulatorBridge.sharedBridge activateGyroscope];
}
CMGyroData *gyroData = GBAEmulatorBridge.sharedBridge.motionManager.gyroData;
int sensorZ = -gyroData.rotationRate.z * 25;
return sensorZ;
}
void systemCartridgeRumble(bool)
{
}
void systemGbPrint(uint8_t * _puiData,
int _iLen,
int _iPages,
int _iFeed,
int _iPalette,
int _iContrast)
{
}
void systemScreenMessage(const char * _csMsg)
{
}
bool systemCanChangeSoundQuality()
{
return true;
}
bool systemPauseOnFrame()
{
return false;
}
void systemGbBorderOn()
{
}
void systemOnSoundShutdown()
{
}
void systemOnWriteDataToSoundBuffer(const uint16_t * finalWave, int length)
{
}
void debuggerMain()
{
}
void debuggerSignal(int, int)
{
}
void log(const char *defaultMsg, ...)
{
static FILE *out = NULL;
if(out == NULL) {
out = fopen("trace.log","w");
}
va_list valist;
va_start(valist, defaultMsg);
vfprintf(out, defaultMsg, valist);
va_end(valist);
}