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