mgh: fix for default HDD DMA mode, that wasn't correctly set
[open-ps2-loader.git] / modules / mcemu / mcemu_sys.c
blob88894e1e72be0e374176ff234980080115d4e550
1 /*
2 Copyright 2006-2008, Romz
3 Copyright 2010, Polo
4 Licenced under Academic Free License version 3.0
5 Review OpenUsbLd README & LICENSE files for further details.
6 */
8 #include "mcemu.h"
10 /* prototype for LOADCORE's function */
11 void *QueryLibraryEntryTable(iop_library_t *lib);
13 //---------------------------------------------------------------------------
14 /* Returns a pointer to a library entry table */
15 void *GetExportTable(char *libname, int version)
17 if (libname != NULL)
19 iop_library_t lib;
20 register int i;
21 register char *psrc;
23 mips_memset(&lib, 0, sizeof(iop_library_t));
24 lib.version = version;
26 for (i = 0, psrc = libname; (i < 8) && (*psrc); i ++, psrc ++) lib.name[i] = *psrc;
28 return QueryLibraryEntryTable(&lib);
31 return NULL;
33 //------------------------------
34 //endfunc
35 //---------------------------------------------------------------------------
36 /* Returns number of entries in the export table */
37 u32 GetExportTableSize(void *table)
39 register void **exp;
40 register u32 size;
42 exp = (void**)table;
43 size = 0;
45 if (exp != NULL) while (*exp ++ != NULL) size ++;
47 return size;
49 //------------------------------
50 //endfunc
51 //---------------------------------------------------------------------------
52 /* Returns an entry from the export table */
53 void *GetExportEntry(void *table, u32 entry)
55 if (entry < GetExportTableSize(table))
57 register void **exp;
59 exp = (void**)table;
61 return exp[entry];
64 return NULL;
66 //------------------------------
67 //endfunc
68 //---------------------------------------------------------------------------
69 /* Replaces an entry in the export table */
70 void *HookExportEntry(void *table, u32 entry, void *func)
72 if (entry < GetExportTableSize(table))
74 int oldstate;
75 register void **exp, *temp;
77 exp = (void**)table;
78 exp = &exp[entry];
80 CpuSuspendIntr(&oldstate);
81 temp = *exp;
82 *exp = func;
83 func = temp;
84 CpuResumeIntr(oldstate);
86 return func;
89 return NULL;
91 //------------------------------
92 //endfunc
93 //---------------------------------------------------------------------------
94 /* Allocates system memory on IOP */
95 void *_SysAlloc(u64 size)
97 int oldstate;
98 register void *p;
100 CpuSuspendIntr(&oldstate);
101 p = AllocSysMemory(ALLOC_FIRST, size, NULL);
102 CpuResumeIntr(oldstate);
104 return p;
106 //------------------------------
107 //endfunc
108 //---------------------------------------------------------------------------
109 /* Frees system memory on IOP */
110 int _SysFree(void *area)
112 int oldstate;
113 register int r;
115 CpuSuspendIntr(&oldstate);
116 r = FreeSysMemory(area);
117 CpuResumeIntr(oldstate);
119 return r;
121 //------------------------------
122 //endfunc
123 //---------------------------------------------------------------------------
124 /* Makes an integer value */
125 int GetInt(void *ptr)
127 register u8 *p;
129 p = (u8*)ptr;
131 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
133 //------------------------------
134 //endfunc
135 //---------------------------------------------------------------------------
136 /* Calculates a "checksum" */
137 u32 CalculateEDC(u8 *buf, u32 size)
139 register int i, x;
140 register u8 *p;
142 p = buf;
143 i = x = 0;
144 while (i ++ < size) x ^= *p ++;
146 return x & 0xFF;
148 //------------------------------
149 //endfunc
150 //---------------------------------------------------------------------------
151 /* Calculates ECC for a 0x80 bytes of data */
152 void CalculateECC(u8 *buf, void *chk)
154 register u8 *ptr;
155 register int i, c1, c2, b, c3;
157 ptr = buf;
158 i = 0;
159 c1 = c2 = c3 = 0;
161 /* calculating ECC for a 0x80-bytes buffer */
164 b = xortable[*ptr ++];
165 c1 ^= b;
166 if (b & 0x80)
168 c2 ^= ~i;
169 c3 ^= i;
171 } while (++ i < 0x80);
173 /* storing ECC */
174 ptr = (u8*)chk;
175 ptr[0] = ~c1 & 0x77;
176 ptr[1] = ~c2 & 0x7F;
177 ptr[2] = ~c3 & 0x7F;
179 //------------------------------
180 //endfunc
181 //---------------------------------------------------------------------------
182 #ifdef HDD_DRIVER
183 /* Return ata sector corresponding to page number in vmc file */
184 u32 Mcpage_to_Apasector(int mc_num, u32 mc_page)
186 register int i;
187 register u32 sector_to_read, lbound, ubound;
189 lbound = 0;
190 ubound = 0;
191 sector_to_read = 0;
193 for(i = 0; i < 10; i++)
195 lbound=ubound;
196 ubound+=vmcSpec[mc_num].blocks[i].count;
198 if((mc_page >= (lbound << 4)) && (mc_page < (ubound << 4)))
199 sector_to_read = vmcSpec[mc_num].parts[vmcSpec[mc_num].blocks[i].subpart].start + (vmcSpec[mc_num].blocks[i].number << 4) + (mc_page - (lbound << 4));
202 return sector_to_read;
204 //------------------------------
205 //endfunc
206 //---------------------------------------------------------------------------
207 #endif
208 //---------------------------------------------------------------------------
209 // End of file: mcsiosys.c
210 //---------------------------------------------------------------------------