Need help with SPC2ROM

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
Need help with SPC2ROM
by on (#119816)
SPC2ROM Source: http://alpha-ii.com/Source/SPC2R11s.rar

I am using SPC2ROM to test various SPC700 instructions on my SNES console. However, I've run into an issue when trying to test the BRK/TCALL instructions. In my SPC700 source file I am defining some vector addresses for the various BRK/TCALL vectors from $FFC0-$FFDF. However, when I run spc2rom on my .spc file and I look at the resulting .sfc my vectors are nowhere to be found. In fact, anything at all that I put in the region from $FFC0-$FFFF is replaced with all-zeros in the final .sfc rom file that is generated.

Being that the region in question is the IPL ROM area I did some investigation into the SPC2ROM source code and here is what I found...

In the file SPC2ROM.cpp a structure is defined called APURAM which is exactly 64Kbytes in size:
Code:
struct APURAM
{
   u8   dp0[0xF0];                        //Direct Page 0
   u8   testReg;                        //Test register (no use)
   u8   control;                        //Control register
   u8   dspAddr;                        //DSP Address
   u8   dspData;                        //DSP Data
   u8   port0;                           //In port 0
   u8   port1;                           //In port 1
   u8   port2;                           //In port 2
   u8   port3;                           //In port 3
   u8   unused1;                        //unused
   u8   unused2;                        //unused
   u8   t0;                              //Timer 0
   u8   t1;                              //Timer 1
   u8   t2;                              //Timer 2
   u8   c0;                              //Counter 0
   u8   c1;                              //Counter 1
   u8   c2;                              //Counter 2

   u8   dp1[0x100];                        //Direct Page 1 (Stack)

   u8   gp[0xFD00];                        //General Pages 2-254

   u8   up[0x0C0];                        //Uppermost Page 255
   u8   ipl[0x40];                        //Program used to transfer memory
};

An instance of APU RAM is created:
Code:
APURAM   ram;

The APURAM instance is filled with the contents of my .spc file that I specified on the command line (called 'fh'):
Code:
//Read various components ------------------
   fread(&spc,0x100,1,fh);
   fread(&ram,0x10000,1,fh); -- line A
   fread(&dsp,0x80,1,fh);
   fseek(fh,0x40,SEEK_CUR); -- line B
   fread(&ram.ipl,0x40,1,fh); -- line C
   fclose(fh);

On line A the contents of the 64K of SPC ram are placed into the APURAM struct. This is good. At this point. All of my vectors from $FFC0-$FFDF still exist (I know because I inserted debug statements to check).

However, on lines B and C we find the problem. On line A the .spc file pointer skips over the 64 bytes of "unused" space and is placed at the "Extra RAM" region of the SPC file header. You can see the "unused" and "Extra RAM" regions in the SPC file format here: http://www.romhacking.net/documents/221/

After line C is executed my BRK/TCALL vectors from $FFC0-$FFDF have been filled with all zeros (since that is the contents of my SPC file header in the "Extra RAM" region). If I comment out line C and re-run SPC2ROM all of my BRK/TCALL vectors are exactly where they should be. Additionally, when I run the new ROM on an emulator or on my real console it works perfectly and all of the BRK/TCALL vectors work just like they should.

So now that I understand how SPC2ROM is wiping out all of my BRK/TCALL vectors, my question is why is it doing this? And why does the SPC file format specify this 64-byte "Extra RAM" region that overwrites the region from $FFC0-$FFFF? Shouldn't it just fill that region with the contents of the 64Kbytes of ram in my .spc file like it does with the data in every other page of ram?