File: schema.c

package info (click to toggle)
xcircuit 2.0a6-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,960 kB
  • ctags: 3,202
  • sloc: ansic: 30,529; makefile: 51; sh: 2
file content (417 lines) | stat: -rw-r--r-- 12,541 bytes parent folder | download
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
/*-------------------------------------------------------------------------*/
/* schema.c --- xcircuit routines specific to the schematic capture system */
/* Copyright (c) 1998  Tim Edwards, Johns Hopkins University        	   */
/*-------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------*/
/*      written by Tim Edwards, 10/13/97    				   */
/*-------------------------------------------------------------------------*/

#include <stdio.h>
#include <string.h>
#include <malloc.h>

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>

/*-------------------------------------------------------------------------*/
/* Local includes							   */
/*-------------------------------------------------------------------------*/

#include "cursors.h"
#include "colordefs.h"
#include "xcircuit.h"

/*------------------------------------------------------------------------*/
/* External Variable definitions                                          */
/*------------------------------------------------------------------------*/

extern Clientdata areastruct;
extern short eventmode;

/*-------------------------------------------------------------------------*/
#ifdef SCHEMA

/*--------------------------------------------------------*/
/* Menu calls (procedure wrappers)			  */
/*--------------------------------------------------------*/

XtCallbackProc callgennet(Widget w, unsigned int mode, caddr_t calldata)
{
   if (mode == 0) {
      gennet("spice", "spc");
   }
   else if (mode == 1) {
      gennet("sim", "sim");
   }
}

/*--------------------------------------------------------*/
/* Change a pinlabel's type to GLOBAL			  */
/*--------------------------------------------------------*/

XtCallbackProc dopinglobal(Widget w, unsigned int mode, caddr_t calldata)
{
   short *gsel;
   labelptr glab;

   if (areastruct.selects == 0) {
      Wprintf("Must first select a label to make global");
      return;
   }

   for (gsel = areastruct.selectlist; gsel < areastruct.selectlist +
		areastruct.selects; gsel++) {
      if (SELECTTYPE(gsel) == LABEL) {
	 glab = SELTOLABEL(gsel);
	 if (glab->pin == GLOBAL) {
	    glab->pin = LOCAL;
	    Wprintf("Changed pin to type LOCAL");
	 }
	 else if (glab->pin == LOCAL) {
	    glab->pin = GLOBAL;
	    Wprintf("Changed pin to type GLOBAL");
	 }
      }
   }
}

/*--------------------------------------------------------*/
/* Change an object's type to Fundamental or back again	  */
/*--------------------------------------------------------*/

XtCallbackProc makefund(Widget w, unsigned int mode, caddr_t calldata)
{
   short *fsel;
   objinstptr fobj;

   if (areastruct.selects == 0) {
      if (objectdata->schemtype == SYMBOL) {
	 objectdata->schemtype = FUNDAMENTAL;
	 Wprintf("Changed top-level object to type FUNDAMENTAL");
      }
      else if (objectdata->schemtype == FUNDAMENTAL) {
	 if (objectdata->symschem == NULL) {
	    objectdata->schemtype = SYMBOL;
	    Wprintf("Changed top-level object to type SYMBOL");
	 }
	 else {
	    if (objectdata->symschem->schemtype == SYMBOL) {
	       objectdata->schemtype = SCHEMATIC;
	       Wprintf("Changed top-level object to type SCHEMATIC");
	    }
	    else if (objectdata->symschem->schemtype == SCHEMATIC) {
	       objectdata->schemtype = SYMBOL;
	       Wprintf("Changed top-level object to type SYMBOL");
	    }
	 }
      }
      return;
   }

   for (fsel = areastruct.selectlist; fsel < areastruct.selectlist +
		areastruct.selects; fsel++) {
      if (SELECTTYPE(fsel) == OBJECT) {
	 fobj = SELTOOBJINST(fsel);
	 if (fobj->thisobject->schemtype == SYMBOL) {
	    fobj->thisobject->schemtype = FUNDAMENTAL;
	    Wprintf("Changed object to type FUNDAMENTAL");
	 }
	 else if (fobj->thisobject->schemtype == FUNDAMENTAL) {
	    if (fobj->thisobject->symschem != NULL) {
	       if (fobj->thisobject->symschem->schemtype == SYMBOL) {
		  fobj->thisobject->schemtype = SCHEMATIC;
	          Wprintf("Changed object to type SCHEMATIC");
	       }
	       else if (fobj->thisobject->symschem->schemtype == SCHEMATIC) {
		  fobj->thisobject->schemtype = SYMBOL;
	          Wprintf("Changed object to type SYMBOL");
	       }
	    }
	    else {
	       fobj->thisobject->schemtype = SYMBOL;
	       Wprintf("Changed object to type SYMBOL");
	    }
	 }
      }
   }
}

/*--------------------------------------------------------*/
/* Find the page number for an object			  */
/*--------------------------------------------------------*/

int findpageobj(pobj)
  objectptr pobj;
{
   int tpage;
   objectptr chkpage;

   for (tpage = 0; tpage < areastruct.pages; tpage++) {
      if (*(areastruct.pagelist + tpage) == pobj) return tpage;
   }
   return -1;
}

/*--------------------------------------------------------*/
/* Recursively find all sub-circuits associated with the  */
/* top-level circuit and set their filenames to be the	  */
/* same.						  */
/*--------------------------------------------------------*/

findsubschems(toppage, cschem)
  int toppage;
  objectptr cschem;
{
   genericptr *cgen;

   for (cgen = cschem->plist; cgen < cschem->plist + cschem->parts; cgen++) {
      if ((*cgen)->type == OBJECT) {
	 objectptr cobj = TOOBJINST(cgen)->thisobject;

	 if (cobj->symschem != NULL) {
	    int pageno = findpageobj(cobj->symschem);
	    printf("Symbol %s has schematic at page %d\n", cobj->name, pageno);
	    if (pageno < 0) {	/* This shouldn't happen (hopefully) */
	       Wprintf("Error: disconnected schematic!\n");
	    }
	    else {
	       areastruct.filename[pageno] = areastruct.filename[toppage];
	    }

	    /* A symbol on its own schematic page is allowed for clarity */
	    /* of the schematic, but this cannot be a functional part of */
	    /* the schematic circuit!					 */
	    
	    if (cobj->symschem != cschem) {
	       printf("Searching for dependencies of symbol %s\n", cobj->name);
	       findsubschems(toppage, cobj->symschem);
	    }
	    else {
	       printf("Notice:  Symbol on its own schematic\n");
	    }
	 }
      }
   }
}

/*--------------------------------------------------------*/
/* Copy label to corresponding schematic/symbol		  */
/*--------------------------------------------------------*/

copypinlabel(pinlab)
  labelptr pinlab;
{
   labelptr *newlabel, tlabel;
   genericptr *tgen;
   objectptr schemobj = objectdata->symschem;

   if (areastruct.schemon && schemobj != NULL && pinlab->pin) {

      /* If this pin already exists, don't copy it */

      for (tgen = schemobj->plist; tgen < schemobj->plist + schemobj->parts;
		tgen++) {
         if ((*tgen)->type == LABEL) {
	    tlabel = TOLABEL(tgen);
	    if (!strcmp(tlabel->string, pinlab->string)) return;
         }
      }

      NEW_LABEL(newlabel, schemobj);
      (*newlabel)->pin = pinlab->pin;
      (*newlabel)->rotation = pinlab->rotation;
      (*newlabel)->justify = pinlab->justify;
      (*newlabel)->color = pinlab->color;
      (*newlabel)->scale = pinlab->scale;
      (*newlabel)->string = (char *)malloc((1 + strlen(pinlab->string))
		* sizeof(char));
      strcpy((*newlabel)->string, pinlab->string);

      /* place label just outside bounding box, then recompute bbox */

      (*newlabel)->position.x = schemobj->lowerleft.x + (schemobj->width >> 1);
      (*newlabel)->position.y = schemobj->lowerleft.y - TEXTHEIGHT
	   * (*newlabel)->scale;
      schemobj->parts++;
      calcbbox(schemobj);
   }
}

/*--------------------------------------------------------*/
/* Check if top-level page is the same name as a library  */
/* object; if so, connect it.				  */
/*--------------------------------------------------------*/

int checkschem()
{
   objectptr *tlib;

   if (areastruct.schemon == False || objectdata->symschem != NULL) return 0;

   for (tlib = areastruct.library; tlib != areastruct.userlib +
	  areastruct.userobjs; tlib++) {
      if (tlib == areastruct.library + areastruct.builtins) {
	 tlib = areastruct.userlib;
	 if (areastruct.userobjs == 0) break;
      }
      if (!strcmp(objectdata->name, (*tlib)->name)) {
	 objectdata->symschem = (*tlib);
	 objectdata->schemtype = SCHEMATIC;
	 (*tlib)->symschem = objectdata;
	 (*tlib)->schemtype = SYMBOL;
	 return 1;
      }
   }
  return 0;
}

/*--------------------------------------------------------*/
/* Find location of corresponding pin in symbol/schematic */
/*--------------------------------------------------------*/

labelptr findotherpin(curlabel)
  labelptr curlabel;
{
   objectptr other = objectdata->symschem;
   genericptr *tgen;
   labelptr tlab;

   if (areastruct.schemon == False || other == NULL || curlabel->pin == False)
      return NULL;

   for (tgen = other->plist; tgen < other->plist + other->parts; tgen++) {
      if ((*tgen)->type == LABEL) {
	 tlab = TOLABEL(tgen);
	 if (tlab->string == curlabel->string) return tlab;
      }
   }
   return NULL;
}

/*--------------------------------------------------------*/
/* Delete label in corresponding schematic/symbol	  */
/*--------------------------------------------------------*/

deletepinlabel(pinlab)
  labelptr pinlab;
{
   labelptr cplab = findotherpin(pinlab);
   genericptr *genobj;

   if (cplab != NULL) {
      for (genobj = objectdata->symschem->plist; genobj <
	     objectdata->symschem->plist + objectdata->symschem->parts; genobj++)
	 if ((*genobj) == (genericptr)cplab) break;

      free(cplab);
      for (++genobj; genobj < objectdata->symschem->plist +
	      objectdata->symschem->parts; genobj++)
         *(genobj - 1) = *genobj;

      objectdata->symschem->parts--;
   }
}

/*------------------------------------------*/
/* Swap object schematic and symbol pages   */
/*------------------------------------------*/

XtCallbackProc swapschem(Widget w, caddr_t clientdata, caddr_t calldata)
{
   objectptr savepage = objectdata;
   labelptr  *pinlab;
   genericptr *plab;
   Boolean lflag;

   if (areastruct.schemon == False) return;

   if (eventmode == PRESS_MODE || eventmode == COPY2_MODE)
      objectdelete(NORMAL);

   if (objectdata->symschem == NULL) {  /* create symbol or schematic */
      if (objectdata->schemtype == SYMBOL) {
	 int tpage;

	 /* create a new page for the new schematic */

	 for (tpage = 0; tpage < areastruct.pages; tpage++)
	    if (*(areastruct.pagelist + tpage) == NULL) break;
         newpage(tpage);
      }
      else {
	 objectptr *newobject;

	 /* create a new library object for the new symbol */

	 areastruct.userlib = (objectptr *) realloc(areastruct.userlib,
		++areastruct.userobjs * sizeof(objectptr));
	 newobject = areastruct.userlib + areastruct.userobjs - 1;
         *newobject = (objectptr) malloc(sizeof(object));
	 initmem(*newobject);
	 (*newobject)->schemtype = SYMBOL;
	 objectdata = *newobject;
      }

      /* set links between the two objects */

      savepage->symschem = objectdata;
      objectdata->symschem = savepage;

      /* make the name of the new object equal to that of the old */

      strcpy(objectdata->name, savepage->name);

      /* copy all pin labels into the new object */

      for (plab = savepage->plist; plab < savepage->plist + savepage->parts;
		plab++)
	 if ((*plab)->type == LABEL) {
	    genericptr *tgen;
	    labelptr tlab, lpin = (labelptr)*plab;

	    if (lpin->pin) {

      	       /* Only make one copy of each pin name */

	       lflag = False;
               for (tgen = objectdata->plist; tgen <
			objectdata->plist + objectdata->parts; tgen++) {
		  if ((*tgen)->type == LABEL) {
		     tlab = TOLABEL(tgen);
	             if (!strcmp(tlab->string, lpin->string)) lflag = True;
		  }
      	       }
	       if (lflag == True) continue;

	       NEW_LABEL(pinlab, objectdata);
	       (*pinlab)->pin = lpin->pin;
	       (*pinlab)->color = lpin->color;
	       (*pinlab)->rotation = 1;
	       (*pinlab)->scale = 1.0;
	       (*pinlab)->justify = areastruct.justify; 
	       (*pinlab)->position.x = 0;
	       (*pinlab)->position.y = objectdata->parts * (TEXTHEIGHT + 10);
	       (*pinlab)->string = (char *)malloc((1 + strlen(lpin->string))
			* sizeof(char));
	       strcpy((*pinlab)->string, lpin->string);
	       objectdata->parts++;
	    }
         }
   }
   else {
      objectdata = objectdata->symschem;
   }

   if (areastruct.selects > 0) free(areastruct.selectlist);
   areastruct.selects = 0;

   setvscale();
   transferselects();
   
   zoomview(NULL, Number(1), NULL);
   printname(objectdata);
}

#endif
/*-------------------------------------------------------------------------*/