2021-02-28 03:17:36 +00:00
|
|
|
import { byte, memory, Memory, Restorable } from './types';
|
2019-03-01 05:21:18 +00:00
|
|
|
import { allocMemPages } from './util';
|
2016-11-22 05:17:34 +00:00
|
|
|
|
2021-02-28 03:17:36 +00:00
|
|
|
export interface RAMState {
|
|
|
|
/** Copy of contents. */
|
|
|
|
mem: memory;
|
2020-11-07 23:49:05 +00:00
|
|
|
}
|
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
|
|
|
/**
|
|
|
|
* Represents RAM from the start page `sp` to end page `ep`. The memory
|
|
|
|
* is addressed by `page` and `offset`.
|
|
|
|
*/
|
2021-02-28 03:17:36 +00:00
|
|
|
export default class RAM implements Memory, Restorable<RAMState> {
|
2020-11-24 16:48:14 +00:00
|
|
|
private start_page: byte;
|
|
|
|
private end_page: byte;
|
|
|
|
private mem: memory;
|
|
|
|
|
|
|
|
constructor(sp: byte, ep: byte) {
|
|
|
|
this.start_page = sp;
|
|
|
|
this.end_page = ep;
|
|
|
|
|
|
|
|
this.mem = allocMemPages(ep - sp + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public start(): byte {
|
|
|
|
return this.start_page;
|
|
|
|
}
|
|
|
|
|
|
|
|
public end(): byte {
|
|
|
|
return this.end_page;
|
|
|
|
}
|
|
|
|
|
|
|
|
public read(page: byte, offset: byte) {
|
|
|
|
return this.mem[(page - this.start_page) << 8 | offset];
|
|
|
|
}
|
|
|
|
|
|
|
|
public write(page: byte, offset: byte, val: byte) {
|
|
|
|
this.mem[(page - this.start_page) << 8 | offset] = val;
|
|
|
|
}
|
|
|
|
|
2021-02-28 03:17:36 +00:00
|
|
|
public getState(): RAMState {
|
2020-11-24 16:48:14 +00:00
|
|
|
return {
|
2021-02-28 03:17:36 +00:00
|
|
|
mem: new Uint8Array(this.mem)
|
2020-11-24 16:48:14 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-28 03:17:36 +00:00
|
|
|
public setState(state: RAMState) {
|
|
|
|
this.mem = new Uint8Array(state.mem);
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
2013-10-10 18:03:07 +00:00
|
|
|
}
|