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
//! ROM loader for NES cartridge emulation.
//!
//! This crate provides functionality for loading and parsing NES ROM files in the iNES format.
//! It handles the details of reading ROM data and creating appropriate cartridge implementations
//! based on the mapper type specified in the ROM header.
//!
//! # Features
//!
//! * Supports iNES format ROM files
//! * Parses ROM headers to determine cartridge configuration
//! * Implements multiple mapper types (0, 1, 2, 3)
//! * Provides error handling for invalid ROM files
//!
//! # Example
//!
//! ```no_run
//! # use std::path::Path;
//! # use std::fs;
//! use cge_nes::rom_loader::load_rom;
//!
//! let mut rom_file = fs::File::open(Path::new("super_mario.nes")).unwrap();
//! match load_rom(&mut rom_file) {
//! Ok(cartridge) => println!("ROM loaded successfully!"),
//! Err(e) => println!("Failed to load ROM: {}", e),
//! }
//! ```
//!
//! # Supported Mappers
//!
//! * Mapper 0: NROM - Nintendo's original mapper
//! * Mapper 1: MMC1 - Memory Management Controller 1
//! * Mapper 2: UxROM
//! * Mapper 3: CNROM
//! * Mapper 4: MMC3 - Memory Management Controller 3
//!
//! # Error Handling
//!
//! The crate uses a custom [`RomError`] type to handle both I/O errors and
//! format validation errors that may occur during ROM loading.
pub use RomError;
use crateCartridge;
use *;
use Read;
/// Result type for ROM loading operations, returns either a boxed Cartridge trait object or a RomError
pub type LoadRomResult = ;
/// Loads and parses an NES ROM file, creating an appropriate cartridge implementation.
///
/// # Arguments
///
/// * `rom_reader` - A reader providing access to the ROM data
///
/// # Returns
///
/// Returns a `LoadRomResult` which is either:
/// * `Ok(Box<dyn Cartridge>)` - A boxed cartridge implementation for the loaded ROM
/// * `Err(RomError)` - An error describing what went wrong during loading
///
/// # Errors
///
/// This function will return an error if:
/// * The ROM data cannot be read
/// * The data doesn't start with the NES magic constant
/// * The data is too small to contain the declared PRG and CHR ROM data
/// * The mapper number is not implemented (see
/// [`RomError::UnsupportedMapper`])
///
/// # Example
///
/// ```no_run
/// # use std::path::Path;
/// # use std::fs;
/// use cge_nes::rom_loader::load_rom;
///
/// let mut rom_file = fs::File::open(Path::new("super_mario.nes")).unwrap();
/// match load_rom(&mut rom_file) {
/// Ok(cartridge) => println!("ROM loaded successfully!"),
/// Err(e) => println!("Failed to load ROM: {}", e),
/// }
/// ```