This repository has been archived by the owner on Dec 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathHiganGameCore.mm
402 lines (316 loc) · 12.4 KB
/
HiganGameCore.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
/*
Copyright (c) 2013, OpenEmu Team
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the OpenEmu Team nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY OpenEmu Team ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL OpenEmu Team BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import <OpenEmuBase/OERingBuffer.h>
#import <OpenGL/gl.h>
#import "HiganGameCore.h"
#import "HiganInterface.h"
#import "HiganImporter.h"
#import "OESNESSystemResponderClient.h"
#import "OEGBASystemResponderClient.h"
#import "OEGBSystemResponderClient.h"
#import "OENESSystemResponderClient.h"
@interface HiganGameCore () <OESNESSystemResponderClient, OEGBASystemResponderClient, OEGBSystemResponderClient, OENESSystemResponderClient>
{
Interface *_interface;
}
@end
@implementation HiganGameCore
- (void)dealloc
{
delete _interface;
}
#pragma mark - Execution
- (BOOL)loadFileAtPath:(NSString *)path error:(NSError **)error
{
_interface = new Interface;
_interface->bundlePath = [[[[self owner] bundle] resourcePath] fileSystemRepresentation];
_interface->supportPath = [[self supportDirectoryPath] fileSystemRepresentation];
vector<uint8_t> buffer = file::read(path.fileSystemRepresentation);
string romName = path.lastPathComponent.fileSystemRepresentation;
string biosPath = [[self biosDirectoryPath] fileSystemRepresentation];
if([[self systemIdentifier] isEqualToString:@"openemu.system.snes"])
{
_interface->loadMedia(romName, "Super Famicom", OESuperFamicomSystem, SuperFamicom::ID::SuperFamicom);
importSuperFamicom(_interface->path(SuperFamicom::ID::SuperFamicom), biosPath, buffer);
string checkChip = checkMissingChipDump();
if(!checkChip.empty())
{
NSString *missingFileName = [NSString stringWithUTF8String:checkChip];
NSError *outErr = [NSError errorWithDomain:OEGameCoreErrorDomain code:OEGameCoreCouldNotLoadROMError userInfo:@{
NSLocalizedDescriptionKey : @"Required chip dump file missing.",
NSLocalizedRecoverySuggestionErrorKey : [NSString stringWithFormat:@"To run this game you need: \"%@\"\n\nObtain this file, drag and drop onto the game library window and try again.", missingFileName]
}];
*error = outErr;
return NO;
}
}
else if([[self systemIdentifier] isEqualToString:@"openemu.system.gba"])
{
string gbaBiosPath = {biosPath, "/bios.rom"};
if(!file::exists(gbaBiosPath))
{
NSError *outErr = [NSError errorWithDomain:OEGameCoreErrorDomain code:OEGameCoreCouldNotLoadROMError userInfo:@{
NSLocalizedDescriptionKey : @"GBA BIOS file is missing.",
NSLocalizedRecoverySuggestionErrorKey : @"To run this game you need: \"bios.rom\"\n\nObtain this file, drag and drop onto the game library window and try again."
}];
*error = outErr;
return NO;
}
_interface->loadMedia(romName, "Game Boy Advance", OEGameBoyAdvanceSystem, GameBoyAdvance::ID::GameBoyAdvance);
importGameBoyAdvance(_interface->path(GameBoyAdvance::ID::GameBoyAdvance), buffer);
file::copy(gbaBiosPath, {_interface->path(0), "bios.rom"});
}
else if([[self systemIdentifier] isEqualToString:@"openemu.system.gb"])
{
string systemName = "Game Boy";
unsigned mediaID = GameBoy::ID::GameBoy;
if(checkGameBoyColorSupport(buffer))
{
systemName = "Game Boy Color";
mediaID = GameBoy::ID::GameBoyColor;
}
_interface->loadMedia(romName, systemName, OEGameBoySystem, mediaID);
importGameBoy(_interface->path(mediaID), buffer);
/* Super Game Boy support is broken in v094
string sgbRomPath = {biosPath, "/Super Game Boy (World).sfc"};
string sgbBootRomPath = {biosPath, "/sgb.rom"};
bool sgbAvailable = file::exists(sgbRomPath) && file::exists(sgbBootRomPath);
// Check for Super Game Boy header
if(sgbAvailable && (buffer[0x0146] & 0x03) == 0x03)
{
buffer = file::read(sgbRomPath);
_interface->loadMedia("Super Game Boy (World).sfc", "Super Famicom", OESuperFamicomSystem, SuperFamicom::ID::SuperFamicom);
importSuperFamicom(_interface->path(SuperFamicom::ID::SuperFamicom), biosPath, buffer);
}
*/
}
else if([[self systemIdentifier] isEqualToString:@"openemu.system.nes"])
{
_interface->loadMedia(romName, "Famicom", OEFamicomSystem, Famicom::ID::Famicom);
importFamicom(_interface->path(Famicom::ID::Famicom), buffer);
}
NSLog(@"Higan: Loading game");
_interface->load();
return YES;
}
- (void)executeFrame
{
_interface->run();
signed samples[2];
while(_interface->resampler.pending()) {
_interface->resampler.read(samples);
[[self ringBufferAtIndex:0] write:&samples[0] maxLength:2];
[[self ringBufferAtIndex:0] write:&samples[1] maxLength:2];
}
}
- (void)resetEmulation
{
_interface->active->reset();
}
- (void)stopEmulation
{
_interface->active->save();
cleanupLibrary(_interface->gamePaths);
[super stopEmulation];
}
#pragma mark - Video
- (OEIntSize)aspectSize
{
switch(_interface->activeSystem)
{
case OEGameBoyAdvanceSystem:
return OEIntSizeMake(3, 2);
case OEGameBoySystem:
return OEIntSizeMake(10, 9);
default:
return OEIntSizeMake(256 * (8.0/7.0), _interface->height);
}
}
- (OEIntRect)screenRect
{
return OEIntRectMake(0, 0, _interface->width, _interface->height);
}
- (OEIntSize)bufferSize
{
return OEIntSizeMake(512, 480);
}
- (const void *)getVideoBufferWithHint:(void *)hint
{
if (!hint) {
return _interface->videoBuffer;
}
return _interface->videoBuffer = (uint32_t*)hint;
}
- (GLenum)pixelFormat
{
return GL_BGRA;
}
- (GLenum)pixelType
{
return GL_UNSIGNED_INT_8_8_8_8_REV;
}
- (NSTimeInterval)frameInterval
{
return _interface->active->videoFrequency();
}
#pragma mark - Audio
- (NSUInteger)channelCount
{
return 2;
}
- (double)audioSampleRate
{
return 44100;
}
#pragma mark - Save State
- (NSData *)serializeStateWithError:(NSError **)outError
{
serializer state = _interface->active->serialize();
return [NSData dataWithBytes:state.data() length:state.size()];
}
- (BOOL)deserializeState:(NSData *)state withError:(NSError **)outError
{
const uint8_t *stateBytes = (const uint8_t *)[state bytes];
unsigned int stateLength = [state length];
serializer stateToLoad(stateBytes, stateLength);
if(_interface->active->unserialize(stateToLoad))
return YES;
if (outError) {
*outError = [NSError errorWithDomain:OEGameCoreErrorDomain code:OEGameCoreCouldNotLoadStateError userInfo:@{
NSLocalizedDescriptionKey : @"The save state data could not be read"
}];
}
return NO;
}
- (void)saveStateToFileAtPath:(NSString *)fileName completionHandler:(void (^)(BOOL, NSError *))block
{
serializer state = _interface->active->serialize();
NSData *stateData = [NSData dataWithBytes:state.data() length:state.size()];
__autoreleasing NSError *error = nil;
BOOL success = [stateData writeToFile:fileName options:NSDataWritingAtomic error:&error];
block(success, success ? nil : error);
}
- (void)loadStateFromFileAtPath:(NSString *)fileName completionHandler:(void (^)(BOOL, NSError *))block
{
__autoreleasing NSError *error = nil;
NSData *state = [NSData dataWithContentsOfFile:fileName options:NSDataReadingMappedIfSafe | NSDataReadingUncached error:&error];
if(state == nil)
{
block(NO, error);
return;
}
serializer stateToLoad((const uint8_t *)[state bytes], [state length]);
if(!_interface->active->unserialize(stateToLoad))
{
NSError *error = [NSError errorWithDomain:OEGameCoreErrorDomain code:OEGameCoreCouldNotLoadStateError userInfo:@{
NSLocalizedDescriptionKey : @"The save state data could not be read",
NSLocalizedRecoverySuggestionErrorKey : [NSString stringWithFormat:@"Could not read the file state in %@.", fileName]
}];
block(NO, error);
return;
}
block(YES, nil);
}
#pragma mark - Input
static const int inputMapSuperFamicom [] = {4, 5, 6, 7, 8, 0, 9, 1,10, 11, 3, 2};
- (oneway void)didPushSNESButton:(OESNESButton)button forPlayer:(NSUInteger)player
{
_interface->inputState[player - 1][inputMapSuperFamicom[button]] = 1;
}
- (oneway void)didReleaseSNESButton:(OESNESButton)button forPlayer:(NSUInteger)player
{
_interface->inputState[player - 1][inputMapSuperFamicom[button]] = 0;
}
static const int inputMapGameBoyAdvance [] = {6, 7, 5, 4, 0, 1, 9, 8, 3, 2};
- (oneway void)didPushGBAButton:(OEGBAButton)button forPlayer:(NSUInteger)player
{
_interface->inputState[player - 1][inputMapGameBoyAdvance[button]] = 1;
}
- (oneway void)didReleaseGBAButton:(OEGBAButton)button forPlayer:(NSUInteger)player
{
_interface->inputState[player - 1][inputMapGameBoyAdvance[button]] = 0;
}
static const int inputMapGameBoy [] = {0, 1, 2, 3, 5, 4, 7, 6};
static const int inputMapSuperGameBoy [] = {4, 5, 6, 7, 8, 0, 3, 2};
- (oneway void)didPushGBButton:(OEGBButton)button
{
if(_interface->activeSystem == OEGameBoySystem)
_interface->inputState[0][inputMapGameBoy[button]] = 1;
else
_interface->inputState[0][inputMapSuperGameBoy[button]] = 1;
}
- (oneway void)didReleaseGBButton:(OEGBButton)button
{
if(_interface->activeSystem == OEGameBoySystem)
_interface->inputState[0][inputMapGameBoy[button]] = 0;
else
_interface->inputState[0][inputMapSuperGameBoy[button]] = 0;
}
static const int inputMapFamicom [] = {4, 5, 6, 7, 0, 1, 3, 2};
- (oneway void)didPushNESButton:(OENESButton)button forPlayer:(NSUInteger)player
{
_interface->inputState[player - 1][inputMapFamicom[button]] = 1;
}
- (oneway void)didReleaseNESButton:(OENESButton)button forPlayer:(NSUInteger)player
{
_interface->inputState[player - 1][inputMapFamicom[button]] = 0;
}
- (oneway void)mouseMovedAtPoint:(OEIntPoint)aPoint
{
}
- (oneway void)leftMouseDownAtPoint:(OEIntPoint)aPoint
{
}
- (oneway void)leftMouseUp
{
}
- (oneway void)rightMouseDownAtPoint:(OEIntPoint)aPoint
{
}
- (oneway void)rightMouseUp
{
}
- (oneway void)didTriggerGunAtPoint:(OEIntPoint)point
{
}
- (oneway void)didReleaseTrigger
{
}
#pragma mark - Cheats
NSMutableDictionary *cheatList = [[NSMutableDictionary alloc] init];
- (void)setCheat:(NSString *)code setType:(NSString *)type setEnabled:(BOOL)enabled
{
lstring list;
if (enabled)
[cheatList setValue:@YES forKey:code];
else
[cheatList removeObjectForKey:code];
for (id key in cheatList)
{
if ([[cheatList valueForKey:key] isEqual:@YES])
list.append([key UTF8String]);
}
_interface->active->cheatSet(list);
}
@end