-
Notifications
You must be signed in to change notification settings - Fork 0
/
ALU.js
86 lines (75 loc) · 2.51 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
class ALU extends Register { // ALU REGISTER
constructor(name) {
super(name)
this.instruction = "."
this.input0 = 0
this.input1 = 0
this.colour = [61, 36, 76]
}
update() { // once per frame
// draw outside box
noStroke()
fill(this.colour)
rect(this.x, this.y, this.w, this.h)
// draw inner boxes
fill(39, 23, 49)
rect(this.x + this.w / 8, this.y + this.h / 2.5, this.w * 0.25, this.w * 0.11)
rect(this.x + this.w / 2 - this.w * 0.065, this.y + this.h / 2.5, this.w * 0.13, this.w * 0.11)
rect(this.x + this.w / 1.6, this.y + this.h / 2.5, this.w * 0.25, this.w * 0.11)
rect(this.x + this.w * 0.3, this.y + this.h / 1.5, this.w * 0.4, this.w * 0.11)
// draw text
fill(255)
textAlign(CENTER, TOP)
text(this.name, this.x + this.w / 2, this.y + this.h / 10)
text(addZeros(this.input0), this.x + this.w / 4, this.y + this.h / 2.5)
text(this.instruction, this.x + this.w / 2, this.y + this.h / 2.5)
text(addZeros(this.input1), this.x + this.w / 1.33, this.y + this.h / 2.5)
text(addZeros(this.value), this.x + this.w / 2, this.y + this.h / 1.5)
}
calculate() { // ran when 2 values need to have arithmetic/logic performed on them
// arithmetic
let result = 0
if (this.instruction === "+") {
result = this.input0 + this.input1
}
else if (this.instruction === "-") {
result = this.input0 - this.input1
}
// overflow
if (result > 999) {
result = -999 + result - 1000
}
if (result < -999) {
result = 999 + result + 1000
}
this.value = result
// logic operations
if (this.instruction === "=") {
if (this.input0 === 0) {
this.value = "TRUE"
animCon.animations.push([true, cu.copyAddressTo, cir, pc]) // copy cir to pc
} else {
this.value = "FALSE"
}
} else if (this.instruction === ">=") {
if (this.input0 >= 0) {
this.value = "TRUE"
animCon.animations.push([true, cu.copyAddressTo, cir, pc]) // copy cir to pc
} else {
this.value = "FALSE"
}
}
}
updateInput0(reg) { // update left input
this.input0 = reg.value
}
updateInput1(reg) { // update right inputt
this.input1 = reg.value
}
clear() { // when program ran
super.clear()
this.instruction = "."
this.input0 = 0
this.input1 = 0
}
}