mirror of
https://github.com/bsnes-emu/bsnes.git
synced 2025-02-22 06:02:28 +01:00
byuu says: (Windows: compile with -fpermissive to silence an annoying error. I'll fix it in the next WIP.) I completely replaced the time management system in higan and overhauled the scheduler. Before, processor threads would have "int64 clock"; and there would be a 1:1 relationship between two threads. When thread A ran for X cycles, it'd subtract X * B.Frequency from clock; and when thread B ran for Y cycles, it'd add Y * A.Frequency from clock. This worked well and allowed perfect precision; but it doesn't work when you have more complicated relationships: eg the 68K can sync to the Z80 and PSG; the Z80 to the 68K and PSG; so the PSG needs two counters. The new system instead uses a "uint64 clock" variable that represents time in attoseconds. Every time the scheduler exits, it subtracts the smallest clock count from all threads, to prevent an overflow scenario. The only real downside is that rounding errors mean that roughly every 20 minutes, we have a rounding error of one clock cycle (one 20,000,000th of a second.) However, this only applies to systems with multiple oscillators, like the SNES. And when you're in that situation ... there's no such thing as a perfect oscillator anyway. A real SNES will be thousands of times less out of spec than 1hz per 20 minutes. The advantages are pretty immense. First, we obviously can now support more complex relationships between threads. Second, we can build a much more abstracted scheduler. All of libco is now abstracted away completely, which may permit a state-machine / coroutine version of Thread in the future. We've basically gone from this: auto SMP::step(uint clocks) -> void { clock += clocks * (uint64)cpu.frequency; dsp.clock -= clocks; if(dsp.clock < 0 && !scheduler.synchronizing()) co_switch(dsp.thread); if(clock >= 0 && !scheduler.synchronizing()) co_switch(cpu.thread); } To this: auto SMP::step(uint clocks) -> void { Thread::step(clocks); synchronize(dsp); synchronize(cpu); } As you can see, we don't have to do multiple clock adjustments anymore. This is a huge win for the SNES CPU that had to update the SMP, DSP, all peripherals and all coprocessors. Likewise, we don't have to synchronize all coprocessors when one runs, now we can just synchronize the active one to the CPU. Third, when changing the frequencies of threads (think SGB speed setting modes, GBC double-speed mode, etc), it no longer causes the "int64 clock" value to be erroneous. Fourth, this results in a fairly decent speedup, mostly across the board. Aside from the GBA being mostly a wash (for unknown reasons), it's about an 8% - 12% speedup in every other emulation core. Now, all of this said ... this was an unbelievably massive change, so ... you know what that means >_> If anyone can help test all types of SNES coprocessors, and some other system games, it'd be appreciated. ---- Lastly, we have a bitchin' new about screen. It unfortunately adds ~200KiB onto the binary size, because the PNG->C++ header file transformation doesn't compress very well, and I want to keep the original resource files in with the higan archive. I might try some things to work around this file size increase in the future, but for now ... yeah, slightly larger archive sizes, sorry. The logo's a bit busted on Windows (the Label control's background transparency and alignment settings aren't working), but works well on GTK. I'll have to fix Windows before the next official release. For now, look on my Twitter feed if you want to see what it's supposed to look like. ---- EDIT: forgot about ICD2::Enter. It's doing some weird inverse run-to-save thing that I need to implement support for somehow. So, save states on the SGB core probably won't work with this WIP.
154 lines
4.0 KiB
C++
154 lines
4.0 KiB
C++
auto CPU::dmaCounter() const -> uint {
|
|
return (status.dmaCounter + hcounter()) & 7;
|
|
}
|
|
|
|
auto CPU::step(uint clocks) -> void {
|
|
status.irqLock = false;
|
|
uint ticks = clocks >> 1;
|
|
while(ticks--) {
|
|
tick();
|
|
if(hcounter() & 2) pollInterrupts();
|
|
}
|
|
|
|
Thread::step(clocks);
|
|
for(auto peripheral : peripherals) synchronize(*peripheral);
|
|
|
|
status.autoJoypadClock += clocks;
|
|
if(status.autoJoypadClock >= 256) {
|
|
status.autoJoypadClock -= 256;
|
|
stepAutoJoypadPoll();
|
|
}
|
|
|
|
if(!status.dramRefreshed && hcounter() >= status.dramRefreshPosition) {
|
|
status.dramRefreshed = true;
|
|
step(40);
|
|
}
|
|
|
|
#if defined(DEBUGGER)
|
|
synchronizeSMP();
|
|
synchronizePPU();
|
|
synchronizeCoprocessors();
|
|
#endif
|
|
}
|
|
|
|
//called by ppu.tick() when Hcounter=0
|
|
auto CPU::scanline() -> void {
|
|
status.dmaCounter = (status.dmaCounter + status.lineClocks) & 7;
|
|
status.lineClocks = lineclocks();
|
|
|
|
//forcefully sync S-CPU to other processors, in case chips are not communicating
|
|
synchronize(smp);
|
|
synchronize(ppu);
|
|
for(auto coprocessor : coprocessors) synchronize(*coprocessor);
|
|
|
|
if(vcounter() == 0) {
|
|
//HDMA init triggers once every frame
|
|
status.hdmaInitPosition = (version == 1 ? 12 + 8 - dmaCounter() : 12 + dmaCounter());
|
|
status.hdmaInitTriggered = false;
|
|
|
|
status.autoJoypadCounter = 0;
|
|
}
|
|
|
|
//DRAM refresh occurs once every scanline
|
|
if(version == 2) status.dramRefreshPosition = 530 + 8 - dmaCounter();
|
|
status.dramRefreshed = false;
|
|
|
|
//HDMA triggers once every visible scanline
|
|
if(vcounter() < ppu.vdisp()) {
|
|
status.hdmaPosition = 1104;
|
|
status.hdmaTriggered = false;
|
|
}
|
|
}
|
|
|
|
auto CPU::aluEdge() -> void {
|
|
if(alu.mpyctr) {
|
|
alu.mpyctr--;
|
|
if(io.rddiv & 1) io.rdmpy += alu.shift;
|
|
io.rddiv >>= 1;
|
|
alu.shift <<= 1;
|
|
}
|
|
|
|
if(alu.divctr) {
|
|
alu.divctr--;
|
|
io.rddiv <<= 1;
|
|
alu.shift >>= 1;
|
|
if(io.rdmpy >= alu.shift) {
|
|
io.rdmpy -= alu.shift;
|
|
io.rddiv |= 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
auto CPU::dmaEdge() -> void {
|
|
//H/DMA pending && DMA inactive?
|
|
//.. Run one full CPU cycle
|
|
//.. HDMA pending && HDMA enabled ? DMA sync + HDMA run
|
|
//.. DMA pending && DMA enabled ? DMA sync + DMA run
|
|
//.... HDMA during DMA && HDMA enabled ? DMA sync + HDMA run
|
|
//.. Run one bus CPU cycle
|
|
//.. CPU sync
|
|
|
|
if(status.dmaActive) {
|
|
if(status.hdmaPending) {
|
|
status.hdmaPending = false;
|
|
if(hdmaEnabledChannels()) {
|
|
if(!dmaEnabledChannels()) {
|
|
dmaStep(8 - dmaCounter());
|
|
}
|
|
status.hdmaMode == 0 ? hdmaInit() : hdmaRun();
|
|
if(!dmaEnabledChannels()) {
|
|
step(status.clockCount - (status.dmaClocks % status.clockCount));
|
|
status.dmaActive = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
if(status.dmaPending) {
|
|
status.dmaPending = false;
|
|
if(dmaEnabledChannels()) {
|
|
dmaStep(8 - dmaCounter());
|
|
dmaRun();
|
|
step(status.clockCount - (status.dmaClocks % status.clockCount));
|
|
status.dmaActive = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
if(!status.hdmaInitTriggered && hcounter() >= status.hdmaInitPosition) {
|
|
status.hdmaInitTriggered = true;
|
|
hdmaInitReset();
|
|
if(hdmaEnabledChannels()) {
|
|
status.hdmaPending = true;
|
|
status.hdmaMode = 0;
|
|
}
|
|
}
|
|
|
|
if(!status.hdmaTriggered && hcounter() >= status.hdmaPosition) {
|
|
status.hdmaTriggered = true;
|
|
if(hdmaActiveChannels()) {
|
|
status.hdmaPending = true;
|
|
status.hdmaMode = 1;
|
|
}
|
|
}
|
|
|
|
if(!status.dmaActive) {
|
|
if(status.dmaPending || status.hdmaPending) {
|
|
status.dmaClocks = 0;
|
|
status.dmaActive = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
//used to test for NMI/IRQ, which can trigger on the edge of every opcode.
|
|
//test one cycle early to simulate two-stage pipeline of x816 CPU.
|
|
//
|
|
//status.irq_lock is used to simulate hardware delay before interrupts can
|
|
//trigger during certain events (immediately after DMA, writes to $4200, etc)
|
|
auto CPU::lastCycle() -> void {
|
|
if(!status.irqLock) {
|
|
status.nmiPending |= nmiTest();
|
|
status.irqPending |= irqTest();
|
|
status.interruptPending = (status.nmiPending || status.irqPending);
|
|
}
|
|
}
|