~ubuntu-branches/ubuntu/utopic/fceux/utopic-proposed

« back to all changes in this revision

Viewing changes to src/fceu.cpp

  • Committer: Package Import Robot
  • Author(s): Joe Nahmias
  • Date: 2014-03-02 19:22:04 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20140302192204-9f0aehi5stfnhn7d
Tags: 2.2.2+dfsg0-1
* Imported Upstream version 2.2.2
  + remove patches merged upstream; refresh remaining
  + remove windows compiled help files and non-free Visual C files
* Use C++11 standard static assertion functionality
* fix upstream installation of support files
* New patch 0004-ignore-missing-windows-help-CHM-file.patch
* update d/copyright for new, renamed, deleted files
* d/control: bump std-ver to 3.9.5, no changes needed

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#include <string>
22
 
#include <string.h>
23
 
#include <stdio.h>
24
 
#include <stdlib.h>
25
 
#include <stdarg.h>
26
 
#include <time.h>
27
21
#include "types.h"
28
22
#include "x6502.h"
29
23
#include "fceu.h"
49
43
#include "file.h"
50
44
#include "vsuni.h"
51
45
#include "ines.h"
52
 
 
53
46
#ifdef WIN32
54
47
#include "drivers/win/pref.h"
 
48
#include "utils/xstring.h"
55
49
 
56
50
extern void CDLoggerROMClosed();
57
51
extern void CDLoggerROMChanged();
65
59
extern void RefreshThrottleFPS();
66
60
#endif
67
61
 
68
 
#include <fstream>
69
 
#include <sstream>
70
 
 
71
62
#ifdef _S9XLUA_H
72
63
#include "fceulua.h"
73
64
#endif
86
77
#include "drivers/sdl/sdl.h"
87
78
#endif
88
79
 
 
80
#include <fstream>
 
81
#include <sstream>
 
82
#include <string>
 
83
 
 
84
#include <cstring>
 
85
#include <cstdio>
 
86
#include <cstdlib>
 
87
#include <cstdarg>
 
88
#include <ctime>
 
89
 
89
90
using namespace std;
90
91
 
91
92
int AFon = 1, AFoff = 1, AutoFireOffset = 0; //For keeping track of autofire settings
98
99
char romNameWhenClosingEmulator[2048] = {0};
99
100
 
100
101
FCEUGI::FCEUGI()
101
 
        : filename(0)
102
 
        , archiveFilename(0) {
 
102
        : filename(0),
 
103
          archiveFilename(0) {
103
104
        //printf("%08x",opsize); // WTF?!
104
105
}
105
106
 
106
107
FCEUGI::~FCEUGI() {
107
 
        if (filename) delete filename;
108
 
        if (archiveFilename) delete archiveFilename;
 
108
        if (filename) {
 
109
        free(filename);
 
110
        filename = NULL;
 
111
    }
 
112
        if (archiveFilename) {
 
113
        delete archiveFilename;
 
114
        archiveFilename = NULL;
 
115
    }
109
116
}
110
117
 
111
118
bool CheckFileExists(const char* filename) {
141
148
{
142
149
        if (GameInfo)
143
150
        {
144
 
                if (AutoResumePlay && (GameInfo->type != GIT_NSF))
 
151
                if (AutoResumePlay)
145
152
                {
146
153
                        // save "-resume" savestate
147
 
                        FCEUSS_Save(FCEU_MakeFName(FCEUMKF_RESUMESTATE, 0, 0).c_str());
 
154
                        FCEUSS_Save(FCEU_MakeFName(FCEUMKF_RESUMESTATE, 0, 0).c_str(), false);
148
155
                }
149
156
 
150
157
#ifdef WIN32
151
158
                extern char LoadedRomFName[2048];
152
 
                if (storePreferences(LoadedRomFName))
153
 
                {
 
159
                if (storePreferences(mass_replace(LoadedRomFName, "|", ".").c_str()))
154
160
                        FCEUD_PrintError("Couldn't store debugging data");
155
 
                }
156
161
                CDLoggerROMClosed();
157
162
#endif
158
163
 
162
167
 
163
168
                if (GameInfo->name) {
164
169
                        free(GameInfo->name);
165
 
                        GameInfo->name = 0;
 
170
                        GameInfo->name = NULL;
166
171
                }
167
172
 
168
173
                if (GameInfo->type != GIT_NSF) {
263
268
                }
264
269
                free(AReadG);
265
270
                free(BWriteG);
266
 
                AReadG = 0;
267
 
                BWriteG = 0;
 
271
                AReadG = NULL;
 
272
                BWriteG = NULL;
268
273
                RWWrap = 0;
269
274
        }
270
275
}
275
280
        else
276
281
                return ARead[a];
277
282
}
 
283
 
278
284
void SetReadHandler(int32 start, int32 end, readfunc func) {
279
285
        int32 x;
 
286
 
280
287
        if (!func)
281
288
                func = ANull;
282
289
 
328
335
 
329
336
static void FreeBuffers() {
330
337
        FCEU_free(RAM);
 
338
    RAM = NULL;
331
339
}
332
340
//------
333
341
 
360
368
        if (GameInfo) FCEU_CloseGame();
361
369
        EmulationPaused = 0; //mbg 5/8/08 - loading games while paused was bad news. maybe this fixes it
362
370
        GameStateRestore = 0;
363
 
        PPU_hook = 0;
364
 
        GameHBIRQHook = 0;
365
 
        FFCEUX_PPURead = 0;
366
 
        FFCEUX_PPUWrite = 0;
 
371
        PPU_hook = NULL;
 
372
        GameHBIRQHook = NULL;
 
373
        FFCEUX_PPURead = NULL;
 
374
        FFCEUX_PPUWrite = NULL;
367
375
        if (GameExpSound.Kill)
368
376
                GameExpSound.Kill();
369
377
        memset(&GameExpSound, 0, sizeof(GameExpSound));
370
 
        MapIRQHook = 0;
 
378
        MapIRQHook = NULL;
371
379
        MMC5Hack = 0;
 
380
        PEC586Hack = 0;
372
381
        PAL &= 1;
373
382
        pale = 0;
374
383
}
386
395
        //----------
387
396
        //attempt to open the files
388
397
        FCEUFILE *fp;
389
 
 
390
 
        FCEU_printf("Loading %s...\n\n", name);
 
398
        char fullname[2048];    // this name contains both archive name and ROM file name
391
399
 
392
400
        const char* romextensions[] = { "nes", "fds", 0 };
393
401
        fp = FCEU_fopen(name, 0, "rb", 0, -1, romextensions);
397
405
                if (!silent)
398
406
                        FCEU_PrintError("Error opening \"%s\"!", name);
399
407
                return 0;
 
408
        } else if (fp->archiveFilename != "")
 
409
        {
 
410
                strcpy(fullname, fp->archiveFilename.c_str());
 
411
                strcat(fullname, "|");
 
412
                strcat(fullname, fp->filename.c_str());
 
413
        } else
 
414
        {
 
415
                strcpy(fullname, name);
400
416
        }
401
417
 
 
418
        //file opened ok. start loading.
 
419
        FCEU_printf("Loading %s...\n\n", fullname);
402
420
        GetFileBase(fp->filename.c_str());
403
 
        //---------
404
 
 
405
 
        //file opened ok. start loading.
406
 
 
407
421
        ResetGameLoaded();
408
 
 
409
 
        //reset parameters so theyre cleared just in case a format's loader doesnt know to do the clearing
 
422
        //reset parameters so they're cleared just in case a format's loader doesn't know to do the clearing
410
423
        MasterRomInfoParams = TMasterRomInfoParams();
411
424
 
412
425
        if (!AutosaveStatus)
419
432
        memset(GameInfo, 0, sizeof(FCEUGI));
420
433
 
421
434
        GameInfo->filename = strdup(fp->filename.c_str());
422
 
        if (fp->archiveFilename != "") GameInfo->archiveFilename = strdup(fp->archiveFilename.c_str());
 
435
        if (fp->archiveFilename != "")
 
436
                GameInfo->archiveFilename = strdup(fp->archiveFilename.c_str());
423
437
        GameInfo->archiveCount = fp->archiveCount;
424
438
 
425
439
        GameInfo->soundchan = 0;
435
449
        bool FCEUXLoad(const char *name, FCEUFILE * fp);
436
450
        /*if(FCEUXLoad(name,fp))
437
451
            goto endlseq;*/
438
 
        if (iNESLoad(name, fp, OverwriteVidMode))
439
 
                goto endlseq;
440
 
        if (NSFLoad(name, fp))
441
 
                goto endlseq;
442
 
        if (UNIFLoad(name, fp))
443
 
                goto endlseq;
444
 
        if (FDSLoad(name, fp))
 
452
        if (iNESLoad(fullname, fp, OverwriteVidMode))
 
453
                goto endlseq;
 
454
        if (NSFLoad(fullname, fp))
 
455
                goto endlseq;
 
456
        if (UNIFLoad(fullname, fp))
 
457
                goto endlseq;
 
458
        if (FDSLoad(fullname, fp))
445
459
                goto endlseq;
446
460
 
447
461
        if (!silent)
462
476
        extern char LoadedRomFName[2048];
463
477
        extern int loadDebugDataFailed;
464
478
 
465
 
        if ((loadDebugDataFailed = loadPreferences(LoadedRomFName)))
 
479
        if ((loadDebugDataFailed = loadPreferences(mass_replace(LoadedRomFName, "|", ".").c_str())))
466
480
                if (!silent)
467
481
                        FCEU_printf("Couldn't load debugging data.\n");
468
482
 
472
486
        FCEU_ResetVidSys();
473
487
 
474
488
        if (GameInfo->type != GIT_NSF)
 
489
        {
475
490
                if (FSettings.GameGenie)
476
 
                        FCEU_OpenGenie();
 
491
                {
 
492
                        if (FCEU_OpenGenie())
 
493
                        {
 
494
                                FCEUI_SetGameGenie(false);
 
495
#ifdef WIN32
 
496
                                genie = 0;
 
497
#endif
 
498
                        }
 
499
                }
 
500
        }
477
501
        PowerNES();
478
502
 
479
503
        if (GameInfo->type != GIT_NSF)
485
509
        if (GameInfo->type != GIT_NSF)
486
510
                FCEU_LoadGameCheats(0);
487
511
 
488
 
        if (AutoResumePlay && (GameInfo->type != GIT_NSF))
 
512
        if (AutoResumePlay)
489
513
        {
490
514
                // load "-resume" savestate
491
 
                if (FCEUSS_Load(FCEU_MakeFName(FCEUMKF_RESUMESTATE, 0, 0).c_str()))
 
515
                if (FCEUSS_Load(FCEU_MakeFName(FCEUMKF_RESUMESTATE, 0, 0).c_str(), false))
492
516
                        FCEU_DispMessage("Old play session resumed.", 0);
493
 
                else
494
 
                        FCEU_DispMessage("", 0);
495
517
        }
496
518
 
497
519
        ResetScreenshotsCounter();
786
808
        SetReadHandler(0, 0x7FF, ARAML);
787
809
        SetWriteHandler(0, 0x7FF, BRAML);
788
810
 
789
 
        SetReadHandler(0x800, 0x1FFF, ARAMH); // Part of a little
790
 
        SetWriteHandler(0x800, 0x1FFF, BRAMH); //hack for a small speed boost.
 
811
        SetReadHandler(0x800, 0x1FFF, ARAMH);   // Part of a little
 
812
        SetWriteHandler(0x800, 0x1FFF, BRAMH);  //hack for a small speed boost.
791
813
 
792
814
        InitializeInput();
793
815
        FCEUSND_Power();
810
832
#endif
811
833
        FCEU_PowerCheats();
812
834
        LagCounterReset();
813
 
        // clear back baffer
 
835
        // clear back buffer
814
836
        extern uint8 *XBackBuf;
815
837
        memset(XBackBuf, 0, 256 * 256);
816
838
 
832
854
                w = FSettings.PAL;
833
855
 
834
856
        PAL = w ? 1 : 0;
 
857
 
835
858
        FCEUPPU_SetVideoSystem(w);
836
859
        SetSoundVariables();
837
860
}
958
981
                AutosaveCounter = 0;
959
982
                AutosaveIndex = (AutosaveIndex + 1) % AutosaveQty;
960
983
                f = strdup(FCEU_MakeFName(FCEUMKF_AUTOSTATE, AutosaveIndex, 0).c_str());
961
 
                FCEUSS_Save(f);
 
984
                FCEUSS_Save(f, false);
962
985
                AutoSS = true;  //Flag that an auto-savestate was made
963
986
                free(f);
 
987
        f = NULL;
964
988
                AutosaveStatus[AutosaveIndex] = 1;
965
989
        }
966
990
}
967
991
 
968
 
void FCEUI_Autosave(void) {
 
992
void FCEUI_RewindToLastAutosave(void) {
969
993
        if (!EnableAutosave || !AutoSS)
970
994
                return;
971
995
 
974
998
                f = strdup(FCEU_MakeFName(FCEUMKF_AUTOSTATE, AutosaveIndex, 0).c_str());
975
999
                FCEUSS_Load(f);
976
1000
                free(f);
 
1001
        f = NULL;
977
1002
 
978
1003
                //Set pointer to previous available slot
979
1004
                if (AutosaveStatus[(AutosaveIndex + AutosaveQty - 1) % AutosaveQty] == 1) {
1028
1053
        case FCEUI_POWER:
1029
1054
        case FCEUI_EJECT_DISK:
1030
1055
        case FCEUI_SWITCH_DISK:
 
1056
        case FCEUI_INSERT_COIN:
1031
1057
                if (!GameInfo) return false;
1032
1058
                if (FCEUMOV_Mode(MOVIEMODE_RECORD)) return true;
1033
1059
#ifdef WIN32