News for hackers. Stuff that matters.
Re:Writing a Game Boy Emulator in OCaml by eatonphil on Wednesday January 12, 2022 @09:58AM (#29907147)
My unpopular opinion is to emulate an extremely reduced subset of x86. For example, jump to main and emulate the result of `int main() {return 2;}` compiled with gcc. You already have the compiler (no need to cross compile), you already have the decompiler and you can run the binary on the reference machine without an emulator. You save so much time having this entire toolchain already and you can start emulating simple C programs for x86 with <10 instructions and 1 or 2 addressing modes. I've written about this process before in Node and Go [0].

Yeah everyone will comment after me that x86 gets way more complex over time but if you just want to get a quick intro and you aren't a perfectionist x86 is kind of compelling.

And recently I wanted to try emulating MSP341 at someone's recommendation because it has a very simple architecture. But just getting a C compiler for it isn't easy! When I've tried to cross-compile C to other archs like RISC-V it also wasn't straightforward and I just got stuck in this toolchain spot.

If it were easier to cross-compile simple languages like C then writing emulators for other environments would be easier.

If you're on a new Mac, my whole argument applies for aarch64 instead of x86.

[0] https://notes.eatonphil.com/tags/emulators.html

Re:Writing a Game Boy Emulator in OCaml by dogprez on Wednesday January 12, 2022 @05:53PM (#29913887)
But emulating the CPU is probably the least interesting part about writing an emulator for a video game system. Writing the other hardware components and getting them to work together is the interesting part imo. I'd say skip writing your own CPU emulator and just start with someone else's.
Re:Writing a Game Boy Emulator in OCaml by eatonphil on Wednesday January 12, 2022 @05:58PM (#29913941)
As a backend dev, being able to emulate my own program compiled with GCC was pretty rewarding personally.

But I'm not arguing everyone must do it. Just trying to share why one unintuitive approach (the "complex" x86 architecture) might actually be easier and cooler than you think.

Re:Writing a Game Boy Emulator in OCaml by gavinray on Wednesday January 12, 2022 @12:04PM (#29909515)
I thoroughly enjoyed what little I read of this, bookmarked it for later -- thanks!

https://notes.eatonphil.com/emulating-amd64-starting-with-el...

Re:Writing a Game Boy Emulator in OCaml by ant6n on Wednesday January 12, 2022 @02:27PM (#29911649)
I wish there was an easy way to compile to a subset of x86 (e.g. mem access only with simple mov, only using one flag, no extensions, no floats, pure 32bit instructions).
Re:Writing a Game Boy Emulator in OCaml by eatonphil on Wednesday January 12, 2022 @02:40PM (#29911789)
Yeah that would be even more convenient!
Re:Writing a Game Boy Emulator in OCaml by pizza234 on Wednesday January 12, 2022 @01:13PM (#29910679)
> My unpopular opinion is to emulate an extremely reduced subset of x86

I don't think 808x is such an unpopular machine for approaching emulation.

Some people use Space invaders (or contemporary titles), which is based on an 8080, as stepping stone between CHIP-8 and more modern (complex) platforms.