KNES library for CC65 (aka Programming the NES in C)

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

Moderator: Moderators

kbessa
Posts: 2
Joined: Sun Jul 24, 2011 7:05 am

Post by kbessa »

thefox wrote:Posted version 0.1.1 in the website (see first post) to fix problems which were discussed in this thread. In other words it now works with the latest dev version of CC65...
Hi thefox,

I've tried with the latest version (snapshot from 2011-07-18), but cannot get it to work, I always get this:


Code: Select all

C:\Nes\knes-0.1.1\knes>make
cl65 -v -g -t none -c crt0.s
cl65 -t none -g -T -Cl -Oirs -c knes.c
cl65 -v -g -t none -c _read_joy.s
copy original/nes.lib knes.lib
original\nes.lib
        1 arquivo(s) copiado(s).
ar65 d knes.lib _scrsize.o cclear.o chline.o clock.o clrscr.o color.o cputc.o cr
t0.o cvline.o get_tv.o gotox.o gotoxy.o gotoy.o mainargs.o ppu.o ppubuf.o random
ize.o revers.o setcursor.o sysuname.o waitvblank.o wherex.o wherey.o
ar65.exe: Error: Read error (file corrupt?)
mingw32-make: *** [knes.lib] Error 255
Any ideas?
User avatar
thefox
Posts: 3139
Joined: Mon Jan 03, 2005 10:36 am
Location: Tampere, Finland
Contact:

Post by thefox »

kbessa wrote:I've tried with the latest version (snapshot from 2011-07-18), but cannot get it to work, I always get this:
I think the object file version has been changed again, try replacing "original/nes.lib" with nes.lib from this package: ftp://ftp.musoftware.de/pub/uz/cc65/sna ... 0718-1.zip

Then delete knes.lib and all .o files and run make.

EDIT: Actually there's also other problem. You changed "cp" to "copy" in the Makefile, so you need to change the forward slash to backward slash or it won't work properly. Change to: copy original\nes.lib knes.lib
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
kbessa
Posts: 2
Joined: Sun Jul 24, 2011 7:05 am

Post by kbessa »

thefox wrote: I think the object file version has been changed again, try replacing "original/nes.lib" with nes.lib from this package: ftp://ftp.musoftware.de/pub/uz/cc65/sna ... 0718-1.zip

Then delete knes.lib and all .o files and run make.

EDIT: Actually there's also other problem. You changed "cp" to "copy" in the Makefile, so you need to change the forward slash to backward slash or it won't work properly. Change to: copy original\nes.lib knes.lib
Yep, I already had replaced the nes.lib, and had to change cp to copy as it was not being recognized (I'm compiling on Windows).

But yeah, changing to a backward slash solved it, was able to make knes.lib and also to rebuild the demo project, it loaded fine in FCEUX 2.1.5. Thanks!
User avatar
qbradq
Posts: 952
Joined: Wed Oct 15, 2008 11:50 am

Post by qbradq »

Could we put together some sort of optimization guide for your C code? For instance, I've noticed that this code:

Code: Select all

someStruct.x[idx] = someStruct.y[idx];
results in a subroutine call with a *lot* of pointer math, whereas this code:

Code: Select all

temp = someStruct.y[idx];
someStruct.x[idx] = temp;
does not.

What other best practices do you all know of?
User avatar
thefox
Posts: 3139
Joined: Mon Jan 03, 2005 10:36 am
Location: Tampere, Finland
Contact:

Post by thefox »

qbradq wrote:Could we put together some sort of optimization guide for your C code? For instance, I've noticed that this code: ... results in a subroutine call with a *lot* of pointer math, whereas this code: ... does not.
Does this happen with or without optimization switches (or both)?

My basic set of tips is listed on my homepage (http://kkfos.aspekt.fi/projects/nes/kne ... -for-cc65/):

1) -Cl switch to make local variables static
2) #pragma bss-name/data-name to set the bss-/data-segment to zeropage
3) __fastcall__ calling convention for faster parameter passing
4) -Oirs
5) Avoid interleaved data
User avatar
qbradq
Posts: 952
Joined: Wed Oct 15, 2008 11:50 am

Post by qbradq »

I found your tips very informative. I was very happy to see the "force static locals" flag.

This is using the same compiler flags as your demo make file. Here's the code I used:

Code: Select all

struct
{
  int x[16];
  int y[16];
  byte flags[16];
} objects;

int main(void)
{
  static byte a;
  
  for(a = 0; a < 16; ++a)
  {
    objects.x[a] = objects.y[a];
  }
  return 0;
}
User avatar
qbradq
Posts: 952
Joined: Wed Oct 15, 2008 11:50 am

Post by qbradq »

Could someone give me an example of implementing a function in pure assembly with accesses to arrays and structs defined in C?
Shiru
Posts: 1161
Joined: Sat Jan 23, 2010 11:41 pm

Post by Shiru »

Arrays are available as pointers (16-bit absolute addresses), structs - I don't know, haven't used them yet.

Simplest form of access to an array:

Code: Select all

;void __fastcall__ func(const unsigned char *array);

_func:
 ;A:X is the pointer to array (LSB,MSB)
 rts
User avatar
qbradq
Posts: 952
Joined: Wed Oct 15, 2008 11:50 am

Post by qbradq »

Great! That helps a lot Shiru, thanks! So the name of the label needs to be preceded by an underscore for C to be able to see it? And I don't need to back anything up (like registers)?

I'll play around with this a bit when I get to sprite mazing. I might not even need to do this.

I'll download your source code for Alter Ego and see what I can learn from it.

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

Post by thefox »

There's some info on the CC65 wiki: http://wiki.cc65.org/doku.php?id=cc65:p ... onventions
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
User avatar
thefox
Posts: 3139
Joined: Mon Jan 03, 2005 10:36 am
Location: Tampere, Finland
Contact:

Post by thefox »

New version: http://kkfos.aspekt.fi/downloads/knes-0.2.zip

- Fixed to work with latest version of CC65, also included Win32 build of that specific version in the package
- Converted library subroutines to 6502 assembly
- OAM, nametables, CHR-RAM, etc are cleared in the init routine
- Updated demo project etc to use "THE" Makefile
- Added a demo of using MUSE (a music/sound library) from a C project
- Added an (almost) empty project template example
- Other small changes
Post Reply