Careful not to lose bits parameter add expression
[iverilog.git] / vpi / lxt2_write.h
blobbd33729fe4980ef2e34ad4eaa35408357e02a99f
1 /*
2 * Copyright (c) 2003-2004 Tony Bybell.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
23 #ifndef DEFS_LXTW_H
24 #define DEFS_LXTW_H
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <inttypes.h>
33 #include <zlib.h>
35 #if defined _MSC_VER || defined __MINGW32__
36 #define fseeko fseek
37 #define ftello ftell
38 #endif
41 #define LXT2_WR_HDRID (0x1380)
42 #define LXT2_WR_VERSION (0x0001)
44 #define LXT2_WR_GRANULE_SIZE (64)
45 #define LXT2_WR_GRANULE_NUM (256)
46 #define LXT2_WR_PARTIAL_SIZE (2048)
48 #define LXT2_WR_GRAN_SECT_TIME 0
49 #define LXT2_WR_GRAN_SECT_DICT 1
50 #define LXT2_WR_GRAN_SECT_TIME_PARTIAL 2
52 #define LXT2_WR_GZWRITE_BUFFER 4096
53 #define LXT2_WR_SYMPRIME 65519
55 typedef uint64_t lxttime_t;
58 #ifndef _MSC_VER
59 #ifdef __MINGW32__
60 #define LXT2_WR_LLD "%I64d"
61 #else
62 #define LXT2_WR_LLD "%lld"
63 #endif
64 #define LXT2_WR_LLDESC(x) x##LL
65 #define LXT2_WR_ULLDESC(x) x##ULL
66 #else
67 #define LXT2_WR_LLD "%I64d"
68 #define LXT2_WR_LLDESC(x) x##i64
69 #define LXT2_WR_ULLDESC(x) x##i64
70 #endif
72 #if LXT2_WR_GRANULE_SIZE > 32
73 typedef unsigned long long granmsk_t;
74 #define LXT2_WR_GRAN_0VAL (LXT2_WR_ULLDESC(0))
75 #define LXT2_WR_GRAN_1VAL (LXT2_WR_ULLDESC(1))
76 #else
77 typedef unsigned int granmsk_t;
78 #define LXT2_WR_GRAN_0VAL (0)
79 #define LXT2_WR_GRAN_1VAL (1)
80 #endif
83 enum LXT2_WR_Encodings {
84 LXT2_WR_ENC_0,
85 LXT2_WR_ENC_1,
86 LXT2_WR_ENC_INV,
87 LXT2_WR_ENC_LSH0,
88 LXT2_WR_ENC_LSH1,
89 LXT2_WR_ENC_RSH0,
90 LXT2_WR_ENC_RSH1,
92 LXT2_WR_ENC_ADD1,
93 LXT2_WR_ENC_ADD2,
94 LXT2_WR_ENC_ADD3,
95 LXT2_WR_ENC_ADD4,
97 LXT2_WR_ENC_SUB1,
98 LXT2_WR_ENC_SUB2,
99 LXT2_WR_ENC_SUB3,
100 LXT2_WR_ENC_SUB4,
102 LXT2_WR_ENC_X,
103 LXT2_WR_ENC_Z,
105 LXT2_WR_ENC_BLACKOUT,
107 LXT2_WR_DICT_START
111 * integer splay
113 typedef struct lxt2_wr_ds_tree_node lxt2_wr_ds_Tree;
114 struct lxt2_wr_ds_tree_node {
115 lxt2_wr_ds_Tree * left, * right;
116 granmsk_t item;
117 int val;
118 lxt2_wr_ds_Tree * next;
123 * string splay
125 typedef struct lxt2_wr_dslxt_tree_node lxt2_wr_dslxt_Tree;
126 struct lxt2_wr_dslxt_tree_node {
127 lxt2_wr_dslxt_Tree * left, * right;
128 char *item;
129 unsigned int val;
130 lxt2_wr_dslxt_Tree * next;
134 struct lxt2_wr_trace
136 FILE *handle;
137 gzFile zhandle;
139 lxt2_wr_dslxt_Tree *dict; /* dictionary manipulation */
140 unsigned int num_dict_entries;
141 unsigned int dict_string_mem_required;
142 lxt2_wr_dslxt_Tree *dict_head;
143 lxt2_wr_dslxt_Tree *dict_curr;
145 lxt2_wr_ds_Tree *mapdict; /* bitmap compression */
146 unsigned int num_map_entries;
147 lxt2_wr_ds_Tree *mapdict_head;
148 lxt2_wr_ds_Tree *mapdict_curr;
150 off_t position;
151 off_t zfacname_predec_size, zfacname_size, zfacgeometry_size;
152 off_t zpackcount, zpackcount_cumulative;
153 off_t current_chunk, current_chunkz;
155 struct lxt2_wr_symbol *sym[LXT2_WR_SYMPRIME];
156 struct lxt2_wr_symbol **sorted_facs;
157 struct lxt2_wr_symbol *symchain;
158 int numfacs, numalias;
159 int numfacbytes;
160 int longestname;
162 int numsections, numblock;
163 off_t facname_offset, facgeometry_offset;
165 lxttime_t mintime, maxtime;
166 unsigned int timegranule;
167 int timescale;
168 int timepos;
169 unsigned int maxgranule;
170 lxttime_t firsttime, lasttime;
171 lxttime_t timetable[LXT2_WR_GRANULE_SIZE];
173 unsigned int partial_iter;
175 char *compress_fac_str;
176 int compress_fac_len;
178 lxttime_t flushtime;
179 unsigned flush_valid : 1;
181 unsigned do_strip_brackets : 1;
182 unsigned emitted : 1; /* gate off change field zmode changes when set */
183 unsigned timeset : 1; /* time has been modified from 0..0 */
184 unsigned bumptime : 1; /* says that must go to next time position in granule as value change exists for current time */
185 unsigned granule_dirty : 1; /* for flushing out final block */
186 unsigned blackout : 1; /* blackout on/off */
187 unsigned partial : 1; /* partial (vertical) trace support */
188 unsigned partial_zip : 1; /* partial (vertical) trace support for zip subregions */
189 unsigned no_checkpoint : 1; /* turns off interblock checkpointing */
190 unsigned partial_preference : 1; /* partial preference encountered on some facs */
192 char initial_value;
194 char zmode[4]; /* fills in with "wb0".."wb9" */
195 unsigned int gzbufpnt;
196 unsigned char gzdest[LXT2_WR_GZWRITE_BUFFER + 4]; /* enough for zlib buffering */
198 char *lxtname;
199 off_t break_size;
200 off_t break_header_size;
201 unsigned int break_number;
205 struct lxt2_wr_symbol
207 struct lxt2_wr_symbol *next;
208 struct lxt2_wr_symbol *symchain;
209 char *name;
210 int namlen;
212 int facnum;
213 struct lxt2_wr_symbol *aliased_to;
215 char *value; /* fac's actual value */
217 unsigned int rows;
218 int msb, lsb;
219 int len;
220 int flags;
222 unsigned partial_preference : 1; /* in order to shove nets to the first partial group */
224 unsigned int chgpos;
225 granmsk_t msk; /* must contain LXT2_WR_GRANULE_SIZE bits! */
226 unsigned int chg[LXT2_WR_GRANULE_SIZE];
230 #define LXT2_WR_SYM_F_BITS (0)
231 #define LXT2_WR_SYM_F_INTEGER (1<<0)
232 #define LXT2_WR_SYM_F_DOUBLE (1<<1)
233 #define LXT2_WR_SYM_F_STRING (1<<2)
234 #define LXT2_WR_SYM_F_TIME (LXT2_WR_SYM_F_STRING) /* user must correctly format this as a string */
235 #define LXT2_WR_SYM_F_ALIAS (1<<3)
237 #define LXT2_WR_SYM_F_SIGNED (1<<4)
238 #define LXT2_WR_SYM_F_BOOLEAN (1<<5)
239 #define LXT2_WR_SYM_F_NATURAL ((1<<6)|(LXT2_WR_SYM_F_INTEGER))
240 #define LXT2_WR_SYM_F_POSITIVE ((1<<7)|(LXT2_WR_SYM_F_INTEGER))
241 #define LXT2_WR_SYM_F_CHARACTER (1<<8)
243 #define LXT2_WR_SYM_F_CONSTANT (1<<9)
244 #define LXT2_WR_SYM_F_VARIABLE (1<<10)
245 #define LXT2_WR_SYM_F_SIGNAL (1<<11)
247 #define LXT2_WR_SYM_F_IN (1<<12)
248 #define LXT2_WR_SYM_F_OUT (1<<13)
249 #define LXT2_WR_SYM_F_INOUT (1<<14)
251 #define LXT2_WR_SYM_F_WIRE (1<<15)
252 #define LXT2_WR_SYM_F_REG (1<<16)
255 /* file I/O */
256 struct lxt2_wr_trace * lxt2_wr_init(const char *name);
257 void lxt2_wr_flush(struct lxt2_wr_trace *lt);
258 void lxt2_wr_close(struct lxt2_wr_trace *lt);
260 /* for dealing with very large traces, split into multiple files approximately "siz" in length */
261 void lxt2_wr_set_break_size(struct lxt2_wr_trace *lt, off_t siz);
263 /* 0 = no compression, 9 = best compression, 4 = default */
264 void lxt2_wr_set_compression_depth(struct lxt2_wr_trace *lt, unsigned int depth);
266 /* default is partial off, turning on makes for faster trace reads, nonzero zipmode causes vertical compression */
267 void lxt2_wr_set_partial_off(struct lxt2_wr_trace *lt);
268 void lxt2_wr_set_partial_on(struct lxt2_wr_trace *lt, int zipmode);
269 void lxt2_wr_set_partial_preference(struct lxt2_wr_trace *lt, const char *name);
271 /* turning off checkpointing makes for smaller files */
272 void lxt2_wr_set_checkpoint_off(struct lxt2_wr_trace *lt);
273 void lxt2_wr_set_checkpoint_on(struct lxt2_wr_trace *lt);
275 /* facility creation */
276 void lxt2_wr_set_initial_value(struct lxt2_wr_trace *lt, char value);
277 struct lxt2_wr_symbol * lxt2_wr_symbol_find(struct lxt2_wr_trace *lt, const char *name);
278 struct lxt2_wr_symbol * lxt2_wr_symbol_add(struct lxt2_wr_trace *lt, const char *name, unsigned int rows, int msb, int lsb, int flags);
279 struct lxt2_wr_symbol * lxt2_wr_symbol_alias(struct lxt2_wr_trace *lt, const char *existing_name, const char *alias, int msb, int lsb);
280 void lxt2_wr_symbol_bracket_stripping(struct lxt2_wr_trace *lt, int doit);
282 /* each granule is LXT2_WR_GRANULE_SIZE (32 or 64) timesteps, default is 256 per section */
283 void lxt2_wr_set_maxgranule(struct lxt2_wr_trace *lt, unsigned int maxgranule);
285 /* time ops */
286 void lxt2_wr_set_timescale(struct lxt2_wr_trace *lt, int timescale);
287 int lxt2_wr_set_time(struct lxt2_wr_trace *lt, unsigned int timeval);
288 int lxt2_wr_inc_time_by_delta(struct lxt2_wr_trace *lt, unsigned int timeval);
289 int lxt2_wr_set_time64(struct lxt2_wr_trace *lt, lxttime_t timeval);
290 int lxt2_wr_inc_time_by_delta64(struct lxt2_wr_trace *lt, lxttime_t timeval);
292 /* allows blackout regions in LXT files */
293 void lxt2_wr_set_dumpoff(struct lxt2_wr_trace *lt);
294 void lxt2_wr_set_dumpon(struct lxt2_wr_trace *lt);
296 /* left fill on bit_string uses vcd semantics (left fill with value[0] unless value[0]=='1', then use '0') */
297 int lxt2_wr_emit_value_int(struct lxt2_wr_trace *lt, struct lxt2_wr_symbol *s, unsigned int row, int value);
298 int lxt2_wr_emit_value_double(struct lxt2_wr_trace *lt, struct lxt2_wr_symbol *s, unsigned int row, double value);
299 int lxt2_wr_emit_value_string(struct lxt2_wr_trace *lt, struct lxt2_wr_symbol *s, unsigned int row, char *value);
300 int lxt2_wr_emit_value_bit_string(struct lxt2_wr_trace *lt, struct lxt2_wr_symbol *s, unsigned int row, char *value);
302 #endif