forked from notaz/picodrive
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPicodriveGameCore.m
More file actions
539 lines (435 loc) · 14.7 KB
/
Copy pathPicodriveGameCore.m
File metadata and controls
539 lines (435 loc) · 14.7 KB
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
/*
Copyright (c) 2020, 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 "PicodriveGameCore.h"
#import <OpenEmuBase/OERingBuffer.h>
#import "OESega32XSystemResponderClient.h"
#import <OpenGL/gl.h>
#include <sys/mman.h>
#include "pico/pico_int.h"
#include "pico/state.h"
#include "pico/patch.h"
#include "../common/input_pico.h"
static int16_t ALIGNED(4) soundBuffer[2 * 44100 / 50];
@interface PicodriveGameCore () <OESega32XSystemResponderClient>
{
uint16_t *_videoBuffer;
int _videoWidth;
NSURL *_romFile;
}
@end
static __weak PicodriveGameCore *_current;
@implementation PicodriveGameCore
- (id)init
{
if((self = [super init]))
{
_videoBuffer = (uint16_t *)malloc(320 * 240 * sizeof(uint16_t));
_videoWidth = 292; // initial viewport width
}
_current = self;
return self;
}
- (void)dealloc
{
free(_videoBuffer);
}
// MARK: - Execution
- (BOOL)loadFileAtPath:(NSString *)path error:(NSError **)error
{
_romFile = [NSURL fileURLWithPath:path];
// Picodrive defaults - not sure what all is truly needed for just 32X
PicoIn.opt = POPT_EN_STEREO|POPT_EN_FM|POPT_EN_PSG|POPT_EN_Z80
| POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_MCD_GFX
| POPT_EN_32X|POPT_EN_PWM
| POPT_ACC_SPRITES|POPT_DIS_32C_BORDER;
PicoIn.sndRate = 44100;
PicoIn.autoRgnOrder = 0x184; // US, EU, JP
PicoInit();
PicoDrawSetOutFormat(PDF_RGB555, 0);
PicoDrawSetOutBuf(_videoBuffer, 320 * 2);
PicoSetInputDevice(0, PICO_INPUT_PAD_6BTN);
PicoSetInputDevice(1, PICO_INPUT_PAD_6BTN);
enum media_type_e media_type;
media_type = PicoLoadMedia(path.fileSystemRepresentation, NULL, NULL, NULL);
switch (media_type) {
case PM_BAD_DETECT:
// Failed to detect ROM image type.
return NO;
case PM_ERROR:
// Load error
return NO;
default:
break;
}
PicoLoopPrepare();
PicoIn.writeSound = sound_write;
memset(soundBuffer, 0, sizeof(soundBuffer));
PicoIn.sndOut = soundBuffer;
PsndRerate(0);
// Set battery saves dir and load SRAM
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:@"sav2"]];
if ([saveFile checkResourceIsReachableAndReturnError:nil])
{
NSData *saveData = [NSData dataWithContentsOfURL:saveFile];
memcpy(Pico.sv.data, saveData.bytes, Pico.sv.size);
NSLog(@"[Picodrive] Loaded sram");
}
return YES;
}
- (void)executeFrame
{
//PicoPatchApply();
PicoFrame();
}
- (void)resetEmulation
{
PicoReset();
}
- (void)stopEmulation
{
// Only save if SRAM has been modified
int sram_size = Pico.sv.size;
uint8_t *sram_data = Pico.sv.data;
// sram save needs some special processing
// see if we have anything to save
for (; sram_size > 0; sram_size--)
if (sram_data[sram_size-1]) break;
if (sram_size && Pico.sv.changed)
{
NSError *error = nil;
NSString *extensionlessFilename = _romFile.lastPathComponent.stringByDeletingPathExtension;
NSURL *batterySavesDirectory = [NSURL fileURLWithPath:self.batterySavesDirectoryPath];
NSURL *saveFile = [batterySavesDirectory URLByAppendingPathComponent:[extensionlessFilename stringByAppendingPathExtension:@"sav2"]];
// copy SRAM data
NSData *saveData = [NSData dataWithBytes:sram_data length:sram_size];
[saveData writeToURL:saveFile options:NSDataWritingAtomic error:&error];
if (error)
NSLog(@"[Picodrive] Error writing sram file: %@", error);
else
NSLog(@"[Picodrive] Saved sram file: %@", saveFile);
}
PicoExit();
[super stopEmulation];
}
- (NSTimeInterval)frameInterval
{
return Pico.m.pal ? 50 : 60;
}
// MARK: - Video
- (const void *)getVideoBufferWithHint:(void *)hint
{
return (uint16_t *)_videoBuffer;
}
- (OEIntRect)screenRect
{
return OEIntRectMake(0, 8, _videoWidth, 224);
}
- (OEIntSize)bufferSize
{
return OEIntSizeMake(320, 240);
}
- (OEIntSize)aspectSize
{
// H32 mode (256px * 8:7 PAR)
// H40 mode (320px * 32:35 PAR)
return OEIntSizeMake(292, 224);
}
- (GLenum)pixelFormat
{
return GL_RGB;
}
- (GLenum)pixelType
{
return GL_UNSIGNED_SHORT_5_6_5;
}
// MARK: - Audio
- (double)audioSampleRate
{
return 44100;
}
- (NSUInteger)channelCount
{
return 2;
}
// MARK: - Save States
- (NSData *)serializeStateWithError:(NSError **)outError
{
size_t length = picodrive_serialize_size();
void *bytes = malloc(length);
if(picodrive_serialize(bytes, length))
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."
}];
}
return nil;
}
- (BOOL)deserializeState:(NSData *)state withError:(NSError **)outError
{
size_t serial_size = picodrive_serialize_size();
if(serial_size != state.length) {
if(outError) {
*outError = [NSError errorWithDomain:OEGameCoreErrorDomain code:OEGameCoreStateHasWrongSizeError userInfo:@{
NSLocalizedDescriptionKey : @"Save state has wrong file size.",
NSLocalizedRecoverySuggestionErrorKey : [NSString stringWithFormat:@"The save state does not have the right size, %ld expected, got: %ld.", serial_size, state.length]
}];
}
return NO;
}
if(picodrive_unserialize(state.bytes, state.length))
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
{
size_t serial_size = picodrive_serialize_size();
NSMutableData *stateData = [NSMutableData dataWithLength:serial_size];
if(!picodrive_serialize(stateData.mutableBytes, serial_size)) {
NSError *error = [NSError errorWithDomain:OEGameCoreErrorDomain code:OEGameCoreCouldNotSaveStateError userInfo:@{
NSLocalizedDescriptionKey : @"Save state data could not be written",
NSLocalizedRecoverySuggestionErrorKey : @"The emulator could not write the state data."
}];
block(NO, error);
return;
}
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
{
NSError *error = nil;
NSData *data = [NSData dataWithContentsOfFile:fileName options:NSDataReadingMappedIfSafe | NSDataReadingUncached error:&error];
if(data == nil) {
block(NO, error);
return;
}
int serial_size = 678514;
if(serial_size != data.length) {
NSError *error = [NSError errorWithDomain:OEGameCoreErrorDomain code:OEGameCoreStateHasWrongSizeError userInfo:@{
NSLocalizedDescriptionKey : @"Save state has wrong file size.",
NSLocalizedRecoverySuggestionErrorKey : [NSString stringWithFormat:@"The size of the file %@ does not have the right size, %d expected, got: %ld.", fileName, serial_size, data.length],
}];
block(NO, error);
return;
}
if(!picodrive_unserialize(data.bytes, serial_size)) {
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);
}
// MARK: - Input
const int Sega32XMap[] = {1 << GBTN_UP, 1 << GBTN_DOWN, 1 << GBTN_LEFT, 1 << GBTN_RIGHT, 1 << GBTN_A, 1 << GBTN_B, 1 << GBTN_C, 1 << GBTN_X, 1 << GBTN_Y, 1 << GBTN_Z, 1 << GBTN_START, 1 << GBTN_MODE};
- (oneway void)didPushSega32XButton:(OESega32XButton)button forPlayer:(NSUInteger)player
{
PicoIn.pad[player-1] |= Sega32XMap[button];
}
- (oneway void)didReleaseSega32XButton:(OESega32XButton)button forPlayer:(NSUInteger)player
{
PicoIn.pad[player-1] &= ~Sega32XMap[button];
}
// MARK: - Callbacks and Misc Helper Methods
static void sound_write(int len)
{
if (!len)
return;
[[_current ringBufferAtIndex:0] write:PicoIn.sndOut maxLength:len];
}
void emu_video_mode_change(int start_line, int line_count, int is_32cols)
{
GET_CURRENT_OR_RETURN();
current->_videoWidth = is_32cols ? 256 : 320;
}
void emu_32x_startup(void)
{
}
void lprintf(const char *fmt, ...)
{
// char buffer[256];
// va_list ap;
// va_start(ap, fmt);
// vsprintf(buffer, fmt, ap);
// NSLog(@"[Picodrive] %s", buffer);
// va_end(ap);
}
void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed)
{
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
void *req, *ret;
req = (void *)(uintptr_t)addr;
ret = mmap(req, size, PROT_READ | PROT_WRITE, flags, -1, 0);
if (ret == MAP_FAILED) {
NSLog(@"[Picodrive] mmap(%08lx, %zd) failed", addr, size);
return NULL;
}
if (addr != 0 && ret != (void *)(uintptr_t)addr) {
NSLog(@"[Picodrive] warning: wanted to map @%08lx, got %p", addr, ret);
if (is_fixed) {
munmap(ret, size);
return NULL;
}
}
return ret;
}
void plat_munmap(void *ptr, size_t size)
{
if (ptr != NULL)
munmap(ptr, size);
}
// Not using carthw.cfg so this is probably never called.
void *plat_mremap(void *ptr, size_t oldsize, size_t newsize)
{
#ifdef __linux__
void *ret = mremap(ptr, oldsize, newsize, 0);
if (ret == MAP_FAILED)
return NULL;
return ret;
#else
void *tmp, *ret;
size_t preserve_size;
preserve_size = oldsize;
if (preserve_size > newsize)
preserve_size = newsize;
tmp = malloc(preserve_size);
if (tmp == NULL)
return NULL;
memcpy(tmp, ptr, preserve_size);
munmap(ptr, oldsize);
ret = mmap(ptr, newsize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (ret == MAP_FAILED) {
free(tmp);
return NULL;
}
memcpy(ret, tmp, preserve_size);
free(tmp);
return ret;
#endif
}
size_t picodrive_serialize_size(void)
{
struct savestate_state state = { 0, };
int ret;
ret = PicoStateFP(&state, 1, NULL, state_skip, NULL, state_fseek);
if (ret != 0)
return 0;
return state.pos;
}
bool picodrive_serialize(void *data, size_t size)
{
struct savestate_state state = { 0, };
int ret;
state.save_buf = data;
state.size = size;
state.pos = 0;
ret = PicoStateFP(&state, 1, NULL, state_write, NULL, state_fseek);
return ret == 0;
}
bool picodrive_unserialize(const void *data, size_t size)
{
struct savestate_state state = { 0, };
int ret;
state.load_buf = data;
state.size = size;
state.pos = 0;
ret = PicoStateFP(&state, 0, state_read, NULL, state_eof, state_fseek);
return ret == 0;
}
struct savestate_state {
const char *load_buf;
char *save_buf;
size_t size;
size_t pos;
};
size_t state_read(void *p, size_t size, size_t nmemb, void *file)
{
struct savestate_state *state = file;
size_t bsize = size * nmemb;
if (state->pos + bsize > state->size) {
NSLog(@"[Picodrive] savestate read error: %lu/%zu", state->pos + bsize, state->size);
bsize = state->size - state->pos;
if ((int)bsize <= 0)
return 0;
}
memcpy(p, state->load_buf + state->pos, bsize);
state->pos += bsize;
return bsize;
}
size_t state_write(void *p, size_t size, size_t nmemb, void *file)
{
struct savestate_state *state = file;
size_t bsize = size * nmemb;
if (state->pos + bsize > state->size) {
NSLog(@"[Picodrive] savestate write error: %lu/%zu", state->pos + bsize, state->size);
bsize = state->size - state->pos;
if ((int)bsize <= 0)
return 0;
}
memcpy(state->save_buf + state->pos, p, bsize);
state->pos += bsize;
return bsize;
}
size_t state_skip(void *p, size_t size, size_t nmemb, void *file)
{
struct savestate_state *state = file;
size_t bsize = size * nmemb;
state->pos += bsize;
return bsize;
}
size_t state_eof(void *file)
{
struct savestate_state *state = file;
return state->pos >= state->size;
}
int state_fseek(void *file, long offset, int whence)
{
struct savestate_state *state = file;
switch (whence) {
case SEEK_SET:
state->pos = offset;
break;
case SEEK_CUR:
state->pos += offset;
break;
case SEEK_END:
state->pos = state->size + offset;
break;
}
return (int)state->pos;
}
@end