-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualizer.js
More file actions
434 lines (376 loc) · 11.6 KB
/
visualizer.js
File metadata and controls
434 lines (376 loc) · 11.6 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
document.addEventListener("DOMContentLoaded", function () {
let currentCycle = 0;
let isPlaying = false;
let playInterval = null;
const cpuDiagram = document.getElementById("cpu-diagram");
const cycleDisplay = document.getElementById("cycle-display");
const prevButton = document.getElementById("prev");
const nextButton = document.getElementById("next");
const playPauseButton = document.getElementById("play-pause");
const speedSelect = document.getElementById("speed");
const instructionInfo = document.getElementById("instruction-info");
const controlSignals = document.getElementById("control-signals");
const registerView = document.getElementById("register-view");
const memoryView = document.getElementById("memory-view");
// main drawing
const svg = d3
.select("#cpu-diagram")
.append("svg")
.attr("width", "100%")
.attr("height", "100%")
.attr("viewBox", "0 0 800 600");
function drawCPU() {
// PC
svg
.append("rect")
.attr("class", "component")
.attr("id", "pc")
.attr("x", 50)
.attr("y", 120)
.attr("width", 80)
.attr("height", 60);
svg
.append("text")
.attr("class", "component-text")
.attr("x", 90)
.attr("y", 150)
.text("PC");
// Instructions
svg
.append("rect")
.attr("class", "component")
.attr("id", "imem")
.attr("x", 200)
.attr("y", 100)
.attr("width", 100)
.attr("height", 100);
svg
.append("text")
.attr("class", "component-text")
.attr("x", 250)
.attr("y", 150)
.text("I-Mem");
// registers
svg
.append("rect")
.attr("class", "component")
.attr("id", "regfile")
.attr("x", 200)
.attr("y", 300)
.attr("width", 100)
.attr("height", 120);
svg
.append("text")
.attr("class", "component-text")
.attr("x", 250)
.attr("y", 360)
.text("Registers");
// ALU
svg
.append("rect")
.attr("class", "component")
.attr("id", "alu")
.attr("x", 400)
.attr("y", 280)
.attr("width", 120)
.attr("height", 120);
svg
.append("text")
.attr("class", "component-text")
.attr("x", 460)
.attr("y", 340)
.text("ALU");
// Data MEm
svg
.append("rect")
.attr("class", "component")
.attr("id", "dmem")
.attr("x", 600)
.attr("y", 300)
.attr("width", 100)
.attr("height", 120);
svg
.append("text")
.attr("class", "component-text")
.attr("x", 650)
.attr("y", 360)
.text("D-Mem");
// Control Unit
svg
.append("rect")
.attr("class", "component")
.attr("id", "control")
.attr("x", 400)
.attr("y", 480)
.attr("width", 120)
.attr("height", 80);
svg
.append("text")
.attr("class", "component-text")
.attr("x", 460)
.attr("y", 520)
.text("Control");
// Draw data paths
drawDataPaths();
// control signals
drawControlSignals();
addLabels();
}
function addLabels() {
svg
.append("text")
.attr("class", "path-label")
.attr("x", 150)
.attr("y", 130)
.text("Fetch");
svg
.append("text")
.attr("class", "path-label")
.attr("x", 250)
.attr("y", 230)
.text("Decode");
svg
.append("text")
.attr("class", "path-label")
.attr("x", 350)
.attr("y", 300)
.text("Execute");
svg
.append("text")
.attr("class", "path-label")
.attr("x", 530)
.attr("y", 330)
.text("Memory");
svg
.append("text")
.attr("class", "path-label")
.attr("x", 560)
.attr("y", 380)
.text("Write Back");
}
function drawDataPaths() {
drawPath("pc-to-imem", "M130,150 H200", false);
drawPath("imem-to-regfile", "M250,200 V300", false);
drawPath("imem-to-control", "M250,200 V520 H400", false);
drawPath("regfile-to-alu1", "M300,320 H400", false);
drawPath("regfile-to-alu2", "M300,360 H400", false);
drawPath("alu-to-dmem", "M520,340 H600", false);
drawPath("dmem-to-regfile", "M600,400 H350 V420 H250 V420", false);
drawPath("alu-to-regfile", "M460,400 V450 H180 V360", false);
drawPath("branch-path", "M400,320 H380 V240 H90 V150", false);
}
function drawControlSignals() {
drawPath("ctrl-reg_write", "M410,480 V420 H180 V330", true);
drawPath("ctrl-alu_src", "M460,480 V430", true);
drawPath("ctrl-mem_write", "M500,480 V460 H650 V420", true);
drawPath("ctrl-mem_read", "M510,480 V470 H660 V420", true);
drawPath("ctrl-mem_to_reg", "M440,480 V440 H350 V400", true);
drawPath("ctrl-branch", "M400,490 H350 V220 H90 V170", true);
}
function drawPath(id, path, isControl) {
svg
.append("path")
.attr("id", id)
.attr("class", isControl ? "control-signal" : "data-path")
.attr("d", path);
}
function updateDisplay() {
if (!cpuData || !cpuData.cycles || cpuData.cycles.length === 0) {
console.error("No CPU data available");
return;
}
const cycleData = cpuData.cycles[currentCycle];
cycleDisplay.textContent = `Cycle: ${currentCycle + 1} of ${
cpuData.cycles.length
}`;
// updates pc instrucion
updateInstructionInfo(cycleData);
// control sigs
updateControlSignals(cycleData);
updateRegisters(cycleData);
updateMemory(cycleData);
highlightActivePaths(cycleData);
prevButton.disabled = currentCycle === 0;
nextButton.disabled = currentCycle === cpuData.cycles.length - 1;
}
function updateInstructionInfo(cycleData) {
let instructionText = `<div>PC: 0x${cycleData.pc
.toString(16)
.padStart(8, "0")}</div>`;
instructionText += `<div>Instruction: 0x${cycleData.instruction
.toString(16)
.padStart(8, "0")}</div>`;
if (cycleData.decodedInstruction) {
instructionText += `<div class="decoded">Executing: ${cycleData.decodedInstruction}</div>`;
}
instructionInfo.innerHTML = instructionText;
}
function updateControlSignals(cycleData) {
const signals = cycleData.controlSignals || {};
let signalsText = "";
for (const [key, value] of Object.entries(signals)) {
signalsText += `<div class="signal ${value ? "active" : "inactive"}">
${key}: <span class="signal-value">${value ? "1" : "0"}</span>
</div>`;
}
controlSignals.innerHTML = signalsText;
}
function updateRegisters(cycleData) {
const registers = cycleData.registers || [];
let registersText = "";
for (let i = 0; i < registers.length; i++) {
const value = registers[i];
const isChanged =
cycleData.changedRegisters && cycleData.changedRegisters.includes(i);
registersText += `<div class="register ${
isChanged ? "register-changed" : ""
}">
<span>x${i}</span>
<span>${value} [0x${value
.toString(16)
.padStart(16, "0")}]</span>
</div>`;
}
registerView.innerHTML = registersText;
}
function updateMemory(cycleData) {
const memory = cycleData.memory || [];
let memoryText = "";
for (let base = 0; base < Math.min(256, memory.length); base += 8) {
let hasNonZero = false;
for (let i = 0; i < 8; i++) {
if (base + i < memory.length && memory[base + i] !== 0) {
hasNonZero = true;
break;
}
}
if (hasNonZero || base < 32 || [32, 40].includes(base)) {
memoryText += `<div class="memory-group">`;
memoryText += `<div class="memory-address">Address [${base
.toString(16)
.padStart(2, "0")}h - ${(base + 7)
.toString(16)
.padStart(2, "0")}h]:</div>`;
for (let i = 0; i < 8; i++) {
const addr = base + i;
if (addr < memory.length) {
const value = memory[addr] || 0;
const isChanged =
cycleData.changedMemory && cycleData.changedMemory.includes(addr);
memoryText += `<div class="memory-byte ${
isChanged ? "register-changed" : ""
}">
<span>mem[${addr}] = ${value} [0x${value
.toString(16)
.padStart(2, "0")}]</span>
</div>`;
}
}
memoryText += `</div>`;
}
}
memoryView.innerHTML = memoryText;
}
function highlightActivePaths(cycleData) {
d3.selectAll(".data-path").classed("active", false);
d3.selectAll(".control-signal").classed("active", false);
if (cycleData.instruction) {
const opcode = cycleData.instruction & 0x7f;
// these are always active on all instructoins
d3.select("#pc-to-imem").classed("active", true);
d3.select("#imem-to-regfile").classed("active", true);
d3.select("#imem-to-control").classed("active", true);
d3.select("#regfile-to-alu1").classed("active", true);
// R-type instruction
if (opcode === 0x33) {
// 0110011
d3.select("#regfile-to-alu2").classed("active", true);
d3.select("#alu-to-regfile").classed("active", true);
}
// I-type instruction (addi)
else if (opcode === 0x13) {
// 0010011
d3.select("#alu-to-regfile").classed("active", true);
}
// I-type instruction (ld)
else if (opcode === 0x03) {
// 0000011
d3.select("#alu-to-dmem").classed("active", true);
d3.select("#dmem-to-regfile").classed("active", true);
}
// S-type instruction (sd)
else if (opcode === 0x23) {
// 0100011
d3.select("#regfile-to-alu2").classed("active", true);
d3.select("#alu-to-dmem").classed("active", true);
}
// B-type instruction (beq)
else if (opcode === 0x63) {
// 1100011
d3.select("#regfile-to-alu2").classed("active", true);
d3.select("#branch-path").classed("active", true);
}
}
const signals = cycleData.controlSignals || {};
for (const [key, value] of Object.entries(signals)) {
if (value) {
d3.select(`#ctrl-${key}`).classed("active", true);
}
}
}
prevButton.addEventListener("click", function () {
if (currentCycle > 0) {
currentCycle--;
updateDisplay();
}
});
nextButton.addEventListener("click", function () {
if (currentCycle < cpuData.cycles.length - 1) {
currentCycle++;
updateDisplay();
}
});
playPauseButton.addEventListener("click", function () {
if (isPlaying) {
clearInterval(playInterval);
playPauseButton.textContent = "Play";
isPlaying = false;
} else {
playInterval = setInterval(function () {
if (currentCycle < cpuData.cycles.length - 1) {
currentCycle++;
updateDisplay();
} else {
clearInterval(playInterval);
playPauseButton.textContent = "Play";
isPlaying = false;
}
}, parseInt(speedSelect.value));
playPauseButton.textContent = "Pause";
isPlaying = true;
}
});
speedSelect.addEventListener("change", function () {
if (isPlaying) {
clearInterval(playInterval);
playInterval = setInterval(function () {
if (currentCycle < cpuData.cycles.length - 1) {
currentCycle++;
updateDisplay();
} else {
clearInterval(playInterval);
playPauseButton.textContent = "Play";
isPlaying = false;
}
}, parseInt(speedSelect.value));
}
});
drawCPU();
if (typeof cpuData !== "undefined") {
updateDisplay();
} else {
console.error("CPU data not loaded!");
instructionInfo.innerHTML =
"<div class='error'>Error: CPU data not available.</div>";
}
});