135 if (l == -1)
return 0;
138 if (spc_command[l].flag & 4) {
139 j = spc_command[l].
tim;
140 m = spc_command[l].
tim2;
146 if (spc_command[l].flag & 1) n = spc_command[l].
b1;
148 l = spc_command[l].
next;
167 spc_command2 = spc_command + i;
168 if (spc_command2->
cmd == 0xef) {
169 k = *(
short *)&(spc_command2->
p1);
171 printf(
"Invalid music address\n");
175 if (spc_command2->
flag & 1) {
179 j += spc_command[k].
tim2 * spc_command2->
b1;
180 j += (spc_command2->
p3 - 1) * spc_command[k].tim2 *
ss_lasttime;
183 spc_command2->
b1 * (m + spc_command[k].
tim2 * spc_command2->
p3);
190 j += (spc_command2->
p3 - 1) *
ss_lasttime * spc_command[k].tim2,
191 m = spc_command[k].tim2;
193 m += spc_command[k].
tim2 * spc_command2->
p3;
196 if (spc_command2->
cmd < 0xe0) m++;
197 if (spc_command2->
flag & 1) {
198 j += m * spc_command[i].
b1;
202 spc_command2->
tim = j;
203 spc_command2->
tim2 = m;
204 spc_command2->
flag |= 4;
208 i = spc_command2->
prev;
212 return spc_command[num].
tim + prevtime * spc_command[num].
tim2;
235 unsigned char *a =
nullptr;
240 if (!addr)
return -1;
245 printf(
"Address not found when loading track");
252 for (c = 0; c < e; c++) {
253 if (sr[c].bank == d) {
254 if (sr[c].start > addr) {
255 if (sr[c].start < f) f = sr[c].
start;
257 }
else if (sr[c].end > addr) {
258 for (f = sr[c].first; f != -1; f = spc_command[f].
next) {
259 if (spc_command[f].flag & 4)
260 m = spc_command[f].
tim, o = spc_command[f].
tim2;
261 if (spc_command[f].addr == addr) {
262 spc_command[f].
tim = m;
263 spc_command[f].
tim2 = o;
267 if (spc_command[f].flag & 1) k = spc_command[f].
b1;
268 if (spc_command[f].cmd < 0xca) {
276 printf(
"Misaligned music pointer");
289 for (g = addr; g < f;) {
290 spc_command2 = spc_command + i;
291 if (spc_command2->
next == -1) {
295 spc_command2 = spc_command + i;
297 while (l < n) spc_command[l].
next = l + 1, l++;
298 spc_command[l].
next = -1;
300 while (l > n) spc_command[l].
prev = l - 1, l--;
301 spc_command[l].
prev = i;
302 spc_command2->
next = l;
304 spc_command2->
addr = g;
307 if (m >= t && b != 0xf9)
break;
312 k = spc_command2->
b1 = b;
314 if (b < 128) j = 3, spc_command2->
b2 = b, b = a[g++];
323 spc_command2->
cmd = b;
324 spc_command2->
flag = j;
327 if (
op_len[b]) spc_command2->
p1 = a[g++];
328 if (
op_len[b] > 1) spc_command2->
p2 = a[g++];
329 if (
op_len[b] > 2) spc_command2->
p3 = a[g++];
332 spc_command[spc_command2->
next].
prev = -1;
335 spc_command2 = spc_command + i;
336 *(
short *)(&(spc_command2->
p1)) = l;
338 spc_command[spc_command2->
next].
prev = i;
340 if (spc_command[l].flag & 4)
342 (spc_command[l].
tim + spc_command[l].
tim2 * k) * spc_command2->
p3;
344 i = spc_command2->
next;
350 i = spc_command2->
next;
353 spc_command[h].
tim = m;
354 spc_command[h].
tim2 = o;
355 spc_command[h].
flag |= 4;
357 if (f == g && m < t) {
358 l = spc_command[i].
prev;
362 if (spc_command[l].flag & 4)
363 spc_command[h].
tim = spc_command[l].
tim + m + spc_command[l].
tim2 * k,
364 spc_command[h].
flag |= 4;
382 spc_command[spc_command[i].
prev].
next = -1;
385 spc_command[i].
prev = -1;
430 sbank_ofs[1] = (b[0x91c] << 15) + ((b[0x918] & 127) << 8) + b[0x914];
431 sbank_ofs[3] = (b[0x93a] << 15) + ((b[0x936] & 127) << 8) + b[0x932];
433 for (i = 0; i < 1024; i++) {
434 spc_command[i].
next = i + 1;
435 spc_command[i].
prev = i - 1;
439 for (i = 0; i < 128; i++) {
441 songs.emplace_back(new_song);
444 spc_command[1023].
next = -1;
445 for (i = 0; i < 3; i++) {
449 if ((r = ((
unsigned short *)b)[j]) >= 0xd000) {
450 r = (r - 0xd000) >> 1;
456 for (j = 0; j < r; j++) {
457 k = ((
unsigned short *)b)[j];
459 songs[l].in_use =
false;
484 if ((n = ((
unsigned short *)c)[m]) < 256)
break;
488 song.
lopst = (((
unsigned short *)c)[m + 1] - k) >> 1;
494 for (m = 0; m < song.
numparts; m++) {
495 k = ((
unsigned short *)c)[m];
505 for (o = 0; o < song2.
numparts; o++)
507 (song.
tbl[m] = song2.
tbl[o])->inst++;
512 for (o = 0; o < m; o++)
514 (song.
tbl[m] = song.
tbl[o])->inst++;
523 for (o = 0; o < 8; o++) {
527 if ((spc_command->
flag & 4) && spc_command->
tim < p)
528 p = spc_command->
tim;
563 for (i = 0; i < p; i += 2) {
564 j = ((
unsigned short *)b)[i];
566 if (j == 65535)
break;
568 for (k = 0; k < i; k += 2) {
569 if (((
unsigned short *)b)[k] == j) {
570 zelda_wave->
copy = (short)(k >> 1);
575 zelda_wave->
copy = -1;
580 e = (
short *)malloc(2048);
589 range = (m >> 4) + 8;
590 filter = (m & 12) >> 2;
592 for (n = 0; n < 8; n++) {
600 o += (t *
fil1[filter] >>
fil2[filter]) -
601 ((u & -256) *
fil3[filter] >> 4);
603 if (o > 0x7fffff) o = 0x7fffff;
605 if (o < -0x800000) o = -0x800000;
621 o += (u *
fil1[filter] >>
fil2[filter]) -
622 ((t & -256) *
fil3[filter] >> 4);
624 if (o > 0x7fffff) o = 0x7fffff;
626 if (o < -0x800000) o = -0x800000;
635 zelda_wave->
lflag = (m & 2) >> 1;
640 e = (
short *)realloc(e, l << 1);
644 e = zelda_wave->
buf = (
short *)realloc(e, (k + 1) << 1);
646 zelda_wave->
lopst = (((
unsigned short *)b)[i + 1] - j) * 16 / 9;
648 if (zelda_wave->
lflag)
649 e[k] = e[zelda_wave->
lopst];
682 if (i == -1)
return 0;
690 if (spc_command[i].flag & 8)
return spc_command[i].
addr;
693 j = spc_command[i].
prev;
698 for (j = 0; j <
srnum; j++) {
699 if (sr[j].first == i) {
705 spc_command2 = spc_command + m;
706 if (spc_command2->
flag & 1) k++, n = spc_command2->
b1;
707 if (spc_command2->
flag & 2) k++;
708 if (spc_command2->
cmd >= 0xe0) k +=
op_len[spc_command2->
cmd - 0xe0];
709 m = spc_command2->
next;
714 l = (songtime + 126) / 127;
715 if (songtime % l) l += 2;
717 if (n && !songtime % n) {
730 spc_command2 = spc_command + i;
732 spc_command2->
flag |= 8;
733 if (spc_command2->
flag & 1) *(b++) = spc_command2->
b1;
734 if (spc_command2->
flag & 2) *(b++) = spc_command2->
b2;
735 *(b++) = spc_command2->
cmd;
736 if (spc_command2->
cmd >= 0xe0) {
738 if (spc_command2->
cmd == 0xef) {
741 if (b) AddSPCReloc(sbl, b - sbl->
buf);
742 b[2] = spc_command2->
p3;
745 if (o) *(b++) = spc_command2->
p1;
746 if (o > 1) *(b++) = spc_command2->
p2;
747 if (o > 2) *(b++) = spc_command2->
p3;
750 i = spc_command2->
next;
755 l = (songtime + 126) / 127;
763 for (; songtime >= n; songtime -= n) *(b++) = 0xc9;
766 *(b++) = (uint8_t)songtime;
771 return spc_command[num].
addr;
775 printf(
"Address %04X not found", num);
788 unsigned char *rom_data = rom.
data();
790 if (!len)
return addr;
792 if (((addr + len + 4) & 0x7fff) > 0x7ffb) {
793 if (addr + 5 > limit)
goto error;
794 *(
int *)(rom_data + addr) = 0x00010140;
795 rom_data[addr + 4] = 0xff;
799 if (addr + len + 4 > limit) {
801 printf(
"Not enough space for sound data");
806 *(
short *)(rom_data + addr) = len;
807 *(
short *)(rom_data + addr + 2) = spc;
809 memcpy(rom_data + addr + 4, buf, len);
811 return addr + len + 4;
835 unsigned short bank_next[4];
836 unsigned short bank_lwr[4];
839 unsigned char *rom_data;
876 rom_data = rom.
data();
880 for (i = 0; i < 3; i++) {
883 for (j = 0; j < k; j++) {
886 if (!song.
in_use)
continue;
890 for (m = 0; m < song.
numparts; m++) {
900 for (i = 0; i < j; i++) {
901 spc_command->
flag &= -13;
907 for (i = 0; i < 3; i++) {
911 for (j = 0; j < k; j++) {
915 ((
short *)(stbl->
buf))[j] = 0;
920 if (song.
flag & 4)
goto alreadysaved;
923 (song.
flag & 1) ? 0 : (i + 1));
925 for (m = 0; m < song.
numparts; m++) {
928 if (sp->
flag & 2)
goto spsaved;
934 for (n = 0; n < 8; n++) {
942 for (n = 0; n < 8; n++) {
951 ((
short *)(sptbl->
buf))[m] = sp->
addr;
953 AddSPCReloc(sptbl, m << 1);
957 ((
short *)(sptbl->
buf))[m++] = 255;
958 ((
short *)(sptbl->
buf))[m] = sptbl->
start + (song.
lopst << 1);
960 AddSPCReloc(sptbl, m << 1);
962 ((
short *)(sptbl->
buf))[m++] = 0;
967 ((
short *)(stbl->
buf))[j] = song.
addr;
969 AddSPCReloc(stbl, j << 1);
974 b = (uint8_t *)malloc(0xc000);
984 for (i = 0; i <
numwave; i++, zelda_wave++) {
985 if (zelda_wave->
copy != -1)
continue;
987 wtbl[i << 1] = j + 0x4000;
989 if (zelda_wave->
lflag) {
990 l = zelda_wave->
end - zelda_wave->
lopst;
993 k = (l << 15) / ((l + 15) & -16);
994 p = (zelda_wave->
end << 15) / k;
995 c = (
short *)malloc(p << 1);
1000 c[n++] = (d[m >> 15] * ((m & 32767) ^ 32767) +
1001 d[(m >> 15) + 1] * (m & 32767)) /
1009 zelda_wave->
lopst = (zelda_wave->
lopst << 15) / k;
1010 zelda_wave->
end = p;
1012 (
short *)realloc(zelda_wave->
buf, (zelda_wave->
end + 1) << 1);
1013 memcpy(zelda_wave->
buf, c, zelda_wave->
end << 1);
1015 zelda_wave->
buf[zelda_wave->
end] = zelda_wave->
buf[zelda_wave->
lopst];
1016 zelda_wave2 =
waves;
1018 for (m = 0; m <
numwave; m++, zelda_wave2++)
1019 if (zelda_wave2->
copy == i)
1020 zelda_wave2->
lopst = zelda_wave2->
lopst << 15 / k;
1024 for (m = 0; m <
numinst; m++) {
1029 if (n == i ||
waves[n].copy == i) {
1049 k = (-zelda_wave->
end) & 15;
1050 d = zelda_wave->
buf;
1052 wtbl[(i << 1) + 1] = ((zelda_wave->
lopst + k) >> 4) * 9 + wtbl[i << 1];
1057 for (o = 0; o < 16; o++) {
1066 for (t = 0; t < 4; t++) {
1068 for (o = 0; o < 16; o++) {
1071 l += (y[o] *
fil3[t] >> 4) - (y[o + 1] *
fil1[t] >>
fil2[t]);
1082 for (q = 0; q < 12; q++, m += m)
1085 if (q && (q < 12 || m == r))
1086 v = (1 << (q - 1)) - 1;
1090 for (o = 0; o < 16; o++) {
1091 l = (y[o + 1] *
fil1[t] >>
fil2[t]) - (y[o] *
fil3[t] >> 4);
1093 r = (w - l + v) >> q;
1094 if ((r + 8) & 0xfff0) {
1106 u = 0, e = q, f = y[16], g = y[17];
1109 if (m < p) p = m, u = t, e = q, f = y[16], g = y[17];
1111 m = (e << 4) | (u << 2);
1112 if (n == zelda_wave->
end) m |= 1;
1113 if (zelda_wave->
lflag) m |= 2;
1117 for (o = 0; o < 16; o++) {
1124 if (n == zelda_wave->
end)
break;
1140 for (i = 0; i <
numwave; i++, zelda_wave++) {
1141 if (zelda_wave->
copy != -1) {
1142 wtbl[i << 1] = wtbl[zelda_wave->
copy << 1];
1143 wtbl[(i << 1) + 1] = (zelda_wave->
lopst >> 4) * 9 + wtbl[i << 1];
1160 bank_next[0] = 0x2880;
1161 bank_next[1] = 0xd000;
1162 bank_next[2] = 0xd000;
1163 bank_next[3] = 0xd000;
1164 bank_lwr[0] = 0x2880;
1166 for (k = 0; k < 4; k++) {
1168 for (i = 0; i <
ss_num; i++) {
1170 if (stbl->
bank != k)
continue;
1172 if (j + stbl->
len > 0xffc0) {
1180 if (j + stbl->
len > 0x3c00 && j < 0xd000) {
1181 printf(
"Not enough space for music bank %d", k);
1185 if (pstbl && (pstbl->
flag & 1) && (stbl->
flag & 2)) j--, pstbl->
len--;
1188 bank_next[k] = j + stbl->
len;
1191 for (i = 0; i <
ss_num; i++) {
1193 for (j = stbl->
relnum - 1; j >= 0; j--) {
1194 k = *(
unsigned short *)(stbl->
buf + stbl->
relocs[j]);
1195 for (l = 0; l <
ss_num; l++) {
1197 if (sptbl->
start <= k && sptbl->
len > k - sptbl->
start)
goto noerror;
1199 printf(
"Internal error");
1205 *(
unsigned short *)(stbl->
buf + stbl->
relocs[j]) =
1208 printf(
"An address outside the bank was referenced.\n");
1215 for (k = 0; k < 4; k++) {
1219 rom[0x918] = (l >> 8) | 128;
1220 rom[0x91c] = l >> 15;
1228 rom[0x936] = (l >> 8) | 128;
1229 rom[0x93a] = l >> 15;
1232 for (o = 0; o < 2; o++) {
1234 for (i = 0; i <
ss_num; i++) {
1236 if (!stbl)
continue;
1237 if ((stbl->
addr < 0xd000) ^ o)
continue;
1238 if (stbl->
bank != k)
continue;
1239 if (n + stbl->
len > ((k == 2) ? 0xdb7fc : 0xd74fc)) {
1240 printf(
"Not enough space for music");
1244 memcpy(rom.
data() + n, stbl->
buf, stbl->
len);
1252 *(
short *)(rom.
data() + l) = n - l - 4;
1253 *(
short *)(rom.
data() + l + 2) = o ? bank_lwr[k] : 0xd000;
1257 *(
short *)(rom.
data() + l) = 0;
1258 *(
short *)(rom.
data() + l + 2) = 0x800;
1259 if (k == 1) m = l + 4;