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 WriteHookWrapper<T> : HookWrapper
16     {
WriteHookWrapper(IBusPeripheral peripheral, Action<long, T> originalMethod, Func<T, long, T> newMethod = null, Range? subrange = null)17         public WriteHookWrapper(IBusPeripheral peripheral, Action<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 Action<long, T> OriginalMethod
25         {
26             get
27             {
28                 return originalMethod;
29             }
30         }
31 
Write(long offset, T value)32         public virtual void Write(long offset, T value)
33         {
34             if(Subrange != null && !Subrange.Value.Contains(checked((ulong)offset)))
35             {
36                 originalMethod(offset, value);
37                 return;
38             }
39             var modifiedValue = newMethod(value, offset);
40             originalMethod(offset, modifiedValue);
41         }
42 
43         private readonly Action<long, T> originalMethod;
44         private readonly Func<T, long, T> newMethod;
45     }
46 }
47 
48