bsnes/higan/ws/eeprom/eeprom.cpp

142 lines
3.0 KiB
C++
Raw Normal View History

#include <ws/ws.hpp>
namespace WonderSwan {
Update to v097r28 release. byuu says: Changelog: (all WSC unless otherwise noted) - fixed LINECMP=0 interrupt case (fixes FF4 world map during airship sequence) - improved CPU timing (fixes Magical Drop flickering and FF1 battle music) - added per-frame OAM caching (fixes sprite glitchiness in Magical Drop, Riviera, etc.) - added RTC emulation (fixes Dicing Knight and Judgement Silversword) - added save state support - added cheat code support (untested because I don't know of any cheat codes that exist for this system) - icarus: can now detect games with RTC chips - SFC: bugfix to SharpRTC emulation (Dai Kaijuu Monogatari II) - ( I was adding the extra leap year day to all 12 months instead of just February ... >_< ) Note that the RTC emulation is very incomplete. It's not really documented at all, and the two games I've tried that use it never even ask you to set the date/time (so they're probably just using it to count seconds.) I'm not even sure if I've implement the level-sensitive behavior correctly (actually, now that I think about it, I need to mask the clear bit in INT_ACK for the level-sensitive interrupts ...) A bit worried about the RTC alarm, because it seems like it'll fire continuously for a full minute. Or even if you turn it off after it fires, then that doesn't seem to be lowering the line until the next second ticks on the RTC, so that likely needs to happen when changing the alarm flag. Also not sure on this RTC's weekday byte. On the SharpRTC, it actually computes this for you. Because it's not at all an easy thing to calculate yourself in 65816 or V30MZ assembler. About 40 lines of code to do it in C. For now, I'm requiring the program to calculate the value itself. Also note that there's some gibberish tiles in Judgement Silversword, sadly. Not sure what's up there, but the game's still fully playable at least. Finally, no surprise: Beat-Mania doesn't run :P
2016-03-25 06:19:08 +00:00
#include "serialization.cpp"
auto EEPROM::name() const -> string { return _name; }
auto EEPROM::data() -> uint16* { return _data; }
auto EEPROM::size() const -> uint { return _size; }
auto EEPROM::setName(string name) -> void {
_name = name;
}
auto EEPROM::setSize(uint size) -> void {
_size = bit::round(size);
}
auto EEPROM::erase() -> void {
for(auto& word : _data) word = 0xffff;
}
auto EEPROM::power() -> void {
r.latch = 0;
r.address = 0;
r.unknown = false;
r.writeRequested = false;
r.readRequested = false;
r.writeCompleted = true; //hack: should require time to complete reads/writes
r.readCompleted = true; //instead; leave bits always set so games don't lock
r.writeProtect = true;
}
auto EEPROM::read(uint port) -> uint8 {
if(!_size) return 0x00;
if(port == DataLo) return r.latch.byte(0);
if(port == DataHi) return r.latch.byte(1);
if(port == AddressLo) return r.address.byte(0);
if(port == AddressHi) return r.address.byte(1);
if(port == Status) return (
1 << 7
| r.unknown << 6
| r.writeRequested << 5
| r.readRequested << 4
| r.writeCompleted << 1
| r.readCompleted << 0
);
return 0x00;
}
auto EEPROM::write(uint port, uint8 data) -> void {
if(!_size) return;
if(port == DataLo) {
r.latch.byte(0) = data;
return;
}
if(port == DataHi) {
r.latch.byte(1) = data;
return;
}
if(port == AddressLo) {
r.address.byte(0) = data;
return;
}
if(port == AddressHi) {
r.address.byte(1) = data;
return;
}
if(port == Command) {
r.unknown = data.bit(6);
r.writeRequested = data.bit(5);
r.readRequested = data.bit(4);
execute();
return;
}
}
auto EEPROM::execute() -> void {
auto bits = bit::first(_size);
auto address = r.address.bits(0, bits - 1);
auto special = r.address.bits(bits - 2, bits - 1);
auto command = r.address.bits(bits, bits + 1);
auto start = r.address.bit(bits + 2);
if(!start) return;
//write disable
if(command == 0 && special == 0) {
r.writeProtect = true;
}
//write all
if(command == 0 && special == 1 && !r.writeProtect) {
for(auto n : range(_size)) _data[n] = r.latch;
}
//erase all
if(command == 0 && special == 2 && !r.writeProtect) {
for(auto n : range(_size)) _data[n] = 0xffff;
}
//write enable
if(command == 0 && special == 3) {
r.writeProtect = false;
}
//write word
if(command == 1 && r.writeRequested && !r.writeProtect) {
_data[address] = r.latch;
r.writeRequested = false;
r.writeCompleted = true;
}
//read word
if(command == 2 && r.readRequested) {
r.latch = _data[address];
r.readRequested = false;
r.readCompleted = true;
}
//erase word
if(command == 3 && r.writeRequested && !r.writeProtect) {
_data[address] = 0xffff;
r.writeRequested = false;
r.writeCompleted = true;
}
}
auto EEPROM::operator[](uint10 addr) -> uint16& {
return _data[addr];
}
}