UNPKG

nes-emu

Version:

A NES emulator

59 lines (54 loc) 2.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _constants = _interopRequireDefault(require("../../../constants")); var _helpers = require("../../../helpers"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /** * An area of memory which defines the shapes of tiles that make up backgrounds and sprites. * It's located at $0000, and it's usually mapped to CHR ROM. * Each tile is 16 bytes, made of two bit planes: * - The first plane controls bit 0 of the color. * - The second plane controls bit 1 of the color. * Any pixel whose color is 0 is background/transparent. */ class PatternTable { constructor() { _helpers.WithContext.apply(this); } /** * Returns the palette index of the pixel located in (`x`, `y`), * from tile `tileId` of `patternTableId`. */ getPaletteIndexOf(patternTableId, tileId, x, y) { const lowByte = this.getLowByteOf(patternTableId, tileId, y); const highByte = this.getHighByteOf(patternTableId, tileId, y); return this.getPaletteIndexFromBytes(lowByte, highByte, x); } /** Get first plane's `y`th byte from tile `tileId` of `patternTableId`. */ getLowByteOf(patternTableId, tileId, y) { const startAddress = this._getStartAddress(patternTableId); const firstPlane = tileId * _constants.default.TILE_SIZE; return this.context.ppu.memory.readAt(startAddress + firstPlane + y); } /** Get second plane's `y`th byte from tile `tileId` of `patternTableId`. */ getHighByteOf(patternTableId, tileId, y) { const startAddress = this._getStartAddress(patternTableId); const firstPlane = tileId * _constants.default.TILE_SIZE; const secondPlane = firstPlane + _constants.default.TILE_SIZE / 2; return this.context.ppu.memory.readAt(startAddress + secondPlane + y); } /** Builds a palette index from `lowByte` and `highByte` (bit 7-`x`). */ getPaletteIndexFromBytes(lowByte, highByte, x) { const column = _constants.default.TILE_LENGTH - 1 - x; const lsb = _helpers.Byte.getBit(lowByte, column); const msb = _helpers.Byte.getBit(highByte, column); return msb << 1 | lsb; } _getStartAddress(patternTableId) { return _constants.default.PATTERN_TABLES_START_ADDRESS + patternTableId * _constants.default.PATTERN_TABLE_SIZE; } } exports.default = PatternTable;