-
Notifications
You must be signed in to change notification settings - Fork 8
/
SMSGameCore.m
486 lines (398 loc) · 14.1 KB
/
SMSGameCore.m
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
/*
Copyright (c) 2009, 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 "SMSGameCore.h"
#import <OpenEmuBase/OERingBuffer.h>
#import <OpenGL/gl.h>
#import "OESMSSystemResponderClient.h"
#import "OEGGSystemResponderClient.h"
#import "OESG1000SystemResponderClient.h"
#import "OEColecoVisionSystemResponderClient.h"
#include "sms.h"
#include "smsmem.h"
#include "sound.h"
#include "smsvdp.h"
#include "smsz80.h"
#include "rom.h"
#include "colecovision.h"
#include "colecomem.h"
#include "cheats.h"
#include "console.h"
#if MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_13
#include "fmemopen/fmemopen.h"
#include "fmemopen/open_memstream.h"
#endif
#define SAMPLERATE 44100
@interface SMSGameCore () <OESMSSystemResponderClient, OEGGSystemResponderClient, OESG1000SystemResponderClient, OEColecoVisionSystemResponderClient>
{
NSLock *bufLock;
BOOL paused;
NSURL *romFile;
NSMutableDictionary *cheatList;
}
@end
@implementation SMSGameCore
// Global variables because the callbacks need to access them...
static OERingBuffer *ringBuffer;
console_t *cur_console;
- (id)init
{
self = [super init];
if(self != nil)
{
bufLock = [[NSLock alloc] init];
cheatList = [[NSMutableDictionary alloc] init];
ringBuffer = [self ringBufferAtIndex:0];
}
return self;
}
- (void)dealloc
{
DLog(@"releasing/deallocating CrabEmu memory");
cur_console->shutdown();
}
# pragma mark - Execution
- (BOOL)loadFileAtPath:(NSString *)path error:(NSError **)error
{
romFile = [NSURL fileURLWithPath:path];
int console = rom_detect_console(path.fileSystemRepresentation);
DLog(@"Loaded File");
//TODO: add choice NTSC/PAL
if(console == CONSOLE_COLECOVISION)
{
NSString *biosPath = [[self biosDirectoryPath] stringByAppendingPathComponent:@"coleco.rom"];
coleco_init(VIDEO_NTSC);
coleco_mem_load_bios(biosPath.fileSystemRepresentation);
coleco_mem_load_rom(path.fileSystemRepresentation);
}
else
{
NSData *dataObj = [NSData dataWithContentsOfFile:[path stringByStandardizingPath]];
const void *data = [dataObj bytes];
uint16_t offset = 0;
int region = SMS_REGION_DOMESTIC;
// Detect SMS ROM header
if(memcmp(&data[0x1ff0], "TMR SEGA", 8) == 0)
offset = 0x1ff0;
else if (memcmp(&data[0x3ff0], "TMR SEGA", 8) == 0)
offset = 0x3ff0;
else if (memcmp(&data[0x7ff0], "TMR SEGA", 8) == 0)
offset = 0x7ff0;
if(offset)
{
// Set machine region
switch (((char *)data)[offset + 0x0f] >> 4)
{
case 3: // SMS Japan
region = SMS_REGION_DOMESTIC;
break;
case 4: // SMS Export
// Force system region to Japan if user locale is Japan and the cart is world/multi-region
region = [[self systemRegion] isEqualToString: @"Japan"] ? SMS_REGION_DOMESTIC : SMS_REGION_EXPORT;
break;
case 5: // GG Japan
case 6: // GG Export
case 7: // GG International
default:
region = SMS_REGION_DOMESTIC;
break;
}
}
else
// No header means Japan region
region = SMS_REGION_DOMESTIC;
sms_init(SMS_VIDEO_NTSC, region, 0); // 1 = VDP borders
sms_mem_load_rom(path.fileSystemRepresentation, console);
cur_console->frame(0);
}
if(cur_console->console_type != CONSOLE_COLECOVISION)
{
NSString *extensionlessFilename = [[romFile lastPathComponent] stringByDeletingPathExtension];
NSURL *batterySavesDirectory = [NSURL fileURLWithPath:[self batterySavesDirectoryPath]];
[[NSFileManager defaultManager] createDirectoryAtURL:batterySavesDirectory withIntermediateDirectories:YES attributes:nil error:nil];
NSURL *saveFile = [batterySavesDirectory URLByAppendingPathComponent:[extensionlessFilename stringByAppendingPathExtension:@"sav"]];
if([saveFile checkResourceIsReachableAndReturnError:nil] && sms_read_cartram_from_file(saveFile.path.fileSystemRepresentation) == 0)
NSLog(@"CrabEmu: Loaded sram");
}
return YES;
}
- (void)executeFrame
{
[bufLock lock];
cur_console->frame(0);
[bufLock unlock];
}
- (void)resetEmulation
{
cur_console->soft_reset();
}
- (void)stopEmulation
{
if(cur_console->console_type != CONSOLE_COLECOVISION)
{
NSString *extensionlessFilename = [[romFile lastPathComponent] stringByDeletingPathExtension];
NSURL *batterySavesDirectory = [NSURL fileURLWithPath:[self batterySavesDirectoryPath]];
NSURL *saveFile = [batterySavesDirectory URLByAppendingPathComponent:[extensionlessFilename stringByAppendingPathExtension:@"sav"]];
cur_console->save_sram(saveFile.path.fileSystemRepresentation);
}
[super stopEmulation];
}
- (NSTimeInterval)frameInterval
{
return 60;
}
# pragma mark - Video
- (OEIntSize)bufferSize
{
uint32_t f_x, f_y;
cur_console->frame_size(&f_x, &f_y);
return OEIntSizeMake(f_x, f_y);
}
- (OEIntRect)screenRect
{
uint32_t a_x, a_y, a_w, a_h;
cur_console->active_size(&a_x, &a_y, &a_w, &a_h);
return OEIntRectMake(a_x, a_y, a_w, a_h);
}
- (OEIntSize)aspectSize
{
return OEIntSizeMake(cur_console->console_type == CONSOLE_GG ? 160 : 256 * (8.0/7.0), cur_console->console_type == CONSOLE_GG ? 144 : 192);
}
- (const void *)getVideoBufferWithHint:(void *)hint
{
if (!hint) {
return cur_console->framebuffer();
}
return smsvdp.framebuffer = (uint32*)hint;
}
- (GLenum)pixelFormat
{
return GL_BGRA;
}
- (GLenum)pixelType
{
return GL_UNSIGNED_INT_8_8_8_8_REV;
}
# pragma mark - Audio
- (double)audioSampleRate
{
return SAMPLERATE;
}
- (NSUInteger)channelCount
{
return 2;
}
# pragma mark - Save States
- (void)saveStateToFileAtPath:(NSString *)fileName completionHandler:(void (^)(BOOL, NSError *))block
{
block(cur_console->save_state([fileName fileSystemRepresentation]) == 0, nil);
}
- (void)loadStateFromFileAtPath:(NSString *)fileName completionHandler:(void (^)(BOOL, NSError *))block
{
if([[self systemIdentifier] isEqualToString:@"openemu.system.sg1000"])
{
cur_console->load_state([fileName fileSystemRepresentation]);
block(YES, nil);
}
else
block(cur_console->load_state([fileName fileSystemRepresentation]) == 0, nil);
}
- (NSData *)serializeStateWithError:(NSError **)outError
{
void *bytes;
size_t length;
FILE *fp = open_memstream((char **)&bytes, &length);
int status;
if(cur_console->console_type == CONSOLE_COLECOVISION)
status = coleco_write_state(fp);
else
status = sms_write_state(fp);
if(status == 0) {
fclose(fp);
return [NSData dataWithBytesNoCopy:bytes length:length];
}
if(outError) {
*outError = [NSError errorWithDomain:OEGameCoreErrorDomain code:OEGameCoreCouldNotSaveStateError userInfo:@{
NSLocalizedDescriptionKey : @"Save state data could not be written",
NSLocalizedRecoverySuggestionErrorKey : @"The emulator could not write the state data."
}];
}
fclose(fp);
free(bytes);
return nil;
}
- (BOOL)deserializeState:(NSData *)state withError:(NSError **)outError
{
const void *bytes = [state bytes];
size_t length = [state length];
FILE *fp = fmemopen((void *)bytes, length, "rb");
int status;
if(cur_console->console_type == CONSOLE_COLECOVISION)
status = coleco_read_state(fp);
else
status = sms_read_state(fp);
fclose(fp);
if(status == 0)
return YES;
if(outError) {
*outError = [NSError errorWithDomain:OEGameCoreErrorDomain code:OEGameCoreCouldNotLoadStateError userInfo:@{
NSLocalizedDescriptionKey : @"The save state data could not be read",
NSLocalizedRecoverySuggestionErrorKey : @"Could not load data from the save state"
}];
}
return NO;
}
/*
CrabEmu callbacks
*/
void sound_update_buffer(signed short *buf, int length)
{
//NSLog(@"%s %p", __FUNCTION__, ringBuffer);
[ringBuffer write:buf maxLength:length];
}
int sound_init(int channels, int region)
{
return 0;
}
void sound_shutdown(void)
{
}
void sound_reset_buffer(void)
{
}
void gui_set_viewport(int w, int h)
{
//NSLog(@"viewport, width: %d, height: %d", w, h);
}
void gui_set_aspect(float x, float y)
{
//NSLog(@"set_aspect, x: %f, y: %f", x, y);
}
void gui_set_title(const char *str)
{
//NSLog(@"set_title%s", str);
}
void gui_set_console(console_t *c)
{
cur_console = c;
}
# pragma mark - Input
const int MasterSystemMap[] = {SMS_UP, SMS_DOWN, SMS_LEFT, SMS_RIGHT, SMS_BUTTON_1, SMS_BUTTON_2, GAMEGEAR_START};
const int ColecoVisionMap[] = {COLECOVISION_UP, COLECOVISION_DOWN, COLECOVISION_LEFT, COLECOVISION_RIGHT, COLECOVISION_L_ACTION, COLECOVISION_R_ACTION, COLECOVISION_1, COLECOVISION_2, COLECOVISION_3, COLECOVISION_4, COLECOVISION_5, COLECOVISION_6, COLECOVISION_7, COLECOVISION_8, COLECOVISION_9, COLECOVISION_0, COLECOVISION_STAR, COLECOVISION_POUND};
- (oneway void)didPushGGButton:(OEGGButton)button;
{
sms_button_pressed(1, MasterSystemMap[button]);
}
- (oneway void)didReleaseGGButton:(OEGGButton)button;
{
sms_button_released(1, MasterSystemMap[button]);
}
- (oneway void)didPushSMSButton:(OESMSButton)button forPlayer:(NSUInteger)player;
{
sms_button_pressed((int)player, MasterSystemMap[button]);
}
- (oneway void)didReleaseSMSButton:(OESMSButton)button forPlayer:(NSUInteger)player;
{
sms_button_released((int)player, MasterSystemMap[button]);
}
- (oneway void)didPushSMSStartButton;
{
sms_button_pressed(1, GAMEGEAR_START);
}
- (oneway void)didReleaseSMSStartButton;
{
sms_button_released(1, GAMEGEAR_START);
}
- (oneway void)didPushSMSResetButton;
{
sms_button_pressed(1, SMS_CONSOLE_RESET);
}
- (oneway void)didReleaseSMSResetButton;
{
sms_button_released(1, SMS_CONSOLE_RESET);
}
- (oneway void)didPushSG1000Button:(OESG1000Button)button forPlayer:(NSUInteger)player
{
//console pause, sms_z80_nmi()
sms_button_pressed((int)player, MasterSystemMap[button]);
}
- (oneway void)didReleaseSG1000Button:(OESG1000Button)button forPlayer:(NSUInteger)player
{
sms_button_released((int)player, MasterSystemMap[button]);
}
- (oneway void)didPushColecoVisionButton:(OEColecoVisionButton)button forPlayer:(NSUInteger)player;
{
coleco_button_pressed((int)player, ColecoVisionMap[button]);
}
- (oneway void)didReleaseColecoVisionButton:(OEColecoVisionButton)button forPlayer:(NSUInteger)player;
{
coleco_button_released((int)player, ColecoVisionMap[button]);
}
#pragma mark - Cheats
- (void)setCheat:(NSString *)code setType:(NSString *)type setEnabled:(BOOL)enabled
{
// Sanitize
code = [code stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
// Remove any spaces
code = [code stringByReplacingOccurrencesOfString:@" " withString:@""];
// Remove address-value separator
code = [code stringByReplacingOccurrencesOfString:@"-" withString:@""];
if (enabled)
[cheatList setValue:@YES forKey:code];
else
[cheatList removeObjectForKey:code];
sms_cheat_reset();
NSArray *multipleCodes = [[NSArray alloc] init];
// Apply enabled cheats found in dictionary
for (id key in cheatList)
{
if ([[cheatList valueForKey:key] isEqual:@YES])
{
// Handle multi-line cheats
multipleCodes = [key componentsSeparatedByString:@"+"];
for (NSString *singleCode in multipleCodes)
{
if ([singleCode length] == 8)
{
// Action Replay GG/SMS format: XXXX-YYYY
NSString *address = [singleCode substringWithRange:NSMakeRange(0, 4)];
NSString *value = [singleCode substringWithRange:NSMakeRange(4, 4)];
// Convert AR hex to int
uint32_t outAddress, outValue;
NSScanner *scanAddress = [NSScanner scannerWithString:address];
NSScanner *scanValue = [NSScanner scannerWithString:value];
[scanAddress scanHexInt:&outAddress];
[scanValue scanHexInt:&outValue];
sms_cheat_t *arCode = (sms_cheat_t *)malloc(sizeof(sms_cheat_t));
memset(arCode, 0, sizeof(sms_cheat_t));
arCode->ar_code = (outAddress << 16) | outValue;
strcpy(arCode->desc, [singleCode UTF8String]);
arCode->enabled = 1;
sms_cheat_add(arCode);
sms_cheat_enable();
}
}
}
}
}
@end