broken-neees
Version:
A really broken NEEES emulator that introduces glitches and random bugs on purpose!
127 lines (121 loc) • 5.32 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _byte = _interopRequireDefault(require("../byte"));
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); }
/**
* DPCM (Δ-Modulation) channel handler.
* Manages sample playback: fetching bits, looping, and outputting variations.
*/
var DPCM = /*#__PURE__*/function () {
function DPCM(dmcChannel) {
_classCallCheck(this, DPCM);
this.dmcChannel = dmcChannel;
this.startFlag = false;
this.isActive = false;
this.buffer = null;
this.cursorByte = 0;
this.cursorBit = 0;
this.dividerPeriod = 0;
this.dividerCount = 0;
this.sampleAddress = 0;
this.sampleLength = 0;
}
/**
* Called each APU cycle to handle DPCM timing, sample fetching,
* bit processing, output variation, and looping.
*/
_createClass(DPCM, [{
key: "update",
value: function update() {
if (this.startFlag) {
var dividerPeriod = dpcmPeriods[this.registers.control.dpcmPeriodId];
this.startFlag = false;
this.isActive = true;
this.cursorByte = -1;
this.cursorBit = 0;
this.dividerPeriod = dividerPeriod;
this.dividerCount = dividerPeriod - 1;
// sample address = %11AAAAAA.AA000000 = $C000 + (A * 64)
this.sampleAddress = 0xc000 + this.registers.sampleAddress.value * 64;
// sample length = %LLLL.LLLL0001 = (L * 16) + 1 bytes
this.sampleLength = this.registers.sampleLength.value * 16 + 1;
this.dmcChannel.outputSample = 0;
}
if (!this.isActive) return;
this.dividerCount++;
if (this.dividerCount >= this.dividerPeriod) this.dividerCount = 0;else return;
var hasSampleFinished = this.cursorByte === this.sampleLength;
var hasByteFinished = this.cursorBit === 8;
if (this.buffer === null || hasByteFinished) {
this.cursorByte++;
this.cursorBit = 0;
if (hasSampleFinished) {
this.isActive = false;
this.buffer = null;
this.dmcChannel.outputSample = 0;
return;
}
var address = this.sampleAddress + this.cursorByte;
if (address > 0xffff) {
// (if it exceeds $FFFF, it is wrapped around to $8000)
address = 0x8000 + address % 0x10000;
}
this.buffer = this.cpu.memory.read(address);
}
var variation = _byte.default.getBit(this.buffer, this.cursorBit) ? 1 : -1;
this.dmcChannel.outputSample += variation;
this.cursorBit++;
if (hasSampleFinished && hasByteFinished && this.registers.control.loop) this.start();
}
/**
* Sets the startFlag so playback begins on next update.
*/
}, {
key: "start",
value: function start() {
this.startFlag = true;
}
/**
* Stops playback immediately by moving cursor to sample end.
*/
}, {
key: "stop",
value: function stop() {
this.cursorByte = this.sampleLength;
}
/**
* Returns remaining bytes in sample, or 0 if inactive.
*/
}, {
key: "remainingBytes",
value: function remainingBytes() {
if (!this.isActive) return 0;
return this.sampleLength - this.cursorByte;
}
}, {
key: "cpu",
get: function get() {
return this.dmcChannel.cpu;
}
}, {
key: "registers",
get: function get() {
return this.dmcChannel.registers;
}
}]);
return DPCM;
}();
/**
* A list of all DPCM periods. The `DMCControl` register points to this table.
*/
exports.default = DPCM;
var dpcmPeriods = [214, 190, 170, 160, 143, 127, 113, 107, 95, 80, 71, 64, 53, 42, 36, 27];