nes-emu
Version:
A NES emulator
67 lines (61 loc) • 1.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _helpers = require("../helpers");
/** An 8-bit register with multiple status flags and values that live in RAM. */
class InMemoryRegister {
constructor() {
_helpers.WithContext.apply(this);
this.memorySize = 1;
this.value = 0;
this._readOnlyFields = [];
}
/** Adds a field of `size` bits named `name`, starting at `startPosition`. */
addField(name, startPosition) {
let size = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
Object.defineProperty(this, name, {
get() {
return _helpers.Byte.getBits(this.value, startPosition, size);
},
set(value) {
this.value = _helpers.Byte.force8Bit(_helpers.Byte.setBits(this.value, startPosition, size, value));
}
});
return this;
}
/** Adds a read-only field of `size` bits named `name`, starting at `startPosition`. */
addReadOnlyField(name, startPosition) {
let size = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
// (this performs better than `addField`)
this._readOnlyFields.push({
name,
startPosition,
size
});
this[name] = 0;
return this;
}
/** Sets the value manually (updating internal accessors). */
setValue(value) {
this.value = _helpers.Byte.force8Bit(value);
this._writeReadOnlyFields();
}
/** Returns the actual value. */
readAt() {
return this.value;
}
/** Sets the actual value. */
writeAt(__, byte) {
this.setValue(byte);
}
_writeReadOnlyFields() {
for (let {
name,
startPosition,
size
} of this._readOnlyFields) this[name] = _helpers.Byte.getBits(this.value, startPosition, size);
}
}
exports.default = InMemoryRegister;