-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
206 lines (175 loc) · 4.17 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
let c = el("output");
c.width = 512;
c.height = 480;
let ctx = c.getContext("2d");
let imgData = ctx.getImageData(0, 0, 512, 480);
let loopId = 0;
let loaded = false;
let paused = false;
let pausedInBg = false;
let romArr = new Uint8Array([]);
let snes = new Snes();
let audioHandler = new AudioHandler();
let logging = false;
let noPpu = false;
let controlsP1 = {
z: 0, // B
a: 1, // Y
shift: 2, // select
enter: 3, // start
arrowup: 4, // up
arrowdown: 5, // down
arrowleft: 6, // left
arrowright: 7, // right
x: 8, // A
s: 9, // X
d: 10, // L
c: 11 // R
}
// Função para carregar o ROM a partir de um ArrayBuffer
function loadRomFromArrayBuffer(buffer) {
let romArr = new Uint8Array(buffer);
loadRom(romArr); // Supondo que loadRom seja uma função definida em outro lugar
}
// Carrega o arquivo main.smc diretamente
fetch('./main.smc').then(response => {
if (response.ok) {
response.arrayBuffer().then(buffer => {
loadRomFromArrayBuffer(buffer);
});
} else {
console.error('Não foi possível carregar o arquivo main.smc');
}
}).catch(error => {
console.error('Erro ao buscar o arquivo main.smc:', error);
});
el("pause").onclick = function() {
if(paused && loaded) {
loopId = requestAnimationFrame(update);
audioHandler.start();
paused = false;
el("pause").textContent = "Pause";
} else {
cancelAnimationFrame(loopId);
audioHandler.stop();
paused = true;
el("pause").textContent = "Continue";
}
}
el("reset").onclick = function(e) {
snes.reset(false);
}
el("hardreset").onclick = function(e) {
snes.reset(true);
}
el("runframe").onclick = function(e) {
if(loaded) {
runFrame();
}
}
el("ishirom").onchange = function(e) {
if(loaded) {
// reload when switching from LoROM to HiROM
loadRom(romArr);
}
}
document.onvisibilitychange = function(e) {
if(document.hidden) {
pausedInBg = false;
if(!paused && loaded) {
el("pause").click();
pausedInBg = true;
}
} else {
if(pausedInBg && loaded) {
el("pause").click();
pausedInBg = false;
}
}
}
function loadRom(rom) {
let hiRom = el("ishirom").checked;;
if(snes.loadRom(rom, hiRom)) {
snes.reset(true);
if(!loaded && !paused) {
loopId = requestAnimationFrame(update);
audioHandler.start();
}
loaded = true;
}
}
function runFrame() {
if(logging) {
do {
snes.cycle();
// TODO: some way of tracing the spc again
// if((snes.xPos % 20) === 0 && snes.apu.spc.cyclesLeft === 0) {
// log(getSpcTrace(
// snes.apu.spc, snes.apu.cycles
// ));
// }
} while(
snes.cpuCyclesLeft > 0 ||
(snes.xPos >= 536 && snes.xPos < 576) ||
snes.hdmaTimer > 0
);
log(getTrace(
snes.cpu, snes.frames * 1364 * 262 + snes.yPos * 1364 + snes.xPos
));
} else {
snes.runFrame(noPpu);
}
snes.setPixels(imgData.data);
ctx.putImageData(imgData, 0, 0);
snes.setSamples(audioHandler.sampleBufferL, audioHandler.sampleBufferR, audioHandler.samplesPerFrame);
audioHandler.nextBuffer();
}
function update() {
runFrame();
loopId = requestAnimationFrame(update);
}
window.onkeydown = function(e) {
switch(e.key) {
case "l":
case "L": {
logging = !logging;
break;
}
case "p":
case "P": {
noPpu = !noPpu;
break;
}
}
if(controlsP1[e.key.toLowerCase()] !== undefined) {
e.preventDefault();
snes.setPad1ButtonPressed(controlsP1[e.key.toLowerCase()]);
}
}
window.onkeyup = function(e) {
if(controlsP1[e.key.toLowerCase()] !== undefined) {
e.preventDefault();
snes.setPad1ButtonReleased(controlsP1[e.key.toLowerCase()]);
}
}
function log(text) {
el("log").innerHTML += text + "\n";
el("log").scrollTop = el("log").scrollHeight;
}
function getByteRep(val) {
return ("0" + val.toString(16)).slice(-2).toUpperCase();
}
function getWordRep(val) {
return ("000" + val.toString(16)).slice(-4).toUpperCase();
}
function getLongRep(val) {
return ("00000" + val.toString(16)).slice(-6).toUpperCase();
}
function clearArray(arr) {
for(let i = 0; i < arr.length; i++) {
arr[i] = 0;
}
}
function el(id) {
return document.getElementById(id);
}