summaryrefslogtreecommitdiff
path: root/Chip/BusDevice.cs
blob: 8b677c54b3f16a0b6e03aeb659eadf04b24146ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
namespace Q1Emu.Chip;

using System.Runtime.CompilerServices;

public abstract class BusDevice
{
    public BusDevice(u16 addressableStart, u16 addressableEnd)
    {
        this.AddressableStart = addressableStart;
        this.AddressableEnd   = addressableEnd;
    }
    
    public Bus Bus { get; set; }
    public abstract u8 Read(u16 address);
    public abstract void Write(u16 address, u8 value);
    
    public u16 ReadWord(u16 address)
    {
        u8 high = this.Read(address);
        u8 low = this.Read((u16) (address + 1));
        u16 value = Make.u16(high, low);
        return value;
    }
    
    public void WriteWord(u16 address, u16 value)
    {
        ref8 data = Make.ref8(value);
        this.Write(address, data[0]);
        this.Write((u16) (address + 1), data[1]);
    }
    
    public i16 ReadSignedWord(u16 address)
    {
        u8 high = this.Read(address);
        u8 low = this.Read((u16) (address + 1));
        return Make.i16(high, low);
    }

    public virtual void Clock() { }
    public virtual void Reset() { }
    
    public void AttachToBus(Bus bus)
    {
        this.Bus = bus;
        bus.Devices.Add(this);
    }
    
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    protected u16 GetRelativeAddress(u16 address)
    {
        return (u16) (address - this.AddressableStart);
    }

    public u16 AddressableStart { get; }
    public u16 AddressableEnd   { get; }
}