2020-11-07 23:49:05 +00:00
|
|
|
import { byte, memory, word } from './types';
|
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
|
|
|
|
2016-11-22 05:17:34 +00:00
|
|
|
/*eslint no-console: 0*/
|
2013-10-10 18:03:07 +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
|
|
|
const hex_digits = '0123456789ABCDEF';
|
|
|
|
const bin_digits = '01';
|
2013-10-10 18:03:07 +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
|
|
|
/** Returns a random byte. */
|
2021-03-09 03:28:52 +00:00
|
|
|
export function garbage(): byte {
|
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
|
|
|
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) {
|
2021-02-08 04:50:50 +00:00
|
|
|
const result = new Uint8Array(size);
|
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
|
|
|
for (let 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)
|
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
|
|
|
for (let idx = 0; idx < size; idx += 0x200) {
|
2019-02-19 04:42:50 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/** 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);
|
2013-10-10 18:03:07 +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
|
|
|
/** Returns a new Uint8Array for the input array. */
|
|
|
|
export function bytify(ary: number[]): memory {
|
2021-02-08 04:50:50 +00:00
|
|
|
return new Uint8Array(ary);
|
2014-06-26 13:46:01 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 01:10:06 +00:00
|
|
|
/** Returns a new Uint8Array with the concatenated data from the inputs. */
|
|
|
|
export function concat(...arys: Array<byte[] | Uint8Array>) {
|
|
|
|
const result = new Uint8Array(arys.reduce((l, ary) => l + ary.length, 0));
|
|
|
|
let offset = 0;
|
|
|
|
for (let i = 0; i < arys.length; i++) {
|
|
|
|
result.set(arys[i], offset);
|
|
|
|
offset += arys[i].length;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/** Writes to the console. */
|
2022-05-18 02:08:28 +00:00
|
|
|
export function debug(...args: unknown[]): void {
|
2022-05-31 15:38:40 +00:00
|
|
|
console.log(...args);
|
2013-10-10 18:03:07 +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
|
|
|
/**
|
|
|
|
* 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) {
|
2013-10-10 18:03:07 +00:00
|
|
|
if (!n) {
|
|
|
|
n = v < 256 ? 2 : 4;
|
|
|
|
}
|
2020-11-07 23:49:05 +00:00
|
|
|
let result = '';
|
|
|
|
for (let idx = 0; idx < n; idx++) {
|
2013-10-10 18:03:07 +00:00
|
|
|
result = hex_digits[v & 0x0f] + result;
|
|
|
|
v >>= 4;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* 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++) {
|
2013-10-10 18:03:07 +00:00
|
|
|
result = bin_digits[v & 0x01] + result;
|
|
|
|
v >>= 1;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|