Hi again, I need once more the help from you masters of emulation.
I managed to nail GBC, I couldn't find any visual bugs with Aladdin, Tomb Raider, Rayman and some others.
So I decided it was time to add the only major feature I was missing from my emulator (and cable link, that's still pending).
I'm using this document, which seems very exhaustive:
http://gbdev.gg8.se/wiki/articles/Gameb ... d_hardware
I started to implement the registers behaviour only for channel 1 with the intent of playing the coin sound from the DMG bios, but then I got mentally blocked and I have tons of questions:
1) Document: A timer generates an output clock every N input clocks, where N is the timer's period. If a timer's rate is given as a frequency, its period is 4194304/frequency in Hz. Each timer has an internal counter that is decremented on each input clock. When the counter becomes zero, it is reloaded with the period and an output clock is generated.
The frame sequencer generates low frequency clocks for the modulation units. It is clocked by a 512 Hz timer.
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 ?
Would this also apply for the other 3 counters the doc mentions ? (Length Ctr (256 hz), Vol Env (64 hz) and Sweep (128 hz))
2) Document: Length Counter = A length counter disables a channel when it decrements to zero. It contains an internal counter and enabled flag. Writing a byte to NRx1 loads the counter with 64-data (256-data for wave channel). The counter can be reloaded at any time.
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 ?
3) Document: Each length counter is clocked at 256 Hz by the frame sequencer. When clocked while enabled by NRx4 and the counter is not zero, it is decremented. If it becomes zero, the channel is disabled.
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 ?
4) Document: A volume envelope has a volume counter and an internal timer clocked at 64 Hz by the frame sequencer. When the timer generates a clock and the envelope period is not zero, a new volume is calculated by adding or subtracting (as set by NRx2) one from the current volume. If this new volume within the 0 to 15 range, the volume is updated, otherwise it is left unchanged and no further automatic increments/decrements are made to the volume until the channel is triggered again.
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 ?
5) Document: The first square channel has a frequency sweep unit, controlled by NR10. This has a timer, internal enabled flag, and frequency shadow register. It can periodically adjust square 1's frequency up or down.
During a trigger event, several things occur:
Square 1's frequency is copied to the shadow register.
The sweep timer is reloaded.
The internal enabled flag is set if either the sweep period or shift are non-zero, cleared otherwise.
If the sweep shift is non-zero, frequency calculation and the overflow check are performed immediately.
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 sweep timer is reloadad, does this mean I need to reset this 128 hz timer ?
what does it mean with Overflow check ?
6) 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.
7) 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 ?
I know these are really beginner questions, and Im still lacking a lot of technical electronic and audio background, but Id be really grateful if you could guide me with these.
Thanks,
I managed to nail GBC, I couldn't find any visual bugs with Aladdin, Tomb Raider, Rayman and some others.
So I decided it was time to add the only major feature I was missing from my emulator (and cable link, that's still pending).
I'm using this document, which seems very exhaustive:
http://gbdev.gg8.se/wiki/articles/Gameb ... d_hardware
I started to implement the registers behaviour only for channel 1 with the intent of playing the coin sound from the DMG bios, but then I got mentally blocked and I have tons of questions:
1) Document: A timer generates an output clock every N input clocks, where N is the timer's period. If a timer's rate is given as a frequency, its period is 4194304/frequency in Hz. Each timer has an internal counter that is decremented on each input clock. When the counter becomes zero, it is reloaded with the period and an output clock is generated.
The frame sequencer generates low frequency clocks for the modulation units. It is clocked by a 512 Hz timer.
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 ?
Would this also apply for the other 3 counters the doc mentions ? (Length Ctr (256 hz), Vol Env (64 hz) and Sweep (128 hz))
2) Document: Length Counter = A length counter disables a channel when it decrements to zero. It contains an internal counter and enabled flag. Writing a byte to NRx1 loads the counter with 64-data (256-data for wave channel). The counter can be reloaded at any time.
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 ?
3) Document: Each length counter is clocked at 256 Hz by the frame sequencer. When clocked while enabled by NRx4 and the counter is not zero, it is decremented. If it becomes zero, the channel is disabled.
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 ?
4) Document: A volume envelope has a volume counter and an internal timer clocked at 64 Hz by the frame sequencer. When the timer generates a clock and the envelope period is not zero, a new volume is calculated by adding or subtracting (as set by NRx2) one from the current volume. If this new volume within the 0 to 15 range, the volume is updated, otherwise it is left unchanged and no further automatic increments/decrements are made to the volume until the channel is triggered again.
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 ?
5) Document: The first square channel has a frequency sweep unit, controlled by NR10. This has a timer, internal enabled flag, and frequency shadow register. It can periodically adjust square 1's frequency up or down.
During a trigger event, several things occur:
Square 1's frequency is copied to the shadow register.
The sweep timer is reloaded.
The internal enabled flag is set if either the sweep period or shift are non-zero, cleared otherwise.
If the sweep shift is non-zero, frequency calculation and the overflow check are performed immediately.
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 sweep timer is reloadad, does this mean I need to reset this 128 hz timer ?
what does it mean with Overflow check ?
6) 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.
7) 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 ?
I know these are really beginner questions, and Im still lacking a lot of technical electronic and audio background, but Id be really grateful if you could guide me with these.
Thanks,