1 /*-------------------------------------------------------------
3 arqmgr.c -- ARAM task request queue management
6 Michael Wiedenbauer (shagkur)
7 Dave Murphy (WinterMute)
9 This software is provided 'as-is', without any express or implied
10 warranty. In no event will the authors be held liable for any
11 damages arising from the use of this software.
13 Permission is granted to anyone to use this software for any
14 purpose, including commercial applications, and to alter it and
15 redistribute it freely, subject to the following restrictions:
17 1. The origin of this software must not be misrepresented; you
18 must not claim that you wrote the original software. If you use
19 this software in a product, an acknowledgment in the product
20 documentation would be appreciated but is not required.
22 2. Altered source versions must be plainly marked as such, and
23 must not be misrepresented as being the original software.
25 3. This notice may not be removed or altered from any source
29 -------------------------------------------------------------*/
35 #include "processor.h"
39 #define ARQM_STACKENTRIES 16
40 #define ARQM_ZEROBYTES 256
41 #define ROUNDUP32(x) (((u32)(x)+0x1f)&~0x1f)
43 typedef struct _arqm_info
{
45 ARQMCallback callback
;
55 static u32 __ARQMStackLocation
;
56 static u32 __ARQMFreeBytes
;
57 static u32 __ARQMStackPointer
[ARQM_STACKENTRIES
];
58 static ARQM_Info __ARQMInfo
[ARQM_STACKENTRIES
];
59 static u8 __ARQMZeroBuffer
[ARQM_ZEROBYTES
] ATTRIBUTE_ALIGN(32);
61 static void __ARQMPollCallback(ARQRequest
*req
)
64 ARQM_Info
*ptr
= NULL
;
67 for(i
=0;i
<ARQM_STACKENTRIES
;i
++) {
69 if(req
==&ptr
->arqhandle
) break;
71 if(i
>=ARQM_STACKENTRIES
) return;
78 void ARQM_Init(u32 arambase
,s32 len
)
84 __ARQMStackLocation
= 0;
85 __ARQMStackPointer
[0] = arambase
;
86 __ARQMFreeBytes
= len
;
88 for(i
=0;i
<ARQM_ZEROBYTES
/sizeof(u32
);i
++) ((u32
*)__ARQMZeroBuffer
)[i
] = 0;
89 ARQM_PushData(__ARQMZeroBuffer
,ARQM_ZEROBYTES
);
93 u32
ARQM_PushData(void *buffer
,s32 len
)
98 if(((u32
)buffer
)&0x1f || len
<=0) return 0;
100 rlen
= ROUNDUP32(len
);
101 if(__ARQMFreeBytes
>=rlen
&& __ARQMStackLocation
<(ARQM_STACKENTRIES
-1)) {
102 ptr
= &__ARQMInfo
[__ARQMStackLocation
];
104 _CPU_ISR_Disable(level
);
106 ptr
->aram_start
= __ARQMStackPointer
[__ARQMStackLocation
++];
107 __ARQMStackPointer
[__ARQMStackLocation
] = ptr
->aram_start
+rlen
;
108 __ARQMFreeBytes
-= rlen
;
110 ARQ_PostRequestAsync(&ptr
->arqhandle
,__ARQMStackLocation
-1,ARQ_MRAMTOARAM
,ARQ_PRIO_HI
,ptr
->aram_start
,(u32
)buffer
,rlen
,__ARQMPollCallback
);
111 _CPU_ISR_Restore(level
);
113 while(ptr
->polled
==FALSE
);
114 return (ptr
->aram_start
);
123 _CPU_ISR_Disable(level
);
125 if(__ARQMStackLocation
>1) {
126 __ARQMFreeBytes
+= (__ARQMStackPointer
[__ARQMStackLocation
]-__ARQMStackPointer
[__ARQMStackLocation
-1]);
127 __ARQMStackLocation
--;
129 _CPU_ISR_Restore(level
);
132 u32
ARQM_GetZeroBuffer()
134 return __ARQMStackPointer
[0];
137 u32
ARQM_GetStackPointer()
141 _CPU_ISR_Disable(level
)
142 tmp
= __ARQMStackPointer
[__ARQMStackLocation
];
143 _CPU_ISR_Restore(level
);
148 u32
ARQM_GetFreeSize()
152 _CPU_ISR_Disable(level
)
153 tmp
= __ARQMFreeBytes
;
154 _CPU_ISR_Restore(level
);