UNPKG

broken-neees

Version:

A really broken NEEES emulator that introduces glitches and random bugs on purpose!

136 lines (123 loc) 5.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } /** * An abstract class that represents a generic mapper. * It's connected to two different memory areas: * - CPU $4020-$FFFF (for PRG ROM, PRG RAM, and mapper registers) * - PPU $0000-$1FFF (for CHR ROM / Pattern tables) */ var Mapper = /*#__PURE__*/function () { function Mapper(cpu, ppu, cartridge) { _classCallCheck(this, Mapper); this.cpu = cpu; this.ppu = ppu; this.cartridge = cartridge; var prg = this.cartridge.prg(); var chr = this.cartridge.chr(); var totalPrgPages = Math.floor(prg.length / this.prgRomPageSize()); var totalChrPages = Math.floor(chr.length / this.chrRomPageSize()); this.prgPages = []; for (var i = 0; i < totalPrgPages; i++) this.prgPages.push(this._getPage(prg, this.prgRomPageSize(), i)); this.chrPages = []; for (var _i = 0; _i < totalChrPages; _i++) this.chrPages.push(this._getPage(chr, this.chrRomPageSize(), _i)); this.onLoad(); } /** Returns the PRG ROM page size (in bytes). */ _createClass(Mapper, [{ key: "prgRomPageSize", value: function prgRomPageSize() { return 16 * 1024; } /** Returns the CHR ROM page size (in bytes). */ }, { key: "chrRomPageSize", value: function chrRomPageSize() { return 8 * 1024; } /** Called when instantiating the mapper. */ }, { key: "onLoad", value: function onLoad() {} /** Maps a CPU read operation (`address` is in CPU range $4020-$FFFF). */ }, { key: "cpuRead", value: function cpuRead( /*address*/ ) { throw new Error("not_implemented"); } /** Maps a CPU write operation (`address` is in CPU range $4020-$FFFF). */ }, { key: "cpuWrite", value: function cpuWrite( /*address, value*/ ) { throw new Error("not_implemented"); } /** Maps a PPU read operation (`address` is in PPU range $0000-$1FFF). */ }, { key: "ppuRead", value: function ppuRead( /*address*/ ) { throw new Error("not_implemented"); } /** Maps a PPU write operation (`address` is in PPU range $0000-$1FFF). */ }, { key: "ppuWrite", value: function ppuWrite( /*address, value*/ ) { throw new Error("not_implemented"); } /** * Runs at cycle 260 of every scanline (including preline). */ }, { key: "tick", value: function tick() {} /** Returns a snapshot of the current state. */ }, { key: "getSaveState", value: function getSaveState() { return { chrPages: this.cartridge.header.usesChrRam ? this.chrPages.map(function (it) { return Array.from(it); }) : null }; } /** Restores state from a snapshot. */ }, { key: "setSaveState", value: function setSaveState(saveState) { if (saveState.chrPages != null) this.chrPages = saveState.chrPages.map(function (it) { return new Uint8Array(it); }); } /** Returns a PRG `page`, wrapping if needed. */ }, { key: "$getPrgPage", value: function $getPrgPage(page) { return this.prgPages[Math.max(0, page % this.prgPages.length)]; } /** Returns a CHR `page`, wrapping if needed. */ }, { key: "$getChrPage", value: function $getChrPage(page) { return this.chrPages[Math.max(0, page % this.chrPages.length)]; } }, { key: "_getPage", value: function _getPage(memory, pageSize, page) { var offset = page * pageSize; return memory.slice(offset, offset + pageSize); } }]); return Mapper; }(); exports.default = Mapper;