diff --git a/bsnes/sfc/cpu/cpu.hpp b/bsnes/sfc/cpu/cpu.hpp index 19a486be..5542b053 100644 --- a/bsnes/sfc/cpu/cpu.hpp +++ b/bsnes/sfc/cpu/cpu.hpp @@ -119,6 +119,9 @@ private: bool hdmaMode = 0; //0 = init, 1 = run uint autoJoypadCounter = 33; //state machine; 4224 / 128 = 33 (inactive) + + uint2 autoJoypadPort0 = 0; + uint2 autoJoypadPort1 = 0; } status; struct IO { diff --git a/bsnes/sfc/cpu/serialization.cpp b/bsnes/sfc/cpu/serialization.cpp index 7a1c2401..556e9a04 100644 --- a/bsnes/sfc/cpu/serialization.cpp +++ b/bsnes/sfc/cpu/serialization.cpp @@ -45,6 +45,9 @@ auto CPU::serialize(serializer& s) -> void { s.integer(status.autoJoypadCounter); + s.integer(status.autoJoypadPort0); + s.integer(status.autoJoypadPort1); + s.integer(io.wramAddress); s.boolean(io.hirqEnable); diff --git a/bsnes/sfc/cpu/timing.cpp b/bsnes/sfc/cpu/timing.cpp index 6153d0d5..2273bac6 100644 --- a/bsnes/sfc/cpu/timing.cpp +++ b/bsnes/sfc/cpu/timing.cpp @@ -232,20 +232,23 @@ auto CPU::joypadEdge() -> void { } } - if(status.autoJoypadCounter >= 2 && !io.autoJoypadPoll) { - // if auto-joypad polling is disabled at this point skip the rest of the polling - status.autoJoypadCounter = 33; - return; - } + if(status.autoJoypadCounter >= 2) { + if (!io.autoJoypadPoll) { + // if auto-joypad polling is disabled at this point skip the rest of the polling + status.autoJoypadCounter = 33; + return; + } - if(status.autoJoypadCounter >= 2 && (status.autoJoypadCounter & 1)) { //sixteen bits are shifted into joy{1-4}, one bit per 256 clocks - uint2 port0 = controllerPort1.device->data(); - uint2 port1 = controllerPort2.device->data(); - - io.joy1 = io.joy1 << 1 | port0.bit(0); - io.joy2 = io.joy2 << 1 | port1.bit(0); - io.joy3 = io.joy3 << 1 | port0.bit(1); - io.joy4 = io.joy4 << 1 | port1.bit(1); + //the bits are read on one 128-clock cycle and written on the next + if ((status.autoJoypadCounter & 1) == 0) { + status.autoJoypadPort0 = controllerPort1.device->data(); + status.autoJoypadPort1 = controllerPort2.device->data(); + } else { + io.joy1 = io.joy1 << 1 | status.autoJoypadPort0.bit(0); + io.joy2 = io.joy2 << 1 | status.autoJoypadPort1.bit(0); + io.joy3 = io.joy3 << 1 | status.autoJoypadPort0.bit(1); + io.joy4 = io.joy4 << 1 | status.autoJoypadPort1.bit(1); + } } }