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