BizHawk/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.cs

241 lines
5.1 KiB
C#
Raw Normal View History

2016-02-25 01:41:55 +00:00
using BizHawk.Emulation.Common;
using BizHawk.Emulation.Cores.Components;
2017-10-13 22:00:08 +00:00
using BizHawk.Emulation.Cores.Components.Z80A;
2012-03-25 01:33:05 +00:00
namespace BizHawk.Emulation.Cores.ColecoVision
2012-03-25 01:33:05 +00:00
{
[Core(
"ColecoHawk",
"Vecna",
isPorted: false,
isReleased: true)]
[ServiceNotApplicable(typeof(ISaveRam), typeof(IDriveLight))]
2017-03-01 02:44:05 +00:00
public sealed partial class ColecoVision : IEmulator, IDebuggable, IInputPollable, IStatable, ISettable<ColecoVision.ColecoSettings, ColecoVision.ColecoSyncSettings>
2012-03-25 01:33:05 +00:00
{
[CoreConstructor("Coleco")]
public ColecoVision(CoreComm comm, GameInfo game, byte[] rom, object syncSettings)
2012-03-25 01:33:05 +00:00
{
2017-05-08 16:37:16 +00:00
var ser = new BasicServiceProvider(this);
2017-05-09 02:07:12 +00:00
ServiceProvider = ser;
MemoryCallbacks = new MemoryCallbackSystem(new[] { "System Bus" });
CoreComm = comm;
_syncSettings = (ColecoSyncSettings)syncSettings ?? new ColecoSyncSettings();
2016-02-25 01:41:55 +00:00
bool skipbios = _syncSettings.SkipBiosIntro;
2017-05-08 16:37:16 +00:00
_cpu = new Z80A
{
2017-10-25 23:17:00 +00:00
FetchMemory = ReadMemory,
ReadMemory = ReadMemory,
WriteMemory = WriteMemory,
ReadHardware = ReadPort,
WriteHardware = WritePort,
MemoryCallbacks = MemoryCallbacks
};
2017-04-15 20:37:30 +00:00
PSG = new SN76489();
2016-12-11 17:33:33 +00:00
_fakeSyncSound = new FakeSyncSound(PSG, 735);
2017-05-08 16:37:16 +00:00
ser.Register<ISoundProvider>(_fakeSyncSound);
ControllerDeck = new ColecoVisionControllerDeck(_syncSettings.Port1, _syncSettings.Port2);
2017-03-01 02:44:05 +00:00
2017-05-08 16:37:16 +00:00
_vdp = new TMS9918A(_cpu);
ser.Register<IVideoProvider>(_vdp);
2017-03-01 02:44:05 +00:00
// TODO: hack to allow bios-less operation would be nice, no idea if its feasible
2017-05-08 16:37:16 +00:00
_biosRom = CoreComm.CoreFileProvider.GetFirmware("Coleco", "Bios", true, "Coleco BIOS file is required.");
2013-12-24 23:32:43 +00:00
// gamedb can overwrite the syncsettings; this is ok
if (game["NoSkip"])
{
skipbios = false;
}
LoadRom(rom, skipbios);
2012-11-20 00:35:22 +00:00
SetupMemoryDomains();
2016-02-25 01:41:55 +00:00
2017-05-08 16:37:16 +00:00
_tracer.Header = _cpu.TraceHeader;
2017-10-13 21:50:54 +00:00
ser.Register<IDisassemblable>(_cpu);
2017-05-08 16:37:16 +00:00
ser.Register<ITraceable>(_tracer);
}
2017-05-08 16:37:16 +00:00
private readonly Z80A _cpu;
private readonly TMS9918A _vdp;
private readonly byte[] _biosRom;
private readonly TraceBuffer _tracer = new TraceBuffer();
2017-05-08 16:37:16 +00:00
private byte[] _romData;
private byte[] _ram = new byte[1024];
private int _frame;
private IController _controller;
2017-05-08 16:37:16 +00:00
private enum InputPortMode
{
Left, Right
}
2017-05-08 16:37:16 +00:00
private InputPortMode _inputPortSelection;
2012-11-20 00:35:22 +00:00
2017-05-08 16:37:16 +00:00
public ColecoVisionControllerDeck ControllerDeck { get; }
private void LoadRom(byte[] rom, bool skipbios)
{
2017-05-08 16:37:16 +00:00
_romData = new byte[0x8000];
for (int i = 0; i < 0x8000; i++)
{
2017-05-08 16:37:16 +00:00
_romData[i] = rom[i % rom.Length];
}
2012-11-18 00:40:22 +00:00
// hack to skip colecovision title screen
if (skipbios)
{
2017-05-08 16:37:16 +00:00
_romData[0] = 0x55;
_romData[1] = 0xAA;
}
}
private byte ReadPort(ushort port)
{
port &= 0xFF;
if (port >= 0xA0 && port < 0xC0)
{
if ((port & 1) == 0)
{
2017-05-08 16:37:16 +00:00
return _vdp.ReadData();
}
2017-05-08 16:37:16 +00:00
return _vdp.ReadVdpStatus();
}
if (port >= 0xE0)
{
if ((port & 1) == 0)
{
return ReadController1();
}
return ReadController2();
}
2012-11-18 00:40:22 +00:00
return 0xFF;
}
private void WritePort(ushort port, byte value)
{
port &= 0xFF;
if (port >= 0xA0 && port <= 0xBF)
{
if ((port & 1) == 0)
{
2017-05-08 16:37:16 +00:00
_vdp.WriteVdpData(value);
}
else
{
2017-05-08 16:37:16 +00:00
_vdp.WriteVdpControl(value);
}
return;
}
if (port >= 0x80 && port <= 0x9F)
{
2017-05-08 16:37:16 +00:00
_inputPortSelection = InputPortMode.Right;
return;
}
if (port >= 0xC0 && port <= 0xDF)
{
2017-05-08 16:37:16 +00:00
_inputPortSelection = InputPortMode.Left;
return;
}
if (port >= 0xE0)
{
2017-05-08 16:37:16 +00:00
PSG.WritePsgData(value, _cpu.TotalExecutedCycles);
}
}
2012-03-25 01:33:05 +00:00
private byte ReadController1()
{
_isLag = false;
byte retval;
2017-05-08 16:37:16 +00:00
if (_inputPortSelection == InputPortMode.Left)
{
retval = ControllerDeck.ReadPort1(_controller, true, false);
return retval;
}
2017-05-08 16:37:16 +00:00
if (_inputPortSelection == InputPortMode.Right)
{
retval = ControllerDeck.ReadPort1(_controller, false, false);
return retval;
}
2017-05-08 16:37:16 +00:00
return 0x7F;
}
private byte ReadController2()
{
_isLag = false;
byte retval;
2017-05-08 16:37:16 +00:00
if (_inputPortSelection == InputPortMode.Left)
{
retval = ControllerDeck.ReadPort2(_controller, true, false);
return retval;
}
2017-05-08 16:37:16 +00:00
if (_inputPortSelection == InputPortMode.Right)
{
retval = ControllerDeck.ReadPort2(_controller, false, false);
return retval;
}
2017-05-08 16:37:16 +00:00
return 0x7F;
}
2017-05-08 16:37:16 +00:00
private byte ReadMemory(ushort addr)
{
if (addr >= 0x8000)
{
2017-05-08 16:37:16 +00:00
return _romData[addr & 0x7FFF];
}
if (addr >= 0x6000)
{
2017-05-08 16:37:16 +00:00
return _ram[addr & 1023];
}
if (addr < 0x2000)
{
2017-05-08 16:37:16 +00:00
return _biosRom[addr];
}
2017-05-08 16:37:16 +00:00
////Console.WriteLine("Unhandled read at {0:X4}", addr);
return 0xFF;
}
2017-05-08 16:37:16 +00:00
private void WriteMemory(ushort addr, byte value)
{
if (addr >= 0x6000 && addr < 0x8000)
{
2017-05-08 16:37:16 +00:00
_ram[addr & 1023] = value;
}
2017-05-08 16:37:16 +00:00
////Console.WriteLine("Unhandled write at {0:X4}:{1:X2}", addr, value);
}
private void HardReset()
{
PSG.Reset();
_cpu.Reset();
}
private void SoftReset()
{
PSG.Reset();
_cpu.Reset();
}
2012-03-25 01:33:05 +00:00
}
2017-10-25 23:17:00 +00:00
}