broken-neees
Version:
A really broken NEEES emulator that introduces glitches and random bugs on purpose!
50 lines (46 loc) • 2.06 kB
JavaScript
"use strict";
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(e) { return e && e.__esModule ? e : { default: e }; }
class TriangleChannel {
constructor(apu) {
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;
}
sample() {
if (!this.isEnabled() || !this.lengthCounter.isActive() || !this.linearLengthCounter.isActive()) return this.outputSample;
const 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;
}
quarterFrame() {
this.linearLengthCounter.clock(this.isEnabled(), this.registers.lengthControl.halt);
}
halfFrame() {
this.lengthCounter.clock(this.isEnabled, this.registers.lengthControl.halt);
}
isEnabled() {
return !!this.apu.registers.apuControl.enableTriangle;
}
}
exports.default = TriangleChannel;