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
;
66 for(i
=0;i
<ARQM_STACKENTRIES
;i
++) {
68 if(req
==&ptr
->arqhandle
) break;
70 if(i
>=ARQM_STACKENTRIES
) return;
76 void ARQM_Init(u32 arambase
,s32 len
)
82 __ARQMStackLocation
= 0;
83 __ARQMStackPointer
[0] = arambase
;
84 __ARQMFreeBytes
= len
;
86 for(i
=0;i
<ARQM_ZEROBYTES
/sizeof(u32
);i
++) ((u32
*)__ARQMZeroBuffer
)[i
] = 0;
87 ARQM_PushData(__ARQMZeroBuffer
,ARQM_ZEROBYTES
);
91 u32
ARQM_PushData(void *buffer
,s32 len
)
96 if(((u32
)buffer
)&0x1f || len
<=0) return 0;
98 rlen
= ROUNDUP32(len
);
99 if(__ARQMFreeBytes
>=rlen
&& __ARQMStackLocation
<(ARQM_STACKENTRIES
-1)) {
100 ptr
= &__ARQMInfo
[__ARQMStackLocation
];
102 _CPU_ISR_Disable(level
);
104 ptr
->aram_start
= __ARQMStackPointer
[__ARQMStackLocation
++];
105 __ARQMStackPointer
[__ARQMStackLocation
] = ptr
->aram_start
+rlen
;
106 __ARQMFreeBytes
-= rlen
;
108 ARQ_PostRequestAsync(&ptr
->arqhandle
,__ARQMStackLocation
-1,ARQ_MRAMTOARAM
,ARQ_PRIO_HI
,ptr
->aram_start
,(u32
)buffer
,rlen
,__ARQMPollCallback
);
109 _CPU_ISR_Restore(level
);
111 while(ptr
->polled
==FALSE
);
112 return (ptr
->aram_start
);
121 _CPU_ISR_Disable(level
);
123 if(__ARQMStackLocation
>1) {
124 __ARQMFreeBytes
+= (__ARQMStackPointer
[__ARQMStackLocation
]-__ARQMStackPointer
[__ARQMStackLocation
-1]);
125 __ARQMStackLocation
--;
127 _CPU_ISR_Restore(level
);
130 u32
ARQM_GetZeroBuffer()
132 return __ARQMStackPointer
[0];
135 u32
ARQM_GetStackPointer()
139 _CPU_ISR_Disable(level
)
140 tmp
= __ARQMStackPointer
[__ARQMStackLocation
];
141 _CPU_ISR_Restore(level
);
146 u32
ARQM_GetFreeSize()
150 _CPU_ISR_Disable(level
)
151 tmp
= __ARQMFreeBytes
;
152 _CPU_ISR_Restore(level
);