using BizHawk.Common;
using BizHawk.Emulation.Common;
using BizHawk.Emulation.Cores.Components.Z80A;
using System;
using System.Collections.Generic;
namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
{
///
/// The abstract class that all emulated models will inherit from
/// * Main properties / fields / contruction*
///
public abstract partial class SpectrumBase
{
///
/// Index of the currently paged ROM
///
protected int ROMPaged;
public virtual int _ROMpaged
{
get { return ROMPaged; }
set { ROMPaged = value; }
}
///
/// Signs that the shadow screen has been paged in
///
protected bool SHADOWPaged;
///
/// Index of the current RAM page
///
public int RAMPaged;
///
/// Signs that all paging is disabled
///
protected bool PagingDisabled;
// +3/+2A only
protected bool ROMhigh = false;
protected bool ROMlow = false;
///
/// Signs that the +2a/+3 special paging mode is activated
///
protected bool SpecialPagingMode;
///
/// Index of the current special paging config
///
protected int PagingConfiguration;
///
/// Signs whether the disk motor is on or off
///
protected bool DiskMotorState;
protected bool PrinterPortStrobe;
///
/// The calling ZXSpectrum class (piped in via constructor)
///
public ZXSpectrum Spectrum { get; set; }
///
/// Reference to the instantiated Z80 cpu (piped in via constructor)
///
public Z80A CPU { get; set; }
///
/// ROM and extended info
///
public RomData RomData { get; set; }
///
/// The emulated ULA device
///
public ULABase ULADevice { get; set; }
///
/// The spectrum buzzer/beeper
///
public IBeeperDevice BuzzerDevice { get; set; }
///
/// Device representing the AY-3-8912 chip found in the 128k and up spectrums
///
public AY38912 AYDevice { get; set; }
///
/// The spectrum keyboard
///
public virtual IKeyboard KeyboardDevice { get; set; }
///
/// The spectrum datacorder device
///
public virtual DatacorderDevice TapeDevice { get; set; }
///
/// Holds the currently selected joysticks
///
public virtual IJoystick[] JoystickCollection { get; set; }
///
/// Signs whether the frame has ended
///
public bool FrameCompleted;
///
/// Overflow from the previous frame (in Z80 cycles)
///
public int OverFlow;
///
/// The total number of frames rendered
///
public int FrameCount;
///
/// The current cycle (T-State) that we are at in the frame
///
public int _frameCycles;
///
/// Stores where we are in the frame after each CPU cycle
///
public int LastFrameStartCPUTick;
///
/// Gets the current frame cycle according to the CPU tick count
///
public virtual int CurrentFrameCycle => CPU.TotalExecutedCycles - LastFrameStartCPUTick;
///
/// Non-Deterministic bools
///
public bool _render;
public bool _renderSound;
///
/// Mask constants
///
protected const int BORDER_BIT = 0x07;
protected const int EAR_BIT = 0x10;
protected const int MIC_BIT = 0x08;
protected const int TAPE_BIT = 0x40;
protected const int AY_SAMPLE_RATE = 16;
///
/// Executes a single frame
///
public virtual void ExecuteFrame(bool render, bool renderSound)
{
InputRead = false;
_render = render;
_renderSound = renderSound;
FrameCompleted = false;
if (_renderSound)
{
BuzzerDevice.StartFrame();
if (AYDevice != null)
AYDevice.StartFrame();
}
PollInput();
while (CurrentFrameCycle < ULADevice.FrameLength) // UlaFrameCycleCount)
{
// check for interrupt
ULADevice.CheckForInterrupt(CurrentFrameCycle);
// run a single CPU instruction
CPU.ExecuteOne();
// update AY
if (_renderSound)
{
if (AYDevice != null)
AYDevice.UpdateSound(CurrentFrameCycle);
}
}
// we have reached the end of a frame
LastFrameStartCPUTick = CPU.TotalExecutedCycles - OverFlow;
// paint the buffer if needed
if (ULADevice.needsPaint && _render)
ULADevice.UpdateScreenBuffer(ULADevice.FrameLength);
if (_renderSound)
BuzzerDevice.EndFrame();
//TapeDevice.CPUFrameCompleted();
FrameCount++;
// setup for next frame
ULADevice.ResetInterrupt();
TapeDevice.EndFrame();
FrameCompleted = true;
// is this a lag frame?
Spectrum.IsLagFrame = !InputRead;
}
///
/// Hard reset of the emulated machine
///
public virtual void HardReset()
{
//ResetBorder();
ULADevice.ResetInterrupt();
}
///
/// Soft reset of the emulated machine
///
public virtual void SoftReset()
{
//ResetBorder();
ULADevice.ResetInterrupt();
}
public void SyncState(Serializer ser)
{
ser.BeginSection("ZXMachine");
ser.Sync("FrameCompleted", ref FrameCompleted);
ser.Sync("OverFlow", ref OverFlow);
ser.Sync("FrameCount", ref FrameCount);
ser.Sync("_frameCycles", ref _frameCycles);
ser.Sync("inputRead", ref inputRead);
ser.Sync("LastFrameStartCPUTick", ref LastFrameStartCPUTick);
ser.Sync("LastULAOutByte", ref LastULAOutByte);
ser.Sync("ROM0", ref ROM0, false);
ser.Sync("ROM1", ref ROM1, false);
ser.Sync("ROM2", ref ROM2, false);
ser.Sync("ROM3", ref ROM3, false);
ser.Sync("RAM0", ref RAM0, false);
ser.Sync("RAM1", ref RAM1, false);
ser.Sync("RAM2", ref RAM2, false);
ser.Sync("RAM3", ref RAM3, false);
ser.Sync("RAM4", ref RAM4, false);
ser.Sync("RAM5", ref RAM5, false);
ser.Sync("RAM6", ref RAM6, false);
ser.Sync("RAM7", ref RAM7, false);
ser.Sync("ROMPaged", ref ROMPaged);
ser.Sync("SHADOWPaged", ref SHADOWPaged);
ser.Sync("RAMPaged", ref RAMPaged);
ser.Sync("PagingDisabled", ref PagingDisabled);
ser.Sync("SpecialPagingMode", ref SpecialPagingMode);
ser.Sync("PagingConfiguration", ref PagingConfiguration);
RomData.SyncState(ser);
KeyboardDevice.SyncState(ser);
BuzzerDevice.SyncState(ser);
ULADevice.SyncState(ser);
if (AYDevice != null)
AYDevice.SyncState(ser);
ser.Sync("tapeMediaIndex", ref tapeMediaIndex);
TapeMediaIndex = tapeMediaIndex;
ser.Sync("diskMediaIndex", ref diskMediaIndex);
DiskMediaIndex = diskMediaIndex;
TapeDevice.SyncState(ser);
ser.EndSection();
ReInitMemory();
}
}
}