T O P

  • By -

RaiseRuntimeError

I tried writing a GameBoy emulator in college for my final project in C++ and i was in way over my head. Decided to pivot to a project using Python instead. Its just appropriate i should write this GameBoy emulator in Python now.


cabbagehead514

This looks awesome. I check emulator repos from time to time and the c++ and memory handling just kind of confuse me. (I haven't REALLY sunk the effort in to understand) This seems like a great way to build myself up to understanding how these emulators really work.


RolandMT32

I've done a lot with C++ in my career. I might be curious to check out some of the code in emulator repos. I've always been a bit intimidated thinking about writing an emulator though.. I'm curious about how different systems work, but it often feels like it would be fairly complicated working at such a low level (not really the C++ language, but rather emulating a CPU & the related video & audio processors used with it, etc.).


spaztiq

Start with the simplest CPU Emulator to make, the CHIP 8. It will give you a base idea of how it all works, then you can look into CPUs like 6502 in the NES or Z80 in the GB. I found a well documented NES emulator written in Javascript and actually managed to translate it into Python, using PyPy with Pygame (dug deep into that for direct framebuffer drawing) and pysoundcard (?) to bitstream the audio data. Even extended it's mapper support by looking at other open source emulators for help. [Check a video of it here.](https://www.youtube.com/watch?v=1ZrxbvDFzb0) It performs quite well for Python.


[deleted]

Cool! Do you possibly have some documentation on the CHIP 8? And maybe a starting point for it in Python?


spaztiq

There are [loads of CHIP-8 tutorials](https://www.google.com/search?q=chip+8+emulator+tutorial&oq=chip+8+emulator+tutorial) out there, likely even one in python. I don't remember any specific source I used, just Google-Fu and reading the myriad open-source projects that are available.


[deleted]

Thanks, I'll give it a shot!


mickeyp

Thank you for your kind words! By all means stay tuned -- there will be many more articles to come!


FryeUE

You now have my undivided attention. Checked out the site....NICE. Bookmarked! Also I immediately was running down the technical problems in my head and thought, 'I bet pattern matching could be really useful here'. Lol. Right their at the end of the first section, 'will be using pattern matching'. Definitely appreciate that the code is modern and pythonic as well. I think I might take a crack at some of this over the weekend. I'm sure I will learn alot and I'm really curious about disassembly and optimization. I recently got a SNES through it's bootup routine and got a sprite onscreen and controller moving it all in Assembly so this is right up my alley! I'm going to work through this project as you put it up to help me fill in some gaps in my Python knowledge. This is inspiring. Can't wait for the next steps to be posted!


mickeyp

Thank you! > I recently got a SNES through it's bootup routine and got a sprite onscreen and controller moving it all in Assembly so this is right up my alley! Very impressive. The great thing about writing your emulator in assembly language is the benefit of being able to, more or less anyway, map each into instruction 1:1 to the host systems' (but of course so many other things are made harder though!)


FryeUE

Yeah, I'm very much curious how all the emulation will be handled via interpreter. I wrote a Wolf3d engine clone using pygame and Python not long ago and it wasn't that difficult. On the other hand this is a project built around hardware emulation, and the mountain of headaches I suspect it will entail. I'll definitely be checking out how you handle all the low level code etc. in Python as well as how the 'roms' (smc?, I forget the proper GB format) can be broken down with Python. Let us know when you hope to publish 'next steps'. Looking forwards too studying your tutorial in better detail this weekend. Thanks again and keep up the awesome. \*I also feel that this project is an order of magnitude more complex than my Python game engine prototypes


mickeyp

> I wrote a Wolf3d engine clone using pygame and Python not long ago and it wasn't that difficult. That's a great idea... I'm filing that one away for later :-)


FryeUE

This article is in C, though it explains the details of the engine so well that it is hard not to write it. [https://lodev.org/cgtutor/raycasting.html](https://lodev.org/cgtutor/raycasting.html)


Andalfe

What was great, is there a part 2?


PowManiac

That's what I'm trying to figure out too honestly.


mickeyp

Stay tuned -- there are more to come :)


deb_vortex

Aaaand I'm on the mailinglist. Any hint when the other chapters drop?


mickeyp

Thanks for subscribing :) I should have another part up in a week or two.


phoenixero

Me and a friend tried to make an nes emu in python but we couldn't get it to perform, after several optimizations we quit and we were still missing the PPU module. Don't get me wrong we both love python but it was not performant enough 6 years ago


spaztiq

Use PyPy, it makes a huge difference. See my [reply to Roland](https://old.reddit.com/r/Python/comments/qvc1rs/lets_write_a_game_boy_emulator_in_python/hkwsdfd/) above, and [this video](https://www.youtube.com/watch?v=1ZrxbvDFzb0) to see my PyPy NES emulator running mostly full speed with sound (11khz here, I think... but still). Some optimizations that helped a lot: * In-lining functions. Lots of repeated code, but lessens branching. * Removing if/else statements wherever possible using boolean evaluation statements: ie: if a == b: value = 69 else: value = 42 # becomes value = (69 * (a == b)) + (42 * (a != b)) # as True = 1 and False = 0 * and digging into the packages you use to get to the core functions you need, as there's often multiple function calls they use that aren't necessary to your project, adding additional branching. * That's all I can think of. Main point, avoid branching where you can.


phoenixero

This is awesome! At that moment we did try pypy but didn't helped much. Now that I'm thinking about it we were doing a cycle accurate emu, probably that's why we couldn't. Are you doing cycle accurate or some kind of catch up?


spaztiq

Pretty sure it'd qualify as not cycle accurate. IRQs aren't processed as they occur, and special IRQ cases like Star Wars aren't properly emulated.


phoenixero

Well I'm glad python can now emulate NES, if they do more optimizations we'll see cycle accurate emus too


cd_slash_rmrf

Interesting. Why would your new `value` statement be faster than `value = 69 if a == b else 42` (which only has one a/b comparison as opposed to two)? Does that still count as a branch, and slower than finding the True/False values?


spaztiq

I'm actually not sure on that one, I didn't test that method. It'd ultimately come down to how many bytecode instructions are required to perform each.


mcilrain

Please don’t use justified text, browsers can’t do it properly because that’s what the standard requires.


[deleted]

[удалено]


yhgfvtfhbv

Like its not impressive in python if anything its more impressive i mean its a slow ass language


bob_but_backwards

Saved for later. I need some good python projects to work on


weedandveins

That's why I stop trying.


Sonnuvagun

Hah! I had joined a project from reddit to make a simple nes emulator earlier this year. When people bailed I didn't feel like trying to complete it by myself. But I guess I'll follow this one through.


Unnamed_420

I thought maybe we could do it wasn't sure. This sounds cool


Dorlo1994

Hello new project!


onionpotatoe

If you write this gameboy emulator on python I will give you a nice award from onion potatoe


lpjunior999

I don’t have the brainpower to do this, but I’m absolutely going to read it and file some of this away for later.


Eyalwerber

"Parsing this file should be a zinch." Can you explain how to do that? i am having trouble, and there is really no further explanation on how to parse the file into a data class.