2019-02-02 15:12:51 +00:00
|
|
|
|
namespace EightBit
|
|
|
|
|
{
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
abstract public class Bus : IMapper
|
|
|
|
|
{
|
|
|
|
|
private byte data;
|
2019-02-03 00:42:55 +00:00
|
|
|
|
private ushort address;
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
|
|
|
|
public event EventHandler<EventArgs> WritingByte;
|
|
|
|
|
public event EventHandler<EventArgs> WrittenByte;
|
|
|
|
|
|
|
|
|
|
public event EventHandler<EventArgs> ReadingByte;
|
|
|
|
|
public event EventHandler<EventArgs> ReadByte;
|
|
|
|
|
|
2019-02-03 20:29:52 +00:00
|
|
|
|
public byte Data { get => data; set => data = value; }
|
|
|
|
|
public ushort Address { get => address; set => address = value; }
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
2019-02-03 00:42:55 +00:00
|
|
|
|
public abstract MemoryMapping Mapping(ushort absolute);
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
|
|
|
|
public byte Peek() => Reference();
|
2019-02-03 00:42:55 +00:00
|
|
|
|
public byte Peek(ushort absolute) => Reference(absolute);
|
|
|
|
|
public byte Peek(byte low, byte high) => Reference(low, high);
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
|
|
|
|
public void Poke(byte value) => Reference() = value;
|
2019-02-03 00:42:55 +00:00
|
|
|
|
public byte Poke(ushort absolute, byte value) => Reference(absolute) = value;
|
|
|
|
|
public byte Poke(byte low, byte high, byte value) => Reference(low, high) = value;
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
|
|
|
|
public byte Read()
|
|
|
|
|
{
|
|
|
|
|
OnReadingByte();
|
|
|
|
|
var returned = Data = Reference();
|
|
|
|
|
OnReadByte();
|
|
|
|
|
return returned;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-03 00:42:55 +00:00
|
|
|
|
public byte Read(ushort absolute)
|
2019-02-02 15:12:51 +00:00
|
|
|
|
{
|
2019-02-03 00:42:55 +00:00
|
|
|
|
Address = absolute;
|
2019-02-02 15:12:51 +00:00
|
|
|
|
return Read();
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-03 20:29:52 +00:00
|
|
|
|
public byte Read(byte low, byte high) => Read(Chip.MakeWord(low, high));
|
2019-02-03 00:42:55 +00:00
|
|
|
|
|
2019-02-02 15:12:51 +00:00
|
|
|
|
public void Write()
|
|
|
|
|
{
|
|
|
|
|
OnWritingByte();
|
|
|
|
|
Reference() = Data;
|
|
|
|
|
OnWrittenByte();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Write(byte value)
|
|
|
|
|
{
|
|
|
|
|
Data = value;
|
|
|
|
|
Write();
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-03 00:42:55 +00:00
|
|
|
|
public void Write(ushort absolute, byte value)
|
2019-02-02 15:12:51 +00:00
|
|
|
|
{
|
2019-02-03 00:42:55 +00:00
|
|
|
|
Address = absolute;
|
2019-02-02 15:12:51 +00:00
|
|
|
|
Write(value);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-03 20:29:52 +00:00
|
|
|
|
public void Write(byte low, byte high, byte value) => Write(Chip.MakeWord(low, high), value);
|
2019-02-03 00:42:55 +00:00
|
|
|
|
|
2019-02-02 15:12:51 +00:00
|
|
|
|
public virtual void RaisePOWER() {}
|
|
|
|
|
public virtual void LowerPOWER() {}
|
|
|
|
|
|
|
|
|
|
public abstract void Initialize();
|
|
|
|
|
|
|
|
|
|
protected virtual void OnWritingByte() => WritingByte?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
protected virtual void OnWrittenByte() => WrittenByte?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
|
|
|
|
|
protected virtual void OnReadingByte() => ReadingByte?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
protected virtual void OnReadByte() => ReadByte?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
|
2019-02-03 00:42:55 +00:00
|
|
|
|
protected ref byte Reference(ushort absolute)
|
2019-02-02 15:12:51 +00:00
|
|
|
|
{
|
|
|
|
|
var mapped = Mapping(absolute);
|
|
|
|
|
var offset = (ushort)((absolute - mapped.Begin) & mapped.Mask);
|
|
|
|
|
if (mapped.Access == AccessLevel.ReadOnly)
|
|
|
|
|
{
|
|
|
|
|
Data = mapped.Memory.Peek(offset);
|
|
|
|
|
return ref data;
|
|
|
|
|
}
|
|
|
|
|
return ref mapped.Memory.Reference(offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected ref byte Reference() => ref Reference(Address);
|
|
|
|
|
|
2019-02-03 20:29:52 +00:00
|
|
|
|
protected ref byte Reference(byte low, byte high) => ref Reference(Chip.MakeWord(low, high));
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
|
|
|
|
//[[nodiscard]] static std::map<uint16_t, std::vector<uint8_t>> parseHexFile(std::string path);
|
|
|
|
|
//void loadHexFile(std::string path);
|
|
|
|
|
}
|
|
|
|
|
}
|