-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.php
420 lines (394 loc) · 11.9 KB
/
index.php
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>NEZ - play NES while surfing the WWW :O</title>
<meta property="og:title" content="NEZ - play NES while surfing the WWW :O" />
<meta property="og:url" content="http://eternal.dk/emu/" />
<meta property="og:image" content="nez.png" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="NEZ - play NES while surfing the WWW :O" />
<meta name="twitter:description" content="A JavaScript based NES emulator" />
<meta name="twitter:image" content="http://eternal.dk/emu/nez.png" />
<meta name="twitter:creator" content="@sumez" />
<script id="vertex" type="x-shader/x-vertex">
attribute vec2 aVertexPosition;
attribute vec2 aTextureCoord;
uniform vec2 u_translation;
uniform vec2 u_resolution;
varying highp vec2 vTextureCoord;
void main(void) {
vec2 cBase = u_resolution / vec2(2, 2);
gl_Position = vec4(
((aVertexPosition) + u_translation - cBase) / cBase
, 0, 1.0) * vec4(1, -1, 1, 1);
vTextureCoord = aTextureCoord;
}
</script>
<script id="textureFragment" type="x-shader/x-fragment">
varying highp vec2 vTextureCoord;
uniform sampler2D uSampler;
void main(void) {
gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
}
</script>
<script id="colorFragment" type="x-shader/x-fragment">
uniform lowp vec4 uColor;
void main(void) {
gl_FragColor = uColor;
}
</script>
<script type="text/javascript">
window.isDebug = location.href.match(/debug=1$/i) ? true : false;
function loadfile(event) {
var file = event.files[0];
if (!file) return;
if (!file.name.match(/\.nes$/i)) {
alert('invalid file');
return;
}
var reader = new FileReader();
reader.onload = function (e) {
setTimeout(function() {
window.emu.volume($('[type=range]').val() / 100)
if (!window.emu.loadRomData(e.target.result, file.name)) {
return;
}
//$('.button.open').remove();
window.emu.run($('canvas')[0], window.isDebug);
});
};
reader.readAsArrayBuffer(file);
}
function fullscreen() {
var canvas = $('canvas')[0];
if (document.webkitIsFullScreen) return;
if (!canvas.webkitRequestFullScreen) return;
canvas.webkitRequestFullScreen();
};
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body <?php if ($_GET['debug'] == '1') { ?>class="debug"<?php } ?>>
<script type="text/javascript" src="emulatorscript.php?2"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<img src="nez.png" style="display: none;" class="logo" />
<div class="emulator">
<canvas class="nes" width="512" height="480"></canvas>
<div class="controls">
<div title="Open NES ROM file" class="button open">
<input class="rom-file" type="file" accept=".nes" onchange="loadfile(this)" />
<img src="open.svg">
</div>
<div title="Toggle pause" class="button pause" onclick="togglePause()" style="margin-right: auto">
<img class="pause-state" src="pause.svg">
</div>
<i class="fas fa-volume-off"></i>
<input type="range" min="0" max="100" value="50" oninput="emu.volume(this.value / 100); config.volume = this.value; save(); this.focus()">
<div title="Enable SNES mouse" class="button" onclick="emu.useMouse()">
<img src="mouse.svg">
</div>
<div title="Configure controller buttons" class="button controller" onclick="emu.buttonConfig()">
<img src="controller.svg">
</div>
<div title="Toggle TV shader (might be slow)" class="button shader" onclick="toggleShader()">
<img src="tv.svg">
</div>
<div title="Full screen" class="button" onclick="fullscreen()">
<img src="fullscreen.svg">
</div>
</div>
</div>
<style type="text/css">
.rom-file {
-webkit-appearance: none;
position: relative;
margin-bottom: 10px;
height: 38px;
font-family: inherit;
font-size: 13px;
background: inherit;
opacity: 0.1;
top: -10px;
left: -10px;
}
.rom-file:before {
content: '';
display: block;
min-width: 100%;
padding: 10px 5px;
background: inherit;
cursor: pointer;
position: absolute;
right: 0;
left: 0;
}
.debug .nes {
width: 256px;
height: 240px;
}
.debug .nametable {
display: inline-block !important;
}
canvas {
display: block;
margin: auto;
}
.controls {
padding: 0 10px;
display: flex;
align-items: center;
justify-content: flex-end;
}
.controls [type=button], .controls .button {
border: none;
background: black;
color: inherit;
text-transform: uppercase;
font-weight: bold;
font-size: 12px;
line-height: 1em;
padding: 3px;
min-width: 1em;
cursor: pointer;
position: relative;
height: 1.5em;
}
.controls [type=button].enabled, .controls .button.enabled {
background: #333333;
}
.controls .button img {
width: 1em;
height: 1em;
margin: 3px;
}
.controls .button.controller img {
width: 1.5em;
height: 1.5em;
margin: 0 3px;
}
.controls .button.open {
width: 1em;
height: 1em;
overflow: hidden;
}
.controls .button.open img {
position: absolute;
top: 0;
left: 0;
pointer-events: none;
}
.controls [type=button]:hover, .controls .button:hover {
background: #666;
}
.controls .fas {
}
.controls [type=button]:active, .controls .button:active {
background: #ccc;
}
.controls [type=range] {
-webkit-appearance: none;
height: 0.3em;
background: #999;
width: 80px;
vertical-align: baseline;
}
.controls [type=range]::-webkit-slider-thumb {
-webkit-appearance: none;
background: white;
width: 0.5em;
height: 1em;
}
.controls input {
cursor: pointer;
outline: none !important;
}
html {
height: 100%;
}
body {
font-family: Tahoma;
font-size: 12px;
background-color: #ccc;
background-image: linear-gradient(#cef, #acf, #acf, #7bc);
background-attachment: fixed;
text-align: center;
}
.emulator {
background: black;
border: 2px solid white;
border-radius: 7px;
box-shadow: 0 0 13px rgba(0,50,100,0.5);
color: white;
display: inline-block;
overflow: hidden;
}
.fullscreen * {
display: none !important;
}
.fullscreen .nes, .fullscreen .emulator {
display: block !important;
}
.fullscreen {
background: black;
background-image: none;
}
.fullscreen .emulator {
display: block !important;
position: absolute !important;
top: 0;
left: 0;
border: none;
border-radius: 0;
box-shadow: none;
}
.overlay {
color: black;
background: white;
padding: 20px;
width: 230px;
margin: auto;
position: fixed;
top: 150px;
left: calc(50vw - 135px);
box-shadow: 0 0 4px rgba(0,0,0,0.5);
}
h2 {
font-size: 2em;
border-radius: 10px;
background: rgba(255,255,255,0.3);
max-width: 700px;
margin: 20px auto;
display: flex;
padding: 5px 15px;
color: rgba(0,30,50,0.3);
}
h2 a {
color: inherit;
margin-right: 10px;
font-size: 1.5em;
}
h2 a:hover, h2 span:hover {
color: rgba(0,30,50,0.5);
}
h2 span {
width: 100%;
cursor: pointer;
padding-top: 10px;
text-align: right;
}
.text {
max-width: 600px;
margin: 20px auto;
text-align: left;
color: rgba(0,0,0,0.5);
}
</style>
<script type="text/javascript">
var config = {};
if (localStorage.config) {
config = JSON.parse(localStorage.config);
if (config.shaderEnabled) toggleShader(true);
if (config.volume) $('[type=range]').val(Math.min(100, config.volume));
}
function save() {
localStorage.config = JSON.stringify(config);
}
window.onresize = function() {
if (window.isDebug) return;
var isFullscreen = ((screen.availHeight || screen.height-20) <= window.innerHeight);
var canvas = $('.nes')[0];
var emulator = $('.emulator')[0];
var windowHeight = window.screen.height;
var windowWidth = window.screen.width;
if (!isFullscreen) {
windowHeight = window.innerHeight - 50; // Add buffer for interface stuff
windowWidth = window.innerWidth - 15;
windowHeight = Math.max(240, windowHeight - (windowHeight % 240));
windowWidth = Math.max(256, windowWidth - (windowWidth % 256));
}
var height = windowHeight;
var width = (16 / 15) * height;
if (width > windowWidth) {
width = windowWidth;
height = (15 / 16) * width
}
canvas.style.width = (canvas.width = width) + 'px';
canvas.style.height = (canvas.height = height) + 'px';
emulator.style.top = ((window.screen.height - height) / 2) + 'px';
emulator.style.left = ((window.screen.width - width) / 2) + 'px';
document.body.className = isFullscreen ? 'fullscreen' : '';
if (emu && emu.isPlaying()) emu.render(); // Refresh canvas after resize
else drawLogo();
};
window.onresize();
$('.nes').on('click', function() { if (!emu.isPlaying()) { $('[type=file]').click(); } });
function drawLogo() {
var ctx = $('.nes')[0].getContext('2d');
ctx.imageSmoothingEnabled = ctx.webkitImageSmoothingEnabled = ctx.mozImageSmoothingEnabled = false;
ctx.drawImage($('.logo')[0], 0, 0, 256, 240, 0, 0, ctx.canvas.width, ctx.canvas.height);
}
$('.logo').on('load', drawLogo);
var paused = false;
function togglePause() {
if (!emu.isPlaying()) return;
if (!paused) {
emu.pause();
$('.pause-state').prop('src', 'play.svg');
}
else {
emu.resume();
$('.pause-state').prop('src', 'pause.svg');
}
paused = !paused;
}
var shaderEnabled = false;
function toggleShader(value) {
if (value != undefined) config.shaderEnabled = value;
else config.shaderEnabled = !config.shaderEnabled;
if (config.shaderEnabled) {
emu.enableShader('crt.glsl');
}
else {
emu.disableShader();
}
$('.button.shader').toggleClass('enabled', config.shaderEnabled);
save();
}
</script>
<h2>
<a href="https://twitter.com/sumez" target="_blank"><i class="fab fa-twitter-square"></i></a>
<a href="https://github.com/sumez/nez" target="_blank"><i class="fab fa-github-square"></i></a>
<span onclick="$('.text').show(); $(window).scrollTop(100000)">
More about NEZ <i class="fas fa-chevron-circle-down"></i>
</span>
</h2>
<div class="text" style="display: none;">
This is an emulator started as a short experiment to test how feasible it even was to pull off such a thing in JavaScript, and quickly improved to support many more games and features than I thought it possibly could, but it is also still evolving.<br />
If you see any glaring issues with certain games, or other support you'd like me to add, feel free to <a href="https://twitter.com/sumez" target="_blank">drop me a line</a>, and I will probably bump up the priority.<br /><br />
~ Sumez<br />
<br />
Features currently in the pipline for the future:<br /><br />
<strong>Support / accuracy:</strong>
<ul>
<li>Support for less common mappers (most notably VRC6 and MMC2 I guess?)</li>
<li>Controller support</li>
<li>Improve mouse support</li>
<li>50hz/PAL support</li>
<li>Famicom Disk System support</li>
<li>Improve CPU/PPU cycle synchronization</li>
<li>All unofficial opcodes</li>
<li>Better CRT shaders (correct scanlines, color bleed, etc)</li>
<li>Debug features (<a href="?debug=1">look here</a> for some testing features, like nametable, CHR and wavetable displays)</li>
<li>Save states.... maaybe?</li>
</ul>
<strong>Technical improvements</strong>
<ul>
<li>Better performance across all areas (most notably PPU emulation)</li>
<li>Average audio samples to improve quality and smoothen out high frequent tones</li>
<li><s>Re-implement mappers to actually map addresses, rather than just copying data around</s> find a new way to do this without a huge performance overhead</li>
<li>Change PPU emulation to use actual PPU registers rather than abstract variables that don't interfer with eachother in the same way</li>
</ul>
</div>
</body>
</html>