forked from kode54/libupse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upse_ps1_memory_manager.c
326 lines (290 loc) · 7.52 KB
/
upse_ps1_memory_manager.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* UPSE: the unix playstation sound emulator.
*
* Filename: upse_ps1_memory_manager.c
* Purpose: libupse: UPSE PS1 Memory Manager
*
* Copyright (c) 2007 William Pitcock <[email protected]>
* Portions copyright (c) 1999-2002 Pcsx Team
* Portions copyright (c) 2004 "Xodnizel"
*
* UPSE is free software, released under the GNU General Public License,
* version 2.
*
* A copy of the GNU General Public License, version 2, is included in
* the UPSE source kit as COPYING.
*
* UPSE is offered without any warranty of any kind, explicit or implicit.
*/
#include <string.h>
#include <stdlib.h>
#include "upse-internal.h"
void upse_ps1_memory_load(upse_module_instance_t *ins, u32 address, s32 length, unsigned char *data)
{
/* track lowest and highest address size for debugging functions. */
if (!ins->lowest_addr || address < ins->lowest_addr)
ins->lowest_addr = address;
if (!ins->highest_addr || address > ins->highest_addr)
{
ins->highest_addr = address;
ins->highest_addr_size = length;
}
while (length > 0)
{
if (address & 65535)
{
u32 tmplen;
tmplen = ((65536 - (address & 65535)) > (u32) length) ? (u32) length : 65536 - (address & 65535);
if (ins->upse_ps1_memory_LUT[address >> 16])
memcpy((char *) (ins->upse_ps1_memory_LUT[address >> 16] + (address & 65535)), data, tmplen);
address += tmplen;
data += tmplen;
length -= tmplen;
continue;
}
if (ins->upse_ps1_memory_LUT[address >> 16])
{
memcpy((char *) (ins->upse_ps1_memory_LUT[address >> 16]), data, (length < 65536) ? length : 65536);
}
data += 65536;
address += 65536;
length -= 65536;
}
}
void upse_ps1_memory_clear(upse_module_instance_t *ins, u32 address, s32 length)
{
while (length > 0)
{
if (address & 65535)
{
u32 tmplen;
tmplen = ((65536 - (address & 65535)) > (u32) length) ? (u32) length : 65536 - (address & 65535);
if (ins->upse_ps1_memory_LUT[address >> 16])
memset((char *) (ins->upse_ps1_memory_LUT[address >> 16] + (address & 65535)), '\0', tmplen);
address += tmplen;
length -= tmplen;
continue;
}
if (ins->upse_ps1_memory_LUT[address >> 16])
{
memset((char *) (ins->upse_ps1_memory_LUT[address >> 16]), '\0', (length < 65536) ? length : 65536);
}
address += 65536;
length -= 65536;
}
}
int upse_ps1_memory_init(upse_module_instance_t *ins)
{
int i;
ins->writeok = 1;
memset(ins->upse_ps1_memory_LUT, 0, 0x10000 * sizeof *ins->upse_ps1_memory_LUT);
if (ins->upse_ps1_memory_LUT == NULL)
{
printf("Error allocating memory");
return -1;
}
for (i = 0; i < 0x80; i++)
ins->upse_ps1_memory_LUT[i + 0x0000] = &ins->psxM[(i & 0x1f) << 16];
memcpy(ins->upse_ps1_memory_LUT + 0x8000, ins->upse_ps1_memory_LUT, 0x80 * sizeof *ins->upse_ps1_memory_LUT);
memcpy(ins->upse_ps1_memory_LUT + 0xa000, ins->upse_ps1_memory_LUT, 0x80 * sizeof *ins->upse_ps1_memory_LUT);
for (i = 0; i < 0x01; i++)
ins->upse_ps1_memory_LUT[i + 0x1f00] = &ins->psxP[i << 16];
for (i = 0; i < 0x01; i++)
ins->upse_ps1_memory_LUT[i + 0x1f80] = &ins->psxH[i << 16];
for (i = 0; i < 0x08; i++)
ins->upse_ps1_memory_LUT[i + 0xbfc0] = &ins->psxR[i << 16];
return 0;
}
void upse_ps1_memory_reset(upse_module_instance_t *ins)
{
memset(ins->psxM, 0, 0x00200000);
memset(ins->psxP, 0, 0x00010000);
if (upse_has_custom_bios())
{
FILE *f;
_DEBUG("custom bios: %s", upse_get_custom_bios());
f = fopen(upse_get_custom_bios(), "rb");
if (f == NULL) {
_DEBUG("opening custom bios failed");
memset(ins->psxR, 0, 0x80000);
upse_set_custom_bios(NULL);
} else {
fread(ins->psxR, 1, 0x80000, f);
fclose(f);
}
}
}
void upse_ps1_memory_shutdown(upse_module_instance_t *ins)
{
}
u8 upse_ps1_memory_read_8(upse_module_instance_t *ins, u32 mem)
{
char *p;
u32 t;
t = mem >> 16;
if (t == 0x1f80)
{
if (mem < 0x1f801000)
return psxHu8(ins, mem);
else
return upse_ps1_hal_read_8(ins, mem);
}
else
{
p = (char *) (ins->upse_ps1_memory_LUT[t]);
if (p != NULL)
{
return *(u8 *) (p + (mem & 0xffff));
}
else
{
return 0;
}
}
}
u16 upse_ps1_memory_read_16(upse_module_instance_t *ins, u32 mem)
{
char *p;
u32 t;
t = mem >> 16;
if (t == 0x1f80)
{
if (mem < 0x1f801000)
return BFLIP16(psxHu16(ins, mem));
else
return upse_ps1_hal_read_16(ins, mem);
}
else
{
p = (char *) (ins->upse_ps1_memory_LUT[t]);
if (p != NULL)
{
return BFLIP16(*(u16 *) (p + (mem & 0xffff)));
}
else
{
return 0;
}
}
}
u32 upse_ps1_memory_read_32(upse_module_instance_t *ins, u32 mem)
{
char *p;
u32 t;
t = mem >> 16;
if (t == 0x1f80)
{
if (mem < 0x1f801000)
return BFLIP32(psxHu32(ins, mem));
else
return upse_ps1_hal_read_32(ins, mem);
}
else
{
p = (char *) (ins->upse_ps1_memory_LUT[t]);
if (p != NULL)
{
return BFLIP32(*(u32 *) (p + (mem & 0xffff)));
}
else
{
return 0;
}
}
}
void upse_ps1_memory_write_8(upse_module_instance_t *ins, u32 mem, u8 value)
{
char *p;
u32 t;
t = mem >> 16;
if (t == 0x1f80)
{
if (mem < 0x1f801000)
psxHu8(ins, mem) = value;
else
upse_ps1_hal_write_8(ins, mem, value);
}
else
{
p = (char *) (ins->upse_ps1_memory_LUT[t]);
if (p != NULL)
{
*(u8 *) (p + (mem & 0xffff)) = value;
}
}
}
void upse_ps1_memory_write_16(upse_module_instance_t *ins, u32 mem, u16 value)
{
char *p;
u32 t;
t = mem >> 16;
if (t == 0x1f80)
{
if (mem < 0x1f801000)
psxHu16(ins, mem) = BFLIP16(value);
else
upse_ps1_hal_write_16(ins, mem, value);
}
else
{
p = (char *) (ins->upse_ps1_memory_LUT[t]);
if (p != NULL)
{
*(u16 *) (p + (mem & 0xffff)) = BFLIP16(value);
}
}
}
void upse_ps1_memory_write_32(upse_module_instance_t *ins, u32 mem, u32 value)
{
char *p;
u32 t;
t = mem >> 16;
if (t == 0x1f80)
{
if (mem < 0x1f801000)
psxHu32(ins, mem) = BFLIP32(value);
else
upse_ps1_hal_write_32(ins, mem, value);
}
else
{
p = (char *) (ins->upse_ps1_memory_LUT[t]);
if (p != NULL)
{
*(u32 *) (p + (mem & 0xffff)) = BFLIP32(value);
}
else
{
if (mem != 0xfffe0130)
{
}
else
{
int i;
switch (value)
{
case 0x800:
case 0x804:
if (ins->writeok == 0)
break;
ins->writeok = 0;
memset(ins->upse_ps1_memory_LUT + 0x0000, 0, 0x80 * sizeof *ins->upse_ps1_memory_LUT);
memset(ins->upse_ps1_memory_LUT + 0x8000, 0, 0x80 * sizeof *ins->upse_ps1_memory_LUT);
memset(ins->upse_ps1_memory_LUT + 0xa000, 0, 0x80 * sizeof *ins->upse_ps1_memory_LUT);
break;
case 0x1e988:
if (ins->writeok == 1)
break;
ins->writeok = 1;
for (i = 0; i < 0x80; i++)
ins->upse_ps1_memory_LUT[i + 0x0000] = &ins->psxM[(i & 0x1f) << 16];
memcpy(ins->upse_ps1_memory_LUT + 0x8000, ins->upse_ps1_memory_LUT, 0x80 * sizeof *ins->upse_ps1_memory_LUT);
memcpy(ins->upse_ps1_memory_LUT + 0xa000, ins->upse_ps1_memory_LUT, 0x80 * sizeof *ins->upse_ps1_memory_LUT);
break;
default:
break;
}
}
}
}
}