1 //
2 // Copyright (c) 2010-2024 Antmicro
3 // Copyright (c) 2011-2015 Realtime Embedded
4 //
5 // This file is licensed under the MIT License.
6 // Full license text is available in 'licenses/MIT.txt'.
7 //
8 using System;
9 using Antmicro.Renode.Core;
10 using Antmicro.Renode.Core.Structure;
11 using Antmicro.Renode.Peripherals.CPU;
12 
13 using Range = Antmicro.Renode.Core.Range;
14 
15 namespace Antmicro.Renode.Peripherals.Bus
16 {
17     public class BusRangeRegistration : BusRegistration
18     {
BusRangeRegistration(Range range, ulong offset = 0, IPeripheral cpu = null, ICluster<ICPU> cluster = null)19         public BusRangeRegistration(Range range, ulong offset = 0, IPeripheral cpu = null, ICluster<ICPU> cluster = null) : this(range, stateMask: null, offset, cpu, cluster)
20         {
21         }
22 
BusRangeRegistration(Range range, string condition, ulong offset = 0)23         public BusRangeRegistration(Range range, string condition, ulong offset = 0) : this(range, stateMask: null, offset, condition: condition)
24         {
25         }
26 
BusRangeRegistration(ulong address, ulong size, ulong offset = 0, IPeripheral cpu = null, ICluster<ICPU> cluster = null)27         public BusRangeRegistration(ulong address, ulong size, ulong offset = 0, IPeripheral cpu = null, ICluster<ICPU> cluster = null) : this(new Range(address, size), stateMask: null, offset, cpu, cluster)
28         {
29         }
30 
BusRangeRegistration(ulong address, ulong size, string condition, ulong offset = 0)31         public BusRangeRegistration(ulong address, ulong size, string condition, ulong offset = 0) : this(new Range(address, size), stateMask: null, offset, condition: condition)
32         {
33         }
34 
35         public override string PrettyString
36         {
37             get
38             {
39                 return ToString();
40             }
41         }
42 
ToString()43         public override string ToString()
44         {
45             var result = Range.ToString();
46             if(Offset != 0)
47             {
48                 result += $" with offset 0x{Offset:X}";
49             }
50             if(Initiator != null)
51             {
52                 result += $" for core {Initiator}";
53             }
54             return result;
55         }
56 
operator BusRangeRegistration(Range range)57         public static implicit operator BusRangeRegistration(Range range)
58         {
59             return new BusRangeRegistration(range);
60         }
61 
62         public Range Range { get; set; }
63 
Equals(object obj)64         public override bool Equals(object obj)
65         {
66             return base.Equals(obj) && Range.Size == ((BusRangeRegistration)obj).Range.Size;
67         }
68 
GetHashCode()69         public override int GetHashCode()
70         {
71             unchecked
72             {
73                 return 17 * base.GetHashCode() + 101 * Range.Size.GetHashCode();
74             }
75         }
76 
WithInitiatorAndStateMask(IPeripheral initiator, StateMask mask)77         public override IConditionalRegistration WithInitiatorAndStateMask(IPeripheral initiator, StateMask mask)
78         {
79             return new BusRangeRegistration(Range, mask, Offset, initiator);
80         }
81 
RegisterForEachContext(Action<BusRangeRegistration> register)82         public void RegisterForEachContext(Action<BusRangeRegistration> register)
83         {
84             RegisterForEachContextInner(register, cpu => new BusRangeRegistration(Range, StateMask, Offset, cpu));
85         }
86 
BusRangeRegistration(Range range, StateMask? stateMask, ulong offset = 0, IPeripheral cpu = null, ICluster<ICPU> cluster = null, string condition = null)87         protected BusRangeRegistration(Range range, StateMask? stateMask, ulong offset = 0, IPeripheral cpu = null, ICluster<ICPU> cluster = null, string condition = null) : base(range.StartAddress, offset, cpu, cluster, stateMask, condition)
88         {
89             Range = range;
90         }
91     }
92 }
93 
94