Merge branch 'master' of steve-icarus@icarus.com:git/verilog
[iverilog.git] / vpi / vcd_priv.c
blob014faeb11eccc3a561b5a0b22df33d7b92ca6a1d
1 /*
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)
8 * any later version.
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
19 #ifdef HAVE_CVS_IDENT
20 #ident "$Id: vcd_priv.c,v 1.6 2004/10/04 01:10:58 steve Exp $"
21 #endif
23 # include "vpi_config.h"
24 # include "vcd_priv.h"
25 # include <stdio.h>
26 # include <stdlib.h>
27 # include <string.h>
28 # include <assert.h>
29 #ifdef HAVE_MALLOC_H
30 # include <malloc.h>
31 #endif
32 # include "stringheap.h"
34 struct stringheap_s name_heap = {0, 0};
36 struct vcd_names_s {
37 const char *name;
38 struct vcd_names_s *next;
41 void vcd_names_add(struct vcd_names_list_s*tab, const char *name)
43 struct vcd_names_s *nl = (struct vcd_names_s *)
44 malloc(sizeof(struct vcd_names_s));
45 assert(nl);
46 nl->name = strdup_sh(&name_heap, name);
47 nl->next = tab->vcd_names_list;
48 tab->vcd_names_list = nl;
49 tab->listed_names ++;
52 static int vcd_names_compare(const void *s1, const void *s2)
54 const char *v1 = *(const char **) s1;
55 const char *v2 = *(const char **) s2;
57 return strcmp(v1, v2);
60 const char *vcd_names_search(struct vcd_names_list_s*tab, const char *key)
62 const char **v;
64 if (tab->vcd_names_sorted == 0)
65 return 0;
67 v = (const char **) bsearch(&key,
68 tab->vcd_names_sorted, tab->sorted_names,
69 sizeof(const char *), vcd_names_compare );
71 return(v ? *v : NULL);
74 void vcd_names_sort(struct vcd_names_list_s*tab)
76 if (tab->listed_names) {
77 struct vcd_names_s *r;
78 const char **l;
80 tab->sorted_names += tab->listed_names;
81 tab->vcd_names_sorted = (const char **)
82 realloc(tab->vcd_names_sorted,
83 tab->sorted_names*(sizeof(const char *)));
84 assert(tab->vcd_names_sorted);
86 l = tab->vcd_names_sorted + tab->sorted_names - tab->listed_names;
87 tab->listed_names = 0;
89 r = tab->vcd_names_list;
90 tab->vcd_names_list = 0x0;
92 while (r) {
93 struct vcd_names_s *rr = r;
94 r = rr->next;
95 *(l++) = rr->name;
96 free(rr);
99 qsort(tab->vcd_names_sorted, tab->sorted_names,
100 sizeof(const char **), vcd_names_compare);
106 Nexus Id cache
108 In structural models, many signals refer to the same nexus.
109 Some structural models also have very many signals. This cache
110 saves nexus_id - vcd_id pairs, and reuses the vcd_id when a signal
111 refers to a nexus that is already dumped.
113 The new signal will be listed as a $var, but no callback
114 will be installed. This saves considerable CPU time and leads
115 to smalle VCD files.
117 The _vpiNexusId is a private (int) property of IVL simulators.
120 struct vcd_id_s
122 const char *id;
123 struct vcd_id_s *next;
124 int nex;
127 static inline unsigned ihash(int nex)
129 unsigned a = nex;
130 a ^= a>>16;
131 a ^= a>>8;
132 return a & 0xff;
135 static struct vcd_id_s **vcd_ids = 0;
137 const char *find_nexus_ident(int nex)
139 struct vcd_id_s *bucket;
141 if (!vcd_ids) {
142 vcd_ids = (struct vcd_id_s **)
143 calloc(256, sizeof(struct vcd_id_s*));
144 assert(vcd_ids);
147 bucket = vcd_ids[ihash(nex)];
148 while (bucket) {
149 if (nex == bucket->nex)
150 return bucket->id;
151 bucket = bucket->next;
154 return 0;
157 void set_nexus_ident(int nex, const char *id)
159 struct vcd_id_s *bucket;
161 assert(vcd_ids);
163 bucket = (struct vcd_id_s *) malloc(sizeof(struct vcd_id_s));
164 bucket->next = vcd_ids[ihash(nex)];
165 bucket->id = id;
166 bucket->nex = nex;
167 vcd_ids[ihash(nex)] = bucket;
172 * $Log: vcd_priv.c,v $
173 * Revision 1.6 2004/10/04 01:10:58 steve
174 * Clean up spurious trailing white space.
176 * Revision 1.5 2004/01/21 01:22:53 steve
177 * Give the vip directory its own configure and vpi_config.h
179 * Revision 1.4 2003/11/10 20:18:02 steve
180 * Missing config.h.
182 * Revision 1.3 2003/04/28 01:03:11 steve
183 * Fix stringheap list management failure.
185 * Revision 1.2 2003/02/13 18:13:28 steve
186 * Make lxt use stringheap to perm-allocate strings.
188 * Revision 1.1 2003/02/11 05:21:33 steve
189 * Support dump of vpiRealVar objects.