Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML Gameboy Emulator  (Read 15348 times)

0 Members and 1 Guest are viewing this topic.

lordseanington

  • Jr. Member
  • **
  • Posts: 86
  • Coffee is the blood of a coder.
    • View Profile
SFML Gameboy Emulator
« 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:


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.

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: SFML Gameboy Emulator
« Reply #1 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 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

lordseanington

  • Jr. Member
  • **
  • Posts: 86
  • Coffee is the blood of a coder.
    • View Profile
Re: SFML Gameboy Emulator
« Reply #2 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.

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: SFML Gameboy Emulator
« Reply #3 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

lordseanington

  • Jr. Member
  • **
  • Posts: 86
  • Coffee is the blood of a coder.
    • View Profile
Re: SFML Gameboy Emulator
« Reply #4 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...

pvigier

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • pvigier's blog
Re: SFML Gameboy Emulator
« Reply #5 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?

lordseanington

  • Jr. Member
  • **
  • Posts: 86
  • Coffee is the blood of a coder.
    • View Profile
Re: SFML Gameboy Emulator
« Reply #6 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.

Rosme

  • Full Member
  • ***
  • Posts: 169
  • Proud member of the shoe club
    • View Profile
    • Code-Concept
Re: SFML Gameboy Emulator
« Reply #7 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!
GitHub
Code Concept
Twitter
Rosme on IRC/Discord

lordseanington

  • Jr. Member
  • **
  • Posts: 86
  • Coffee is the blood of a coder.
    • View Profile
Re: SFML Gameboy Emulator
« Reply #8 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.

lordseanington

  • Jr. Member
  • **
  • Posts: 86
  • Coffee is the blood of a coder.
    • View Profile
Re: SFML Gameboy Emulator
« Reply #9 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
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.

Rosme

  • Full Member
  • ***
  • Posts: 169
  • Proud member of the shoe club
    • View Profile
    • Code-Concept
Re: SFML Gameboy Emulator
« Reply #10 on: December 17, 2019, 03:21:16 pm »
Awesome, can't wait to dive into that code! Thanks for sharing!
GitHub
Code Concept
Twitter
Rosme on IRC/Discord

lordseanington

  • Jr. Member
  • **
  • Posts: 86
  • Coffee is the blood of a coder.
    • View Profile
Re: SFML Gameboy Emulator
« Reply #11 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: SFML Gameboy Emulator
« Reply #12 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!
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

lordseanington

  • Jr. Member
  • **
  • Posts: 86
  • Coffee is the blood of a coder.
    • View Profile
Re: SFML Gameboy Emulator
« Reply #13 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

lordseanington

  • Jr. Member
  • **
  • Posts: 86
  • Coffee is the blood of a coder.
    • View Profile
Re: SFML Gameboy Emulator
« Reply #14 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.

 

anything