apple1js/js/util.ts

68 lines
1.7 KiB
TypeScript
Raw Normal View History

2023-08-06 01:38:18 +00:00
/* Copyright 2010-2023 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
* software for any purpose. It is provided "as is" without express or
* implied warranty.
*/
2023-08-06 14:32:11 +00:00
import { byte, word } from './types';
2023-08-06 01:38:18 +00:00
2023-08-06 14:32:11 +00:00
const hex_digits = '0123456789ABCDEF';
const bin_digits = '01';
2023-08-06 01:38:18 +00:00
export function allocMem(size: word) {
2023-08-06 14:32:11 +00:00
let result;
2023-08-06 01:38:18 +00:00
if (window.Uint8Array) {
result = new Uint8Array(size);
} else {
result = new Array(size);
}
return result;
}
export function allocMemPages(pages: byte) {
return allocMem(pages * 0x100);
}
export function debug(...msg: unknown[]) {
/*eslint no-console: 0 */
console.log(...msg);
}
export function toHex(v: byte, n?: 2 | 4) {
if (!n) {
n = v < 256 ? 2 : 4;
}
2023-08-06 14:32:11 +00:00
let result = '';
for (let idx = 0; idx < n; idx++) {
2023-08-06 01:38:18 +00:00
result = hex_digits[v & 0x0f] + result;
v >>= 4;
}
return result;
}
export function toBinary(v: byte) {
2023-08-06 14:32:11 +00:00
let result = '';
for (let idx = 0; idx < 8; idx++) {
2023-08-06 01:38:18 +00:00
result = bin_digits[v & 0x01] + result;
v >>= 1;
}
return result;
}
export function gup(name: string) {
const params = new URLSearchParams(window.location.search);
return params.get(name);
}
export function hup() {
2023-08-06 14:32:11 +00:00
const regex = new RegExp('#(.*)');
const results = regex.exec(window.location.hash);
if (!results) return '';
2023-08-06 01:38:18 +00:00
else return decodeURIComponent(results[1]);
}