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 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 do_add_parser(cls, parser):
45        parser.add_argument('--gdb-port', default=DEFAULT_ARC_GDB_PORT,
46                            help='nsim gdb port, defaults to 3333')
47        parser.add_argument('--props', default=DEFAULT_PROPS_FILE,
48                            help='nsim props file, defaults to nsim.props')
49
50    @classmethod
51    def do_create(cls, cfg, args):
52        return NsimBinaryRunner(
53            cfg,
54            gdb_port=args.gdb_port,
55            props=args.props)
56
57    def do_run(self, command, **kwargs):
58        self.require(self.nsim_cmd[0])
59        kwargs['nsim-cfg'] = path.join(self.cfg.board_dir, 'support',
60                                       self.props)
61
62        if command == 'flash':
63            self.do_flash(**kwargs)
64        elif command == 'debug':
65            self.do_debug(**kwargs)
66        else:
67            self.debugserver(**kwargs)
68
69    def do_flash(self, **kwargs):
70        config = kwargs['nsim-cfg']
71
72        cmd = (self.nsim_cmd + ['-propsfile', config, self.cfg.elf_file])
73        self.check_call(cmd)
74
75    def do_debug(self, **kwargs):
76        if self.gdb_cmd is None:
77            raise ValueError('Cannot debug; gdb is missing')
78
79        config = kwargs['nsim-cfg']
80
81        server_cmd = (self.nsim_cmd + ['-gdb',
82                                       '-port={}'.format(self.gdb_port),
83                                       '-propsfile', config])
84        gdb_cmd = (self.gdb_cmd +
85                   ['-ex', 'target remote :{}'.format(self.gdb_port),
86                    '-ex', 'load', self.cfg.elf_file])
87        self.require(gdb_cmd[0])
88
89        self.run_server_and_client(server_cmd, gdb_cmd)
90
91    def debugserver(self, **kwargs):
92        config = kwargs['nsim-cfg']
93
94        cmd = (self.nsim_cmd +
95               ['-gdb', '-port={}'.format(self.gdb_port),
96                '-propsfile', config])
97
98        self.check_call(cmd)
99