Cycle-accurate Nintendo NES emulator in ~1000

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
User avatar
zeroone
Posts: 939
Joined: Mon Dec 29, 2014 1:46 pm
Location: New York, NY
Contact:

Cycle-accurate Nintendo NES emulator in ~1000

Post by zeroone »

Sour
Posts: 891
Joined: Sun Feb 07, 2016 6:16 pm

Re: Cycle-accurate Nintendo NES emulator in ~1000

Post by Sour »

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!
User avatar
rainwarrior
Posts: 8735
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Cycle-accurate Nintendo NES emulator in ~1000

Post by rainwarrior »

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 ... lternative

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.
User avatar
koitsu
Posts: 4201
Joined: Sun Sep 19, 2004 9:28 pm
Location: A world gone mad

Re: Cycle-accurate Nintendo NES emulator in ~1000

Post by koitsu »

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 ... lternative

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.
User avatar
Bregalad
Posts: 8056
Joined: Fri Nov 12, 2004 2:49 pm
Location: Divonne-les-bains, France

Re: Cycle-accurate Nintendo NES emulator in ~1000

Post by Bregalad »

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.
Sour
Posts: 891
Joined: Sun Feb 07, 2016 6:16 pm

Re: Cycle-accurate Nintendo NES emulator in ~1000

Post by Sour »

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: Select all

#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.
User avatar
Jarhmander
Formerly ~J-@D!~
Posts: 569
Joined: Sun Mar 12, 2006 12:36 am
Location: Rive nord de Montréal

Re: Cycle-accurate Nintendo NES emulator in ~1000

Post by Jarhmander »

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.
((λ (x) (x x)) (λ (x) (x x)))
User avatar
James
Posts: 431
Joined: Sat Jan 22, 2005 8:51 am
Location: Chicago, IL
Contact:

Re: Cycle-accurate Nintendo NES emulator in ~1000

Post by James »

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.
get nemulator
http://nemulator.com
Sour
Posts: 891
Joined: Sun Feb 07, 2016 6:16 pm

Re: Cycle-accurate Nintendo NES emulator in ~1000

Post by Sour »

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.
Post Reply