Typescript conversion of several files, including js/cpu6502 (#38)
* Convert `js/util.js` to Typescript and add tests
Besides converting `js/util.js` to Typescript, this change also adds
`js/types.ts` that defines common types used in apple2js. Some of
these types, like `byte` and `word` are for information only.
* Convert `js/base64.js` to Typescript
This also adds a new type, `memory`, that is either an array of
numbers, or a Uint8Array.
* Convert `js/ram.js` to Typescript
This change does not convert `RAM` to a class; it just introduces types.
* Basic typing of cpu6502
This is a really rough first pass. There are some problems that can't
be fixed until this is turned into a real class, but at least all of
the function arguments are now typed. This caught a few cases where
extra arguments were being passed in.
* Convert `js/cpu6502` to a class
In theory, idiomatic classes should be better than the previous
closure-based classes. However, this conversion shows that the
instruction table does not fit well with idiomatic classes as method
referenced in the table need to be called with the correct `this`
everywhere.
This should, at best, be considered a first attempt.
2020-11-01 16:43:48 +00:00
|
|
|
|
2021-02-08 04:50:50 +00:00
|
|
|
/**
|
|
|
|
* Extracts the members of a constant array as a type. Used as:
|
2021-02-22 02:38:21 +00:00
|
|
|
*
|
2021-02-08 04:50:50 +00:00
|
|
|
* @example
|
|
|
|
* const SOME_VALUES = ['a', 'b', 1, 2] as const;
|
|
|
|
* type SomeValues = MemberOf<typeof SOME_VALUES>; // 'a' | 'b' | 1 | 2
|
|
|
|
*/
|
|
|
|
export type MemberOf<T extends ReadonlyArray<unknown>> =
|
|
|
|
T extends ReadonlyArray<infer E> ? E : never;
|
|
|
|
|
2021-03-28 23:39:18 +00:00
|
|
|
/**
|
|
|
|
* Recursively extracts all members of a constant array as a type. Used as:
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* const SOME_ARRAYS = [['a'],['b', 2], 3] as const;
|
|
|
|
* type SomeArrayValues = DeepMemberOf<typeof SOME_ARRAYS>; // 'a' | 'b' | 2 | 3
|
|
|
|
*/
|
|
|
|
export type DeepMemberOf<T extends ReadonlyArray<unknown>> =
|
|
|
|
T extends ReadonlyArray<infer E>
|
|
|
|
? (E extends ReadonlyArray<unknown> ? DeepMemberOf<E> : E)
|
|
|
|
: never;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extracts the declared keys of a type by removing `string` and `number`.
|
|
|
|
*
|
|
|
|
* Cribbed from the interwebs:
|
|
|
|
* https://github.com/microsoft/TypeScript/issues/25987#issuecomment-408339599
|
|
|
|
*/
|
|
|
|
export type KnownKeys<T> = {
|
|
|
|
[K in keyof T]: string extends K ? never : number extends K ? never : K
|
|
|
|
} extends { [_ in keyof T]: infer U } ? U : never;
|
|
|
|
|
|
|
|
|
2021-02-08 04:50:50 +00:00
|
|
|
/** A bit. */
|
|
|
|
export type bit = 0 | 1;
|
|
|
|
|
|
|
|
/** A nibble. */
|
|
|
|
export type nibble =
|
|
|
|
0x0 | 0x1 | 0x2 | 0x3 | 0x4 | 0x5 | 0x6 | 0x7 |
|
|
|
|
0x8 | 0x9 | 0xa | 0xb | 0xc | 0xd | 0xe | 0xf;
|
|
|
|
|
Typescript conversion of several files, including js/cpu6502 (#38)
* Convert `js/util.js` to Typescript and add tests
Besides converting `js/util.js` to Typescript, this change also adds
`js/types.ts` that defines common types used in apple2js. Some of
these types, like `byte` and `word` are for information only.
* Convert `js/base64.js` to Typescript
This also adds a new type, `memory`, that is either an array of
numbers, or a Uint8Array.
* Convert `js/ram.js` to Typescript
This change does not convert `RAM` to a class; it just introduces types.
* Basic typing of cpu6502
This is a really rough first pass. There are some problems that can't
be fixed until this is turned into a real class, but at least all of
the function arguments are now typed. This caught a few cases where
extra arguments were being passed in.
* Convert `js/cpu6502` to a class
In theory, idiomatic classes should be better than the previous
closure-based classes. However, this conversion shows that the
instruction table does not fit well with idiomatic classes as method
referenced in the table need to be called with the correct `this`
everywhere.
This should, at best, be considered a first attempt.
2020-11-01 16:43:48 +00:00
|
|
|
/** A byte (0..255). This is not enforced by the compiler. */
|
|
|
|
export type byte = number;
|
|
|
|
|
|
|
|
/** A word (0..65535). This is not enforced by the compiler. */
|
|
|
|
export type word = number;
|
|
|
|
|
2020-11-24 16:48:14 +00:00
|
|
|
/** A raw region of memory. */
|
2021-02-08 04:50:50 +00:00
|
|
|
export type memory = Uint8Array;
|
|
|
|
|
|
|
|
/** A raw region of memory. */
|
|
|
|
export type rom = ReadonlyUint8Array;
|
Typescript conversion of several files, including js/cpu6502 (#38)
* Convert `js/util.js` to Typescript and add tests
Besides converting `js/util.js` to Typescript, this change also adds
`js/types.ts` that defines common types used in apple2js. Some of
these types, like `byte` and `word` are for information only.
* Convert `js/base64.js` to Typescript
This also adds a new type, `memory`, that is either an array of
numbers, or a Uint8Array.
* Convert `js/ram.js` to Typescript
This change does not convert `RAM` to a class; it just introduces types.
* Basic typing of cpu6502
This is a really rough first pass. There are some problems that can't
be fixed until this is turned into a real class, but at least all of
the function arguments are now typed. This caught a few cases where
extra arguments were being passed in.
* Convert `js/cpu6502` to a class
In theory, idiomatic classes should be better than the previous
closure-based classes. However, this conversion shows that the
instruction table does not fit well with idiomatic classes as method
referenced in the table need to be called with the correct `this`
everywhere.
This should, at best, be considered a first attempt.
2020-11-01 16:43:48 +00:00
|
|
|
|
2020-11-24 16:48:14 +00:00
|
|
|
export interface Memory {
|
2021-02-08 04:50:50 +00:00
|
|
|
/** Read a byte. */
|
|
|
|
read(page: byte, offset: byte): byte;
|
|
|
|
/** Write a byte. */
|
|
|
|
write(page: byte, offset: byte, value: byte): void;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
|
2021-03-06 23:04:13 +00:00
|
|
|
/** A mapped region of memory. */
|
|
|
|
export interface MemoryPages extends Memory {
|
|
|
|
/** Start page. */
|
|
|
|
start(): byte;
|
|
|
|
/** End page, inclusive. */
|
|
|
|
end(): byte;
|
|
|
|
}
|
|
|
|
|
2021-02-22 02:38:21 +00:00
|
|
|
/* An interface card */
|
2021-02-28 03:17:36 +00:00
|
|
|
export interface Card extends Memory, Restorable {
|
2021-02-22 02:38:21 +00:00
|
|
|
/* Reset the card */
|
2021-03-13 21:18:32 +00:00
|
|
|
reset?(): void;
|
2021-02-22 02:38:21 +00:00
|
|
|
|
|
|
|
/* Draw card to canvas */
|
2021-03-15 19:51:40 +00:00
|
|
|
blit?(): ImageData | undefined;
|
2021-02-22 02:38:21 +00:00
|
|
|
|
|
|
|
/* Process period events */
|
|
|
|
tick?(): void;
|
|
|
|
|
|
|
|
/* Read or Write an I/O switch */
|
|
|
|
ioSwitch(off: byte, val?: byte): byte | undefined;
|
|
|
|
}
|
|
|
|
|
2021-02-08 04:50:50 +00:00
|
|
|
export const DISK_FORMATS = [
|
|
|
|
'2mg',
|
|
|
|
'd13',
|
|
|
|
'do',
|
|
|
|
'dsk',
|
|
|
|
'hdv',
|
|
|
|
'po',
|
|
|
|
'nib',
|
|
|
|
'woz'
|
|
|
|
] as const;
|
|
|
|
|
|
|
|
export type DiskFormat = MemberOf<typeof DISK_FORMATS>;
|
Typescript conversion of several files, including js/cpu6502 (#38)
* Convert `js/util.js` to Typescript and add tests
Besides converting `js/util.js` to Typescript, this change also adds
`js/types.ts` that defines common types used in apple2js. Some of
these types, like `byte` and `word` are for information only.
* Convert `js/base64.js` to Typescript
This also adds a new type, `memory`, that is either an array of
numbers, or a Uint8Array.
* Convert `js/ram.js` to Typescript
This change does not convert `RAM` to a class; it just introduces types.
* Basic typing of cpu6502
This is a really rough first pass. There are some problems that can't
be fixed until this is turned into a real class, but at least all of
the function arguments are now typed. This caught a few cases where
extra arguments were being passed in.
* Convert `js/cpu6502` to a class
In theory, idiomatic classes should be better than the previous
closure-based classes. However, this conversion shows that the
instruction table does not fit well with idiomatic classes as method
referenced in the table need to be called with the correct `this`
everywhere.
This should, at best, be considered a first attempt.
2020-11-01 16:43:48 +00:00
|
|
|
|
|
|
|
export interface Drive {
|
2021-02-08 04:50:50 +00:00
|
|
|
format: DiskFormat,
|
|
|
|
volume: number,
|
|
|
|
tracks: Array<byte[] | Uint8Array>,
|
|
|
|
trackMap: unknown,
|
2020-11-07 23:49:05 +00:00
|
|
|
}
|
Typescript conversion of several files, including js/cpu6502 (#38)
* Convert `js/util.js` to Typescript and add tests
Besides converting `js/util.js` to Typescript, this change also adds
`js/types.ts` that defines common types used in apple2js. Some of
these types, like `byte` and `word` are for information only.
* Convert `js/base64.js` to Typescript
This also adds a new type, `memory`, that is either an array of
numbers, or a Uint8Array.
* Convert `js/ram.js` to Typescript
This change does not convert `RAM` to a class; it just introduces types.
* Basic typing of cpu6502
This is a really rough first pass. There are some problems that can't
be fixed until this is turned into a real class, but at least all of
the function arguments are now typed. This caught a few cases where
extra arguments were being passed in.
* Convert `js/cpu6502` to a class
In theory, idiomatic classes should be better than the previous
closure-based classes. However, this conversion shows that the
instruction table does not fit well with idiomatic classes as method
referenced in the table need to be called with the correct `this`
everywhere.
This should, at best, be considered a first attempt.
2020-11-01 16:43:48 +00:00
|
|
|
|
|
|
|
export interface DiskIIDrive extends Drive {
|
2021-02-08 04:50:50 +00:00
|
|
|
rawTracks: unknown,
|
|
|
|
track: number,
|
|
|
|
head: number,
|
|
|
|
phase: number,
|
|
|
|
readOnly: boolean,
|
|
|
|
dirty: boolean,
|
2020-11-07 23:49:05 +00:00
|
|
|
}
|
2020-11-24 16:48:14 +00:00
|
|
|
|
2021-02-22 02:38:21 +00:00
|
|
|
export type TapeData = Array<[duration: number, high: boolean]>;
|
|
|
|
|
2021-02-28 03:17:36 +00:00
|
|
|
export interface Restorable<T = any> {
|
2021-02-08 04:50:50 +00:00
|
|
|
getState(): T;
|
|
|
|
setState(state: T): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read-only typed arrays for constants
|
|
|
|
export type TypedArrayMutableProperties = 'copyWithin' | 'fill' | 'reverse' | 'set' | 'sort';
|
|
|
|
export interface ReadonlyUint8Array extends Omit<Uint8Array, TypedArrayMutableProperties> {
|
|
|
|
readonly [n: number]: number
|
2021-02-22 02:38:21 +00:00
|
|
|
}
|
2021-03-15 19:51:40 +00:00
|
|
|
|
|
|
|
// Readonly RGB color value
|
|
|
|
export type Color = readonly [r: byte, g: byte, b: byte];
|