Cycle-accurate Nintendo NES emulator in ~1000

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
Cycle-accurate Nintendo NES emulator in ~1000
by on (#183591)
This link recently appeared on Reddit (https://www.reddit.com/r/programming/co ... 000_lines/):

https://github.com/AndreaOrru/LaiNES
Re: Cycle-accurate Nintendo NES emulator in ~1000
by on (#183663)
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!
Re: Cycle-accurate Nintendo NES emulator in ~1000
by on (#183668)
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.
Re: Cycle-accurate Nintendo NES emulator in ~1000
by on (#183674)
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.
Re: Cycle-accurate Nintendo NES emulator in ~1000
by on (#183678)
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.
Re: Cycle-accurate Nintendo NES emulator in ~1000
by on (#183687)
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.
Re: Cycle-accurate Nintendo NES emulator in ~1000
by on (#183691)
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.
Re: Cycle-accurate Nintendo NES emulator in ~1000
by on (#183694)
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.
Re: Cycle-accurate Nintendo NES emulator in ~1000
by on (#183706)
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.