using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum { /// /// The abstract class that all emulated models will inherit from /// * Memory * /// public abstract partial class SpectrumBase { /// /// ROM Banks /// public byte[] ROM0 = new byte[0x4000]; public byte[] ROM1 = new byte[0x4000]; public byte[] ROM2 = new byte[0x4000]; public byte[] ROM3 = new byte[0x4000]; /// /// RAM Banks /// public byte[] RAM0 = new byte[0x4000]; // Bank 0 public byte[] RAM1 = new byte[0x4000]; // Bank 1 public byte[] RAM2 = new byte[0x4000]; // Bank 2 public byte[] RAM3 = new byte[0x4000]; // Bank 3 public byte[] RAM4 = new byte[0x4000]; // Bank 4 public byte[] RAM5 = new byte[0x4000]; // Bank 5 public byte[] RAM6 = new byte[0x4000]; // Bank 6 public byte[] RAM7 = new byte[0x4000]; // Bank 7 /// /// Represents the addressable memory space of the spectrum /// All banks for the emulated system should be added during initialisation /// public Dictionary Memory = new Dictionary(); /// /// Simulates reading from the bus /// Paging should be handled here /// /// /// public virtual byte ReadBus(ushort addr) { throw new NotImplementedException("Must be overriden"); } /// /// Simulates writing to the bus /// Paging should be handled here /// /// /// public virtual void WriteBus(ushort addr, byte value) { throw new NotImplementedException("Must be overriden"); } /// /// Reads a byte of data from a specified memory address /// (with memory contention if appropriate) /// /// /// public virtual byte ReadMemory(ushort addr) { throw new NotImplementedException("Must be overriden"); } /* /// /// Reads a byte of data from a specified memory address /// (with no memory contention) /// /// /// public virtual byte PeekMemory(ushort addr) { var data = ReadBus(addr); return data; } */ /// /// Writes a byte of data to a specified memory address /// (with memory contention if appropriate) /// /// /// public virtual void WriteMemory(ushort addr, byte value) { throw new NotImplementedException("Must be overriden"); } /* /// /// Writes a byte of data to a specified memory address /// (without contention) /// /// /// public virtual void PokeMemory(ushort addr, byte value) { if (addr < 0x4000) { // Do nothing - we cannot write to ROM return; } WriteBus(addr, value); } */ /// /// Fills memory from buffer /// /// /// public virtual void FillMemory(byte[] buffer, ushort startAddress) { //buffer?.CopyTo(RAM, startAddress); } /// /// Sets up the ROM /// /// /// public virtual void InitROM(RomData romData) { RomData = romData; // for 16/48k machines only ROM0 is used (no paging) RomData.RomBytes?.CopyTo(ROM0, 0); } /// /// ULA reads the memory at the specified address /// (No memory contention) /// /// /// public virtual byte FetchScreenMemory(ushort addr) { //var value = RAM0[(addr & 0x3FFF)];// + 0x4000]; var value = ReadBus((ushort)((addr & 0x3FFF) + 0x4000)); return value; } /// /// Helper function to refresh memory array (probably not the best way to do things) /// public virtual void ReInitMemory() { throw new NotImplementedException("Must be overriden"); } /// /// Returns the memory contention value for the specified T-State (cycle) /// The ZX Spectrum memory access is contended when the ULA is accessing the lower 16k of RAM /// /// /// public virtual byte GetContentionValue(int cycle) { var val = _renderingCycleTable[cycle % UlaFrameCycleCount].ContentionDelay; return val; } } }