remove const from TPL_OpenTPLFromMemory since the memory is altered
[libogc.git] / libogc / arqmgr.c
blob08262b4b53568f6594ed4aa467a7ed399d3171a0
1 /*-------------------------------------------------------------
3 arqmgr.c -- ARAM task request queue management
5 Copyright (C) 2004
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
26 distribution.
29 -------------------------------------------------------------*/
32 #include <stdlib.h>
34 #include "asm.h"
35 #include "processor.h"
36 #include "arqueue.h"
37 #include "arqmgr.h"
39 #define ARQM_STACKENTRIES 16
40 #define ARQM_ZEROBYTES 256
41 #define ROUNDUP32(x) (((u32)(x)+0x1f)&~0x1f)
43 typedef struct _arqm_info {
44 ARQRequest arqhandle;
45 ARQMCallback callback;
46 void *buffer;
47 u32 file_len;
48 u32 read_len;
49 u32 aram_start;
50 u32 curr_read_offset;
51 u32 curr_aram_offset;
52 volatile BOOL polled;
53 } ARQM_Info;
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)
63 u32 i;
64 ARQM_Info *ptr = NULL;
65 ARQMCallback tccb;
67 for(i=0;i<ARQM_STACKENTRIES;i++) {
68 ptr = &__ARQMInfo[i];
69 if(req==&ptr->arqhandle) break;
71 if(i>=ARQM_STACKENTRIES) return;
73 tccb = ptr->callback;
74 ptr->callback = NULL;
75 ptr->polled = TRUE;
78 void ARQM_Init(u32 arambase,s32 len)
80 u32 i;
82 if(len<=0) return;
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)
95 u32 rlen,level;
96 ARQM_Info *ptr;
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);
105 ptr->polled = FALSE;
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);
116 return 0;
119 void ARQM_Pop()
121 u32 level;
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()
139 u32 level,tmp;
141 _CPU_ISR_Disable(level)
142 tmp = __ARQMStackPointer[__ARQMStackLocation];
143 _CPU_ISR_Restore(level);
145 return tmp;
148 u32 ARQM_GetFreeSize()
150 u32 level,tmp;
152 _CPU_ISR_Disable(level)
153 tmp = __ARQMFreeBytes;
154 _CPU_ISR_Restore(level);
156 return tmp;