Skip to content

Commit

Permalink
-added fds support, make sure to have your fds files in the same fold…
Browse files Browse the repository at this point in the history
…er as the fds bios named disksys.rom to use it

-added new keyboard command "B" to switch disk sides in fds titles that need it
  • Loading branch information
FIX94 committed Feb 14, 2017
1 parent 4e36aa3 commit 4f36224
Show file tree
Hide file tree
Showing 8 changed files with 520 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ NTSC and PAL .nes ROMs are supported right now, it also creates .sav files if th
Supported Mappers: 0,1,2,3,4,7,9,10,11,13,15,36,37,38,44,46,47,66,71,79,87,99,101,113,133,140,144,145,146,147,148,149,185,240 and 242.
To start a game, simply drag and drop its .nes file into it or call it via command line with the .nes file as argument.
If you are starting a PAL NES title then make sure it has (E) in the name to be started in PAL mode.
You can also play FDS titles if you have the FDS BIOS named disksys.rom in the same folder as your .fds files.
You can also listen to .nsf files by dragging them in, changing tracks works by pressing left/right.

Controls right now are keyboard only and do the following:
Expand All @@ -16,6 +17,7 @@ S is select
Arrow Keys is DPad
Keys 1-9 integer-scale the window to number
P is Pause
B is Disk Switching (for FDS)
O is Enable/Disable vertical Overscan

That is all I can say about it right now, who knows if I will write some more on it.
9 changes: 8 additions & 1 deletion cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ static bool interrupt;
bool dmc_interrupt;
bool mmc5_dmc_interrupt;
bool apu_interrupt;
bool fds_interrupt;
bool fds_transfer_interrupt;
uint32_t cpu_oam_dma;
extern bool nesPause;

Expand All @@ -40,6 +42,8 @@ void cpuInit()
dmc_interrupt = false;
mmc5_dmc_interrupt = false;
apu_interrupt = false;
fds_interrupt = false;
fds_transfer_interrupt = false;
cpu_oam_dma = 0;
p = (P_FLAG_IRQ_DISABLE | P_FLAG_S1 | P_FLAG_S2);
a = 0;
Expand Down Expand Up @@ -415,6 +419,8 @@ bool cpuCycle()
#endif
if(interrupt)
interrupt = false;
if(fds_transfer_interrupt)
fds_transfer_interrupt = false;
waitCycles+=5;
}
uint16_t instrPtr = pc;
Expand Down Expand Up @@ -1906,7 +1912,8 @@ bool cpuCycle()
}
//update interrupt values
ppu_nmi_handler_req = ppuNMI();
cpu_interrupt_req = (interrupt || ((mapper_interrupt || dmc_interrupt || apu_interrupt || mmc5_dmc_interrupt) && !(p & P_FLAG_IRQ_DISABLE)));
cpu_interrupt_req = (interrupt || ((mapper_interrupt || dmc_interrupt || apu_interrupt ||
fds_interrupt || fds_transfer_interrupt || mmc5_dmc_interrupt) && !(p & P_FLAG_IRQ_DISABLE)));
//if(instrPtr > 0xa980 && instrPtr < 0xa9C0) printf("%d %d %d %04x %04x\n",a,x,y,instrPtr,memGet8(instrPtr)|(memGet8(instrPtr+1)<<8));
return true;
}
Expand Down
149 changes: 147 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
#define DEBUG_KEY 0
#define DEBUG_LOAD_INFO 1

static const char *VERSION_STRING = "fixNES Alpha v0.5.4";
static const char *VERSION_STRING = "fixNES Alpha v0.5.5";

static void nesEmuDisplayFrame(void);
static void nesEmuMainLoop(void);
static 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);
Expand All @@ -54,6 +55,7 @@ bool nesEmuNSFPlayback = false;
static bool inPause = false;
static bool inOverscanToggle = false;
static bool inResize = false;
static bool inDiskSwitch = false;

#if WINDOWS_BUILD
#include <windows.h>
Expand All @@ -72,6 +74,7 @@ static DWORD emuTotalElapsed = 0;
static const int visibleImg = VISIBLE_DOTS*VISIBLE_LINES*4;
static int scaleFactor = 2;
static bool emuSaveEnabled = false;
static bool emuFdsHasSideB = false;
static int emuApuClockCycles;
static int emuApuClock;
static int mainLoopRuns;
Expand Down Expand Up @@ -178,6 +181,101 @@ int main(int argc, char** argv)
}
nesEmuNSFPlayback = true;
}
else if(argc >= 2 && (strstr(argv[1],".fds") != NULL || strstr(argv[1],".FDS") != NULL
|| strstr(argv[1],".qd") != NULL || strstr(argv[1],".QD") != NULL))
{
emuSaveName = strdup(argv[1]);
memcpy(emuSaveName+strlen(emuSaveName)-3,"sav",3);
bool saveValid = false;
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);
}
if(!saveValid)
{
FILE *nesF = fopen(argv[1],"rb");
if(!nesF) return EXIT_SUCCESS;
fseek(nesF,0,SEEK_END);
size_t fsize = ftell(nesF);
rewind(nesF);
uint8_t *nesFread = malloc(fsize);
fread(nesFread,1,fsize,nesF);
fclose(nesF);
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 = fsize-0x10;
}
else
{
fds_src = nesFread;
fds_src_len = fsize;
}
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;
emuNesROM = malloc(0x20000);
memset(emuNesROM, 0, 0x20000);
nesEmuFdsSetup(fds_src, emuNesROM); //setup individually
nesEmuFdsSetup(fds_src+0xFFDC, emuNesROM+0x10000);
}
else if(fds_src_len == 0xFFDC)
{
emuNesROM = malloc(0x10000);
memset(emuNesROM, 0, 0x10000);
nesEmuFdsSetup(fds_src, emuNesROM);
}
}
else
{
if(fds_src_len == 0x20000)
{
emuFdsHasSideB = true;
emuNesROM = malloc(0x20000);
memcpy(emuNesROM, fds_src, 0x20000);
}
else if(fds_src_len == 0x10000)
{
emuNesROM = malloc(0x10000);
memcpy(emuNesROM, fds_src, 0x10000);
}
}
free(nesFread);
}
emuPrgRAMsize = 0x8000;
emuPrgRAM = malloc(emuPrgRAMsize);
cpuInit();
ppuInit();
memInit();
apuInit();
inputInit();
if(!mapperInitFDS(emuNesROM, emuFdsHasSideB, emuPrgRAM, emuPrgRAMsize))
{
printf("FDS init failed!\n");
free(emuNesROM);
emuNesROM = NULL;
return EXIT_SUCCESS;
}
}
if(emuNesROM == NULL)
return EXIT_SUCCESS;
#if WINDOWS_BUILD
Expand Down Expand Up @@ -236,7 +334,21 @@ static void nesEmuDeinit(void)
audioDeinit();
apuDeinit();
if(emuNesROM != NULL)
{
if(fdsEnabled)
{
FILE *save = fopen(emuSaveName, "wb");
if(save)
{
if(emuFdsHasSideB)
fwrite(emuNesROM,1,0x20000,save);
else
fwrite(emuNesROM,1,0x10000,save);
fclose(save);
}
}
free(emuNesROM);
}
emuNesROM = NULL;
if(emuPrgRAM != NULL)
{
Expand Down Expand Up @@ -344,7 +456,7 @@ void nesEmuResetApuClock(void)
{
emuApuClock = 0;
}

extern bool fdsSwitch;
static void nesEmuHandleKeyDown(unsigned char key, int x, int y)
{
(void)x;
Expand Down Expand Up @@ -397,6 +509,14 @@ static void nesEmuHandleKeyDown(unsigned char key, int x, int y)
memDumpMainMem();
exit(EXIT_SUCCESS);
break;
case 'b':
case 'B':
if(!inDiskSwitch)
{
fdsSwitch = true;
inDiskSwitch = true;
}
break;
case 'p':
case 'P':
if(!inPause)
Expand Down Expand Up @@ -528,6 +648,10 @@ static void nesEmuHandleKeyUp(unsigned char key, int x, int y)
#endif
inValReads[BUTTON_START]=0;
break;
case 'b':
case 'B':
inDiskSwitch = false;
break;
case 'p':
case 'P':
#if DEBUG_KEY
Expand Down Expand Up @@ -670,3 +794,24 @@ static void nesEmuDisplayFrame()

glutSwapBuffers();
}

static void nesEmuFdsSetup(uint8_t *src, uint8_t *dst)
{
memcpy(dst, src, 0x38);
memcpy(dst+0x3A, src+0x38, 2);
uint16_t cDiskPos = 0x3E;
uint16_t cROMPos = 0x3A;
do
{
if(src[cROMPos] != 0x03)
break;
memcpy(dst+cDiskPos, src+cROMPos, 0x10);
uint16_t copySize = (*(uint16_t*)(src+cROMPos+0xD))+1;
cDiskPos+=0x12;
cROMPos+=0x10;
memcpy(dst+cDiskPos, src+cROMPos, copySize);
cDiskPos+=copySize+2;
cROMPos+=copySize;
} while(cROMPos < 0xFFDC && cDiskPos < 0xFFFF);
printf("%04x -> %04x\n", cROMPos, cDiskPos);
}
35 changes: 35 additions & 0 deletions mapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "mapper.h"
#include "mapperList.h"
#include "mapper_h/nsf.h"
#include "mapper_h/fds.h"

get8FuncT mapperGet8;
set8FuncT mapperSet8;
Expand Down Expand Up @@ -45,3 +46,37 @@ bool mapperInitNSF(uint8_t *nsfBIN, uint32_t nsfBINsize, uint8_t *prgRAM, uint32
mapperCycle = nsfcycle;
return true;
}

static uint8_t fdsBIOS[0x2000];
bool mapperInitFDS(uint8_t *fdsFile, bool fdsSideB, uint8_t *prgRAM, uint32_t prgRAMsize)
{
if(fdsFile == NULL)
{
printf("No FDS loaded!\n");
return false;
}
FILE *f = fopen("disksys.rom","rb");
if(f == NULL)
{
printf("disksys.rom not found!\n");
return false;
}
fseek(f,0,SEEK_END);
size_t fsize = ftell(f);
rewind(f);
if(fsize != 0x2000)
{
printf("disksys.rom has a wrong size, is %i bytes, should be 8192 bytes!\n", fsize);
fclose(f);
return false;
}
fread(fdsBIOS, 1, 0x2000, f);
fclose(f);
fdsinit(fdsBIOS, fsize, fdsFile, fdsSideB, prgRAM, prgRAMsize);
mapperGet8 = fdsget8;
mapperSet8 = fdsset8;
mapperChrGet8 = fdschrGet8;
mapperChrSet8 = fdschrSet8;
mapperCycle = fdscycle;
return true;
}
1 change: 1 addition & 0 deletions mapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ typedef void (*cycleFuncT)();

bool mapperInit(uint8_t mapper, uint8_t *prgROM, uint32_t prgROMsize, uint8_t *prgRAM, uint32_t prgRAMsize, uint8_t *chrROM, uint32_t chrROMsize);
bool mapperInitNSF(uint8_t *nsfBIN, uint32_t nsfBINsize, uint8_t *prgRAM, uint32_t prgRAMsize);
bool mapperInitFDS(uint8_t *fdsFile, bool fdsSideB, uint8_t *prgRAM, uint32_t prgRAMsize);

extern get8FuncT mapperGet8;
extern set8FuncT mapperSet8;
Expand Down
Loading

0 comments on commit 4f36224

Please sign in to comment.