gbsdiff 1.0.1

A tool for highlighting the differences in the execution of two GBS files.
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
 * 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 https://mozilla.org/MPL/2.0/.
 */

use std::{
    cmp::Ordering,
    fmt::{Display, LowerHex},
    fs::{self, File},
    io,
    str::FromStr,
};

use argh::FromArgs;
use owo_colors::{
    OwoColorize,
    Stream::{Stderr, Stdout},
};
use slicedisplay::SliceDisplay;

mod diff;
mod gbs;
use gbs::Gbs;
mod run;

const CYCLES_PER_SEC: u32 = 1048576;

#[derive(FromArgs)]
/// Analyze differences in audio register writes between two GBS files.
struct Args {
    #[argh(option, short = 'l', default = "DiagnosticLevel::Warning")]
    /// silence diagnostics with a higher level than this (default: warning)
    max_level: DiagnosticLevel,
    #[argh(option, short = 'm', default = "1000")]
    /// how many differences to report per song, at most (default: 1000)
    max_reports: usize,
    #[argh(option, short = 't', default = "60")]
    /// time out simulation of a song after this many seconds (default: 60)
    timeout: u16,
    #[argh(switch, short = 'T')]
    /// make timeout non-fatal (useful for looping tracks)
    allow_timeout: bool,
    #[argh(option, short = 's', default = "4")]
    /// consider that a song ended after this many seconds of silence (default: 4)
    slience_timeout: u8,
    #[argh(option, short = 'w', from_str_fn(parse_watch_arg))]
    /// consider that a song ended when `ADDR=VALUE` (both hex numbers)
    watch: Option<(u16, u8)>,
    #[argh(option)]
    /// log CPU activity to this file (significant slowdown)
    trace: Option<String>,
    #[argh(option, short = 'd', default = "BeforeOrAfter::After")]
    /// print the diagnostics of either the "before" GBS, the "after" one, or "none" (default: after)
    print_diagnostics: BeforeOrAfter,
    #[argh(option, short = 'j', default = "20")]
    /// identical IO writes displaced by strictly less cycles than this will be treated as notes instead of errors (default: 20)
    jitter: u16,
    #[argh(option, default = "None", from_str_fn(parse_color_arg))]
    /// whether to colorize output: auto (default), always, never
    color: Option<bool>,

    #[argh(positional)]
    /// path to the GBS file that was built before the changes
    before: String,
    #[argh(positional)]
    /// path to the GBS file that was built after the changes
    after: String,
}
fn main() {
    let args: Args = argh::from_env();
    let timeout = u32::from(args.timeout) * CYCLES_PER_SEC;
    let silence_timeout = u32::from(args.slience_timeout) * CYCLES_PER_SEC;
    let mut trace_file = args.trace.map(|path| {
        File::create(path).unwrap_or_else(|err| {
            eprintln!("Failed to open trace file: {}", err);
            std::process::exit(2);
        })
    });

    if let Some(args_color) = args.color {
        owo_colors::set_override(args_color)
    }
    macro_rules! colorize {
        ($stream:expr, $base:expr, $($func:ident),+ $(,)?) => {
            ($base $(.if_supports_color($stream, |text| text.$func()))+)
        };
    }

    let read_file = |path| {
        println!(
            "{} {} {}...",
            colorize!(Stdout, "==>", bold),
            colorize!(Stdout, "Reading", bright_cyan, bold),
            &path
        );

        fs::read(&path).unwrap_or_else(|err| {
            eprintln!(
                "{} while reading {}: {}",
                colorize!(Stderr, "Error", bright_red, bold),
                path,
                err
            );
            std::process::exit(2);
        })
    };
    let parse_gbs = |data, path| {
        Gbs::new(data).unwrap_or_else(|err| {
            eprintln!(
                "{} parsing {}: {}",
                colorize!(Stderr, "Error", bright_red, bold),
                path,
                err
            );
            std::process::exit(2);
        })
    };
    let before_data = read_file(&args.before);
    let before_gbs = parse_gbs(&before_data, &args.before);
    let after_data = read_file(&args.after);
    let after_gbs = parse_gbs(&after_data, &args.after);

    let nb_songs = std::cmp::min(before_gbs.nb_songs(), after_gbs.nb_songs());
    if before_gbs.nb_songs() != after_gbs.nb_songs() {
        println!(
            "{}: Earlier GBS has {} songs, later has {}; only comparing first {}",
            colorize!(Stdout, "warning", bright_yellow, bold),
            before_gbs.nb_songs(),
            after_gbs.nb_songs(),
            nb_songs,
        );
    }

    let mut failed = Vec::new();
    for i in 0..nb_songs {
        let song_ids = (i + before_gbs.first_song(), i + after_gbs.first_song());

        println!(
            "{} {} songs {}...",
            colorize!(Stdout, "==>", bold),
            colorize!(Stdout, "Simulating", bright_cyan, bold),
            SongIDs(song_ids),
        );
        macro_rules! simulate {
            ($gbs:expr, $song_id:expr, $path:expr) => {
                match run::simulate_song(
                    $gbs,
                    $song_id,
                    args.max_level,
                    timeout,
                    args.allow_timeout,
                    silence_timeout,
                    args.watch,
                    trace_file.as_mut(),
                ) {
                    Ok(log) => log,
                    Err(err) => {
                        println!(
                            "{} to simulate {} song #{}: {}",
                            colorize!(Stdout, "Failed", bold, bright_red),
                            $path,
                            $song_id,
                            err
                        );
                        failed.push(SongIDs(song_ids));
                        continue;
                    }
                }
            };
        }
        let logs = (
            simulate!(&before_gbs, song_ids.0, args.before),
            simulate!(&after_gbs, song_ids.1, args.after),
        );

        println!(
            "{} {} songs {}...",
            colorize!(Stdout, "==>", bold),
            colorize!(Stdout, "Comparing", bright_cyan, bold),
            SongIDs(song_ids),
        );

        let mut ok = true;
        let mut tick = u64::MAX;
        let mut diagnostics = match args.print_diagnostics {
            BeforeOrAfter::Before => Some(&logs.0),
            BeforeOrAfter::After => Some(&logs.1),
            BeforeOrAfter::None => None,
        }
        .map(|logs| logs.diagnostics.iter().peekable());

        let print_tick = |tick| {
            println!(
                "{} Tick {} {}",
                colorize!(Stdout, "====", bold),
                tick,
                colorize!(Stdout, "====", bold)
            )
        };
        let mut i = 0;
        macro_rules! report {
            ($diag:expr $(, $label:tt)?) => {
                println!(
                    "{} on cycle {} (PC = ${:04x}): {}",
                    $diag.level, $diag.when.cycle, $diag.pc, $diag.kind
                );
                i += 1;
                if i == args.max_reports {
                    println!(
                        "...stopping at {} diagnostics. Go fix your code!",
                        args.max_reports
                    );
                    break $($label)?;
                }
            };
        }

        'report: for diagnostic in
            diff::DiffGenerator::new(&logs.0.io_log, &logs.1.io_log, args.jitter)
                .filter(|diag| diag.level <= args.max_level)
        {
            ok = false;

            if diagnostic.when.tick != tick {
                if let Some(diagnostics) = diagnostics.as_mut() {
                    while let Some(diag) = diagnostics.peek() {
                        match tick.cmp(&diag.when.tick) {
                            Ordering::Greater => break, // Don't print diagnostics for upcoming ticks quite yet
                            Ordering::Less => {
                                tick = diag.when.tick;
                                print_tick(tick);
                            }
                            Ordering::Equal => (),
                        }

                        report!(diag, 'report);

                        diagnostics.next();
                    }
                }

                if tick != diagnostic.when.tick {
                    tick = diagnostic.when.tick;
                    print_tick(tick);
                }
            }

            report!(diagnostic);
        }

        // Print any leftover diagnostics
        if i != args.max_reports {
            if let Some(diagnostics) = diagnostics.as_mut() {
                for diag in diagnostics {
                    if tick != diag.when.tick {
                        tick = diag.when.tick;
                        print_tick(tick);
                    }
                    report!(diag);
                }
            }
        }

        if ok {
            println!("{}", colorize!(Stdout, "OK!", bright_green, bold));
        } else {
            failed.push(SongIDs(song_ids));
        }
    }

    if failed.is_empty() {
        println!(
            "{} {}",
            colorize!(Stdout, "==>", bold),
            colorize!(Stdout, "All songs are OK!", bright_green, bold)
        );
    } else if failed.len() == 1 {
        println!(
            "{} song: {}",
            colorize!(Stdout, "Failing", bright_red, bold),
            failed[0]
        );
        std::process::exit(1);
    } else {
        println!(
            "{} songs: {}",
            colorize!(Stdout, "Failing", bright_red, bold),
            failed.display()
        );
        std::process::exit(1);
    }
}

fn trace_write_fail(err: io::Error) {
    eprintln!("Failed to write to trace file: {}", err);
    std::process::exit(2);
}

#[derive(Debug)]
pub struct Diagnostic<K> {
    when: Timestamp,
    pc: Address,
    level: DiagnosticLevel,
    kind: K,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum DiagnosticLevel {
    Error,
    Warning,
    Note,
}

impl FromStr for DiagnosticLevel {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.eq_ignore_ascii_case("error") {
            Ok(Self::Error)
        } else if s.eq_ignore_ascii_case("warning") {
            Ok(Self::Warning)
        } else if s.eq_ignore_ascii_case("note") {
            Ok(Self::Note)
        } else {
            Err("unknown diagnostic level")
        }
    }
}

#[derive(Debug)]
enum BeforeOrAfter {
    Before,
    After,
    None,
}

impl FromStr for BeforeOrAfter {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.eq_ignore_ascii_case("before") {
            Ok(Self::Before)
        } else if s.eq_ignore_ascii_case("after") {
            Ok(Self::After)
        } else if s.eq_ignore_ascii_case("none") {
            Ok(Self::None)
        } else {
            Err("must be either \"before\" or \"after\"")
        }
    }
}

fn parse_watch_arg(arg: &str) -> Result<(u16, u8), String> {
    let (addr, value) = arg
        .split_once('=')
        .ok_or_else(|| "expected \"ADDR=VALUE\", e.g. \"CAFE=2A\"".to_string())?;
    Ok((
        u16::from_str_radix(addr.trim(), 16).map_err(|err| format!("invalid address: {}", err))?,
        u8::from_str_radix(value.trim(), 16).map_err(|err| format!("invalid value: {}", err))?,
    ))
}

fn parse_color_arg(arg: &str) -> Result<Option<bool>, String> {
    if arg.eq_ignore_ascii_case("auto") {
        Ok(None)
    } else if arg.eq_ignore_ascii_case("always") {
        Ok(Some(true))
    } else if arg.eq_ignore_ascii_case("never") {
        Ok(Some(false))
    } else {
        Err("expected \"auto\", \"always\", or \"never\"".to_string())
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Timestamp {
    /// Tick 0 is the "init" phase.
    tick: u64,
    cycle: u16,
}

impl Display for DiagnosticLevel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Error => write!(
                f,
                "{}",
                "Error".if_supports_color(Stdout, |text| text.bright_red())
            ),
            Self::Warning => write!(
                f,
                "{}",
                "Warning".if_supports_color(Stdout, |text| text.bright_yellow())
            ),
            Self::Note => write!(
                f,
                "{}",
                "Note".if_supports_color(Stdout, |text| text.bright_blue())
            ),
        }
    }
}

struct SongIDs((u8, u8));

impl Display for SongIDs {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.0 .0 == self.0 .1 {
            write!(f, "{}", self.0 .0)
        } else {
            write!(f, "{} and {}", self.0 .0, self.0 .1)
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct Address(u8, u16);

impl LowerHex for Address {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.1 {
            0x4000..=0x7FFF => write!(f, "{:02x}:{:04x}", self.0, self.1),
            _ => write!(f, "00:{:04x}", self.1),
        }
    }
}