change PAD_ScanPads()s behaviour. the return value now contains a bitmask of the...
[libogc.git] / libogc / arqmgr.c
blobed3a99936b9b55b170da33b0873486d8a1cd4ff2
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;
66 for(i=0;i<ARQM_STACKENTRIES;i++) {
67 ptr = &__ARQMInfo[i];
68 if(req==&ptr->arqhandle) break;
70 if(i>=ARQM_STACKENTRIES) return;
72 ptr->callback = NULL;
73 ptr->polled = TRUE;
76 void ARQM_Init(u32 arambase,s32 len)
78 u32 i;
80 if(len<=0) return;
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)
93 u32 rlen,level;
94 ARQM_Info *ptr;
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);
103 ptr->polled = FALSE;
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);
114 return 0;
117 void ARQM_Pop()
119 u32 level;
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()
137 u32 level,tmp;
139 _CPU_ISR_Disable(level)
140 tmp = __ARQMStackPointer[__ARQMStackLocation];
141 _CPU_ISR_Restore(level);
143 return tmp;
146 u32 ARQM_GetFreeSize()
148 u32 level,tmp;
150 _CPU_ISR_Disable(level)
151 tmp = __ARQMFreeBytes;
152 _CPU_ISR_Restore(level);
154 return tmp;