r/programming • • Jan 17 '16

I've started livecoding an n64 emulator in Rust! Here's a recording of the first session :) [x-post from /r/rust]

https://www.youtube.com/watch?v=Fsi9HPcyrU8
95 Upvotes

62 comments sorted by

31

u/astrange Jan 17 '16

You'll be contributing a lot to emulation if you get graphics to work in the first place - basically every N64 emulator out there has totally inaccurate graphics, since they just got the top 10 games working well enough and gave up.

8

u/yupferris Jan 17 '16

haha cool, I'll keep that in mind XD

11

u/immibis Jan 17 '16

HLE is a very strange thing indeed.

Imagine you wanted to emulate a PC running Linux, and for some reason you can't get a syscall to run correctly in the emulator. You could make it so the emulator detects that syscall, and when that syscall is called, then instead of jumping to the kernel and letting the kernel handle it, the emulator handles it directly. That's HLE.

(HLE isn't the only hack used in N64 emulators)

9

u/yupferris Jan 17 '16

There's a difference between good and bad HLE though. If it's correct it's correct ¯\(ツ)/¯

7

u/[deleted] Jan 17 '16

(HLE isn't the only hack used in N64 emulators)

Except HLE is not necessarily a "hack", it's often far more performant than LLE for complex operations (say, running some memory through a native copy of libpng instead of emulating the guest CPU running libpng on the same data).

It can accomplish the same thing as the real console/LLE, but faster.

0

u/immibis Jan 17 '16

The non-hacky solution is to properly emulate the code behind the syscall. The non-hacky solution for performance is to recompile it.

3

u/yupferris Jan 17 '16

I would say one non-hacky solution is to properly emulate the code behind the syscall. I think HLE if accurate enough is perfectly acceptable.

1

u/immibis Jan 18 '16

Until you try to emulate a kernel whose "get current process ID" syscall looks enough like Linux's "write to file" syscall, and then it corrupts a random file every time a process gets its ID.

3

u/yupferris Jan 18 '16

Then it wasn't accurate enough ;D

1

u/immibis Jan 18 '16

So you're saying that an HLE that is accurate enough to be acceptable is accurate enough to be acceptable?

4

u/yupferris Jan 18 '16

I'm saying that's true for both HLE and LLE, and that I'm not dogmatic about either approach.

2

u/so_you_like_donuts Jan 18 '16

That's because the N64 graphics and audio coprocessors are programmable through microcode and for some reason or other it's still impossible to find any documentation about it apart from bits and pieces here and there.

Then there's the fact that there are a lot of popular N64 games that use custom microcode. So unless someone decides to leak substantial microcode documentation, graphics emulation has to be implemented on a game-by-game basis (and is going to be pretty inaccurate, with a lot of workarounds).

8

u/AngularBeginner Jan 17 '16

As someone who was/is writing an console emulator himself I wonder: Why did you decide on the N64? Is this your first emulator, or are you experienced in writing emulators? I think there are a lot of easier consoles than the N64. :-)

6

u/yupferris Jan 17 '16

I've done chip-8, the the audio portion of the SNES, and I've started various ones that I haven't finished (virtual boy, williams arcade machines, ...). I chose N64 because I thought it'd be much more fun hardware to learn about, and because it'd be a nice challenge for me as well. I don't think I'd be motivated enough to record it all if I knew everything up front :)

7

u/yupferris Jan 17 '16

Forgot to mention I've also done a fair bit of coding for these consoles (and more modern stuff) in my demoscene works. Part of this choice is definitely motivated by the unique graphics hardware.

10

u/yupferris Jan 17 '16

Most notable/relevant are probably this and this

7

u/thegunn Jan 17 '16

Very cool! Love both of them.

8

u/yupferris Jan 17 '16

Hey thanks :) I gave a somewhat frantic talk on the tech at NVScene last year if you're curious!

3

u/thegunn Jan 17 '16

Oh wow, that was really cool. Most of it went over my head but I was able to get the gist of what you were talking about.

1

u/yupferris Jan 17 '16

thanks for watching, glad you liked it!

2

u/thegunn Jan 17 '16

I'm curious, what are you working on? I have dreams of making an NES emulator someday. I haven't quite worked up the nerve to start.

2

u/williamfwm Jan 17 '16

What are you afraid of exactly? There's nothing to lose....except all of your free time ;)

Writing your own emulator is a great learning experience. At this point practically everything about the NES is known, so you likely won't contirbute anything new to the community's collective knowledge, but there's no reason you shouldn't still write it for your own benefit.

If you're afraid that your emulator won't break new ground....you're right. If you're afraid that it will be a waste of your time, you're absolutely wrong. No time like the present to get started.

Oh...just don't make SMB1 your first milstone. It actually requires a few difficult features to get running. Start with other mapper 0 games and make SMB1 a second or third major milestone.

1

u/yupferris Jan 17 '16

+1 to all of this

1

u/thegunn Jan 18 '16

It's just a massive undertaking. I'm afraid that it's too big for me with my knowledge set.

2

u/williamfwm Jan 18 '16

Compatibility with most games is a massive undertaking, sure. But you can get started with a few hours here and there. The CPU is pretty straightforward. All the 6502 opcodes and timings are well documented.

But it's not satisfying until you see something, so you move into the PPU. You can write a slow PPU emulator in a reasonable time too.

Then hacking it to be efficient yet fairly accurate, and implementing all the various mappers, is what will keep your project going for months or years. But be glad the fun isn't over too quickly!

3

u/AngularBeginner Jan 17 '16

GameBoy emulator.

2

u/thegunn Jan 17 '16

Nice! Good luck man, post here if you ever release it.

2

u/cristiandonosoc Jan 17 '16

I'm "finishing" my gameboy emulator too (trying to get sound to run correctly is a bitch). I highly recommend to write one, it's fun, challenging and much less complicated that it seems from the outside.

1

u/Sotriuj Jan 18 '16 edited Jan 18 '16

Sounds fun, but really complicated to me, dont even know where to start tackling the problem. Is there any good starting read out there ? Dont think I'm up to that task, but Im curious on how one works on this kind of software.

2

u/cristiandonosoc Jan 20 '16

That's exactly what I mean when it's much more intimidating from the outside!

I remember very well how you fell about it and the only way to break it is to investigate and try to get a feel of the problem. Get the idea of we have a CPU that changes memory. Some of that memory actually corresponds to video RAM. The video hardware draws according to that RAM. The controllers write into a specific place.... and so on and on. In the case of an emulator, you look for specifications of the machine and also there are a couple of blogs that document how to make one. These blogs are fairly insufficient but are enough to give you a kick start.

Then you simply start simulating the machine piece by piece. It helps to know a little computer architecture, mainly concepts of address space and instruction fetch. With that you can start writing the CPU code. What was surprising is that the simple approach I took at first is actually a correct one, as it was with a lot of the things in the emulator. That's why I said that it's less complicated than what it seems, you just have to delve into the problem and start tackling it little by little. Though I won't lie: it's a lot of work.

If you're interested, I plan (so maybe will happen, maybe not) to write a fairly concise blog about how to make a gameboy emulator because the actual ones are lacking a lot of details. I plan too document everything necessary using the github repo as reference code. If I ever get around to it, I'll let you know.

1

u/Sotriuj Jan 20 '16

I'm quite interested, please do let me know if it ends up happening!

3

u/[deleted] Jan 17 '16

He mentioned having written emulators before (but not for which consoles). I'd definitely agree though, especially not knowing much about the technical details beforehand.

14

u/immibis Jan 17 '16

I tried to write an N64 emulator once. Much harder than actually writing the emulator was finding all the necessary information about N64s. That, and debugging the JIT compiler (then called a dynarec, because I guess "JIT compiler" wasn't in fashion).

2

u/yupferris Jan 17 '16

Yeah, gonna go with interpretation first for this project. Will probably be slow as shit, but we'll see :)

0

u/[deleted] Jan 17 '16

[deleted]

2

u/yupferris Jan 17 '16

really nice work btw :)

1

u/yupferris Jan 17 '16

Oh I definitely plan to! Just not at first :)

1

u/Sean1708 Jan 17 '16

Is there a difference between a JIT compiler and a dynamic recompiler? Or is it a case of all dynamic recompilers are JIT compilers but not all JIT compilers are dynamic recompilers?

2

u/yupferris Jan 17 '16

they're the same thing

2

u/[deleted] Jan 17 '16

dynamic recompiler, suddenly I have flashbacks to 10 years ago

2

u/brandonpelfrey Jan 17 '16

I really like the start; Subscribed. Emulation is hard work and especially as you go forward in time, finding decent documentation gets ridiculous quickly. I'll be interested to see how you go forward with this!

This last year a friend and I wrote an NES emulator with support for a few mappers. It was super rewarding. I'm working with someone else now on a Dreamcast emulator, but I'd like to do something similar to you to either 1) Teach how the NES works in a new C++ code base, or 2) Build from scratch a PS1 emulator. Thoughts?

1

u/yupferris Jan 17 '16

Thanks, glad you liked it! I would personally prefer to see a PS1 emu done as there's a lot less info about that system already available (afaik). In the rust scene there's rustation, which was a huge inspiration for me in starting this one, and a really nice writeup as well. I don't really know much about PS1 emulation beyond that, but yeah, I personally would much rather see PS1 than NES :)

2

u/beefngravy Jan 17 '16

Hi, really interesting video! Unfortunately a lot of it went over my head but I still enjoyed learning and your presentation. What was your path to learning so much rust in just 7 months? I'm trying to learn Go and would love to hear if you have like a 'learning plan' or something similar?

1

u/yupferris Jan 17 '16

Hey thanks! So one thing that helped is I've done a fair bit of C/C++/C# professionally. I also did F# before, so there were a lot of Rust concepts that I found pretty natural. But whenever I learn a new language I find the best thing by far is just to have a few projects you can use it on consistently where you touch enough of the standard library etc. That said, you can see in the video even though I understand the language I forget loads of tiny things ALL the time - this seems to always just be the case for me, no matter how long I use a language for :)

1

u/thejacer87 Jan 17 '16

just a heads up.. you have the date as Jan 15 2015

1

u/yupferris Jan 17 '16

haha thanks, my bad :)

1

u/yupferris Jan 17 '16

actually I looked into this; it looks like I only have the year wrong in the "we'll be right back screen", is it wrong somewhere else too?

2

u/thejacer87 Jan 17 '16

no, that would be where i saw it...

when i saw 2015, i thought "oh, sweet all the episodes are probably done already!!"

1

u/yupferris Jan 17 '16

aha, gotcha. Unfortunately we've just started, haha! I'll have that fixed for the next one; thanks again for the heads up :)

1

u/thejacer87 Jan 17 '16

ya np, this project is kinda good timing for me cuz im taking a SPARC/assembly course right now and i felt that i could understand most of what you were saying lol. i may just have to give Rust a try and follow along.

Can i ask why you are using rust? maybe you said it at the start of the stream. i skipped the first 15 mins cuz that where u started actually coding :)

2

u/yupferris Jan 17 '16

haha, nice! Yeah I went over it briefly; basically we need a systems programming language for performance reasons, and Rust offers a lot of benefits in terms of memory safety and just plain less typing. But really, it's just a language I like to use these days and it's at least as appropriate for the task as C/C++ :)

3

u/Sean1708 Jan 17 '16

it's just a language I like to use these days

The best reason to use any language.

1

u/dingari Jan 22 '16

Any info on when the third episode will go live?

2

u/yupferris Jan 22 '16

I'll be streaming tomorrow (Saturday), most likely around 1-2PM PST, so you can watch live at http://www.twitch.tv/ferrisstreamsstuff ! The video will go up on youtube a couple hours after we finish the session (takes awhile to upload).

1

u/OriginalPostSearcher Jan 17 '16

X-Post referenced from /r/rust by /u/yupferris
I've started livecoding an n64 emulator in Rust! Here's a recording of the first session :)


I am a bot made for your convenience (Especially for mobile users).
Contact | Code | FAQ

0

u/[deleted] Jan 18 '16

[deleted]

4

u/yupferris Jan 18 '16

Fact. :)

0

u/[deleted] Jan 17 '16

[deleted]

2

u/yupferris Jan 17 '16

Thanks for watching! Yeah, I'll be pulling in some public domain ROM's for the next one already.

0

u/[deleted] Jan 17 '16

[deleted]

8

u/yupferris Jan 17 '16

my editor is just emacs, with a custom theme. The integrated git client is called magit (and it's lovely :D)

10

u/_Skuzzzy Jan 17 '16

vim is a great editor, and plugins can make it git aware. Unfortunately the maker of the video uses Emacs, which is a sin.

18

u/yupferris Jan 17 '16

I know I'm going to hell from using emacs; my pinky's burn already!

14

u/csman11 Jan 17 '16

I'm pretty sure Editor God just lets all programmers into heaven. He gave us all the different editors because he enjoys the editor flame wars so much. He's probably the same god that introduced the tab/space debate as well.

2

u/Sean1708 Jan 17 '16

He's not quite as sadistic as Brackets God though.