onerom-gen 0.6.0

ROM metadata and ROM image generator for One ROM - the flexible retro ROM replacement
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
// Copyright (C) 2026 Piers Finlayson <piers@piers.rocks>
//
// MIT License

//! One ROM chip compatibility document generator.
//!
//! Writes docs/COMPATIBILITY.md at the repository root.
//!
//! Run with:
//!   cargo run --bin compat

use std::fs::File;
use std::io::{self, BufWriter, Write};
use std::path::{Path, PathBuf};

use onerom_config::chip::{CHIP_TYPE_NAMES, ChipType};
use onerom_config::hw::{Board, Model};
use onerom_gen::compat::{CompatResult, check_chip_on_board, is_v2_chip};

// ── Repository paths ──────────────────────────────────────────────────────────

/// Levels up from `CARGO_MANIFEST_DIR` to the repository root.
/// `CARGO_MANIFEST_DIR` = `<repo>/rust/gen`, so two pops reach `<repo>`.
const LEVELS_UP_TO_REPO_ROOT: usize = 2;

/// Output file path relative to the repository root.
const OUTPUT_FILE: &str = "docs/COMPATIBILITY.md";

/// Makefile path relative to the repository root (contains version numbers).
const VERSION_FILE: &str = "Makefile";

fn repo_root() -> PathBuf {
    let mut path = Path::new(env!("CARGO_MANIFEST_DIR")).to_path_buf();
    for _ in 0..LEVELS_UP_TO_REPO_ROOT {
        path.pop();
    }
    path
}

fn output_path() -> PathBuf {
    repo_root().join(OUTPUT_FILE)
}

// ── Version ───────────────────────────────────────────────────────────────────

fn read_version() -> String {
    let content = match std::fs::read_to_string(repo_root().join(VERSION_FILE)) {
        Ok(c) => c,
        Err(_) => return "unknown".to_string(),
    };

    let mut major = None;
    let mut minor = None;
    let mut patch = None;

    for line in content.lines() {
        let line = line.trim();
        if let Some(v) = line.strip_prefix("VERSION_MAJOR :=") {
            major = Some(v.trim().to_string());
        } else if let Some(v) = line.strip_prefix("VERSION_MINOR :=") {
            minor = Some(v.trim().to_string());
        } else if let Some(v) = line.strip_prefix("VERSION_PATCH :=") {
            patch = Some(v.trim().to_string());
        }
    }

    match (major, minor, patch) {
        (Some(ma), Some(mi), Some(pa)) => format!("{}.{}.{}", ma, mi, pa),
        _ => "unknown".to_string(),
    }
}

// ── Excluded boards ───────────────────────────────────────────────────────────

/// Boards omitted from all output sections.
const EXCLUDED_BOARDS: &[Board] = &[
    Board::Fire24Eadb01, // custom ADB01 variant, not for general release
];

// ── Board abbreviations ───────────────────────────────────────────────────────

/// Generate a short display name from the board's own name and chip_pins().
///
/// Algorithm: strip the "fire-" prefix, take the last dash-separated
/// component, uppercase its first character, prepend the pin count.
///
///   "fire-24-a"     →  "24A"
///   "fire-24-usb-b" →  "24B"   (last component of "24-usb-b")
///   "fire-28-c"     →  "28C"
fn board_short(board: Board) -> String {
    let suffix = board.name().strip_prefix("fire-").unwrap_or(board.name());
    let rev = suffix
        .rsplit('-')
        .next()
        .and_then(|p| p.chars().next())
        .unwrap_or('?')
        .to_ascii_uppercase();
    format!("{}{}", board.chip_pins(), rev)
}

// ── Formatting helpers ────────────────────────────────────────────────────────

fn format_size(bytes: u32) -> String {
    if bytes >= 1024 * 1024 {
        format!("{}MB", bytes / (1024 * 1024))
    } else if bytes >= 1024 {
        format!("{}KB", bytes / 1024)
    } else {
        format!("{}B", bytes)
    }
}

/// Format a compat result as a Markdown table cell.
///
/// `-` = not supported; `64KB` = native fit; `64KB*` = One ROM overhangs
/// the socket; `64KB†` = fly-leads required to X1/X2 header pins.
fn format_result(result: &Option<CompatResult>) -> String {
    match result {
        None => "-".to_string(),
        Some(r) => {
            let size = format_size(r.slot_size_bytes);
            if r.is_native() {
                size
            } else if r.requires_fly_leads() {
                format!("{}", size)
            } else {
                format!("{}*", size)
            }
        }
    }
}

/// Human-readable socket fit description for per-board tables.
fn format_socket(result: &CompatResult) -> String {
    if result.is_native() {
        "native".to_string()
    } else if result.requires_fly_leads() {
        match result.num_fly_lead_pins {
            0 => "no fly-leads required".to_string(),
            1 => "fly-lead to X1".to_string(),
            2 => "fly-lead to X1 and X2".to_string(),
            n => format!("fly-lead ({} pins)", n),
        }
    } else {
        "overhang".to_string()
    }
}

// ── Sort key ──────────────────────────────────────────────────────────────────

/// Sort key for entries: native first, then overhang (ascending by chip size
/// difference), then fly-lead (ascending by chip size difference).
fn pin_offset_order(pin_offset: i16) -> i32 {
    if pin_offset == 0 {
        0
    } else if pin_offset > 0 {
        1
    } else {
        2
    }
}

// ── Data structures ───────────────────────────────────────────────────────────

/// One chip row in the compatibility matrix.
struct MatrixEntry {
    pin_offset: i16,
    alias: &'static str,
    rom_size: u32,
    results: Vec<Option<CompatResult>>,
}

/// A group of same-offset chips in a matrix section.
struct MatrixGroup {
    pin_offset: i16,
    entries: Vec<MatrixEntry>,
}

/// One supported chip in a per-board table.
struct BoardEntry {
    alias: &'static str,
    rom_size: u32,
    chip_pins: u8,
    result: CompatResult,
}

/// A group of same-offset chips in a per-board table.
struct BoardGroup {
    pin_offset: i16,
    chip_pins: u8,
    entries: Vec<BoardEntry>,
}

// ── Matrix section ────────────────────────────────────────────────────────────

fn write_matrix_section(w: &mut impl Write, title: &str, boards: &[Board]) -> io::Result<()> {
    if boards.is_empty() {
        return Ok(());
    }

    let board_pins = boards[0].chip_pins();

    writeln!(w, "## {}", title)?;
    writeln!(w)?;

    let mut entries: Vec<MatrixEntry> = CHIP_TYPE_NAMES
        .iter()
        .filter_map(|alias| {
            let chip_type = ChipType::try_from_str(alias)?;
            if !is_v2_chip(chip_type) {
                return None;
            }
            let results: Vec<Option<CompatResult>> = boards
                .iter()
                .map(|b| check_chip_on_board(*b, chip_type))
                .collect();
            if results.iter().all(|r| r.is_none()) {
                return None;
            }
            let pin_offset = results
                .iter()
                .find_map(|r| r.as_ref())
                .map(|r| r.pin_offset)
                .unwrap_or(0);
            Some(MatrixEntry {
                pin_offset,
                alias,
                rom_size: chip_type.size_bytes() as u32,
                results,
            })
        })
        .collect();

    entries.sort_by_key(|e| {
        (
            pin_offset_order(e.pin_offset),
            e.pin_offset.abs(),
            e.rom_size,
            e.alias,
        )
    });

    let mut groups: Vec<MatrixGroup> = Vec::new();
    for entry in entries {
        match groups.last_mut() {
            Some(g) if g.pin_offset == entry.pin_offset => g.entries.push(entry),
            _ => groups.push(MatrixGroup {
                pin_offset: entry.pin_offset,
                entries: vec![entry],
            }),
        }
    }

    let show_headers = groups.len() > 1;

    for group in &groups {
        if show_headers {
            let chip_pins = board_pins as i16 - 2 * group.pin_offset;
            if group.pin_offset == 0 {
                writeln!(w, "*{}-pin chips (native)*", board_pins)?;
            } else if group.pin_offset > 0 {
                writeln!(w, "*{}-pin chips (with overhang)*", chip_pins)?;
            } else {
                writeln!(w, "*{}-pin chips (with fly-leads)*", chip_pins)?;
            }
            writeln!(w)?;
        }

        write!(w, "| Chip | ROM size |")?;
        for board in boards {
            write!(w, " {} |", board_short(*board))?;
        }
        writeln!(w)?;
        write!(w, "|:---|---:|")?;
        for _ in boards {
            write!(w, "---:|")?;
        }
        writeln!(w)?;

        for entry in &group.entries {
            write!(w, "| {} | {} |", entry.alias, format_size(entry.rom_size))?;
            for result in &entry.results {
                write!(w, " {} |", format_result(result))?;
            }
            writeln!(w)?;
        }
        writeln!(w)?;
    }

    Ok(())
}

// ── Per-board table ───────────────────────────────────────────────────────────

fn write_board_table(w: &mut impl Write, board: Board) -> io::Result<()> {
    writeln!(w, "## {}{}", board.description(), board.name())?;
    writeln!(w)?;

    let mut entries: Vec<BoardEntry> = CHIP_TYPE_NAMES
        .iter()
        .filter_map(|alias| {
            let chip_type = ChipType::try_from_str(alias)?;
            let result = check_chip_on_board(board, chip_type)?;
            Some(BoardEntry {
                alias,
                rom_size: chip_type.size_bytes() as u32,
                chip_pins: chip_type.chip_pins(),
                result,
            })
        })
        .collect();

    if entries.is_empty() {
        writeln!(w, "*(no supported chips)*")?;
        writeln!(w)?;
        return Ok(());
    }

    entries.sort_by_key(|e| {
        (
            pin_offset_order(e.result.pin_offset),
            e.result.pin_offset.abs(),
            e.rom_size,
            e.alias,
        )
    });

    let mut groups: Vec<BoardGroup> = Vec::new();
    for entry in entries {
        match groups.last_mut() {
            Some(g) if g.pin_offset == entry.result.pin_offset => g.entries.push(entry),
            _ => groups.push(BoardGroup {
                pin_offset: entry.result.pin_offset,
                chip_pins: entry.chip_pins,
                entries: vec![entry],
            }),
        }
    }

    let show_headers = groups.len() > 1;

    for group in &groups {
        if show_headers {
            if group.pin_offset == 0 {
                writeln!(w, "*{}-pin chips (native)*", board.chip_pins())?;
            } else if group.pin_offset > 0 {
                writeln!(w, "*{}-pin chips (with overhang)*", group.chip_pins)?;
            } else {
                writeln!(w, "*{}-pin chips (with fly-leads)*", group.chip_pins)?;
            }
            writeln!(w)?;
        }

        writeln!(w, "| Chip | ROM size | Image size | Fit |")?;
        writeln!(w, "|:---|---:|---:|:---|")?;

        for entry in &group.entries {
            writeln!(
                w,
                "| {} | {} | {} | {} |",
                entry.alias,
                format_size(entry.rom_size),
                format_size(entry.result.slot_size_bytes),
                format_socket(&entry.result),
            )?;
        }
        writeln!(w)?;
    }

    Ok(())
}

// ── Document generation ───────────────────────────────────────────────────────

fn generate_document(w: &mut impl Write) -> io::Result<()> {
    let version = read_version();

    let mut fire_boards: Vec<Board> = Model::Fire
        .boards()
        .iter()
        .filter(|b| b.mcu_pio() && !EXCLUDED_BOARDS.contains(b))
        .copied()
        .collect();
    fire_boards.sort_by_key(|b| board_short(*b));

    let boards_24: Vec<Board> = fire_boards
        .iter()
        .filter(|b| b.chip_pins() == 24)
        .copied()
        .collect();
    let boards_28: Vec<Board> = fire_boards
        .iter()
        .filter(|b| b.chip_pins() == 28)
        .copied()
        .collect();
    let boards_32: Vec<Board> = fire_boards
        .iter()
        .filter(|b| b.chip_pins() == 32)
        .copied()
        .collect();
    let boards_40: Vec<Board> = fire_boards
        .iter()
        .filter(|b| b.chip_pins() == 40)
        .copied()
        .collect();

    writeln!(w, "# One ROM Chip Compatibility — firmware v{}", version)?;
    writeln!(w)?;
    writeln!(
        w,
        "This document shows which chips each One ROM Fire hardware variant emulates."
    )?;
    writeln!(w)?;
    writeln!(w, "**ROM size** is the chip's actual storage capacity.")?;
    writeln!(w)?;
    writeln!(
        w,
        "**Image size** is the space used on One ROM device's internal \
                 flash to emulate that chip. This can be larger than the original \
                 ROM itself, due to the way One ROM works."
    )?;
    writeln!(w)?;
    writeln!(
        w,
        "One ROM typically has a 2MB flash, with 64KB reserved for the firmware \
                 and metadata. The remainder is available for ROM images. The total number \
                 of images that can be supported is based on the image size of each ROM."
    )?;
    writeln!(w)?;
    writeln!(
        w,
        "Some lower pin count ROMs can \
                 be emulated by larger One ROMs, by inserting the larger One ROM in the \
                 smaller socket, with the top pins (1, 2, ...) hanging out (and it is not \
                 necessary to solder these pins to One ROM if using like this). If doing \
                 this, it is _extremely_ important that power is rerouted to One ROM's VCC \
                 pin, or to the 5V header pin, or One ROM may be damaged."
    )?;
    writeln!(w)?;
    writeln!(
        w,
        "Some greater pin count ROMs can be emulated by a smaller One ROM, provided \
                 the ROM's extra address pins fall on socket positions that One ROM does not \
                 use. In this case, the smaller One ROM gets installed to the bottom \
                 of the larger socket, with the top pins of the socket unpopulated. \
                 A short fly-lead must be run from each additional address pin of those \
                 socket pins to the X1 (and, if two are needed, X2) header pin on One ROM."
    )?;
    writeln!(w)?;
    writeln!(w, "| Cell | Meaning |")?;
    writeln!(w, "|:---|:---|")?;
    writeln!(
        w,
        "| `64KB` | Chip is supported on this board; shows the image size |"
    )?;
    writeln!(
        w,
        "| `64KB*` | Supported with One ROM overhanging the socket (top pins exposed — power reroute required) |"
    )?;
    writeln!(
        w,
        "| `64KB†` | Supported with fly-lead wire(s) from the chip socket's address pin(s) to One ROM's X1 (and X2) header pin |"
    )?;
    writeln!(w, "| `-` | Not supported on this board |")?;
    writeln!(w)?;

    write_matrix_section(w, "24-pin boards", &boards_24)?;
    write_matrix_section(w, "28-pin boards", &boards_28)?;
    write_matrix_section(w, "32-pin boards", &boards_32)?;
    write_matrix_section(w, "40-pin boards", &boards_40)?;

    writeln!(w, "---")?;
    writeln!(w)?;
    writeln!(w, "# Per-board details")?;
    writeln!(w)?;
    writeln!(
        w,
        "Full chip list for each board. Where a particular ROM type goes \
                 by multiple identifiers (for example 27512, 27C512, 27SF512), each \
                 type appears as a separate row."
    )?;
    writeln!(w)?;

    for board in &fire_boards {
        write_board_table(w, *board)?;
    }

    Ok(())
}

// ── Main ──────────────────────────────────────────────────────────────────────

fn main() -> io::Result<()> {
    let output = output_path();
    if let Some(parent) = output.parent() {
        std::fs::create_dir_all(parent)?;
    }
    let mut w = BufWriter::new(File::create(&output)?);
    generate_document(&mut w)?;
    w.flush()?;
    eprintln!("Written to {}", output.display());
    Ok(())
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    /// Command to regenerate the compatibility document.
    const REGEN_CMD: &str = "cargo run -p onerom-gen --bin compat";

    #[test]
    fn compatibility_doc_is_current() {
        let mut generated = Vec::new();
        generate_document(&mut generated).expect("document generation failed");
        let generated =
            String::from_utf8(generated).expect("generated document is not valid UTF-8");

        let checked_in = std::fs::read_to_string(output_path()).unwrap_or_else(|_| {
            panic!(
                "docs/COMPATIBILITY.md is missing — regenerate with: {}",
                REGEN_CMD
            )
        });

        assert!(
            checked_in == generated,
            "docs/COMPATIBILITY.md is out of date — regenerate with: {}",
            REGEN_CMD
        );
    }
}