⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spc7110.cpp

📁 SFC游戏模拟器 snes9x 1.43 的原代码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
/*******************************************************************************  Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.   (c) Copyright 1996 - 2002 Gary Henderson (gary.henderson@ntlworld.com) and                            Jerremy Koot (jkoot@snes9x.com)  (c) Copyright 2001 - 2004 John Weidman (jweidman@slip.net)  (c) Copyright 2002 - 2004 Brad Jorsch (anomie@users.sourceforge.net),                            funkyass (funkyass@spam.shaw.ca),                            Joel Yliluoma (http://iki.fi/bisqwit/)                            Kris Bleakley (codeviolation@hotmail.com),                            Matthew Kendora,                            Nach (n-a-c-h@users.sourceforge.net),                            Peter Bortas (peter@bortas.org) and                            zones (kasumitokoduck@yahoo.com)  C4 x86 assembler and some C emulation code  (c) Copyright 2000 - 2003 zsKnight (zsknight@zsnes.com),                            _Demo_ (_demo_@zsnes.com), and Nach  C4 C++ code  (c) Copyright 2003 Brad Jorsch  DSP-1 emulator code  (c) Copyright 1998 - 2004 Ivar (ivar@snes9x.com), _Demo_, Gary Henderson,                            John Weidman, neviksti (neviksti@hotmail.com),                            Kris Bleakley, Andreas Naive  DSP-2 emulator code  (c) Copyright 2003 Kris Bleakley, John Weidman, neviksti, Matthew Kendora, and                     Lord Nightmare (lord_nightmare@users.sourceforge.net  OBC1 emulator code  (c) Copyright 2001 - 2004 zsKnight, pagefault (pagefault@zsnes.com) and                            Kris Bleakley  Ported from x86 assembler to C by sanmaiwashi  SPC7110 and RTC C++ emulator code  (c) Copyright 2002 Matthew Kendora with research by                     zsKnight, John Weidman, and Dark Force  S-DD1 C emulator code  (c) Copyright 2003 Brad Jorsch with research by                     Andreas Naive and John Weidman   S-RTC C emulator code  (c) Copyright 2001 John Weidman    ST010 C++ emulator code  (c) Copyright 2003 Feather, Kris Bleakley, John Weidman and Matthew Kendora  Super FX x86 assembler emulator code   (c) Copyright 1998 - 2003 zsKnight, _Demo_, and pagefault   Super FX C emulator code   (c) Copyright 1997 - 1999 Ivar, Gary Henderson and John Weidman  SH assembler code partly based on x86 assembler code  (c) Copyright 2002 - 2004 Marcus Comstedt (marcus@mc.pp.se)    Specific ports contains the works of other authors. See headers in  individual files.   Snes9x homepage: http://www.snes9x.com   Permission to use, copy, modify and distribute Snes9x in both binary and  source form, for non-commercial purposes, is hereby granted without fee,  providing that this license information and copyright notice appear with  all copies and any derived work.   This software is provided 'as-is', without any express or implied  warranty. In no event shall the authors be held liable for any damages  arising from the use of this software.   Snes9x is freeware for PERSONAL USE only. Commercial users should  seek permission of the copyright holders first. Commercial use includes  charging money for Snes9x or software derived from Snes9x.   The copyright holders request that bug fixes and improvements to the code  should be forwarded to them so everyone can benefit from the modifications  in future versions.   Super NES and Super Nintendo Entertainment System are trademarks of  Nintendo Co., Limited and its subsidiary companies.*******************************************************************************///#define SPC7110_DEBUG#include "spc7110.h"#include "memmap.h"#include <time.h>#include <sys/stat.h>//Windows includes#ifdef __WIN32__#ifndef _XBOX				// chdir and getcwd not supported on Xbox hardware#include <direct.h>#define chdir _chdir#define getcwd _getcwd#endif#define FREEZEFOLDER	GUI.FreezeFileDir//zinx suggested this, for *nix compatibility#define PATH_MAX	MAX_PATH#else // Unix#include "display.h"#include <limits.h>#include <unistd.h>#ifdef __MACOSX__const char * S9xGetSPC7110Directory(void);#define FREEZEFOLDER S9xGetSPC7110Directory()#else#define FREEZEFOLDER S9xGetSnapshotDirectory ()#endif#endifextern "C" const char *S9xGetFilename (const char *);extern "C" char *osd_GetPackDir();//really not needed, but usually MS adds the _ to POSIX functions,//while *nix doesn't, so this was to "un-M$" the function.#define splitpath _splitpath//not much headroom, but FEOEZ has 41 tables, I think, and SPL4 has 38.#define MAX_TABLES 48//default to using 5 megs of RAM for method 3 caching.uint16 cacheMegs=5;//using function pointers to initialize cache managementvoid (*CleanUp7110)(void)=NULL;void (*LoadUp7110)(char*)=&SPC7110Load;void (*Copy7110)(void)=NULL;//size and offset of the pack data//offset and size of reads from packtypedef struct SPC7110DecompressionLocationStruct{	uint32 offset;	uint32 size;	uint16 used_offset;	uint16 used_len;} Data7110;//this maps an index.bin table to the decompression packtypedef struct SPC7110DecompressionIndexStruct{	int table;	bool is_file;	Data7110 location[256];} Index7110;//this contains all the data for the decompression pack.typedef struct SPC7110DecompressionPackStructure{	uint8* binfiles[MAX_TABLES];	Index7110 tableEnts[MAX_TABLES];	int last_table;	int idx;	uint8 last_idx;	uint16 last_offset;} Pack7110;char pfold[9];				//hack variable for log naming (each game makes a different log)Pack7110* decompack=NULL;   //decompression pack uses a fair chunk of RAM, so dynalloc it.SPC7110Regs s7r;			//SPC7110 registers, about 33KBS7RTC rtc_f9;				//FEOEZ (and Shounen Jump no SHou) RTCvoid	S9xUpdateRTC ();	//S-RTC function hacked to work with the RTC//Emulate power on statevoid S9xSpc7110Init(){	s7r.DataRomOffset=0x00100000;//handy constant!	s7r.DataRomSize=Memory.CalculatedSize-s7r.DataRomOffset;	s7r.reg4800=0;	s7r.reg4801=0;	s7r.reg4802=0;	s7r.reg4803=0;	s7r.reg4804=0;	s7r.reg4805=0;	s7r.reg4806=0;	s7r.reg4807=0;	s7r.reg4808=0;	s7r.reg4809=0;	s7r.reg480A=0;	s7r.reg480B=0;	s7r.reg480C=0;	s7r.reg4811=0;	s7r.reg4812=0;	s7r.reg4813=0;	s7r.reg4814=0;	s7r.reg4815=0;	s7r.reg4816=0;	s7r.reg4817=0;	s7r.reg4818=0;	s7r.reg4820=0;	s7r.reg4821=0;	s7r.reg4822=0;	s7r.reg4823=0;	s7r.reg4824=0;	s7r.reg4825=0;	s7r.reg4826=0;	s7r.reg4827=0;	s7r.reg4828=0;	s7r.reg4829=0;	s7r.reg482A=0;	s7r.reg482B=0;	s7r.reg482C=0;	s7r.reg482D=0;	s7r.reg482E=0;	s7r.reg482F=0;	s7r.reg4830=0;	s7r.reg4831=0;	s7r.reg4832=1;	s7r.reg4833=2;	s7r.reg4834=0;	s7r.reg4840=0;	s7r.reg4841=0;	s7r.reg4842=0;	s7r.written=0;	s7r.offset_add=0;	s7r.AlignBy=1;	(*LoadUp7110)(osd_GetPackDir());	if(Settings.SPC7110RTC)		Settings.TurboMode=false;	s7r.bank50Internal=0;	memset(s7r.bank50,0x00,DECOMP_BUFFER_SIZE);}//full cache decompression routine (memcpy) Method 1void MovePackData(){	//log the last entry	Data7110* log=&(decompack->tableEnts[decompack->idx].location[decompack->last_idx]);	if((log->used_len+log->used_offset)<(decompack->last_offset+(unsigned short)s7r.bank50Internal))	{		log->used_len=s7r.bank50Internal;		log->used_offset=decompack->last_offset;	}		//set up for next logging	decompack->last_offset=(s7r.reg4805)|(s7r.reg4806<<8);		decompack->last_idx=s7r.reg4804;	//start decompression	int table=(s7r.reg4803<<16)|(s7r.reg4802<<8)|s7r.reg4801;		//the table is a offset multiplier byte and a big-endian pointer	int j= 4*s7r.reg4804;	j+=s7r.DataRomOffset;	j+=table;		//set proper offsetting.	if(s7r.reg480B==0)		s7r.AlignBy=0;	else	{		switch(ROM[j])		{		case 0x03:			s7r.AlignBy=8;			break;		case 0x01:			s7r.AlignBy=2;			break;		case 0x02:			s7r.AlignBy=4;			break;		case 0x00:		default:			s7r.AlignBy=1;			break;		}	}	//note that we are still setting up for the next log.	decompack->last_offset*=s7r.AlignBy;	decompack->last_offset%=DECOMP_BUFFER_SIZE;	//find the table	if(table!=decompack->last_table)	{		int i=0;		while(i<MAX_TABLES&&decompack->tableEnts[i].table!=table)			i++;		if(i==MAX_TABLES)		{// zones: mac specific begin/*#ifdef _XBOX			FILE* fp=fopen("T:¥¥sp7err.out","a");#else			FILE* fp=fopen("sp7err.out","a");#endif*/			printf("Table Entry %06X:%02X not found\n", table, s7r.reg4804);			//fprintf(fp, "Table Entry %06X:%02X not found¥n", table, s7r.reg4804);			//fclose(fp);// zones: end			return;		}		decompack->idx=i;		decompack->last_table=table;	}	//copy data	if(decompack->binfiles[decompack->idx])	{		memcpy(s7r.bank50,			&(decompack->binfiles[decompack->idx][decompack->tableEnts[decompack->idx].location[s7r.reg4804].offset]),			decompack->tableEnts[decompack->idx].location[s7r.reg4804].size);	}}//this is similar to the last function, but it keeps the last 5 accessed files open,// and reads the data directly. Method 2void ReadPackData(){	static int table_age_2;	static int table_age_3;	static int table_age_4;	static int table_age_5;		int table=(s7r.reg4803<<16)|(s7r.reg4802<<8)|s7r.reg4801;	if(table==0)	{		table_age_2=table_age_3=table_age_4=table_age_5=MAX_TABLES;		return;	}	if(table_age_2==0&&table_age_3==0&&table_age_4==0&&table_age_5==0)		table_age_2=table_age_3=table_age_4=table_age_5=MAX_TABLES;	Data7110* log=&(decompack->tableEnts[decompack->idx].location[decompack->last_idx]);	if((log->used_len+log->used_offset)<(decompack->last_offset+(unsigned short)s7r.bank50Internal))	{		log->used_len=s7r.bank50Internal;		log->used_offset=decompack->last_offset;	}		decompack->last_offset=(s7r.reg4805)|(s7r.reg4806<<8);		decompack->last_idx=s7r.reg4804;		int j= 4*s7r.reg4804;	j+=s7r.DataRomOffset;	j+=table;		if(s7r.reg480B==0)		s7r.AlignBy=0;	else	{		switch(ROM[j])		{		case 0x03:			s7r.AlignBy=8;			break;		case 0x01:			s7r.AlignBy=2;			break;		case 0x02:			s7r.AlignBy=4;			break;		case 0x00:		default:			s7r.AlignBy=1;			break;		}	}	decompack->last_offset*=s7r.AlignBy;	decompack->last_offset%=DECOMP_BUFFER_SIZE;	if(table!=decompack->last_table)	{		int i=0;		while(i<MAX_TABLES&&decompack->tableEnts[i].table!=table)			i++;		if(i==MAX_TABLES)		{			// zones: mac specific begin			//FILE* fp=fopen("sp7err.out","a");			printf("Table Entry %06X:%02X not found\n", table, s7r.reg4804);			//fprintf(fp, "Table Entry %06X:%02X not found¥n", table, s7r.reg4804);			//fclose(fp);			// zones: end			return;		}		if(i!= table_age_2 && i!= table_age_3 && i!= table_age_4 && i!= table_age_5)		{			if(table_age_5!=MAX_TABLES&&decompack->binfiles[table_age_5])			{				fclose((FILE*)(decompack->binfiles[table_age_5]));				(decompack->binfiles[table_age_5])=NULL;			}			table_age_5=table_age_4;			table_age_4=table_age_3;			table_age_3=table_age_2;			table_age_2=decompack->idx;			// zones: mac specific begin						//char name[PATH_MAX];			char name[PATH_MAX + 1];			//open file			//char drive [_MAX_DRIVE + 1];			//char dir [_MAX_DIR + 1];			//char fname [_MAX_FNAME + 1];			//char ext [_MAX_EXT + 1];			//if (strlen (FREEZEFOLDER))			//{				//splitpath (Memory.ROMFilename, drive, dir, fname, ext);				strcpy (name, FREEZEFOLDER);			//	strcat (name, "/");			//}			//else			//{			//	splitpath (Memory.ROMFilename, drive, dir, fname, ext);			//	strcpy(name, drive);				//strcat(filename, "¥¥");			//	strcat(name, dir);			//}			//strcat(name, pfold);			char bfname[11];			sprintf(bfname, "%06X.bin", table);			//strcat(name, "/");			// zones: end			strcat(name, bfname);			decompack->binfiles[i]=(uint8*)fopen(name, "rb");		}		else		{			//fix tables in this case			if(table_age_5==i)			{				table_age_5=table_age_4;				table_age_4=table_age_3;				table_age_3=table_age_2;				table_age_2=decompack->idx;			}			if(table_age_4==i)			{				table_age_4=table_age_3;				table_age_3=table_age_2;				table_age_2=decompack->idx;			}			if(table_age_3==i)			{				table_age_3=table_age_2;				table_age_2=decompack->idx;			}			if(table_age_2==i)			{				table_age_2=decompack->idx;			}		}		decompack->idx=i;		decompack->last_table=table;	}	//do read here.	if(decompack->binfiles[decompack->idx])	{		fseek((FILE*)(decompack->binfiles[decompack->idx]), decompack->tableEnts[decompack->idx].location[s7r.reg4804].offset, 0);		fread(s7r.bank50,1, (decompack->tableEnts[decompack->idx].location[s7r.reg4804].size), (FILE*)(decompack->binfiles[decompack->idx]));	}}//Cache Method 3: some entries are cached, others are file handles.//use is_file to distinguish.void GetPackData(){	Data7110* log=&(decompack->tableEnts[decompack->idx].location[decompack->last_idx]);	if((log->used_len+log->used_offset)<(decompack->last_offset+(unsigned short)s7r.bank50Internal))	{		log->used_len=s7r.bank50Internal;		log->used_offset=decompack->last_offset;	}		decompack->last_offset=(s7r.reg4805)|(s7r.reg4806<<8);		decompack->last_idx=s7r.reg4804;	int table=(s7r.reg4803<<16)|(s7r.reg4802<<8)|s7r.reg4801;		int j= 4*s7r.reg4804;	j+=s7r.DataRomOffset;	j+=table;		if(s7r.reg480B==0)		s7r.AlignBy=0;	else	{		switch(ROM[j])		{		case 0x03:			s7r.AlignBy=8;			break;		case 0x01:			s7r.AlignBy=2;			break;		case 0x02:			s7r.AlignBy=4;			break;		case 0x00:		default:			s7r.AlignBy=1;			break;		}	}	decompack->last_offset*=s7r.AlignBy;	decompack->last_offset%=DECOMP_BUFFER_SIZE;	if(table!=decompack->last_table)	{		int i=0;		while(i<MAX_TABLES&&decompack->tableEnts[i].table!=table)			i++;		if(i==MAX_TABLES)		{			// zones: mac specific begin			//FILE* fp=fopen("sp7err.out","a");			printf("Table Entry %06X:%02X not found\n", table, s7r.reg4804);			//fprintf(fp, "Table Entry %06X:%02X not found¥n", table, s7r.reg4804);			//fclose(fp);			// zones: end			return;		}		decompack->idx=i;		decompack->last_table=table;	}	if(decompack->binfiles[decompack->idx])	{		if(decompack->tableEnts[decompack->idx].is_file)		{			fseek((FILE*)decompack->binfiles[decompack->idx], decompack->tableEnts[decompack->idx].location[s7r.reg4804].offset, 0);			fread(s7r.bank50,1, (decompack->tableEnts[decompack->idx].location[s7r.reg4804].size), (FILE*)(decompack->binfiles[decompack->idx]));		}		else		{			memcpy(s7r.bank50,				&(decompack->binfiles[decompack->idx][decompack->tableEnts[decompack->idx].location[s7r.reg4804].offset]),				decompack->tableEnts[decompack->idx].location[s7r.reg4804].size);		}	}}extern "C"{//reads SPC7110 and RTC registers.uint8 S9xGetSPC7110(uint16 Address){#ifdef SPC7110_DEBUG	printf("%04X read\n", Address);#endif	switch (Address)	{		//decompressed data read port. decrements 4809-A (with wrap)		//4805-6 is the offset into the bank		//AlignBy is set (afaik) at decompression time, and is the offset multiplier		//bank50internal is an internal pointer to the actual byte to read.		//so you read from offset*multiplier + bank50internal		//the offset registers cannot be incremented due to the offset multiplier.	case 0x4800:		{			unsigned short count=s7r.reg4809|(s7r.reg480A<<8);			uint32 i, j;			j=(s7r.reg4805|(s7r.reg4806<<8));			j*=s7r.AlignBy;			i=j;			if(count >0)				count--;			else count = 0xFFFF;			s7r.reg4809=0x00ff&count;			s7r.reg480A=(0xff00&count)>>8;			i+=s7r.bank50Internal;			i%=DECOMP_BUFFER_SIZE;			s7r.reg4800=s7r.bank50[i];						s7r.bank50Internal++;			s7r.bank50Internal%=DECOMP_BUFFER_SIZE;#ifdef SPC7110_DEBUG			printf("Returned %02X\n", s7r.reg4800);#endif		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -