blifutils 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,446 @@
1
+ /*
2
+ * Copyright (C) 2017 Théotime bollengier <theotime.bollengier@gmail.com>
3
+ *
4
+ * This file is part of Blifutils.
5
+ *
6
+ * Blifutils is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * Blifutils is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU General Public License
17
+ * along with Blifutils. If not, see <http://www.gnu.org/licenses/>.
18
+ *
19
+ */
20
+
21
+
22
+ #include <inttypes.h>
23
+ #include <iostream>
24
+ #include <cstdlib>
25
+
26
+
27
+ class Net
28
+ {
29
+ private:
30
+
31
+ int value; // 0, 1 or 2 for unknown
32
+ int nbFanoutComponentIndex;
33
+ int *fanoutComponentIndex;
34
+ bool *gateChanged;
35
+
36
+ public:
37
+
38
+ Net(int *fanoutIndexes, int fanoutIndexesLength, bool *gateChangedArray);
39
+ ~Net();
40
+
41
+ int getValue();
42
+ void setValue(int val);
43
+ };
44
+
45
+
46
+ class BitVector
47
+ {
48
+ /* For now we are doing with uint64_t, will see later for more than 64 bits */
49
+
50
+ private:
51
+
52
+ int width;
53
+ Net **nets;
54
+
55
+ public:
56
+
57
+ BitVector(Net **netArray, int bitwidth);
58
+ ~BitVector();
59
+
60
+ void setValue(int64_t val);
61
+ void setValue(uint64_t val);
62
+
63
+ uint64_t getValue(bool *valid);
64
+ int64_t getValueSigned(bool *valid);
65
+
66
+ int bitWidth();
67
+ };
68
+
69
+
70
+ class Latch
71
+ {
72
+ private:
73
+
74
+ Net *input;
75
+ Net *output;
76
+ int initValue;
77
+
78
+ public:
79
+
80
+ Latch(Net *inputNet, Net *outputNet, int initVal);
81
+
82
+ void reset();
83
+ void clock();
84
+ };
85
+
86
+
87
+ class Gate
88
+ {
89
+ private:
90
+
91
+ int nbInputs;
92
+ Net **inputs;
93
+ Net *output;
94
+ uint32_t *singleOutputCover;
95
+
96
+ public:
97
+
98
+ Gate(Net **inputNets, int nbinputs, Net *outputNet, uint32_t *singleoutputcover);
99
+ ~Gate();
100
+
101
+ void propagate();
102
+ };
103
+
104
+
105
+ class Model
106
+ {
107
+ private:
108
+
109
+ unsigned int nbNets;
110
+ unsigned int nbLatches;
111
+ unsigned int nbGates;
112
+
113
+ Net **nets;
114
+ Latch **latches;
115
+ Gate **gates;
116
+ bool *changed;
117
+
118
+ public:
119
+
120
+ Model(unsigned int nbNet, unsigned int nbLatche, unsigned int nbGate);
121
+ virtual ~Model();
122
+
123
+ void propagate();
124
+ void clock();
125
+ void cycle();
126
+ void reset();
127
+
128
+ protected:
129
+
130
+ virtual void setConstants() = 0;
131
+ void setNets(Net **netArr);
132
+ void setLatches(Latch **latchArr);
133
+ void setGates(Gate **gateArr);
134
+ void setChanges(bool *changeArr);
135
+ };
136
+
137
+
138
+ /******************************************************************************/
139
+
140
+
141
+ Net::Net(int *fanoutIndexes, int fanoutIndexesLength, bool *gateChangedArray) :
142
+ value(2),
143
+ nbFanoutComponentIndex(fanoutIndexesLength),
144
+ fanoutComponentIndex(fanoutIndexes),
145
+ gateChanged(gateChangedArray)
146
+ {
147
+ }
148
+
149
+
150
+ Net::~Net()
151
+ {
152
+ if (fanoutComponentIndex) {
153
+ delete fanoutComponentIndex;
154
+ }
155
+ }
156
+
157
+
158
+ int Net::getValue()
159
+ {
160
+ return value;
161
+ }
162
+
163
+
164
+ void Net::setValue(int val)
165
+ {
166
+ if (val != value) {
167
+ for (int i(0); i < nbFanoutComponentIndex; i++) {
168
+ gateChanged[fanoutComponentIndex[i]] = true;
169
+ }
170
+ }
171
+ value = val & 3;
172
+ }
173
+
174
+
175
+ /******************************************************************************/
176
+
177
+
178
+ BitVector::BitVector(Net **netArray, int bitwidth) :
179
+ width(bitwidth),
180
+ nets(netArray)
181
+ {
182
+ if (bitwidth > 64) {
183
+ std::cerr << "ERROR: For now bit vectors are limited to 64 bits, sorry" << std::endl;
184
+ exit(EXIT_FAILURE);
185
+ }
186
+ }
187
+
188
+
189
+ BitVector::~BitVector()
190
+ {
191
+ if (nets) {
192
+ delete nets;
193
+ }
194
+ }
195
+
196
+
197
+ void BitVector::setValue(uint64_t val)
198
+ {
199
+ uint64_t mask;
200
+
201
+ for (int i(0); i < width; i++) {
202
+ mask = (uint64_t)1ULL << i;
203
+ if ((val & mask) == 0) {
204
+ nets[i]->setValue(0);
205
+ } else {
206
+ nets[i]->setValue(1);
207
+ }
208
+ }
209
+ }
210
+
211
+
212
+ void BitVector::setValue(int64_t val)
213
+ {
214
+ uint64_t mask;
215
+
216
+ for (int i(0); i < width; i++) {
217
+ mask = (uint64_t)1ULL << i;
218
+ if ((val & mask) == 0) {
219
+ nets[i]->setValue(0);
220
+ } else {
221
+ nets[i]->setValue(1);
222
+ }
223
+ }
224
+ }
225
+
226
+
227
+ uint64_t BitVector::getValue(bool *valid)
228
+ {
229
+ uint64_t res(0);
230
+ int netVal;
231
+
232
+ if (valid != NULL) {
233
+ *valid = true;
234
+ }
235
+
236
+ for (int i(0); i < width; i++) {
237
+ netVal = nets[i]->getValue();
238
+ if (netVal == 2) {
239
+ if (valid != NULL) {
240
+ *valid = false;
241
+ }
242
+ return 0;
243
+ }
244
+ res |= (netVal << i);
245
+ }
246
+
247
+ return res;
248
+ }
249
+
250
+
251
+ int64_t BitVector::getValueSigned(bool *valid)
252
+ {
253
+ int64_t res(0);
254
+ unsigned int netVal;
255
+ int i;
256
+
257
+ if (valid != NULL) {
258
+ *valid = true;
259
+ }
260
+
261
+ for (i = 0; i < width; i++) {
262
+ netVal = nets[i]->getValue();
263
+ if (netVal == 2) {
264
+ if (valid != NULL) {
265
+ *valid = false;
266
+ }
267
+ return 0;
268
+ }
269
+ res |= ((uint64_t)netVal << (uint64_t)i);
270
+ }
271
+ if (nets[width-1]->getValue() == 1) {
272
+ for (i = width; i < 64; i++) {
273
+ res |= (1UL << (uint64_t)i);
274
+ }
275
+ }
276
+
277
+
278
+ return res;
279
+ }
280
+
281
+
282
+ int BitVector::bitWidth()
283
+ {
284
+ return width;
285
+ }
286
+
287
+
288
+ /******************************************************************************/
289
+
290
+
291
+ Latch::Latch(Net *inputNet, Net *outputNet, int initVal) :
292
+ input(inputNet),
293
+ output(outputNet),
294
+ initValue(initVal)
295
+ {
296
+ if (initValue != 0 && initValue != 1) {
297
+ initValue = 2;
298
+ }
299
+ }
300
+
301
+
302
+ void Latch::reset()
303
+ {
304
+ output->setValue(initValue);
305
+ }
306
+
307
+
308
+ void Latch::clock()
309
+ {
310
+ output->setValue(input->getValue());
311
+ }
312
+
313
+
314
+ /******************************************************************************/
315
+
316
+
317
+ Gate::Gate(Net **inputNets, int nbinputs, Net *outputNet, uint32_t *singleoutputcover) :
318
+ nbInputs(nbinputs),
319
+ inputs(inputNets),
320
+ output(outputNet),
321
+ singleOutputCover(singleoutputcover)
322
+ {
323
+ }
324
+
325
+
326
+ Gate::~Gate()
327
+ {
328
+ if (singleOutputCover) {
329
+ delete singleOutputCover;
330
+ }
331
+
332
+ if (inputs) {
333
+ delete inputs;
334
+ }
335
+ }
336
+
337
+
338
+ void Gate::propagate()
339
+ {
340
+ uint32_t addr(0);
341
+ uint32_t index(0);
342
+ uint32_t shift(0);
343
+ int val;
344
+
345
+ for (int i(0); i < nbInputs; i++) {
346
+ val = inputs[i]->getValue();
347
+ if (val == 2) {
348
+ output->setValue(2);
349
+ return;
350
+ }
351
+ addr |= (val << i);
352
+ }
353
+
354
+ shift = addr & 0x1f;
355
+ index = addr >> 5;
356
+
357
+ output->setValue((singleOutputCover[index] >> shift) & 1);
358
+ }
359
+
360
+
361
+ /******************************************************************************/
362
+
363
+
364
+ Model::Model(unsigned int nbNet, unsigned int nbLatche, unsigned int nbGate) :
365
+ nbNets(nbNet),
366
+ nbLatches(nbLatche),
367
+ nbGates(nbGate),
368
+ nets(NULL),
369
+ latches(NULL),
370
+ gates(NULL),
371
+ changed(NULL)
372
+ {
373
+ }
374
+
375
+
376
+ Model::~Model()
377
+ {
378
+ }
379
+
380
+
381
+ void Model::propagate()
382
+ {
383
+ for (unsigned int i(0); i < nbGates; i++) {
384
+ if (changed[i]) {
385
+ gates[i]->propagate();
386
+ changed[i] = false;
387
+ }
388
+ }
389
+ }
390
+
391
+
392
+ void Model::clock()
393
+ {
394
+ for (unsigned int i(0); i < nbLatches; i++) {
395
+ latches[i]->clock();
396
+ }
397
+ }
398
+
399
+
400
+ void Model::cycle()
401
+ {
402
+ propagate();
403
+ clock();
404
+ propagate();
405
+ }
406
+
407
+
408
+ void Model::reset()
409
+ {
410
+ unsigned int i;
411
+
412
+ for (i = 0; i < nbNets; i++) {
413
+ nets[i]->setValue(2);
414
+ }
415
+
416
+ for (i = 0; i < nbLatches; i++) {
417
+ latches[i]->reset();
418
+ }
419
+
420
+ setConstants();
421
+ }
422
+
423
+
424
+ void Model::setNets(Net **netArr)
425
+ {
426
+ nets = netArr;
427
+ }
428
+
429
+
430
+ void Model::setLatches(Latch **latchArr)
431
+ {
432
+ latches = latchArr;
433
+ }
434
+
435
+
436
+ void Model::setGates(Gate **gateArr)
437
+ {
438
+ gates = gateArr;
439
+ }
440
+
441
+
442
+ void Model::setChanges(bool *changeArr)
443
+ {
444
+ changed = changeArr;
445
+ }
446
+
@@ -0,0 +1,136 @@
1
+ /*
2
+ * Copyright (C) 2017 Théotime bollengier <theotime.bollengier@gmail.com>
3
+ *
4
+ * This file is part of Blifutils.
5
+ *
6
+ * Blifutils is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * Blifutils is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU General Public License
17
+ * along with Blifutils. If not, see <http://www.gnu.org/licenses/>.
18
+ *
19
+ */
20
+
21
+
22
+ #include <inttypes.h>
23
+ #include <iostream>
24
+ #include <cstdlib>
25
+
26
+
27
+ class Net
28
+ {
29
+ private:
30
+
31
+ int value; // 0, 1 or 2 for unknown
32
+ int nbFanoutComponentIndex;
33
+ int *fanoutComponentIndex;
34
+ bool *gateChanged;
35
+
36
+ public:
37
+
38
+ Net(int *fanoutIndexes, int fanoutIndexesLength, bool *gateChangedArray);
39
+ ~Net();
40
+
41
+ int getValue();
42
+ void setValue(int val);
43
+ };
44
+
45
+
46
+ class BitVector
47
+ {
48
+ /* For now we are doing with uint64_t, will see later for more than 64 bits */
49
+
50
+ private:
51
+
52
+ int width;
53
+ Net **nets;
54
+
55
+ public:
56
+
57
+ BitVector(Net **netArray, int bitwidth);
58
+ ~BitVector();
59
+
60
+ void setValue(int64_t val);
61
+ void setValue(uint64_t val);
62
+
63
+ uint64_t getValue(bool *valid);
64
+ int64_t getValueSigned(bool *valid);
65
+
66
+ int bitWidth();
67
+ };
68
+
69
+
70
+ class Latch
71
+ {
72
+ private:
73
+
74
+ Net *input;
75
+ Net *output;
76
+ int initValue;
77
+
78
+ public:
79
+
80
+ Latch(Net *inputNet, Net *outputNet, int initVal);
81
+
82
+ void reset();
83
+ void clock();
84
+ };
85
+
86
+
87
+ class Gate
88
+ {
89
+ private:
90
+
91
+ int nbInputs;
92
+ Net **inputs;
93
+ Net *output;
94
+ uint32_t *singleOutputCover;
95
+
96
+ public:
97
+
98
+ Gate(Net **inputNets, int nbinputs, Net *outputNet, uint32_t *singleoutputcover);
99
+ ~Gate();
100
+
101
+ void propagate();
102
+ };
103
+
104
+
105
+ class Model
106
+ {
107
+ private:
108
+
109
+ unsigned int nbNets;
110
+ unsigned int nbLatches;
111
+ unsigned int nbGates;
112
+
113
+ Net **nets;
114
+ Latch **latches;
115
+ Gate **gates;
116
+ bool *changed;
117
+
118
+ public:
119
+
120
+ Model(unsigned int nbNet, unsigned int nbLatche, unsigned int nbGate);
121
+ virtual ~Model();
122
+
123
+ void propagate();
124
+ void clock();
125
+ void cycle();
126
+ void reset();
127
+
128
+ protected:
129
+
130
+ virtual void setConstants() = 0;
131
+ void setNets(Net **netArr);
132
+ void setLatches(Latch **latchArr);
133
+ void setGates(Gate **gateArr);
134
+ void setChanges(bool *changeArr);
135
+ };
136
+