-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnes_ppu.c
More file actions
1238 lines (1113 loc) · 36.1 KB
/
nes_ppu.c
File metadata and controls
1238 lines (1113 loc) · 36.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* nes_ppu.c
*
* NES PPU emulation
*/
/* $Id: nes_ppu.c,v 1.112 2001/03/06 06:02:38 nyef Exp $ */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "nes_ppu.h"
#include "nes.h"
#include "tool.h"
#include "ui.h"
#include "video.h"
#include "mappers.h"
#include "nes_psg.h"
#include "cal.h"
#include "tiledraw.h"
#include "palette.h"
#include "cop.h"
#include "nespal.c"
extern int frameskip;
struct nes_ppu nes_ppu_temp = {
0, 0, 0, 0,
};
nes_ppu nes_ppu_true = &nes_ppu_temp;
unsigned char PPU_sprite_ram[0x100];
/* ppu memory areas */
unsigned char *PPU_memory=NULL; /* PPU Ram */
unsigned char *PPU_banks[8];
unsigned char **PPU_patterns[2] = {
&PPU_banks[0],
&PPU_banks[4],
};
unsigned char *PPU_nametables[4];
ppulatch_t ppu_latchfunc; /* for mapper support */
long PPU_amode; /* $2006 */
void PPU_render_scan_line(nes_ppu ppu);
extern cal_cpu cpu;
extern char *nes_image,*nes_imageb;
int n0,n1,n2,n3,n4,n5,n6,n7;
int fstatus1,fstatus2,fstatus3;
int wnow=0,wthen=0;
int useconds=0;
int nesppu_periodic(nes_ppu ppu)
{
if (((ppu->scanline < 240) || (ppu->scanline == 262)) && (ppu->control_2 & 0x18)) {
ppu->mapper->hsync(ppu->mapper, cpu, 1);
} else {
ppu->mapper->hsync(ppu->mapper, cpu, 0);
}
if (ppu->scanline < 240) {
if (ppu->scanline == 0)
{
ppu->refresh_data = ppu->refresh_temp;
if (wthen==0) wthen=inl(0x60005010);
wnow=inl(0x60005010);
useconds+=16000;
wthen=wnow;
}
else
{
ppu->refresh_data &= 0xfbe0;
ppu->refresh_data |= ppu->refresh_temp & 0x041f;
}
PPU_render_scan_line(ppu);
} else if (ppu->scanline == 240) {
ppu->status |= 0x80;
if (frameskip==0) video_display_buffer();
}
ppu->scanline++;
if (ppu->scanline == 262) {
ppu->status = 0;
ppu->scanline = 0;
ppu->refresh_data = ppu->refresh_temp;
}
return (ppu->control_1 & 0x80) && (ppu->scanline == 243);
}
void nesppu_cache_tile_line(unsigned char pattern0, unsigned char pattern1, unsigned char *cache)
{
unsigned char chunks_0;
unsigned char chunks_1;
unsigned char *cacheptr;
cacheptr = cache;
chunks_0 = ((pattern0 >> 1) & 0x55) | (pattern1 & 0xaa);
chunks_1 = (pattern0 & 0x55) | ((pattern1 << 1) & 0xaa);
*cacheptr++ = (chunks_0 >> 6) & 3;
*cacheptr++ = (chunks_1 >> 6) & 3;
*cacheptr++ = (chunks_0 >> 4) & 3;
*cacheptr++ = (chunks_1 >> 4) & 3;
*cacheptr++ = (chunks_0 >> 2) & 3;
*cacheptr++ = (chunks_1 >> 2) & 3;
*cacheptr++ = chunks_0 & 3;
*cacheptr++ = chunks_1 & 3;
}
void nesppu_cache_chr_rom(unsigned char *chr_data, int chr_size, unsigned char *tilecache)
{
int page;
int tile;
int line;
unsigned char *cacheptr;
unsigned char *dataptr;
cacheptr = tilecache;
for (page = 0; page < (chr_size << 3); page++) {
for (line = 0; line < 8; line++) {
dataptr = chr_data + (page * 0x400) + line;
for (tile = 0; tile < 0x40; tile++) {
nesppu_cache_tile_line(*dataptr, *(dataptr + 8), cacheptr);
cacheptr += 8;
dataptr += 0x10;
}
}
}
}
extern int nomenu;
void PPU_init(nes_rom romfile)
{
int i;
nes_ppu_true->romfile = romfile;
video_setsize(256, 240);
video_setpal(64, nes_palbase_red, nes_palbase_green, nes_palbase_blue);
PPU_memory = malloc(0x3000);
nes_ppu_true->render_palette = new_palette_8(8, 4);
if(!nomenu){
if (romfile->chr_size) {
nes_ppu_true->tilecache = malloc(romfile->chr_size * 0x2000 * 4);
nesppu_cache_chr_rom(romfile->chr_data, romfile->chr_size, nes_ppu_true->tilecache);
} else {
nes_ppu_true->tilecache = malloc(0x2000 * 4);
nes_ppu_true->pattern_cache[0] = nes_ppu_true->tilecache + 0x0000;
nes_ppu_true->pattern_cache[1] = nes_ppu_true->tilecache + 0x1000;
nes_ppu_true->pattern_cache[2] = nes_ppu_true->tilecache + 0x2000;
nes_ppu_true->pattern_cache[3] = nes_ppu_true->tilecache + 0x3000;
nes_ppu_true->pattern_cache[4] = nes_ppu_true->tilecache + 0x4000;
nes_ppu_true->pattern_cache[5] = nes_ppu_true->tilecache + 0x5000;
nes_ppu_true->pattern_cache[6] = nes_ppu_true->tilecache + 0x6000;
nes_ppu_true->pattern_cache[7] = nes_ppu_true->tilecache + 0x7000;
}
for (i = 0; i < 8; i++) {
PPU_banks[i] = PPU_memory + (i * 0x400);
}
if (romfile->mirror_vertical) {
PPU_mirror_vertical();
} else {
PPU_mirror_horizontal();
}
nes_ppu_true->chr_is_rom = romfile->chr_size;}
}
void PPU_shutdown()
{
if (PPU_memory) { free(PPU_memory); PPU_memory=NULL; }
if (nes_ppu_true->tilecache) { free(nes_ppu_true->tilecache); nes_ppu_true->tilecache=NULL; }
if (nes_ppu_true->pageram_memory) { free(nes_ppu_true->pageram_memory); nes_ppu_true->pageram_memory=NULL; }
if (nes_ppu_true->pageram_tilecache) { free(nes_ppu_true->pageram_tilecache); nes_ppu_true->pageram_tilecache=NULL; }
}
void nesppu_set_mapper(nes_ppu ppu, nes_mapper mapper)
{
ppu->mapper = mapper;
}
int nesppu_mask_page_to(int page, int mask)
{
/*
* NOTE: This is two cheap hacks piled one atop another. The first is
* simply anding page with mask - 1, which relies on mask being a power
* of two (I believe it is in all good dumps of non-pirate games). The
* second is to not actually mask the page if the page number is less
* than the mask. This fixes for the unfortunately rather popular bad
* dump of Zelda II, which is missing some otherwise unused CHR-ROM
* pages, "but it works with every other emulator". The _reason_ it
* works with every other emulator, of course, is because every other
* emulator author got fed up with hearing "but it works with NESticle",
* and worked around the problem in some way, but people don't want to
* hear that, they just want to play their thrice-damned "p1r473 R0MZ".
*/
if (page < mask) return page;
return (page & (mask - 1));
}
void nesppu_map_1k(nes_ppu ppu, int bank, int page)
{
int masked_page;
int num_banks;
u8 *mem_base;
u8 *cache_base;
if (ppu->chr_is_rom) {
num_banks = ppu->romfile->chr_size << 3;
mem_base = ppu->romfile->chr_data;
cache_base = ppu->tilecache;
} else {
num_banks = ppu->pageram_size << 3;
mem_base = ppu->pageram_memory;
cache_base = ppu->pageram_tilecache;
}
if (!num_banks) {
return;
}
masked_page = nesppu_mask_page_to(page, num_banks);
PPU_banks[bank] = mem_base + (masked_page * 0x400);
ppu->pattern_cache[bank] = cache_base + (masked_page * 0x400 * 4);
}
void nesppu_map_2k(nes_ppu ppu, int bank, int page)
{
nesppu_map_1k(ppu, (bank << 1) + 0, (page << 1) + 0);
nesppu_map_1k(ppu, (bank << 1) + 1, (page << 1) + 1);
}
void nesppu_map_4k(nes_ppu ppu, int bank, int page)
{
nesppu_map_2k(ppu, (bank << 1) + 0, (page << 1) + 0);
nesppu_map_2k(ppu, (bank << 1) + 1, (page << 1) + 1);
}
void nesppu_map_8k(nes_ppu ppu, int bank, int page)
{
nesppu_map_4k(ppu, (bank << 1) + 0, (page << 1) + 0);
nesppu_map_4k(ppu, (bank << 1) + 1, (page << 1) + 1);
}
void nesppu_paged_ram_init(int num_8k_pages)
{
nes_ppu_true->pageram_memory = malloc(num_8k_pages * 0x2000);
nes_ppu_true->pageram_tilecache = malloc(num_8k_pages * 0x8000);
nes_ppu_true->pageram_size = num_8k_pages;
}
void nesppu_paged_ram_mode(int enabled)
{
if (enabled) {
nes_ppu_true->chr_is_rom = 0;
} else {
nes_ppu_true->chr_is_rom = nes_ppu_true->romfile->chr_size;
if (!nes_ppu_true->chr_is_rom) {
nes_ppu_true->pattern_cache[0] = nes_ppu_true->tilecache + 0x0000;
nes_ppu_true->pattern_cache[1] = nes_ppu_true->tilecache + 0x1000;
nes_ppu_true->pattern_cache[2] = nes_ppu_true->tilecache + 0x2000;
nes_ppu_true->pattern_cache[3] = nes_ppu_true->tilecache + 0x3000;
nes_ppu_true->pattern_cache[4] = nes_ppu_true->tilecache + 0x4000;
nes_ppu_true->pattern_cache[5] = nes_ppu_true->tilecache + 0x5000;
nes_ppu_true->pattern_cache[6] = nes_ppu_true->tilecache + 0x6000;
nes_ppu_true->pattern_cache[7] = nes_ppu_true->tilecache + 0x7000;
}
}
}
void PPU_mirror_horizontal()
{
PPU_nametables[0] = &(PPU_memory[0x2000]);
PPU_nametables[1] = &(PPU_memory[0x2000]);
PPU_nametables[2] = &(PPU_memory[0x2400]);
PPU_nametables[3] = &(PPU_memory[0x2400]);
}
void PPU_mirror_vertical()
{
PPU_nametables[0] = &(PPU_memory[0x2000]);
PPU_nametables[1] = &(PPU_memory[0x2400]);
PPU_nametables[2] = &(PPU_memory[0x2000]);
PPU_nametables[3] = &(PPU_memory[0x2400]);
}
void PPU_mirror_one_low()
{
PPU_nametables[0] = &(PPU_memory[0x2000]);
PPU_nametables[1] = &(PPU_memory[0x2000]);
PPU_nametables[2] = &(PPU_memory[0x2000]);
PPU_nametables[3] = &(PPU_memory[0x2000]);
}
void PPU_mirror_one_high()
{
PPU_nametables[0] = &(PPU_memory[0x2400]);
PPU_nametables[1] = &(PPU_memory[0x2400]);
PPU_nametables[2] = &(PPU_memory[0x2400]);
PPU_nametables[3] = &(PPU_memory[0x2400]);
}
void PPU_write(nes_ppu ppu, unsigned short address, unsigned char value)
{
address = address & 0x3fff;
if (ppu_latchfunc) {
ppu_latchfunc(ppu->mapper, address);
}
if (address >= 0x3f00) {
u8 screen_color;
screen_color = vid_pre_xlat[value & 0x3f];
if ((address & 0x3f03) == 0x3f00) {
ppu->palette[0x00 + (address & 0x0f)] = value;
ppu->palette[0x10 + (address & 0x0f)] = value;
ppu->render_palette->set(ppu->render_palette,
0x00 + (address & 0x0f), value);
ppu->render_palette->set(ppu->render_palette,
0x10 + (address & 0x0f), value);
} else {
ppu->palette[address & 0x1f] = value;
ppu->render_palette->set(ppu->render_palette,
address & 0x1f, value);
}
} else if (address > 0x1fff) {
PPU_nametables[(address >> 10) & 3][address & 0x3ff] = value;
} else if (!nes_ppu_true->chr_is_rom) {
unsigned char *cur_bank;
unsigned char *cache_bank;
cur_bank = PPU_banks[(address >> 10)];
cache_bank = ppu->pattern_cache[address >> 10];
cur_bank[(address & 0x3ff)] = value;
nesppu_cache_tile_line(cur_bank[(address & 0x3f7)], cur_bank[(address & 0x3ff) | 8], cache_bank + ((address & 0x3f0) >> 1) + ((address & 7) << 9));
}
}
unsigned char PPU_read(nes_ppu ppu, unsigned short address)
{
unsigned char value;
address &= 0x3fff;
if (ppu_latchfunc) {
ppu_latchfunc(ppu->mapper, address);
}
if (address >= 0x3f00) {
value = ppu->palette[address & 0x1f];
} else if (address >= 0x2000) {
value = PPU_nametables[(address >> 10) & 3][address & 0x3ff];
} else {
value = PPU_banks[(address >> 10)][(address & 0x3ff)];
}
return value;
}
struct spritedata {
unsigned char vpos;
unsigned char tile;
unsigned char flags;
unsigned char xpos;
};
struct {
unsigned char xpos;
unsigned char pattern0;
unsigned char pattern8;
unsigned char flags;
u8 *pattern;
} spritecache[8];
#define SFL_VFLIP 0x80
#define SFL_HFLIP 0x40
#define SFL_PRIO 0x20
#define SFL_COLOR 0x03
int num_sprites_line;
void PPU_init_spritecache_(nes_ppu ppu, unsigned short line, int is_8x16)
{
int i;
struct spritedata *sprites;
unsigned char cur_tile;
unsigned char cur_pattern_line;
unsigned char *cur_pattern;
int cur_ptbl;
sprites = (struct spritedata *)PPU_sprite_ram;
num_sprites_line = 0;
cur_ptbl = (ppu->control_1 & 0x08)? 1: 0;
/* FIXME: clear sprite count */
spritecache[num_sprites_line].xpos = sprites[i].xpos;
spritecache[num_sprites_line].flags = sprites[i].flags;
cur_pattern_line = (line - (sprites[0].vpos+1)) & (is_8x16? 15: 7);
if (sprites[0].flags & SFL_VFLIP) {
cur_pattern_line = (is_8x16? 15: 7) - cur_pattern_line;
}
cur_tile = sprites[i].tile;
if (is_8x16) {
cur_ptbl = cur_tile & 1;
cur_tile = (cur_pattern_line & 8)?
(cur_tile | 1): (cur_tile & 0xfe);
cur_pattern_line &= 7;
}
if (ppu_latchfunc) {
ppu_latchfunc(ppu->mapper, (cur_ptbl << 12) | (cur_tile << 4) | (cur_pattern_line));
ppu_latchfunc(ppu->mapper, (cur_ptbl << 12) | (cur_tile << 4) | (cur_pattern_line) | 8);
}
cur_pattern = &(PPU_patterns[cur_ptbl][(cur_tile >> 6)]
[((cur_tile & 0x3f) << 4) + cur_pattern_line]);
spritecache[num_sprites_line].pattern0 = cur_pattern[0];
spritecache[num_sprites_line].pattern8 = cur_pattern[8];
if (((cur_pattern[0]) || cur_pattern[8])) {
//ppu->status |= 0x40;
}
}
int sprite0=0;
void PPU_init_spritecache(nes_ppu ppu, unsigned short line, int is_8x16)
{
int i;
struct spritedata *sprites;
unsigned char cur_tile;
unsigned char cur_pattern_line;
unsigned char *cur_pattern;
int cur_ptbl;
sprites = (struct spritedata *)PPU_sprite_ram;
num_sprites_line = 0;
cur_ptbl = (ppu->control_1 & 0x08)? 1: 0;
/* FIXME: clear sprite count */
for (i = 0; i < 64; i++) {
if ((sprites[i].vpos + 1 <= line) &&
(sprites[i].vpos + (is_8x16? 17: 9) > line)) {
if (num_sprites_line > 7) {
/* FIXME: set sprite count */
break;
}
spritecache[num_sprites_line].xpos = sprites[i].xpos;
spritecache[num_sprites_line].flags = sprites[i].flags;
cur_pattern_line = (line - (sprites[i].vpos+1)) & (is_8x16? 15: 7);
if (sprites[i].flags & SFL_VFLIP) {
cur_pattern_line = (is_8x16? 15: 7) - cur_pattern_line;
}
cur_tile = sprites[i].tile;
if (is_8x16) {
cur_ptbl = cur_tile & 1;
cur_tile = (cur_pattern_line & 8)?
(cur_tile | 1): (cur_tile & 0xfe);
cur_pattern_line &= 7;
}
u32 vram_addr= (cur_ptbl << 12) | (cur_tile << 4) | (cur_pattern_line);
if (((vram_addr & 0x0FF0) == 0x0FD0) || ((vram_addr & 0x0FF0) == 0x0FE0))
if (ppu_latchfunc && i==0) {
ppu_latchfunc(ppu->mapper, (cur_ptbl << 12) | (cur_tile << 4) | (cur_pattern_line));
ppu_latchfunc(ppu->mapper, (cur_ptbl << 12) | (cur_tile << 4) | (cur_pattern_line) | 8);
}
cur_pattern = &(PPU_patterns[cur_ptbl][(cur_tile >> 6)]
[((cur_tile & 0x3f) << 4) + cur_pattern_line]);
spritecache[num_sprites_line].pattern0 = cur_pattern[0];
spritecache[num_sprites_line].pattern8 = cur_pattern[8];
spritecache[num_sprites_line].pattern =
&(ppu->pattern_cache[(cur_ptbl << 2) + (cur_tile >> 6)]
[((cur_tile & 0x3f) << 3) + (cur_pattern_line << 9)]);
if ((i == 0) && ((cur_pattern[0]) || cur_pattern[8])) {
//ppu->status |= 0x40;
sprite0=1;
}
num_sprites_line++;
}
}
}
void spritedraw_8(u8 *vbp, int offset, u8 *pattern, u8 *palette)
{
int i;
u8 *cur_vbp;
u8 *tiledata;
cur_vbp = vbp + offset;
tiledata = pattern;
for (i = 0; i < 8; i++) {
if (*tiledata) {
*cur_vbp = palette[*tiledata];
}
tiledata++;
cur_vbp++;
}
}
void spritedraw_rev_8(u8 *vbp, int offset, u8 *pattern, u8 *palette)
{
int i;
u8 *cur_vbp;
u8 *tiledata;
cur_vbp = vbp + offset;
tiledata = pattern + 7;
for (i = 0; i < 8; i++) {
if (*tiledata) {
*cur_vbp = palette[*tiledata];
}
tiledata--;
cur_vbp++;
}
}
void PPU_render_sprites(nes_ppu ppu, unsigned char *cur_vbp, int behind)
{
int i;
unsigned char *colors;
if (!num_sprites_line) {
return;
}
for (i = (num_sprites_line - 1); i >= 0; i--) {
if (behind? !(spritecache[i].flags & SFL_PRIO):
(spritecache[i].flags & SFL_PRIO)) {
continue;
}
colors = ppu->render_palette->palettes[4 + (spritecache[i].flags & SFL_COLOR)];
if (!(spritecache[i].flags & SFL_HFLIP)) {
spritedraw_8(cur_vbp, spritecache[i].xpos, spritecache[i].pattern, colors);
} else {
spritedraw_rev_8(cur_vbp, spritecache[i].xpos, spritecache[i].pattern, colors);
}
}
}
void nesppu_cache_background(nes_ppu ppu, unsigned char **patterns, unsigned char **attrs)
{
unsigned char *cur_tile;
unsigned char tile_offset;
unsigned char *cur_bank;
unsigned char *cur_attrline;
unsigned short cur_patternline;
unsigned char cur_attrbase;
unsigned char **cur_ptbl;
int i;
cur_patternline = (ppu->refresh_data >> 3) & 0x0e00;
cur_attrbase = (ppu->refresh_data >> 4) & 4;
cur_ptbl = &ppu->pattern_cache[(ppu->control_1 & 0x10) >> 2];
cur_bank = PPU_nametables[(ppu->refresh_data >> 10) & 3];
tile_offset = ppu->refresh_data & 0x1f;
cur_tile = &cur_bank[(ppu->refresh_data & 0x3e0) + tile_offset];
cur_attrline = cur_bank + ((ppu->refresh_data >> 4) & 0x38) + 0x3c0 + (tile_offset >> 2);
cur_attrbase |= tile_offset & 2;
for (i = 0; i < 33; i++) {
patterns[i] = &(cur_ptbl[(*cur_tile >> 6)]
[((*cur_tile & 0x3f) << 3) + cur_patternline]);
u32 vram_addr= ((ppu->control_1 & 0x10) << 8) | ((*cur_tile) << 4) |8;
if (((vram_addr & 0x0FF0) == 0x0FD0) || ((vram_addr & 0x0FF0) == 0x0FE0))
if (ppu_latchfunc) {
// ppu_latchfunc(ppu->mapper, ((ppu->control_1 & 0x10) << 8) | ((*cur_tile) << 4));
ppu_latchfunc(ppu->mapper, vram_addr);
}
attrs[i] = ppu->render_palette->palettes[(*cur_attrline >> cur_attrbase) & 3];
if (tile_offset & 1) {
cur_attrbase ^= 2;
}
if ((tile_offset & 3) == 3) {
cur_attrline++;
}
cur_tile++;
tile_offset++;
if (tile_offset > 31) {
tile_offset = 0;
cur_bank = PPU_nametables[((ppu->refresh_data >> 10) & 3) ^ 1];
cur_tile = &cur_bank[ppu->refresh_data & 0x3e0];
cur_attrline = cur_bank + ((ppu->refresh_data >> 4) & 0x38) + 0x3c0;
}
}
}
void PPU_render_background(nes_ppu ppu, char *cur_vbp)
{
unsigned char *patterns[33];
unsigned char *attrs[33];
int i;
nesppu_cache_background(ppu, patterns, attrs);
if (ppu->control_2 & 0x02) {
i = 0;
} else {
for (i = 0; i < 8; i++) {
*cur_vbp++ = vid_pre_xlat[0x0f];
}
i = 1;
}
tiledraw_8(cur_vbp, patterns + i, attrs + i, 32 - i, ppu->finescroll);
}
void floodfill_8(u8 *vbp, int width, u8 *palette, int index)
{
memset(vbp, palette[index], width);
}
int tframeskip=0;
int tflip=0;
extern int roffset_x;
extern int roffset_y;
extern int H;
u32 lastframe=0;
u32 now=0;
u32 avgframe=0x3000;
u32 vframe=0;
u32 aframe=0;
u32 atime=0;
u32 vtime=0;
int scanline_cache[262];
void PPU_render_scan_line(nes_ppu ppu)
{
/* if (ppu->scanline==239)
{
// frameskip++;
atime+=16666;
if (frameskip>tframeskip) { frameskip=0; aframe++; }
vframe++;
// if (tflip==0) { tflip=1; tframeskip=1; } else { tflip=0; tframeskip=2; } }
if (lastframe==0) lastframe=inl(0x60005010);
now=inl(0x60005010);
vtime+=now-lastframe;
if (vframe>60) { vframe>>=1; vtime>>=1; }
avgframe=vtime/vframe; //(avgframe+now-lastframe)>>1;
if (avgframe<16666 && frameskip!=1) { usleep((16666-avgframe)*vframe); }
if (avgframe<=16666) frameskip=0; else frameskip=1;
lastframe=now;
}*/
if (ppu->scanline==0)
{
frameskip++;
if (frameskip>tframeskip) { frameskip=0; }
// if (tflip==0) { tflip=1; tframeskip=1; } else { tflip=0; tframeskip=2; } }
lastframe=inl(0x60005010);
}
if (ppu->scanline==239)
{
now=inl(0x60005010);
avgframe=(avgframe+now-lastframe)>>1;
if (avgframe>0x343f) tframeskip++; else tframeskip--;
if (tframeskip<1) tframeskip=1;
if (tframeskip>2) tframeskip=2;
}
// if (ppu->scanline==0) { frameskip=(frameskip+1); if (frameskip>2) frameskip=0; }
// if ((frameskip==0) && ((ppu->scanline & 1)==0))
if ((frameskip==0)) // && (ppu->scanline>=roffset_y) && (ppu->scanline<roffset_y+H))
{
//while ((inl(COP_STATUS) & COP_LINE_REQ) != 0) {} //outl(inl(COP_STATUS) | COP_LINE_REQ, COP_STATUS);
unsigned char *cur_vbp;
cur_vbp = video_get_vbp(ppu->scanline);
floodfill_8(cur_vbp, 256, ppu->render_palette->palettes[0], 0);
if (ppu->control_2 & 0x10) {
PPU_init_spritecache(ppu, ppu->scanline, ppu->control_1 & 0x20);
scanline_cache[ppu->scanline]=ppu->status & 0x40;
PPU_render_sprites(ppu, cur_vbp, 1);
}
if (ppu->control_2 & 0x08) {
PPU_render_background(ppu, cur_vbp);
}
if (ppu->control_2 & 0x10) {
PPU_render_sprites(ppu, cur_vbp, 0);
}
}
else
// PPU_init_spritecache(ppu, ppu->scanline, ppu->control_1 & 0x20);
{
ppu->status|=scanline_cache[ppu->scanline];
}
/* ppu->refresh_data += 0x1000;
if (ppu->refresh_data & 0x8000) {
unsigned short tmp;
tmp = (ppu->refresh_data & 0x03e0) + 0x20;
ppu->refresh_data &= 0x7c1f;
if (tmp == 0x03c0) {
ppu->refresh_data ^= 0x0800;
} else {
ppu->refresh_data |= (tmp & 0x03e0);
}
}*/
if ((ppu->refresh_data & 0x7000)==0x7000)
{
ppu->refresh_data &=0x8fff;
if ((ppu->refresh_data & 0x03e0) == 0x03a0)
{
ppu->refresh_data ^= 0x0800;
ppu->refresh_data &= 0xfc1f;
}
else
{
if ((ppu->refresh_data & 0x3e0) == 0x03e0)
{
ppu->refresh_data &=0xfc1f;
}
else
{
ppu->refresh_data += 0x0020;
}
}
}
else
{
ppu->refresh_data += 0x1000;
}
}
extern int updatelr;
extern int cycles_left;
void ppu_io_write(nes_ppu ppu, unsigned short addr, unsigned char value)
{
switch (addr & 7) {
case 0:
ppu->control_1 = value;
ppu->refresh_temp &= 0xf3ff;
ppu->refresh_temp |= (value & 3) << 10;
break;
case 1:
ppu->control_2 = value;
break;
case 2:
deb_printf("ppu_io_write(2, %02x): Unknown register.\n", value);
break;
case 3:
ppu->sprite_addy = value;
break;
case 4:
updatelr=0;
PPU_sprite_ram[(ppu->sprite_addy++) & 0xff] = value;
break;
case 5:
if (PPU_amode & 1) {
PPU_amode &= ~1;
ppu->refresh_temp &= 0x0c1f;
ppu->refresh_temp |= (value & 7) << 12;
ppu->refresh_temp |= (value << 2) & 0x03e0;
} else {
PPU_amode |= 1;
ppu->refresh_temp &= 0x7fe0;
ppu->refresh_temp |= value >> 3;
ppu->finescroll = value & 7;
}
break;
case 6:
if (PPU_amode & 1) {
PPU_amode &= ~1;
ppu->address = (ppu->address_latch << 8) | value;
ppu->refresh_temp &= 0xff00;
ppu->refresh_temp |= value;
ppu->refresh_data = ppu->refresh_temp;
} else {
PPU_amode |= 1;
ppu->address_latch = value;
ppu->refresh_temp &= 0x00ff;
ppu->refresh_temp |= (value & 0x3f) << 8;
}
break;
case 7:
if (PPU_amode & 1) {
deb_printf("ppu_w_2007(): ppu write during addr sequence.\n");
}
PPU_write(ppu, ppu->address, value);
ppu->address += ((ppu->control_1 & 0x04)? 0x20: 1);
break;
}
}
unsigned char ppu_io_read(nes_ppu ppu, unsigned short addr)
{
unsigned char retval;
switch (addr & 7) {
case 2: n2++;
/* NOTE: to the best of my knowledge, these ports are synonymous */
if (sprite0==1) { ppu->status|=0x40; sprite0=0; }
retval = ppu->status;
if (retval & 0x20) fstatus1++;
if (retval & 0x40) fstatus2++;
if (retval & 0x80) fstatus3++;
ppu->status &= 0x7f;
PPU_amode &= ~1;
return retval;
break;
case 7: n7++;
retval = ppu->read_latch;
ppu->read_latch = PPU_read(ppu, ppu->address);
ppu->address += ((ppu->control_1 & 0x04)? 0x20: 1);
return retval;
break;
}
return 0; /* gcc -O2 is a chode. */
}
u8 ppu_readreg(u32 addr)
{
return ppu_io_read(nes_ppu_true,addr);
}
void ppu_writereg(u32 addr,u32 value)
{
ppu_io_write(nes_ppu_true,addr,value);
}
/*
* $Log: nes_ppu.c,v $
* Revision 1.112 2001/03/06 06:02:38 nyef
* "fixed" nesppu_mask_page_to() to not mask pages if page is < mask
*
* Revision 1.111 2000/10/09 16:37:55 nyef
* added preliminary support for paged CHR RAM
*
* Revision 1.110 2000/10/05 08:45:13 nyef
* moved PPU_memory from nes.c to nes_ppu.c
*
* Revision 1.109 2000/10/05 08:37:45 nyef
* moved sprite DMA handling from nes_ppu.c to nes.c
*
* Revision 1.108 2000/10/02 14:18:29 nyef
* removed call to nes_psg_vsync() (part of the psg rewrite)
*
* Revision 1.107 2000/09/16 22:49:21 nyef
* added mirror_vertical field to nes_rom structure
*
* Revision 1.106 2000/08/04 23:33:38 nyef
* moved the generic palette code out to palette.[ch]
*
* Revision 1.105 2000/07/26 01:26:13 nyef
* changed the CHR ROM switching code to use guard clauses
*
* Revision 1.104 2000/07/26 01:23:30 nyef
* hacked out the beginnings of a generic palette system
*
* Revision 1.103 2000/06/07 00:34:59 nyef
* removed the call to ppu->mapper->vsync() in nesppu_periodic()
*
* Revision 1.102 2000/06/03 04:26:07 nyef
* fixed palette mirroring
*
* Revision 1.101 2000/04/29 15:58:29 nyef
* cleaned up the sprite renderer slightly
*
* Revision 1.100 2000/02/27 15:03:52 nyef
* cleaned up the definition of struct nes_ppu
* moved the address, address latch, and read latch to struct nes_ppu
*
* Revision 1.99 2000/02/27 06:16:40 nyef
* removed (disused) ppu_flags variable
*
* Revision 1.98 2000/02/27 06:10:54 nyef
* reduced number of sprites per line to the correct value of 8
* cleaned up the sprite renderer slightly
*
* Revision 1.97 2000/02/27 06:01:00 nyef
* cleaned up mirroring implementation
* cleaned up memory access to nametable areas
*
* Revision 1.96 2000/02/05 19:46:58 nyef
* cleared ppu status at end of vblank (fixes bigfoot)
*
* Revision 1.95 1999/12/04 04:38:07 nyef
* removed inclusion of debug.h
*
* Revision 1.94 1999/11/26 01:28:36 nyef
* fixed typos in nesppu_map_4k() and nesppu_map_8k()
*
* Revision 1.93 1999/11/23 03:21:40 nyef
* changed to use new tiledraw interface
*
* Revision 1.92 1999/11/23 01:41:48 nyef
* changed to use tile renderer from tiledraw.c
*
* Revision 1.91 1999/11/21 17:23:14 nyef
* moved palette stuff in from nes.c
*
* Revision 1.90 1999/11/20 18:20:20 nyef
* fixed typo in nesppu_map_8k()
*
* Revision 1.89 1999/11/20 18:10:23 nyef
* fixed stupid mistake with CHR-RAM tile cache
*
* Revision 1.88 1999/11/20 17:44:01 nyef
* fixed tile cache to work with CHR-RAM games
*
* Revision 1.87 1999/11/20 16:53:33 nyef
* minor strength reduction in nesppu_cache_background()
*
* Revision 1.86 1999/11/20 16:36:18 nyef
* added preliminary tile cache (only works on CHR-ROM games)
*
* Revision 1.85 1999/11/20 05:38:10 nyef
* rebuilt romfile handling
* rebuilt chr-rom mapping to automatically handle page out of range conditions
* rebuilt chr-rom mapping to ignore mapping when using chr-ram
*
* Revision 1.84 1999/11/16 03:11:30 nyef
* extracted nesppu_cache_background() from PPU_render_background()
*
* Revision 1.83 1999/11/16 03:06:52 nyef
* changed PPU_render_background() to defer fetching the pattern data
* until actually blitting the data
*
* Revision 1.82 1999/11/16 02:36:22 nyef
* removed PPU_bank_XX and simplified the code that depended on it
*
* Revision 1.81 1999/11/15 04:05:04 nyef
* implemented preliminary version of new CHR ROM mapping interface
*
* Revision 1.80 1999/11/14 16:20:14 nyef
* fixed latch function interface to work with new mapper interface
*
* Revision 1.79 1999/11/14 07:00:18 nyef
* changed to use new mapper interface
*
* Revision 1.78 1999/11/14 04:12:56 nyef
* moved some global variables into struct nes_ppu
*
* Revision 1.77 1999/11/02 02:19:07 nyef
* took out change of revision 1.75 because it broke the snow demo
*
* Revision 1.76 1999/10/21 23:35:59 nyef
* changed to use new mapper hblank interface
*
* Revision 1.75 1999/08/23 00:57:56 nyef
* fixed NMI generation to only occur if the vblank flag hasn't been cleared.
*
* Revision 1.74 1999/08/20 02:25:52 nyef
* fixed mapper hsync routine to be called 240 times per frame
*
* Revision 1.73 1999/07/18 01:24:05 nyef
* fixed to use the same flag for writes to both $2005 and $2006
*
* Revision 1.72 1999/06/19 15:58:42 nyef
* rebuilt the palette code
*
* Revision 1.71 1999/06/19 14:54:15 nyef
* cleaned out a lot of dead code
*
* Revision 1.70 1999/06/19 14:10:48 nyef
* added new refresh system
*
* Revision 1.69 1999/05/29 22:31:36 nyef
* started reorganizing the NES PPU
*