-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
166 lines (152 loc) · 5.79 KB
/
app.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
$(document).ready(function () {
var uiFetchedInstructions = [];
// Pre instructions
var source = "LD F6 34 R2\r\nLD F2 45 R3\r\nMULT F0 F2 F4\r\n";
source += "SUBD F8 F6 F2\r\nDIVD F10 F0 F6\r\nADDD F6 F8 F2";
$("#new_instructions").val(source);
// New simulator controller
var controller = new Controller([]);
var timeID = 0;
uiRunSimulate();
$("#continue").click(function() {
window.clearTimeout(timeID);
timeID = window.setTimeout(uiRunSimulate, 2000);
});
$("#pause").click(function() {
window.clearTimeout(timeID);
});
$("#load").click(function () {
// Load the new instructions
var new_instructions = $("#new_instructions").val().split("\n");
try {
new_instructions.forEach(function (instruction) {
if (instruction == "") {
return;
}
var elements = instruction.split(" ");
if (elements.length != 4) {
throw BreakException;
}
if (controller.isInInstructionSet(elements[0])) {
controller.addInstruction(new Instruction(elements[0],
elements[1], elements[2], elements[3]));
} else {
throw BreakException;
}
});
$("#new_instructions").val("");
var all_instructions = controller.getNotExecInctruction().map(
function (obj) {
return '<div style="border: 1px solid">' + obj.getSource() + "</div>";
}
);
var str = all_instructions.join("");
$("#all_instructions").html(str);
window.clearTimeout(timeID);
timeID = window.setTimeout(uiRunSimulate, 2000);
} catch(e) {
alert("error instruction");
}
});
// Forward the simulator and update the UI
function uiRunSimulate() {
if (controller.forward() ) {
timeID = window.setTimeout(uiRunSimulate, 2000);
}
uiUpdateInstructions(controller);
uiUpdtateFunctionUnit(controller);
uiUpdateRegister(controller);
var all_instructions = controller.getNotExecInctruction().map(
function (obj) {
return '<div style="border: 1px solid">' + obj.getSource() + "</div>";
}
);
var str = all_instructions.join("");
$("#all_instructions").html(str);
}
// Update the function unit UI
function uiUpdtateFunctionUnit(simulator) {
for (var key in simulator.functionUnitSet) {
var unit = simulator.functionUnitSet[key];
var row = $("#function_unit").find("#" + key);
if (row.length == 0) {
$("#function_unit").append(uiMakeUnitRow(unit));
} else {
uiSetUnitStage(row, unit.exec);
for (var element in unit) {
var temp = row.find("." + element);
if (temp.length != 0) {
if (temp.html() != unit[element] + "") {
temp.html(unit[element] + "");
}
}
}
}
}
}
function uiMakeUnitRow (unit) {
var temp = '<tr id="' + unit.name + '">';
temp += '<td class="name">' + unit.name + "</td>";
temp += '<td class="busy">' + unit.busy + "</td>";
temp += '<td class="Op">' + unit.Op + "</td>";
temp += '<td class="Fi">' + unit.Fi + "</td>";
temp += '<td class="Fj">' + unit.Fj + "</td>";
temp += '<td class="Fk">' + unit.Fk + "</td>";
temp += '<td class="Qj">' + unit.Qj + "</td>";
temp += '<td class="Qk">' + unit.Qk + "</td>";
temp += '<td class="Rj">' + unit.Rj + "</td>";
temp += '<td class="Rk">' + unit.Rk + "</td>";
temp += "</tr>";
return temp;
}
function uiSetUnitStage (row, exec) {
switch (exec) {
case 0:
row.css("background-color", "#FFFFFF");
break;
case 1:
row.css("background-color", "#449D44");
break;
case 2:
row.css("background-color", "#31B0D5");
break;
case 3:
row.css("background-color", "#EC971F");
break;
case 4:
row.css("background-color", "#D9534F");
break;
}
}
// Update the register UI
function uiUpdateRegister(simulator) {
for (var key in simulator.registers) {
var row = $("#" + key);
if (row.html() != simulator.registers[key].manipulation) {
row.html(simulator.registers[key].manipulation);
}
}
}
// Update the Instructions UI
function uiUpdateInstructions(simulator) {
var rows = $("#instruction_table").find("tr");
for (var i = 0; i < simulator.fetched.length; i ++) {
var instruction = simulator.fetched[i];
if (i >= rows.length) {
var temp = "<tr>";
temp += "<td>" + instruction.getSource() + "</td>";
for (var j = 0; j < instruction.stage.length; j ++) {
temp += "<td>" + instruction.stage[j] + "</td>";
}
temp += "</tr>";
$("#instruction_table").append(temp);
} else {
var cols = $(rows[i]).find("td");
$(cols[0]).html(instruction.getSource());
for (var j = 0; j < instruction.stage.length; j ++) {
$(cols[j + 1]).html(instruction.stage[j]);
}
}
}
}
});