-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemulate.js
255 lines (237 loc) · 7.71 KB
/
emulate.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
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
const ColecoAsync = require("./index");
const SoundDriver = require("./SoundDriver");
const biosUrl = require("url:./roms/bios.col");
const romUrl = require("url:./roms/dk.col");
const biosPromise = fetch(biosUrl, {cache: "force-cache"}).then((res) =>
res.arrayBuffer()
);
const romPromise = fetch(romUrl, {cache: "force-cache"}).then((res) =>
res.arrayBuffer()
);
ColecoAsync.then((ColecoInstance) => {
const Coleco = ColecoInstance.exports;
Promise.all([
biosPromise.then((bios) =>
Coleco.__pin(
Coleco.__newArray(Coleco.UINT8ARRAY_ID, new Uint8Array(bios))
)
),
romPromise.then((rom) =>
Coleco.__pin(Coleco.__newArray(Coleco.UINT8ARRAY_ID, new Uint8Array(rom)))
),
]).then(([bios, cartridge]) => {
console.log("Setup!");
const ctx = document.getElementById("coleco").getContext("2d");
ColecoInstance.refreshScreen = (videoData) => {
const data = new ImageData(videoData, 256, 192);
ctx.putImageData(data, 0, 0);
};
const regPC = document.getElementById("reg-pc");
const regA = document.getElementById("reg-a");
const regF = document.getElementById("reg-f");
const regBC = document.getElementById("reg-bc");
const regDE = document.getElementById("reg-de");
const regHL = document.getElementById("reg-hl");
const regIX = document.getElementById("reg-ix");
const regIY = document.getElementById("reg-iy");
const regI = document.getElementById("reg-i");
const regR = document.getElementById("reg-r");
const regSP = document.getElementById("reg-sp");
const emulator = Coleco.createEmulator(bios, cartridge);
Coleco.__unpin(bios);
Coleco.__unpin(cartridge);
const memoryPtr = Coleco.getMemory(emulator);
ColecoInstance.updateRegisters = (
pc,
a,
f,
bc,
de,
hl,
ix,
iy,
i,
r,
sp
) => {
regPC.textContent = pc.toString(16).padStart(4, 0) + "h";
regA.textContent = a.toString(16).padStart(2, 0) + "h";
regF.textContent = `${f & (1 << 7) ? "S" : "."}${
f & (1 << 6) ? "Z" : "."
}${f & (1 << 4) ? "H" : "."}${f & (1 << 2) ? "P" : "."}${
f & (1 << 1) ? "N" : "."
}${f & 1 ? "C" : "."}`;
// regF.textContent = f.toString(2).padStart(8, 0);
regBC.textContent = bc.toString(16).padStart(4, 0) + "h";
regDE.textContent = de.toString(16).padStart(4, 0) + "h";
regHL.textContent = hl.toString(16).padStart(4, 0) + "h";
regIX.textContent = ix.toString(16).padStart(4, 0) + "h";
regIY.textContent = iy.toString(16).padStart(4, 0) + "h";
regI.textContent = i.toString(16).padStart(2, 0) + "h";
regR.textContent = r.toString(16).padStart(2, 0) + "h";
regSP.textContent = sp.toString(16).padStart(4, 0) + "h";
};
const executing = document.getElementById("executing");
executing.addEventListener("change", () => {
SoundDriver.setMuted(!executing.checked);
blinkenlights.style.display = executing.checked ? "none" : "";
});
blinkenlights.style.display = executing.checked ? "none" : "";
function step(many) {
if (executing.checked) {
return Coleco.step(emulator, many);
} else {
return null;
}
}
const speed = (1 / 3580000) * 1000;
function stepMany(delay) {
setTimeout(() => {
const now = performance.now();
const stepAmount = step(true);
if (!stepAmount) {
return;
}
const amount = stepAmount * speed;
const delay = amount - (performance.now() - now);
// console.log("PC:", Coleco.pc(emulator).toString(16), delay);
stepMany(delay);
}, delay);
}
stepMany();
tickOnce.addEventListener("click", () => {
step(false);
});
tick.addEventListener("click", () => {
stepMany();
});
let updateTimeout = null;
function updateKeys() {
let arrowKeys = 0;
if (joyKeySet.has("ArrowLeft")) {
arrowKeys |= 0x800;
} else if (joyKeySet.has("ArrowRight")) {
arrowKeys |= 0x200;
}
if (joyKeySet.has("ArrowUp")) {
arrowKeys |= 0x100;
} else if (joyKeySet.has("ArrowDown")) {
arrowKeys |= 0x400;
}
console.log("Pushing keys!", state | arrowKeys, arrowKeys);
if (updateTimeout) {
clearTimeout(updateTimeout);
}
updateTimeout = setTimeout(
() => Coleco.controllerKeys(emulator, state | arrowKeys),
1000 / 120
);
}
let state = 0;
const keys = {
Digit0: 0x5,
Digit1: 0x2,
Digit2: 0x8,
Digit3: 0x3,
Digit4: 0xd,
Digit5: 0xc,
Digit6: 0x1,
Digit7: 0xa,
Digit8: 0xe,
Digit9: 0x4,
Minus: 0x6,
Equal: 0x9,
// Left fire
ControlLeft: 0x4000,
// Right fire
ControlRight: 0x0040,
};
const joyKeys = ["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"];
const joyKeySet = new Set();
window.addEventListener("keydown", (event) => {
if (keys[event.code]) {
if (!(state & keys[event.code])) {
state |= keys[event.code];
updateKeys();
}
} else if (joyKeys.includes(event.code)) {
if (!joyKeySet.has(event.code)) {
joyKeySet.add(event.code);
updateKeys();
}
}
});
window.addEventListener("keyup", (event) => {
if (keys[event.code]) {
if (state & keys[event.code]) {
state &= 0xffff - keys[event.code];
updateKeys();
}
} else if (joyKeys.includes(event.code)) {
if (joyKeySet.has(event.code)) {
joyKeySet.delete(event.code);
updateKeys();
}
}
});
const previewCtx = preview.getContext("2d");
window.check = function () {
const ids = new Set();
for (let x = 0; x < 32; ++x) {
for (let y = 0; y < 24; ++y) {
const id = Coleco.getPatternAt(emulator, x, y);
ids.add(id);
console.log("ID", id, x, y);
}
}
console.log("ID:", ids);
const spriteTablePtr = Coleco.getSprites(emulator);
const spriteTable = Coleco.__getUint8Array(spriteTablePtr);
return spriteTable;
};
window.draw = function (x, y) {
const id = Coleco.getPatternAt(emulator, x, y);
drawPattern(id);
};
window.drawPattern = function (id) {
console.log("Pattern is:", id);
const spriteTablePtr = Coleco.getSprites(emulator);
const spriteTable = Coleco.__getUint8Array(spriteTablePtr);
const data = new Uint8ClampedArray(8 * 8 * 4);
for (let y = 0; y < 8; ++y) {
let line = spriteTable[(id << 3) | y];
let x = 0;
for (let x = 0; x < 8; ++x) {
const isSet = (line >> x) & 1;
const value = isSet ? 255 : 0;
const base = x * 4 + y * 8 * 4;
data[base] = value;
data[base + 1] = value;
data[base + 2] = value;
data[base + 3] = 255;
}
console.log(line.toString(2).replace(/0/g, "."));
}
const imageData = new ImageData(data, 8, 8);
previewCtx.putImageData(imageData, 0, 0, 0, 0, 64, 64);
};
window.checkValues = function () {
return Coleco.__getArray(Coleco.debugLog(emulator));
};
const romElement = document.getElementById("rom");
romElement.addEventListener("change", () => {
const file = romElement.files[0];
if (file) {
file.arrayBuffer().then((arrayBuffer) => {
cartridge = Coleco.__pin(
Coleco.__newArray(Coleco.UINT8ARRAY_ID, new Uint8Array(arrayBuffer))
);
Coleco.updateROM(emulator, cartridge);
Coleco.reset(emulator);
const oldCart = cartridge;
Coleco.__unpin(oldCart);
});
}
});
});
});