This repository has been archived by the owner on Jul 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
cDyn.cpp
359 lines (345 loc) · 12.6 KB
/
cDyn.cpp
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
/******************************************************************************
Copyright 2012 Victor Yurkovsky
This file is part of FPGAsm
FPGAsm 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, either version 3 of the License, or
(at your option) any later version.
FPGAsm 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 FPGAsm. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "global.h"
#include "cDyn.h"
#include "cModule.h"
#include "cSub.h"
#include "cPrim.h"
#include "cDevice.h"
extern cDevice* pDevice;
#define CLASS cDyn
//char* CLASS::namebuf=(char*)malloc(256);
FILE* CLASS::fout; //static
/*
Note: net names are hier followed by seq number. This allows duplication
in net sources. Otherwise, we would need to build a wire sorter.
=====================================================================
======================================================================*/
CLASS::CLASS(cSub* ero, cDyn* const pop){
hero=ero;
dad=pop;
}
CLASS::~CLASS(){};
/*=====================================================================
======================================================================*/
int CLASS::banglen(char* str,char*fullstr){ //just past the bang
char* nextbang=strchr(str,'!');
if(!nextbang) {
errorIn("banglen()");
fprintf(stderr,"Argument value '%s' is missing the closing !",
fullstr);
error(1);
}
return nextbang-str;
}
/*=====================================================================
expandFile
called by expand for cfgfile parameters
======================================================================*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
char* CLASS::expandFile(const char* filename){
int f = open(filename,O_RDONLY);
if(-1==f){
errorIn("expandFile");
fprintf(stderr,"Trying to open cfgfile \"%s\"\n",filename);
error(-1);
}
char* data = (char*)malloc(0x10000);
strcpy(data,"cfg: ");
read(f,data+5,0x10000-5);
//TODO: clean up
//fprintf(stderr,"EXPANDED\n----------------\n%s\n-------------------\n",data);
//fprintf(stderr,"EXPANDED length %d\n",strlen(data));
return data;
}
/*------------------------------------------------------------------
expand
All data is in unrealized form; now is the time to realize it.
!..! substitution takes place here.
*-----------------------------------------------------------------*/
/*=====================================================================
======================================================================*/
void CLASS::expand(){
char expandbuf[8192]; //TODO: BUFSIZEwill be expanding to here
char* dest;
char* src;
char c;
//hierName(stderr);fprintf(stderr,"\n");
//fprintf(stderr," being expanded %p\n",this);
//--------------------------
// expand parameters. Start by copying hero's
if(hero->pparams){
pparams = new cCollection(*hero->pparams);
//pparams->dump(stderr,"expanding");
// hero->pparams->dump(stderr,"expanding");
// now step through and fix the parsub values
int i;
for(i=0;i<pparams->size;i++){
// If cfgfile parameter is encountered, substitute
// the value of the file for val.
// if(0==strcmp("cfgfile",pparams->name[i])){
// //(pparams->name[i])=(char*)"cfg"; //convert cfgfile into cfg
// char*filedata=expandFile(pparams->data[i]->valStr);
// //now replace the cDatum's string with expanded
// free(pparams->data[i]->valStr); //don't need the filename
// pparams->data[i]->valStr=filedata; //replace with data
// }
cDatum* val=pparams->data[i];
src=val->valStr;
dest=expandbuf; *dest=0;
//copy characters unless a special one is encountered...
bool done=false;
while(!done){
c=*src++;
switch(c){
case '!':
// Substitution!
{
int blen = banglen(src,val->valStr);
//fprintf(stderr,"substitution of %.*s\n",blen,src);
//Dad should have a named parameter like this
int idx=dad->pparams->find(src,blen);
if(-1==idx) {
// char pname[64];
// strncpy(pname,src,blen);
errorIn("expand()");
fprintf(stderr,"While expanding argument '%s',",val->valStr);
fprintf(stderr,"noticed that:\n containing module %s has no parameter ",dad->hero->type->name);
fprintf(stderr,"named '%.*s'",blen,src);
//Argument value !%.*s! requires module %s to have such a parameter named %s"
// ,blen,val->valStr,hero->type->name,pname);
error(1);
}
src+=blen; //In source string !xxx!, skip it
src++; //skip the ! too
//now copy dad's value
//fprintf(stderr,"will copy %s\n",dad->pparams->data[idx]->valStr);
//fprintf(stderr,"dest %s\n",dest);
strcpy(dest,dad->pparams->data[idx]->valStr);
dest+=strlen(dad->pparams->data[idx]->valStr);
//fprintf(stderr,"final answer: %s\n",expandbuf);
}
break;
// case ' ':
case 0:
done=true;
default:
*dest++=c;
break;
}
}
// Now replace parameter data with our string.
// fprintf(stderr,"REPLACING %s's parameter '%s' was {%s} now {%s}\n",//
// hero->name,pparams->name[i],pparams->data[i]->valStr,expandbuf);
//Do not delete - it is a shallow copy...
pparams->data[i]=cDatum::newUnrealized(expandbuf,strlen(expandbuf));
//Now realize...
//fprintf(stderr,"Realizing parameter %d [%s]\n",(int)strlen(pparams->data[i]->valStr),pparams->data[i]->valStr);
pparams->data[i]->realize();
//fprintf(stderr,"Done realizing \n");
}
} else {
pparams = new cCollection();
pparams->solidify();
}
//pparams->dump(stderr,"params");
//if(hero->pparams)
// hero->pparams->dump(stderr,"hero");
//--------------------------
// children.
psubs=0;
psubcnt=0;
if(hero->type->psubs){
psubcnt=hero->type->psubs->size;
//fprintf(stderr,"SUB COUNT at %p %d\n",&psubcnt,psubcnt);
if(psubcnt){
// Now go through the children. Inside dyn, create an array
//pointing to each of the children, as we create them.
psubs = (cDyn**)malloc(psubcnt*sizeof(cDyn*));
int i;
for(i=0;i<psubcnt;i++){
cDatum* dat = hero->type->psubs->data[i];
if(TYPE_SUB != dat->type) {
errorIn("expand()");
fprintf(stderr,"Module '%s' stores an invalid inst (seq %d)\n",
hero->type->name,i);
error(1);
}
cSub* sub = dat->valSub;
//do whatever expansion here
psubs[i]=new cDyn(sub,this);
psubs[i]->expand();
}
}
}
}
/*=====================================================================
======================================================================*/
void CLASS::place(){
//fprintf(stderr,"1. Start placing %s %p\n",hero->name,this);
//hierName(stderr);
//fprintf(stderr,"\n");
//fprintf(stderr," subcnt at %p %d \n",&psubcnt,psubcnt);
//first place ourselves...Simple if we are absolut
cDatum* hloc = getLocation();
//fprintf(stderr,"2. Got location %p\n",hloc);
if(!hloc) {
/* No location specified in the hero. This means use dad's loc,
unless we are top. In this case, just set 0,0. */
//fprintf(stderr,"No loc specified\n");
if(dad)
loc = dad->loc;
else
loc=cDatum::newLocXY(0,0);
} else {
//fprintf(stderr," loc:%p \n",hloc);
//fprintf(stderr," loc:%s \n",hloc->valStr);
//fprintf(stderr,"2.location found: %d\n",hloc->type);
switch (hloc->type){
case TYPE_STR:
//fprintf(stderr,"5. hloc->type is STR %s\n",hloc->valStr);
loc = hloc; //just use our hero's location!
break;
case TYPE_LOCXY: //our hero is xy..
//fprintf(stderr,"6. hloc->type is XY (%d,%d)\n",hloc->valX,hloc->valY);
loc = cDatum::newLocXY(hloc->valX,hloc->valY);//copy constructor...
if(dad){ //TODO: what if no dad
switch(dad->loc->type){
case TYPE_LOCABS:
errorIn("place()");
fprintf(stderr,"cDyn::place(): %s has an absolute location; %s cannot be relative\n",dad->hero->name,hero->name);
error(1);
break;
case TYPE_LOCXY: //dad is xy, so adjust...
loc->valX += dad->loc->valX;
loc->valY += dad->loc->valY;
//fprintf(stderr,"61. dad's location is xy(%d,%d). offsetting to (%d,%d)\n",
// dad->loc->valX,dad->loc->valY,
// loc->valX,loc->valY);
break;
default:
errorIn("place()");
fprintf(stderr,"cDyn:place %s invalid loc type %d\n",hero->name,dad->loc->type);
error(1);
}
} else {
errorIn("place()");
fprintf(stderr,"cDyn:place %s has no parent and cannot be placed\n",hero->name);
error(1);
}
break;
default:
errorIn("place()");
fprintf(stderr,"loc type of %s not ABS or XY, it is %d\n",hero->name,hloc->type);
error(1);
break;
}
}
// now place children
int i;
for(i=0;i<psubcnt;i++){
psubs[i]->place();
}
//fprintf(stderr,"99.done placing %s\n",hero->name);
//fprintf(stderr,"loc is %p\n",loc);
}
U32 CLASS::seq=0;
/*=====================================================================
======================================================================*/
int CLASS::childIndex(cDyn*p){
int i;
for(i=0;i<psubcnt;i++){
if(p==psubs[i]) return i;
}
return -1;
}
/*void CLASS::hierName(){
if(dad) {
dad->hierName();
strcat(namebuf,"/");
strcat(namebuf,hero->name);
} else {
strcpy(namebuf,hero->name);
}
}*/
/*=====================================================================
======================================================================*/
void CLASS::hierName(FILE*f){
if(dad){
dad->hierName(f);
fprintf(f,"/%s",hero->name);
}
else
fprintf(f,"%s",hero->name);
}
/*=====================================================================
======================================================================*/
cDatum* CLASS::getLocation(){
if(pparams){
int iloc = pparams->find("loc",3);
if(-1==iloc) return 0; //0 means no set location
return pparams->data[iloc];
} else {
return 0; //no parameters at all
}
}
/*=====================================================================
error
First, fprintf an error message to stderr.
Then, call error - it outputs the position.
======================================================================*/
void CLASS::errorIn(const char* from){
fprintf(stderr,"----------------------------------------------------------------------\n");
fprintf(stderr,"Error in function cDyn::%s\n",from);
}
void CLASS::error(int errnox){
fprintf(stderr,"\nError occured in definition of module '%s' instance '%s'\n",
dad->hero->type->name, hero->name);
fputs("while processing '",stderr);
hierName(stderr);
fputs("' \n",stderr);
fprintf(stderr,"----------------------------------------------------------------------\n");
throw(errnox);
}
/*=====================================================================
top module not connected
While generating xdl, we found that the top module is not connected.
TODO:This is normally an error, but what about macros?
======================================================================*/
void CLASS::errTopModuleNotConnected(const char* from,const char* pin){
errorIn(from);
fprintf(stderr,"Top module's pin %s is not connected\n",pin);
error(-1);
}
/******************************************************************************
isPrimitive
Originally I thought that having no subs makes it a root node, and a primitive.
But for verilog output it is useful to put in LUT/MUXCY etc. subs that do
not get xdl'd but do get verilog'd. So how do we know if we are a primitive
or not? Simple - if we have a cfg we are a primitive!
******************************************************************************/
bool CLASS::isPrimitive(){
//
// if(psubs) //primitives have psubs=NULL. modules create a collection.
// return false;
// return true;
int iCfg = pparams->find("cfg",3);
return (-1 != iCfg);
}
#include "cDynXdl.cpp"
//#include "cDynVerilog.cpp"