OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gdb/] [gdb-6.8/] [sim/] [scarts_32/] [scarts_32-plugins.c] - Blame information for rev 26

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 26 jlechner
/* SCARTS (32-bit) target-dependent code for the GNU simulator.
2
   Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3
   Free Software Foundation, Inc.
4
   Contributed by Martin Walter <mwalter@opencores.org>
5
 
6
   This file is part of the GNU simulators.
7
 
8
   This program is free software; you can redistribute it and/or modify
9
   it under the terms of the GNU General Public License as published by
10
   the Free Software Foundation; either version 3 of the License, or
11
   (at your option) any later version.
12
 
13
   This program is distributed in the hope that it will be useful,
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
   GNU General Public License for more details.
17
 
18
   You should have received a copy of the GNU General Public License
19
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
 
21
 
22
#include <dirent.h>
23
#include <dlfcn.h>
24
#include <pwd.h>
25
#include <stdint.h>
26
#include <stdio.h>
27
#include <stdlib.h>
28
#include <string.h>
29
#include <sys/types.h>
30
#include "modules.h"
31
#include "scarts_32-plugins.h"
32
 
33
/* Macros for preprocessor macro stringification. */
34
#define STRINGIFY(x) #x
35
#define TOSTRING(x) STRINGIFY(x)
36
 
37
#define SCARTS_TOOLCHAIN_LIB_SIM_DIR_STR TOSTRING(SCARTS_TOOLCHAIN_LIB_SIM_DIR)
38
 
39
int              num_plugins;
40
static scarts_plugin_t  plugins[SCARTS_MAX_PLUGINS];
41
 
42
static int              filter_plugin (const struct dirent *entry);
43
static scarts_plugin_t *load_plugin   (char *name);
44
 
45
static int
46
filter_plugin (const struct dirent *entry)
47
{
48
  char *p;
49
  int result;
50
 
51
  result = 0;
52
 
53
  /* Check if '.so' is contained in the entry's name. */
54
  p = strstr (entry->d_name, ".so");
55
  if (p != NULL)
56
  {
57
    /* Check if '.so' terminates the entry's name. */
58
    if (*(p + 3) == '\0')
59
      result = 1;
60
  }
61
 
62
  return result;
63
}
64
 
65
static scarts_plugin_t*
66
load_plugin (char *name)
67
{
68
  char path[SCARTS_MAX_PLUGIN_PATH_LEN + 1];
69
  uint32_t addr, size;
70
  scarts_plugin_t *plugin;
71
 
72
  plugin = &plugins[num_plugins];
73
 
74
  if (num_plugins >= SCARTS_MAX_PLUGINS - 1)
75
  {
76
    fprintf (stderr, "Please recompile GDB with increased SCARTS_MAX_PLUGINS.\n");
77
    return NULL;
78
  }
79
 
80
  strncpy (path, SCARTS_TOOLCHAIN_LIB_SIM_DIR_STR, SCARTS_MAX_PLUGIN_PATH_LEN - (9 + SCARTS_MAX_PLUGIN_NAME_LEN));
81
  strcat (path, "/scarts_32/");
82
  strncat (path, name, SCARTS_MAX_PLUGIN_NAME_LEN);
83
 
84
  plugin->handle = dlopen (path, RTLD_LAZY);
85
  if (!plugin->handle)
86
  {
87
    fprintf (stderr, "Unable to open object file %s\n", path);
88
    return NULL;
89
  }
90
 
91
  strncpy (plugin->name, name, SCARTS_MAX_PLUGIN_NAME_LEN);
92
 
93
  dlerror();
94
 
95
  *(void **) (&plugin->get_mem) = dlsym (plugin->handle, "get_mem");
96
  if (dlerror() != NULL)
97
  {
98
    fprintf (stderr, "Unable to find symbol '%s' in object file %s\n", "get_mem", path);
99
    return NULL;
100
  }
101
 
102
  *(void **) (&plugin->get_mem_map) = dlsym (plugin->handle, "get_mem_map");
103
  if (dlerror() != NULL)
104
  {
105
    fprintf (stderr, "Unable to find symbol '%s' in object file %s\n", "get_mem_map", path);
106
    return NULL;
107
  }
108
 
109
  *(void **) (&plugin->get_status) = dlsym (plugin->handle, "get_status");
110
  if (dlerror() != NULL)
111
    plugin->get_status = NULL;
112
 
113
  *(void **) (&plugin->mem_read) = dlsym (plugin->handle, "mem_read");
114
  if (dlerror() != NULL)
115
  {
116
    fprintf (stderr, "Unable to find symbol '%s' in object file %s\n", "mem_read", path);
117
    return NULL;
118
  }
119
 
120
  *(void **) (&plugin->mem_write) = dlsym (plugin->handle, "mem_write");
121
  if (dlerror() != NULL)
122
  {
123
    fprintf (stderr, "Unable to find symbol '%s' in object file %s\n", "mem_write", path);
124
    return NULL;
125
  }
126
 
127
  *(void **) (&plugin->reset) = dlsym (plugin->handle, "reset");
128
  if (dlerror() != NULL)
129
  {
130
    fprintf (stderr, "Unable to find symbol '%s' in object file %s\n", "reset", path);
131
    return NULL;
132
  }
133
 
134
  *(void **) (&plugin->set_codemem_read_fptr) = dlsym (plugin->handle, "set_codemem_read_fptr");
135
  if (dlerror() != NULL)
136
    plugin->set_codemem_read_fptr = NULL;
137
 
138
  *(void **) (&plugin->set_codemem_write_fptr) = dlsym (plugin->handle, "set_codemem_write_fptr");
139
  if (dlerror() != NULL)
140
    plugin->set_codemem_write_fptr = NULL;
141
 
142
  *(void **) (&plugin->set_datamem_read_fptr) = dlsym (plugin->handle, "set_datamem_read_fptr");
143
  if (dlerror() != NULL)
144
    plugin->set_datamem_read_fptr = NULL;
145
 
146
  *(void **) (&plugin->set_datamem_write_fptr) = dlsym (plugin->handle, "set_datamem_write_fptr");
147
  if (dlerror() != NULL)
148
    plugin->set_datamem_write_fptr = NULL;
149
 
150
  *(void **) (&plugin->tick) = dlsym (plugin->handle, "tick");
151
  if (dlerror() != NULL)
152
  {
153
    fprintf (stderr, "Unable to find symbol '%s' in object file %s\n", "tick", path);
154
    return NULL;
155
  }
156
 
157
  /* Check if the plugin is connected to an interrupt line. */
158
  plugin->int_num = -1;
159
  if (plugin->get_status != NULL)
160
  {
161
    int path_len = strlen (path);
162
 
163
    strcat (path, ".int");
164
    FILE* fint = fopen (path, "r");
165
    if (fint != NULL)
166
    {
167
      if (fscanf (fint, "%d", &(plugin->int_num)) == EOF || plugin->int_num > SCARTS_MAX_INTERRUPT_NUM)
168
        plugin->int_num = -1;
169
 
170
      fclose (fint);
171
    }
172
 
173
    /* Remove the '.int' extension before printing any messages. */
174
    path[path_len] = '\0';
175
  }
176
 
177
  /* Get the start address and size (bytes) of the plugin. */
178
  (*plugin->get_mem_map) (&addr, &size);
179
 
180
  if (plugin->int_num != -1)
181
    fprintf (stdout, "Plugin: %s, Address: 0x%X - %X, Interrupt: %d\n", path, addr, addr + size - 1, plugin->int_num);
182
  else
183
    fprintf (stdout, "Plugin: %s, Address: 0x%X - %X\n", path, addr, addr + size - 1);
184
 
185
  ++num_plugins;
186
  return plugin;
187
}
188
 
189
scarts_plugin_t*
190
scarts_get_plugin (uint32_t addr)
191
{
192
  int i;
193
  uint32_t start, size;
194
 
195
  for (i = 0; i < num_plugins; ++i)
196
  {
197
    (*plugins[i].get_mem_map) (&start, &size);
198
 
199
    if (addr >= start && addr < start + size)
200
      return &plugins[i];
201
  }
202
 
203
  return NULL;
204
}
205
 
206
void
207
scarts_load_plugins (void)
208
{
209
  char path[SCARTS_MAX_PLUGIN_PATH_LEN + 1];
210
  int n;
211
  struct dirent **entries;
212
 
213
  num_plugins = 0;
214
 
215
  strncpy (path, SCARTS_TOOLCHAIN_LIB_SIM_DIR_STR, SCARTS_MAX_PLUGIN_PATH_LEN - (8 + SCARTS_MAX_PLUGIN_NAME_LEN));
216
  strcat (path, "/scarts_32");
217
 
218
  fprintf (stdout, "Scanning for plugins in %s\n", path);
219
  n = scandir (path, &entries, filter_plugin, alphasort);
220
  if (n >= 0)
221
  {
222
    while (n--)
223
    {
224
      load_plugin (entries[n]->d_name);
225
      free (entries[n]);
226
    }
227
 
228
    free (entries);
229
  }
230
  else
231
    fprintf (stderr, "Unable to scan directory %s\n", path);
232
}
233
 
234
void
235
scarts_reset_plugins (void)
236
{
237
  int i;
238
 
239
  for (i = 0; i < num_plugins; ++i)
240
    plugins[i].reset ();
241
}
242
 
243
void
244
scarts_tick_plugins (uint32_t *pc)
245
{
246
  int i;
247
 
248
  for (i = 0; i < num_plugins; ++i)
249
    plugins[i].tick (*pc);
250
}
251
 
252
int
253
scarts_get_plugin_int_request (void)
254
{
255
  int i;
256
  scarts_plugin_t *plugin;
257
 
258
  for (i = 0; i < num_plugins; ++i)
259
  {
260
    plugin = &plugins[i];
261
 
262
    if (plugin->int_num != -1)
263
    {
264
      /* Check if a plugin requested an interrupt. */
265
      if (plugin->get_status != NULL && (*plugin->get_status() & (1 << PROC_CTRL_STATUS_INT)))
266
        return plugin->int_num;
267
    }
268
  }
269
 
270
  return -1;
271
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.