-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.hpp
415 lines (371 loc) · 11.4 KB
/
module.hpp
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
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
template<
typename IdType,
typename ValueType,
typename SizeType>
class NetValue;
string interchangable_types[] = {"OrGate", "XorGate", "NorGate", "AndGate", "NandGate", "XandGate"};
const int num_interchangable_types = 6;
template<
typename IdType,
typename ValueType,
typename SizeType>
class Module
{
public:
string type ; // Type of the module
string name ; // Name of the module
SizeType bit_width ; // Bit width (if applicable)
SizeType min_inputs ; // minimum number of inputs
SizeType max_inputs ; // maximum number of inputs
SizeType num_inputs ; // Number of inputs to the module
SizeType min_outputs ; // minimum number of outputs
SizeType max_outputs ; // maximum number of outputs
SizeType num_outputs ; // Number of outputs to the module
string *input_names ; // Names of the inputs
string *output_names; // Names of the outputs
IdType *input_Ids ; // Ids of the inputs
IdType *output_Ids ; // Ids of the outputs
int *affects ; // Whether this affects others
Module() :
type ("undefined"),
name ("undefined"),
bit_width (0 ),
min_inputs (1 ),
max_inputs (-1 ),
num_inputs (0 ),
min_outputs (1 ),
max_outputs (1 ),
num_outputs (0 ),
input_names (NULL ),
output_names(NULL ),
input_Ids (NULL ),
output_Ids (NULL ),
//num_affected_modules(0 ),
affects (NULL )
{
} // end Module()
virtual ~Module()
{
if (input_names != NULL) {delete[] input_names ; input_names = NULL;}
if (output_names != NULL) {delete[] output_names; output_names = NULL;}
if (input_Ids != NULL) {delete[] input_Ids ; input_Ids = NULL;}
if (output_Ids != NULL) {delete[] output_Ids ; output_Ids = NULL;}
if (affects != NULL) {delete[] affects ; affects = NULL;}
} // end ~Module()
int Init(
string type,
string name,
SizeType bit_width,
SizeType num_inputs,
SizeType num_outputs,
string *input_names,
string *output_names)
{
if (bit_width > (SizeType)sizeof(ValueType)*8)
{
cerr<<"Error ["<<__FILE__<<","<<__LINE__<<"]: @ "<<name
<<" bit_width "<<bit_width<<" > "<<sizeof(ValueType)*8<<endl;
return 3;
}
if (min_inputs!=-1 && num_inputs < min_inputs)
{
cerr<<"Error ["<<__FILE__<<","<<__LINE__<<"]: @ "<<name
<<" #inputs "<<num_inputs<<" < "<<min_inputs<<endl;
return 4;
}
if (max_inputs!=-1 && num_inputs > max_inputs)
{
cerr<<"Error ["<<__FILE__<<","<<__LINE__<<"]: @ "<<name
<<" #inputs "<<num_inputs<<" > "<<max_inputs<<endl;
return 5;
}
if (min_outputs!=-1 && num_outputs < min_outputs)
{
cerr<<"Error ["<<__FILE__<<","<<__LINE__<<"]: @ "<<name
<<" #outputs "<<num_outputs<<" < "<<min_outputs<<endl;
return 6;
}
if (max_outputs!=-1 && num_outputs > max_outputs)
{
cerr<<"Error ["<<__FILE__<<","<<__LINE__<<"]: @ "<<name
<<" #outputs "<<num_outputs<<" > "<<max_outputs<<endl;
return 7;
}
this -> type = type ;
this -> name = name ;
this -> bit_width = bit_width ;
this -> num_inputs = num_inputs ;
this -> num_outputs = num_outputs;
this -> input_names = new string[num_inputs ];
this -> output_names = new string[num_outputs];
this -> input_Ids = new IdType[num_inputs ];
this -> output_Ids = new IdType[num_outputs];
transform(this->name.begin(), this->name.end(),
this->name.begin(), ::toupper);
for (SizeType i=0; i<num_inputs; i++)
{
string str = input_names[i];
transform(str.begin(), str.end(), str.begin(), ::tolower);
this->input_names[i] = str;
// this -> Input_Ids[i] =
}
for (SizeType i=0; i<num_outputs; i++)
{
string str = output_names[i];
transform(str.begin(), str.end(), str.begin(), ::tolower);
this->output_names[i] = str;
// this -> output_Ids[i] =
}
return 0;
} // end Init(...)
virtual int Readin(string str)
{
//cerr<<"Error ["<<__FILE__<<","<<__LINE__<<"]: @ "<<this->name
// <<" Readin undefined"<<endl;
//return 1;
return Readin(this->type, str);
} // end Readin
int Readin(string type, string str)
{
stringstream sinput;
string name;
SizeType bit_width = 0, num_inputs = 0, num_outputs = 0;
sinput.str(str);
sinput >> name >> bit_width >> num_inputs >> num_outputs;
string* input_names = new string[num_inputs];
string* output_names = new string[num_outputs];
for (SizeType i=0; i<num_inputs; i++)
sinput >> input_names[i];
for (SizeType i=0; i<num_outputs; i++)
sinput >> output_names[i];
return Init(type, name, bit_width, num_inputs, num_outputs,
input_names, output_names);
}
virtual int Evaluate(ValueType *inputs, ValueType *outputs)
{
cerr<<"Error ["<<__FILE__<<","<<__LINE__<<"]: @ "<<this->name
<<" Evaluate undefined"<<endl;
return 2;
} // end Evaluate(...)
};
template<typename IdType, typename ValueType, typename SizeType>
class Connecter : public Module<IdType, ValueType, SizeType>
{
public:
Connecter() {
this->type = "Connecter";
this->max_inputs = 1;
this->max_outputs = -1;
}
int Evaluate(ValueType *inputs, ValueType *outputs)
{
for (SizeType i=0; i<this->num_outputs; i++)
outputs[i] = inputs[0];
return 0;
}
};
template<typename IdType, typename ValueType, typename SizeType>
class NotGate : public Module<IdType, ValueType, SizeType>
{
public:
NotGate() {
this->type = "NotGate";
this->max_inputs = 1;
}
int Evaluate(ValueType *inputs, ValueType *outputs)
{
outputs[0] = ~inputs[0];
return 0;
}
};
template<typename IdType, typename ValueType, typename SizeType>
class OrGate : public Module<IdType, ValueType, SizeType>
{
public:
OrGate() {
this->type = "OrGate";
this->min_inputs = 2;
}
int Evaluate(ValueType *inputs, ValueType *outputs)
{
ValueType temp=inputs[0];
for (SizeType i=1; i<this->num_inputs; i++)
temp = temp | inputs[i];
outputs[0] = temp;
return 0;
}
};
template<typename IdType, typename ValueType, typename SizeType>
class XorGate : public Module<IdType, ValueType, SizeType>
{
public:
XorGate() {
this->type = "XorGate";
this->min_inputs = 2;
}
int Evaluate(ValueType *inputs, ValueType *outputs)
{
ValueType temp=inputs[0];
for (SizeType i=1; i<this->num_inputs; i++)
temp = temp ^ inputs[i];
outputs[0] = temp;
return 0;
}
};
template<typename IdType, typename ValueType, typename SizeType>
class NorGate : public Module<IdType, ValueType, SizeType>
{
public:
NorGate() {
this->type = "NorGate";
this->min_inputs = 2;
}
int Evaluate(ValueType *inputs, ValueType *outputs)
{
ValueType temp=inputs[0];
for (SizeType i=1; i<this->num_inputs; i++)
temp = temp | inputs[i];
outputs[0] = ~temp;
return 0;
}
};
template<typename IdType, typename ValueType, typename SizeType>
class AndGate : public Module<IdType, ValueType, SizeType>
{
public:
AndGate() {
this->type = "AndGate";
this->min_inputs = 2;
}
int Evaluate(ValueType *inputs, ValueType *outputs)
{
ValueType temp=inputs[0];
for (SizeType i=1; i<this->num_inputs; i++)
temp = temp & inputs[i];
outputs[0] = temp;
return 0;
}
};
template<typename IdType, typename ValueType, typename SizeType>
class XandGate : public Module<IdType, ValueType, SizeType>
{
public:
XandGate() {
this->type = "XandGate";
this->min_inputs = 2;
}
int Evaluate(ValueType *inputs, ValueType *outputs)
{
ValueType temp=inputs[0];
for (SizeType i=1; i<this->num_inputs; i++)
temp = ~(temp ^ inputs[i]);
outputs[0] = temp;
return 0;
}
};
template<typename IdType, typename ValueType, typename SizeType>
class NandGate : public Module<IdType, ValueType, SizeType>
{
public:
NandGate() {
this->type = "NandGate";
this->min_inputs = 2;
}
int Evaluate(ValueType *inputs, ValueType *outputs)
{
ValueType temp=inputs[0];
for (SizeType i=1; i<this->num_inputs; i++)
temp = temp & inputs[i];
outputs[0] = ~temp;
return 0;
}
};
template<typename IdType, typename ValueType, typename SizeType>
class Adder : public Module<IdType, ValueType, SizeType>
{
public:
Adder() {
this->type = "adder";
this->min_inputs = 2;
}
int Evaluate(ValueType *inputs, ValueType *outputs)
{
ValueType temp=inputs[0];
for (SizeType i=1; i<this->num_inputs; i++)
temp += inputs[i];
outputs[0] = temp;
return 0;
}
};
template<typename IdType, typename ValueType, typename SizeType>
class Subtractor : public Module<IdType, ValueType, SizeType>
{
public:
Subtractor() {
this->type = "subtractor";
this->min_inputs = 2;
}
int Evaluate(ValueType *inputs, ValueType *outputs)
{
ValueType temp=inputs[0];
for (SizeType i=1; i<this->num_inputs; i++)
temp -= inputs[i];
outputs[0] = temp;
return 0;
}
};
template<typename IdType, typename ValueType, typename SizeType>
class Enabler : public Module<IdType, ValueType, SizeType>
{
public:
Enabler() {
this->type = "enabler";
this->min_inputs = 2;
this->max_inputs = 2;
}
int Evaluate(ValueType *inputs, ValueType *outputs)
{
if (inputs[0] == 0) outputs[0] = 0;
else outputs[0] = inputs[1];
return 0;
}
};
template<typename IdType, typename ValueType, typename SizeType>
class Mux : public Module<IdType, ValueType, SizeType>
{
public:
Mux() {
this->type = "mux";
this->min_inputs = 3;
}
int Evaluate(ValueType *inputs, ValueType *outputs)
{
ValueType sel=inputs[0];
outputs[0] = inputs[sel+1];
return 0;
}
};
template<typename IdType, typename ValueType, typename SizeType>
class Demux : public Module<IdType, ValueType, SizeType>
{
public:
Demux() {
this->type = "demux";
this->min_inputs = 2;
this->max_inputs = 2;
this->min_inputs = 2;
this->max_outputs = -1;
}
int Evaluate(ValueType *inputs, ValueType *outputs)
{
ValueType sel=inputs[0];
for (SizeType i=0; i<this->num_outputs; i++)
outputs[i] = 0;
outputs[sel+1] = inputs[1];
return 0;
}
};