broken-neees
Version:
A really broken NEEES emulator that introduces glitches and random bugs on purpose!
102 lines (99 loc) • 4.65 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _dpcmPeriods = _interopRequireDefault(require("../lib/apu/dpcmPeriods"));
var _byte = _interopRequireDefault(require("../lib/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); }
var DMCChannel = /*#__PURE__*/function () {
function DMCChannel(apu, cpu) {
_classCallCheck(this, DMCChannel);
this.apu = apu;
this.cpu = cpu;
this.registers = this.apu.registers.dmc;
// Direct load
this.outputSample = 0;
// DPCM
this.startFlag = false;
this.isUsingDPCM = false;
this.buffer = null;
this.cursorByte = 0;
this.cursorBit = 0;
this.dividerCount = 0;
this.samplePeriod = 0;
this.sampleAddress = 0;
this.sampleLength = 0;
}
_createClass(DMCChannel, [{
key: "sample",
value: function sample() {
return this.outputSample;
}
}, {
key: "step",
value: function step() {
if (this.startFlag) {
var samplePeriod = _dpcmPeriods.default[this.registers.control.dpcmPeriodId];
this.startFlag = false;
this.isUsingDPCM = true;
this.cursorByte = -1;
this.cursorBit = 0;
this.dividerCount = samplePeriod - 1;
this.samplePeriod = samplePeriod;
this.sampleAddress = this.registers.sampleAddress.absoluteAddress();
this.sampleLength = this.registers.sampleLength.lengthInBytes();
this.outputSample = 0;
}
if (!this.isUsingDPCM) return;
this.dividerCount++;
if (this.dividerCount >= this.samplePeriod) 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.isUsingDPCM = false;
this.buffer = null;
this.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 % 0xffff;
}
this.buffer = this.cpu.memory.read(address);
}
var variation = _byte.default.getBit(this.buffer, this.cursorBit) ? 1 : -1;
this.outputSample += variation;
this.cursorBit++;
if (hasSampleFinished && hasByteFinished && this.registers.control.loop) this.startDPCM();
}
}, {
key: "startDPCM",
value: function startDPCM() {
this.startFlag = true;
}
}, {
key: "stopDPCM",
value: function stopDPCM() {
this.cursorByte = this.sampleLength;
}
}, {
key: "remainingBytes",
value: function remainingBytes() {
if (!this.isUsingDPCM) return 0;
return this.sampleLength - this.cursorByte;
}
}]);
return DMCChannel;
}();
exports.default = DMCChannel;