summaryrefslogtreecommitdiff
path: root/base/actel.c
blob: 0cc05de3e3bbc67141d9c4295624eae2928e9944 (plain)
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
/* "NETGEN", a netlist-specification tool for VLSI
   Copyright (C) 1989, 1990   Massimo A. Sivilotti
   Author's address: mass@csvax.cs.caltech.edu;
                     Caltech 256-80, Pasadena CA 91125.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation (any version).

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; see the file copying.  If not, write to
the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

/*  actel.c -- Output routines for ACTEL's .als format */

#include "config.h"

#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
#ifdef IBMPC
#include <stdlib.h>  /* for strtol on PC */
#endif

#ifdef TCL_NETGEN
#include <tcl.h>
#endif

#include "netgen.h"
#include "objlist.h"
#include "netfile.h"
#include "hash.h"
#include "print.h"
	
#define ACTELHASHSIZE 99
static long actelhashbase = 0xA00;
static struct hashdict actelnamedict;
static FILE *actelfile;

char *ActelName(char *Name);
int PrintActelName(struct hashlist *ptr)
{
	fprintf(actelfile,"%s == %s\n",ActelName(ptr->name),ptr->name);
	return(1);
}
	
void PrintActelNames(char *filename)
{
  if (filename == NULL) actelfile = stdout;
  else actelfile = fopen(filename,"w");
  RecurseHashTable(&actelnamedict, PrintActelName);
  if (actelfile != stdout) fclose(actelfile);
}

long ActelNameHash(char *name)
/* hashes name into namedict if necessary, then returns address of entry */
{
  struct hashlist *p;

  p = HashInstall(name, &actelnamedict);
  if (p == NULL) return(0);
  if (p->ptr != NULL) return ((long)(p->ptr));
  actelhashbase++;
  p->ptr = (void *)actelhashbase;
  return(actelhashbase);
}


#define ACTELNAMESIZE 3
static char	ActelNames[ACTELNAMESIZE][500] ;
static int	ActelIndex = 0;

char	*ActelName(char *Name)
/* returns a pointer to one of the elements in ActelNames,
   which is a copy of 'name', 
   quoted according to the ACTEL requirements
*/
{
  int	index, index2;
  int	NeedsQuoting;
  char    name[500];
  char    *nm;

  strcpy(name,Name);
  /* strip physical-pin information, if it exists */
  if ((nm = strrchr(name,PHYSICALPIN[0])) != NULL) *nm = '\0';
  if (strlen(name) > 13) {
    ActelIndex = (++ActelIndex) % ACTELNAMESIZE;
    /* format the value of the hashed value of the string */
    sprintf(ActelNames[ActelIndex], "$%lX", ActelNameHash(name));
if (Debug) 
Printf("ActelNameHash returns %s on name %s\n",ActelNames[ActelIndex], name);
    return(ActelNames[ActelIndex]);
  }

  NeedsQuoting = 0;
  if (NULL != strpbrk(name, ".,:; \t\"'\n\r")) NeedsQuoting = 1;

  ActelIndex = (++ActelIndex) % ACTELNAMESIZE;
  if (!NeedsQuoting) {
    strcpy(ActelNames[ActelIndex], name);
    return(ActelNames[ActelIndex]);
  }
  /* else, needs quoting */
  index2 = 0;
  ActelNames[ActelIndex][index2++] = '"';
  for (index = 0; index < strlen(name); index ++) {
    if (name[index] == '"')
      ActelNames[ActelIndex][index2++] = '"';
    ActelNames[ActelIndex][index2++] = name[index];
  }
  ActelNames[ActelIndex][index2++] = '"';
  ActelNames[ActelIndex][index2++] = '\0';
  return(ActelNames[ActelIndex]);
}

void ActelPins(char *name, int format)
/* print out a pins file containing all nodes that:
     1) connect to pad
     2) have names of the format <name>(<int>)

if format = 0, just dump the list of pins
if format = 1, use the actel .pin file format
*/
{
  struct nlist *tp;
  struct objlist *ob, *ob2;
  char *ptr;
  char physicalpin[200];

  tp = LookupCell(name);
  if (tp == NULL) return;
  if (tp->class != CLASS_SUBCKT) return;
  if (format == 1) FlushString("DEF %s.\n", ActelName(name));
  if (format == 0) FlushString("%20s  %3s  %s\n\n",
			       "Pad name","pin","Actel name");

  for (ob = tp->cell; ob != NULL; ob = ob->next) 
    if (IsPortInPortlist(ob,tp) &&
	strcasecmp(ob->name, "GND") &&
	strcasecmp(ob->name, "VDD") ) 
      /* scan entire list, 
	 looking for same nodenum, but physical port assignment */
      for (ob2 = tp->cell; ob2 != NULL; ob2 = ob2->next) {
	if (ob->node == ob2->node &&
	    (ptr = strrchr(ob2->name,PHYSICALPIN[0])) != NULL) {
	  ptr++; /* point to next char */
	  strcpy(physicalpin, ptr);
	  ptr = strchr(physicalpin, ENDPHYSICALPIN[0]);
	  if (ptr == NULL)
	    Printf("Bad Actel Pin specification: %s\n", ob2->name);
	  else {
	    *ptr = '\0';
	    if (format == 0) FlushString("%20s  %3s  %s\n", ob->name,
					 physicalpin,ActelName(ob->name));
	    if (format == 1) FlushString("NET %s; ; PIN:%s.\n",
					 ActelName(ob->name),physicalpin);
	    break;  /* out of for loop */
	  }
	}
      }

  if (format == 1) FlushString("END.\n");
}

void actelCell(char *name)
{
  struct nlist *tp, *tp2;
  struct objlist *ob;
  int maxnode;	 /* maximum node number assigned in cell */
  int nodenum;	 /* node number currently being dumped in NET */
  int nodedumped;/* flag set true when first element of net written */
  int netdumped; /* flag set true when NET stmt. written for given nodenum */
  int vddnode, gndnode;  /* flags set to true when processing power rail */

  tp = LookupCell(name);
  if (tp == NULL) {
    Printf ("No cell '%s' found.\n", name);
    return;
  }
  /* do NOT dump primitive cells */
  if (tp->class != CLASS_SUBCKT) 
    return;

  /* check to see that all children have been dumped */
  ob = tp->cell;
  while (ob != NULL) {
    tp2 = LookupCell(ob->model.class);
    if ((tp2 != NULL) && !(tp2->dumped)) 
      actelCell(tp2->name);
    ob = ob->next;
  }

  /* print out header list */
  FlushString("DEF %s", ActelName(tp->name));
  netdumped = 0;
  nodedumped = 0;
  for (ob = tp->cell; ob != NULL; ob = ob->next) {
    if (IsPortInPortlist(ob, tp) && 
	strcasecmp(ob->name, "GND")&&
	strcasecmp(ob->name, "VDD") ) {
      /* unique port */
      if (nodedumped) 
	FlushString (", ");
      else 
	nodedumped = 1;
      if (!netdumped) 
	FlushString("; ");
      /*			FlushString("%s", ActelName(ob->name)); */
      FlushString("%s", ActelName(NodeAlias(tp, ob)));
      netdumped = 1;
    }
  }
  FlushString(".\n");

#if 0
/*  I'm not sure whether this works.  It is based on a reading of 
  the .adl syntax manual, and has never been tested.
*/

  /* run through cell's contents, defining all global elements */
  ob = tp->cell;
  while (ob != NULL) {
    if ((ob->type == GLOBAL)
	&& match(ob->name, NodeAlias(tp, ob)))
      FlushString ("SIG %s; GLOBAL.\n", ActelName(ob->name));
    ob = ob->next;
  }
#endif

  /* now run through cell's contents, print instances */
  ob = tp->cell;
  while (ob != NULL) {
    if (ob->type == FIRSTPIN) { /* this is an instance */
      if ((LookupCell(ob->model.class))->class != CLASS_SUBCKT)
	FlushString ("USE ADLIB:%s; %s.\n",
		     ActelName(ob->model.class), ActelName(ob->instance.name));
      else
	FlushString ("USE %s; %s.\n",
		     ActelName(ob->model.class), ActelName(ob->instance.name));
    }
    ob = ob->next;
  }

  /* now print out nets */
  ob = tp->cell;
  /* find maximum nodenumber in use */
  maxnode = -1;
  while (ob != NULL) {
    if (ob->node >  maxnode) 
      maxnode = ob->node;
    ob = ob->next;
  }
#if 1
  for (nodenum = 1; nodenum <= maxnode; nodenum++) {
    nodedumped = 0;
    netdumped = 0;
    vddnode = 0;
    gndnode = 0;
    for (ob = tp->cell; ob != NULL; ob = ob->next) {
      if (ob->node == nodenum && 
	  (IsPortInPortlist(ob, tp) || ob->type >= FIRSTPIN)) {
	char *nm;

	nm = strchr(ob->name, SEPARATOR[0]);
	/* suppress pins that are global connections to power rails */
	/* i.e., all pins that are <instancename>/VDD */
	if (nm == NULL || (strcasecmp(nm+1,"VDD") && strcasecmp(nm+1,"GND"))) {
	  if (!netdumped)
	    FlushString("NET %s; ", ActelName(NodeAlias(tp, ob)));
	  netdumped = 1;
	  /* print out the element in list */
	  if (!(strcasecmp(ob->name, "GND"))) gndnode = 1;
	  else if (!(strcasecmp(ob->name, "VDD"))) vddnode = 1;
	  else {
	    if (nodedumped) 
	      FlushString(", ");
	    /* write it out if it is a REAL port or a PIN */
	    if (ob->type >= FIRSTPIN)
	      FlushString("%s:%s", ActelName(ob->instance.name),
	      /* was strchr below, but failed for FLATTENED objects 12/12/88 */
			  ActelName(strrchr(ob->name, SEPARATOR[0]) + 1));
	    else
	      FlushString("%s", ActelName(NodeAlias(tp, ob))); 
	    nodedumped = 1;
	  }
	}
      }
    }
    /* changed from "if (nodedumped)" to "if (netdumped)" 
       to correctly terminate power and ground nets that have no pins
       (i.e., they only connect to ports of cells that have their
       own (internal) connections to these global resources */
    if (netdumped) {
      if (gndnode) {
	if (nodedumped) FlushString("; ");
	FlushString("GLOBAL, POWER:GND");
      }
      if (vddnode) {
	if (nodedumped) FlushString("; ");
	FlushString("GLOBAL, POWER:VCC");
      }
      FlushString(".\n");
    }
  }
#else
  /* this code incorrectly wrote power and ground nets when these
     nodes were only connected to VDD and GND pins of instances
     (i.e., they were not connected to logic inputs) */
  for (nodenum = 1; nodenum <= maxnode; nodenum++) {
    nodedumped = 0;
    netdumped = 0;
    vddnode = 0;
    gndnode = 0;
    for (ob = tp->cell; ob != NULL; ob = ob->next) {
      if (ob->node == nodenum && 
	  (IsPortInPortlist(ob, tp) || ob->type >= FIRSTPIN)) {
	char *nm;

	nm = strchr(ob->name, SEPARATOR[0]);
	/* suppress pins that are global connections to power rails */
	if (nm == NULL || (strcasecmp(nm+1,"VDD") && strcasecmp(nm+1,"GND"))) {
	  if (!netdumped)
	    FlushString("NET %s; ", ActelName(NodeAlias(tp, ob)));
	  netdumped = 1;
	  /* print out the element in list */
	  if (!(strcasecmp(ob->name, "GND"))) gndnode = 1;
	  else if (!(strcasecmp(ob->name, "VDD"))) vddnode = 1;
	  else {
	    if (nodedumped) 
	      FlushString(", ");
	    /* write it out if it is a REAL port or a PIN */
	    if (ob->type >= FIRSTPIN)
	      FlushString("%s:%s", ActelName(ob->instance.name),
	      /* was strchr below, but failed for FLATTENED objects 12/12/88 */
			  ActelName(strrchr(ob->name, SEPARATOR[0]) + 1));
	    else
	      FlushString("%s", ActelName(NodeAlias(tp, ob))); 
	    nodedumped = 1;
	  }
	}
      }
    }
    if (nodedumped) {
      if (gndnode) 
	FlushString("; GLOBAL, POWER:GND");
      if (vddnode) 
	FlushString("; GLOBAL, POWER:VCC");
      FlushString(".\n");
    }
  }
#endif


#if 0
  /* old style; one pin per net statement.  Ok, but ugly (but doesn't
   handle VDD and GND pins correctly */
  ob = tp->cell;
  while (ob != NULL) {
    if (ob->type != NODE) {
      FlushString ("NET NET%d; ", ob->node);
      if (IsPort(ob)) 
	FlushString("%s.\n",
		    ActelName(ob->name));
      else if (ob->type >= FIRSTPIN) 
	FlushString("%s:%s.\n",
		    ActelName(ob->model.class), 
		    /* was strchr below 12/12/88 */
		    ActelName(strrchr(ob->name, SEPARATOR[0]) + 1));
    }
    ob = ob->next;
  }
#endif  

  FlushString ("END.\n\n");
  tp->dumped = 1;		/* set dumped flag */
}


void Actel(char *name, char *filename)
{
  char Path[500];
  char FileName[500];

  if (LookupCell(name) == NULL) {
    Printf("No such cell name: %s\n", name);
    return;
  }
  if (filename == NULL || strlen(filename) == 0)  
    strcpy(Path, name);
  else 
    strcpy(Path,filename);

  SetExtension(FileName, Path, ACTEL_EXTENSION);
  if (!OpenFile(FileName, 80)) {
    Printf("Failed to open file named: %s\n",FileName);
    perror("Actel(): Unable to open output file.");
    return;
  }
  ClearDumpedList();
  InitializeHashTable(&actelnamedict, ACTELHASHSIZE);
  if (LookupCell(name) != NULL) 
    actelCell(name);
  CloseFile(FileName);

  SetExtension(FileName, Path, ".pin");
  OpenFile(FileName, 80);
  ActelPins(name,1);
  CloseFile(FileName);

  SetExtension(FileName, Path, ".pads");
  OpenFile(FileName, 80);
  ActelPins(name,0);
  CloseFile(FileName);

  SetExtension(FileName, Path, ".crt");
  OpenFile(FileName, 80);
  FlushString("DEF %s.\n", ActelName(name));
  FlushString("END.\n");
  CloseFile(FileName);

  SetExtension(FileName, Path, ".nam");
  PrintActelNames(FileName);
}