1 /****************************************************************************
2     Copyright (C) 1987-2015 by Jeffery P. Hansen
3 
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License along
15     with this program; if not, write to the Free Software Foundation, Inc.,
16     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 ****************************************************************************/
18 /*
19     Declarations for circuit representation structures.
20 */
21 #ifndef __elements_h
22 #define __elements_h
23 
24 #include "html.h"
25 
26 /*****************************************************************************
27  * Net types used to mark nets in HDL modules.
28  *****************************************************************************/
29 #define NT_NONE		0
30 #define NT_INPUT	1
31 #define NT_OUTPUT	2
32 #define NT_INOUT	3
33 #define NT_WIRE		4
34 #define NT_REG		5
35 
36 /*****************************************************************************
37  * Net types used to mark nets in netlist modules.
38  *****************************************************************************/
39 #define NV_WIRE		0
40 #define NV_TRI		1
41 #define NV_WAND		2
42 #define NV_TRIAND	3
43 #define NV_WOR		4
44 #define NV_TRIOR	5
45 #define NV_TRI0		6
46 #define NV_TRI1		7
47 #define NV_TRIREG	8
48 
49 /*
50  * GCellSpec - Decoded built-in gate cell
51  */
52 struct GCellSpec_str {
53   char		gc_name[STRMAX];	/* Name of cell */
54   GGateInfo    *gc_info;		/* Gate info pointer */
55   char		gc_invSpec[STRMAX];	/* Inverter specification */
56   int		gc_multiPad;		/* Multi-pad count */
57   int		gc_numBits;		/* Bit width of cell */
58 #if 0
59   int		gc_numDelays;		/* Number of delay values */
60   int		gc_delays[MAXDELAYS];	/* Delay value for cell */
61 #endif
62   int		gc_numParms;		/* Number of parameters */
63   int		gc_parms[MAXPARMS];	/* Parameters values */
64 };
65 
66 struct wirelist {
67   GWire		*wl_wire;	/* Wire in the wirelist */
68   GWireList	*wl_next;	/* Next in the wirelist */
69 };
70 
71 typedef struct {
72   int driver;			/* Which joint is driver */
73   int gidx;			/* Index of joint (for reading circuits) */
74 } GGateJoint;
75 
76 typedef struct {
77   char *memfile;		/* File Containing memory definition */
78 } GGateMem;
79 
80 typedef struct {
81   char *moduleName;		/* Function definition of block */
82   int gwidth,gheight;		/* Block dimensions */
83   GModSymbol *symbol;		/* Symbol for this element */
84   int numModParms;		/* Number of module parameters */
85   char **modParms;		/* Module parameters (not set to default) */
86 } GGateBlock;
87 
88 typedef struct {
89   short omega,phi,duty;
90 } GGateClock;
91 
92 typedef struct {
93   unsigned perm_dipval;		/* Permanent dipval */
94   unsigned dipval;		/* Current value */
95   unsigned showNet;		/* Show net name with gate */
96 } GGateDip;
97 
98 typedef struct {
99   unsigned ltype;		/* Type of LED */
100   unsigned value;		/* Current led value */
101   unsigned valid;		/* Valid bits in value */
102 } GGateLed;
103 
104 typedef struct {
105   int spliceside;		/* Specifies which side of the wire the splice it on */
106   int msb,lsb;			/* Range of bits selected by splice */
107   int gidx;			/* Index of splice (for reading circuits) */
108 } GGateTap;
109 
110 typedef struct {
111   int portDir;			/* Specifies direction wires are layed out */
112   int drivePort;		/* The port designated as the signal driver (0 or 1)*/
113   int type;			/* Type code (AUTO, FIXED, TRAN) */
114   int newDrivePort;		/* Used for AUTO updating */
115 } GGateConcat;
116 
117 typedef struct {
118   int extbar;			/* Use extender bar for multi-input basic gates */
119 } GGateBasic;
120 
121 typedef struct {
122   int		width,height;	/* Size of the comment block in pixels */
123   int		reqWidth;	/* Requested width (html only) */
124   TextLine	*first;		/* First line in comment */
125   TextLine	*last;		/* Last line in comment */
126   Html		*html;		/* Formatted html */
127 
128   /*
129    * These are used for converting old-style comments.
130    */
131   int		doLink;		/* Old-style link flag */
132   char		*link;		/* Old-style link text */
133 } GGateComment;
134 
135 typedef struct {
136   int		width,height;	/* Size of the frame */
137   char		*text;		/* Text of the frame */
138   Html		*html;		/* Formatted html */
139 } GGateFrame;
140 
141 typedef struct {
142   int		select_side;	/* Side that selector is on (0 means left in 0 orientation) */
143   int		data_order;	/* Order of mux data lines (0 means increasing to right in 0 orientation) */
144 } GGateMux;
145 
146 typedef struct {
147   int		mirror;		/* Mirror image of ports */
148 } GGateFF;
149 
150 /*
151  * This is the data structure for a circuit element (GCElement defined above).  All
152  * instance of gates, modules, etc. are defined by this data structure.  This data
153  * structure is a sort of a poor man's C++ class with virtual functions.  The "typeinfo"
154  * member points to a structure that represents the derived class (AND, OR, MUX, etc.).
155  * It containes some type specific data as well as a bunch of function pointers that
156  * can be thought of as virtual methods.  There is also a "union" which contains gate
157  * type specific data.  For example u.clock contains data that is specific to clock gates.
158  */
159 struct celemnt {
160   GGateInfo *typeinfo;		/* Type information of gate */
161   char *ename;			/* Element Name */
162   int enumb;			/* Element number */
163   int xpos,ypos,orient;		/* Position of element */
164   char *tech;			/* Technology of gate */
165   int *delays;			/* Delay values for the gate */
166 
167   unsigned selected:1;		/* Gate selected */
168   unsigned top:1;		/* Top edge selected */
169   unsigned bottom:1;		/* Bottom edge selected */
170   unsigned right:1;		/* Right edge selected */
171   unsigned left:1;		/* Left edge selected */
172   unsigned mark:1;		/* Generic mark for gates */
173   unsigned show_name:1;		/* Names should be displayed */
174   unsigned anchored:1;		/* Gate position is fixed */
175   unsigned cpath_cut:1;		/* Gate is a critical path cut-point */
176   unsigned cust_delay:1;	/* Use custom delay values */
177   unsigned isModuleInterface:1;	/* This intance is a module interface */
178 
179   union {
180     GGateClock		clock;
181     GGateDip		sw;
182     GGateLed		led;
183     GGateTap		tap;
184     GGateJoint		joint;
185     GGateMem		mem;
186     GGateBlock		block;
187     GGateConcat		cat;
188     GGateBasic		basic;
189     GGateFrame		frame;
190     GGateComment	comment;
191     GGateMux		mux;
192     GGateFF		ff;
193   } u;
194 
195   /*
196    * Vector of wire pads (allocated in gate_new).  This array can be extended
197    * for some symbol modules by doing a realloc.
198    */
199   GWire *wires[DEFAULT_PADMAX];
200 };
201 
202 /*
203  * Location information for a gate "pad"
204  */
205 typedef struct locate {
206   int x1,y1;			/* Starting position of pad */
207   int x2,y2;			/* Ending position of pad */
208   int dir;			/* Direction of pad */
209 } GPadLoc;
210 
211 /*
212    Used for gateinfo on wires.
213 */
214 typedef struct padinfo {
215   char *name;			/* Pin name for wires on this pad */
216   int iotype;			/* I/O type (IN, OUT, TRI) */
217   int size;			/* Default bit-size */
218   int num;			/* Initial number of pins */
219   GPadLoc *Loc;			/* Location info for pins */
220   unsigned CanAdd : 1;		/* Can add to pin list */
221 } GPadInfo;
222 
223 /*
224   Used to specify the position of a gate name label.
225  */
226 typedef struct {
227   int x,y;			/* Position of label */
228   int just;			/* Justification mode for text */
229 } GLabelPos;
230 
231 typedef struct gKeyMenuEnt {
232   char *key_seq;		/* Key sequence */
233   struct {
234     char *name;			/* Root menu entry to use */
235     int ul;			/* Underline position */
236   } root;
237   struct {
238     char *name;			/* Entry to use */
239     int ul;			/* Underline position */
240     char *gtag;			/* Grouping tag */
241     int order;			/* Ordering within group */
242   } entry;
243   char *command;		/* Command to execute */
244   short accel_label_only;	/* Use key_seq only as menu accelerator label */
245 } GKeyMenuEnt;
246 
247 /*
248  * GGateInfo - Gate type definition
249  *
250  * This data structure is used to describe a gate type.
251  *
252  */
253 struct gateinfo {
254   int		code;					/* Code number for gate */
255   const char	*name;					/* Name of this gate type */
256   char		*vnames;				/* Verilog names */
257   unsigned	vmask;					/* Verilog name mutating ports */
258   const char	*psprint;				/* Postscript display function for gate */
259   char	**psdef;				/* Postscript definition for gate */
260 
261   int		gi_multiPad;				/* Pad used to generate number of ports of gate */
262   int		gi_bitPad;				/* Pad used to generate bit size of gate */
263 
264   GKeyMenuEnt cmds[MAXKBINDS];				/* Menu and keyboard shortcuts */
265 
266   iconDimensions *dim;					/* Size of gate */
267 
268   int NumPads;						/* Number of pin types */
269   GPadInfo Pad[DEFAULT_PADMAX];				/* Pad type descriptions */
270 
271   GLabelPos lpos[NUMORIENTS];				/* Positions of gate name labels */
272 
273   struct {
274     unsigned CanRot:1;			/* Do rotations exist */
275     unsigned NoInvert:1;		/* Can't put inverters on it */
276     unsigned IsIOnet:1;			/* Is this an I/O net (port, ground, pullup, pulldown or vdd) */
277     unsigned WireProps:1;		/* Is gate display affected by wire property changes? */
278     unsigned special_move : 1;		/* Special movement method */
279     unsigned single_icon : 1;		/* set if this is a single icon gate  */
280   } Flags;
281 
282   char		*delayNames[MAXDELAYS];			/* Name of delay parameters */
283 
284   /* Function for gate creation */
285   GCElement *(*MakeFunction)(EditState**,GModuleDef*,
286 			     int,int,int,int,const char*,int,const char**,int);
287   void (*WriteCellDef)(FILE *,GCellSpec*);		/* Write a cell definition */
288   void (*InitGate)(GCElement*);				/* Initialize new gate */
289   void (*DeleteGate)(GCElement*,GModuleDef*,int);	/* Delete gate function */
290   void (*GetExtents)(GCElement *g
291 		     ,TargetDev_e target
292 		     ,int *minx,int *miny
293 		     ,int *maxx,int *maxy,int *bd);	/* Get bounding box of gate */
294   int (*HitDistance)(GCElement*,int,int);		/* Distance to gate from (x,y) */
295   void (*DrawGate)(GCElement*,int);			/* Draw function */
296   void (*MoveGate)(GCElement*,int,int);			/* Move function */
297   GCElement *(*CopyGate)(GModuleDef*,GCElement*
298 			      ,int,int,unsigned);	/* Copy a gate */
299   void (*AddInput)(EditState*,GCElement*);		/* Add Input to a gate */
300   void (*AddOutput)(EditState*,GCElement*);		/* Add Output to a gate */
301   void (*AddTri)(EditState*,GCElement*);		/* Add Tri-State pin to a gate */
302   void (*Rotate)(GCElement*,int,int,int);		/* Rotate a gate and fix wires */
303   void (*RemovePort)(EditState*,GCElement*,GWire*);	/* Remove port from a gate */
304   void (*ChangePin)(EditState*,GCElement*);		/* Change direction of pin */
305   void (*SimInitFunc)(EditState*,GCElement*,const char*); /* For simulator initialization */
306   int (*SimHitFunc)(EditState*,GCElement*);		/* Handles sim-mode mouse hit on gate */
307   void (*PSWrite)(GPrint*,GModLayout*,GCElement*);	/* Write PostScript for gate */
308   int (*EditProps)(GCElement*,int); 			/* Edit gate properties (new style) */
309   void (*VerSave)(FILE*,GCElement*); 			/* Save in verilog format */
310   void (*SetProp)(GCElement*,const char*,const void*);	/* Set property */
311   void (*VersionDelta)(GCElement*,Version*);		/* Update from old version */
312   GWireNode *(*WireSnap)(GCElement*,GWire*,int *,int);	/* Snap wires attached to a gate */
313 
314   /*********** members after here are initilized at run-time ***********/
315   Icon		*icon[2*NUMORIENTS];			/* Icons for each orientation */
316   GDelayDef	*delay_defs;				/* Delay definitions for this primitive */
317   int		num_delays;				/* Number of elements in delayNames[] */
318   Icon		*altIcon[2*NUMORIENTS];			/* Icons for each orientation */
319 };
320 
321 int GCElement_numPads(GCElement *g);
322 GPadLoc *GCElement_getPadLoc(GCElement *g,int p,int r);
323 int GCElement_getPadDir(GCElement *g,int p);
324 const char *GCElement_getPadName(GCElement *g,int p);
325 #define GCElement_getPadNum(g,p) (g)->typeinfo->Pad[(p)].num
326 int GCElement_getPadCanAdd(GCElement *g,int p);
327 #define GCElement_getType(g) (g)->typeinfo->code
328 #define GCElement_isModule(g) ((g)->typeinfo->code == GC_BLOCK || (g)->typeinfo->code == GC_SYMBLOCK)
329 void GCElement_setSymbol(GCElement *g, GModSymbol *s);
330 #define GCElement_getSymbol(g) ((g)->u.block.symbol)
331 GWire *GCElement_getPort(GCElement *g, const char *portName);
332 
333 GGateInfo *GGateInfo_lookup(const char*);
334 GGateInfo *GGateInfo_vlookup(const char*);
335 int GGateInfo_variantNum(GGateInfo *gi,const char *func);
336 GGateInfo *GGateInfo_codeLookup();
337 
338 /*
339  * Public comment methods.
340  */
341 const char *Comment_getHyperlink(GCElement *g,int x,int y);
342 
343 #endif
344