SFML community forums

General => SFML projects => Topic started by: lordseanington on November 09, 2019, 09:17:37 am

Title: SFML Gameboy Emulator
Post by: lordseanington on November 09, 2019, 09:17:37 am
Hello there everyone!
Long time no see.

I have been working on a project for the last 4 months or so during my spare time, and I decided to show it off here a bit. It isn't anything overly complex as far as emulators go, and it's still got heaps of bugs and inaccuracies, but it can run a few games and looks/sounds okay.

I posted a video I took here:
https://youtu.be/alVzvLPZ3TU (https://youtu.be/alVzvLPZ3TU)

There is still quite a bit to to for it before I am fully content with it, but as always I am never content with it.
A few TODOs:
Implement the 4th audio channel (the noise channel)
Implement some more cartridge types (right now it only does MBC1 and rom only carts)
Add support for gameboy color
and add a per channel waveform renderer (this is just for style, and because I love old video game music).

SFML is used for audio, video, and events (so basically everything).
Let me know what you think, like, ect.
Title: Re: SFML Gameboy Emulator
Post by: fallahn on November 10, 2019, 10:49:11 am
Nice work!

How did you handle the audio with SFML? When I did mine I had real trouble keeping the audio in sync/at the right speed due to the sample rate effectively being that of the emulated CPU speed. I ended up resampling the output (https://github.com/fallahn/speljongen/blob/master/SpelJongEmu/src/Sampler.cpp) and sent it to the soundcard via SDL because it gave more direct access to the audio hardware. Would be nice to drop the dependency and use only sfml! :D
Title: Re: SFML Gameboy Emulator
Post by: lordseanington on November 10, 2019, 07:16:43 pm
I created custom sound streams that were essentially waveform generators that were controlled by the values in the hardware registers. The audio is completely disconnected from the CPU clock cycles other than the length counter, sweep, and envelope.
This ensures that the audio keeps at the propper frequency at all times. It is kinda a non purist way of doing it, because of how gameboy hardware works if the cpu slows down so does the audio, but I think it still worked nice.
Title: Re: SFML Gameboy Emulator
Post by: fallahn on November 10, 2019, 09:33:00 pm
Ah! That's a much more practical approach. I'll definitely have to try that next time I work on something similar - I'm by no means a purist and would much rather get good results :D
Title: Re: SFML Gameboy Emulator
Post by: lordseanington on November 11, 2019, 09:16:20 am
Most of the waves that the gameboy outputs are really simple and can be calculated fairly quickly. So, it works nicely to generate them on the spot. The only issue i have noticed is that i need to provide at least a certain ammount of samples into the future, so there is a tiny lag in changing the audio registers and it actually being reflected.
However, it is excessively small and doesnt diminish audio quality. Im not sure exactly how many samples should be provided by my custom audio stream each time though...
Title: Re: SFML Gameboy Emulator
Post by: pvigier on November 18, 2019, 11:57:56 am
Looks great! I would love to work on an emulator myself one day.

Do you plan to open-source it?
Title: Re: SFML Gameboy Emulator
Post by: lordseanington on November 20, 2019, 12:31:16 am
Sorry about the late response, school has been busy as you might imagine.

I am concidering putting it open source, although if i do its a ways off (as I have quite a bit to clean up). When i get some time ill clean some stuff up and try to get it put up. Ill post a link when i do.
Title: Re: SFML Gameboy Emulator
Post by: Rosme on November 29, 2019, 07:05:24 pm
Wow that is very nice.

If I may, I'd suggest publish it even if not clean. Facilitate code review, but also shows the evolution and things work!

But good job!
Title: Re: SFML Gameboy Emulator
Post by: lordseanington on November 30, 2019, 12:35:18 am
Thank you,
and when i get a chance ill put it up on github so people can get a look at it. For the moment I am away from the computer that has it on it, so I shall do it when i get back to it.
Title: Re: SFML Gameboy Emulator
Post by: lordseanington on December 13, 2019, 04:55:25 am
Hello everyone, I am back now that I have a breath with finals being done.
I have put up the code on my github as well as put up a release version. I cleaned up the code a bit but didn't have the time to finish the documentation. Thanks to the computer I am working on, I do not have git so I had to put up the files manually.
Here is the link to the repository:
         https://github.com/aracitdev/GameBoyEmu (https://github.com/aracitdev/GameBoyEmu)
Feel free to check out the code, however there  is still a lack of commenting and a few spots that I do say are fairly difficult to understand because of it.
Title: Re: SFML Gameboy Emulator
Post by: Rosme on December 17, 2019, 03:21:16 pm
Awesome, can't wait to dive into that code! Thanks for sharing!
Title: Re: SFML Gameboy Emulator
Post by: lordseanington on December 17, 2019, 11:57:01 pm
In a little while i will probably start implementing a bunch of quality of life things and some other basic functionality
 Things like save states, sram support, button remapping, and some general optimization and bug fixes.
Please tell me what you think, any possible improvements you might see, and any cool features I could implement.
Title: Re: SFML Gameboy Emulator
Post by: Nexus on December 18, 2019, 08:59:41 pm
Really cool project! I didn't think it would be possible to come up with an emulator in only a few months as a free-time project. 8)

Knowing nothing about the GameBoy architecture, how are the ROMs encoded, and how do you get started?
Drawing it in SFML has a lot of potential, you could even add some fancy effects/shaders that would never have been possible on a GameBoy ;)

You could consider adding your project here: https://github.com/gbdev/awesome-gbdev#emulators
Or even on the SFML Wiki: https://github.com/SFML/SFML/wiki/Projects
There seems to be quite a community around this!
Title: Re: SFML Gameboy Emulator
Post by: lordseanington on December 19, 2019, 03:10:03 am
Roms are essemtially a direct dump of the different gameboy memory banks next to each other in order. The emulator takes the rom data and pretends to be a memory bank controller, allowing it to memory map certain banks to certain addresses.
 It didnt take an excessive ammount of time to get the basic functionality down, but the hardest part was tracking down bugs in the instruction code. Those took many hours to track down.
I will add some more features and optomize a few things before I add it, as I have some other basic features and bugs i wanna work out.

If you want to know how to get started in making one:
https://gbdev.gg8.se/wiki/articles/Main_Page

Gameboy memory map:
http://gameboy.mongenel.com/dmg/asmmemmap.html

Gameboy Instruction Set:
https://www.pastraiser.com/cpu/gameboy/gameboy_opcodes.html

Bgb Doc (Super in depth and is a must have):
http://bgb.bircd.org/pandocs.htm
Title: Re: SFML Gameboy Emulator
Post by: lordseanington on December 21, 2019, 10:29:59 am
Hello, didn't expect to be posting again so soon, but I managed to churn out another feature a lot quicker than I expected to. So Iv gotten key bindings, xml configuration loading, and save states implemented now. I just finished it up and committed (It is super late) and figured I'd give a little update now that I'm done.

It now has a dependency on tinyxml2 (for the xml configuration loading), but tinyxml2 is extremely light and easy to work with.

Tinyxml2: http://www.grinninglizard.com/tinyxml2/

Next I'm probably going to be working on getting battery backup ram (save game ram) working as well as fixing some bugs.
Title: Re: SFML Gameboy Emulator
Post by: lordseanington on January 19, 2020, 06:40:40 am
Hello! I am back.

I have fixed some minor bugs with the graphics, however there is still more work to be done there.
I did some more documentation and added support for SRAM as well. Its coming out very nicely.

Iv debated on adding dynamic recompilation, despite it being a boatload of work. For those who don't know, dynamic recompilation is basically converting the Gameboy machine code to native machine code and running it in an environment so it works properly. It's done as it's run (as opposed to static recompilation, where the rom is entirely recompiled into a whole new program). Give me your opinion on adding this feature, but really it'd not be for speed but rather be for fun and learning experience.
Thank you.
Title: Re: SFML Gameboy Emulator
Post by: lordseanington on April 24, 2020, 07:45:16 pm
I have fixed enough that I deemed another video was in order, so here you go:

https://youtu.be/FSHBia7rW8k (https://youtu.be/FSHBia7rW8k)

There are a lot of graphical bug fixes and a whole bunch of extra features added.
Thank you.