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

📄 dma.cpp

📁 SFC游戏模拟器 snes9x 1.43 的原代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*******************************************************************************  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.*******************************************************************************/#ifdef HAVE_CONFIG_H#include <config.h>#endif#include "snes9x.h"#include "memmap.h"#include "ppu.h"#include "cpuexec.h"#include "missing.h"#include "dma.h"#include "apu.h"#include "gfx.h"#include "sa1.h"#include "spc7110.h"#ifdef SDD1_DECOMP#include "sdd1emu.h"#endif#ifdef SDD1_DECOMPuint8 buffer[0x10000];#endifextern int HDMA_ModeByteCounts [8];extern uint8 *HDMAMemPointers [8];extern uint8 *HDMABasePointers [8];// #define SETA010_HDMA_FROM_CART#ifdef SETA010_HDMA_FROM_CARTuint32 HDMARawPointers[8];	// Cart address space pointer#endif#if defined(__linux__) || defined(__WIN32__) || defined(__MACOSX__)static int S9xCompareSDD1IndexEntries (const void *p1, const void *p2){    return (*(uint32 *) p1 - *(uint32 *) p2);}#endif/**********************************************************************************************//* S9xDoDMA()                                                                                   *//* This function preforms the general dma transfer                                            *//**********************************************************************************************/void S9xDoDMA (uint8 Channel){    uint8 Work;	    if (Channel > 7 || CPU.InDMA)		return;	    CPU.InDMA = TRUE;    bool8 in_sa1_dma = FALSE;    uint8 *in_sdd1_dma = NULL;	uint8 *spc7110_dma=NULL;	bool s7_wrap=false;    SDMA *d = &DMA[Channel];	    int count = d->TransferBytes;	    if (count == 0)		count = 0x10000;	    int inc = d->AAddressFixed ? 0 : (!d->AAddressDecrement ? 1 : -1);	if((d->ABank==0x7E||d->ABank==0x7F)&&d->BAddress==0x80)	{		d->AAddress+= d->TransferBytes;		//does an invalid DMA actually take time?		// I'd say yes, since 'invalid' is probably just the WRAM chip		// not being able to read and write itself at the same time		CPU.Cycles+=(d->TransferBytes+1)*SLOW_ONE_CYCLE;		S9xUpdateAPUTimer();		goto update_address;	}    switch (d->BAddress)    {    case 0x18:    case 0x19:		if (IPPU.RenderThisFrame)			FLUSH_REDRAW ();		break;    }    if (Settings.SDD1)    {		if (d->AAddressFixed && Memory.FillRAM [0x4801] > 0)		{			// Hacky support for pre-decompressed S-DD1 data			inc = !d->AAddressDecrement ? 1 : -1;			uint32 address = (((d->ABank << 16) | d->AAddress) & 0xfffff) << 4;						address |= Memory.FillRAM [0x4804 + ((d->ABank - 0xc0) >> 4)];#ifdef SDD1_DECOMP			if(Settings.SDD1Pack)			{				uint8* in_ptr=GetBasePointer(((d->ABank << 16) | d->AAddress));				in_ptr+=d->AAddress;				SDD1_decompress(buffer,in_ptr,d->TransferBytes);				in_sdd1_dma=buffer;#ifdef SDD1_VERIFY				void *ptr = bsearch (&address, Memory.SDD1Index, 					Memory.SDD1Entries, 12, S9xCompareSDD1IndexEntries);				if(memcmp(buffer, ptr, d->TransferBytes))				{					uint8 *p = Memory.SDD1LoggedData;					bool8 found = FALSE;					uint8 SDD1Bank = Memory.FillRAM [0x4804 + ((d->ABank - 0xc0) >> 4)] | 0xf0;										for (uint32 i = 0; i < Memory.SDD1LoggedDataCount; i++, p += 8)					{						if (*p == d->ABank ||							*(p + 1) == (d->AAddress >> 8) &&							*(p + 2) == (d->AAddress & 0xff) &&							*(p + 3) == (count >> 8) &&							*(p + 4) == (count & 0xff) &&							*(p + 7) == SDD1Bank)						{							found = TRUE;						}					}					if (!found && Memory.SDD1LoggedDataCount < MEMMAP_MAX_SDD1_LOGGED_ENTRIES)					{						int j=0;						while(ptr[j]==buffer[j])							j++;							*p = d->ABank;						*(p + 1) = d->AAddress >> 8;						*(p + 2) = d->AAddress & 0xff;						*(p + 3) = j&0xFF;						*(p + 4) = (j>>8)&0xFF;						*(p + 7) = SDD1Bank;						Memory.SDD1LoggedDataCount += 1;					}				}#endif			}			else			{#endif#if defined(__linux__) || defined (__WIN32__) || defined(__MACOSX__)			void *ptr = bsearch (&address, Memory.SDD1Index, 				Memory.SDD1Entries, 12, S9xCompareSDD1IndexEntries);			if (ptr)				in_sdd1_dma = *(uint32 *) ((uint8 *) ptr + 4) + Memory.SDD1Data;#else			uint8 *ptr = Memory.SDD1Index;						for (uint32 e = 0; e < Memory.SDD1Entries; e++, ptr += 12)			{				if (address == *(uint32 *) ptr)				{					in_sdd1_dma = *(uint32 *) (ptr + 4) + Memory.SDD1Data;					break;				}			}#endif						if (!in_sdd1_dma)			{				// No matching decompressed data found. Must be some new 				// graphics not encountered before. Log it if it hasn't been				// already.				uint8 *p = Memory.SDD1LoggedData;				bool8 found = FALSE;				uint8 SDD1Bank = Memory.FillRAM [0x4804 + ((d->ABank - 0xc0) >> 4)] | 0xf0;								for (uint32 i = 0; i < Memory.SDD1LoggedDataCount; i++, p += 8)				{					if (*p == d->ABank ||						*(p + 1) == (d->AAddress >> 8) &&						*(p + 2) == (d->AAddress & 0xff) &&						*(p + 3) == (count >> 8) &&						*(p + 4) == (count & 0xff) &&						*(p + 7) == SDD1Bank)					{						found = TRUE;						break;					}				}				if (!found && Memory.SDD1LoggedDataCount < MEMMAP_MAX_SDD1_LOGGED_ENTRIES)				{					*p = d->ABank;					*(p + 1) = d->AAddress >> 8;					*(p + 2) = d->AAddress & 0xff;					*(p + 3) = count >> 8;					*(p + 4) = count & 0xff;					*(p + 7) = SDD1Bank;					Memory.SDD1LoggedDataCount += 1;				}			}		}#ifdef SDD1_DECOMP		}#endif		Memory.FillRAM [0x4801] = 0;    }	if(Settings.SPC7110&&(d->AAddress==0x4800||d->ABank==0x50))	{		uint32 i,j;		i=(s7r.reg4805|(s7r.reg4806<<8));#ifdef SPC7110_DEBUG		printf("DMA Transfer of %04X bytes from %02X%02X%02X:%02X, offset of %04X, internal bank of %04X, multiplier %02X\n",d->TransferBytes,s7r.reg4803,s7r.reg4802,s7r.reg4801, s7r.reg4804,i,  s7r.bank50Internal, s7r.AlignBy);#endif		i*=s7r.AlignBy;		i+=s7r.bank50Internal;		i%=DECOMP_BUFFER_SIZE;		j=0;		if((i+d->TransferBytes)<DECOMP_BUFFER_SIZE)		{			spc7110_dma=&s7r.bank50[i];		}		else		{			spc7110_dma=new uint8[d->TransferBytes];			j=DECOMP_BUFFER_SIZE-i;			memcpy(spc7110_dma, &s7r.bank50[i], j);			memcpy(&spc7110_dma[j],s7r.bank50,d->TransferBytes-j);			s7_wrap=true;		}		int icount=s7r.reg4809|(s7r.reg480A<<8);		icount-=d->TransferBytes;		s7r.reg4809=0x00ff&icount;		s7r.reg480A=(0xff00&icount)>>8;		s7r.bank50Internal+=d->TransferBytes;		s7r.bank50Internal%=DECOMP_BUFFER_SIZE;		inc=1;		d->AAddress-=count;	}    if (d->BAddress == 0x18 && SA1.in_char_dma && (d->ABank & 0xf0) == 0x40)    {		// Perform packed bitmap to PPU character format conversion on the		// data before transmitting it to V-RAM via-DMA.		int num_chars = 1 << ((Memory.FillRAM [0x2231] >> 2) & 7);		int depth = (Memory.FillRAM [0x2231] & 3) == 0 ? 8 :		(Memory.FillRAM [0x2231] & 3) == 1 ? 4 : 2;				int bytes_per_char = 8 * depth;		int bytes_per_line = depth * num_chars;		int char_line_bytes = bytes_per_char * num_chars;		uint32 addr = (d->AAddress / char_line_bytes) * char_line_bytes;		uint8 *base = GetBasePointer ((d->ABank << 16) + addr) + addr;		uint8 *buffer = &Memory.ROM [CMemory::MAX_ROM_SIZE - 0x10000];		uint8 *p = buffer;		uint32 inc = char_line_bytes - (d->AAddress % char_line_bytes);		uint32 char_count = inc / bytes_per_char;				in_sa1_dma = TRUE;				//printf ("%08x,", base); fflush (stdout);		//printf ("depth = %d, count = %d, bytes_per_char = %d, bytes_per_line = %d, num_chars = %d, char_line_bytes = %d\n",		//depth, count, bytes_per_char, bytes_per_line, num_chars, char_line_bytes);		int i;				switch (depth)		{		case 2:			for (i = 0; i < count; i += inc, base += char_line_bytes, 				inc = char_line_bytes, char_count = num_chars)			{				uint8 *line = base + (num_chars - char_count) * 2;				for (uint32 j = 0; j < char_count && p - buffer < count; 				j++, line += 2)				{					uint8 *q = line;					for (int l = 0; l < 8; l++, q += bytes_per_line)					{						for (int b = 0; b < 2; b++)						{							uint8 r = *(q + b);							*(p + 0) = (*(p + 0) << 1) | ((r >> 0) & 1);							*(p + 1) = (*(p + 1) << 1) | ((r >> 1) & 1);							*(p + 0) = (*(p + 0) << 1) | ((r >> 2) & 1);							*(p + 1) = (*(p + 1) << 1) | ((r >> 3) & 1);							*(p + 0) = (*(p + 0) << 1) | ((r >> 4) & 1);							*(p + 1) = (*(p + 1) << 1) | ((r >> 5) & 1);							*(p + 0) = (*(p + 0) << 1) | ((r >> 6) & 1);							*(p + 1) = (*(p + 1) << 1) | ((r >> 7) & 1);						}						p += 2;					}				}			}			break;		case 4:			for (i = 0; i < count; i += inc, base += char_line_bytes, 				inc = char_line_bytes, char_count = num_chars)			{				uint8 *line = base + (num_chars - char_count) * 4;				for (uint32 j = 0; j < char_count && p - buffer < count; 				j++, line += 4)				{					uint8 *q = line;					for (int l = 0; l < 8; l++, q += bytes_per_line)					{						for (int b = 0; b < 4; b++)						{							uint8 r = *(q + b);							*(p +  0) = (*(p +  0) << 1) | ((r >> 0) & 1);							*(p +  1) = (*(p +  1) << 1) | ((r >> 1) & 1);							*(p + 16) = (*(p + 16) << 1) | ((r >> 2) & 1);							*(p + 17) = (*(p + 17) << 1) | ((r >> 3) & 1);

⌨️ 快捷键说明

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