nes-emu
Version:
A NES emulator
71 lines (67 loc) • 2.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _memory = require("../memory");
var _mirroring = _interopRequireDefault(require("./mirroring"));
var _helpers = require("../helpers");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/** The PPU memory map. Address space size: 16KB. */
class PPUMemoryMap {
constructor() {
_helpers.WithContext.apply(this);
_memory.WithCompositeMemory.apply(this);
}
/** When a context is loaded. */
onLoad(_ref) {
let {
cartridge,
mapper
} = _ref;
// (the system only has memory for two Name tables, the other two are mirrored)
this.nameTables = new _memory.RewiredMemoryChunk(0x1000);
this.changeNameTablesMirroringTo(cartridge.header.mirroring);
const nameTablesMirror = new _memory.MemoryMirror(this.nameTables, 0x0f00);
this.paletteRam = new _memory.RewiredMemoryChunk(0x20, {
// ($3F10/$3F14/$3F18/$3F1C are mirrors of $3F00/$3F04/$3F08/$3F0C)
0x10: 0x00,
0x14: 0x04,
0x18: 0x08,
0x1c: 0x0c
});
const paletteRamMirror = new _memory.MemoryMirror(this.paletteRam, 0x00e0);
this.defineChunks([
// Address range Size Device
mapper.segments.ppu,
// $0000-$1FFF $2000 Pattern tables 0 and 1 (mapper)
this.nameTables,
// $2000-$2FFF $1000 Name tables 0 to 3 (VRAM + mirror)
nameTablesMirror,
// $3000-$3EFF $0F00 Mirrors of $2000-$2EFF
this.paletteRam,
// $3F00-$3F1F $0020 Palette RAM indexes
paletteRamMirror // $3F20-$3FFF $00E0 Mirrors of $3F00-$3F1F
]);
}
changeNameTablesMirroringTo(mirroringType) {
if (this.context.cartridge.header.mirroring === "FOUR_SCREENS") mirroringType = "FOUR_SCREENS";
this.nameTables.$mirroringType = mirroringType;
this.nameTables.mapping = _mirroring.default[mirroringType];
}
/** Returns a snapshot of the current state. */
getSaveState() {
return {
nameTables: Array.from(this.nameTables.bytes),
mirroringType: this.nameTables.$mirroringType,
paletteRam: Array.from(this.paletteRam.bytes)
};
}
/** Restores state from a snapshot. */
setSaveState(saveState) {
this.nameTables.bytes.set(saveState.nameTables);
this.changeNameTablesMirroringTo(saveState.mirroringType);
this.paletteRam.bytes.set(saveState.paletteRam);
}
}
exports.default = PPUMemoryMap;