2017-12-11 09:05:12 +00:00
|
|
|
|
using BizHawk.Common;
|
|
|
|
|
using BizHawk.Emulation.Common;
|
2017-11-29 21:32:34 +00:00
|
|
|
|
using BizHawk.Emulation.Cores.Components;
|
|
|
|
|
using BizHawk.Emulation.Cores.Components.Z80A;
|
2017-12-11 09:05:12 +00:00
|
|
|
|
using BizHawk.Emulation.Cores.Properties;
|
2017-11-29 21:32:34 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2017-11-30 09:41:30 +00:00
|
|
|
|
using System.IO;
|
2017-11-29 21:32:34 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
|
|
|
|
|
{
|
|
|
|
|
[Core(
|
|
|
|
|
"ZXHawk",
|
|
|
|
|
"Asnivor",
|
|
|
|
|
isPorted: false,
|
|
|
|
|
isReleased: false)]
|
|
|
|
|
[ServiceNotApplicable(typeof(IDriveLight))]
|
|
|
|
|
public partial class ZXSpectrum : IDebuggable, IInputPollable, IStatable, IRegionable
|
|
|
|
|
{
|
|
|
|
|
[CoreConstructor("ZXSpectrum")]
|
2018-03-05 13:29:34 +00:00
|
|
|
|
public ZXSpectrum(CoreComm comm, IEnumerable<byte[]> files, List<GameInfo> game, object settings, object syncSettings)
|
2018-03-06 16:40:25 +00:00
|
|
|
|
{
|
2017-11-29 21:32:34 +00:00
|
|
|
|
var ser = new BasicServiceProvider(this);
|
|
|
|
|
ServiceProvider = ser;
|
|
|
|
|
InputCallbacks = new InputCallbackSystem();
|
2018-03-06 17:04:12 +00:00
|
|
|
|
MemoryCallbacks = new MemoryCallbackSystem(new[] { "System Bus" });
|
2017-11-29 21:32:34 +00:00
|
|
|
|
|
|
|
|
|
CoreComm = comm;
|
|
|
|
|
|
2018-03-05 13:29:34 +00:00
|
|
|
|
_gameInfo = game;
|
|
|
|
|
|
2017-11-29 21:32:34 +00:00
|
|
|
|
_cpu = new Z80A();
|
|
|
|
|
|
2017-11-30 09:41:30 +00:00
|
|
|
|
_tracer = new TraceBuffer { Header = _cpu.TraceHeader };
|
|
|
|
|
|
2018-03-05 11:17:22 +00:00
|
|
|
|
//_file = file;
|
|
|
|
|
_files = files?.ToList() ?? new List<byte[]>();
|
2017-11-30 09:41:30 +00:00
|
|
|
|
|
2018-03-06 16:40:25 +00:00
|
|
|
|
if (settings == null)
|
|
|
|
|
settings = new ZXSpectrumSettings();
|
|
|
|
|
if (syncSettings == null)
|
|
|
|
|
syncSettings = new ZXSpectrumSyncSettings();
|
|
|
|
|
|
|
|
|
|
PutSyncSettings((ZXSpectrumSyncSettings)syncSettings ?? new ZXSpectrumSyncSettings());
|
|
|
|
|
PutSettings((ZXSpectrumSettings)settings ?? new ZXSpectrumSettings());
|
|
|
|
|
|
|
|
|
|
List <JoystickType> joysticks = new List<JoystickType>();
|
2018-03-06 15:47:14 +00:00
|
|
|
|
joysticks.Add(((ZXSpectrumSyncSettings)syncSettings as ZXSpectrumSyncSettings).JoystickType1);
|
|
|
|
|
joysticks.Add(((ZXSpectrumSyncSettings)syncSettings as ZXSpectrumSyncSettings).JoystickType2);
|
|
|
|
|
joysticks.Add(((ZXSpectrumSyncSettings)syncSettings as ZXSpectrumSyncSettings).JoystickType3);
|
|
|
|
|
|
2018-03-07 12:21:36 +00:00
|
|
|
|
deterministicEmulation = ((ZXSpectrumSyncSettings)syncSettings as ZXSpectrumSyncSettings).DeterministicEmulation;
|
|
|
|
|
|
2017-12-04 15:40:27 +00:00
|
|
|
|
switch (SyncSettings.MachineType)
|
2017-11-29 21:32:34 +00:00
|
|
|
|
{
|
2017-12-07 16:03:23 +00:00
|
|
|
|
case MachineType.ZXSpectrum16:
|
|
|
|
|
ControllerDefinition = ZXSpectrumControllerDefinition;
|
2018-03-06 15:47:14 +00:00
|
|
|
|
Init(MachineType.ZXSpectrum16, SyncSettings.BorderType, SyncSettings.TapeLoadSpeed, _files, joysticks);
|
2017-12-07 16:03:23 +00:00
|
|
|
|
break;
|
2017-11-29 21:32:34 +00:00
|
|
|
|
case MachineType.ZXSpectrum48:
|
2017-11-30 12:08:36 +00:00
|
|
|
|
ControllerDefinition = ZXSpectrumControllerDefinition;
|
2018-03-06 15:47:14 +00:00
|
|
|
|
Init(MachineType.ZXSpectrum48, SyncSettings.BorderType, SyncSettings.TapeLoadSpeed, _files, joysticks);
|
2017-11-29 21:32:34 +00:00
|
|
|
|
break;
|
2017-12-05 10:02:57 +00:00
|
|
|
|
case MachineType.ZXSpectrum128:
|
|
|
|
|
ControllerDefinition = ZXSpectrumControllerDefinition;
|
2018-03-06 15:47:14 +00:00
|
|
|
|
Init(MachineType.ZXSpectrum128, SyncSettings.BorderType, SyncSettings.TapeLoadSpeed, _files, joysticks);
|
2017-12-05 10:02:57 +00:00
|
|
|
|
break;
|
2017-12-05 10:26:06 +00:00
|
|
|
|
case MachineType.ZXSpectrum128Plus2:
|
|
|
|
|
ControllerDefinition = ZXSpectrumControllerDefinition;
|
2018-03-06 15:47:14 +00:00
|
|
|
|
Init(MachineType.ZXSpectrum128Plus2, SyncSettings.BorderType, SyncSettings.TapeLoadSpeed, _files, joysticks);
|
2017-12-05 10:26:06 +00:00
|
|
|
|
break;
|
2017-12-07 15:43:28 +00:00
|
|
|
|
case MachineType.ZXSpectrum128Plus3:
|
|
|
|
|
ControllerDefinition = ZXSpectrumControllerDefinition;
|
2018-03-06 15:47:14 +00:00
|
|
|
|
Init(MachineType.ZXSpectrum128Plus3, SyncSettings.BorderType, SyncSettings.TapeLoadSpeed, _files, joysticks);
|
2017-12-07 15:43:28 +00:00
|
|
|
|
break;
|
2017-11-29 21:32:34 +00:00
|
|
|
|
default:
|
|
|
|
|
throw new InvalidOperationException("Machine not yet emulated");
|
2018-01-15 12:50:07 +00:00
|
|
|
|
}
|
2017-11-29 21:32:34 +00:00
|
|
|
|
|
|
|
|
|
_cpu.MemoryCallbacks = MemoryCallbacks;
|
|
|
|
|
|
|
|
|
|
HardReset = _machine.HardReset;
|
|
|
|
|
SoftReset = _machine.SoftReset;
|
|
|
|
|
|
|
|
|
|
_cpu.FetchMemory = _machine.ReadMemory;
|
|
|
|
|
_cpu.ReadMemory = _machine.ReadMemory;
|
|
|
|
|
_cpu.WriteMemory = _machine.WriteMemory;
|
|
|
|
|
_cpu.ReadHardware = _machine.ReadPort;
|
|
|
|
|
_cpu.WriteHardware = _machine.WritePort;
|
2017-12-01 11:36:57 +00:00
|
|
|
|
_cpu.FetchDB = _machine.PushBus;
|
2017-11-29 21:32:34 +00:00
|
|
|
|
|
|
|
|
|
ser.Register<ITraceable>(_tracer);
|
|
|
|
|
ser.Register<IDisassemblable>(_cpu);
|
2017-12-11 12:54:48 +00:00
|
|
|
|
ser.Register<IVideoProvider>(_machine.ULADevice);
|
2017-12-05 13:08:47 +00:00
|
|
|
|
|
|
|
|
|
SoundMixer = new SoundProviderMixer(_machine.BuzzerDevice);
|
|
|
|
|
if (_machine.AYDevice != null)
|
|
|
|
|
SoundMixer.AddSource(_machine.AYDevice);
|
|
|
|
|
|
2018-01-15 12:50:07 +00:00
|
|
|
|
dcf = new DCFilter(SoundMixer, 256);
|
2017-12-07 10:49:43 +00:00
|
|
|
|
ser.Register<ISoundProvider>(dcf);
|
|
|
|
|
|
|
|
|
|
|
2017-11-29 21:32:34 +00:00
|
|
|
|
|
|
|
|
|
HardReset();
|
|
|
|
|
|
|
|
|
|
SetupMemoryDomains();
|
|
|
|
|
}
|
2017-11-30 09:41:30 +00:00
|
|
|
|
|
2017-11-29 21:32:34 +00:00
|
|
|
|
public Action HardReset;
|
|
|
|
|
public Action SoftReset;
|
|
|
|
|
|
|
|
|
|
private readonly Z80A _cpu;
|
|
|
|
|
private readonly TraceBuffer _tracer;
|
|
|
|
|
public IController _controller;
|
2018-03-06 15:47:14 +00:00
|
|
|
|
public SpectrumBase _machine;
|
2017-11-29 21:32:34 +00:00
|
|
|
|
|
2018-03-05 13:29:34 +00:00
|
|
|
|
private List<GameInfo> _gameInfo;
|
|
|
|
|
|
2018-01-15 12:50:07 +00:00
|
|
|
|
private SoundProviderMixer SoundMixer;
|
|
|
|
|
|
2017-12-07 10:49:43 +00:00
|
|
|
|
private DCFilter dcf;
|
|
|
|
|
|
2018-03-05 11:17:22 +00:00
|
|
|
|
//private byte[] _file;
|
|
|
|
|
private readonly List<byte[]> _files;
|
2017-11-29 21:32:34 +00:00
|
|
|
|
|
2018-03-06 16:05:50 +00:00
|
|
|
|
public bool DiagRom = false;
|
2017-11-30 09:41:30 +00:00
|
|
|
|
|
2017-11-29 21:32:34 +00:00
|
|
|
|
private byte[] GetFirmware(int length, params string[] names)
|
|
|
|
|
{
|
2017-11-30 09:41:30 +00:00
|
|
|
|
if (DiagRom & File.Exists(Directory.GetCurrentDirectory() + @"\DiagROM.v28"))
|
|
|
|
|
{
|
|
|
|
|
var rom = File.ReadAllBytes(Directory.GetCurrentDirectory() + @"\DiagROM.v28");
|
|
|
|
|
return rom;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-11 09:05:12 +00:00
|
|
|
|
// Amstrad licensed ROMs are free to distribute and shipped with BizHawk
|
|
|
|
|
byte[] embeddedRom = new byte[length];
|
|
|
|
|
bool embeddedFound = true;
|
|
|
|
|
switch (names.FirstOrDefault())
|
|
|
|
|
{
|
|
|
|
|
case "48ROM":
|
|
|
|
|
embeddedRom = Util.DecompressGzipFile(new MemoryStream(Resources.ZX_48_ROM));
|
|
|
|
|
break;
|
|
|
|
|
case "128ROM":
|
|
|
|
|
embeddedRom = Util.DecompressGzipFile(new MemoryStream(Resources.ZX_128_ROM));
|
|
|
|
|
break;
|
|
|
|
|
case "PLUS2ROM":
|
|
|
|
|
embeddedRom = Util.DecompressGzipFile(new MemoryStream(Resources.ZX_plus2_rom));
|
|
|
|
|
break;
|
|
|
|
|
case "PLUS3ROM":
|
|
|
|
|
embeddedRom = Util.DecompressGzipFile(new MemoryStream(Resources.ZX_plus3_rom));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
embeddedFound = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (embeddedFound)
|
|
|
|
|
return embeddedRom;
|
|
|
|
|
|
|
|
|
|
// Embedded ROM not found, maybe this is a peripheral ROM?
|
2017-11-29 21:32:34 +00:00
|
|
|
|
var result = names.Select(n => CoreComm.CoreFileProvider.GetFirmware("ZXSpectrum", n, false)).FirstOrDefault(b => b != null && b.Length == length);
|
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
|
|
|
|
throw new MissingFirmwareException($"At least one of these firmwares is required: {string.Join(", ", names)}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-03-06 15:47:14 +00:00
|
|
|
|
private void Init(MachineType machineType, BorderType borderType, TapeLoadSpeed tapeLoadSpeed, List<byte[]> files, List<JoystickType> joys)
|
2017-11-29 21:32:34 +00:00
|
|
|
|
{
|
|
|
|
|
// setup the emulated model based on the MachineType
|
|
|
|
|
switch (machineType)
|
|
|
|
|
{
|
2017-12-07 16:03:23 +00:00
|
|
|
|
case MachineType.ZXSpectrum16:
|
2018-03-06 15:47:14 +00:00
|
|
|
|
_machine = new ZX16(this, _cpu, borderType, files, joys);
|
2017-12-07 16:03:23 +00:00
|
|
|
|
var _systemRom16 = GetFirmware(0x4000, "48ROM");
|
|
|
|
|
var romData16 = RomData.InitROM(machineType, _systemRom16);
|
|
|
|
|
_machine.InitROM(romData16);
|
|
|
|
|
break;
|
2017-11-29 21:32:34 +00:00
|
|
|
|
case MachineType.ZXSpectrum48:
|
2018-03-06 15:47:14 +00:00
|
|
|
|
_machine = new ZX48(this, _cpu, borderType, files, joys);
|
2017-11-29 21:32:34 +00:00
|
|
|
|
var _systemRom = GetFirmware(0x4000, "48ROM");
|
|
|
|
|
var romData = RomData.InitROM(machineType, _systemRom);
|
|
|
|
|
_machine.InitROM(romData);
|
|
|
|
|
break;
|
2017-12-05 10:02:57 +00:00
|
|
|
|
case MachineType.ZXSpectrum128:
|
2018-03-06 15:47:14 +00:00
|
|
|
|
_machine = new ZX128(this, _cpu, borderType, files, joys);
|
2017-12-05 10:02:57 +00:00
|
|
|
|
var _systemRom128 = GetFirmware(0x8000, "128ROM");
|
|
|
|
|
var romData128 = RomData.InitROM(machineType, _systemRom128);
|
|
|
|
|
_machine.InitROM(romData128);
|
|
|
|
|
break;
|
2017-12-05 10:26:06 +00:00
|
|
|
|
case MachineType.ZXSpectrum128Plus2:
|
2018-03-06 15:47:14 +00:00
|
|
|
|
_machine = new ZX128Plus2(this, _cpu, borderType, files, joys);
|
2017-12-05 10:26:06 +00:00
|
|
|
|
var _systemRomP2 = GetFirmware(0x8000, "PLUS2ROM");
|
|
|
|
|
var romDataP2 = RomData.InitROM(machineType, _systemRomP2);
|
|
|
|
|
_machine.InitROM(romDataP2);
|
|
|
|
|
break;
|
2017-12-07 15:43:28 +00:00
|
|
|
|
case MachineType.ZXSpectrum128Plus3:
|
2018-03-06 15:47:14 +00:00
|
|
|
|
_machine = new ZX128Plus3(this, _cpu, borderType, files, joys);
|
2017-12-07 15:43:28 +00:00
|
|
|
|
var _systemRomP3 = GetFirmware(0x10000, "PLUS3ROM");
|
|
|
|
|
var romDataP3 = RomData.InitROM(machineType, _systemRomP3);
|
|
|
|
|
_machine.InitROM(romDataP3);
|
2017-12-07 17:24:30 +00:00
|
|
|
|
System.Windows.Forms.MessageBox.Show("+3 is not working at all yet :/");
|
2017-12-07 15:43:28 +00:00
|
|
|
|
break;
|
2017-11-29 21:32:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region IRegionable
|
|
|
|
|
|
|
|
|
|
public DisplayType Region => DisplayType.PAL;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|