nes-emu
Version:
A NES emulator
46 lines (42 loc) • 1.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _WithLittleEndian = _interopRequireDefault(require("./WithLittleEndian"));
var _lodash = _interopRequireDefault(require("lodash"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/** A memory chunk that can store `bytes` (it can be a number or a Buffer). */
class MemoryChunk {
constructor(bytes) {
_WithLittleEndian.default.apply(this);
if (_lodash.default.isFinite(bytes)) bytes = new Uint8Array(bytes);
this.bytes = bytes;
this.readOnly = false;
this.memorySize = bytes.length;
}
/** Reads a byte from `address`. */
readAt(address) {
this._assertValidAddress(address);
return this.bytes[address];
}
/** Writes a `byte` to `address`. */
writeAt(address, byte) {
this._assertValidAddress(address);
if (this.readOnly) return;
this.bytes[address] = byte;
}
/** Sets the chunk's `readOnly` state. */
asReadOnly() {
let readOnly = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
this.readOnly = readOnly;
return this;
}
_assertValidAddress(address) {
if (address < 0 || address > this.memorySize) this._throwInvalidAddressError(address);
}
_throwInvalidAddressError(address) {
throw new Error("Invalid memory access at 0x".concat(address.toString(16), "."));
}
}
exports.default = MemoryChunk;