sfc_cart 0.2.1

SNES/SFC ROM header library and utilities.
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
// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR MPL-2.0
// SPDX-FileCopyrightText: 2024 Gabriel Marcano <gabemarcano@yahoo.com>

use crate::error::Error;

use std::fmt;
use std::io::Read;
use std::io::Seek;
use std::io::SeekFrom;
use std::str;

use byteorder::LittleEndian;
use byteorder::ReadBytesExt;

/// Represents the ROM access speeds.
///
/// SNES/SFC ROMs were made with two different access speeds. Slow ROMs is clocked at
/// 2.68MHz, while Fast ROMs are clocked at 3.58MHz.
#[derive(Debug)]
pub enum Speed {
    Slow,
    Fast,
}

impl fmt::Display for Speed {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Slow => write!(fmt, "slow")?,
            Self::Fast => write!(fmt, "fast")?,
        }
        Ok(())
    }
}

/// Represents the memory map mode of the ROM.
#[derive(Debug)]
pub enum Mode {
    Lorom,
    SDD1,
    Hirom,
    SA1,
    Exhirom,
}

impl fmt::Display for Mode {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Lorom => write!(fmt, "LoROM")?,
            Self::SDD1 => write!(fmt, "S-DD1")?,
            Self::Hirom => write!(fmt, "HiROM")?,
            Self::SA1 => write!(fmt, "SA-1")?,
            Self::Exhirom => write!(fmt, "ExHiROM")?,
        }
        Ok(())
    }
}

/// Represents the chipset of the game cartridge.
///
/// This represents the types of chips that are on the cartridge.
#[derive(Debug)]
pub enum Chipset {
    RomOnly,
    RomRam,
    RomRamBattery,
    RomCoprocessor,
    RomCoprocessorRam,
    RomCoprocessorRamBattery,
    RomCoprocessorBattery,
}

impl fmt::Display for Chipset {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::RomOnly => write!(fmt, "ROM only")?,
            Self::RomRam => write!(fmt, "ROM + RAM")?,
            Self::RomRamBattery => write!(fmt, "ROM + RAM + battery")?,
            Self::RomCoprocessor => write!(fmt, "ROM + coprocessor")?,
            Self::RomCoprocessorRam => write!(fmt, "ROM + coprocessor + RAM")?,
            Self::RomCoprocessorRamBattery => write!(fmt, "ROM + coprocessor + RAM + battery")?,
            Self::RomCoprocessorBattery => write!(fmt, "ROM + coprocessor + battery")?,
        }
        Ok(())
    }
}

/// Represents game cartridge coprocessors.
#[derive(Debug)]
pub enum Coprocessor {
    None,
    DSP,
    SuperFX,
    OBC1,
    SA1,
    SDD1,
    SRTC,
    Other,
    Custom,
}

impl fmt::Display for Coprocessor {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::None => write!(fmt, "None")?,
            Self::DSP => write!(fmt, "DSP")?,
            Self::SuperFX => write!(fmt, "GSU/SuperFX")?,
            Self::OBC1 => write!(fmt, "OBC1")?,
            Self::SA1 => write!(fmt, "SA-1")?,
            Self::SDD1 => write!(fmt, "S-DD1")?,
            Self::SRTC => write!(fmt, "S-RTC")?,
            Self::Other => write!(fmt, "other")?,
            Self::Custom => write!(fmt, "custom")?,
        }
        Ok(())
    }
}

/// The region the cartridge is meant for.
#[derive(Debug)]
pub enum Region {
    Japan,
    USA,
    Europe,
}

impl fmt::Display for Region {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Japan => write!(fmt, "Japan")?,
            Self::USA => write!(fmt, "USA")?,
            Self::Europe => write!(fmt, "Europe")?,
        }
        Ok(())
    }
}

/// Interrupt vectors when running in native mode.
#[derive(Debug)]
pub struct NativeVectors {
    pub cop: u16,
    pub brk: u16,
    pub abort: u16,
    pub nmi: u16,
    pub irq: u16,
}

/// Interrupt vectors when running in emulation mode.
///
/// The SNES/SFC boots in emulation mode.
#[derive(Debug)]
pub struct EmulationVectors {
    pub cop: u16,
    pub abort: u16,
    pub nmi: u16,
    pub reset: u16,
    pub irq_brk: u16,
}

/// The complete set of interrupt vectors of the SNES/SFC.
#[derive(Debug)]
pub struct Vectors {
    pub native: NativeVectors,
    pub emulation: EmulationVectors,
}

/// Represents the metadata held by the SFC internal header.
#[derive(Debug)]
pub struct Cart {
    /// The title of the cartridge.
    ///
    /// This is no more than 21 ASCII bytes. On the cartridge any unused bytes are replaced with
    /// spaces, but in this struct we only store the used bytes.
    pub title: String,
    /// The speed setting of the ROM.
    pub speed: Speed,
    /// The memory mapping mode of the cartridge.
    pub mode: Mode,
    /// The chipset of the cartridge.
    pub chipset: Chipset,
    /// The coprocessor on the cartridge, if any.
    pub coprocessor: Coprocessor,
    /// The size of the ROM, in KiB.
    pub rom_size: u32,
    /// The size of the RAM, in KiB.
    pub save_ram_size: u32,
    /// The region the cartridge is released for.
    pub region: Region,
    /// The developer id.
    pub dev_id: u8,
    /// The version of the ROM.
    pub version: String,
    /// The checksum of the ROM.
    pub checksum: u16,
    /// Interrupt vectors.
    pub vectors: Vectors,
    /// Additional metadata, only available if the developer ID is equal to 0x33.
    pub expanded_header: Option<ExpandedHeader>,
}

/// Represents additional metadata held by the SFC internal header.
///
/// This metadata is available if the developer ID is equal to 0x33.
#[derive(Debug)]
pub struct ExpandedHeader {
    /// 2 bytes ASCII maker code.
    pub maker_code: String,
    /// 4 byte ASCII game code.
    pub game_code: String,
    /// Expansion flash size, in KiB.
    pub expansion_flash_size: u32,
    /// Expansion RAM size, in KiB.
    pub expansion_ram_size: u32,
    /// Special version.
    pub special_version: u8,
    /// Chipset subtype.
    pub chipset_subtype: u8,
}

/// Compute the SFC checksum
///
/// SFC checksum is computed by summing all of the bytes of the file into an unsigned 16-bit
/// counter. If the data isn't a power of two, the data is sub-divided into two parts: the first
/// large portion is equal in size to the largest power of two that fits into the current size, and
/// then there's the rest. The rest is repeated as many times until the total new size is equal to
/// the next power of two.
fn compute_sfc_checksum(data: &[u8]) -> u16 {
    let mut checksum = 0u16;
    for byte in data {
        checksum = checksum.wrapping_add(u16::from(*byte));
    }
    let size = data.len();
    // If it's already power of two size, we can exit
    if size == 0 || size.is_power_of_two() {
        return checksum;
    }

    //Otherwise, we need to pad until we're at a power of two size.
    let rom1_size = 1 << size.ilog2();
    let rom2_size = size - rom1_size;
    let full_size = rom1_size << 1;
    let mirror_size = full_size - size;
    let times = mirror_size / rom2_size;
    for _ in 0..times {
        for byte in &data[rom1_size..] {
            checksum = checksum.wrapping_add(u16::from(*byte));
        }
    }
    checksum
}

impl Cart {
    /// Returns a new Cart object, after parsing the header.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Io`] if an IO error took place while reading from the file.
    pub fn from_io<T: Read + Seek>(io: &mut T) -> Result<Self, Error> {
        io.read_sfc_header()
    }

    /// Computes the checksum of the SFC file.
    ///
    /// SFC checksum is computed by summing all of the bytes of the file into an unsigned 16-bit
    /// counter.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Io`] if an IO error took place while reading from the file.
    pub fn compute_checksum<T: Read + Seek>(&self, io: &mut T) -> Result<u16, Error> {
        io.seek(SeekFrom::Start(0))?;
        let mut data = Vec::new();
        // It's faster to read in the entire SFC... on modern systems, the max size fits in memory
        // just fine, so just read it all
        io.read_to_end(&mut data)?;
        Ok(compute_sfc_checksum(&data))
    }
}

/// Trims null and whitespace characters (in that order) from the given string.
fn trim(string: &str) -> &str {
    string.trim_matches('\0').trim()
}

/// Checks that the header has valid characters.
fn check_header_name(io: &mut (impl Read + Seek), location: u32) -> bool {
    if io.seek(SeekFrom::Start((location + 0x10).into())).is_err() {
        return false;
    }
    let mut header = [0u8; 21];
    if io.read_exact(&mut header).is_err() {
        return false;
    }

    if !header.is_ascii() {
        return false;
    }

    let name = str::from_utf8(&header);
    name.is_ok()
}

/// Checks the CRC of the file against the computed CRC of the file.
fn check_crc(io: &mut (impl Read + Seek), data: &[u8], location: u32) -> bool {
    if io.seek(SeekFrom::Start((location + 0x2E).into())).is_err() {
        return false;
    }
    let crc = io.read_u16::<LittleEndian>();
    if crc.is_err() {
        return false;
    }
    let crc = crc.unwrap();
    crc == compute_sfc_checksum(data)
}

/// Finds the SFC header.
///
/// SFC headers are usually in one of three places:
///  - `LoROM`:   $007FB0
///  - `HiROM`:   $00FFB0
///  - `ExHiROM`: $40FFB0
///
/// We try to find the location by checking to see if the CRC passes, and if not, if the title
/// check passes.
///
/// One implementation note, we scan `ExHiROM`, `HiROM`, and `LoROM` in that order. Apparently there are
/// some titles that include some kind of header in both their `LoROM` and `HiROM` locations, but only
/// the `HiROM` one is actually valid.
///
/// # Errors
///
/// Returns [`Error::Io`] if an IO error took place while reading the file. If no header is found,
/// returns [`Error::Parse`].
fn find_header(io: &mut (impl Read + Seek)) -> Result<u32, Error> {
    io.seek(SeekFrom::Start(0))?;
    let mut data = Vec::new();
    // It's faster to read in the entire SFC... on modern systems, the max size fits in memory
    // just fine, so just read it all
    io.read_to_end(&mut data)?;
    let data = data;

    let valid_locations = [0x0040_FFB0_u32, 0x0000_FFB0_u32, 0x0000_7FB0_u32];
    for location in &valid_locations {
        if check_crc(io, &data, *location) {
            return Ok(*location);
        }
    }
    // If we failed to find a good CRC, try finding a good title
    for location in &valid_locations {
        if check_header_name(io, *location) {
            return Ok(*location);
        }
    }
    Err(Error::Parse("failed to find header".into()))
}

pub trait CartRead {
    /// Parses the  file metadata from the object provided, returning a Metadata object with the
    ///  file metadata.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Parse`] if the header cannot be found or if a field in the header contains
    /// an unexpected value.
    /// Returns [`Error::Io`] if an IO error took place while reading from the file.
    fn read_sfc_header(&mut self) -> Result<Cart, Error>;
}

impl TryFrom<u8> for Mode {
    type Error = Error;

    fn try_from(data: u8) -> Result<Self, Self::Error> {
        match data & 0xF {
            0 => Ok(Self::Lorom),
            1 => Ok(Self::Hirom),
            2 => Ok(Self::SDD1), // Star Ocean apparently is this
            3 => Ok(Self::SA1),
            5 => Ok(Self::Exhirom),
            _ => Err(Self::Error::Parse("failed to parse cart mode".into())),
        }
    }
}

impl From<u8> for Speed {
    fn from(data: u8) -> Self {
        if ((data >> 4) & 0x1) == 1 {
            Self::Fast
        } else {
            Self::Slow
        }
    }
}

impl TryFrom<u8> for Chipset {
    type Error = Error;

    fn try_from(data: u8) -> Result<Self, Self::Error> {
        match data & 0xF {
            0x0 => Ok(Self::RomOnly),
            0x1 => Ok(Self::RomRam),
            0x2 => Ok(Self::RomRamBattery),
            0x3 => Ok(Self::RomCoprocessor),
            0x4 => Ok(Self::RomCoprocessorRam),
            0x5 => Ok(Self::RomCoprocessorRamBattery),
            0x6 => Ok(Self::RomCoprocessorBattery),
            _ => Err(Self::Error::Parse("failed to parse cart chipset".into())),
        }
    }
}

impl TryFrom<u8> for Coprocessor {
    type Error = Error;

    fn try_from(data: u8) -> Result<Self, Self::Error> {
        if data <= 2 {
            return Ok(Self::None);
        }
        match data >> 4 {
            0x0 => Ok(Self::DSP),
            0x1 => Ok(Self::SuperFX),
            0x2 => Ok(Self::OBC1),
            0x3 => Ok(Self::SA1),
            0x4 => Ok(Self::SDD1),
            0x5 => Ok(Self::SRTC),
            0xE => Ok(Self::Other),
            0xF => Ok(Self::Custom),
            _ => Err(Self::Error::Parse(
                "failed to parse cart coprocessor".into(),
            )),
        }
    }
}

impl TryFrom<u8> for Region {
    type Error = Error;

    fn try_from(data: u8) -> Result<Self, Self::Error> {
        match data {
            0x00 => Ok(Self::Japan),
            0x01 => Ok(Self::USA),
            0x02 => Ok(Self::Europe),
            _ => Err(Self::Error::Parse("failed to parse cart region".into())),
        }
    }
}

impl TryFrom<&[u8]> for Vectors {
    type Error = Error;

    fn try_from(data: &[u8]) -> Result<Self, Self::Error> {
        Ok(Self {
            native: NativeVectors {
                cop: (&data[4..6]).read_u16::<LittleEndian>()?,
                brk: (&data[6..8]).read_u16::<LittleEndian>()?,
                abort: (&data[8..0xA]).read_u16::<LittleEndian>()?,
                nmi: (&data[0xA..0xC]).read_u16::<LittleEndian>()?,
                irq: (&data[0xE..0x10]).read_u16::<LittleEndian>()?,
            },
            emulation: EmulationVectors {
                cop: (&data[0x14..0x16]).read_u16::<LittleEndian>()?,
                abort: (&data[0x18..0x1A]).read_u16::<LittleEndian>()?,
                nmi: (&data[0x1A..0x1C]).read_u16::<LittleEndian>()?,
                reset: (&data[0x1C..0x1E]).read_u16::<LittleEndian>()?,
                irq_brk: (&data[0x1E..0x20]).read_u16::<LittleEndian>()?,
            },
        })
    }
}

/// Parse the version of the ROM.
///
/// It's just "1.[whatever the version flag is]".
fn parse_version(data: u8) -> String {
    format!("1.{data}")
}

impl<T: Read + Seek> CartRead for T {
    fn read_sfc_header(&mut self) -> Result<Cart, Error> {
        // Check file header, and 2 magic bytes
        let location = find_header(self)?;
        self.seek(SeekFrom::Start(location.into()))?;
        let mut data = [0u8; 80];
        self.read_exact(&mut data)?;

        let dev_id = data[0x2A];
        let expanded_header = if dev_id == 0x33 {
            // Uses extended header
            let maker_code = &data[0..2];
            let game_code = &data[2..6];
            let maker_code = if maker_code.is_ascii() {
                str::from_utf8(maker_code)?.to_string()
            } else {
                return Err(Error::Parse("maker code is not ASCII".into()));
            };

            let game_code = if game_code.is_ascii() {
                str::from_utf8(game_code)?.to_string()
            } else {
                return Err(Error::Parse("game code is not ASCII".into()));
            };

            Some(ExpandedHeader {
                maker_code,
                game_code,
                expansion_flash_size: 1 << data[0xC],
                expansion_ram_size: 1 << data[0xD],
                special_version: data[0xE],
                chipset_subtype: data[0xF],
            })
        } else {
            None
        };

        if data[0x27] > 31 || data[0x28] > 31 {
            return Err(Error::Parse("cart size too large".into()));
        }

        let rom_size: u32 = match data[0x27] {
            0 => 0,
            x => 1 << x,
        };
        let save_ram_size: u32 = match data[0x28] {
            0 => 0,
            x => 1 << x,
        };

        let title = &data[0x10..0x25];
        let title = if title.is_ascii() {
            trim(str::from_utf8(title)?).to_string()
        } else {
            return Err(Error::Parse("title is not ASCII".into()));
        };

        Ok(Cart {
            title,
            speed: data[0x25].into(),
            mode: data[0x25].try_into()?,
            chipset: data[0x26].try_into()?,
            coprocessor: data[0x26].try_into()?,
            rom_size,
            save_ram_size,
            region: data[0x29].try_into()?,
            dev_id: data[0x2A],
            version: parse_version(data[0x2B]),
            checksum: (&data[0x2E..0x30]).read_u16::<LittleEndian>()?,
            vectors: data[0x30..0x50].try_into()?,
            expanded_header,
        })
    }
}

#[cfg(test)]
mod tests {
    use crate::cart::Cart;
    use crate::cart::CartRead;
    use crate::cart::Chipset;
    use crate::cart::Coprocessor;
    use crate::cart::Mode;
    use crate::cart::Region;
    use crate::cart::Speed;

    use std::env;
    use std::fs::File;
    use std::io::Cursor;
    use std::io::Read;
    use std::path::Path;

    fn setup(filename: &str) -> Cursor<Vec<u8>> {
        let root = env::var("CARGO_MANIFEST_DIR").unwrap();
        let test_dir = Path::new(&root).join("resources/test");
        let mut file = File::open(test_dir.join(filename)).unwrap();
        let mut data = Vec::new();
        file.read_to_end(&mut data).unwrap();
        Cursor::new(data)
    }

    fn test_parse(data: &mut Cursor<Vec<u8>>) -> Cart {
        let cart = data.read_sfc_header();
        assert!(cart.is_ok());
        cart.unwrap()
    }

    #[test]
    fn test_lorom() {
        let mut sfc = setup("lorom.sfc");
        let cart = test_parse(&mut sfc);

        assert!(cart.title == "LoROM Test");
        assert!(matches!(cart.speed, Speed::Slow));
        assert!(matches!(cart.mode, Mode::Lorom));
        assert!(matches!(cart.chipset, Chipset::RomRam));
        assert!(matches!(cart.coprocessor, Coprocessor::None));
        assert!(cart.rom_size == 4096);
        assert!(cart.save_ram_size == 32);
        assert!(matches!(cart.region, Region::USA));
        assert!(cart.dev_id == 0xFF);
        assert!(cart.version == "1.3");
        assert!(cart.checksum == cart.compute_checksum(&mut sfc).unwrap());
    }

    #[test]
    fn test_hirom() {
        let mut sfc = setup("hirom.sfc");
        let cart = test_parse(&mut sfc);
        assert!(cart.title == "HiROM Test");
        assert!(matches!(cart.speed, Speed::Slow));
        assert!(matches!(cart.mode, Mode::Lorom));
        assert!(matches!(cart.chipset, Chipset::RomRam));
        assert!(matches!(cart.coprocessor, Coprocessor::None));
        assert!(cart.rom_size == 4096);
        assert!(cart.save_ram_size == 32);
        assert!(matches!(cart.region, Region::USA));
        assert!(cart.dev_id == 0xFF);
        assert!(cart.version == "1.3");
        assert!(cart.checksum == cart.compute_checksum(&mut sfc).unwrap());
    }

    #[test]
    fn test_exhirom() {
        let mut sfc = setup("exhirom.sfc");
        let cart = test_parse(&mut sfc);
        assert!(cart.title == "ExHiROM Test");
        assert!(matches!(cart.speed, Speed::Slow));
        assert!(matches!(cart.mode, Mode::Lorom));
        assert!(matches!(cart.chipset, Chipset::RomRam));
        assert!(matches!(cart.coprocessor, Coprocessor::None));
        assert!(cart.rom_size == 4096);
        assert!(cart.save_ram_size == 32);
        assert!(matches!(cart.region, Region::USA));
        assert!(cart.dev_id == 0xFF);
        assert!(cart.version == "1.3");
        assert!(cart.checksum == cart.compute_checksum(&mut sfc).unwrap());
    }
}