apple2js/js/ram.ts
Ian Flanigan c4df78cf06
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 08:43:48 -08:00

64 lines
1.9 KiB
TypeScript

/* Copyright 2010-2019 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.
*/
import { base64_decode, base64_encode } from './base64';
import { byte, memory } from './types';
import { allocMemPages } from './util';
export interface State {
/** Start of memory region. */
start: byte;
/** End of memory region. */
end: byte;
/** Base64-encoded contents. */
mem: string;
};
/**
* Represents RAM from the start page `sp` to end page `ep`. The memory
* is addressed by `page` and `offset`.
*/
export default function RAM(sp: byte, ep: byte) {
let start_page = sp;
let end_page = ep;
let mem = allocMemPages(ep - sp + 1);
return {
start: function () {
return start_page;
},
end: function () {
return end_page;
},
read: function (page: byte, offset: byte) {
return mem[(page - start_page) << 8 | offset];
},
write: function (page: byte, offset: byte, val: byte) {
mem[(page - start_page) << 8 | offset] = val;
},
getState: function (): State {
return {
start: start_page,
end: end_page,
mem: base64_encode(mem)
};
},
setState: function (state: State) {
start_page = state.start;
end_page = state.end;
mem = base64_decode(state.mem);
}
};
}