-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.rs
290 lines (240 loc) · 9.73 KB
/
build.rs
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//! Audio Driver build script
// SPDX-FileCopyrightText: © 2023 Marcus Rowe <undisbeliever@gmail.com>
//
// SPDX-License-Identifier: MIT
use std::collections::HashMap;
use std::ffi::OsStr;
use std::fmt::Write;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::{env, fs};
use regex::Regex;
const LOADER: &str = "loader";
const AUDIO_DRIVER: &str = "audio-driver";
const BLANK_SONG: &str = "blank-song";
const COMMON_SYMBOLS: &[&str] = &["__loader_dataType", "__loader_songPtr"];
const LOADER_SYMBOLS: &[(&str, &str)] = &[("start_loader", "LOADER")];
const IO_COMMANDS_FILE: &str = "io-commands.wiz";
const IO_COMMANDS_VERSION_REGEX: &str = r"\nlet TAD_IO_VERSION = ([0-9]+);";
const TAD_IO_VERSION: &str = "TAD_IO_VERSION";
#[rustfmt::skip]
const AUDIO_DRIVER_SYMBOLS: &[(&str, &str)] = &[
("main", "DRIVER_CODE"),
("mainloop", "MAINLOOP_CODE"),
("process_music_channels", "PROCESS_MUSIC_CHANNELS_CODE"),
("__loader_songPtr", "SONG_PTR"),
("__loader_dataType", "LOADER_DATA_TYPE"),
("songTickCounter", "SONG_TICK_COUNTER"),
("keyOnShadow_music", "KEYON_SHADOW_MUSIC"),
("keyOffShadow_music", "KEYOFF_SHADOW_MUSIC"),
("keyOnMask_music", "KEYON_MASK_MUSIC"),
("pmonShadow", "PMON_SHADOW"),
("eonShadow_music", "EON_SHADOW_MUSIC"),
("eonShadow_sfx", "EON_SHADOW_SFX"),
("io_musicChannelsMask", "IO_MUSIC_CHANNELS_MASK"),
("__bcStack", "BYTECODE_STACK"),
("voiceChannelsDirty_music", "VOICE_CHANNELS_DIRTY_MUSIC"),
("channelSoA.virtualChannels.vol_l", "CHANNEL_VC_VOL_L"),
("channelSoA.virtualChannels.vol_r", "CHANNEL_VC_VOL_R"),
("channelSoA.virtualChannels.pitch_l", "CHANNEL_VC_PITCH_L"),
("channelSoA.virtualChannels.pitch_h", "CHANNEL_VC_PITCH_H"),
("channelSoA.virtualChannels.scrn", "CHANNEL_VC_SCRN"),
("channelSoA.virtualChannels.adsr1", "CHANNEL_VC_ADSR1"),
("channelSoA.virtualChannels.adsr2OrGain", "CHANNEL_VC_ADSR2_OR_GAIN"),
("channelSoA.virtualChannels.tempGain", "CHANNEL_VC_TEMP_GAIN"),
("channelSoA.countdownTimer", "CHANNEL_COUNTDOWN_TIMER"),
("channelSoA.instPitchOffset", "CHANNEL_INST_PITCH_OFFSET"),
("channelSoA.instructionPtr_l", "CHANNEL_INSTRUCTION_PTR_L"),
("channelSoA.instructionPtr_h", "CHANNEL_INSTRUCTION_PTR_H"),
("channelSoA.stackPointer", "CHANNEL_STACK_POINTER"),
("channelSoA.loopStackPointer", "CHANNEL_LOOP_STACK_POINTER"),
("channelSoA.nextEventIsKeyOff", "CHANNEL_NEXT_EVENT_IS_KEY_OFF"),
("channelSoA.portamento_direction", "CHANNEL_DIRECTION"),
("channelSoA.vibrato_pitchOffsetPerTick", "CHANNEL_VIBRATO_PITCH_OFFSET_PER_TICK"),
("channelSoA.vibrato_direction", "CHANNEL_VIBRATO_DIRECTION"),
("channelSoA.vibrato_tickCounter", "CHANNEL_VIBRATO_TICK_COUNTER"),
("channelSoA.vibrato_tickCounterStart", "CHANNEL_VIBRATO_TICK_COUNTER_START"),
("channelSoA.vibrato_halfWavelength", "CHANNEL_VIBRATO_HALF_WAVELENGTH"),
("channelSoA.prevTempGain", "CHANNEL_PREV_TEMP_GAIN"),
("channelSoA.earlyRelease_cmp", "CHANNEL_EARLY_RELEASE_CMP"),
("channelSoA.earlyRelease_minTicks", "CHANNEL_EARLY_RELEASE_MIN_TICKS"),
("channelSoA.earlyRelease_gain", "CHANNEL_EARLY_RELEASE_GAIN"),
("channelSoA.detune_l", "CHANNEL_DETUNE_L"),
("channelSoA.detune_h", "CHANNEL_DETUNE_H"),
("channelSoA.volume", "CHANNEL_VOLUME"),
("channelSoA.subVolume", "CHANNEL_SUB_VOLUME"),
("channelSoA.volEffect_direction", "CHANNEL_VOL_EFFECT_DIRECTION"),
("channelSoA.volEffect_offset_l", "CHANNEL_VOL_EFFECT_OFFSET_L"),
("channelSoA.volEffect_offset_h", "CHANNEL_VOL_EFFECT_OFFSET_H"),
("channelSoA.volEffect_counter", "CHANNEL_VOL_EFFECT_COUNTER"),
("channelSoA.volEffect_halfWavelength", "CHANNEL_VOL_EFFECT_HALF_WAVELENGTH"),
("channelSoA.pan", "CHANNEL_PAN"),
("channelSoA.subPan", "CHANNEL_SUB_PAN"),
("channelSoA.panEffect_direction", "CHANNEL_PAN_EFFECT_DIRECTION"),
("channelSoA.panEffect_offset_l", "CHANNEL_PAN_EFFECT_OFFSET_L"),
("channelSoA.panEffect_offset_h", "CHANNEL_PAN_EFFECT_OFFSET_H"),
("channelSoA.panEffect_counter", "CHANNEL_PAN_EFFECT_COUNTER"),
("channelSoA.panEffect_halfWavelength", "CHANNEL_PAN_EFFECT_HALF_WAVELENGTH"),
("channelSoA.invertFlags", "CHANNEL_INVERT_FLAGS"),
("echo", "ECHO_VARIABLES"),
("echoDirty", "ECHO_DIRTY"),
("maxEdl", "MAX_EDL"),
];
fn find_wiz_binary() -> PathBuf {
// Using join to ensure directory separators are correct
let wiz_exe = Path::new("wiz").join("bin").join("wiz");
#[cfg(windows)]
let wiz_exe = wiz_exe.with_extension("exe");
let workspace_dir = Path::new("..").join("..");
let wiz_bin = workspace_dir.join(&wiz_exe);
if !wiz_bin.is_file() {
panic!(
r##"Cannot find wiz binary. Please import the wiz git submodule and compile wiz.
This build script is looking for an executable at "{}"."##,
wiz_exe.display()
);
}
wiz_bin
}
fn read_sym_line(s: &str) -> Option<(&str, u16)> {
if s.starts_with('[') {
return None;
}
let (addr, name) = s.split_once(' ')?;
let (bank, addr) = addr.split_once(':')?;
match usize::from_str_radix(bank, 16) {
Ok(0) => (),
_ => panic!("Error in symbol file: {} bank is not 0", name),
}
match u16::from_str_radix(addr, 16) {
Ok(a) => Some((name, a)),
Err(_) => panic!("Error in symbol file: {} is not 16 bit", name),
}
}
fn read_symbol_file<'a>(name: &'a str, sym_file: &'a str) -> Symbols<'a> {
Symbols {
name,
symbols: sym_file.lines().filter_map(read_sym_line).collect(),
}
}
struct Symbols<'a> {
name: &'a str,
symbols: HashMap<&'a str, u16>,
}
impl Symbols<'_> {
fn get(&self, symbol: &str) -> u16 {
match self.symbols.get(symbol) {
Some(addr) => *addr,
None => panic!("Cannot find {} symbol: {}", self.name, symbol),
}
}
fn last_addr(&self) -> u16 {
self.symbols.values().max().cloned().unwrap_or(0)
}
}
fn assert_valid_const_name(name: &str) {
if !name
.chars()
.all(|c| c.is_ascii_uppercase() || c.is_ascii_digit() || c == '_')
{
panic!("Invalid const name: {name}");
}
}
fn read_tad_io_version(audio_driver_dir: &Path) -> usize {
let path = audio_driver_dir.join(IO_COMMANDS_FILE);
let io_commands = match fs::read_to_string(&path) {
Ok(s) => s,
Err(e) => panic!("Error reading file {}: {}", path.display(), e),
};
let re = Regex::new(IO_COMMANDS_VERSION_REGEX).unwrap();
match re.captures(&io_commands) {
Some(c) => c[1].parse().unwrap(),
None => panic!(
"Could not find {TAD_IO_VERSION} const in {}.",
path.display()
),
}
}
fn build_rust_consts(
symbols: Symbols,
const_list: &[(&str, &str)],
last_symbol_name: &str,
) -> String {
let mut out = String::new();
for (s, const_name) in const_list {
let addr: u16 = symbols.get(s);
assert_valid_const_name(const_name);
writeln!(out, "pub(crate) const {} : u16 = 0x{:x};", const_name, addr).unwrap();
}
let last_addr = symbols.last_addr();
assert_valid_const_name(last_symbol_name);
writeln!(
out,
"pub(crate) const {} : u16 = 0x{:x};",
last_symbol_name, last_addr
)
.unwrap();
out
}
struct AudioDriverCompiler {
audio_driver_dir: PathBuf,
out_dir: PathBuf,
wiz_bin: PathBuf,
}
impl AudioDriverCompiler {
fn compile(&self, name: &OsStr) {
let in_path = self.audio_driver_dir.join(name).with_extension("wiz");
let out_path = self.out_dir.join(name).with_extension("bin");
let status = Command::new(&self.wiz_bin)
.args(["--system=spc700", "-s", "wla", "-o"])
.arg(&out_path)
.arg(&in_path)
.status()
.expect("failed to execute wiz");
if !status.success() {
panic!("Error compiling {}: {}", in_path.display(), status);
}
}
fn load_sym_file(&self, name: &OsStr) -> String {
let sym_path = self.out_dir.join(name).with_extension("sym");
match fs::read_to_string(&sym_path) {
Ok(s) => s,
Err(e) => panic!("Error reading symbol file {}: {}", sym_path.display(), e),
}
}
}
fn main() {
let wiz = AudioDriverCompiler {
audio_driver_dir: "../../audio-driver/src".into(),
out_dir: env::var_os("OUT_DIR").unwrap().into(),
wiz_bin: find_wiz_binary(),
};
wiz.compile(LOADER.as_ref());
wiz.compile(AUDIO_DRIVER.as_ref());
wiz.compile(BLANK_SONG.as_ref());
let loader_sym_file = wiz.load_sym_file(LOADER.as_ref());
let driver_sym_file = wiz.load_sym_file(AUDIO_DRIVER.as_ref());
let loader_symbols = read_symbol_file("loader", &loader_sym_file);
let driver_symbols = read_symbol_file("audio-driver", &driver_sym_file);
let tad_io_version = read_tad_io_version(&wiz.audio_driver_dir);
// confirm common variables are the same in the loader and driver
for label in COMMON_SYMBOLS {
assert_eq!(
loader_symbols.get(label),
driver_symbols.get(label),
"symbol mismatch between loader and driver: {label}"
);
}
// Generate symbols.rs
let symbols_rs_path = wiz.out_dir.join("symbols.rs");
let rust_symbol_consts = [
"// autogenerated by build.rs\n\n",
&format!("pub(crate) const {TAD_IO_VERSION} : usize = {tad_io_version};\n\n"),
&build_rust_consts(loader_symbols, LOADER_SYMBOLS, "_LAST_LOADER_SYMBOL"),
&build_rust_consts(driver_symbols, AUDIO_DRIVER_SYMBOLS, "_LAST_DRIVER_SYMBOL"),
]
.concat();
fs::write(symbols_rs_path, rust_symbol_consts.as_bytes()).unwrap();
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed={}", wiz.audio_driver_dir.display());
}