2015-11-21 07:36:48 +00:00
|
|
|
auto Cartridge::MBC3::second() -> void {
|
2016-06-28 10:43:47 +00:00
|
|
|
if(!rtc.halt) {
|
|
|
|
if(++rtc.second >= 60) {
|
|
|
|
rtc.second = 0;
|
|
|
|
if(++rtc.minute >= 60) {
|
|
|
|
rtc.minute = 0;
|
|
|
|
if(++rtc.hour >= 24) {
|
|
|
|
rtc.hour = 0;
|
|
|
|
if(++rtc.day >= 512) {
|
|
|
|
rtc.day = 0;
|
|
|
|
rtc.dayCarry = true;
|
2011-01-04 10:42:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-28 10:43:47 +00:00
|
|
|
auto Cartridge::MBC3::readIO(uint16 addr) -> uint8 {
|
2011-12-26 10:49:48 +00:00
|
|
|
if((addr & 0xc000) == 0x0000) { //$0000-3fff
|
2016-06-28 10:43:47 +00:00
|
|
|
return cartridge.readROM(addr);
|
2011-01-04 10:42:27 +00:00
|
|
|
}
|
|
|
|
|
2011-12-26 10:49:48 +00:00
|
|
|
if((addr & 0xc000) == 0x4000) { //$4000-7fff
|
2016-06-28 10:43:47 +00:00
|
|
|
return cartridge.readROM(rom.select<< 14 | (uint14)addr);
|
2011-01-04 10:42:27 +00:00
|
|
|
}
|
|
|
|
|
2011-12-26 10:49:48 +00:00
|
|
|
if((addr & 0xe000) == 0xa000) { //$a000-bfff
|
2016-06-28 10:43:47 +00:00
|
|
|
if(ram.enable) {
|
|
|
|
if(ram.select <= 0x03) {
|
|
|
|
return cartridge.readRAM(ram.select << 13 | (uint13)addr);
|
2011-01-04 10:42:27 +00:00
|
|
|
}
|
2016-06-28 10:43:47 +00:00
|
|
|
if(ram.select == 0x08) return rtc.latchSecond;
|
|
|
|
if(ram.select == 0x09) return rtc.latchMinute;
|
|
|
|
if(ram.select == 0x0a) return rtc.latchHour;
|
|
|
|
if(ram.select == 0x0b) return rtc.latchDay;
|
|
|
|
if(ram.select == 0x0c) return rtc.latchDayCarry << 7 | rtc.latchDay >> 8;
|
2011-01-04 10:42:27 +00:00
|
|
|
}
|
2016-01-08 09:23:46 +00:00
|
|
|
return 0xff;
|
2011-01-04 10:42:27 +00:00
|
|
|
}
|
|
|
|
|
2016-01-08 09:23:46 +00:00
|
|
|
return 0xff;
|
2011-01-04 10:42:27 +00:00
|
|
|
}
|
|
|
|
|
2016-06-28 10:43:47 +00:00
|
|
|
auto Cartridge::MBC3::writeIO(uint16 addr, uint8 data) -> void {
|
2011-12-26 10:49:48 +00:00
|
|
|
if((addr & 0xe000) == 0x0000) { //$0000-1fff
|
2016-06-28 10:43:47 +00:00
|
|
|
ram.enable = (data & 0x0f) == 0x0a;
|
2011-01-04 10:42:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-26 10:49:48 +00:00
|
|
|
if((addr & 0xe000) == 0x2000) { //$2000-3fff
|
2016-06-28 10:43:47 +00:00
|
|
|
rom.select = (data & 0x7f) + ((data & 0x7f) == 0);
|
2011-01-04 10:42:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-26 10:49:48 +00:00
|
|
|
if((addr & 0xe000) == 0x4000) { //$4000-5fff
|
2016-06-28 10:43:47 +00:00
|
|
|
ram.select = data;
|
2011-01-04 10:42:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-26 10:49:48 +00:00
|
|
|
if((addr & 0xe000) == 0x6000) { //$6000-7fff
|
2016-06-28 10:43:47 +00:00
|
|
|
if(rtc.latch == 0 && data == 1) {
|
|
|
|
rtc.latchSecond = rtc.second;
|
|
|
|
rtc.latchMinute = rtc.minute;
|
|
|
|
rtc.latchHour = rtc.hour;
|
|
|
|
rtc.latchDay = rtc.day;
|
|
|
|
rtc.latchDayCarry = rtc.dayCarry;
|
2011-01-04 10:42:27 +00:00
|
|
|
}
|
2016-06-28 10:43:47 +00:00
|
|
|
rtc.latch = data;
|
2011-01-04 10:42:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-26 10:49:48 +00:00
|
|
|
if((addr & 0xe000) == 0xa000) { //$a000-bfff
|
2016-06-28 10:43:47 +00:00
|
|
|
if(ram.enable) {
|
|
|
|
if(ram.select <= 0x03) {
|
|
|
|
cartridge.writeRAM(ram.select << 13 | (uint13)addr, data);
|
|
|
|
} else if(ram.select == 0x08) {
|
2011-01-04 10:42:27 +00:00
|
|
|
if(data >= 60) data = 0;
|
2016-06-28 10:43:47 +00:00
|
|
|
rtc.second = data;
|
|
|
|
} else if(ram.select == 0x09) {
|
2011-01-04 10:42:27 +00:00
|
|
|
if(data >= 60) data = 0;
|
2016-06-28 10:43:47 +00:00
|
|
|
rtc.minute = data;
|
|
|
|
} else if(ram.select == 0x0a) {
|
2011-01-04 10:42:27 +00:00
|
|
|
if(data >= 24) data = 0;
|
2016-06-28 10:43:47 +00:00
|
|
|
rtc.hour = data;
|
|
|
|
} else if(ram.select == 0x0b) {
|
|
|
|
rtc.day = (rtc.day & 0x0100) | data;
|
|
|
|
} else if(ram.select == 0x0c) {
|
|
|
|
rtc.day = ((data & 1) << 8) | (rtc.day & 0xff);
|
|
|
|
rtc.halt = data & 0x40;
|
|
|
|
rtc.dayCarry = data & 0x80;
|
2011-01-04 10:42:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-21 07:36:48 +00:00
|
|
|
auto Cartridge::MBC3::power() -> void {
|
2016-06-28 10:43:47 +00:00
|
|
|
rom.select = 0x01;
|
2011-01-04 10:42:27 +00:00
|
|
|
|
2016-06-28 10:43:47 +00:00
|
|
|
ram.enable = false;
|
|
|
|
ram.select = 0x00;
|
2011-01-04 10:42:27 +00:00
|
|
|
|
2016-06-28 10:43:47 +00:00
|
|
|
rtc.latch = 0;
|
|
|
|
|
|
|
|
rtc.halt = true;
|
|
|
|
rtc.second = 0;
|
|
|
|
rtc.minute = 0;
|
|
|
|
rtc.hour = 0;
|
|
|
|
rtc.day = 0;
|
|
|
|
rtc.dayCarry = false;
|
|
|
|
|
|
|
|
rtc.latchSecond = 0;
|
|
|
|
rtc.latchMinute = 0;
|
|
|
|
rtc.latchHour = 0;
|
|
|
|
rtc.latchDay = 0;
|
|
|
|
rtc.latchDayCarry = false;
|
2011-01-04 10:42:27 +00:00
|
|
|
}
|