forked from tommythorn/virtual-nascom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtual-nascom.html
251 lines (250 loc) · 8.02 KB
/
virtual-nascom.html
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
<!DOCTYPE html>
<!--
Author: Peter Jensen
-->
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="viewport-fit=cover, user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1">
<title>Nascom 2 Simulator</title>
<style>
:root {
--font-family:Arial;
--font-size:1em;
--font-size-input:1em;
--container-width:960x;
--canvas-width:960px;
--canvas-height:512px;
--canvas-margin:0px;
}
@media (max-width: 959px) {
:root {
--container-width:414px;
--canvas-width:480px;
--canvas-height:256px;
--canvas-margin:-33px;
}
}
@media (max-width: 413px) {
:root {
--container-width:360px;
--canvas-margin:-48px;
}
}
html {
display: table;
margin: auto;
}
body {
display: table-cell;
font-family: var(--font-family);
font-size: var(--font-size);
background: #e0e0e0;
}
button {
font-family:var(--font-family);
font-size: var(--font-size-input);
}
select {
font-family:var(--font-family);
font-size: var(--font-size-input);
}
h1 {
text-align: center;
}
#container {
width: var(--container-width);
overflow:hidden;
}
#canvas-container {
text-align: center;
overflow:hidden;
}
#canvas {
width:var(--canvas-width);
height:var(--canvas-height);
margin-left:var(--canvas-margin);
border-radius:10px;
}
#controls {
text-align: center;
}
#sub-header {
text-align: center;
font-style: italic;
padding-bottom: 10px;
}
</style>
</head>
<body onload="onLoaded()">
<div id="container">
<h1>Nascom 2 Simulator</h1>
<div id="sub-header">Ported to the Web by: Peter Jensen</div>
<div style="width:0px; height:0px; overflow:hidden">
<textarea id="text"></textarea>
</div>
<div id="canvas-container">
<canvas id="canvas" oncontextmenu="event.preventDefault()"></canvas>
</div>
<div id="controls">
<button id="reset">Reset</button>
<select id="pick">
<option>Avalanch.nas</option>
<option>BLS-Breakout.nas</option>
<option>BLS-Pascal-1.2.nas</option>
<option>BLS-Spacezap.nas</option>
<option>Hole.nas</option>
<option>KKS-3dmaze.nas</option>
<option>Monster.nas</option>
<option>Reversi.nas</option>
<option>Serpent.nas</option>
<option>Spcinv.nas</option>
<option>galaxian.nas</option>
<option>jailbrk.nas</option>
<option>pacman.nas</option>
<option>pacman2.nas</option>
<option>spacewar.nas</option>
</select>
<button id="load">Load</button>
<button id="keyboard">Keyboard</button>
</div>
<div>
<h3>Usage</h3>
<ul>
<li>Pick the program to load, and click 'Load'</li>
<li>Execute the program with the NAS-SYS command 'E1000' (typically)</li>
</ul>
<h3>Notes</h3>
<ul>
<li>Adapted from: <A href="https://github.com/tommythorn/virtual-nascom">Tommy Thorn's Simulator</A></li>
<li>Built with <A href="https://emscripten.org">Emscripten</A></li>
<li>Runs with WebAssembly and Javascript</li>
</ul>
<h3>TODO</h3>
<ul>
<li>Figure out how to simulate 'save to tape'. Maybe use IndexedDb or localStorage</li>
</ul>
</div>
</div>
<script>
// Initialization is done after the DOM has loaded AND the Wasm runtime is initialized.
// The order of these two events is depending on network speed, so make no assumption
// on which one occurs last.
var isRuntimeInitialized = false;
var isLoaded = false;
function onInit() {
isRuntimeInitialized = true;
if (isLoaded)
init();
}
function onLoaded() {
isLoaded = true;
if (isRuntimeInitialized)
init();
}
var Module = {
noInitialRun: true, // invoke main manually after document load
canvas: null, // initialized after document is loaded
onRuntimeInitialized: onInit,
print: function(text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
console.log(text);
},
printErr: function(text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
console.error(text);
}
};
</script>
<script>
var elems = {
canvas: null,
text: null,
reset: null,
pick: null,
load: null,
keyboard: null
};
function load_nascom_string(str) {
let ptr = allocate(intArrayFromString(str), ALLOC_NORMAL);
Module._load_nascom_string(ptr);
_free(ptr);
}
function load(e) {
const fileName = elems.pick.options[elems.pick.selectedIndex].innerText;
const fullFileName = "programs/e1000/" + fileName;
fetch(fullFileName)
.then(response => response.text())
.then(text => load_nascom_string(text));
}
function keyboardClick(e) {
e.preventDefault();
elems.keyboard.blur();
elems.text.focus();
elems.canvas.scrollIntoView(true);
}
function setCanvasSize() {
const bodyWidth = document.body.clientWidth;
const canvasMul = Math.floor(bodyWidth/15);
let canvasWidth = canvasMul * 15;
let canvasHeight = canvasMul * 8;
let marginLeft = 0;
if (canvasWidth < 480) {
canvasWidth = 480;
canvasHeight = 256;
marginLeft = (480 - bodyWidth)/2;
marginLeft = marginLeft > 48 ? 48 : marginLeft;
}
elems.canvas.style.width = canvasWidth + "px";
elems.canvas.style.height = canvasHeight + "px";
elems.canvas.style.marginLeft = -marginLeft + "px";
}
function initElems() {
elems.reset = document.getElementById("reset");
elems.pick = document.getElementById("pick");
elems.load = document.getElementById("load");
elems.text = document.getElementById("text");
elems.canvas = document.getElementById("canvas");
elems.keyboard = document.getElementById("keyboard");
}
function setupKeyboardHandlers() {
// this is really only necessary for Android screen keyboards
function keyEventHandler(e) {
if (e.keyCode == 229) {
if (typeof e.custom === "undefined") {
// e.stopImmediatePropagation();
let oldText = elems.text.value;
setTimeout(function () {
const keyString = elems.text.value.replace(oldText, '');
// alert("keyString = '" + keyString + "', oldText = '" + oldText +"'");
if (keyString.length < 1)
return;
let customEventInit = {custom: true};
customEventInit = Object.assign(customEventInit, e);
customEventInit.keyCode = keyString.charCodeAt(keyString.length-1);
let customKeydown = new KeyboardEvent("keydown", customEventInit);
window.dispatchEvent(customKeydown);
let customKeyup = new KeyboardEvent("keyup", customEventInit);
window.dispatchEvent(customKeyup);
}, 0);
}
}
}
// Setup handler for keydown event
window.addEventListener("keydown", keyEventHandler, true);
}
function init() {
initElems();
elems.load.addEventListener("click", load);
elems.reset.addEventListener("click", Module._reset_nascom);
elems.keyboard.addEventListener("click", keyboardClick);
setupKeyboardHandlers();
Module.canvas = elems.canvas;
//setCanvasSize();
Module._main(); // Invoke main from wasm file
}
</script>
<script src="virtual-nascom.js"></script>
</body>
</html>