Skip to content

Commit

Permalink
All attempts at moving the audiobank index table redline, maybe I'm i…
Browse files Browse the repository at this point in the history
…n the wrong part of code but nothing works and the path forward is slow and daunting, considering prototype ZoeyZolotova#2 a failure, moving to ZoeyZolotova#3
  • Loading branch information
isghj5 committed Jan 28, 2020
1 parent 1746c91 commit 9d2b48d
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Utils/RomUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ public static byte[] BuildROM(string FileName)
});
byte[] ROM = new byte[0x2000000];
int ROMAddr = 0;
// write all files to rom
for (int i = 0; i < RomData.MMFileList.Count; i++)
{
if (RomData.MMFileList[i].Cmp_Addr == -1)
Expand All @@ -315,6 +316,9 @@ public static byte[] BuildROM(string FileName)
RomData.MMFileList[i].Cmp_End = ROMAddr + file_len;
}
ReadWriteUtils.Arr_Insert(RomData.MMFileList[i].Data, 0, file_len, ROM, ROMAddr);
if (i < 10)
Debug.WriteLine("File " + i + " is at loc:[" + RomData.MMFileList[i].Cmp_Addr.ToString("X") + "] with len:[" + file_len.ToString("X") + "]");

ROMAddr += file_len;
}
UpdateFileTable(ROM);
Expand Down
100 changes: 95 additions & 5 deletions Utils/SequenceUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,96 @@ public static void RebuildAudioSeq(List<SequenceInfo> SequenceList)
// prototype instrument set injection
if (SequenceList[j].CustomInstrument == true)
{
byte[] new_audiobank_index = new byte[0x400]; // old was 2a0, 400 is space for 22 more
//int audiobank_index_destination = 0x97F70 - new_audiobank_index.Length; // 0x00097F70: end of audioseq
int audiobank_index_destination = 0x1fff100; // 0x00097F70: end of audioseq

//f = RomUtils.GetFileIndexForWriting(Addresses.AudiobankTable);
//int audioseq_table_adjusted_addr = Addresses.AudiobankTable - RomData.MMFileList[RomUtils.GetFileIndexForWriting(Addresses.SeqTable)].Addr;

// copy the original audiobank index table to a new byte array, with extra space
// you CAN NOT copy from rom because it's yaz0 encoded, which is likely why it gets permanently stored in ram
f = RomUtils.GetFileIndexForWriting(Addresses.AudiobankTable);
int old_audiobank_index_offset = Addresses.AudiobankTable - 0x10 - RomData.MMFileList[f].Addr;
for (int old_index_iter = 0; old_index_iter < 0x2a0; ++old_index_iter){
new_audiobank_index[old_index_iter] = RomData.MMFileList[f].Data[old_audiobank_index_offset + old_index_iter];
}
/*for (int old_index_iter = 0; old_index_iter < 0x2a0; ++old_index_iter)
{
Debug.WriteLine("Byte " + new_audiobank_index[old_index_iter].ToString("X"));
}*/

// add to it as needed
// write additions to the bank and the end of the previous bank
//modify the bank count before re-injection

// inject it into the new location
// assuming the audioseq never goes back, there's lots of space there, let's place it at the tail end of the empty audioseq space
// I mean the audioseq was twice the size of the bank, so bank would need to swell to 3x to reach the end of what used to be bank+
// This was tested AFTER writing as writing correctly, just not making it to the rom correctly... maybe it's getting re-encoded?
f = RomUtils.GetFileIndexForWriting(audiobank_index_destination);
int audiobank_index_fileoffset = audiobank_index_destination - RomData.MMFileList[f].Addr;
for (int new_table_iter = 0; new_table_iter < new_audiobank_index.Length; ++new_table_iter)
{
//RomData.MMFileList[f].Data[audiobank_index_fileoffset + new_table_iter] = new_audiobank_index[new_table_iter];
RomData.MMFileList[f].Data[audiobank_index_fileoffset] = new_audiobank_index[new_table_iter]; // end of rom test
//ReadWriteUtils.WriteToROM(audiobank_index_destination + new_table_iter, new_audiobank_index[new_table_iter]);
Debug.WriteLine("After Byte " + (audiobank_index_destination + new_table_iter).ToString("X") + " is " + ReadWriteUtils.Read(audiobank_index_destination + new_table_iter).ToString("X"));
//Debug.WriteLine("After Byte " + (audiobank_index_destination + new_table_iter).ToString("X") + " is " + RomData.MMFileList[f].Data[audiobank_index_fileoffset + new_table_iter].ToString("X"));
}
// okay let's try using the old audioseq file for just the table, since we're going to expand the old audiobank file to fill
//f = RomUtils.GetFileIndexForWriting(0x60000); // puts it in rom correctly, but still red line
//RomData.MMFileList[f].Data = new_audiobank_index;
//RomData.MMFileList[f].Addr = audiobank_index_destination; // does nothing, gets re-sized anyway

// do we need to set the address?

/*int new_table_index = RomUtils.AppendFile(new_audiobank_index);
ResourceUtils.ApplyHack(Values.ModsDirectory + "reloc-audio");
var fileTable = 0xF8B0;
var filetable_offset = (fileTable + (new_table_index * 0x10) + 8) & 0xFFFF;*/



// redirect the asm pointer for the bank index table to the new location
// taken from RelocateSeq
//ReadWriteUtils.WriteToROM(0x00C2739C, new byte[] { 0x3C, 0x08, 0x80, 0x0A, 0x8D, 0x05, (byte)(offset >> 8), (byte)(offset & 0xFF) });
// 80190E18 is LUI T2,0x801E : 3C 0A 80 1E in uncompressed this is at 58
// 80190E28 is ADDIU T2,T2,0x1180 : 25 4A 11 80 in uncompressed this is at 68
// in rom this is at C27368 and 78 WAIT now its not here wtf
int location_table_pointer_code = 0xC27358; // this is asm that points to the table
f = RomUtils.GetFileIndexForWriting(location_table_pointer_code); // audioseq is between 40000 and 90000
int asm_fileoffset = location_table_pointer_code - RomData.MMFileList[f].Addr;
/*for (int x = 0; x < 50; ++x)
{
//Debug.WriteLine("Byte " + (location_table_pointer_code + x).ToString("X") + " is " +ReadWriteUtils.Read((int)location_table_pointer_code + x).ToString("X"));
Debug.WriteLine("Byte " + (location_table_pointer_code + x).ToString("X") + " is " + RomData.MMFileList[f].Data[asm_fileoffset + x].ToString("X"));
}*/
// DO NOT DO THIS it write to undecoded rom, we need to modify decoded rom
//ReadWriteUtils.WriteToROM(0xC2735a, (ushort)0x9); // even if the table was triple the size, its still not so large to shrink into 0x8 space
//ReadWriteUtils.WriteToROM(0xC2736a, (ushort)(audiobank_index_destination & 0xFFFF));
// this red lines, even though everything looks like it should work
/*RomData.MMFileList[f].Data[asm_fileoffset + 2] = 0; // LUI
RomData.MMFileList[f].Data[asm_fileoffset + 3] = 0x09;
RomData.MMFileList[f].Data[asm_fileoffset + 0x10 + 2] = (byte)((audiobank_index_destination & 0xff00) >> 8); // ADDI
RomData.MMFileList[f].Data[asm_fileoffset + 0x10 + 3] = (byte)(audiobank_index_destination & 0xff); */

// attempt to just add it as a file and let the file system handle it
/*RomData.MMFileList[f].Data[asm_fileoffset + 2] = 0x80;//(byte)((filetable_offset & 0xff000000) >> 24); // LUI
RomData.MMFileList[f].Data[asm_fileoffset + 3] = 0x0A;//(byte)((filetable_offset & 0xff0000) >> 16);
RomData.MMFileList[f].Data[asm_fileoffset + 0x10 + 2] = (byte)((filetable_offset & 0xff00) >> 8); // ADDI
RomData.MMFileList[f].Data[asm_fileoffset + 0x10 + 3] = (byte)(filetable_offset & 0xff); */

//Debug.WriteLine("The new table should be at " + audiobank_index_destination.ToString("X"));
//audiobank_index_destination = 0x46AF0; // temporary, in reality I think this needs to be set after we know how big the bank changes
RomData.MMFileList[f].Data[asm_fileoffset + 2] = (byte)((audiobank_index_destination & 0xff000000) >> 24); // LUI
RomData.MMFileList[f].Data[asm_fileoffset + 3] = (byte)((audiobank_index_destination & 0xff0000) >> 16);
RomData.MMFileList[f].Data[asm_fileoffset + 0x10 + 2] = (byte)((audiobank_index_destination & 0xff00) >> 8); // ADDI
RomData.MMFileList[f].Data[asm_fileoffset + 0x10 + 3] = (byte)(audiobank_index_destination & 0xff);

// ******************** //


// we need to load the custom bank, and change the bank
// I'm ignoring file IO errors here because this is prototype code
string bank_name = SequenceList[j].Name;
Expand All @@ -240,7 +330,7 @@ public static void RebuildAudioSeq(List<SequenceInfo> SequenceList)

// find the address of the bank we wish to replace
int bank_number = SequenceList[j].Instrument;
int table_pointer_addr = Addresses.AudiobankTable + 16 * bank_number;
int table_pointer_addr = Addresses.AudiobankTable + (16 * bank_number);
Debug.WriteLine("Seq " + j.ToString("X") + " should use bank #" + bank_number.ToString("X"));
Debug.WriteLine("Bank " + bank_number.ToString("X") + " should have a pointer in the table at " + table_pointer_addr.ToString("X"));
int bank_location = (ReadWriteUtils.ReadU16(table_pointer_addr) << 16) + ReadWriteUtils.ReadU16(table_pointer_addr + 2);
Expand All @@ -264,8 +354,8 @@ public static void RebuildAudioSeq(List<SequenceInfo> SequenceList)
// the audiobanktable isn't considered it's own file in the randomizer, we need to find the actual index of the file
f = RomUtils.GetFileIndexForWriting(Addresses.AudiobankTable);
int audioseq_table_adjusted_addr = Addresses.AudiobankTable - RomData.MMFileList[RomUtils.GetFileIndexForWriting(Addresses.SeqTable)].Addr;
for (int meta_iter = 8; meta_iter < 16; ++meta_iter)
Debug.WriteLine("Byte " + meta_iter + " is " + RomData.MMFileList[f].Data[audioseq_table_adjusted_addr + (bank_number * 16) + meta_iter].ToString("X"));
//for (int meta_iter = 8; meta_iter < 16; ++meta_iter)
// Debug.WriteLine("Byte " + meta_iter + " is " + RomData.MMFileList[f].Data[audioseq_table_adjusted_addr + (bank_number * 16) + meta_iter].ToString("X"));


// update the index with the new bank size and metadata
Expand Down Expand Up @@ -321,9 +411,9 @@ public static void RebuildAudioSeq(List<SequenceInfo> SequenceList)
int index = RomUtils.AppendFile(NewAudioSeq);
ResourceUtils.ApplyHack(Values.ModsDirectory + "reloc-audio");
RelocateSeq(index);
RomData.MMFileList[4].Data = new byte[0];
/*RomData.MMFileList[4].Data = new byte[0]; // removed: this results in the audiotable getting pulled back
RomData.MMFileList[4].Cmp_Addr = -1;
RomData.MMFileList[4].Cmp_End = -1;
RomData.MMFileList[4].Cmp_End = -1;*/
}
else
{
Expand Down

0 comments on commit 9d2b48d

Please sign in to comment.