It is currently Mon Oct 16, 2017 5:46 pm

All times are UTC - 7 hours





Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Sun Nov 27, 2016 10:37 pm 
Offline
User avatar

Joined: Mon Dec 29, 2014 1:46 pm
Posts: 709
Location: New York, NY
This link recently appeared on Reddit (https://www.reddit.com/r/programming/co ... 000_lines/):

https://github.com/AndreaOrru/LaiNES


Top
 Profile  
 
PostPosted: Tue Nov 29, 2016 6:19 pm 
Offline

Joined: Sun Feb 07, 2016 6:16 pm
Posts: 285
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!


Top
 Profile  
 
PostPosted: Tue Nov 29, 2016 8:41 pm 
Offline
User avatar

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 5706
Location: Canada
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.


Top
 Profile  
 
PostPosted: Tue Nov 29, 2016 9:50 pm 
Offline
User avatar

Joined: Sun Sep 19, 2004 9:28 pm
Posts: 3192
Location: Mountain View, CA, USA
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.


Top
 Profile  
 
PostPosted: Wed Nov 30, 2016 12:10 am 
Offline
User avatar

Joined: Fri Nov 12, 2004 2:49 pm
Posts: 7223
Location: Chexbres, VD, Switzerland
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.


Top
 Profile  
 
PostPosted: Wed Nov 30, 2016 5:00 am 
Offline

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


Top
 Profile  
 
PostPosted: Wed Nov 30, 2016 7:15 am 
Offline
Formerly ~J-@D!~
User avatar

Joined: Sun Mar 12, 2006 12:36 am
Posts: 445
Location: Rive nord de Montréal
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.


Top
 Profile  
 
PostPosted: Wed Nov 30, 2016 8:00 am 
Offline
User avatar

Joined: Sat Jan 22, 2005 8:51 am
Posts: 427
Location: Chicago, IL
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


Top
 Profile  
 
PostPosted: Wed Nov 30, 2016 3:22 pm 
Offline

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group