8 ms·
Porting a NES Emulator from Go to Nim
- warmwaffles 11y agoWell this is new. I've never seen Nim before. What does it offer that Rust, Haskell, Erlang, etc... do not?
- madez 11y agoI'd say that it feels more like a scripting language in comparison to Rust and is more approachable than Haskell or Erlang while being fast since it's compiled to C.
- mreiland 11y agobeing compiled to C doesn't imply fast :)
- def- 11y agoIt does have other advantages though, like inheriting C's excellent portability.
- coldtea 11y agoUnless done badly, it does. Transpiling to C gives you the opportunity to emit code that is straightforward clean and fast C for most operations, which avoid an abstraction overhead.
- jerf 11y agoNo, it doesn't. You could easily compile Python into C (basically, "just" inline the core interpreter loop), but it will still be slow, because the Python semantics still require a lot of dictionary resolutions on every operation. Getting efficient C still requires you to think about getting efficient code. You can not get it "for free" just by waving a C compiler over your language. Otherwise, everyone would just do that and there would be no languages slower than C. Before you reply to disagree, make sure to very, very carefully think through the implications of the last sentence there. It is not as if compiling to C is particularly hard; it is literally "homework assignment for a normal college senior in a compilers course"-difficult. Many languages have gone through a phase where they compile to C. Usually they deliberately leave C at some point, another thing to carefully ponder the implications of before replying in disagreement.
- coldtea 11y ago>No, it doesn't. You could easily compile Python into C (basically, "just" inline the core interpreter loop), but it will still be slow, because the Python semantics still require a lot of dictionary resolutions on every operation. Yeah, and you could do this too at the start of your emitted code from the transpiler: sleep(UINT_MAX) which would also make it slower than an interpreted language. But I specified I wasn't talking about that, or about "just inlining the core interpreter loop", but about transpiling letting you also emit straightforward and clean code without extra abstractions for most operations. A new language like Nim doesn't come with all the baggage of Python's runtime pre-existing, and doesn't need to recreate it.
- mreiland 11y agoHipHop doesn't support the entirety of the PHP runtime for exactly the reasons that have been laid out for you. That's reality from a company with a shitton of money and a vested interest in doing it successfully. They can't do it in the general case because reality disagrees with you.
- coldtea 11y agoNot sure what are you getting at. What I didn't say: transpiling is magic fairy dust for speed, and you don't have to care about language semantics at all. What I said: "unless done badly" (my words on the original comment), it's fast, as it gives you a chance to emit straightfoward C code for most operations. Now, regarding your example: First, HipHop is not transpiled to C. It's a runtime with a JIT. Second, what does "HipHop not supporting the entirety of the PHP runtime" has to do with anything? I take it you mean that the fact they had to skip some parts of the runtime for increased speed, proves something related to what I wrote. But (besides the JIT thing) PHP is not greenfield like Nim. They have a runtime, and it has to work in a certain way. PHP wasn't designed with transpiling in mind, and HipHop had to follow it closely as a design goal. I never said runtime decisions you have to mimic to be 100% compatible can't slow you down.
- 11y ago
- Dewie3 11y ago> while being fast since it's compiled to C. Which is fast compared to what? Being compiled to JS? It's not like being compiled to a "fast language" automatically makes that compiled program fast. You also have to consider how complex the runtime is, and other variables which are probably a direct result of the semantics of the language.
- def- 11y agoTwo of my old posts may answer that question: - http://hookrace.net/blog/what-is-special-about-nim/ http://hookrace.net/blog/what-is-special-about-nim/ - http://hookrace.net/blog/what-makes-nim-practical/ http://hookrace.net/blog/what-makes-nim-practical/ Summary by benhoyt: https://news.ycombinator.com/item?id=8822918 https://news.ycombinator.com/item?id=8822918 * Run regular code at compile time * Extend the language (AST templates and macros); this can be used to add a form of list comprehensions to the language! * Add your own optimizations to the compiler! * Bind (easily) to your favorite C functions and libraries * Control when and for how long the garbage collector runs * Type safe sets and arrays of enums; this was cool: "Internally the set works as an efficient bitvector." * Unified Call Syntax, so mystr.len() is equivalent to len(mystr) * Good performance -- not placing too much emphasis on this, but it's faster than C++ in his benchmark * Compile to JavaScript
- jedi_stannis 11y agoThanks, that first post is really interesting. Two things stand out to me: * Run regular code at run time - does this just error if you try to use const on something that depends on a runtime value? * Add your own optimizations to the compiler - isn't this very dangerous, especially if optimizations from various sources of code get combined? Even if your optimizations are valid (which there seems to be no guarantee of), computer math is notorious for being different from real math. People unaware of the nuances of integers rolling over or floating point seem like they could easily shoot themselves in the foot here.
- dom96 11y ago> * Run regular code at run time - does this just error if you try to use const on something that depends on a runtime value? Yep. For example `const foo = stdin.readLine()` would result in "Error: cannot evaluate at compile time: stdin". As for your second point. I haven't used this feature personally but I have heard that the compiler will tell you exactly where these optimisations are applied. You can also disable these optimisations very easily during compilation.
- deleted 11y ago[deleted]
- jug 11y agoI find the easiest way to think of Nim is "A Python-style language that compiles to C". Of course, the truth isn't as simple as that. But I like it, especially how pragmatic the language is. It doesn't try to "prove" a philosophy like Rust, but I think it gains other advantages in return.
- guelo 11y agoIt's closer to Go than Rust since it is garbage collected. It's statically typed, has OO features and first-class functions. It compiles to C making it pretty portable.
- andolanra 11y agoYou can have manual control over memory if you want, though. This worked the last time I looked at the language, at least: foo = cast[ptr Foo](alloc(sizeof(Foo))) # do stuff with foo dealloc(foo) EDIT: Here's the documentation on the site itself. Most things are 'traced' by the GC, but by using alloc and dealloc you can work with 'untraced' (i.e. manually allocated) memory: http://nim-lang.org/0.11.0/manual.html#types-reference-and-pointer-types http://nim-lang.org/0.11.0/manual.html#types-reference-and-p...
- pcwalton 11y agoGo has unsafe.Pointer too.
- rdtsc 11y agoHas generics, meta-programming and GC can be disabled (or tuned to run up to some max # of milliseconds). So it is Go++ if you wish. But I am not sure about its concurrency. Go does have channels which are nice abstractions for doing concurrency right.
- dom96 11y agoNim has channels too (although I think they need replacing). There is also spawn which is similar to Goroutines in some ways: http://nim-lang.org/0.11.0/manual.html#parallel-spawn-spawn-statement http://nim-lang.org/0.11.0/manual.html#parallel-spawn-spawn-...
- shurcooL 11y agoDid you port from Go to Nim by hand, or was it automated in any way? I thought that Go would be the last language I'd write by hand. Previously I wrote C++, which was a dead end in that I could never use tools to parse it and translate to a new language. But with Go it should be much easier to do that if/when I ever decide to switch to something else. The performance of the emulator in browser (compiled via emscrimpten) is very impressive! It felt like solid 60 FPS to me. I wonder how the Go version compiled via GopherJS would compare? Have you tried?
- def- 11y agoI did it by hand, changing the code as I saw fit and learning as much about the NES as I could. I haven't tried GopherJS, but I don't have much Go experience apart from reading it. On a related note, I read that porting the Go version to Android would be difficult and is still far in the future: https://github.com/fogleman/nes/issues/7 https://github.com/fogleman/nes/issues/7
- conradev 11y agoThere is also a NES emulator written entirely in JavaScript called jsnes[1] that would be interesting to compare to. The "solid 60 FPS" doesn't always translate to mobile. For example, jsnes runs fine on an iPhone 5S (64-bit processor), but stutters on an iPhone 5. [1] https://fir.sh/projects/jsnes/ https://fir.sh/projects/jsnes/
- Narishma 11y agoForget about mobile, it runs at like 5 FPS on my Core i3 laptop.
- mwcampbell 11y agoThe binary size difference is quite striking. Linux distro packagers are going to like Nim, I think.
- vezzy-fnord 11y agoI couldn't tell if the Nim result was actually statically linked. If it was, then it almost certainly wasn't using glibc, because the binary would certainly be much fatter.
- def- 11y agoThe Nim binary dynamically links against glibc and SDL2. If you really want to statically link against a C standard library, musl works just fine: @if musl: passL = "-static" gcc.exe = "/usr/local/musl/bin/musl-gcc" gcc.linkerexe = "/usr/local/musl/bin/musl-gcc" @end Now it compiles like this (SDL2 is still dynamically loaded, +1 MB for that): $ nim -d:release -d:musl c src/nimes $ ls -lha src/nimes -rwxr-xr-x 1 def users 219K Mai 1 22:00 src/nimes*
- loudmax 11y agoIf the Nim binary is dynamically linked, comparing the size to a Go binary isn't always valid. There are reasonable trade-offs to consider when selecting dynamic vs static linking. Admittedly, when you're running an NES emulator on your desktop probably isn't one of them, you're not likely to care either way. The Nim -> C -> emscripten path is very impressive. Kudos.
- ploxiln 11y agoGo also dynamically links glibc by default on linux, for the domain name resolver functions. I'm not sure what Go does on OS X, but I know that it's not possible to statically link the c library on OS X.
- sunnyps 11y agoI thought gc only statically links and it was gccgo that had the ability to dynamically link.
- haberman 11y agoLet me get this straight. We have an emulator for 1985 hardware that was written in a pretty new language (Go), ported to a language that isn't even 1.0 (Nim), compiled to C, then compiled to JavaScript? And the damn thing actually works? That's kind of amazing.
- shurcooL 11y agoWhat's that obligatory talk link where he describes that everything will be compiled to JavaScript and run in the browser over the next 50 years?
- madez 11y agoOld hardware is not necessarily hard to emulate. Often it's the opposite.
- haberman 11y agoI agree it's not the hardest part, it just makes the overall picture more delightful to imagine.
- deleted 11y ago[deleted]
- nahiluhmot 11y agoYou're part of the post now!
- deleted 11y ago[deleted]
- fogleman 11y ago> As I really liked fogleman's NES emulator in Go I ended up mostly porting it to Nim. The source code is so clean that it's often easier to understand the internals of the NES by reading the source code than by reading documentation about it. Win!
- AYBABTME 11y agoWent to read your code after that comment, it is truly, very clean code! :+1:
- mhd 11y agoI'm quite impressed about the small amount of code required for a NES emulator. I thought they'd have to do all kinds of special casing for cartridge-specific stuff…
- masklinn 11y agoThe more accurate the emulator is, the less special casing there's a need for: special cases/hacks are necessary when the emulator takes shortcuts and doesn't implement the hardware features the game relied on. If the hardware features are fully implemented all games ought work without hacks. Hence bsnes (as far as I know) not needing game-specific hacks but requiring a hefty config to run.
- chongli 11y agoThe problem with the NES is that games have all kinds of different hardware inside the cartridges. In order to achieve high compatibility you need to emulate all of these chips. It's not nearly enough to accurately emulate just the NES itself.
- def- 11y agoNot all games work. Only the most popular mappers are implemented, which covers about 85% of games.
- mreiland 11y agoMany cartridges came with custom hardware, you have to implement them specifically, which implies special casing for the hardware, which sometimes implies special casing for the individual game.
- esaym 11y agoIndeed, I didn't think it could be so simple either. Figured "what the hey" and thought about writing one in perl, but a few already beat me to it looks like: https://metacpan.org/pod/Games::NES::Emulator https://metacpan.org/pod/Games::NES::Emulator
- allendoerfer 11y agoThis title covers the essence of hacker news pretty well.
- nicklaf 11y agoAlso see: https://news.ycombinator.com/item?id=7745561 https://news.ycombinator.com/item?id=7745561 My favorites: > I decided to re-implement Javascript in Javascript. It failed. Here is my story > ReactOS running IE6 in a JavaScript x86 emulator: we put a browser in your browser so you can browse while you browse > Introducing js.js: a JIT compiler from JavaScript to JavaScript
- doomrobo 11y agoFor anyone who was wondering what I was: porting from libSDL calls to drawing on an html canvas is done automatically by emscripten
- lqdc13 11y agoAny plans to do let people call Nim functions from Python with Python standard objects like strings/dicts/lists as arguments? This would let people write the fast parts in Nim and slow parts in Python.
- progman 11y agoWhat's the point? Why not write everything in Nim? If you depend on certain Python libs that's understandable. But it should not be too hard to translate them to Nim since the syntax of Python and Nim are not very different.
- jboy 11y agoIf a company has a significant amount of existing code in Python, no sensible engineering manager will agree to a complete re-write of the existing code-base. It would divert resources from more-pressing functionality (new features, bugfixes, etc) and almost certainly introduce its own new bugs due to the complete re-write. (See also this classic Joel Spolsky article, "Things You Should Never Do, Part I": http://www.joelonsoftware.com/articles/fog0000000069.html http://www.joelonsoftware.com/articles/fog0000000069.html ) This would be the case whether you schedule the re-write in a single blocking development effort (in which case, all forward progress would stop during that time) or broken into batches over time (in which case, it will be much longer until the new system is ready, and the old system will be a moving target as it continues to be developed). Instead, the chances of a Nim-integration being beneficial to the company (and thus, your chances of getting approval from your engineering manager) are MUCH higher if you can simply write NEW functionality in Nim (or occasionally re-write small, self-contained inner loops in Nim) and the new Nim code integrates smoothly into the existing Python as a Python module. This is the approach I've taken at my company (with my engineering director's approval). In theory, you could even use skunkworks-Nim in your large Python codebase, as long as your Nim code presents itself as a good-citizen Python module, much like the tales of skunkworks-Scala being used in large Java codebases.
- progman 11y agoI am not talking about a complete rewrite of big business software written in Python. I just had small projects in mind which are typical for Python. Big business usually depends on Java, C# and C++ but not on Python. http://www.quora.com/Why-do-large-corporations-use-Java-or-C-over-Python-Ruby-and-PHP http://www.quora.com/Why-do-large-corporations-use-Java-or-C...
- beagle3 11y agoUbercool. Question about nim: from looking at https://github.com/def-/nimes/blob/master/src/nes/cpu.nim https://github.com/def-/nimes/blob/master/src/nes/cpu.nim , I wonder: is there way to give the no. of cycles and instruction encoding with the "op" template, so those 256 byte arrays get built automatically?
- dom96 11y agoI'm not the author but I don't see why not, you can just have an extra param to the "op" template.
- beagle3 11y agoBut how would you put it in the right place in the table? Can you just have something like cycles[opcode] := ncycles run during compile time in the macro, resulting in a populated 256-byte table of all the opcode cycles and no anything done in runtime?
- filwit 11y agoI'm not sure exactly what you're asking, but I can answer at least part of it. You can build lists at compile time in Nim via the `static` statement or `compileTime` pragma. Eg: # using the pragma here, but we could use a 'static' block instead var cycles {.compileTime.}: int # --- proc doSomething = static: # invoked once at compile-time (at this definition) cycles += 1 proc doSomethingGeneric[T] = static: # invoked once at compile-time (per unique generic call) cycles += 1 macro doSomethingAtCompileTime(n): stmt = # invoked at compile-time (per call) let ncycles = int n.intVal cycles += ncycles # --- doSomething() # this call doesn't effect 'cycles', it's declaration does (+0) doSomething() # ditto (just here to prove a point) (+0) doSomethingGeneric[int]() # this call effects 'cycles' (+1).. doSomethingGeneric[int]() # ..but only once (+0) doSomethingGeneric[float]() # this call also effects 'cycles' (+1) doSomethingAtCompileTime(5) # this call effects 'cycles' (+5) doSomethingAtCompileTime(12) # ditto (+12) static: echo cycles # prints '20' I'm not sure this helps solve anything in NimES, but this can be really useful for meta-programming in general. For instance I'm using it to make an event system which generates efficient dispatch lists based on the types/procs defined. It's designed for game logic, to both minimize boiler-plate and dynamic dispatch. Ie, invocation it's type-specific and usually does not use function pointers per-instance, so smaller 'events' can be inlined. Plus, instances are generic (decoupled from object inheritance), and often don't require any header data. That combination should, in theory (still WIP), give devs a 'universal' way to model a broad range of game objects from heavy single-instance types to lightweight projectiles and particles. Here's an example for a little clarity: # note: 'impl', 'spawn', and 'invoke' are macros type Foo = object val: int Bar = object val: string impl Foo: proc update = echo "Foo: ", me.val proc render = echo "Rendering Foo!" impl Bar: proc update = echo "Bar: ", me.val spawn Foo(val:123) spawn Bar(val:"abc") invoke update invoke render invoke blah # ouptput: # Foo: 123 # Bar: abc # Rendering Foo! # Invoke Error: No event 'blah' defined. Perhaps that's not the best example to illustrate Nim's meta capabilities, but so far Nim is the only language I've come across that allows me to achieve this short of thing (at least, directly from user-code).