Hello,
I'm making progress on writing a NES emulator, and having gotten through CPU/PPU emulation and the NROM mapper (testing with Donkey Kong), am now working on the APU. The pulse channels seem alright, but the triangle channel implementation has a bug that I can't diagnose for the life of me; I was hoping someone could shed light on a possible cause. (I've searched for similar issues on the forum with no luck, so apologies if this is a duplicate question.)
The erroneous behaviour can be seen at https://www.youtube.com/watch?v=8m0_M51mzgg, beginning at timestamp 0:24. The triangle channel should be producing something resembling this: https://www.youtube.com/watch?v=n5drmQRAaes, but instead seems to output every 'beat' (in musical terms), instead of on the beats that the music should play on.
The relevant source code can be found at https://github.com/olivecc/nos/tree/master/core. Given that the triangle channel timer can only clock the sequencer (and thus, is audible) when both length and linear counters are non-zero[1], I'd presume that one of the latter two are at fault, although I can't be sure. The relevant files are lectr.h, lictr.h (length and linear counters respectively), triangle.h, and apu.h. Feel free to request further clarification if anything's unclear.
Does anyone have suggestions for what might be causing this? Alternatively, is there another emulator with the ability to log APU state that I might be able to compare mine to?
On a side note, I'm immensely grateful to all wiki and forum contributors for documenting and explaining hardware behaviour so clearly that only now do I need to ask a question, kudos.
EDIT: For some further context, if I comment out the 'lectr.is_active()' in the line linked in [1], the output is identical, implying the length counter has no effect; if I do the same for 'lictr.is_active()', the triangle channel is never 'turned off' and is continually audible, only changing pitch at the same points as before.
[1]: https://github.com/olivecc/nos/blob/4aafcf7ab7f2af576fb1df969b9114e0092cfdc2/core/triangle.h#L52
I'm making progress on writing a NES emulator, and having gotten through CPU/PPU emulation and the NROM mapper (testing with Donkey Kong), am now working on the APU. The pulse channels seem alright, but the triangle channel implementation has a bug that I can't diagnose for the life of me; I was hoping someone could shed light on a possible cause. (I've searched for similar issues on the forum with no luck, so apologies if this is a duplicate question.)
The erroneous behaviour can be seen at https://www.youtube.com/watch?v=8m0_M51mzgg, beginning at timestamp 0:24. The triangle channel should be producing something resembling this: https://www.youtube.com/watch?v=n5drmQRAaes, but instead seems to output every 'beat' (in musical terms), instead of on the beats that the music should play on.
The relevant source code can be found at https://github.com/olivecc/nos/tree/master/core. Given that the triangle channel timer can only clock the sequencer (and thus, is audible) when both length and linear counters are non-zero[1], I'd presume that one of the latter two are at fault, although I can't be sure. The relevant files are lectr.h, lictr.h (length and linear counters respectively), triangle.h, and apu.h. Feel free to request further clarification if anything's unclear.
Does anyone have suggestions for what might be causing this? Alternatively, is there another emulator with the ability to log APU state that I might be able to compare mine to?
On a side note, I'm immensely grateful to all wiki and forum contributors for documenting and explaining hardware behaviour so clearly that only now do I need to ask a question, kudos.
EDIT: For some further context, if I comment out the 'lectr.is_active()' in the line linked in [1], the output is identical, implying the length counter has no effect; if I do the same for 'lictr.is_active()', the triangle channel is never 'turned off' and is continually audible, only changing pitch at the same points as before.
[1]: https://github.com/olivecc/nos/blob/4aafcf7ab7f2af576fb1df969b9114e0092cfdc2/core/triangle.h#L52