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 #include "tkgate.h"
19 #include "comment.h"
20 
21 static GCElement *Script_Make(EditState **es,GModuleDef *env,int GType,
22 			      int x,int y,int r,const char *Name,int noWires,
23 			      const char **options,int nOptions);
24 static int Script_EditProps(GCElement *g,int isLoadDialog);
25 static void Script_VerSave(FILE *f,GCElement *g);
26 static void Script_SetProp(GCElement *g,const char *prop,const void *value);
27 static GCElement *Script_Copy(GModuleDef *M,GCElement *g,int x,int y,unsigned flags);
28 static void Script_Delete(GCElement *g,GModuleDef *M,int drawp);
29 
30 
31 static iconDimensions script_iconDims[] = {
32   {0,  0, 31, 31, 15, 15},
33   {32, 0, 31, 31, 15, 15},
34   {32, 32, 31, 31, 15, 15},
35   {0,  32, 31, 31, 15, 15},
36 };
37 static int script_iconBoldOffset = 64;
38 
39 static char *psScript[] = {
40   "%",
41   "% A Script",
42   "/script {",
43   "  [[0 0][0 0][0 0][0 0]] adjstartgate",
44   "  -15.5 -15.5 moveto",
45   "  -15.5 15.5 lineto",
46   "  15.5 15.5 lineto",
47   "  15.5 -15.5 lineto",
48   "  closepath stroke",
49   "  grestore",
50   "} def",
51   0
52 };
53 
54 GGateInfo gate_script_info = {
55   /*.Code = */0,
56   /*.name = */"SCRIPT",
57   /*.vnames = */"script", /*.vmask = */0x0,
58   /*.psprint = */"script", /*.psdef = */psScript,
59   /*.gi_multiPad = */-1, /*.gi_bitPad = */0,
60 
61   /*.cmds = */{
62    { /*.key_seq = */"V",
63      /*.root = */{/*.name = */0, /*.ul = */0},
64      /*.entry = */{/*.name = */"gm.script", /*.ul = */0, /*.gtag = */0},
65      /*.command = */"gat_make SCRIPT" },
66    {0}
67   },
68 
69   /*.dim = */script_iconDims,
70   /*.NumPads = */0,
71   /*.Pad = */{{0}},
72   /*.lpos = */{{0,-19,CT},{0,-19,CT},{0,-19,CT},{0,-19,CT}},
73   /*.Flags = */{1},
74   /*.delayNames = */{0},
75 
76   /*.MakeFunction = */Script_Make,
77   /*.WriteCellDef = */Nop_WriteCellDef,
78   Generic_Init,
79   Script_Delete,
80   Generic_GetExtents,
81   Generic_HitDistance,
82   Generic_Draw,
83   Generic_Move,
84   Script_Copy,
85   Err_AddInput,
86   Err_AddOutput,
87   Err_AddInOut,
88   Generic_Rotate,
89   Err_RemovePort,
90   Err_ChangePin,
91   Nop_SimInitFunc,
92   Nop_SimHitFunc,
93   Generic_PSWrite,
94   Script_EditProps,
95   Script_VerSave,
96   Script_SetProp
97 };
98 
Script_Make(EditState ** es,GModuleDef * env,int GType,int x,int y,int r,const char * Name,int noWires,const char ** options,int nOptions)99 static GCElement *Script_Make(EditState **es,GModuleDef *env,int GType,
100 			      int x,int y,int r,const char *Name,int noWires,const char **options,int nOptions)
101 {
102   GCElement *g;
103   char buf[STRMAX];
104 
105   if (!Name) {
106     Name = buf;
107     sprintf(buf,"script1");
108   }
109 
110   g = Generic_Make(es,env,GType,x,y,r,Name,noWires,options,nOptions);
111   ob_touch(g);
112   g->show_name = 1;
113 
114   return g;
115 }
116 
Script_Copy(GModuleDef * M,GCElement * g,int x,int y,unsigned flags)117 static GCElement *Script_Copy(GModuleDef *M,GCElement *g,int x,int y,unsigned flags)
118 {
119   GCElement *ng = Script_Make(&TkGate.circuit->es, M, g->typeinfo->code, x, y, g->orient, g->ename, 0,0,0);
120   TextLine *L;
121 
122   ng->u.comment.first = ng->u.comment.last = 0;
123   for (L = g->u.comment.first;L;L = L->next) {
124     Comment_addLine(ng,L->text);
125   }
126 
127 
128   return ng;
129 }
130 
Script_Delete(GCElement * g,GModuleDef * M,int drawp)131 void Script_Delete(GCElement *g,GModuleDef *M,int drawp)
132 {
133   if (M)     gate_remove(M,g);
134   if (drawp) gate_draw(g,0);
135 
136   Comment_flushLines(g);
137 }
138 
139 
Script_SetProp(GCElement * g,const char * prop,const void * value)140 static void Script_SetProp(GCElement *g,const char *prop,const void *value)
141 {
142   ob_touch(g);
143 
144   if (strcmp(prop,"/line") == 0) {
145     Comment_addLine(g,value);
146   }
147 }
148 
Script_EditProps(GCElement * g,int isLoadDialog)149 static int Script_EditProps(GCElement *g,int isLoadDialog)
150 {
151   char buf[STRMAX],pos[64];
152 
153   Generic_EditProps(g,isLoadDialog);
154 
155   if (isLoadDialog) {
156     TextLine *L;
157     int n = 0;
158 
159     for (L = g->u.comment.first;L;L = L->next) {
160       sprintf(pos,"%d",n++);
161       Tcl_SetVar2(TkGate.tcl,"edgat_commentLines",pos,L->text,TCL_GLOBAL_ONLY);
162     }
163 
164     sprintf(buf,"%d",n);
165     Tcl_SetVar(TkGate.tcl,"edgat_commentLen",buf,TCL_GLOBAL_ONLY);
166   } else {
167     const char *p;
168     int n = 0;
169     int i;
170     Tcl_Interp *tcl = TkGate.tcl;
171 
172     Comment_flushLines(g);
173 
174     if ((p = Tcl_GetVar(tcl,"edgat_commentLen",TCL_GLOBAL_ONLY)))
175       sscanf(p,"%d",&n);
176     for (i = 0;i < n;i++) {
177       sprintf(buf,"%d",i);
178       p = Tcl_GetVar2(tcl,"edgat_commentLines",buf,TCL_GLOBAL_ONLY);
179       if (p) Comment_addLine(g,p);
180     }
181    }
182   return 0;
183 }
184 
185 /*****************************************************************************
186  *
187  * Generate primitive cell definition for scripts.
188  *
189  * Parameters:
190  *    f			File to write cell to.
191  *    name		Name of cell to write.
192  *
193  *****************************************************************************/
Script_VerSave(FILE * f,GCElement * g)194 static void Script_VerSave(FILE *f,GCElement *g)
195 {
196   TextLine *L;
197   void *encoder = Circuit_getSaveFileEncoder(TkGate.circuit);
198 
199   fprintf(f,"//: beginscript %s",g->ename);
200   VerilogBasicGateComment(f,g,0);
201   fprintf(f,"\n");
202 
203   for (L = g->u.comment.first;L;L = L->next) {
204     char buf[STRMAX];
205     recodeText(encoder,buf,STRMAX,L->text);
206     fprintf(f,"%s\n",buf);
207   }
208 
209   fprintf(f,"//: endscript\n");
210 }
211 
init_script()212 void init_script()
213 {
214   Pixmap P;
215 
216   P = Pixmap_registerFromFile("script","script.b");
217   gateinfo_iconInit(&gate_script_info,P,script_iconDims,script_iconBoldOffset);
218   RegisterGate(&gate_script_info);
219 }
220