r/EmuDev • u/C_Sorcerer • Oct 31 '26
struggling with emulator development in C (compared to C++)
This might sound obvious since C lacks a lot of the QOL that C++ does but I wanted to hear opinions on emulator dev in C. I am working on my first emulator (gameboy emulator) which is a passion project I have wanted to do for a while. I graduated with CS degree (conc. hardware and embedded systems) in May and I am waiting on my GF to get out of school before I get a job, and I have a lot of time on my hands outside of my menial labor maintenance job. So I said, maybe I should work on this! And to make it even more juicy, I began my gameboy DMG emulator in C.
Now dont let my reddit name fool you; I love C, but for the past 5 years or so I have almost exclusively used C++, particularly for game engines and renderers. Mostly because I love computer graphics, and wanted to do that, but upon the realization that computer graphics is going to require a PhD and is a bloodbath to get into, I went with my next biggest interest which is embedded systems and computer hardware and low level systems in general. Because of that, I thought it'd be a good idea to make this in C, right? It would be better experience for getting into embedded/firmware roles? Well, not really. After 2 months of working on it, I just hate the structure of my code. I think it looks like shit. Every modification I have to make feels like a 2 day long chore. I finished the entire set of opcodes, interrupts, and the memory architecture, and have gotten through Blarggs test roms, all but 02-interrupts which is failing on #5, im assuming because of the timer not being implemented. But now I have realized I am going to have to completely restructure my code and how I already had it is just not working out. I never had this problem with C++; I like expressive and explicitly defined objects and thats just what I love about it. But honestly I never had this problem with C before this project. I have written several Linux Kernel drivers and embedded programs in bare metal C, but for a project like this its making me lose my mind. I have too many things tightly coupled and then other things not coupled at all, weird function names, structs that are all over the place, and honestly i feel like im losing my fucking mind lol. Is this supposed to be this difficult with C over C++? I mean I wrote a game engine in 3 months with C++ (after years of experience of course) and it didnt feel that bad to modify certain aspects, but this is grueling.
I just want to know what your thoughts are on this and if I am just a noob getting massively skill checked by using C in a bigger application like an emulator, or if its just not as well suited to an emulator. Any anecdotal stories on if doing an emulator in C or C++ makes a difference as far as getting hired for roles in low level programming is appreciated as well :)
14
u/deaddodo Oct 31 '26 edited Nov 01 '26
If you just wanted to get your feelings off your chest, you succeeded.
But, having developed emulators in C, C++, Rust, Zig, JS, etc....I don't think my feelings align with yours. It would help if you gave specific examples and what about C++ you feel makes them easier for you to manage. C++ certainly has language features that make these sorts of projects easier, but it's hard to know what pain points you're having and if you're just doing something wrong, otherwise.
2
u/Anime_Coomer Nov 01 '26 edited Nov 01 '26
You see, programming emulators in C is a great learning effort, a little heavier than with other languages (and not necessarily a handicap in all cases imo), precisely because of your first point. I'm making a PSP emulator in C, and i'm using it in such a way that i think had i written it in C++ it would've come out way different (because of different practices and some features C++ compilers don't even support or work well with), so like all languages it is a tool and you need to use it the best way you can, and since C will not hold your hand for anything, you're forced to make up for that by yourself, it keeps your brain just a little more active, it definitely is not a losing battle in comparison to C++.
Edit: in essence, the way i see it, you can do anything in C, you just need to find the best way to do it, and i'm sure you can find ways to restructure your code and eliminate the issues you've mentioned
4
u/reybrujo Oct 31 '26
Well, there's not really much to do to make it not look "like shit" because you are used at programming with objects (imperative ones, not pure ones but hey, it's better than nothing). You need to just dismiss most of what you know about and just go the "static" route, as if your C++ code was a bunch of static functions. You can, at most, go the old good C (nowadays Golang) route of having one file per pseudo class having only extern a determined set of instructions and having everything else static inside but other than that you shouldn't really try to express C++ in C (even though it's a superset): I've fallen for that writing arrays of void pointers to accomodate functions and abusing unions, eventually you realize that's not why you are using C, you are going to C because it's light, it's quickly and you really don't care much about everything inside. Just pick any old code written in C (or even C++ for that matter) like the Linux kernel, it's a bunch of one-character variables deferencing everything with null checks everywhere and stuff so cryptic not even the author remembers what it does after a week of vacations.
I didn't write an emulator in C though I wrote a scripting language in C once, it was cool but yeah, it was during college when I learned about automatas and went hardcore on it. And yes, the code looked like shit but it was the fastest code with the exception of assembler I've ever written (which was kind of the goal anyways).
2
u/Producdevity Oct 31 '26
Could you give some examples that you find hard to do in C and not in C++? There might be some ergonomics in C++ that you are used to, but there isn’t something fundamentally different about C, and especially if you have enough experience with the language to have written several kernel drivers and embedded programs.
Maybe you can share more about what is so different with a project like this? Would like to help but I don’t think I understand the problem and feel like it is just that you are more comfortable with C++.
Also, bit off topic; but what is your need for C if you seem to dislike so much about the language?
1
u/Dezgeg Oct 31 '26
Are you aiming for career in bare-metal embedded development or embedded Linux development? If for the latter, then it's quite high chance you end up working with C++ anyway (in my experience at least). Are you really sure you actually have a "need" to learn more C?
1
u/ShinyHappyREM Nov 01 '26
That's to be expected, right? Getting the stucture perfectly right on your first try is unlikely. And changing aspects of the CPU emulation is always going to be labor-intensive, because there are hundreds of opcodes.
You can certainly define the CPU, the other components and the mainboard as objects (or to be more precise, structs with functions). Note that most of them will be singletons.
My philosophy is that any components bundled as its own "thing" (chips, cartridges, the base unit) can be coupled as tightly as you want. Cartridges can have bodge wires that connect components in unusual ways. The CPU can theoretically connect any register to any other register. But things outside the CPU cannot access registers directly, and things outside the base unit can't access RAM / VRAM.
How about you just post a link to your source code?
1
u/panaeon Nov 03 '26
well, zig can help given how many qol features it has over c
-2
u/sergio-gdr Oct 31 '26
Hey, I feel you… C is just a different beast, in my opinion. It is simply too ‘raw’, for lack of a better word.
I’m also working on a C Gameboy emulator, with a focus on structure and design, so maybe take a look for inspiration: https://github.com/sergio-gdr/realboy