Constant value for both, C and Assembly

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

Moderator: Moderators

bootmii
Posts: 12
Joined: Sat Jul 25, 2015 11:53 am

Re: Constant value for eats, Shoots and Leaves

Post by bootmii »

DRW wrote:What do I have to do if I want to use the same constant value in both, my C and Assembly code? (I'm using of course CC65.)

In Assembly, I would write this:

Code: Select all

GAMESTATE_TITLE = $01
GAMESTATE_RUNNING = $02
And in C, I would write this:

Code: Select all

#define GAMESTATE_TITLE 0x01
#define GAMESTATE_RUNNING 0x02
But how can I make these values known in both code files without declaring them twice?

O.k., I could declare a constant in C:

Code: Select all

const unsigned char GAMESTATE_TITLE = 0x01;
const unsigned char GAMESTATE_RUNNING = 0x02;
This then translates to an actual memory address which can be used in Assembly:

Code: Select all

_GAMESTATE_TITLE:
  .byte $01
_GAMESTATE_RUNNING:
  .byte $02
But that's the problem: A const would occupy actual space in the ROM. And this is what I don't want since my constants are just supposed to be possible values for a variable.
Sure, I want them to be declared in one location in the source code, so in case the value changes, I only have to change it in one location.
But in the actual compiled binary, all comparisons shall of course work directly with the numeric value (i.e. the value appears more than once in the ROM), not with a value at an address in the ROM.

Is there a possibility to do this?
lol comma
Post Reply