Path: blob/main/misc/emulator/xnes/snes9x/cpuaddr.h
28550 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 _CPUADDR_H_179#define _CPUADDR_H_180181typedef enum182{183NONE = 0,184READ = 1,185WRITE = 2,186MODIFY = 3,187JUMP = 5,188JSR = 8189} AccessMode;190191static inline uint8 Immediate8Slow (AccessMode a)192{193uint8 val = S9xGetByte(Registers.PBPC);194if (a & READ)195OpenBus = val;196Registers.PCw++;197198return (val);199}200201static inline uint8 Immediate8 (AccessMode a)202{203uint8 val = CPU.PCBase[Registers.PCw];204if (a & READ)205OpenBus = val;206AddCycles(CPU.MemSpeed);207Registers.PCw++;208209return (val);210}211212static inline uint16 Immediate16Slow (AccessMode a)213{214uint16 val = S9xGetWord(Registers.PBPC, WRAP_BANK);215if (a & READ)216OpenBus = (uint8) (val >> 8);217Registers.PCw += 2;218219return (val);220}221222static inline uint16 Immediate16 (AccessMode a)223{224uint16 val = READ_WORD(CPU.PCBase + Registers.PCw);225if (a & READ)226OpenBus = (uint8) (val >> 8);227AddCycles(CPU.MemSpeedx2);228Registers.PCw += 2;229230return (val);231}232233static inline uint32 RelativeSlow (AccessMode a) // branch $xx234{235int8 offset = Immediate8Slow(a);236237return ((int16) Registers.PCw + offset) & 0xffff;238}239240static inline uint32 Relative (AccessMode a) // branch $xx241{242int8 offset = Immediate8(a);243244return ((int16) Registers.PCw + offset) & 0xffff;245}246247static inline uint32 RelativeLongSlow (AccessMode a) // BRL $xxxx248{249int16 offset = Immediate16Slow(a);250251return ((int32) Registers.PCw + offset) & 0xffff;252}253254static inline uint32 RelativeLong (AccessMode a) // BRL $xxxx255{256int16 offset = Immediate16(a);257258return ((int32) Registers.PCw + offset) & 0xffff;259}260261static inline uint32 AbsoluteIndexedIndirectSlow (AccessMode a) // (a,X)262{263uint16 addr;264265if (a & JSR)266{267// JSR (a,X) pushes the old address in the middle of loading the new.268// OpenBus needs to be set to account for this.269addr = Immediate8Slow(READ);270if (a == JSR)271OpenBus = Registers.PCl;272addr |= Immediate8Slow(READ) << 8;273}274else275addr = Immediate16Slow(READ);276277AddCycles(ONE_CYCLE);278addr += Registers.X.W;279280// Address load wraps within the bank281uint16 addr2 = S9xGetWord(ICPU.ShiftedPB | addr, WRAP_BANK);282OpenBus = addr2 >> 8;283284return (addr2);285}286287static inline uint32 AbsoluteIndexedIndirect (AccessMode a) // (a,X)288{289uint16 addr = Immediate16Slow(READ);290addr += Registers.X.W;291292// Address load wraps within the bank293uint16 addr2 = S9xGetWord(ICPU.ShiftedPB | addr, WRAP_BANK);294OpenBus = addr2 >> 8;295296return (addr2);297}298299static inline uint32 AbsoluteIndirectLongSlow (AccessMode a) // [a]300{301uint16 addr = Immediate16Slow(READ);302303// No info on wrapping, but it doesn't matter anyway due to mirroring304uint32 addr2 = S9xGetWord(addr);305OpenBus = addr2 >> 8;306addr2 |= (OpenBus = S9xGetByte(addr + 2)) << 16;307308return (addr2);309}310311static inline uint32 AbsoluteIndirectLong (AccessMode a) // [a]312{313uint16 addr = Immediate16(READ);314315// No info on wrapping, but it doesn't matter anyway due to mirroring316uint32 addr2 = S9xGetWord(addr);317OpenBus = addr2 >> 8;318addr2 |= (OpenBus = S9xGetByte(addr + 2)) << 16;319320return (addr2);321}322323static inline uint32 AbsoluteIndirectSlow (AccessMode a) // (a)324{325// No info on wrapping, but it doesn't matter anyway due to mirroring326uint16 addr2 = S9xGetWord(Immediate16Slow(READ));327OpenBus = addr2 >> 8;328329return (addr2);330}331332static inline uint32 AbsoluteIndirect (AccessMode a) // (a)333{334// No info on wrapping, but it doesn't matter anyway due to mirroring335uint16 addr2 = S9xGetWord(Immediate16(READ));336OpenBus = addr2 >> 8;337338return (addr2);339}340341static inline uint32 AbsoluteSlow (AccessMode a) // a342{343return (ICPU.ShiftedDB | Immediate16Slow(a));344}345346static inline uint32 Absolute (AccessMode a) // a347{348return (ICPU.ShiftedDB | Immediate16(a));349}350351static inline uint32 AbsoluteLongSlow (AccessMode a) // l352{353uint32 addr = Immediate16Slow(READ);354355// JSR l pushes the old bank in the middle of loading the new.356// OpenBus needs to be set to account for this.357if (a == JSR)358OpenBus = Registers.PB;359360addr |= Immediate8Slow(a) << 16;361362return (addr);363}364365static inline uint32 AbsoluteLong (AccessMode a) // l366{367uint32 addr = READ_3WORD(CPU.PCBase + Registers.PCw);368AddCycles(CPU.MemSpeedx2 + CPU.MemSpeed);369if (a & READ)370OpenBus = addr >> 16;371Registers.PCw += 3;372373return (addr);374}375376static inline uint32 DirectSlow (AccessMode a) // d377{378uint16 addr = Immediate8Slow(a) + Registers.D.W;379if (Registers.DL != 0)380AddCycles(ONE_CYCLE);381382return (addr);383}384385static inline uint32 Direct (AccessMode a) // d386{387uint16 addr = Immediate8(a) + Registers.D.W;388if (Registers.DL != 0)389AddCycles(ONE_CYCLE);390391return (addr);392}393394static inline uint32 DirectIndirectSlow (AccessMode a) // (d)395{396uint32 addr = S9xGetWord(DirectSlow(READ), (!CheckEmulation() || Registers.DL) ? WRAP_BANK : WRAP_PAGE);397if (a & READ)398OpenBus = (uint8) (addr >> 8);399addr |= ICPU.ShiftedDB;400401return (addr);402}403404static inline uint32 DirectIndirectE0 (AccessMode a) // (d)405{406uint32 addr = S9xGetWord(Direct(READ));407if (a & READ)408OpenBus = (uint8) (addr >> 8);409addr |= ICPU.ShiftedDB;410411return (addr);412}413414static inline uint32 DirectIndirectE1 (AccessMode a) // (d)415{416uint32 addr = S9xGetWord(DirectSlow(READ), Registers.DL ? WRAP_BANK : WRAP_PAGE);417if (a & READ)418OpenBus = (uint8) (addr >> 8);419addr |= ICPU.ShiftedDB;420421return (addr);422}423424static inline uint32 DirectIndirectIndexedSlow (AccessMode a) // (d),Y425{426uint32 addr = DirectIndirectSlow(a);427if (a & WRITE || !CheckIndex() || (addr & 0xff) + Registers.YL >= 0x100)428AddCycles(ONE_CYCLE);429430return (addr + Registers.Y.W);431}432433static inline uint32 DirectIndirectIndexedE0X0 (AccessMode a) // (d),Y434{435uint32 addr = DirectIndirectE0(a);436AddCycles(ONE_CYCLE);437438return (addr + Registers.Y.W);439}440441static inline uint32 DirectIndirectIndexedE0X1 (AccessMode a) // (d),Y442{443uint32 addr = DirectIndirectE0(a);444if (a & WRITE || (addr & 0xff) + Registers.YL >= 0x100)445AddCycles(ONE_CYCLE);446447return (addr + Registers.Y.W);448}449450static inline uint32 DirectIndirectIndexedE1 (AccessMode a) // (d),Y451{452uint32 addr = DirectIndirectE1(a);453if (a & WRITE || (addr & 0xff) + Registers.YL >= 0x100)454AddCycles(ONE_CYCLE);455456return (addr + Registers.Y.W);457}458459static inline uint32 DirectIndirectLongSlow (AccessMode a) // [d]460{461uint16 addr = DirectSlow(READ);462uint32 addr2 = S9xGetWord(addr);463OpenBus = addr2 >> 8;464addr2 |= (OpenBus = S9xGetByte(addr + 2)) << 16;465466return (addr2);467}468469static inline uint32 DirectIndirectLong (AccessMode a) // [d]470{471uint16 addr = Direct(READ);472uint32 addr2 = S9xGetWord(addr);473OpenBus = addr2 >> 8;474addr2 |= (OpenBus = S9xGetByte(addr + 2)) << 16;475476return (addr2);477}478479static inline uint32 DirectIndirectIndexedLongSlow (AccessMode a) // [d],Y480{481return (DirectIndirectLongSlow(a) + Registers.Y.W);482}483484static inline uint32 DirectIndirectIndexedLong (AccessMode a) // [d],Y485{486return (DirectIndirectLong(a) + Registers.Y.W);487}488489static inline uint32 DirectIndexedXSlow (AccessMode a) // d,X490{491pair addr;492addr.W = DirectSlow(a);493if (!CheckEmulation() || Registers.DL)494addr.W += Registers.X.W;495else496addr.B.l += Registers.XL;497498AddCycles(ONE_CYCLE);499500return (addr.W);501}502503static inline uint32 DirectIndexedXE0 (AccessMode a) // d,X504{505uint16 addr = Direct(a) + Registers.X.W;506AddCycles(ONE_CYCLE);507508return (addr);509}510511static inline uint32 DirectIndexedXE1 (AccessMode a) // d,X512{513if (Registers.DL)514return (DirectIndexedXE0(a));515else516{517pair addr;518addr.W = Direct(a);519addr.B.l += Registers.XL;520AddCycles(ONE_CYCLE);521522return (addr.W);523}524}525526static inline uint32 DirectIndexedYSlow (AccessMode a) // d,Y527{528pair addr;529addr.W = DirectSlow(a);530if (!CheckEmulation() || Registers.DL)531addr.W += Registers.Y.W;532else533addr.B.l += Registers.YL;534535AddCycles(ONE_CYCLE);536537return (addr.W);538}539540static inline uint32 DirectIndexedYE0 (AccessMode a) // d,Y541{542uint16 addr = Direct(a) + Registers.Y.W;543AddCycles(ONE_CYCLE);544545return (addr);546}547548static inline uint32 DirectIndexedYE1 (AccessMode a) // d,Y549{550if (Registers.DL)551return (DirectIndexedYE0(a));552else553{554pair addr;555addr.W = Direct(a);556addr.B.l += Registers.YL;557AddCycles(ONE_CYCLE);558559return (addr.W);560}561}562563static inline uint32 DirectIndexedIndirectSlow (AccessMode a) // (d,X)564{565uint32 addr = S9xGetWord(DirectIndexedXSlow(READ), (!CheckEmulation() || Registers.DL) ? WRAP_BANK : WRAP_PAGE);566if (a & READ)567OpenBus = (uint8) (addr >> 8);568569return (ICPU.ShiftedDB | addr);570}571572static inline uint32 DirectIndexedIndirectE0 (AccessMode a) // (d,X)573{574uint32 addr = S9xGetWord(DirectIndexedXE0(READ));575if (a & READ)576OpenBus = (uint8) (addr >> 8);577578return (ICPU.ShiftedDB | addr);579}580581static inline uint32 DirectIndexedIndirectE1 (AccessMode a) // (d,X)582{583uint32 addr = S9xGetWord(DirectIndexedXE1(READ), Registers.DL ? WRAP_BANK : WRAP_PAGE);584if (a & READ)585OpenBus = (uint8) (addr >> 8);586587return (ICPU.ShiftedDB | addr);588}589590static inline uint32 AbsoluteIndexedXSlow (AccessMode a) // a,X591{592uint32 addr = AbsoluteSlow(a);593if (a & WRITE || !CheckIndex() || (addr & 0xff) + Registers.XL >= 0x100)594AddCycles(ONE_CYCLE);595596return (addr + Registers.X.W);597}598599static inline uint32 AbsoluteIndexedXX0 (AccessMode a) // a,X600{601uint32 addr = Absolute(a);602AddCycles(ONE_CYCLE);603604return (addr + Registers.X.W);605}606607static inline uint32 AbsoluteIndexedXX1 (AccessMode a) // a,X608{609uint32 addr = Absolute(a);610if (a & WRITE || (addr & 0xff) + Registers.XL >= 0x100)611AddCycles(ONE_CYCLE);612613return (addr + Registers.X.W);614}615616static inline uint32 AbsoluteIndexedYSlow (AccessMode a) // a,Y617{618uint32 addr = AbsoluteSlow(a);619if (a & WRITE || !CheckIndex() || (addr & 0xff) + Registers.YL >= 0x100)620AddCycles(ONE_CYCLE);621622return (addr + Registers.Y.W);623}624625static inline uint32 AbsoluteIndexedYX0 (AccessMode a) // a,Y626{627uint32 addr = Absolute(a);628AddCycles(ONE_CYCLE);629630return (addr + Registers.Y.W);631}632633static inline uint32 AbsoluteIndexedYX1 (AccessMode a) // a,Y634{635uint32 addr = Absolute(a);636if (a & WRITE || (addr & 0xff) + Registers.YL >= 0x100)637AddCycles(ONE_CYCLE);638639return (addr + Registers.Y.W);640}641642static inline uint32 AbsoluteLongIndexedXSlow (AccessMode a) // l,X643{644return (AbsoluteLongSlow(a) + Registers.X.W);645}646647static inline uint32 AbsoluteLongIndexedX (AccessMode a) // l,X648{649return (AbsoluteLong(a) + Registers.X.W);650}651652static inline uint32 StackRelativeSlow (AccessMode a) // d,S653{654uint16 addr = Immediate8Slow(a) + Registers.S.W;655AddCycles(ONE_CYCLE);656657return (addr);658}659660static inline uint32 StackRelative (AccessMode a) // d,S661{662uint16 addr = Immediate8(a) + Registers.S.W;663AddCycles(ONE_CYCLE);664665return (addr);666}667668static inline uint32 StackRelativeIndirectIndexedSlow (AccessMode a) // (d,S),Y669{670uint32 addr = S9xGetWord(StackRelativeSlow(READ));671if (a & READ)672OpenBus = (uint8) (addr >> 8);673addr = (addr + Registers.Y.W + ICPU.ShiftedDB) & 0xffffff;674AddCycles(ONE_CYCLE);675676return (addr);677}678679static inline uint32 StackRelativeIndirectIndexed (AccessMode a) // (d,S),Y680{681uint32 addr = S9xGetWord(StackRelative(READ));682if (a & READ)683OpenBus = (uint8) (addr >> 8);684addr = (addr + Registers.Y.W + ICPU.ShiftedDB) & 0xffffff;685AddCycles(ONE_CYCLE);686687return (addr);688}689690#endif691692693