2 * Copyright (c) 2003 Stephen Williams (steve@icarus.com)
4 * This source code is free software; you can redistribute it
5 * and/or modify it in source code form under the terms of the GNU
6 * General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 #ident "$Id: vcd_priv.c,v 1.6 2004/10/04 01:10:58 steve Exp $"
23 # include "vpi_config.h"
24 # include "vcd_priv.h"
33 # include "stringheap.h"
35 int is_escaped_id(const char *name
)
40 /* The first digit must be alpha or '_' to be a normal id. */
41 if (isalpha(name
[0]) || name
[0] == '_') {
42 for (lp
=1; name
[lp
] != '\0'; lp
++) {
43 /* If this digit is not alpha-numeric or '_' we have
44 * an escaped identifier. */
45 if (!(isalnum(name
[lp
]) || name
[lp
] == '_')) {
49 /* We looked at all the digits, so this is a normal id. */
55 struct stringheap_s name_heap
= {0, 0};
59 struct vcd_names_s
*next
;
62 void vcd_names_add(struct vcd_names_list_s
*tab
, const char *name
)
64 struct vcd_names_s
*nl
= (struct vcd_names_s
*)
65 malloc(sizeof(struct vcd_names_s
));
67 nl
->name
= strdup_sh(&name_heap
, name
);
68 nl
->next
= tab
->vcd_names_list
;
69 tab
->vcd_names_list
= nl
;
73 static int vcd_names_compare(const void *s1
, const void *s2
)
75 const char *v1
= *(const char **) s1
;
76 const char *v2
= *(const char **) s2
;
78 return strcmp(v1
, v2
);
81 const char *vcd_names_search(struct vcd_names_list_s
*tab
, const char *key
)
85 if (tab
->vcd_names_sorted
== 0)
88 v
= (const char **) bsearch(&key
,
89 tab
->vcd_names_sorted
, tab
->sorted_names
,
90 sizeof(const char *), vcd_names_compare
);
92 return(v
? *v
: NULL
);
95 void vcd_names_sort(struct vcd_names_list_s
*tab
)
97 if (tab
->listed_names
) {
98 struct vcd_names_s
*r
;
101 tab
->sorted_names
+= tab
->listed_names
;
102 tab
->vcd_names_sorted
= (const char **)
103 realloc(tab
->vcd_names_sorted
,
104 tab
->sorted_names
*(sizeof(const char *)));
105 assert(tab
->vcd_names_sorted
);
107 l
= tab
->vcd_names_sorted
+ tab
->sorted_names
- tab
->listed_names
;
108 tab
->listed_names
= 0;
110 r
= tab
->vcd_names_list
;
111 tab
->vcd_names_list
= 0x0;
114 struct vcd_names_s
*rr
= r
;
120 qsort(tab
->vcd_names_sorted
, tab
->sorted_names
,
121 sizeof(const char **), vcd_names_compare
);
129 In structural models, many signals refer to the same nexus.
130 Some structural models also have very many signals. This cache
131 saves nexus_id - vcd_id pairs, and reuses the vcd_id when a signal
132 refers to a nexus that is already dumped.
134 The new signal will be listed as a $var, but no callback
135 will be installed. This saves considerable CPU time and leads
138 The _vpiNexusId is a private (int) property of IVL simulators.
144 struct vcd_id_s
*next
;
148 static inline unsigned ihash(int nex
)
156 static struct vcd_id_s
**vcd_ids
= 0;
158 const char *find_nexus_ident(int nex
)
160 struct vcd_id_s
*bucket
;
163 vcd_ids
= (struct vcd_id_s
**)
164 calloc(256, sizeof(struct vcd_id_s
*));
168 bucket
= vcd_ids
[ihash(nex
)];
170 if (nex
== bucket
->nex
)
172 bucket
= bucket
->next
;
178 void set_nexus_ident(int nex
, const char *id
)
180 struct vcd_id_s
*bucket
;
184 bucket
= (struct vcd_id_s
*) malloc(sizeof(struct vcd_id_s
));
185 bucket
->next
= vcd_ids
[ihash(nex
)];
188 vcd_ids
[ihash(nex
)] = bucket
;
191 /* This is used by the compiletf routines to check if an argument
193 static void check_numeric_arg(vpiHandle arg
, char *msg
, PLI_BYTE8
*name
)
197 switch (vpi_get(vpiType
, arg
)) {
200 /* String constants are invalid numeric values. */
201 if (vpi_get(vpiConstType
, arg
) == vpiStringConst
) {
202 vpi_mcd_printf(1, msg
, name
);
203 vpi_control(vpiFinish
, 1);
207 /* These have valid numeric values. */
217 /* Anything else is not a numeric value. */
218 vpi_mcd_printf(1, msg
, name
);
219 vpi_control(vpiFinish
, 1);
224 /* This is used by the compiletf routines to check if an argument
225 * is a string value. */
226 static void check_string_arg(vpiHandle arg
, char *msg
, PLI_BYTE8
*name
)
231 switch (vpi_get(vpiType
, arg
)) {
234 /* These must be a string or binary constant. */
235 ctype
= vpi_get(vpiConstType
, arg
);
236 if (ctype
!= vpiStringConst
&& ctype
!= vpiBinaryConst
) {
237 vpi_mcd_printf(1, msg
, name
);
238 vpi_control(vpiFinish
, 1);
242 /* These have valid string values. */
251 /* Anything else is not a string. */
252 vpi_mcd_printf(1, msg
, name
);
253 vpi_control(vpiFinish
, 1);
259 * Since the compiletf routines are all the same they are located here,
260 * so we only need a single copy.
263 /* $dumpall does not take an argument. */
264 PLI_INT32
sys_dumpall_compiletf(PLI_BYTE8
*name
)
266 vpiHandle callh
= vpi_handle(vpiSysTfCall
, 0);
267 vpiHandle argv
= vpi_iterate(vpiArgument
, callh
);
270 vpi_mcd_printf(1, "ERROR: %s does not take an argument.\n", name
);
271 vpi_control(vpiFinish
, 1);
277 /* $dumpfile takes a single string argument. */
278 PLI_INT32
sys_dumpfile_compiletf(PLI_BYTE8
*name
)
280 vpiHandle callh
= vpi_handle(vpiSysTfCall
, 0);
281 vpiHandle argv
= vpi_iterate(vpiArgument
, callh
);
283 /* Check that there is an argument and that it is a string. */
285 vpi_mcd_printf(1, "ERROR: %s requires an argument.\n", name
);
286 vpi_control(vpiFinish
, 1);
289 check_string_arg(vpi_scan(argv
), "ERROR: %s's argument must be a"
292 /* Check that there is only a single argument. */
293 if (vpi_scan(argv
) != 0) {
294 vpi_mcd_printf(1, "ERROR: %s takes a single argument.\n", name
);
295 vpi_control(vpiFinish
, 1);
302 /* $dumpflush does not take an argument. */
303 PLI_INT32
sys_dumpflush_compiletf(PLI_BYTE8
*name
)
305 vpiHandle callh
= vpi_handle(vpiSysTfCall
, 0);
306 vpiHandle argv
= vpi_iterate(vpiArgument
, callh
);
309 vpi_mcd_printf(1, "ERROR: %s does not take an argument.\n", name
);
310 vpi_control(vpiFinish
, 1);
316 /* $dumplimit takes a single numeric argument. */
317 PLI_INT32
sys_dumplimit_compiletf(PLI_BYTE8
*name
)
319 vpiHandle callh
= vpi_handle(vpiSysTfCall
, 0);
320 vpiHandle argv
= vpi_iterate(vpiArgument
, callh
);
322 /* Check that there is an argument and that it is numeric. */
324 vpi_mcd_printf(1, "ERROR: %s requires an argument.\n", name
);
325 vpi_control(vpiFinish
, 1);
328 check_numeric_arg(vpi_scan(argv
), "ERROR: %s's argument must be"
329 " numeric.\n", name
);
331 /* Check that there is only a single argument. */
332 if (vpi_scan(argv
) != 0) {
333 vpi_mcd_printf(1, "ERROR: %s takes a single argument.\n", name
);
334 vpi_control(vpiFinish
, 1);
341 /* $dumpoff does not take an argument. */
342 PLI_INT32
sys_dumpoff_compiletf(PLI_BYTE8
*name
)
344 vpiHandle callh
= vpi_handle(vpiSysTfCall
, 0);
345 vpiHandle argv
= vpi_iterate(vpiArgument
, callh
);
348 vpi_mcd_printf(1, "ERROR: %s does not take an argument.\n", name
);
349 vpi_control(vpiFinish
, 1);
355 /* $dumpon does not take an argument. */
356 PLI_INT32
sys_dumpon_compiletf(PLI_BYTE8
*name
)
358 vpiHandle callh
= vpi_handle(vpiSysTfCall
, 0);
359 vpiHandle argv
= vpi_iterate(vpiArgument
, callh
);
362 vpi_mcd_printf(1, "ERROR: %s does not take an argument.\n", name
);
363 vpi_control(vpiFinish
, 1);
369 /* $dumpvars takes a variety of arguments. */
370 PLI_INT32
sys_dumpvars_compiletf(PLI_BYTE8
*name
)
372 vpiHandle callh
= vpi_handle(vpiSysTfCall
, 0);
373 vpiHandle argv
= vpi_iterate(vpiArgument
, callh
);
376 /* No arguments is OK, dump everything. */
380 /* The first argument is the numeric level. */
381 check_numeric_arg(vpi_scan(argv
), "ERROR: %s's first argument must be"
382 " numeric.\n", name
);
384 /* The rest of the arguments are either a module or a variable. */
385 while ((arg
=vpi_scan(argv
)) != NULL
) {
386 switch(vpi_get(vpiType
, arg
)) {
387 /* The module types. */
393 /* The variable types. */
402 vpi_mcd_printf(1, "ERROR: %s cannot dump a %s.\n",
403 name
, vpi_get_str(vpiType
, arg
));
404 vpi_control(vpiFinish
, 1);