/
3dsimpl.cpp
1509 lines (1254 loc) · 51.6 KB
/
3dsimpl.cpp
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
//=============================================================================
// Contains all the hooks and interfaces between the emulator interface
// and the main emulator core.
//=============================================================================
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <3ds.h>
#include <dirent.h>
#include "3dstypes.h"
#include "3dsemu.h"
#include "3dsexit.h"
#include "3dsgpu.h"
#include "3dssound.h"
#include "3dsui.h"
#include "3dsinput.h"
#include "3dsfiles.h"
#include "3dsinterface.h"
#include "3dsmain.h"
#include "3dsasync.h"
#include "3dsimpl.h"
#include "3dsopt.h"
#include "3dsconfig.h"
//---------------------------------------------------------
// All other codes that you need here.
//---------------------------------------------------------
#include "3dsdbg.h"
#include "3dsimpl.h"
#include "3dsimpl_gpu.h"
#include "3dsimpl_tilecache.h"
#include "shaderfast2_shbin.h"
#include "shaderslow_shbin.h"
#include "shaderslow2_shbin.h"
#include "vsect.h"
#include "extern.h"
#include "common.h"
//----------------------------------------------------------------------
// Settings
//----------------------------------------------------------------------
SSettings3DS settings3DS;
#define SETTINGS_SOFTWARERENDERING 0
#define SETTINGS_IDLELOOPPATCH 1
#define SETTINGS_BIOS 2
//----------------------------------------------------------------------
// Menu options
//----------------------------------------------------------------------
SMenuItem optionsForFont[] = {
MENU_MAKE_DIALOG_ACTION (0, "Tempesta", ""),
MENU_MAKE_DIALOG_ACTION (1, "Ronda", ""),
MENU_MAKE_DIALOG_ACTION (2, "Arial", ""),
MENU_MAKE_LASTITEM ()
};
SMenuItem optionsForStretch[] = {
MENU_MAKE_DIALOG_ACTION (0, "No Stretch", "'Pixel Perfect'"),
MENU_MAKE_DIALOG_ACTION (1, "4:3 Fit", "Stretch to 320x240"),
MENU_MAKE_DIALOG_ACTION (2, "Fullscreen", "Stretch to 400x240"),
MENU_MAKE_DIALOG_ACTION (3, "Cropped 4:3 Fit", "Crop & Stretch to 320x240"),
MENU_MAKE_DIALOG_ACTION (4, "Cropped Fullscreen", "Crop & Stretch to 400x240"),
MENU_MAKE_LASTITEM ()
};
SMenuItem optionsForFrameskip[] = {
MENU_MAKE_DIALOG_ACTION (0, "Disabled", ""),
MENU_MAKE_DIALOG_ACTION (1, "Enabled (max 1 frame)", ""),
MENU_MAKE_DIALOG_ACTION (2, "Enabled (max 2 frames)", ""),
MENU_MAKE_DIALOG_ACTION (3, "Enabled (max 3 frames)", ""),
MENU_MAKE_DIALOG_ACTION (4, "Enabled (max 4 frames)", ""),
MENU_MAKE_LASTITEM ()
};
SMenuItem optionsForFrameRate[] = {
MENU_MAKE_DIALOG_ACTION (0, "Default based on ROM", ""),
MENU_MAKE_DIALOG_ACTION (1, "50 FPS", ""),
MENU_MAKE_DIALOG_ACTION (2, "60 FPS", ""),
MENU_MAKE_LASTITEM ()
};
SMenuItem optionsForAutoSaveSRAMDelay[] = {
MENU_MAKE_DIALOG_ACTION (1, "1 second", ""),
MENU_MAKE_DIALOG_ACTION (2, "10 seconds", ""),
MENU_MAKE_DIALOG_ACTION (3, "60 seconds", ""),
MENU_MAKE_DIALOG_ACTION (4, "Disabled", "Touch bottom screen to save"),
MENU_MAKE_LASTITEM ()
};
SMenuItem optionsForTurboFire[] = {
MENU_MAKE_DIALOG_ACTION (0, "None", ""),
MENU_MAKE_DIALOG_ACTION (10, "Slowest", ""),
MENU_MAKE_DIALOG_ACTION (8, "Slower", ""),
MENU_MAKE_DIALOG_ACTION (6, "Slow", ""),
MENU_MAKE_DIALOG_ACTION (4, "Fast", ""),
MENU_MAKE_DIALOG_ACTION (2, "Faster", ""),
MENU_MAKE_DIALOG_ACTION (1, "Very Fast", ""),
MENU_MAKE_LASTITEM ()
};
SMenuItem optionsForButtons[] = {
MENU_MAKE_DIALOG_ACTION (0, "None", ""),
MENU_MAKE_DIALOG_ACTION (IO_BUTTON_I, "Button I", ""),
MENU_MAKE_DIALOG_ACTION (IO_BUTTON_II, "Button II", ""),
MENU_MAKE_DIALOG_ACTION (IO_BUTTON_III, "Button III", ""),
MENU_MAKE_DIALOG_ACTION (IO_BUTTON_IV, "Button IV", ""),
MENU_MAKE_DIALOG_ACTION (IO_BUTTON_V, "Button V", ""),
MENU_MAKE_DIALOG_ACTION (IO_BUTTON_VI, "Button VI", ""),
MENU_MAKE_DIALOG_ACTION (IO_BUTTON_SELECT, "PCE's SELECT", ""),
MENU_MAKE_DIALOG_ACTION (IO_BUTTON_RUN, "PCE's RUN", ""),
MENU_MAKE_LASTITEM ()
};
SMenuItem optionsForSpriteFlicker[] =
{
MENU_MAKE_DIALOG_ACTION (0, "Hardware Accurate", "Flickers like real hardware"),
MENU_MAKE_DIALOG_ACTION (1, "Better Visuals", "Looks better, less accurate"),
MENU_MAKE_LASTITEM ()
};
SMenuItem optionsForIdleLoopPatch[] =
{
MENU_MAKE_DIALOG_ACTION (1, "Enabled", "Faster but some games may freeze"),
MENU_MAKE_DIALOG_ACTION (0, "Disabled", "Slower but better compatibility"),
MENU_MAKE_LASTITEM ()
};
SMenuItem optionsForRendering[] =
{
MENU_MAKE_DIALOG_ACTION (0, "Hardware", "Faster"),
MENU_MAKE_DIALOG_ACTION (1, "Software", "More accurate"),
MENU_MAKE_LASTITEM ()
};
SMenuItem optionsForBIOS[] =
{
MENU_MAKE_DIALOG_ACTION (2, "CD-ROM v1", "/3ds/syscards/syscard1.pce"),
MENU_MAKE_DIALOG_ACTION (1, "CD-ROM v2", "/3ds/syscards/syscard2.pce"),
MENU_MAKE_DIALOG_ACTION (0, "CD-ROM v3", "/3ds/syscards/syscard3.pce"),
MENU_MAKE_DIALOG_ACTION (3, "Arcade CD", "/3ds/syscards/syscard3.pce"),
MENU_MAKE_DIALOG_ACTION (4, "Games Express", "/3ds/syscards/games_express.pce"),
MENU_MAKE_LASTITEM ()
};
SMenuItem optionMenu[] = {
MENU_MAKE_HEADER1 ("GLOBAL SETTINGS"),
MENU_MAKE_PICKER (11000, " Screen Stretch", "How would you like the final screen to appear?", optionsForStretch, DIALOGCOLOR_CYAN),
MENU_MAKE_PICKER (18000, " Font", "The font used for the user interface.", optionsForFont, DIALOGCOLOR_CYAN),
MENU_MAKE_CHECKBOX (15001, " Hide text in bottom screen", 0),
MENU_MAKE_DISABLED (""),
MENU_MAKE_HEADER1 ("GAME-SPECIFIC SETTINGS"),
MENU_MAKE_PICKER (20000, " Idle Loop Patching", "You must reload the ROM after changing this.", optionsForIdleLoopPatch, DIALOGCOLOR_CYAN),
MENU_MAKE_PICKER (10000, " Frameskip", "Try changing this if the game runs slow. Skipping frames help it run faster but less smooth.", optionsForFrameskip, DIALOGCOLOR_CYAN),
MENU_MAKE_PICKER (21000, " BIOS", "The BIOS must be in your /3ds/syscards folder. Re-load ROM after changing.", optionsForBIOS, DIALOGCOLOR_CYAN),
MENU_MAKE_DISABLED (""),
MENU_MAKE_HEADER1 ("AUDIO"),
MENU_MAKE_CHECKBOX (50002, " Apply volume to all games", 0),
MENU_MAKE_GAUGE (14000, " Volume Amplification", 0, 8, 4),
MENU_MAKE_DISABLED (""),
MENU_MAKE_HEADER1 ("BUTTON CONFIGURATION"),
MENU_MAKE_CHECKBOX (50000, " Apply button mappings to all games", 0),
MENU_MAKE_CHECKBOX (50001, " Apply rapid fire settings to all games", 0),
MENU_MAKE_PICKER (13010, " 3DS 'A'", "", optionsForButtons, DIALOGCOLOR_CYAN),
MENU_MAKE_GAUGE (13000, " Rapid-Fire Speed", 0, 10, 0),
MENU_MAKE_PICKER (13011, " 3DS 'B'", "", optionsForButtons, DIALOGCOLOR_CYAN),
MENU_MAKE_GAUGE (13001, " Rapid-Fire Speed", 0, 10, 0),
MENU_MAKE_PICKER (13012, " 3DS 'X'", "", optionsForButtons, DIALOGCOLOR_CYAN),
MENU_MAKE_GAUGE (13002, " Rapid-Fire Speed", 0, 10, 0),
MENU_MAKE_PICKER (13013, " 3DS 'Y'", "", optionsForButtons, DIALOGCOLOR_CYAN),
MENU_MAKE_GAUGE (13003, " Rapid-Fire Speed", 0, 10, 0),
MENU_MAKE_PICKER (13014, " 3DS 'L'", "", optionsForButtons, DIALOGCOLOR_CYAN),
MENU_MAKE_GAUGE (13004, " Rapid-Fire Speed", 0, 10, 0),
MENU_MAKE_PICKER (13015, " 3DS 'R'", "", optionsForButtons, DIALOGCOLOR_CYAN),
MENU_MAKE_GAUGE (13005, " Rapid-Fire Speed", 0, 10, 0),
MENU_MAKE_LASTITEM ()
};
//-------------------------------------------------------
SMenuItem optionsForDisk[] =
{
MENU_MAKE_DIALOG_ACTION (0, "Eject Disk", ""),
MENU_MAKE_DIALOG_ACTION (1, "Change to Disk 1 Side A", ""),
MENU_MAKE_DIALOG_ACTION (2, "Change to Disk 1 Side B", ""),
MENU_MAKE_DIALOG_ACTION (3, "Change to Disk 2 Side A", ""),
MENU_MAKE_DIALOG_ACTION (4, "Change to Disk 2 Side B", ""),
MENU_MAKE_DIALOG_ACTION (5, "Change to Disk 3 Side A", ""),
MENU_MAKE_DIALOG_ACTION (6, "Change to Disk 3 Side B", ""),
MENU_MAKE_DIALOG_ACTION (7, "Change to Disk 4 Side A", ""),
MENU_MAKE_DIALOG_ACTION (8, "Change to Disk 4 Side B", ""),
MENU_MAKE_LASTITEM ()
};
//-------------------------------------------------------
// Standard in-game emulator menu.
// You should not modify those menu items that are
// marked 'do not modify'.
//-------------------------------------------------------
SMenuItem emulatorMenu[] = {
MENU_MAKE_HEADER2 ("Emulator"), // Do not modify
MENU_MAKE_ACTION (1000, " Resume Game"), // Do not modify
MENU_MAKE_PICKER2 (30000," Choose Disk", "", optionsForDisk, DIALOGCOLOR_CYAN),
MENU_MAKE_HEADER2 (""),
MENU_MAKE_HEADER2 ("Savestates"),
MENU_MAKE_ACTION (2001, " Save Slot #1"), // Do not modify
MENU_MAKE_ACTION (2002, " Save Slot #2"), // Do not modify
MENU_MAKE_ACTION (2003, " Save Slot #3"), // Do not modify
MENU_MAKE_ACTION (2004, " Save Slot #4"), // Do not modify
MENU_MAKE_HEADER2 (""),
MENU_MAKE_ACTION (3001, " Load Slot #1"), // Do not modify
MENU_MAKE_ACTION (3002, " Load Slot #2"), // Do not modify
MENU_MAKE_ACTION (3003, " Load Slot #3"), // Do not modify
MENU_MAKE_ACTION (3004, " Load Slot #4"), // Do not modify
MENU_MAKE_HEADER2 (""),
MENU_MAKE_HEADER2 ("Others"), // Do not modify
MENU_MAKE_ACTION (4001, " Take Screenshot"),// Do not modify
MENU_MAKE_ACTION (5001, " Reset Console"), // Do not modify
MENU_MAKE_ACTION (6001, " Exit"), // Do not modify
MENU_MAKE_LASTITEM ()
};
//------------------------------------------------------------------------
// Memory Usage = 0.003 MB for 4-point rectangle (triangle strip) vertex buffer
#define RECTANGLE_BUFFER_SIZE 0x20000
//------------------------------------------------------------------------
// Memory Usage = 0.003 MB for 6-point quad vertex buffer (Citra only)
#define CITRA_VERTEX_BUFFER_SIZE 0x200000
// Memory Usage = Not used (Real 3DS only)
#define CITRA_TILE_BUFFER_SIZE 0x200000
//------------------------------------------------------------------------
// Memory Usage = 0.003 MB for 6-point quad vertex buffer (Real 3DS only)
#define REAL3DS_VERTEX_BUFFER_SIZE 0x10000
// Memory Usage = 0.003 MB for 2-point rectangle vertex buffer (Real 3DS only)
#define REAL3DS_TILE_BUFFER_SIZE 0x200000
//---------------------------------------------------------
// Our textures
//---------------------------------------------------------
SGPUTexture *emuMainScreenHWTarget;
SGPUTexture *emuMainScreenTarget[2];
SGPUTexture *emuTileCacheTexture;
SGPUTexture *emuDepthForScreens;
//SGPUTexture *nesDepthForOtherTextures;
uint32 *bufferRGBA[2];
unsigned char linecolor[256];
//---------------------------------------------------------
// Settings related to the emulator.
//---------------------------------------------------------
extern SSettings3DS settings3DS;
//---------------------------------------------------------
// Provide a comma-separated list of file extensions
//---------------------------------------------------------
char *impl3dsRomExtensions = "pce,cue";
//---------------------------------------------------------
// The title image .PNG filename.
//---------------------------------------------------------
char *impl3dsTitleImage = "./temperpce_3ds_top.png";
//---------------------------------------------------------
// The title that displays at the bottom right of the
// menu.
//---------------------------------------------------------
char *impl3dsTitleText = "TemperPCE for 3DS v0.9";
int soundSamplesPerGeneration = 0;
int soundSamplesPerSecond = 0;
short soundSamples[1000];
int audioFrame = 0;
int emulatorFrame = 0;
//---------------------------------------------------------
// Initializes the emulator core.
//---------------------------------------------------------
bool impl3dsInitializeCore()
{
// compute a sample rate closes to 44100 kHz.
//
int numberOfGenerationsPerSecond = 60 * 1;
soundSamplesPerGeneration = 44100 / numberOfGenerationsPerSecond;
soundSamplesPerSecond = soundSamplesPerGeneration * numberOfGenerationsPerSecond;
audio.output_frequency = soundSamplesPerSecond;
config.per_game_bram = true;
snprintf(config.main_path, MAX_PATH, ".");
initialize_video();
initialize_memory();
initialize_io();
initialize_irq();
initialize_timer();
initialize_psg();
initialize_cpu();
initialize_cd();
initialize_adpcm();
initialize_arcade_card();
initialize_debug();
// Initialize our GPU.
// Load up and initialize any shaders
//
if (emulator.isReal3DS)
{
gpu3dsLoadShader(0, (u32 *)shaderslow_shbin, shaderslow_shbin_size, 0); // copy to screen
gpu3dsLoadShader(1, (u32 *)shaderfast2_shbin, shaderfast2_shbin_size, 6); // draw tiles
}
else
{
gpu3dsLoadShader(0, (u32 *)shaderslow_shbin, shaderslow_shbin_size, 0); // copy to screen
gpu3dsLoadShader(1, (u32 *)shaderslow2_shbin, shaderslow2_shbin_size, 0); // draw tiles
}
gpu3dsInitializeShaderRegistersForRenderTarget(0, 10);
gpu3dsInitializeShaderRegistersForTexture(4, 14);
gpu3dsInitializeShaderRegistersForTextureOffset(6);
// Create all the necessary textures
//
emuTileCacheTexture = gpu3dsCreateTextureInLinearMemory(1024, 1024, GPU_RGBA5551);
// Main screen
emuMainScreenHWTarget = gpu3dsCreateTextureInVRAM(512, 256, GPU_RGBA8); // 0.250 MB
emuMainScreenTarget[0] = gpu3dsCreateTextureInLinearMemory(512, 256, GPU_RGBA8); // 0.250 MB
emuMainScreenTarget[1] = gpu3dsCreateTextureInLinearMemory(512, 256, GPU_RGBA8); // 0.250 MB
// Depth textures, if required
//
emuDepthForScreens = gpu3dsCreateTextureInVRAM(512, 256, GPU_RGBA8); // 0.250 MB
//nesDepthForOtherTextures = gpu3dsCreateTextureInVRAM(256, 256, GPU_RGBA8); // 0.250 MB
bufferRGBA[0] = linearMemAlign(512*256*4, 0x80);
bufferRGBA[1] = linearMemAlign(512*256*4, 0x80);
if (emuTileCacheTexture == NULL ||
emuMainScreenHWTarget == NULL ||
emuMainScreenTarget[0] == NULL ||
emuMainScreenTarget[1] == NULL ||
emuDepthForScreens == NULL /*||
nesDepthForOtherTextures == NULL*/)
{
printf ("Unable to allocate textures\n");
return false;
}
// allocate all necessary vertex lists
//
if (emulator.isReal3DS)
{
gpu3dsAllocVertexList(&GPU3DSExt.rectangleVertexes, RECTANGLE_BUFFER_SIZE, sizeof(SVertexColor), 2, SVERTEXCOLOR_ATTRIBFORMAT);
gpu3dsAllocVertexList(&GPU3DSExt.quadVertexes, REAL3DS_VERTEX_BUFFER_SIZE, sizeof(SVertexTexCoord), 2, SVERTEXTEXCOORD_ATTRIBFORMAT);
gpu3dsAllocVertexList(&GPU3DSExt.tileVertexes, REAL3DS_TILE_BUFFER_SIZE, sizeof(STileVertex), 2, STILETEXCOORD_ATTRIBFORMAT);
}
else
{
gpu3dsAllocVertexList(&GPU3DSExt.rectangleVertexes, RECTANGLE_BUFFER_SIZE, sizeof(SVertexColor), 2, SVERTEXCOLOR_ATTRIBFORMAT);
gpu3dsAllocVertexList(&GPU3DSExt.quadVertexes, CITRA_VERTEX_BUFFER_SIZE, sizeof(SVertexTexCoord), 2, SVERTEXTEXCOORD_ATTRIBFORMAT);
gpu3dsAllocVertexList(&GPU3DSExt.tileVertexes, CITRA_TILE_BUFFER_SIZE, sizeof(STileVertex), 2, STILETEXCOORD_ATTRIBFORMAT);
}
if (GPU3DSExt.quadVertexes.ListBase == NULL ||
GPU3DSExt.tileVertexes.ListBase == NULL ||
GPU3DSExt.rectangleVertexes.ListBase == NULL)
{
printf ("Unable to allocate vertex list buffers \n");
return false;
}
gpu3dsUseShader(0);
return true;
}
//---------------------------------------------------------
// Finalizes and frees up any resources.
//---------------------------------------------------------
void impl3dsFinalize()
{
if (emuMainScreenHWTarget) gpu3dsDestroyTextureFromVRAM(emuMainScreenHWTarget);
if (emuTileCacheTexture) gpu3dsDestroyTextureFromLinearMemory(emuTileCacheTexture);
if (emuMainScreenTarget[0]) gpu3dsDestroyTextureFromLinearMemory(emuMainScreenTarget[0]);
if (emuMainScreenTarget[1]) gpu3dsDestroyTextureFromLinearMemory(emuMainScreenTarget[1]);
if (emuDepthForScreens) gpu3dsDestroyTextureFromVRAM(emuDepthForScreens);
//if (nesDepthForOtherTextures) gpu3dsDestroyTextureFromVRAM(nesDepthForOtherTextures);
if (bufferRGBA[0]) linearFree(bufferRGBA[0]);
if (bufferRGBA[1]) linearFree(bufferRGBA[1]);
}
//---------------------------------------------------------
// Mix sound samples into a temporary buffer.
//
// This gives time for the sound generation to execute
// from the 2nd core before copying it to the actual
// output buffer.
//---------------------------------------------------------
void impl3dsGenerateSoundSamples()
{
render_psg(soundSamplesPerGeneration);
}
//---------------------------------------------------------
// Mix sound samples into a temporary buffer.
//
// This gives time for the sound generation to execute
// from the 2nd core before copying it to the actual
// output buffer.
//
// For a console with only MONO output, simply copy
// the samples into the leftSamples buffer.
//---------------------------------------------------------
void impl3dsOutputSoundSamples(short *leftSamples, short *rightSamples)
{
static int volumeMul[] = { 16, 20, 24, 28, 32, 40, 48, 56, 64 };
int volume = volumeMul[settings3DS.Volume];
int cd_write_index = cd.cdda_audio_buffer_index;
int cd_read_index = cd.cdda_audio_read_buffer_index;
int cd_write_index_diff = cd_write_index - cd_read_index;
if (cd_write_index_diff > soundSamplesPerGeneration * 2)
cd_write_index_diff = soundSamplesPerGeneration * 2;
if (cd_write_index_diff < 0)
cd_write_index_diff += CD_AUDIO_BUFFER_SIZE;
cd_write_index_diff /= 2;
int cd_read_ctr = 0;
int adpcm_write_index = adpcm.audio_buffer_index;
int adpcm_read_index = adpcm.audio_read_buffer_index;
int adpcm_write_index_diff = adpcm_write_index - adpcm_read_index;
if (adpcm_write_index_diff > soundSamplesPerGeneration * 2)
adpcm_write_index_diff = soundSamplesPerGeneration * 2;
if (adpcm_write_index_diff < 0)
adpcm_write_index_diff += ADPCM_AUDIO_BUFFER_SIZE;
adpcm_write_index_diff /= 2;
int adpcm_read_ctr = 0;
for (int i = 0; i < soundSamplesPerGeneration; i++)
{
int leftSample = 0;
int rightSample = 0;
leftSample = (audio.buffer[i * 2] >> 5) * volume / 16;
rightSample = (audio.buffer[i * 2 + 1] >> 5) * volume / 16;
if (config.cd_loaded)
{
if (cd_write_index_diff)
{
leftSample += (cd.audio_buffer[cd_read_index] >> 5) * volume / 16;
rightSample += (cd.audio_buffer[cd_read_index + 1] >> 5) * volume / 16;
cd_read_ctr += cd_write_index_diff;
// We slow down the sample output, if the sound generation
// is too fast.
//
// This may cause the pitch to go a bit off for some
// frames, but it helps to sound smoother.
//
if (cd_read_ctr >= soundSamplesPerGeneration)
{
cd_read_index = (cd_read_index + 2) & (CD_AUDIO_BUFFER_SIZE - 1);
cd_read_ctr -= soundSamplesPerGeneration;
}
}
if (adpcm_write_index_diff)
{
leftSample += (adpcm.audio_buffer[adpcm_read_index] >> 5) * volume / 16;
rightSample += (adpcm.audio_buffer[adpcm_read_index + 1] >> 5) * volume / 16;
adpcm_read_ctr += adpcm_write_index_diff;
// We slow down the sample output, if the sound generation
// is too fast.
//
// This may cause the pitch to go a bit off for some
// frames, but it helps to sound smoother.
//
if (adpcm_read_ctr >= soundSamplesPerGeneration)
{
adpcm_read_index = (adpcm_read_index + 2) & (ADPCM_AUDIO_BUFFER_SIZE - 1);
adpcm_read_ctr -= soundSamplesPerGeneration;
}
}
}
if(leftSample > 32767)
leftSample = 32767;
if(leftSample < -32768)
leftSample = -32768;
leftSamples[i] = leftSample;
if(rightSample > 32767)
rightSample = 32767;
if(rightSample < -32768)
rightSample = -32768;
rightSamples[i] = rightSample;
}
audioFrame++;
if (config.cd_loaded)
{
cd.cdda_audio_read_buffer_index = cd_read_index;
adpcm.audio_read_buffer_index = adpcm_read_index;
}
}
extern VerticalSections screenWidthVerticalSection;
char bramSaveFilepath[_MAX_PATH] = "";
//---------------------------------------------------------
// This is called when a ROM needs to be loaded and the
// emulator engine initialized.
//---------------------------------------------------------
bool impl3dsLoadROM(char *romFilePath)
{
//config.patch_idle_loops = 0;
strncpy(bramSaveFilepath, file3dsReplaceFilenameExtension(romFileNameFullPath, ".sav"), _MAX_PATH - 1);
if (load_rom(romFilePath) == -1)
return false;
SMenuItem *menuItem = menu3dsGetMenuItemByID(1, 21000);
if (menuItem != NULL)
{
if (config.cd_loaded)
menuItem->Type = MENUITEM_PICKER;
else
menuItem->Type = MENUITEM_DISABLED;
}
impl3dsResetConsole();
snd3dsSetSampleRate(
true,
soundSamplesPerSecond,
soundSamplesPerGeneration,
true);
return true;
}
//---------------------------------------------------------
// This is called to determine what the frame rate of the
// game based on the ROM's region.
//---------------------------------------------------------
int impl3dsGetROMFrameRate()
{
return 60;
}
//---------------------------------------------------------
// This is called when the user chooses to reset the
// console
//---------------------------------------------------------
void impl3dsResetConsole()
{
cache3dsInit();
reset_video();
reset_video_hw();
reset_memory();
reset_io();
reset_irq();
reset_timer();
reset_psg();
reset_cpu();
reset_cd();
reset_adpcm();
reset_arcade_card();
reset_debug();
}
//---------------------------------------------------------
// This is called when preparing to start emulating
// a new frame. Use this to do any preparation of data,
// the hardware, swap any vertex list buffers, etc,
// before the frame is emulated
//---------------------------------------------------------
void impl3dsPrepareForNewFrame()
{
gpu3dsSwapVertexListForNextFrame(&GPU3DSExt.quadVertexes);
gpu3dsSwapVertexListForNextFrame(&GPU3DSExt.tileVertexes);
gpu3dsSwapVertexListForNextFrame(&GPU3DSExt.rectangleVertexes);
}
bool isOddFrame = false;
bool skipDrawingPreviousFrame = true;
uint32 *bufferToTransfer = 0;
SGPUTexture *screenTexture = 0;
//---------------------------------------------------------
// Initialize any variables or state of the GPU
// before the emulation loop begins.
//---------------------------------------------------------
void impl3dsEmulationBegin()
{
audioFrame = 0;
emulatorFrame = 0;
bufferToTransfer = 0;
screenTexture = 0;
skipDrawingPreviousFrame = true;
gpu3dsUseShader(0);
gpu3dsDisableAlphaBlending();
gpu3dsDisableDepthTest();
gpu3dsDisableAlphaTest();
gpu3dsDisableStencilTest();
gpu3dsSetTextureEnvironmentReplaceTexture0();
gpu3dsSetRenderTargetToTopFrameBuffer();
gpu3dsFlush();
//if (emulator.isReal3DS)
// gpu3dsWaitForPreviousFlush();
}
u32 prevConsoleJoyPad;
u32 prevConsoleButtonPressed[6];
u32 buttons3dsPressed[6];
void impl3dsEmulationPollInput()
{
u32 keysHeld3ds = input3dsGetCurrentKeysHeld();
u32 consoleJoyPad = 0;
if (keysHeld3ds & KEY_UP) consoleJoyPad |= IO_BUTTON_UP;
if (keysHeld3ds & KEY_DOWN) consoleJoyPad |= IO_BUTTON_DOWN;
if (keysHeld3ds & KEY_LEFT) consoleJoyPad |= IO_BUTTON_LEFT;
if (keysHeld3ds & KEY_RIGHT) consoleJoyPad |= IO_BUTTON_RIGHT;
if (keysHeld3ds & KEY_SELECT) consoleJoyPad |= IO_BUTTON_SELECT;
if (keysHeld3ds & KEY_START) consoleJoyPad |= IO_BUTTON_RUN;
buttons3dsPressed[BTN3DS_L] = (keysHeld3ds & KEY_L);
if (keysHeld3ds & KEY_L) consoleJoyPad |= settings3DS.ButtonMapping[BTN3DS_L];
buttons3dsPressed[BTN3DS_R] = (keysHeld3ds & KEY_R);
if (keysHeld3ds & KEY_R) consoleJoyPad |= settings3DS.ButtonMapping[BTN3DS_R];
buttons3dsPressed[BTN3DS_A] = (keysHeld3ds & KEY_A);
if (keysHeld3ds & KEY_A) consoleJoyPad |= settings3DS.ButtonMapping[BTN3DS_A];
buttons3dsPressed[BTN3DS_B] = (keysHeld3ds & KEY_B);
if (keysHeld3ds & KEY_B) consoleJoyPad |= settings3DS.ButtonMapping[BTN3DS_B];
buttons3dsPressed[BTN3DS_X] = (keysHeld3ds & KEY_X);
if (keysHeld3ds & KEY_X) consoleJoyPad |= settings3DS.ButtonMapping[BTN3DS_X];
buttons3dsPressed[BTN3DS_Y] = (keysHeld3ds & KEY_Y);
if (keysHeld3ds & KEY_Y) consoleJoyPad |= settings3DS.ButtonMapping[BTN3DS_Y];
// Handle turbo / rapid fire buttons.
//
#define HANDLE_TURBO(i, mask) \
if (settings3DS.Turbo[i] && buttons3dsPressed[i]) { \
if (!prevConsoleButtonPressed[i]) \
{ \
prevConsoleButtonPressed[i] = 11 - settings3DS.Turbo[i]; \
} \
else \
{ \
prevConsoleButtonPressed[i]--; \
consoleJoyPad &= ~mask; \
} \
} \
HANDLE_TURBO(BTN3DS_L, settings3DS.ButtonMapping[BTN3DS_L]);
HANDLE_TURBO(BTN3DS_R, settings3DS.ButtonMapping[BTN3DS_R]);
HANDLE_TURBO(BTN3DS_A, settings3DS.ButtonMapping[BTN3DS_A]);
HANDLE_TURBO(BTN3DS_B, settings3DS.ButtonMapping[BTN3DS_B]);
HANDLE_TURBO(BTN3DS_X, settings3DS.ButtonMapping[BTN3DS_X]);
HANDLE_TURBO(BTN3DS_Y, settings3DS.ButtonMapping[BTN3DS_Y]);
prevConsoleJoyPad = consoleJoyPad;
io.button_status[0] = consoleJoyPad ^ 0xFFF;
}
//---------------------------------------------------------
// The following pipeline is used if the
// emulation engine does software rendering.
//
// You can potentially 'hide' the wait latencies by
// waiting only after some work on the main thread
// is complete.
//---------------------------------------------------------
int lastWait = 0;
#define WAIT_PPF 1
#define WAIT_P3D 2
int currentFrameIndex = 0;
void impl3dsRenderTransferSoftRenderedScreenToTexture(u32 *buffer, int textureIndex)
{
t3dsStartTiming(11, "FlushDataCache");
GSPGPU_FlushDataCache(buffer, 512*240*4);
t3dsEndTiming(11);
t3dsStartTiming(12, "DisplayTransfer");
GX_DisplayTransfer(
(uint32 *)(buffer), GX_BUFFER_DIM(512, 240),
(uint32 *)(emuMainScreenTarget[textureIndex]->PixelData), GX_BUFFER_DIM(512, 240),
GX_TRANSFER_OUT_TILED(1) |
GX_TRANSFER_FLIP_VERT(1) |
0
);
t3dsEndTiming(12);
}
void impl3dsRenderDrawTextureToTopFrameBuffer(SGPUTexture *texture, int tx_offset, int ty_offset)
{
t3dsStartTiming(14, "Draw Texture");
int scrWidth;
bool cropped = false;
/*
{
gpu3dsUseShader(1);
gpu3dsSetTextureEnvironmentReplaceColor();
gpu3dsDrawRectangle(0, 0, 400, 240, 0, 0xff00ffff);
gpu3dsSetRenderTargetToTexture(emuMainScreenHWTarget, emuDepthForScreens);
gpu3dsSetTextureEnvironmentReplaceTexture0();
gpu3dsBindTexture(emuTileCacheTexture, GPU_TEXUNIT0);
gpu3dsDisableAlphaTest();
gpu3dsDisableDepthTest();
static int counter = 0;
int texturePosition = 0;
int tx = texturePosition % 128;
int ty = (texturePosition / 128) & 0x7f;
texturePosition = (127 - ty) * 128 + tx; // flip vertically.
uint32 base = texturePosition * 64;
uint16 *tileTexture = (uint16 *)emuTileCacheTexture->PixelData;
u16 *dest = emuTileCacheTexture->PixelData;
for (int i = 0; i < 64; i++)
{
dest[base + i] = ((i + counter) & 0xffff) * 4;
}
counter++;
{
int x = 40;
int y = 50;
gpu3dsAddTileVertexes(
x, y, x + 8, y + 8,
0, 0,
8, 8, 0);
}
gpu3dsDrawVertexes();
}*/
// Draw a black colored rectangle covering the entire screen.
//
gpu3dsUseShader(1);
gpu3dsSetRenderTargetToTopFrameBuffer();
switch (settings3DS.ScreenStretch)
{
case 0:
scrWidth = 256;
break;
case 1:
scrWidth = 320;
break;
case 2:
scrWidth = 400;
break;
case 3:
scrWidth = 320;
cropped = true;
break;
case 4:
scrWidth = 400;
cropped = true;
break;
}
int sideBorderWidth = (400 - scrWidth) / 2;
gpu3dsSetTextureEnvironmentReplaceColor();
gpu3dsDrawRectangle(0, 0, sideBorderWidth, 240, 0, 0x000000ff);
gpu3dsDrawRectangle(400 - sideBorderWidth, 0, 400, 240, 0, 0x000000ff);
gpu3dsUseShader(0);
gpu3dsSetTextureEnvironmentReplaceTexture0();
gpu3dsBindTextureMainScreen(texture, GPU_TEXUNIT0);
/* // Software rendering:
gpu3dsAddQuadVertexes(
sideBorderWidth, 0, 400 - sideBorderWidth, 240,
0, 0,
256, 240, 0);
gpu3dsDrawVertexes();
*/
// Hardware rendering:
for (int i = 0; i < screenWidthVerticalSection.Count; i++)
{
int startY = screenWidthVerticalSection.Section[i].StartY;
int endY = screenWidthVerticalSection.Section[i].EndY;
int pceWidth = screenWidthVerticalSection.Section[i].Value;
int hds = screenWidthVerticalSection.Section[i].Value2;
int hsw = screenWidthVerticalSection.Section[i].Value4;
//printf ("width sect: %d (w:%3d s:%3d e:%3d) %3d to %3d\n", i, pceWidth, hds, hsw, startY, endY);
int xOffset = 0;
if (pceWidth == 0)
continue;
if (pceWidth < 256)
xOffset = (256 - pceWidth) / 2;
if (startY > endY)
continue;
float tx0 = 0;
float ty0 = startY;
float tx1 = tx0 + pceWidth, ty1 = endY + 1;
if (cropped)
{
tx0 += 8; ty0 += 8;
tx1 -= 8; ty1 -= 8;
}
if (scrWidth == 320)
{
tx0 += 0.2;
tx1 -= 0.2;
}
gpu3dsAddQuadVertexes(
sideBorderWidth + xOffset, startY, 400 - sideBorderWidth - xOffset, endY + 1,
tx0 + tx_offset, ty0 + ty_offset,
tx1 + tx_offset, ty1 + ty_offset, 0);
}
vsectReset(&screenWidthVerticalSection);
gpu3dsDrawVertexes();
//gpu3dsSetTextureEnvironmentReplaceTexture0();
//gpu3dsBindTextureMainScreen(emuTileCacheTexture, GPU_TEXUNIT0);
//gpu3dsAddQuadVertexes(0, 0, 200, 200, 0, 0, 256, 256, 0);
//gpu3dsDrawVertexes();
t3dsEndTiming(14);
}
//---------------------------------------------------------
// Executes one frame and draw to the screen.
//
// Note: TRUE will be passed in the firstFrame if this
// frame is to be run just after the emulator has booted
// up or returned from the menu.
//---------------------------------------------------------
void impl3dsEmulationRunOneFrame(bool firstFrame, bool skipDrawingFrame)
{
// Hardware rendering:
//
skipDrawingFrame = false;
t3dsStartTiming(1, "RunOneFrame");
t3dsStartTiming(10, "EmulateFrame");
{
impl3dsEmulationPollInput();
if (!skipDrawingFrame)
currentFrameIndex ^= 1;
gpu3dsUseShader(1);
gpu3dsSetRenderTargetToTexture(emuMainScreenHWTarget, emuDepthForScreens);
update_frame(skipDrawingFrame);
if (config.cd_loaded)
update_cdda();
emulatorFrame++;
#define SYNC_SAMPLES (735 * 8)
emulator.waitBehavior = WAIT_FULL;
if (config.cd_loaded)
{
// If we are running too slowly for the CD / ADPCM audio,
// we will have to ask the main loop to avoid waits.
//
if (cd.has_samples)
{
int cd_write_index_diff = cd.cdda_audio_buffer_index - cd.cdda_audio_read_buffer_index;
if (cd_write_index_diff < 0)
cd_write_index_diff += CD_AUDIO_BUFFER_SIZE;
if (cd_write_index_diff < SYNC_SAMPLES)
emulator.waitBehavior = WAIT_NONE;
}
if (adpcm.has_samples)
{
int adpcm_write_index_diff = cd.cdda_audio_buffer_index - cd.cdda_audio_read_buffer_index;
if (adpcm_write_index_diff < 0)
adpcm_write_index_diff += ADPCM_AUDIO_BUFFER_SIZE;
if (adpcm_write_index_diff < SYNC_SAMPLES)
emulator.waitBehavior = WAIT_NONE;
}
//if (emulator.waitBehavior == WAIT_NONE)
// printf ("WAIT_NONE\n");
cd.has_samples = false;
adpcm.has_samples = false;
}
/*
if (emulatorFrame % 5 == 0)
{
//printf ("af=%d ef=%d diff=%d bf=%d,%d\n", audioFrame, emulatorFrame, emulatorFrame - audioFrame, cd.cdda_audio_read_buffer_index, adpcm.audio_buffer_index);
int cd_diff = cd.cdda_audio_buffer_index - cd.cdda_audio_read_buffer_index;
if (cd_diff < 0)
cd_diff += CD_AUDIO_BUFFER_SIZE;
printf ("adpcm: %d,%d cd:%d,%d (%d)\n", adpcm.audio_read_buffer_index, adpcm.audio_buffer_index, cd.cdda_audio_read_buffer_index, cd.cdda_audio_buffer_index, cd_diff);
}
*/
gpu3dsDrawVertexes();
// debugging only
/*render_psg(soundSamplesPerGeneration * 2);
for (int i = 0; i < soundSamplesPerGeneration * 2; i += 2)
{
printf ("%4x", (u16)audio.buffer[i*2]);
}
printf ("---------------------------\n\n");
*/
/*printf ("%10llx - %10llx = %10lld\n",
cpu.global_cycles, psg.cpu_sync_cycles >> step_fractional_bits_clock,
cpu.global_cycles - (psg.cpu_sync_cycles >> step_fractional_bits_clock));
*/
}
t3dsEndTiming(10);
if (!skipDrawingFrame)
{
impl3dsRenderDrawTextureToTopFrameBuffer(emuMainScreenHWTarget, 32, 0);
}
if (!skipDrawingPreviousFrame)
{
t3dsStartTiming(16, "Transfer");
gpu3dsTransferToScreenBuffer();