rom-analyzer 1.1.0

A CLI tool for analyzing console ROM file headers.
Documentation
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
//! Provides header analysis functionality for Super Nintendo Entertainment System (SNES) ROMs.
//!
//! This module can detect SNES ROM mapping types (LoROM, HiROM),
//! validate checksums, and extract game title and region information.
//!
//! Super Nintendo header documentation referenced here:
//! <https://snes.nesdev.org/wiki/ROM_header>

use log::error;
use serde::Serialize;

use crate::error::RomAnalyzerError;
use crate::region::{Region, check_region_mismatch};

// Map Mode byte offset relative to the header start (0x7FC0 for LoROM, 0xFFC0 for HiROM)
const MAP_MODE_OFFSET: usize = 0x15;

// Expected Map Mode byte values for LoROM and HiROM
const LOROM_MAP_MODES: &[u8] = &[0x20, 0x30, 0x25, 0x35];
const HIROM_MAP_MODES: &[u8] = &[0x21, 0x31, 0x22, 0x32];

/// Struct to hold the analysis results for a SNES ROM.
#[derive(Debug, PartialEq, Clone, Serialize)]
pub struct SnesAnalysis {
    /// The name of the source file.
    pub source_name: String,
    /// The identified region(s) as a region::Region bitmask.
    pub region: Region,
    /// The identified region name (e.g., "Japan (NTSC)").
    pub region_string: String,
    /// If the region in the ROM header doesn't match the region in the filename.
    pub region_mismatch: bool,
    /// The raw region code byte.
    pub region_code: u8,
    /// The game title extracted from the ROM header.
    pub game_title: String,
    /// The detected mapping type (e.g., "LoROM", "HiROM").
    pub mapping_type: String,
}

impl SnesAnalysis {
    /// Returns a printable String of the analysis results.
    pub fn print(&self) -> String {
        format!(
            "{}\n\
             System:       Super Nintendo (SNES)\n\
             Game Title:   {}\n\
             Mapping:      {}\n\
             Region Code:  0x{:02X}\n\
             Region:       {}",
            self.source_name, self.game_title, self.mapping_type, self.region_code, self.region
        )
    }
}

/// Determines the SNES game region name based on a given region byte.
///
/// The region byte typically comes from the ROM header. This function extracts the relevant bits
/// from the byte and maps it to a human-readable region string and a Region bitmask.
///
/// # Arguments
///
/// * `region_byte` - The byte containing the region code, usually found in the ROM header.
///
/// # Returns
///
/// A tuple containing:
/// - A `&'static str` representing the region as written in the ROM header (e.g., "Japan (NTSC)",
///   "USA / Canada (NTSC)", etc.) or "Unknown" if the region code is not recognized.
/// - A [`Region`] bitmask representing the region(s) associated with the code.
///
/// # Examples
///
/// ```rust
/// use rom_analyzer::console::snes::map_region;
/// use rom_analyzer::region::Region;
///
/// let (region_str, region_mask) = map_region(0x00);
/// assert_eq!(region_str, "Japan (NTSC)");
/// assert_eq!(region_mask, Region::JAPAN);
///
/// let (region_str, region_mask) = map_region(0x01);
/// assert_eq!(region_str, "USA / Canada (NTSC)");
/// assert_eq!(region_mask, Region::USA);
///
/// let (region_str, region_mask) = map_region(0x02);
/// assert_eq!(region_str, "Europe / Oceania / Asia (PAL)");
/// assert_eq!(region_mask, Region::EUROPE | Region::ASIA);
/// ```
pub fn map_region(code: u8) -> (&'static str, Region) {
    match code {
        0x00 => ("Japan (NTSC)", Region::JAPAN),
        0x01 => ("USA / Canada (NTSC)", Region::USA),
        0x02 => (
            "Europe / Oceania / Asia (PAL)",
            Region::EUROPE | Region::ASIA,
        ),
        0x03 => ("Sweden / Scandinavia (PAL)", Region::EUROPE),
        0x04 => ("Finland (PAL)", Region::EUROPE),
        0x05 => ("Denmark (PAL)", Region::EUROPE),
        0x06 => ("France (PAL)", Region::EUROPE),
        0x07 => ("Netherlands (PAL)", Region::EUROPE),
        0x08 => ("Spain (PAL)", Region::EUROPE),
        0x09 => ("Germany (PAL)", Region::EUROPE),
        0x0A => ("Italy (PAL)", Region::EUROPE),
        0x0B => ("China (PAL)", Region::CHINA),
        0x0C => ("Indonesia (PAL)", Region::EUROPE | Region::ASIA),
        0x0D => ("South Korea (NTSC)", Region::KOREA),
        0x0E => (
            "Common / International",
            Region::USA | Region::EUROPE | Region::JAPAN | Region::ASIA,
        ),
        0x0F => ("Canada (NTSC)", Region::USA),
        0x10 => ("Brazil (NTSC)", Region::USA),
        0x11 => ("Australia (PAL)", Region::EUROPE),
        0x12 => ("Other (Variation 1)", Region::UNKNOWN),
        0x13 => ("Other (Variation 2)", Region::UNKNOWN),
        0x14 => ("Other (Variation 3)", Region::UNKNOWN),
        _ => ("Unknown", Region::UNKNOWN),
    }
}

/// Helper function to validate the SNES ROM checksum.
///
/// This function checks if the 16-bit checksum and its complement, located
/// within the SNES header, sum up to `0xFFFF`. This is a common method
/// for validating the integrity of SNES ROM headers.
///
/// # Arguments
///
/// * `rom_data` - A byte slice (`&[u8]`) containing the raw ROM data.
/// * `header_offset` - The starting offset of the SNES header within `rom_data`.
///
/// # Returns
///
/// `true` if the checksum and its complement are valid (sum to 0xFFFF),
/// `false` otherwise, or if the `header_offset` is out of bounds.
pub fn validate_snes_checksum(rom_data: &[u8], header_offset: usize) -> bool {
    // Ensure we have enough data for checksum and complement bytes.
    if header_offset + 0x20 > rom_data.len() {
        return false;
    }

    // Checksum is at 0x1E (relative to header start), complement at 0x1C.
    // Both are 16-bit values, little-endian.
    let complement_bytes: [u8; 2] =
        match rom_data[header_offset + 0x1C..header_offset + 0x1E].try_into() {
            Ok(b) => b,
            Err(_) => return false, // Should not happen if header_offset + 0x20 is within bounds.
        };
    let checksum_bytes: [u8; 2] =
        match rom_data[header_offset + 0x1E..header_offset + 0x20].try_into() {
            Ok(b) => b,
            Err(_) => return false, // Should not happen if header_offset + 0x20 is within bounds.
        };

    let complement = u16::from_le_bytes(complement_bytes);
    let checksum = u16::from_le_bytes(checksum_bytes);

    // The checksum algorithm: (checksum + complement) should equal 0xFFFF.
    (checksum as u32 + complement as u32) == 0xFFFF
}

/// Analyzes SNES ROM data.
///
/// This function first attempts to detect a copier header. It then tries to determine
/// the ROM's mapping type (LoROM or HiROM) by validating checksums and examining
/// the Map Mode byte at expected header locations. If both checksum and Map Mode
/// are consistent, that mapping is chosen. If only the checksum is valid, it uses
/// that mapping with an "Map Mode Unverified" tag. If neither is fully consistent,
/// it falls back to LoROM (Unverified). Once the header location is determined,
/// it extracts the game title and region code, maps the region code to a human-readable
/// name, and performs a region mismatch check against the `source_name`.
///
/// # Arguments
///
/// * `data` - A byte slice (`&[u8]`) containing the raw ROM data.
/// * `source_name` - The name of the ROM file, used for logging and region mismatch checks.
///
/// # Returns
///
/// A `Result` which is:
/// - `Ok`([`SnesAnalysis`]) containing the detailed analysis results.
/// - `Err`([`RomAnalyzerError`]) if the ROM data is too small or the header is deemed invalid
///   such that critical information cannot be read.
pub fn analyze_snes_data(data: &[u8], source_name: &str) -> Result<SnesAnalysis, RomAnalyzerError> {
    let file_size = data.len();
    let mut header_offset = 0;

    // Detect copier header (often 512 bytes, common for some older dumps/tools)
    if file_size >= 512 && (file_size % 1024 == 512) {
        // Heuristic: If file size ends in 512 and is divisible by 1024
        header_offset = 512;
        // Note: This copier header detection is a simple heuristic and might not be foolproof.
        // More advanced detection could involve checking for specific patterns.
    }

    // Determine ROM mapping type (LoROM vs HiROM) by checking checksums and Map Mode byte.
    // The relevant header information is usually found at 0x7FC0 for LoROM and 0xFFC0 for HiROM
    // (relative to the start of the ROM, accounting for the header_offset).
    let lorom_header_start = 0x7FC0 + header_offset; // Header block starts here
    let hirom_header_start = 0xFFC0 + header_offset; // Header block starts here

    let mapping_type: String;
    let valid_header_offset: usize;

    let lorom_checksum_valid = validate_snes_checksum(data, lorom_header_start);
    let hirom_checksum_valid = validate_snes_checksum(data, hirom_header_start);

    // Get Map Mode bytes if headers are within bounds
    let lorom_map_mode_byte = if lorom_header_start + MAP_MODE_OFFSET < file_size {
        Some(data[lorom_header_start + MAP_MODE_OFFSET])
    } else {
        None
    };
    let hirom_map_mode_byte = if hirom_header_start + MAP_MODE_OFFSET < file_size {
        Some(data[hirom_header_start + MAP_MODE_OFFSET])
    } else {
        None
    };

    let is_lorom_map_mode = lorom_map_mode_byte.is_some_and(|b| LOROM_MAP_MODES.contains(&b));
    let is_hirom_map_mode = hirom_map_mode_byte.is_some_and(|b| HIROM_MAP_MODES.contains(&b));

    // Decision logic: Prioritize HiROM if both checksum and map mode are consistent.
    // Then check LoROM similarly. If only one checksum is valid, use that.
    // If neither is fully consistent, fallback to LoROM (unverified) with a warning.
    if hirom_checksum_valid && is_hirom_map_mode {
        mapping_type = "HiROM".to_string();
        valid_header_offset = hirom_header_start;
    } else if lorom_checksum_valid && is_lorom_map_mode {
        mapping_type = "LoROM".to_string();
        valid_header_offset = lorom_header_start;
    } else if hirom_checksum_valid {
        mapping_type = "HiROM (Map Mode Unverified)".to_string();
        valid_header_offset = hirom_header_start;
        error!(
            "[!] HiROM checksum valid for {}, but Map Mode byte (0x{:02X?}) is not a typical HiROM value. Falling back to HiROM.",
            source_name, hirom_map_mode_byte
        );
    } else if lorom_checksum_valid {
        mapping_type = "LoROM (Map Mode Unverified)".to_string();
        valid_header_offset = lorom_header_start;
        error!(
            "[!] LoROM checksum valid for {}, but Map Mode byte (0x{:02X?}) is not a typical LoROM value. Falling back to LoROM.",
            source_name, lorom_map_mode_byte
        );
    } else {
        // If neither checksum is valid, log a warning and try LoROM as a fallback, as it's more common.
        error!(
            "[!] Checksum validation failed for {}. Attempting to read header from LoROM location ({:X}) as fallback.",
            source_name, lorom_header_start
        );
        mapping_type = "LoROM (Unverified)".to_string();
        valid_header_offset = lorom_header_start; // Fallback to LoROM offset
    }

    // Ensure the determined header offset plus the header size needed for analysis is within the file bounds.
    // We need at least up to the region code (offset 0x19 relative to header start) and game title (offset 0x0 to 0x14).
    // Thus, we check if `valid_header_offset + 0x20` is within bounds, as this covers the checksum bytes.
    if valid_header_offset + 0x20 > file_size {
        return Err(RomAnalyzerError::DataTooSmall {
            file_size,
            required_size: valid_header_offset + 0x20,
            details: format!("Checked header at offset: {}.", valid_header_offset),
        });
    }

    // Extract region code and game title from the identified header.
    let region_byte_offset = valid_header_offset + 0x19; // Offset for region code within the header
    let region_code = data[region_byte_offset];
    let (region_name, region) = map_region(region_code);

    // Game title is located at the beginning of the header (offset 0x0 relative to valid_header_offset) for 21 bytes.
    // It is null-terminated, so we trim null bytes and leading/trailing whitespace.
    let game_title = String::from_utf8_lossy(&data[valid_header_offset..valid_header_offset + 21])
        .trim_matches(char::from(0)) // Remove null bytes
        .trim()
        .to_string();

    let region_mismatch = check_region_mismatch(source_name, region);

    Ok(SnesAnalysis {
        source_name: source_name.to_string(),
        region,
        region_string: region_name.to_string(),
        region_mismatch,
        region_code,
        game_title,
        mapping_type,
    })
}

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

    /// Helper to create a dummy SNES ROM with a valid checksum.
    /// It allows specifying ROM size, copier header offset, region code, mapping type.
    fn generate_snes_header(
        rom_size: usize,
        copier_header_offset: usize,
        region_code: u8,
        is_hirom: bool,
        title: &str,
        map_mode_byte: Option<u8>,
    ) -> Vec<u8> {
        let mut data = vec![0; rom_size];

        // Calculate the actual start of the SNES header based on mapping type and copier offset.
        let header_start = (if is_hirom { 0xFFC0 } else { 0x7FC0 }) + copier_header_offset;

        // Ensure the data is large enough
        if header_start + 0x20 > rom_size {
            panic!(
                "Provided ROM size {} is too small for SNES header at offset {} (needs at least {}).",
                rom_size,
                header_start,
                header_start + 0x20
            );
        }

        // 1. Set Title (21 bytes starting at header_start + 0x00)
        let mut title_bytes: Vec<u8> = title.as_bytes().to_vec();
        // Truncate if longer than 21 bytes, then pad with spaces if shorter.
        title_bytes.truncate(21);
        title_bytes.resize(21, b' '); // Pad with spaces, standard SNES header practice

        data[header_start..header_start + 21].copy_from_slice(&title_bytes);

        // 2. Set Region Code (at header_start + 0x19)
        data[header_start + 0x19] = region_code;

        // 3. Set Map Mode byte if provided (at header_start + MAP_MODE_OFFSET)
        if let Some(map_mode) = map_mode_byte {
            data[header_start + MAP_MODE_OFFSET] = map_mode;
        }

        // 4. Set a valid checksum and its complement.
        // The checksum algorithm is (checksum + complement) == 0xFFFF. We use a simple pair.
        let complement: u16 = 0x5555;
        let checksum: u16 = 0xFFFF - complement; // 0xAAAA

        // Set Checksum Complement (0x1C relative to header start)
        data[header_start + 0x1C..header_start + 0x1E].copy_from_slice(&complement.to_le_bytes());
        // Set Checksum (0x1E relative to header start)
        data[header_start + 0x1E..header_start + 0x20].copy_from_slice(&checksum.to_le_bytes());

        data
    }

    #[test]
    fn test_analyze_snes_data_lorom_japan() -> Result<(), RomAnalyzerError> {
        let data = generate_snes_header(0x80000, 0, 0x00, false, "TEST GAME TITLE", None); // 512KB ROM, LoROM, Japan
        let analysis = analyze_snes_data(&data, "test_lorom_jp.sfc")?;

        assert_eq!(analysis.source_name, "test_lorom_jp.sfc");
        assert_eq!(analysis.game_title, "TEST GAME TITLE");
        assert_eq!(analysis.mapping_type, "LoROM (Map Mode Unverified)");
        assert_eq!(analysis.region_code, 0x00);
        assert_eq!(analysis.region, Region::JAPAN);
        assert_eq!(analysis.region_string, "Japan (NTSC)");
        assert_eq!(
            analysis.print(),
            "test_lorom_jp.sfc\n\
             System:       Super Nintendo (SNES)\n\
             Game Title:   TEST GAME TITLE\n\
             Mapping:      LoROM (Map Mode Unverified)\n\
             Region Code:  0x00\n\
             Region:       Japan"
        );
        Ok(())
    }

    #[test]
    fn test_analyze_snes_data_hirom_usa() -> Result<(), RomAnalyzerError> {
        let data = generate_snes_header(0x100000, 0, 0x01, true, "TEST GAME TITLE", None); // 1MB ROM, HiROM, USA
        let analysis = analyze_snes_data(&data, "test_hirom_us.sfc")?;

        assert_eq!(analysis.source_name, "test_hirom_us.sfc");
        assert_eq!(analysis.game_title, "TEST GAME TITLE");
        assert_eq!(analysis.mapping_type, "HiROM (Map Mode Unverified)");
        assert_eq!(analysis.region_code, 0x01);
        assert_eq!(analysis.region, Region::USA);
        assert_eq!(analysis.region_string, "USA / Canada (NTSC)");
        Ok(())
    }

    #[test]
    fn test_analyze_snes_data_lorom_europe_copier_header() -> Result<(), RomAnalyzerError> {
        // Rom size ends with 512 bytes, e.g., 800KB + 512 bytes = 800512 bytes.
        let data = generate_snes_header(0x80000 + 512, 512, 0x02, false, "TEST GAME TITLE", None); // LoROM, Europe, with 512-byte copier header
        let analysis = analyze_snes_data(&data, "test_lorom_eur_copier.sfc")?;

        assert_eq!(analysis.source_name, "test_lorom_eur_copier.sfc");
        assert_eq!(analysis.game_title, "TEST GAME TITLE");
        assert_eq!(analysis.mapping_type, "LoROM (Map Mode Unverified)"); // Should detect copier header but still identify LoROM
        assert_eq!(analysis.region_code, 0x02);
        assert_eq!(analysis.region, Region::EUROPE | Region::ASIA);
        assert_eq!(analysis.region_string, "Europe / Oceania / Asia (PAL)");
        Ok(())
    }

    #[test]
    fn test_analyze_snes_data_hirom_canada_copier_header() -> Result<(), RomAnalyzerError> {
        // Data size: 1MB + 512 bytes for copier header
        let data = generate_snes_header(
            0x100200,
            512,  // Copier Header offset
            0x0F, // Region: Canada (0x0F)
            true, // HiROM
            "TEST GAME TITLE",
            None,
        );
        let analysis = analyze_snes_data(&data, "test_hirom_can_copier.sfc")?;

        assert_eq!(analysis.source_name, "test_hirom_can_copier.sfc");
        assert_eq!(analysis.game_title, "TEST GAME TITLE");
        assert_eq!(analysis.mapping_type, "HiROM (Map Mode Unverified)");
        assert_eq!(analysis.region_code, 0x0F);
        assert_eq!(analysis.region, Region::USA);
        assert_eq!(analysis.region_string, "Canada (NTSC)");
        Ok(())
    }

    #[test]
    fn test_analyze_snes_data_unknown_region() -> Result<(), RomAnalyzerError> {
        let data = generate_snes_header(0x80000, 0, 0xFF, false, "TEST GAME TITLE", None); // LoROM, Unknown region
        let analysis = analyze_snes_data(&data, "test_lorom_unknown.sfc")?;

        assert_eq!(analysis.source_name, "test_lorom_unknown.sfc");
        assert_eq!(analysis.game_title, "TEST GAME TITLE");
        assert_eq!(analysis.mapping_type, "LoROM (Map Mode Unverified)");
        assert_eq!(analysis.region_code, 0xFF);
        assert_eq!(analysis.region, Region::UNKNOWN);
        assert_eq!(analysis.region_string, "Unknown");
        Ok(())
    }

    #[test]
    fn test_analyze_snes_data_lorom_indonesia() -> Result<(), RomAnalyzerError> {
        let data = generate_snes_header(0x80000, 0, 0x0C, false, "TEST GAME TITLE", None); // LoROM, Indonesia
        let analysis = analyze_snes_data(&data, "test_lorom_indonesia.sfc")?;

        assert_eq!(analysis.source_name, "test_lorom_indonesia.sfc");
        assert_eq!(analysis.game_title, "TEST GAME TITLE");
        assert_eq!(analysis.mapping_type, "LoROM (Map Mode Unverified)");
        assert_eq!(analysis.region_code, 0x0C);
        assert_eq!(analysis.region, Region::EUROPE | Region::ASIA);
        assert_eq!(analysis.region_string, "Indonesia (PAL)");
        Ok(())
    }

    #[test]
    fn test_analyze_snes_data_lorom_common() -> Result<(), RomAnalyzerError> {
        let data = generate_snes_header(0x80000, 0, 0x0E, false, "TEST GAME TITLE", None); // LoROM, Common
        let analysis = analyze_snes_data(&data, "test_lorom_common.sfc")?;

        assert_eq!(analysis.source_name, "test_lorom_common.sfc");
        assert_eq!(analysis.game_title, "TEST GAME TITLE");
        assert_eq!(analysis.mapping_type, "LoROM (Map Mode Unverified)");
        assert_eq!(analysis.region_code, 0x0E);
        assert_eq!(
            analysis.region,
            Region::USA | Region::EUROPE | Region::JAPAN | Region::ASIA
        );
        assert_eq!(analysis.region_string, "Common / International");
        Ok(())
    }

    #[test]
    fn test_analyze_snes_data_minimal_lorom_size() -> Result<(), RomAnalyzerError> {
        // Minimal size for LoROM: header at 0x7FC0, needs up to 0x7FE0 for checksum.
        let data = generate_snes_header(0x7FE0, 0, 0x00, false, "MINIMAL", None);
        let analysis = analyze_snes_data(&data, "minimal_lorom.sfc")?;
        assert_eq!(analysis.mapping_type, "LoROM (Map Mode Unverified)");
        Ok(())
    }

    #[test]
    fn test_validate_snes_checksum_minimal_size() {
        // Test with data exactly the size needed for LoROM checksum validation.
        // LoROM header starts at 0x7FC0, needs up to 0x7FE0 (0x7FC0 + 0x20).
        let mut data = vec![0; 0x7FE0];
        // Set valid checksum: complement 0x5555, checksum 0xAAAA (0xFFFF - 0x5555)
        data[0x7FC0 + 0x1C..0x7FC0 + 0x1E].copy_from_slice(&0x5555u16.to_le_bytes());
        data[0x7FC0 + 0x1E..0x7FC0 + 0x20].copy_from_slice(&0xAAAAu16.to_le_bytes());
        assert!(validate_snes_checksum(&data, 0x7FC0));
    }

    #[test]
    fn test_analyze_snes_data_too_small_for_header() {
        // Test with data that is large enough to detect LoROM header location
        // but not large enough for the header content (needs valid_header_offset + 0x20)
        // For LoROM, valid_header_offset = 0x7FC0, so we need at least 0x7FE0 bytes
        let data = vec![0; 0x7FDF]; // One byte short of 0x7FE0
        let result = analyze_snes_data(&data, "too_small_for_header.sfc");
        assert!(result.is_err());
        let err = result.unwrap_err();
        match err {
            RomAnalyzerError::DataTooSmall {
                file_size,
                required_size,
                details,
            } => {
                assert_eq!(file_size, 0x7FDF);
                assert_eq!(required_size, 0x7FC0 + 0x20); // valid_header_offset + 0x20
                assert!(details.contains("Checked header at offset"));
            }
            _ => panic!("Expected DataTooSmall error"),
        }
    }

    #[test]
    fn test_analyze_snes_data_hirom_checksum_map_mode_consistent() -> Result<(), RomAnalyzerError> {
        let data =
            generate_snes_header(0x100000, 0, 0x01, true, "TEST HIROM CONSISTENT", Some(0x21)); // HiROM, USA, HiROM Map Mode
        let analysis = analyze_snes_data(&data, "test_hirom_consistent.sfc")?;

        assert_eq!(analysis.mapping_type, "HiROM");
        assert_eq!(analysis.game_title, "TEST HIROM CONSISTENT");
        Ok(())
    }

    #[test]
    fn test_analyze_snes_data_lorom_checksum_map_mode_consistent() -> Result<(), RomAnalyzerError> {
        let data =
            generate_snes_header(0x80000, 0, 0x00, false, "TEST LOROM CONSISTENT", Some(0x20)); // LoROM, Japan, LoROM Map Mode
        let analysis = analyze_snes_data(&data, "test_lorom_consistent.sfc")?;

        assert_eq!(analysis.mapping_type, "LoROM");
        assert_eq!(analysis.game_title, "TEST LOROM CONSISTENT");
        Ok(())
    }

    #[test]
    fn test_analyze_snes_data_hirom_checksum_map_mode_inconsistent() -> Result<(), RomAnalyzerError>
    {
        let data = generate_snes_header(
            0x100000,
            0,
            0x01,
            true,
            "TEST HIROM INCONSISTENT",
            Some(0x20),
        ); // HiROM, USA, LoROM Map Mode
        let analysis = analyze_snes_data(&data, "test_hirom_inconsistent.sfc")?;

        assert_eq!(analysis.mapping_type, "HiROM (Map Mode Unverified)");
        assert_eq!(analysis.game_title, "TEST HIROM INCONSISTE");
        Ok(())
    }

    #[test]
    fn test_analyze_snes_data_lorom_checksum_map_mode_inconsistent() -> Result<(), RomAnalyzerError>
    {
        let data = generate_snes_header(
            0x80000,
            0,
            0x00,
            false,
            "TEST LOROM INCONSISTENT",
            Some(0x21),
        ); // LoROM, Japan, HiROM Map Mode
        let analysis = analyze_snes_data(&data, "test_lorom_inconsistent.sfc")?;

        assert_eq!(analysis.mapping_type, "LoROM (Map Mode Unverified)");
        assert_eq!(analysis.game_title, "TEST LOROM INCONSISTE");
        Ok(())
    }

    #[test]
    fn test_analyze_snes_data_no_valid_checksum_map_mode_consistent_hirom_only()
    -> Result<(), RomAnalyzerError> {
        let mut data = generate_snes_header(
            0x100000,
            0,
            0x01,
            true,
            "TEST NO CHECKSUM HIROM MAP",
            Some(0x21),
        ); // HiROM, USA, HiROM Map Mode
        // Invalidate both checksums
        let lorom_checksum_start = 0x7FC0 + 0x1C;
        data[lorom_checksum_start..lorom_checksum_start + 4]
            .copy_from_slice(&[0x00, 0x00, 0x00, 0x00]);
        let hirom_checksum_start = 0xFFC0 + 0x1C;
        data[hirom_checksum_start..hirom_checksum_start + 4]
            .copy_from_slice(&[0x00, 0x00, 0x00, 0x00]);

        let analysis = analyze_snes_data(&data, "test_no_checksum_hirom_map.sfc")?;

        assert_eq!(analysis.mapping_type, "LoROM (Unverified)"); // Expect fallback
        Ok(())
    }
    #[test]
    fn test_analyze_snes_data_no_valid_checksum_map_mode_consistent_lorom_only()
    -> Result<(), RomAnalyzerError> {
        let mut data = generate_snes_header(
            0x80000,
            0,
            0x00,
            false,
            "TEST NO CHECKSUM LOROM MAP",
            Some(0x20),
        ); // LoROM, Japan, LoROM Map Mode
        // Invalidate both checksums
        let lorom_checksum_start = 0x7FC0 + 0x1C;
        data[lorom_checksum_start..lorom_checksum_start + 4]
            .copy_from_slice(&[0x00, 0x00, 0x00, 0x00]);
        let hirom_checksum_start = 0xFFC0 + 0x1C;
        data[hirom_checksum_start..hirom_checksum_start + 4]
            .copy_from_slice(&[0x00, 0x00, 0x00, 0x00]);

        let analysis = analyze_snes_data(&data, "test_no_checksum_lorom_map.sfc")?;

        assert_eq!(analysis.mapping_type, "LoROM (Unverified)"); // Expect fallback
        Ok(())
    }

    #[test]
    fn test_map_region_all_codes() {
        // Test all known region codes to catch "delete match arm" mutations
        let test_cases = vec![
            (0x00, "Japan (NTSC)", Region::JAPAN),
            (0x01, "USA / Canada (NTSC)", Region::USA),
            (
                0x02,
                "Europe / Oceania / Asia (PAL)",
                Region::EUROPE | Region::ASIA,
            ),
            (0x03, "Sweden / Scandinavia (PAL)", Region::EUROPE),
            (0x04, "Finland (PAL)", Region::EUROPE),
            (0x05, "Denmark (PAL)", Region::EUROPE),
            (0x06, "France (PAL)", Region::EUROPE),
            (0x07, "Netherlands (PAL)", Region::EUROPE),
            (0x08, "Spain (PAL)", Region::EUROPE),
            (0x09, "Germany (PAL)", Region::EUROPE),
            (0x0A, "Italy (PAL)", Region::EUROPE),
            (0x0B, "China (PAL)", Region::CHINA),
            (0x0C, "Indonesia (PAL)", Region::EUROPE | Region::ASIA),
            (0x0D, "South Korea (NTSC)", Region::KOREA),
            (
                0x0E,
                "Common / International",
                Region::USA | Region::EUROPE | Region::JAPAN | Region::ASIA,
            ),
            (0x0F, "Canada (NTSC)", Region::USA),
            (0x10, "Brazil (NTSC)", Region::USA),
            (0x11, "Australia (PAL)", Region::EUROPE),
            (0x12, "Other (Variation 1)", Region::UNKNOWN),
            (0x13, "Other (Variation 2)", Region::UNKNOWN),
            (0x14, "Other (Variation 3)", Region::UNKNOWN),
            (0xFF, "Unknown", Region::UNKNOWN),
        ];
        for (code, expected_name, expected_region) in test_cases {
            let (name, region) = map_region(code);
            assert_eq!(name, expected_name, "Failed for code 0x{:02X}", code);
            assert_eq!(region, expected_region, "Failed for code 0x{:02X}", code);
        }
    }
}