lib.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. "use strict";
  2. var goog = goog || {};
  3. goog.exportSymbol = function() {};
  4. goog.exportProperty = function() {};
  5. var v86util = v86util || {};
  6. // pad string with spaces on the right
  7. v86util.pads = function(str, len)
  8. {
  9. str = (str || str === 0) ? str + "" : "";
  10. return str.padEnd(len, " ");
  11. };
  12. // pad string with zeros on the left
  13. v86util.pad0 = function(str, len)
  14. {
  15. str = (str || str === 0) ? str + "" : "";
  16. return str.padStart(len, "0");
  17. };
  18. // generates array given size with zeros
  19. v86util.zeros = function(size)
  20. {
  21. return Array(size).fill(0);
  22. };
  23. // generates [0, 1, 2, ..., size-1]
  24. v86util.range = function(size)
  25. {
  26. return Array.from(Array(size).keys());
  27. };
  28. v86util.view = function(constructor, memory, offset, length)
  29. {
  30. return new Proxy({},
  31. {
  32. get: function(target, property, receiver)
  33. {
  34. const b = new constructor(memory.buffer, offset, length);
  35. const x = b[property];
  36. if(typeof x === "function")
  37. {
  38. return x.bind(b);
  39. }
  40. dbg_assert(/^\d+$/.test(property) || property === "buffer" || property === "length" ||
  41. property === "BYTES_PER_ELEMENT" || property === "byteOffset");
  42. return x;
  43. },
  44. set: function(target, property, value, receiver)
  45. {
  46. dbg_assert(/^\d+$/.test(property));
  47. new constructor(memory.buffer, offset, length)[property] = value;
  48. return true;
  49. },
  50. });
  51. };
  52. /**
  53. * number to hex
  54. * @param {number} n
  55. * @param {number=} len
  56. * @return {string}
  57. */
  58. function h(n, len)
  59. {
  60. if(!n)
  61. {
  62. var str = "";
  63. }
  64. else
  65. {
  66. var str = n.toString(16);
  67. }
  68. return "0x" + v86util.pad0(str.toUpperCase(), len || 1);
  69. }
  70. if(typeof crypto !== "undefined" && crypto.getRandomValues)
  71. {
  72. let rand_data = new Int32Array(1);
  73. v86util.get_rand_int = function()
  74. {
  75. crypto.getRandomValues(rand_data);
  76. return rand_data[0];
  77. };
  78. }
  79. else if(typeof require !== "undefined")
  80. {
  81. /** @type {{ randomBytes: Function }} */
  82. const crypto = require("crypto");
  83. v86util.get_rand_int = function()
  84. {
  85. return crypto.randomBytes(4)["readInt32LE"](0);
  86. };
  87. }
  88. else
  89. {
  90. dbg_assert(false, "Unsupported platform: No cryptographic random values");
  91. }
  92. /**
  93. * Synchronous access to ArrayBuffer
  94. * @constructor
  95. */
  96. function SyncBuffer(buffer)
  97. {
  98. dbg_assert(buffer instanceof ArrayBuffer);
  99. this.buffer = buffer;
  100. this.byteLength = buffer.byteLength;
  101. this.onload = undefined;
  102. this.onprogress = undefined;
  103. }
  104. SyncBuffer.prototype.load = function()
  105. {
  106. this.onload && this.onload({ buffer: this.buffer });
  107. };
  108. /**
  109. * @param {number} start
  110. * @param {number} len
  111. * @param {function(!Uint8Array)} fn
  112. */
  113. SyncBuffer.prototype.get = function(start, len, fn)
  114. {
  115. dbg_assert(start + len <= this.byteLength);
  116. fn(new Uint8Array(this.buffer, start, len));
  117. };
  118. /**
  119. * @param {number} start
  120. * @param {!Uint8Array} slice
  121. * @param {function()} fn
  122. */
  123. SyncBuffer.prototype.set = function(start, slice, fn)
  124. {
  125. dbg_assert(start + slice.byteLength <= this.byteLength);
  126. new Uint8Array(this.buffer, start, slice.byteLength).set(slice);
  127. fn();
  128. };
  129. /**
  130. * @param {function(!ArrayBuffer)} fn
  131. */
  132. SyncBuffer.prototype.get_buffer = function(fn)
  133. {
  134. fn(this.buffer);
  135. };
  136. SyncBuffer.prototype.get_state = function()
  137. {
  138. const state = [];
  139. state[0] = this.byteLength;
  140. state[1] = new Uint8Array(this.buffer);
  141. return state;
  142. };
  143. SyncBuffer.prototype.set_state = function(state)
  144. {
  145. this.byteLength = state[0];
  146. this.buffer = state[1].slice().buffer;
  147. };
  148. (function()
  149. {
  150. if(typeof Math.clz32 === "function" && Math.clz32(0) === 32 &&
  151. Math.clz32(0x12345) === 15 && Math.clz32(-1) === 0)
  152. {
  153. /**
  154. * calculate the integer logarithm base 2 of a byte
  155. * @param {number} x
  156. * @return {number}
  157. */
  158. v86util.int_log2_byte = function(x)
  159. {
  160. dbg_assert(x > 0);
  161. dbg_assert(x < 0x100);
  162. return 31 - Math.clz32(x);
  163. };
  164. /**
  165. * calculate the integer logarithm base 2
  166. * @param {number} x
  167. * @return {number}
  168. */
  169. v86util.int_log2 = function(x)
  170. {
  171. dbg_assert(x > 0);
  172. return 31 - Math.clz32(x);
  173. };
  174. return;
  175. }
  176. var int_log2_table = new Int8Array(256);
  177. for(var i = 0, b = -2; i < 256; i++)
  178. {
  179. if(!(i & i - 1))
  180. b++;
  181. int_log2_table[i] = b;
  182. }
  183. /**
  184. * calculate the integer logarithm base 2 of a byte
  185. * @param {number} x
  186. * @return {number}
  187. */
  188. v86util.int_log2_byte = function(x)
  189. {
  190. dbg_assert(x > 0);
  191. dbg_assert(x < 0x100);
  192. return int_log2_table[x];
  193. };
  194. /**
  195. * calculate the integer logarithm base 2
  196. * @param {number} x
  197. * @return {number}
  198. */
  199. v86util.int_log2 = function(x)
  200. {
  201. x >>>= 0;
  202. dbg_assert(x > 0);
  203. // http://jsperf.com/integer-log2/6
  204. var tt = x >>> 16;
  205. if(tt)
  206. {
  207. var t = tt >>> 8;
  208. if(t)
  209. {
  210. return 24 + int_log2_table[t];
  211. }
  212. else
  213. {
  214. return 16 + int_log2_table[tt];
  215. }
  216. }
  217. else
  218. {
  219. var t = x >>> 8;
  220. if(t)
  221. {
  222. return 8 + int_log2_table[t];
  223. }
  224. else
  225. {
  226. return int_log2_table[x];
  227. }
  228. }
  229. };
  230. })();
  231. /**
  232. * @constructor
  233. *
  234. * Queue wrapper around Uint8Array
  235. * Used by devices such as the PS2 controller
  236. */
  237. function ByteQueue(size)
  238. {
  239. var data = new Uint8Array(size),
  240. start,
  241. end;
  242. dbg_assert((size & size - 1) === 0);
  243. this.length = 0;
  244. this.push = function(item)
  245. {
  246. if(this.length === size)
  247. {
  248. // intentional overwrite
  249. }
  250. else
  251. {
  252. this.length++;
  253. }
  254. data[end] = item;
  255. end = end + 1 & size - 1;
  256. };
  257. this.shift = function()
  258. {
  259. if(!this.length)
  260. {
  261. return -1;
  262. }
  263. else
  264. {
  265. var item = data[start];
  266. start = start + 1 & size - 1;
  267. this.length--;
  268. return item;
  269. }
  270. };
  271. this.peek = function()
  272. {
  273. if(!this.length)
  274. {
  275. return -1;
  276. }
  277. else
  278. {
  279. return data[start];
  280. }
  281. };
  282. this.clear = function()
  283. {
  284. start = 0;
  285. end = 0;
  286. this.length = 0;
  287. };
  288. this.clear();
  289. }
  290. /**
  291. * @constructor
  292. *
  293. * Queue wrapper around Float32Array
  294. * Used by devices such as the sound blaster sound card
  295. */
  296. function FloatQueue(size)
  297. {
  298. this.size = size;
  299. this.data = new Float32Array(size);
  300. this.start = 0;
  301. this.end = 0;
  302. this.length = 0;
  303. dbg_assert((size & size - 1) === 0);
  304. }
  305. FloatQueue.prototype.push = function(item)
  306. {
  307. if(this.length === this.size)
  308. {
  309. // intentional overwrite
  310. this.start = this.start + 1 & this.size - 1;
  311. }
  312. else
  313. {
  314. this.length++;
  315. }
  316. this.data[this.end] = item;
  317. this.end = this.end + 1 & this.size - 1;
  318. };
  319. FloatQueue.prototype.shift = function()
  320. {
  321. if(!this.length)
  322. {
  323. return undefined;
  324. }
  325. else
  326. {
  327. var item = this.data[this.start];
  328. this.start = this.start + 1 & this.size - 1;
  329. this.length--;
  330. return item;
  331. }
  332. };
  333. FloatQueue.prototype.shift_block = function(count)
  334. {
  335. var slice = new Float32Array(count);
  336. if(count > this.length)
  337. {
  338. count = this.length;
  339. }
  340. var slice_end = this.start + count;
  341. var partial = this.data.subarray(this.start, slice_end);
  342. slice.set(partial);
  343. if(slice_end >= this.size)
  344. {
  345. slice_end -= this.size;
  346. slice.set(this.data.subarray(0, slice_end), partial.length);
  347. }
  348. this.start = slice_end;
  349. this.length -= count;
  350. return slice;
  351. };
  352. FloatQueue.prototype.peek = function()
  353. {
  354. if(!this.length)
  355. {
  356. return undefined;
  357. }
  358. else
  359. {
  360. return this.data[this.start];
  361. }
  362. };
  363. FloatQueue.prototype.clear = function()
  364. {
  365. this.start = 0;
  366. this.end = 0;
  367. this.length = 0;
  368. };
  369. /**
  370. * Simple circular queue for logs
  371. *
  372. * @param {number} size
  373. * @constructor
  374. */
  375. function CircularQueue(size)
  376. {
  377. this.data = [];
  378. this.index = 0;
  379. this.size = size;
  380. }
  381. CircularQueue.prototype.add = function(item)
  382. {
  383. this.data[this.index] = item;
  384. this.index = (this.index + 1) % this.size;
  385. };
  386. CircularQueue.prototype.toArray = function()
  387. {
  388. return [].slice.call(this.data, this.index).concat([].slice.call(this.data, 0, this.index));
  389. };
  390. CircularQueue.prototype.clear = function()
  391. {
  392. this.data = [];
  393. this.index = 0;
  394. };
  395. /**
  396. * @param {Array} new_data
  397. */
  398. CircularQueue.prototype.set = function(new_data)
  399. {
  400. this.data = new_data;
  401. this.index = 0;
  402. };
  403. function dump_file(ab, name)
  404. {
  405. if(!(ab instanceof Array))
  406. {
  407. ab = [ab];
  408. }
  409. var blob = new Blob(ab);
  410. download(blob, name);
  411. }
  412. function download(file_or_blob, name)
  413. {
  414. var a = document.createElement("a");
  415. a["download"] = name;
  416. a.href = window.URL.createObjectURL(file_or_blob);
  417. a.dataset["downloadurl"] = ["application/octet-stream", a["download"], a.href].join(":");
  418. if(document.createEvent)
  419. {
  420. var ev = document.createEvent("MouseEvent");
  421. ev.initMouseEvent("click", true, true, window,
  422. 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  423. a.dispatchEvent(ev);
  424. }
  425. else
  426. {
  427. a.click();
  428. }
  429. window.URL.revokeObjectURL(a.href);
  430. }
  431. /**
  432. * A simple 1d bitmap
  433. * @constructor
  434. */
  435. v86util.Bitmap = function(length_or_buffer)
  436. {
  437. if(typeof length_or_buffer === "number")
  438. {
  439. this.view = new Uint8Array(length_or_buffer + 7 >> 3);
  440. }
  441. else if(length_or_buffer instanceof ArrayBuffer)
  442. {
  443. this.view = new Uint8Array(length_or_buffer);
  444. }
  445. else
  446. {
  447. console.assert(false);
  448. }
  449. };
  450. v86util.Bitmap.prototype.set = function(index, value)
  451. {
  452. const bit_index = index & 7;
  453. const byte_index = index >> 3;
  454. const bit_mask = 1 << bit_index;
  455. this.view[byte_index] =
  456. value ? this.view[byte_index] | bit_mask : this.view[byte_index] & ~bit_mask;
  457. };
  458. v86util.Bitmap.prototype.get = function(index)
  459. {
  460. const bit_index = index & 7;
  461. const byte_index = index >> 3;
  462. return this.view[byte_index] >> bit_index & 1;
  463. };
  464. v86util.Bitmap.prototype.get_buffer = function()
  465. {
  466. return this.view.buffer;
  467. };