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
/**
 *  Copyright (c) 2015-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.
 */

%{
#include <stdio.h>
/* OS includes */
#include "zos/zassert.h"
#include "zos/zmessage.h"
#include "zos/zosmem.h"
#include "zos/zos.h"
#include "zos/ztypes.h"
#include "zos/zerror.h"
/* ZDB includes */
#include "zdb/zdb.h"

extern int  ndllex();
extern unsigned Curline;
extern DB* zdb;
static Module* module;
static Module* duplModule;

static void ndlerror(const char* msg) {
    printf("%d %s\n", Curline, msg);
}

static PortFlags portFlags;


static int portNameCmp(const void* objP1, const void* objP2) {
    Port* p1 = (Port*)objP1;
    Port* p2 = (Port*)objP2;
    return zStrcmpAlphaNum(p1->name, p2->name);
}


/**
 * For all instances of oldCell create a new instance of newCell.
 */
static zBool reInstance(DB* db, Cell* oldCell, Cell* newCell) {
    int m, i, p;
    int cellCount = LISTLEN(db->cellList);

    /**
     * If the new cell has less ports than the old cell then
     * reInstance() cannot work.
     */
    if (LISTLEN(newCell->portList) < LISTLEN(oldCell->portList)) {
        zPrintError("Too few ports in %.128s", newCell->name);
        return zFalse;
    }

    /* Loop over all cells */
    for (m = 0; m < cellCount; m++) {
        Module* module;
        Cell*   cell = db->cellList[m];
        int     instCount;

        /* Skip primitive cells*/
        if (zCellIsPrimitive(cell)) continue;

        module    = (Module*)cell;
        instCount = LISTLEN(module->instList);

        /* Loop over all instances in this module */
        for (i = 0; i < instCount; i++) {
            Inst* inst = module->instList[i];
            Inst* newInst;

            /* Skip all instances that do not reference the given cell */
            if (inst->cellRef != oldCell) continue;

            /* Create a new instance of the new cell */
            newInst  = zNewInst(db, module, inst->name, newCell);

            /**
             * Loop over all pins of the old instance.
             * Search the pinName at the new cell and connect
             * the net also the new instance.
             */
            for (p = 0; p < PINCOUNT(inst); p++) {
                Pin* pin = inst->pin+p;
                Net* net = pin->net;
                const char* pname = oldCell->portList[p].name;
                int         pinNo = zSearchPort(db,newCell,pname,zTrue,zFalse);
                if (!net) continue;
                if (pinNo == -1) {
                    newInst->flags |= InstFZombie;
                    newCell->flags |= CellFZombie;
                    zDeleteZombies(db);
                    return zFalse;
                }
                zNewPinRef(db, net, newInst, pinNo);
            }
            inst->flags |= InstFZombie;
        }
    }
    oldCell->flags |= CellFZombie;
    zDeleteZombies(db);
    return zTrue;
}


/**
 * Create port buses of ports at the given cell. The bus delimiter is '-'.
 */
static void createPortBus(DB* zdb, Cell* cell) {
    int i, len;

    zAssert(cell);
    zAssert(cell->refCount == 0);

    len = LISTLEN(cell->portList);
    for (i=0; i < len; i++) {
        zBool ok = zTableRemoveObj(zdb, &cell->portTable,
                                 cell->portList[i].name, &i);
        if (!ok) printf("zTableRemoveObj ERR\n");
    }
    zListSort(cell->portList, portNameCmp);
    for (i=0; i < len; i++) {
        Net* net;
        zTableInsert(zdb, &cell->portTable, cell->portList[i].name, &i);
        net = zNewNet(zdb, (Module*)cell, cell->portList[i].name);
        zNewPortRef(zdb, net, cell, i);
    }

    for (i=0; i < len; i++) {
        char* name  = cell->portList[i].name;
        char* delim = zStrrchr(name, '-');
        int portBusNo;
        zBool ok;

        if (!delim) continue;

        *delim = '\0';

        portBusNo = zSearchPortBus(zdb, cell, name, zTrue, zFalse);

        if (portBusNo == -1) {
            portBusNo = zNewPortBus(zdb, cell, name, cell->portList[i].flags);
        }

        *delim = '-';

        ok = zNewPortBusMember(zdb, cell, portBusNo, i);
        if (!ok) printf("ERR: %s\n", zGetError());
    }
}

static void createNetBus(DB* zdb, Module* module) {
    int i, len;
    int busIndex = -1;

    len = LISTLEN(module->netList);

    for (i=0; i < len; i++) {
        Net*    net = module->netList[i];
        NetBus* netBus;
        char*   name  = net->name;
        char*   delim = zStrrchr(name, '-');
        zBool   ok;

        if (!delim) continue;

        *delim = '\0';

        netBus = zSearchNetBus(zdb, module, name, zTrue, zFalse);
        if (!netBus) netBus = zNewNetBus(zdb, module, name, 1);

        if (busIndex < 0) {
            int j;
            for (j = 0; j < LISTLEN(module->netBusList); j++) {
                if ((module->netBusList[j]) == netBus) {
                    busIndex = j;
                    break;
                }
            }
            zAssert(busIndex >= 0);
        }

        *delim = '-';
        ok = zNewNetBusMemberByIndex(zdb, module, busIndex, net);
        if (!ok) {
            printf("ERR: %s\n", zGetError());
        }
    }
}


static void createPortIfNeeded(DB* zdb, Cell* cell, const char* name,
                               PortFlags dir) {
    int portNo = zSearchPort(zdb, cell, name, zTrue, zFalse);

    if (portNo < 0) {
        portNo = zNewPort(zdb, cell, name, dir);
    } else {
        cell->portList[portNo].flags |= dir;
    }
}

static Cell* createCell(DB* zdb, const char* name, const char** outPorts,
                        const char** inPorts) {
    Cell* cell;
    int i, len;

    /**
     * Create a new cell.
     */
    cell = (Cell*)zNewModule(zdb, name);

    /**
     * Create output ports.
     */
    len = ARRAYLEN(outPorts);
    for (i = 1; i < len; i+=2) zNewPort(zdb, cell, outPorts[i], PortFOut);

    /**
     * Create input ports.
     */
    len = ARRAYLEN(inPorts);
    for (i = 1; i < len; i+=2) zNewPort(zdb, cell, inPorts[i], PortFIn);

    createPortBus(zdb, cell);
    return cell;
}
static Cell* createCellIfNeeded(DB* zdb, const char* name,
                                const char** outPorts, const char** inPorts) {
    Cell* cell;
    Cell* newCell;
    cell = zSearchCell(zdb, name, zTrue, zFalse);
    newCell = createCell(zdb, name, outPorts, inPorts);
    if (cell) {
        if (zIdenticalInterface(cell,newCell,zTrue)) zDeleteCell(zdb, newCell);
        else {
            char* name = zUniqueNameAlloc(
                zdb, &zdb->cellTable, newCell->name, zFalse);
            zMessage_print(
                ZMESSAGE_WARNING, "Creating unique name for %s", newCell->name);
            zRenameCell(zdb, newCell, name ? name : newCell->name);
            zFree(name);
            cell = newCell;
        }
    } else {
        zMessage_print(ZMESSAGE_WARNING, "Creating blackbox cell %s", name);
        cell = newCell;
    }
    return cell;
}

/**
 * Loop over the given conList of this cell and create the connectivity.
 */
static void connectPins(DB* zdb, Cell* cell, Module* module, Inst* inst,
                        const char** conList) {
    int i, len = ARRAYLEN(conList);
    for (i = 0; i < len; i+=2) {
        Net* net;
        int pinNo = zSearchPort(zdb, cell, conList[i+1], zTrue, zFalse);
        zAssert(pinNo != -1);
        net = zSearchNet(zdb, module, conList[i], zTrue, zFalse);
        if (!net) net = zNewNet(zdb, module, conList[i]);
        zFree((char*)conList[i]);
        zFree((char*)conList[i+1]);
        zNewPinRef(zdb, net, inst, pinNo);
    }
}

%}

%union {
        const char*             tok;
        const char**            sList;
}

%token  <tok>   T_COMPILE
%token  <tok>   T_DESCRIPTION
%token  <tok>   T_MODULE
%token  <tok>   T_OUTPUT
%token  <tok>   T_INPUT
%token  <tok>   T_INOUT
%token  <tok>   T_DEFINE
%token  <tok>   T_END
%token  <tok>   T_IDENTIFIER
%token  <tok>   T_UNKNOWN
%type   <tok>   identifier
%type   <sList> list_of_con
%right          '='
%token          '(' ')'
%left           ';'
%left           ','

%%

compile:
          T_COMPILE ';' list_of_module T_END T_COMPILE ';' T_END ';'
        | list_of_module
 ;

list_of_module:
          /* empty */
        | list_of_module module_block
 ;

module_block:
          module
          module_header
          define
          list_of_instances
          T_END T_MODULE { createNetBus(zdb, module); } ';'
 ;

module:
          T_MODULE T_IDENTIFIER {
              duplModule = (Module*)zSearchCell(zdb, $2, zTrue, zFalse);
              module = zNewModule(zdb, $2);
          } ';'
 ;

module_header:
        /* empty */
        | module_header description
        | module_header inputs
        | module_header outputs
        | module_header bidirect
 ;

description:
          T_DESCRIPTION T_IDENTIFIER '(' T_IDENTIFIER ')'  ';'
        | T_DESCRIPTION ';'
        | T_DESCRIPTION error ';' { yyerrok; }
 ;

inputs:
          T_INPUT { portFlags = PortFIn; } list_of_names ';'
        | T_INPUT error ';' { yyerrok; }
 ;

outputs:
          T_OUTPUT { portFlags = PortFOut; } list_of_names ';'
        | T_OUTPUT error ';' { yyerrok; }
 ;

bidirect:
          T_INOUT { portFlags = PortFInOut; } list_of_names ';'
        | T_INOUT error ';' { yyerrok; }
 ;

list_of_names:
          T_IDENTIFIER { createPortIfNeeded(zdb, (Cell*)module,$1,portFlags); }
        | list_of_names ','
          T_IDENTIFIER { createPortIfNeeded(zdb, (Cell*)module,$3,portFlags); }
        | /* empty */
 ;

define:
          T_DEFINE {
              createPortBus(zdb, (Cell*)module);
              if (duplModule) {
                  if (zIdenticalInterface(&module->c, &duplModule->c, zTrue)) {
                      zDeleteCell(zdb, &module->c);
                      module = duplModule;
                  } else {
                      if (!reInstance(zdb, (Cell*)duplModule, (Cell*)module)) {
                          char* name = zUniqueNameAlloc(
                            zdb, &zdb->cellTable, duplModule->c.name, zFalse);
                          zMessage_print(
                              ZMESSAGE_WARNING,
                              "%s: Creating duplicate module %s",
                              zGetError(),
                              name ? name : duplModule->c.name);
                          zRenameCell(zdb, &duplModule->c,
                              name ? name : duplModule->c.name);
                          zFree(name);
                      }
                  }
              }
          }
 ;

list_of_instances:
          instance
        | list_of_instances instance
 ;

instance:
          identifier '(' list_of_con ')' '=' identifier '(' list_of_con ')' ';'
        {
          Inst* inst;
          Cell* cell;

          cell = createCellIfNeeded(zdb, $6, $3, $8);
          inst = zNewInst(zdb, module, $1, cell);

          connectPins(zdb, cell, module, inst, $3);
          connectPins(zdb, cell, module, inst, $8);

          zFree((char*)$1);
          zFree((char*)$6);
          zArrayFree($3);
          zArrayFree($8);
        }
        | identifier '=' '(' identifier ')' ';' {
            Cell* buf;
            Net *net1, *net2;
            Inst* inst;
            const char* iName = $4;
            char* newName = NULL;
            const char* bufName = "__internal_buf__";
            buf = zSearchCell(zdb, bufName, zTrue, zFalse);
            if (!buf) {
                buf = (Cell*)zNewPrimitive(zdb, bufName, PrimFBUF);
                zNewPort(zdb, buf, "O", PortFOut);
                zNewPort(zdb, buf, "I", PortFIn);
            }
            inst = zSearchInst(zdb, module, iName, zTrue, zFalse);
            if (inst) {
                newName = zUniqueNameAlloc(
                    zdb, &module->instTable, iName, zFalse);
            }
            inst = zNewInst(zdb, module, newName ? newName : iName, buf);
            zFree(newName);
            net1 = zSearchNet(zdb, module, $1, zTrue, zFalse);
            net2 = zSearchNet(zdb, module, $4, zTrue, zFalse);
            if (!net1) net1 = zNewNet(zdb, module, $1);
            if (!net2) net2 = zNewNet(zdb, module, $4);
            zNewPinRef(zdb, net1, inst, 0);
            zNewPinRef(zdb, net2, inst, 1);
            zFree((char*)$1);
            zFree((char*)$4);
        }
        | error ';' { yyerrok; }

 ;

list_of_con:
          identifier '=' identifier { $$ = NULL;
                                      zArrayAppendPtr(&$$, $1);
                                      zArrayAppendPtr(&$$, $3);
                                    }
        | list_of_con ',' identifier '=' identifier { $$ = $1;
                                                      zArrayAppendPtr(&$$, $3);
                                                      zArrayAppendPtr(&$$, $5);
                                                    }
        | /* empty */ {$$ = NULL;}
 ;

identifier:
          T_IDENTIFIER { $$ = zStrdup($1); }
 ;