nesdev.com
http://forums.nesdev.com/

Should I simplify my blog code?
http://forums.nesdev.com/viewtopic.php?f=2&t=15709
Page 3 of 4

Author:  tokumaru [ Mon Apr 10, 2017 9:02 am ]
Post subject:  Re: Should I simplify my blog code?

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

Author:  tepples [ Mon Apr 10, 2017 9:11 am ]
Post subject:  Re: Should I simplify my blog code?

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?

Author:  dougeff [ Mon Apr 10, 2017 10:50 am ]
Post subject:  Re: Should I simplify my blog code?

Comparisons are easier to understand in C than assembly.

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

Author:  adam_smasher [ Mon Apr 10, 2017 11:09 am ]
Post subject:  Re: Should I simplify my blog code?

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.

Author:  calima [ Mon Apr 10, 2017 11:24 am ]
Post subject:  Re: Should I simplify my blog code?

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.

Author:  gauauu [ Mon Apr 10, 2017 11:49 am ]
Post subject:  Re: Should I simplify my blog code?

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)

Author:  dougeff [ Mon Apr 10, 2017 12:07 pm ]
Post subject:  Re: Should I simplify my blog code?

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?

Author:  rainwarrior [ Mon Apr 10, 2017 12:42 pm ]
Post subject:  Re: Should I simplify my blog code?

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/

Author:  na_th_an [ Tue Apr 11, 2017 1:15 am ]
Post subject:  Re: Should I simplify my blog code?

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.

Author:  calima [ Tue Apr 11, 2017 3:08 am ]
Post subject:  Re: Should I simplify my blog code?

Case fallthroughs are standard practice, common in places like the linux kernel. The comment is usually just // fallthrough.

Author:  dougeff [ Wed Apr 12, 2017 6:36 pm ]
Post subject:  Re: Should I simplify my blog code?

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.

Author:  dougeff [ Thu Apr 13, 2017 7:42 am ]
Post subject:  Re: Should I simplify my blog code?

Also, I changed the 'theme' to a darker background, because the white background kills my eyes at night.

Author:  gauauu [ Thu Apr 13, 2017 9:25 am ]
Post subject:  Re: Should I simplify my blog code?

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.

Author:  dougeff [ Fri Apr 14, 2017 4:00 pm ]
Post subject:  Re: Should I simplify my blog code?

Changed themes again. This one has a little more contrast.

Author:  na_th_an [ Tue Apr 18, 2017 12:50 pm ]
Post subject:  Re: Should I simplify my blog code?

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.

Page 3 of 4 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/