-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdpgen_withCritPath.cpp
More file actions
1217 lines (1130 loc) · 35.9 KB
/
dpgen_withCritPath.cpp
File metadata and controls
1217 lines (1130 loc) · 35.9 KB
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// main.cpp
// 574
//
// Created by Kristopher Rockowitz on 10/25/19.
// Copyright © 2019 r0. All rights reserved.
//
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>
#include <stack>
using namespace std;
// inputs, outputs, registers, wires are of this type
class Variable {
public:
string name;
string type;
int bits;
// constructor
Variable(){
this->name = "";
this->type = "";
this->bits = 0;
}
Variable(string name, string type, int bits){
this->name = name;
this->type = type;
this->bits = bits;
}
};
// class that holds processed operations
class Operation {
public:
Variable result;
string equals;
Variable var1;
Variable var2;
Variable var3;
string op1;
string op2;
string name;
string type;
int bits;
Operation(){
this->result = Variable();
this->equals = "";
this->var1 = Variable();
this->var2 = Variable();
this->var3 = Variable();
this->op1 = "";
this->op2 = "";
this->name = "";
this->type = "";
this->bits = 0;
}
};
vector<string> ReadFile(string inputFile){
cout << "Reading File...\n";
vector<string> parsedFile;
ifstream netlistFile;
netlistFile.open(inputFile);
if(netlistFile){
string str;
while(getline(netlistFile, str)){
parsedFile.push_back(str);
}
}
else{
cout << "unable to read file" << endl;
}
cout << "Done reading file." << endl;
return parsedFile;
}
class Path {
public:
vector<Operation> operations;
Path(){
this->operations = {};
}
Path(vector<Operation> opers){
this->operations = opers;
}
};
// search single vector, 1 if founde, else 0
int findVariable(vector<Variable> in, string find){
int found = 0;
for(unsigned int i = 0; i < in.size(); i++){
if(in.at(i).name == find){
found = 1;
}
}
return found;
}
void printVector(vector<Operation> vec){
vector<Operation>::iterator x;
for(x = vec.begin(); x != vec.end(); x++){
cout << x->name + " ";
}
}
void printStringVector(vector<string> vec){
vector<string>::iterator x;
for(x = vec.begin(); x != vec.end(); x++){
cout << *x + " ";
}
}
string findComponent(vector<Operation> ops, string f){
string component = "";
vector<Operation>::iterator i;
for(i = ops.begin(); i != ops.end(); i++){
if(i->result.name == f){
component = i->name;
}
}
return component;
}
int findBits(vector<Operation> ops, string f){
int bits = 0;
vector<Operation>::iterator i;
for(i = ops.begin(); i != ops.end(); i++){
if(i->result.name == f){
bits = i->bits;
}
}
return bits;
}
float getLatency(string comp, int bit){
float latency = 0.0;
if(bit == 0){
cout << "ERROR: 0 bits latency input" << endl;
}
if(comp == "REG"){
if(bit == 1){
latency = 2.616;
}
else if(bit == 2){
latency = 2.644;
}
else if(bit == 8){
latency = 2.879;
}
else if(bit == 16){
latency = 3.061;
}
else if(bit == 32){
latency = 3.602;
}
else if(bit == 64){
latency = 3.966;
}
}
else if(comp == "ADD"){
if(bit == 1){
latency = 2.704;
}
else if(bit == 2){
latency = 3.713;
}
else if(bit == 8){
latency = 4.924;
}
else if(bit == 16){
latency = 5.638;
}
else if(bit == 32){
latency = 7.270;
}
else if(bit == 64){
latency = 9.566;
}
}
else if(comp == "SUB"){
if(bit == 1){
latency = 3.024;
}
else if(bit == 2){
latency = 3.412;
}
else if(bit == 8){
latency = 4.890;
}
else if(bit == 16){
latency = 5.569;
}
else if(bit == 32){
latency = 7.253;
}
else if(bit == 64){
latency = 9.566;
}
}
else if(comp == "MUL"){
if(bit == 1){
latency = 2.438;
}
else if(bit == 2){
latency = 3.651;
}
else if(bit == 8){
latency = 7.453;
}
else if(bit == 16){
latency = 7.811;
}
else if(bit == 32){
latency = 12.395;
}
else if(bit == 64){
latency = 15.354;
}
}
else if(comp == "COMP"){
if(bit == 1){
latency = 3.031;
}
else if(bit == 2){
latency = 3.934;
}
else if(bit == 8){
latency = 5.949;
}
else if(bit == 16){
latency = 6.256;
}
else if(bit == 32){
latency = 7.264;
}
else if(bit == 64){
latency = 8.416;
}
}
else if(comp == "MUX2x1"){
if(bit == 1){
latency = 4.083;
}
else if(bit == 2){
latency = 4.115;
}
else if(bit == 8){
latency = 4.815;
}
else if(bit == 16){
latency = 5.623;
}
else if(bit == 32){
latency = 8.079;
}
else if(bit == 64){
latency = 8.766;
}
}
else if(comp == "SHR"){
if(bit == 1){
latency = 3.644;
}
else if(bit == 2){
latency = 4.007;
}
else if(bit == 8){
latency = 5.178;
}
else if(bit == 16){
latency = 6.460;
}
else if(bit == 32){
latency = 8.819;
}
else if(bit == 64){
latency = 11.095;
}
}
else if(comp == "SHL"){
if(bit == 1){
latency = 3.614;
}
else if(bit == 2){
latency = 3.980;
}
else if(bit == 8){
latency = 5.152;
}
else if(bit == 16){
latency = 6.549;
}
else if(bit == 32){
latency = 8.565;
}
else if(bit == 64){
latency = 11.220;
}
}
else if(comp == "DIV"){
if(bit == 1){
latency = 0.619;
}
else if(bit == 2){
latency = 2.144;
}
else if(bit == 8){
latency = 15.439;
}
else if(bit == 16){
latency = 33.093;
}
else if(bit == 32){
latency = 86.312;
}
else if(bit == 64){
latency = 243.233;
}
}
else if(comp == "MOD"){
if(bit == 1){
latency = 0.758;
}
else if(bit == 2){
latency = 2.149;
}
else if(bit == 8){
latency = 16.078;
}
else if(bit == 16){
latency = 35.563;
}
else if(bit == 32){
latency = 88.142;
}
else if(bit == 64){
latency = 250.583;
}
}
else if(comp == "INC"){
if(bit == 1){
latency = 1.792;
}
else if(bit == 2){
latency = 2.218;
}
else if(bit == 8){
latency = 3.111;
}
else if(bit == 16){
latency = 3.471;
}
else if(bit == 32){
latency = 4.347;
}
else if(bit == 64){
latency = 6.200;
}
}
else if(comp == "DEC"){
if(bit == 1){
latency = 1.792;
}
else if(bit == 2){
latency = 2.218;
}
else if(bit == 8){
latency = 3.108;
}
else if(bit == 16){
latency = 3.701;
}
else if(bit == 32){
latency = 4.685;
}
else if(bit == 64){
latency = 6.503;
}
}
return latency;
}
void criticalPath(vector<Operation> ops, vector<Variable> outs, vector<Variable> regs){
// a little newline to separate other printing
cout << endl;
// vector to store the path
vector<string> path;
string critString = "";
float pathLatency = 0.0;
float compLatency = 0.0;
float critPath = 0.0;
// stack for outputs (nodes)
stack<string> stack;
string topNode;
// standard iterators for vectors
vector<Operation>::iterator i;
vector<Operation>::iterator j;
// Depth First Search (DFS)
// loop through all operations
for(i = ops.begin(); i != ops.end(); i++){
topNode = i->result.name;
// push topNode onto stack
stack.push(topNode);
// while the stack isn't empty, keep visiting nodes
while(!stack.empty()){
string letter = stack.top();
// prevent redundancy
if(letter != topNode){
path.push_back(letter);
}
string tempPath = "";
// if reached an output or register, done with iteration, print the path
if(findVariable(outs, letter) || findVariable(regs, letter)){
path.insert(path.begin(), topNode);
// printStringVector(path);
// cout << endl;
pathLatency = 0.0;
for(unsigned int i = 0; i < path.size(); i++){
// find the component
string comp = findComponent(ops, path.at(i));
// get the bits
int bits = findBits(ops, path.at(i));
// cout << "bits = " + to_string(bits) << endl;
// get the latency
compLatency = getLatency(comp, bits);
pathLatency += compLatency;
// print it out
tempPath += comp + " " + to_string(compLatency) + " ";
// cout << comp + " " << fixed << setprecision(3) << compLatency << " ";
}
// cout << "PATH LATENCY = " << fixed << setprecision(3) << pathLatency;
// cout << endl;
tempPath += "Total Latency = " + to_string(pathLatency);
path = {};
}
if(pathLatency * 1000 > critPath * 1000){
critString = tempPath;
critPath = pathLatency;
}
// pop node from stack
stack.pop();
// look for all new nodes which have the current node as an input
for(j = ops.begin(); j != ops.end(); j++){
if(letter == j->var1.name || letter == j->var2.name || letter == j->var3.name){
stack.push(j->result.name);
}
}
}
}
cout << critString << endl;
cout << "Critical Path : " << fixed << setprecision(3) << critPath << " ns" << endl << endl;
}
void PrintOperands(vector<Variable> &inputs, vector<Variable> &outputs, vector<Variable> &wires, vector<Variable> ®isters, vector<string>& operations){
cout << "Inputs:\n";
for(vector<Variable>::iterator i = inputs.begin(); i != inputs.end(); i++){
cout << distance(inputs.begin(), i)+1 << ". "<< (*i).name << " has bitwidth " << (*i).bits << "\n";
}
cout << "\nOutputs:\n";
for(vector<Variable>::iterator i = outputs.begin(); i != outputs.end(); i++){
cout << distance(outputs.begin(), i)+1 << ". "<< (*i).name << " has bitwidth " << (*i).bits << "\n";
}
cout << "\nWires:\n";
for(vector<Variable>::iterator i = wires.begin(); i != wires.end(); i++){
cout << distance(wires.begin(), i)+1 << ". "<< (*i).name << " has bitwidth " << (*i).bits << "\n";
}
cout << "\nRegisters:\n";
for(vector<Variable>::iterator i = registers.begin(); i != registers.end(); i++){
cout << distance(registers.begin(), i)+1 << ". "<< (*i).name << " has bitwidth " << (*i).bits << "\n";
}
cout << "\nOperations:\n";
for(vector<string>::iterator i = operations.begin(); i != operations.end(); i++){
cout << (*i) << "\n";
}
}
void ParseLine(vector<Variable>& inputs, vector<Variable>& outputs,vector<Variable>& wires, vector<Variable>& registers, vector<string>& operations, string line){
//parse into inputs, outputs, wires and operations
if(line.find("input")==0){
//input Int8 a, b, c
int BW = -1;
BW = atoi(&line[line.find("Int")+3]);
char U = line.at(line.find("Int")-1);
for(int i = line.find(' ',line.find("Int")); i< line.length(); i++){
if(line[i] == '/'){
if(line[i+1] == '/'){
i = line.length();
}
}
else if(line[i] != ' ' && line[i]!=',' && line[i] != '\t'){
Variable input;
int j = i;
while(line[j] != ',' && line[j] != '\0' && line[j]!= ' ' && line[j] != '\t'){
if((j-i)>0)
i = j;
input.name += line[j];
j++;
}
if(U == 'U'){
input.type = "u";
}
else{
input.type = "s";
}
input.bits = BW;
inputs.push_back(input);
}
}
}
else if(line.find("output")==0){
int BW = -1;
BW = atoi(&line[line.find("Int")+3]);
char U = line.at(line.find("Int")-1);
for(int i = line.find(' ',line.find("Int")); i< line.length(); i++){
if(line[i] == '/'){
if(line[i+1] == '/'){
i = line.length();
}
}
else if(line[i] != ' ' && line[i]!=',' && line[i] != '\t'){
Variable output;
int j = i;
while(line[j] != ',' && line[j] != '\0' && line[j]!= ' ' && line[j] != '\t'){
if((j-i)>0)
i = j;
output.name += line[j];
j++;
}
if(U == 'U'){
output.type = "u";
}
else{
output.type = "s";
}
output.bits = BW;
outputs.push_back(output);
}
}
}
else if(line.find("wire")==0){
int BW = -1;
BW = atoi(&line[line.find("Int")+3]);
char U = line.at(line.find("Int")-1);
for(int i = line.find(' ',line.find("Int")); i< line.length(); i++){
if(line[i] == '/'){
if(line[i+1] == '/'){
i = line.length();
}
}
else if(line[i] != ' ' && line[i]!=',' && line[i] != '\t'){
Variable wire;
int j = i;
while(line[j] != ',' && line[j] != '\0' && line[j]!= ' ' && line[j] != '\t'){
if((j-i)>0)// check to skip rest of whole words
i = j;
wire.name += line[j];
j++;
}
if(U == 'U' || BW == 1){
wire.type = "u";
}
else{
wire.type = "s";
}
wire.bits = BW;
wires.push_back(wire);
}
}
}
else if(line.find("register")==0){
int BW = -1;
BW = atoi(&line[line.find("Int")+3]);
char U = line.at(line.find("Int")-1);
for(int i = line.find(' ',line.find("Int")); i< line.length(); i++){
if(line[i] == '/'){
if(line[i+1] == '/'){
i = line.length();
}
}
else if(line[i] != ' ' && line[i]!=',' && line[i] != '\t'){
Variable reg;
int j = i;
while(line[j] != ',' && line[j] != '\0' && line[j]!= ' ' && line[j] != '\t'){
if((j-i)>0)// check to skip rest of whole words
i = j;
reg.name += line[j];
j++;
}
if(U == 'U' || BW == 1){
reg.type = "u";
}
else{
reg.type = "s";
}
reg.bits = BW;
registers.push_back(reg);
}
}
}
else if(line.find("=") != std::string::npos){
//vector<string> op;
//string word = "";
// for(int i = 0; line[i] != '\0'; i++){
// if(line[i+1] == '\0' && line[i] != ' '){
// word += line[i];
// op.push_back(word);
// word = "";
// }
// else if(line[i] != ' '){
// word += line[i];
// }
// else{
// if(word != "") op.push_back(word);
// word = "";
// }
// }
// if(line.find_first_of("/") != std::string::npos){
// line = line.substr(0,line.find_first_of("///")-1);
// }
operations.push_back(line);
}
}
// search single vector and return the Variable, else empty Variable
Variable getVariable(vector<Variable> in, string find){
Variable found;
for (std::vector<Variable>::iterator it = in.begin() ; it != in.end(); ++it){
if(it->name == find){
return *it;
}
}
return found;
}
// search all the vectors and return the Variable, else empty Variable
Variable searchVariables(vector<Variable> in, vector<Variable> out, vector<Variable> wire, vector<Variable> reg, string find){
std::vector<Variable>::iterator it;
for(it = in.begin(); it != in.end(); ++it){
if(it->name == find){
return *it;
}
}
for(it = out.begin(); it != out.end(); ++it){
if(it->name == find){
return *it;
}
}
for(it = wire.begin(); it != wire.end(); ++it){
if(it->name == find){
return *it;
}
}
for(it = reg.begin(); it != reg.end(); ++it){
if(it->name == find){
return *it;
}
}
Variable var;
return var;
}
Operation SignExtend(Operation o1){
// Sign Extend (signed and unsigned)
// var1
if(o1.var1.type == "s" && o1.var1.bits < o1.bits){
// calculate bit difference
int diff = o1.bits - o1.var1.bits;
// $signed({24'hFFFFFF, temp2[7:0]})
string nameSE = "$signed({" + to_string(diff) + "'b1, " + o1.var1.name + "[" + to_string(o1.var1.bits-1) + ":0]})";
o1.var1.name = nameSE;
}
else if(o1.var1.type == "u" && o1.var1.bits < o1.bits){
// calculate bit difference
int diff = o1.bits - o1.var1.bits;
// $signed({24'hFFFFFF, temp2[7:0]})
string nameSE = "{" + to_string(diff) + "'b0, " + o1.var1.name + "[" + to_string(o1.var1.bits-1) + ":0]})";
o1.var1.name = nameSE;
}
// var2
if(o1.var2.type == "s" && o1.var2.bits < o1.bits){
// calculate bit difference
int diff = o1.bits - o1.var2.bits;
// $signed({24'hFFFFFF, temp2[7:0]})
string nameSE = "$signed({" + to_string(diff) + "'b1, " + o1.var2.name + "[" + to_string(o1.var2.bits-1) + ":0]})";
o1.var2.name = nameSE;
}
else if(o1.var2.type == "u" && o1.var2.bits < o1.bits){
// calculate bit difference
int diff = o1.bits - o1.var2.bits;
// $signed({24'hFFFFFF, temp2[7:0]})
string nameSE = "{" + to_string(diff) + "'b0, " + o1.var2.name + "[" + to_string(o1.var2.bits-1) + ":0]})";
o1.var2.name = nameSE;
}
// var3
if(o1.var3.type == "s" && o1.var3.bits < o1.bits){
// calculate bit difference
int diff = o1.bits - o1.var3.bits;
// $signed({24'hFFFFFF, temp2[7:0]})
string nameSE = "$signed({" + to_string(diff) + "'b1, " + o1.var3.name + "[" + to_string(o1.var3.bits-1) + ":0]})";
o1.var3.name = nameSE;
}
else if(o1.var3.type == "u" && o1.var3.bits < o1.bits){
// calculate bit difference
int diff = o1.bits - o1.var3.bits;
// $signed({24'hFFFFFF, temp2[7:0]})
string nameSE = "{" + to_string(diff) + "'b0, " + o1.var3.name + "[" + to_string(o1.var3.bits-1) + ":0]})";
o1.var3.name = nameSE;
}
return o1;
}
// if input too large, take the lsb
Operation LSB(Operation o1){
if(o1.var1.bits > o1.bits){
// calculate the difference
int diff = o1.var1.bits - o1.bits;
string nameLSB = o1.var1.name + "[" + to_string(diff-1) + ":0]";
o1.var1.name = nameLSB;
}
if(o1.var2.bits > o1.bits){
// calculate the difference
int diff = o1.var2.bits - o1.bits;
string nameLSB = o1.var2.name + "[" + to_string(diff-1) + ":0]";
o1.var2.name = nameLSB;
}
if(o1.var3.bits > o1.bits){
// calculate the difference
int diff = o1.var3.bits - o1.bits;
string nameLSB = o1.var3.name + "[" + to_string(diff-1) + ":0]";
o1.var3.name = nameLSB;
}
return o1;
}
// write header
void writeHeader(ofstream &file){
cout << "`timescale 1ns / 1ps \n" << endl;
file << "`timescale 1ns / 1ps \n" << endl;
}
// write module
void writeModule(string name, vector<Variable> in, vector<Variable> out, ofstream &file){
cout << "module " << name << "(clk, rst, ";
file << "module " << name << "(clk, rst, ";
std::vector<Variable>::iterator it;
for ( it = in.begin() ; it != in.end(); ++it){
cout << it->name << ", ";
file << it->name << ", ";
}
for (it = out.begin() ; it != out.end(); ++it){
if(it == out.end()-1){
cout << it->name;
file << it->name;
}
else{
cout << it->name << ", ";
file << it->name << ", ";
}
}
cout << ");" << endl << endl;
file << ");" << endl << endl;
}
// write inputs
void writeInputs(vector<Variable> var, ofstream &file){
cout << "input clk, rst;" << endl;
file << "input clk, rst;" << endl;
for (std::vector<Variable>::iterator it = var.begin() ; it != var.end(); ++it){
cout << "input ";
file << "input ";
if(it->type == "s"){
cout << "signed ";
file << "signed ";
}
if(it->bits > 1){
cout << "[" << it->bits - 1 << ":0] " << it->name << ";" << endl;
file << "[" << it->bits - 1 << ":0] " << it->name << ";" << endl;
}
else{
cout << it->name << ";" << endl;
file << it->name << ";" << endl;
}
}
}
// write outputs
void writeOutputs(vector<Variable> var, ofstream &file){
for (std::vector<Variable>::iterator it = var.begin() ; it != var.end(); ++it){
cout << "output ";
file << "output ";
if(it->type == "s"){
cout << "signed ";
file << "signed ";
}
if(it->bits > 1){
cout << "[" << it->bits - 1 << ":0] " << it->name << ";" << endl;
file << "[" << it->bits - 1 << ":0] " << it->name << ";" << endl;
}
else{
cout << it->name << ";" << endl;
file << it->name << ";" << endl;
}
}
}
// write wires
void writeWires(vector<Variable> var, ofstream &file){
for (std::vector<Variable>::iterator it = var.begin() ; it != var.end(); ++it){
cout << "wire ";
file << "wire ";
if(it->type == "s"){
cout << "signed ";
file << "signed ";
}
if(it->bits > 1){
cout << "[" << it->bits - 1 << ":0] " << it->name << ";" << endl;
file << "[" << it->bits - 1 << ":0] " << it->name << ";" << endl;
}
else{
cout << it->name << ";" << endl;
file << it->name << ";" << endl;
}
}
}
// write registers
void writeRegisters(vector<Variable> var, ofstream &file){
for (std::vector<Variable>::iterator it = var.begin() ; it != var.end(); ++it){
cout << "register ";
file << "register ";
if(it->type == "s"){
cout << "signed ";
file << "signed ";
}
if(it->bits > 1){
cout << "[" << it->bits - 1 << ":0] " << it->name << ";" << endl;
file << "[" << it->bits - 1 << ":0] " << it->name << ";" << endl;
}
else{
cout << it->name << ";" << endl;
file << it->name << ";" << endl;
}
}
}
// write operations
void writeOperations(vector<Operation> ops, ofstream &file){
cout << endl;
file << endl;
unsigned int i = 0;
for (std::vector<Operation>::iterator it = ops.begin() ; it != ops.end(); ++it){
// TODO: test this to ensure it still works
// if any input is signed, the operation is signed
if(it->result.type == "s" || it->var1.type == "s" || it->var2.type == "s" || it->var3.type == "s"){
it->type = "s";
string temp = "S";
it->name = temp + it->name;
}
else{
it->type = "u";
}
// TODO: test this to ensure it still works
// Sign Extend (signed and unsigned)
*it = SignExtend(*it);
// TODO: test this to ensure it still works
// if input to large, take the lsb
*it = LSB(*it);
// comp(a,b,gt,lt,eq)
cout << it->name << " #(.DATAWIDTH(" << it->bits << ")) " << it->name << i << "(";
file << it->name << " #(.DATAWIDTH(" << it->bits << ")) " << it->name << i << "(";
cout << it->var1.name << ", ";
file << it->var1.name << ", ";
if(it->name == "REG" || it->name == "SREG"){
cout << "clk, rst, ";
file << "clk, rst, ";
}
if(it->var2.name != ""){
cout << it->var2.name << ", ";
file << it->var2.name << ", ";
}
if(it->var3.name != ""){
cout << it->var3.name << ", ";
file << it->var3.name << ", ";
}
if(it->name == "COMP" || it->name == "SCOMP"){
if(it->op1 == ">"){
cout << it->result.name << ", 0" << ", 0" << ");" << endl;
file << it->result.name << ", 0" << ", 0" << ");" << endl;
}
else if(it->op1 == "<"){
cout << "0, " << it->result.name << ", 0" << ");" << endl;
file << "0, " << it->result.name << ", 0" << ");" << endl;
}
else if(it->op1 == "=="){
cout << "0, " << "0, " << it->result.name << ");" << endl;
file << "0, " << "0, " << it->result.name << ");" << endl;
}
}
else{
cout << it->result.name << ");" << endl;
file << it->result.name << ");" << endl;
}
++i;
}
cout << endl << "endmodule" << endl;
file << endl << "endmodule" << endl;
}
int maxInputBits(Operation op){
unsigned int max = 0;
if(op.var1.bits > max){
max = op.var1.bits;
}
if(op.var2.bits > max){
max = op.var2.bits;
}
if(op.var3.bits > max){
max = op.var3.bits;
}
return max;
}
// process the string operations and return the Operation vector
vector<Operation> processOperation(vector<string> operations, vector<Variable> inputs, vector<Variable> outputs, vector<Variable> wires, vector<Variable> registers, bool* validCircuit){
vector<Operation> temp;
// TODO: move this shit to a function
// create a stringstream to parse the elements of the operation line string
// iterate through the operation vector
for(unsigned int i = 0; i < operations.size() ; i++){
stringstream opstream;
string result, equals, var1, op1, var2, op2, var3;
// throw the line into the stream
opstream << operations.at(i);
// stream into the variables
opstream >> result >> equals >> var1 >> op1 >> var2 >> op2 >> var3;
// create new operation with parsed operators
Operation o1;
o1.equals = equals;
o1.op1 = op1;
o1.op2 = op2;
// discover component and assign to name
if(op1 == "")
o1.name = "REG";
else if(op1 == "+" && var2 == "1")
o1.name = "INC";
else if(op1 == "+")
o1.name = "ADD";
else if(op1 == "-" && var2 == "1")
o1.name = "DEC";
else if(op1 == "-")
o1.name = "SUB";
else if(op1 == "*")
o1.name = "MUL";
else if(op1 == ">" || op1 == "<" || op1 == "==")
o1.name = "COMP";
else if(op1 == "?" && op2 == ":")
o1.name = "MUX2x1";
else if(op1 == ">>")
o1.name = "SHR";
else if(op1 == "<<")
o1.name = "SHL";
else if(op1 == "/")
o1.name = "DIV";
else if(op1 == "%")
o1.name = "MOD";
else if(op1 == "/")