Debian Codegraph @

mednafen/wswan/wswan-memory.c

raw symbols SCIP
1 /* Cygne
2 *
3 * Copyright notice for this file:
4 * Copyright (C) 2002 Dox [email protected]
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20 #include <stdlib.h>
21 #include <math.h>
22
23 #include <time.h>
24
25 #include "wswan.h"
26 #include "gfx.h"
27 #include "interrupt.h"
28 #include "wswan-memory.h"
29 #include "sound.h"
30 #include "eeprom.h"
31 #include "rtc.h"
32 #include "v30mz.h"
33 #include "../mempatcher.h"
34 #include "../settings.h"
35 #include "../state_inline.h"
36
37 static bool SkipSL; // Skip save and load
38
39 uint32 wsRAMSize;
40 uint8 wsRAM[65536];
41 uint8 *wsSRAM = NULL;
42
43 uint8 *wsCartROM;
44 static uint32 sram_size;
45 uint32 eeprom_size;
46
47 static uint8 ButtonWhich, ButtonReadLatch;
48
49 static uint32 DMASource;
50 static uint16 DMADest;
51 static uint16 DMALength;
52 static uint8 DMAControl;
53
54 static uint32 SoundDMASource, SoundDMASourceSaved;
55 static uint32 SoundDMALength, SoundDMALengthSaved;
56 static uint8 SoundDMAControl;
57 static uint8 SoundDMATimer;
58
59 static uint8 BankSelector[4];
60
61 static uint8 CommControl, CommData;
62
63 static bool language;
64
65 extern uint16 WSButtonStatus;
66
67 void WSwan_writemem20(uint32 A, uint8 V)
68 {
69 uint32 offset = A & 0xffff;
70 uint32 bank = (A>>16) & 0xF;
71
72 if(!bank) /*RAM*/
73 {
74 WSwan_SoundCheckRAMWrite(offset);
75 wsRAM[offset] = V;
76
77 WSWan_TCacheInvalidByAddr(offset);
78
79 if(offset>=0xfe00) /*WSC palettes*/
80 WSwan_GfxWSCPaletteRAMWrite(offset, V);
81 }
82 else if(bank == 1) /* SRAM */
83 {
84 if(sram_size)
85 wsSRAM[(offset | (BankSelector[1] << 16)) & (sram_size - 1)] = V;
86 }
87 }
88
89 uint8 WSwan_readmem20(uint32 A)
90 {
91 uint8 bank_num;
92 uint32 offset = A & 0xFFFF;
93 uint32 bank = (A >> 16) & 0xF;
94
95 switch(bank)
96 {
97 case 0:
98 return wsRAM[offset];
99 case 1:
100 if(sram_size)
101 return wsSRAM[(offset | (BankSelector[1] << 16)) & (sram_size - 1)];
102 return(0);
103 case 2:
104 case 3:
105 return wsCartROM[offset+((BankSelector[bank]&((rom_size>>16)-1))<<16)];
106 default:
107 break;
108 }
109
110 bank_num = ((BankSelector[0] & 0xF) << 4) | (bank & 0xf);
111 bank_num &= (rom_size >> 16) - 1;
112
113 return(wsCartROM[(bank_num << 16) | offset]);
114 }
115
116 static void ws_CheckDMA(void)
117 {
118 if(DMAControl & 0x80)
119 {
120 while(DMALength)
121 {
122 WSwan_writemem20(DMADest, WSwan_readmem20(DMASource));
123 WSwan_writemem20(DMADest+1, WSwan_readmem20(DMASource+1));
124
125 if(DMAControl & 0x40)
126 {
127 DMASource -= 2;
128 DMADest -= 2;
129 }
130 else
131 {
132 DMASource += 2;
133 DMADest += 2;
134 }
135 DMASource &= 0x000FFFFE;
136 DMALength -= 2;
137 }
138 }
139 DMAControl &= ~0x80;
140 }
141
142 void WSwan_CheckSoundDMA(void)
143 {
144 if(!(SoundDMAControl & 0x80))
145 return;
146
147 if(!SoundDMATimer)
148 {
149 uint8 zebyte = WSwan_readmem20(SoundDMASource);
150
151 if(SoundDMAControl & 0x10)
152 WSwan_SoundWrite(0x95, zebyte); // Pick a port, any port?!
153 else
154 WSwan_SoundWrite(0x89, zebyte);
155
156 if(SoundDMAControl & 0x40)
157 SoundDMASource--;
158 else
159 SoundDMASource++;
160 SoundDMASource &= 0x000FFFFF;
161
162 SoundDMALength--;
163 SoundDMALength &= 0x000FFFFF;
164 if(!SoundDMALength)
165 {
166 if(SoundDMAControl & 8)
167 {
168 SoundDMALength = SoundDMALengthSaved;
169 SoundDMASource = SoundDMASourceSaved;
170 }
171 else
172 {
173 SoundDMAControl &= ~0x80;
174 }
175 }
176
177 switch(SoundDMAControl & 3)
178 {
179 case 0: SoundDMATimer = 5; break;
180 case 1: SoundDMATimer = 3; break;
181 case 2: SoundDMATimer = 1; break;
182 case 3: SoundDMATimer = 0; break;
183 }
184 }
185 else
186 {
187 SoundDMATimer--;
188 }
189 }
190
191 uint8 WSwan_readport(uint32 number)
192 {
193 number &= 0xFF;
194
195 if((number >= 0x80 && number <= 0x9F) || (number == 0x6A) || (number == 0x6B))
196 return(WSwan_SoundRead(number));
197 else if(number <= 0x3F || (number >= 0xA0 && number <= 0xAF) || (number == 0x60))
198 return(WSwan_GfxRead(number));
199 else if((number >= 0xBA && number <= 0xBE) || (number >= 0xC4 && number <= 0xC8))
200 return(WSwan_EEPROMRead(number));
201 else if(number >= 0xCA && number <= 0xCB)
202 return(WSwan_RTCRead(number));
203 else switch(number)
204 {
205 case 0x40: return(DMASource >> 0);
206 case 0x41: return(DMASource >> 8);
207 case 0x42: return(DMASource >> 16);
208
209 case 0x44: return(DMADest >> 0);
210 case 0x45: return(DMADest >> 8);
211
212 case 0x46: return(DMALength >> 0);
213 case 0x47: return(DMALength >> 8);
214
215 case 0x48: return(DMAControl);
216
217 case 0xB0:
218 case 0xB2:
219 case 0xB6: return(WSwan_InterruptRead(number));
220
221 case 0xC0: return(BankSelector[0] | 0x20);
222 case 0xC1: return(BankSelector[1]);
223 case 0xC2: return(BankSelector[2]);
224 case 0xC3: return(BankSelector[3]);
225
226 case 0x4a: return(SoundDMASource >> 0);
227 case 0x4b: return(SoundDMASource >> 8);
228 case 0x4c: return(SoundDMASource >> 16);
229 case 0x4e: return(SoundDMALength >> 0);
230 case 0x4f: return(SoundDMALength >> 8);
231 case 0x50: return(SoundDMALength >> 16);
232 case 0x52: return(SoundDMAControl);
233
234 case 0xB1: return(CommData);
235
236 case 0xb3:
237 {
238 uint8 ret = CommControl & 0xf0;
239
240 if(CommControl & 0x80)
241 ret |= 0x4; // Send complete
242
243 return(ret);
244 }
245 case 0xb5:
246 {
247 uint8 ret = (ButtonWhich << 4) | ButtonReadLatch;
248 return(ret);
249 }
250 }
251
252 if(number >= 0xC8)
253 return(0xD0 | language);
254
255 return(0);
256 }
257
258 void WSwan_writeport(uint32 IOPort, uint8 V)
259 {
260 IOPort &= 0xFF;
261
262 if((IOPort >= 0x80 && IOPort <= 0x9F) || (IOPort == 0x6A) || (IOPort == 0x6B))
263 WSwan_SoundWrite(IOPort, V);
264 else if((IOPort >= 0x00 && IOPort <= 0x3F) || (IOPort >= 0xA0 && IOPort <= 0xAF) || (IOPort == 0x60))
265 WSwan_GfxWrite(IOPort, V);
266 else if((IOPort >= 0xBA && IOPort <= 0xBE) || (IOPort >= 0xC4 && IOPort <= 0xC8))
267 WSwan_EEPROMWrite(IOPort, V);
268 else if(IOPort >= 0xCA && IOPort <= 0xCB)
269 WSwan_RTCWrite(IOPort, V);
270 else switch(IOPort)
271 {
272 case 0x40: DMASource &= 0xFFFF00; DMASource |= (V << 0) & ~1; break;
273 case 0x41: DMASource &= 0xFF00FF; DMASource |= (V << 8); break;
274 case 0x42: DMASource &= 0x00FFFF; DMASource |= ((V & 0x0F) << 16); break;
275
276 case 0x44: DMADest &= 0xFF00; DMADest |= (V << 0) & ~1; break;
277 case 0x45: DMADest &= 0x00FF; DMADest |= (V << 8); break;
278
279 case 0x46: DMALength &= 0xFF00; DMALength |= (V << 0) & ~1; break;
280 case 0x47: DMALength &= 0x00FF; DMALength |= (V << 8); break;
281
282 case 0x48: DMAControl = V & ~0x3F;
283 ws_CheckDMA();
284 break;
285
286 case 0x4a: SoundDMASource &= 0xFFFF00; SoundDMASource |= (V << 0); SoundDMASourceSaved = SoundDMASource; break;
287 case 0x4b: SoundDMASource &= 0xFF00FF; SoundDMASource |= (V << 8); SoundDMASourceSaved = SoundDMASource; break;
288 case 0x4c: SoundDMASource &= 0x00FFFF; SoundDMASource |= ((V & 0xF) << 16); SoundDMASourceSaved = SoundDMASource; break;
289 //case 0x4d: break; // Unused?
290 case 0x4e: SoundDMALength &= 0xFFFF00; SoundDMALength |= (V << 0); SoundDMALengthSaved = SoundDMALength; break;
291 case 0x4f: SoundDMALength &= 0xFF00FF; SoundDMALength |= (V << 8); SoundDMALengthSaved = SoundDMALength; break;
292 case 0x50: SoundDMALength &= 0x00FFFF; SoundDMALength |= ((V & 0xF) << 16); SoundDMALengthSaved = SoundDMALength; break;
293 //case 0x51: break; // Unused?
294 case 0x52: SoundDMAControl = V & ~0x20;
295 break;
296
297 case 0xB0:
298 case 0xB2:
299 case 0xB6: WSwan_InterruptWrite(IOPort, V); break;
300
301 case 0xB1: CommData = V; break;
302 case 0xB3: CommControl = V & 0xF0; break;
303
304 case 0xb5: ButtonWhich = V >> 4;
305 ButtonReadLatch = 0;
306
307 if(ButtonWhich & 0x4) /*buttons*/
308 ButtonReadLatch |= ((WSButtonStatus >> 8) << 1) & 0xF;
309
310 if(ButtonWhich & 0x2) /* H/X cursors */
311 ButtonReadLatch |= WSButtonStatus & 0xF;
312
313 if(ButtonWhich & 0x1) /* V/Y cursors */
314 ButtonReadLatch |= (WSButtonStatus >> 4) & 0xF;
315 break;
316
317 case 0xC0: BankSelector[0] = V & 0xF; break;
318 case 0xC1: BankSelector[1] = V; break;
319 case 0xC2: BankSelector[2] = V; break;
320 case 0xC3: BankSelector[3] = V; break;
321 }
322 }
323
324 void WSwan_MemoryKill(void)
325 {
326 if(wsSRAM)
327 free(wsSRAM);
328 wsSRAM = NULL;
329 }
330
331 void WSwan_MemoryInit(bool lang, bool IsWSC, uint32 ssize, bool SkipSaveLoad)
332 {
333 const uint16 byear = MDFN_GetSettingUI("wswan.byear");
334 const uint8 bmonth = MDFN_GetSettingUI("wswan.bmonth");
335 const uint8 bday = MDFN_GetSettingUI("wswan.bday");
336 const uint8 sex = MDFN_GetSettingI("wswan.sex");
337 const uint8 blood = MDFN_GetSettingI("wswan.blood");
338
339 language = lang;
340 SkipSL = SkipSaveLoad;
341
342 wsRAMSize = 65536;
343 sram_size = ssize;
344
345 // WSwan_EEPROMInit() will also clear wsEEPROM
346 WSwan_EEPROMInit(MDFN_GetSettingS("wswan.name"), byear, bmonth, bday, sex, blood);
347
348 if(sram_size)
349 {
350 wsSRAM = (uint8*)malloc(sram_size);
351 memset(wsSRAM, 0, sram_size);
352 }
353
354 MDFNMP_AddRAM(wsRAMSize, 0x00000, wsRAM);
355
356 if(sram_size)
357 MDFNMP_AddRAM(sram_size, 0x10000, wsSRAM);
358 }
359
360 void WSwan_MemoryReset(void)
361 {
362 memset(wsRAM, 0, 65536);
363
364 wsRAM[0x75AC] = 0x41;
365 wsRAM[0x75AD] = 0x5F;
366 wsRAM[0x75AE] = 0x43;
367 wsRAM[0x75AF] = 0x31;
368 wsRAM[0x75B0] = 0x6E;
369 wsRAM[0x75B1] = 0x5F;
370 wsRAM[0x75B2] = 0x63;
371 wsRAM[0x75B3] = 0x31;
372
373 memset(BankSelector, 0, sizeof(BankSelector));
374 ButtonWhich = 0;
375 ButtonReadLatch = 0;
376 DMASource = 0;
377 DMADest = 0;
378 DMALength = 0;
379 DMAControl = 0;
380
381 SoundDMASource = SoundDMASourceSaved = 0;
382 SoundDMALength = SoundDMALengthSaved = 0;
383 SoundDMAControl = 0;
384 SoundDMATimer = 0;
385
386 CommControl = 0;
387 CommData = 0;
388 }
389
390 int WSwan_MemoryStateAction(StateMem *sm, int load, int data_only)
391 {
392 SFORMAT StateRegs[] =
393 {
394 SFARRAYN(wsRAM, 65536, "RAM"),
395 SFARRAYN(sram_size ? wsSRAM : NULL, sram_size, "SRAM"),
396 SFVAR(ButtonWhich),
397 SFVAR(ButtonReadLatch),
398 SFVAR(WSButtonStatus),
399 SFVAR(DMASource),
400 SFVAR(DMADest),
401 SFVAR(DMALength),
402 SFVAR(DMAControl),
403
404 SFVAR(SoundDMASource),
405 SFVAR(SoundDMASourceSaved),
406 SFVAR(SoundDMALength),
407 SFVAR(SoundDMALengthSaved),
408 SFVAR(SoundDMAControl),
409 SFVAR(SoundDMATimer),
410
411 SFVAR(CommControl),
412 SFVAR(CommData),
413
414 SFARRAY(BankSelector, 4),
415
416 SFEND
417 };
418
419 if(!MDFNSS_StateAction(sm, load, data_only, StateRegs, "MEMR", false))
420 return(0);
421
422 if(load)
423 {
424 uint32 A;
425 for(A = 0xfe00; A <= 0xFFFF; A++)
426 WSwan_GfxWSCPaletteRAMWrite(A, wsRAM[A]);
427 }
428 return(1);
429 }