No, I don't see how your function could work, unless every time you write to the buffer you write about 28 buffer values or so. Your function creates one sample every 1153 cpu cycles, but by the time we reach 1789 cpu cyles the 44.1khz wave requires we have 44 samples already in our buffer, unless you have a differenct scheme of sending ticks to the function. Check out my code (which is written in delphi, but you'll get it):
This writes out 28 or so bytes per "sample" that the APU would send out. Thus the 1153 square wave is consisted of 16 * 28 or 448 bytes. For a 2/14 duty cycle, About 58 of those bytes should be 1s and the other 390 0s. We have already acknowledged this, and the code outputs this, however the sound isn't slightly off, it's OFF. Other emulators spit out 31 and 220 for the 0 and 1 phases respectively, which is 1.77x smaller each than ours.