nes-emu
Version:
A NES emulator
36 lines (34 loc) • 2.03 kB
JavaScript
;
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 what palette (0~3) each background tile uses.
* It's a 64-byte array located at the end of each Name table.
* The background is divided into blocks, each of which is 4x4 tiles.
* Every block is divided into four regions of 2x2 tiles.
* Each byte represents a block in that matrix.
* (Bits 0,1 = Quad 0; Bits 2,3 = Quad 1; Bits 4,5 = Quad 2; Bits 6,7 = Quad 3)
*/
class AttributeTable {
constructor() {
_helpers.WithContext.apply(this);
}
/** Returns the palette id of the pixel located in (`x`, `y`) from `nameTableId`. */
getPaletteIdOf(nameTableId, x, y) {
const startAddress = _constants.default.NAME_TABLES_START_ADDRESS + (nameTableId + 1) * _constants.default.NAME_TABLE_SIZE - _constants.default.ATTRIBUTE_TABLE_SIZE;
const blockX = Math.floor(x / _constants.default.ATTRIBUTE_TABLE_BLOCK_SIZE);
const blockY = Math.floor(y / _constants.default.ATTRIBUTE_TABLE_BLOCK_SIZE);
const blockIndex = blockY * _constants.default.ATTRIBUTE_TABLE_TOTAL_BLOCKS_X + blockX;
const regionX = Math.floor(x % _constants.default.ATTRIBUTE_TABLE_BLOCK_SIZE / _constants.default.ATTRIBUTE_TABLE_REGION_SIZE);
const regionY = Math.floor(y % _constants.default.ATTRIBUTE_TABLE_BLOCK_SIZE / _constants.default.ATTRIBUTE_TABLE_REGION_SIZE);
const regionIndex = regionY * _constants.default.ATTRIBUTE_TABLE_TOTAL_REGIONS_X + regionX;
const block = this.context.ppu.memory.readAt(startAddress + blockIndex);
return _constants.default.PALETTE_BACKGROUND_START + _helpers.Byte.getBits(block, regionIndex * _constants.default.ATTRIBUTE_TABLE_REGION_SIZE_BITS, _constants.default.ATTRIBUTE_TABLE_REGION_SIZE_BITS);
}
}
exports.default = AttributeTable;