1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     snd_DriverCommandManager.h
4 
5   Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Revision: 25759 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NW_SND_DRIVER_COMMAND_MANAGER_H_
17 #define NW_SND_DRIVER_COMMAND_MANAGER_H_
18 
19 #include <nw/snd/snd_DriverCommand.h>
20 #include <nw/snd/snd_Config.h>
21 #include <nn/os.h>
22 
23 namespace nw {
24 namespace snd {
25 namespace internal {
26 
27 class DriverCommandManager
28 {
29   public:
30     static DriverCommandManager& GetInstance();
31     static DriverCommandManager& GetInstanceForTaskThread();
32 
33     void Initialize( void* commandBuffer, u32 commandBufferSize );
34     void Finalize();
35 
36     template< typename Command >
37     Command* AllocCommand();
38     u32 GetAllocatableCommandSize() const;
39 
40     u32 PushCommand( DriverCommand* command );
41     void RecvCommandReply();
42 
43     u32 FlushCommand( bool forceFlag );
44     void WaitCommandReply( u32 tag );
45 
46     bool IsFinishCommand( u32 tag ) const;
47 
48     void ProcessCommand();
49 
50   private:
51     static const int COMMAND_QUEUE_SIZE = 32;
52 
53     DriverCommandManager();
54 
55     void* AllocMemory( u32 count );
56     void* TryAllocMemory( u32 count );
57     void FinalizeCommandList( DriverCommand* command );
58 
59 #ifdef NW_SND_CONFIG_USE_INTERCORE_CRITICALSECTION
60     typedef nn::os::InterCoreBlockingQueue CommandQueue;
61 #else
62     typedef nn::os::BlockingQueue CommandQueue;
63 #endif
64 
65     CommandQueue   m_SendCommandQueue;
66     uptr m_SendCommandQueueBuffer[ COMMAND_QUEUE_SIZE ];
67     CommandQueue   m_RecvCommandQueue;
68     uptr m_RecvCommandQueueBuffer[ COMMAND_QUEUE_SIZE ];
69 
70     DriverCommand* m_CommandListBegin;
71     DriverCommand* m_CommandListEnd;
72 
73     u32 m_CommandTag;
74     u32 m_FinishCommandTag;
75 
76     u32* m_CommandMemoryArea;
77     u32 m_CommandMemoryAreaSize;
78 
79     u32 m_CommandMemoryAreaBegin;
80     u32 m_CommandMemoryAreaEnd;
81 
82     // m_CommandMemoryAreaBegin == m_CommandMemoryAreaBegin の時、
83     // エリアサイズがゼロなのか、最大なのかを判定するフラグ
84     bool m_CommandMemoryAreaZeroFlag;
85 };
86 
87 template< typename Command >
AllocCommand()88 Command* DriverCommandManager::AllocCommand()
89 {
90     u32 commandAreaCount = ( sizeof(Command) + sizeof(m_CommandMemoryArea[0]) - 1 ) / sizeof(m_CommandMemoryArea[0]);
91 
92     Command* command = reinterpret_cast<Command*>( AllocMemory( commandAreaCount ) );
93     command->memory_next = m_CommandMemoryAreaBegin;
94 
95     return command;
96 }
97 
98 
99 } // namespace nw::snd::internal
100 } // namespace nw::snd
101 } // namespace nw
102 
103 
104 #endif /* NW_SND_DRIVER_COMMAND_MANAGER_H_ */
105 
106