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
use super::mbc_type::MbcType;
use rtc::Rtc;

pub struct Cartridge {
    rom_banks: i32,
    ram_banks: i32,
    ram_size: i32,
    has_rtc: bool,
    has_battery: bool,
    rom: Vec<u8>,
    ram: Vec<u8>,
    name: String,
    mbc_type: MbcType,
    is_cgb: bool,
    rtc: Rtc,
    last_time: u64,
}

fn pow2ceil(i: i32) -> i32 {
    let mut i = i - 1;
    i |= i >> 1;
    i |= i >> 2;
    i |= i >> 4;
    i |= i >> 8;
    i += 1;
    i
}

impl Cartridge {
    pub fn from_rom(rom: Vec<u8>) -> Cartridge {
        let cartridge_type = i32::from(rom[0x0147]);

        let rom_banks = std::cmp::max(pow2ceil(rom.len() as i32 / 0x4000), 2);

        let ram_size = i32::from(rom[0x0149]);
        let ram_banks = match ram_size {
            0x0 => 0,
            0x1 => 1,
            0x2 => 1,
            0x3 => 4,
            0x4 => 16,
            _ => panic!("Unknown number of RAM banks"),
        };

        let has_rtc = match cartridge_type {
            0x0F | 0x10 => true,
            _ => false,
        };
        let has_battery = match cartridge_type {
            0x03 | 0x06 | 0x09 | 0x0D | 0x0F | 0x10 | 0x13 | 0x17 | 0x1E | 0x1B | 0x22 | 0xFD
            | 0xFF => true,
            _ => false,
        };

        let mut name = String::new();
        let mut name_index = 0x0134;
        while rom[name_index] != 0x00 && name_index < 0x0143 {
            let c = rom[name_index] as char;
            name.push(c);
            name_index += 1;
        }

        let mbc_type = match cartridge_type {
            0x00 | 0x08 | 0x09 => MbcType::RomOnly,
            0x01 | 0x02 | 0x03 | 0xEA | 0xFF => MbcType::Mbc1,
            0x05 | 0x06 => MbcType::Mbc2,
            0x0F | 0x10 | 0x11 | 0x12 | 0x13 | 0xFC => MbcType::Mbc3,
            0x19 | 0x1A | 0x1B | 0x1C | 0x1D | 0x1E => MbcType::Mbc5,
            _ => panic!("Unsupported cartridge type: {:?}", cartridge_type),
        };

        let is_cgb = rom[0x0143] == 0xC0 || rom[0x0143] == 0x80;

        let ram = match mbc_type {
            MbcType::Mbc2 => vec![0x0F; 0x200],
            MbcType::Mbc5 => vec![0xFF; 0x20000],
            _ => vec![0xFF; 0x8000],
        };

        Cartridge {
            rom_banks,
            ram_banks,
            ram_size,
            has_rtc,
            has_battery,
            rom,
            ram,
            name,
            mbc_type,
            is_cgb,
            rtc: Rtc::new(),
            last_time: 0,
        }
    }

    pub fn get_rom_banks(&self) -> i32 {
        self.rom_banks
    }

    pub fn get_ram_banks(&self) -> i32 {
        self.ram_banks
    }

    pub fn get_ram_size(&self) -> i32 {
        self.ram_size
    }

    pub fn has_rtc(&self) -> bool {
        self.has_rtc
    }

    pub fn has_battery(&self) -> bool {
        self.has_battery
    }

    pub fn get_rom(&self) -> &[u8] {
        self.rom.as_ref()
    }

    pub fn get_ram_mut(&mut self) -> &mut [u8] {
        self.ram.as_mut()
    }

    pub fn get_ram(&self) -> &[u8] {
        self.ram.as_ref()
    }

    pub fn set_ram(&mut self, ram: Vec<u8>) {
        self.ram = ram;
    }

    pub fn get_mbc_type(&self) -> MbcType {
        self.mbc_type
    }

    pub fn get_name(&self) -> &str {
        &self.name
    }

    pub fn is_cgb(&self) -> bool {
        self.is_cgb
    }

    pub fn get_last_timestamp(&self) -> (Rtc, u64) {
        (self.rtc, self.last_time)
    }

    pub fn set_last_timestamp(&mut self, rtc: Rtc, last_time: u64) {
        self.rtc.seconds = rtc.seconds;
        self.rtc.minutes = rtc.minutes;
        self.rtc.hours = rtc.hours;
        self.rtc.days_low = rtc.days_low;
        self.rtc.days_high = rtc.days_high;
        self.last_time = last_time;
    }
}