// // Copyright (c) Adrian Conlon. All rights reserved. // namespace EightBit { using System; public abstract class Bus : IMapper { private Register16 address; private byte data; public event EventHandler WritingByte; public event EventHandler WrittenByte; public event EventHandler ReadingByte; public event EventHandler ReadByte; public byte Data { get => this.data; set => this.data = value; } public ref Register16 Address() => ref this.address; public abstract MemoryMapping Mapping(ushort absolute); public byte Peek() => this.Reference(); public byte Peek(ushort absolute) => this.Reference(absolute); public byte Peek(Register16 absolute) => this.Peek(absolute.Word); public byte Peek(byte low, byte high) => this.Reference(low, high); public void Poke(byte value) => this.Reference() = value; public byte Poke(ushort absolute, byte value) => this.Reference(absolute) = value; public byte Poke(Register16 absolute, byte value) => this.Poke(absolute.Word, value); public byte Poke(byte low, byte high, byte value) => this.Reference(low, high) = value; public byte Read() { this.OnReadingByte(); var returned = this.Data = this.Reference(); this.OnReadByte(); return returned; } public byte Read(ushort absolute) { this.Address().Word = absolute; return this.Read(); } public byte Read(Register16 absolute) => this.Read(absolute.Word); public byte Read(byte low, byte high) { this.Address().Low = low; this.Address().High = high; return this.Read(); } public void Write() { this.OnWritingByte(); this.Reference() = this.Data; this.OnWrittenByte(); } public void Write(byte value) { this.Data = value; this.Write(); } public void Write(ushort absolute, byte value) { this.Address().Word = absolute; this.Write(value); } public void Write(Register16 absolute, byte value) => this.Write(absolute.Word, value); public void Write(byte low, byte high, byte value) { this.Address().Low = low; this.Address().High = high; this.Write(value); } public virtual void RaisePOWER() { } public virtual void LowerPOWER() { } public abstract void Initialize(); protected virtual void OnWritingByte() => this.WritingByte?.Invoke(this, EventArgs.Empty); protected virtual void OnWrittenByte() => this.WrittenByte?.Invoke(this, EventArgs.Empty); protected virtual void OnReadingByte() => this.ReadingByte?.Invoke(this, EventArgs.Empty); protected virtual void OnReadByte() => this.ReadByte?.Invoke(this, EventArgs.Empty); protected ref byte Reference(ushort absolute) { var mapped = this.Mapping(absolute); var offset = (ushort)((absolute - mapped.Begin) & mapped.Mask); if (mapped.Access == AccessLevel.ReadOnly) { this.Data = mapped.Memory.Peek(offset); return ref this.data; } return ref mapped.Memory.Reference(offset); } protected ref byte Reference(Register16 absolute) => ref this.Reference(absolute.Word); protected ref byte Reference() => ref this.Reference(this.Address()); protected ref byte Reference(byte low, byte high) => ref this.Reference(new Register16(low, high).Word); ////[[nodiscard]] static std::map> parseHexFile(std::string path); ////void loadHexFile(std::string path); } }