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