broken-neees
Version:
A really broken NEEES emulator that introduces glitches and random bugs on purpose!
67 lines (63 loc) • 4.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _TriangleOscillator = _interopRequireDefault(require("../lib/apu/TriangleOscillator"));
var _LengthCounter = _interopRequireDefault(require("./LengthCounter"));
var _LinearLengthCounter = _interopRequireDefault(require("./LinearLengthCounter"));
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 TriangleChannel = /*#__PURE__*/function () {
function TriangleChannel(apu) {
_classCallCheck(this, TriangleChannel);
this.apu = apu;
this.registers = this.apu.registers.triangle;
this.oscillator = new _TriangleOscillator.default();
this.lengthCounter = new _LengthCounter.default();
this.linearLengthCounter = new _LinearLengthCounter.default();
this.outputSample = 0;
// [!!!]
this.random = 200;
}
_createClass(TriangleChannel, [{
key: "sample",
value: function sample() {
if (!this.isEnabled() || !this.lengthCounter.isActive() || !this.linearLengthCounter.isActive()) return this.outputSample;
var timer = _byte.default.buildU16(this.registers.timerHighLCL.timerHigh, this.registers.timerLow.value);
// this channel only outputs a sample if the timer is between [2, 0x7ff]
if (!(timer >= 2 && timer <= 0x7ff)) return 0;
this.oscillator.frequency = 1789773 / (16 * (timer + 1)) / 2;
// from nesdev: f = fCPU / (16 * (t + 1))
// (the pitch is one octave below the pulse channels with an equivalent timer value)
// (i.e. use the formula above but divide the resulting frequency by two).
// [!!!]
if (!this.apu.cpu.unbroken) this.oscillator.frequency = 1789773 / (16 * (timer + 1)) / 2 + this.random;
this.outputSample = this.oscillator.sample();
return this.outputSample;
}
}, {
key: "quarterFrame",
value: function quarterFrame() {
this.linearLengthCounter.clock(this.isEnabled(), this.registers.lengthControl.halt);
}
}, {
key: "halfFrame",
value: function halfFrame() {
this.lengthCounter.clock(this.isEnabled, this.registers.lengthControl.halt);
}
}, {
key: "isEnabled",
value: function isEnabled() {
return !!this.apu.registers.apuControl.enableTriangle;
}
}]);
return TriangleChannel;
}();
exports.default = TriangleChannel;