@@ -478,13 +478,21 @@ void CPU::writeCPU(unsigned addr, uint8_t data) {
478478    // bit 0 is shared between JOYSER0 and JOYSER1:
479479    // strobing $4016.d0 affects both controller port latches.
480480    // $4017 bit 0 writes are ignored.
481-     controllerPort1.device ->latch (data & 1 );
482-     controllerPort2.device ->latch (data & 1 );
481+     status.cpuLatch  = data & 1 ;
482+     controllerPort1.device ->latch (status.autoJoypadLatch  | status.cpuLatch );
483+     controllerPort2.device ->latch (status.autoJoypadLatch  | status.cpuLatch );
483484    return ;
484485
485486  case  0x4200 :  // NMITIMEN
486487    io.autoJoypadPoll  = data & 1 ;
487-     if (!io.autoJoypadPoll ) status.autoJoypadCounter  = 33 ; //  Disable auto-joypad read
488+     if (status.autoJoypadCounter  == 0 ) {
489+       //  allow controller latches during this time
490+       status.autoJoypadLatch  = io.autoJoypadPoll ;
491+       controllerPort1.device ->latch (status.autoJoypadLatch  | status.cpuLatch );
492+       controllerPort2.device ->latch (status.autoJoypadLatch  | status.cpuLatch );
493+     } else  if  (!io.autoJoypadPoll  && status.autoJoypadCounter  >= 2 ) {
494+       status.autoJoypadCounter  = 33 ;
495+     }
488496    nmitimenUpdate (data);
489497    return ;
490498
@@ -820,47 +828,56 @@ void CPU::dmaEdge() {
820828
821829// called every 128 clocks from inside the CPU::stepOnce() function
822830void  CPU::joypadEdge () {
823-   // it is not yet confirmed if polling can be stopped early and/or (re)started later
824-   if (!io.autoJoypadPoll ) return ;
825- 
826-   if (vcounter () == ppu.vdisp () && hcounter () >= 130  && hcounter () <= 256 ) {
831+   if (vcounter () == ppu.vdisp () && (counter.cpu  & 255 ) == 0  && hcounter () >= 130  && hcounter () <= 384 ) {
827832    // begin new polling sequence
828833    status.autoJoypadCounter  = 0 ;
829-   }
834+   } else  {
835+     // stop after polling has been completed for this frame
836+     if (status.autoJoypadCounter  >= 33 ) return ;
830837
831-   // stop after polling has been completed for this frame 
832-   if (status. autoJoypadCounter  >=  33 )  return ; 
838+     status. autoJoypadCounter ++; 
839+   } 
833840
834841  if (status.autoJoypadCounter  == 0 ) {
835842    // latch controller states on the first polling cycle
836-     controllerPort1.device ->latch (1 );
837-     controllerPort2.device ->latch (1 );
843+     status.autoJoypadLatch  = io.autoJoypadPoll ;
844+     controllerPort1.device ->latch (status.autoJoypadLatch  | status.cpuLatch );
845+     controllerPort2.device ->latch (status.autoJoypadLatch  | status.cpuLatch );
846+     if (io.autoJoypadPoll ) {
847+       // shift registers are cleared to zero at start of auto-joypad polling
848+       io.joy1  = 0 ;
849+       io.joy2  = 0 ;
850+       io.joy3  = 0 ;
851+       io.joy4  = 0 ;
852+     }
838853  }
839854
840855  if (status.autoJoypadCounter  == 1 ) {
841856    // release latch and begin reading on the second cycle
842-     controllerPort1.device ->latch (0 );
843-     controllerPort2.device ->latch (0 );
844- 
845-     // shift registers are cleared to zero at start of auto-joypad polling
846-     io.joy1  = 0 ;
847-     io.joy2  = 0 ;
848-     io.joy3  = 0 ;
849-     io.joy4  = 0 ;
857+     status.autoJoypadLatch  = 0 ;
858+     controllerPort1.device ->latch (status.autoJoypadLatch  | status.cpuLatch );
859+     controllerPort2.device ->latch (status.autoJoypadLatch  | status.cpuLatch );
850860  }
851861
852-   if (status.autoJoypadCounter  >= 2  && !(status.autoJoypadCounter  & 1 )) {
853-     // sixteen bits are shifted into joy{1-4}, one bit per 256 clocks
854-     uint8_t  port0 = controllerPort1.device ->data ();
855-     uint8_t  port1 = controllerPort2.device ->data ();
856- 
857-     io.joy1  = io.joy1  << 1  | (port0 & 1 );
858-     io.joy2  = io.joy2  << 1  | (port1 & 1 );
859-     io.joy3  = io.joy3  << 1  | ((port0 & 2 ) >> 1 );
860-     io.joy4  = io.joy4  << 1  | ((port1 & 2 ) >> 1 );
862+   if (status.autoJoypadCounter  != 1  && !io.autoJoypadPoll ) {
863+     //  if auto-joypad polling is disabled at this point skip the rest of the polling
864+     status.autoJoypadCounter  = 33 ;
865+     return ;
861866  }
862867
863-   ++status.autoJoypadCounter ;
868+   if (status.autoJoypadCounter  >= 2 ) {
869+     // sixteen bits are shifted into joy{1-4}, one bit per 256 clocks
870+     // the bits are read on one 128-clock cycle and written on the next
871+     if  ((status.autoJoypadCounter  & 1 ) == 0 ) {
872+       status.autoJoypadPort1  = controllerPort1.device ->data ();
873+       status.autoJoypadPort2  = controllerPort2.device ->data ();
874+     } else  {
875+       io.joy1  = io.joy1  << 1  | (status.autoJoypadPort1  & 1 );
876+       io.joy2  = io.joy2  << 1  | (status.autoJoypadPort2  & 1 );
877+       io.joy3  = io.joy3  << 1  | ((status.autoJoypadPort1  & 2 ) >> 1 );
878+       io.joy4  = io.joy4  << 1  | ((status.autoJoypadPort2  & 2 ) >> 1 );
879+     }
880+   }
864881}
865882
866883// nmiPoll() and irqPoll() are called once every four clock cycles;
@@ -1005,6 +1022,14 @@ void CPU::serialize(serializer& s) {
10051022
10061023  s.integer (status.autoJoypadCounter );
10071024
1025+   if  (s.version ) {
1026+     s.integer (status.autoJoypadPort1 );
1027+     s.integer (status.autoJoypadPort2 );
1028+ 
1029+     s.boolean (status.cpuLatch );
1030+     s.boolean (status.autoJoypadLatch );
1031+   }
1032+ 
10081033  s.integer (io.wramAddress );
10091034
10101035  s.boolean (io.hirqEnable );
0 commit comments