using BizHawk.Common; using BizHawk.Emulation.Cores.Components.Z80A; namespace BizHawk.Emulation.Cores.Computers.AmstradCPC { /// /// The abstract class that all emulated models will inherit from /// * Main properties / fields / contruction* /// public abstract partial class CPCBase { #region Devices /// /// The calling ZXSpectrum class (piped in via constructor) /// public AmstradCPC CPC { 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 Amstrad datacorder device /// public virtual DatacorderDevice TapeDevice { get; set; } /// /// beeper output for the tape /// public IBeeperDevice TapeBuzzer { get; set; } /// /// Device representing the AY-3-8912 chip found in the CPC /// public IPSG AYDevice { get; set; } /// /// The keyboard device /// Technically, this is controlled by the PSG, but has been abstracted due to the port over from ZXHawk /// public IKeyboard KeyboardDevice { get; set; } /// /// The Amstrad disk drive /// public virtual NECUPD765 UPDDiskDevice { get; set; } /// /// The Cathode Ray Tube Controller chip /// public CRCT_6845 CRCT { get; set; } /// /// The Amstrad gate array /// public AmstradGateArray GateArray { get; set; } // /// // /// Renders pixels to the screen // /// // public CRTDevice CRT { get; set; } /// /// The PPI contoller chip /// public PPI_8255 PPI { get; set; } /// /// The length of a standard frame in CPU cycles /// public int FrameLength; #endregion #region Emulator State /// /// 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 long _frameCycles; /// /// Stores where we are in the frame after each CPU cycle /// public long LastFrameStartCPUTick; /// /// Gets the current frame cycle according to the CPU tick count /// public virtual long CurrentFrameCycle => GateArray.FrameClock; // CPU.TotalExecutedCycles - LastFrameStartCPUTick; /// /// Non-Deterministic bools /// public bool _render; public bool _renderSound; #endregion #region Constants /// /// Mask constants & misc /// 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; #endregion #region Emulation Loop /// /// Executes a single frame /// public virtual void ExecuteFrame(bool render, bool renderSound) { GateArray.FrameEnd = false; CRCT.lineCounter = 0; InputRead = false; _render = render; _renderSound = renderSound; FrameCompleted = false; if (UPDDiskDevice == null || !UPDDiskDevice.FDD_IsDiskLoaded) TapeDevice.StartFrame(); if (_renderSound) { AYDevice.StartFrame(); } PollInput(); //CRT.SetupVideo(); //CRT.ScanlineCounter = 0; while (!GateArray.FrameEnd) { GateArray.ClockCycle(); // cycle the tape device if (UPDDiskDevice == null || !UPDDiskDevice.FDD_IsDiskLoaded) TapeDevice.TapeCycle(); } // we have reached the end of a frame LastFrameStartCPUTick = CPU.TotalExecutedCycles; // - OverFlow; if (AYDevice != null) AYDevice.EndFrame(); FrameCount++; if (UPDDiskDevice == null || !UPDDiskDevice.FDD_IsDiskLoaded) TapeDevice.EndFrame(); FrameCompleted = true; // is this a lag frame? CPC.IsLagFrame = !InputRead; // FDC debug if (UPDDiskDevice != null && UPDDiskDevice.writeDebug) { // only write UPD log every second if (FrameCount % 10 == 0) { System.IO.File.AppendAllLines(UPDDiskDevice.outputfile, UPDDiskDevice.dLog); UPDDiskDevice.dLog = new System.Collections.Generic.List(); //System.IO.File.WriteAllText(UPDDiskDevice.outputfile, UPDDiskDevice.outputString); } } GateArray.FrameClock = 0; } #endregion #region Reset Functions /// /// Hard reset of the emulated machine /// public virtual void HardReset() { /* //ULADevice.ResetInterrupt(); ROMPaged = 0; SpecialPagingMode = false; RAMPaged = 0; CPU.RegPC = 0; Spectrum.SetCpuRegister("SP", 0xFFFF); Spectrum.SetCpuRegister("IY", 0xFFFF); Spectrum.SetCpuRegister("IX", 0xFFFF); Spectrum.SetCpuRegister("AF", 0xFFFF); Spectrum.SetCpuRegister("BC", 0xFFFF); Spectrum.SetCpuRegister("DE", 0xFFFF); Spectrum.SetCpuRegister("HL", 0xFFFF); Spectrum.SetCpuRegister("SP", 0xFFFF); Spectrum.SetCpuRegister("Shadow AF", 0xFFFF); Spectrum.SetCpuRegister("Shadow BC", 0xFFFF); Spectrum.SetCpuRegister("Shadow DE", 0xFFFF); Spectrum.SetCpuRegister("Shadow HL", 0xFFFF); CPU.Regs[CPU.I] = 0; CPU.Regs[CPU.R] = 0; TapeDevice.Reset(); if (AYDevice != null) AYDevice.Reset(); byte[][] rams = new byte[][] { RAM0, RAM1, RAM2, RAM3, RAM4, RAM5, RAM6, RAM7 }; foreach (var r in rams) { for (int i = 0; i < r.Length; i++) { r[i] = 0x00; } } */ } /// /// Soft reset of the emulated machine /// public virtual void SoftReset() { /* //ULADevice.ResetInterrupt(); ROMPaged = 0; SpecialPagingMode = false; RAMPaged = 0; CPU.RegPC = 0; Spectrum.SetCpuRegister("SP", 0xFFFF); Spectrum.SetCpuRegister("IY", 0xFFFF); Spectrum.SetCpuRegister("IX", 0xFFFF); Spectrum.SetCpuRegister("AF", 0xFFFF); Spectrum.SetCpuRegister("BC", 0xFFFF); Spectrum.SetCpuRegister("DE", 0xFFFF); Spectrum.SetCpuRegister("HL", 0xFFFF); Spectrum.SetCpuRegister("SP", 0xFFFF); Spectrum.SetCpuRegister("Shadow AF", 0xFFFF); Spectrum.SetCpuRegister("Shadow BC", 0xFFFF); Spectrum.SetCpuRegister("Shadow DE", 0xFFFF); Spectrum.SetCpuRegister("Shadow HL", 0xFFFF); CPU.Regs[CPU.I] = 0; CPU.Regs[CPU.R] = 0; TapeDevice.Reset(); if (AYDevice != null) AYDevice.Reset(); byte[][] rams = new byte[][] { RAM0, RAM1, RAM2, RAM3, RAM4, RAM5, RAM6, RAM7 }; foreach (var r in rams) { for (int i = 0; i < r.Length; i++) { r[i] = 0x00; } } */ } #endregion #region IStatable public void SyncState(Serializer ser) { ser.BeginSection("CPCMachine"); ser.Sync(nameof(FrameCompleted), ref FrameCompleted); ser.Sync(nameof(OverFlow), ref OverFlow); ser.Sync(nameof(FrameCount), ref FrameCount); ser.Sync(nameof(_frameCycles), ref _frameCycles); ser.Sync(nameof(inputRead), ref inputRead); ser.Sync(nameof(LastFrameStartCPUTick), ref LastFrameStartCPUTick); ser.Sync(nameof(ROMLower), ref ROMLower, false); ser.Sync(nameof(ROM0), ref ROM0, false); ser.Sync(nameof(ROM7), ref ROM7, false); ser.Sync(nameof(RAM0), ref RAM0, false); ser.Sync(nameof(RAM1), ref RAM1, false); ser.Sync(nameof(RAM2), ref RAM2, false); ser.Sync(nameof(RAM3), ref RAM3, false); ser.Sync(nameof(RAM4), ref RAM4, false); ser.Sync(nameof(RAM5), ref RAM5, false); ser.Sync(nameof(RAM6), ref RAM6, false); ser.Sync(nameof(RAM7), ref RAM7, false); ser.Sync(nameof(UpperROMPosition), ref UpperROMPosition); ser.Sync(nameof(UpperROMPaged), ref UpperROMPaged); ser.Sync(nameof(LowerROMPaged), ref LowerROMPaged); ser.Sync(nameof(RAMConfig), ref RAMConfig); ser.Sync(nameof(RAM64KBank), ref RAM64KBank); CRCT.SyncState(ser); //CRT.SyncState(ser); GateArray.SyncState(ser); KeyboardDevice.SyncState(ser); TapeBuzzer.SyncState(ser); AYDevice.SyncState(ser); ser.Sync(nameof(tapeMediaIndex), ref tapeMediaIndex); if (ser.IsReader) TapeMediaIndex = tapeMediaIndex; TapeDevice.SyncState(ser); ser.Sync(nameof(diskMediaIndex), ref diskMediaIndex); if (ser.IsReader) DiskMediaIndex = diskMediaIndex; if (UPDDiskDevice != null) { UPDDiskDevice.SyncState(ser); } ser.EndSection(); } #endregion } }