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.Migrant.Hooks;
10 using Microsoft.Scripting.Hosting;
11 using Antmicro.Migrant;
12 using Antmicro.Renode.Logging;
13 using Antmicro.Renode.Peripherals.Bus;
14 using Antmicro.Renode.Peripherals.CPU;
15 using Antmicro.Renode.Exceptions;
16 
17 namespace Antmicro.Renode.Hooks
18 {
19     public class WatchpointHookPythonEngine : PythonEngine
20     {
WatchpointHookPythonEngine(IBusController sysbus, string script)21         public WatchpointHookPythonEngine(IBusController sysbus, string script)
22         {
23             this.sysbus = sysbus;
24             this.script = script;
25 
26             InnerInit();
27             Hook = (cpu, address, width, value) =>
28             {
29                 Scope.SetVariable("cpu", cpu);
30                 Scope.SetVariable("address", address);
31                 Scope.SetVariable("width", width);
32                 Scope.SetVariable("value", value);
33                 Execute(code, error =>
34                 {
35                     this.sysbus.Log(LogLevel.Error, "Python runtime error: {0}", error);
36                 });
37             };
38         }
39 
40         public BusHookDelegate Hook { get; private set; }
41 
42         [PostDeserialization]
InnerInit()43         private void InnerInit()
44         {
45             Scope.SetVariable("self", sysbus);
46 
47             var source = Engine.CreateScriptSourceFromString(script);
48             code = Compile(source);
49         }
50 
51         [Transient]
52         private CompiledCode code;
53 
54         private readonly string script;
55         private readonly IBusController sysbus;
56     }
57 }
58 
59