It is currently Mon Oct 23, 2017 5:38 pm

All times are UTC - 7 hours





Post new topic Reply to topic  [ 46 posts ]  Go to page Previous  1, 2, 3, 4  Next
Author Message
PostPosted: Mon Apr 10, 2017 9:02 am 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10068
Location: Rio de Janeiro - Brazil
na_th_an wrote:
once I started coding for 8 bits platforms in C, I had to forget about readability, proper use of semantics, good practice and readability if I wanted to get proper results.

If you get to the point where tou have to sacrifice even readability, wouldn't it be better to just write well structured assembly code?

I never wrote C code for 8-bit CPUs, but from reading about it here in the forums it sounds like many of the benefits of using a high level language have to be thrown out the window if you want to get passable results, so I end up wondering if it wouldn't be better to not sacrifice anything and write proper ASM code instead...


Top
 Profile  
 
PostPosted: Mon Apr 10, 2017 9:11 am 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19122
Location: NE Indiana, USA (NTSC)
tokumaru wrote:
na_th_an wrote:
once I started coding for 8 bits platforms in C, I had to forget about readability, proper use of semantics, good practice and readability if I wanted to get proper results.

If you get to the point where tou have to sacrifice even readability, wouldn't it be better to just write well structured assembly code?

Not if you want the same game logic code in the NES and PC versions of the same game. Or would it be better to statically transpile assembly language to C?


Top
 Profile  
 
PostPosted: Mon Apr 10, 2017 10:50 am 
Offline
User avatar

Joined: Fri May 08, 2015 7:17 pm
Posts: 1787
Location: DIGDUG
Comparisons are easier to understand in C than assembly.

If (X > 50 && Y <= 80 + B)

_________________
nesdoug.com -- blog/tutorial on programming for the NES


Top
 Profile  
 
PostPosted: Mon Apr 10, 2017 11:09 am 
Offline

Joined: Sun Mar 27, 2011 10:49 am
Posts: 192
tepples wrote:
tokumaru wrote:
na_th_an wrote:
once I started coding for 8 bits platforms in C, I had to forget about readability, proper use of semantics, good practice and readability if I wanted to get proper results.

If you get to the point where tou have to sacrifice even readability, wouldn't it be better to just write well structured assembly code?

Not if you want the same game logic code in the NES and PC versions of the same game.


If the "NES-appropriate C" is nasty enough, I think (might?) prefer having a readable and idiomatic "reference" C implementation on the PC, regardless of whether the NES version is written in unreadable C or unreadable 6502.


Top
 Profile  
 
PostPosted: Mon Apr 10, 2017 11:24 am 
Offline

Joined: Tue Oct 06, 2015 10:16 am
Posts: 558
I don't find using global variables and NES-specific pragmas to impede readability much. Writing C is still 10-100x faster than writing the same thing in asm.


Top
 Profile  
 
PostPosted: Mon Apr 10, 2017 11:49 am 
Offline
User avatar

Joined: Sat Jan 09, 2016 9:21 pm
Posts: 214
Location: Central Illinois, USA
dougeff wrote:
Comparisons are easier to understand in C than assembly.

If (X > 50 && Y <= 80 + B)


I came here to say this, but Doug beat me to it. You can read and write this kind of thing it A LOT faster in C. There's quite a few bits of my games that were just as easy in assembly, but big sections of comparisons mixed with simple math were really tedious and error-prone.

That said, it would be nice to be able to "jump up" into C syntax from assembly for a few of these expressions. I see lots of information about dropping to assembly from a C program, but I think the reverse would be just as useful, even though it's not often discussed. (although I can think of quite a few hurdles that would make it complicated)

_________________
My games: http://www.bitethechili.com


Top
 Profile  
 
PostPosted: Mon Apr 10, 2017 12:07 pm 
Offline
User avatar

Joined: Fri May 08, 2015 7:17 pm
Posts: 1787
Location: DIGDUG
Quote:
be able to "jump up" into C syntax from assembly


I bet you could write a macro in ca65 to automate comparisons. Right tepples?

_________________
nesdoug.com -- blog/tutorial on programming for the NES


Top
 Profile  
 
PostPosted: Mon Apr 10, 2017 12:42 pm 
Offline
User avatar

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 5736
Location: Canada
dougeff wrote:
I bet you could write a macro in ca65 to automate comparisons. Right tepples?

I think you mean "right Movax12", who spent a few months working on a "high level" macro pack for ca65 a while back:
https://forums.nesdev.com/viewtopic.php?f=2&t=9272

As a proof of concept, it was then applied as a "high level disassembly" of Super Mario Bros:
http://www.romhacking.net/documents/635/


Top
 Profile  
 
PostPosted: Tue Apr 11, 2017 1:15 am 
Offline

Joined: Mon May 27, 2013 9:40 am
Posts: 351
tokumaru wrote:
na_th_an wrote:
once I started coding for 8 bits platforms in C, I had to forget about readability, proper use of semantics, good practice and readability if I wanted to get proper results.

If you get to the point where tou have to sacrifice even readability, wouldn't it be better to just write well structured assembly code?


I sacrifice *some* readability. To my untrained eye (although I understand and can produce assembly code), even unreadable C code is easier to grasp, follow and understand. What I mean with unreadable code, of my own, could be something like where I have to add a comment to help me remember this si not a typo, or strange reordering of cases in a switch so I can share code and save bytes, using a less intuitive approach to an expression 'cause I know cc65 will generate better code, etc. And, as mentioned, the resulting mess is still easy to port to different systems.

Code:
    case 3:
        // Gravity
        envy = enems_lut_falling [enmy [enit]];
        if (enmy [enit] < ENEMS_LUT_FALLING_MAXIDX) enmy [enit] ++;
        // Correct! no break here. Move left/right
    case 2:
    case 7:
        if (enx1 [enit] || half_life ) envx = enmx [enit];
        break;
        ...


Code:
if (en_ac [gpit] == 8) {
    // Killed baddie
    if (0 == --en_x2 [gpit]) en_ac [gpit] = 0;
    oam_meta_spr (en_x [gpit] >> FIXBITS, (en_y [gpit] >> FIXBITS) - camera_position, gpit << 4, spr_explosion);
} else if (gpjt = en_ac [gpit]) switch (gpjt) {
    // Yeah, this is an assignment. Don't panic
    case 1:
        if (rand8 () < SHOOT_TE...


They joy of cc65 is that when it doesn't behave you can always inline a better solution, as in:

Code:
      // cc65 could do better, so:
      
      /*
      rda = c_enems_t [enidx];
      rdb = c_enems_yx1 [enidx];
      rdc = c_enems_yx2 [enidx];
      rdd = c_enems_mn [enidx];
      */

      __asm__ ("ldy %v", enidx);
      __asm__ ("lda (%v), y", c_enems_t);
      __asm__ ("sta %v", rda);
      __asm__ ("lda (%v), y", c_enems_yx1);
      __asm__ ("sta %v", rdb);
      __asm__ ("lda (%v), y", c_enems_yx2);
      __asm__ ("sta %v", rdc);
      __asm__ ("lda (%v), y", c_enems_mn);
      __asm__ ("sta %v", rdd);


The code produced for the commented section was five times as long and fiddly, as it did recalculate the offset for each access when you'd rather use indirect addressing sharing the smae index register as enidx is unsigned char.

_________________
http://www.mojontwins.com


Top
 Profile  
 
PostPosted: Tue Apr 11, 2017 3:08 am 
Offline

Joined: Tue Oct 06, 2015 10:16 am
Posts: 558
Case fallthroughs are standard practice, common in places like the linux kernel. The comment is usually just // fallthrough.


Top
 Profile  
 
PostPosted: Wed Apr 12, 2017 6:36 pm 
Offline
User avatar

Joined: Fri May 08, 2015 7:17 pm
Posts: 1787
Location: DIGDUG
This is not exactly what I had in mind, but I just ended up using a slightly modified neslib code.

Here are my detailed neslib notes.

https://nesdoug.com/2017/04/13/my-neslib-notes/

And 2 simple example source codes.

https://nesdoug.com/2017/04/13/neslib-example-code/

Let me know if this is easy to follow. Or, if anything needs more explanation. Thanks.

_________________
nesdoug.com -- blog/tutorial on programming for the NES


Top
 Profile  
 
PostPosted: Thu Apr 13, 2017 7:42 am 
Offline
User avatar

Joined: Fri May 08, 2015 7:17 pm
Posts: 1787
Location: DIGDUG
Also, I changed the 'theme' to a darker background, because the white background kills my eyes at night.

_________________
nesdoug.com -- blog/tutorial on programming for the NES


Top
 Profile  
 
PostPosted: Thu Apr 13, 2017 9:25 am 
Offline
User avatar

Joined: Sat Jan 09, 2016 9:21 pm
Posts: 214
Location: Central Illinois, USA
The dark mostly looks nice, although the contrast is a little low on my monitor. Slightly darker background or slightly brighter text would be a little easier.

_________________
My games: http://www.bitethechili.com


Top
 Profile  
 
PostPosted: Fri Apr 14, 2017 4:00 pm 
Offline
User avatar

Joined: Fri May 08, 2015 7:17 pm
Posts: 1787
Location: DIGDUG
Changed themes again. This one has a little more contrast.

_________________
nesdoug.com -- blog/tutorial on programming for the NES


Top
 Profile  
 
PostPosted: Tue Apr 18, 2017 12:50 pm 
Offline

Joined: Mon May 27, 2013 9:40 am
Posts: 351
dougeff wrote:
Let me know if this is easy to follow. Or, if anything needs more explanation. Thanks.


I don't know if I'm the right one to judge this, as I know neslib in and out, but I think it's easy to follow and I will be linking your articles when I want to introduce the library to other people.

EDIT: about the ppu_wait_frame funcion, I don't think it's buggy, just not suited for games with splits as it will skip every 6th frame in NTSC. It works great in other situations.

_________________
http://www.mojontwins.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 46 posts ]  Go to page Previous  1, 2, 3, 4  Next

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 10 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