8 ms·
Pokegb: A gameboy emulator that only plays Pokémon Blue, in 68 lines of C++
- ggambetta 5y agoI did something similar with an X86, with enough opcodes implemented so it could run Goody (an ancient CGA game): https://gabrielgambetta.com/remakes.html https://gabrielgambetta.com/remakes.html Super fun exercise!
- mettamage 5y agoYou might want to add (obfuscated) to the title. The actual source code, while small, is much larger [1]. (edit: at 589 lines it's still impressive!) For people who like this kind of thing and/or for people who want to be able to understand what is going on at all, you might like the course NAND2Tetris [2]. It's a good prerequisite to understanding this blog post if computer systems isn't a topic that one has explored before. [1] https://gist.github.com/binji/395669d45e9005950232043ab4378abe https://gist.github.com/binji/395669d45e9005950232043ab4378a... -- the author notes this in the article. [2] https://www.nand2tetris.org/ https://www.nand2tetris.org/
- ngokevin 5y agoThe obfuscated one is awesome though, the code is in the shape of pokeballs!
- nighthawk454 5y agoStill, the unbofuscated one is only 589 lines including formatting, a copyright header, and what looks to be an 80-char line limit. Seems sufficiently interesting either way
- Aachen 5y agoSee, the truth is just as impressive to me, but knowing it's not a lie and a more than fair line length makes me appreciate it.
- userbinator 5y agoI think instead of line count, either final binary size or source code size in bytes should be more common in claims of minimalism; the demoscene, for example, always uses binary size.
- Blikkentrekker 5y agosource code size can still be manipulated by using very small identifiers or reusing them. There should rather be a normalization metric where all identifiers are treated as eight characters long Resulting binary will be different depending on the compiler and type of optimization and would simply be manipulated by optimizing heavily for size.
- binji 5y agoI got kinda dragged about this on reddit/twitter. Afterwards I posted on r/tinycode with the size in bytes: https://www.reddit.com/r/tinycode/comments/nn5djb/pokegb_a_gameboy_emulator_that_only_plays_pokemon/ https://www.reddit.com/r/tinycode/comments/nn5djb/pokegb_a_g...
- kzrdude 5y agowould it be possible to push it down to IOCCC size? Would be fun to see it there, of course.
- binji 5y agoI think so, when I was doing the write-up I noticed a few things I could have improved (and a few bugs!)
- iampims 5y agoDon’t let any of these comments take away from how great this is, _especially_ with the in-depth blog post.
- IgorPartola 5y agoNumber of nodes in the AST or number of machine instructions might be more meaningful.
- smithza 5y ago68 lines of code is pretty much arbitrary. It could have been far less, he just put it all to fit his poke ball pattern.
- dheera 5y agoThat, and obfuscation is also relative. You could call the 589 lines obfuscated as well.
- anonymousiam 5y agoThe author's tweet claimed 62 lines.
- binji 5y agoYep, I made a mistake it's actually 68.
- deleted 5y ago[deleted]
- andrewmcwatters 5y ago> When I started pokegb, I spent most of the first few days implementing the CPU. Every time I hit an instruction that wasn't implemented, I'd implement it and see if it would run any further. If you've ever written a drop-in replacement for some existing piece of software and filled the implementation with a bunch of `notimplemented()` (example)[1], you know exactly how satisfying this is. I love it. [1]: https://github.com/Planimeter/lgf/blob/master/lua/framework/love.lua#L38 https://github.com/Planimeter/lgf/blob/master/lua/framework/...
- binji 5y agoIt really is one of the best things about emulation development. It's surprising how quickly you go from "nothing works" to "I'm playing Super Mario Bros!"
- missblit 5y agoI never did get very far with my NES emulator after getting an actual job (it was plan B for if no one liked my resume), but I do remember that being oddly satisfying [1] [1] https://github.com/missblit/nesnes/blob/master/instructions_impl.h#L4 https://github.com/missblit/nesnes/blob/master/instructions_...
- city41 5y agoIt's one of my favorite things about programming. For example I made a Neo Geo tile viewer and decoding the format and figuring out just what needs to be done to get it working then suddenly BAM! tiles tiles everywhere! It's so satisfying.
- makapuf 5y agoI made a game console on an embedded system. The magic when you actually bitbang pixels to a screen and you see that first image is breathtaking.
- tiddles 5y agoThis x1000. I started implementing a JVM for shits and giggles a while ago, and once I had laid out ~220 panics for each opcode, it was incredibly satisfying to implement them one at a time to work towards bootstrapping the standard library. Granted, most opcodes are implemented incorrectly as minor things like method and field resolution aren’t quite there yet.. as soon as I’ve implemented enough opcodes to actually run something and return/exit/throw, the correctness will be a bugger to fix :^)
- binji 5y agoHi all, I'm the author of this post! Happy to answer any questions. :-)
- crowf 5y agoI may have missed it in the post, but how is blue different than red? I realise that they have slightly different Pokemon, but is that enough to make the emulator run only blue?
- binji 5y agoOops you're right, I should have mentioned that. Red works too, I just didn't want to write Blue/Red everwhere :)
- northern-lights 5y agoWhat about Yellow? If yes, you could shorted it as RBY which is more familiar to most old genners.
- glhaynes 5y agoIn the spirit of the project I'm surprised you didn't pick "Red" because it's one fewer characters. :)
- Y_Y 5y agoIt probably needs less memory, since you only 8 bits to store blue but red needs 3.
- shawntan 5y agoDoes the MissingNo glitch work?
- binji 5y agoNot sure, I think it should! Would be really cool if someone tried it out. I'm a little worried I didn't implement some necessary instructions/hardware features so the game isn't actually winnable :-}
- belthesar 5y agoThis reminds me of the GB emulator that ran inside of Pokémon Stadium. Folks found some tricks to make it work with other cartridges, but it turned out the emulator was not complete. https://n64today.com/2019/06/02/play-game-boy-games-on-n64/ https://n64today.com/2019/06/02/play-game-boy-games-on-n64/
- vmception 5y ago“This is super impressive Binji we would love to have you on our team, now that you’ve got our attention pass this leetcode challenge with a random assortment of unrelated skills instead, and faster than the people that only study this specific kind of test”
- mosselman 5y ago“It’s nothing personal, everybody has to do these, it wouldn’t be fair to the rest otherwise.”
- tkiolp4 5y agoHa. This made my day.
- binji 5y agoHa, I guess we'll see when I go for my next gig :-)
- userbinator 5y agoI can't speak for Binji but in my experience those who know the low-level enough to be able to write emulators like this will also do very well on leetcode-like challenges.
- ReflectedImage 5y agoI wrote a GB emulator, the main issue was debugging it. Subtlely wrong tends to lead to game crash very quickly. The trick I used was to take an existing GB emulator, add a printf debugging statement to print out each current instruction and the registers. Add the same printf statements on mine. Then use diff on the outputs of both emulators. It does play Pokemon. The emulator is called FireGB.
- progbits 5y agoComparing to an existing, known-good emulator is probably the most convenient way. This can be printf debugging like you describe, or even trying to compare framebuffer contents etc. However I feel like that "spoils" a bit of the fun. A more adventurous approach is to test with test ROMs [1] - they are simpler to follow by hand and discover why they don't work than real games. Of course if some game ROM is relying on some more obscure hardware quirks this might not be enough. Just wanted to bring it to attention for anyone interested in writing and debugging their emulators. [1]: https://gbdev.gg8.se/wiki/articles/Test_ROMs https://gbdev.gg8.se/wiki/articles/Test_ROMs
- ReflectedImage 5y agoAhh but it already passed the test ROMs at the point I turned to printf debugging.
- greatNespresso 5y agoThat's really incredible ! Still, for a total beginner like I am, 589 lines of C++ don't seem enough to cover it all, so what's the fuss? Where are the assets? What should I look for? Agreed, feel free to post on show HN to level up the debate
- hoten 5y agoIt's an emulator, not the game data/ROM.
- deepfriedrice 5y agoLove the explanation of the DAA instruction. I wrote a gbc emulator in college, but couldn't figure out what that instruction was supposed to do. I ended up just copying a lookup table from an open source emulator at the time, which of course didn't help my understanding: https://github.com/visualboyadvance-m/visualboyadvance-m/blob/e55ed5c0f49757652fbe0ee82e74da42e8e4d856/src/gb/GB.cpp#L286 https://github.com/visualboyadvance-m/visualboyadvance-m/blo... Funny to finally realize what this does, and now see how the lookup table works.
- binji 5y agoI kind of dreaded writing that one, to be honest! I didn't quite cover all the details but I hope it made a little more sense. :)
- anthk 5y agoSome Forth wacko would reduce that to 40 lines...
- sarak12070 5y agoSecret plan for weight loss Step by step guide http://healthwithbeauty.xyz/2021/06/02/six-week-makeover-diet-plan/ http://healthwithbeauty.xyz/2021/06/02/six-week-makeover-die... Orchid mantis Fun Facts These flowers can walk like animals https://www.interestingnews.club/2021/06/orchid-mantis-fun-facts.html https://www.interestingnews.club/2021/06/orchid-mantis-fun-f... Wheel whackers' keep snakes out of planes in California desert https://www.interestingnews.club/2021/06/wheel-whackers-keep-snakes-out-of.html https://www.interestingnews.club/2021/06/wheel-whackers-keep...
- anonytrary 5y ago#include <SDL2/SDL.h> How big is the final binary? It would be interesting to compare the final size to the total size of the GBC game. Although apparently > Many features are not implemented! So I'm not sure it's ready for that comparison.