@@ -94,6 +94,16 @@ void GameCubeControllerAnalyzer::AdvanceToEndOfPacket()
9494 mGamecube ->AdvanceToNextEdge ();
9595 }
9696
97+ // if a complete packet was received successfully, we're already at the end of the packet
98+ if ( mDecodedReception )
99+ {
100+ mDecodedReception = false ;
101+ return ;
102+ }
103+
104+ // otherwise, something was corrupted. synchronize to at least 100us of inactivity.
105+ // this way, we can be sure we're at the beginning of a transmission and not in between
106+ // a transmission and reception
97107 while ( GetPulseWidthNs ( mGamecube ->GetSampleNumber (), mGamecube ->GetSampleOfNextEdge () ) < 100000 )
98108 {
99109 mGamecube ->AdvanceToNextEdge ();
@@ -104,7 +114,12 @@ void GameCubeControllerAnalyzer::AdvanceToEndOfPacket()
104114// advances to the falling edge of the next bit in a packet
105115bool GameCubeControllerAnalyzer::AdvanceToNextBitInPacket ()
106116{
107- if ( GetPulseWidthNs ( mGamecube ->GetSampleNumber (), mGamecube ->GetSampleOfNextEdge () ) < 100000 )
117+ // if the transmission from the host completed, the controller has ~100us to respond
118+ // in this condition, provide the extra leniency
119+ int duration = mDecodedTransmission ? 100000 : 5000 ;
120+ mDecodedTransmission = false ;
121+
122+ if ( GetPulseWidthNs ( mGamecube ->GetSampleNumber (), mGamecube ->GetSampleOfNextEdge () ) < duration )
108123 {
109124 mGamecube ->AdvanceToNextEdge ();
110125 return true ;
@@ -146,6 +161,7 @@ void GameCubeControllerAnalyzer::DecodeFrames()
146161 AdvanceToEndOfPacket ();
147162 return ;
148163 }
164+ mDecodedTransmission = ok;
149165
150166 // response
151167 uint8_t device[ 2 ];
@@ -170,6 +186,7 @@ void GameCubeControllerAnalyzer::DecodeFrames()
170186 }
171187 if ( ok )
172188 ok = AdvanceToNextBitInPacket () && DecodeStopBit ();
189+ mDecodedReception = ok;
173190 AdvanceToEndOfPacket ();
174191
175192 U64 end_sample = mGamecube ->GetSampleNumber ();
@@ -189,6 +206,7 @@ void GameCubeControllerAnalyzer::DecodeFrames()
189206 return ;
190207 }
191208 frame_v2.AddByte ( " Poll Mode" , data );
209+ U8 poll_mode = data;
192210
193211 // command arg2
194212 if ( !( AdvanceToNextBitInPacket () && DecodeByte ( data ) ) )
@@ -204,6 +222,7 @@ void GameCubeControllerAnalyzer::DecodeFrames()
204222 AdvanceToEndOfPacket ();
205223 return ;
206224 }
225+ mDecodedTransmission = ok;
207226
208227 // response
209228 uint8_t buttons[ 2 ];
@@ -236,29 +255,78 @@ void GameCubeControllerAnalyzer::DecodeFrames()
236255 ok = AdvanceToNextBitInPacket () && DecodeByte ( data );
237256 if ( ok )
238257 {
239- frame_v2.AddByte ( " C-Stick X" , data );
258+ if ( poll_mode == 1 || poll_mode == 2 )
259+ {
260+ frame_v2.AddByte ( " C-Stick X" , data & 0xF0 );
261+ frame_v2.AddByte ( " C-Stick Y" , data & 0x0F );
262+ }
263+ else
264+ {
265+ frame_v2.AddByte ( " C-Stick X" , data );
266+ }
240267 }
241268 if ( ok )
242269 ok = AdvanceToNextBitInPacket () && DecodeByte ( data );
243270 if ( ok )
244271 {
245- frame_v2.AddByte ( " C-Stick Y" , data );
272+ if ( poll_mode == 1 )
273+ {
274+ frame_v2.AddByte ( " L Analog" , data );
275+ }
276+ else if ( poll_mode == 2 )
277+ {
278+ frame_v2.AddByte ( " L Analog" , data & 0xF0 );
279+ frame_v2.AddByte ( " R Analog" , data & 0x0F );
280+ }
281+ else
282+ {
283+ frame_v2.AddByte ( " C-Stick Y" , data );
284+ }
246285 }
247286 if ( ok )
248287 ok = AdvanceToNextBitInPacket () && DecodeByte ( data );
249288 if ( ok )
250289 {
251- frame_v2.AddByte ( " L Analog" , data );
290+ if ( poll_mode == 0 )
291+ {
292+ frame_v2.AddByte ( " L Analog" , data & 0xF0 );
293+ frame_v2.AddByte ( " R Analog" , data & 0x0F );
294+ }
295+ else if ( poll_mode == 1 )
296+ {
297+ frame_v2.AddByte ( " R Analog" , data );
298+ }
299+ else if ( poll_mode == 2 || poll_mode == 4 )
300+ {
301+ frame_v2.AddByte ( " A Analog" , data );
302+ }
303+ else
304+ {
305+ frame_v2.AddByte ( " L Analog" , data );
306+ }
252307 }
253308 if ( ok )
254309 ok = AdvanceToNextBitInPacket () && DecodeByte ( data );
255310 if ( ok )
256311 {
257- frame_v2.AddByte ( " R Analog" , data );
312+ if ( poll_mode == 0 || poll_mode == 1 )
313+ {
314+ frame_v2.AddByte ( " A Analog" , data & 0xF0 );
315+ frame_v2.AddByte ( " B Analog" , data & 0x0F );
316+ }
317+ else if ( poll_mode == 2 || poll_mode == 4 )
318+ {
319+ frame_v2.AddByte ( " B Analog" , data );
320+ }
321+ else
322+ {
323+ frame_v2.AddByte ( " R Analog" , data );
324+ }
258325 }
259326
260327 if ( ok )
261328 ok = AdvanceToNextBitInPacket () && DecodeStopBit ();
329+ mDecodedReception = ok;
262330 AdvanceToEndOfPacket ();
263331
264332 U64 end_sample = mGamecube ->GetSampleNumber ();
@@ -277,6 +345,7 @@ void GameCubeControllerAnalyzer::DecodeFrames()
277345 AdvanceToEndOfPacket ();
278346 return ;
279347 }
348+ mDecodedTransmission = ok;
280349
281350 // response
282351 uint8_t buttons[ 2 ];
@@ -344,6 +413,7 @@ void GameCubeControllerAnalyzer::DecodeFrames()
344413
345414 if ( ok )
346415 ok = AdvanceToNextBitInPacket () && DecodeStopBit ();
416+ mDecodedReception = ok;
347417 AdvanceToEndOfPacket ();
348418
349419 U64 end_sample = mGamecube ->GetSampleNumber ();
@@ -379,6 +449,7 @@ void GameCubeControllerAnalyzer::DecodeFrames()
379449 AdvanceToEndOfPacket ();
380450 return ;
381451 }
452+ mDecodedTransmission = ok;
382453
383454 // response
384455 uint8_t buttons[ 2 ];
@@ -446,6 +517,7 @@ void GameCubeControllerAnalyzer::DecodeFrames()
446517
447518 if ( ok )
448519 ok = AdvanceToNextBitInPacket () && DecodeStopBit ();
520+ mDecodedReception = ok;
449521 AdvanceToEndOfPacket ();
450522
451523 U64 end_sample = mGamecube ->GetSampleNumber ();
@@ -480,6 +552,7 @@ void GameCubeControllerAnalyzer::DecodeFrames()
480552 AdvanceToEndOfPacket ();
481553 return ;
482554 }
555+ mDecodedTransmission = ok;
483556
484557 // response
485558 uint8_t buttons[ 2 ];
@@ -547,6 +620,7 @@ void GameCubeControllerAnalyzer::DecodeFrames()
547620
548621 if ( ok )
549622 ok = AdvanceToNextBitInPacket () && DecodeStopBit ();
623+ mDecodedReception = ok;
550624 AdvanceToEndOfPacket ();
551625
552626 U64 end_sample = mGamecube ->GetSampleNumber ();
0 commit comments