Live data from Hacker News

Implementing a NES Emulator in Rust

michaelburge.us

11–20 of 93 posts

Re: Implementing a NES Emulator in Rust

#11
post #2

Question, what made it easier to build this in Rust than other languages? I know the advantages of Rust, but what exactly was the experience in this context?

It seems that unsafe references were used, so the advantages of Rust appear to have been somewhat relinquished in this case.

I am not sure you can not use `unsafe` in an embedded type of project. `unsafe` isn't necessarily bad, but when used at least makes it very clear where extra attention should be paid.

Re: Implementing a NES Emulator in Rust

#12

Earlier quoted context omitted.

It seems that unsafe references were used, so the advantages of Rust appear to have been somewhat relinquished in this case.

I am not sure you can not use `unsafe` in an embedded type of project. `unsafe` isn't necessarily bad, but when used at least makes it very clear where extra attention should be paid.

I also wrote a NES emulator in Rust, and managed to do it without using a single unsafe block. I used SDL bindings for graphics, if that counts, but it can also run in headless mode (for testing) without SDL perfectly well.

Re: Implementing a NES Emulator in Rust

#13
post #2

Question, what made it easier to build this in Rust than other languages? I know the advantages of Rust, but what exactly was the experience in this context?

It seems that unsafe references were used, so the advantages of Rust appear to have been somewhat relinquished in this case.

unsafe exists in Rust for when you need it. When it is used, it means nothing more than you’re writing code for which you must ensure the safety instead of the compiler.

The times when unsafe is poorly used are when it’s being abused to ignore safety related compilation issues, like lifetimes and/or mutability. Though, even in those cases it might be ok if you’re using lower level concurrency primitives to enforce those guarantees.

unsafe does not remove all advantages of Rust, it only removes a few restrictions. It should be avoided unless necessary.

Re: Implementing a NES Emulator in Rust

#14
Tiny off-topic, I was at hackaday Belgrade last year and a very good friend of mine ported a NES emulator on the badge. It was amazing for me seeing him how he solved the issues so quickly and how productive he was. And it was fun fiddling with an emulator on a uncommon embedded target.

Re: Implementing a NES Emulator in Rust

#17
post #12

Earlier quoted context omitted.

I am not sure you can not use `unsafe` in an embedded type of project. `unsafe` isn't necessarily bad, but when used at least makes it very clear where extra attention should be paid.

I also wrote a NES emulator in Rust, and managed to do it without using a single unsafe block. I used SDL bindings for graphics, if that counts, but it can also run in headless mode (for testing) without SDL perfectly well.

Do you have a link? I'd like to see how yours compares with OPs

Re: Implementing a NES Emulator in Rust

#18
post #8

Earlier quoted context omitted.

Implementing an NES emulator is a great way to learn more about a language and the internals of the NES (which is well documented). It's sorta like a rite of passage and has been done in most of the "newer" languages like Nim ( https://github.com/def-/nimes ) , Go ( https://github.com/fogleman/nes )

Well-documented, but many sources are contradictory. For such well researched hardware, there is a lot to be confused about when researching this topic by yourself.

At least there's no shortage of implementations to reference.

Re: Implementing a NES Emulator in Rust

#19

What was the most challenging part for you in building this emulator?

It took 14 days to get it functionally-complete, where I could beat Mario using savestates.

The PPU took 7 days, the CPU took 5 days, the APU took 1 day, and there's a spare day for everything else.

The CPU was easy to make: Its natural unit of output is a single CPU instruction. Even if there are many details, it's a straightforward process to compare an instruction-by-instruction log with a reference log until there are no differences.

The PPU was harder because the natural unit of output is an entire frame: It's not any easier to get the first pixel correct, than to get them all correct. So almost everything needs to be in place before you can validate its output.

Re: Implementing a NES Emulator in Rust

#20
post #12

Earlier quoted context omitted.

I also wrote a NES emulator in Rust, and managed to do it without using a single unsafe block. I used SDL bindings for graphics, if that counts, but it can also run in headless mode (for testing) without SDL perfectly well.

Do you have a link? I'd like to see how yours compares with OPs

Not finished, but for what it's worth: https://github.com/nukeop/mr-cool-nes
Post reply on HN