00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "CeylanFIFO.h"
00028
00029 #include "CeylanLogPlug.h"
00030 #include "CeylanOperators.h"
00031 #include "CeylanSystem.h"
00032 #include "CeylanUtils.h"
00033
00034
00035 #ifdef CEYLAN_USES_CONFIG_H
00036 #include "CeylanConfig.h"
00037 #endif // CEYLAN_USES_CONFIG_H
00038
00039
00040 #if CEYLAN_ARCH_NINTENDO_DS
00041 #include "CeylanConfigForNintendoDS.h"
00042 #endif // CEYLAN_ARCH_NINTENDO_DS
00043
00044
00045 #include "CeylanARM7Codes.h"
00046 #include "CeylanIPCCommands.h"
00047
00048
00049
00050 using std::string ;
00051
00052 using namespace Ceylan::System ;
00053 using namespace Ceylan::Log ;
00054 using namespace Ceylan ;
00055
00056
00057
00058
00059
00060 #if CEYLAN_DEBUG_FIFO
00061
00062 #define CEYLAN_LOG_FIFO(message) LogPlug::debug(message)
00063
00064 #else // CEYLAN_DEBUG_FIFO
00065
00066 #define CEYLAN_LOG_FIFO(message)
00067
00068 #endif // CEYLAN_DEBUG_FIFO
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 FIFO * FIFO::_FIFO = 0 ;
00095
00096
00097
00098 FIFO::FIFOException::FIFOException( const string & reason ) :
00099 SystemException( reason )
00100 {
00101
00102 }
00103
00104
00105
00106 FIFO::FIFOException::~FIFOException() throw()
00107 {
00108
00109 }
00110
00111
00112
00113
00114
00115 FIFO::FIFOFull::FIFOFull( const string & reason ) :
00116 FIFO::FIFOException( reason )
00117 {
00118
00119 }
00120
00121
00122
00123 FIFO::FIFOEmpty::FIFOEmpty( const string & reason ) :
00124 FIFO::FIFOException( reason )
00125 {
00126
00127 }
00128
00129
00130
00131
00132 FIFO::FIFO():
00133 _arm7StatusWordPointer( 0 ),
00134 _arm7ErrorCodePointer( 0 ),
00135 _localCommandCount( 0 ),
00136 _remoteCommandCount( 0 ),
00137 _processedCount( 0 ),
00138 _sentCount( 0 ),
00139 _activated( false ),
00140 _batteryStatus( BatteryStatusUnknown ),
00141 _dsType( DSTypeUnknown )
00142 {
00143
00144
00145
00146 #if CEYLAN_ARCH_NINTENDO_DS
00147
00148 #ifdef CEYLAN_RUNS_ON_ARM9
00149
00150 if ( FIFO::_FIFO != 0 )
00151 throw FIFOException( "FIFO constructor failed: "
00152 "there is already a FIFO instance registered" ) ;
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169 #define CEYLAN_USES_CACHE_FLUSH 1
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 #if CEYLAN_USES_CACHE_FLUSH
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196 Ceylan::Byte * cacheLine = CacheProtectedNew( sizeof( ARM7StatusWord ) +
00197 sizeof( ARM7ErrorCode ) ) ;
00198
00199
00200
00201 _arm7StatusWordPointer = (volatile ARM7StatusWord* volatile) cacheLine ;
00202
00203 (*_arm7StatusWordPointer) = NoStatusAvailable ;
00204
00205 if ( getLastARM7StatusWord() != NoStatusAvailable )
00206 CEYLAN_LOG_FIFO( "FIFO constructor: "
00207 "ARM7 status setting failed before flushing cache" ) ;
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217 _arm7ErrorCodePointer = (volatile ARM7StatusWord* volatile) (
00218 cacheLine + sizeof( ARM7StatusWord ) );
00219
00220 (*_arm7ErrorCodePointer) = NoErrorAvailable ;
00221
00222
00223 Ceylan::Uint8 sleepCount = 5 ;
00224
00225 while ( sleepCount > 0 )
00226 {
00227
00228 atomicSleep() ;
00229 sleepCount-- ;
00230
00231 }
00232
00233 if ( getLastARM7ErrorCode() != NoErrorAvailable )
00234 CEYLAN_LOG_FIFO( "FIFO constructor: "
00235 "ARM7 error setting failed just before flushing cache" ) ;
00236
00237
00238
00239
00240 DC_FlushRange( (void*) _arm7StatusWordPointer, sizeof(ARM7StatusWord) ) ;
00241
00242 if ( getLastARM7StatusWord() != NoStatusAvailable )
00243 throw FIFOException( "FIFO constructor: "
00244 "ARM7 status setting finally failed after flushing cache" ) ;
00245
00246
00247 DC_FlushRange( (void*) _arm7ErrorCodePointer, sizeof(ARM7ErrorCode) ) ;
00248
00249 if ( getLastARM7ErrorCode() != NoErrorAvailable )
00250 throw FIFOException( "FIFO constructor: "
00251 "ARM7 error setting finally failed after flushing cache" ) ;
00252
00253 DC_InvalidateRange( (void*) _arm7StatusWordPointer,
00254 sizeof(ARM7StatusWord) ) ;
00255
00256 DC_InvalidateRange( (void*) _arm7ErrorCodePointer,
00257 sizeof(ARM7ErrorCode) ) ;
00258
00259
00260 #else // CEYLAN_USES_CACHE_FLUSH
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271 ARM7StatusWord * tempStatus = new ARM7StatusWord ;
00272
00273 _arm7ErrorCodePointer =
00274 ConvertToNonCacheable<ARM7StatusWord>( tempStatus ) ;
00275
00276 (*_arm7StatusWordPointer) = NoStatusAvailable ;
00277 *tempStatus = NoStatusAvailable ;
00278
00279 DC_InvalidateAll() ;
00280
00281
00282 if ( getLastARM7StatusWord() != NoStatusAvailable )
00283 throw FIFOException( "FIFO constructor: "
00284 "ARM7 status setting failed, read: "
00285 + Ceylan::toString( getLastARM7StatusWord() ) ) ;
00286
00287
00288 ARM7ErrorCode * tempError = new ARM7ErrorCode ;
00289
00290 _arm7ErrorCodePointer =
00291 ConvertToNonCacheable<ARM7ErrorCode>( tempError ) ;
00292
00293 (*_arm7ErrorCodePointer) = NoErrorAvailable ;
00294 *tempError = NoErrorAvailable ;
00295
00296 DC_InvalidateAll() ;
00297
00298
00299 if ( getLastARM7StatusWord() != NoErrorAvailable )
00300 throw FIFOException( "FIFO constructor: "
00301 "ARM7 error setting failed, read: "
00302 + Ceylan::toString( getLastARM7ErrorCode() ) ) ;
00303
00304 #endif // CEYLAN_USES_CACHE_FLUSH
00305
00306
00307
00308
00309 System::InitializeInterrupts() ;
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327 irqSet( IRQ_IPC_SYNC, SyncHandlerForFIFO ) ;
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339 irqSet( IRQ_VBLANK, 0 ) ;
00340
00341
00342 irqEnable( IRQ_IPC_SYNC | IRQ_VBLANK ) ;
00343
00344
00345 _FIFO = this ;
00346
00347
00348 if ( getLastARM7StatusWord() != NoStatusAvailable )
00349 throw FIFOException( "FIFO constructor: "
00350 "ARM7 status setting failed, read finally: "
00351 + Ceylan::toString( getLastARM7StatusWord() ) ) ;
00352
00353
00354 if ( getLastARM7ErrorCode() != NoErrorAvailable )
00355 throw FIFOException( "FIFO constructor: "
00356 "ARM7 error setting failed, read finally: "
00357 + Ceylan::toString( getLastARM7ErrorCode() ) ) ;
00358
00359 CEYLAN_LOG_FIFO(
00360 "FIFO constructor: setting of status and error codes succeeded." ) ;
00361
00362 #else // CEYLAN_RUNS_ON_ARM9
00363
00364 throw FIFO::FIFOException( "FIFO constructor failed: "
00365 "not available on the ARM7." ) ;
00366
00367 #endif // CEYLAN_RUNS_ON_ARM9
00368
00369
00370 #else // CEYLAN_ARCH_NINTENDO_DS
00371
00372 throw FIFO::FIFOException( "FIFO constructor failed: "
00373 "not available on this platform." ) ;
00374
00375 #endif // CEYLAN_ARCH_NINTENDO_DS
00376
00377 }
00378
00379
00380
00381 FIFO::~FIFO() throw()
00382 {
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394 _FIFO = 0 ;
00395
00396
00397 CacheProtectedDelete( (Ceylan::Byte*) _arm7StatusWordPointer ) ;
00398
00399 _arm7StatusWordPointer = 0 ;
00400 _arm7ErrorCodePointer = 0 ;
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417 }
00418
00419
00420
00421
00422
00423
00424
00425 bool FIFO::isActive() const
00426 {
00427
00428 return _activated ;
00429
00430 }
00431
00432
00433
00434 void FIFO::activate()
00435 {
00436
00437 #if CEYLAN_ARCH_NINTENDO_DS
00438
00439 #ifdef CEYLAN_RUNS_ON_ARM9
00440
00441
00442
00443 if ( _activated )
00444 throw FIFOException( "FIFO::activate: already active" ) ;
00445
00446 ARM7StatusWord initialStatus = getLastARM7StatusWord() ;
00447
00448 ARM7ErrorCode initialError = getLastARM7ErrorCode() ;
00449
00450 CEYLAN_LOG_FIFO( "FIFO::activate: before sending command, "
00451 "the ARM7 status word is equal to "
00452 + Ceylan::toString( initialStatus ) + ", the error code is "
00453 + Ceylan::toString( initialError ) ) ;
00454
00455
00456 if ( initialStatus != NoStatusAvailable )
00457 throw FIFOException( "FIFO::activate: "
00458 "ARM7 status word not set to correct initial value: read "
00459 + Ceylan::toString( initialStatus ) + ", instead of "
00460 + Ceylan::toString( NoStatusAvailable ) ) ;
00461
00462 if ( initialError != NoErrorAvailable )
00463 throw FIFOException( "FIFO::activate: "
00464 "ARM7 error code not set to correct initial value: read "
00465 + Ceylan::toString( initialError ) + ", instead of "
00466 + Ceylan::toString( NoErrorAvailable ) ) ;
00467
00468
00469
00470
00471 _localCommandCount = 0 ;
00472 _remoteCommandCount = 0 ;
00473
00474 _processedCount = 0 ;
00475 _sentCount = 0 ;
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486 REG_IPC_FIFO_CR = IPC_FIFO_ENABLE | IPC_FIFO_SEND_CLEAR ;
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497 REG_IPC_SYNC = IPC_SYNC_IRQ_ENABLE ;
00498
00499
00500
00501 CEYLAN_LOG_FIFO( "FIFO::activate: sending IPC activate report command." ) ;
00502
00503
00504
00505
00506
00507
00508 InterruptMask previous = SetEnabledInterrupts( AllInterruptsDisabled ) ;
00509
00510
00511
00512
00513
00514
00515
00516
00517 FIFOElement commandElement = prepareFIFOCommand( StatusInitRequest ) ;
00518
00519 writeBlocking( commandElement ) ;
00520
00521 CEYLAN_LOG_FIFO( "FIFO::activate: sending address of the status word." ) ;
00522
00523 writeBlocking(
00524
00525 reinterpret_cast<FIFOElement>( _arm7StatusWordPointer ) ) ;
00526
00527 CEYLAN_LOG_FIFO( "FIFO::activate: sending address of the error word." ) ;
00528
00529 writeBlocking(
00530 reinterpret_cast<FIFOElement>( _arm7ErrorCodePointer ) ) ;
00531
00532 SetEnabledInterrupts( previous ) ;
00533
00534 notifyCommandToARM7() ;
00535
00536
00537
00538 Ceylan::Uint8 VBlankCount = 60 ;
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552 while ( ( ( getLastARM7StatusWord() == NoStatusAvailable )
00553 || ( getLastARM7ErrorCode() == NoErrorAvailable ) )
00554 && ( VBlankCount > 0 ) )
00555 {
00556
00557 sendSynchronizeInterruptToARM7() ;
00558
00559 #if CEYLAN_DEBUG_FIFO
00560
00561
00562 LogPlug::debug( "act: status "
00563 + Ceylan::toString( getLastARM7StatusWord() ) + ", error "
00564 + Ceylan::toString( getLastARM7ErrorCode() ) ) ;
00565
00566 #endif // CEYLAN_DEBUG_FIFO
00567
00568 atomicSleep() ;
00569 VBlankCount-- ;
00570
00571 }
00572
00573
00574 if ( VBlankCount == 0 )
00575 throw FIFOException( "FIFO::activate: "
00576 "time-out reached while waiting for ARM7 update" ) ;
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592 if ( getLastARM7StatusWord() != ARM7Running )
00593 throw FIFOException( "FIFO::activate: ARM7 status updated, "
00594 "but is not in expected running state: "
00595 + interpretLastARM7StatusWord() ) ;
00596
00597 if ( getLastARM7ErrorCode() != NoError )
00598 throw FIFOException( "FIFO::activate: ARM7 error updated, "
00599 "but is not in expected no-error state: "
00600 + interpretLastARM7ErrorCode() ) ;
00601
00602 _activated = true ;
00603
00604 CEYLAN_LOG_FIFO( "FIFO successfully activated, ARM handshake completed" ) ;
00605
00606
00607 #endif // CEYLAN_RUNS_ON_ARM9
00608
00609 #endif // CEYLAN_ARCH_NINTENDO_DS
00610
00611 }
00612
00613
00614
00615 void FIFO::deactivate()
00616 {
00617
00618 #if CEYLAN_ARCH_NINTENDO_DS
00619
00620 #ifdef CEYLAN_RUNS_ON_ARM9
00621
00622 CEYLAN_LOG_FIFO( "FIFO deactivate, stopping IPC system" ) ;
00623
00624
00625 InterruptMask previous = SetEnabledInterrupts( AllInterruptsDisabled ) ;
00626
00627
00628
00629
00630
00631
00632 writeBlocking( prepareFIFOCommand( ShutdownIPCRequest ) ) ;
00633
00634 SetEnabledInterrupts( previous ) ;
00635
00636 notifyCommandToARM7() ;
00637
00638
00639
00640 Ceylan::Uint8 VBlankCount = 60 ;
00641
00642
00643 while ( ( getLastARM7StatusWord() != ARM7IPCShutdown )
00644 && ( VBlankCount > 0 ) )
00645 {
00646
00647 #if CEYLAN_DEBUG_FIFO
00648
00649 if ( VBlankCount % 10 == 0 )
00650 LogPlug::trace( "deactivating: " + interpretLastARM7StatusWord() ) ;
00651
00652 #endif // CEYLAN_DEBUG_FIFO
00653
00654 atomicSleep() ;
00655 VBlankCount-- ;
00656
00657 }
00658
00659
00660 if ( VBlankCount == 0 )
00661 LogPlug::error( "FIFO::deactivate ended on a time-out" ) ;
00662 else
00663 CEYLAN_LOG_FIFO( "ARM7 shutdown successful" ) ;
00664
00665
00666 irqDisable( IRQ_IPC_SYNC ) ;
00667 REG_IPC_SYNC &= ~IPC_SYNC_IRQ_ENABLE ;
00668
00669
00670 REG_IPC_FIFO_CR = REG_IPC_FIFO_CR & ~IPC_FIFO_ENABLE ;
00671
00672 _activated = false ;
00673
00674
00675 #endif // CEYLAN_RUNS_ON_ARM9
00676
00677 #else // CEYLAN_ARCH_NINTENDO_DS
00678
00679 throw FIFO::FIFOException( "FIFO constructor failed: "
00680 "not available on this platform." ) ;
00681
00682 #endif // CEYLAN_ARCH_NINTENDO_DS
00683
00684 }
00685
00686
00687
00688
00689
00690
00691
00692 ARM7StatusWord FIFO::getLastARM7StatusWord()
00693 {
00694
00695 #if CEYLAN_ARCH_NINTENDO_DS
00696
00697 if ( _arm7StatusWordPointer == 0 )
00698 return NoStatusVariableAvailable ;
00699
00700
00701 volatile ARM7StatusWord * temp = (volatile ARM7StatusWord *)
00702 ( ( (Ceylan::Uint32) _arm7StatusWordPointer) | 0x400000 ) ;
00703
00704 return ( *temp ) ;
00705
00706 #else // CEYLAN_ARCH_NINTENDO_DS
00707
00708
00709 return 1 ;
00710
00711 #endif // CEYLAN_ARCH_NINTENDO_DS
00712
00713 }
00714
00715
00716
00717 string FIFO::interpretLastARM7StatusWord()
00718 {
00719
00720 ARM7StatusWord status = getLastARM7StatusWord() ;
00721
00722
00723
00724
00725
00726
00727
00728
00729 if ( status == StatusVoluntarilyLeftBlank )
00730 return "unexpected unassigned null value for ARM7 status word" ;
00731
00732 if ( status == ARM7Running )
00733 return "ARM7 running normally" ;
00734
00735 if ( status == ARM7InError )
00736 return "ARM7 in error: " + interpretLastARM7ErrorCode() ;
00737
00738 if ( status == ARM7IPCShutdown )
00739 return "ARM7 IPC shutdown" ;
00740
00741 if ( status == NoStatusAvailable )
00742 return "no ARM7 status available" ;
00743
00744 if ( status == NoStatusVariableAvailable )
00745 return "ARM7 status variable not set" ;
00746
00747 if ( status == StatusReset )
00748 return "ARM7 status had been reset" ;
00749
00750 return "unexpected ARM7 status word (" + Ceylan::toString( status ) + ")" ;
00751
00752
00753 }
00754
00755
00756
00757 ARM7ErrorCode FIFO::getLastARM7ErrorCode()
00758 {
00759
00760
00761 if ( _arm7ErrorCodePointer == 0 )
00762 return NoErrorVariableAvailable ;
00763
00764
00765 volatile ARM7ErrorCode * temp = (volatile ARM7ErrorCode *)
00766 ( ( (Ceylan::Uint32) _arm7ErrorCodePointer) | 0x400000 ) ;
00767
00768 return ( *temp ) ;
00769
00770 }
00771
00772
00773
00774 string FIFO::interpretLastARM7ErrorCode()
00775 {
00776
00777 ARM7ErrorCode error = getLastARM7ErrorCode() ;
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788 if ( error == ErrorVoluntarilyLeftBlank )
00789 return "unexpected unassigned null value for ARM7 error code" ;
00790
00791 if ( error == UnexpectedSystemCommand )
00792 return "ARM7 received an unknown system (Ceylan-specific) command" ;
00793
00794 if ( error == UnexpectedApplicationCommand )
00795 return "ARM7 received an unknown user "
00796 "(application-specific) command" ;
00797
00798 if ( error == FIFOErrorWhileReading )
00799 return "ARM7 encountered a FIFO error while reading" ;
00800
00801 if ( error == FIFOErrorWhileWriting )
00802 return "ARM7 encountered a FIFO error while writing" ;
00803
00804 if ( error == NoError )
00805 return "ARM7 has no error registered" ;
00806
00807 if ( error == NoErrorVariableAvailable )
00808 return "ARM7 error variable not set" ;
00809
00810 if ( error == CommandOverlapping )
00811 return "ARM7 prevented attempt of command overlapping" ;
00812
00813 if ( error == UnexpectedBehaviour )
00814 return "ARM7 received an HelloToTheARM7 (i.e. null) command" ;
00815
00816 if ( error == IPCAlreadyStarted )
00817 return
00818 "ARM7 received an IPC start command whereas already started" ;
00819
00820 if ( error == IPCAlreadyStopped )
00821 return "ARM7 received an IPC stop command whereas not running" ;
00822
00823 if ( error == AwokenWithNothingToRead )
00824 return "ARM7 had its sync IRQ triggered "
00825 "whereas its input FIFO was empty" ;
00826
00827 if ( error == IncorrectInitialStatus )
00828 return "ARM7 detected its initial status, set by the ARM9, "
00829 "is incorrect" ;
00830
00831 if ( error == IncorrectInitialError )
00832 return "ARM7 detected its initial error code, set by the ARM9, "
00833 "is incorrect" ;
00834
00835 if ( error == UnexpectedCommandCount )
00836 return "ARM7 encountered a command embedding an unexpected count" ;
00837
00838 if ( error == IncorrectApplicationAnswer )
00839 return "ARM7 received an incorrect application-specific answer" ;
00840
00841 if ( error == FIFOTimeOutWhileReading )
00842 return "ARM7 made a time-out while reading" ;
00843
00844 if ( error == FIFOTimeOutWhileWriting )
00845 return "ARM7 made a time-out while writing" ;
00846
00847
00848 if ( error > 1023 )
00849 return "Non-Ceylan error code: " + Ceylan::toString( error ) ;
00850 else
00851 return "unexpected ARM7 error code (" + Ceylan::toString( error )
00852 + ")" ;
00853
00854 }
00855
00856
00857
00858 void FIFO::sendBatteryStatusRequest()
00859 {
00860
00861 InterruptMask previous = SetEnabledInterrupts( AllInterruptsDisabled ) ;
00862
00863
00864 _batteryStatus = BatteryStatusUnknown ;
00865
00866 FIFOElement commandElement = prepareFIFOCommand( BatteryStatusRequest ) ;
00867
00868 writeBlocking( commandElement ) ;
00869
00870 SetEnabledInterrupts( previous ) ;
00871
00872 notifyCommandToARM7() ;
00873
00874 }
00875
00876
00877
00878 BatteryStatus FIFO::getBatteryStatus()
00879 {
00880
00881 Ceylan::Uint8 sleepCount = 10 ;
00882
00883 while ( ( sleepCount > 0 ) && ( _batteryStatus == BatteryStatusUnknown ) )
00884 {
00885
00886 atomicSleep() ;
00887 sleepCount-- ;
00888
00889 }
00890
00891 if ( _batteryStatus == BatteryStatusUnknown )
00892 throw FIFOException( "FIFO::getBatteryStatus failed: "
00893 "time-out expired while waiting for status update" ) ;
00894
00895 return _batteryStatus ;
00896
00897 }
00898
00899
00900
00901 void FIFO::sendDSTypeRequest()
00902 {
00903
00904 InterruptMask previous = SetEnabledInterrupts( AllInterruptsDisabled ) ;
00905
00906
00907 _dsType = DSTypeUnknown ;
00908
00909 FIFOElement commandElement = prepareFIFOCommand( DSTypeRequest ) ;
00910
00911 writeBlocking( commandElement ) ;
00912
00913 SetEnabledInterrupts( previous ) ;
00914
00915 notifyCommandToARM7() ;
00916
00917 }
00918
00919
00920
00921 DSType FIFO::getDSType()
00922 {
00923
00924 Ceylan::Uint8 sleepCount = 10 ;
00925
00926 while ( ( sleepCount > 0 ) && ( _dsType == DSTypeUnknown ) )
00927 {
00928
00929 atomicSleep() ;
00930 sleepCount-- ;
00931
00932 }
00933
00934 if ( _dsType == DSTypeUnknown )
00935 throw FIFOException( "FIFO::getDSType failed: "
00936 "time-out expired while waiting for type update" ) ;
00937
00938 return _dsType ;
00939
00940 }
00941
00942
00943
00944 const std::string FIFO::toString( Ceylan::VerbosityLevels level ) const
00945 {
00946
00947 string res ;
00948
00949 if ( _activated )
00950 res = "Activated" ;
00951 else
00952 res = "Non-activated" ;
00953
00954 res += "FIFO with " ;
00955
00956 if ( ! dataAvailableForReading() )
00957 res += "no " ;
00958
00959 res += "data available for reading, and with " ;
00960
00961 if ( ! spaceAvailableForWriting() )
00962 res += "no " ;
00963
00964 res += "space available for writing" ;
00965
00966 if ( level == Ceylan::low )
00967 return res ;
00968
00969 if ( _arm7StatusWordPointer == 0 )
00970 res += ". State variables not available" ;
00971
00972 res += ". 8-bit overall (truncated) local command count is "
00973 + Ceylan::toString( _localCommandCount ) + ", remote one is "
00974 + Ceylan::toString( _remoteCommandCount ) + ", processed count is "
00975 + Ceylan::toString( getProcessedCount() ) + ", sent count is "
00976 + Ceylan::toString( getSentCount() )
00977 + ", ARM7 processed count (if used) is "
00978 + GetARM7ProcessedCount() ;
00979
00980 return res ;
00981
00982 }
00983
00984
00985
00986 FIFOCommandID FIFO::GetFIFOCommandIDFrom( const FIFOElement & element )
00987 {
00988
00989 return ( element & 0xff000000 ) >> 24 ;
00990
00991 }
00992
00993
00994
00995 FIFOCommandCount FIFO::GetFIFOCommandCountFrom(
00996 const FIFOElement & element )
00997 {
00998
00999 return ( element & 0x00ff0000 ) >> 16 ;
01000
01001 }
01002
01003
01004
01005 FIFOCommandCount FIFO::GetARM7ProcessedCount()
01006 {
01007
01008 #if CEYLAN_ARCH_NINTENDO_DS
01009
01010 #ifdef CEYLAN_RUNS_ON_ARM9
01011
01012
01013 return REG_IPC_SYNC & 0x0f ;
01014
01015 #endif // CEYLAN_RUNS_ON_ARM9
01016
01017
01018 return 0 ;
01019
01020 #else // CEYLAN_ARCH_NINTENDO_DS
01021
01022
01023 return 0 ;
01024
01025 #endif // CEYLAN_ARCH_NINTENDO_DS
01026
01027 }
01028
01029
01030
01031 FIFOCommandCount FIFO::GetARM9ProcessedCount()
01032 {
01033
01034 #if CEYLAN_ARCH_NINTENDO_DS
01035
01036 #ifdef CEYLAN_RUNS_ON_ARM9
01037
01038
01039 return ( REG_IPC_SYNC & 0x0f00 ) >> 8 ;
01040
01041 #endif // CEYLAN_RUNS_ON_ARM9
01042
01043
01044 return 0 ;
01045
01046 #else // CEYLAN_ARCH_NINTENDO_DS
01047
01048
01049 return 0 ;
01050
01051 #endif // CEYLAN_ARCH_NINTENDO_DS
01052
01053 }
01054
01055
01056
01057 void FIFO::VBlankHandlerForFIFO()
01058 {
01059
01060 #if CEYLAN_ARCH_NINTENDO_DS
01061
01062 #ifdef CEYLAN_RUNS_ON_ARM9
01063
01064
01065
01066
01067 if ( ! ( REG_IPC_FIFO_CR & IPC_FIFO_RECV_EMPTY ) )
01068 {
01069
01070
01071
01072
01073
01074
01075 ManageReceivedCommand() ;
01076
01077 }
01078
01079 #endif // CEYLAN_RUNS_ON_ARM9
01080
01081 #endif // CEYLAN_ARCH_NINTENDO_DS
01082
01083 }
01084
01085
01086
01087 FIFO & FIFO::GetActivatedFIFO() throw ( FIFOException )
01088 {
01089
01090 if ( _FIFO == 0 )
01091 throw FIFOException( "FIFO::GetActivatedFIFO failed: "
01092 "no FIFO already available" ) ;
01093
01094 if ( ! _FIFO ->isActive() )
01095 throw FIFOException( "FIFO::GetActivatedFIFO failed: "
01096 "a FIFO is available but it is not active" ) ;
01097
01098 return *_FIFO ;
01099
01100 }
01101
01102
01103
01104 FIFO & FIFO::GetExistingFIFO() throw ( FIFOException )
01105 {
01106
01107 if ( _FIFO == 0 )
01108 throw FIFOException( "FIFO::GetExistingFIFO failed: "
01109 "no FIFO available" ) ;
01110
01111 return *_FIFO ;
01112
01113 }
01114
01115
01116
01117 FIFO & FIFO::GetFIFO() throw ( FIFOException )
01118 {
01119
01120
01121 if ( _FIFO == 0 )
01122 new FIFO() ;
01123
01124 return *_FIFO ;
01125
01126 }
01127
01128
01129
01130 bool FIFO::RemoveFIFO()
01131 {
01132
01133 if ( _FIFO != 0 )
01134 {
01135
01136 delete _FIFO ;
01137
01138
01139 _FIFO = 0 ;
01140
01141 return true ;
01142
01143 }
01144
01145 return false ;
01146
01147 }
01148
01149
01150
01151
01152
01153
01154
01155 void FIFO::handleReceivedCommand()
01156 {
01157
01158 #if CEYLAN_ARCH_NINTENDO_DS
01159
01160
01161 FIFOElement firstElement ;
01162 FIFOCommandID id ;
01163
01164
01165
01166
01167
01168
01169
01170
01171
01172
01173
01174
01175
01176
01177 while ( dataAvailableForReading() )
01178 {
01179
01180
01181 firstElement = readBlocking() ;
01182
01183 #if CEYLAN_SAFE_FIFO
01184
01185 FIFOCommandCount count = GetFIFOCommandCountFrom( firstElement ) ;
01186
01187 if ( count != _remoteCommandCount )
01188 {
01189
01190 LogPlug::error( "FIFO::handleReceivedCommand: "
01191 "unexpected embedded command count of "
01192 + Ceylan::toNumericalString( count ) + " where "
01193 + Ceylan::toNumericalString( _remoteCommandCount )
01194 + " was expected." ) ;
01195
01196 return ;
01197
01198 }
01199
01200 _remoteCommandCount++ ;
01201
01202 #endif // CEYLAN_SAFE_FIFO
01203
01204
01205 id = GetFIFOCommandIDFrom( firstElement ) ;
01206
01207 if ( id > 127 )
01208 {
01209
01210
01211 handleReceivedApplicationCommand( id, firstElement ) ;
01212
01213 }
01214 else
01215 {
01216
01217 if ( id < 33 )
01218 {
01219
01220
01221 handleReceivedSystemSpecificCommand( id, firstElement ) ;
01222
01223 }
01224 else
01225 {
01226
01227
01228 handleReceivedIntegratingLibrarySpecificCommand( id,
01229 firstElement ) ;
01230
01231 }
01232
01233 }
01234
01235
01236
01237 incrementProcessedCount() ;
01238
01239
01240 }
01241
01242 #endif // CEYLAN_ARCH_NINTENDO_DS
01243
01244 }
01245
01246
01247
01248 void FIFO::handleReceivedSystemSpecificCommand( FIFOCommandID commandID,
01249 FIFOElement firstElement )
01250 {
01251
01252 #if CEYLAN_ARCH_NINTENDO_DS
01253
01254
01255
01256
01257
01258
01259
01260
01261 if ( commandID == HelloToTheARM9 )
01262 {
01263
01264 LogPlug::info( "The ARM7 says hello to the ARM9 !" ) ;
01265
01266 }
01267 else if ( commandID == PongARM9 )
01268 {
01269
01270 LogPlug::info( "Pong answer received from the ARM7" ) ;
01271
01272 }
01273 else if ( commandID == BatteryStatusAnswer )
01274 {
01275
01276
01277 if ( ( firstElement & 0x000000ff ) != 0 )
01278 _batteryStatus = WellCharged ;
01279 else
01280 _batteryStatus = AlmostEmpty ;
01281
01282 }
01283 else if ( commandID == DSTypeAnswer )
01284 {
01285
01286
01287 if ( ( firstElement & 0x000000ff ) == 1 )
01288 _dsType = DSFat ;
01289 else if ( ( firstElement & 0x000000ff ) == 2 )
01290 _dsType = DSLite ;
01291
01292 }
01293 else
01294 {
01295
01296 LogPlug::error( "handleReceivedSystemSpecificCommand ) "
01297 "unexpected command ) " + Ceylan::toNumericalString( commandID )
01298 + ", ignored." ) ;
01299 }
01300
01301
01302 #endif // CEYLAN_ARCH_NINTENDO_DS
01303
01304 }
01305
01306
01307
01308 void FIFO::handleReceivedIntegratingLibrarySpecificCommand(
01309 FIFOCommandID commandID, FIFOElement firstElement )
01310 {
01311
01312
01313
01314 #if CEYLAN_ARCH_NINTENDO_DS
01315
01316
01317 LogPlug::warning( "handleReceivedIntegratingLibrarySpecificCommand: "
01318 "received command whose ID is " + Ceylan::toNumericalString( commandID )
01319 + ", ignored." ) ;
01320
01321 #endif // CEYLAN_ARCH_NINTENDO_DS
01322
01323 }
01324
01325
01326
01327 void FIFO::handleReceivedApplicationCommand( FIFOCommandID commandID,
01328 FIFOElement firstElement )
01329 {
01330
01331
01332
01333
01334
01335
01336 handleUnexpectedApplicationCommand( commandID ) ;
01337
01338 }
01339
01340
01341
01342 void FIFO::handleUnexpectedApplicationCommand( FIFOCommandID commandID )
01343 {
01344
01345 #if CEYLAN_ARCH_NINTENDO_DS
01346
01347 LogPlug::error( "handleUnexpectedApplicationCommand called: "
01348 "unexpected application-specific command identifier: "
01349 + Ceylan::toNumericalString( commandID ) ) ;
01350
01351 #endif // CEYLAN_ARCH_NINTENDO_DS
01352
01353 }
01354
01355
01356
01357 FIFOElement FIFO::prepareFIFOCommand( FIFOCommandID id )
01358 {
01359
01360 #if CEYLAN_ARCH_NINTENDO_DS
01361
01362 #if CEYLAN_SAFE_FIFO
01363
01364 FIFOElement res = ( id << 24 ) | ( _localCommandCount << 16 ) ;
01365
01366
01367 _localCommandCount++ ;
01368
01369 return res ;
01370
01371 #else // CEYLAN_SAFE_FIFO
01372
01373 FIFOElement res = id << 24 ;
01374
01375 return res ;
01376
01377 #endif // CEYLAN_SAFE_FIFO
01378
01379 #else // CEYLAN_ARCH_NINTENDO_DS
01380
01381
01382 return 0 ;
01383
01384 #endif // CEYLAN_ARCH_NINTENDO_DS
01385
01386 }
01387
01388
01389
01390 void FIFO::notifyCommandToARM7()
01391 {
01392
01393 _sentCount++ ;
01394 sendSynchronizeInterruptToARM7() ;
01395
01396 }
01397
01398
01399
01400 void FIFO::sendSynchronizeInterruptToARM7()
01401 {
01402
01403 #if CEYLAN_ARCH_NINTENDO_DS
01404
01405 #ifdef CEYLAN_RUNS_ON_ARM9
01406
01407
01408
01409
01410
01411
01412
01413
01414
01415
01416
01417
01418
01419 REG_IPC_SYNC = REG_IPC_SYNC | IPC_SYNC_IRQ_REQUEST ;
01420
01421
01422
01423 #endif // CEYLAN_RUNS_ON_ARM9
01424
01425 #endif // CEYLAN_ARCH_NINTENDO_DS
01426
01427 }
01428
01429
01430
01431 bool FIFO::dataAvailableForReading() const
01432 {
01433
01434 #if CEYLAN_ARCH_NINTENDO_DS
01435
01436 return ! ( REG_IPC_FIFO_CR & IPC_FIFO_RECV_EMPTY ) ;
01437
01438 #else // CEYLAN_ARCH_NINTENDO_DS
01439
01440
01441 return false ;
01442
01443 #endif // CEYLAN_ARCH_NINTENDO_DS
01444
01445 }
01446
01447
01448
01449 bool FIFO::spaceAvailableForWriting() const
01450 {
01451
01452 #if CEYLAN_ARCH_NINTENDO_DS
01453
01454 return ! ( REG_IPC_FIFO_CR & IPC_FIFO_SEND_FULL ) ;
01455
01456 #else // CEYLAN_ARCH_NINTENDO_DS
01457
01458
01459 return 1 ;
01460
01461 #endif // CEYLAN_ARCH_NINTENDO_DS
01462
01463 }
01464
01465
01466
01467 FIFOElement FIFO::read()
01468 {
01469
01470
01471
01472 #if CEYLAN_ARCH_NINTENDO_DS
01473
01474 #if CEYLAN_SAFE_FIFO
01475
01476 if ( REG_IPC_FIFO_CR & IPC_FIFO_ERROR )
01477 throw FIFOException( "FIFO::read: FIFO in error before reading" ) ;
01478
01479 #endif // CEYLAN_SAFE_FIFO
01480
01481
01482 if ( ! dataAvailableForReading() )
01483 throw FIFOEmpty( "Cannot read from an empty FIFO" ) ;
01484
01485 FIFOElement res = REG_IPC_FIFO_RX ;
01486
01487
01488 #if CEYLAN_SAFE_FIFO
01489
01490 if ( REG_IPC_FIFO_CR & IPC_FIFO_ERROR )
01491 throw FIFOException( "FIFO::read: FIFO in error after reading" ) ;
01492
01493 #endif // CEYLAN_SAFE_FIFO
01494
01495 return res ;
01496
01497 #else // CEYLAN_ARCH_NINTENDO_DS
01498
01499
01500 return 1 ;
01501
01502 #endif // CEYLAN_ARCH_NINTENDO_DS
01503
01504 }
01505
01506
01507
01508 FIFOElement FIFO::readBlocking()
01509 {
01510
01511
01512
01513 #if CEYLAN_ARCH_NINTENDO_DS
01514
01515 #if CEYLAN_SAFE_FIFO
01516
01517 if ( REG_IPC_FIFO_CR & IPC_FIFO_ERROR )
01518 throw FIFOException(
01519 "FIFO::readBlocking: FIFO in error before waiting" ) ;
01520
01521 #endif // CEYLAN_SAFE_FIFO
01522
01523
01524 Ceylan::Uint32 attemptCount = 100000 ;
01525
01526
01527
01528 while ( ! dataAvailableForReading() && attemptCount > 0 )
01529 attemptCount-- ;
01530
01531 if ( attemptCount == 0 )
01532 {
01533
01534 LogPlug::warning( "FIFO::readBlocking: never ending?" ) ;
01535
01536
01537 sendSynchronizeInterruptToARM7() ;
01538
01539 while ( ! dataAvailableForReading() )
01540 ;
01541
01542 }
01543
01544 #if CEYLAN_SAFE_FIFO
01545
01546 if ( REG_IPC_FIFO_CR & IPC_FIFO_ERROR )
01547 throw FIFOException( "FIFO::readBlocking: "
01548 "FIFO in error after waiting but before reading" ) ;
01549
01550 #endif // CEYLAN_SAFE_FIFO
01551
01552
01553 FIFOElement res = REG_IPC_FIFO_RX ;
01554
01555
01556 #if CEYLAN_SAFE_FIFO
01557
01558 if ( REG_IPC_FIFO_CR & IPC_FIFO_ERROR )
01559 throw FIFOException( "FIFO::readBlocking: "
01560 "FIFO in error after reading" ) ;
01561
01562 #endif // CEYLAN_SAFE_FIFO
01563
01564 return res ;
01565
01566 #else // CEYLAN_ARCH_NINTENDO_DS
01567
01568
01569 return 1 ;
01570
01571 #endif // CEYLAN_ARCH_NINTENDO_DS
01572
01573 }
01574
01575
01576
01577 void FIFO::write( FIFOElement toSend )
01578 {
01579
01580
01581
01582 #if CEYLAN_ARCH_NINTENDO_DS
01583
01584 #if CEYLAN_SAFE_FIFO
01585
01586 if ( REG_IPC_FIFO_CR & IPC_FIFO_ERROR )
01587 throw FIFOException( "FIFO::write: FIFO in error before writing" ) ;
01588
01589 #endif // CEYLAN_SAFE_FIFO
01590
01591
01592 if ( ! spaceAvailableForWriting() )
01593 throw FIFOFull( "Cannot write to a full FIFO" ) ;
01594
01595
01596 REG_IPC_FIFO_TX = toSend ;
01597
01598
01599 #if CEYLAN_SAFE_FIFO
01600
01601 if ( REG_IPC_FIFO_CR & IPC_FIFO_ERROR )
01602 throw FIFOException( "FIFO::write: FIFO in error after writing" ) ;
01603
01604 #endif // CEYLAN_SAFE_FIFO
01605
01606 #endif // CEYLAN_ARCH_NINTENDO_DS
01607
01608 }
01609
01610
01611
01612 void FIFO::writeBlocking( FIFOElement toSend )
01613 {
01614
01615
01616
01617 #if CEYLAN_ARCH_NINTENDO_DS
01618
01619 #if CEYLAN_SAFE_FIFO
01620
01621 if ( REG_IPC_FIFO_CR & IPC_FIFO_ERROR )
01622 throw FIFOException(
01623 "FIFO::writeBlocking: FIFO in error before waiting" ) ;
01624
01625 #endif // CEYLAN_SAFE_FIFO
01626
01627
01628
01629 Ceylan::Uint32 attemptCount = 100000 ;
01630
01631
01632
01633 while ( ! spaceAvailableForWriting() && attemptCount > 0 )
01634 attemptCount-- ;
01635
01636
01637 if ( attemptCount == 0 )
01638 {
01639
01640 LogPlug::warning( "FIFO::writeBlocking: never ending?" ) ;
01641
01642
01643 sendSynchronizeInterruptToARM7() ;
01644
01645 while ( ! spaceAvailableForWriting() )
01646 ;
01647
01648 }
01649
01650
01651 #if CEYLAN_SAFE_FIFO
01652
01653 if ( REG_IPC_FIFO_CR & IPC_FIFO_ERROR )
01654 throw FIFOException( "FIFO::writeBlocking: "
01655 "FIFO in error after waiting but before writing" ) ;
01656
01657 #endif // CEYLAN_SAFE_FIFO
01658
01659
01660 REG_IPC_FIFO_TX = toSend ;
01661
01662
01663 #if CEYLAN_SAFE_FIFO
01664
01665 if ( REG_IPC_FIFO_CR & IPC_FIFO_ERROR )
01666 throw FIFOException( "FIFO::writeBlocking: "
01667 "FIFO in error after writing" ) ;
01668
01669 #endif // CEYLAN_SAFE_FIFO
01670
01671 #endif // CEYLAN_ARCH_NINTENDO_DS
01672
01673 }
01674
01675
01676
01677 FIFOCommandCount FIFO::getProcessedCount() const
01678 {
01679
01680 return ( _processedCount & 0x0f ) ;
01681
01682 }
01683
01684
01685
01686 FIFOCommandCount FIFO::getSentCount() const
01687 {
01688
01689 return ( _sentCount & 0x0f ) ;
01690
01691 }
01692
01693
01694
01695 void FIFO::incrementProcessedCount()
01696 {
01697
01698 #if CEYLAN_ARCH_NINTENDO_DS
01699
01700 _processedCount++ ;
01701
01702
01703
01704
01705
01706
01707
01708
01709
01710
01711
01712
01713 #endif // CEYLAN_ARCH_NINTENDO_DS
01714
01715 }
01716
01717
01718
01719
01720
01721
01722
01723 void FIFO::SyncHandlerForFIFO()
01724 {
01725
01726
01727
01728 #if CEYLAN_ARCH_NINTENDO_DS
01729
01730
01731 if ( ! ( REG_IPC_FIFO_CR & IPC_FIFO_RECV_EMPTY ) )
01732 ManageReceivedCommand() ;
01733
01734 #endif // CEYLAN_ARCH_NINTENDO_DS
01735
01736 }
01737
01738
01739
01740 void FIFO::ManageReceivedCommand()
01741 {
01742
01743 #if CEYLAN_ARCH_NINTENDO_DS
01744
01753 #if CEYLAN_SAFE_FIFO
01754
01770 static bool CommandInProgress = false ;
01771
01772
01773
01774
01775
01776
01777
01778
01779
01780
01781
01782
01783
01784
01785
01786
01787
01788
01789 if ( ! CommandInProgress )
01790 {
01791
01792 CommandInProgress = true ;
01793
01794 #endif // CEYLAN_SAFE_FIFO
01795
01796
01797 if ( _FIFO != 0 )
01798 _FIFO->handleReceivedCommand() ;
01799
01800 #if CEYLAN_SAFE_FIFO
01801
01802 CommandInProgress = false ;
01803
01804 }
01805 else
01806 {
01807
01808 LogPlug::warning( "FIFO::ManageReceivedCommand: "
01809 "concurrent calls hindered" ) ;
01810
01811 }
01812
01813
01814
01815
01816
01817
01818
01819
01820
01821
01822
01823 #endif // CEYLAN_SAFE_FIFO
01824
01825 #endif // CEYLAN_ARCH_NINTENDO_DS
01826
01827 }
01828
01829
01830
01831 string FIFO::DescribeCommand( FIFOElement element )
01832 {
01833
01834 #if CEYLAN_ARCH_NINTENDO_DS
01835
01836 string res = "Command is equal to " + Ceylan::toString( element )
01837 + ", i.e. " + Ceylan::toString( element, true ) ;
01838
01839 #if CEYLAN_SAFE_FIFO
01840
01841 res += ". Command ID is "
01842 + Ceylan::toNumericalString( GetFIFOCommandIDFrom( element ) )
01843 + ", command count is "
01844 + Ceylan::toNumericalString( GetFIFOCommandCountFrom( element ) ) ;
01845
01846 #else // CEYLAN_SAFE_FIFO
01847
01848 res += ". Command ID is "
01849 + Ceylan::toNumericalString( GetFIFOCommandIDFrom( element ) ) ;
01850
01851 #endif // CEYLAN_SAFE_FIFO
01852
01853 return res ;
01854
01855 #else // CEYLAN_ARCH_NINTENDO_DS
01856
01857 return "(not available on this platform)" ;
01858
01859 #endif // CEYLAN_ARCH_NINTENDO_DS
01860
01861 }
01862