It is currently Fri Sep 20, 2019 3:16 pm

All times are UTC - 7 hours





Post new topic Reply to topic  [ 27 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: C/C++ NES programming
PostPosted: Fri Apr 09, 2010 7:58 am 
Offline

Joined: Fri Apr 09, 2010 7:06 am
Posts: 3
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>>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 09, 2010 8:35 am 
Offline
Site Admin
User avatar

Joined: Mon Sep 20, 2004 6:04 am
Posts: 3726
Location: Indianapolis
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.


Top
 Profile  
 
PostPosted: Fri Apr 09, 2010 8:55 am 
Offline

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 09, 2010 11:57 am 
Offline
User avatar

Joined: Mon Jan 03, 2005 10:36 am
Posts: 3141
Location: Tampere, Finland
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.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 09, 2010 1:38 pm 
Offline
User avatar

Joined: Mon Sep 27, 2004 8:33 am
Posts: 3715
Location: Central Texas, USA
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).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 09, 2010 1:50 pm 
Offline
User avatar

Joined: Mon Jan 03, 2005 10:36 am
Posts: 3141
Location: Tampere, Finland
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. :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 09, 2010 3:08 pm 
Offline
User avatar

Joined: Fri Nov 12, 2004 2:49 pm
Posts: 7741
Location: Chexbres, VD, Switzerland
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.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 09, 2010 7:57 pm 
Offline
User avatar

Joined: Sun Nov 09, 2008 9:18 pm
Posts: 1107
Location: Pennsylvania, USA
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.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 09, 2010 9:56 pm 
Offline

Joined: Mon Mar 27, 2006 5:23 pm
Posts: 1524
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)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 10, 2010 3:11 am 
Offline

Joined: Thu Oct 05, 2006 6:29 am
Posts: 917
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.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 10, 2010 3:25 am 
Offline
User avatar

Joined: Mon Sep 27, 2004 8:33 am
Posts: 3715
Location: Central Texas, USA
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.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 10, 2010 3:25 am 
Offline
User avatar

Joined: Mon Jan 03, 2005 10:36 am
Posts: 3141
Location: Tampere, Finland
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:
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.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 10, 2010 11:05 am 
Offline

Joined: Sun Sep 19, 2004 11:07 pm
Posts: 172
That's not so much treating everything as volatile, as not doing any CSE whatsoever.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 13, 2010 8:02 pm 
Offline
User avatar

Joined: Mon Dec 22, 2008 10:45 pm
Posts: 311
Location: Argentina
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.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2010 10:23 pm 
Offline
Formerly 65024U

Joined: Sat Mar 27, 2010 12:57 pm
Posts: 2269
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:


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

All times are UTC - 7 hours


Who is online

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