UNPKG

nes-emu

Version:

A NES emulator

50 lines (46 loc) 1.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _MemoryChunk = _interopRequireDefault(require("./MemoryChunk")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /** * A memory segment that accepts a `mapping` object which translates internal addresses. * For example, in PPU's Palette RAM, $10/$14/$18/$1C are mirrors of $00/$04/$08/$0C, so: * mapping = { 0x10: 0x00, 0x14: 0x04, 0x18: 0x08, 0x1c: 0x0c } */ class RewiredMemoryChunk extends _MemoryChunk.default { constructor(bytes, mapping) { super(bytes); this.mapping = mapping; } /** * Creates a mapping object from a `ranges` array. * For example: * The list [{ from: 10, size: 5, to: 1 }]... * ...creates the mapping { 10: 1, 11: 2, 12: 3, 13: 4, 14: 5 } */ static createMapping(ranges) { const mapping = {}; for (let range of ranges) { for (let i = 0; i < range.size; i++) { mapping[range.from + i] = range.to + i; } } return mapping; } /** Reads a byte from `address`, following mirroring rules. */ readAt(address) { if (this.mapping[address] != null) address = this.mapping[address]; this._assertValidAddress(address); return this.bytes[address]; } /** Writes a `byte` to `address`, following mirroring rules. */ writeAt(address, byte) { if (this.mapping[address] != null) address = this.mapping[address]; this._assertValidAddress(address); this.bytes[address] = byte; } } exports.default = RewiredMemoryChunk;