broken-neees
Version:
A really broken NEEES emulator that introduces glitches and random bugs on purpose!
81 lines (77 loc) • 4.52 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _PulseOscillator = _interopRequireDefault(require("../lib/apu/PulseOscillator"));
var _LengthCounter = _interopRequireDefault(require("./LengthCounter"));
var _VolumeEnvelope = _interopRequireDefault(require("./VolumeEnvelope"));
var _FrequencySweep = _interopRequireDefault(require("./FrequencySweep"));
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 PulseChannel = /*#__PURE__*/function () {
function PulseChannel(apu, id, enableFlagName) {
_classCallCheck(this, PulseChannel);
this.apu = apu;
this.id = id;
this.enableFlagName = enableFlagName;
this.timer = 0;
this.registers = this.apu.registers.pulses[this.id];
this.oscillator = new _PulseOscillator.default();
this.lengthCounter = new _LengthCounter.default();
this.volumeEnvelope = new _VolumeEnvelope.default();
this.frequencySweep = new _FrequencySweep.default(this);
this.outputSample = 0;
// [!!!]
this.random = 200;
}
_createClass(PulseChannel, [{
key: "sample",
value: function sample() {
if (!this.isEnabled() || !this.lengthCounter.isActive() || this.frequencySweep.mute) return this.outputSample;
// from nesdev: f = fCPU / (16 * (t + 1))
this.oscillator.frequency = 1789773 / (16 * (this.timer + 1));
this.oscillator.dutyCycle = this.registers.control.dutyCycleId;
this.oscillator.volume = this.registers.control.constantVolume ? this.registers.control.volumeOrEnvelopePeriod : this.volumeEnvelope.volume;
// [!!!]
if (!this.apu.cpu.unbroken) this.oscillator.frequency = Math.abs(1789773 / (16 * (this.timer + 1)) - this.random);
this.outputSample = this.oscillator.sample();
return this.outputSample;
}
}, {
key: "updateTimer",
value: function updateTimer() {
this.timer = _byte.default.buildU16(this.registers.timerHighLCL.timerHigh, this.registers.timerLow.value);
}
}, {
key: "step",
value: function step() {
this.frequencySweep.muteIfNeeded();
if (!this.registers.sweep.enabledFlag) this.updateTimer();
}
}, {
key: "quarterFrame",
value: function quarterFrame() {
this.volumeEnvelope.clock(this.registers.control.volumeOrEnvelopePeriod, this.registers.control.envelopeLoopOrLengthCounterHalt);
}
}, {
key: "halfFrame",
value: function halfFrame() {
this.lengthCounter.clock(this.isEnabled(), this.registers.control.envelopeLoopOrLengthCounterHalt);
this.frequencySweep.clock();
}
}, {
key: "isEnabled",
value: function isEnabled() {
return !!this.apu.registers.apuControl[this.enableFlagName];
}
}]);
return PulseChannel;
}();
exports.default = PulseChannel;