broken-neees
Version:
A really broken NEEES emulator that introduces glitches and random bugs on purpose!
84 lines (77 loc) • 2.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _byte = _interopRequireDefault(require("./byte"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/** An 8-bit register with multiple status flags and values mapped to memory. */
class InMemoryRegister {
constructor() {
this.value = 0;
this._readOnlyFields = [];
this.onLoad();
}
/** Called when instantiating the register. */
onLoad() {}
/** Called when the CPU reads the memory address. */
onRead() {
return 0;
}
/** Called when the CPU writes the memory address. */
onWrite(value) {}
/** Sets the value manually (updating internal accessors). */
setValue(value) {
this.value = _byte.default.toU8(value);
this._writeReadOnlyFields();
}
/** Adds a read-only field of `size` bits named `name`, starting at `startPosition`. */
addField(name, startPosition) {
let size = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
this._readOnlyFields.push({
name,
startPosition,
size
});
this[name] = 0;
return this;
}
/** Adds a writable field of `size` bits named `name`, starting at `startPosition`. */
addWritableField(name, startPosition) {
let size = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
Object.defineProperty(this, name, {
get() {
return _byte.default.getBits(this.value, startPosition, size);
},
set(value) {
this.value = _byte.default.toU8(_byte.default.setBits(this.value, startPosition, size, value));
}
});
return this;
}
_writeReadOnlyFields() {
for (let {
name,
startPosition,
size
} of this._readOnlyFields) this[name] = _byte.default.getBits(this.value, startPosition, size);
}
static get PPU() {
return class PPUInMemoryRegister extends InMemoryRegister {
constructor(ppu) {
super();
this.ppu = ppu;
}
};
}
static get APU() {
return class APUInMemoryRegister extends InMemoryRegister {
constructor(apu, id) {
super();
this.apu = apu;
this.id = id;
}
};
}
}
exports.default = InMemoryRegister;