-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmix-wasm-page.js
282 lines (244 loc) · 9.01 KB
/
mmix-wasm-page.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
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
initialMixalProgram = ` LOC 80
GREG @
mask OCTA #0102040810204080
output BYTE "UUUUUUUU",10,0,0,0,0,0,0,0
#input OCTA #aabb112233445566
input BYTE "y2kbuggr"
LOC #100
GREG @
x IS $1
y IS $2
z IS $3
Main LDOU y,input
LDOU z,mask
MXOR x,y,z
STOU x,output
LDA $255,output
TRAP 0,Fputs,StdOut
TRAP 0,Halt,0`
function makeWriter(elementid) {
var element = document.getElementById(elementid);
if (element) element.value = ''; // clear browser cache
return function(text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
if (text == "Calling stub instead of signal()") return
if (element) {
element.value += text + "\n";
element.scrollTop = element.scrollHeight; // focus on bottom
}
// if we need to put these at console for some reason
};
}
var inputElement = document.getElementById('input');
var mmix;
var mmixal;
var rerunOldVal = "";
function rerun(){
if (inputElement.value == rerunOldVal) return;
// store the program across refreshes
try {
MixalProgram = localStorage.setItem('MixalProgram',inputElement.value);
} catch {
MixalProgram = inputElement.value;
}
document.getElementById('error').value = "";
FS.writeFile("/y2k.mms", inputElement.value);
mmixal();
if (document.getElementById('error').value == "")
{
// mmix_sim();
document.getElementById('output').value = "";
mmix_sim_js()
}
rerunOldVal = inputElement.value;
}
sim_timer = undefined;
instruction_count = 0;
function mmix_sim_js(){
if (sim_timer != undefined) {
mmix_sim_js_stop()
}
instruction_count = 0;
$('#instruction-count').text(instruction_count);
mmix_lib_initialize();
mmix_initialize();
mmix_boot();
y2k_mmix_load_file();
interval_ms = 1000 / $('#freq').val()
sim_timer = setInterval(mmix_sim_loop, interval_ms);
// sim_timer = setInterval(mmix_sim_loop, 1000);
// set_breakpoint(0);
// if(get_interacting())
// {
// mmix_interact();
// }
}
function mmix_sim_js_stop(){
clearInterval(sim_timer);
sim_timer = undefined;
mmix_finalize();
}
function mmix_sim_loop() {
// if(get_interrupt()&&!get_breakpoint())
// {
// set_breakpoint(get_breakpoint() | get_trace_bit());
// set_interacting(true);
// set_interrupt(false);
// }
// else
// {
// set_breakpoint(0);
// if(get_interacting())
// {
// mmix_interact();
// }
// }
console.log("Outer_looper");
if (get_halted())
{
console.log('halting');
show_stats(true);
mmix_sim_js_stop();
return;
}
// do {
// This inner loop is normal running
// The out loop (this whole function is the part handling interrupt resumes and breakpoints
console.log('Inner_looper');
if(!get_resuming())
{
mmix_fetch_instruction();
instruction_count += 1;
$('#instruction-count').text(instruction_count);
}
mmix_perform_instruction();
reg_num = $('#g_reg_num').val();
$('#g_reg_val').html(g_hex(reg_num));
// mmix_trace();
mmix_dynamic_trap();
// if(get_resuming() && op != RESUME)
// fixme we need to add back this op checking
if(get_resuming())
{
set_resuming(false);
}
// } while ((get_resuming() || (!get_interrupt() && !get_breakpoint())))
// mmix_sim_js_stop();
// if(get_interact_after_break())
// {
// set_interacting(true);
// set_interact_after_break(false);
// }
}
// Module = new Object();
Module.print = makeWriter('output');
Module.printErr = makeWriter('error');
Module.onRuntimeInitialized = function (){
// wait until runtimes are loaded to map these
mmix_sim = Module.cwrap('mmix_sim', 'number', []);
mmixal = Module.cwrap('mmixal_wasm', 'number', []);
halted = Module.cwrap('halted_wasm', 'number', []);
print_mystr = Module.cwrap('print_mystr', 'number', ['string']);
mmix_initialize_globals = Module.cwrap('mmix_initialize_globals', 'number', []);
y2k_mmix_load_file = Module.cwrap('y2k_mmix_load_file', null, []);
mmix_lib_initialize = Module.cwrap('mmix_lib_initialize', 'number', []);
mmix_initialize = Module.cwrap('mmix_initialize', 'number', []);
mmix_boot = Module.cwrap('mmix_boot', 'number', []);
mmix_load_file = Module.cwrap('mmix_boot', 'number', ['string']);
mmix_interact = Module.cwrap('mmix_interact', 'number', ['string']);
mmix_fetch_instruction = Module.cwrap('mmix_fetch_instruction', 'number', []);
mmix_perform_instruction = Module.cwrap('mmix_perform_instruction', 'number', []);
mmix_trace = Module.cwrap('mmix_trace', 'number', []);
mmix_dynamic_trap = Module.cwrap('mmix_dynamic_trap', 'number', []);
show_stats = Module.cwrap('show_stats', 'number', []);
mmix_finalize = Module.cwrap('mmix_finalize', 'number', []);
get_halted = Module.cwrap('get_halted', 'bool', []);
set_halted = Module.cwrap('set_halted', null, ['bool']);
get_breakpoint = Module.cwrap('get_breakpoint', 'number', []);
set_breakpoint = Module.cwrap('set_breakpoint', null, ['number']);
get_interrupt = Module.cwrap('get_interrupt', 'bool', []);
set_interrupt = Module.cwrap('set_interrupt', null, ['bool']);
get_resuming = Module.cwrap('get_resuming', 'bool', []);
set_resuming = Module.cwrap('set_resuming', null, ['bool']);
get_interacting = Module.cwrap('get_interacting', 'bool', []);
set_interacting = Module.cwrap('set_interacting', null, ['bool']);
get_interact_after_break = Module.cwrap('get_interact_after_break', 'bool', []);
set_interact_after_break = Module.cwrap('set_interact_after_break', null, ['bool']);
get_profiling = Module.cwrap('get_profiling', 'bool', []);
set_profiling = Module.cwrap('set_profiling', null, ['bool']);
get_showing_stats = Module.cwrap('get_showing_stat', 'bool', []);
set_showing_stats = Module.cwrap('set_showing_stat', null, ['bool']);
get_trace_bit = Module.cwrap('get_trace_bit', 'number', []);
get_general_register = Module.cwrap('get_general_register', 'number', ['number','bool']);
try {
MixalProgram = localStorage.getItem('MixalProgram');
} catch {
MixalProgram = null;
}
if (MixalProgram == null) {
// Set up initial program and run once
MixalProgram = initialMixalProgram;
}
inputElement.value = MixalProgram;
rerun();
}
$("#input").on('keyup paste', rerun);
document.getElementById("input").addEventListener("keydown", function(e) {
if (e.which===9) {
e.preventDefault();
document.execCommand("insertText", false, "\t");
}
}, false);
function g_hex(register_num, noformat) {
h = (get_general_register(register_num, true) >>>0).toString(16);
l = (get_general_register(register_num, false) >>>0).toString(16);
hs = '0'.repeat(8 - h.length);
ls = '0'.repeat(8 - l.length);
if (noformat) return hs + h + ls + l;
if (h == '0') {
return hs + h + ls + '<b>' + l + '</b>';
}
else {
return hs + '<b>' + h + ls + l +'</b>';
}
}
function g_bin(register_num, noformat) {
h = (get_general_register(register_num, true) >>>0).toString(2);
l = (get_general_register(register_num, false) >>>0).toString(2);
hs = '0'.repeat(32 - h.length);
ls = '0'.repeat(32 - l.length);
if (noformat) return hs + h + ls + l;
if (h == '0') {
return hs + h + ls + '<b>' + l + '</b>';
}
else {
return hs + '<b>' + h + ls + l +'</b>';
}
}
function g_dec(register_num, noformat) {
if (h != 0) return "overflow"; // this could be handled at some point
// h = get_g_register_h(register_num).toString(2);
l = get_g_register_l(register_num).toString(10);
if (noformat) return l;
return '<b>' + l + '</b>';
}
// https://stackoverflow.com/questions/6637341/use-tab-to-indent-in-textarea
// // Create a Web Worker (separate thread) that we'll pass the WebAssembly module to.
// var g_WebWorker = new Worker("webworker.js");
// g_WebWorker.onerror = function (evt) { console.log(`Error from Web Worker: ${evt.message}`); }
// g_WebWorker.onmessage = function (evt) { alert(`Message from the Web Worker:\n\n ${evt.data}`); }
// // Request the wasm file from the server and compile it...(Typically we would call
// // 'WebAssembly.instantiate' which compiles and instantiates the module. In this
// // case, however, we just want the compiled module which will be passed to the Web
// // Worker. The Web Worker will be responsible for instantiating the module.)
// fetch("mmix.wasm").then(response =>
// response.arrayBuffer()
// ).then(bytes =>
// WebAssembly.compile(bytes)
// ).then(WasmModule =>
// g_WebWorker.postMessage({ "MessagePurpose": "CompiledModule", "WasmModule": WasmModule })
// );
// function OnClickTest() {
// // Ask the Web Worker to add two values
// g_WebWorker.postMessage({ "MessagePurpose": "AddValues", "Val1": 1, "Val2": 2 });
// }