1 //
2 // Copyright (c) 2010-2022 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 Microsoft.Scripting.Hosting;
10 using Antmicro.Migrant.Hooks;
11 using Antmicro.Migrant;
12 using Antmicro.Renode.Core;
13 using Antmicro.Renode.Logging;
14 using Antmicro.Renode.Exceptions;
15 using Antmicro.Renode.Peripherals.CPU;
16 
17 namespace Antmicro.Renode.Hooks
18 {
19     public class SyncPointHookPythonEngine : PythonEngine
20     {
SyncPointHookPythonEngine(string script, Emulation emulation)21         public SyncPointHookPythonEngine(string script, Emulation emulation)
22         {
23             this.script = script;
24             this.emulation = emulation;
25             InnerInit();
26 
27             Hook = new Action<long>(syncCount =>
28             {
29                 Scope.SetVariable("syncCount", syncCount);
30                 Execute(code, error =>
31                 {
32                     Logger.Log(LogLevel.Error, "Python runtime error: {0}", error);
33                 });
34             });
35         }
36 
37         [PostDeserialization]
InnerInit()38         private void InnerInit()
39         {
40             Scope.SetVariable("self", emulation);
41             var source = Engine.CreateScriptSourceFromString(script);
42             code = Compile(source);
43         }
44 
45         public Action<long> Hook { get; private set; }
46 
47         private readonly string script;
48         private readonly Emulation emulation;
49 
50         [Transient]
51         private CompiledCode code;
52     }
53 }
54 
55