apple1js/test/util/memory.ts

61 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-08-06 14:32:11 +00:00
import { MemoryPages, byte, word } from "js/types";
import { assertByte } from "./asserts";
2023-08-06 13:20:11 +00:00
2023-08-06 14:32:11 +00:00
export type Log = [address: word, value: byte, types: "read" | "write"];
2023-08-06 13:20:11 +00:00
export class TestMemory implements MemoryPages {
2023-08-06 14:32:11 +00:00
private data: Buffer;
private logging: boolean = false;
private log: Log[] = [];
2023-08-06 13:20:11 +00:00
2023-08-06 14:32:11 +00:00
constructor(private size: number) {
this.data = Buffer.alloc(size << 8);
}
2023-08-06 13:20:11 +00:00
2023-08-06 14:32:11 +00:00
start() {
return 0;
}
2023-08-06 13:20:11 +00:00
2023-08-06 14:32:11 +00:00
end() {
return this.size - 1;
}
2023-08-06 13:20:11 +00:00
2023-08-06 14:32:11 +00:00
read(page: byte, off: byte) {
assertByte(page);
assertByte(off);
2023-08-06 13:20:11 +00:00
2023-08-06 14:32:11 +00:00
const val = this.data[(page << 8) | off];
if (this.logging) {
this.log.push([(page << 8) | off, val, "read"]);
2023-08-06 13:20:11 +00:00
}
2023-08-06 14:32:11 +00:00
return val;
}
2023-08-06 13:20:11 +00:00
2023-08-06 14:32:11 +00:00
write(page: byte, off: byte, val: byte) {
assertByte(page);
assertByte(off);
assertByte(val);
2023-08-06 13:20:11 +00:00
2023-08-06 14:32:11 +00:00
if (this.logging) {
this.log.push([(page << 8) | off, val, "write"]);
2023-08-06 13:20:11 +00:00
}
2023-08-06 14:32:11 +00:00
this.data[(page << 8) | off] = val;
}
2023-08-06 13:20:11 +00:00
2023-08-06 14:32:11 +00:00
reset() {
this.log = [];
}
2023-08-06 13:20:11 +00:00
2023-08-06 14:32:11 +00:00
logStart() {
this.log = [];
this.logging = true;
}
2023-08-06 13:20:11 +00:00
2023-08-06 14:32:11 +00:00
logStop() {
this.logging = false;
}
2023-08-06 13:20:11 +00:00
2023-08-06 14:32:11 +00:00
getLog() {
return this.log;
}
2023-08-06 13:20:11 +00:00
}