So, my emulator runs in "parallel" cpu, video, dma,etc. broken down in 4 cycles, so if a instruction uses 12 cycles, my emulator updates video 3 times. That works perfectly. Now, if I hookup the sound, I created a new "frame timer" which starts at 4194304/ 512 hz = 8192, that would by my cicles right ? If my instruction uses 12 cycles, I would also deduce 12 cycles from those 8192 ?
Yes, this is generally a good way to approach the sound controller's timers. I assume you do something similar for the Game Boy's internal hardware timer (the one that generates interrupts), and it works on the same principle. FYI, so will Serial Input-Output, which runs at frequencies that divide 4194304 by a power of 2. So yeah, you've got the right idea.
Would this also apply for the other 3 counters the doc mentions ? (Length Ctr (256 hz), Vol Env (64 hz) and Sweep (128 hz))
Yup. If you're doing something at XYZ hz, just divide the main CPU clock (4194304) by that number to get the amount of cycles you need to watch.
So, I created 2 extra variables to account to the internal counter and the internal flag, but is this internal counter just the same as the one in NR11 ? or is just a counter which reflects the same value as the one in NR11 ?
I think what the document is saying is that the length counter is made of 2 components, a counter component and a flag component. It seems phrased a bit weirdly, like there could be two different counters. But basically, if you have those two components, that's all you need, you're good to go. What you're doing is exactly what I've done, and I have had working audio for about 2 years+
Disabling the channel is disabling that internal flag ? or putting a 0 in bit 0 of FF26 (NR52), or both ? If the channel is disabled, is it just muted but the audio logic keeps working, or no register for channel 1 works anymore until it somehow reactivates again ?
You disable the channel whenever the length counter reaches zero. Every time the length counter clocks (@ 256Hz) you decrement the counter. When you get to zero, the sound has stopped playing. At this point, for Sound 1 (Square Wave w/sweep + envelope), Bit 0 of NR52 automatically gets set to 0. Please note, Bits 0-3 of NR52 are READ ONLY. You can't write to them. They only reflect the status of current sounds, but play no role in disabling sounds. Bit 7 of NR52 disables all sounds, however, if set to 1. Be careful though, because writing 1 to Bit 7 of NR52 wipes out the rest of the NRxx registers if they hold any values.
Anyway, when they say the sound stops, everything about it stops. You don't clock audio logic for envelopes, sweeps, or nothing. The sound is done. This is different from a sound that simply has been muted in certain scenarios (e.g. imagine a decreasing envelope still needs to be "on" even after the envelope volume reaches zero).
Does this mean when the volume envelope timer reaches 0 ?
It says it works at 64 hz, so that would mean the timer decreases every 4194304/64 = 65536 cycles ? and when that happens it checks for the envelope period not being zero ? If that's the case, what happens when this timer reaches 0 ?
Yes, every 65536 cycles, the envelope timer clocks (i.e. reaches zero), which means you have to process it in your audio handling. If the envelope period (NR12, bits 0-2 for example) is non-zero, that indicates how many times the envelope timer must clock before the volume increases or decreases. Say the envelope period is 2, we have to wait 65536 * 2 cycles before deciding if the audio should increase or decrease in volume.
I created again a sweep timer with 128 hz, a new enabled flag and another variable "shadow register". What's the purpose of this shadow register ? It looks like a variable with the previous frequency when the frequency is updated ?
The shadow register is for internal use by the Game Boy's sound controller. It's used when sweeping. The Game Boy keeps track of the original frequency (the bits you wrote into NR13 and NR14) but in order to calculate the sweep it has to change this frequency. Rather than destroying the contents in NR13 and NR14 (this would not be helpful when programming your own sound engine...) it makes a shadow register, or in other words, a temporary variable to store the sweep frequency.
The sweep timer is reloadad, does this mean I need to reset this 128 hz timer ?
what does it mean with Overflow check ?
When Sound 1's sweep is set to increase the frequency, the sound controller places a limit on the maximum allowable frequency. It may not be greater than ~131KHz. Essentially, the frequency for Sound 1 is stored as a value between 0 and 2047 (0x00 through 0x7FF). Any value greater than 0x800 causes Sound 1 to stop.
On the other hand, when Sound 1's sweep is set to decrease, it will continue to decrease as long as the new frequency is greater than zero. However, Sound 1 will not stop when it hits this "limit" when decreasing; the channel still remains "on" just the frequency is always going to be super-low.
With video, I just collect all pixels, rendering dot by dot, line by line, and when I reach VBLANK I draw them to the screen. How does this translate to sound ? Should I play whatever frequency I have when I reach VBLANK ? Or when that "frame" timer reaches 0 ? Not really sure about the timing of this.
The simplest, cleanest way is to render audio sample-by-sample and store it in a buffer. Once this buffer fills, you push it to the computer's audio hardware. Obviously, larger buffers take longer to process, so if you wait too long to output audio it could desync with video. Smaller buffers are preferable when applicable. Ideally, you would be updating this buffer by the smallest audio timer (the length counter's 256Hz timer).
How does the sound output, sound like a gameboy ? I know I can have a certain frequency (like 440 hz, being the standard A), but how can I change the "instrument" (I think it's determined by the wave form), to sound exactly like a gameboy ?
Have you looked into what square waves are, mathematically speaking? If you can grasp that the waves are just different sequences of rising and falling amplitudes, you're on your way to understanding basic sound theory. Whenever you change an "instrument" on the Game Boy, all you're doing is changing the properties of the square wave, if we're just talking about Sound 1. You change the timbre by manipulating the duty-cycle (changing the periods between rising and falling amplitudes) and change the pitch by manipulating the frequency. For most sounds using Sound 1 on the Game Boy, altering those two properties go a long way to making different and "unique" sounds.
I would very much suggest you play around with the sound channels to get a feel for how the different registers affect the output. Take a look at this Sound Test ROM -> https://github.com/Emu-Docs/Emu-Docs/tr ... sound_test
Hope this helps. If you're still confused, let me see if I can clarify some things for you