Hello.
I'm currently working on a Game Boy/Game Boy Color emulator. More information can be found here: realboyemulator.wordpress.com
It is fairly stable (well, not so much for the Game Boy Color part), and it even passes the tests cpu_instrs, instr_timing and mem_timing. It also implements channels 1, 2 and 4, but I think I'm really missing something and I want to understand what it is. If you could test the emulator, you would notice that sound indeed is not very good.
I'll try now to describe where my doubts arises:
I use the SDL libraries, but this doesn't really matter. SDL creates a thread that calls a 'callback' function every time audio need updating. So, because sample sizes are 1470 (quite arbitrary) bytes, the function gets called 30 times per second when sampling at 44100hz. Right now, the 'callback' function just calls another function that fills the sound buffer according to the values in the sound registers.
Now, I have seen some implementations (VisualBoy's, for instance), and all of them seem to do some kind of synchronization that I can't figure out. For example, when writing to a sound register, a mutex is locked so the 'callback' function does not update sound state. Then, before actually writing the new value to the sound register, the update function is called (the same one that the 'callback' function calls to update sound state). But this time, the whole buffer (the 1470 bytes, for example) are not filled (maybe less samples are needed at that particular point); instead, a small portion is filled depending on the time that has passed since last update. Also, a variable of the likes of 'buffer_position' is used, so for the next update (either through the 'callback' function or the last-mentioned mechanism), the buffer starts filling at 'buffer_position' offset.
This synchronization mechanism is what I fail to understand. What could go wrong with my current implementation, which currently only updates the sound buffer when the 'callback' function is called, and not when a sound register is being updated?
Thanks a lot in advance.
I'm currently working on a Game Boy/Game Boy Color emulator. More information can be found here: realboyemulator.wordpress.com
It is fairly stable (well, not so much for the Game Boy Color part), and it even passes the tests cpu_instrs, instr_timing and mem_timing. It also implements channels 1, 2 and 4, but I think I'm really missing something and I want to understand what it is. If you could test the emulator, you would notice that sound indeed is not very good.
I'll try now to describe where my doubts arises:
I use the SDL libraries, but this doesn't really matter. SDL creates a thread that calls a 'callback' function every time audio need updating. So, because sample sizes are 1470 (quite arbitrary) bytes, the function gets called 30 times per second when sampling at 44100hz. Right now, the 'callback' function just calls another function that fills the sound buffer according to the values in the sound registers.
Now, I have seen some implementations (VisualBoy's, for instance), and all of them seem to do some kind of synchronization that I can't figure out. For example, when writing to a sound register, a mutex is locked so the 'callback' function does not update sound state. Then, before actually writing the new value to the sound register, the update function is called (the same one that the 'callback' function calls to update sound state). But this time, the whole buffer (the 1470 bytes, for example) are not filled (maybe less samples are needed at that particular point); instead, a small portion is filled depending on the time that has passed since last update. Also, a variable of the likes of 'buffer_position' is used, so for the next update (either through the 'callback' function or the last-mentioned mechanism), the buffer starts filling at 'buffer_position' offset.
This synchronization mechanism is what I fail to understand. What could go wrong with my current implementation, which currently only updates the sound buffer when the 'callback' function is called, and not when a sound register is being updated?
Thanks a lot in advance.
Last edited: