1 //
2 // Copyright (c) 2010-2023 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 
11 using Range = Antmicro.Renode.Core.Range;
12 
13 namespace Antmicro.Renode.Peripherals.Bus.Wrappers
14 {
15     public class ReadHookWrapper<T> : HookWrapper
16     {
ReadHookWrapper(IBusPeripheral peripheral, Func<long, T> originalMethod, Func<T, long, T> newMethod = null, Range? subrange = null)17         public ReadHookWrapper(IBusPeripheral peripheral, Func<long, T> originalMethod, Func<T, long, T> newMethod = null,
18                               Range? subrange = null) : base(peripheral, typeof(T), subrange)
19         {
20             this.originalMethod = originalMethod;
21             this.newMethod = newMethod;
22         }
23 
24         public Func<long, T> OriginalMethod
25         {
26             get
27             {
28                 return originalMethod;
29             }
30         }
31 
Read(long offset)32         public virtual T Read(long offset)
33         {
34             if(Subrange != null && !Subrange.Value.Contains(checked((ulong)offset)))
35             {
36                 return originalMethod(offset);
37             }
38             return newMethod(originalMethod(offset), offset);
39         }
40 
41         private readonly Func<long, T> originalMethod;
42         private readonly Func<T, long, T> newMethod;
43     }
44 }
45 
46