UNPKG

broken-neees

Version:

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

102 lines (93 loc) 5.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _mirroringTypes = _interopRequireDefault(require("../lib/ppu/mirroringTypes")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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); } var VRAM_SIZE = 4096; var PALETTE_RAM_SIZE = 32; var OAM_RAM_SIZE = 256; var PPUMemory = /*#__PURE__*/function () { function PPUMemory() { _classCallCheck(this, PPUMemory); this.vram = new Uint8Array(VRAM_SIZE); this.paletteRam = new Uint8Array(PALETTE_RAM_SIZE); this.oamRam = new Uint8Array(OAM_RAM_SIZE); this.mirroringId = null; } _createClass(PPUMemory, [{ key: "onLoad", value: function onLoad(cartridge, mapper) { this.cartridge = cartridge; this.mapper = mapper; this.changeNameTableMirroringTo(cartridge.header.mirroringId); } }, { key: "changeNameTableMirroringTo", value: function changeNameTableMirroringTo(mirroringId) { if (this.cartridge.header.mirroringId === "FOUR_SCREEN") mirroringId = "FOUR_SCREEN"; this.mirroringId = mirroringId; this._mirroring = _mirroringTypes.default[mirroringId]; } }, { key: "read", value: function read(address) { // Pattern tables 0 and 1 (mapper) if (address <= 0x1fff) return this.mapper.ppuRead(address); // Name tables 0 to 3 (VRAM + mirror) if (address >= 0x2000 && address <= 0x2fff) { if (address >= 0x2000 && address < 0x2400) return this.vram[this._mirroring.$2000 + address - 0x2000]; if (address >= 0x2400 && address < 0x2800) return this.vram[this._mirroring.$2400 + address - 0x2400]; if (address >= 0x2800 && address < 0x2c00) return this.vram[this._mirroring.$2800 + address - 0x2800]; if (address >= 0x2c00 && address < 0x3000) return this.vram[this._mirroring.$2C00 + address - 0x2c00]; } // Mirrors of $2000-$2EFF if (address >= 0x3000 && address <= 0xeff) return this.read(0x2000 + (address - 0x3000) % 0x1000); // Palette RAM indexes if (address >= 0x3f00 && address <= 0x3f1f) { if (address === 0x3f10) return this.read(0x3f00); if (address === 0x3f14) return this.read(0x3f04); if (address === 0x3f18) return this.read(0x3f08); if (address === 0x3f1c) return this.read(0x3f0c); return this.paletteRam[address - 0x3f00]; } // Mirrors of $3F00-$3F1F if (address >= 0x3f20 && address <= 0x3fff) return this.read(0x3f00 + (address - 0x3f20) % 0x0020); return 0; } }, { key: "write", value: function write(address, value) { // Pattern tables 0 and 1 (mapper) if (address <= 0x1fff) return this.mapper.ppuWrite(address, value); // Name tables 0 to 3 (VRAM + mirror) if (address >= 0x2000 && address <= 0x2fff) { if (address >= 0x2000 && address < 0x2400) return this.vram[this._mirroring.$2000 + address - 0x2000] = value; if (address >= 0x2400 && address < 0x2800) return this.vram[this._mirroring.$2400 + address - 0x2400] = value; if (address >= 0x2800 && address < 0x2c00) return this.vram[this._mirroring.$2800 + address - 0x2800] = value; if (address >= 0x2c00 && address < 0x3000) return this.vram[this._mirroring.$2C00 + address - 0x2c00] = value; } // Mirrors of $2000-$2EFF if (address >= 0x3000 && address <= 0xeff) return this.write(0x2000 + (address - 0x3000) % 0x1000, value); // Palette RAM indexes if (address >= 0x3f00 && address <= 0x3f1f) { if (address === 0x3f10) return this.write(0x3f00, value); if (address === 0x3f14) return this.write(0x3f04, value); if (address === 0x3f18) return this.write(0x3f08, value); if (address === 0x3f1c) return this.write(0x3f0c, value); return this.paletteRam[address - 0x3f00] = value; } // Mirrors of $3F00-$3F1F if (address >= 0x3f20 && address <= 0x3fff) return this.write(0x3f00 + (address - 0x3f20) % 0x0020, value); } }]); return PPUMemory; }(); exports.default = PPUMemory;