Debian Codegraph @

sfc/alt/ppu-balanced/render/cache.cpp

raw
1 #ifdef PPU_CPP
2
3 #define render_bg_tile_line_2bpp(mask) \
4 col = !!(d0 & mask) << 0; \
5 col += !!(d1 & mask) << 1; \
6 *dest++ = col
7
8 #define render_bg_tile_line_4bpp(mask) \
9 col = !!(d0 & mask) << 0; \
10 col += !!(d1 & mask) << 1; \
11 col += !!(d2 & mask) << 2; \
12 col += !!(d3 & mask) << 3; \
13 *dest++ = col
14
15 #define render_bg_tile_line_8bpp(mask) \
16 col = !!(d0 & mask) << 0; \
17 col += !!(d1 & mask) << 1; \
18 col += !!(d2 & mask) << 2; \
19 col += !!(d3 & mask) << 3; \
20 col += !!(d4 & mask) << 4; \
21 col += !!(d5 & mask) << 5; \
22 col += !!(d6 & mask) << 6; \
23 col += !!(d7 & mask) << 7; \
24 *dest++ = col
25
26 template<unsigned color_depth>
27 void PPU::render_bg_tile(uint16 tile_num) {
28 uint8 col, d0, d1, d2, d3, d4, d5, d6, d7;
29
30 if(color_depth == COLORDEPTH_4) {
31 uint8* dest = (uint8*)bg_tiledata[TILE_2BIT] + tile_num * 64;
32 unsigned pos = tile_num * 16;
33 unsigned y = 8;
34 while(y--) {
35 d0 = vram[pos ];
36 d1 = vram[pos + 1];
37 render_bg_tile_line_2bpp(0x80);
38 render_bg_tile_line_2bpp(0x40);
39 render_bg_tile_line_2bpp(0x20);
40 render_bg_tile_line_2bpp(0x10);
41 render_bg_tile_line_2bpp(0x08);
42 render_bg_tile_line_2bpp(0x04);
43 render_bg_tile_line_2bpp(0x02);
44 render_bg_tile_line_2bpp(0x01);
45 pos += 2;
46 }
47 bg_tiledata_state[TILE_2BIT][tile_num] = 0;
48 }
49
50 if(color_depth == COLORDEPTH_16) {
51 uint8* dest = (uint8*)bg_tiledata[TILE_4BIT] + tile_num * 64;
52 unsigned pos = tile_num * 32;
53 unsigned y = 8;
54 while(y--) {
55 d0 = vram[pos ];
56 d1 = vram[pos + 1];
57 d2 = vram[pos + 16];
58 d3 = vram[pos + 17];
59 render_bg_tile_line_4bpp(0x80);
60 render_bg_tile_line_4bpp(0x40);
61 render_bg_tile_line_4bpp(0x20);
62 render_bg_tile_line_4bpp(0x10);
63 render_bg_tile_line_4bpp(0x08);
64 render_bg_tile_line_4bpp(0x04);
65 render_bg_tile_line_4bpp(0x02);
66 render_bg_tile_line_4bpp(0x01);
67 pos += 2;
68 }
69 bg_tiledata_state[TILE_4BIT][tile_num] = 0;
70 }
71
72 if(color_depth == COLORDEPTH_256) {
73 uint8* dest = (uint8*)bg_tiledata[TILE_8BIT] + tile_num * 64;
74 unsigned pos = tile_num * 64;
75 unsigned y = 8;
76 while(y--) {
77 d0 = vram[pos ];
78 d1 = vram[pos + 1];
79 d2 = vram[pos + 16];
80 d3 = vram[pos + 17];
81 d4 = vram[pos + 32];
82 d5 = vram[pos + 33];
83 d6 = vram[pos + 48];
84 d7 = vram[pos + 49];
85 render_bg_tile_line_8bpp(0x80);
86 render_bg_tile_line_8bpp(0x40);
87 render_bg_tile_line_8bpp(0x20);
88 render_bg_tile_line_8bpp(0x10);
89 render_bg_tile_line_8bpp(0x08);
90 render_bg_tile_line_8bpp(0x04);
91 render_bg_tile_line_8bpp(0x02);
92 render_bg_tile_line_8bpp(0x01);
93 pos += 2;
94 }
95 bg_tiledata_state[TILE_8BIT][tile_num] = 0;
96 }
97 }
98
99 #undef render_bg_tile_line_2bpp
100 #undef render_bg_tile_line_4bpp
101 #undef render_bg_tile_line_8bpp
102
103 void PPU::flush_pixel_cache() {
104 uint16 main = get_palette(0);
105 uint16 sub = (regs.pseudo_hires || regs.bg_mode == 5 || regs.bg_mode == 6)
106 ? main
107 : regs.color_rgb;
108
109 unsigned i = 255;
110 do {
111 pixel_cache[i].src_main = main;
112 pixel_cache[i].src_sub = sub;
113 pixel_cache[i].bg_main = BACK;
114 pixel_cache[i].bg_sub = BACK;
115 pixel_cache[i].ce_main = false;
116 pixel_cache[i].ce_sub = false;
117 pixel_cache[i].pri_main = 0;
118 pixel_cache[i].pri_sub = 0;
119 } while(i--);
120 }
121
122 void PPU::alloc_tiledata_cache() {
123 bg_tiledata[TILE_2BIT] = new uint8_t[262144]();
124 bg_tiledata[TILE_4BIT] = new uint8_t[131072]();
125 bg_tiledata[TILE_8BIT] = new uint8_t[ 65536]();
126 bg_tiledata_state[TILE_2BIT] = new uint8_t[ 4096]();
127 bg_tiledata_state[TILE_4BIT] = new uint8_t[ 2048]();
128 bg_tiledata_state[TILE_8BIT] = new uint8_t[ 1024]();
129 }
130
131 //marks all tiledata cache entries as dirty
132 void PPU::flush_tiledata_cache() {
133 for(unsigned i = 0; i < 4096; i++) bg_tiledata_state[TILE_2BIT][i] = 1;
134 for(unsigned i = 0; i < 2048; i++) bg_tiledata_state[TILE_4BIT][i] = 1;
135 for(unsigned i = 0; i < 1024; i++) bg_tiledata_state[TILE_8BIT][i] = 1;
136 }
137
138 void PPU::free_tiledata_cache() {
139 delete[] bg_tiledata[TILE_2BIT];
140 delete[] bg_tiledata[TILE_4BIT];
141 delete[] bg_tiledata[TILE_8BIT];
142 delete[] bg_tiledata_state[TILE_2BIT];
143 delete[] bg_tiledata_state[TILE_4BIT];
144 delete[] bg_tiledata_state[TILE_8BIT];
145 }
146
147 #endif