12 ms·
Nestur: NES Emulator in Rust
- lilyball 7y agopcwalton wrote a NES emulator in Rust 7 years ago as a demonstration of Rust: https://github.com/pcwalton/sprocketnes https://github.com/pcwalton/sprocketnes Also, a nitpick: > One line of unsafe (std::mem::transmute::<u8>() -> i8) You don't need unsafe for this, you can do that with an `as` cast.
- deleted 7y ago[deleted]
- swolchok 7y agoThey seem to be equivalent: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=c852d2684941fe04eeb103a97bfd936b https://play.rust-lang.org/?version=stable&mode=debug&editio...
- deleted 7y ago[deleted]
- SlowRobotAhead 7y agoLike the that your hobby might be opening Rust code, searching unsafe just for the thrills :) Edit: Jesus kids, learn a little chill it was a joke. Someone didn’t like this so much they went through and downvoted all my other posts they could. Wow... that’s real serious.
- nevi-me 7y agoIt's a quote from TFA
- zellyn 7y agoIt was mentioned in the README, and stood out to me as surprising (surely you don't need unsafe to cast from unsigned to signed bytes?)
- slunk 7y agoWithout knowing enough about Rust to speak authoritatively... u8 can represent larger integers than i8 (no sign bit). Surely it's not completely safe.
- zellyn 7y agoHmmm. I was assuming this was just changing interpretations of the same underlying 8 bits. No? Edit: yeah, “Casting between two integers of the same size (e.g. i32 -> u32) is a no-op” (https://doc.rust-lang.org/reference/expressions/operator-expr.html#semantics https://doc.rust-lang.org/reference/expressions/operator-exp...)
- slunk 7y agoYup, fair enough.
- p4lindromica 7y ago`mem::transmute` is roughly equivalent to a `reinterpret_cast` in C++. It treats the bits of a u8 as an i8. In the Rust definition of safety (mutable xor shared, no data races, memory safety, etc), treating the bits of a u8 as an i8 is safe and can be done with an `as` cast.
- slunk 7y agoThanks for clarifying that for me!
- spieglt 7y agoYeah, I'm going to fix that. I thought I'd tried `u8 as i8` and it hadn't worked, but I guess not.
- Macha 7y agoPossibly clippy complained about it and you mistook it for a compile error?
- zellyn 7y agoI noticed that both this and pcwalton's CPU emulation simply perform the operation, and increment the cycle count. I'm curious: are the cycle-accurate reads and writes (and false reads and writes -- see https://github.com/zellyn/a2audit/issues/5 https://github.com/zellyn/a2audit/issues/5) not necessary for NES emulation? I've never emulated a NES, only an Apple II. The cycle-accurate reads and writes are only mostly unnecessary, but they can make a difference, as the discussion in that linked issue shows.
- jchw 7y agoSometimes, especially when dealing with interactions between chips, even sub-cycle behavior needs to be emulated for correctness. I suspect for basic emulation though, rigorous cycle accuracy is not really necessary for most software...
- monocasa 7y agoIt's equivalent to Apple II. You can get pretty far with the naive solution of atomically completing the instruction, and incrementing the cycle count appropriately, but there'll be some issues until you switch to a cycle accurate state machine with all the warts (false accesses, etc.) NES actually gets a bit grosser because the PPU runs (IIRC) five cycles for every three CPU cycles or something like that, and you need to handle that case. I've seen some emulators that just run it off of a 15x clock, and sparsely run cycles on the CPU and PPU (but most of the master cycles don't do any work).
- nycdotnet 7y agoliterally fizzbuzz
- klodolph 7y agoI think the NES PPU:CPU clocks are 3:1 for NTSC and 16:5 for PAL. The exact speeds are integer ratios of the color subcarrier frequency. The master clock runs at 6x the color subcarrier frequency, which gives you 12 different hues in the color palette.
- MBlume 7y agoI see an SDL dll file in the repo, and on my mac the build fails with `library not found for -lSDL2` -- is there something special I need to do to get this to build, or is it Windows-only?
- MBlume 7y agoAh, `brew install sdl2` fixed it
- reificator 7y ago> (Warning: this pipe currently takes you to an empty room, it's not the only one, and I don't know why.) If you look into speedruns you'll see that pipe behavior is very strange. One of the top speedruns involves pushing Mario into blocks in order to move the camera by something like 10 px, so that when you go down a certain pipe it goes to the warp zone instead of the bonus area. Might be worth watching some videos about that speedrun and trying to trace what's going on in the code to allow that. It might get you closer to understanding your own pipe issue. Unfortunately I don't have a link offhand, but search something like `perfect mario speedrun` and you'll probably find it.
- simlevesque 7y agoYou are thinking of 4-2. This is the 1-2 pipe. Here is the video: https://youtu.be/i1AHCaokqhg https://youtu.be/i1AHCaokqhg
- reificator 7y agoOh I recognize where it is, I've wasted away plenty of time in Mario games over the years. I brought it up because I'm told that the general idea applies to all pipes. They take you to the secret area for that level, and if there are multiple pipes that lead to different areas then that secret area has to be switched out. That sounds like it's relevant if the secret area in this case is an empty space, perhaps that memory hasn't been initialized or there's a specific load instruction that's doing something different in the emulator as compared to the hardware. (And for all I know the emulator could be following the spec correctly, but the hardware worked differently) Either way, thank you for the video link.
- eyegor 7y agoTrying to divine the eccentrics of the nes would be a nightmare. In order to emulate the hardware properly, there are several bugs that need to be reproduced such as the oamaddr bug for example. Most small projects like this aren't striving for cycle and bug perfect emulation, just for proof of concept. If you want state of the art emulation for accuracy, look at mesen or bizhawk. Unfortunately they both incorporate so many other features that reading the "core" emulation bits is difficult. So for casual reference, projects like this one are great. [1] https://github.com/sourmesen/mesen https://github.com/sourmesen/mesen [2] https://github.com/TASVideos/BizHawk https://github.com/TASVideos/BizHawk
- ltriant 7y agoI recently wrote a NES emulator in Rust aswell. It was one of the most fulfilling side projects I've done. I did a brief write-up too: https://ltriant.github.io/2019/11/22/nes-emulator.html https://ltriant.github.io/2019/11/22/nes-emulator.html Kudos to getting to this point. Adding support for more mappers is a nice incremental thing that can be done at your leisure, and once you get save states implemented, playing games at your own pace is super fun :)
- Cyph0n 7y agoThat’s an excellent write up and some well written code! I might use this as a reference if I ever try to write an emulator in Rust :)
- mkagenius 7y agoADC = A + V + C SBC = A - V - (1 - C) SBC = A + (-1 * (V - (1 - C))) SBC = A + (-V + (1 - C)) SBC = A + -V + 1 + C I am not able to follow the mathemagic behind the third line. Is it normal algebra or its in 1's complement?
- ltriant 7y agoWow, yeah, that's a bit off. And incorrect. It should end up being equivalent to: SBC = A - V - 1 + C Thanks. I'll fix this up :)
- mkagenius 7y ago> but the - 1 is taken care of because of the fact that ADC and SBC do the opposite thing with the carry flag. Also, this line is bothersome, they do opposite things but that's why you have already done (1 - C). Maybe the fact that -V is actually (!V + 1) (in 2's complement) that eats up the extra -1 is the correct explanation? So, SBC = A - V - 1 + C SBC = A + !V + 1 - 1 + C SBC = A + !V + C
- ltriant 7y agoAh yeah you’re right. I had a brain fart and got my -V and !V confused. At the time of writing I was sure I had it right too! I’ll fix that up. Thanks :)
- deleted 7y ago[deleted]
- sdrothrock 7y agoI wonder if the name "Nestur" is a callback to "Nester" from the "Howard & Nester" comics in Nintendo Power: https://hn.iodized.net/main.htm https://hn.iodized.net/main.htm
- spieglt 7y agoActually a nod to https://adventuretime.fandom.com/wiki/Neptr https://adventuretime.fandom.com/wiki/Neptr
- sdrothrock 7y agoAhh... a neat coincidence anyway. :) Thanks for letting me know!