-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
166 lines (142 loc) · 3.8 KB
/
index.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
var pixels = [];
var computer;
function convertColor(smallColor)
{
if(smallColor instanceof String || typeof(smallColor) === "string")
{
if(/^#[0-9A-F]$/.test(smallColor))
{
smallColor = parseInt(smallColor.split("#")[1], 16);
}
else
{
throw new Error("Bad color");
}
}
var R = smallColor >> 3;
var G = (smallColor >> 1) & 0b11;
var B = smallColor & 0b1;
var hexR = R * 0xFF;
var hexG = (G / 3) * 0xFF;
var hexB = B * 0xFF;
var finalHex = (hexR << 16) + (hexG << 8) + hexB;
return "#" + finalHex.toString(16).padStart(6, 0);
}
function Pixel(element)
{
this.color = 0b0000;
this.element = element;
this.update = function()
{
this.element.style.backgroundColor = convertColor(this.color);
}
}
function setOutput(str)
{
$("#output").text(str);
}
function setInstructions()
{
var temp = $("#codePanel").val().split("\n");
computer.instructions = [];
for(var i = 0; i < temp.length; i++)
{
if(temp[i].length != 0 && !temp[i].startsWith("#") && !/^\s+$/.test(temp[i]))
computer.instructions.push(temp[i]);
}
}
function run()
{
computer.setRegister(PC, 0);
computer.running = false;
computer.fastMode = $("#fastMode").is(":checked");
console.log(computer.fastMode);
setInstructions();
computer.running = true;
computer.run();
}
function stop()
{
computer.running = false;
}
function step()
{
computer.step();
}
function cont()
{
computer.running = true;
computer.run();
}
function updateMemory()
{
var bank = computer.memory[parseInt($("#memSelect").val())];
var text = "";
for(var i = 0; i < 16; i++)
{
text += "0x" + i.toString(16).toUpperCase() + ": " + bank[i].toString(2).padStart(4, 0) + "<br />";
}
$("#memory").html(text);
}
function updateScreen()
{
for(var i = 0; i < 16; i++)
{
pixels[i].color = computer.memory[15][i];
pixels[i].update();
}
}
function updateStatus()
{
$("#AVal").text(computer.registers.A.toString(2).padStart(4, 0));
$("#BVal").text(computer.registers.B.toString(2).padStart(4, 0));
$("#CVal").text(computer.registers.C.toString(2).padStart(4, 0));
$("#DVal").text(computer.registers.D.toString(2).padStart(4, 0));
$("#EVal").text(computer.registers.E.toString(2).padStart(4, 0));
$("#FVal").text(computer.registers.F.toString(2).padStart(4, 0));
$("#GVal").text(computer.registers.G.toString(2).padStart(4, 0));
$("#HVal").text(computer.registers.H.toString(2).padStart(4, 0));
$("#MBVal").text(computer.registers.MB.toString(2).padStart(4, 0));
$("#PCVal").text(computer.registers.PC.toString(2).padStart(8, 0));
$("#PC2Val").text(computer.registers.PC2.toString(2).padStart(8, 0));
$("#SPVal").text(computer.registers.SP.toString(2).padStart(4, 0));
$("#SBVal").text(computer.registers.SB.toString(2).padStart(4, 0));
$("#FLAGSVal").text(computer.registers.FLAGS.toString(2).padStart(4, 0));
$("#HIGHVal").text(computer.registers.HIGH.toString(2).padStart(4, 0));
$("#LOWVal").text(computer.registers.LOW.toString(2).padStart(4, 0));
}
function updateBreakPoints()
{
computer.breakPoints = [];
var breakPointArr = $("#breakPoints").val().split("\n");
for(var i = 0; i < breakPointArr.length; i++)
{
computer.breakPoints.push(parseInt(breakPointArr[i]));
}
}
function initElements()
{
var screenPanelDim = screen.width * 0.3;
$("#screenPanel").width(screenPanelDim);
$("#screenPanel").height(screenPanelDim);
for(var i = 0; i < 16; i++)
{
var pixelElement = document.createElement("div");
pixelElement.classList.add("screenPixel");
pixelElement.style.width = (screenPanelDim / 4) + "px";
pixelElement.style.height = (screenPanelDim / 4) + "px";
$("#screenPanel").append(pixelElement);
var newPixel = new Pixel(pixelElement);
pixels.push(newPixel);
}
computer = new Computer();
setInterval(() => {
updateStatus();
updateScreen();
updateMemory();
updateBreakPoints();
}, 100);
}
$(() => {
initElements();
});