-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
216 lines (185 loc) · 5.79 KB
/
main.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
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
import { CHIP8 } from './src/chip8.js';
window.debugMode = true;
window.loadWithManual = true;
window.chip8 = null;
document.getElementById('fileInput').addEventListener('change', function(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
const hexString = e.target.result;
if(!(window.chip8 == null || window.chip8.length === 0))
{
window.chip8.stopExecution()
delete window.chip8
}
window.chip8 = new CHIP8(hexString, "emulator-display", window.debugMode , window.debugMode );
window.chip8.manualPass = window.loadWithManual;
window.chip8.printMemory()
};
reader.readAsArrayBuffer(file);
}
});
function httpGetAsync(theUrl)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
const ret = xmlHttp.response
const hexString = ret;
if(!(window.chip8 == null || window.chip8.length === 0))
{
window.chip8.stopExecution()
delete window.chip8
}
window.chip8 = new CHIP8(hexString, "emulator-display", window.debugMode , window.debugMode );
window.chip8.manualPass = window.loadWithManual;
window.chip8.printMemory()
}
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.responseType = 'arraybuffer'; // Set response type to arraybuffer
xmlHttp.send(null);
}
window.selectProgram = function(elem)
{
const val = document.getElementsByTagName("select")[0].options[elem].value
console.log(val)
httpGetAsync(val)
}
function listenKeyboard()
{
setTimeout(function()
{
if(window.chip8 !== null)
{
document.getElementById("screen-tooltip").style.opacity = "1"
document.getElementById("screen-tooltip").innerHTML = "Used Keys: " + chip8.await;
}else
{
document.getElementById("screen-tooltip").style.opacity = "0"
}
listenKeyboard();
}, 10);
}
listenKeyboard()
var x, i, j, l, ll, selElmnt, a, b, c;
/* Look for any elements with the class "custom-select": */
x = document.getElementsByClassName("custom-select");
x.selectedIndex = 0;
l = x.length;
for (i = 0; i < l; i++) {
selElmnt = x[i].getElementsByTagName("select")[0];
selElmnt.selectedIndex = 0;
ll = selElmnt.length;
/* For each element, create a new DIV that will act as the selected item: */
a = document.createElement("DIV");
a.setAttribute("class", "select-selected");
a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
x[i].appendChild(a);
/* For each element, create a new DIV that will contain the option list: */
b = document.createElement("DIV");
b.setAttribute("class", "select-items select-hide");
for (j = 1; j < ll; j++) {
/* For each option in the original select element,
create a new DIV that will act as an option item: */
c = document.createElement("DIV");
c.innerHTML = selElmnt.options[j].innerHTML;
c.addEventListener("click", function(e) {
/* When an item is clicked, update the original select box,
and the selected item: */
var y, i, k, s, h, sl, yl;
s = this.parentNode.parentNode.getElementsByTagName("select")[0];
sl = s.length;
h = this.parentNode.previousSibling;
for (i = 0; i < sl; i++) {
if (s.options[i].innerHTML == this.innerHTML) {
s.selectedIndex = i;
h.innerHTML = this.innerHTML;
selectProgram(s.selectedIndex)
y = this.parentNode.getElementsByClassName("same-as-selected");
yl = y.length;
for (k = 0; k < yl; k++) {
y[k].removeAttribute("class");
}
this.setAttribute("class", "same-as-selected");
break;
}
}
h.click();
});
b.appendChild(c);
}
x[i].appendChild(b);
a.addEventListener("click", function(e) {
/* When the select box is clicked, close any other select boxes,
and open/close the current select box: */
e.stopPropagation();
closeAllSelect(this);
this.nextSibling.classList.toggle("select-hide");
this.classList.toggle("select-arrow-active");
});
}
function closeAllSelect(elmnt) {
/* A function that will close all select boxes in the document,
except the current select box: */
var x, y, i, xl, yl, arrNo = [];
x = document.getElementsByClassName("select-items");
y = document.getElementsByClassName("select-selected");
xl = x.length;
yl = y.length;
for (i = 0; i < yl; i++) {
if (elmnt == y[i]) {
arrNo.push(i)
} else {
y[i].classList.remove("select-arrow-active");
}
}
for (i = 0; i < xl; i++) {
if (arrNo.indexOf(i)) {
x[i].classList.add("select-hide");
}
}
}
/* If the user clicks anywhere outside the select box,
then close all select boxes: */
document.addEventListener("click", closeAllSelect);
window.changeClockSpeed = function(speed)
{
if(window.chip8 !== null)
{
window.chip8.CLOCK_SPEED = parseInt(speed);
}
}
window.nextInstruction = function()
{
if(window.chip8 !== null)
{
window.chip8.checkNextInstruction()
}
}
window.CheckManualPass = function(pass)
{
if(window.chip8 !== null)
{
window.chip8.manualPass = pass;
console.log(window.chip8.manualPass)
if(pass == false)
{
window.chip8.checkNextInstruction()
}
}else
{
window.loadWithManual = pass;
console.log(window.loadWithManual)
}
}
try
{
window.document.getElementById("manual_pass").checked = true;
}catch
{
window.loadWithManual = false;
window.debugMode = false;
}