From 01eb150b3d3b01040c91ae0cf37f0314e131a6e0 Mon Sep 17 00:00:00 2001 From: adelikat Date: Mon, 8 May 2017 11:37:16 -0500 Subject: [PATCH] ColecoVision - cleanup --- .../Coleco/ColecoVision.IDebuggable.cs | 106 ++++++++--------- .../Consoles/Coleco/ColecoVision.IEmulator.cs | 18 +-- .../Coleco/ColecoVision.IMemoryDomains.cs | 8 +- .../Consoles/Coleco/ColecoVision.IStatable.cs | 8 +- .../Consoles/Coleco/ColecoVision.cs | 109 +++++++++--------- 5 files changed, 122 insertions(+), 127 deletions(-) diff --git a/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.IDebuggable.cs b/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.IDebuggable.cs index 62a69601dd..1c4e048e64 100644 --- a/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.IDebuggable.cs +++ b/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.IDebuggable.cs @@ -12,36 +12,36 @@ namespace BizHawk.Emulation.Cores.ColecoVision { return new Dictionary { - ["A"] = Cpu.RegisterA, - ["AF"] = Cpu.RegisterAF, - ["B"] = Cpu.RegisterB, - ["BC"] = Cpu.RegisterBC, - ["C"] = Cpu.RegisterC, - ["D"] = Cpu.RegisterD, - ["DE"] = Cpu.RegisterDE, - ["E"] = Cpu.RegisterE, - ["F"] = Cpu.RegisterF, - ["H"] = Cpu.RegisterH, - ["HL"] = Cpu.RegisterHL, - ["I"] = Cpu.RegisterI, - ["IX"] = Cpu.RegisterIX, - ["IY"] = Cpu.RegisterIY, - ["L"] = Cpu.RegisterL, - ["PC"] = Cpu.RegisterPC, - ["R"] = Cpu.RegisterR, - ["Shadow AF"] = Cpu.RegisterShadowAF, - ["Shadow BC"] = Cpu.RegisterShadowBC, - ["Shadow DE"] = Cpu.RegisterShadowDE, - ["Shadow HL"] = Cpu.RegisterShadowHL, - ["SP"] = Cpu.RegisterSP, - ["Flag C"] = Cpu.RegisterF.Bit(0), - ["Flag N"] = Cpu.RegisterF.Bit(1), - ["Flag P/V"] = Cpu.RegisterF.Bit(2), - ["Flag 3rd"] = Cpu.RegisterF.Bit(3), - ["Flag H"] = Cpu.RegisterF.Bit(4), - ["Flag 5th"] = Cpu.RegisterF.Bit(5), - ["Flag Z"] = Cpu.RegisterF.Bit(6), - ["Flag S"] = Cpu.RegisterF.Bit(7) + ["A"] = _cpu.RegisterA, + ["AF"] = _cpu.RegisterAF, + ["B"] = _cpu.RegisterB, + ["BC"] = _cpu.RegisterBC, + ["C"] = _cpu.RegisterC, + ["D"] = _cpu.RegisterD, + ["DE"] = _cpu.RegisterDE, + ["E"] = _cpu.RegisterE, + ["F"] = _cpu.RegisterF, + ["H"] = _cpu.RegisterH, + ["HL"] = _cpu.RegisterHL, + ["I"] = _cpu.RegisterI, + ["IX"] = _cpu.RegisterIX, + ["IY"] = _cpu.RegisterIY, + ["L"] = _cpu.RegisterL, + ["PC"] = _cpu.RegisterPC, + ["R"] = _cpu.RegisterR, + ["Shadow AF"] = _cpu.RegisterShadowAF, + ["Shadow BC"] = _cpu.RegisterShadowBC, + ["Shadow DE"] = _cpu.RegisterShadowDE, + ["Shadow HL"] = _cpu.RegisterShadowHL, + ["SP"] = _cpu.RegisterSP, + ["Flag C"] = _cpu.RegisterF.Bit(0), + ["Flag N"] = _cpu.RegisterF.Bit(1), + ["Flag P/V"] = _cpu.RegisterF.Bit(2), + ["Flag 3rd"] = _cpu.RegisterF.Bit(3), + ["Flag H"] = _cpu.RegisterF.Bit(4), + ["Flag 5th"] = _cpu.RegisterF.Bit(5), + ["Flag Z"] = _cpu.RegisterF.Bit(6), + ["Flag S"] = _cpu.RegisterF.Bit(7) }; } @@ -52,70 +52,70 @@ namespace BizHawk.Emulation.Cores.ColecoVision default: throw new InvalidOperationException(); case "A": - Cpu.RegisterA = (byte)value; + _cpu.RegisterA = (byte)value; break; case "AF": - Cpu.RegisterAF = (byte)value; + _cpu.RegisterAF = (byte)value; break; case "B": - Cpu.RegisterB = (byte)value; + _cpu.RegisterB = (byte)value; break; case "BC": - Cpu.RegisterBC = (byte)value; + _cpu.RegisterBC = (byte)value; break; case "C": - Cpu.RegisterC = (byte)value; + _cpu.RegisterC = (byte)value; break; case "D": - Cpu.RegisterD = (byte)value; + _cpu.RegisterD = (byte)value; break; case "DE": - Cpu.RegisterDE = (byte)value; + _cpu.RegisterDE = (byte)value; break; case "E": - Cpu.RegisterE = (byte)value; + _cpu.RegisterE = (byte)value; break; case "F": - Cpu.RegisterF = (byte)value; + _cpu.RegisterF = (byte)value; break; case "H": - Cpu.RegisterH = (byte)value; + _cpu.RegisterH = (byte)value; break; case "HL": - Cpu.RegisterHL = (byte)value; + _cpu.RegisterHL = (byte)value; break; case "I": - Cpu.RegisterI = (byte)value; + _cpu.RegisterI = (byte)value; break; case "IX": - Cpu.RegisterIX = (byte)value; + _cpu.RegisterIX = (byte)value; break; case "IY": - Cpu.RegisterIY = (byte)value; + _cpu.RegisterIY = (byte)value; break; case "L": - Cpu.RegisterL = (byte)value; + _cpu.RegisterL = (byte)value; break; case "PC": - Cpu.RegisterPC = (ushort)value; + _cpu.RegisterPC = (ushort)value; break; case "R": - Cpu.RegisterR = (byte)value; + _cpu.RegisterR = (byte)value; break; case "Shadow AF": - Cpu.RegisterShadowAF = (byte)value; + _cpu.RegisterShadowAF = (byte)value; break; case "Shadow BC": - Cpu.RegisterShadowBC = (byte)value; + _cpu.RegisterShadowBC = (byte)value; break; case "Shadow DE": - Cpu.RegisterShadowDE = (byte)value; + _cpu.RegisterShadowDE = (byte)value; break; case "Shadow HL": - Cpu.RegisterShadowHL = (byte)value; + _cpu.RegisterShadowHL = (byte)value; break; case "SP": - Cpu.RegisterSP = (byte)value; + _cpu.RegisterSP = (byte)value; break; } } @@ -130,6 +130,6 @@ namespace BizHawk.Emulation.Cores.ColecoVision throw new NotImplementedException(); } - public int TotalExecutedCycles => Cpu.TotalExecutedCycles; + public int TotalExecutedCycles => _cpu.TotalExecutedCycles; } } diff --git a/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.IEmulator.cs b/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.IEmulator.cs index 83998168f0..68d8493693 100644 --- a/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.IEmulator.cs +++ b/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.IEmulator.cs @@ -12,14 +12,14 @@ namespace BizHawk.Emulation.Cores.ColecoVision public void FrameAdvance(IController controller, bool render, bool renderSound) { _controller = controller; - Cpu.Debug = Tracer.Enabled; - frame++; + _cpu.Debug = _tracer.Enabled; + _frame++; _isLag = true; - PSG.BeginFrame(Cpu.TotalExecutedCycles); + PSG.BeginFrame(_cpu.TotalExecutedCycles); - if (Cpu.Debug && Cpu.Logger == null) // TODO, lets not do this on each frame. But lets refactor CoreComm/CoreComm first + if (_cpu.Debug && _cpu.Logger == null) // TODO, lets not do this on each frame. But lets refactor CoreComm/CoreComm first { - Cpu.Logger = (s) => Tracer.Put(s); + _cpu.Logger = (s) => _tracer.Put(s); } byte tempRet1 = ControllerDeck.ReadPort1(controller, true, true); @@ -27,9 +27,9 @@ namespace BizHawk.Emulation.Cores.ColecoVision bool intPending = (!tempRet1.Bit(4)) | (!tempRet2.Bit(4)); - VDP.ExecuteFrame(intPending); + _vdp.ExecuteFrame(intPending); - PSG.EndFrame(Cpu.TotalExecutedCycles); + PSG.EndFrame(_cpu.TotalExecutedCycles); if (_isLag) { @@ -37,7 +37,7 @@ namespace BizHawk.Emulation.Cores.ColecoVision } } - public int Frame => frame; + public int Frame => _frame; public string SystemId => "Coleco"; @@ -45,7 +45,7 @@ namespace BizHawk.Emulation.Cores.ColecoVision public void ResetCounters() { - frame = 0; + _frame = 0; _lagCount = 0; _isLag = false; } diff --git a/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.IMemoryDomains.cs b/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.IMemoryDomains.cs index 97a064c057..5223a5aff2 100644 --- a/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.IMemoryDomains.cs +++ b/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.IMemoryDomains.cs @@ -24,7 +24,7 @@ namespace BizHawk.Emulation.Cores.ColecoVision throw new ArgumentOutOfRangeException(); } - return Cpu.ReadMemory((ushort)addr); + return _cpu.ReadMemory((ushort)addr); }, (addr, value) => { @@ -33,7 +33,7 @@ namespace BizHawk.Emulation.Cores.ColecoVision throw new ArgumentOutOfRangeException(); } - Cpu.WriteMemory((ushort)addr, value); + _cpu.WriteMemory((ushort)addr, value); }, 1) }; @@ -47,8 +47,8 @@ namespace BizHawk.Emulation.Cores.ColecoVision private void SyncAllByteArrayDomains() { - SyncByteArrayDomain("Main RAM", Ram); - SyncByteArrayDomain("Video RAM", VDP.VRAM); + SyncByteArrayDomain("Main RAM", _ram); + SyncByteArrayDomain("Video RAM", _vdp.VRAM); } private void SyncByteArrayDomain(string name, byte[] data) diff --git a/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.IStatable.cs b/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.IStatable.cs index 71d825f22d..62b5297675 100644 --- a/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.IStatable.cs +++ b/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.IStatable.cs @@ -53,11 +53,11 @@ namespace BizHawk.Emulation.Cores.ColecoVision private void SyncState(Serializer ser) { ser.BeginSection("Coleco"); - Cpu.SyncState(ser); - VDP.SyncState(ser); + _cpu.SyncState(ser); + _vdp.SyncState(ser); PSG.SyncState(ser); - ser.Sync("RAM", ref Ram, false); - ser.Sync("Frame", ref frame); + ser.Sync("RAM", ref _ram, false); + ser.Sync("Frame", ref _frame); ser.Sync("LagCount", ref _lagCount); ser.Sync("IsLag", ref _isLag); ser.EndSection(); diff --git a/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.cs b/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.cs index 57f39c227c..f5b61b0b9c 100644 --- a/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.cs +++ b/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.cs @@ -15,13 +15,13 @@ namespace BizHawk.Emulation.Cores.ColecoVision [CoreConstructor("Coleco")] public ColecoVision(CoreComm comm, GameInfo game, byte[] rom, object syncSettings) { - ServiceProvider = new BasicServiceProvider(this); + var ser = new BasicServiceProvider(this); MemoryCallbacks = new MemoryCallbackSystem(); CoreComm = comm; _syncSettings = (ColecoSyncSettings)syncSettings ?? new ColecoSyncSettings(); bool skipbios = _syncSettings.SkipBiosIntro; - Cpu = new Z80A + _cpu = new Z80A { ReadMemory = ReadMemory, WriteMemory = WriteMemory, @@ -32,15 +32,15 @@ namespace BizHawk.Emulation.Cores.ColecoVision PSG = new SN76489(); _fakeSyncSound = new FakeSyncSound(PSG, 735); - (ServiceProvider as BasicServiceProvider).Register(_fakeSyncSound); + ser.Register(_fakeSyncSound); ControllerDeck = new ColecoVisionControllerDeck(_syncSettings.Port1, _syncSettings.Port2); - VDP = new TMS9918A(Cpu); - (ServiceProvider as BasicServiceProvider).Register(VDP); + _vdp = new TMS9918A(_cpu); + ser.Register(_vdp); // TODO: hack to allow bios-less operation would be nice, no idea if its feasible - BiosRom = CoreComm.CoreFileProvider.GetFirmware("Coleco", "Bios", true, "Coleco BIOS file is required."); + _biosRom = CoreComm.CoreFileProvider.GetFirmware("Coleco", "Bios", true, "Coleco BIOS file is required."); // gamedb can overwrite the syncsettings; this is ok if (game["NoSkip"]) @@ -49,45 +49,47 @@ namespace BizHawk.Emulation.Cores.ColecoVision } LoadRom(rom, skipbios); - _game = game; SetupMemoryDomains(); - Tracer.Header = Cpu.TraceHeader; - var serviceProvider = ServiceProvider as BasicServiceProvider; - serviceProvider.Register(new Disassembler()); - serviceProvider.Register(Tracer); + _tracer.Header = _cpu.TraceHeader; + ser.Register(new Disassembler()); + ser.Register(_tracer); + + ServiceProvider = ser; } - // ROM - private byte[] RomData; - private byte[] BiosRom; - - // Machine - private Z80A Cpu; - private TMS9918A VDP; - - private byte[] Ram = new byte[1024]; - private readonly TraceBuffer Tracer = new TraceBuffer(); - - public ColecoVisionControllerDeck ControllerDeck { get; private set; } - - private const ushort RamSizeMask = 0x03FF; + private readonly Z80A _cpu; + private readonly TMS9918A _vdp; + private readonly byte[] _biosRom; + private readonly TraceBuffer _tracer = new TraceBuffer(); + private byte[] _romData; + private byte[] _ram = new byte[1024]; + private int _frame; private IController _controller; + private enum InputPortMode + { + Left, Right + } + + private InputPortMode _inputPortSelection; + + public ColecoVisionControllerDeck ControllerDeck { get; } + private void LoadRom(byte[] rom, bool skipbios) { - RomData = new byte[0x8000]; + _romData = new byte[0x8000]; for (int i = 0; i < 0x8000; i++) { - RomData[i] = rom[i % rom.Length]; + _romData[i] = rom[i % rom.Length]; } // hack to skip colecovision title screen if (skipbios) { - RomData[0] = 0x55; - RomData[1] = 0xAA; + _romData[0] = 0x55; + _romData[1] = 0xAA; } } @@ -99,10 +101,10 @@ namespace BizHawk.Emulation.Cores.ColecoVision { if ((port & 1) == 0) { - return VDP.ReadData(); + return _vdp.ReadData(); } - return VDP.ReadVdpStatus(); + return _vdp.ReadVdpStatus(); } if (port >= 0xE0) @@ -126,11 +128,11 @@ namespace BizHawk.Emulation.Cores.ColecoVision { if ((port & 1) == 0) { - VDP.WriteVdpData(value); + _vdp.WriteVdpData(value); } else { - VDP.WriteVdpControl(value); + _vdp.WriteVdpControl(value); } return; @@ -138,43 +140,38 @@ namespace BizHawk.Emulation.Cores.ColecoVision if (port >= 0x80 && port <= 0x9F) { - InputPortSelection = InputPortMode.Right; + _inputPortSelection = InputPortMode.Right; return; } if (port >= 0xC0 && port <= 0xDF) { - InputPortSelection = InputPortMode.Left; + _inputPortSelection = InputPortMode.Left; return; } if (port >= 0xE0) { - PSG.WritePsgData(value, Cpu.TotalExecutedCycles); - return; + PSG.WritePsgData(value, _cpu.TotalExecutedCycles); } } - private GameInfo _game; - - public enum InputPortMode { Left, Right } - private InputPortMode InputPortSelection; - private byte ReadController1() { _isLag = false; byte retval; - if (InputPortSelection == InputPortMode.Left) + if (_inputPortSelection == InputPortMode.Left) { retval = ControllerDeck.ReadPort1(_controller, true, false); return retval; } - if (InputPortSelection == InputPortMode.Right) + if (_inputPortSelection == InputPortMode.Right) { retval = ControllerDeck.ReadPort1(_controller, false, false); return retval; } + return 0x7F; } @@ -182,52 +179,50 @@ namespace BizHawk.Emulation.Cores.ColecoVision { _isLag = false; byte retval; - if (InputPortSelection == InputPortMode.Left) + if (_inputPortSelection == InputPortMode.Left) { retval = ControllerDeck.ReadPort2(_controller, true, false); return retval; } - if (InputPortSelection == InputPortMode.Right) + if (_inputPortSelection == InputPortMode.Right) { retval = ControllerDeck.ReadPort2(_controller, false, false); return retval; } + return 0x7F; } - private int frame; - - public byte ReadMemory(ushort addr) + private byte ReadMemory(ushort addr) { if (addr >= 0x8000) { - return RomData[addr & 0x7FFF]; + return _romData[addr & 0x7FFF]; } if (addr >= 0x6000) { - return Ram[addr & 1023]; + return _ram[addr & 1023]; } if (addr < 0x2000) { - return BiosRom[addr]; + return _biosRom[addr]; } - //Console.WriteLine("Unhandled read at {0:X4}", addr); + ////Console.WriteLine("Unhandled read at {0:X4}", addr); return 0xFF; } - public void WriteMemory(ushort addr, byte value) + private void WriteMemory(ushort addr, byte value) { if (addr >= 0x6000 && addr < 0x8000) { - Ram[addr & 1023] = value; - return; + _ram[addr & 1023] = value; } - //Console.WriteLine("Unhandled write at {0:X4}:{1:X2}", addr, value); + ////Console.WriteLine("Unhandled write at {0:X4}:{1:X2}", addr, value); } } } \ No newline at end of file