Mesen/Core/VRC3.h

76 lines
1.9 KiB
C
Raw Normal View History

2016-01-23 18:07:56 +00:00
#pragma once
#include "stdafx.h"
#include "BaseMapper.h"
#include "CPU.h"
class VRC3 : public BaseMapper
{
private:
bool _irqEnableOnAck = false;
bool _irqEnabled = false;
bool _smallCounter = false;
uint16_t _irqReload = 0;
uint16_t _irqCounter = 0;
protected:
2016-12-18 04:14:47 +00:00
virtual uint16_t GetPRGPageSize() override { return 0x4000; }
virtual uint16_t GetCHRPageSize() override { return 0x2000; }
2016-01-23 18:07:56 +00:00
2016-12-18 04:14:47 +00:00
void InitMapper() override
2016-01-23 18:07:56 +00:00
{
SelectPRGPage(1, -1);
SelectCHRPage(0, 0);
}
2016-12-18 04:14:47 +00:00
virtual void StreamState(bool saving) override
2016-01-23 18:07:56 +00:00
{
BaseMapper::StreamState(saving);
Stream(_irqEnableOnAck, _smallCounter, _irqEnabled, _irqCounter, _irqReload);
2016-01-23 18:07:56 +00:00
}
2016-12-18 04:14:47 +00:00
virtual void ProcessCpuClock() override
2016-01-23 18:07:56 +00:00
{
if(_irqEnabled) {
if(_smallCounter) {
uint8_t smallCounter = _irqCounter & 0xFF;
smallCounter++;
if(_smallCounter == 0) {
smallCounter = _irqReload & 0xFF;
CPU::SetIRQSource(IRQSource::External);
}
_irqCounter = (_irqCounter & 0xFF00) | smallCounter;
} else {
_irqCounter++;
if(_irqCounter == 0) {
_irqCounter = _irqReload;
CPU::SetIRQSource(IRQSource::External);
}
}
}
}
2016-12-18 04:14:47 +00:00
void WriteRegister(uint16_t addr, uint8_t value) override
2016-01-23 18:07:56 +00:00
{
switch(addr & 0xF000) {
case 0x8000: _irqReload = (_irqReload & 0xFFF0) | (value & 0x0F); break;
case 0x9000: _irqReload = (_irqReload & 0xFF0F) | ((value & 0x0F) << 4); break;
case 0xA000: _irqReload = (_irqReload & 0xF0FF) | ((value & 0x0F) << 8); break;
case 0xB000: _irqReload = (_irqReload & 0x0FFF) | ((value & 0x0F) << 12); break;
case 0xC000:
_irqEnabled = (value & 0x02) == 0x02;
if(_irqEnabled) {
_irqCounter = _irqReload;
}
_smallCounter = (value & 0x04) == 0x04;
_irqEnableOnAck = (value & 0x01) == 0x01;
CPU::ClearIRQSource(IRQSource::External);
break;
case 0xD000:
CPU::ClearIRQSource(IRQSource::External);
_irqEnabled = _irqEnableOnAck;
break;
case 0xF000: SelectPRGPage(0, value & 0x07); break;
}
}
};