It is currently Sat Nov 17, 2018 6:17 pm

All times are UTC - 7 hours





Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Sat Oct 31, 2015 5:16 pm 
Offline
User avatar

Joined: Sun Nov 09, 2008 9:18 pm
Posts: 1107
Location: Pennsylvania, USA
I want to conveniently point to page $0200 of RAM using a C structure for manipulating sprites. In my nes.cfg file, I've explicitly created a segment for this page. In my sprite.s file I have

Code:
.segment "SPRITE"
_sprite_ram: .res 256


Assume I have an include file for exporting the symbol.

In my sprite.h file I have:

Code:
typedef struct {
    char y;
    char tile;
    char attribute;
    char x;
} sprite;

#define SPRITE_RAM ((sprite*) 0x0200)


However, I would much prefer it look like the following. Does CC65 offer a similar pragma to zpsym for saying a symbol exists in a specific segment? I have been reading the docs, but haven't spotted anything. I've only seen additional pragmas for changing what segment code is written to, but not for symbols in ram.
Code:
extern sprite* sprite_ram;
#pragma symseg ("sprite_ram", "SPRITE")  //I made this up, is there anything like this?


I searched for previous threads on this topic---only found a post by thefox concerning the understood usage of zpsym for using a zp variable in C code.


Top
 Profile  
 
PostPosted: Sat Oct 31, 2015 5:26 pm 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 20777
Location: NE Indiana, USA (NTSC)
Try this:
Code:
extern sprite sprite_ram[64];

If accessing elements of sprite_ram in this way doesn't produce the result you expect, I'd like to see the assembly language that cc65 generates.


Top
 Profile  
 
PostPosted: Sat Oct 31, 2015 5:31 pm 
Offline
User avatar

Joined: Mon Jan 03, 2005 10:36 am
Posts: 3137
Location: Tampere, Finland
What tepples said should work. Another option is to define the array in C code. You can place it in the correct segment with #pragma bss-name, or #pragma data-name if you need to initialize its values.

(NOTE: The compiler is not interested in what segments imported symbols come from, so there is no #pragma for specifying that.)

_________________
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi


Top
 Profile  
 
PostPosted: Sun Nov 01, 2015 7:26 am 
Offline
User avatar

Joined: Sun Nov 09, 2008 9:18 pm
Posts: 1107
Location: Pennsylvania, USA
I tried this:

Code:
typedef struct {
    char y;
    char tile;
    char attribute;
    char x;
} sprite;

extern sprite[64] sprite_ram;  //line 11

//#define SPRITE_RAM ((sprite*) 0x0200)


And the compiler output now says this:

Code:
inc/sprite.h(11): Error: Identifier expected


I'm able to get the above to compile with just extern sprite* sprite_ram; but then something else must be wrong as the same code which currently works using my previous define now does not (no sprite on screen).


Top
 Profile  
 
PostPosted: Sun Nov 01, 2015 7:57 am 
Offline
User avatar

Joined: Sun Nov 09, 2008 9:18 pm
Posts: 1107
Location: Pennsylvania, USA
Maybe I'm just really rusty at C. With absolutely everything else commented out except a successfully compiling empty main function, then adding the following:

Code:
typedef struct {
    int x;
} my_struct;

my_struct[1] an_array;


I get:
Code:
src\gglib.c(23): Error: Identifier expected


Top
 Profile  
 
PostPosted: Sun Nov 01, 2015 8:18 am 
Offline

Joined: Mon Apr 01, 2013 11:17 pm
Posts: 437
In C, you have to define your arrays like this:
Code:
extern sprite sprite_ram[64];

Or this:
Code:
my_struct an_array[1];


Top
 Profile  
 
PostPosted: Sun Nov 01, 2015 8:22 am 
Offline
User avatar

Joined: Sun Nov 09, 2008 9:18 pm
Posts: 1107
Location: Pennsylvania, USA
Good lord, I think I'm dyslexic. :oops: Thanks. Probably due to spending a disproportionate amount of time with Java over the past 5 years.

*edit* Yup, tepple's original suggestion worked once I realized my brain was putting the [64] in a different place than where it actually was on the screen.


Top
 Profile  
 
PostPosted: Sun Nov 01, 2015 8:27 am 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 20777
Location: NE Indiana, USA (NTSC)
GradualGames wrote:
I tried this:
Code:
extern sprite[64] sprite_ram;  //line 11


And the compiler output now says this:
Code:
inc/sprite.h(11): Error: Identifier expected


This is your problem. Java allows the number of elements to be placed before or after the array's name. C allows it only after. You have it before. Try the exact syntax I mentioned before:
Code:
extern sprite sprite_ram[64];


Top
 Profile  
 
PostPosted: Sun Nov 01, 2015 8:29 am 
Offline
User avatar

Joined: Sun Nov 09, 2008 9:18 pm
Posts: 1107
Location: Pennsylvania, USA
Yeah. That did it. Thanks tepples and joe! Kinda embarassing on my part, but I appreciate it. :)


Top
 Profile  
 
PostPosted: Sun Nov 01, 2015 6:12 pm 
Offline
User avatar

Joined: Mon Jan 03, 2005 10:36 am
Posts: 3137
Location: Tampere, Finland
GradualGames wrote:
I'm able to get the above to compile with just extern sprite* sprite_ram; but then something else must be wrong as the same code which currently works using my previous define now does not (no sprite on screen).

Yeah extern sprite* sprite_ram and extern sprite sprite_ram[] are not equivalent, even though C can often make it seem like it. In the first case the pointer (2 bytes) would be literally stored in memory. In the second case the sprite struct contents are stored in memory (but the array can implicitly be casted into a pointer if you do sprite *foo = sprite_ram;).

_________________
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 7 hours


Who is online

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