c62d2810 |
1 | /* FCE Ultra - NES/Famicom Emulator |
2 | * |
3 | * Copyright notice for this file: |
15300263 |
4 | * Copyright (C) 1998 BERO |
c62d2810 |
5 | * Copyright (C) 2002 Ben Parnell |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by |
9 | * the Free Software Foundation; either version 2 of the License, or |
10 | * (at your option) any later version. |
11 | * |
12 | * This program is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * GNU General Public License for more details. |
16 | * |
17 | * You should have received a copy of the GNU General Public License |
18 | * along with this program; if not, write to the Free Software |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 | */ |
21 | |
22 | #include <string.h> |
23 | #include <stdio.h> |
24 | #include <stdlib.h> |
25 | |
26 | #include "types.h" |
27 | #include "x6502.h" |
28 | #include "fce.h" |
29 | #include "sound.h" |
30 | #include "svga.h" |
31 | #include "netplay.h" |
32 | #include "general.h" |
33 | #include "endian.h" |
34 | #include "version.h" |
35 | #include "memory.h" |
36 | |
37 | #include "cart.h" |
38 | #include "nsf.h" |
39 | #include "fds.h" |
40 | #include "ines.h" |
41 | #include "unif.h" |
42 | #include "cheat.h" |
43 | |
44 | #include "state.h" |
45 | #include "video.h" |
46 | #include "input.h" |
47 | #include "file.h" |
48 | #include "crc32.h" |
5232c20c |
49 | #include "ppu.h" |
c62d2810 |
50 | |
92764e62 |
51 | #include "palette.h" |
5a2aa426 |
52 | #include "movie.h" |
53 | |
4fdfab07 |
54 | #include "dprintf.h" |
55 | |
6587f346 |
56 | #ifdef GP2X |
57 | #include "drivers/gp2x/asmutils.h" |
58 | #endif |
59 | |
c62d2810 |
60 | #define Pal (PALRAM) |
61 | |
5232c20c |
62 | |
4a1bf31b |
63 | static void (*RefreshLine)(uint8 *P, uint32 vofs) = NULL; |
c62d2810 |
64 | static void PRefreshLine(void); |
5232c20c |
65 | |
c62d2810 |
66 | static void ResetPPU(void); |
67 | static void PowerPPU(void); |
68 | |
69 | uint64 timestampbase=0; |
70 | |
4fdfab07 |
71 | static int ppudead=1; |
72 | static int kook=0; |
73 | |
c62d2810 |
74 | int MMC5Hack; |
75 | uint32 MMC5HackVROMMask; |
76 | uint8 *MMC5HackExNTARAMPtr; |
77 | uint8 *MMC5HackVROMPTR; |
15300263 |
78 | uint8 MMC5HackCHRMode=0; |
c62d2810 |
79 | uint8 MMC5HackSPMode; |
80 | uint8 MMC5HackSPScroll; |
81 | uint8 MMC5HackSPPage; |
82 | |
83 | uint8 *MMC5SPRVPage[8]; |
84 | uint8 *MMC5BGVPage[8]; |
85 | |
86 | |
87 | uint8 VRAMBuffer,PPUGenLatch; |
88 | |
89 | uint8 *vnapage[4]; |
90 | uint8 PPUNTARAM; |
91 | uint8 PPUCHRRAM; |
92 | |
93 | /* Color deemphasis emulation. Joy... */ |
94 | static uint8 deemp=0; |
95 | static int deempcnt[8]; |
96 | |
c62d2810 |
97 | FCEUGI FCEUGameInfo; |
c4980f9e |
98 | void (*GameInterface)(int h, void *param); |
c62d2810 |
99 | |
100 | void FP_FASTAPASS(1) (*PPU_hook)(uint32 A); |
101 | |
102 | void (*GameStateRestore)(int version); |
d97315ac |
103 | void (*GameHBIRQHook)(void), (*GameHBIRQHook2)(void); |
c62d2810 |
104 | |
105 | readfunc ARead[0x10000]; |
106 | writefunc BWrite[0x10000]; |
107 | static readfunc *AReadG; |
108 | static writefunc *BWriteG; |
109 | static int RWWrap=0; |
110 | |
92e249b1 |
111 | #ifdef ASM_6502 |
112 | static void asmcpu_update(int32 cycles) |
113 | { |
92e249b1 |
114 | // some code from x6502.c |
115 | fhcnt-=cycles; |
116 | if(fhcnt<=0) |
117 | { |
118 | FrameSoundUpdate(); |
119 | fhcnt+=fhinc; |
120 | } |
121 | |
122 | if(PCMIRQCount>0) |
123 | { |
124 | PCMIRQCount-=cycles; |
125 | if(PCMIRQCount<=0) |
126 | { |
127 | vdis=1; |
128 | if((PSG[0x10]&0x80) && !(PSG[0x10]&0x40)) |
129 | { |
130 | extern uint8 SIRQStat; |
131 | SIRQStat|=0x80; |
132 | X6502_IRQBegin(FCEU_IQDPCM); |
133 | } |
134 | } |
135 | } |
136 | } |
137 | |
138 | void asmcpu_unpack(void) |
139 | { |
140 | nes_registers[0] = X.A << 24; |
141 | nes_registers[1] = X.X; |
142 | nes_registers[2] = X.Y; |
143 | pc_base = 0; |
144 | nes_registers[3] = X.PC; |
c0bf6f9f |
145 | X6502_Rebase_a(); |
92e249b1 |
146 | nes_registers[4] = X.S << 24; |
147 | nes_registers[4]|= X.IRQlow << 8; |
0bb3fe12 |
148 | if (MapIRQHook) |
149 | nes_registers[4] |= 1<<16; // MapIRQHook set bit |
8fa5eb33 |
150 | nes_registers[7] = (uint32)X.count << 16; |
92e249b1 |
151 | |
152 | // NVUB DIZC |
153 | nes_registers[4]|= X.P & 0x5d; |
154 | nes_registers[5] = X.P << 24; // N |
155 | if (!(X.P&0x02)) nes_registers[5] |= 1; // Z |
156 | } |
157 | |
158 | void asmcpu_pack(void) |
159 | { |
160 | X.A = nes_registers[0] >> 24; |
161 | X.X = nes_registers[1]; |
162 | X.Y = nes_registers[2]; |
163 | X.PC= nes_registers[3] - pc_base; |
164 | X.S = nes_registers[4] >> 24; |
165 | X.IRQlow = nes_registers[4] >> 8; |
8fa5eb33 |
166 | X.count = (int32) nes_registers[7] >> 16; |
92e249b1 |
167 | |
168 | // NVUB DIZC |
169 | X.P = nes_registers[4] & 0x5d; |
170 | if ( nes_registers[5]&0x80000000) X.P |= 0x80; // N |
171 | if (!(nes_registers[5]&0x000000ff)) X.P |= 0x02; // Z |
172 | } |
173 | #endif |
174 | |
c62d2810 |
175 | DECLFW(BNull) |
176 | { |
177 | |
178 | } |
179 | |
180 | DECLFR(ANull) |
181 | { |
182 | return(X.DB); |
183 | } |
184 | |
185 | int AllocGenieRW(void) |
186 | { |
187 | if(!(AReadG=FCEU_malloc(0x8000*sizeof(readfunc)))) |
188 | return 0; |
189 | if(!(BWriteG=FCEU_malloc(0x8000*sizeof(writefunc)))) |
190 | return 0; |
191 | RWWrap=1; |
192 | return 1; |
193 | } |
194 | |
195 | void FlushGenieRW(void) |
196 | { |
197 | int32 x; |
198 | |
199 | if(RWWrap) |
200 | { |
201 | for(x=0;x<0x8000;x++) |
202 | { |
203 | ARead[x+0x8000]=AReadG[x]; |
204 | BWrite[x+0x8000]=BWriteG[x]; |
205 | } |
b8da43fb |
206 | #ifdef ASM_6502 |
207 | GenieSetPages(1); |
208 | #endif |
c62d2810 |
209 | free(AReadG); |
210 | free(BWriteG); |
211 | AReadG=0; |
212 | BWriteG=0; |
213 | RWWrap=0; |
214 | } |
215 | } |
216 | |
217 | readfunc FASTAPASS(1) GetReadHandler(int32 a) |
218 | { |
219 | if(a>=0x8000 && RWWrap) |
220 | return AReadG[a-0x8000]; |
221 | else |
222 | return ARead[a]; |
223 | } |
224 | |
225 | void FASTAPASS(3) SetReadHandler(int32 start, int32 end, readfunc func) |
226 | { |
227 | int32 x; |
228 | |
229 | if(!func) |
230 | func=ANull; |
231 | |
232 | if(RWWrap) |
233 | for(x=end;x>=start;x--) |
234 | { |
235 | if(x>=0x8000) |
236 | AReadG[x-0x8000]=func; |
237 | else |
238 | ARead[x]=func; |
239 | } |
240 | else |
241 | |
242 | for(x=end;x>=start;x--) |
243 | ARead[x]=func; |
244 | } |
245 | |
246 | writefunc FASTAPASS(1) GetWriteHandler(int32 a) |
247 | { |
248 | if(RWWrap && a>=0x8000) |
249 | return BWriteG[a-0x8000]; |
250 | else |
251 | return BWrite[a]; |
252 | } |
253 | |
254 | void FASTAPASS(3) SetWriteHandler(int32 start, int32 end, writefunc func) |
255 | { |
256 | int32 x; |
257 | |
258 | if(!func) |
259 | func=BNull; |
260 | |
261 | if(RWWrap) |
262 | for(x=end;x>=start;x--) |
263 | { |
264 | if(x>=0x8000) |
265 | BWriteG[x-0x8000]=func; |
266 | else |
267 | BWrite[x]=func; |
268 | } |
269 | else |
270 | for(x=end;x>=start;x--) |
271 | BWrite[x]=func; |
272 | } |
273 | |
274 | uint8 vtoggle=0; |
275 | uint8 XOffset=0; |
276 | |
277 | uint32 TempAddr,RefreshAddr; |
278 | |
c62d2810 |
279 | |
280 | /* scanline is equal to the current visible scanline we're on. */ |
281 | |
282 | int scanline; |
c62d2810 |
283 | |
9115e7d2 |
284 | uint8 GameMemBlock[131072] __attribute__ ((aligned (4))); |
285 | uint8 NTARAM[0x800] __attribute__ ((aligned (4))); |
286 | uint8 PALRAM[0x20] __attribute__ ((aligned (4))); |
92e249b1 |
287 | #if !defined(ASM_6502) || defined(DEBUG_ASM_6502) |
9115e7d2 |
288 | uint8 RAM[0x800] __attribute__ ((aligned (4))); |
af32b6c2 |
289 | #endif |
9115e7d2 |
290 | |
c62d2810 |
291 | uint8 PPU[4]; |
292 | uint8 PPUSPL; |
293 | |
c62d2810 |
294 | uint8 PAL=0; |
295 | |
5232c20c |
296 | |
c62d2810 |
297 | #define MMC5BGVRAMADR(V) &MMC5BGVPage[(V)>>10][(V)] |
298 | #define VRAMADR(V) &VPage[(V)>>10][(V)] |
15300263 |
299 | |
4fdfab07 |
300 | static int linestartts; |
301 | static int tofix=0; |
302 | |
e328100e |
303 | static uint8 *Plinef; |
304 | |
305 | extern uint8 sprlinebuf[256+8]; |
306 | extern int32 sphitx; |
307 | extern uint8 sphitdata; |
308 | |
309 | extern int spork; /* spork the world. Any sprites on this line? |
310 | Then this will be set to 1. Needed for zapper |
311 | emulation and *gasp* sprite emulation. |
312 | */ |
313 | |
314 | static void ResetRL(uint8 *target) |
4fdfab07 |
315 | { |
e328100e |
316 | if(InputScanlineHook) |
317 | InputScanlineHook(0,0,0,0); |
318 | Plinef=target; |
4fdfab07 |
319 | linestartts=timestamp*48+X6502_GetCycleCount(); |
320 | tofix=1; |
321 | } |
322 | |
323 | static INLINE void Fixit1(void); |
324 | |
e328100e |
325 | /* faking FCEUPPU_LineUpdate() from later versions of the emu */ |
326 | static void FakedLineUpdate(void) |
4fdfab07 |
327 | { |
328 | #define TOFIXNUM (272-0x4) |
329 | int lastpixel; |
330 | |
e328100e |
331 | if (scanline >= 240) return; |
332 | |
333 | if (tofix || sphitx != 0x100) |
4fdfab07 |
334 | { |
335 | lastpixel = (timestamp*48-linestartts)>>4; |
336 | if (PAL) lastpixel += lastpixel>>4; |
4fdfab07 |
337 | //printf("lastpixel: %i\n", lastpixel); |
e328100e |
338 | } |
339 | |
340 | if (tofix && lastpixel>=TOFIXNUM) |
341 | { |
342 | Fixit1(); |
343 | tofix=0; |
344 | } |
4fdfab07 |
345 | |
e328100e |
346 | // CheckSpriteHit() |
347 | if(sphitx!=0x100) |
348 | { |
349 | int l=lastpixel-16; |
350 | int x; |
351 | |
352 | for(x=sphitx;x<(sphitx+8) && x<l;x++) |
4fdfab07 |
353 | { |
e328100e |
354 | if((sphitdata&(0x80>>(x-sphitx))) && !(Plinef[x]&64)) |
355 | { |
356 | PPU_status|=0x40; |
357 | sphitx=0x100; |
358 | break; |
359 | } |
4fdfab07 |
360 | } |
361 | } |
362 | } |
363 | |
364 | |
c62d2810 |
365 | static DECLFW(BRAML) |
15300263 |
366 | { |
c62d2810 |
367 | RAM[A]=V; |
368 | } |
369 | |
370 | static DECLFW(BRAMH) |
371 | { |
372 | RAM[A&0x7FF]=V; |
373 | } |
374 | |
375 | static DECLFR(ARAML) |
376 | { |
377 | return RAM[A]; |
378 | } |
379 | |
380 | static DECLFR(ARAMH) |
381 | { |
382 | return RAM[A&0x7FF]; |
383 | } |
384 | |
15300263 |
385 | |
c62d2810 |
386 | static DECLFR(A2002) |
387 | { |
4fdfab07 |
388 | /* merged */ |
c62d2810 |
389 | uint8 ret; |
4fdfab07 |
390 | |
e328100e |
391 | FakedLineUpdate(); |
c62d2810 |
392 | ret = PPU_status; |
4fdfab07 |
393 | ret|=PPUGenLatch&0x1F; |
c62d2810 |
394 | vtoggle=0; |
395 | PPU_status&=0x7F; |
4fdfab07 |
396 | PPUGenLatch=ret; |
ea80a45b |
397 | //dprintf("r [2002] %02x",ret); |
4fdfab07 |
398 | return ret; |
c62d2810 |
399 | } |
400 | |
401 | static DECLFR(A200x) |
402 | { |
4fdfab07 |
403 | /* merged */ |
e328100e |
404 | FakedLineUpdate(); |
c62d2810 |
405 | return PPUGenLatch; |
406 | } |
407 | |
408 | static DECLFR(A2007) |
409 | { |
4fdfab07 |
410 | /* merged */ |
c62d2810 |
411 | uint8 ret; |
412 | uint32 tmp=RefreshAddr&0x3FFF; |
413 | |
e328100e |
414 | FakedLineUpdate(); |
4fdfab07 |
415 | |
416 | ret=VRAMBuffer; |
417 | |
c62d2810 |
418 | if(PPU_hook) PPU_hook(tmp); |
4fdfab07 |
419 | PPUGenLatch=VRAMBuffer; |
15300263 |
420 | if(tmp<0x2000) |
c62d2810 |
421 | { |
422 | VRAMBuffer=VPage[tmp>>10][tmp]; |
423 | } |
15300263 |
424 | else |
c62d2810 |
425 | { |
426 | VRAMBuffer=vnapage[(tmp>>10)&0x3][tmp&0x3FF]; |
427 | } |
428 | |
429 | if (INC32) RefreshAddr+=32; |
430 | else RefreshAddr++; |
431 | if(PPU_hook) PPU_hook(RefreshAddr&0x3fff); |
4fdfab07 |
432 | dprintf("r [2007] %02x",ret); |
c62d2810 |
433 | return ret; |
434 | } |
435 | |
436 | static DECLFW(B2000) |
437 | { |
4fdfab07 |
438 | /* NMI2? */ |
e328100e |
439 | FakedLineUpdate(); |
c62d2810 |
440 | PPUGenLatch=V; |
441 | PPU[0]=V; |
442 | TempAddr&=0xF3FF; |
443 | TempAddr|=(V&3)<<10; |
444 | } |
445 | |
446 | static DECLFW(B2001) |
447 | { |
4fdfab07 |
448 | /* merged */ |
e328100e |
449 | FakedLineUpdate(); |
c62d2810 |
450 | PPUGenLatch=V; |
451 | PPU[1]=V; |
452 | if(V&0xE0) |
453 | deemp=V>>5; |
454 | //printf("$%04x:$%02x, %d\n",X.PC,V,scanline); |
455 | } |
456 | |
457 | static DECLFW(B2002) |
458 | { |
4fdfab07 |
459 | /* merged */ |
c62d2810 |
460 | PPUGenLatch=V; |
461 | } |
462 | |
463 | static DECLFW(B2003) |
464 | { |
4fdfab07 |
465 | /* merged */ |
c62d2810 |
466 | PPUGenLatch=V; |
467 | PPU[3]=V; |
468 | PPUSPL=V&0x7; |
469 | } |
470 | |
471 | static DECLFW(B2004) |
472 | { |
4fdfab07 |
473 | /* merged */ |
c62d2810 |
474 | PPUGenLatch=V; |
4fdfab07 |
475 | if(PPUSPL>=8) |
c62d2810 |
476 | { |
477 | if(PPU[3]>=8) |
478 | SPRAM[PPU[3]]=V; |
479 | } |
480 | else |
481 | { |
482 | //printf("$%02x:$%02x\n",PPUSPL,V); |
483 | SPRAM[PPUSPL]=V; |
c62d2810 |
484 | } |
485 | PPU[3]++; |
4fdfab07 |
486 | PPUSPL++; |
5232c20c |
487 | |
c62d2810 |
488 | } |
489 | |
490 | static DECLFW(B2005) |
491 | { |
4fdfab07 |
492 | /* merged */ |
c62d2810 |
493 | uint32 tmp=TempAddr; |
e328100e |
494 | FakedLineUpdate(); |
c62d2810 |
495 | PPUGenLatch=V; |
496 | if (!vtoggle) |
497 | { |
498 | tmp&=0xFFE0; |
499 | tmp|=V>>3; |
500 | XOffset=V&7; |
501 | } |
502 | else |
503 | { |
504 | tmp&=0x8C1F; |
505 | tmp|=((V&~0x7)<<2); |
506 | tmp|=(V&7)<<12; |
507 | } |
508 | |
509 | TempAddr=tmp; |
510 | vtoggle^=1; |
511 | } |
512 | |
513 | static DECLFW(B2006) |
514 | { |
4fdfab07 |
515 | /* merged */ |
e328100e |
516 | FakedLineUpdate(); |
4fdfab07 |
517 | |
c62d2810 |
518 | PPUGenLatch=V; |
519 | if(!vtoggle) |
520 | { |
521 | TempAddr&=0x00FF; |
522 | TempAddr|=(V&0x3f)<<8; |
523 | } |
524 | else |
525 | { |
526 | TempAddr&=0xFF00; |
527 | TempAddr|=V; |
c62d2810 |
528 | |
4fdfab07 |
529 | RefreshAddr=TempAddr; |
c62d2810 |
530 | if(PPU_hook) |
531 | PPU_hook(RefreshAddr); |
532 | } |
533 | vtoggle^=1; |
534 | } |
535 | |
536 | static DECLFW(B2007) |
15300263 |
537 | { |
4fdfab07 |
538 | /* merged */ |
c62d2810 |
539 | uint32 tmp=RefreshAddr&0x3FFF; |
c62d2810 |
540 | PPUGenLatch=V; |
541 | if(tmp>=0x3F00) |
542 | { |
4fdfab07 |
543 | // hmmm.... |
544 | if(!(tmp&0xf)) |
545 | PALRAM[0x00]=PALRAM[0x04]=PALRAM[0x08]=PALRAM[0x0C]=V&0x3f; |
546 | else if(tmp&3) PALRAM[(tmp&0x1f)]=V&0x3f; |
c62d2810 |
547 | } |
548 | else if(tmp<0x2000) |
549 | { |
550 | if(PPUCHRRAM&(1<<(tmp>>10))) |
551 | VPage[tmp>>10][tmp]=V; |
552 | } |
553 | else |
15300263 |
554 | { |
c62d2810 |
555 | if(PPUNTARAM&(1<<((tmp&0xF00)>>10))) |
556 | vnapage[((tmp&0xF00)>>10)][tmp&0x3FF]=V; |
557 | } |
558 | if (INC32) RefreshAddr+=32; |
559 | else RefreshAddr++; |
560 | if(PPU_hook) PPU_hook(RefreshAddr&0x3fff); |
561 | } |
562 | |
563 | static DECLFW(B4014) |
15300263 |
564 | { |
c62d2810 |
565 | uint32 t=V<<8; |
566 | int x; |
4fdfab07 |
567 | |
c62d2810 |
568 | for(x=0;x<256;x++) |
569 | B2004(0x2004,X.DB=ARead[t+x](t+x)); |
570 | X6502_AddCycles(512); |
571 | } |
572 | |
5232c20c |
573 | void BGRender(uint8 *target) |
c62d2810 |
574 | { |
4a1bf31b |
575 | uint32 tem, vofs; |
576 | vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7); |
577 | |
578 | Pal[0]|=64; |
579 | Pal[4]|=64; |
580 | Pal[8]|=64; |
581 | Pal[0xC]|=64; |
582 | RefreshLine(target-XOffset, vofs); |
583 | Pal[0]&=63; |
584 | Pal[4]&=63; |
585 | Pal[8]&=63; |
586 | Pal[0xC]&=63; |
587 | |
c62d2810 |
588 | if(!(PPU[1]&2)) |
589 | { |
9115e7d2 |
590 | tem=Pal[0]|0x40; |
591 | tem|=tem<<8; |
592 | tem|=tem<<16; |
c62d2810 |
593 | *(uint32 *)target=*(uint32 *)(target+4)=tem; |
594 | } |
595 | } |
596 | |
597 | #ifdef FRAMESKIP |
5232c20c |
598 | int FSkip=0; |
c62d2810 |
599 | void FCEUI_FrameSkip(int x) |
600 | { |
601 | FSkip=x; |
602 | } |
603 | #endif |
604 | |
605 | /* This is called at the beginning of each visible scanline */ |
e328100e |
606 | static void LineUpdate(uint8 *target) |
c62d2810 |
607 | { |
608 | uint32 tem; |
c0623dcf |
609 | int y; |
c62d2810 |
610 | |
078eb4c0 |
611 | /* PRefreshLine() will not get called on skipped frames. This |
612 | * could cause a problem, but the solution would be rather complex, |
613 | * due to the current sprite 0 hit code. |
614 | */ |
c0623dcf |
615 | if(FSkip) |
616 | { |
617 | y=(int)SPRAM[0] + 1; |
618 | if(scanline==y && SpriteON) PPU_status|=0x40; // hack |
619 | return; |
620 | } |
078eb4c0 |
621 | |
622 | if(scanline < FSettings.FirstSLine || scanline > FSettings.LastSLine) |
e328100e |
623 | { |
624 | if(PPU_hook) |
625 | PRefreshLine(); |
c0623dcf |
626 | y=(int)SPRAM[0] + 1; |
627 | if(scanline==y && SpriteON) PPU_status|=0x40; |
e328100e |
628 | } |
629 | else |
630 | { |
631 | if(ScreenON) |
c62d2810 |
632 | { |
21afaa36 |
633 | BGRender(target); |
c62d2810 |
634 | } |
e328100e |
635 | else |
c62d2810 |
636 | { |
e328100e |
637 | tem=Pal[0]|0x40; |
638 | tem|=tem << 8; |
639 | tem|=tem << 16; |
640 | FCEU_dwmemset(target,tem,256); |
c62d2810 |
641 | } |
15300263 |
642 | } |
e328100e |
643 | |
c62d2810 |
644 | if(InputScanlineHook) |
e328100e |
645 | InputScanlineHook(target,spork?sprlinebuf:0,linestartts,256); |
646 | } |
647 | |
648 | |
649 | static void LineUpdateEnd(uint8 *target) |
650 | { |
651 | #ifdef GP2X |
652 | if(ScreenON || SpriteON) // Yes, very el-cheapo. |
653 | { |
654 | if(PPU[1]&0x01) |
655 | block_and(target, 256, 0x30); |
656 | } |
657 | if((PPU[1]>>5)==0x7) |
658 | block_or(target, 256, 0xc0); |
659 | else if(PPU[1]&0xE0) |
660 | block_or(target, 256, 0x40); |
661 | else |
662 | block_andor(target, 256, 0x3f, 0x80); |
663 | #else |
664 | int x; |
665 | |
666 | if(ScreenON || SpriteON) // Yes, very el-cheapo. |
667 | { |
668 | if(PPU[1]&0x01) |
669 | { |
670 | for(x=63;x>=0;x--) |
671 | *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])&0x30303030; |
672 | } |
673 | } |
674 | if((PPU[1]>>5)==0x7) |
675 | { |
676 | for(x=63;x>=0;x--) |
677 | *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0xc0c0c0c0; |
678 | } |
679 | else if(PPU[1]&0xE0) |
680 | for(x=63;x>=0;x--) |
681 | *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])|0x40404040; |
682 | else |
683 | for(x=63;x>=0;x--) |
684 | *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0x80808080; |
685 | #endif |
686 | |
687 | // black borders |
688 | ((uint32 *)target)[-2]=((uint32 *)target)[-1]=0; |
689 | ((uint32 *)target)[64]=((uint32 *)target)[65]=0; |
c62d2810 |
690 | } |
691 | |
692 | #define PAL(c) ((c)+cc) |
693 | |
694 | |
695 | static void PRefreshLine(void) |
696 | { |
078eb4c0 |
697 | uint32 vofs; |
698 | int X1; |
699 | vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7); |
700 | void (*PPU_hook_)(uint32 A) = PPU_hook; |
c62d2810 |
701 | |
c62d2810 |
702 | |
078eb4c0 |
703 | for(X1=33;X1;X1--) |
704 | { |
705 | uint32 zz2; |
706 | uint32 vadr; |
707 | |
708 | zz2=(RefreshAddr>>10)&3; |
709 | PPU_hook_(0x2000|(RefreshAddr&0xFFF)); |
710 | |
711 | vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs; |
712 | |
713 | PPU_hook_(vadr); |
c62d2810 |
714 | |
c62d2810 |
715 | if((RefreshAddr&0x1f)==0x1f) |
716 | RefreshAddr^=0x41F; |
717 | else |
718 | RefreshAddr++; |
078eb4c0 |
719 | } |
c62d2810 |
720 | } |
721 | |
4a1bf31b |
722 | /* This high-level graphics MMC5 emulation code was written |
723 | for MMC5 carts in "CL" mode. It's probably not totally |
724 | correct for carts in "SL" mode. |
725 | */ |
726 | static void RefreshLine_MMC5Hack1(uint8 *P, uint32 vofs) |
c62d2810 |
727 | { |
4a1bf31b |
728 | int8 tochange, X1; |
c62d2810 |
729 | |
730 | tochange=MMC5HackSPMode&0x1F; |
731 | |
732 | for(X1=33;X1;X1--,P+=8) |
733 | { |
734 | uint8 *C; |
5232c20c |
735 | uint8 cc,zz,zz2; |
c62d2810 |
736 | uint32 vadr; |
737 | |
15300263 |
738 | if((tochange<=0 && MMC5HackSPMode&0x40) || |
c62d2810 |
739 | (tochange>0 && !(MMC5HackSPMode&0x40))) |
740 | { |
741 | uint8 xs,ys; |
742 | |
15300263 |
743 | xs=33-X1; |
c62d2810 |
744 | ys=((scanline>>3)+MMC5HackSPScroll)&0x1F; |
745 | if(ys>=0x1E) ys-=0x1E; |
746 | vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7); |
747 | |
748 | C = MMC5HackVROMPTR+vadr; |
749 | C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12); |
750 | |
751 | cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)]; |
752 | cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2; |
753 | } |
754 | else |
755 | { |
756 | zz=RefreshAddr&0x1F; |
757 | zz2=(RefreshAddr>>10)&3; |
758 | vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs; |
759 | C = MMC5BGVRAMADR(vadr); |
760 | cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)]; |
761 | cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2; |
762 | } |
763 | #include "fceline.h" |
764 | |
765 | if((RefreshAddr&0x1f)==0x1f) |
766 | RefreshAddr^=0x41F; |
767 | else |
768 | RefreshAddr++; |
769 | tochange--; |
770 | } |
4a1bf31b |
771 | } |
772 | |
773 | static void RefreshLine_MMC5Hack2(uint8 *P, uint32 vofs) |
774 | { |
775 | int8 tochange, X1; |
c62d2810 |
776 | |
777 | tochange=MMC5HackSPMode&0x1F; |
778 | |
779 | for(X1=33;X1;X1--,P+=8) |
780 | { |
781 | uint8 *C; |
5232c20c |
782 | uint8 cc; |
783 | uint8 zz2; |
c62d2810 |
784 | uint32 vadr; |
785 | |
786 | if((tochange<=0 && MMC5HackSPMode&0x40) || |
787 | (tochange>0 && !(MMC5HackSPMode&0x40))) |
788 | { |
789 | uint8 xs,ys; |
790 | |
15300263 |
791 | xs=33-X1; |
c62d2810 |
792 | ys=((scanline>>3)+MMC5HackSPScroll)&0x1F; |
793 | if(ys>=0x1E) ys-=0x1E; |
794 | vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7); |
795 | |
796 | C = MMC5HackVROMPTR+vadr; |
797 | C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12); |
798 | |
799 | cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)]; |
800 | cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2; |
801 | } |
802 | else |
803 | { |
804 | C=MMC5HackVROMPTR; |
805 | zz2=(RefreshAddr>>10)&3; |
806 | vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs; |
807 | C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f & |
808 | MMC5HackVROMMask) << 12) + (vadr & 0xfff); |
809 | vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4; |
810 | cc = vadr; |
811 | } |
812 | #include "fceline.h" |
813 | if((RefreshAddr&0x1f)==0x1f) |
814 | RefreshAddr^=0x41F; |
815 | else |
816 | RefreshAddr++; |
817 | tochange--; |
818 | } |
4a1bf31b |
819 | } |
820 | |
821 | static void RefreshLine_MMC5Hack3(uint8 *P, uint32 vofs) |
822 | { |
823 | int8 X1; |
c62d2810 |
824 | |
c62d2810 |
825 | for(X1=33;X1;X1--,P+=8) |
826 | { |
15300263 |
827 | uint8 *C; |
5232c20c |
828 | uint8 cc; |
829 | uint8 zz2; |
15300263 |
830 | uint32 vadr; |
c62d2810 |
831 | |
832 | C=MMC5HackVROMPTR; |
833 | zz2=(RefreshAddr>>10)&3; |
834 | vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs; |
15300263 |
835 | C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f & |
c62d2810 |
836 | MMC5HackVROMMask) << 12) + (vadr & 0xfff); |
837 | vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4; |
838 | cc = vadr; |
839 | |
840 | #include "fceline.h" |
841 | if((RefreshAddr&0x1f)==0x1f) |
842 | RefreshAddr^=0x41F; |
843 | else |
844 | RefreshAddr++; |
845 | } |
4a1bf31b |
846 | } |
847 | |
848 | static void RefreshLine_MMC5Hack4(uint8 *P, uint32 vofs) |
849 | { |
850 | int8 X1; |
851 | |
c62d2810 |
852 | for(X1=33;X1;X1--,P+=8) |
853 | { |
854 | uint8 *C; |
5232c20c |
855 | uint8 cc,zz,zz2; |
c62d2810 |
856 | uint32 vadr; |
857 | |
858 | zz=RefreshAddr&0x1F; |
859 | zz2=(RefreshAddr>>10)&3; |
860 | vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs; |
861 | C = MMC5BGVRAMADR(vadr); |
862 | cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)]; |
863 | cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2; |
864 | |
865 | #include "fceline.h" |
15300263 |
866 | |
c62d2810 |
867 | if((RefreshAddr&0x1f)==0x1f) |
868 | RefreshAddr^=0x41F; |
869 | else |
870 | RefreshAddr++; |
15300263 |
871 | } |
4a1bf31b |
872 | } |
873 | |
874 | static void RefreshLine_PPU_hook(uint8 *P, uint32 vofs) |
875 | { |
876 | int8 X1; |
078eb4c0 |
877 | void (*PPU_hook_)(uint32 A) = PPU_hook; |
878 | uint32 rfraddr = RefreshAddr; |
879 | uint8 *page = vnapage[(rfraddr>>10)&3]; |
c62d2810 |
880 | |
c62d2810 |
881 | for(X1=33;X1;X1--,P+=8) |
882 | { |
15300263 |
883 | uint8 *C; |
078eb4c0 |
884 | uint8 cc,zz; |
15300263 |
885 | uint32 vadr; |
c62d2810 |
886 | |
078eb4c0 |
887 | zz=rfraddr&0x1F; |
888 | PPU_hook_(0x2000|(rfraddr&0xFFF)); |
889 | cc=page[0x3c0+(zz>>2)+((rfraddr&0x380)>>4)]; |
890 | cc=((cc >> ((zz&2) + ((rfraddr&0x40)>>4))) &3) <<2; |
891 | vadr=(page[rfraddr&0x3ff]<<4)+vofs; |
c62d2810 |
892 | C = VRAMADR(vadr); |
893 | |
894 | #include "fceline.h" |
895 | |
078eb4c0 |
896 | PPU_hook_(vadr); |
c62d2810 |
897 | |
078eb4c0 |
898 | if((rfraddr&0x1f)==0x1f) { |
899 | rfraddr^=0x41F; |
900 | page = vnapage[(rfraddr>>10)&3]; |
901 | } else |
902 | rfraddr++; |
c62d2810 |
903 | } |
078eb4c0 |
904 | RefreshAddr = rfraddr; |
4a1bf31b |
905 | } |
906 | |
9115e7d2 |
907 | static void RefreshLine_normal(uint8 *P, uint32 vofs) // vofs is 0x107 max |
4a1bf31b |
908 | { |
909 | int8 X1; |
9115e7d2 |
910 | uint32 rfraddr = RefreshAddr; |
911 | uint8 *page = vnapage[(rfraddr>>10)&3]; |
912 | uint32 cc2=0; |
4a1bf31b |
913 | |
9115e7d2 |
914 | if ((rfraddr&0xc)!=0) |
915 | cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0); |
916 | |
917 | for (X1=33;X1;X1--,P+=8) |
c62d2810 |
918 | { |
9115e7d2 |
919 | uint8 cc,*C; |
c62d2810 |
920 | uint32 vadr; |
921 | |
9115e7d2 |
922 | vadr=(page[rfraddr&0x3ff]<<4)+vofs; |
c62d2810 |
923 | C = VRAMADR(vadr); |
9115e7d2 |
924 | if ((rfraddr&0xc)==0) |
925 | cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0); |
926 | cc=((cc2 >> ((rfraddr&2) + ((rfraddr&0x40)>>4) + ((rfraddr&0xc)<<1))) & 3) << 2; |
4a1bf31b |
927 | |
9115e7d2 |
928 | #include "fceline.h" |
15300263 |
929 | |
9115e7d2 |
930 | if((rfraddr&0x1f)==0x1f) { |
931 | rfraddr^=0x41F; |
932 | page = vnapage[(rfraddr>>10)&3]; |
933 | } else |
934 | rfraddr++; |
c62d2810 |
935 | } |
9115e7d2 |
936 | RefreshAddr = rfraddr; |
4a1bf31b |
937 | } |
c62d2810 |
938 | |
4a1bf31b |
939 | static void SetRefreshLine(void) |
940 | { |
941 | if(MMC5Hack && geniestage!=1) |
942 | { |
943 | if(MMC5HackCHRMode==0 && (MMC5HackSPMode&0x80)) |
944 | { |
945 | if (RefreshLine != RefreshLine_MMC5Hack1) printf("set refr RefreshLine_MMC5Hack1\n"); |
946 | RefreshLine = RefreshLine_MMC5Hack1; |
947 | } |
948 | else if(MMC5HackCHRMode==1 && (MMC5HackSPMode&0x80)) |
949 | { |
950 | if (RefreshLine != RefreshLine_MMC5Hack2) printf("set refr RefreshLine_MMC5Hack2\n"); |
951 | RefreshLine = RefreshLine_MMC5Hack2; |
952 | } |
953 | else if(MMC5HackCHRMode==1) |
954 | { |
955 | if (RefreshLine != RefreshLine_MMC5Hack3) printf("set refr RefreshLine_MMC5Hack3\n"); |
956 | RefreshLine = RefreshLine_MMC5Hack3; |
957 | } |
958 | else |
959 | { |
960 | if (RefreshLine != RefreshLine_MMC5Hack4) printf("set refr RefreshLine_MMC5Hack4\n"); |
961 | RefreshLine = RefreshLine_MMC5Hack4; |
962 | } |
963 | } // End if(MMC5Hack) |
964 | else if(PPU_hook) |
965 | { |
966 | if (RefreshLine != RefreshLine_PPU_hook) printf("set refr RefreshLine_PPU_hook\n"); |
967 | RefreshLine = RefreshLine_PPU_hook; |
968 | } |
969 | else |
970 | { |
971 | if (RefreshLine != RefreshLine_normal) printf("set refr RefreshLine_normal\n"); |
972 | RefreshLine = RefreshLine_normal; |
973 | } |
c62d2810 |
974 | } |
975 | |
4fdfab07 |
976 | static INLINE |
937bf65b |
977 | void Fixit2(void) |
c62d2810 |
978 | { |
979 | if(ScreenON || SpriteON) |
980 | { |
981 | uint32 rad=RefreshAddr; |
982 | rad&=0xFBE0; |
983 | rad|=TempAddr&0x041f; |
984 | RefreshAddr=rad; |
985 | //PPU_hook(RefreshAddr,-1); |
986 | } |
987 | } |
988 | |
4fdfab07 |
989 | static INLINE |
937bf65b |
990 | void Fixit1(void) |
c62d2810 |
991 | { |
992 | if(ScreenON || SpriteON) |
993 | { |
994 | uint32 rad=RefreshAddr; |
995 | |
996 | if((rad&0x7000)==0x7000) |
997 | { |
998 | rad^=0x7000; |
999 | if((rad&0x3E0)==0x3A0) |
1000 | { |
1001 | rad^=0x3A0; |
1002 | rad^=0x800; |
1003 | } |
1004 | else |
1005 | { |
1006 | if((rad&0x3E0)==0x3e0) |
1007 | rad^=0x3e0; |
1008 | else rad+=0x20; |
1009 | } |
1010 | } |
1011 | else |
1012 | rad+=0x1000; |
1013 | RefreshAddr=rad; |
1014 | //PPU_hook(RefreshAddr,-1); |
1015 | } |
1016 | } |
1017 | |
937bf65b |
1018 | |
5232c20c |
1019 | // ============================// |
1020 | // end of new code |
1021 | // ===========================// |
c62d2810 |
1022 | |
1023 | void ResetMapping(void) |
1024 | { |
1025 | int x; |
1026 | |
1027 | SetReadHandler(0x0000,0xFFFF,ANull); |
1028 | SetWriteHandler(0x0000,0xFFFF,BNull); |
1029 | |
1030 | SetReadHandler(0,0x7FF,ARAML); |
1031 | SetWriteHandler(0,0x7FF,BRAML); |
1032 | |
1033 | SetReadHandler(0x800,0x1FFF,ARAMH); /* Part of a little */ |
1034 | SetWriteHandler(0x800,0x1FFF,BRAMH); /* hack for a small speed boost. */ |
1035 | |
1036 | for(x=0x2000;x<0x4000;x+=8) |
1037 | { |
1038 | ARead[x]=A200x; |
1039 | BWrite[x]=B2000; |
1040 | ARead[x+1]=A200x; |
1041 | BWrite[x+1]=B2001; |
1042 | ARead[x+2]=A2002; |
1043 | BWrite[x+2]=B2002; |
1044 | ARead[x+3]=A200x; |
1045 | BWrite[x+3]=B2003; |
1046 | ARead[x+4]=A200x; |
1047 | BWrite[x+4]=B2004; |
1048 | ARead[x+5]=A200x; |
1049 | BWrite[x+5]=B2005; |
1050 | ARead[x+6]=A200x; |
1051 | BWrite[x+6]=B2006; |
1052 | ARead[x+7]=A2007; |
1053 | BWrite[x+7]=B2007; |
1054 | } |
1055 | |
1056 | BWrite[0x4014]=B4014; |
1057 | SetNESSoundMap(); |
1058 | InitializeInput(); |
1059 | } |
1060 | |
1061 | int GameLoaded=0; |
1062 | void CloseGame(void) |
1063 | { |
c4980f9e |
1064 | FCEUI_StopMovie(); |
c62d2810 |
1065 | if(GameLoaded) |
1066 | { |
1067 | if(FCEUGameInfo.type!=GIT_NSF) |
92764e62 |
1068 | FCEU_FlushGameCheats(0,0); |
c62d2810 |
1069 | #ifdef NETWORK |
1070 | if(FSettings.NetworkPlay) KillNetplay(); |
15300263 |
1071 | #endif |
c4980f9e |
1072 | GameInterface(GI_CLOSE, 0); |
c62d2810 |
1073 | CloseGenie(); |
1074 | GameLoaded=0; |
1075 | } |
1076 | } |
1077 | |
1078 | void ResetGameLoaded(void) |
1079 | { |
1080 | if(GameLoaded) CloseGame(); |
1081 | GameStateRestore=0; |
1082 | PPU_hook=0; |
d97315ac |
1083 | GameHBIRQHook=GameHBIRQHook2=0; |
c62d2810 |
1084 | GameExpSound.Fill=0; |
1085 | GameExpSound.RChange=0; |
1086 | if(GameExpSound.Kill) |
1087 | GameExpSound.Kill(); |
1088 | GameExpSound.Kill=0; |
1089 | MapIRQHook=0; |
1090 | MMC5Hack=0; |
1091 | PAL&=1; |
1092 | pale=0; |
1093 | |
1094 | FCEUGameInfo.name=0; |
1095 | FCEUGameInfo.type=GIT_CART; |
1096 | FCEUGameInfo.vidsys=GIV_USER; |
1097 | FCEUGameInfo.input[0]=FCEUGameInfo.input[1]=-1; |
1098 | FCEUGameInfo.inputfc=-1; |
1099 | } |
1100 | |
5a2aa426 |
1101 | char lastLoadedGameName [2048]; |
989672f4 |
1102 | int LoadGameLastError = 0; |
d97315ac |
1103 | int UNIFLoad(const char *name, int fp); |
1104 | int iNESLoad(const char *name, int fp); |
1105 | int FDSLoad(const char *name, int fp); |
1106 | int NSFLoad(int fp); |
5a2aa426 |
1107 | |
c62d2810 |
1108 | FCEUGI *FCEUI_LoadGame(char *name) |
1109 | { |
5a2aa426 |
1110 | char name2[512]; |
1111 | int have_movie = 0; |
c62d2810 |
1112 | int fp; |
1113 | |
b2b95d2e |
1114 | //Exit=1; |
989672f4 |
1115 | LoadGameLastError = 0; |
c62d2810 |
1116 | ResetGameLoaded(); |
1117 | |
5a2aa426 |
1118 | strncpy(name2, name, sizeof(name2)); |
1119 | name2[sizeof(name2)-1] = 0; |
1120 | |
1121 | fp=FCEU_fopen(name2,"rb"); |
c62d2810 |
1122 | if(!fp) |
1123 | { |
1124 | FCEU_PrintError("Error opening \"%s\"!",name); |
989672f4 |
1125 | LoadGameLastError = 1; |
c62d2810 |
1126 | return 0; |
1127 | } |
1128 | |
5a2aa426 |
1129 | { |
1130 | char *p = name2 + strlen(name2) - 4; |
1131 | if (strcmp(p, ".fcm") == 0) |
1132 | { |
1133 | // movie detected |
1134 | printf("movie detected\n"); |
1135 | FCEU_fclose(fp); |
1136 | *p = 0; |
1137 | fp=FCEU_fopen(name2,"rb"); |
b2b95d2e |
1138 | if (!fp && p - name2 > 2) p[-2] = 0; |
1139 | fp=FCEU_fopen(name2,"rb"); |
5a2aa426 |
1140 | if (!fp) { |
1141 | printf("no ROM for movie\n"); |
989672f4 |
1142 | LoadGameLastError = 2; |
5a2aa426 |
1143 | return 0; |
1144 | } |
1145 | have_movie = 1; |
1146 | } |
1147 | } |
1148 | |
5a2aa426 |
1149 | GetFileBase(name2); |
1150 | if(iNESLoad(name2,fp)) |
c62d2810 |
1151 | goto endlseq; |
1152 | if(NSFLoad(fp)) |
1153 | goto endlseq; |
5a2aa426 |
1154 | if(FDSLoad(name2,fp)) |
c62d2810 |
1155 | goto endlseq; |
5a2aa426 |
1156 | if(UNIFLoad(name2,fp)) |
c62d2810 |
1157 | goto endlseq; |
1158 | |
1159 | FCEU_PrintError("An error occurred while loading the file."); |
1160 | FCEU_fclose(fp); |
989672f4 |
1161 | // format handlers may set LoadGameLastError themselves. |
1162 | if (LoadGameLastError == 0) LoadGameLastError = 3; |
c62d2810 |
1163 | return 0; |
1164 | |
1165 | endlseq: |
1166 | FCEU_fclose(fp); |
15300263 |
1167 | GameLoaded=1; |
c62d2810 |
1168 | |
1169 | FCEU_ResetVidSys(); |
1170 | if(FCEUGameInfo.type!=GIT_NSF) |
1171 | if(FSettings.GameGenie) |
1172 | OpenGenie(); |
1173 | |
1174 | PowerNES(); |
1175 | #ifdef NETWORK |
1176 | if(FSettings.NetworkPlay) InitNetplay(); |
1177 | #endif |
1178 | SaveStateRefresh(); |
1179 | if(FCEUGameInfo.type!=GIT_NSF) |
1180 | { |
92764e62 |
1181 | FCEU_LoadGamePalette(); |
1182 | FCEU_LoadGameCheats(0); |
c62d2810 |
1183 | } |
15300263 |
1184 | |
c62d2810 |
1185 | FCEU_ResetPalette(); |
1186 | Exit=0; |
5a2aa426 |
1187 | |
1188 | if (have_movie) |
1189 | FCEUI_LoadMovie(name, 1); |
989672f4 |
1190 | |
1191 | strcpy(lastLoadedGameName, name2); |
1192 | |
c62d2810 |
1193 | return(&FCEUGameInfo); |
1194 | } |
1195 | |
1196 | |
1197 | void FCEU_ResetVidSys(void) |
1198 | { |
1199 | int w; |
1200 | |
1201 | if(FCEUGameInfo.vidsys==GIV_NTSC) |
1202 | w=0; |
1203 | else if(FCEUGameInfo.vidsys==GIV_PAL) |
1204 | w=1; |
1205 | else |
1206 | w=FSettings.PAL; |
1207 | |
1208 | if(w) |
1209 | { |
1210 | PAL=1; |
c62d2810 |
1211 | FSettings.FirstSLine=FSettings.UsrFirstSLine[1]; |
1212 | FSettings.LastSLine=FSettings.UsrLastSLine[1]; |
1213 | } |
1214 | else |
1215 | { |
1216 | PAL=0; |
c62d2810 |
1217 | FSettings.FirstSLine=FSettings.UsrFirstSLine[0]; |
1218 | FSettings.LastSLine=FSettings.UsrLastSLine[0]; |
1219 | } |
0b65fdb3 |
1220 | printf("ResetVidSys: PAL = %i\n", PAL); |
c62d2810 |
1221 | SetSoundVariables(); |
1222 | } |
1223 | |
1224 | int FCEUI_Initialize(void) |
1225 | { |
1226 | if(!InitVirtualVideo()) |
1227 | return 0; |
1228 | memset(&FSettings,0,sizeof(FSettings)); |
1229 | FSettings.UsrFirstSLine[0]=8; |
1230 | FSettings.UsrFirstSLine[1]=0; |
1231 | FSettings.UsrLastSLine[0]=FSettings.UsrLastSLine[1]=239; |
92764e62 |
1232 | FSettings.SoundVolume=100; |
c62d2810 |
1233 | return 1; |
1234 | } |
1235 | |
ea80a45b |
1236 | void MMC5_hb(int); /* Ugh ugh ugh. */ |
e328100e |
1237 | static void DoLine(void) |
c62d2810 |
1238 | { |
e328100e |
1239 | uint8 *target=XBuf+scanline*320+32; |
1240 | |
1241 | LineUpdate(target); |
c62d2810 |
1242 | |
ea80a45b |
1243 | if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline); |
1244 | |
e328100e |
1245 | X6502_Run(256); |
1246 | |
f12b1f31 |
1247 | // check: Battletoads & Double Dragon, Addams Family |
1248 | // sky glitches in SMB1 if done wrong |
e328100e |
1249 | FakedLineUpdate(); |
1250 | |
1251 | #ifdef FRAMESKIP |
1252 | if(!FSkip) |
1253 | #endif |
21afaa36 |
1254 | if(scanline>=FSettings.FirstSLine && scanline<=FSettings.LastSLine) |
1255 | { |
1256 | if(SpriteON && spork) |
1257 | CopySprites(target); |
e328100e |
1258 | |
21afaa36 |
1259 | LineUpdateEnd(target); |
1260 | } |
e328100e |
1261 | sphitx=0x100; |
1262 | |
1263 | if(ScreenON || SpriteON) |
1264 | FetchSpriteData(); |
1265 | |
1266 | // DoHBlank(); |
1267 | if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18)) |
15300263 |
1268 | { |
e328100e |
1269 | X6502_Run(6); |
1270 | Fixit2(); |
1271 | X6502_Run(4); |
1272 | GameHBIRQHook(); |
1273 | X6502_Run(85-10-16); |
c62d2810 |
1274 | } |
1275 | else |
1276 | { |
e328100e |
1277 | X6502_Run(6); // Tried 65, caused problems with Slalom(maybe others) |
1278 | Fixit2(); |
1279 | X6502_Run(85-6-16); |
c62d2810 |
1280 | } |
e328100e |
1281 | |
1282 | if(SpriteON) |
1283 | RefreshSprites(); |
1284 | if(GameHBIRQHook2 && (ScreenON || SpriteON)) |
1285 | GameHBIRQHook2(); |
1286 | scanline++; |
1287 | if (scanline<240) |
1288 | ResetRL(XBuf+scanline*320+32); |
1289 | X6502_Run(16); |
c62d2810 |
1290 | } |
c62d2810 |
1291 | |
e328100e |
1292 | |
c62d2810 |
1293 | void EmLoop(void) |
1294 | { |
1295 | for(;;) |
1296 | { |
ea80a45b |
1297 | int x; |
92e249b1 |
1298 | uint32 scanlines_per_frame = PAL ? 312 : 262; |
4fdfab07 |
1299 | UpdateInput(); |
92764e62 |
1300 | FCEU_ApplyPeriodicCheats(); |
4fdfab07 |
1301 | |
1302 | // FCEUPPU_Loop: |
1303 | if(ppudead) /* Needed for Knight Rider, possibly others. */ |
1304 | { |
6587f346 |
1305 | //memset(XBuf, 0, 320*240); |
4fdfab07 |
1306 | X6502_Run(scanlines_per_frame*(256+85)); |
1307 | ppudead--; |
1308 | goto update; |
1309 | } |
ea80a45b |
1310 | |
c62d2810 |
1311 | X6502_Run(256+85); |
1312 | |
1313 | PPU[2]|=0x80; |
1314 | PPU[3]=PPUSPL=0; /* Not sure if this is correct. According |
1315 | to Matt Conte and my own tests, it is. Timing is probably |
1316 | off, though. NOTE: Not having this here |
1317 | breaks a Super Donkey Kong game. */ |
1318 | |
1319 | X6502_Run(12); /* I need to figure out the true nature and length |
15300263 |
1320 | of this delay. |
c62d2810 |
1321 | */ |
1322 | if(FCEUGameInfo.type==GIT_NSF) |
ea80a45b |
1323 | DoNSFFrame(); |
c62d2810 |
1324 | else if(VBlankON) |
1325 | TriggerNMI(); |
1326 | |
655f8df0 |
1327 | // Note: this is needed for asm core |
1328 | // Warning: using 'scanline' var here breaks Castlevania III |
1329 | { |
1330 | int lines; |
1331 | X6502_Run(256+85-12); |
1332 | for (lines=scanlines_per_frame-242-1;lines;lines--) |
1333 | X6502_Run(256+85); |
1334 | } |
1335 | // X6502_Run((scanlines_per_frame-242)*(256+85)-12); |
c62d2810 |
1336 | PPU_status&=0x1f; |
c62d2810 |
1337 | X6502_Run(256); |
ea80a45b |
1338 | |
c62d2810 |
1339 | { |
c62d2810 |
1340 | if(ScreenON || SpriteON) |
d97315ac |
1341 | { |
c62d2810 |
1342 | if(GameHBIRQHook) |
1343 | GameHBIRQHook(); |
ea80a45b |
1344 | if(PPU_hook) |
1345 | for(x=0;x<42;x++) {PPU_hook(0x2000); PPU_hook(0);} // ugh |
d97315ac |
1346 | if(GameHBIRQHook2) |
1347 | GameHBIRQHook2(); |
1348 | } |
c62d2810 |
1349 | |
4fdfab07 |
1350 | X6502_Run(85-16); |
c62d2810 |
1351 | |
4fdfab07 |
1352 | if(ScreenON || SpriteON) |
1353 | { |
1354 | RefreshAddr=TempAddr; |
1355 | if(PPU_hook) PPU_hook(RefreshAddr&0x3fff); |
1356 | } |
e328100e |
1357 | spork=0; |
1358 | ResetRL(XBuf+32); |
4fdfab07 |
1359 | |
1360 | X6502_Run(16-kook); |
1361 | kook ^= 1; |
c62d2810 |
1362 | } |
4fdfab07 |
1363 | |
c62d2810 |
1364 | if(FCEUGameInfo.type==GIT_NSF) |
937bf65b |
1365 | { |
6587f346 |
1366 | // run scanlines for asm core to fuction |
13624c8f |
1367 | for(scanline=0;scanline<240;scanline++) |
1368 | X6502_Run(256+85); |
937bf65b |
1369 | } |
c62d2810 |
1370 | else |
1371 | { |
1372 | int x,max,maxref; |
1373 | |
1374 | deemp=PPU[1]>>5; |
4a1bf31b |
1375 | SetRefreshLine(); |
4fdfab07 |
1376 | for(scanline=0;scanline<240;) // scanline is incremented in DoLine. Evil. :/ |
c62d2810 |
1377 | { |
1378 | deempcnt[deemp]++; |
e328100e |
1379 | DoLine(); |
c62d2810 |
1380 | } |
ea80a45b |
1381 | if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline); |
c62d2810 |
1382 | for(x=1,max=0,maxref=0;x<7;x++) |
1383 | { |
1384 | if(deempcnt[x]>max) |
1385 | { |
1386 | max=deempcnt[x]; |
1387 | maxref=x; |
1388 | } |
1389 | deempcnt[x]=0; |
1390 | } |
c62d2810 |
1391 | SetNESDeemph(maxref,0); |
1392 | } |
1393 | |
4fdfab07 |
1394 | update: |
47d0839e |
1395 | if(Exit) |
1396 | { |
1397 | //CloseGame(); |
1398 | break; |
1399 | } |
1400 | |
c62d2810 |
1401 | { |
1402 | int ssize; |
1403 | |
1404 | ssize=FlushEmulateSound(); |
1405 | |
4fdfab07 |
1406 | timestampbase += timestamp; |
1407 | timestamp = 0; |
1408 | |
c62d2810 |
1409 | #ifdef FRAMESKIP |
1410 | if(FSkip) |
1411 | { |
1412 | FCEU_PutImageDummy(); |
1413 | FSkip--; |
5232c20c |
1414 | FCEUD_Update(0,WaveFinalMono,ssize); |
c62d2810 |
1415 | } |
1416 | else |
1417 | #endif |
1418 | { |
1419 | FCEU_PutImage(); |
5232c20c |
1420 | FCEUD_Update(XBuf+8,WaveFinalMono,ssize); |
c62d2810 |
1421 | } |
c62d2810 |
1422 | } |
1423 | |
4fdfab07 |
1424 | } // for |
c62d2810 |
1425 | } |
1426 | |
1427 | #ifdef FPS |
1428 | #include <sys/time.h> |
1429 | uint64 frcount; |
1430 | #endif |
1431 | void FCEUI_Emulate(void) |
1432 | { |
1433 | #ifdef FPS |
1434 | uint64 starttime,end; |
1435 | struct timeval tv; |
1436 | frcount=0; |
1437 | gettimeofday(&tv,0); |
1438 | starttime=((uint64)tv.tv_sec*1000000)+tv.tv_usec; |
1439 | #endif |
1440 | EmLoop(); |
1441 | |
1442 | #ifdef FPS |
1443 | // Probably won't work well on Windows port; for |
1444 | // debugging/speed testing. |
1445 | { |
1446 | uint64 w; |
1447 | int i,frac; |
1448 | gettimeofday(&tv,0); |
1449 | end=((uint64)tv.tv_sec*1000000)+tv.tv_usec; |
1450 | w=frcount*10000000000LL/(end-starttime); |
1451 | i=w/10000; |
1452 | frac=w-i*10000; |
1453 | printf("Average FPS: %d.%04d\n",i,frac); |
1454 | } |
1455 | #endif |
1456 | |
1457 | } |
1458 | |
1459 | void FCEUI_CloseGame(void) |
1460 | { |
1461 | Exit=1; |
1462 | } |
1463 | |
1464 | static void ResetPPU(void) |
1465 | { |
1466 | VRAMBuffer=PPU[0]=PPU[1]=PPU[2]=PPU[3]=0; |
1467 | PPUSPL=0; |
1468 | PPUGenLatch=0; |
1469 | RefreshAddr=TempAddr=0; |
1470 | vtoggle = 0; |
4fdfab07 |
1471 | ppudead = 2; |
1472 | kook = 0; |
c62d2810 |
1473 | } |
1474 | |
1475 | static void PowerPPU(void) |
1476 | { |
1477 | memset(NTARAM,0x00,0x800); |
1478 | memset(PALRAM,0x00,0x20); |
1479 | memset(SPRAM,0x00,0x100); |
1480 | ResetPPU(); |
1481 | } |
1482 | |
1483 | void ResetNES(void) |
1484 | { |
92764e62 |
1485 | if(!GameLoaded) return; |
c4980f9e |
1486 | GameInterface(GI_RESETM2, 0); |
c62d2810 |
1487 | ResetSound(); |
1488 | ResetPPU(); |
1489 | X6502_Reset(); |
1490 | } |
1491 | |
b8da43fb |
1492 | static void FCEU_MemoryRand(uint8 *ptr, uint32 size) |
1493 | { |
1494 | int x=0; |
1495 | while(size) |
1496 | { |
1497 | *ptr=(x&4)?0xFF:0x00; |
1498 | x++; |
1499 | size--; |
1500 | ptr++; |
1501 | } |
1502 | } |
1503 | |
15300263 |
1504 | void PowerNES(void) |
c62d2810 |
1505 | { |
1506 | if(!GameLoaded) return; |
1507 | |
1508 | FCEU_CheatResetRAM(); |
1509 | FCEU_CheatAddRAM(2,0,RAM); |
1510 | |
1511 | GeniePower(); |
1512 | |
b8da43fb |
1513 | #ifndef DEBUG_ASM_6502 |
1514 | FCEU_MemoryRand(RAM,0x800); |
1515 | #else |
c62d2810 |
1516 | memset(RAM,0x00,0x800); |
b8da43fb |
1517 | #endif |
c62d2810 |
1518 | ResetMapping(); |
c62d2810 |
1519 | PowerSound(); |
1520 | PowerPPU(); |
0bb3fe12 |
1521 | GameInterface(GI_POWER, 0); |
c62d2810 |
1522 | timestampbase=0; |
b8da43fb |
1523 | #ifdef ASM_6502 |
1524 | if (geniestage) |
1525 | GenieSetPages(0); |
1526 | #endif |
c62d2810 |
1527 | X6502_Power(); |
1528 | } |
1529 | |
890e37ba |
1530 | |
1531 | /* savestate stuff */ |
1532 | uint16 TempAddrT,RefreshAddrT; |
1533 | |
1534 | SFORMAT FCEUPPU_STATEINFO[]={ |
1535 | { NTARAM, 0x800, "NTAR"}, |
1536 | { PALRAM, 0x20, "PRAM"}, |
1537 | { SPRAM, 0x100, "SPRA"}, |
1538 | { PPU, 0x4, "PPUR"}, |
1539 | { &XOffset, 1, "XOFF"}, |
1540 | { &vtoggle, 1, "VTOG"}, |
1541 | { &RefreshAddrT, 2|RLSB, "RADD"}, |
1542 | { &TempAddrT, 2|RLSB, "TADD"}, |
1543 | { &VRAMBuffer, 1, "VBUF"}, |
1544 | { &PPUGenLatch, 1, "PGEN"}, |
1545 | // from 0.98.15 |
1546 | { &kook, 1, "KOOK"}, |
1547 | { &ppudead, 1, "DEAD"}, |
1548 | { &PPUSPL, 1, "PSPL"}, |
1549 | { 0 } |
1550 | }; |
1551 | |
1552 | |