stephena, on Sun Dec 11, 2011 9:20 PM, said:
I can confirm (on real hardware) that the image displayed is different from any emulator I've tested with. So there's definitely more than audio issues here. Maybe if the screen roll/VSYNC/VBLANK issues are properly emulated in Stella, it will fix the sound issues too ??
I don't know what the sound issues are, but I'm inclined to doubt that emulating screen roll would affect the sound, as screen roll is a video problem and the audio should neither affect, nor be affected by, the video. The TIA outputs its two audio clocks (A-phi-1 and A-phi-2) twice each on every scanline, including scanlines where VSYNC is turned on, so any sounds that are being played will continue being generated even during the VSYNC.
I'm inclined to suspect that any audio differences between real hardware and an emulator are due to differences in the ways the audio is being generated. On a real Atari 2600 the TIA is generating waveforms through a combination of how the audio clocks are being modified by the frequency divider, then how the audio control register is telling the 5-bit LFSR and the 4-bit LFSR to produce the final waveform, with the audio volume registers determining the strength of each "on" bit in the waveform. But the signal from the AU0 pin (and/or the AU1 pin) then gets fed into the RF modulator circuitry, which apparently affects the final shape of the waveform. For example, the AU0 pin might be outputting an audio signal made up of rectangular pulse waves, but when I recorded the output of the RF modulator to DVD and looked at the waves on my computer, the waveforms were definitely *not* rectangular pulses-- they look curved, more like sine waves (but not pure sine waves), or like curved sawtooth waves (the final shape is apparently affected by how many 1s or 0s are being output, as well as how many 1s or 0s were previously being output before a change occurred).
I don't know how Stella's audio is programmed, but I imagine it's based on an idealized attempt to emulate the TIA's waveforms. There are a number of areas where this probably needs to be improved if possible:
(1) The audio clocks occur at specific moments during each scanline, and they don't follow a symmetrical pattern. The audio 1 (A-phi-1) clocks are less important to the resulting waveform than the audio 2 (A-phi-2) clocks, because the audio 1 clocks are mainly used (in conjunction with the audio 2 clocks) to drive the frequency divider. But the audio 2 clocks occur at intervals of 112 color clocks and 116 color clocks, whereas I imagine the TIA audio emulation is assuming a perfect 114-114 separation. The difference is probably negligible, so it's probably more crucial to try to get the audio 2 clocks to coincide with the general moment on each scanline when they occur. The issue here is a question of how the exact timing of making changes to the AUDF0/1 and AUDC0/1 registers will interact with the audio clocks-- that is, on a real Atari 2600 the changes might occur just before the audio 2 clock is generated, whereas in an emulator the changes might occur *afterward*, if you see what I mean.
(2) The primary frequency division is accomplished by feeding the audio 1 and audio 2 clocks through the frequency divider circuitry, which then outputs the tone 1 and tone 2 clocks (T-phi-1 and T-phi-2). Basically, the tone 1 and tone 2 clocks are the same as the audio 1 and audio 2 clocks, except some of the audio 1 and audio 2 clocks get skipped due to the division-- that is, division by 2 (AUDF0=1) will let every other audio 1 and audio 2 clock through, division by 3 will let every third clock through, and so on. The main problem with this step is that changes to the AUDF0/1 registers do not necessarily result in immediate changes to the tone 1 and tone 2 clocks, because the frequency divider may need to finish its current process before the new AUDF0/1 value can begin to have an effect-- and that could potentially take the equivalent of several scanlines. If the emulation is changing the frequency immediately after the AUDF0/1 register has been written to, then it won't be accurate.
(3) Likewise, the waveforms on a real Atari 2600 don't change as soon as the AUDC0/1 registers are written to, because the current contents of the 5-bit LFSR and 4-bit LFSR need to keep shifting before their new input values can go through each "bit" of the polynomials and emerge from their last "bit." So if the emulator is making immediate changes to the waveforms as soon as AUDC0/1 is written to, it won't be accurate.
(4) Finally, if the emulator isn't able to duplicate the results of feeding the AU0/1 signals through the RF modulator, then the sounds may sound different than they do on a real Atari 2600. I don't know how the RF modulator works-- I haven't tried to decipher that portion of the schematics yet-- but it does seem like it might be possible to emulate the results to some extent, because there must be some sort of logic to how the RF modulator is affecting the final audio output.
I've been working on understanding the TIA's audio generation, as well as other portions of the schematics. I'd started making a spreadsheet to simulate the TIA-- using IF() functions to duplicate the effects of the inverters, NOR gates, AND gates, NAND gates, flip-flops, etc.-- but what I'd like to do is create a program that does the same thing, with entry fields for the different registers. This will probably be a long and drawn-out project, and I'm currently preoccupied with family matters and work-related matters, so I can't say when I'll be able to work on this particular pet project. But I'll certainly pass along any insights, and post anything I manage to put together, in the hopes it will help with improving TIA emulation.