-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
622 lines (594 loc) · 14.8 KB
/
Copy pathmain.cpp
File metadata and controls
622 lines (594 loc) · 14.8 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
/*
* main.cpp
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <time.h>
using namespace std;
#define NAND 1
#define AND 2
#define NOT 3
#define NOR 4
#define OR 5
#define XOR 6
#define BUFF 7
#define RandomTestDelay 1000
char fileName[50];
bool getValueOfInputLine(int _lineNum);
bool calcGateOutput(int _gateType, bool value_one, bool value_two);
struct GATE
{
int GateType;
int No_Of_Inputs;
vector<int> Input_Nodes;
int Output_Node;
int Output_Value;
bool Output_Ready;
};
// store first 5 lines of input benchmark file
char Bench_Name[50];
int Bench_Inputs;
int Bench_Outputs;
int Bench_Inverter;
int Bench_TotalGates;
struct InputLine
{
int lineNumber;
bool Value;
bool First_Input;
};
struct StuckAtLine
{
int lineNumber;
bool StuckAtValue;
};
vector<InputLine*> InputVector;
vector<int> OutputVector;
vector<StuckAtLine*> StuckAtVector;
vector<GATE*> GateCkt;
bool getValueOfInputLine(int _lineNum)
{
for(vector<InputLine*>::iterator it=InputVector.begin();it!=InputVector.end();++it)
{
if((*it)->lineNumber == _lineNum)
return (*it)->Value;
}
return 0;
}
bool isLineFirstInput(int _lineNum)
{
for(vector<InputLine*>::iterator it=InputVector.begin();it!=InputVector.end();++it)
{
if((*it)->lineNumber == _lineNum && (*it)->First_Input)
return true;
}
return false;
}
void setValueOfInputLine(int _lineNum, bool _value)
{
for(vector<InputLine*>::iterator it=InputVector.begin();it!=InputVector.end();++it)
{
if((*it)->lineNumber == _lineNum)
(*it)->Value=_value;
}
}
bool isLineStuckAt(int _lineNum)
{
for(vector<StuckAtLine*>::iterator it=StuckAtVector.begin();it!=StuckAtVector.end();++it)
{
if((*it)->lineNumber == _lineNum)
return true;
}
return false;
}
bool getLineStuckAtValue(int _lineNum)
{
bool errorFlag=0;
for(vector<StuckAtLine*>::iterator it=StuckAtVector.begin();it!=StuckAtVector.end();++it)
{
if((*it)->lineNumber == _lineNum)
{
errorFlag=1;
return (*it)->StuckAtValue;
}
}
if(!errorFlag)
{
cerr<<"\n\nError/bug in Stuck-At Logic\n";
}
return errorFlag;
}
void generateRandomInputVector()
{
srand ( time(NULL) );
for(vector<InputLine*>::iterator it=InputVector.begin();it!=InputVector.end();++it)
{
if((*it)->First_Input)
{
int ranValue = rand() % 10 + 1;
(*it)->Value = ranValue & 0x0001;
}
}
usleep( RandomTestDelay * 1000 );
}
void processGatesWithInput(int option)
{
for(vector<GATE*>::iterator it=GateCkt.begin();it!=GateCkt.end();++it)
{
if(option==6)
cout<<"\n* Intermediate:\t\tGate: "<<(*it)->Output_Node;
int tempLine=(*(*it)->Input_Nodes.begin());
int tempVal=getValueOfInputLine(tempLine);
if((*it)->No_Of_Inputs==1)
{
if(option==6)
{
cout<<" #Inputs:"<<(*it)->No_Of_Inputs<<" in:"<<tempLine;
cout<<" GateType: "<<(*it)->GateType;
cout<<" outVal:"<<calcGateOutput( (*it)->GateType, getValueOfInputLine(tempLine),getValueOfInputLine(tempLine) );
}
if(isLineStuckAt((*it)->Output_Node))
{
setValueOfInputLine((*it)->Output_Node, getLineStuckAtValue((*it)->Output_Node));
}
else
setValueOfInputLine((*it)->Output_Node, calcGateOutput((*it)->GateType, getValueOfInputLine(tempLine),getValueOfInputLine(tempLine)));
}
else if((*it)->No_Of_Inputs>1)
{
if(option==6)
cout<<" #Inputs:"<<(*it)->No_Of_Inputs<<" GateType:"<<(*it)->GateType;
for(vector<int>::iterator inpt=++((*it)->Input_Nodes.begin()); inpt!=(*it)->Input_Nodes.end();inpt++)
{
tempVal = calcGateOutput( (*it)->GateType, tempVal, getValueOfInputLine(*inpt) );
if(isLineStuckAt((*it)->Output_Node))
{
setValueOfInputLine((*it)->Output_Node, getLineStuckAtValue((*it)->Output_Node));
}
else
setValueOfInputLine( (*it)->Output_Node, tempVal);
if((*it)->GateType==NAND || (*it)->GateType==NOR)
tempVal=(tempVal+1)%2;
tempLine = (*inpt);
if(option==6)
cout<<" outVal:"<<getValueOfInputLine((*it)->Output_Node);
}
}
}
}
void printOutputVector()
{
cout<<"\nOUTPUT: ";
for(vector<InputLine*>::iterator it=InputVector.begin();it!=InputVector.end();++it)
{
for(vector<int>::iterator i=OutputVector.begin();i!=OutputVector.end();++i)
{
if((*i)==(*it)->lineNumber)
cout<<(*it)->Value;
}
}
cout<<"\n";
}
void printInputVector()
{
cout<<"\nINPUT: ";
for(vector<InputLine*>::iterator it=InputVector.begin();it!=InputVector.end();++it)
{
if((*it)->First_Input)
cout<<(*it)->Value;
}
}
void readBenchMarkFile(int fileReadFlag)
{
char line[100];
char *ptr;
int outLine;
int i=0,k=10;
bool flag=false;
FILE* benchFile;
if(!fileReadFlag)
{
cout<<"\nAvailable benchmark files:\n \
c17.bench, c432.bench, c499.bench, c880.bench, c1355.bench,\n \
c1980.bench, c2670.bench, c3540.bench, c5315.bench, c6288.bench, c7552.bench";
cout<<"\n\nEnter filename>> ";
cin>>fileName;
cout<<"\n\nf: "<<fileName;
}
benchFile = fopen(fileName,"r");
cout<<"\n\ndone here";
if (benchFile == NULL)
{
cerr<<"No such benchmark file\n\n";
}
InputVector.clear();
OutputVector.clear();
StuckAtVector.clear();
GateCkt.clear();
// read input benchmark file
while(fgets(line, sizeof line, benchFile)!=NULL)
{
static int count=0;
static int countState=0;
/*
* count: (" 0, 1, 2, 3 ")
* countState: 4 types of data in input file("#..., INPUTS(*)..., OUTPUTS(*)..., Connections...")
*/
if(countState==3)
{
if(count)
{
GATE *gate = new GATE();
// find Inputs
while(line[i]!='\n')
{
if(line[i]=='(')
{
char *temp = new char[5];
while(line[i]!=',')
{
if(line[i]==')')
{
flag=true;
break;
}
temp[k++]=line[i];
i++;
}
temp[0]='0';
gate->Input_Nodes.push_back(atoi(temp));
}
else if(line[i]==' ' && line[i-1]==',')
{
char *temp = new char[5];
while(line[i]!=',')
{
if(line[i]==')')
{
flag=true;
break;
}
temp[k++]=line[i];
i++;
}
temp[0]='0';
gate->Input_Nodes.push_back(atoi(temp));
}
k=0;
i++;
if(flag==true)
{
break;
}
}
flag=false;
i=0;
// example: 10 = NAND(1, 3)
// find output line
ptr = strtok(line, "=");
outLine=atoi(ptr);
gate->Output_Node=outLine;
InputLine *inputLine = new InputLine();
inputLine->Value=false;
inputLine->First_Input=false;
inputLine->lineNumber=outLine;
InputVector.push_back(inputLine);
// find GATE
ptr = strtok(NULL, " \t\n\r()=,");
if(!strcmp(ptr,"NAND"))
{
gate->GateType=NAND;
}
else if(!strcmp(ptr, "AND"))
{
gate->GateType=AND;
}
else if(!strcmp(ptr, "NOT"))
{
gate->GateType=NOT;
}
else if(!strcmp(ptr, "NOR"))
{
gate->GateType=NOR;
}
else if(!strcmp(ptr, "OR"))
{
gate->GateType=OR;
}
else if(!strcmp(ptr, "XOR"))
{
gate->GateType=XOR;
}
else if(!strcmp(ptr, "BUFF"))
{
gate->GateType=BUFF;
}
gate->Output_Value=false;
gate->Output_Ready=false;
gate->No_Of_Inputs=gate->Input_Nodes.size();
GateCkt.push_back(gate);
}
count++;
}
/*
* scan Outputs
*/
else if(countState==2)
{
if(count)
{
ptr = strtok(line, "OUTPUT()");
OutputVector.push_back(atoi(ptr));
}
count++;
if(count==Bench_Outputs+1)
{
count=0;
countState=3;
}
}
/*
* scan Inputs
*/
else if(countState==1)
{
if(count)
{
ptr = strtok(line, "INPUT()");
InputLine *inputLine = new InputLine();
inputLine->Value=false;
inputLine->First_Input=true;
inputLine->lineNumber=atoi(ptr);
InputVector.push_back(inputLine);
}
count++;
if(count==Bench_Inputs+1)
{
count=0;
countState=2;
}
}
else if(countState==0)
{
if(line[0]=='#')
{
ptr = strtok(line, "#");
if(count==0)
strcpy(Bench_Name, ptr);
if(count==1)
Bench_Inputs=atoi(ptr);
if(count==2)
Bench_Outputs=atoi(ptr);
if(count==3)
Bench_Inverter=atoi(ptr);
if(count==4)
Bench_TotalGates=atoi(ptr);
count++;
if(count==5)
{
cout<<"\n*************\n";
cout<<"\nBenchmark Name:"<<Bench_Name<<"\n#_Inputs:"<<Bench_Inputs<<"\n#_Outputs:" \
<<Bench_Outputs<<"\n#_Inverter:"<<Bench_Inverter<<"\n#_Gates:"<<Bench_TotalGates<<endl;
cout<<"\n*************\n";
count=0;
countState=1;
}
}
}
}
// done reading input benchmark file
cout<<"\n\nINPUTS:";
for(vector<InputLine*>::iterator it=InputVector.begin();it!=InputVector.end();++it)
{
cout<<" "<<(*it)->lineNumber;
}
cout<<"\n\nOUTPUTS:";
for(vector<int>::iterator it=OutputVector.begin();it!=OutputVector.end();++it)
cout<<" "<<*it;
fclose(benchFile);
}
int main(int argc, char** argv)
{
if(argc<2)
readBenchMarkFile(0);
else
{
strcpy(fileName, argv[1]);
cout<<"\n "<<fileName;
readBenchMarkFile(1);
}
int option=0;
while(1)
{
cout<<"\n\n*******************************************************************";
cout<<"\n* *";
cout<<"\n* Available options: *"
"\n* 1) Provide Input Vector *"
"\n* 2) Set Input Vector *"
"\n* 3) Reset Input Vector *"
"\n* 4) Print Input Vector *"
"\n* 5) Process Gates With Input Vector *"
"\n* 6) Process Gates With Input Vector And Print Line Values *"
"\n* 7) Print Output Vector *"
"\n* 8) Print Number of Gates/Nodes *"
"\n* 9) Set Stuck-At-Lines *"
"\n* 10) Print Stuck-At-Lines *"
"\n* 11) Clear Stuck-At-Lines (fault free circuit) *"
"\n* 12) Random Input Vector, Generate-> Process-> Print *"
"\n* 13) Provide New Benchmark File *"
"\n* 14) Quit *";
cout<<"\n* *";
cout<<"\n*******************************************************************";
cout<<"\n\nEnter option>> ";
cin>>option;
if(option==1 || option==2 || option==3 || option==4)
{
/*
* Get input vector from user
*/
for(vector<InputLine*>::iterator it=InputVector.begin();it!=InputVector.end();++it)
{
if((*it)->First_Input)
{
if(option==1)
{
cout<<"\nLine:"<<(*it)->lineNumber<<" :";
cin>>(*it)->Value;
}
else if(option==3)
(*it)->Value=0;
else if(option==2)
(*it)->Value=1;
else if(option==4)
cout<<"\nLine: "<<(*it)->lineNumber<<" Value:"<<(*it)->Value;
}
}
}
else if(option==5 || option==6)
{
processGatesWithInput(option);
}
else if(option==7)
{
cout<<"\n\nProcessed Output Lines:";
cout<<"\n";
for(vector<InputLine*>::iterator it=InputVector.begin();it!=InputVector.end();++it)
{
for(vector<int>::iterator i=OutputVector.begin();i!=OutputVector.end();++i)
{
if((*i)==(*it)->lineNumber)
// cout<<"\n LineNo: "<<(*it)->lineNumber<<" Value:"<<(*it)->Value;
cout<<(*it)->Value;
}
}
}
else if(option==8)
{
cout<<"\n\nNumber of Nodes: "<<GateCkt.size();
cout<<"\n\n*************\n";
}
else if(option==9)
{
static int inptLine=0;
cout<<"\nEnter Stuck-At-Line numbers and their Values\nEnter Line number as '0' to escape";
while(1)
{
cout<<"\nLine Number>> ";
StuckAtLine* temp=new StuckAtLine();
cin>>inptLine;
if(!inptLine)
{
inptLine=0;
break;
}
else
temp->lineNumber=inptLine;
cout<<" Value>> ";
cin>>temp->StuckAtValue;
StuckAtVector.push_back(temp);
if(isLineFirstInput(inptLine))
{
cout<<", changing Value of first input line";
setValueOfInputLine(inptLine, temp->StuckAtValue);
}
}
cout<<"\n\nTotal Stuck-At-Lines: "<<StuckAtVector.size();
}
else if(option==10)
{
cout<<"\nStuck-At-Lines: ";
for(vector<StuckAtLine*>::iterator it=StuckAtVector.begin();it!=StuckAtVector.end();++it)
{
cout<<" "<<(*it)->lineNumber;
}
}
else if(option==11)
{
StuckAtVector.clear();
cout<<"\n\nClearing StuckAtVector, ";
cout<<"Total Stuck-At-Lines: "<<StuckAtVector.size();
}
else if(option==12)
{
cout<<"\n\nGenerating Random Input Vector";
for(int cnt=0; cnt<20; cnt++)
{
generateRandomInputVector();
printInputVector();
processGatesWithInput(5);
printOutputVector();
}
}
else if(option==13)
{
readBenchMarkFile(0);
}
else if(option==14)
{
cout<<"\n\nBye..!\n\n";
break;
}
else
cout<<"\nUnknown option..! Try again";
}
return 0;
}
bool calcGateOutput(int _gateType, bool value_one, bool value_two)
{
bool temp=0;
if(_gateType == NAND)
{
temp = value_one*value_two;
if(temp==0)
temp=1;
else
temp=0;
}
else if(_gateType == AND)
{
temp = value_one*value_two;
}
else if(_gateType == NOT)
{
if(value_two==false)
temp = true;
else
temp = false;
}
else if(_gateType == NOR)
{
temp = value_one+value_two;
if(temp==0)
temp=1;
else if(temp>=1)
temp=0;
}
else if(_gateType == OR)
{
temp = value_one+value_two;
if(temp==1 || temp==2)
temp=1;
else
temp=0;
}
else if(_gateType == XOR)
{
temp = value_one+value_two;
if(temp==2)
temp=0;
else if(temp==1)
temp=1;
else if(temp==0)
temp=0;
}
else if(_gateType == BUFF)
{
temp = value_two;
}
else
cerr<<"halt";
return temp;
}