2019-03-01 05:21:18 +00:00
|
|
|
/* Copyright 2010-2019 Will Scullin <scullin@scullinsteel.com>
|
2013-10-10 18:03:07 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, distribute, and sell this software and its
|
|
|
|
* documentation for any purpose is hereby granted without fee, provided that
|
|
|
|
* the above copyright notice appear in all copies and that both that
|
|
|
|
* copyright notice and this permission notice appear in supporting
|
|
|
|
* documentation. No representations are made about the suitability of this
|
|
|
|
* software for any purpose. It is provided "as is" without express or
|
|
|
|
* implied warranty.
|
|
|
|
*/
|
|
|
|
|
2016-11-22 05:17:34 +00:00
|
|
|
/*eslint no-console: 0*/
|
2013-10-10 18:03:07 +00:00
|
|
|
|
2016-11-22 05:17:34 +00:00
|
|
|
var hex_digits = '0123456789ABCDEF';
|
|
|
|
var bin_digits = '01';
|
2013-10-10 18:03:07 +00:00
|
|
|
|
2019-03-01 05:21:18 +00:00
|
|
|
export function allocMem(size) {
|
2019-02-19 04:42:50 +00:00
|
|
|
function garbage() {
|
|
|
|
return (Math.random() * 0x100) & 0xff;
|
|
|
|
}
|
2013-10-10 18:03:07 +00:00
|
|
|
var result;
|
|
|
|
if (window.Uint8Array) {
|
|
|
|
result = new Uint8Array(size);
|
|
|
|
} else {
|
|
|
|
result = new Array(size);
|
|
|
|
}
|
2019-02-19 04:42:50 +00:00
|
|
|
var idx;
|
|
|
|
for (idx = 0; idx < size; idx++) {
|
2017-09-23 04:42:57 +00:00
|
|
|
result[idx] = (idx & 0x02) ? 0x00 : 0xff;
|
|
|
|
}
|
2019-02-19 04:42:50 +00:00
|
|
|
// Borrowed from AppleWin (https://github.com/AppleWin/AppleWin)
|
|
|
|
for(idx = 0; idx < size; idx += 0x200 ) {
|
|
|
|
result[idx + 0x28] = garbage();
|
|
|
|
result[idx + 0x29] = garbage();
|
|
|
|
result[idx + 0x68] = garbage();
|
|
|
|
result[idx + 0x69] = garbage();
|
|
|
|
}
|
2013-10-10 18:03:07 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-03-01 05:21:18 +00:00
|
|
|
export function allocMemPages(pages) {
|
2019-11-24 03:05:52 +00:00
|
|
|
return allocMem(pages << 8);
|
2013-10-10 18:03:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 05:21:18 +00:00
|
|
|
export function bytify(ary) {
|
2014-06-26 13:46:01 +00:00
|
|
|
var result = ary;
|
|
|
|
if (window.Uint8Array) {
|
|
|
|
result = new Uint8Array(ary);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-03-01 05:21:18 +00:00
|
|
|
export function debug() {
|
2020-01-02 19:11:04 +00:00
|
|
|
console.log.apply(console, arguments);
|
2013-10-10 18:03:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 05:21:18 +00:00
|
|
|
export function toHex(v, n) {
|
2013-10-10 18:03:07 +00:00
|
|
|
if (!n) {
|
|
|
|
n = v < 256 ? 2 : 4;
|
|
|
|
}
|
2016-11-22 05:17:34 +00:00
|
|
|
var result = '';
|
2013-10-10 18:03:07 +00:00
|
|
|
for (var idx = 0; idx < n; idx++) {
|
|
|
|
result = hex_digits[v & 0x0f] + result;
|
|
|
|
v >>= 4;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-03-01 05:21:18 +00:00
|
|
|
export function toBinary(v) {
|
2016-11-22 05:17:34 +00:00
|
|
|
var result = '';
|
2013-10-10 18:03:07 +00:00
|
|
|
for (var idx = 0; idx < 8; idx++) {
|
|
|
|
result = bin_digits[v & 0x01] + result;
|
|
|
|
v >>= 1;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// From http://www.netlobo.com/url_query_string_javascript.html
|
2019-03-01 05:21:18 +00:00
|
|
|
export function gup( name )
|
2013-10-10 18:03:07 +00:00
|
|
|
{
|
2017-12-03 04:45:02 +00:00
|
|
|
name = name.replace(/[[]/,'\\[').replace(/[\]]/,'\\]');
|
2016-11-22 05:17:34 +00:00
|
|
|
var regexS = '[\\?&]'+name+'=([^&#]*)';
|
|
|
|
var regex = new RegExp( regexS );
|
|
|
|
var results = regex.exec( window.location.href );
|
|
|
|
if( !results )
|
|
|
|
return '';
|
|
|
|
else
|
|
|
|
return results[1];
|
2013-10-10 18:03:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 05:21:18 +00:00
|
|
|
export function hup() {
|
2016-11-22 05:17:34 +00:00
|
|
|
var regex = new RegExp('#(.*)');
|
2013-10-10 18:03:07 +00:00
|
|
|
var results = regex.exec(window.location.hash);
|
|
|
|
if ( !results )
|
2016-11-22 05:17:34 +00:00
|
|
|
return '';
|
2013-10-10 18:03:07 +00:00
|
|
|
else
|
|
|
|
return results[1];
|
|
|
|
}
|
2016-11-22 05:17:34 +00:00
|
|
|
|
2020-01-02 19:11:04 +00:00
|
|
|
export function numToString(num) {
|
|
|
|
let result = '';
|
|
|
|
for (let idx = 0; idx < 4; idx++) {
|
|
|
|
result += String.fromCharCode(num & 0xff);
|
|
|
|
num >>= 8;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|