aboutsummaryrefslogtreecommitdiff
path: root/source/nds/entry.cpp
blob: 80b1221f809e5e6dbcc30b49ca0d45b166531fb3 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
//entry.c
#include <stdio.h>

#include "ds2_types.h"
#include "ds2_cpu.h"
#include "ds2_timer.h"
#include "ds2io.h"
#include "fs_api.h"

#include "snes9x.h"
#include "soundux.h"
#include "memmap.h"
#include "apu.h"
#include "cheats.h"
#include "snapshot.h"
#include "display.h"
#include "gfx.h"
#include "cpuexec.h"
#include "spc7110.h"

#include "draw.h"
#include "gui.h"
#include "ds2sound.h"

void S9xProcessSound (unsigned int);
void NDSSFCProduceSound (unsigned int);

char *rom_filename = NULL;
char *SDD1_pack = NULL;

static u8 Buf[MAX_BUFFER_SIZE];

#define FIXED_POINT 0x10000
#define FIXED_POINT_SHIFT 16
#define FIXED_POINT_REMAINDER 0xffff

static volatile bool8 block_signal = FALSE;
static volatile bool8 block_generate_sound = FALSE;
static volatile bool8 pending_signal = FALSE;

static void Init_Timer (void);

void S9xMessage (int /*type*/, int /*number*/, const char *message)
{
#if 1
#define MAX_MESSAGE_LEN (36 * 3)

    static char buffer [MAX_MESSAGE_LEN + 1];

    printf ("%s\n", message);
    strncpy (buffer, message, MAX_MESSAGE_LEN);
    buffer [MAX_MESSAGE_LEN] = 0;
    S9xSetInfoString (buffer);
#endif
}

void S9xExtraUsage ()
{
    /*empty*/
}

/*
*   Release display device
*/
void S9xDeinitDisplay (void)
{
    if(GFX.Screen) free(GFX.Screen);
    if(GFX.SubScreen) free(GFX.SubScreen);
    if(GFX.ZBuffer) free(GFX.ZBuffer);
    if(GFX.SubZBuffer) free(GFX.SubZBuffer);
}

void S9xInitDisplay (int, char **)
{
    int h = IMAGE_HEIGHT;

	GFX.Pitch = IMAGE_WIDTH * 2;
	GFX.Screen =	(unsigned char*) malloc (GFX.Pitch * h);
	GFX.SubScreen = (unsigned char*) malloc (GFX.Pitch * h);
	GFX.ZBuffer = 	(unsigned char*) malloc ((GFX.Pitch >> 1) * h);
	GFX.SubZBuffer =(unsigned char*) malloc ((GFX.Pitch >> 1) * h);
	GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
}

void S9xParseArg (char **argv, int &i, int argc)
{
}

void S9xParseDisplayArg (char **argv, int &ind, int)
{
}

void S9xExit ()
{
  ds2_setCPUclocklevel(13); // Crank it up to exit quickly
  if(Settings.SPC7110)
    (*CleanUp7110)();

	S9xSetSoundMute (TRUE);
    S9xDeinitDisplay ();
    Memory.SaveSRAM (S9xGetFilename (".srm"));
    // S9xSaveCheatFile (S9xGetFilename (".chb")); // cheat binary file
	// Do this when loading a cheat file!
    Memory.Deinit ();
    S9xDeinitAPU ();

#ifdef _NETPLAY_SUPPORT
    if (Settings.NetPlay)
	S9xNetPlayDisconnect ();
#endif

	exit(0);
}

const char *S9xBasename (const char *f)
{
    const char *p;
    if ((p = strrchr (f, '/')) != NULL || (p = strrchr (f, '\\')) != NULL)
	return (p + 1);

    return (f);
}

bool8 S9xInitUpdate ()
{
    return (TRUE);
}


extern void NDSSFCDrawFrameAntialiased();
	

bool8 S9xDeinitUpdate (int Width, int Height, bool8 /*sixteen_bit*/)
{
	switch(game_config.graphic)
	{
		//Up
		case 1:
		    memcpy(up_screen_addr, GFX.Screen+256*32*2, 256*192*2);
			break;

		//Down
		case 2:
		    memcpy(up_screen_addr, GFX.Screen, 256*192*2);
			break;

		//Both
		case 3:
		    memcpy(up_screen_addr, GFX.Screen+256*16*2, 256*192*2);
			break;
			
		case 4:
			NDSSFCDrawFrameAntialiased ();
		break;
		

		default:
		{
			unsigned char *src, *dst;
			unsigned int m, n;

			src = GFX.Screen;
			dst = (unsigned char*)up_screen_addr;
			for(m = 0; m < 32; m++)
			{
				memcpy(dst, src, 256*6*2);
				dst += 256*6*2;
				src += 256*7*2;
			}
		}
			break;
	}

	ds2_flipScreen(UP_SCREEN, UP_SCREEN_UPDATE_METHOD);
	// A problem with update method 1 (wait, double buffer) means that, after
	// about 15 minutes of play time, the screen starts to half-redraw every
	// frame. With update method 0, this is mitigated. (Method 2 is too slow.)

	return (TRUE);
}

void _makepath (char *path, const char *, const char *dir,
		const char *fname, const char *ext)
{
    if (dir && *dir)
    {
	strcpy (path, dir);
	strcat (path, "/");
    }
    else
	*path = 0;
    strcat (path, fname);
    if (ext && *ext)
    {
        strcat (path, ".");
        strcat (path, ext);
    }
}

void _splitpath (const char *path, char *drive, char *dir, char *fname,
		 char *ext)
{
    *drive = 0;

    char *slash = strrchr (path, '/');
    if (!slash)
	slash = strrchr (path, '\\');

    char *dot = strrchr (path, '.');

    if (dot && slash && dot < slash)
	dot = NULL;

    if (!slash)
    {
	strcpy (dir, "");
	strcpy (fname, path);
        if (dot)
        {
	    *(fname + (dot - path)) = 0;
	    strcpy (ext, dot + 1);
        }
	else
	    strcpy (ext, "");
    }
    else
    {
	strcpy (dir, path);
	*(dir + (slash - path)) = 0;
	strcpy (fname, slash + 1);
        if (dot)
	{
	    *(fname + (dot - slash) - 1) = 0;
    	    strcpy (ext, dot + 1);
	}
	else
	    strcpy (ext, "");
    }
}

void S9xProcessEvents (bool8 block)
{

}

void OutOfMemory ()
{
}


const char *S9xGetROMDirectory ()
{
	return ((const char*)g_default_rom_dir);
}


const char *S9xGetSnapshotDirectory ()
{
    return ((const char*)DEFAULT_RTS_DIR);
}

const char *S9xGetFilename (const char *ex)
{
    static char filename [PATH_MAX + 1];
    char drive [_MAX_DRIVE + 1];
    char dir [_MAX_DIR + 1];
    char fname [_MAX_FNAME + 1];
    char ext [_MAX_EXT + 1];

    _splitpath (Memory.ROMFilename, drive, dir, fname, ext);
    strcpy (filename, S9xGetSnapshotDirectory ());
    strcat (filename, SLASH_STR);
    strcat (filename, fname);
    strcat (filename, ex);

    return (filename);
}
 
const char *S9xGetFilenameInc (const char *e)
{
    return e;
#if 0
    static char filename [_MAX_PATH + 1];
    char drive [_MAX_DRIVE + 1];
    char dir [_MAX_DIR + 1];
    char fname [_MAX_FNAME + 1];
    char ext [_MAX_EXT + 1];
    char *ptr;
    struct stat buf;

    if (strlen (S9xGetSnapshotDirectory()))
    {
        _splitpath (Memory.ROMFilename, drive, dir, fname, ext);
        strcpy (filename, S9xGetSnapshotDirectory());
        strcat (filename, "/");
        strcat (filename, fname);
        ptr = filename + strlen (filename);
        strcat (filename, "00/");
        strcat (filename, e);
    }
    else
    {
        _splitpath (Memory.ROMFilename, drive, dir, fname, ext);
        strcat (fname, "00/");
        _makepath (filename, drive, dir, fname, e);
        ptr = strstr (filename, "00/");
    }

    do
    {
        if (++*(ptr + 2) > '9')
        {
            *(ptr + 2) = '0';
            if (++*(ptr + 1) > '9')
            {
                *(ptr + 1) = '0';
                if (++*ptr > '9')
                    break;
            }
        }
    } while( stat(filename, &buf) == 0 );

    return (filename);
#endif
}

void S9xInitInputDevices ()
{
#ifdef JOYSTICK_SUPPORT
    InitJoysticks ();
#endif
}



void game_disableAudio()
{
	if( game_enable_audio == 1)
	{
		S9xSetSoundMute (FALSE);
	}
	else
	{
		S9xSetSoundMute (TRUE);
	}
}

void game_set_frameskip()
{
	if( game_config.frameskip_value == 0)
	{
		Settings.SkipFrames = AUTO_FRAMERATE;
	}
	else
	{
		Settings.SkipFrames = game_config.frameskip_value - 1 /* 1 -> 0 and so on */;
	}
}
	
void init_sfc_setting(void)
{
    ZeroMemory (&Settings, sizeof (Settings));
#ifdef JOYSTICK_SUPPORT
    Settings.JoystickEnabled = TRUE;
#else
    Settings.JoystickEnabled = FALSE;
#endif

    Settings.SoundPlaybackRate = SNES9X_SRATE_ID;	// -> ds2sound.h for defs
    Settings.Stereo = TRUE;
    Settings.SoundBufferSize = DS2_BUFFER_SIZE;
    Settings.CyclesPercentage = 100;
    Settings.DisableSoundEcho = FALSE;
	//sound settings
    Settings.APUEnabled = Settings.NextAPUEnabled = TRUE;
	Settings.FixFrequency = 1;


    Settings.H_Max = SNES_CYCLES_PER_SCANLINE;
    Settings.SkipFrames = AUTO_FRAMERATE;
    Settings.ShutdownMaster = TRUE;
    Settings.FrameTimePAL = 20000;
    Settings.FrameTimeNTSC = 16667;
    Settings.DisableSampleCaching = FALSE;
    Settings.DisableMasterVolume = FALSE;
    Settings.Mouse = TRUE;
    Settings.SuperScope = TRUE;
    Settings.MultiPlayer5 = TRUE;
    Settings.ControllerOption = SNES_JOYPAD;

    Settings.Transparency = TRUE;
#ifndef FOREVER_16_BIT
    Settings.SixteenBit = TRUE;
#endif

    Settings.SupportHiRes = FALSE;
    Settings.ThreadSound = FALSE;
	Settings.SoundSync = TRUE;
    Settings.AutoSaveDelay = 0;
#ifdef _NETPLAY_SUPPORT
    Settings.NetPlay = FALSE;
    Settings.ServerName [0] = 0;
    Settings.Port = NP_DEFAULT_PORT;
#endif
    Settings.ApplyCheats = TRUE;
    Settings.TurboMode = FALSE;
    Settings.TurboSkipFrames = 40;
    Settings.StretchScreenshots = 1;

	Settings.HBlankStart = (256 * Settings.H_Max) / SNES_HCOUNTER_MAX;
}

extern "C" {
	int game_load_state(char* file);
	int game_save_state(char* file);
	void S9xAutoSaveSRAM ();
}

void S9xAutoSaveSRAM ()
{
    Memory.SaveSRAM (S9xGetFilename (".srm"));
}

int game_load_state(char* file)
{
	int flag;

	flag = 0;
	if(S9xUnfreezeGame(file) == FALSE)
		flag = -1;

	return flag;
}

int game_save_state(char* file)
{
	int flag;

	flag = 0;
	if(S9xFreezeGame(file) == FALSE)
		flag = -1;

	S9xAutoSaveSRAM ();

	return flag;
}

extern "C" void game_restart(void);

void game_restart(void)
{
	CPU.Flags = 0;
	S9xReset ();
}

extern "C" int load_gamepak(char* file);

int load_gamepak(char* file)
{
	CPU.Flags = 0;
	// mdelay(50); // Delete this delay
	if (!Memory.LoadROM (file))
		return -1;
	S9xReset ();

	Settings.FrameTime = (Settings.PAL ? Settings.FrameTimePAL : Settings.FrameTimeNTSC);

	Memory.LoadSRAM (S9xGetFilename (".srm"));
	// mdelay(50); // Delete this delay
	S9xLoadCheatFile (S9xGetFilename (".chb")); // cheat binary file, as opposed to text

#ifdef _NETPLAY_SUPPORT
    if (strlen (Settings.ServerName) == 0)
    {
	char *server = getenv ("S9XSERVER");
	if (server)
	{
	    strncpy (Settings.ServerName, server, 127);
	    Settings.ServerName [127] = 0;
	}
    }
    char *port = getenv ("S9XPORT");
    if (Settings.Port >= 0 && port)
    	Settings.Port = atoi (port);
    else if (Settings.Port < 0)
        Settings.Port = -Settings.Port;

    if (Settings.NetPlay)
    {
	int player;

	if (!S9xNetPlayConnectToServer (Settings.ServerName, Settings.Port,
					Memory.ROMName, player))
	{
	    fprintf (stderr, "Failed to connected to Snes9x netplay"
                     " server \"%s\" on port %d.\n",
                     Settings.ServerName, Settings.Port);
	    S9xExit ();
	}
	fprintf (stderr, "Connected to \"%s\" on port %d as"
                 " player #%d playing \"%s\"\n",
		 Settings.ServerName, Settings.Port, player, Memory.ROMName);
    }
    
#endif
/*
    if (snapshot_filename)
    {
        int Flags = CPU.Flags & (DEBUG_MODE_FLAG | TRACE_FLAG);
        if (!S9xLoadSnapshot (snapshot_filename))
            exit (1);
        CPU.Flags |= Flags;
    }
*/

	// mdelay(50); // Delete this delay

	return 0;
}

extern "C" int sfc_main (int argc, char **argv);

int sfc_main (int argc, char **argv)
{
	//Initialize GUI
	gui_init(0);

	init_sfc_setting();

    if (!Memory.Init () || !S9xInitAPU())
		OutOfMemory ();

	S9xInitDisplay (argc, argv);
    if (!S9xGraphicsInit())
		OutOfMemory ();

    S9xInitSound (Settings.SoundPlaybackRate, Settings.Stereo,
                  Settings.SoundBufferSize);
	// Start a timer for the sound
	initTimer(0 /* timer channel, 0 or 1 */,
	          INTERRUPT_TIME /* period in microseconds */,
	          NDSSFCProduceSound /* timer function, void (unsigned int) */,
	          0 /* programmer-specified argument to ^ */);
	runTimer(0 /* timer channel, 0 or 1 */);

#ifdef GFX_MULTI_FORMAT
//    S9xSetRenderPixelFormat (RGB565);
    S9xSetRenderPixelFormat (BGR555);
#endif

#ifdef JOYSTICK_SUPPORT
    uint32 JoypadSkip = 0;
#endif

//    Init_Timer ();

    /* FIXME: Is someone using this dead code, or should it go? */
#if 0
    {
	FILE *fs = fopen ("test.bin", "r");
	if (fs)
	{
	    memset (IAPU.RAM, 0, 1024 * 64);
	    int bytes = fread (IAPU.RAM + 1024, 1, 13, fs);
	    bytes = fread (IAPU.RAM + 1024, 1, 1024 * 63, fs);
	    fclose (fs);
#ifdef SPCTOOL
	    _FixSPC (1024, 0, 0, 0, 0, 0xff);
#else
	    IAPU.PC = IAPU.RAM + 1024;
#endif
	    APU.Flags ^= TRACE_FLAG;
	    extern FILE *apu_trace;
	    if (APU.Flags & TRACE_FLAG)
	    {
#ifdef SPCTOOL
		printf ("ENABLED\n");
		_SetSPCDbg (TraceSPC);                   //Install debug handler
#endif
		if (apu_trace == NULL)
		    apu_trace = fopen ("aputrace.log", "wb");
	    }
	    CPU.Cycles = 1024 * 10;
	    APU_EXECUTE ();
	    exit (0);
	}
    }
#endif

	Settings.Paused = 1;

    while (1)
    {
		if (!Settings.Paused
#ifdef DEBUGGER
			|| (CPU.Flags & (DEBUG_MODE_FLAG | SINGLE_STEP_FLAG)
#endif
			)
			S9xMainLoop ();


#ifdef DEBUGGER
		if (CPU.Flags & DEBUG_MODE_FLAG)
		{
			S9xDoDebug ();
		}
		else
#endif
		if (Settings.Paused)
		{
			S9xSetSoundMute (TRUE);
			unsigned short screen[256*192];

			copy_screen((void*)screen, up_screen_addr, 0, 0, 256, 192);
			menu(screen);
			game_disableAudio();
			Settings.Paused = 0;
		}

#ifdef JOYSTICK_SUPPORT
		//if (Settings.JoystickEnabled && (JoypadSkip++ & 1) == 0)
		if (Settings.JoystickEnabled)
			ReadJoysticks ();
#endif

    }

    return (0);
}

static unsigned int sync_last= 0;
static unsigned int sync_next = 0;
static unsigned int auto_equivalent_skip = 0;

extern "C" u32 game_fast_forward;

static unsigned int skip_rate= 0;

void S9xSyncSpeed ()
{
	uint32 syncnow;
	int32 syncdif;

#if 0
    if (Settings.SoundSync == 2)
    {
	IPPU.RenderThisFrame = TRUE;
	IPPU.SkippedFrames = 0;
	return;
    }
#endif
	syncnow = getSysTime();

	if (game_fast_forward)
	{
		sync_last = syncnow;
		sync_next = syncnow;

		if(++skip_rate < 10)
			IPPU.RenderThisFrame = false;
		else
		{
			skip_rate = 0;
			IPPU.RenderThisFrame = true;
		}
	}
	else if (Settings.SkipFrames == AUTO_FRAMERATE /* && !game_fast_forward */)
	{
		// frame_time is in getSysTime units: 42.667 microseconds.
		uint32 frame_time = Settings.PAL ? 468 /* = 20.0 ms */ : 391 /* = 16.67 ms */;
		if (sync_last > syncnow) // Overflow occurred! (every 50 hrs)
		{
			// Render this frame regardless, set the
			// sync_next, and get the hell out.
			IPPU.RenderThisFrame = TRUE;
			sync_last = syncnow;
			sync_next = syncnow + frame_time;
			return;
		}
		sync_last = syncnow;
		// If this is positive, we have syncdif*42.66 microseconds to
		// spare.
		// If this is negative, we're late by syncdif*42.66
		// microseconds.
		syncdif = sync_next - syncnow;
		if (syncdif < 0 && syncdif >= -(frame_time / 2))
		{
			// We're late, but by less than half a frame. Draw it
			// anyway. If the next frame is too late, it'll be
			// skipped.
			skip_rate = 0;
			IPPU.RenderThisFrame = true;
			sync_next += frame_time;
		}
		else if(syncdif < 0)
		{
			/*
			 * If we're consistently late, delay up to 8 frames.
			 * 
			 * That really helps with certain games, such as
			 * Super Mario RPG and Yoshi's Island.
			 */
			if(++skip_rate < 10)
			{
				if(syncdif >= -11719 /* not more than 500.0 ms late */)
				{
					IPPU.RenderThisFrame = FALSE;
					sync_next += frame_time;
				}
				else
				{	//lag more than 0.5s, maybe paused
					IPPU.RenderThisFrame = TRUE;
					sync_next = syncnow + frame_time;
				}
			}
			else
			{
				skip_rate = 0;
				IPPU.RenderThisFrame = TRUE;
				sync_next = syncnow + frame_time;
			}
		}
		else // Early
		{
			skip_rate = 0;
			ds2_setCPUclocklevel(0);
			if (syncdif > 0)
				udelay(syncdif * 128 / 3 /* times 42 + 2/3 microseconds */);
			set_cpu_clock(clock_speed_number);
			S9xProcessSound (0);

			IPPU.RenderThisFrame = TRUE;
			sync_next += frame_time;
		}
#if 0
		if(++framenum >= 60)
		{
			syncdif = syncnow - sync_last;
			sync_last = syncnow;
			framenum = 0;
			//printf("T %d %d\n", syncdif*42667/1000, realframe);
			realframe = 0;
		}
#endif
	}
	else /* if (Settings.SkipFrames != AUTO_FRAMERATE && !game_fast_forward) */
	{
		// frame_time is in getSysTime units: 42.667 microseconds.
		uint32 frame_time = Settings.PAL ? 468 /* = 20.0 ms */ : 391 /* = 16.67 ms */;
		sync_last = syncnow;
		if (++skip_rate > Settings.SkipFrames)
		{
			skip_rate = 0;
			IPPU.RenderThisFrame = TRUE;
			// Are we early?
			syncdif = sync_next - syncnow;
			if (syncdif > 0)
			{
				udelay(syncdif * 128 / 3 /* times 42 + 2/3 microseconds */);
				S9xProcessSound (0);
				// After that little delay, what time is it?
				syncnow = getSysTime();
			}
			sync_next = syncnow + frame_time * (Settings.SkipFrames + 1);
		}
		else
		{
			IPPU.RenderThisFrame = FALSE;
		}
	}

#ifdef __sgi
    /* BS: saves on CPU usage */
    sginap(1);
#endif

#if 0
    /* Check events */
    
    static struct timeval next1 = {0, 0};
    struct timeval now;

    CHECK_SOUND();
//  S9xProcessEvents(FALSE);

    while (gettimeofday (&now, NULL) < 0) ;
    
    /* If there is no known "next" frame, initialize it now */
    if (next1.tv_sec == 0) { next1 = now; ++next1.tv_usec; }

    /* If we're on AUTO_FRAMERATE, we'll display frames always
     * only if there's excess time.
     * Otherwise we'll display the defined amount of frames.
     */
    unsigned limit = Settings.SkipFrames == AUTO_FRAMERATE
                     ? (timercmp(&next1, &now, <) ? 10 : 1)
                     : Settings.SkipFrames;
    
    IPPU.RenderThisFrame = ++IPPU.SkippedFrames >= limit;
    if(IPPU.RenderThisFrame)
    {
        IPPU.SkippedFrames = 0;
    }
    else
    {
        /* If we were behind the schedule, check how much it is */
        if(timercmp(&next1, &now, <))
        {
            unsigned lag =
                (now.tv_sec - next1.tv_sec) * 1000000
               + now.tv_usec - next1.tv_usec;
            if(lag >= 1000000)
            {
                /* More than a second behind means probably
                 * pause. The next line prevents the magic
                 * fast-forward effect.
                 */
                next1 = now;
            }
        }
    }
    
    /* Delay until we're completed this frame */

    /* Can't use setitimer because the sound code already could
     * be using it. We don't actually need it either.
     */

    while(timercmp(&next1, &now, >))
    {
        /* If we're ahead of time, sleep a while */
        unsigned timeleft =
            (next1.tv_sec - now.tv_sec) * 1000000
           + next1.tv_usec - now.tv_usec;
        //fprintf(stderr, "<%u>", timeleft);
        usleep(timeleft);

        CHECK_SOUND();
//  S9xProcessEvents(FALSE);

        while (gettimeofday (&now, NULL) < 0) ;
        /* Continue with a while-loop because usleep()
         * could be interrupted by a signal
         */
    }

    /* Calculate the timestamp of the next frame. */
    next1.tv_usec += Settings.FrameTime;
    if (next1.tv_usec >= 1000000)
    {
        next1.tv_sec += next1.tv_usec / 1000000;
        next1.tv_usec %= 1000000;
    }
#endif
}

bool8 S9xOpenSoundDevice (int mode, bool8 stereo, int buffer_size)
{
	so.sixteen_bit = TRUE;
    so.stereo = stereo;
    so.playback_rate = SND_SAMPLE_RATE;
    S9xSetPlaybackRate (so.playback_rate);

    if (buffer_size == 0)
	    buffer_size = DS2_BUFFER_SIZE;

    if (buffer_size > MAX_BUFFER_SIZE / 4)
	    buffer_size = MAX_BUFFER_SIZE / 4;
    if (so.sixteen_bit)
	    buffer_size *= 2;
    if (so.stereo)
	    buffer_size *= 2;

	so.buffer_size = buffer_size;

    return (TRUE);
}

/*
 * In the interrupt model, calls to NDSSFCProduceSound could happen even
 * when the CPU or APU is being reset or reloaded from a file. Prevent
 * glitches and/or crashes (!) from occurring while sound is not being
 * generated.
 */
bool8 IsSoundGenerated = FALSE;

void S9xGenerateSound ()
{
    int bytes_so_far = so.sixteen_bit ? (so.samples_mixed_so_far << 1) :
			so.samples_mixed_so_far;

	if (bytes_so_far >= so.buffer_size)
		return;

    block_signal = TRUE;

    so.err_counter += so.err_rate;
    if (so.err_counter >= FIXED_POINT)
    {
        int sample_count = so.err_counter >> FIXED_POINT_SHIFT;
		int byte_offset;
		int byte_count;

        so.err_counter &= FIXED_POINT_REMAINDER;
		if (so.stereo)
			sample_count <<= 1;
		byte_offset = bytes_so_far + so.play_position;

		do
		{
			int sc = sample_count;
			byte_count = sample_count;
			if (so.sixteen_bit)
				byte_count <<= 1;

			if ((byte_offset & SOUND_BUFFER_SIZE_MASK) + byte_count > SOUND_BUFFER_SIZE)
			{
				sc = SOUND_BUFFER_SIZE - (byte_offset & SOUND_BUFFER_SIZE_MASK);
				byte_count = sc;
				if (so.sixteen_bit)
					sc >>= 1;
			}

			if (bytes_so_far + byte_count > so.buffer_size)
			{
				byte_count = so.buffer_size - bytes_so_far;
				if (byte_count == 0)
					break;
				sc = byte_count;
				if (so.sixteen_bit)
					sc >>= 1;
			}

			S9xMixSamplesO (Buf, sc, byte_offset & SOUND_BUFFER_SIZE_MASK);
			so.samples_mixed_so_far += sc;
			sample_count -= sc;
			bytes_so_far = so.sixteen_bit ? (so.samples_mixed_so_far << 1) :
				so.samples_mixed_so_far;
			byte_offset += byte_count;
		} while (sample_count > 0);
    }

    block_signal = FALSE;
	IsSoundGenerated = TRUE;

    if (pending_signal)
    {
		// S9xProcessSound (0);
		NDSSFCProduceSound (0);
		pending_signal = FALSE;
    }
}

void S9xProcessSound (unsigned int)
{
#if 0
	unsigned short *audiobuff;

	if (so.mute_sound || !game_enable_audio)
		return;

	if(ds2_checkAudiobuff() > 4)
		return;

	/* Number of samples to generate now */
	int sample_count = so.buffer_size;

	if (so.sixteen_bit)
	{
		/* to prevent running out of buffer space,
		* create less samples
		*/
		sample_count >>= 1;
	}

	if (block_signal)
	{
		pending_signal = TRUE;
		return;
	}

//	block_generate_sound = TRUE;

	audiobuff = (unsigned short*)ds2_getAudiobuff();
	if(NULL == audiobuff)	//There are audio queue in sending or wait to send
	{
		return;
	}

	/* If we need more audio samples */
	if (so.samples_mixed_so_far < sample_count)
	{
		/* Where to put the samples to */
		unsigned byte_offset = so.play_position + 
		(so.sixteen_bit ? (so.samples_mixed_so_far << 1) : so.samples_mixed_so_far);

		//printf ("%d:", sample_count - so.samples_mixed_so_far); fflush (stdout);
		if (Settings.SoundSync == 2)
		{
			/*memset (Buf + (byte_offset & SOUND_BUFFER_SIZE_MASK), 0,
			sample_count - so.samples_mixed_so_far);*/
		}
		else
		{
			/* Mix the missing samples */
			S9xMixSamplesO (Buf, sample_count - so.samples_mixed_so_far,
				byte_offset & SOUND_BUFFER_SIZE_MASK);
		}
		so.samples_mixed_so_far = sample_count;
	}

//    if (!so.mute_sound)
	{
		unsigned bytes_to_write = sample_count;
		if(so.sixteen_bit) bytes_to_write <<= 1;

		unsigned byte_offset = so.play_position;
		so.play_position += bytes_to_write;
		so.play_position &= SOUND_BUFFER_SIZE_MASK; /* wrap to beginning */

//		block_generate_sound = FALSE;

		unsigned short *dst_pt = audiobuff;
		unsigned short *dst_pt1 = dst_pt + DS2_BUFFER_SIZE;

		/* Feed the samples to the soundcard until nothing is left */
		for(;;)
		{
			int I = bytes_to_write;
			if (byte_offset + I > SOUND_BUFFER_SIZE)
			{
				I = SOUND_BUFFER_SIZE - byte_offset;
			}
			if(I == 0) break;

//			memcpy(dst_pt, (char *) Buf + byte_offset, I);
//			dst_pt += I;

			unsigned short *src_pt= (unsigned short*)(Buf + byte_offset);
			for(int m= 0; m < I/4; m++)
			{
				*dst_pt++= *src_pt++;//(*src_pt++) <<1;
				*dst_pt1++= *src_pt++;//(*src_pt++) <<1;
			}

			bytes_to_write -= I;
			byte_offset += I;
			byte_offset &= SOUND_BUFFER_SIZE_MASK; /* wrap */
		}

		ds2_updateAudio();

		/* All data sent. */
	}

	so.samples_mixed_so_far -= sample_count;
#endif
}

void NDSSFCProduceSound (unsigned int unused)
{
	unsigned short *audiobuff;

	if (Settings.Paused || !IsSoundGenerated || so.mute_sound || !game_enable_audio)
		return;

	if(ds2_checkAudiobuff() > 4)
		return;

	/* Number of samples to generate now */
	int sample_count = so.buffer_size;
	if (so.sixteen_bit)
	{
		/* to prevent running out of buffer space,
		* create less samples
		*/
		sample_count >>= 1;
	}

	if (block_signal)
	{
		pending_signal = TRUE;
		return;
	}

//	block_generate_sound = TRUE;

	audiobuff = (unsigned short*)ds2_getAudiobuff();
	if(NULL == audiobuff)	//There are audio queue in sending or wait to send
	{
		return;
	}

	/* If we need more audio samples */
	if (so.samples_mixed_so_far < sample_count)
	{
		/* Where to put the samples to */
		unsigned byte_offset = so.play_position + 
		(so.sixteen_bit ? (so.samples_mixed_so_far << 1) : so.samples_mixed_so_far);

		//printf ("%d:", sample_count - so.samples_mixed_so_far); fflush (stdout);
		if (Settings.SoundSync == 2)
		{
			/*memset (Buf + (byte_offset & SOUND_BUFFER_SIZE_MASK), 0,
			sample_count - so.samples_mixed_so_far);*/
		}
		else
		{
			/* Mix the missing samples */
			S9xMixSamplesO (Buf, sample_count - so.samples_mixed_so_far,
				byte_offset & SOUND_BUFFER_SIZE_MASK);
		}
		so.samples_mixed_so_far = sample_count;
	}

//    if (!so.mute_sound)
	{
		unsigned bytes_to_write = sample_count;
		if(so.sixteen_bit) bytes_to_write <<= 1;

		unsigned byte_offset = so.play_position;
		so.play_position += bytes_to_write;
		so.play_position &= SOUND_BUFFER_SIZE_MASK; /* wrap to beginning */

//		block_generate_sound = FALSE;

		unsigned short *dst_pt = audiobuff;
		unsigned short *dst_pt1 = dst_pt + DS2_BUFFER_SIZE;

		/* Feed the samples to the soundcard until nothing is left */
		for(;;)
		{
			int I = bytes_to_write;
			if (byte_offset + I > SOUND_BUFFER_SIZE)
			{
				I = SOUND_BUFFER_SIZE - byte_offset;
			}
			if(I == 0) break;

//			memcpy(dst_pt, (char *) Buf + byte_offset, I);
//			dst_pt += I;

			unsigned short *src_pt= (unsigned short*)(Buf + byte_offset);
			for(int m= 0; m < I/4; m++)
			{
				*dst_pt++= *src_pt++;//(*src_pt++) <<1;
				*dst_pt1++= *src_pt++;//(*src_pt++) <<1;
			}

			bytes_to_write -= I;
			byte_offset += I;
			byte_offset &= SOUND_BUFFER_SIZE_MASK; /* wrap */
		}

		ds2_updateAudio();

		/* All data sent. */
	}

	IsSoundGenerated = FALSE;

	so.samples_mixed_so_far -= sample_count;
}

void Init_Timer (void)
{
}


/*
const unsigned int keymap[12] = {
		0x80,		//KEY_A
		0x8000,		//KEY_B
		0x2000,		//KEY_SELECT
		0x1000,		//KEY_START
		0x100,		//KEY_RIGHT
		0x200,		//KEY_LEFT
		0x800,		//KEY_UP
		0x400,		//KEY_DOWN
		0x10,		//KEY_R
		0x20,		//KEY_L
		0x40,		//KEY_X
		0x4000		//KEY_Y
	};
*/

unsigned int S9xReadJoypad (int which1)
{
    struct key_buf inputdata;

	ds2_getrawInput(&inputdata);

	if (inputdata.key & KEY_LID)
	{
		ds2_setCPUclocklevel(0);
		ds2_setSupend();
		do {
			ds2_getrawInput(&inputdata);
			mdelay(1);
		} while (inputdata.key & KEY_LID);
		ds2_wakeup();
		set_cpu_clock(clock_speed_number);
	}

	if(inputdata.key & KEY_TOUCH)	//Active menu
		Settings.Paused = 1;

	if(which1 < 1)
	{
		unsigned int key;
		unsigned int i;

		                                           //   DS   ->  SNES
		key  = (inputdata.key & KEY_A     ) <<  7; // 0x0001 -> 0x0080
		key |= (inputdata.key & KEY_B     ) << 14; // 0x0002 -> 0x8000
		key |= (inputdata.key & KEY_SELECT) << 11; // 0x0004 -> 0x2000
		key |= (inputdata.key & KEY_START ) <<  9; // 0x0008 -> 0x1000
		key |= (inputdata.key & KEY_UP    ) <<  5; // 0x0040 -> 0x0800
		// 0x0010 -> 0x0100; 0x0020 -> 0x0200
		// 0x0030 -> 0x0300
		key |= (inputdata.key & (KEY_RIGHT | KEY_LEFT))  <<  4;
		// 0x0100 -> 0x0010; 0x0200 -> 0x0020; 0x0400 -> 0x0040
		// 0x0700 -> 0x0070
		key |= (inputdata.key & (KEY_R | KEY_L | KEY_X)) >>  4;
		// 0x0080 -> 0x0400; 0x0800 -> 0x4000
		// 0x0880 -> 0x4400
		key |= (inputdata.key & (KEY_DOWN | KEY_Y))      <<  3;
/*
		for(i= 0; i < 12; i++)	//remap key
		{
			key |= (inputdata.key & (1<<i)) ? keymap[i] : 0;
		}
*/

		return (key | 0x80000000);
	}
	else
		return 0;
}

static int S9xCompareSDD1IndexEntries (const void *p1, const void *p2)
{
    return (*(uint32 *) p1 - *(uint32 *) p2);
}

void S9xLoadSDD1Data ()
{
    char filename [_MAX_PATH + 1];
    char index [_MAX_PATH + 1];
    char data [_MAX_PATH + 1];
    char patch [_MAX_PATH + 1];

    Memory.FreeSDD1Data ();

    strcpy (filename, S9xGetSnapshotDirectory ());

    Settings.SDD1Pack=FALSE;
    if (strncmp (Memory.ROMName, "Star Ocean", 10) == 0){
        if(SDD1_pack) strcpy (filename, SDD1_pack);
#ifdef SDD1_DECOMP
        else Settings.SDD1Pack=TRUE;
#else
	strcat (filename, "/socnsdd1");
#endif
    } else if(strncmp(Memory.ROMName, "STREET FIGHTER ALPHA2", 21)==0){
        if(SDD1_pack) strcpy (filename, SDD1_pack);
#ifdef SDD1_DECOMP
        else Settings.SDD1Pack=TRUE;
#else
	strcat (filename, "/sfa2sdd1");
#endif
    } else {
        if(SDD1_pack) strcpy (filename, SDD1_pack);
#ifdef SDD1_DECOMP
        else Settings.SDD1Pack=TRUE;
#else
	S9xMessage(S9X_WARNING, S9X_ROM_INFO, "WARNING: No default SDD1 pack for this ROM");
#endif
    }

    if(Settings.SDD1Pack) return;

    DIR *dir = opendir (filename);

    index [0] = 0;
    data [0] = 0;
    patch [0] = 0;

    if (dir)
    {
//	struct dirent *d;
	dirent *d;
	
	while ((d = readdir (dir)))
	{
	    if (strcasecmp (d->d_name, "SDD1GFX.IDX") == 0)
	    {
		strcpy (index, filename);
		strcat (index, "/");
		strcat (index, d->d_name);
	    }
	    else
	    if (strcasecmp (d->d_name, "SDD1GFX.DAT") == 0)
	    {
		strcpy (data, filename);
		strcat (data, "/");
		strcat (data, d->d_name);
	    }
	    if (strcasecmp (d->d_name, "SDD1GFX.PAT") == 0)
	    {
		strcpy (patch, filename);
		strcat (patch, "/");
		strcat (patch, d->d_name);
	    }
	}
	closedir (dir);

	if (strlen (index) && strlen (data))
	{
	    FILE *fs = fopen (index, "rb");
	    int len = 0;

	    if (fs)
	    {
		// Index is stored as a sequence of entries, each entry being
		// 12 bytes consisting of:
		// 4 byte key: (24bit address & 0xfffff * 16) | translated block
		// 4 byte ROM offset
		// 4 byte length
		fseek (fs, 0, SEEK_END);
		len = ftell (fs);
		//rewind (fs);
        fseek (fs, 0, SEEK_SET);
		Memory.SDD1Index = (uint8 *) malloc (len);
		fread (Memory.SDD1Index, 1, len, fs);
		fclose (fs);
		Memory.SDD1Entries = len / 12;

		if (!(fs = fopen (data, "rb")))
		{
		    free ((char *) Memory.SDD1Index);
		    Memory.SDD1Index = NULL;
		    Memory.SDD1Entries = 0;
		}
		else
		{
		    fseek (fs, 0, SEEK_END);
		    len = ftell (fs);
		    //rewind (fs);
            fseek (fs, 0, SEEK_SET);
		    Memory.SDD1Data = (uint8 *) malloc (len);
		    fread (Memory.SDD1Data, 1, len, fs);
		    fclose (fs);

		    if (strlen (patch) > 0 &&
			(fs = fopen (patch, "rb")))
		    {
			fclose (fs);
		    }
#ifdef MSB_FIRST
		    // Swap the byte order of the 32-bit value triplets on
		    // MSBFirst machines.
		    uint8 *ptr = Memory.SDD1Index;
		    for (int i = 0; i < Memory.SDD1Entries; i++, ptr += 12)
		    {
			SWAP_DWORD ((*(uint32 *) (ptr + 0)));
			SWAP_DWORD ((*(uint32 *) (ptr + 4)));
			SWAP_DWORD ((*(uint32 *) (ptr + 8)));
		    }
#endif
		    qsort (Memory.SDD1Index, Memory.SDD1Entries, 12,
			   S9xCompareSDD1IndexEntries);
		}
	    }
	}
	else
	{
	    fprintf (stderr, "Decompressed data pack not found in '%s'.\n", 
                     filename);
	}
    }
}

bool8 S9xReadMousePosition (int which1, int &x, int &y, uint32 &buttons)
{
    return (FALSE);
}

bool8 S9xReadSuperScopePosition (int &x, int &y, uint32 &buttons)
{
    return (TRUE);
}

bool JustifierOffscreen()
{
    return (FALSE);
}

void JustifierButtons(uint32& justifiers)
{
}

START_EXTERN_C
char* osd_GetPackDir()
{
  static char filename[_MAX_PATH];
  memset(filename, 0, _MAX_PATH);
  
  if(strlen(S9xGetSnapshotDirectory())!=0)
    strcpy (filename, S9xGetSnapshotDirectory());
  else
  {
    char dir [_MAX_DIR + 1];
    char drive [_MAX_DRIVE + 1];
    char name [_MAX_FNAME + 1];
    char ext [_MAX_EXT + 1];
    _splitpath(Memory.ROMFilename, drive, dir, name, ext);
    _makepath(filename, drive, dir, NULL, NULL);
  }
  
  if(!strncmp((char*)&Memory.ROM [0xffc0], "SUPER POWER LEAG 4   ", 21))
  {
    if (getenv("SPL4PACK"))
      return getenv("SPL4PACK");
    else 
      strcat(filename, "/SPL4-SP7");
  }
  else if(!strncmp((char*)&Memory.ROM [0xffc0], "MOMOTETSU HAPPY      ",21))
  {
    if (getenv("MDHPACK"))
      return getenv("MDHPACK");
    else 
      strcat(filename, "/SMHT-SP7");
  }
  else if(!strncmp((char*)&Memory.ROM [0xffc0], "HU TENGAI MAKYO ZERO ", 21))
  {
    if (getenv("FEOEZPACK"))
      return getenv("FEOEZPACK");
    else 
      strcat(filename, "/FEOEZSP7");
  }
  else if(!strncmp((char*)&Memory.ROM [0xffc0], "JUMP TENGAIMAKYO ZERO",21))
  {
    if (getenv("SJNSPACK"))
      return getenv("SJNSPACK");
    else 
      strcat(filename, "/SJUMPSP7");
  } else strcat(filename, "/MISC-SP7");
  return filename;
}
END_EXTERN_C