Jspice3
mutual.c
Go to the documentation of this file.
1 /***************************************************************************
2 SCED - Schematic Capture Editor
3 JSPICE3 adaptation of Spice3e2 - Copyright (c) Stephen R. Whiteley 1992
4 Copyright 1990 Regents of the University of California. All rights reserved.
5 Authors: 1981 Giles C. Billingsley (parts of KIC layout editor)
6  1992 Stephen R. Whiteley
7 ****************************************************************************/
8 
9 /*
10  * SCED code for handling mutual inductors.
11  */
12 
13 #include "spice.h"
14 #include "sced.h"
15 #include "scedmacs.h"
16 
17 
18 
19 /***********************************************************************
20  *
21  * Routines to keep track of mutual inductors.
22  *
23  ***********************************************************************/
24 
25 extern char *MenuMUTUL;
26 
27 #ifdef __STDC__
28 static int mut_kb(struct prpty*,struct o*,struct o*,int*);
29 static int add_mutual(int*);
30 static struct o *select_inductor(long,long,struct s*);
31 static struct o *get_mut(long,long,struct s*);
32 #else
33 static int mut_kb();
34 static int add_mutual();
35 static struct o *select_inductor();
36 static struct o *get_mut();
37 #endif
38 
39 
40 void
41 ShowMutual(LookedAhead)
42 
43 /* Show the mutual inductors, and allow additions or deletions */
44 int *LookedAhead;
45 {
46  struct prpty *PDesc,*PNext;
47  struct o *Pointer1,*Pointer2;
48  char *TypeIn;
49  long L1X,L2X,L1Y,L2Y;
50  int i, mutfound = False;
51 
53 
54 again:
55  for (PDesc = Parameters.kpCellDesc->sPrptyList; PDesc; PDesc = PNext) {
56  PNext = PDesc->prpty_Succ;
57  if (PDesc->prpty_Value != P_MUT) continue;
58  /* Referenced by lower left corner */
59  L1X = PDesc->prpty_Data->p_mut.x1;
60  L1Y = PDesc->prpty_Data->p_mut.y1;
61  L2X = PDesc->prpty_Data->p_mut.x2;
62  L2Y = PDesc->prpty_Data->p_mut.y2;
63  Pointer1 = get_mut(L1X,L1Y,Parameters.kpCellDesc);
64  Pointer2 = get_mut(L2X,L2Y,Parameters.kpCellDesc);
65  if (Pointer1 == NULL || Pointer2 == NULL)
66  continue;
67  mutfound = True;
68  ShowCurrentObject(Pointer1,DISPLAY);
69  ShowCurrentObject(Pointer2,DISPLAY);
70  sprintf(TypeOut,"k = %g (a add, d delete) > ",
71  PDesc->prpty_Data->p_mut.coeff);
73  i = mut_kb(PDesc,Pointer1,Pointer2,LookedAhead);
74  if (i < 0)
75  goto quit;
76  if (i > 0)
77  goto again;
78  }
79  if (mutfound == False) {
80  ShowPrompt(
81  "No mutual inductors found. Enter 'a' to add mutual inductor > ");
82  if (mut_kb(NULL,NULL,NULL,LookedAhead) > 0)
83  goto again;
84  }
85 quit:
86  ShowCurrentObject((struct o *)NULL,ERASE);
88  ErasePrompt();
89 }
90 
91 
92 static int
93 mut_kb(PDesc,Pointer1,Pointer2,LookedAhead)
94 
95 /* Process keyboard input */
96 struct prpty *PDesc;
97 struct o *Pointer1,*Pointer2;
98 int *LookedAhead;
99 {
100  struct prpty *pd;
101  char InChar;
102 
103  InChar = FBGetchar(DISPLAY);
104 
105  if (Pointer1 != NULL)
106  ShowCurrentObject(Pointer1,ERASE);
107  if (Pointer2 != NULL)
108  ShowCurrentObject(Pointer2,ERASE);
109 
110  if (InChar == ESCAPE)
111  return (-1);
112  if (InChar == '\0' || InChar == '\n' || InChar == '\r')
113  return (0);
114  if (InChar == 'a' || InChar == 'A')
115  return (add_mutual(LookedAhead));
116  if (InChar == 'd' || InChar == 'D') {
117  if (PDesc == NULL) {
118  ShowPromptAndWait("No mutual inductor to delete.");
119  return (0);
120  }
122  if (pd == PDesc)
124  else {
125  /* must be in the list */
126  for (; pd && pd->prpty_Succ != PDesc; pd = pd->prpty_Succ) ;
127  pd->prpty_Succ = PDesc->prpty_Succ;
128  }
129  free(PDesc->prpty_Data);
130  tfree(PDesc);
132  ShowPromptAndWait("Mutual inductance deleted.");
133  }
134  return (0);
135 }
136 
137 
138 static int
139 add_mutual(LookedAhead)
140 
141 /* Add a mutual inductor */
142 int *LookedAhead;
143 {
144  struct o *Pointer1,*Pointer2;
145  struct prpty *PDesc;
146  char value[32],*TypeIn;
147  double kv;
148  int ret = True;
149 
150 top:
151  ShowPrompt("Point to first coupled inductor.");
152  loop {
153  switch (PointLoop(LookedAhead)) {
154  case PL_UND:
155  case PL_CMD:
156  case PL_ESC:
157  ret = False;
158  goto quit;
159  case PL_PCW:
160  if ((Pointer1 =
162  Parameters.kpCellDesc)) != NULL) {
163  ShowCurrentObject(Pointer1,DISPLAY);
164  break;
165  }
166  continue;
167  }
168  break;
169  }
170 
171  ShowPrompt("Point to second coupled inductor.");
172  loop {
173  switch (PointLoop(LookedAhead)) {
174  case PL_UND:
175  goto top;
176  case PL_ESC:
177  case PL_CMD:
178  ret = False;
179  goto quit;
180  case PL_PCW:
181  if ((Pointer2 =
183  Parameters.kpCellDesc)) != NULL &&
184  Pointer2 != Pointer1) {
185  ShowCurrentObject(Pointer2,DISPLAY);
186  break;
187  }
188  continue;
189  }
190  break;
191  }
192 
193  do {
194  ShowPrompt("Enter SPICE coupling factor: k = ");
195  if ((TypeIn = FBEdit(NULL)) == NULL) {
196  ret = False;
197  goto quit;
198  }
199  strcpy(value,TypeIn);
200  }
201  while (sscanf(value,"%le",&kv) < 1 || kv < 0 || kv > 1);
202 
203  /* see if this mutual already exists */
205  for (; PDesc; PDesc = PDesc->prpty_Succ) {
206  if (PDesc->prpty_Value != P_MUT) continue;
207  if ((Pointer1->oLeft == PDesc->prpty_Data->p_mut.x1 &&
208  Pointer1->oBottom == PDesc->prpty_Data->p_mut.y1 &&
209  Pointer2->oLeft == PDesc->prpty_Data->p_mut.x2 &&
210  Pointer2->oBottom == PDesc->prpty_Data->p_mut.y2) ||
211  (Pointer2->oLeft == PDesc->prpty_Data->p_mut.x1 &&
212  Pointer2->oBottom == PDesc->prpty_Data->p_mut.y1 &&
213  Pointer1->oLeft == PDesc->prpty_Data->p_mut.x2 &&
214  Pointer1->oBottom == PDesc->prpty_Data->p_mut.y2)) {
215  PDesc->prpty_Data->p_mut.coeff = kv;
216  ShowPromptAndWait("Existing mutual inductor updated");
217  goto quit;
218  }
219  }
220 
221  sprintf(TypeOut,"%ld %ld %ld %ld %s",
222  Pointer1->oLeft,Pointer1->oBottom,
223  Pointer2->oLeft,Pointer2->oBottom,
224  value);
226 
227 quit:
228  ShowCurrentObject((struct o *)NULL,ERASE);
229  return (ret);
230 }
231 
232 
233 static struct o *
234 select_inductor(X,Y,CellDesc)
235 
236 long X,Y;
237 struct s *CellDesc;
238 {
239  struct g *GenDesc;
240  struct o *Pointer;
241  char *name;
242 
243  if (Not CDInitGen(CellDesc,0,
244  X,Y,X,Y,&GenDesc)) MallocFailed();
245 
246  loop {
247  CDGen(CellDesc,GenDesc,&Pointer);
248  if (Pointer == NULL) break;
249  if (Pointer->oInfo == SQ_GONE) continue;
250 
251  name = ((struct c *)Pointer->oRep)->cMaster->mName;
252  if (!cieq(name,"ind")) continue;
253  /* folks, we have an inductor */
254  tfree(GenDesc);
255  return (Pointer);
256  }
257  return (NULL);
258 }
259 
260 
261 static struct o *
262 get_mut(X,Y,CellDesc)
263 
264 /* Return the inductor with lower left corner at X,Y */
265 long X,Y;
266 struct s *CellDesc;
267 {
268  struct g *GenDesc;
269  struct o *Pointer;
270  char *name;
271 
272  if (Not CDInitGen(CellDesc,0,
273  X+100,Y+100,X+100,Y+100,&GenDesc)) MallocFailed();
274 
275  loop {
276  CDGen(CellDesc,GenDesc,&Pointer);
277  if (Pointer == NULL) break;
278  if (Pointer->oInfo == SQ_GONE) continue;
279 
280  name = ((struct c *)Pointer->oRep)->cMaster->mName;
281  if (!cieq(name,"ind")) continue;
282  if (X == Pointer->oLeft && Y == Pointer->oBottom) {
283  tfree(GenDesc);
284  return (Pointer);
285  }
286  }
287  return (NULL);
288 }
289 
290 
291 struct line *
292 PrintMutual(CellDesc)
293 
294 /* Return the SPICE lines for the mutual inductors */
295 struct s *CellDesc;
296 {
297  struct prpty *PDesc,*pd1,*pd2;
298  struct o *Pointer1,*Pointer2;
299  struct line *d,*d0 = NULL;
300  char name1[8],name2[8],value[32];
301  long L1X,L2X,L1Y,L2Y;
302  int count = 0;
303 
304  PDesc = CellDesc->sPrptyList;
305  for (; PDesc; PDesc = PDesc->prpty_Succ) {
306  if (PDesc->prpty_Value != P_MUT) continue;
307  L1X = PDesc->prpty_Data->p_mut.x1;
308  L1Y = PDesc->prpty_Data->p_mut.y1;
309  L2X = PDesc->prpty_Data->p_mut.x2;
310  L2Y = PDesc->prpty_Data->p_mut.y2;
311  Pointer1 = get_mut(L1X,L1Y,CellDesc);
312  Pointer2 = get_mut(L2X,L2Y,CellDesc);
313  if (Pointer1 == NULL || Pointer2 == NULL)
314  continue;
315 
316  strcpy(name1,GetName(Pointer1->oPrptyList));
317  strcpy(name2,GetName(Pointer2->oPrptyList));
318  count++;
319  sprintf(TypeOut,"K%d %s %s %g",count,name1,name2,
320  PDesc->prpty_Data->p_mut.coeff);
321  if (d0 == NULL)
322  d = d0 = AllocateLine(TypeOut);
323  else {
325  d = d->li_next;
326  }
327  }
328  return (d0);
329 }
330 
331 
332 char *
333 GetName(PDesc)
334 
335 /* Return the device name, as known to SPICE */
336 struct prpty *PDesc;
337 {
338  static char nbuf[8];
339 
340  for (; PDesc; PDesc = PDesc->prpty_Succ) {
341  if (PDesc->prpty_Value != P_NAME) continue;
342  sprintf(nbuf,"%s%d",PDesc->prpty_Data->p_name.name,
343  PDesc->prpty_Data->p_name.num);
344  return (nbuf);
345  }
346  return (NULL);
347 }
348 
struct prpty * sPrptyList
Definition: cddefs.h:133
#define PL_ESC
Definition: sced.h:62
void ErasePrompt()
Definition: viewport.c:538
#define loop
Definition: cdmacs.h:11
int struct o * Pointer
Definition: cd.c:1311
int prpty_Value
Definition: cdprpty.h:67
#define SQ_GONE
Definition: sced.h:347
long kcRawY
Definition: sced.h:103
#define PL_UND
Definition: sced.h:63
int kpModified
Definition: sced.h:261
int cieq()
long kcRawX
Definition: sced.h:103
char * strcpy()
Definition: cddefs.h:119
int PointLoop()
static int add_mutual()
struct kc SCursor
Definition: init.c:21
void ShowCurrentObject()
#define ERASE
Definition: scedmacs.h:11
#define Not
Definition: cdmacs.h:16
struct s * kpCellDesc
Definition: sced.h:207
struct line * AllocateLine()
int x1
Definition: cdprpty.h:31
#define PL_PCW
Definition: sced.h:65
void CDGen()
void ShowPromptAndWait()
union prp_data * prpty_Data
Definition: cdprpty.h:65
void MallocFailed()
Definition: scedintr.c:857
Definition: fteinp.h:14
long oBottom
Definition: cddefs.h:143
long X
Definition: actions.c:450
Definition: sced.h:146
char * MenuMUTUL
Definition: scedintr.c:89
void MenuDeselect()
Definition: cddefs.h:237
#define tfree(x)
Definition: cdmacs.h:22
int CDInitGen()
int CDAddProperty()
#define NULL
Definition: spdefs.h:121
char TypeOut[200]
Definition: init.c:23
struct kp Parameters
Definition: init.c:19
int y1
Definition: cdprpty.h:32
#define True
Definition: scedstub.c:16
short FBGetchar()
static struct o * select_inductor()
int y2
Definition: cdprpty.h:34
struct line * li_next
Definition: fteinp.h:18
struct o * oRep
Definition: cddefs.h:144
#define DISPLAY
Definition: scedmacs.h:12
Definition: cddefs.h:142
#define P_NAME
Definition: cdprpty.h:13
Definition: cddefs.h:177
static int mut_kb()
#define P_MUT
Definition: cdprpty.h:14
struct prpty * prpty_Succ
Definition: cdprpty.h:66
long oLeft
Definition: cddefs.h:143
char * FBEdit()
int count
Definition: output.c:152
Definition: cddefs.h:227
double coeff
Definition: cdprpty.h:35
#define False
Definition: scedstub.c:15
struct line * PrintMutual(struct s *CellDesc)
Definition: mutual.c:292
#define PL_CMD
Definition: sced.h:64
static struct o * get_mut()
int x2
Definition: cdprpty.h:33
#define ESCAPE
Definition: sced.h:91
void MenuSelect()
struct prp_mut p_mut
Definition: cdprpty.h:53
long Y
Definition: actions.c:450
Definition: cdprpty.h:62
void ShowPrompt(char *str)
Definition: scedstub.c:71
short oInfo
Definition: cddefs.h:147
char * GetName(struct prpty *PDesc)
Definition: mutual.c:333
struct prpty * oPrptyList
Definition: cddefs.h:146
void free()
void ShowMutual(int *LookedAhead)
Definition: mutual.c:41