libgambatte: Refactor Memory

This commit is contained in:
MrWint 2019-05-26 14:17:22 +02:00
parent 51e8ba3cf5
commit ae98698758
5 changed files with 816 additions and 773 deletions

View File

@ -1206,6 +1206,7 @@ void gambatte::setInitState(SaveState &state, const bool cgb, const bool gbaCgbM
state.mem.lastOamDmaUpdate = disabled_time;
state.mem.unhaltTime = disabled_time;
state.mem.minIntTime = 0;
state.mem.halttime = 0;
state.mem.rombank = 1;
state.mem.dmaSource = 0;
state.mem.dmaDestination = 0;
@ -1217,6 +1218,7 @@ void gambatte::setInitState(SaveState &state, const bool cgb, const bool gbaCgbM
state.mem.rambankMode = false;
state.mem.hdmaTransfer = false;
state.mem.gbIsCgb = cgb;
state.mem.stopped = false;
for (int i = 0x00; i < 0x40; i += 0x02) {

File diff suppressed because it is too large Load Diff

View File

@ -1,30 +1,30 @@
/***************************************************************************
* Copyright (C) 2007 by Sindre Aamås *
* aamas@stud.ntnu.no *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License version 2 as *
* published by the Free Software Foundation. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License version 2 for more details. *
* *
* You should have received a copy of the GNU General Public License *
* version 2 along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
//
// Copyright (C) 2007 by sinamas <sinamas at users.sourceforge.net>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License version 2 for more details.
//
// You should have received a copy of the GNU General Public License
// version 2 along with this program; if not, write to the
// Free Software Foundation, Inc.,
// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
//
#ifndef MEMORY_H
#define MEMORY_H
static unsigned char const agbOverride[0xD] = { 0xFF, 0x00, 0xCD, 0x03, 0x35, 0xAA, 0x31, 0x90, 0x94, 0x00, 0x00, 0x00, 0x00 };
#include "mem/cartridge.h"
#include "video.h"
#include "sound.h"
#include "tima.h"
#include "video.h"
#include "newstate.h"
#include "gambatte.h"
@ -32,111 +32,59 @@ namespace gambatte {
class FilterInfo;
class Memory {
Cartridge cart;
unsigned char ioamhram[0x200];
unsigned char cgbBios[0x900];
unsigned char dmgBios[0x100];
bool biosMode;
bool cgbSwitching;
bool agbMode;
bool gbIsCgb_;
bool stopped;
unsigned short &SP;
unsigned short &PC;
unsigned long basetime;
unsigned long halttime;
MemoryCallback readCallback;
MemoryCallback writeCallback;
MemoryCallback execCallback;
CDCallback cdCallback;
void(*linkCallback)();
unsigned (*getInput)();
unsigned long divLastUpdate;
unsigned long lastOamDmaUpdate;
InterruptRequester intreq;
Tima tima;
LCD display;
PSG sound;
unsigned short dmaSource;
unsigned short dmaDestination;
unsigned char oamDmaPos;
unsigned char serialCnt;
bool blanklcd;
bool LINKCABLE;
bool linkClockTrigger;
void decEventCycles(IntEventId eventId, unsigned long dec);
void oamDmaInitSetup();
void updateOamDma(unsigned long cycleCounter);
void startOamDma(unsigned long cycleCounter);
void endOamDma(unsigned long cycleCounter);
const unsigned char * oamDmaSrcPtr() const;
unsigned nontrivial_ff_read(unsigned P, unsigned long cycleCounter);
unsigned nontrivial_read(unsigned P, unsigned long cycleCounter);
void nontrivial_ff_write(unsigned P, unsigned data, unsigned long cycleCounter);
void nontrivial_write(unsigned P, unsigned data, unsigned long cycleCounter);
unsigned nontrivial_peek(unsigned P);
unsigned nontrivial_ff_peek(unsigned P);
void updateSerial(unsigned long cc);
void updateTimaIrq(unsigned long cc);
void updateIrqs(unsigned long cc);
bool isDoubleSpeed() const { return display.isDoubleSpeed(); }
public:
explicit Memory(unsigned short &sp, unsigned short &pc);
bool loaded() const { return cart.loaded(); }
unsigned curRomBank() const { return cart.curRomBank(); }
char const * romTitle() const { return cart.romTitle(); }
int debugGetLY() const { return display.debugGetLY(); }
bool loaded() const { return cart_.loaded(); }
unsigned char curRomBank() const { return cart_.curRomBank(); }
char const * romTitle() const { return cart_.romTitle(); }
int debugGetLY() const { return lcd_.debugGetLY(); }
void setStatePtrs(SaveState &state);
void loadState(const SaveState &state/*, unsigned long oldCc*/);
void loadSavedata(char const *data) { cart.loadSavedata(data); }
int saveSavedataLength() {return cart.saveSavedataLength(); }
void saveSavedata(char *dest) { cart.saveSavedata(dest); }
void loadState(SaveState const &state);
void loadSavedata(char const *data) { cart_.loadSavedata(data); }
int saveSavedataLength() {return cart_.saveSavedataLength(); }
void saveSavedata(char *dest) { cart_.saveSavedata(dest); }
void updateInput();
unsigned char* cgbBiosBuffer() { return (unsigned char*)cgbBios; }
unsigned char* dmgBiosBuffer() { return (unsigned char*)dmgBios; }
unsigned char* cgbBiosBuffer() { return (unsigned char*)cgbBios_; }
unsigned char* dmgBiosBuffer() { return (unsigned char*)dmgBios_; }
bool gbIsCgb() { return gbIsCgb_; }
bool getMemoryArea(int which, unsigned char **data, int *length); // { return cart.getMemoryArea(which, data, length); }
unsigned long stop(unsigned long cycleCounter);
bool isCgb() const { return display.isCgb(); }
bool ime() const { return intreq.ime(); }
bool halted() const { return intreq.halted(); }
unsigned long nextEventTime() const { return intreq.minEventTime(); }
bool isCgb() const { return lcd_.isCgb(); }
bool ime() const { return intreq_.ime(); }
bool halted() const { return intreq_.halted(); }
unsigned long nextEventTime() const { return intreq_.minEventTime(); }
void setLayers(unsigned mask) { lcd_.setLayers(mask); }
bool isActive() const { return intreq_.eventTime(intevent_end) != disabled_time; }
void setLayers(unsigned mask) { display.setLayers(mask); }
long cyclesSinceBlit(unsigned long cc) const {
if (cc < intreq_.eventTime(intevent_blit))
return -1;
bool isActive() const { return intreq.eventTime(intevent_end) != disabled_time; }
long cyclesSinceBlit(const unsigned long cc) const {
return cc < intreq.eventTime(intevent_blit) ? -1 : static_cast<long>((cc - intreq.eventTime(intevent_blit)) >> isDoubleSpeed());
return (cc - intreq_.eventTime(intevent_blit)) >> isDoubleSpeed();
}
void halt(unsigned long cycleCounter) { halttime = cycleCounter; intreq.halt(); }
void ei(unsigned long cycleCounter) { if (!ime()) { intreq.ei(cycleCounter); } }
void halt(unsigned long cycleCounter) { halttime_ = cycleCounter; intreq_.halt(); }
void ei(unsigned long cycleCounter) { if (!ime()) { intreq_.ei(cycleCounter); } }
void di() { intreq_.di(); }
void di() { intreq.di(); }
unsigned readBios(unsigned p) {
if (gbIsCgb_) {
if (agbMode_ && p >= 0xF3 && p < 0x100) {
return (agbOverride[p - 0xF3] + cgbBios_[p]) & 0xFF;
}
return cgbBios_[p];
}
return dmgBios_[p];
}
unsigned ff_read(const unsigned P, const unsigned long cycleCounter) {
if (readCallback)
readCallback(P, (cycleCounter - basetime) >> 1);
return P < 0x80 ? nontrivial_ff_read(P, cycleCounter) : ioamhram[P + 0x100];
unsigned ff_read(unsigned p, unsigned long cc) {
if (readCallback_)
readCallback_(p, (cc - basetime_) >> 1);
return p < 0x80 ? nontrivial_ff_read(p, cc) : ioamhram_[p + 0x100];
}
struct CDMapResult
@ -145,30 +93,30 @@ public:
unsigned addr;
};
CDMapResult CDMap(const unsigned P) const
CDMapResult CDMap(const unsigned p) const
{
if(P<0x4000)
if(p < 0x4000)
{
CDMapResult ret = { eCDLog_AddrType_ROM, P };
CDMapResult ret = { eCDLog_AddrType_ROM, p };
return ret;
}
else if(P<0x8000)
else if(p < 0x8000)
{
unsigned bank = cart.rmem(P>>12) - cart.rmem(0);
unsigned addr = P+bank;
unsigned bank = cart_.rmem(p >> 12) - cart_.rmem(0);
unsigned addr = p + bank;
CDMapResult ret = { eCDLog_AddrType_ROM, addr };
return ret;
}
else if(P<0xA000) {}
else if(P<0xC000)
else if(p < 0xA000) {}
else if(p < 0xC000)
{
if(cart.wsrambankptr())
if(cart_.wsrambankptr())
{
//not bankable. but. we're not sure how much might be here
unsigned char *data;
int length;
bool has = cart.getMemoryArea(3,&data,&length);
unsigned addr = P&(length-1);
bool has = cart_.getMemoryArea(3,&data,&length);
unsigned addr = p & (length-1);
if(has && length!=0)
{
CDMapResult ret = { eCDLog_AddrType_CartRAM, addr };
@ -176,14 +124,14 @@ public:
}
}
}
else if(P<0xE000)
else if(p < 0xE000)
{
unsigned bank = cart.wramdata(P >> 12 & 1) - cart.wramdata(0);
unsigned addr = (P&0xFFF)+bank;
unsigned bank = cart_.wramdata(p >> 12 & 1) - cart_.wramdata(0);
unsigned addr = (p & 0xFFF) + bank;
CDMapResult ret = { eCDLog_AddrType_WRAM, addr };
return ret;
}
else if(P<0xFF80) {}
else if(p < 0xFF80) {}
else
{
////this is just for debugging, really, it's pretty useless
@ -195,157 +143,190 @@ public:
return ret;
}
unsigned readBios(const unsigned P) {
if (gbIsCgb_) {
if (agbMode && P >= 0xF3 && P < 0x100) {
return (agbOverride[P - 0xF3] + cgbBios[P]) & 0xFF;
}
return cgbBios[P];
}
return dmgBios[P];
}
unsigned read(const unsigned P, const unsigned long cycleCounter) {
if (readCallback)
readCallback(P, (cycleCounter - basetime) >> 1);
bool biosRange = ((!gbIsCgb_ && P < 0x100) || (gbIsCgb_ && P < 0x900 && (P < 0x100 || P >= 0x200)));
if(biosMode) {
unsigned read(unsigned p, unsigned long cc) {
if (readCallback_)
readCallback_(p, (cc - basetime_) >> 1);
bool biosRange = ((!gbIsCgb_ && p < 0x100) || (gbIsCgb_ && p < 0x900 && (p < 0x100 || p >= 0x200)));
if(biosMode_) {
if (biosRange)
return readBios(P);
return readBios(p);
}
else
{
if(cdCallback)
{
CDMapResult map = CDMap(P);
if(map.type != eCDLog_AddrType_None)
cdCallback(map.addr,map.type,eCDLog_Flags_Data);
}
else if(cdCallback_) {
CDMapResult map = CDMap(p);
if(map.type != eCDLog_AddrType_None)
cdCallback_(map.addr, map.type, eCDLog_Flags_Data);
}
return cart.rmem(P >> 12) ? cart.rmem(P >> 12)[P] : nontrivial_read(P, cycleCounter);
return cart_.rmem(p >> 12) ? cart_.rmem(p >> 12)[p] : nontrivial_read(p, cc);
}
unsigned read_excb(const unsigned P, const unsigned long cycleCounter, bool first) {
if (execCallback)
execCallback(P, (cycleCounter - basetime) >> 1);
bool biosRange = ((!gbIsCgb_ && P < 0x100) || (gbIsCgb_ && P < 0x900 && (P < 0x100 || P >= 0x200)));
if (biosMode) {
unsigned read_excb(unsigned p, unsigned long cc, bool first) {
if (execCallback_)
execCallback_(p, (cc - basetime_) >> 1);
bool biosRange = ((!gbIsCgb_ && p < 0x100) || (gbIsCgb_ && p < 0x900 && (p < 0x100 || p >= 0x200)));
if (biosMode_) {
if(biosRange)
return readBios(P);
return readBios(p);
}
else
{
if(cdCallback)
{
CDMapResult map = CDMap(P);
if(map.type != eCDLog_AddrType_None)
cdCallback(map.addr,map.type,first?eCDLog_Flags_ExecFirst : eCDLog_Flags_ExecOperand);
}
}
return cart.rmem(P >> 12) ? cart.rmem(P >> 12)[P] : nontrivial_read(P, cycleCounter);
}
unsigned peek(const unsigned P) {
if (biosMode && ((!gbIsCgb_ && P < 0x100) || (gbIsCgb_ && P < 0x900 && (P < 0x100 || P >= 0x200)))) {
return readBios(P);
}
return cart.rmem(P >> 12) ? cart.rmem(P >> 12)[P] : nontrivial_peek(P);
}
void write_nocb(const unsigned P, const unsigned data, const unsigned long cycleCounter) {
if (cart.wmem(P >> 12)) {
cart.wmem(P >> 12)[P] = data;
} else
nontrivial_write(P, data, cycleCounter);
}
void write(const unsigned P, const unsigned data, const unsigned long cycleCounter) {
if (cart.wmem(P >> 12)) {
cart.wmem(P >> 12)[P] = data;
} else
nontrivial_write(P, data, cycleCounter);
if (writeCallback)
writeCallback(P, (cycleCounter - basetime) >> 1);
if(cdCallback && !biosMode)
{
CDMapResult map = CDMap(P);
else if(cdCallback_) {
CDMapResult map = CDMap(p);
if(map.type != eCDLog_AddrType_None)
cdCallback(map.addr,map.type,eCDLog_Flags_Data);
cdCallback_(map.addr, map.type, first ? eCDLog_Flags_ExecFirst : eCDLog_Flags_ExecOperand);
}
return cart_.rmem(p >> 12) ? cart_.rmem(p >> 12)[p] : nontrivial_read(p, cc);
}
unsigned peek(unsigned p) {
bool biosRange = ((!gbIsCgb_ && p < 0x100) || (gbIsCgb_ && p < 0x900 && (p < 0x100 || p >= 0x200)));
if (biosMode_ && biosRange) {
return readBios(p);
}
return cart_.rmem(p >> 12) ? cart_.rmem(p >> 12)[p] : nontrivial_peek(p);
}
void write_nocb(unsigned p, unsigned data, unsigned long cc) {
if (cart_.wmem(p >> 12)) {
cart_.wmem(p >> 12)[p] = data;
} else
nontrivial_write(p, data, cc);
}
void write(unsigned p, unsigned data, unsigned long cc) {
if (cart_.wmem(p >> 12)) {
cart_.wmem(p >> 12)[p] = data;
} else
nontrivial_write(p, data, cc);
if (writeCallback_)
writeCallback_(p, (cc - basetime_) >> 1);
if(cdCallback_ && !biosMode_) {
CDMapResult map = CDMap(p);
if(map.type != eCDLog_AddrType_None)
cdCallback_(map.addr, map.type, eCDLog_Flags_Data);
}
}
void ff_write(const unsigned P, const unsigned data, const unsigned long cycleCounter) {
if (P - 0x80u < 0x7Fu) {
ioamhram[P + 0x100] = data;
void ff_write(unsigned p, unsigned data, unsigned long cc) {
if (p - 0x80u < 0x7Fu) {
ioamhram_[p + 0x100] = data;
} else
nontrivial_ff_write(P, data, cycleCounter);
if (writeCallback)
writeCallback(P, (cycleCounter - basetime) >> 1);
if(cdCallback && !biosMode)
nontrivial_ff_write(p, data, cc);
if (writeCallback_)
writeCallback_(0xff00 + p, (cc - basetime_) >> 1);
if(cdCallback_ && !biosMode_)
{
CDMapResult map = CDMap(P);
CDMapResult map = CDMap(0xff00 + p);
if(map.type != eCDLog_AddrType_None)
cdCallback(map.addr,map.type,eCDLog_Flags_Data);
cdCallback_(map.addr, map.type, eCDLog_Flags_Data);
}
}
unsigned long event(unsigned long cycleCounter);
unsigned long resetCounters(unsigned long cycleCounter);
LoadRes loadROM(char const *romfiledata, unsigned romfilelength, bool forceDmg, bool multicartCompat);
void setInputGetter(unsigned (*getInput)()) {
this->getInput = getInput;
getInput_ = getInput;
}
void setReadCallback(MemoryCallback callback) {
this->readCallback = callback;
this->readCallback_ = callback;
}
void setWriteCallback(MemoryCallback callback) {
this->writeCallback = callback;
this->writeCallback_ = callback;
}
void setExecCallback(MemoryCallback callback) {
this->execCallback = callback;
this->execCallback_ = callback;
}
void setCDCallback(CDCallback cdc) {
this->cdCallback = cdc;
this->cdCallback_ = cdc;
}
void setScanlineCallback(void (*callback)(), int sl) {
display.setScanlineCallback(callback, sl);
lcd_.setScanlineCallback(callback, sl);
}
void setRTCCallback(std::uint32_t (*callback)()) {
cart.setRTCCallback(callback);
cart_.setRTCCallback(callback);
}
void setLinkCallback(void(*callback)()) {
this->linkCallback = callback;
this->linkCallback_ = callback;
}
void setBasetime(unsigned long cc) { basetime = cc; }
void setEndtime(unsigned long cc, unsigned long inc);
void setBasetime(unsigned long cc) { basetime_ = cc; }
void setSoundBuffer(uint_least32_t *const buf) { sound.setBuffer(buf); }
void setSoundBuffer(uint_least32_t *buf) { psg_.setBuffer(buf); }
std::size_t fillSoundBuffer(unsigned long cc);
void setVideoBuffer(uint_least32_t *const videoBuf, const std::ptrdiff_t pitch) {
display.setVideoBuffer(videoBuf, pitch);
void setVideoBuffer(uint_least32_t *videoBuf, std::ptrdiff_t pitch) {
lcd_.setVideoBuffer(videoBuf, pitch);
}
void setDmgPaletteColor(int palNum, int colorNum, unsigned long rgb32) {
display.setDmgPaletteColor(palNum, colorNum, rgb32);
lcd_.setDmgPaletteColor(palNum, colorNum, rgb32);
}
void setCgbPalette(unsigned *lut);
void blackScreen() {
display.blackScreen();
lcd_.blackScreen();
}
int linkStatus(int which);
private:
Cartridge cart_;
unsigned char ioamhram_[0x200];
unsigned char cgbBios_[0x900];
unsigned char dmgBios_[0x100];
unsigned (*getInput_)();
unsigned long divLastUpdate_;
unsigned long lastOamDmaUpdate_;
InterruptRequester intreq_;
Tima tima_;
LCD lcd_;
PSG psg_;
unsigned short dmaSource_;
unsigned short dmaDestination_;
unsigned char oamDmaPos_;
unsigned char serialCnt_;
bool blanklcd_;
bool biosMode_;
bool cgbSwitching_;
bool agbMode_;
bool gbIsCgb_;
unsigned short &sp_;
unsigned short &pc_;
unsigned long basetime_;
unsigned long halttime_;
bool stopped_;
MemoryCallback readCallback_;
MemoryCallback writeCallback_;
MemoryCallback execCallback_;
CDCallback cdCallback_;
void(*linkCallback_)();
bool LINKCABLE_;
bool linkClockTrigger_;
void decEventCycles(IntEventId eventId, unsigned long dec);
void oamDmaInitSetup();
void updateOamDma(unsigned long cycleCounter);
void startOamDma(unsigned long cycleCounter);
void endOamDma(unsigned long cycleCounter);
unsigned char const * oamDmaSrcPtr() const;
unsigned nontrivial_ff_read(unsigned p, unsigned long cycleCounter);
unsigned nontrivial_read(unsigned p, unsigned long cycleCounter);
void nontrivial_ff_write(unsigned p, unsigned data, unsigned long cycleCounter);
void nontrivial_write(unsigned p, unsigned data, unsigned long cycleCounter);
unsigned nontrivial_peek(unsigned p);
unsigned nontrivial_ff_peek(unsigned p);
void updateSerial(unsigned long cc);
void updateTimaIrq(unsigned long cc);
void updateIrqs(unsigned long cc);
bool isDoubleSpeed() const { return lcd_.isDoubleSpeed(); }
public:
template<bool isReader>void SyncState(NewState *ns);
};

View File

@ -70,6 +70,7 @@ struct SaveState {
unsigned long lastOamDmaUpdate;
unsigned long minIntTime;
unsigned long unhaltTime;
unsigned long halttime;
unsigned short rombank;
unsigned short dmaSource;
unsigned short dmaDestination;
@ -84,6 +85,7 @@ struct SaveState {
unsigned char /*bool*/ cgbSwitching;
unsigned char /*bool*/ agbMode;
unsigned char /*bool*/ gbIsCgb;
unsigned char /*bool*/ stopped;
} mem;
struct PPU {

Binary file not shown.