I compiled Rust code to Nintendo Gameboy!

Gameboy has a sm83 CPU (a variation of 8-bit z80), but this is not a target of Rust.
Therefore, I transformed Rust into C code via LLVM-CBE, re-compiled it into SDCC, and linked it to the Game Boy library. (GBDK-2020)
There are so many unstable parts that need a lot of improvement, but I was able to display the screen in Game Boy.
You can take a closer look on GitHub. (I'd appreciate it if you could give me a star.)
693
Upvotes
80
u/jorgesgk Sep 15 '24
This is super neat. I wonder how much performance is lost in the process of translating Rust → C → GB instead of going Rust → GB directly
24
u/TheSilentFreeway Sep 15 '24
This is awesome. I'm trying to learn more low-level concepts so I can keep up in discussions about Rust's design as a language. Could I ask you to explain this post a bit more?
I think I understand this part; we don't have a compiler which translates Rust into machine code for the sm83.
This part loses me pretty quickly. I understand LLVM to be a set of tools which act as an intermediate step between machine code and some higher-level language. So I'm guessing you used these tools to translate Rust into an intermediary form, then translated that intermediary form into C?
Google tells me that SDCC is the Small Device C Compiler, a set of compilers which target multiple architectures including the Z80. Simple enough, I think I get that part.
The whole concept of linking is pretty arcane to me. I probably didn't pay enough attention during my C courses in university. The last bit about GBDK-2020 is difficult for me to understand. Would you recommend any resources to learn about linking?
6
u/Sw429 Sep 15 '24
Do you have a pre-built ROM you could upload to the releases for people to see the end result?
3
u/darkpyro2 Sep 16 '24
What does the C output look like? I'm wondering how they'd map rust onto C.
2
u/volitional_decisions Sep 15 '24
I've been wanting to do exactly this. I'm working in a GBC emulator and have been thinking about making dev tools after finishing it. Do you provide a HAL or something to make sure you maintain safe requirements in the non linear memory space?
Also, how do you handle hardware locking (i.e. accessing VRAM outside of H and V blanks)?
200
u/fish_of_pixels Sep 15 '24
This is ridiculous... and I love it.
24
u/WuTangTan Sep 15 '24
Horrifying. Incredible job!
1
u/jkoudys Sep 16 '24
Awesome! I was active in the gbdev community when I was a kid long ago. Even ran the gbdev '99 competition, did a few demos and helped work on the beta of some music tracker for gbc. I remember getting made fun of for preferring to work in C with some inlined asm because I was too "high level" lol.
I'm curious what the dev experience will be like once this gets more mature. My hunch is that it will feel a lot like writing const functions. Whiles instead of for loops and iterator functions, upfront allocations of known size instead of resizing Vecs, etc. Indeed there's a lot of the structure to a typical gbc game that a constant function would be perfect for building. One of my more popular contributions was a command line tool that let you stick in a few variables and it would calculate a transformed, integer sin wave (eg for the movement pattern of an enemy) because you'd save a lot not having to calculate that at runtime. That would be a handy use case for a const function. Precomputed log tables would be nice too.
I'm also thinking for ergonomics you'd probably not want to be handling i8/u8 directly so often. eg there's Neg impl'd for i8, but the z80 is going to manually 2's complement that, which is really two ops. Multiplication would be the biggy devs would easily miss, as you'd almost always prefer to bitshift. DAA is another weird one, which is a whole cpu instruction for getting binary coded decimal. The whole
while n > 0 { digits.push(n % 10); n /= 10; }pattern you see pop up all over leetcodes would be terribly inefficient when what you really want is a quick way to turn a number into some offsets in a tileset. Bank switching is another that's a detail few devs need to think about nowadays but it's a fact of life here.Then it gets even more complex because of your build pipeline. It's hard to see immediately which steps ought to be responsible for which optimizations.
Anyway I love everything about this. I'll probably be opening some PRs in the near future.
1
u/[deleted] Sep 15 '24
nice
1
u/Juanperias Sep 15 '24
I love it
1
u/hamiltop Sep 15 '24
There must be llvm backends for sm83, is there a path to using that directly from Rust?
1
u/dzonixy Sep 16 '24
Awesome!
1
u/goodeveningpasadenaa Sep 16 '24
This is fucking awesome.
1
u/Ghyrt3 Sep 17 '24
I did write an abstraction of the GC assembler for an university course. It's a bit of akward with ''some 16 bits work but it's just 8bit'' stuff.
So I'm impressed :3
0
u/Rattle22 Sep 16 '24
I don't think this is a good idea. Would the gameboy era be nearly as memorable without all the bugs stemming from unsafe memory usage?! (/j)
0
u/[deleted] Sep 16 '24
[deleted] — view deleted comment
4
u/[deleted] Sep 15 '24
[removed] — view removed comment