Rename SPC-related structures and methods for consistent casing

This commit is contained in:
scawful
2024-11-18 14:05:21 -05:00
parent 731eb456ab
commit f54e5bf21c
2 changed files with 79 additions and 74 deletions

View File

@@ -22,7 +22,7 @@ namespace zelda3 {
namespace { namespace {
void AddSPCReloc(music::SongSPCBlock *sbl, short addr) { void AddSPCReloc(music::SongSpcBlock *sbl, short addr) {
sbl->relocs[sbl->relnum++] = addr; sbl->relocs[sbl->relnum++] = addr;
if (sbl->relnum == sbl->relsz) { if (sbl->relnum == sbl->relsz) {
sbl->relsz += 16; sbl->relsz += 16;
@@ -33,16 +33,16 @@ void AddSPCReloc(music::SongSPCBlock *sbl, short addr) {
} // namespace } // namespace
namespace music { namespace music {
SongSPCBlock *Tracker::AllocSPCBlock(int len, int bank) { SongSpcBlock *Tracker::AllocSpcBlock(int len, int bank) {
SongSPCBlock *sbl; SongSpcBlock *sbl;
if (!len) { if (!len) {
printf("warning zero length block allocated"); printf("warning zero length block allocated");
} }
if (ss_num == ss_size) { if (ss_num == ss_size) {
ss_size += 512; ss_size += 512;
ssblt = (SongSPCBlock **)realloc(ssblt, ss_size << 2); ssblt = (SongSpcBlock **)realloc(ssblt, ss_size << 2);
} }
ssblt[ss_num] = sbl = (SongSPCBlock *)malloc(sizeof(SongSPCBlock)); ssblt[ss_num] = sbl = (SongSpcBlock *)malloc(sizeof(SongSpcBlock));
ss_num++; ss_num++;
sbl->start = ss_next; sbl->start = ss_next;
sbl->len = len; sbl->len = len;
@@ -58,7 +58,7 @@ SongSPCBlock *Tracker::AllocSPCBlock(int len, int bank) {
// ============================================================================= // =============================================================================
unsigned char *Tracker::GetSPCAddr(Rom &rom, unsigned short addr, short bank) { unsigned char *Tracker::GetSpcAddr(Rom &rom, unsigned short addr, short bank) {
unsigned char *rom_ptr; unsigned char *rom_ptr;
unsigned short a; unsigned short a;
unsigned short b; unsigned short b;
@@ -94,16 +94,16 @@ again:
// ============================================================================= // =============================================================================
short Tracker::AllocSPCCommand() { short Tracker::AllocSpcCommand() {
int i = m_free; int i = m_free;
int j; int j;
int k; int k;
SPCCommand *spc_command; SpcCommand *spc_command;
if (i == -1) { if (i == -1) {
j = m_size; j = m_size;
m_size += 1024; m_size += 1024;
spc_command = current_spc_command_ = (SPCCommand *)realloc( spc_command = current_spc_command_ = (SpcCommand *)realloc(
current_spc_command_, m_size * sizeof(SPCCommand)); current_spc_command_, m_size * sizeof(SpcCommand));
k = 1023; k = 1023;
while (k--) spc_command[j].next = j + 1, j++; while (k--) spc_command[j].next = j + 1, j++;
spc_command[j].next = -1; spc_command[j].next = -1;
@@ -121,8 +121,8 @@ short Tracker::AllocSPCCommand() {
// ============================================================================= // =============================================================================
short Tracker::GetBlockTime(Rom &rom, short num, short prevtime) { short Tracker::GetBlockTime(Rom &rom, short num, short prevtime) {
SPCCommand *spc_command = current_spc_command_; SpcCommand *spc_command = current_spc_command_;
SPCCommand *spc_command2; SpcCommand *spc_command2;
int i = -1; int i = -1;
int j = 0; int j = 0;
@@ -214,7 +214,7 @@ short Tracker::GetBlockTime(Rom &rom, short num, short prevtime) {
// ============================================================================= // =============================================================================
short Tracker::LoadSPCCommand(Rom &rom, unsigned short addr, short bank, short Tracker::LoadSpcCommand(Rom &rom, unsigned short addr, short bank,
int t) { int t) {
int b = 0; int b = 0;
int c = 0; int c = 0;
@@ -235,11 +235,11 @@ short Tracker::LoadSPCCommand(Rom &rom, unsigned short addr, short bank,
unsigned char *a = nullptr; unsigned char *a = nullptr;
SongRange *sr; SongRange *sr;
SPCCommand *spc_command = current_spc_command_; SpcCommand *spc_command = current_spc_command_;
SPCCommand *spc_command2; SpcCommand *spc_command2;
if (!addr) return -1; if (!addr) return -1;
a = GetSPCAddr(rom, addr, bank); a = GetSpcAddr(rom, addr, bank);
d = spcbank; d = spcbank;
if (!a) { if (!a) {
printf("Address not found when loading track"); printf("Address not found when loading track");
@@ -250,7 +250,7 @@ short Tracker::LoadSPCCommand(Rom &rom, unsigned short addr, short bank,
f = 0x10000; f = 0x10000;
for (c = 0; c < e; c++) { for (c = 0; c < e; c++) {
if (sr[c].bank == d) if (sr[c].bank == d) {
if (sr[c].start > addr) { if (sr[c].start > addr) {
if (sr[c].start < f) f = sr[c].start; if (sr[c].start < f) f = sr[c].start;
n = c; n = c;
@@ -265,15 +265,18 @@ short Tracker::LoadSPCCommand(Rom &rom, unsigned short addr, short bank,
return f; return f;
} }
if (spc_command[f].flag & 1) k = spc_command[f].b1; if (spc_command[f].flag & 1) k = spc_command[f].b1;
if (spc_command[f].cmd < 0xca) if (spc_command[f].cmd < 0xca) {
if (k) if (k) {
m -= k; m -= k;
else } else {
o--; o--;
}
}
} }
printf("Misaligned music pointer"); printf("Misaligned music pointer");
return -1; return -1;
} }
}
} }
c = n; c = n;
@@ -287,8 +290,8 @@ short Tracker::LoadSPCCommand(Rom &rom, unsigned short addr, short bank,
spc_command2 = spc_command + i; spc_command2 = spc_command + i;
if (spc_command2->next == -1) { if (spc_command2->next == -1) {
l = m_size; l = m_size;
spc_command = current_spc_command_ = (SPCCommand *)realloc( spc_command = current_spc_command_ = (SpcCommand *)realloc(
spc_command, sizeof(SPCCommand) * (m_size += 1024)); spc_command, sizeof(SpcCommand) * (m_size += 1024));
spc_command2 = spc_command + i; spc_command2 = spc_command + i;
n = l + 1023; n = l + 1023;
while (l < n) spc_command[l].next = l + 1, l++; while (l < n) spc_command[l].next = l + 1, l++;
@@ -310,11 +313,13 @@ short Tracker::LoadSPCCommand(Rom &rom, unsigned short addr, short bank,
b = a[g++]; b = a[g++];
if (b < 128) j = 3, spc_command2->b2 = b, b = a[g++]; if (b < 128) j = 3, spc_command2->b2 = b, b = a[g++];
} }
if (b < 0xe0) if (b < 0xe0) {
if (k) if (k) {
m += k; m += k;
else } else {
o++; o++;
}
}
spc_command2->cmd = b; spc_command2->cmd = b;
spc_command2->flag = j; spc_command2->flag = j;
if (b >= 0xe0) { if (b >= 0xe0) {
@@ -325,7 +330,7 @@ short Tracker::LoadSPCCommand(Rom &rom, unsigned short addr, short bank,
if (b == 15) { if (b == 15) {
m_free = spc_command2->next; m_free = spc_command2->next;
spc_command[spc_command2->next].prev = -1; spc_command[spc_command2->next].prev = -1;
l = LoadSPCCommand(rom, *(short *)(&(spc_command2->p1)), bank, t - m); l = LoadSpcCommand(rom, *(short *)(&(spc_command2->p1)), bank, t - m);
spc_command = current_spc_command_; spc_command = current_spc_command_;
spc_command2 = spc_command + i; spc_command2 = spc_command + i;
*(short *)(&(spc_command2->p1)) = l; *(short *)(&(spc_command2->p1)) = l;
@@ -394,7 +399,7 @@ void Tracker::LoadSongs(Rom &rom) {
Song song; Song song;
Song song2; Song song2;
SongPart *sp; SongPart *sp;
SPCCommand *spc_command; SpcCommand *spc_command;
ZeldaWave *zelda_wave; ZeldaWave *zelda_wave;
int i; int i;
@@ -413,7 +418,7 @@ void Tracker::LoadSongs(Rom &rom) {
int filter; int filter;
spc_command = current_spc_command_ = spc_command = current_spc_command_ =
(SPCCommand *)malloc(1024 * sizeof(SPCCommand)); (SpcCommand *)malloc(1024 * sizeof(SpcCommand));
m_free = 0; m_free = 0;
m_size = 1024; m_size = 1024;
srnum = 0; srnum = 0;
@@ -439,7 +444,7 @@ void Tracker::LoadSongs(Rom &rom) {
spc_command[1023].next = -1; spc_command[1023].next = -1;
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
// Extract the song banks. // Extract the song banks.
b = GetSPCAddr(rom, 0xd000, i); b = GetSpcAddr(rom, 0xd000, i);
for (j = 0;; j++) { for (j = 0;; j++) {
if ((r = ((unsigned short *)b)[j]) >= 0xd000) { if ((r = ((unsigned short *)b)[j]) >= 0xd000) {
r = (r - 0xd000) >> 1; r = (r - 0xd000) >> 1;
@@ -453,7 +458,7 @@ void Tracker::LoadSongs(Rom &rom) {
if (!k) if (!k)
songs[l].in_use = false; songs[l].in_use = false;
else { else {
c = GetSPCAddr(rom, k, i); c = GetSpcAddr(rom, k, i);
// Init the bank index we are current loading. // Init the bank index we are current loading.
if (!spcbank) if (!spcbank)
@@ -488,7 +493,7 @@ void Tracker::LoadSongs(Rom &rom) {
for (m = 0; m < song.numparts; m++) { for (m = 0; m < song.numparts; m++) {
k = ((unsigned short *)c)[m]; k = ((unsigned short *)c)[m];
d = GetSPCAddr(rom, k, i); d = GetSpcAddr(rom, k, i);
if (!spcbank) if (!spcbank)
n = 0; n = 0;
else else
@@ -517,7 +522,7 @@ void Tracker::LoadSongs(Rom &rom) {
p = 50000; p = 50000;
for (o = 0; o < 8; o++) { for (o = 0; o < 8; o++) {
q = sp->tbl[o] = q = sp->tbl[o] =
LoadSPCCommand(rom, ((unsigned short *)d)[o], i, p); LoadSpcCommand(rom, ((unsigned short *)d)[o], i, p);
spc_command = current_spc_command_ + q; spc_command = current_spc_command_ + q;
if ((spc_command->flag & 4) && spc_command->tim < p) if ((spc_command->flag & 4) && spc_command->tim < p)
p = spc_command->tim; p = spc_command->tim;
@@ -530,28 +535,28 @@ void Tracker::LoadSongs(Rom &rom) {
} }
} }
b = GetSPCAddr(rom, 0x800, 0); b = GetSpcAddr(rom, 0x800, 0);
snddat1 = (char *)malloc(spclen); snddat1 = (char *)malloc(spclen);
sndlen1 = spclen; sndlen1 = spclen;
memcpy(snddat1, b, spclen); memcpy(snddat1, b, spclen);
b = GetSPCAddr(rom, 0x17c0, 0); b = GetSpcAddr(rom, 0x17c0, 0);
snddat2 = (char *)malloc(spclen); snddat2 = (char *)malloc(spclen);
sndlen2 = spclen; sndlen2 = spclen;
memcpy(snddat2, b, spclen); memcpy(snddat2, b, spclen);
b = GetSPCAddr(rom, 0x3d00, 0); b = GetSpcAddr(rom, 0x3d00, 0);
insts = (ZeldaInstrument *)malloc(spclen); insts = (ZeldaInstrument *)malloc(spclen);
memcpy(insts, b, spclen); memcpy(insts, b, spclen);
numinst = spclen / 6; numinst = spclen / 6;
b = GetSPCAddr(rom, 0x3e00, 0); b = GetSpcAddr(rom, 0x3e00, 0);
m_ofs = b - rom.data() + spclen; m_ofs = b - rom.data() + spclen;
sndinsts = (ZeldaSfxInstrument *)malloc(spclen); sndinsts = (ZeldaSfxInstrument *)malloc(spclen);
memcpy(sndinsts, b, spclen); memcpy(sndinsts, b, spclen);
numsndinst = spclen / 9; numsndinst = spclen / 9;
b = GetSPCAddr(rom, 0x3c00, 0); b = GetSpcAddr(rom, 0x3c00, 0);
zelda_wave = waves = (ZeldaWave *)malloc(sizeof(ZeldaWave) * (spclen >> 2)); zelda_wave = waves = (ZeldaWave *)malloc(sizeof(ZeldaWave) * (spclen >> 2));
p = spclen >> 1; p = spclen >> 1;
@@ -571,7 +576,7 @@ void Tracker::LoadSongs(Rom &rom) {
foundwave: foundwave:
d = GetSPCAddr(rom, j, 0); d = GetSpcAddr(rom, j, 0);
e = (short *)malloc(2048); e = (short *)malloc(2048);
k = 0; k = 0;
@@ -655,12 +660,12 @@ void Tracker::LoadSongs(Rom &rom) {
w_modf = 0; w_modf = 0;
} }
short Tracker::SaveSPCCommand(Rom &rom, short num, short songtime, short Tracker::SaveSpcCommand(Rom &rom, short num, short songtime,
short endtr) { short endtr) {
SPCCommand *spc_command = current_spc_command_; SpcCommand *spc_command = current_spc_command_;
SPCCommand *spc_command2; SpcCommand *spc_command2;
SongRange *sr = song_range_; SongRange *sr = song_range_;
SongSPCBlock *sbl; SongSpcBlock *sbl;
text_buf_ty buf; text_buf_ty buf;
@@ -717,7 +722,7 @@ short Tracker::SaveSPCCommand(Rom &rom, short num, short songtime,
k += l; k += l;
} }
k++; k++;
sbl = AllocSPCBlock(k, sr[j].bank | ((!endtr) << 3) | 16); sbl = AllocSpcBlock(k, sr[j].bank | ((!endtr) << 3) | 16);
b = sbl->buf; b = sbl->buf;
for (;;) { for (;;) {
@@ -732,7 +737,7 @@ short Tracker::SaveSPCCommand(Rom &rom, short num, short songtime,
o = op_len[spc_command2->cmd - 0xe0]; o = op_len[spc_command2->cmd - 0xe0];
if (spc_command2->cmd == 0xef) { if (spc_command2->cmd == 0xef) {
*(short *)b = *(short *)b =
SaveSPCCommand(rom, *(short *)&(spc_command2->p1), 0, 1); SaveSpcCommand(rom, *(short *)&(spc_command2->p1), 0, 1);
if (b) AddSPCReloc(sbl, b - sbl->buf); if (b) AddSPCReloc(sbl, b - sbl->buf);
b[2] = spc_command2->p3; b[2] = spc_command2->p3;
b += 3; b += 3;
@@ -778,7 +783,7 @@ short Tracker::SaveSPCCommand(Rom &rom, short num, short songtime,
// ============================================================================= // =============================================================================
int Tracker::WriteSPCData(Rom &rom, void *buf, int len, int addr, int spc, int Tracker::WriteSpcData(Rom &rom, void *buf, int len, int addr, int spc,
int limit) { int limit) {
unsigned char *rom_data = rom.data(); unsigned char *rom_data = rom.data();
@@ -836,14 +841,14 @@ void Tracker::SaveSongs(Rom &rom) {
Song song; Song song;
SPCCommand *spc_command; SpcCommand *spc_command;
SongPart *sp; SongPart *sp;
SongSPCBlock *stbl; SongSpcBlock *stbl;
SongSPCBlock *sptbl; SongSpcBlock *sptbl;
SongSPCBlock *trtbl; SongSpcBlock *trtbl;
SongSPCBlock *pstbl; SongSpcBlock *pstbl;
ZeldaWave *zelda_wave; ZeldaWave *zelda_wave;
ZeldaWave *zelda_wave2; ZeldaWave *zelda_wave2;
@@ -864,7 +869,7 @@ void Tracker::SaveSongs(Rom &rom) {
// if the music has not been modified, return. // if the music has not been modified, return.
if (!(m_modf)) return; if (!(m_modf)) return;
ssblt = (SongSPCBlock **)malloc(512 * sizeof(SongSPCBlock)); ssblt = (SongSpcBlock **)malloc(512 * sizeof(SongSpcBlock));
// set it so the music has not been modified. (reset the status) // set it so the music has not been modified. (reset the status)
m_modf = 0; m_modf = 0;
@@ -901,7 +906,7 @@ void Tracker::SaveSongs(Rom &rom) {
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
k = numsong[i]; k = numsong[i];
stbl = AllocSPCBlock(k << 1, i + 1); stbl = AllocSpcBlock(k << 1, i + 1);
for (j = 0; j < k; j++) { for (j = 0; j < k; j++) {
song = songs[l++]; song = songs[l++];
@@ -914,7 +919,7 @@ void Tracker::SaveSongs(Rom &rom) {
if (song.flag & 4) goto alreadysaved; if (song.flag & 4) goto alreadysaved;
sptbl = AllocSPCBlock(((song.numparts + 1) << 1) + (song.flag & 2), sptbl = AllocSpcBlock(((song.numparts + 1) << 1) + (song.flag & 2),
(song.flag & 1) ? 0 : (i + 1)); (song.flag & 1) ? 0 : (i + 1));
for (m = 0; m < song.numparts; m++) { for (m = 0; m < song.numparts; m++) {
@@ -922,7 +927,7 @@ void Tracker::SaveSongs(Rom &rom) {
if (sp->flag & 2) goto spsaved; if (sp->flag & 2) goto spsaved;
trtbl = AllocSPCBlock(16, (sp->flag & 1) ? 0 : (i + 1)); trtbl = AllocSpcBlock(16, (sp->flag & 1) ? 0 : (i + 1));
p = 0; p = 0;
@@ -935,7 +940,7 @@ void Tracker::SaveSongs(Rom &rom) {
q = 1; q = 1;
for (n = 0; n < 8; n++) { for (n = 0; n < 8; n++) {
core::stle16b_i(trtbl->buf, n, SaveSPCCommand(rom, sp->tbl[n], p, q)); core::stle16b_i(trtbl->buf, n, SaveSpcCommand(rom, sp->tbl[n], p, q));
if (core::ldle16b_i(trtbl->buf, n)) AddSPCReloc(trtbl, n << 1), q = 0; if (core::ldle16b_i(trtbl->buf, n)) AddSPCReloc(trtbl, n << 1), q = 0;
} }
@@ -1139,14 +1144,14 @@ void Tracker::SaveSongs(Rom &rom) {
} }
} }
m = WriteSPCData(rom, wtbl, numwave << 2, 0xc8000, 0x3c00, 0xd74fc); m = WriteSpcData(rom, wtbl, numwave << 2, 0xc8000, 0x3c00, 0xd74fc);
m = WriteSPCData(rom, b, j, m, 0x4000, 0xd74fc); m = WriteSpcData(rom, b, j, m, 0x4000, 0xd74fc);
free(b); free(b);
m = WriteSPCData(rom, insts, numinst * 6, m, 0x3d00, 0xd74fc); m = WriteSpcData(rom, insts, numinst * 6, m, 0x3d00, 0xd74fc);
m = WriteSPCData(rom, snddat1, sndlen1, m, 0x800, 0xd74fc); m = WriteSpcData(rom, snddat1, sndlen1, m, 0x800, 0xd74fc);
m = WriteSPCData(rom, snddat2, sndlen2, m, 0x17c0, 0xd74fc); m = WriteSpcData(rom, snddat2, sndlen2, m, 0x17c0, 0xd74fc);
m = WriteSPCData(rom, sndinsts, numsndinst * 9, m, 0x3e00, 0xd74fc); m = WriteSpcData(rom, sndinsts, numsndinst * 9, m, 0x3e00, 0xd74fc);
m_ofs = m; m_ofs = m;
} else { } else {
m = m_ofs; m = m_ofs;
@@ -1261,7 +1266,7 @@ void Tracker::SaveSongs(Rom &rom) {
void Tracker::EditTrack(Rom &rom, short i) { void Tracker::EditTrack(Rom &rom, short i) {
int j, k, l; int j, k, l;
SongRange *sr = song_range_; SongRange *sr = song_range_;
SPCCommand *spc_command; SpcCommand *spc_command;
text_buf_ty buf; text_buf_ty buf;
@@ -1309,7 +1314,7 @@ void Tracker::EditTrack(Rom &rom, short i) {
// ============================================================================= // =============================================================================
void Tracker::NewSR(Rom &rom, int bank) { void Tracker::NewSR(Rom &rom, int bank) {
SPCCommand *spc_command; SpcCommand *spc_command;
SongRange *sr; SongRange *sr;
if (srnum == srsize) { if (srnum == srsize) {
@@ -1319,7 +1324,7 @@ void Tracker::NewSR(Rom &rom, int bank) {
sr = song_range_ + srnum; sr = song_range_ + srnum;
srnum++; srnum++;
sr->first = AllocSPCCommand(); sr->first = AllocSpcCommand();
sr->bank = bank; sr->bank = bank;
sr->editor = 0; sr->editor = 0;
spc_command = current_spc_command_ + sr->first; spc_command = current_spc_command_ + sr->first;

View File

@@ -38,7 +38,7 @@ constexpr int kDungeonMusicBank = 0x0D8000;
using text_buf_ty = char[512]; using text_buf_ty = char[512];
// ============================================================================ // ============================================================================
struct SongSPCBlock { struct SongSpcBlock {
unsigned short start; unsigned short start;
unsigned short len; unsigned short len;
unsigned short relnum; unsigned short relnum;
@@ -147,7 +147,7 @@ struct ZeldaSfxInstrument {
// ============================================================================= // =============================================================================
struct SPCCommand { struct SpcCommand {
unsigned short addr; unsigned short addr;
short next; short next;
short prev; short prev;
@@ -166,22 +166,22 @@ struct SPCCommand {
class Tracker { class Tracker {
public: public:
SongSPCBlock *AllocSPCBlock(int len, int bank); SongSpcBlock *AllocSpcBlock(int len, int bank);
unsigned char *GetSPCAddr(Rom &rom, unsigned short addr, short bank); unsigned char *GetSpcAddr(Rom &rom, unsigned short addr, short bank);
short AllocSPCCommand(); short AllocSpcCommand();
short GetBlockTime(Rom &rom, short num, short prevtime); short GetBlockTime(Rom &rom, short num, short prevtime);
short SaveSPCCommand(Rom &rom, short num, short songtime, short endtr); short SaveSpcCommand(Rom &rom, short num, short songtime, short endtr);
short LoadSPCCommand(Rom &rom, unsigned short addr, short bank, int t); short LoadSpcCommand(Rom &rom, unsigned short addr, short bank, int t);
void SaveSongs(Rom &rom); void SaveSongs(Rom &rom);
void LoadSongs(Rom &rom); void LoadSongs(Rom &rom);
int WriteSPCData(Rom &rom, void *buf, int len, int addr, int spc, int limit); int WriteSpcData(Rom &rom, void *buf, int len, int addr, int spc, int limit);
void EditTrack(Rom &rom, short i); void EditTrack(Rom &rom, short i);
@@ -241,9 +241,9 @@ class Tracker {
std::vector<Song> songs; std::vector<Song> songs;
SongPart *sp_mark; SongPart *sp_mark;
SongRange *song_range_; SongRange *song_range_;
SPCCommand *current_spc_command_; SpcCommand *current_spc_command_;
SongSPCBlock **ssblt; SongSpcBlock **ssblt;
ZeldaWave *waves; ZeldaWave *waves;
ZeldaInstrument *insts; ZeldaInstrument *insts;