C/C++ NES programming

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

yassergsNESDEV
Posts: 3
Joined: Fri Apr 09, 2010 7:06 am

C/C++ NES programming

Post by yassergsNESDEV » Fri Apr 09, 2010 7:58 am

Ok, Hello Every Body. I am /new on this NES gaming world. I have seen a couple of assembler programming and it´s really troublesome, since rogramming differs from 8080/80286/80386/80486 micro assembler programming. For me is more useful if I make it on C/C++. So do you know some tutorials about C nes programming I already got the cc65 compiler. But how I start drawing picture, sprites point and Shapes? Know my pourpose is make nes or snes games for emulators for PC or portables, like Dingoo or GP32 << uff I´m a really long-tonged newbie, sorry for the boring chat>>

User avatar
Memblers
Site Admin
Posts: 3833
Joined: Mon Sep 20, 2004 6:04 am
Location: Indianapolis
Contact:

Post by Memblers » Fri Apr 09, 2010 8:35 am

Unfortunately the NES libraries included with cc65 don't work properly (unless they were fixed when I wasn't looking, I doubt it).

I think it was a guy going by the name Petruza who was working on making some C libraries for NES. It was a little while back, I'm not sure how that progressed.

I don't think there is any example program written in C for NES that actually will work on an NES.. as far as I know. There was a Tetris game written by Groepaz that was cross-platform (he wrote the old NES cc65 libraries), but it's probably not gonna work anymore on most emulators or the real system. Of course the compiler works just fine, if you don't mind writing your own libraries and stuff.

tepples
Posts: 21975
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: C/C++ NES programming

Post by tepples » Fri Apr 09, 2010 8:55 am

yassergsNESDEV wrote:For me is more useful if I make it on C/C++. So do you know some tutorials about C nes programming I already got the cc65 compiler. But how I start drawing picture, sprites point and Shapes? Know my pourpose is make nes or snes games for emulators for PC or portables
If you want to code for machines that can run emulators, and you want to code in C, you could always try cutting out the middleman: write your game in C using SDL or Allegro.

User avatar
thefox
Posts: 3141
Joined: Mon Jan 03, 2005 10:36 am
Location: Tampere, Finland
Contact:

Post by thefox » Fri Apr 09, 2010 11:57 am

I've been working on an couple of examples and a small library (replacement for the built-in CC65 one) for NES development with C.

Based on my experience I think it's definitely possible to write reasonably complex games in C, but there's a lot of low-level stuff one must be aware of, like where variables are allocated and what C code will produce the fastest assembly code (CC65 isn't particularly good with optimizing). Also you still need to know all the nitty gritty NES hardware details.. you won't find any blit() functions here.

The main example is a single direction scrolling SMB-like platformer engine with a simple music engine also written in C (just for fun). I think I'll add some basic enemy support and then it's ready to go.

User avatar
blargg
Posts: 3715
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Post by blargg » Fri Apr 09, 2010 1:38 pm

I looked at cc65's code efficiency and concluded that it would be too much of a limitation for writing a game. You'd have to limit what you did, or write lots in assembly. I don't even think it's reasonable on the SNES. They did it on many of the Game Boy Color games, but the CPU ran significantly faster, and the games all ran at a sucky 30 FPS (Bionic Command, I'm looking at you).

User avatar
thefox
Posts: 3141
Joined: Mon Jan 03, 2005 10:36 am
Location: Tampere, Finland
Contact:

Post by thefox » Fri Apr 09, 2010 1:50 pm

blargg wrote:You'd have to limit what you did, or write lots in assembly.
I'd think that goes without saying. It's never going to be as fast as assembly. Also the complexity in finding how to make the compiler emit reasonably fast code (and other quirks like that) kinda defeats the purpose of using C in the first case.

But I think a game like Battle Kid would most likely be possible to be written in C (it'd make sense to have completely separate modules like the music engine written in assembly however). Would it be any less work than writing it in assembly... not so sure. But it's fun for sure. :)

User avatar
Bregalad
Posts: 7880
Joined: Fri Nov 12, 2004 2:49 pm
Location: Chexbres, VD, Switzerland

Post by Bregalad » Fri Apr 09, 2010 3:08 pm

What is a bit annoying is that CC65 hardcore the ($xx),Y adressing mode whenever you acess to any local variable, where Y is the pointer in C variable stack.
A good thing would be to limit the stack length to 256 bytes and use the $xxx,Y adressing instead but there is no option to do this.

Yes the code will be slow, but you will greatly reduce the chances of having variable conflicts, something that happens all the time when you write in assembly and have no "local variables", but numbered Temp and other generic varaibles. Even if you clearly indicate which ones are used/erased on the header of each routine, it's still hard not to mess up.

I think it also depends on the kind of game you code. Usually in a standard game, a good part of CPU time is dedicated to write sprites to OAM. If this routine is written in assembly (it should), along with other routines which are called every frame, it is probably possible to have the core of the game written in C and have it run at fine speed.
Slow code is an issue only if the code is executed regularly.
Life is complex: it has both real and imaginary components.

User avatar
GradualGames
Posts: 1107
Joined: Sun Nov 09, 2008 9:18 pm
Location: Pennsylvania, USA
Contact:

Post by GradualGames » Fri Apr 09, 2010 7:57 pm

One thing that writing an NES game in C would lack is an easy way to step through your code as it runs. Unless there's a debugging emulator out there that can read debug output for C programs, you'd have to know 6502 anyway for debugging/stepping. I guess since C would be less error prone to bugs involving shared zp space (as per Bregalad's example) maybe this would be less of an issue, though. I'm so used to being able to step through code from working with C# and C++ professionally though I can't imagine working without it in my hobby.

On the subjective side of things, I know for me I just wouldn't enjoy coding for the NES in pure C. It'd take away the sensation of being 100% in control that makes asm so much fun.

@yassergsNESDEV: When you say troublesome and then mention the x86 processors, do you mean you're familiar with x86 assembly language and find 6502 to be alien by comparison...or? Because I know for me, knowing a little bit of x86 really helped out when I learned 6502. Most of the "hard" things about assembly language are pretty much the same from processor to processor.

Near
Founder of higan project
Posts: 1550
Joined: Mon Mar 27, 2006 5:23 pm

Post by Near » Fri Apr 09, 2010 9:56 pm

NES, no way. SNES, probably not. Targeting the SA-1 @ 10.74MHz could be doable.

C isn't really appropriate for a processor with only a single accumulator. An intermediate between assembler and C should work well. The point would be:
- function arguments
- automatic variable location handling
- less opcode mnemonics, more raw logic (inx; lda #$0100 -> x++; a = 256)

mic_
Posts: 922
Joined: Thu Oct 05, 2006 6:29 am

Post by mic_ » Sat Apr 10, 2010 3:11 am

There's an Arkanoid clone written in C that comes with the SNES_SDK (based on TCC). So it's doable for simple games on the SNES, but for more advanced stuff you'd have to write most of it in assembly.
And looking at the code generated by TCC is enough to turn my stomach, even after it's gone though the included optimizer. It's just really, really bad. Can't say anything about CC65's code quality, though thefox mentioned that it's not very good either.

User avatar
blargg
Posts: 3715
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Post by blargg » Sat Apr 10, 2010 3:25 am

The 65816 includes various stack-relative modes, very obviously for languages like C where you have plenty of local variables. The stack pointer is 16 bits, so you don't have to do the things cc65 does on the 6502.

User avatar
thefox
Posts: 3141
Joined: Mon Jan 03, 2005 10:36 am
Location: Tampere, Finland
Contact:

Post by thefox » Sat Apr 10, 2010 3:25 am

Bregalad wrote:What is a bit annoying is that CC65 hardcore the ($xx),Y adressing mode whenever you acess to any local variable, where Y is the pointer in C variable stack.
A good thing would be to limit the stack length to 256 bytes and use the $xxx,Y adressing instead but there is no option to do this.
Yeah it uses a software stack for local variables and function arguments so to get faster code one should use static local variables or global variables. There's a compiler switch (-Cl) that makes all local variables static which speeds up things a bit.

EDIT: Also, it treats all variables as "volatile", so even if a variable already exists in a register it will always read it again from memory. For example:

Code: Select all

byte a = 1, b, c;
b = a;
c = a;
Will read "a" twice from memory always. However, code like "b = c = a;" will read it only once.

ReaperSMS
Posts: 174
Joined: Sun Sep 19, 2004 11:07 pm
Contact:

Post by ReaperSMS » Sat Apr 10, 2010 11:05 am

That's not so much treating everything as volatile, as not doing any CSE whatsoever.

User avatar
Petruza
Posts: 311
Joined: Mon Dec 22, 2008 10:45 pm
Location: Argentina

Post by Petruza » Thu May 13, 2010 8:02 pm

Memblers wrote:Unfortunately the NES libraries included with cc65 don't work properly (unless they were fixed when I wasn't looking, I doubt it).

I think it was a guy going by the name Petruza who was working on making some C libraries for NES. It was a little while back, I'm not sure how that progressed.

I don't think there is any example program written in C for NES that actually will work on an NES.. as far as I know. There was a Tetris game written by Groepaz that was cross-platform (he wrote the old NES cc65 libraries), but it's probably not gonna work anymore on most emulators or the real system. Of course the compiler works just fine, if you don't mind writing your own libraries and stuff.
You're right, it didn't progress. Although I'd like to get back to it, I stoppped writing the lib in C, because I realized it would be better if it was coded in Assembler, but exported C functions to be used from C code, which was the original idea.

Actually, there are at least two example programs written in C that work on a NES (Emulator), or did you mean the actual Hardware?
Not sure about that, but it may.
It's a sprite and scrolling demo and a working metronome.

If the original poster is still interested, contact me by PM and I'll send you the sources.

3gengames
Formerly 65024U
Posts: 2275
Joined: Sat Mar 27, 2010 12:57 pm

Post by 3gengames » Fri May 14, 2010 10:23 pm

I'm not even gona lie, I think C is evil....especially when people debate what's better, C or assembly, which is too obvious ⌐.⌐ :P :roll:

Post Reply