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