apple2js/js/util.ts

140 lines
3.8 KiB
TypeScript
Raw Normal View History

2019-03-01 05:21:18 +00:00
/* Copyright 2010-2019 Will Scullin <scullin@scullinsteel.com>
*
* 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
2020-11-07 23:49:05 +00:00
* software for any purpose. It is provided ' as is' without express or
* implied warranty.
*/
2020-11-07 23:49:05 +00:00
import { byte, memory, word } from './types';
2016-11-22 05:17:34 +00:00
/*eslint no-console: 0*/
const hex_digits = '0123456789ABCDEF';
const bin_digits = '01';
/** Returns a random byte. */
function garbage(): byte {
return (Math.random() * 0x100) & 0xff;
}
export const testables = {
garbage
};
/**
* Returns an array or Uint8Array of `size` bytes filled as if the computer
* was just powered on.
*/
export function allocMem(size: number) {
let result: number[] | Uint8Array;
if (window.Uint8Array) {
result = new Uint8Array(size);
} else {
result = new Array(size);
}
2020-11-07 23:49:05 +00:00
for (let idx = 0; idx < size; idx++) {
2017-09-23 04:42:57 +00:00
result[idx] = (idx & 0x02) ? 0x00 : 0xff;
}
// Borrowed from AppleWin (https://github.com/AppleWin/AppleWin)
for (let idx = 0; idx < size; idx += 0x200) {
result[idx + 0x28] = garbage();
result[idx + 0x29] = garbage();
result[idx + 0x68] = garbage();
result[idx + 0x69] = garbage();
}
return result;
}
/** Returns an array or Uint8Array of 256 * `pages` bytes. */
export function allocMemPages(pages: number): memory {
2019-11-24 03:05:52 +00:00
return allocMem(pages << 8);
}
/** Returns a new Uint8Array for the input array. */
export function bytify(ary: number[]): memory {
let result: number[] | Uint8Array = ary;
2014-06-26 13:46:01 +00:00
if (window.Uint8Array) {
result = new Uint8Array(ary);
}
return result;
}
/** Writes to the console. */
2020-11-07 23:49:05 +00:00
export function debug(...args: any[]): void {
2020-11-08 20:26:12 +00:00
console.log.apply(console, args);
}
/**
* Returns a string of hex digits (all caps).
* @param v the value to encode
* @param n the number of nibbles. If `n` is missing, it is guessed from the value
* of `v`. If `v` < 256, it is assumed to be 2 nibbles, otherwise 4.
*/
export function toHex(v: byte | word | number, n?: number) {
if (!n) {
n = v < 256 ? 2 : 4;
}
2020-11-07 23:49:05 +00:00
let result = '';
for (let idx = 0; idx < n; idx++) {
result = hex_digits[v & 0x0f] + result;
v >>= 4;
}
return result;
}
/**
* Returns a string of 8 binary digits.
* @param v the value to encode
*/
export function toBinary(v: byte) {
2020-11-07 23:49:05 +00:00
let result = '';
for (let idx = 0; idx < 8; idx++) {
result = bin_digits[v & 0x01] + result;
v >>= 1;
}
return result;
}
/**
* Returns the value of a query parameter or the empty string if it does not
* exist.
* @param name the parameter name. Note that `name` must not have any RegExp
* meta-characters except '[' and ']' or it will fail.
*/
// From http://www.netlobo.com/url_query_string_javascript.html
export function gup(name: string) {
name = name.replace(/[[]/, '\\[').replace(/[\]]/, '\\]');
2020-11-07 23:49:05 +00:00
const regexS = '[\\?&]' + name + '=([^&#]*)';
const regex = new RegExp(regexS);
const results = regex.exec(window.location.href);
if (!results)
2016-11-22 05:17:34 +00:00
return '';
else
return results[1];
}
/** Returns the URL fragment. */
2019-03-01 05:21:18 +00:00
export function hup() {
2020-11-07 23:49:05 +00:00
const regex = new RegExp('#(.*)');
const results = regex.exec(window.location.hash);
if (!results)
2016-11-22 05:17:34 +00:00
return '';
else
return results[1];
}
2016-11-22 05:17:34 +00:00
/** Packs a 32-bit integer into a string in little-endian order. */
export function numToString(num: number) {
let result = '';
for (let idx = 0; idx < 4; idx++) {
result += String.fromCharCode(num & 0xff);
num >>= 8;
}
return result;
}