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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628 | /**
* Copyright (c) 2000-2024 by Altair Engineering, Inc.
* All rights reserved.
*
* Altair Engineering, Inc. makes this software available as part of the Vision
* tool platform. As long as you are a licensee of the Vision tool platform
* you may make copies of the software and modify it to be used within the
* Vision tool platform, but you must include all of this notice on any copy.
* Redistribution without written permission to any third party, with or
* without modification, is not permitted.
* Altair Engineering, Inc. does not warrant that this software is error free
* or fit for any purpose. Altair Engineering, Inc. disclaims any liability for
* all claims, expenses, losses, damages and costs any user may incur as a
* result of using, copying or modifying the software.
* ============================================================================
* @c-code
* Dump Zdb to ASCII Text File
* @section
* C Level Examples
* @description
* Dump the contents of the zdb database into an ASCII text file. This
* example do not need Tcl and can be used as a reference how to access
* the ZDB structures.
* @files
* zdb/zdbDump.c
* @tag
* zdb
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "zos/zassert.h"
#include "zos/ztypes.h"
#include "zos/zos.h"
#include "zos/zio.h"
#include "zos/zerror.h"
#include "zos/zhash.h"
#include "zdb/zdb.h"
#include "zdb/zdbmem.h"
#include "zdb/zpos.h"
#include "zdb/zprim.h"
/* ===========================================================================
* Declare prototypes.
* ===========================================================================
*/
zBool zDump(DB* db, const char* fname);
/* ===========================================================================
* netName - Return the name of a net or an empty string if the net is NULL.
* ===========================================================================
*/
static const char* netName(Net* net) {
return net ? net->name : "";
}
/* ===========================================================================
* Define hash table FileIntMap that hashes Sfile* to int.
* ===========================================================================
*/
defineHash1(static, FileIntMap, Sfile*, int, Find)
/* ===========================================================================
* dumpAttr - Dump attributes.
* ===========================================================================
*/
static void dumpAttr(FILE* fp, char** attrList) {
int i;
/**
* Loop over all attributes and print each attribute to the output file.
*/
for (i = 0; i < LISTLEN(attrList); i++) {
fprintf(fp, " %s", attrList[i]);
}
}
/* ===========================================================================
* dumpFlags - Dump flags.
* ===========================================================================
*/
static void dumpFlags(
FILE* fp,
enum zFlagObjType tidx,
unsigned short flags,
unsigned short mflags,
char orient,
char value
) {
int cnt;
const char* strs[64];
int i;
char bar = ' ';
/**
* If an orientation is set then print the orientation to the output file.
*/
if (orient) {
fprintf(fp, " Orient:%d", orient);
}
/**
* If this net has a value then print the value to the output file.
*/
if (value) {
char val = 0;
switch ((enum zNetValue)value) {
case NetVUndef: break;
case NetV0: val = '0'; break;
case NetV1: val = '1'; break;
case NetVX: val = 'X'; break;
case NetVZ: val = 'Z'; break;
case NetVNeg: val = 'N'; break;
case NetBusVSet: val = '*'; break;
}
if (val) {
fprintf(fp, " Value:%c", val);
}
}
/**
* Loop over all flags and if the flag is set then print the flag name.
*/
cnt = zFlag2Str(tidx, flags, 0, mflags, 0, strs, COUNT_OF(strs));
for (i=0; i < cnt; i++) {
fprintf(fp, "%c%s", bar, strs[i]);
bar = '|';
}
}
/* ===========================================================================
* dumpSpos - Dump spos info.
* ===========================================================================
*/
static void dumpSpos(DB* db, FILE* fp, Spos* spos,
const struct FileIntMap* fnameMap)
{
Spos** sposList = NULL;
int i, cnt;
/**
* Return if the fnameMap is not defined.
*/
if (!fnameMap) {
return;
}
/**
* Create a list of all source positions.
*/
zArrayAlloc(&sposList, 50);
zArrayLength(sposList) = 0;
while(spos) {
Sfile* sfile = db->sfileList[spos->sfileIdx];
if (!sfile) {
continue;
}
zArrayAppendPtr(&sposList, spos);
spos = zSposNEXT(spos);
}
/**
* Loop over the list of source positions and print them to the output file.
*/
cnt = LISTLEN(sposList);
for(i=0; i<cnt; i++) {
Spos *srcPos = sposList[i];
Sfile *sfile = db->sfileList[srcPos->sfileIdx];
zFPos begPos = srcPos->begPos;
zFPos endPos = srcPos->begPos + srcPos->length;
zFPos dummy;
unsigned lineNo;
int idx = -1;
if (!sfile) {
continue;
}
FileIntMapFind(fnameMap, sfile, &idx);
if(!zSposSearchLine(db, sfile, begPos, &lineNo, &dummy, &dummy)) {
lineNo = 0;
}
fprintf(fp, " (%d %u %lld %lld)",
idx, lineNo,
(long long)begPos, (long long)(endPos-begPos));
}
zArrayFree(sposList);
}
/* ===========================================================================
* dir2str - Dump port direction.
* ===========================================================================
*/
static const char* dir2str(PortFlags flags) {
switch(flags & PortFInOut) {
case PortFIn : return "INPUT";
case PortFOut : return "OUTPUT";
case PortFInOut : return "BIDIR";
case PortFUnknown : return "UNKNOWN";
default : zAssert(0);
}
return "?";
}
/* ===========================================================================
* dumpPortBus - dump portBus with sub-ports
* ===========================================================================
*/
static void dumpPortBus(DB* db, FILE* fp, Cell* cell, PortBus* pbus,
const struct FileIntMap* fnameMap) {
int n;
/**
* First print the portBus name, direction and width.
*/
fprintf(fp,"B %s %s width=%d",pbus->name,dir2str(pbus->flags),pbus->width);
/**
* Now loop over all members and dump them.
*/
for(n = 0; n < pbus->width; n++) {
Port* sub = cell->portList + (pbus->first+n);
const char* nname = netName(sub->net);
fprintf(fp, " %s=%s", sub->name, nname );
}
/**
* Dump attributes, flags and spos information to the output file.
*/
dumpAttr(fp, pbus->attrList);
dumpFlags(fp, FlagObjPort, pbus->flags, 0, 0, 0);
dumpSpos(db, fp, pbus->spos, fnameMap);
fprintf(fp, "\n");
}
/* ===========================================================================
* dumpPorts - dump ports and port bundles
* ===========================================================================
*/
static void dumpPorts(DB* db, FILE* fp, Cell* cell,
const struct FileIntMap* fnameMap) {
int i, cnt;
/**
* Loop over all ports of the given cell.
*/
cnt = LISTLEN(cell->portList);
for(i = 0; i < cnt; i++) {
Port* port = cell->portList+i;
/**
* If this port is not a member of a bus then dump the port.
*/
if (port->busMember == -1) {
const char *pname, *nname;
pname = port->name;
nname = netName(port->net);
fprintf(fp, "P %s=%s %s", pname, nname, dir2str(port->flags));
/**
* Dump attributes, flags and spos information to the output file.
*/
dumpAttr(fp, port->attrList);
dumpFlags(fp, FlagObjPort, port->flags, 0, 0, 0);
dumpSpos(db, fp, port->spos, fnameMap);
fprintf(fp, "\n");
/**
* If this port is member of a bus then dump the portBus and skip all
* members.
*/
} else {
PortBus* pbus = cell->portBusList+port->busMember;
dumpPortBus(db, fp, cell, pbus, fnameMap);
i += pbus->width-1;
}
}
}
/* ===========================================================================
* dumpPrim - Dump one primitive.
* ===========================================================================
*/
static void dumpPrim(DB* db, FILE* fp, Cell* cell,
const struct FileIntMap* fnameMap)
{
fprintf(fp, "PRIMITIVE %s", cell->name);
fprintf(fp, " %s", zPrintPrimitiveFunc(cell->func, cell->flags));
/**
* Dump attributes, flags and spos information to the output file.
*/
dumpAttr(fp, cell->attrList);
dumpFlags(fp, FlagObjCell, cell->flags, 0, 0, 0);
dumpSpos(db, fp, cell->spos,fnameMap);
fprintf(fp, "\n");
/**
* Dump the interface of the primitive.
*/
dumpPorts(db, fp, cell, fnameMap);
}
/* ===========================================================================
* dumpMod - Dump one module including the contents.
* ===========================================================================
*/
static void dumpMod(DB* db, FILE* fp, Module* mod,
const struct FileIntMap* fnameMap)
{
int i, cnt;
Cell* cell = &mod->c;
int* sortedPins = NULL;
NetBus** netBusList = NULL;
fprintf(fp, "MODULE %s", cell->name);
fprintf(fp, " %s", zPrintPrimitiveFunc(cell->func, cell->flags));
/**
* Dump attributes, flags and spos information to the output file.
*/
dumpAttr(fp, cell->attrList);
dumpFlags(fp, FlagObjMod, cell->flags, mod->mflags, 0, 0);
dumpSpos(db, fp, cell->spos, fnameMap);
fprintf(fp, "\n");
/**
* Dump the interface of the module.
*/
dumpPorts(db, fp, cell, fnameMap);
/**
* Dump all instances in this module.
*/
cnt = LISTLEN(mod->instList);
for(i = 0; i < cnt; i++) {
Inst* inst = mod->instList[i];
Cell* ref;
if (!inst) {
continue;
}
ref = inst->cellRef;
fprintf(fp,"I %s %s",inst->name,cell->name);
/**
* Dump attributes, flags and spos information to the output file.
*/
dumpAttr(fp, inst->attrList);
dumpFlags(fp, FlagObjInst, inst->flags, 0, inst->orient, 0);
dumpSpos(db, fp, inst->spos, fnameMap);
fprintf(fp, "\n");
/**
* Loop over all instance pins and dump them.
*/
if (PINCOUNT(inst) > 0) {
int j;
int jcnt = PINCOUNT(inst);
if (sortedPins) {
zArrayLength(sortedPins) = 0;
}
for(j=0; j<jcnt; j++) {
zArrayAppend(&sortedPins, &j);
}
for(j=0; j<jcnt; j++) {
int p = sortedPins[j];
Port* port = ref->portList+p;
Pin* pin = inst->pin+p;
if (pin->net == NULL) {
continue;
}
fprintf(fp, " p %s=%s", port->name, netName(pin->net));
/**
* Dump attributes, flags and spos information to the output
* file.
*/
dumpAttr(fp, pin->attrList);
dumpFlags(fp, FlagObjPin, pin->flags, 0, 0, 0);
dumpSpos(db, fp, pin->spos, fnameMap);
fprintf(fp, "\n");
}
}
}
zArrayFree(sortedPins);
/**
* Dump all nets in this module.
*/
cnt = LISTLEN(mod->netList);
for (i=0; i < cnt; i++) {
Net* net = mod->netList[i];
fprintf(fp, "N %s", netName(net));
/**
* Dump attributes, flags and spos information to the output file.
*/
dumpAttr(fp, net->attrList);
dumpFlags(fp, FlagObjNet, net->flags, 0, 0, net->value);
dumpSpos(db, fp, net->spos, fnameMap);
fprintf(fp, "\n");
}
/**
* Dump all netBuses in this module.
*/
cnt = LISTLEN(mod->netBusList);
zArrayAlloc(&netBusList, cnt);
for (i=0; i<cnt; i++) {
zArrayAppendPtr(&netBusList, mod->netBusList[i]);
}
for (i=0; i<cnt; i++) {
NetBus* nbus = netBusList[i];
int j, w;
w = LISTLEN(nbus->subnetList);
fprintf(fp, "L %s %d", nbus->name, w);
for(j=0; j<w; j++) {
Net* sub = nbus->subnetList[j];
fprintf(fp, " %s", sub ? netName(sub) : "{}");
}
/**
* Dump attributes, flags and spos information to the output file.
*/
dumpAttr(fp, nbus->attrList);
dumpFlags(fp, FlagObjNet, nbus->flags, 0, 0, nbus->value);
dumpSpos(db, fp, nbus->spos, fnameMap);
fprintf(fp, "\n");
}
zArrayFree(netBusList);
}
/* ===========================================================================
* zDump - Dump the contents of the zdb to an ASCII file given as fname.
* Return zFalse and set zLastErrMsg if something went wrong.
* ===========================================================================
*/
zBool zDump(DB* db, const char* fname) {
FILE* fp;
int i, cnt;
HashIter it;
Cell** primList;
Module** modList;
Module** parasiticList;
struct FileIntMap fnameMap;
/**
* Open the output file name. If the name is '-' then use stdout.
*/
if (zStringIsEqual(fname, "-")) {
fp = stdout;
} else {
fp = fopen(fname, "wb");
}
if (!fp) {
zPrintError("%.200s: %.200s", fname, strerror(errno));
return zFalse;
}
/**
* Fill a hash table and assign each sfile an index.
*/
FileIntMapInit(&fnameMap, zSposSfileHash, zSposSfileIsEqual);
cnt = LISTLEN(db->sfileList);
for(i = 0; i < cnt; i++) {
Sfile* sfile = db->sfileList[i];
if(sfile) {
FileIntMapInsert(&fnameMap, sfile, &i);
}
}
/**
* Collect a list of primitives and modules.
*/
primList = NULL;
modList = NULL;
cnt = LISTLEN(db->cellList);
for(i = 0; i < cnt; i++) {
Cell* cell = db->cellList[i];
/**
* If the cell has the CellFModule flag set then it is a hierarchical
* module and can be cast into a Module* type.
* Otherwise it is a primitive.
*/
if (cell->flags & CellFModule) {
zArrayAppendPtr(&modList, (Module*)cell);
} else {
zArrayAppendPtr(&primList, cell);
}
}
/**
* Dump all primitives.
*/
cnt = LISTLEN(primList);
for(i = 0; i < cnt; i++) {
dumpPrim(db, fp, primList[i], &fnameMap);
}
zArrayFree(primList);
/**
* Dump all modules.
*/
cnt = LISTLEN(modList);
for(i = 0; i < cnt; i++) {
dumpMod(db, fp, modList[i], &fnameMap);
}
zArrayFree(modList);
/**
* Collect a list of parasitics.
*/
parasiticList = NULL;
for (zIterInit(&it, &db->parasiticTable); zIterMore(&it); zIterNext(&it)) {
Module* mod = NULL;
zIterValue(&it, &mod);
zArrayAppendPtr(¶siticList, mod);
}
/**
* Dump all parasitics (modules).
*/
cnt = LISTLEN(parasiticList);
for(i = 0; i < cnt; i++) {
dumpMod(db, fp, parasiticList[i], &fnameMap);
}
zArrayFree(parasiticList);
/**
* Dump spos file names
*/
cnt = LISTLEN(db->sfileList);
for(i = 0; i < cnt; i++) {
Sfile* sfile = db->sfileList[i];
char buf[PATH_MAX];
int idx = -1;
const char* slash;
const char *srcFname;
if (!sfile) {
continue;
}
if (!zSposGetFilename(db, sfile, buf, PATH_MAX)) {
zAssert(0); /* LCOV_EXCL_LINE */
continue;
}
srcFname = buf;
slash = zStrrchr(srcFname, '/');
if(slash) {
srcFname = slash + 1;
}
slash = zStrrchr(srcFname, '\\');
if(slash) {
srcFname = slash + 1;
}
FileIntMapFind(&fnameMap, sfile, &idx);
fprintf(fp, "FILE %d:%s\n", idx, srcFname);
}
fclose(fp);
FileIntMapFree(&fnameMap);
return zTrue;
}
/* ===========================================================================
* main - The main procedure of this program. Handle command line arguments,
* open the binfile and call the zDump function.
* ===========================================================================
*/
int main(int argc, char* argv[]) {
DB* db = NULL;
/**
* Expect two arguments to the binary, the binfile and the output file name.
*/
if (argc != 3) {
fprintf(stderr, "Usage: %s Binfile DumpFile|-\n", argv[0]);
return 1;
}
/**
* Open the binfile readonly.
*/
db = zOpenDataBase(argv[1]);
if (!db) {
fprintf(stderr, "%s\n", zGetError());
return 1;
}
/**
* Call the zDump function.
*/
if (!zDump(db, argv[2])) {
fprintf(stderr, "%s\n", zGetError());
return 1;
}
/**
* Close the database.
*/
if (!zCloseDataBase(db)) {
fprintf(stderr, "%s\n", zGetError());
return 1;
}
return 0;
}
|