I'm experimenting with writing C code for the NES, for future projects. Not sure if I'll stick with it or not, but I wanted to try anyway. I was able to figure out how to customize the C runtime that comes with CC65, and got as far as integrating my own startup code. And yes I know a few folks have provided libraries for all this stuff but I wanted to see if I could do it, for fun.
The following function works just fine (please assume I'm calling it during vblank):
#define PPUW (unsigned char*) 0x2007
void ppu_load_palette(const unsigned char* palette)
{
int i;
*PPUA = 0x3f;
*PPUA = 0x00;
for(i = 0; i < 32; i++)
*PPUW = palette[i];
}
The result in FCEUX ppu viewer:
But at one point, I was (quite arbitrarily, just from having to get familiar with C again) assigning PPUW to a local variable, like this:
The result, in FCEUX ppu viewer:
Either I'm way rusty with C, or there's something else going on.
I was looking into using thefox's NintendulatorDX debugging stuff for this (to see what is actually being generated by the compiler...)---I thought I had seen that he could debug C using that---but the header files included seem to only be for CA65.
The following function works just fine (please assume I'm calling it during vblank):
Code:
#define PPUW (unsigned char*) 0x2007
void ppu_load_palette(const unsigned char* palette)
{
int i;
*PPUA = 0x3f;
*PPUA = 0x00;
for(i = 0; i < 32; i++)
*PPUW = palette[i];
}
The result in FCEUX ppu viewer:
But at one point, I was (quite arbitrarily, just from having to get familiar with C again) assigning PPUW to a local variable, like this:
Code:
#define PPUW (unsigned char*) 0x2007
void ppu_load_palette(const unsigned char* palette)
{
int i;
unsigned char* ppuw = PPUW;
while((*PPUS & 0x80) > 0);
*PPUA = 0x3f;
*PPUA = 0x00;
for(i = 0; i < 32; i++)
*ppuw = palette[i];
}
void ppu_load_palette(const unsigned char* palette)
{
int i;
unsigned char* ppuw = PPUW;
while((*PPUS & 0x80) > 0);
*PPUA = 0x3f;
*PPUA = 0x00;
for(i = 0; i < 32; i++)
*ppuw = palette[i];
}
The result, in FCEUX ppu viewer:
Either I'm way rusty with C, or there's something else going on.
I was looking into using thefox's NintendulatorDX debugging stuff for this (to see what is actually being generated by the compiler...)---I thought I had seen that he could debug C using that---but the header files included seem to only be for CA65.