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

Should I simplify my blog code?

Post by dougeff »

I recently saw someone elses (very small) example code of C for cc65 for NES... they did things in a way that the user, basically, doesn't need to understand anything under the hood. Hiding 2006/2007 writes in hidden function definitions, for example.

Personally, I like to understand everything that is going on, but I suppose a casual user, who just wants to make a simple game, might prefer to know nothing about the inner workings of the PPU.

This seems to me, to be a more modern approach. The library takes care of technical details, the programmer can focus on the game.

Thoughts?
nesdoug.com -- blog/tutorial on programming for the NES
Pokun
Posts: 2675
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Should I simplify my blog code?

Post by Pokun »

Personally I'm with you that I don't want to include anything I don't fully understand, so I think it's good if both types of tutorials are out there. I prefer to at least know how the code I'm using works, so no libraries, but I don't need to know every little hardware detail under the hood (although I try to learn that too to a degree).

However, although a tutorial that explains everything, doesn't need explain everything the moment they are introduced, as long as it tells the reader that it is something necessary that will be explained later.
For example the Nerdy Nights doesn't explain the init code, leaving the reader wondering why there's suddenly extra code in the source file that the tutorial didn't mention and even seems to contradict what it said.
slobu
Posts: 276
Joined: Tue Jul 12, 2011 10:58 am

Re: Should I simplify my blog code?

Post by slobu »

I stick to higher level languages because it allows me to focus on game logic and prototyping ideas easily.

I kinda failed to even get a basic program working in NESICIDE. So, hardware details are nice but a distant priority/motivator.
User avatar
dougeff
Posts: 3078
Joined: Fri May 08, 2015 7:17 pm

Re: Should I simplify my blog code?

Post by dougeff »

Thanks for the replies.

This was the example code I saw, that made me think I could have made it MUCH easier...

https://github.com/algofoogle/nes-gamed ... ello-nes.c

Especially lines like this...

Code: Select all

/* Write a byte to a given address: */
#define poke(addr, data)        (*((unsigned char*)addr) = data)

/* Write a pair of bytes to the PPU VRAM Address 2. */
#define ppu_2(a, b)             { poke(PPU_VRAM_ADDR2, a); poke(PPU_VRAM_ADDR2, b); }
I wouldn't use this 'poke' stuff, but it's interesting. I was thinking of something like set_ppu_address (x,y) where the definition somehow converts the x,y coordinates AT COMPILE TIME into writes to $2006.

The user would then only have to use set_ppu_address() and never worry about PPU addresses.

Or, perhaps, everything should be buffered, and written during V-blank. So people won't have to worry about timing things, just an automated system, that buffers all writes, and auto-writes it during NMI routine.

Hmm.

Edit, actually, the very next line of code above...

#define ppu_set_cursor(x, y) ppu_2(0x20+((y)>>3), ((y)<<5)+(x))

Would do what I said.
Last edited by dougeff on Tue Mar 28, 2017 5:26 pm, edited 1 time in total.
nesdoug.com -- blog/tutorial on programming for the NES
User avatar
dustmop
Posts: 136
Joined: Wed Oct 16, 2013 7:55 am

Re: Should I simplify my blog code?

Post by dustmop »

dougeff wrote:I wouldn't use this 'poke' stuff, but it's interesting. I was thinking of something like set_ppu_address (x,y) where the definition somehow converts the x,y coordinates AT COMPILE TIME into writes to $2006.
Every neslib I've seen has used NTADR to do this:

https://github.com/jmk/cc65-nes-example ... mple1.c#L9

I like to use PPU_DATA and PPU_ADDR in my C projects to simplify memory-mapped registers:

https://github.com/dustmop/annotatecc65 ... e/common.h

In general, macros are dangerous, but can be super useful for readability if used carefully.
User avatar
dougeff
Posts: 3078
Joined: Fri May 08, 2015 7:17 pm

Re: Should I simplify my blog code?

Post by dougeff »

It's cool that you can highlight specific lines on github with a #L

I'm learning lots today :D
nesdoug.com -- blog/tutorial on programming for the NES
User avatar
dustmop
Posts: 136
Joined: Wed Oct 16, 2013 7:55 am

Re: Should I simplify my blog code?

Post by dustmop »

If you click on the line-numbers on the left-side of the code display, it sends your browser to the line-highlighted link. No need to memorize the anchor or anything!
User avatar
dougeff
Posts: 3078
Joined: Fri May 08, 2015 7:17 pm

Re: Should I simplify my blog code?

Post by dougeff »

I was very reluctant to use Shiru's neslib in my example code. For one thing, I'm not sure I understand it 100% fully. Another thing. I'm pretty sure Shiru hates me. A lot.
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 »

Neslib is pretty straightforward, is well built, simple, barebones, yet it manages to conceal all the fiddly stuff. But it's there anyways and easily modifiable if you know what you are doing. So it was great for my beginnings and also great now that I have a proper understanding of the system. So easily tweakable. I like it.

I think it's great for beginners 'cause it solves all the fiddly stuff, and still great for intermediates and advanced coders, 'cause the fiddly stuff is easily modifiable. It was pretty straightforward to make it work in UNROM, MMC1 and MMC3, for example.
User avatar
dougeff
Posts: 3078
Joined: Fri May 08, 2015 7:17 pm

Re: Should I simplify my blog code?

Post by dougeff »

dustmop wrote:
dougeff wrote:I wouldn't use this 'poke' stuff, but it's interesting. I was thinking of something like set_ppu_address (x,y) where the definition somehow converts the x,y coordinates AT COMPILE TIME into writes to $2006.
Every neslib I've seen has used NTADR to do this:

https://github.com/jmk/cc65-nes-example ... mple1.c#L9

I like to use PPU_DATA and PPU_ADDR in my C projects to simplify memory-mapped registers:

https://github.com/dustmop/annotatecc65 ... e/common.h

In general, macros are dangerous, but can be super useful for readability if used carefully.
The 'cc65-nes-examples' github you linked to is an outdated version. The newer version is from 2015. It has more example code and a completely different neslib.s

Interestingly...the neslib.s from the 2015 examples is different from the example in Chase and different from other Shiru source code (Zooming secretary)

EDIT - I think the newest examples uses the same as the newest Chase source neslib.s, and I think that is what dustmop and na_th_an are using. Nevermind.


Na_th_an / dustmop, which version of neslib.s do you use? It seems that Chase is the most recent version.
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 »

My base version comes from this revised version repackaged for the "new" cc65 (current active branch) by lidnariq.

Calima maintains an enhaced version with some fixes.

My own UNROM version is here. I have created simple MMC1 and MMC3 versions but haven't released them yet.
cppchriscpp
Posts: 102
Joined: Fri Dec 27, 2013 4:28 pm

Re: Should I simplify my blog code?

Post by cppchriscpp »

I'm definitely a fan of having the little details in there. I like what I've seen of your tutorials, (disclaimer: I haven't read all that much) because of those nitty-gritty details. I may be biased due to already using 6502 assembly a lot, however.

At the same time, neslib is awesome, and makes working in C a bit more enjoyable. Neslib made it much easier to bootstrap some examples of my most recent project. It also made those examples more readable - at least as far as using my library goes, as I was able to abstract the system details away.

It seems to me like there's room on the internet and a use for both. I guess it's a question of which experience you want to provide - quickly bootstrapping a working NES game, or giving the user the tools to understand and tinker with everything going on in their game.

This is admittedly not a fully-formed thought - could you do some sort of a hybrid approach, where you use neslib, but also have info boxes that explain the macros/etc for anyone that's inclined to read them? Or maybe even just links to the source for where to see things?


Unrelated note, I didn't realize how many people already had neslib-based bootstrap repos. I'm partway through making an mmc1 example for myself. (Mainly want to use it for ludum dare in a couple weeks) I still probably will, but nice to see more examples coming out. It gives people options for getting started. For example, my typical setup involves using makefiles, a lot of integration with vs code, and automatic circleci builds/releases of every pushed commit. (I only recently figured that last one out; check out my nesnet project if you want an example!) I can see people wanting something simpler or just different.
User avatar
dougeff
Posts: 3078
Joined: Fri May 08, 2015 7:17 pm

Re: Should I simplify my blog code?

Post by dougeff »

This gentleman has the newer example code (Shiru) on his github

https://github.com/peterhellberg/nesdev ... s_examples

Also found source code for SplatooD, which uses neslib / cc65.

https://github.com/SplatooD/splatood

hybrid approach, where you use neslib
I might try to use a modified (expanded) version of neslib. Now that I've looked into it, I do think I understand it 99%. Some of the functions could be changed a bit. I'm not a fan of the current controller read functions, for example.
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 »

As for the main topic, I appreciate that you get low in the mud in the tutorials from your site. They have been quite helpful for me, specially undestanding how scrolling works in a lower level (neslib hides this) or understanding MMC3's IRQs.
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:This gentleman has the newer example code (Shiru) on his github

https://github.com/peterhellberg/nesdev ... s_examples

Also found source code for SplatooD, which uses neslib / cc65.

https://github.com/SplatooD/splatood
Just promise us that you won't put global data in .h files like these examples do :shock:
Post Reply