-
Notifications
You must be signed in to change notification settings - Fork 10
/
nsf.cpp
657 lines (565 loc) · 15.3 KB
/
nsf.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
/* FCE Ultra - NES/Famicom Emulator
*
* Copyright notice for this file:
* Copyright (C) 2002 Xodnizel
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/// \file
/// \brief implements a built-in NSF player. This is a perk--not a part of the emu core
#include "types.h"
#include "x6502.h"
#include "fceu.h"
#include "video.h"
#include "sound.h"
#include "nsf.h"
#include "utils/general.h"
#include "utils/memory.h"
#include "file.h"
#include "fds.h"
#include "cart.h"
#include "ines.h"
#include "input.h"
#include "state.h"
#include "driver.h"
#ifdef _S9XLUA_H
#include "fceulua.h"
#endif
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
static const int FIXED_EXWRAM_SIZE = 32768+8192;
static uint8 SongReload;
static int32 CurrentSong;
static DECLFW(NSF_write);
static DECLFR(NSF_read);
static int vismode=1; //we cant consider this state, because the UI may be controlling it and wouldnt know we loadstated it
//mbg 7/31/06 todo - no reason this couldnt be assembled on the fly from actual asm source code. thatd be less obscure.
//here it is disassembled, for reference
/*
00:8000:8D F4 3F STA $3FF4 = #$00
00:8003:A2 FF LDX #$FF
00:8005:9A TXS
00:8006:AD F0 3F LDA $3FF0 = #$00
00:8009:F0 09 BEQ $8014
00:800B:AD F1 3F LDA $3FF1 = #$00
00:800E:AE F3 3F LDX $3FF3 = #$00
00:8011:20 00 00 JSR $0000
00:8014:A9 00 LDA #$00
00:8016:AA TAX
00:8017:A8 TAY
00:8018:20 00 00 JSR $0000
00:801B:8D F5 EF STA $EFF5 = #$FF
00:801E:90 FE BCC $801E
00:8020:8D F3 3F STA $3FF3 = #$00
00:8023:18 CLC
00:8024:90 FE BCC $8024
*/
static uint8 NSFROM[0x30+6]=
{
/* 0x00 - NMI */
0x8D,0xF4,0x3F, /* Stop play routine NMIs. */
0xA2,0xFF,0x9A, /* Initialize the stack pointer. */
0xAD,0xF0,0x3F, /* See if we need to init. */
0xF0,0x09, /* If 0, go to play routine playing. */
0xAD,0xF1,0x3F, /* Confirm and load A */
0xAE,0xF3,0x3F, /* Load X with PAL/NTSC byte */
0x20,0x00,0x00, /* JSR to init routine */
0xA9,0x00,
0xAA,
0xA8,
0x20,0x00,0x00, /* JSR to play routine */
0x8D,0xF5,0x3F, /* Start play routine NMIs. */
0x90,0xFE, /* Loopie time. */
/* 0x20 */
0x8D,0xF3,0x3F, /* Init init NMIs */
0x18,
0x90,0xFE /* Loopie time. */
};
static DECLFR(NSFROMRead)
{
return (NSFROM-0x3800)[A];
}
static uint8 doreset=0; //state
static uint8 NSFNMIFlags; //state
uint8 *NSFDATA=0; //configration, loaded from rom?
int NSFMaxBank; //configuration
static int32 NSFSize; //configuration
static uint8 BSon; //configuration
static uint8 BankCounter; //configuration
static uint16 PlayAddr; //configuration
static uint16 InitAddr; //configuration
static uint16 LoadAddr; //configuration
NSF_HEADER NSFHeader; //mbg merge 6/29/06 - needs to be global
void NSFMMC5_Close(void);
static uint8 *ExWRAM=0;
void NSFGI(GI h)
{
switch(h)
{
case GI_CLOSE:
if(NSFDATA) {free(NSFDATA);NSFDATA=0;}
if(ExWRAM) {free(ExWRAM);ExWRAM=0;}
if(NSFHeader.SoundChip&1) {
// NSFVRC6_Init();
} else if(NSFHeader.SoundChip&2) {
// NSFVRC7_Init();
} else if(NSFHeader.SoundChip&4) {
// FDSSoundReset();
} else if(NSFHeader.SoundChip&8) {
NSFMMC5_Close();
} else if(NSFHeader.SoundChip&0x10) {
// NSFN106_Init();
} else if(NSFHeader.SoundChip&0x20) {
// NSFAY_Init();
}
break;
case GI_RESETM2:
case GI_POWER:
NSF_init();
break;
default:
//Unhandled cases
break;
}
}
// First 32KB is reserved for sound chip emulation in the iNES mapper code.
static INLINE void BANKSET(uint32 A, uint32 bank)
{
bank&=NSFMaxBank;
if(NSFHeader.SoundChip&4)
memcpy(ExWRAM+(A-0x6000),NSFDATA+(bank<<12),4096);
else
setprg4(A,bank);
}
int NSFLoad(const char *name, FCEUFILE *fp)
{
int x;
FCEU_fseek(fp,0,SEEK_SET);
FCEU_fread(&NSFHeader,1,0x80,fp);
if(memcmp(NSFHeader.ID,"NESM\x1a",5))
return LOADER_INVALID_FORMAT;
NSFHeader.SongName[31]=NSFHeader.Artist[31]=NSFHeader.Copyright[31]=0;
LoadAddr=NSFHeader.LoadAddressLow;
LoadAddr|=NSFHeader.LoadAddressHigh<<8;
if(LoadAddr<0x6000)
{
FCEUD_PrintError("Invalid load address.");
return LOADER_HANDLED_ERROR;
}
InitAddr=NSFHeader.InitAddressLow;
InitAddr|=NSFHeader.InitAddressHigh<<8;
PlayAddr=NSFHeader.PlayAddressLow;
PlayAddr|=NSFHeader.PlayAddressHigh<<8;
NSFSize=FCEU_fgetsize(fp)-0x80;
NSFMaxBank=((NSFSize+(LoadAddr&0xfff)+4095)/4096);
NSFMaxBank=PRGsize[0]=uppow2(NSFMaxBank);
if (!(NSFDATA = (uint8 *)FCEU_malloc(NSFMaxBank * 4096)))
{
FCEU_PrintError("Unable to allocate memory.");
return LOADER_HANDLED_ERROR;
}
FCEU_fseek(fp,0x80,SEEK_SET);
memset(NSFDATA,0x00,NSFMaxBank*4096);
FCEU_fread(NSFDATA+(LoadAddr&0xfff),1,NSFSize,fp);
NSFMaxBank--;
BSon=0;
for(x=0;x<8;x++)
{
BSon|=NSFHeader.BankSwitch[x];
}
if(BSon==0)
{
BankCounter=0x00;
if ((NSFHeader.LoadAddressHigh & 0x70) >= 0x70)
{
//Ice Climber, and other F000 base address tunes need this
BSon=0xFF;
}
else {
for(x=(NSFHeader.LoadAddressHigh & 0x70) / 0x10;x<8;x++)
{
NSFHeader.BankSwitch[x]=BankCounter;
BankCounter+=0x01;
}
BSon=0;
}
}
for(x=0;x<8;x++)
BSon|=NSFHeader.BankSwitch[x];
GameInfo->type=GIT_NSF;
GameInfo->input[0]=GameInfo->input[1]=SI_GAMEPAD;
GameInfo->cspecial=SIS_NSF;
for(x=0;;x++)
{
if(NSFROM[x]==0x20)
{
NSFROM[x+1]=InitAddr&0xFF;
NSFROM[x+2]=InitAddr>>8;
NSFROM[x+8]=PlayAddr&0xFF;
NSFROM[x+9]=PlayAddr>>8;
break;
}
}
if(NSFHeader.VideoSystem==0)
GameInfo->vidsys=GIV_NTSC;
else if(NSFHeader.VideoSystem==1)
GameInfo->vidsys=GIV_PAL;
GameInterface=NSFGI;
strcpy(LoadedRomFName,name);
FCEU_printf("\nNSF Loaded.\nFile information:\n");
FCEU_printf(" Name: %s\n Artist: %s\n Copyright: %s\n\n",NSFHeader.SongName,NSFHeader.Artist,NSFHeader.Copyright);
if(NSFHeader.SoundChip)
{
static const char *tab[6]={"Konami VRCVI","Konami VRCVII","Nintendo FDS","Nintendo MMC5","Namco 106","Sunsoft FME-07"};
for(x=0;x<6;x++)
if(NSFHeader.SoundChip&(1<<x))
{
FCEU_printf(" Expansion hardware: %s\n",tab[x]);
NSFHeader.SoundChip=1<<x; /* Prevent confusing weirdness if more than one bit is set. */
break;
}
}
if(BSon)
FCEU_printf(" Bank-switched.\n");
FCEU_printf(" Load address: $%04x\n Init address: $%04x\n Play address: $%04x\n",LoadAddr,InitAddr,PlayAddr);
FCEU_printf(" %s\n",(NSFHeader.VideoSystem&1)?"PAL":"NTSC");
FCEU_printf(" Starting song: %d / %d\n\n",NSFHeader.StartingSong,NSFHeader.TotalSongs);
//choose exwram size and allocate
int exwram_size = 8192;
if(NSFHeader.SoundChip&4)
exwram_size = 32768+8192;
//lets just always use this size, for savestate simplicity
exwram_size = FIXED_EXWRAM_SIZE;
ExWRAM=(uint8*)FCEU_gmalloc(exwram_size);
FCEUI_SetVidSystem(NSFHeader.VideoSystem);
return LOADER_OK;
}
static DECLFR(NSFVectorRead)
{
if(((NSFNMIFlags&1) && SongReload) || (NSFNMIFlags&2) || doreset)
{
if(A==0xFFFA) return(0x00);
else if(A==0xFFFB) return(0x38);
else if(A==0xFFFC) return(0x20);
else if(A==0xFFFD) {doreset=0;return(0x38);}
return(X.DB);
}
else
return(CartBR(A));
}
void NSFVRC6_Init(void);
void NSFVRC7_Init(void);
void NSFMMC5_Init(void);
void NSFN106_Init(void);
void NSFAY_Init(void);
//zero 17-apr-2013 - added
static SFORMAT StateRegs[] = {
{&SongReload, 1, "SREL"},
{&CurrentSong, 4 | FCEUSTATE_RLSB, "CURS"},
{&doreset, 1, "DORE"},
{&NSFNMIFlags, 1, "NMIF"},
{ 0 }
};
void NSF_init(void)
{
doreset=1;
ResetCartMapping();
if(NSFHeader.SoundChip&4)
{
SetupCartPRGMapping(0,ExWRAM,32768+8192,1);
setprg32(0x6000,0);
setprg8(0xE000,4);
memset(ExWRAM,0x00,32768+8192);
SetWriteHandler(0x6000,0xDFFF,CartBW);
SetReadHandler(0x6000,0xFFFF,CartBR);
}
else
{
memset(ExWRAM,0x00,8192);
SetReadHandler(0x6000,0x7FFF,CartBR);
SetWriteHandler(0x6000,0x7FFF,CartBW);
SetupCartPRGMapping(0,NSFDATA,((NSFMaxBank+1)*4096),0);
SetupCartPRGMapping(1,ExWRAM,8192,1);
setprg8r(1,0x6000,0);
SetReadHandler(0x8000,0xFFFF,CartBR);
}
if(BSon)
{
int32 x;
for(x=0;x<8;x++)
{
if(NSFHeader.SoundChip&4 && x>=6)
BANKSET(0x6000+(x-6)*4096,NSFHeader.BankSwitch[x]);
BANKSET(0x8000+x*4096,NSFHeader.BankSwitch[x]);
}
}
else
{
int32 x;
for(x=(LoadAddr&0xF000);x<0x10000;x+=0x1000)
BANKSET(x,((x-(LoadAddr&0x7000))>>12));
}
SetReadHandler(0xFFFA,0xFFFD,NSFVectorRead);
SetWriteHandler(0x2000,0x3fff,0);
SetReadHandler(0x2000,0x37ff,0);
SetReadHandler(0x3836,0x3FFF,0);
SetReadHandler(0x3800,0x3835,NSFROMRead);
SetWriteHandler(0x5ff6,0x5fff,NSF_write);
SetWriteHandler(0x3ff0,0x3fff,NSF_write);
SetReadHandler(0x3ff0,0x3fff,NSF_read);
if(NSFHeader.SoundChip&1) {
NSFVRC6_Init();
} else if(NSFHeader.SoundChip&2) {
NSFVRC7_Init();
} else if(NSFHeader.SoundChip&4) {
FDSSoundReset();
} else if(NSFHeader.SoundChip&8) {
NSFMMC5_Init();
} else if(NSFHeader.SoundChip&0x10) {
NSFN106_Init();
} else if(NSFHeader.SoundChip&0x20) {
NSFAY_Init();
}
CurrentSong=NSFHeader.StartingSong;
SongReload=0xFF;
NSFNMIFlags=0;
//zero 17-apr-2013 - added
AddExState(StateRegs, ~0, 0, 0);
AddExState(ExWRAM, FIXED_EXWRAM_SIZE, 0, "ERAM");
}
static DECLFW(NSF_write)
{
switch(A)
{
case 0x3FF3:NSFNMIFlags|=1;break;
case 0x3FF4:NSFNMIFlags&=~2;break;
case 0x3FF5:NSFNMIFlags|=2;break;
case 0x5FF6:
case 0x5FF7:if(!(NSFHeader.SoundChip&4)) return;
case 0x5FF8:
case 0x5FF9:
case 0x5FFA:
case 0x5FFB:
case 0x5FFC:
case 0x5FFD:
case 0x5FFE:
case 0x5FFF:if(!BSon) return;
A&=0xF;
BANKSET((A*4096),V);
break;
}
}
static DECLFR(NSF_read)
{
int x;
switch(A)
{
case 0x3ff0:x=SongReload;
if(!fceuindbg)
SongReload=0;
return x;
case 0x3ff1:
if(!fceuindbg)
{
memset(RAM,0x00,0x800);
BWrite[0x4015](0x4015,0x0);
for(x=0;x<0x14;x++)
BWrite[0x4000+x](0x4000+x,0);
BWrite[0x4015](0x4015,0xF);
if(NSFHeader.SoundChip&4)
{
BWrite[0x4017](0x4017,0xC0); /* FDS BIOS writes $C0 */
BWrite[0x4089](0x4089,0x80);
BWrite[0x408A](0x408A,0xE8);
}
else
{
memset(ExWRAM,0x00,8192);
BWrite[0x4017](0x4017,0xC0);
BWrite[0x4017](0x4017,0xC0);
BWrite[0x4017](0x4017,0x40);
}
if(BSon)
{
for(x=0;x<8;x++)
BANKSET(0x8000+x*4096,NSFHeader.BankSwitch[x]);
}
#ifdef _S9XLUA_H
//CallRegisteredLuaMemHook(A, 1, V, LUAMEMHOOK_WRITE); FIXME
#endif
return (CurrentSong-1);
}
case 0x3FF3:return PAL;
}
return 0;
}
uint8 FCEU_GetJoyJoy(void);
static int special=0;
void DrawNSF(uint8 *XBuf)
{
char snbuf[16];
int x;
if(vismode==0) return;
memset(XBuf,0,256*240);
memset(XDBuf,0,256*240);
{
int32 *Bufpl;
int32 mul=0;
int l;
l=GetSoundBuffer(&Bufpl);
if(special==0)
{
if(FSettings.SoundVolume)
mul=8192*240/(16384*FSettings.SoundVolume/50);
for(x=0;x<256;x++)
{
uint32 y;
y=142+((Bufpl[(x*l)>>8]*mul)>>14);
if(y<240)
XBuf[x+y*256]=3;
}
}
else if(special==1)
{
if(FSettings.SoundVolume)
mul=8192*240/(8192*FSettings.SoundVolume/50);
for(x=0;x<256;x++)
{
double r;
uint32 xp,yp;
r=(Bufpl[(x*l)>>8]*mul)>>14;
xp=128+r*cos(x*M_PI*2/256);
yp=120+r*sin(x*M_PI*2/256);
xp&=255;
yp%=240;
XBuf[xp+yp*256]=3;
}
}
else if(special==2)
{
static double theta=0;
if(FSettings.SoundVolume)
mul=8192*240/(16384*FSettings.SoundVolume/50);
for(x=0;x<128;x++)
{
double xc,yc;
double r,t;
uint32 m,n;
xc=(double)128-x;
yc=0-((double)( ((Bufpl[(x*l)>>8]) *mul)>>14));
t=M_PI+atan(yc/xc);
r=sqrt(xc*xc+yc*yc);
t+=theta;
m=128+r*cos(t);
n=120+r*sin(t);
if(m<256 && n<240)
XBuf[m+n*256]=3;
}
for(x=128;x<256;x++)
{
double xc,yc;
double r,t;
uint32 m,n;
xc=(double)x-128;
yc=(double)((Bufpl[(x*l)>>8]*mul)>>14);
t=atan(yc/xc);
r=sqrt(xc*xc+yc*yc);
t+=theta;
m=128+r*cos(t);
n=120+r*sin(t);
if(m<256 && n<240)
XBuf[m+n*256]=3;
}
theta+=(double)M_PI/256;
}
}
static const int kFgColor = 1;
DrawTextTrans(ClipSidesOffset+XBuf+10*256+4+(((31-strlen((char*)NSFHeader.SongName))<<2)), 256, NSFHeader.SongName, kFgColor);
DrawTextTrans(ClipSidesOffset+XBuf+26*256+4+(((31-strlen((char*)NSFHeader.Artist))<<2)), 256,NSFHeader.Artist, kFgColor);
DrawTextTrans(ClipSidesOffset+XBuf+42*256+4+(((31-strlen((char*)NSFHeader.Copyright))<<2)), 256,NSFHeader.Copyright, kFgColor);
DrawTextTrans(ClipSidesOffset+XBuf+70*256+4+(((31-strlen("Song:"))<<2)), 256, (uint8*)"Song:", kFgColor);
sprintf(snbuf,"<%d/%d>",CurrentSong,NSFHeader.TotalSongs);
DrawTextTrans(XBuf+82*256+4+(((31-strlen(snbuf))<<2)), 256, (uint8*)snbuf, kFgColor);
{
static uint8 last=0;
uint8 tmp;
tmp=FCEU_GetJoyJoy();
if((tmp&JOY_RIGHT) && !(last&JOY_RIGHT))
{
if(CurrentSong<NSFHeader.TotalSongs)
{
CurrentSong++;
SongReload=0xFF;
}
}
else if((tmp&JOY_LEFT) && !(last&JOY_LEFT))
{
if(CurrentSong>1)
{
CurrentSong--;
SongReload=0xFF;
}
}
else if((tmp&JOY_UP) && !(last&JOY_UP))
{
CurrentSong+=10;
if(CurrentSong>NSFHeader.TotalSongs) CurrentSong=NSFHeader.TotalSongs;
SongReload=0xFF;
}
else if((tmp&JOY_DOWN) && !(last&JOY_DOWN))
{
CurrentSong-=10;
if(CurrentSong<1) CurrentSong=1;
SongReload=0xFF;
}
else if((tmp&JOY_START) && !(last&JOY_START))
SongReload=0xFF;
else if((tmp&JOY_A) && !(last&JOY_A))
{
special=(special+1)%3;
}
last=tmp;
}
}
void DoNSFFrame(void)
{
if(((NSFNMIFlags&1) && SongReload) || (NSFNMIFlags&2))
TriggerNMI();
}
void FCEUI_NSFSetVis(int mode)
{
vismode=mode;
}
int FCEUI_NSFChange(int amount)
{
CurrentSong+=amount;
if(CurrentSong<1) CurrentSong=1;
else if(CurrentSong>NSFHeader.TotalSongs) CurrentSong=NSFHeader.TotalSongs;
SongReload=0xFF;
return(CurrentSong);
}
//Returns total songs
int FCEUI_NSFGetInfo(uint8 *name, uint8 *artist, uint8 *copyright, int maxlen)
{
strncpy((char*)name,(char*)NSFHeader.SongName,maxlen); //mbg merge 7/17/06 added casts
strncpy((char*)artist,(char*)NSFHeader.Artist,maxlen); //mbg merge 7/17/06 added casts
strncpy((char*)copyright,(char*)NSFHeader.Copyright,maxlen); //mbg merge 7/17/06 added casts
return(NSFHeader.TotalSongs);
}