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