This link recently appeared on Reddit (https://www.reddit.com/r/programming/co ... 000_lines/):
https://github.com/AndreaOrru/LaiNES
Cycle-accurate Nintendo NES emulator in ~1000
Moderator: Moderators
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!
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!
- rainwarrior
- Posts: 8735
- Joined: Sun Jan 22, 2012 12:03 pm
- Location: Canada
- Contact:
Re: Cycle-accurate Nintendo NES emulator in ~1000
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: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!
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.
Re: Cycle-accurate Nintendo NES emulator in ~1000
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).rainwarrior wrote: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: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!
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.
Know thy language and all that.
Re: Cycle-accurate Nintendo NES emulator in ~1000
With #defines you can do pretty much anything this style. I do not know whether that's what he did or not, though.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!
Re: Cycle-accurate Nintendo NES emulator in ~1000
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.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.
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) ); }
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.
- 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
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.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.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!
((λ (x) (x x)) (λ (x) (x x)))
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.
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.Sour wrote:Bisqwit also made a ~1k line emulator in his youtube video a while ago - this looks like a pretty similar concept.
get nemulator
http://nemulator.com
http://nemulator.com
Re: Cycle-accurate Nintendo NES emulator in ~1000
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.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.
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.