I built a Game Boy emulator in F#

(nickkossolapov.github.io)

69 points | by elvis70 1 hour ago

10 comments

  • debugnik 41 minutes ago
    Cool to see F# here! Emulators are a great way to learn a language. On first sight you chose well between more or less idiomatic F# for each job.

    Some low hanging fruit to reduce allocations: the discriminated unions in Instructions.fs could be [<Struct>], reusing field names to reuse internal fields.

    Also, minor nitpick but I'm confused about some of the registers. They are already of type byte, the setters with `a &&& 0xFFuy` don't add anything over `member val A = 0uy with get, set`. I'm guessing this changed over time.

  • cermicelli 1 hour ago
    Finally someone putting in actual human effort to learn something, and not a LLM helped me build X in Y minutes.

    There is some hope for humanity after all I suppose.

  • thrownawaysz 2 minutes ago
    mildy related but wasn't there an emulator (maybe not GB but NES or SNES?) which had a visual panel showing each CPU cycle step by step? afaik it was very slow but the 1000% accuracy was the goal not playability.
  • CSMastermind 12 minutes ago
    Insanely cool. I've had it in the back of my mind to write a Rust compiler for the game boy for a long time and everytime I see something like this I think about brushing off that project.
  • yoyohello13 45 minutes ago
    I always find emulators written in functional languages impressive. It tends to be much easier to map hardware to an imperative language. I enjoy seeing the functional abstractions people come up with.
    • skrebbel 25 minutes ago
      Did you look at the code? F# has mutable variables/arrays and this uses that for eg memory.
      • yoyohello13 1 minute ago
        Yeah I did see that part. Although he mentioned his Chip8 emulator which was fully immutable. Still interesting so see when people use the mutability escape hatches.
  • z500 56 minutes ago
    That's so cool! I love F#, but I wrote a little Smalltalk interpreter in it and I can confirm it isn't exactly a speed demon for that kind of thing if you use it as intended lol
    • tombert 49 minutes ago
      I've found that with F#, I get better performance if I do dumb imperative stuff, but keep the side effects within a function. At that point, the functions can basically be "pure" but you can get decent speed.

      For example, I usually like using the `Map` data structure, and that's a pretty neat immutable structure and is usually fine for most stuff, but when performance becomes critical, it's easy enough to break into a boring imperative loop with a regular hash map. If I keep everything contained into one function, I usually can avoid feeling super dirty about it.

    • jackmott42 43 minutes ago
      With some care about what features to use and when, F# can be very fast. Which is nice, use functional paradigm when you want, or low level imperative code in hot loops if you need. But yeah if you use linked lists and sequences and immutable data types everywhere it sure isn't Rust.
  • hmokiguess 1 hour ago
    F# is super fun, awesome work!
  • MattCruikshank 48 minutes ago
    Sorry for the tangent - does anyone have some really zoomed in views of GB, GBColor, GBA screens in operation? I'd love for retro shaders to be able to more faithfully reproduce.

    I mean, ideally, we'd run different color test patterns through, in different lighting conditions, to build a really detailed model, right?

    • Galanwe 32 minutes ago
      I guess buying the second hand devices wouldnt be that expensive.
  • __loam 54 minutes ago
    I'm actually starting a new project to create a gba emulator in zig, and also starting with chip8. I'm going to skip nand to tetris because I played Turing complete. Cool to see I'm on the right track!
  • Ginop 34 minutes ago
    I misread Fem-Boy and I was not understanding the context anymore lol