nesdev.com
http://forums.nesdev.com/

Cycle-accurate Nintendo NES emulator in ~1000
http://forums.nesdev.com/viewtopic.php?f=3&t=15168
Page 1 of 1

Author:  zeroone [ Sun Nov 27, 2016 10:37 pm ]
Post subject:  Cycle-accurate Nintendo NES emulator in ~1000

This link recently appeared on Reddit (https://www.reddit.com/r/programming/co ... 000_lines/):

https://github.com/AndreaOrru/LaiNES

Author:  Sour [ Tue Nov 29, 2016 6:19 pm ]
Post subject:  Re: Cycle-accurate Nintendo NES emulator in ~1000

I took a quick look for fun, and calling it cycle-accurate is a bit of a stretch.

Bisqwit also made a ~1k line emulator in his youtube video a while ago - this looks like a pretty similar concept.
That video does have 160k views, so I guess writing something in the smallest number of lines possible is something that interests people in general. :)

On an unrelated note, the code uses "and" instead of "&&", and that's actually a part of C/C++, first time I ever saw it!

Author:  rainwarrior [ Tue Nov 29, 2016 8:41 pm ]
Post subject:  Re: Cycle-accurate Nintendo NES emulator in ~1000

Sour wrote:
On an unrelated note, the code uses "and" instead of "&&", and that's actually a part of C/C++, first time I ever saw it!

They weren't originally in C, but they eventually added a bunch of alternatives for operators, mostly to support locales/systems that might not have so many special characters:
http://en.cppreference.com/w/c/language/operator_alternative

I don't see people use them very often, but if you're more familiar with Python perhaps using and feels more natural than &&? I dunno.

Author:  koitsu [ Tue Nov 29, 2016 9:50 pm ]
Post subject:  Re: Cycle-accurate Nintendo NES emulator in ~1000

rainwarrior wrote:
Sour wrote:
On an unrelated note, the code uses "and" instead of "&&", and that's actually a part of C/C++, first time I ever saw it!

They weren't originally in C, but they eventually added a bunch of alternatives for operators, mostly to support locales/systems that might not have so many special characters:
http://en.cppreference.com/w/c/language/operator_alternative

I don't see people use them very often, but if you're more familiar with Python perhaps using and feels more natural than &&? I dunno.

I can't speak on behalf of said extensions, or Python, but and and && (like or and ||) in Perl are "almost" the same -- except when they aren't (TL;DR -- they differ in precedence and in what they actually return).

Know thy language and all that.

Author:  Bregalad [ Wed Nov 30, 2016 12:10 am ]
Post subject:  Re: Cycle-accurate Nintendo NES emulator in ~1000

Sour wrote:
On an unrelated note, the code uses "and" instead of "&&", and that's actually a part of C/C++, first time I ever saw it!

With #defines you can do pretty much anything this style. I do not know whether that's what he did or not, though.

Author:  Sour [ Wed Nov 30, 2016 5:00 am ]
Post subject:  Re: Cycle-accurate Nintendo NES emulator in ~1000

Bregalad wrote:
With #defines you can do pretty much anything this style. I do not know whether that's what he did or not, though.

Yea, but this is actually part of the spec - though it looks like the C spec just has these as a bunch of #defines in a specific header file.

I'm not a big fan of #define though and pretty much never use it, otherwise you end up with things like this: (From the linked emu)
Code:
#define T   tick()
#define G  u16 a = m(); u8 p = rd(a)  /* Fetch parameter */
template<Mode m> void ROL() { G; u8 c = P[C]     ; P[C] = p & 0x80; T; upd_nz(wr(a, (p << 1) | c) ); }
At this point, you either assume G or T defined the "a", "p", "C", "P" variables, or that they are globals (like "P" and "C" are in this case - I guess capital letter = global is what they are going for?)
Also, you have P, p and C, c as variable names in the same function, and they mean completely different things.

I understand some people like writing this kind of code, but when you go this far to shorten syntax, reading the code starts to feel a bit more like deciphering, rather than just reading.

Author:  Jarhmander [ Wed Nov 30, 2016 7:15 am ]
Post subject:  Re: Cycle-accurate Nintendo NES emulator in ~1000

Bregalad wrote:
Sour wrote:
On an unrelated note, the code uses "and" instead of "&&", and that's actually a part of C/C++, first time I ever saw it!

With #defines you can do pretty much anything this style. I do not know whether that's what he did or not, though.

You didn't understand. The keywords and, or and not (and a bunch of others) are reserved; they are actual keywords built in the language, not macros like in C.

Author:  James [ Wed Nov 30, 2016 8:00 am ]
Post subject:  Re: Cycle-accurate Nintendo NES emulator in ~1000

I like his use of templates for handling opcodes. I've been working on a Genesis emulator on-and-off for a while, but have struggled with finding a good way to deal with the 68000 CPU. I may borrow some of these techniques for that.

Sour wrote:
Bisqwit also made a ~1k line emulator in his youtube video a while ago - this looks like a pretty similar concept.

Bisqwit's 6502 code is super clever, but really incomprehensible... at least without watching his video that explains it. In terms of LoC (not that it really matters), I'd say Bisqwit comes out on top. While both have limited mapper support, Bisqwit's code is otherwise all inclusive. LaiNES uses Blargg's APU.

Author:  Sour [ Wed Nov 30, 2016 3:22 pm ]
Post subject:  Re: Cycle-accurate Nintendo NES emulator in ~1000

James wrote:
I like his use of templates for handling opcodes. I've been working on a Genesis emulator on-and-off for a while, but have struggled with finding a good way to deal with the 68000 CPU. I may borrow some of these techniques for that.

Just to be clear, I'm not saying all of it is bad, I feel like it goes just a bit too far in terms of trying to compress the code itself.
Templates can definitely be useful, I've used them in Mesen to make the syntax to load/save save states really simple and avoid boiler plate code.
But templates, just like define macros, can become really cryptic when you go too far with them.

Page 1 of 1 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/