-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.c
1315 lines (1282 loc) · 30.6 KB
/
main.c
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2017 - 2020 FIX94
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <malloc.h>
#include <inttypes.h>
#include <ctype.h>
#ifndef __LIBRETRO__
#include <GL/glut.h>
#include <GL/glext.h>
#endif
#include <time.h>
#include <math.h>
#include "mapper.h"
#include "cpu.h"
#include "ppu.h"
#include "mem.h"
#include "input.h"
#include "fm2play.h"
#include "apu.h"
#include "audio.h"
#include "audio_fds.h"
#include "audio_vrc7.h"
#include "mapper_h/nsf.h"
#include "mapper_h/fds.h"
#if ZIPSUPPORT
#include "unzip/unzip.h"
#endif
#define DEBUG_HZ 0
#define DEBUG_MAIN_CALLS 0
#define DEBUG_KEY 0
#define DEBUG_LOAD_INFO 1
const char *VERSION_STRING = "fixNES Alpha v1.3.1";
static char window_title[256];
static char window_title_pause[256];
enum {
FTYPE_UNK = 0,
FTYPE_NES,
FTYPE_NSF,
FTYPE_FDS,
FTYPE_QD,
#if ZIPSUPPORT
FTYPE_ZIP,
#endif
};
static void nesEmuFileOpen(const char *name);
static bool nesEmuFileRead();
static void nesEmuFileClose();
static void nesEmuDisplayFrame(void);
void nesEmuMainLoop(void);
void nesEmuDeinit(void);
static void nesEmuFdsSetup(uint8_t *src, uint8_t *dst);
static void nesEmuHandleKeyDown(unsigned char key, int x, int y);
static void nesEmuHandleKeyUp(unsigned char key, int x, int y);
static void nesEmuHandleSpecialDown(int key, int x, int y);
static void nesEmuHandleSpecialUp(int key, int x, int y);
#if WINDOWS_BUILD
static void nesEmuSetWindowsVSync(int vsync);
#endif
static int emuFileType = FTYPE_UNK;
static char emuFileName[1024];
uint8_t *emuNesROM = NULL;
uint32_t emuNesROMsize = 0;
#ifndef __LIBRETRO__
static char emuSaveName[1024];
#endif
uint8_t *emuPrgRAM = NULL;
uint32_t emuPrgRAMsize = 0;
//used externally
#ifdef COL_32BIT
uint32_t textureImage[TEX_DOTS*TEX_LINES];
#define TEXIMAGE_LEN TEX_DOTS*TEX_LINES*4
#ifdef COL_GL_BSWAP
#define GL_TEX_FMT GL_UNSIGNED_INT_8_8_8_8_REV
#else //no REVerse
#define GL_TEX_FMT GL_UNSIGNED_INT_8_8_8_8
#endif
#else //COL_16BIT
uint16_t textureImage[TEX_DOTS*TEX_LINES];
#define TEXIMAGE_LEN TEX_DOTS*TEX_LINES*2
#ifdef COL_GL_BSWAP
#define GL_TEX_FMT GL_UNSIGNED_SHORT_5_6_5_REV
#else //no REVerse
#define GL_TEX_FMT GL_UNSIGNED_SHORT_5_6_5
#endif
#endif
bool nesPause = false;
bool ppuDebugPauseFrame = false;
bool doOverscan = false;
bool nesPAL = false;
bool nesEmuNSFPlayback = false;
uint8_t emuInitialNT = NT_UNKNOWN;
#ifndef __LIBRETRO__
static bool inPause = false;
static bool inOverscanToggle = false;
static bool inResize = false;
static bool inDiskSwitch = false;
static bool inReset = false;
#if WINDOWS_BUILD
#include <windows.h>
typedef bool (APIENTRY *PFNWGLSWAPINTERVALEXTPROC) (int interval);
PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = NULL;
#if DEBUG_HZ
static DWORD emuFrameStart = 0;
static DWORD emuTimesCalled = 0;
static DWORD emuTotalElapsed = 0;
#endif //DEBUG_HZ
#if DEBUG_MAIN_CALLS
static DWORD emuMainFrameStart = 0;
static DWORD emuMainTimesCalled = 0;
static DWORD emuMainTimesSkipped = 0;
static DWORD emuMainTotalElapsed = 0;
#endif //DEBUG_MAIN_CALLS
#endif //WINDOWS_BUILD
#ifdef DO_4_3_ASPECT
#define FIXNES_GL_SCALE_MODE GL_LINEAR
#define DOTS_TO_DRAW 320
static uint32_t linesToDrawTex = TEX_LINES;
#else
#define FIXNES_GL_SCALE_MODE GL_NEAREST
#define DOTS_TO_DRAW 256
static uint32_t linesToDrawTex = TEX_LINES;
#endif //DO_4_3_ASPECT
#endif // __LIBRETRO__
static uint32_t linesToDraw = VISIBLE_LINES;
static uint8_t scaleFactor = 2;
bool emuSaveEnabled = false;
bool emuFdsHasSideB = false;
//static uint16_t ppuCycleTimer;
uint32_t cpuCycleTimer;
uint32_t vrc7CycleTimer;
//from input.c
extern uint8_t inValReads[8];
//from m30.c
extern bool m30_flashable;
extern bool m30_singlescreen;
//from m32.c
extern bool m32_singlescreen;
//from p16c8.c
extern bool m78_m78a;
//from ppu.c
extern bool ppuMapper5;
#ifdef __LIBRETRO__
int nesEmuLoadGame(const char* filename)
{
int argc = 2;
const char* argv[] = {"fixNES", filename};
#else
int main(int argc, char** argv)
{
#endif
puts(VERSION_STRING);
strcpy(window_title, VERSION_STRING);
memset(textureImage,0,TEXIMAGE_LEN);
emuFileType = FTYPE_UNK;
#ifdef DISPLAY_PPUWRITES
linesToDraw = LINES_NTSC;
#else
linesToDraw = VISIBLE_LINES;
#endif //end DISPLAY_PPUWRITES
scaleFactor = 2;
emuSaveEnabled = false;
emuFdsHasSideB = false;
nesPause = false;
ppuDebugPauseFrame = false;
doOverscan = false;
nesPAL = false;
nesEmuNSFPlayback = false;
m30_flashable = false;
m30_singlescreen = false;
m32_singlescreen = false;
m78_m78a = false;
ppuMapper5 = false;
emuInitialNT = NT_UNKNOWN;
memset(emuFileName,0,1024);
#ifndef __LIBRETRO__
memset(emuSaveName,0,1024);
#endif
if(argc >= 2)
nesEmuFileOpen(argv[1]);
if(emuFileType == FTYPE_NES)
{
if(!nesEmuFileRead())
{
nesEmuFileClose();
printf("Main: Could not read %s!\n", emuFileName);
puts("Press enter to exit");
getc(stdin);
return EXIT_FAILURE;
}
nesEmuFileClose();
nesPAL = (strstr(emuFileName,"(E)") != NULL) || (strstr(emuFileName,"(Europe)") != NULL) || (strstr(emuFileName,"(Australia)") != NULL)
|| (strstr(emuFileName,"(France)") != NULL) || (strstr(emuFileName,"(Germany)") != NULL) || (strstr(emuFileName,"(Italy)") != NULL)
|| (strstr(emuFileName,"(Spain)") != NULL) || (strstr(emuFileName,"(Sweden)") != NULL) || (strstr(emuFileName,"(PAL)") != NULL);
#ifdef DISPLAY_PPUWRITES
if(nesPAL) linesToDraw = LINES_PAL;
#endif
uint8_t mapper = ((emuNesROM[6] & 0xF0) >> 4) | ((emuNesROM[7] & 0xF0));
emuSaveEnabled = (emuNesROM[6] & (1<<1)) != 0;
bool trainer = (emuNesROM[6] & (1<<2)) != 0;
uint32_t ROMsize = emuNesROMsize-16;
uint32_t prgROMsize = emuNesROM[4] * 0x4000;
if(prgROMsize > ROMsize) //ensure we read in bounds
{
printf("Suggested PRG ROM of 0x%04x is too big, using 0x%04x instead\n", prgROMsize, ROMsize);
prgROMsize = ROMsize;
}
ROMsize -= prgROMsize;
uint32_t chrROMsize = emuNesROM[5] * 0x2000;
if(prgROMsize == 0) //can happen on some roms...
{
printf("PRG ROM size was 0, forcing the whole file to be PRG ROM\n");
prgROMsize = ROMsize;
if(chrROMsize) //force use CHR RAM in this case for now...
{
printf("CHR ROM was set to 0x%04x, instead of the suggested CHR ROM it will now use CHR RAM\n", chrROMsize);
chrROMsize = 0;
}
}
else if(chrROMsize > ROMsize) //ensure we read in bounds
{
printf("Suggested CHR ROM of 0x%04x is too big, using 0x%04x instead\n", chrROMsize, ROMsize);
chrROMsize = ROMsize;
}
if(mapper == 30 && emuSaveEnabled) //rewritable prg
{
emuPrgRAMsize = prgROMsize;
m30_flashable = true;
}
else
{
m30_flashable = false;
if(mapper == 5) //just to be on the safe side
emuPrgRAMsize = 0x10000;
else
{
emuPrgRAMsize = emuNesROM[8] * 0x2000;
if(emuPrgRAMsize == 0) emuPrgRAMsize = 0x2000;
}
}
emuPrgRAM = malloc(emuPrgRAMsize);
//start out clear
memset(emuPrgRAM, 0, emuPrgRAMsize);
uint8_t *prgROM = emuNesROM+16;
if(m30_flashable) //initial prg fill
memcpy(emuPrgRAM, prgROM, prgROMsize);
else if(trainer)
{
memcpy(emuPrgRAM+0x1000,prgROM,0x200);
prgROM += 512;
}
uint8_t *chrROM = NULL;
if(chrROMsize)
{
chrROM = emuNesROM+16+prgROMsize;
if(trainer) chrROM += 512;
}
apuInitBufs();
cpuInit();
ppuInit();
memInit();
apuInit();
inputInit();
//special 1-screen case
if(mapper == 30 && (emuNesROM[6] & 9) == 8)
{
printf("Using Single Screen for Mapper 30\n");
m30_singlescreen = true;
}
else if(emuNesROM[6] & 8)
{
emuInitialNT = NT_4SCREEN;
ppuSetNameTbl4Screen();
}
else if(emuNesROM[6] & 1)
{
emuInitialNT = NT_VERTICAL;
ppuSetNameTblVertical();
}
else
{
emuInitialNT = NT_HORIZONTAL;
ppuSetNameTblHorizontal();
}
#if DEBUG_LOAD_INFO
printf("Used Mapper: %i\n", mapper);
printf("PRG: 0x%x bytes PRG RAM: 0x%x bytes CHR: 0x%x bytes\n", prgROMsize, emuPrgRAMsize, chrROMsize);
#endif
if(mapper == 5)
ppuMapper5 = true;
else if(mapper == 32)
{
if(strstr(emuFileName,"Major League") != NULL)
{
printf("Using Single Screen for Major League Mapper 32\n");
m32_singlescreen = true;
}
else
m32_singlescreen = false;
}
else if(mapper == 78)
{
if(strstr(emuFileName,"Holy Diver") != NULL)
{
printf("Using Holy Diver Variant for Mapper 78\n");
m78_m78a = true;
}
else
m78_m78a = false;
}
if(!mapperInit(mapper, prgROM, prgROMsize, emuPrgRAM, emuPrgRAMsize, chrROM, chrROMsize))
{
printf("Mapper init failed!\n");
free(emuNesROM);
puts("Press enter to exit");
getc(stdin);
return EXIT_FAILURE;
}
#if DEBUG_LOAD_INFO
printf("Trainer: %i Saving: %i VRAM Mode: %s\n", trainer, emuSaveEnabled, (emuNesROM[6] & 8) ? "4-Screen" :
((emuNesROM[6] & 1) ? "Vertical" : "Horizontal"));
#endif
sprintf(window_title, "%s NES - %s\n", nesPAL ? "PAL" : "NTSC", VERSION_STRING);
#ifndef __LIBRETRO__
if(emuSaveEnabled)
{
memcpy(emuSaveName, emuFileName, 1024);
memcpy(emuSaveName+strlen(emuSaveName)-3,"sav",3);
printf("Save Path: %s\n",emuSaveName);
FILE *save = fopen(emuSaveName, "rb");
if(save)
{
fread(emuPrgRAM,1,emuPrgRAMsize,save);
fclose(save);
}
}
#endif
#if 0
if(argc == 5 && (strstr(argv[2],".fm2") != NULL || strstr(argv[2],".FM2") != NULL))
fm2playInit(argv[2], atoi(argv[3]), !!atoi(argv[4]));
#endif
}
else if(emuFileType == FTYPE_NSF)
{
if(!nesEmuFileRead())
{
nesEmuFileClose();
printf("Main: Could not read %s!\n", emuFileName);
puts("Press enter to exit");
getc(stdin);
return EXIT_FAILURE;
}
nesEmuFileClose();
emuPrgRAMsize = 0x2000;
emuPrgRAM = malloc(emuPrgRAMsize);
if(!mapperInitNSF(emuNesROM, emuNesROMsize, emuPrgRAM, emuPrgRAMsize))
{
printf("NSF init failed!\n");
free(emuNesROM);
puts("Press enter to exit");
getc(stdin);
return EXIT_FAILURE;
}
if(emuNesROM[0xE] != 0)
sprintf(window_title, "%.32s (%s NSF) - %s\n", (char*)(emuNesROM+0xE), nesPAL ? "PAL" : "NTSC", VERSION_STRING);
nesEmuNSFPlayback = true;
linesToDraw = 30;
scaleFactor = 3;
}
else if(emuFileType == FTYPE_FDS || emuFileType == FTYPE_QD)
{
bool saveValid = false;
#ifndef __LIBRETRO__
memcpy(emuSaveName, emuFileName, 1024);
if(emuFileType == FTYPE_FDS)
memcpy(emuSaveName+strlen(emuSaveName)-3,"sav",3);
else //.qd has one less character
memcpy(emuSaveName+strlen(emuSaveName)-2,"sav",3);
printf("Save Path: %s\n",emuSaveName);
FILE *save = fopen(emuSaveName, "rb");
if(save)
{
fseek(save,0,SEEK_END);
size_t saveSize = ftell(save);
if(saveSize == 0x10000 || saveSize == 0x20000)
{
emuNesROM = malloc(saveSize);
rewind(save);
fread(emuNesROM,1,saveSize,save);
saveValid = true;
if(saveSize == 0x20000)
emuFdsHasSideB = true;
}
else
printf("Save file ignored\n");
fclose(save);
}
#endif
if(saveValid) //can use save as ROM
nesEmuFileClose();
else //no (valid) save, parse file
{
if(!nesEmuFileRead())
{
nesEmuFileClose();
printf("Main: Could not read %s!\n", emuFileName);
puts("Press enter to exit");
getc(stdin);
return EXIT_FAILURE;
}
nesEmuFileClose();
uint8_t *nesFread = emuNesROM;
uint32_t nesFread_len = emuNesROMsize;
emuNesROM = NULL;
emuNesROMsize = 0;
uint8_t *fds_src;
uint32_t fds_src_len;
if(nesFread[0] == 0x46 && nesFread[1] == 0x44 && nesFread[2] == 0x53)
{
fds_src = nesFread+0x10;
fds_src_len = nesFread_len-0x10;
}
else
{
fds_src = nesFread;
fds_src_len = nesFread_len;
}
bool fds_no_crc = (fds_src[0x38] == 0x02 && fds_src[0x3A] == 0x03
&& fds_src[0x3A] != 0x02 && fds_src[0x3E] != 0x03);
if(fds_no_crc)
{
if(fds_src_len == 0x1FFB8)
{
emuFdsHasSideB = true;
emuNesROMsize = 0x20000;
emuNesROM = malloc(emuNesROMsize);
memset(emuNesROM, 0, emuNesROMsize);
nesEmuFdsSetup(fds_src, emuNesROM); //setup individually
nesEmuFdsSetup(fds_src+0xFFDC, emuNesROM+0x10000);
}
else if(fds_src_len == 0xFFDC)
{
emuFdsHasSideB = false;
emuNesROMsize = 0x10000;
emuNesROM = malloc(emuNesROMsize);
memset(emuNesROM, 0, emuNesROMsize);
nesEmuFdsSetup(fds_src, emuNesROM);
}
else
printf("Unknown FDS Length: %x\n", fds_src_len);
}
else
{
if(fds_src_len == 0x20000)
{
emuFdsHasSideB = true;
emuNesROMsize = 0x20000;
emuNesROM = malloc(emuNesROMsize);
memcpy(emuNesROM, fds_src, emuNesROMsize);
}
else if(fds_src_len == 0x10000)
{
emuFdsHasSideB = false;
emuNesROMsize = 0x10000;
emuNesROM = malloc(emuNesROMsize);
memcpy(emuNesROM, fds_src, emuNesROMsize);
}
else
printf("Unknown FDS Length: %x\n", fds_src_len);
}
free(nesFread);
}
if(emuNesROM != NULL)
{
emuPrgRAMsize = 0x8000;
emuPrgRAM = malloc(emuPrgRAMsize);
apuInitBufs();
cpuInit();
ppuInit();
memInit();
apuInit();
inputInit();
if(!mapperInitFDS(emuNesROM, emuFdsHasSideB, emuPrgRAM, emuPrgRAMsize))
{
printf("FDS init failed!\n");
free(emuNesROM);
puts("Press enter to exit");
getc(stdin);
return EXIT_FAILURE;
}
sprintf(window_title, "Famicom Disk System - %s\n", VERSION_STRING);
}
}
if(emuNesROM == NULL)
{
#if ZIPSUPPORT
printf("Main: No File to Open! Make sure to call fixNES with a .nes/.nsf/.fds/.qd/.zip File as Argument.\n");
#else
printf("Main: No File to Open! Make sure to call fixNES with a .nes/.nsf/.fds/.qd File as Argument.\n");
#endif
puts("Press enter to exit");
getc(stdin);
return EXIT_FAILURE;
}
sprintf(window_title_pause, "%s (Pause)", window_title);
#if WINDOWS_BUILD
#if DEBUG_HZ
emuFrameStart = GetTickCount();
#endif
#if DEBUG_MAIN_CALLS
emuMainFrameStart = GetTickCount();
#endif
#endif
cpuCycleTimer = nesPAL ? 16 : 12;
#if NUKEDOPLL
vrc7CycleTimer = 24 / cpuCycleTimer;
#else
vrc7CycleTimer = 432 / cpuCycleTimer;
#endif
//do one scanline per idle loop
//ppuCycleTimer = nesPAL ? 5 : 4;
//mainLoopRuns = nesPAL ? DOTS*ppuCycleTimer : DOTS*ppuCycleTimer;
//mainLoopPos = mainLoopRuns;
#ifndef __LIBRETRO__
glutInit(&argc, argv);
glutInitWindowSize(DOTS_TO_DRAW*scaleFactor, linesToDraw*scaleFactor);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutCreateWindow(nesPause ? window_title_pause : window_title);
audioInit();
atexit(&nesEmuDeinit);
glutKeyboardFunc(&nesEmuHandleKeyDown);
glutKeyboardUpFunc(&nesEmuHandleKeyUp);
glutSpecialFunc(&nesEmuHandleSpecialDown);
glutSpecialUpFunc(&nesEmuHandleSpecialUp);
glutDisplayFunc(&nesEmuDisplayFrame);
glutIdleFunc(&nesEmuMainLoop);
#if WINDOWS_BUILD
/* Enable OpenGL VSync */
wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
nesEmuSetWindowsVSync(1);
#endif
/* Set used texture height */
#ifdef DO_4_3_ASPECT
linesToDrawTex = linesToDraw*4;
#else
linesToDrawTex = linesToDraw;
#endif
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
#ifdef COL_32BIT
#ifdef COL_BGRA
printf("Drawing onto BGRA8 Texture\n");
glTexImage2D(GL_TEXTURE_2D, 0, 4, TEX_DOTS, linesToDrawTex, 0, GL_BGRA, GL_TEX_FMT, textureImage);
#else //case RGBA
printf("Drawing onto RGBA8 Texture\n");
glTexImage2D(GL_TEXTURE_2D, 0, 4, TEX_DOTS, linesToDrawTex, 0, GL_RGBA, GL_TEX_FMT, textureImage);
#endif //end COL_BGRA
#else //case COL_16BIT
printf("Drawing onto RGB565 Texture\n");
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TEX_DOTS, linesToDrawTex, 0, GL_RGB, GL_TEX_FMT, textureImage);
#endif //end COL_32BIT
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, FIXNES_GL_SCALE_MODE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, FIXNES_GL_SCALE_MODE);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_FLAT);
glutMainLoop();
#endif // __LIBRETRO__
return EXIT_SUCCESS;
}
static FILE *nesEmuFilePointer = NULL;
#if ZIPSUPPORT
static bool nesEmuFileIsZip = false;
static uint8_t *nesEmuZipBuf = NULL;
static uint32_t nesEmuZipLen = 0;
static unzFile nesEmuZipObj;
static unz_file_info nesEmuZipObjInfo;
#endif
static int nesEmuGetFileType(const char *name)
{
int nLen = strlen(name);
if(nLen > 4 && name[nLen-4] == '.')
{
if(tolower(name[nLen-3]) == 'n' && tolower(name[nLen-2]) == 'e' && tolower(name[nLen-1]) == 's')
return FTYPE_NES;
else if(tolower(name[nLen-3]) == 'n' && tolower(name[nLen-2]) == 's' && tolower(name[nLen-1]) == 'f')
return FTYPE_NSF;
else if(tolower(name[nLen-3]) == 'f' && tolower(name[nLen-2]) == 'd' && tolower(name[nLen-1]) == 's')
return FTYPE_FDS;
#if ZIPSUPPORT
else if(tolower(name[nLen-3]) == 'z' && tolower(name[nLen-2]) == 'i' && tolower(name[nLen-1]) == 'p')
return FTYPE_ZIP;
#endif
}
else if(nLen > 3 && name[nLen-3] == '.')
{
if(tolower(name[nLen-2]) == 'q' && tolower(name[nLen-1]) == 'd')
return FTYPE_QD;
}
return FTYPE_UNK;
}
static void nesEmuFileOpen(const char *name)
{
emuFileType = FTYPE_UNK;
memset(emuFileName,0,1024);
#ifndef __LIBRETRO__
memset(emuSaveName,0,1024);
#endif
int baseType = nesEmuGetFileType(name);
#if ZIPSUPPORT
if(baseType == FTYPE_ZIP)
{
printf("Base ZIP File: %s\n", name);
FILE *tmp = fopen(name,"rb");
if(!tmp)
{
printf("Main: Could not open %s!\n", name);
return;
}
fseek(tmp,0,SEEK_END);
nesEmuZipLen = ftell(tmp);
rewind(tmp);
nesEmuZipBuf = malloc(nesEmuZipLen);
if(!nesEmuZipBuf)
{
printf("Main: Could not allocate ZIP buffer!\n");
fclose(tmp);
return;
}
fread(nesEmuZipBuf,1,nesEmuZipLen,tmp);
fclose(tmp);
char filepath[20];
snprintf(filepath,20,"%x+%x",(unsigned int)nesEmuZipBuf,nesEmuZipLen);
nesEmuZipObj = unzOpen(filepath);
int err = unzGoToFirstFile(nesEmuZipObj);
while (err == UNZ_OK)
{
char tmpName[256];
err = unzGetCurrentFileInfo(nesEmuZipObj,&nesEmuZipObjInfo,tmpName,256,NULL,0,NULL,0);
if(err == UNZ_OK)
{
int curInZipType = nesEmuGetFileType(tmpName);
if(curInZipType != FTYPE_ZIP && curInZipType != FTYPE_UNK)
{
emuFileType = curInZipType;
nesEmuFileIsZip = true;
if(strchr(name,'/') != NULL || strchr(name,'\\') != NULL)
{
const char *nPath = name;
if(strchr(nPath,'/') != NULL)
nPath = (strrchr(nPath,'/')+1);
if(strchr(nPath,'\\') != NULL)
nPath = (strrchr(nPath,'\\')+1);
strncpy(emuFileName, name, nPath-name);
}
const char *zName = tmpName;
if(strchr(zName,'/') != NULL)
zName = (strrchr(zName,'/')+1);
if(strchr(zName,'\\') != NULL)
zName = (strrchr(zName,'\\')+1);
strcat(emuFileName, zName);
printf("File in ZIP Type: %s\n", emuFileType == FTYPE_NES ? "NES" : (emuFileType == FTYPE_NSF ? "NSF" : "FDS"));
printf("Full Path from ZIP: %s\n", emuFileName);
break;
}
else
err = unzGoToNextFile(nesEmuZipObj);
}
}
if(emuFileType == FTYPE_UNK)
{
printf("Found no usable file in ZIP\n");
unzClose(nesEmuZipObj);
if(nesEmuZipBuf)
free(nesEmuZipBuf);
nesEmuZipBuf = NULL;
nesEmuZipLen = 0;
}
}
else if(baseType != FTYPE_UNK)
#else
if(baseType != FTYPE_UNK)
#endif
{
nesEmuFilePointer = fopen(name,"rb");
if(!nesEmuFilePointer)
printf("Main: Could not open %s!\n", name);
else
{
emuFileType = baseType;
strncpy(emuFileName, name, 1024);
printf("File Type: %s\n", baseType == FTYPE_NES ? "NES" : (baseType == FTYPE_NSF ? "NSF" : "FDS"));
printf("Full Path: %s\n", emuFileName);
}
}
}
static bool nesEmuFileRead()
{
#if ZIPSUPPORT
if(nesEmuFileIsZip)
{
unzOpenCurrentFile(nesEmuZipObj);
emuNesROMsize = nesEmuZipObjInfo.uncompressed_size;
emuNesROM = malloc(emuNesROMsize);
if(emuNesROM)
unzReadCurrentFile(nesEmuZipObj,emuNesROM,emuNesROMsize);
unzCloseCurrentFile(nesEmuZipObj);
}
else
#endif
{
fseek(nesEmuFilePointer,0,SEEK_END);
emuNesROMsize = ftell(nesEmuFilePointer);
rewind(nesEmuFilePointer);
emuNesROM = malloc(emuNesROMsize);
if(emuNesROM)
fread(emuNesROM,1,emuNesROMsize,nesEmuFilePointer);
}
if(emuNesROM)
return true;
//else
printf("Main: Could not allocate ROM buffer!\n");
return false;
}
//cleans up vars from read
static void nesEmuFileClose()
{
#if ZIPSUPPORT
if(nesEmuFileIsZip)
unzClose(nesEmuZipObj);
nesEmuFileIsZip = false;
if(nesEmuZipBuf)
free(nesEmuZipBuf);
nesEmuZipBuf = NULL;
nesEmuZipLen = 0;
#endif
if(nesEmuFilePointer)
fclose(nesEmuFilePointer);
nesEmuFilePointer = NULL;
}
#ifndef __LIBRETRO__
static volatile bool emuRenderFrame = false;
#endif
extern uint8_t audioExpansion;
void nesEmuDeinit(void)
{
//printf("\n");
#ifndef __LIBRETRO__
emuRenderFrame = false;
audioDeinit();
#endif
apuDeinitBufs();
if(emuNesROM != NULL)
{
#ifndef __LIBRETRO__
if(!nesEmuNSFPlayback && (audioExpansion&EXP_FDS))
{
FILE *save = fopen(emuSaveName, "wb");
if(save)
{
if(emuFdsHasSideB)
fwrite(emuNesROM,1,0x20000,save);
else
fwrite(emuNesROM,1,0x10000,save);
fclose(save);
}
}
#endif
free(emuNesROM);
}
audioExpansion = 0;
nesEmuNSFPlayback = false;
emuNesROM = NULL;
emuNesROMsize = 0;
if(emuPrgRAM != NULL)
{
#ifndef __LIBRETRO__
if(emuSaveEnabled)
{
FILE *save = fopen(emuSaveName, "wb");
if(save)
{
fwrite(emuPrgRAM,1,emuPrgRAMsize,save);
fclose(save);
}
}
#endif
free(emuPrgRAM);
}
emuSaveEnabled = false;
emuPrgRAM = NULL;
emuPrgRAMsize = 0;
//printf("Bye!\n");
}
//used externally
#ifndef __LIBRETRO__
bool emuSkipFrame = false;
#endif
//static uint32_t mCycles = 0;
void nesEmuMainLoop(void)
{
#ifndef __LIBRETRO__
if(emuRenderFrame || nesPause)
{
#if (WINDOWS_BUILD && DEBUG_MAIN_CALLS)
emuMainTimesSkipped++;
#endif
audioSleep();
return;
}
#endif
#ifdef DISPLAY_PPUWRITES
//clear out previous frame dots
if(!nesEmuNSFPlayback)
memset(textureImage,0,TEXIMAGE_LEN);
#endif
while(1)
{
//main CPU clock
if(!cpuCycle())
exit(EXIT_SUCCESS);
//run graphics
ppuCycle();
//run audio
apuCycle();
//mapper related irqs
mapperCycle();
//mCycles++;
if(ppuDrawDone())
{
//printf("%i\n",mCycles);
//mCycles = 0;
#ifndef __LIBRETRO__
emuRenderFrame = true;
#if 0
if(fm2playRunning())
fm2playUpdate();
#endif
#if (WINDOWS_BUILD && DEBUG_HZ)
emuTimesCalled++;
DWORD end = GetTickCount();
emuTotalElapsed += end - emuFrameStart;
if(emuTotalElapsed >= 1000)
{
printf("\r%iHz ", emuTimesCalled);
emuTimesCalled = 0;
emuTotalElapsed = 0;
}
emuFrameStart = end;
#endif
//update audio before drawing
while(!apuUpdate()) audioSleep();
glutPostRedisplay();
#if 0
if(ppuDebugPauseFrame)
{
ppuDebugPauseFrame = false;
nesPause = true;
}
#endif
#endif
if(nesEmuNSFPlayback)
nsfVsync(); //for next song checks
else if(audioExpansion&EXP_FDS)
fdsupdatedisc(); //for possible disc insert/switch
break;
}
}
#if (WINDOWS_BUILD && DEBUG_MAIN_CALLS)
emuMainTimesCalled++;
DWORD end = GetTickCount();
emuMainTotalElapsed += end - emuMainFrameStart;
if(emuMainTotalElapsed >= 1000)
{
printf("\r%i calls, %i skips ", emuMainTimesCalled, emuMainTimesSkipped);
emuMainTimesCalled = 0;
emuMainTimesSkipped = 0;
emuMainTotalElapsed = 0;
}
emuMainFrameStart = end;
#endif
}
#ifndef __LIBRETRO__
extern bool fdsSwitch;
static void nesEmuHandleKeyDown(unsigned char key, int x, int y)
{
(void)x;
(void)y;
switch (key)
{
case 'y':
case 'z':
case 'Y':
case 'Z':
if(fm2playRunning())
break;
#if DEBUG_KEY
if(inValReads[BUTTON_A]==0)
printf("a\n");
#endif
inValReads[BUTTON_A]=1;
break;
case 'x':
case 'X':
if(fm2playRunning())
break;
#if DEBUG_KEY
if(inValReads[BUTTON_B]==0)
printf("b\n");
#endif
inValReads[BUTTON_B]=1;
break;
case 's':
case 'S':
if(fm2playRunning())
break;
#if DEBUG_KEY
if(inValReads[BUTTON_SELECT]==0)
printf("sel\n");
#endif
inValReads[BUTTON_SELECT]=1;
break;
case 'a':
case 'A':
if(fm2playRunning())
break;
#if DEBUG_KEY
if(inValReads[BUTTON_START]==0)
printf("start\n");
#endif
inValReads[BUTTON_START]=1;
break;
case '\x1B': //Escape
memDumpMainMem();
exit(EXIT_SUCCESS);
break;
case 'b':
case 'B':
if(!inDiskSwitch)
{
fdsSwitch = true;
inDiskSwitch = true;
}
break;
case 'p':
case 'P':
if(!inPause)
{
#if DEBUG_KEY
printf("pause\n");
#endif
inPause = true;
nesPause ^= true;
glutSetWindowTitle(nesPause ? window_title_pause : window_title);
}
break;
case '1':
if(!inResize)
{
inResize = true;
glutReshapeWindow(DOTS_TO_DRAW*1, linesToDraw*1);
}
break;
case '2':
if(!inResize)
{
inResize = true;
glutReshapeWindow(DOTS_TO_DRAW*2, linesToDraw*2);
}
break;
case '3':
if(!inResize)