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 | /**
* Copyright (c) 2008-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
* Create Flat Binary Database
* @section
* C Level Examples
* @description
* Create and fill flat binary database using the C-Level API.
* @files
* zdb/createFlat.c
* @tag
* zdb
*/
#include <stdio.h>
#include "zos/ztypes.h"
#include "zos/zos.h"
#include "zos/zerror.h"
#include "zdb/zdb.h"
#include "zdb/zdbmem.h"
#include "zdb/zflat.h"
#include "zdb/zvalidate.h"
/**
* This is an error handling function. Print the given msg to stderr and
* return zFalse
*/
static zBool errorMsg(const char* msg) {
fprintf(stderr, "%s\n", msg);
return zFalse;
}
/**
* newPortBusMember - Create a port bus member by name.
*/
static zBool newPortBusMember(DB* zdb,
Cell* cell,
const char* pbusName,
const char* portName,
PortFlags dir)
{
int pbusNo;
int portNo;
/**
* Use an existing portBus or create a new one.
*/
pbusNo = zSearchPortBus(zdb, cell, pbusName, zFalse, zTrue);
if (pbusNo < 0) {
pbusNo = zNewPortBus(zdb, cell, pbusName, dir);
}
/**
* Create new port and add it to portbus.
*/
portNo = zNewPort(zdb, cell, portName, dir);
if (portNo < 0) {
return zFalse;
}
if (!zNewPortBusMember(zdb, cell, pbusNo, portNo)) {
return zFalse;
}
return zTrue;
}
/**
* connectNet - Create (or use an existing) net and connect it to a pin or port.
*/
static zBool connectNet(DB* zdb,
Module* mod,
Inst* inst,
const char* portName,
const char* netName)
{
int portNo;
Net* net;
/**
* Use an existing net or create a new one.
*/
net = zSearchNet(zdb, mod, netName, zFalse, zTrue);
if (!net) {
net = zNewNet(zdb, mod, netName);
}
/**
* Connect either a pin or port to this net.
*/
if (inst) {
portNo = zSearchPort(zdb, inst->cellRef, portName, zFalse, zTrue);
if (portNo < 0) {
return zFalse;
}
if (!zNewPinRef(zdb, net, inst,portNo)) {
return zFalse;
}
} else {
portNo = zSearchPort(zdb, &mod->c, portName, zFalse, zTrue);
if (portNo < 0) {
return zFalse;
}
if (!zNewPortRef(zdb, net, &mod->c, portNo)) {
return zFalse;
}
}
return zTrue;
}
/**
* fillDataBase - fill zdb with hierarchical example.
* returns top module or NULL.
*/
static Module* fillDataBase(DB* zdb) {
Primitive* prim = NULL;
Module* top = NULL;
Module* pre1 = NULL;
Module* pre2 = NULL;
Module* sub = NULL;
Module* low = NULL;
Inst* inst = NULL;
/**
* Create a new primitive, it's ports and nets.
*/
prim = zNewPrimitive(zdb, "prim", PrimFUNKNOWN);
if (!prim) {
goto error;
}
if (zNewPort(zdb, &prim->c, "A", PortFOut) < 0) {
goto error;
}
if (!newPortBusMember(zdb, &prim->c, "B", "B(1)", PortFOut)) {
goto error;
}
if (!zNewPort(zdb, &prim->c, "C", PortFOut)) {
goto error;
}
if (!zNewPort(zdb, &prim->c, "D", PortFOut)) {
goto error;
}
/**
* Create a low-module, it's ports and nets.
*/
low = zNewModule(zdb, "LOW");
if (!low) {
goto error;
}
if (zNewPort(zdb, &low->c, "A", PortFOut) < 0) {
goto error;
}
if (!newPortBusMember(zdb, &low->c, "B", "B(1)", PortFOut)) {
goto error;
}
if (!connectNet(zdb, low, NULL, "A", "A")) {
goto error;
}
if (!connectNet(zdb, low, NULL, "B(1)", "B(1)")) {
goto error;
}
/**
* Create primitive instance.
*/
inst = zNewInst(zdb, low, "p1", &prim->c);
if (!connectNet(zdb, low, inst, "A", "A")) {
goto error;
}
if (!connectNet(zdb, low, inst, "B(1)", "B(1)")) {
goto error;
}
if (!connectNet(zdb, low, inst, "C", "n1")) {
goto error;
}
if (!connectNet(zdb, low, inst, "D", "n1")) {
goto error;
}
/**
* Create a sub-module, it's ports and nets.
*/
sub = zNewModule(zdb, "SUB");
if (!sub) {
goto error;
}
if (zNewPort(zdb, &sub->c, "A", PortFOut) < 0) {
goto error;
}
if (!newPortBusMember(zdb, &sub->c, "B", "B(1)", PortFOut)) {
goto error;
}
if (!connectNet(zdb, sub, NULL, "A", "A")) {
goto error;
}
if (!connectNet(zdb, sub, NULL, "B(1)", "B(1)")) {
goto error;
}
/**
* Create low instance 1 and it's connectivity.
*/
inst = zNewInst(zdb, sub, "l1", &low->c);
if (!connectNet(zdb, sub, inst, "A", "A")) {
goto error;
}
if (!connectNet(zdb, sub, inst, "B(1)", "B(1)")) {
goto error;
}
/**
* Create low instance 2 and it's connectivity.
*/
inst = zNewInst(zdb, sub, "l2", &low->c);
if (!connectNet(zdb, sub, inst, "A", "A")) {
goto error;
}
if (!connectNet(zdb, sub, inst, "B(1)", "B(1)")) {
goto error;
}
/**
* Create a pre2-module, it's ports and nets.
*/
pre2 = zNewModule(zdb, "PRE2");
if (!pre2) {
goto error;
}
zNewInst(zdb, pre2, "s1", &sub->c);
/**
* Create a pre1-module, it's ports and nets.
*/
pre1 = zNewModule(zdb, "PRE1");
if (!pre1) {
goto error;
}
zNewInst(zdb, pre1, "pre2", &pre2->c);
/**
* Create top level module, it's ports and nets.
*/
top = zNewModule(zdb, "TOP");
if (!top) {
goto error;
}
if (!zTopModule(zdb, top)) {
goto error;
}
if (zNewPort(zdb, &top->c, "A", PortFOut) < 0) {
goto error;
}
if (!newPortBusMember(zdb, &top->c, "B", "B(1)", PortFOut)) {
goto error;
}
if (!connectNet(zdb, top, NULL, "A", "A")) {
goto error;
}
if (!connectNet(zdb, top, NULL, "B(1)", "B(1)")) {
goto error;
}
/**
* Create one sub instance, and connect pins to nets.
*/
zNewInst(zdb, top, "pre1", &pre1->c);
return top;
error:
errorMsg(zGetError());
return NULL;
}
/**
* addFAttr - add name/value pair to fattr array.
*/
static void addFAttr(FAttr* fattrs,
int* cnt,
const char* name,
const char* value)
{
fattrs[*cnt].name = name;
fattrs[*cnt].value = value;
fattrs[*cnt].nlen = zStrlen(name);
(*cnt)++;
}
/**
* fillFlat - fill flat data
*/
static zBool fillFlat(DB* zdb) {
Module* top;
struct FTopNode* topNode = NULL;
struct FNode* rootNode = NULL;
struct FNode* instNode = NULL;
struct FNode* node = NULL;
int cnt;
FAttr fattrs[5];
const char* l1Path[] = {"s1", "l1", NULL};
const char* l2Path[] = {"s1", "l2", NULL};
/**
* Create top module.
*/
top = zNewModule(zdb, "TOP");
if (!top) {
return zFalse;
}
if (!zTopModule(zdb, top)) {
return zFalse;
}
topNode = zFlatNewTopNode( zdb, top, 0);
rootNode = zFlatGetRootNode( zdb, topNode, 0);
/**
* Create path nodes to {inst TOP s1 l1}.
*/
instNode = zFlatMakeInstNodes(zdb, topNode, rootNode, l1Path);
/**
* Create net node {net TOP s1 l1 n1}.
*/
node = zFlatNewNetNode(zdb, topNode, instNode, "n1");
/**
* Set flags and Highlight color for net.
*/
zFlatSetFlags(node, FNodeFOrange);
zFlatSetHicol(node, 1, 0);
/**
* Set attributes at flat net.
*/
cnt = 0;
addFAttr(fattrs, &cnt, "attr1", "value1");
addFAttr(fattrs, &cnt, "attr2", "value2");
if (!zFlatNodeSetAttrs(zdb, topNode, node, fattrs, cnt)) {
errorMsg(zGetError());
return zFalse;
}
/**
* Create path nodes to {inst TOP s2 l1}.
*/
instNode = zFlatMakeInstNodes(zdb, topNode, rootNode, l2Path);
/**
* Create net node {net TOP s2 l1 n1}.
*/
node = zFlatNewNetNode(zdb, topNode, instNode, "n1");
/**
* Set flags and Highlight color for net>
*/
zFlatSetFlags(node, FNodeFCyan);
zFlatSetHicol(node, 2, 0);
/**
* Set attributes at flat object.
*/
cnt = 0;
addFAttr(fattrs, &cnt, "attr1", "value3");
addFAttr(fattrs, &cnt, "attr2", "value4");
if (!zFlatNodeSetAttrs(zdb, topNode, node, fattrs, cnt)) {
errorMsg(zGetError());
return zFalse;
}
/**
* Set Highlight color for pin node {pin TOP s2 l1 A}.
*/
node = zFlatNewPinNode(zdb, topNode, instNode, "A");
zFlatSetHicol(node, 3, 0);
/**
* Set Highlight color for pinBus node {pin TOP s2 l1 B}.
*/
node = zFlatNewPinBusNode(zdb, topNode, instNode, "B");
zFlatSetHicol(node, 4, 0);
/**
* Set Highlight color for port node {port TOP s2 l1 A}.
*/
node = zFlatNewPortNode(zdb, topNode, instNode, "A");
zFlatSetHicol(node, 5, 0);
/**
* Set Highlight color for portBus node {portBus TOP s2 l1 B}.
*/
node = zFlatNewPortBusNode(zdb, topNode, instNode, "B");
zFlatSetHicol(node, 6, 0);
return zTrue;
}
/**
* Main function.
*/
int main(int argc, char** argv) {
const char* binfile = NULL;
DB* zdb1 = NULL;
DB* zdb2 = NULL;
Module* top = NULL;
const char* prefixPath[] = {"TOP", "pre1", "pre2", NULL};
/**
* Need exactly one argument: the path to the binfile.
*/
if (argc != 2) {
fprintf(stderr, "Usage: %s <BINFILE>\n", argv[0]);
return 1;
}
binfile = argv[1];
/**
* Create a new database.
*/
zdb1 = zNewDataBase();
if (!zdb1) {
goto error;
}
/**
* Fill hierarchical database.
*/
top = fillDataBase(zdb1);
if (!top) {
goto error;
}
/**
* Create a new database.
*/
zdb2 = zNewDataBase();
if (!zdb2) {
goto error;
}
/**
* fill flat data.
*/
if (!fillFlat(zdb2)) {
goto error;
}
/**
* merge flat data into hierarchical database,
* using the prefix path to fit paths to db.
*/
if (!zFlatMerge(zdb2, zdb1, prefixPath, 3)) {
goto error;
}
/**
* Validate (incl. flatpedantic).
*/
if (!zValidateDB(zdb1, USE_BUILTIN_DEFAULT_FLAGS | VALIDATE_FLATPEDANTIC)) {
goto error;
}
/**
* Save to binfile to store the data.
*/
if (!zSaveDataBaseCompact(zdb1, binfile)) {
goto error;
}
/**
* Close the databases.
*/
if (!zCloseDataBase(zdb2)) {
goto error;
}
if (!zCloseDataBase(zdb1)) {
goto error;
}
return 0;
error:
errorMsg(zGetError());
return 1;
}
|