summaryrefslogtreecommitdiff
path: root/src/grt/grt-cvpi.c
blob: 3e427c551c61b215175045434c4b6c3a24c9906c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*  GRT VPI C helpers.
    Copyright (C) 2003, 2004, 2005 Tristan Gingold & Felix Bertram

    GHDL is free software; you can redistribute it and/or modify it under
    the terms of the GNU General Public License as published by the Free
    Software Foundation; either version 2, or (at your option) any later
    version.

    GHDL is distributed in the hope that it will be useful, but WITHOUT ANY
    WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with GCC; see the file COPYING.  If not, write to the Free
    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
    02111-1307, USA.
*/
//-----------------------------------------------------------------------------
// Description: VPI interface for GRT runtime, "C" helpers
//              the main purpose of this code is to interface with the
//              Icarus Verilog Interactive (IVI) simulator GUI
//-----------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>

//-----------------------------------------------------------------------------
// VPI callback functions
typedef void *vpiHandle, *p_vpi_time, *p_vpi_value;
typedef struct t_cb_data s_cb_data, *p_cb_data;

//-----------------------------------------------------------------------------
// vpi thunking a la Icarus Verilog
#include <stdarg.h>
typedef void *s_vpi_time, *p_vpi_vlog_info, *p_vpi_error_info;
#define VPI_THUNK_MAGIC  (0x87836BA5)
struct t_vpi_systf_data;
void         vpi_register_systf  (const struct t_vpi_systf_data*ss);
void         vpi_vprintf         (const char*fmt, va_list ap);
unsigned int vpi_mcd_close       (unsigned int mcd);
char *       vpi_mcd_name        (unsigned int mcd);
unsigned int vpi_mcd_open        (char *name);
unsigned int vpi_mcd_open_x      (char *name, char *mode);
int          vpi_mcd_vprintf     (unsigned int mcd, const char*fmt, va_list ap);
int          vpi_mcd_fputc       (unsigned int mcd, unsigned char x);
int          vpi_mcd_fgetc       (unsigned int mcd);
vpiHandle    vpi_register_cb     (p_cb_data data);
int          vpi_remove_cb       (vpiHandle ref);
void         vpi_sim_vcontrol    (int operation, va_list ap);
vpiHandle    vpi_handle          (int type, vpiHandle ref);
vpiHandle    vpi_iterate         (int type, vpiHandle ref);
vpiHandle    vpi_scan            (vpiHandle iter);
vpiHandle    vpi_handle_by_index (vpiHandle ref, int index);
void         vpi_get_time        (vpiHandle obj, s_vpi_time*t);
int          vpi_get             (int property, vpiHandle ref);
char*        vpi_get_str         (int property, vpiHandle ref);
void         vpi_get_value       (vpiHandle expr, p_vpi_value value);
vpiHandle    vpi_put_value       (vpiHandle obj, p_vpi_value value,
                                  p_vpi_time when, int flags);
int          vpi_free_object     (vpiHandle ref);
int          vpi_get_vlog_info   (p_vpi_vlog_info vlog_info_p);
int          vpi_chk_error       (p_vpi_error_info info);
vpiHandle    vpi_handle_by_name  (char *name, vpiHandle scope);

typedef struct {
	int magic;
	void         (*vpi_register_systf) (const struct t_vpi_systf_data*ss);
	void         (*vpi_vprintf)        (const char*fmt, va_list ap);
	unsigned int (*vpi_mcd_close)      (unsigned int mcd);
	char*        (*vpi_mcd_name)       (unsigned int mcd);
	unsigned int (*vpi_mcd_open)       (char *name);
	unsigned int (*vpi_mcd_open_x)     (char *name, char *mode);
	int          (*vpi_mcd_vprintf)    (unsigned int mcd, const char*fmt, va_list ap);
	int          (*vpi_mcd_fputc)      (unsigned int mcd, unsigned char x);
	int          (*vpi_mcd_fgetc)      (unsigned int mcd);
	vpiHandle    (*vpi_register_cb)    (p_cb_data data);
	int          (*vpi_remove_cb)      (vpiHandle ref);
	void         (*vpi_sim_vcontrol)   (int operation, va_list ap);
	vpiHandle    (*vpi_handle)         (int type, vpiHandle ref);
	vpiHandle    (*vpi_iterate)        (int type, vpiHandle ref);
	vpiHandle    (*vpi_scan)           (vpiHandle iter);
	vpiHandle    (*vpi_handle_by_index)(vpiHandle ref, int index);
	void         (*vpi_get_time)       (vpiHandle obj, s_vpi_time*t);
	int          (*vpi_get)            (int property, vpiHandle ref);
	char*        (*vpi_get_str)        (int property, vpiHandle ref);
	void         (*vpi_get_value)      (vpiHandle expr, p_vpi_value value);
	vpiHandle    (*vpi_put_value)      (vpiHandle obj, p_vpi_value value,
	                                    p_vpi_time when, int flags);
	int          (*vpi_free_object)    (vpiHandle ref);
	int          (*vpi_get_vlog_info)  (p_vpi_vlog_info vlog_info_p);
	int          (*vpi_chk_error)      (p_vpi_error_info info);
	vpiHandle    (*vpi_handle_by_name) (char *name, vpiHandle scope);
} vpi_thunk, *p_vpi_thunk;

int vpi_register_sim(p_vpi_thunk tp);

static vpi_thunk thunkTable =
{	VPI_THUNK_MAGIC,
	vpi_register_systf,
	vpi_vprintf,
	vpi_mcd_close,
	vpi_mcd_name,
	vpi_mcd_open,
	0, //vpi_mcd_open_x,
	0, //vpi_mcd_vprintf,
	0, //vpi_mcd_fputc,
	0, //vpi_mcd_fgetc,
	vpi_register_cb,
	vpi_remove_cb,
	0, //vpi_sim_vcontrol,
	vpi_handle,
	vpi_iterate,
	vpi_scan,
	vpi_handle_by_index,
	vpi_get_time,
	vpi_get,
	vpi_get_str,
	vpi_get_value,
	vpi_put_value,
	vpi_free_object,
	vpi_get_vlog_info,
	vpi_chk_error,
	vpi_handle_by_name
};

//-----------------------------------------------------------------------------
// VPI module load & startup
static void * module_open (const char *path);
static void * module_symbol (void *handle, const char *symbol);
static const char *module_error (void);

#if defined(__WIN32__)
#include <windows.h>
static void *
module_open (const char *path)
{
  return (void *)LoadLibrary (path);
}

static void *
module_symbol (void *handle, const char *symbol)
{
  return (void *)GetProcAddress ((HMODULE)handle, symbol);
}

static const char *
module_error (void)
{
  static char msg[256];

  FormatMessage
    (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
     NULL,
     GetLastError (),
     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
     (LPTSTR) &msg,
     sizeof (msg) - 1,
     NULL);
  return msg;
}
#else
#include <dlfcn.h>
static void *
module_open (const char *path)
{
  return dlopen (path, RTLD_LAZY);
}

static void *
module_symbol (void *handle, const char *symbol)
{
  return dlsym (handle, symbol);
}

static const char *
module_error (void)
{
  return dlerror ();
}
#endif

int
loadVpiModule (const char* modulename)
{
  static const char * const vpitablenames[] =
    {
      "_vlog_startup_routines", // with leading underscore: MacOSX
      "vlog_startup_routines"   // w/o  leading underscore: Linux
    };
  static const char * const vpithunknames[] =
    {
      "_vpi_register_sim",      // with leading underscore: MacOSX
      "vpi_register_sim"        // w/o  leading underscore: Linux
    };

  int i;
  void* vpimod;

  fprintf (stderr, "loading VPI module '%s'\n", modulename);

  vpimod = module_open (modulename);

  if (vpimod == NULL)
    {
      const char *msg;

      msg = module_error ();

      fprintf (stderr, "%s\n", msg == NULL ? "unknown dlopen error" : msg);
      return -1;
    }

  for (i = 0; i < 2; i++) // try with and w/o leading underscores
    {
      void* vpithunk;
      void* vpitable;

      vpitable = module_symbol (vpimod, vpitablenames[i]);
      vpithunk = module_symbol (vpimod, vpithunknames[i]);

      if (vpithunk)
	{
	  typedef int (*funT)(p_vpi_thunk tp);
	  funT regsim;

	  regsim = (funT)vpithunk;
	  regsim (&thunkTable);
	}
      else
	{
	  // this is not an error, as the register-mechanism
	  // is not standardized
	}

      if (vpitable)
	{
	  unsigned int tmp;
	  //extern void (*vlog_startup_routines[])();
	  typedef void (*vlog_startup_routines_t)(void);
	  vlog_startup_routines_t *vpifuns;

	  vpifuns = (vlog_startup_routines_t*)vpitable;
	  for (tmp = 0; vpifuns[tmp]; tmp++)
	    {
	      vpifuns[tmp]();
	    }

	  fprintf (stderr, "VPI module loaded!\n");
	  return 0; // successfully registered VPI module
	}
    }
  fprintf (stderr, "vlog_startup_routines not found\n");
  return -1; // failed to register VPI module
}

void
vpi_printf (const char *fmt, ...)
{
  va_list params;

  va_start (params, fmt);
  vprintf (fmt, params);
  va_end (params);
}

//-----------------------------------------------------------------------------
// end of file