1from time import sleep
2from Antmicro import Renode
3
4def mc_uart_connect(device_name):
5    def __printer(b):
6        sys.stdout.write(chr(b))
7
8    uart = None
9    try:
10        uart = clr.Convert(self.Machine[str(device_name)], Renode.Peripherals.UART.IUART)
11    except:
12        print("Peripheral %s not found or not an IUART." % device_name)
13        return 1
14
15    print("Redirecting the input to %s, press <ESC> to quit..." % device_name)
16    uart.CharReceived += __printer
17    while True:
18       c = sys.stdin.read(1)
19       if ord(c) == 27:
20           break
21       uart.WriteChar(ord(c))
22    uart.CharReceived -= __printer
23    print("Disconnected from %s" % device_name)
24
25def mc_next_value(offset = 0):
26    print "%d" % (mc_next_value.current_value + offset)
27    mc_next_value.current_value = mc_next_value.current_value + 1
28
29mc_next_value.current_value = 0
30
31def mc_sleep(time):
32    sleep(float(time))
33
34def mc_console_log(string):
35    System.Console.WriteLine(string)
36
37def mc_echo(*value):
38    if len(value) == 2:
39        if value[0] == "-n":
40            sys.stdout.write(value[1])
41            return
42    elif len(value) == 1:
43        print value[0]
44        return
45    elif len(value) == 0:
46        print
47        return
48    print "usage: echo [-n] [string]"
49
50def mc_dump(mem_start_val, mem_count_val, wid_val = 16):
51    wid = int(wid_val)
52    sysbus = self.Machine["sysbus"]
53    mem_start = int(mem_start_val)
54    mem_count = int(mem_count_val)
55    for a in range(mem_start, mem_start + mem_count, wid):
56        data = sysbus.ReadBytes(a, wid)
57        print "0x%08X |" % a,
58        for b in range(0, wid):
59            print "%02X" % data[b] ,
60        print "| " ,
61        for b in range(0, wid):
62            c = data[b]
63            if not ((c < 0x20) or (c > 127)):
64                print "%c%c" % (chr(8), chr(c)) ,
65            else:
66                print "%c." % chr(8) ,
67        print
68
69def mc_dump_file(mem_start_val, mem_count_val, filename):
70    sysbus = self.Machine["sysbus"]
71    mem_start = int(mem_start_val)
72    mem_count = int(mem_count_val)
73    tab = sysbus.ReadBytes(mem_start, mem_count)
74    fl = System.IO.FileStream(filename, System.IO.FileMode.Create, System.IO.FileAccess.Write)
75    fl.Write(tab, 0, mem_count)
76    fl.Close()
77
78def mc_get_environ(variable):
79    v = System.Environment.GetEnvironmentVariable(variable)
80    if v != None:
81        print v
82
83def mc_if(condition, cmd):
84    if condition:
85        for line in cmd.splitlines():
86            monitor.Parse(line)
87
88externals = type('ExternalsManagerAccessor', (object,), dict(
89    __getitem__ = lambda _, name: Renode.Core.Structure.IHasChildren[Renode.Core.IExternal].TryGetByName(emulationManager.CurrentEmulation.ExternalsManager, name)[0],
90    __getattr__ = lambda self, name: self.__getitem__(name),
91))()
92
93variables = type('MonitorVariablesAccessor', (object,), dict(
94    __getitem__ = lambda _, name: monitor.GetVariable(name),
95    __getattr__ = lambda self, name: self.__getitem__(name),
96))()
97