-
Notifications
You must be signed in to change notification settings - Fork 15
/
worker_funcs.js
211 lines (192 loc) · 4.54 KB
/
worker_funcs.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
var Module;
if (!Module) Module = {};
Module.setStatus = function (s) {
postMessage({
cmd: "print",
txt: s
});
};
function cout_print(s) {
postMessage({
cmd: "print",
txt: s
});
}
function set_progress(k, r) {
postMessage({
cmd: "setUI",
key: k + "_progress",
properties: r
});
}
function show_error(prefix) {
return function (evt) {
cout_print(prefix);
cout_print(String(evt.target.error));
}
}
Module['print'] = cout_print;
var vram_ptr, soundbuffer_ptr, isMute_ptr;
var vram_dels = 0,
vram_cres = 0;
var vram_arrs = [];
var render = function (x, y, sx, sy, dx, dy, rgb24) {
var vram_arr;
var vram_src = Module.HEAPU8.subarray(vram_ptr, vram_ptr + 1024 * 2048);
while (vram_arrs.length > 10) {
vram_arrs.pop();
vram_dels++;
//cout_print("delete vram "+vram_dels+"\n");
}
if (vram_arrs.length > 0) {
vram_arr = vram_arrs.pop();
vram_arr.set(vram_src);
} else {
vram_cres++;
//cout_print("create vram "+vram_cres+"\n");
vram_arr = new Uint8Array(vram_src);
}
postMessage({
cmd: "render",
x: x,
y: y,
sx: sx,
sy: sy,
dx: dx,
dy: dy,
rgb24: rgb24,
vram: vram_arr
}, [vram_arr.buffer]);
}
var pSound_arrs = [];
var SendSound = function (pSound_ptr, lBytes) {
var pSound_arr;
var pSound_src = Module.HEAPU8.subarray(pSound_ptr, pSound_ptr + lBytes);
while (pSound_arrs.length > 50) {
pSound_arrs.pop();
}
if (pSound_arrs.length > 0) {
pSound_arr = pSound_arrs.pop();
} else {
pSound_arr = new Uint8Array(4800);
}
pSound_arr.set(pSound_src);
postMessage({
cmd: "SoundFeedStreamData",
pSound: pSound_arr,
lBytes: lBytes
}, [pSound_arr.buffer]);
}
function pcsx_mainloop() {
_one_iter();
}
var pcsx_init = Module.cwrap("pcsx_init", "number", ["string"])
var ls = Module.cwrap("ls", "null", ["string"])
var padStatus1;
var isoDB;
var stdout_array;
var readfile_and_run = function (iso_name, blob) {
var run_arr = function (arr) {
//Module.FS_createDataFile("/", iso_name, arr, true, true);
stdout_array = arr;
Module.setStatus('Running!');
pcsx_init("/" + iso_name);
padStatus1 = _get_ptr(-2);
vram_ptr = _get_ptr(-1);
soundbuffer_ptr = _get_ptr(7);
isMute_ptr = _get_ptr(8);
cout_print("before mainloop\n");
pcsx_mainloop();
}
cout_print("readfile and run ");
var reader = new FileReader();
Module.setStatus("reading file");
reader.onprogress = function (e) {
if (e.lengthComputable) {
//cout_print(Math.round((e.loaded / e.total) * 100) + "%");
set_progress('readfile', {
value: e.loaded,
max: e.total,
hidden: false
});
} else
cout_print(e.loaded + "bytes")
//document.getElementById("start").disabled=false
}
reader.onload = function (e) {
cout_print("" + iso_name + " loaded");
set_progress('readfile', {
value: 1,
max: 1,
hidden: false
});
run_arr(new Uint8Array(this.result))
}
reader.readAsArrayBuffer(blob);
}
var event_history = [];
var clear_event_history = function () {
self.onmessage = main_onmessage;
for (var i in event_history) {
main_onmessage(event_history[i]);
}
event_history = [];
Module.setStatus = function (s) {
postMessage({
cmd: "setStatus",
txt: s
});
};
setTimeout("Module.setStatus('Open an iso file using the above button(worker ready!).')", 1);
}
var pre_onmessage = function (event) {
if (event.data.cmd != 'soundBytes') {
event_history.push(event);
cout_print("push event" + event.data.cmd);
}
}
self.onmessage = pre_onmessage;
var main_onmessage = function (event) {
var data = event.data;
switch (data.cmd) {
case "padStatus":
Module.HEAPU8.set(data.states, padStatus1);
postMessage({
cmd: "return_states",
states: data.states
}, [data.states.buffer]);
//Module.setValue(soundbuffer_ptr, data.soundbuffer, "i32");
break;
case "soundBytes":
Module.setValue(soundbuffer_ptr, Module.getValue(soundbuffer_ptr, "i32") - data.lBytes, "i32");
break;
case "return_vram":
vram_arrs.push(data.vram)
break;
case "return_pSound":
pSound_arrs.push(data.pSound)
break;
case "ls":
ls(data.dir);
break;
case "loadfile":
Module.setStatus('Downloading...');
cout_print(data.file.name)
readfile_and_run(data.file.name, data.file);
break;
case "loadurl":
cout_print("load..." + data.iso);
load_or_fetch(data.iso)
break;
default:
postMessage({
cmd: "print",
txt: "unknown command " + data.cmd
})
}
}
cout_print("worker started\n");
onerror = function (event) {
// TODO: do not warn on ok events like simulating an infinite loop or exitStatus
Module.setStatus('Exception thrown, see JavaScript console ' + String(event));
};