127 if (l == -1)
return 0;
130 if (spc_command[l].flag & 4) {
131 j = spc_command[l].
tim;
132 m = spc_command[l].
tim2;
138 if (spc_command[l].flag & 1) n = spc_command[l].
b1;
140 l = spc_command[l].
next;
159 spc_command2 = spc_command + i;
160 if (spc_command2->
cmd == 0xef) {
161 k = *(
short *)&(spc_command2->
p1);
163 printf(
"Invalid music address\n");
167 if (spc_command2->
flag & 1) {
171 j += spc_command[k].
tim2 * spc_command2->
b1;
172 j += (spc_command2->
p3 - 1) * spc_command[k].tim2 *
ss_lasttime;
175 spc_command2->
b1 * (m + spc_command[k].
tim2 * spc_command2->
p3);
182 j += (spc_command2->
p3 - 1) *
ss_lasttime * spc_command[k].tim2,
183 m = spc_command[k].tim2;
185 m += spc_command[k].
tim2 * spc_command2->
p3;
188 if (spc_command2->
cmd < 0xe0) m++;
189 if (spc_command2->
flag & 1) {
190 j += m * spc_command[i].
b1;
194 spc_command2->
tim = j;
195 spc_command2->
tim2 = m;
196 spc_command2->
flag |= 4;
200 i = spc_command2->
prev;
204 return spc_command[num].
tim + prevtime * spc_command[num].
tim2;
225 unsigned char *a =
nullptr;
230 if (!addr)
return -1;
235 printf(
"Address not found when loading track");
242 for (c = 0; c < e; c++) {
243 if (sr[c].bank == d) {
244 if (sr[c].start > addr) {
245 if (sr[c].start < f) f = sr[c].
start;
247 }
else if (sr[c].end > addr) {
248 for (f = sr[c].first; f != -1; f = spc_command[f].
next) {
249 if (spc_command[f].flag & 4)
250 m = spc_command[f].
tim, o = spc_command[f].
tim2;
251 if (spc_command[f].addr == addr) {
252 spc_command[f].
tim = m;
253 spc_command[f].
tim2 = o;
257 if (spc_command[f].flag & 1) k = spc_command[f].
b1;
258 if (spc_command[f].cmd < 0xca) {
266 printf(
"Misaligned music pointer");
279 for (g = addr; g < f;) {
280 spc_command2 = spc_command + i;
281 if (spc_command2->
next == -1) {
285 spc_command2 = spc_command + i;
287 while (l < n) spc_command[l].
next = l + 1, l++;
288 spc_command[l].
next = -1;
290 while (l > n) spc_command[l].
prev = l - 1, l--;
291 spc_command[l].
prev = i;
292 spc_command2->
next = l;
294 spc_command2->
addr = g;
297 if (m >= t && b != 0xf9)
break;
302 k = spc_command2->
b1 = b;
304 if (b < 128) j = 3, spc_command2->
b2 = b, b = a[g++];
313 spc_command2->
cmd = b;
314 spc_command2->
flag = j;
317 if (
op_len[b]) spc_command2->
p1 = a[g++];
318 if (
op_len[b] > 1) spc_command2->
p2 = a[g++];
319 if (
op_len[b] > 2) spc_command2->
p3 = a[g++];
322 spc_command[spc_command2->
next].
prev = -1;
325 spc_command2 = spc_command + i;
326 *(
short *)(&(spc_command2->
p1)) = l;
328 spc_command[spc_command2->
next].
prev = i;
330 if (spc_command[l].flag & 4)
332 (spc_command[l].
tim + spc_command[l].
tim2 * k) * spc_command2->
p3;
334 i = spc_command2->
next;
340 i = spc_command2->
next;
343 spc_command[h].
tim = m;
344 spc_command[h].
tim2 = o;
345 spc_command[h].
flag |= 4;
347 if (f == g && m < t) {
348 l = spc_command[i].
prev;
352 if (spc_command[l].flag & 4)
353 spc_command[h].
tim = spc_command[l].
tim + m + spc_command[l].
tim2 * k,
354 spc_command[h].
flag |= 4;
372 spc_command[spc_command[i].
prev].
next = -1;
375 spc_command[i].
prev = -1;
418 sbank_ofs[1] = (b[0x91c] << 15) + ((b[0x918] & 127) << 8) + b[0x914];
419 sbank_ofs[3] = (b[0x93a] << 15) + ((b[0x936] & 127) << 8) + b[0x932];
421 for (i = 0; i < 1024; i++) {
422 spc_command[i].
next = i + 1;
423 spc_command[i].
prev = i - 1;
427 for (i = 0; i < 128; i++) {
429 songs.emplace_back(new_song);
432 spc_command[1023].
next = -1;
433 for (i = 0; i < 3; i++) {
437 if ((r = ((
unsigned short *)b)[j]) >= 0xd000) {
438 r = (r - 0xd000) >> 1;
444 for (j = 0; j < r; j++) {
445 k = ((
unsigned short *)b)[j];
447 songs[l].in_use =
false;
472 if ((n = ((
unsigned short *)c)[m]) < 256)
break;
476 song.
lopst = (((
unsigned short *)c)[m + 1] - k) >> 1;
482 for (m = 0; m < song.
numparts; m++) {
483 k = ((
unsigned short *)c)[m];
493 for (o = 0; o < song2.
numparts; o++)
495 (song.
tbl[m] = song2.
tbl[o])->inst++;
500 for (o = 0; o < m; o++)
502 (song.
tbl[m] = song.
tbl[o])->inst++;
511 for (o = 0; o < 8; o++) {
515 if ((spc_command->
flag & 4) && spc_command->
tim < p)
516 p = spc_command->
tim;
551 for (i = 0; i < p; i += 2) {
552 j = ((
unsigned short *)b)[i];
554 if (j == 65535)
break;
556 for (k = 0; k < i; k += 2) {
557 if (((
unsigned short *)b)[k] == j) {
558 zelda_wave->
copy = (short)(k >> 1);
563 zelda_wave->
copy = -1;
568 e = (
short *)malloc(2048);
577 range = (m >> 4) + 8;
578 filter = (m & 12) >> 2;
580 for (n = 0; n < 8; n++) {
588 o += (t *
fil1[filter] >>
fil2[filter]) -
589 ((u & -256) *
fil3[filter] >> 4);
591 if (o > 0x7fffff) o = 0x7fffff;
593 if (o < -0x800000) o = -0x800000;
609 o += (u *
fil1[filter] >>
fil2[filter]) -
610 ((t & -256) *
fil3[filter] >> 4);
612 if (o > 0x7fffff) o = 0x7fffff;
614 if (o < -0x800000) o = -0x800000;
623 zelda_wave->
lflag = (m & 2) >> 1;
628 e = (
short *)realloc(e, l << 1);
632 e = zelda_wave->
buf = (
short *)realloc(e, (k + 1) << 1);
634 zelda_wave->
lopst = (((
unsigned short *)b)[i + 1] - j) * 16 / 9;
636 if (zelda_wave->
lflag)
637 e[k] = e[zelda_wave->
lopst];
670 if (i == -1)
return 0;
678 if (spc_command[i].flag & 8)
return spc_command[i].
addr;
681 j = spc_command[i].
prev;
686 for (j = 0; j <
srnum; j++) {
687 if (sr[j].first == i) {
693 spc_command2 = spc_command + m;
694 if (spc_command2->
flag & 1) k++, n = spc_command2->
b1;
695 if (spc_command2->
flag & 2) k++;
696 if (spc_command2->
cmd >= 0xe0) k +=
op_len[spc_command2->
cmd - 0xe0];
697 m = spc_command2->
next;
702 l = (songtime + 126) / 127;
703 if (songtime % l) l += 2;
705 if (n && !songtime % n) {
718 spc_command2 = spc_command + i;
720 spc_command2->
flag |= 8;
721 if (spc_command2->
flag & 1) *(b++) = spc_command2->
b1;
722 if (spc_command2->
flag & 2) *(b++) = spc_command2->
b2;
723 *(b++) = spc_command2->
cmd;
724 if (spc_command2->
cmd >= 0xe0) {
726 if (spc_command2->
cmd == 0xef) {
729 if (b) AddSpcReloc(sbl, b - sbl->
buf);
730 b[2] = spc_command2->
p3;
733 if (o) *(b++) = spc_command2->
p1;
734 if (o > 1) *(b++) = spc_command2->
p2;
735 if (o > 2) *(b++) = spc_command2->
p3;
738 i = spc_command2->
next;
743 l = (songtime + 126) / 127;
751 for (; songtime >= n; songtime -= n) *(b++) = 0xc9;
754 *(b++) = (uint8_t)songtime;
759 return spc_command[num].
addr;
763 printf(
"Address %04X not found", num);
776 if (!len)
return addr;
778 if (((addr + len + 4) & 0x7fff) > 0x7ffb) {
779 if (addr + 5 > limit)
goto error;
780 *(
int *)(rom_data + addr) = 0x00010140;
781 rom_data[addr + 4] = 0xff;
785 if (addr + len + 4 > limit) {
787 printf(
"Not enough space for sound data");
792 *(
short *)(rom_data + addr) = len;
793 *(
short *)(rom_data + addr + 2) = spc;
795 memcpy(rom_data + addr + 4, buf, len);
797 return addr + len + 4;
819 unsigned short bank_next[4];
820 unsigned short bank_lwr[4];
823 unsigned char *rom_data;
864 for (i = 0; i < 3; i++) {
867 for (j = 0; j < k; j++) {
870 if (!song.
in_use)
continue;
874 for (m = 0; m < song.
numparts; m++) {
884 for (i = 0; i < j; i++) {
885 spc_command->
flag &= -13;
891 for (i = 0; i < 3; i++) {
895 for (j = 0; j < k; j++) {
899 ((
short *)(stbl->
buf))[j] = 0;
904 if (song.
flag & 4)
goto alreadysaved;
907 (song.
flag & 1) ? 0 : (i + 1));
909 for (m = 0; m < song.
numparts; m++) {
912 if (sp->
flag & 2)
goto spsaved;
918 for (n = 0; n < 8; n++) {
926 for (n = 0; n < 8; n++) {
929 if (
ldle16b_i(trtbl->
buf, n)) AddSpcReloc(trtbl, n << 1), q = 0;
935 ((
short *)(sptbl->
buf))[m] = sp->
addr;
937 AddSpcReloc(sptbl, m << 1);
941 ((
short *)(sptbl->
buf))[m++] = 255;
942 ((
short *)(sptbl->
buf))[m] = sptbl->
start + (song.
lopst << 1);
944 AddSpcReloc(sptbl, m << 1);
946 ((
short *)(sptbl->
buf))[m++] = 0;
951 ((
short *)(stbl->
buf))[j] = song.
addr;
953 AddSpcReloc(stbl, j << 1);
958 b = (uint8_t *)malloc(0xc000);
968 for (i = 0; i <
numwave; i++, zelda_wave++) {
969 if (zelda_wave->
copy != -1)
continue;
971 wtbl[i << 1] = j + 0x4000;
973 if (zelda_wave->
lflag) {
974 l = zelda_wave->
end - zelda_wave->
lopst;
977 k = (l << 15) / ((l + 15) & -16);
978 p = (zelda_wave->
end << 15) / k;
979 c = (
short *)malloc(p << 1);
984 c[n++] = (d[m >> 15] * ((m & 32767) ^ 32767) +
985 d[(m >> 15) + 1] * (m & 32767)) /
993 zelda_wave->
lopst = (zelda_wave->
lopst << 15) / k;
996 (
short *)realloc(zelda_wave->
buf, (zelda_wave->
end + 1) << 1);
997 memcpy(zelda_wave->
buf, c, zelda_wave->
end << 1);
999 zelda_wave->
buf[zelda_wave->
end] = zelda_wave->
buf[zelda_wave->
lopst];
1000 zelda_wave2 =
waves;
1002 for (m = 0; m <
numwave; m++, zelda_wave2++)
1003 if (zelda_wave2->
copy == i)
1004 zelda_wave2->
lopst = zelda_wave2->
lopst << 15 / k;
1008 for (m = 0; m <
numinst; m++) {
1013 if (n == i ||
waves[n].copy == i) {
1033 k = (-zelda_wave->
end) & 15;
1034 d = zelda_wave->
buf;
1036 wtbl[(i << 1) + 1] = ((zelda_wave->
lopst + k) >> 4) * 9 + wtbl[i << 1];
1041 for (o = 0; o < 16; o++) {
1050 for (t = 0; t < 4; t++) {
1052 for (o = 0; o < 16; o++) {
1055 l += (y[o] *
fil3[t] >> 4) - (y[o + 1] *
fil1[t] >>
fil2[t]);
1066 for (q = 0; q < 12; q++, m += m)
1069 if (q && (q < 12 || m == r))
1070 v = (1 << (q - 1)) - 1;
1074 for (o = 0; o < 16; o++) {
1075 l = (y[o + 1] *
fil1[t] >>
fil2[t]) - (y[o] *
fil3[t] >> 4);
1077 r = (w - l + v) >> q;
1078 if ((r + 8) & 0xfff0) {
1090 u = 0, e = q, f = y[16], g = y[17];
1093 if (m < p) p = m, u = t, e = q, f = y[16], g = y[17];
1095 m = (e << 4) | (u << 2);
1096 if (n == zelda_wave->
end) m |= 1;
1097 if (zelda_wave->
lflag) m |= 2;
1101 for (o = 0; o < 16; o++) {
1108 if (n == zelda_wave->
end)
break;
1124 for (i = 0; i <
numwave; i++, zelda_wave++) {
1125 if (zelda_wave->
copy != -1) {
1126 wtbl[i << 1] = wtbl[zelda_wave->
copy << 1];
1127 wtbl[(i << 1) + 1] = (zelda_wave->
lopst >> 4) * 9 + wtbl[i << 1];
1144 bank_next[0] = 0x2880;
1145 bank_next[1] = 0xd000;
1146 bank_next[2] = 0xd000;
1147 bank_next[3] = 0xd000;
1148 bank_lwr[0] = 0x2880;
1150 for (k = 0; k < 4; k++) {
1152 for (i = 0; i <
ss_num; i++) {
1154 if (stbl->
bank != k)
continue;
1156 if (j + stbl->
len > 0xffc0) {
1164 if (j + stbl->
len > 0x3c00 && j < 0xd000) {
1165 printf(
"Not enough space for music bank %d", k);
1169 if (pstbl && (pstbl->
flag & 1) && (stbl->
flag & 2)) j--, pstbl->
len--;
1172 bank_next[k] = j + stbl->
len;
1175 for (i = 0; i <
ss_num; i++) {
1177 for (j = stbl->
relnum - 1; j >= 0; j--) {
1178 k = *(
unsigned short *)(stbl->
buf + stbl->
relocs[j]);
1179 for (l = 0; l <
ss_num; l++) {
1181 if (sptbl->
start <= k && sptbl->
len > k - sptbl->
start)
goto noerror;
1183 printf(
"Internal error");
1189 *(
unsigned short *)(stbl->
buf + stbl->
relocs[j]) =
1192 printf(
"An address outside the bank was referenced.\n");
1199 for (k = 0; k < 4; k++) {
1203 rom[0x918] = (l >> 8) | 128;
1204 rom[0x91c] = l >> 15;
1212 rom[0x936] = (l >> 8) | 128;
1213 rom[0x93a] = l >> 15;
1216 for (o = 0; o < 2; o++) {
1218 for (i = 0; i <
ss_num; i++) {
1220 if (!stbl)
continue;
1221 if ((stbl->
addr < 0xd000) ^ o)
continue;
1222 if (stbl->
bank != k)
continue;
1223 if (n + stbl->
len > ((k == 2) ? 0xdb7fc : 0xd74fc)) {
1224 printf(
"Not enough space for music");
1237 *(
short *)(rom.
mutable_data() + l + 2) = o ? bank_lwr[k] : 0xd000;
1243 if (k == 1) m = l + 4;