comparison src/sexypsf/PsxMem.c @ 12:3da1b8942b8b trunk

[svn] - remove src/Input src/Output src/Effect src/General src/Visualization src/Container
author nenolod
date Mon, 18 Sep 2006 03:14:20 -0700
parents src/Input/sexypsf/PsxMem.c@13389e613d67
children 8a4fbe599b05
comparison
equal deleted inserted replaced
11:cff1d04026ae 12:3da1b8942b8b
1 /* Pcsx - Pc Psx Emulator
2 * Copyright (C) 1999-2002 Pcsx Team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <string.h>
20 #include <stdlib.h>
21
22 #include "PsxCommon.h"
23
24 void LoadPSXMem(u32 address, s32 length, unsigned char *data)
25 {
26 //printf("%08x %08x\n",address,length);
27 while(length>0)
28 {
29 if(address&65535)
30 {
31 u32 tmplen;
32
33 //puts("Squishy");
34 tmplen=((65536-(address&65535))>(u32)length)?(u32)length:65536-(address&65535);
35 if(psxMemLUT[address>>16])
36 memcpy((char *)(psxMemLUT[address>>16]+(address&65535)),data,tmplen);
37 address+=tmplen;
38 data+=tmplen;
39 length-=tmplen;
40 //printf("%08x %08x\n",address,tmplen);
41 continue;
42 }
43 if(psxMemLUT[address>>16])
44 {
45 memcpy((char *)(psxMemLUT[address>>16]),data,(length<65536)?length:65536);
46 }
47 data+=65536;
48 address+=65536;
49 length-=65536;
50 }
51 }
52
53 static int writeok;
54 int psxMemInit() {
55 int i;
56
57 writeok=1;
58
59 psxMemLUT = malloc(0x10000 * sizeof *psxMemLUT);
60 memset(psxMemLUT, 0, 0x10000 * sizeof *psxMemLUT);
61
62 psxM = (char*)malloc(0x00200000);
63 psxP = (char*)malloc(0x00010000);
64 psxH = (char*)malloc(0x00010000);
65 psxR = (char*)malloc(0x00080000);
66 if (psxMemLUT == NULL || psxM == NULL || psxP == NULL || psxH == NULL || psxR == NULL) {
67 printf("Error allocating memory"); return -1;
68 }
69
70 for (i=0; i<0x80; i++) psxMemLUT[i + 0x0000] = &psxM[(i & 0x1f) << 16];
71
72 memcpy(psxMemLUT + 0x8000, psxMemLUT, 0x80 * sizeof *psxMemLUT);
73 memcpy(psxMemLUT + 0xa000, psxMemLUT, 0x80 * sizeof *psxMemLUT);
74
75 for (i=0; i<0x01; i++) psxMemLUT[i + 0x1f00] = &psxP[i << 16];
76
77 for (i=0; i<0x01; i++) psxMemLUT[i + 0x1f80] = &psxH[i << 16];
78
79 for (i=0; i<0x08; i++) psxMemLUT[i + 0xbfc0] = &psxR[i << 16];
80
81 return 0;
82 }
83
84 void psxMemReset() {
85 memset(psxM, 0, 0x00200000);
86 memset(psxP, 0, 0x00010000);
87 }
88
89 void psxMemShutdown() {
90 if (psxM)
91 free(psxM);
92
93 if (psxP)
94 free(psxP);
95
96 if (psxH)
97 free(psxH);
98
99 if (psxR)
100 free(psxR);
101
102 if (psxMemLUT)
103 free(psxMemLUT);
104
105 psxM = psxP = psxH = psxR = NULL;
106 psxMemLUT = NULL;
107 }
108
109 u8 psxMemRead8(u32 mem) {
110 char *p;
111 u32 t;
112
113 t = mem >> 16;
114 if (t == 0x1f80) {
115 if (mem < 0x1f801000)
116 return psxHu8(mem);
117 else
118 return psxHwRead8(mem);
119 } else {
120 p = (char *)(psxMemLUT[t]);
121 if (p != NULL) {
122 return *(u8 *)(p + (mem & 0xffff));
123 } else {
124 return 0;
125 }
126 }
127 }
128
129 u16 psxMemRead16(u32 mem) {
130 char *p;
131 u32 t;
132
133 t = mem >> 16;
134 if (t == 0x1f80) {
135 if (mem < 0x1f801000)
136 return BFLIP16(psxHu16(mem));
137 else
138 return psxHwRead16(mem);
139 } else {
140 p = (char *)(psxMemLUT[t]);
141 if (p != NULL) {
142 return BFLIP16(*(u16 *)(p + (mem & 0xffff)));
143 } else {
144 return 0;
145 }
146 }
147 }
148
149 u32 psxMemRead32(u32 mem) {
150 char *p;
151 u32 t;
152
153 t = mem >> 16;
154 if (t == 0x1f80) {
155 if (mem < 0x1f801000)
156 return BFLIP32(psxHu32(mem));
157 else
158 return psxHwRead32(mem);
159 } else {
160 p = (char *)(psxMemLUT[t]);
161 if (p != NULL) {
162 return BFLIP32(*(u32 *)(p + (mem & 0xffff)));
163 } else {
164 return 0;
165 }
166 }
167 }
168
169 void psxMemWrite8(u32 mem, u8 value) {
170 char *p;
171 u32 t;
172
173 t = mem >> 16;
174 if (t == 0x1f80) {
175 if (mem < 0x1f801000)
176 psxHu8(mem) = value;
177 else
178 psxHwWrite8(mem, value);
179 } else {
180 p = (char *)(psxMemLUT[t]);
181 if (p != NULL) {
182 *(u8 *)(p + (mem & 0xffff)) = value;
183 }
184 }
185 }
186
187 void psxMemWrite16(u32 mem, u16 value) {
188 char *p;
189 u32 t;
190
191 t = mem >> 16;
192 if (t == 0x1f80) {
193 if (mem < 0x1f801000)
194 psxHu16(mem) = BFLIP16(value);
195 else
196 psxHwWrite16(mem, value);
197 } else {
198 p = (char *)(psxMemLUT[t]);
199 if (p != NULL) {
200 *(u16 *)(p + (mem & 0xffff)) = BFLIP16(value);
201 }
202 }
203 }
204
205 void psxMemWrite32(u32 mem, u32 value) {
206 char *p;
207 u32 t;
208
209 // if ((mem&0x1fffff) == 0x71E18 || value == 0x48088800) SysPrintf("t2fix!!\n");
210 t = mem >> 16;
211 if (t == 0x1f80) {
212 if (mem < 0x1f801000)
213 psxHu32(mem) = BFLIP32(value);
214 else
215 psxHwWrite32(mem, value);
216 } else {
217 p = (char *)(psxMemLUT[t]);
218 if (p != NULL) {
219 *(u32 *)(p + (mem & 0xffff)) = BFLIP32(value);
220 } else {
221 if (mem != 0xfffe0130) {
222
223 } else {
224 int i;
225
226 switch (value) {
227 case 0x800: case 0x804:
228 if (writeok == 0) break;
229 writeok = 0;
230 memset(psxMemLUT + 0x0000, 0, 0x80 * sizeof *psxMemLUT);
231 memset(psxMemLUT + 0x8000, 0, 0x80 * sizeof *psxMemLUT);
232 memset(psxMemLUT + 0xa000, 0, 0x80 * sizeof *psxMemLUT);
233 break;
234 case 0x1e988:
235 if (writeok == 1) break;
236 writeok = 1;
237 for (i=0; i<0x80; i++) psxMemLUT[i + 0x0000] = &psxM[(i & 0x1f) << 16];
238 memcpy(psxMemLUT + 0x8000, psxMemLUT, 0x80 * sizeof *psxMemLUT);
239 memcpy(psxMemLUT + 0xa000, psxMemLUT, 0x80 * sizeof *psxMemLUT);
240 break;
241 default:
242 break;
243 }
244 }
245 }
246 }
247 }
248