From 7b8a61b8e4d25a4be039aca316e3671c67afe22b Mon Sep 17 00:00:00 2001 From: alyosha-tas Date: Thu, 26 Oct 2017 09:58:24 -0400 Subject: [PATCH] NESHawk Cleanup --- .../Consoles/Nintendo/NES/APU.cs | 5 +- .../Consoles/Nintendo/NES/NES.Core.cs | 12 +- .../Nintendo/NES/NES.ICodeDataLogger.cs | 113 ++++++++++++++++++ .../Consoles/Nintendo/NES/NES.IStatable.cs | 1 - .../Consoles/Nintendo/NES/NES.cs | 9 +- .../Consoles/Nintendo/NES/PPU.cs | 11 ++ .../Consoles/Nintendo/NES/PPU.regs.cs | 12 +- .../Consoles/Nintendo/NES/PPU.run.cs | 36 ++---- 8 files changed, 144 insertions(+), 55 deletions(-) create mode 100644 BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.ICodeDataLogger.cs diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/APU.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/APU.cs index 2b755f983c..72cbcba367 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/APU.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/APU.cs @@ -20,8 +20,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES { public sealed class APU { - public static bool CFG_DECLICK = true; - public int m_vol = 1; public int dmc_dma_countdown = -1; @@ -1330,8 +1328,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES } SyncIRQ(); - nes.irq_apu = irq_pending; - + // since the units run concurrently, the APU frame sequencer is ran last because // it can change the ouput values of the pulse/triangle channels // we want the changes to affect it on the *next* cycle. diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.Core.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.Core.cs index 67617348eb..69943117e5 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.Core.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.Core.cs @@ -13,7 +13,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES { //hardware/state public MOS6502X cpu; - int cpu_accumulate; //cpu timekeeper public PPU ppu; public APU apu; public byte[] ram; @@ -26,12 +25,10 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES int sprdma_countdown; bool _irq_apu; //various irq signals that get merged to the cpu irq pin - /// clock speed of the main cpu in hz + + /// clock speed of the main cpu in hz public int cpuclockrate { get; private set; } - //irq state management - public bool irq_apu { get { return _irq_apu; } set { _irq_apu = value; } } - //user configuration int[] palette_compiled = new int[64 * 8]; @@ -362,8 +359,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES VS_coin_inserted &= 1; } - FrameGo = true; - if (ppu.ppudead > 0) { while (ppu.ppudead > 0) @@ -416,10 +411,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES num_cheats = 0; } - public bool FrameGo; - //PAL: - //0 15 30 45 60 -> 12 27 42 57 -> 9 24 39 54 -> 6 21 36 51 -> 3 18 33 48 -> 0 //sequence of ppu clocks per cpu clock: 3,3,3,3,4 //at least it should be, but something is off with that (start up time?) so it is 3,3,3,4,3 for now //NTSC: diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.ICodeDataLogger.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.ICodeDataLogger.cs new file mode 100644 index 0000000000..c1f2a5b4fa --- /dev/null +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.ICodeDataLogger.cs @@ -0,0 +1,113 @@ +using System; +using System.IO; +using BizHawk.Emulation.Common; + +namespace BizHawk.Emulation.Cores.Nintendo.NES +{ + public sealed partial class NES : ICodeDataLogger + { + public void SetCDL(ICodeDataLog cdl) + { + CDL = cdl; + if (cdl == null) + { + cpu.ReadMemory = ReadMemory; + cpu.WriteMemory = WriteMemory; + cpu.PeekMemory = PeekMemory; + } + else + { + cpu.ReadMemory = ReadMemory_CDL; + cpu.WriteMemory = WriteMemory; + cpu.PeekMemory = FetchMemory_CDL; + } + } + + public void NewCDL(ICodeDataLog cdl) + { + cdl["RAM"] = new byte[_memoryDomains["RAM"].Size]; + + if (_memoryDomains.Has("Save RAM")) + { + cdl["Save RAM"] = new byte[_memoryDomains["Save RAM"].Size]; + } + + if (_memoryDomains.Has("Battery RAM")) + { + cdl["Battery RAM"] = new byte[_memoryDomains["Battery RAM"].Size]; + } + + if (_memoryDomains.Has("Battery RAM")) + { + cdl["Battery RAM"] = new byte[_memoryDomains["Battery RAM"].Size]; + } + + cdl.SubType = "NES"; + cdl.SubVer = 0; + } + + [FeatureNotImplemented] + public void DisassembleCDL(Stream s, ICodeDataLog cdl) + { + + } + + private enum CDLog_AddrType + { + None, + ROM, + MainRAM, + SaveRAM, + } + + [Flags] + private enum CDLog_Flags + { + ExecFirst = 0x01, + ExecOperand = 0x02, + Data = 0x04 + }; + + private struct CDLog_MapResults + { + public CDLog_AddrType Type; + public int Address; + } + + private delegate CDLog_MapResults MapMemoryDelegate(ushort addr, bool write); + private MapMemoryDelegate MapMemory; + private ICodeDataLog CDL; + + private void RunCDL(ushort address, CDLog_Flags flags) + { + if (MapMemory != null) + { + CDLog_MapResults results = MapMemory(address, false); + switch (results.Type) + { + case CDLog_AddrType.None: break; + case CDLog_AddrType.MainRAM: CDL["Main RAM"][results.Address] |= (byte)flags; break; + case CDLog_AddrType.SaveRAM: CDL["Save RAM"][results.Address] |= (byte)flags; break; + } + } + } + + /// + /// A wrapper for FetchMemory which inserts CDL logic + /// + private byte FetchMemory_CDL(ushort address) + { + RunCDL(address, CDLog_Flags.ExecFirst); + return PeekMemory(address); + } + + /// + /// A wrapper for ReadMemory which inserts CDL logic + /// + private byte ReadMemory_CDL(ushort address) + { + RunCDL(address, CDLog_Flags.Data); + return ReadMemory(address); + } + } +} \ No newline at end of file diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.IStatable.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.IStatable.cs index e5ac4f54b9..e2dd212602 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.IStatable.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.IStatable.cs @@ -57,7 +57,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES cpu.SyncState(ser); ser.Sync("ram", ref ram, false); ser.Sync("CIRAM", ref CIRAM, false); - ser.Sync("cpu_accumulate", ref cpu_accumulate); ser.Sync("_irq_apu", ref _irq_apu); ser.Sync("sprdma_countdown", ref sprdma_countdown); ser.Sync("cpu_deadcounter", ref cpu_deadcounter); diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs index 3549facdd3..c5e1fbc725 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs @@ -15,7 +15,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES isPorted: false, isReleased: true)] public partial class NES : IEmulator, ISaveRam, IDebuggable, IStatable, IInputPollable, IRegionable, - IBoardInfo, ISettable + IBoardInfo, ISettable, ICodeDataLogger { [CoreConstructor("NES")] public NES(CoreComm comm, GameInfo game, byte[] rom, object settings, object syncSettings) @@ -876,10 +876,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES //todo //http://blog.ntrq.net/?p=428 -//cpu bus junk bits - -//UBER DOC -//http://nocash.emubase.de/everynes.htm //A VERY NICE board assignments list //http://personales.epsg.upv.es/~jogilmo1/nes/TEXTOS/ARXIUS/BOARDTABLE.TXT @@ -889,6 +885,3 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES //a mappers list //http://tuxnes.sourceforge.net/nesmapper.txt - -//some ppu tests -//http://nesdev.parodius.com/bbs/viewtopic.php?p=4571&sid=db4c7e35316cc5d734606dd02f11dccb diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/PPU.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/PPU.cs index a8484758c0..320113d52f 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/PPU.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/PPU.cs @@ -218,12 +218,22 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES ser.Sync("VRAMBuffer", ref VRAMBuffer); ser.Sync("ppu_addr_temp", ref ppu_addr_temp); + ser.Sync("spr_true_count", ref spr_true_count); + ser.Sync("sprite_eval_write", ref sprite_eval_write); ser.Sync("Read_Value", ref read_value); ser.Sync("Prev_soam_index", ref soam_index_prev); ser.Sync("Spr_Zero_Go", ref sprite_zero_go); ser.Sync("Spr_zero_in_Range", ref sprite_zero_in_range); ser.Sync("Is_even_cycle", ref is_even_cycle); ser.Sync("soam_index", ref soam_index); + ser.Sync("soam_m_index", ref soam_m_index); + ser.Sync("oam_index", ref oam_index); + ser.Sync("oam_index_aux", ref oam_index_aux); + ser.Sync("soam_index_aux", ref soam_index_aux); + ser.Sync("yp", ref yp); + ser.Sync("target", ref target); + ser.Sync("ppu_was_on", ref ppu_was_on); + ser.Sync("spriteHeight", ref spriteHeight); ser.Sync("install_2006", ref install_2006); ser.Sync("race_2006", ref race_2006); ser.Sync("install_2001", ref install_2001); @@ -236,6 +246,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES ser.Sync("glitchy_reads_2003", ref glitchy_reads_2003, false); ser.Sync("OAM", ref OAM, false); + ser.Sync("soam", ref soam, false); ser.Sync("PALRAM", ref PALRAM, false); ser.Sync("Reg2002_objoverflow", ref Reg2002_objoverflow); diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/PPU.regs.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/PPU.regs.cs index 28b54729da..a0aed3f502 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/PPU.regs.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/PPU.regs.cs @@ -1,10 +1,10 @@ //blargg: Reading from $2007 when the VRAM address is $3fxx will fill the internal read buffer with the contents at VRAM address $3fxx, in addition to reading the palette RAM. - //static const byte powerUpPalette[] = - //{ - // 0x3F,0x01,0x00,0x01, 0x00,0x02,0x02,0x0D, 0x08,0x10,0x08,0x24, 0x00,0x00,0x04,0x2C, - // 0x09,0x01,0x34,0x03, 0x00,0x04,0x00,0x14, 0x08,0x3A,0x00,0x02, 0x00,0x20,0x2C,0x08 - //}; +//static const byte powerUpPalette[] = +//{ +// 0x3F,0x01,0x00,0x01, 0x00,0x02,0x02,0x0D, 0x08,0x10,0x08,0x24, 0x00,0x00,0x04,0x2C, +// 0x09,0x01,0x34,0x03, 0x00,0x04,0x00,0x14, 0x08,0x3A,0x00,0x02, 0x00,0x20,0x2C,0x08 +//}; using System; using BizHawk.Common; @@ -90,8 +90,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES ser.Sync("_ht", ref _ht); ser.Sync("fh", ref fh); ser.Sync("status.cycle", ref status.cycle); - int junk = 0; - ser.Sync("status.end_cycle", ref junk); ser.Sync("status.sl", ref status.sl); } diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/PPU.run.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/PPU.run.cs index 7886a1ab22..768c95f13b 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/PPU.run.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/PPU.run.cs @@ -8,9 +8,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES { sealed partial class PPU { - const int kFetchTime = 2; - const int kLineTime = 341; - struct BGDataRecord { public byte nt, at; @@ -29,8 +26,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES public int soam_index_prev; public int soam_m_index; public int oam_index; - public byte read_value_aux; - public int soam_m_index_aux; public int oam_index_aux; public int soam_index_aux; public bool is_even_cycle; @@ -39,8 +34,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES public int yp; public int target; public int spriteHeight; - public byte[] soam = new byte[512]; // in a real nes, this would only be 32, but we wish to allow more then 8 sprites per scanline - public bool reg_2001_color_disable_latch; // the value used here is taken + public byte[] soam = new byte[256]; // in a real nes, this would only be 32, but we wish to allow more then 8 sprites per scanline public bool ppu_was_on; public byte[] sl_sprites = new byte[3 * 256]; @@ -69,7 +63,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES // experimental int pixelcolor_latch_1; int pixelcolor_latch_2; - void pipeline(int pixelcolor, int target, int row_check) + void pipeline(int pixelcolor, int row_check) { if (row_check > 1) { @@ -81,11 +75,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES xbuf[(target - 2)] = (short)(pixelcolor_latch_2 | reg_2001.intensity_lsl_6); } - if (row_check >= 1) - { - pixelcolor_latch_2 = pixelcolor_latch_1; - } - + pixelcolor_latch_2 = pixelcolor_latch_1; pixelcolor_latch_1 = pixelcolor; } @@ -203,7 +193,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES spr_true_count = 0; soam_index = 0; soam_m_index = 0; - soam_m_index_aux = 0; oam_index_aux = 0; oam_index = 0; is_even_cycle = true; @@ -252,7 +241,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES if (ppur.status.sl != 0) { ///////////////////////////////////////////// - // Sprite Evaluation End + // Sprite Evaluation Start ///////////////////////////////////////////// if (sprite_eval_cycle <= 63 && !is_even_cycle) @@ -479,7 +468,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES } //oamcount loop - pipeline(pixelcolor, target, xt * 8 + xp); + pipeline(pixelcolor, xt * 8 + xp); target++; // clear out previous sprites from scanline buffer @@ -557,8 +546,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES { //look for sprites soam[soam_index_aux * 4] = OAM[oam_index_aux * 4]; - read_value_aux = OAM[oam_index_aux * 4]; - if (yp >= read_value_aux && yp < read_value_aux + spriteHeight) + if (yp >= OAM[oam_index_aux * 4] && yp < OAM[oam_index_aux * 4] + spriteHeight) { soam[soam_index_aux * 4 + 1] = OAM[oam_index_aux * 4 + 1]; soam[soam_index_aux * 4 + 2] = OAM[oam_index_aux * 4 + 2]; @@ -637,7 +625,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES if (target <= 61441 && target > 0 && s == 0) { - pipeline(0, target, 256); + pipeline(0, 256); target++; } @@ -648,14 +636,14 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES if (target <= 61441 && target > 0 && s == 0) { - pipeline(0, target, 257); // last pipeline call option 1 of 2 + pipeline(0, 257); // last pipeline call option 1 of 2 } } else { if (target <= 61441 && target > 0 && s == 0) { - pipeline(0, target, 256); + pipeline(0, 256); target++; } @@ -664,7 +652,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES if (target <= 61441 && target > 0 && s == 0) { - pipeline(0, target, 257); // last pipeline call option 2 of 2 + pipeline(0, 257); // last pipeline call option 2 of 2 } } break; @@ -772,7 +760,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES sl_sprites[temp_x + i] = (byte)s; sl_sprites[256 + temp_x + i] = (byte)spixel; sl_sprites[512 + temp_x + i] = t_oam[s].oam_attr; - } } } @@ -870,7 +857,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES sl_sprites[temp_x + i] = (byte)s; sl_sprites[256 + temp_x + i] = (byte)spixel; sl_sprites[512 + temp_x + i] = t_oam[s].oam_attr; - } } } @@ -974,7 +960,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES { runppu(); - if (ppur.status.cycle == 241 * kLineTime - 3) + if (ppur.status.cycle == 241 * 341 - 3) { ppudead--; }