1 //
2 // Copyright (c) 2010-2023 Antmicro
3 //
4 // This file is licensed under the MIT License.
5 // Full license text is available in 'licenses/MIT.txt'.
6 //
7 using System;
8 using Antmicro.Renode.Core;
9 using Antmicro.Renode.Logging;
10 using Antmicro.Renode.Peripherals.Bus;
11 
12 namespace Antmicro.Renode.Peripherals.Miscellaneous
13 {
14     public sealed class BitAccess : IBytePeripheral, IWordPeripheral, IDoubleWordPeripheral
15     {
BitAccess(IMachine machine, ulong address, BitAccessMode mode)16         public BitAccess(IMachine machine, ulong address, BitAccessMode mode)
17         {
18             sysbus = machine.GetSystemBus(this);
19             this.address = address;
20             switch(mode)
21             {
22                 case BitAccessMode.Set:
23                     operation = (register, mask) => (register | mask);
24                     break;
25                 case BitAccessMode.Clear:
26                     operation = (register, mask) => (register & ~mask);
27                     break;
28             }
29         }
30 
Reset()31         public void Reset()
32         {
33             // intentionally left empty
34         }
35 
ReadByte(long offset)36         public byte ReadByte(long offset)
37         {
38             this.Log(LogLevel.Warning, "Reading from BitAccess is not supported.");
39             return 0x0;
40         }
41 
WriteByte(long offset, byte mask)42         public void WriteByte(long offset, byte mask)
43         {
44             sysbus.WriteByte(address + (ulong)offset, (byte)operation(sysbus.ReadByte(address + (ulong)offset), mask));
45         }
46 
ReadWord(long offset)47         public ushort ReadWord(long offset)
48         {
49             this.Log(LogLevel.Warning, "Reading from BitAccess is not supported.");
50             return 0;
51         }
52 
WriteWord(long offset, ushort mask)53         public void WriteWord(long offset, ushort mask)
54         {
55             sysbus.WriteWord(address + (ulong)offset, (ushort)operation(sysbus.ReadWord(address + (ulong)offset), mask));
56         }
57 
ReadDoubleWord(long offset)58         public uint ReadDoubleWord(long offset)
59         {
60             this.Log(LogLevel.Warning, "Reading from BitAccess is not supported.");
61             return 0;
62         }
63 
WriteDoubleWord(long offset, uint mask)64         public void WriteDoubleWord(long offset, uint mask)
65         {
66             sysbus.WriteDoubleWord(address + (ulong)offset, (uint)operation(sysbus.ReadDoubleWord(address + (ulong)offset), mask));
67         }
68 
69         private readonly IBusController sysbus;
70         private readonly ulong address;
71         private readonly Func<uint, uint, uint> operation;
72 
73         public enum BitAccessMode
74         {
75             Set,
76             Clear,
77         }
78     }
79 }
80