Should I simplify my blog code?

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Should I simplify my blog code?

Post by tokumaru »

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...
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Should I simplify my blog code?

Post by tepples »

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?
User avatar
dougeff
Posts: 3079
Joined: Fri May 08, 2015 7:17 pm

Re: Should I simplify my blog code?

Post by dougeff »

Comparisons are easier to understand in C than assembly.

If (X > 50 && Y <= 80 + B)
nesdoug.com -- blog/tutorial on programming for the NES
adam_smasher
Posts: 271
Joined: Sun Mar 27, 2011 10:49 am
Location: Victoria, BC

Re: Should I simplify my blog code?

Post by adam_smasher »

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.
calima
Posts: 1745
Joined: Tue Oct 06, 2015 10:16 am

Re: Should I simplify my blog code?

Post by calima »

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.
User avatar
gauauu
Posts: 779
Joined: Sat Jan 09, 2016 9:21 pm
Location: Central Illinois, USA
Contact:

Re: Should I simplify my blog code?

Post by gauauu »

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)
User avatar
dougeff
Posts: 3079
Joined: Fri May 08, 2015 7:17 pm

Re: Should I simplify my blog code?

Post by dougeff »

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

Re: Should I simplify my blog code?

Post by rainwarrior »

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/
na_th_an
Posts: 558
Joined: Mon May 27, 2013 9:40 am

Re: Should I simplify my blog code?

Post by na_th_an »

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

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

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

		// 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.
calima
Posts: 1745
Joined: Tue Oct 06, 2015 10:16 am

Re: Should I simplify my blog code?

Post by calima »

Case fallthroughs are standard practice, common in places like the linux kernel. The comment is usually just // fallthrough.
User avatar
dougeff
Posts: 3079
Joined: Fri May 08, 2015 7:17 pm

Re: Should I simplify my blog code?

Post by dougeff »

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
User avatar
dougeff
Posts: 3079
Joined: Fri May 08, 2015 7:17 pm

Re: Should I simplify my blog code?

Post by dougeff »

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
User avatar
gauauu
Posts: 779
Joined: Sat Jan 09, 2016 9:21 pm
Location: Central Illinois, USA
Contact:

Re: Should I simplify my blog code?

Post by gauauu »

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.
User avatar
dougeff
Posts: 3079
Joined: Fri May 08, 2015 7:17 pm

Re: Should I simplify my blog code?

Post by dougeff »

Changed themes again. This one has a little more contrast.
nesdoug.com -- blog/tutorial on programming for the NES
na_th_an
Posts: 558
Joined: Mon May 27, 2013 9:40 am

Re: Should I simplify my blog code?

Post by na_th_an »

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