-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombinationalAnalysis.js
More file actions
236 lines (207 loc) · 8.37 KB
/
combinationalAnalysis.js
File metadata and controls
236 lines (207 loc) · 8.37 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
var inputSample = 5;
var dataSample=[['01---','11110','01---','00000'],['01110','1-1-1','----0'],['01---','11110','01110','1-1-1','0---0'],['----1']];
var sampleInputListNames=["A", "B"];
var sampleOutputListNames=["X"];
createCombinationalAnalysisPrompt=function(scope=globalScope){
console.log("Ya");
$('#combinationalAnalysis').empty();
$('#combinationalAnalysis').append("<p>Enter Input names separated by spaces: <input id='inputNameList' type='text' placeHolder='eg. A B C'></p>");
$('#combinationalAnalysis').append("<p>Enter Output names separated by spaces: <input id='outputNameList' type='text' placeHolder='eg. X Y Z'></p>");
$('#combinationalAnalysis').dialog({
width:"auto",
buttons: [
{
text: "Next",
click: function() {
// console.log($("#inputNameList"),$("#inputNameList").val(),$("#inputNameList").html());
var inputList=$("#inputNameList").val().split(' ');
var outputList=$("#outputNameList").val().split(' ');
$( this ).dialog( "close" );
createBooleanPrompt(inputList,outputList,scope);
},
}
]
});
}
function createBooleanPrompt(inputListNames,outputListNames,scope=globalScope){
inputListNames=inputListNames||(prompt("Enter inputs separated by space").split(' '));
outputListNames=outputListNames||(prompt("Enter outputs separated by space").split(' '));
var s='<table>'
s+='<tr>';
for(var i=0;i<inputListNames.length;i++)
s+='<th>'+inputListNames[i]+'</th>';
for(var i=0;i<outputListNames.length;i++)
s+='<th>'+outputListNames[i]+'</th>';
s+='</tr>';
var matrix = [];
for(var i=0; i<inputListNames.length; i++) {
matrix[i] = new Array((1<<inputListNames.length));
}
for(var i=0;i<inputListNames.length;i++){
for(var j=0;j<(1<<inputListNames.length);j++){
matrix[i][j]=(+((j&(1<<(inputListNames.length-i-1)))!=0));
}
}
for(var j=0;j<(1<<inputListNames.length);j++){
s+='<tr>';
for(var i=0;i<inputListNames.length;i++){
s+='<td>'+matrix[i][j]+'</td>';
}
for(var i=0;i<outputListNames.length;i++){
s+='<td class ="output '+outputListNames[i]+'" id="'+j+'">'+'x'+'</td>';
}
s+='</tr>';
}
s+='</table>';
console.log(s)
$('#combinationalAnalysis').empty()
$('#combinationalAnalysis').append(s)
$('#combinationalAnalysis').dialog({
width:"auto",
buttons: [
{
text: "Generate Circuit",
click: function() {
$( this ).dialog( "close" );
var data = generateBooleanTableData(outputListNames);
minmizedCircuit = [];
for(let output in data){
let temp = new BooleanMinimize(
inputListNames.length,
data[output][1].map(Number),
data[output]['x'].map(Number)
)
minmizedCircuit.push(temp.result);
}
// console.log(dataSample);
drawCombinationalAnalysis(minmizedCircuit,inputListNames,outputListNames,scope)
},
}
]
});
$('.output').click(function (){
var v=$(this).html();
if(v==0)v=$(this).html(1);
else if(v==1)v=$(this).html('x');
else if(v=='x')v=$(this).html(0);
})
}
function generateBooleanTableData(outputListNames){
var data={};
for(var i=0;i<outputListNames.length;i++){
data[outputListNames[i]]={
'x':[],
'1':[],
'0':[],
}
$rows=$('.'+outputListNames[i]);
for(j=0;j<$rows.length;j++){
console.log($rows[j].innerHTML)
data[outputListNames[i]][$rows[j].innerHTML].push($rows[j].id);
}
}
console.log(data);
return data;
}
function drawCombinationalAnalysis(combinationalData,inputList,outputListNames,scope=globalScope){
console.log(combinationalData);
var inputCount=inputList.length;
var maxTerms=0;
for(var i=0;i<combinationalData.length;i++)
maxTerms=Math.max(maxTerms,combinationalData[i].length);
var startPosX=200;
var startPosY=200;
var currentPosY=300;
var andPosX=startPosX+inputCount*40+40;
var orPosX=andPosX+Math.floor(maxTerms/2)*10+80;
var outputPosX=orPosX+60;
var inputObjects=[];
var logixNodes=[];
for(var i=0;i<inputCount;i++){
inputObjects.push(new Input(startPosX+i*40,startPosY,scope,"DOWN",1));
inputObjects[i].setLabel(inputList[i]);
inputObjects[i].newLabelDirection("UP");
var v1=new Node(startPosX+i*40,startPosY+20,2,scope.root);
inputObjects[i].output1.connect(v1);
var v2=new Node(startPosX+i*40+20,startPosY+20,2,scope.root);
v1.connect(v2);
var notG=new NotGate(startPosX+i*40+20, startPosY+40, scope, "DOWN", 1);
notG.inp1.connect(v2);
logixNodes.push(v1);
logixNodes.push(notG.output1);
}
function countTerm(s){
var c=0;
for(var i=0;i<s.length;i++)
if(s[i]!=='-')c++;
return c;
}
for(var i=0;i<combinationalData.length;i++){
// console.log(combinationalData[i]);
var andGateNodes=[];
for(var j=0;j<combinationalData[i].length;j++){
var c=countTerm(combinationalData[i][j]);
if(c>1){
var andGate=new AndGate(andPosX, currentPosY, scope, "RIGHT", c, 1);
andGateNodes.push(andGate.output1);
var misses=0;
for(var k=0;k<combinationalData[i][j].length;k++){
if(combinationalData[i][j][k]=='-'){misses++;continue;}
var index=2*k+(combinationalData[i][j][k]==0);
console.log(index);
console.log(andGate);
var v=new Node(logixNodes[index].absX(),andGate.inp[k-misses].absY(),2,scope.root);
logixNodes[index].connect(v);
logixNodes[index]=v;
v.connect(andGate.inp[k-misses]);
}
}
else{
for(var k=0;k<combinationalData[i][j].length;k++){
if(combinationalData[i][j][k]=='-')continue;
var index=2*k+(combinationalData[i][j][k]==0);
var andGateSubstituteNode= new Node(andPosX, currentPosY, 2,scope.root);
var v=new Node(logixNodes[index].absX(),andGateSubstituteNode.absY(),2,scope.root);
logixNodes[index].connect(v);
logixNodes[index]=v;
v.connect(andGateSubstituteNode);
andGateNodes.push(andGateSubstituteNode);
}
}
currentPosY+=c*10+30;
}
var andGateCount=andGateNodes.length;
var midWay=Math.floor(andGateCount/2);
var orGatePosY=(andGateNodes[midWay].absY()+andGateNodes[Math.floor((andGateCount-1)/2)].absY())/2;
if(andGateCount>1){
var o=new OrGate(orPosX,orGatePosY,scope,"RIGHT",andGateCount,1);
if(andGateCount%2==1)andGateNodes[midWay].connect(o.inp[midWay]);
for(var j=0;j<midWay;j++){
var v=new Node(andPosX+30+(midWay-j)*10,andGateNodes[j].absY(),2,scope.root);
v.connect(andGateNodes[j]);
var v2=new Node(andPosX+30+(midWay-j)*10,o.inp[j].absY(),2,scope.root);
v2.connect(v)
o.inp[j].connect(v2);
var v=new Node(andPosX+30+(midWay-j)*10,andGateNodes[andGateCount-j-1].absY(),2,scope.root);
v.connect(andGateNodes[andGateCount-j-1]);
var v2=new Node(andPosX+30+(midWay-j)*10,o.inp[andGateCount-j-1].absY(),2,scope.root);
v2.connect(v)
o.inp[andGateCount-j-1].connect(v2);
}
var out=new Output(outputPosX,o.y,scope,"LEFT",1);
out.inp1.connect(o.output1);
}
else{
var out=new Output(outputPosX,andGateNodes[0].absY(),scope,"LEFT",1);
out.inp1.connect(andGateNodes[0]);
}
out.setLabel(outputListNames[i]);
out.newLabelDirection("RIGHT");
}
for(var i=0;i<logixNodes.length;i++){
if(logixNodes[i].absY()!=currentPosY){
var v=new Node(logixNodes[i].absX(),currentPosY,2,scope.root);
logixNodes[i].connect(v)
}
}
}