mgh: fix for default HDD DMA mode, that wasn't correctly set
[open-ps2-loader/simon.git] / modules / mcemu / mcemu_rpc.c
blob90a433c73b24bdec0a5bf7df117d32627f9dc473
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 /* PS2 Memory Card cluster size */
11 /* do NOT change this value unless you are absolutely sure what you are doing */
12 #define MC2_CLUSTER_SIZE 0x400
14 /* size of memory to allocate for FastIO support */
15 #define FIO_ALLOC_SIZE (MC2_CLUSTER_SIZE + LIBMC_RPC_BUFFER_SIZE + sizeof(SifRpcClientData_t))
17 /* Replacement for MCMAN's library function #62 */
18 int hookMcman62()
20 register char *ptr;
22 /* checking if the memory block had been allocated */
23 if (pFastBuf == NULL)
25 /* allocating memory for Fast I/O buffer, RPC buffer and RPC client data structure */
26 ptr = (char*)_SysAlloc((FIO_ALLOC_SIZE + 0xFF) & ~(u64)0xFF);
27 if (ptr == NULL)
29 DPRINTF("Not enough memory for FastIO support.\n");
30 return 0;
33 /* initializing buffer pointers */
34 pFastBuf = ptr;
35 pFastRpcBuf = &ptr[MC2_CLUSTER_SIZE];
36 pClientData = (SifRpcClientData_t*)&ptr[MC2_CLUSTER_SIZE + LIBMC_RPC_BUFFER_SIZE];
38 else ptr = NULL;
40 /* binding to EE libmc's RPC server */
41 if (SifBindRpc(pClientData, LIBMC_RPCNO, 0) < 0)
43 if (ptr != NULL)
45 /* freeing memory on RPC bind error */
46 pFastBuf = NULL;
47 _SysFree(ptr);
50 DPRINTF("libmc RPC bind error.\n");
51 return 0;
54 return 1;
57 /* Reads file to EE memory for sceMcReadFast() support */
58 int hookMcman63(int fd, u32 eeaddr, int nbyte)
60 int oldstate;
61 SifDmaTransfer_t sdd;
62 register int rlen;
63 register int rval;
64 register int size;
65 register u32 id;
66 register char *rpcbuf, *fiobuf;
67 SifRpcClientData_t *cldata;
69 // DPRINTF("sceMcReadFast(%d, 0x%X, 0x%X)\n", fd, eeaddr, nbyte);
71 cldata = pClientData;
72 rpcbuf = (char*)pFastRpcBuf;
73 fiobuf = (char*)pFastBuf;
75 for (rlen = nbyte; rlen > 0; rlen -= size)
77 size = (rlen > MC2_CLUSTER_SIZE) ? MC2_CLUSTER_SIZE : rlen;
79 /* reading file with MCMAN's sceMcRead() call */
80 rval = pMcRead(fd, fiobuf, size);
81 if (rval < 0) return rval;
83 /* preparing to transfer data to libmc */
84 sdd.dest = (void*)((u32)fiobuf);
85 sdd.src = (void*)eeaddr;
86 sdd.size = MC2_CLUSTER_SIZE;
87 sdd.attr = 0;
89 *(int*)(&rpcbuf[0xC]) = size;
91 /* sending data to EE */
92 CpuSuspendIntr(&oldstate);
93 id = sceSifSetDma(&sdd, 1);
94 CpuResumeIntr(oldstate);
96 /* informing libmc on new data */
97 sceSifCallRpc(cldata, 2, 0, rpcbuf, LIBMC_RPC_BUFFER_SIZE, rpcbuf, LIBMC_RPC_BUFFER_SIZE, NULL, NULL);
100 return 0;
103 /* Write file from EE memory for sceMcWriteFast() support */
104 int hookMcman68(int fd, u32 eeaddr, int nbyte)
106 SifRpcReceiveData_t od;
107 register int rval;
108 register int size;
109 register int wlen;
110 register u32 ea;
111 register char *fiobuf;
113 // DPRINTF("sceMcWriteFast(%d, 0x%X, 0x%X)\n", fd, eeaddr, nbyte);
115 fiobuf = (char*)pFastBuf;
117 ea = eeaddr;
118 wlen = nbyte;
120 /* check for EE address alignment (64 bytes) */
121 rval = ea & 0x3F;
122 if (rval)
124 /* getting unaligned data from EE side */
125 SifRpcGetOtherData(&od, (void*)(ea & ~(u32)0x3F), fiobuf, MC2_CLUSTER_SIZE, 0);
127 size = MC2_CLUSTER_SIZE - rval;
128 if (wlen < size) size = wlen;
130 /* writing unaligned data to a file */
131 rval = pMcWrite(fd, &fiobuf[rval], size);
132 if (rval < 0)
134 DPRINTF("sceMcWrite error %d\n", rval);
135 return rval;
138 wlen -= size;
139 ea += size;
142 for (; wlen > 0; wlen -= size, ea += size)
144 size = (wlen > MC2_CLUSTER_SIZE) ? MC2_CLUSTER_SIZE : wlen;
146 /* receiving data from EE */
147 SifRpcGetOtherData(&od, (void*)ea, fiobuf, MC2_CLUSTER_SIZE, 0);
149 /* writing data to a file */
150 rval = pMcWrite(fd, fiobuf, size);
151 if (rval < 0)
153 DPRINTF("sceMcWrite error %d\n", rval);
154 return rval;
158 return 0;