6060
6161@interface FreeDOGameCore () <OE3DOSystemResponderClient>
6262{
63+ NSString *romName;
64+
6365 unsigned char *biosRom1Copy;
6466 unsigned char *biosRom2Copy;
65- void *nvramCopy;
6667 VDLFrame *frame;
6768
6869 NSFileHandle *isoStream;
@@ -99,10 +100,8 @@ @implementation FreeDOGameCore
99100 break ;
100101 }
101102 case EXT_READ_NVRAM:
102- memcpy (data, current->nvramCopy , NVRAM_SIZE);
103103 break ;
104104 case EXT_WRITE_NVRAM:
105- // ?
106105 break ;
107106 case EXT_SWAPFRAME:
108107 {
@@ -177,6 +176,54 @@ @implementation FreeDOGameCore
177176 return (void *)0 ;
178177}
179178
179+ static void loadSaveFile (const char * path)
180+ {
181+ FILE *file;
182+
183+ file = fopen (path, " rb" );
184+ if ( !file )
185+ {
186+ return ;
187+ }
188+
189+ size_t size = NVRAM_SIZE;
190+ void *data = _freedo_Interface (FDP_GETP_NVRAM, (void *)0 );
191+
192+ if (size == 0 || !data)
193+ {
194+ fclose (file);
195+ return ;
196+ }
197+
198+ int rc = fread (data, sizeof (uint8_t ), size, file);
199+ if ( rc != size )
200+ {
201+ NSLog (@" Couldn't load save file." );
202+ }
203+
204+ NSLog (@" Loaded save file: %s " , path);
205+
206+ fclose (file);
207+ }
208+
209+ static void writeSaveFile (const char * path)
210+ {
211+ size_t size = NVRAM_SIZE;
212+ void *data = _freedo_Interface (FDP_GETP_NVRAM, (void *)0 );
213+
214+ if (data != NULL && size > 0 )
215+ {
216+ FILE *file = fopen (path, " wb" );
217+ if (file != NULL )
218+ {
219+ NSLog (@" Saving NVRAM %s . Size: %d bytes." , path, (int )size);
220+ if (fwrite (data, sizeof (uint8_t ), size, file) != size)
221+ NSLog (@" Did not save file properly." );
222+ fclose (file);
223+ }
224+ }
225+ }
226+
180227- (id )init
181228{
182229 if ((self = [super init ]))
@@ -195,6 +242,8 @@ - (void)dealloc
195242#pragma mark Execution
196243- (BOOL )loadFileAtPath : (NSString *)path error : (NSError **)error
197244{
245+ romName = [path copy ];
246+
198247 NSString *isoPath;
199248 NSError *errorCue;
200249
@@ -232,6 +281,32 @@ - (BOOL)loadFileAtPath:(NSString *)path error:(NSError **)error
232281 VolumeHeader *header = (VolumeHeader*)sectorZero;
233282 sectorCount = (int )reverseBytes (header->blockCount );
234283 NSLog (@" Sector count is %d " , sectorCount);
284+
285+ // init libfreedo
286+ [self loadBIOSes ];
287+ [self initVideo ];
288+
289+ currentSector = 0 ;
290+ sampleCurrent = 0 ;
291+ memset (sampleBuffer, 0 , sizeof (int32_t ) * TEMP_BUFFER_SIZE);
292+
293+ _freedo_Interface (FDP_INIT, (void *)*fdcCallback);
294+
295+ // init NVRAM
296+ memcpy (_freedo_Interface (FDP_GETP_NVRAM, (void *)0 ), nvramhead, sizeof (nvramhead));
297+
298+ // load NVRAM save file
299+ NSString *extensionlessFilename = [[path lastPathComponent ] stringByDeletingPathExtension ];
300+ NSString *batterySavesDirectory = [current batterySavesDirectoryPath ];
301+
302+ if ([batterySavesDirectory length ] != 0 )
303+ {
304+ [[NSFileManager defaultManager ] createDirectoryAtPath: batterySavesDirectory withIntermediateDirectories: YES attributes: nil error: NULL ];
305+
306+ NSString *filePath = [batterySavesDirectory stringByAppendingPathComponent: [extensionlessFilename stringByAppendingPathExtension: @" sav" ]];
307+
308+ loadSaveFile ([filePath UTF8String ]);
309+ }
235310
236311 return YES ;
237312}
@@ -246,26 +321,26 @@ - (void)executeFrameSkippingFrame:(BOOL)skip
246321 _freedo_Interface (FDP_DO_EXECFRAME, frame); // FDP_DO_EXECFRAME_MT ?
247322}
248323
249- - (void )setupEmulation
250- {
251- [self loadBIOSes ];
252- [self initNVRAM ];
253- [self initVideo ];
254-
255- currentSector = 0 ;
256- sampleCurrent = 0 ;
257- memset (sampleBuffer, 0 , sizeof (int32_t ) * TEMP_BUFFER_SIZE);
258-
259- _freedo_Interface (FDP_INIT, (void *)*fdcCallback);
260- }
261-
262324- (void )resetEmulation
263325{
264326 // looks like libfreedo cannot do this :|
265327}
266328
267329- (void )stopEmulation
268330{
331+ // save NVRAM file
332+ NSString *extensionlessFilename = [[romName lastPathComponent ] stringByDeletingPathExtension ];
333+ NSString *batterySavesDirectory = [self batterySavesDirectoryPath ];
334+
335+ if ([batterySavesDirectory length ] != 0 )
336+ {
337+ [[NSFileManager defaultManager ] createDirectoryAtPath: batterySavesDirectory withIntermediateDirectories: YES attributes: nil error: NULL ];
338+
339+ NSString *filePath = [batterySavesDirectory stringByAppendingPathComponent: [extensionlessFilename stringByAppendingPathExtension: @" sav" ]];
340+
341+ writeSaveFile ([filePath UTF8String ]);
342+ }
343+
269344 _freedo_Interface (FDP_DESTROY, (void *)0 );
270345 [super stopEmulation ];
271346}
@@ -499,11 +574,6 @@ char CalculateDeviceHighByte(int deviceNumber)
499574
500575#pragma mark - FreeDoInterface
501576// TODO: investigate these
502- // -(void*)fdcGetPointerNVRAM
503- // {
504- // return [self _freedoActionWithInterfaceFunction:FDP_GETP_NVRAM datum:(void*)0];
505- // }
506- //
507577// -(void*)fdcGetPointerRAM
508578// {
509579// return [self _freedoActionWithInterfaceFunction:FDP_GETP_RAMS datum:(void*)0];
@@ -551,13 +621,6 @@ - (void)initVideo
551621 fver2=fver1=0 ;
552622}
553623
554- - (void )initNVRAM
555- {
556- nvramCopy = malloc (65536 /2 );
557- memset (nvramCopy, 0 , 65536 /2 );
558- memcpy (nvramCopy, nvramhead, sizeof (nvramhead));
559- }
560-
561624- (void )loadBIOSes
562625{
563626 NSString *rom1Path = [[self biosDirectoryPath ] stringByAppendingPathComponent: @" panafz10.bin" ];
0 commit comments