2012-09-04 00:20:36 +00:00
|
|
|
#include <snes/snes.hpp>
|
|
|
|
|
|
|
|
#define SMP_CPP
|
|
|
|
namespace SNES {
|
|
|
|
|
|
|
|
SMP smp;
|
|
|
|
|
|
|
|
#include "serialization.cpp"
|
|
|
|
#include "iplrom.cpp"
|
|
|
|
#include "memory/memory.cpp"
|
|
|
|
#include "timing/timing.cpp"
|
|
|
|
|
|
|
|
void SMP::step(unsigned clocks) {
|
|
|
|
clock += clocks * (uint64)cpu.frequency;
|
|
|
|
dsp.clock -= clocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMP::synchronize_cpu() {
|
2012-12-24 18:07:13 +00:00
|
|
|
if(CPU::Threaded == true) {
|
2012-09-04 00:20:36 +00:00
|
|
|
if(clock >= 0 && scheduler.sync != Scheduler::SynchronizeMode::All) co_switch(cpu.thread);
|
|
|
|
} else {
|
|
|
|
while(clock >= 0) cpu.enter();
|
|
|
|
}
|
2012-12-24 18:07:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SMP::synchronize_cpu_force() {
|
|
|
|
if(CPU::Threaded == true) {
|
|
|
|
if(clock >= 0 && scheduler.sync != Scheduler::SynchronizeMode::All)
|
|
|
|
co_switch(cpu.thread);
|
|
|
|
else if(clock >= 0 && scheduler.sync == Scheduler::SynchronizeMode::All)
|
2012-12-25 20:36:04 +00:00
|
|
|
interface()->message("SMP had to advance nondeterministically!");
|
2012-12-24 18:07:13 +00:00
|
|
|
} else {
|
|
|
|
while(clock >= 0) cpu.enter();
|
|
|
|
}
|
|
|
|
}
|
2012-09-04 00:20:36 +00:00
|
|
|
|
|
|
|
void SMP::synchronize_dsp() {
|
|
|
|
if(DSP::Threaded == true) {
|
|
|
|
if(dsp.clock < 0 && scheduler.sync != Scheduler::SynchronizeMode::All) co_switch(dsp.thread);
|
|
|
|
} else {
|
|
|
|
while(dsp.clock < 0) dsp.enter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMP::Enter() { smp.enter(); }
|
|
|
|
|
|
|
|
void SMP::enter() {
|
2012-12-24 18:07:13 +00:00
|
|
|
while(true) {
|
|
|
|
// see comment in timing.cpp
|
snes: Fix rewind issue with tales of phantasia. Full details are as follows: Synchronize events occur right BEFORE the original thread tries to modify the data. That means that if, for instance, the SMP thread switches off to the CPU thread for a synchronize, but then the CPU switches to the PPU, commits a frame, and then in runtosave, finalizes, without having ever advanced past the SMP, when the SMP runs to save, it will do a data modify without being properly synced. The solution is for the CPU to not stop in runtosave until there's enough time for both the PPU and SMP to finish. Because of our stateful design, there's at most one event pending, so that simplifies to (smp.clock < 0 && ppu.clock < 0). This is unfortunately never guaranteed to finish, but with short timing segments on stateful PPU and SMP, and special guards in the PPU and SMP to switch back to the CPU as quickly as possible when in runtosave, it seems to work out.
2012-12-24 21:14:29 +00:00
|
|
|
if(clock > +(768 * 24 * (int64)24000000))
|
|
|
|
synchronize_cpu();
|
2012-12-24 18:07:13 +00:00
|
|
|
|
snes: Fix rewind issue with tales of phantasia. Full details are as follows: Synchronize events occur right BEFORE the original thread tries to modify the data. That means that if, for instance, the SMP thread switches off to the CPU thread for a synchronize, but then the CPU switches to the PPU, commits a frame, and then in runtosave, finalizes, without having ever advanced past the SMP, when the SMP runs to save, it will do a data modify without being properly synced. The solution is for the CPU to not stop in runtosave until there's enough time for both the PPU and SMP to finish. Because of our stateful design, there's at most one event pending, so that simplifies to (smp.clock < 0 && ppu.clock < 0). This is unfortunately never guaranteed to finish, but with short timing segments on stateful PPU and SMP, and special guards in the PPU and SMP to switch back to the CPU as quickly as possible when in runtosave, it seems to work out.
2012-12-24 21:14:29 +00:00
|
|
|
if(scheduler.sync == Scheduler::SynchronizeMode::CPU) {
|
|
|
|
synchronize_cpu(); // when in CPU sync mode, always switch back to CPU as soon as possible
|
|
|
|
}
|
2012-09-04 00:20:36 +00:00
|
|
|
if(scheduler.sync == Scheduler::SynchronizeMode::All) {
|
|
|
|
scheduler.exit(Scheduler::ExitReason::SynchronizeEvent);
|
2012-12-24 18:07:13 +00:00
|
|
|
}
|
2012-09-04 00:20:36 +00:00
|
|
|
|
|
|
|
debugger.op_exec(regs.pc);
|
|
|
|
op_step();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMP::power() {
|
|
|
|
//targets not initialized/changed upon reset
|
|
|
|
timer0.target = 0;
|
|
|
|
timer1.target = 0;
|
|
|
|
timer2.target = 0;
|
2012-12-01 08:29:47 +00:00
|
|
|
|
|
|
|
//zero 01-dec-2012
|
|
|
|
//gotta clear these to something, sometime
|
|
|
|
dp.w = sp.w = rd.w = wr.w = bit.w = ya.w = 0;
|
2012-09-04 00:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SMP::reset() {
|
|
|
|
create(Enter, system.apu_frequency());
|
|
|
|
|
2012-12-24 18:07:13 +00:00
|
|
|
regs.pc = 0xffc0;
|
|
|
|
// exact value doesn't matter much, so long as "fetch" is next
|
|
|
|
opcode = 0; // NOP
|
|
|
|
uindex = 1; // fetch phase
|
|
|
|
|
2012-09-04 00:20:36 +00:00
|
|
|
regs.a = 0x00;
|
|
|
|
regs.x = 0x00;
|
|
|
|
regs.y = 0x00;
|
|
|
|
regs.s = 0xef;
|
|
|
|
regs.p = 0x02;
|
|
|
|
|
2012-12-25 20:36:04 +00:00
|
|
|
for(int i=0;i<64*1024;i++) apuram[i] = random(0x00);
|
2012-09-04 00:20:36 +00:00
|
|
|
apuram[0x00f4] = 0x00;
|
|
|
|
apuram[0x00f5] = 0x00;
|
|
|
|
apuram[0x00f6] = 0x00;
|
|
|
|
apuram[0x00f7] = 0x00;
|
|
|
|
|
|
|
|
status.clock_counter = 0;
|
|
|
|
status.dsp_counter = 0;
|
|
|
|
status.timer_step = 3;
|
|
|
|
|
|
|
|
//$00f0
|
|
|
|
status.clock_speed = 0;
|
|
|
|
status.timer_speed = 0;
|
|
|
|
status.timers_enable = true;
|
|
|
|
status.ram_disable = false;
|
|
|
|
status.ram_writable = true;
|
|
|
|
status.timers_disable = false;
|
|
|
|
|
|
|
|
//$00f1
|
|
|
|
status.iplrom_enable = true;
|
|
|
|
|
|
|
|
//$00f2
|
|
|
|
status.dsp_addr = 0x00;
|
|
|
|
|
|
|
|
//$00f8,$00f9
|
|
|
|
status.ram00f8 = 0x00;
|
|
|
|
status.ram00f9 = 0x00;
|
|
|
|
|
|
|
|
timer0.stage0_ticks = 0;
|
|
|
|
timer1.stage0_ticks = 0;
|
|
|
|
timer2.stage0_ticks = 0;
|
|
|
|
|
|
|
|
timer0.stage1_ticks = 0;
|
|
|
|
timer1.stage1_ticks = 0;
|
|
|
|
timer2.stage1_ticks = 0;
|
|
|
|
|
|
|
|
timer0.stage2_ticks = 0;
|
|
|
|
timer1.stage2_ticks = 0;
|
|
|
|
timer2.stage2_ticks = 0;
|
|
|
|
|
|
|
|
timer0.stage3_ticks = 0;
|
|
|
|
timer1.stage3_ticks = 0;
|
|
|
|
timer2.stage3_ticks = 0;
|
|
|
|
|
|
|
|
timer0.current_line = 0;
|
|
|
|
timer1.current_line = 0;
|
|
|
|
timer2.current_line = 0;
|
|
|
|
|
|
|
|
timer0.enable = false;
|
|
|
|
timer1.enable = false;
|
|
|
|
timer2.enable = false;
|
|
|
|
}
|
|
|
|
|
2012-12-25 20:36:04 +00:00
|
|
|
SMP::SMP()
|
|
|
|
: apuram(nullptr)
|
|
|
|
{
|
2012-09-04 00:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SMP::~SMP() {
|
2012-12-25 20:36:04 +00:00
|
|
|
interface()->freeSharedMemory(apuram);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMP::initialize()
|
|
|
|
{
|
|
|
|
apuram = (uint8*)interface()->allocSharedMemory("APURAM",64 * 1024);
|
2012-09-04 00:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|