This repository has been archived by the owner on Sep 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ALU.js
151 lines (129 loc) · 2.92 KB
/
ALU.js
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
function ALU() {
// BitVector
this.op0 = undefined;
this.op1 = undefined;
this.ctrl = undefined;
// Wire
this.outWire = undefined;
this.zeroWire = undefined;
this.result = undefined;
this.actionCounter = 0;
}
ALU.prototype.receiveOp0 = function(op) {
if(op.length() != 32) {
console.log(op)
throw "ALU input not valid."
}
this.op0 = op;
this.next();
}
ALU.prototype.receiveOp1 = function(op) {
if(op.length() != 32) {
console.log(op)
throw "ALU input not valid."
}
this.op1 = op;
this.next();
}
ALU.prototype.receiveCtrl = function(op) {
if((op.length() != 3) || (op.equals([0,1,1]))) {
console.log(op)
throw "ALU control input not valid"
}
this.ctrl = op;
this.next();
}
ALU.prototype.next = function() {
this.actionCounter++;
if(this.actionCounter != 3) {
return;
}
console.log("ALU is doing serious busines.")
console.log(this)
switch(this.ctrl.toString()) {
case "000":
this.and();
break;
case "001":
this.or();
break;
case "010":
this.add();
break;
case "110":
this.sub();
break;
}
this.actionCounter = 0;
}
/**
* On this.ctrl == [0,0,0] this.outWire is
* sent the logical "and" of the two operands.
*/
ALU.prototype.and = function() {
this.result = "";
for(var i = 0; i < 31; i++) {
this.result = Math.min(this.op0.get(i), this.op1.get(i)) + this.result;
}
this.outWire.receive(new BitVector(this.result));
}
/**
* On this.ctrl == [0,0,1] this.outWire is
* sent the logical "or" of the two operands.
*/
ALU.prototype.or = function() {
this.result = "";
for(var i = 0; i < 31; i++) {
this.result = Math.max(this.op0.get(i), this.op1.get(i)) + this.result;
}
this.outWire.receive(new BitVector(this.result));
}
/**
* On this.ctrl == [0,1,0] this.outWire is
* sent the sum of the two operands.
*/
ALU.prototype.add = function() {
// just cheating :)
var op0int = this.op0.toInt();
var op1int = this.op1.toInt();
var result = (op0int + op1int).toString(2).split('');
this.outWire.receive(result);
}
/**
* On this.ctrl == [1,0,0] nothing happens.
*/
ALU.prototype.andn = function() {
}
/**
* On this.ctrl == [1,0,0] nothing happens.
*/
ALU.prototype.orn = function() {
}
/**
* On this.ctrl == [1,1,0] this.outWire is
* set to "this.op0 - this.op1" and this.zeroWire
* is sent accordingly.
*/
ALU.prototype.sub = function() {
// just cheating :)
var op0int = this.op0.toInt();
var op1int = this.op1.toInt();
var result = (op0int - op1int).toString(2).split('');
this.outWire.receive(result);
}
/**
* On this.ctrl == [1,1,1] this.outWire is
* sent [0,..,0,1] if op0 < op1,
* else [0,...,0]
*/
ALU.prototype.lt = function() {
// just cheating :)
var op0int = bitVectorToValue(op0);
var op1int = bitVectorToValue(op1)
if(op0int < op1int) {
var result = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
} else {
var result = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
}
this.outWire.receive(result);
}