1# Copyright (c) 2018 Synopsys Inc.
2# Copyright (c) 2017 Open Source Foundries Limited.
3#
4# SPDX-License-Identifier: Apache-2.0
5
6'''ARC architecture-specific runners.'''
7
8from os import path
9
10from runners.core import RunnerCaps, ZephyrBinaryRunner
11
12DEFAULT_ARC_GDB_PORT = 3333
13DEFAULT_PROPS_FILE = 'nsim_em.props'
14
15
16class NsimBinaryRunner(ZephyrBinaryRunner):
17    '''Runner front-end for the ARC nSIM.'''
18
19    # This unusual 'flash' implementation matches the original shell script.
20    #
21    # It works by starting a GDB server in a separate session, connecting a
22    # client to it to load the program, and running 'continue' within the
23    # client to execute the application.
24    #
25
26    def __init__(self, cfg,
27                 tui=False,
28                 gdb_port=DEFAULT_ARC_GDB_PORT,
29                 props=DEFAULT_PROPS_FILE):
30        super().__init__(cfg)
31        if cfg.gdb is None:
32            self.gdb_cmd = None
33        else:
34            self.gdb_cmd = [cfg.gdb] + (['-tui'] if tui else [])
35        self.nsim_cmd = ['nsimdrv']
36        self.gdb_port = gdb_port
37        self.props = props
38
39    @classmethod
40    def name(cls):
41        return 'arc-nsim'
42
43    @classmethod
44    def capabilities(cls):
45        return RunnerCaps(commands={'flash', 'debug', 'debugserver', 'attach'})
46
47    @classmethod
48    def do_add_parser(cls, parser):
49        parser.add_argument('--gdb-port', default=DEFAULT_ARC_GDB_PORT,
50                            help='nsim gdb port, defaults to 3333')
51        parser.add_argument('--props', default=DEFAULT_PROPS_FILE,
52                            help='nsim props file, defaults to nsim.props')
53
54    @classmethod
55    def do_create(cls, cfg, args):
56        return NsimBinaryRunner(
57            cfg,
58            gdb_port=args.gdb_port,
59            props=args.props)
60
61    def do_run(self, command, **kwargs):
62        self.require(self.nsim_cmd[0])
63        kwargs['nsim-cfg'] = path.join(self.cfg.board_dir, 'support',
64                                       self.props)
65
66        if command == 'flash':
67            self.do_flash(**kwargs)
68        elif command == 'debug':
69            self.do_debug(**kwargs)
70        else:
71            self.debugserver(**kwargs)
72
73    def do_flash(self, **kwargs):
74        config = kwargs['nsim-cfg']
75
76        cmd = (self.nsim_cmd + ['-propsfile', config, self.cfg.elf_file])
77        self.check_call(cmd)
78
79    def do_debug(self, **kwargs):
80        if self.gdb_cmd is None:
81            raise ValueError('Cannot debug; gdb is missing')
82
83        config = kwargs['nsim-cfg']
84
85        server_cmd = (self.nsim_cmd + ['-gdb',
86                                       f'-port={self.gdb_port}',
87                                       '-propsfile', config])
88        gdb_cmd = (self.gdb_cmd +
89                   ['-ex', f'target remote :{self.gdb_port}',
90                    '-ex', 'load', self.cfg.elf_file])
91        self.require(gdb_cmd[0])
92
93        self.run_server_and_client(server_cmd, gdb_cmd)
94
95    def debugserver(self, **kwargs):
96        config = kwargs['nsim-cfg']
97
98        cmd = (self.nsim_cmd +
99               ['-gdb', f'-port={self.gdb_port}',
100                '-propsfile', config])
101
102        self.check_call(cmd)
103