Path: blob/main/misc/emulator/xnes/snes9x/fxinst.h
28515 views
/***********************************************************************************1Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.23(c) Copyright 1996 - 2002 Gary Henderson ([email protected]),4Jerremy Koot ([email protected])56(c) Copyright 2002 - 2004 Matthew Kendora78(c) Copyright 2002 - 2005 Peter Bortas ([email protected])910(c) Copyright 2004 - 2005 Joel Yliluoma (http://iki.fi/bisqwit/)1112(c) Copyright 2001 - 2006 John Weidman ([email protected])1314(c) Copyright 2002 - 2006 funkyass ([email protected]),15Kris Bleakley ([email protected])1617(c) Copyright 2002 - 2010 Brad Jorsch ([email protected]),18Nach ([email protected]),1920(c) Copyright 2002 - 2011 zones ([email protected])2122(c) Copyright 2006 - 2007 nitsuja2324(c) Copyright 2009 - 2011 BearOso,25OV2262728BS-X C emulator code29(c) Copyright 2005 - 2006 Dreamer Nom,30zones3132C4 x86 assembler and some C emulation code33(c) Copyright 2000 - 2003 _Demo_ ([email protected]),34Nach,35zsKnight ([email protected])3637C4 C++ code38(c) Copyright 2003 - 2006 Brad Jorsch,39Nach4041DSP-1 emulator code42(c) Copyright 1998 - 2006 _Demo_,43Andreas Naive ([email protected]),44Gary Henderson,45Ivar ([email protected]),46John Weidman,47Kris Bleakley,48Matthew Kendora,49Nach,50neviksti ([email protected])5152DSP-2 emulator code53(c) Copyright 2003 John Weidman,54Kris Bleakley,55Lord Nightmare ([email protected]),56Matthew Kendora,57neviksti5859DSP-3 emulator code60(c) Copyright 2003 - 2006 John Weidman,61Kris Bleakley,62Lancer,63z80 gaiden6465DSP-4 emulator code66(c) Copyright 2004 - 2006 Dreamer Nom,67John Weidman,68Kris Bleakley,69Nach,70z80 gaiden7172OBC1 emulator code73(c) Copyright 2001 - 2004 zsKnight,74pagefault ([email protected]),75Kris Bleakley76Ported from x86 assembler to C by sanmaiwashi7778SPC7110 and RTC C++ emulator code used in 1.39-1.5179(c) Copyright 2002 Matthew Kendora with research by80zsKnight,81John Weidman,82Dark Force8384SPC7110 and RTC C++ emulator code used in 1.52+85(c) Copyright 2009 byuu,86neviksti8788S-DD1 C emulator code89(c) Copyright 2003 Brad Jorsch with research by90Andreas Naive,91John Weidman9293S-RTC C emulator code94(c) Copyright 2001 - 2006 byuu,95John Weidman9697ST010 C++ emulator code98(c) Copyright 2003 Feather,99John Weidman,100Kris Bleakley,101Matthew Kendora102103Super FX x86 assembler emulator code104(c) Copyright 1998 - 2003 _Demo_,105pagefault,106zsKnight107108Super FX C emulator code109(c) Copyright 1997 - 1999 Ivar,110Gary Henderson,111John Weidman112113Sound emulator code used in 1.5-1.51114(c) Copyright 1998 - 2003 Brad Martin115(c) Copyright 1998 - 2006 Charles Bilyue'116117Sound emulator code used in 1.52+118(c) Copyright 2004 - 2007 Shay Green ([email protected])119120SH assembler code partly based on x86 assembler code121(c) Copyright 2002 - 2004 Marcus Comstedt ([email protected])1221232xSaI filter124(c) Copyright 1999 - 2001 Derek Liauw Kie Fa125126HQ2x, HQ3x, HQ4x filters127(c) Copyright 2003 Maxim Stepin ([email protected])128129NTSC filter130(c) Copyright 2006 - 2007 Shay Green131132GTK+ GUI code133(c) Copyright 2004 - 2011 BearOso134135Win32 GUI code136(c) Copyright 2003 - 2006 blip,137funkyass,138Matthew Kendora,139Nach,140nitsuja141(c) Copyright 2009 - 2011 OV2142143Mac OS GUI code144(c) Copyright 1998 - 2001 John Stiles145(c) Copyright 2001 - 2011 zones146147148Specific ports contains the works of other authors. See headers in149individual files.150151152Snes9x homepage: http://www.snes9x.com/153154Permission to use, copy, modify and/or distribute Snes9x in both binary155and source form, for non-commercial purposes, is hereby granted without156fee, providing that this license information and copyright notice appear157with all copies and any derived work.158159This software is provided 'as-is', without any express or implied160warranty. In no event shall the authors be held liable for any damages161arising from the use of this software or it's derivatives.162163Snes9x is freeware for PERSONAL USE only. Commercial users should164seek permission of the copyright holders first. Commercial use includes,165but is not limited to, charging money for Snes9x or software derived from166Snes9x, including Snes9x or derivatives in commercial game bundles, and/or167using Snes9x as a promotion for your commercial product.168169The copyright holders request that bug fixes and improvements to the code170should be forwarded to them so everyone can benefit from the modifications171in future versions.172173Super NES and Super Nintendo Entertainment System are trademarks of174Nintendo Co., Limited and its subsidiary companies.175***********************************************************************************/176177178#ifndef _FXINST_H_179#define _FXINST_H_180181/*182* FxChip(GSU) register space specification183* (Register address space 3000-32ff)184*185* The 16 generic 16 bit registers:186* (Some have a special function in special circumstances)187* 3000 - R0 default source/destination register188* 3002 - R1 pixel plot X position register189* 3004 - R2 pixel plot Y position register190* 3006 - R3191* 3008 - R4 lower 16 bit result of lmult192* 300a - R5193* 300c - R6 multiplier for fmult and lmult194* 300e - R7 fixed point texel X position for merge195* 3010 - R8 fixed point texel Y position for merge196* 3012 - R9197* 3014 - R10198* 3016 - R11 return address set by link199* 3018 - R12 loop counter200* 301a - R13 loop point address201* 301c - R14 rom address for getb, getbh, getbl, getbs202* 301e - R15 program counter203*204* 3020-302f - unused205*206* Other internal registers207* 3030 - SFR status flag register (16bit)208* 3032 - unused209* 3033 - BRAMR Backup RAM register (8bit)210* 3034 - PBR program bank register (8bit)211* 3035 - unused212* 3036 - ROMBR rom bank register (8bit)213* 3037 - CFGR control flags register (8bit)214* 3038 - SCBR screen base register (8bit)215* 3039 - CLSR clock speed register (8bit)216* 303a - SCMR screen mode register (8bit)217* 303b - VCR version code register (8bit) (read only)218* 303c - RAMBR ram bank register (8bit)219* 303d - unused220* 303e - CBR cache base register (16bit)221*222* 3040-30ff - unused223*224* 3100-32ff - CACHERAM 512 bytes of GSU cache memory225*226* SFR status flag register bits:227* 0 -228* 1 Z Zero flag229* 2 CY Carry flag230* 3 S Sign flag231* 4 OV Overflow flag232* 5 G Go flag (set to 1 when the GSU is running)233* 6 R Set to 1 when reading ROM using R14 address234* 7 -235* 8 ALT1 Mode set-up flag for the next instruction236* 9 ALT2 Mode set-up flag for the next instruction237* 10 IL Immediate lower 8-bit flag238* 11 IH Immediate higher 8-bit flag239* 12 B Set to 1 when the WITH instruction is executed240* 13 -241* 14 -242* 15 IRQ Set to 1 when GSU caused an interrupt243* Set to 0 when read by 658c16244*245* BRAMR = 0, BackupRAM is disabled246* BRAMR = 1, BackupRAM is enabled247*248* CFGR control flags register bits:249* 0 -250* 1 -251* 2 -252* 3 -253* 4 -254* 5 MS0 Multiplier speed, 0=standard, 1=high speed255* 6 -256* 7 IRQ Set to 1 when GSU interrupt request is masked257*258* CLSR clock speed register bits:259* 0 CLSR clock speed, 0 = 10.7Mhz, 1 = 21.4Mhz260*261* SCMR screen mode register bits:262* 0 MD0 color depth mode bit 0263* 1 MD1 color depth mode bit 1264* 2 HT0 screen height bit 1265* 3 RAN RAM access control266* 4 RON ROM access control267* 5 HT1 screen height bit 2268* 6 -269* 7 -270*271* RON = 0 SNES CPU has ROM access272* RON = 1 GSU has ROM access273*274* RAN = 0 SNES has game pak RAM access275* RAN = 1 GSU has game pak RAM access276*277* HT1 HT0 Screen height mode278* 0 0 128 pixels high279* 0 1 160 pixels high280* 1 0 192 pixels high281* 1 1 OBJ mode282*283* MD1 MD0 Color depth mode284* 0 0 4 color mode285* 0 1 16 color mode286* 1 0 not used287* 1 1 256 color mode288*289* CBR cache base register bits:290* 15-4 Specify base address for data to cache from ROM or RAM291* 3-0 Are 0 when address is read292*293* Write access to the program counter (301e) from294* the SNES-CPU will start the GSU, and it will not295* stop until it reaches a stop instruction.296*297*/298299// Number of banks in GSU RAM300#define FX_RAM_BANKS 4301302// Emulate proper R14 ROM access (slower, but safer)303#define FX_DO_ROMBUFFER304305// Address checking (definately slow)306//#define FX_ADDRESS_CHECK307308struct FxRegs_s309{310// FxChip registers311uint32 avReg[16]; // 16 Generic registers312uint32 vColorReg; // Internal color register313uint32 vPlotOptionReg; // Plot option register314uint32 vStatusReg; // Status register315uint32 vPrgBankReg; // Program bank index register316uint32 vRomBankReg; // Rom bank index register317uint32 vRamBankReg; // Ram bank index register318uint32 vCacheBaseReg; // Cache base address register319uint32 vCacheFlags; // Saying what parts of the cache was written to320uint32 vLastRamAdr; // Last RAM address accessed321uint32 *pvDreg; // Pointer to current destination register322uint32 *pvSreg; // Pointer to current source register323uint8 vRomBuffer; // Current byte read by R14324uint8 vPipe; // Instructionset pipe325uint32 vPipeAdr; // The address of where the pipe was read from326327// Status register optimization stuff328uint32 vSign; // v & 0x8000329uint32 vZero; // v == 0330uint32 vCarry; // a value of 1 or 0331int32 vOverflow; // (v >= 0x8000 || v < -0x8000)332333// Other emulator variables334int32 vErrorCode;335uint32 vIllegalAddress;336337uint8 bBreakPoint;338uint32 vBreakPoint;339uint32 vStepPoint;340341uint8 *pvRegisters; // 768 bytes located in the memory at address 0x3000342uint32 nRamBanks; // Number of 64kb-banks in FxRam (Don't confuse it with SNES-Ram!!!)343uint8 *pvRam; // Pointer to FxRam344uint32 nRomBanks; // Number of 32kb-banks in Cart-ROM345uint8 *pvRom; // Pointer to Cart-ROM346347uint32 vMode; // Color depth/mode348uint32 vPrevMode; // Previous depth349uint8 *pvScreenBase;350uint8 *apvScreen[32]; // Pointer to each of the 32 screen colums351int32 x[32];352uint32 vScreenHeight; // 128, 160, 192 or 256 (could be overriden by cmode)353uint32 vScreenRealHeight; // 128, 160, 192 or 256354uint32 vPrevScreenHeight;355uint32 vScreenSize;356void (*pfPlot) (void);357void (*pfRpix) (void);358359uint8 *pvRamBank; // Pointer to current RAM-bank360uint8 *pvRomBank; // Pointer to current ROM-bank361uint8 *pvPrgBank; // Pointer to current program ROM-bank362363uint8 *apvRamBank[FX_RAM_BANKS]; // Ram bank table (max 256kb)364uint8 *apvRomBank[256]; // Rom bank table365366uint8 bCacheActive;367uint8 *pvCache; // Pointer to the GSU cache368uint8 avCacheBackup[512]; // Backup of ROM when the cache has replaced it369uint32 vCounter;370uint32 vInstCount;371uint32 vSCBRDirty; // If SCBR is written, our cached screen pointers need updating372373uint8 *avRegAddr; // To reference avReg in snapshot.cpp374};375376extern struct FxRegs_s GSU;377378// GSU registers379#define GSU_R0 0x000380#define GSU_R1 0x002381#define GSU_R2 0x004382#define GSU_R3 0x006383#define GSU_R4 0x008384#define GSU_R5 0x00a385#define GSU_R6 0x00c386#define GSU_R7 0x00e387#define GSU_R8 0x010388#define GSU_R9 0x012389#define GSU_R10 0x014390#define GSU_R11 0x016391#define GSU_R12 0x018392#define GSU_R13 0x01a393#define GSU_R14 0x01c394#define GSU_R15 0x01e395#define GSU_SFR 0x030396#define GSU_BRAMR 0x033397#define GSU_PBR 0x034398#define GSU_ROMBR 0x036399#define GSU_CFGR 0x037400#define GSU_SCBR 0x038401#define GSU_CLSR 0x039402#define GSU_SCMR 0x03a403#define GSU_VCR 0x03b404#define GSU_RAMBR 0x03c405#define GSU_CBR 0x03e406#define GSU_CACHERAM 0x100407408// SFR flags409#define FLG_Z (1 << 1)410#define FLG_CY (1 << 2)411#define FLG_S (1 << 3)412#define FLG_OV (1 << 4)413#define FLG_G (1 << 5)414#define FLG_R (1 << 6)415#define FLG_ALT1 (1 << 8)416#define FLG_ALT2 (1 << 9)417#define FLG_IL (1 << 10)418#define FLG_IH (1 << 11)419#define FLG_B (1 << 12)420#define FLG_IRQ (1 << 15)421422// Test flag423#define TF(a) (GSU.vStatusReg & FLG_##a)424#define CF(a) (GSU.vStatusReg &= ~FLG_##a)425#define SF(a) (GSU.vStatusReg |= FLG_##a)426427// Test and set flag if condition, clear if not428#define TS(a, b) GSU.vStatusReg = ((GSU.vStatusReg & (~FLG_##a)) | ((!!(##b)) * FLG_##a))429430// Testing ALT1 & ALT2 bits431#define ALT0 (!TF(ALT1) && !TF(ALT2))432#define ALT1 ( TF(ALT1) && !TF(ALT2))433#define ALT2 (!TF(ALT1) && TF(ALT2))434#define ALT3 ( TF(ALT1) && TF(ALT2))435436// Sign extend from 8/16 bit to 32 bit437#define SEX8(a) ((int32) ((int8) (a)))438#define SEX16(a) ((int32) ((int16) (a)))439440// Unsign extend from 8/16 bit to 32 bit441#define USEX8(a) ((uint32) ((uint8) (a)))442#define USEX16(a) ((uint32) ((uint16) (a)))443#define SUSEX16(a) ((int32) ((uint16) (a)))444445// Set/Clr Sign and Zero flag446#define TSZ(num) TS(S, ((num) & 0x8000)); TS(Z, (!USEX16(num)))447448// Clear flags449#define CLRFLAGS GSU.vStatusReg &= ~(FLG_ALT1 | FLG_ALT2 | FLG_B); GSU.pvDreg = GSU.pvSreg = &R0450451// Read current RAM-Bank452#define RAM(adr) GSU.pvRamBank[USEX16(adr)]453454// Read current ROM-Bank455#define ROM(idx) GSU.pvRomBank[USEX16(idx)]456457// Access the current value in the pipe458#define PIPE GSU.vPipe459460// Access data in the current program bank461#define PRGBANK(idx) GSU.pvPrgBank[USEX16(idx)]462463// Update pipe from ROM464#if 0465#define FETCHPIPE { PIPE = PRGBANK(R15); GSU.vPipeAdr = (GSU.vPrgBankReg << 16) + R15; }466#else467#define FETCHPIPE { PIPE = PRGBANK(R15); }468#endif469470// ABS471#define ABS(x) ((x) < 0 ? -(x) : (x))472473// Access source register474#define SREG (*GSU.pvSreg)475476// Access destination register477#define DREG (*GSU.pvDreg)478479#ifndef FX_DO_ROMBUFFER480481// Don't read R14482#define READR14483484// Don't test and/or read R14485#define TESTR14486487#else488489// Read R14490#define READR14 GSU.vRomBuffer = ROM(R14)491492// Test and/or read R14493#define TESTR14 if (GSU.pvDreg == &R14) READR14494495#endif496497// Access to registers498#define R0 GSU.avReg[0]499#define R1 GSU.avReg[1]500#define R2 GSU.avReg[2]501#define R3 GSU.avReg[3]502#define R4 GSU.avReg[4]503#define R5 GSU.avReg[5]504#define R6 GSU.avReg[6]505#define R7 GSU.avReg[7]506#define R8 GSU.avReg[8]507#define R9 GSU.avReg[9]508#define R10 GSU.avReg[10]509#define R11 GSU.avReg[11]510#define R12 GSU.avReg[12]511#define R13 GSU.avReg[13]512#define R14 GSU.avReg[14]513#define R15 GSU.avReg[15]514#define SFR GSU.vStatusReg515#define PBR GSU.vPrgBankReg516#define ROMBR GSU.vRomBankReg517#define RAMBR GSU.vRamBankReg518#define CBR GSU.vCacheBaseReg519#define SCBR USEX8(GSU.pvRegisters[GSU_SCBR])520#define SCMR USEX8(GSU.pvRegisters[GSU_SCMR])521#define COLR GSU.vColorReg522#define POR GSU.vPlotOptionReg523#define BRAMR USEX8(GSU.pvRegisters[GSU_BRAMR])524#define VCR USEX8(GSU.pvRegisters[GSU_VCR])525#define CFGR USEX8(GSU.pvRegisters[GSU_CFGR])526#define CLSR USEX8(GSU.pvRegisters[GSU_CLSR])527528// Execute instruction from the pipe, and fetch next byte to the pipe529#define FX_STEP \530{ \531uint32 vOpcode = (uint32) PIPE; \532FETCHPIPE; \533(*fx_OpcodeTable[(GSU.vStatusReg & 0x300) | vOpcode])(); \534}535536extern void (*fx_PlotTable[]) (void);537extern void (*fx_OpcodeTable[]) (void);538539// Set this define if branches are relative to the instruction in the delay slot (I think they are)540#define BRANCH_DELAY_RELATIVE541542#endif543544545