Dolphin, the GameCube and Wii emulator - Forums
Programming Discussion Thread - Printable Version

+- Dolphin, the GameCube and Wii emulator - Forums (https://forums.dolphin-emu.org)
+-- Forum: Offtopic (https://forums.dolphin-emu.org/Forum-offtopic)
+--- Forum: Delfino Plaza (https://forums.dolphin-emu.org/Forum-delfino-plaza)
+--- Thread: Programming Discussion Thread (/Thread-programming-discussion-thread)



RE: Programming Discussion Thread - HoodedMan - 12-03-2014

Hello I really enjoy the Dolphin Progress Reports;they have inspired me to take up emulation again. For background sake I am currently in High school and have about a year of experience with C++. I am currently learning Haskell to round at my experiences as a programmer. I have also dabbled in assembly. I have worked on Emulators before, but have I always ended up quitting before completing one. I am hoping that someone can steer me in the right direction, so I can complete my own emulator fully. Some questions include...

What is the best system to first start emulating?
How do I get started?
Were did you guys get started?

Thank you,

Josh


RE: Programming Discussion Thread - NaturalViolence - 12-03-2014

1. Chip8
2. Google
3. N/A to me


RE: Programming Discussion Thread - garrlker - 12-03-2014

Chip 8 is the best system to start on to get a grasp on the understanding internally. From there a GameBoy emu would be the next step, then GBA, etc.
Here is a quote from Shonumi from when I asked him the same question a while back:
Shonumi Wrote:It's great to see aspiring and curious types interested in emulation. Just like ShizZy (Gekko founder) I started with the GB, now I'm working on GC emulation Tongue

Anyway, yeah, you'll want the tutorials from Imran Nazar. Obviously start with the emulated CPU then work your way up. As for tips, here's a few things that you can do to avoid pitfalls and generally have an easier time of it.
Wrap your head around how the CPU works. Learn what registers, opcodes, cycles, stack pointers, and program counters are, and learn them well. It'll serve you well if you move onto other emulation projects
Get used to low-level byte operations. You'll have to use AND, OR, and XOR bitwise manipulators, as well as bit shifting. It's very beneficial to know how to read hexadecimal as well
Focus on getting your CPU off the ground first. While the fun stuff doesn't start until you get to graphics, if your emulated GB CPU isn't up to snuff, you'll be banging your head on something sooner or later. Google for Blarrg's GB CPU tests; they're ROMs meant to test the accuracy of certain instructions. I have a local copy I can upload if you can't find them (he just made a new website early this year, might not turn up in searches).
Imran Nazar's JavaScript GB emu is not cycle-accurate, so don't go off of what it says. Use this CPU manual for accurate timings. It doesn't account for conditionals such as JR NZ (you'll learn what that is), but look at VBAM, ZBoy, or this GB emu if you want readable source code. Personally, the last one has the clearest code.
Get used to the idea of pointers. Many games will read use a 16-bit value to represent and address that should be read from to get the value that will be stored in a register. It may be confusing if you have never used memory management in something like C++, but you'll need to know what you're doing when programming the CPU.
GET A DEBUGGER. This will save your life. Even though it's not totally cycle-accurate, I use JSGB by Pedro Ladaria. You can setup your own offline local version by copying all of the necessary file (jsgb.lcd.js, jsgb.timer.js, etc) This is useful if you want to test your own ROMS instead of the ones provided by the website and thus keep yourself legal.

Now for some pro-tips, i.e. how to avoid major PITA moments:
When doing input, you should never return zero when reading from P1 (0xFF00). This can cause a variety of graphical glitches. You should instead return 0xFF, which means no input is being entered on the P1 register.
The LCD renders per-scanline, so on each HBlank, you'll need to determine which line needs to be draw using which pixels
Opcodes 0x07, 0x0F, 0x17, and 0x1F require you to set Bit 7 of the Flag Register, unlike the extended opcodes (0xCB07, 0xCB0F, etc) with the same mnemonic name.
The GB compares two registers (LY and LYC) to determine if a STAT interrupt should occur. You should compare LY and LYC after you update LY. LY represents the current scanline, so when you enter into an HBlank, update LY by one first, then compare LY and LYC to see if you should raise a STAT interrupt.

I'm always available to personally help you too, so don't be afraid to ask. Additionally, this might be a good read too: http://www.multigesture.net/articles/how-to-write-an-emulator-chip-8-interpreter/ Of course a CHIP-8 emu is not as difficult as a GB emu, but if you want to try something first, or try to get your head around emu-programming, it's a good article to look at.
Just a note, Shonumi sent me this in june of 2013 so I'm sure he has a ton of new experience on this subject that he can add to this.
As for question 2, just use your language of choice(although c,c++ would be best) and program. The multigesture link would give you a good understanding of
how to structure the emulator while the CPU tutorial will obviously help for the cpu core.
And Q#3, I asked him this when I only knew Python 3(and barely knew it tbh) and didn't know hardly any C++.
I'm a lot more capable with C++ now and haven't started on it due to having a busy schedule, although you can expect some posts here if I ever get something working Big Grin
Your best bet is to ask Shonumi or go on the Dolphin-dev IRC and ask developers there. Either way good luck and happy programming!


RE: Programming Discussion Thread - Shonumi - 12-03-2014

@HoodedMan - I second emulating the Chip8 (although Chip8 technically isn't a console but you get what I mean). Even if you've never finished an emulator, it's something you can do a lot with as a weekend project. This link seems pretty in-depth -> http://www.multigesture.net/articles/how-to-write-an-emulator-chip-8-interpreter/

Getting started is easy. Just, erm, start. Really, even if you don't have a final idea of how the emulator will function, start throwing together code that you know how to make (reading a file into memory, setting up functions you know will be used often) and start emulating the parts you best understand. After that, write test code (in your case Chip8 test code), compare it to a "completed"/working emulator or real hardware, verify the results, fix stuff, then move on to the next thing that's missing from your emulator.

The short answer of where I started: the GameBoy.

The long answer of where I started: I discovered Dolphin late in my college career, gave emulators on Linux a try (was previously unsuccessful/disappointed). I was (like you) impressed with the devs and wanted to be like them. I first fiddled around with several abandoned emulators (an SDL port of OSwan and RealityBoy) but all I ever did was make GUIs or add trivial features like screenshots. I decided I was tired messing around, and I got serious about learning how to program an emulator. I went straight for the GameBoy (at this time, I also worked sporadically on Gekko, a would-be GC emulator) and spent a while doing that off and on. I also messed around with Desmume, trying to get it to render 3D objects at higher resolutions like Dolphin. This was way before Desmume X432R, but it was quite a journey. That kind of also gave me a purpose too; I'm actually still serious about writing my own DS emulator with user-defined internal resolutions from Day 1.

EDIT: A year later, I have to correct myself... I'm sorry, but I've learned better now.

Shonumi Wrote:but look at VBAM, ZBoy, or this GB emu if you want readable source code.

ಠ_ಠ

No. VBA-M's code is absolutely unreadable. That isn't an opinion. I've dug through it, and only a compiler could love that coding style (or lack thereof). I don't mean to offend the work of previous emulator authors, but every time I try to parse what's going on in there, I only rarely understand wtf it's doing. The distinct lack of comments helps not one bit.


RE: Programming Discussion Thread - HoodedMan - 12-03-2014

Thanks I will look into the Zboy and chip 8. I am normally good with CPU stuff, but I always get stuck when doing graphics.


RE: Programming Discussion Thread - Shonumi - 12-03-2014

Well, you could checkout ZBoy for reference, or you could check out GBE -> http://code.google.com/p/gb-enhanced/source/browse/

Just saying. :p


RE: Programming Discussion Thread - HoodedMan - 12-03-2014

Very Interesting, I will take a lot at GBE as well!


RE: Programming Discussion Thread - AnyOldName3 - 12-04-2014

I took part in a hackathon today, and won a raspberry pi. For some reason, all the other teams seemed to think they needed to program their game from the ground up in Java or Python. My team were quickly persuaded that if the rules didn't forbid the use of GameMaker, then it'd be a quick and easy way to get something working with basically no effort. We met the spec about an hour before anyone else, and had released two sequels before a third team met the spec. No-one else bothered with animations, or sounds, or so-bad-they're-good sprites, or evil ghost reindeer, or steering, so Banta Clause (and thereafter Banta Clause Plus and Banta Clause 2) was the winner by a long chalk.

All of this was after we've had weeks of lectures reminding us that it's bad practice to rewrite code that already exists if it's known to work. Anyone who accused us of bending the rules or generally being mean stopped complaining after being reminded of this.


RE: Programming Discussion Thread - garrlker - 12-04-2014

Congrats man! GM for the win! Just wondering but were you on the GMC back in the glory days(2004-2008)?


RE: Programming Discussion Thread - AnyOldName3 - 12-04-2014

Assuming GMC is their forums, no, but I did have a pro key for GM6 when I was little. I've known how to do some things with it for forever, but missed the window to convert my GM6 key to a GM7/8 one