1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
/**
* SPDX-FileCopyrightText: Copyright (C) 2024 ZeldaRET
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "xml.h"
#include "aifc.h"
#include "samplebank.h"
#include "util.h"
NORETURN static void
usage(const char *progname)
{
fprintf(stderr, "Usage: %s [--matching] [--makedepend <out.d>] <in.xml> <out.s>\n", progname);
exit(EXIT_FAILURE);
}
int
main(int argc, char **argv)
{
static uint8_t match_buf[BUG_BUF_SIZE];
const char *filename = NULL;
xmlDocPtr document;
const char *outfilename = NULL;
const char *mdfilename = NULL;
FILE *mdfile;
FILE *outf;
samplebank sb;
uint8_t *match_buf_ptr;
size_t match_buf_pos;
bool matching = false;
// parse args
#define arg_error(fmt, ...) \
do { \
fprintf(stderr, fmt "\n", ##__VA_ARGS__); \
usage(argv[0]); \
} while (0)
int argn = 0;
for (int i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
// Optional args
if (strequ(argv[i], "--matching")) {
if (matching)
arg_error("Received --matching option twice");
matching = true;
continue;
}
if (strequ(argv[i], "--makedepend")) {
if (mdfilename != NULL)
arg_error("Received --makedepend option twice");
if (i + 1 == argc)
arg_error("--makedepend missing required argument");
mdfilename = argv[++i];
continue;
}
arg_error("Unknown option \"%s\"", argv[i]);
} else {
// Required args
switch (argn) {
case 0:
filename = argv[i];
break;
case 1:
outfilename = argv[i];
break;
default:
arg_error("Unknown positional argument \"%s\"", argv[i]);
break;
}
argn++;
}
}
if (argn != 2)
arg_error("Not enough positional arguments");
#undef arg_error
// open xml
document = xmlReadFile(filename, NULL, XML_PARSE_NONET);
if (document == NULL)
return EXIT_FAILURE;
// parse xml
read_samplebank_xml(&sb, document);
// open output asm file
outf = fopen(outfilename, "w");
if (outf == NULL)
error("Unable to open output file [%s] for writing", outfilename);
// open output dep file if applicable
if (mdfilename != NULL) {
mdfile = fopen(mdfilename, "w");
if (mdfile == NULL)
error("Unable to open dependency file [%s] for writing", mdfilename);
fprintf(mdfile, "%s: \\\n %s", outfilename, filename);
}
// write output
fprintf(outf,
// clang-format off
".rdata" "\n"
".balign 16" "\n"
"\n"
".global %s_Start" "\n"
"%s_Start:" "\n"
"$start:" "\n",
// clang-format on
sb.name, sb.name);
// original tool appears to have a buffer clearing bug involving a buffer sized BUG_BUF_SIZE
match_buf_ptr = (matching && sb.buffer_bug) ? match_buf : NULL;
match_buf_pos = 0;
for (size_t i = 0; i < sb.num_samples; i++) {
const char *name = sb.sample_names[i];
const char *path = sb.sample_paths[i];
bool is_sample = sb.is_sample[i];
if (mdfilename != NULL)
fprintf(mdfile, " \\\n %s", path);
if (!is_sample) {
// blob
fprintf(outf,
// clang-format off
"\n"
"# BLOB %s" "\n"
"\n"
".incbin \"%s\"" "\n"
"\n"
".balign 16" "\n"
"\n",
// clang-format on
name, path);
continue;
}
// aifc sample
fprintf(outf,
// clang-format off
"\n"
"# SAMPLE %lu" "\n"
"\n"
".global %s_%s_Abs" "\n"
"%s_%s_Abs:" "\n"
".global %s_%s_Off" "\n"
".set %s_%s_Off, . - $start" "\n"
"\n",
// clang-format on
i, sb.name, name, sb.name, name, sb.name, name, sb.name, name);
aifc_data aifc;
aifc_read(&aifc, path, match_buf_ptr, &match_buf_pos);
fprintf(outf, ".incbin \"%s\", 0x%lX, 0x%lX\n", path, aifc.ssnd_offset, aifc.ssnd_size);
if (match_buf_ptr != NULL && i == sb.num_samples - 1) {
// emplace garbage
fprintf(outf, "\n# Garbage data from buffer bug\n");
size_t end = ALIGN16(match_buf_pos);
for (; match_buf_pos < end; match_buf_pos++)
fprintf(outf, ".byte 0x%02X\n", match_buf_ptr[match_buf_pos]);
} else {
fputs("\n.balign 16\n", outf);
}
aifc_dispose(&aifc);
}
if (mdfilename != NULL) {
fputs("\n", mdfile);
fclose(mdfile);
}
fprintf(outf,
// clang-format off
".global %s_Size" "\n"
".set %s_Size, . - $start" "\n",
// clang-format on
sb.name, sb.name);
fclose(outf);
xmlFreeDoc(document);
return EXIT_SUCCESS;
}
|