apple2js/js/util.ts

92 lines
2.3 KiB
TypeScript
Raw Normal View History

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. */
2021-03-09 03:28:52 +00:00
export 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) {
Convert `cards/disk2.js` to Typescript (#54) * Convert `cards/disk2.js` to Typescript This is mostly a straightforward conversion of `cards/disk2.js` to Typescript, with the following exceptions: * `setState()` did not restore the drive light state correctly because the callback was called with the old `on` value. * `setPhase()` did not work for WOZ images. * `getBinary()` did not work for `nib` files. * `getBase64()` did not work for `nib` files and maybe didn't work right at all. Even with these fixes, local storage still doesn't work correctly. I have also added several TODOs where methods don't support WOZ disks. * Convert most uses of `memory` to `Uint8Array` There are many places in the existing code where we use `Uint8Array` directly. This change merely makes the `memory` type equivalent to `Uint8Array`. This change also changes most ROM data to be read-only in Typescript to ensure that it is not modified by mistake. This can't be done just by applying `as const` to the declaration because `Uint8Array`s are can not be expressed as literals. Instead, we create a new type, `ReadonlyUint8Array` that drops the mutation methods and makes indexed access read-only. See https://www.growingwiththeweb.com/2020/10/typescript-readonly-typed-arrays.html for details. * Tighten types and document `disk2.ts` While trying to understand the Disk ][ emulation, I tighted the types and documented the parts that I could, including references to other sources, like _Understanding the Apple //e_ by Jim Sather. The one functional change is the addition of the P6 ROM of DOS 3.2 and earlier. This is automatically selected if the card is initialized for 13 sector disks.
2021-02-08 04:50:50 +00:00
const result = new Uint8Array(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 {
Convert `cards/disk2.js` to Typescript (#54) * Convert `cards/disk2.js` to Typescript This is mostly a straightforward conversion of `cards/disk2.js` to Typescript, with the following exceptions: * `setState()` did not restore the drive light state correctly because the callback was called with the old `on` value. * `setPhase()` did not work for WOZ images. * `getBinary()` did not work for `nib` files. * `getBase64()` did not work for `nib` files and maybe didn't work right at all. Even with these fixes, local storage still doesn't work correctly. I have also added several TODOs where methods don't support WOZ disks. * Convert most uses of `memory` to `Uint8Array` There are many places in the existing code where we use `Uint8Array` directly. This change merely makes the `memory` type equivalent to `Uint8Array`. This change also changes most ROM data to be read-only in Typescript to ensure that it is not modified by mistake. This can't be done just by applying `as const` to the declaration because `Uint8Array`s are can not be expressed as literals. Instead, we create a new type, `ReadonlyUint8Array` that drops the mutation methods and makes indexed access read-only. See https://www.growingwiththeweb.com/2020/10/typescript-readonly-typed-arrays.html for details. * Tighten types and document `disk2.ts` While trying to understand the Disk ][ emulation, I tighted the types and documented the parts that I could, including references to other sources, like _Understanding the Apple //e_ by Jim Sather. The one functional change is the addition of the P6 ROM of DOS 3.2 and earlier. This is automatically selected if the card is initialized for 13 sector disks.
2021-02-08 04:50:50 +00:00
return new Uint8Array(ary);
2014-06-26 13:46:01 +00:00
}
/** Writes to the console. */
2022-05-18 02:08:28 +00:00
export function debug(...args: unknown[]): void {
console.log(...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;
}
/** 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;
}