src/Core/display.c

filter: Statementstmt 45.2% 654 / 1447 Branchbran 30.1% 378 / 1254 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 38.2% 1032 / 2701 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/Core/joypad.c ↑ Summary src/Core/sm83_cpu.c »
1 #include <stdbool.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <string.h>
5 #include <math.h>
6 #include "gb.h"
7  
8 const GB_palette_t GB_PALETTE_GREY = {{{0x00, 0x00, 0x00}, {0x55, 0x55, 0x55}, {0xAA, 0xAA, 0xAA}, {0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF}}};
9 const GB_palette_t GB_PALETTE_DMG = {{{0x08, 0x18, 0x10}, {0x39, 0x61, 0x39}, {0x84, 0xA5, 0x63}, {0xC6, 0xDE, 0x8C}, {0xD2, 0xE6, 0xA6}}};
10 const GB_palette_t GB_PALETTE_MGB = {{{0x07, 0x10, 0x0E}, {0x3A, 0x4C, 0x3A}, {0x81, 0x8D, 0x66}, {0xC2, 0xCE, 0x93}, {0xCF, 0xDA, 0xAC}}};
11 const GB_palette_t GB_PALETTE_GBL = {{{0x0A, 0x1C, 0x15}, {0x35, 0x78, 0x62}, {0x56, 0xB4, 0x95}, {0x7F, 0xE2, 0xC3}, {0x91, 0xEA, 0xD0}}};
12  
13 20 void GB_update_dmg_palette(GB_gameboy_t *gb)
14 {
15 20 ▶ const GB_palette_t *palette = gb->dmg_palette ?: &GB_PALETTE_GREY;
Branch: 1 branch
BranchTrueFalseTotal
const GB_palette_t *palette = gb->dmg_palette ?: &GB_PALETTE_GREY; 1 19 20
16 20 ▶ if (gb->rgb_encode_callback && !GB_is_cgb(gb)) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->rgb_encode_callback && !GB_is_cgb(gb)) { 17 3 20
if (gb->rgb_encode_callback && !GB_is_cgb(gb)) { 4 13 17
17 4 gb->object_palettes_rgb[4] = gb->object_palettes_rgb[0] = gb->background_palettes_rgb[0] =
18 4 gb->rgb_encode_callback(gb, palette->colors[3].r, palette->colors[3].g, palette->colors[3].b);
19 4 gb->object_palettes_rgb[5] = gb->object_palettes_rgb[1] = gb->background_palettes_rgb[1] =
20 4 gb->rgb_encode_callback(gb, palette->colors[2].r, palette->colors[2].g, palette->colors[2].b);
21 4 gb->object_palettes_rgb[6] = gb->object_palettes_rgb[2] = gb->background_palettes_rgb[2] =
22 4 gb->rgb_encode_callback(gb, palette->colors[1].r, palette->colors[1].g, palette->colors[1].b);
23 4 gb->object_palettes_rgb[7] = gb->object_palettes_rgb[3] = gb->background_palettes_rgb[3] =
24 4 gb->rgb_encode_callback(gb, palette->colors[0].r, palette->colors[0].g, palette->colors[0].b);
25
26 // LCD off color
27 4 gb->background_palettes_rgb[4] =
28 4 gb->rgb_encode_callback(gb, palette->colors[4].r, palette->colors[4].g, palette->colors[4].b);
29 }
30 20 }
31  
32 1 void GB_set_palette(GB_gameboy_t *gb, const GB_palette_t *palette)
33 {
34 1 gb->dmg_palette = palette;
35 1 GB_update_dmg_palette(gb);
36 1 }
37  
38 0 const GB_palette_t *GB_get_palette(GB_gameboy_t *gb)
39 {
40 0 return gb->dmg_palette;
41 }
42  
43 16 void GB_set_vblank_callback(GB_gameboy_t *gb, GB_vblank_callback_t callback)
44 {
45 if (!callback) {
46 GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
47 }
48 16 gb->vblank_callback = callback;
49 16 }
50  
51 16 void GB_set_rgb_encode_callback(GB_gameboy_t *gb, GB_rgb_encode_callback_t callback)
52 {
53 if (!callback) {
54 GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
55 }
56
57 16 gb->rgb_encode_callback = callback;
58 16 GB_update_dmg_palette(gb);
59
60 528 ▶ for (unsigned i = 0; i < 32; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 0; i < 32; i++) { 512 16 528
61 512 GB_palette_changed(gb, true, i * 2);
62 512 GB_palette_changed(gb, false, i * 2);
63 }
64 16 }
65  
66 16 void GB_set_pixels_output(GB_gameboy_t *gb, uint32_t *output)
67 {
68 GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb)
69 16 gb->screen = output;
70 16 }
71  
72 /* FIFO functions */
73  
74 12695781 static inline unsigned fifo_size(GB_fifo_t *fifo)
75 {
76 12695781 return fifo->size;
77 }
78  
79 63648 static void fifo_clear(GB_fifo_t *fifo)
80 {
81 63648 fifo->read_end = fifo->size = 0;
82 63648 }
83  
84 5345592 static const GB_fifo_item_t *fifo_pop(GB_fifo_t *fifo)
85 {
86 assert(fifo->size);
87 assert(fifo->size <= 8);
88 5345592 GB_fifo_item_t *ret = &fifo->fifo[fifo->read_end];
89 5345592 fifo->read_end++;
90 5345592 fifo->read_end &= (GB_FIFO_LENGTH - 1);
91 5345592 fifo->size--;
92 5345592 return ret;
93 }
94  
95 700023 static void fifo_push_bg_row(GB_fifo_t *fifo, uint8_t lower, uint8_t upper, uint8_t palette, bool bg_priority, bool flip_x)
96 {
97 assert(fifo->size == 0);
98 700023 fifo->size = 8;
99 700023 ▶ if (!flip_x) {
Branch: 1 branch
BranchTrueFalseTotal
if (!flip_x) { 700023 0 700023
100 6300207 ▶ unrolled for (unsigned i = 0; i < 8; i++) {
Branch: 1 branch
BranchTrueFalseTotal
unrolled for (unsigned i = 0; i < 8; i++) { 5600184 700023 6300207
101 5600184 fifo->fifo[i] = (GB_fifo_item_t) {
102 5600184 (lower >> 7) | ((upper >> 7) << 1),
103 palette,
104 0,
105 bg_priority,
106 };
107 5600184 lower <<= 1;
108 5600184 upper <<= 1;
109 }
110 }
111 else {
112 0 ▶ unrolled for (unsigned i = 0; i < 8; i++) {
Branch: 1 branch
BranchTrueFalseTotal
unrolled for (unsigned i = 0; i < 8; i++) { 0 0 0
113 0 fifo->fifo[i] = (GB_fifo_item_t) {
114 0 (lower & 1) | ((upper & 1) << 1),
115 palette,
116 0,
117 bg_priority,
118 };
119 0 lower >>= 1;
120 0 upper >>= 1;
121 }
122 }
123 700023 }
124  
125 0 static void fifo_overlay_object_row(GB_fifo_t *fifo, uint8_t lower, uint8_t upper, uint8_t palette, bool bg_priority, uint8_t priority, bool flip_x)
126 {
127 0 ▶ while (fifo->size < GB_FIFO_LENGTH) {
Branch: 1 branch
BranchTrueFalseTotal
while (fifo->size < GB_FIFO_LENGTH) { 0 0 0
128 0 fifo->fifo[(fifo->read_end + fifo->size) & (GB_FIFO_LENGTH - 1)] = (GB_fifo_item_t) {0,};
129 0 fifo->size++;
130 }
131
132 0 ▶ uint8_t flip_xor = flip_x? 0: 0x7;
Branch: 1 branch
BranchTrueFalseTotal
uint8_t flip_xor = flip_x? 0: 0x7; 0 0 0
133
134 0 ▶ unrolled for (unsigned i = 8; i--;) {
Branch: 1 branch
BranchTrueFalseTotal
unrolled for (unsigned i = 8; i--;) { 0 0 0
135 0 uint8_t pixel = (lower >> 7) | ((upper >> 7) << 1);
136 0 GB_fifo_item_t *target = &fifo->fifo[(fifo->read_end + (i ^ flip_xor)) & (GB_FIFO_LENGTH - 1)];
137 0 ▶ if (pixel != 0 && (target->pixel == 0 || target->priority > priority)) {
Branch: 3 branches
BranchTrueFalseTotal
if (pixel != 0 && (target->pixel == 0 || target->priority > priority)) { 0 0 0
if (pixel != 0 && (target->pixel == 0 || target->priority > priority)) { 0 0 0
if (pixel != 0 && (target->pixel == 0 || target->priority > priority)) { 0 0 0
138 0 target->pixel = pixel;
139 0 target->palette = palette;
140 0 target->bg_priority = bg_priority;
141 0 target->priority = priority;
142 }
143 0 lower <<= 1;
144 0 upper <<= 1;
145 }
146 0 }
147  
148  
149 /*
150 Each line is 456 cycles. Without scrolling, objects or a window:
151 Mode 2 - 80 cycles / OAM Transfer
152 Mode 3 - 172 cycles / Rendering
153 Mode 0 - 204 cycles / HBlank
154
155 Mode 1 is VBlank
156 */
157  
158 #define MODE2_LENGTH (80)
159 #define LINE_LENGTH (456)
160 #define LINES (144)
161 #define WIDTH (160)
162 #define BORDERED_WIDTH 256
163 #define BORDERED_HEIGHT 224
164 #define VIRTUAL_LINES (LCDC_PERIOD / LINE_LENGTH) // = 154
165  
166 typedef struct __attribute__((packed)) {
167 uint8_t y;
168 uint8_t x;
169 uint8_t tile;
170 uint8_t flags;
171 } object_t;
172  
173 291 void GB_display_vblank(GB_gameboy_t *gb, GB_vblank_type_t type)
174 {
175 291 gb->vblank_just_occured = true;
176 291 gb->cycles_since_vblank_callback = 0;
177 291 gb->lcd_disabled_outside_of_vblank = false;
178
179 #ifndef GB_DISABLE_DEBUGGER
180 gb->last_frame_idle_cycles = gb->current_frame_idle_cycles;
181 gb->last_frame_busy_cycles = gb->current_frame_busy_cycles;
182 gb->current_frame_idle_cycles = 0;
183 gb->current_frame_busy_cycles = 0;
184
185 if (gb->usage_frame_count++ == 60) {
186 gb->last_second_idle_cycles = gb->current_second_idle_cycles;
187 gb->last_second_busy_cycles = gb->current_second_busy_cycles;
188 gb->current_second_idle_cycles = 0;
189 gb->current_second_busy_cycles = 0;
190 gb->usage_frame_count = 0;
191 }
192 #endif
193
194 /* TODO: Slow in turbo mode! */
195 291 ▶ if (GB_is_hle_sgb(gb)) {
Branch: 1 branch
BranchTrueFalseTotal
if (GB_is_hle_sgb(gb)) { 0 291 291
196 0 GB_sgb_render(gb, type != GB_VBLANK_TYPE_NORMAL_FRAME);
197 }
198
199 291 ▶ if (gb->turbo) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->turbo) { 291 0 291
200 #ifndef GB_DISABLE_DEBUGGER
201 if (unlikely(gb->backstep_instructions)) return;
202 #endif
203 291 ▶ if (GB_timing_sync_turbo(gb)) {
Branch: 1 branch
BranchTrueFalseTotal
if (GB_timing_sync_turbo(gb)) { 0 291 291
204 0 ▶ if (gb->vblank_callback && gb->enable_skipped_frame_vblank_callbacks) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->vblank_callback && gb->enable_skipped_frame_vblank_callbacks) { 0 0 0
if (gb->vblank_callback && gb->enable_skipped_frame_vblank_callbacks) { 0 0 0
205 0 gb->vblank_callback(gb, GB_VBLANK_TYPE_SKIPPED_FRAME);
206 }
207 0 return;
208 }
209 }
210
211 291 ▶ if (GB_is_cgb(gb) && type == GB_VBLANK_TYPE_NORMAL_FRAME && gb->frame_repeat_countdown > 0 && gb->frame_skip_state == GB_FRAMESKIP_LCD_TURNED_ON) {
Branch: 4 branches
BranchTrueFalseTotal
if (GB_is_cgb(gb) && type == GB_VBLANK_TYPE_NORMAL_FRAME && gb->frame_repeat_countdown > 0 && gb->frame_skip_state == GB_FRAMESKIP_LCD_TURNED_ON) { 261 30 291
if (GB_is_cgb(gb) && type == GB_VBLANK_TYPE_NORMAL_FRAME && gb->frame_repeat_countdown > 0 && gb->frame_skip_state == GB_FRAMESKIP_LCD_TURNED_ON) { 195 66 261
if (GB_is_cgb(gb) && type == GB_VBLANK_TYPE_NORMAL_FRAME && gb->frame_repeat_countdown > 0 && gb->frame_skip_state == GB_FRAMESKIP_LCD_TURNED_ON) { 190 5 195
if (GB_is_cgb(gb) && type == GB_VBLANK_TYPE_NORMAL_FRAME && gb->frame_repeat_countdown > 0 && gb->frame_skip_state == GB_FRAMESKIP_LCD_TURNED_ON) { 0 190 190
212 0 GB_handle_rumble(gb);
213
214 0 ▶ if (gb->vblank_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->vblank_callback) { 0 0 0
215 0 gb->vblank_callback(gb, GB_VBLANK_TYPE_REPEAT);
216 }
217 0 GB_timing_sync(gb);
218 0 return;
219 }
220
221 291 ▶ bool is_ppu_stopped = !GB_is_cgb(gb) && gb->stopped && gb->io_registers[GB_IO_LCDC] & GB_LCDC_ENABLE;
Branch: 3 branches
BranchTrueFalseTotal
bool is_ppu_stopped = !GB_is_cgb(gb) && gb->stopped && gb->io_registers[GB_IO_LCDC] & GB_LCDC_ENABLE; 30 261 291
bool is_ppu_stopped = !GB_is_cgb(gb) && gb->stopped && gb->io_registers[GB_IO_LCDC] & GB_LCDC_ENABLE; 0 30 30
bool is_ppu_stopped = !GB_is_cgb(gb) && gb->stopped && gb->io_registers[GB_IO_LCDC] & GB_LCDC_ENABLE; 0 0 0
222
223 291 ▶ if (!gb->disable_rendering && ((!(gb->io_registers[GB_IO_LCDC] & GB_LCDC_ENABLE) || is_ppu_stopped) || gb->frame_skip_state == GB_FRAMESKIP_LCD_TURNED_ON)) {
Branch: 4 branches
BranchTrueFalseTotal
if (!gb->disable_rendering && ((!(gb->io_registers[GB_IO_LCDC] & GB_LCDC_ENABLE) || is_ppu_stopped) || gb->frame_skip_state == GB_FRAMESKIP_LCD_TURNED_ON)) { 291 0 291
if (!gb->disable_rendering && ((!(gb->io_registers[GB_IO_LCDC] & GB_LCDC_ENABLE) || is_ppu_stopped) || gb->frame_skip_state == GB_FRAMESKIP_LCD_TURNED_ON)) { 221 70 291
if (!gb->disable_rendering && ((!(gb->io_registers[GB_IO_LCDC] & GB_LCDC_ENABLE) || is_ppu_stopped) || gb->frame_skip_state == GB_FRAMESKIP_LCD_TURNED_ON)) { 221 0 221
if (!gb->disable_rendering && ((!(gb->io_registers[GB_IO_LCDC] & GB_LCDC_ENABLE) || is_ppu_stopped) || gb->frame_skip_state == GB_FRAMESKIP_LCD_TURNED_ON)) { 6 215 221
224 /* LCD is off, set screen to white or black (if LCD is on in stop mode) */
225 76 ▶ if (!GB_is_sgb(gb)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!GB_is_sgb(gb)) { 76 0 76
226 76 uint32_t color = 0;
227 76 ▶ if (GB_is_cgb(gb)) {
Branch: 1 branch
BranchTrueFalseTotal
if (GB_is_cgb(gb)) { 71 5 76
228 71 color = GB_convert_rgb15(gb, 0x7FFF, false);
229 }
230 else {
231 5 color = is_ppu_stopped ?
232 5 ▶ gb->background_palettes_rgb[0] :
Branch: 1 branch
BranchTrueFalseTotal
gb->background_palettes_rgb[0] : 0 5 5
233 gb->background_palettes_rgb[4];
234 }
235 76 ▶ if (gb->border_mode == GB_BORDER_ALWAYS) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->border_mode == GB_BORDER_ALWAYS) { 0 76 76
236 0 ▶ for (unsigned y = 0; y < LINES; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned y = 0; y < LINES; y++) { 0 0 0
237 0 ▶ for (unsigned x = 0; x < WIDTH; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned x = 0; x < WIDTH; x++) { 0 0 0
238 0 gb ->screen[x + y * BORDERED_WIDTH + (BORDERED_WIDTH - WIDTH) / 2 + (BORDERED_HEIGHT - LINES) / 2 * BORDERED_WIDTH] = color;
239 }
240 }
241 }
242 else {
243 1751116 ▶ for (unsigned i = 0; i < WIDTH * LINES; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 0; i < WIDTH * LINES; i++) { 1751040 76 1751116
244 1751040 gb ->screen[i] = color;
245 }
246 }
247 }
248 }
249
250 291 ▶ if (!gb->disable_rendering && gb->border_mode == GB_BORDER_ALWAYS && !GB_is_sgb(gb)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!gb->disable_rendering && gb->border_mode == GB_BORDER_ALWAYS && !GB_is_sgb(gb)) { 291 0 291
if (!gb->disable_rendering && gb->border_mode == GB_BORDER_ALWAYS && !GB_is_sgb(gb)) { 0 291 291
if (!gb->disable_rendering && gb->border_mode == GB_BORDER_ALWAYS && !GB_is_sgb(gb)) { 0 0 0
251 0 GB_borrow_sgb_border(gb);
252 uint32_t border_colors[16 * 4];
253
254 0 ▶ if (!gb->has_sgb_border && GB_is_cgb(gb) && gb->model <= GB_MODEL_CGB_E) {
Branch: 3 branches
BranchTrueFalseTotal
if (!gb->has_sgb_border && GB_is_cgb(gb) && gb->model <= GB_MODEL_CGB_E) { 0 0 0
if (!gb->has_sgb_border && GB_is_cgb(gb) && gb->model <= GB_MODEL_CGB_E) { 0 0 0
if (!gb->has_sgb_border && GB_is_cgb(gb) && gb->model <= GB_MODEL_CGB_E) { 0 0 0
255 0 uint16_t colors[] = {
256 0x2095, 0x5129, 0x1EAF, 0x1EBA, 0x4648,
257 0x30DA, 0x69AD, 0x2B57, 0x2B5D, 0x632C,
258 0x1050, 0x3C84, 0x0E07, 0x0E18, 0x2964,
259 };
260 0 ▶ unsigned index = gb->rom? gb->rom[0x14E] % 5 : 0;
Branch: 1 branch
BranchTrueFalseTotal
unsigned index = gb->rom? gb->rom[0x14E] % 5 : 0; 0 0 0
261 0 ▶ if (gb->model == GB_MODEL_CGB_0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->model == GB_MODEL_CGB_0) { 0 0 0
262 0 index = 1; // CGB 0 was only available in indigo!
263 }
264 0 ▶ else if (gb->model == GB_MODEL_CGB_A) {
Branch: 1 branch
BranchTrueFalseTotal
else if (gb->model == GB_MODEL_CGB_A) { 0 0 0
265 0 index = 0; // CGB A was only available in red!
266 }
267 0 gb->borrowed_border.palette[0] = LE16(colors[index]);
268 0 gb->borrowed_border.palette[10] = LE16(colors[5 + index]);
269 0 gb->borrowed_border.palette[14] = LE16(colors[10 + index]);
270  
271 }
272
273 0 ▶ for (unsigned i = 0; i < 16 * 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 0; i < 16 * 4; i++) { 0 0 0
274 0 border_colors[i] = GB_convert_rgb15(gb, LE16(gb->borrowed_border.palette[i]), true);
275 }
276
277 0 ▶ for (unsigned tile_y = 0; tile_y < 28; tile_y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned tile_y = 0; tile_y < 28; tile_y++) { 0 0 0
278 0 ▶ for (unsigned tile_x = 0; tile_x < 32; tile_x++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned tile_x = 0; tile_x < 32; tile_x++) { 0 0 0
279 0 ▶ if (tile_x >= 6 && tile_x < 26 && tile_y >= 5 && tile_y < 23) {
Branch: 4 branches
BranchTrueFalseTotal
if (tile_x >= 6 && tile_x < 26 && tile_y >= 5 && tile_y < 23) { 0 0 0
if (tile_x >= 6 && tile_x < 26 && tile_y >= 5 && tile_y < 23) { 0 0 0
if (tile_x >= 6 && tile_x < 26 && tile_y >= 5 && tile_y < 23) { 0 0 0
if (tile_x >= 6 && tile_x < 26 && tile_y >= 5 && tile_y < 23) { 0 0 0
280 0 continue;
281 }
282 0 uint16_t tile = LE16(gb->borrowed_border.map[tile_x + tile_y * 32]);
283 0 ▶ uint8_t flip_x = (tile & 0x4000)? 0:7;
Branch: 1 branch
BranchTrueFalseTotal
uint8_t flip_x = (tile & 0x4000)? 0:7; 0 0 0
284 0 ▶ uint8_t flip_y = (tile & 0x8000)? 7:0;
Branch: 1 branch
BranchTrueFalseTotal
uint8_t flip_y = (tile & 0x8000)? 7:0; 0 0 0
285 0 uint8_t palette = (tile >> 10) & 3;
286 0 ▶ for (unsigned y = 0; y < 8; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned y = 0; y < 8; y++) { 0 0 0
287 0 unsigned base = (tile & 0xFF) * 32 + (y ^ flip_y) * 2;
288 0 ▶ for (unsigned x = 0; x < 8; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned x = 0; x < 8; x++) { 0 0 0
289 0 uint8_t bit = 1 << (x ^ flip_x);
290 0 uint8_t color = ((gb->borrowed_border.tiles[base] & bit) ? 1 : 0) |
291 0 ▶ ((gb->borrowed_border.tiles[base + 1] & bit) ? 2 : 0) |
Branch: 1 branch
BranchTrueFalseTotal
((gb->borrowed_border.tiles[base + 1] & bit) ? 2 : 0) | 0 0 0
292 0 ▶ ((gb->borrowed_border.tiles[base + 16] & bit) ? 4 : 0) |
Branch: 1 branch
BranchTrueFalseTotal
((gb->borrowed_border.tiles[base + 16] & bit) ? 4 : 0) | 0 0 0
293 0 ▶ ((gb->borrowed_border.tiles[base + 17] & bit) ? 8 : 0);
Branch: 1 branch
BranchTrueFalseTotal
((gb->borrowed_border.tiles[base + 17] & bit) ? 8 : 0); 0 0 0
294 0 uint32_t *output = gb->screen + tile_x * 8 + x + (tile_y * 8 + y) * 256;
295 0 ▶ if (color == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (color == 0) { 0 0 0
296 0 *output = border_colors[0];
297 }
298 else {
299 0 *output = border_colors[color + palette * 16];
300 }
301 }
302 }
303 }
304 }
305 }
306 291 GB_handle_rumble(gb);
307  
308 291 ▶ if (gb->vblank_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->vblank_callback) { 291 0 291
309 291 gb->vblank_callback(gb, type);
310 }
311 291 GB_timing_sync(gb);
312 }
313  
314 0 static inline void temperature_tint(double temperature, double *r, double *g, double *b)
315 {
316 0 ▶ if (temperature >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (temperature >= 0) { 0 0 0
317 0 *r = 1;
318 0 *g = pow(1 - temperature, 0.375);
319 0 ▶ if (temperature >= 0.75) {
Branch: 1 branch
BranchTrueFalseTotal
if (temperature >= 0.75) { 0 0 0
320 0 *b = 0;
321 }
322 else {
323 0 *b = sqrt(0.75 - temperature) / sqrt(0.75);
324 }
325 }
326 else {
327 0 *b = 1;
328 0 double squared = pow(temperature, 2);
329 0 *g = 0.125 * squared + 0.3 * temperature + 1.0;
330 0 *r = 0.21875 * squared + 0.5 * temperature + 1.0;
331 }
332 0 }
333  
334 4245 static inline uint8_t scale_channel(uint8_t x)
335 {
336 4245 return (x << 3) | (x >> 2);
337 }
338  
339 0 static inline uint8_t scale_channel_with_curve(uint8_t x)
340 {
341 0 return inline_const(uint8_t[], {0,6,12,20,28,36,45,56,66,76,88,100,113,125,137,149,161,172,182,192,202,210,218,225,232,238,243,247,250,252,254,255})[x];
342 }
343  
344 0 static inline uint8_t scale_channel_with_curve_agb(uint8_t x)
345 {
346 0 return inline_const(uint8_t[], {0,3,8,14,20,26,33,40,47,54,62,70,78,86,94,103,112,120,129,138,147,157,166,176,185,195,205,215,225,235,245,255})[x];
347 }
348  
349 0 static inline uint8_t scale_channel_with_curve_sgb(uint8_t x)
350 {
351 0 return inline_const(uint8_t[], {0,2,5,9,15,20,27,34,42,50,58,67,76,85,94,104,114,123,133,143,153,163,173,182,192,202,211,220,229,238,247,255})[x];
352 }
353  
354  
355 1415 uint32_t GB_convert_rgb15(GB_gameboy_t *gb, uint16_t color, bool for_border)
356 {
357 1415 uint8_t r = (color) & 0x1F;
358 1415 uint8_t g = (color >> 5) & 0x1F;
359 1415 uint8_t b = (color >> 10) & 0x1F;
360
361 1415 ▶ if (gb->color_correction_mode == GB_COLOR_CORRECTION_DISABLED || (for_border && !gb->has_sgb_border)) {
Branch: 3 branches
BranchTrueFalseTotal
if (gb->color_correction_mode == GB_COLOR_CORRECTION_DISABLED || (for_border && !gb->has_sgb_border)) { 0 1415 1415
if (gb->color_correction_mode == GB_COLOR_CORRECTION_DISABLED || (for_border && !gb->has_sgb_border)) { 0 0 0
if (gb->color_correction_mode == GB_COLOR_CORRECTION_DISABLED || (for_border && !gb->has_sgb_border)) { 0 0 0
362 1415 r = scale_channel(r);
363 1415 g = scale_channel(g);
364 1415 b = scale_channel(b);
365 }
366 0 ▶ else if (GB_is_sgb(gb) || for_border) {
Branch: 2 branches
BranchTrueFalseTotal
else if (GB_is_sgb(gb) || for_border) { 0 0 0
else if (GB_is_sgb(gb) || for_border) { 0 0 0
367 0 r = scale_channel_with_curve_sgb(r);
368 0 g = scale_channel_with_curve_sgb(g);
369 0 b = scale_channel_with_curve_sgb(b);
370 }
371 else {
372 0 bool agb = gb->model > GB_MODEL_CGB_E;
373 0 ▶ r = agb? scale_channel_with_curve_agb(r) : scale_channel_with_curve(r);
Branch: 1 branch
BranchTrueFalseTotal
r = agb? scale_channel_with_curve_agb(r) : scale_channel_with_curve(r); 0 0 0
374 0 ▶ g = agb? scale_channel_with_curve_agb(g) : scale_channel_with_curve(g);
Branch: 1 branch
BranchTrueFalseTotal
g = agb? scale_channel_with_curve_agb(g) : scale_channel_with_curve(g); 0 0 0
375 0 ▶ b = agb? scale_channel_with_curve_agb(b) : scale_channel_with_curve(b);
Branch: 1 branch
BranchTrueFalseTotal
b = agb? scale_channel_with_curve_agb(b) : scale_channel_with_curve(b); 0 0 0
376
377 0 ▶ if (gb->color_correction_mode != GB_COLOR_CORRECTION_CORRECT_CURVES) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->color_correction_mode != GB_COLOR_CORRECTION_CORRECT_CURVES) { 0 0 0
378 uint8_t new_r, new_g, new_b;
379 0 ▶ if (g != b) { // Minor optimization
Branch: 1 branch
BranchTrueFalseTotal
if (g != b) { // Minor optimization 0 0 0
380 0 double gamma = 2.2;
381 0 ▶ if (gb->color_correction_mode < GB_COLOR_CORRECTION_REDUCE_CONTRAST) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->color_correction_mode < GB_COLOR_CORRECTION_REDUCE_CONTRAST) { 0 0 0
382 /* Don't use absolutely gamma-correct mixing for the high-contrast
383 modes, to prevent the blue hues from being too washed out */
384 0 gamma = 1.6;
385 }
386
387 // TODO: Optimze pow out using a LUT
388 0 ▶ if (agb) {
Branch: 1 branch
BranchTrueFalseTotal
if (agb) { 0 0 0
389 0 new_g = round(pow((pow(g / 255.0, gamma) * 5 + pow(b / 255.0, gamma)) / 6, 1 / gamma) * 255);
390 }
391 else {
392 0 new_g = round(pow((pow(g / 255.0, gamma) * 3 + pow(b / 255.0, gamma)) / 4, 1 / gamma) * 255);
393 }
394 }
395 else {
396 0 new_g = g;
397 }
398
399 0 new_r = r;
400 0 new_b = b;
401 0 ▶ if (gb->color_correction_mode == GB_COLOR_CORRECTION_REDUCE_CONTRAST) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->color_correction_mode == GB_COLOR_CORRECTION_REDUCE_CONTRAST) { 0 0 0
402 0 r = new_r;
403 0 g = new_g;
404 0 b = new_b;
405
406 0 new_r = new_r * 15 / 16 + ( g + b) / 32;
407 0 new_g = new_g * 15 / 16 + (r + b) / 32;
408 0 new_b = new_b * 15 / 16 + (r + g ) / 32;
409
410 0 ▶ if (agb) {
Branch: 1 branch
BranchTrueFalseTotal
if (agb) { 0 0 0
411 0 new_r = new_r * (224 - 20) / 255 + 20;
412 0 new_g = new_g * (220 - 18) / 255 + 18;
413 0 new_b = new_b * (216 - 16) / 255 + 16;
414 }
415 else {
416 0 new_r = new_r * (220 - 40) / 255 + 40;
417 0 new_g = new_g * (224 - 36) / 255 + 36;
418 0 new_b = new_b * (216 - 32) / 255 + 32;
419 }
420 }
421 0 ▶ else if (gb->color_correction_mode == GB_COLOR_CORRECTION_LOW_CONTRAST) {
Branch: 1 branch
BranchTrueFalseTotal
else if (gb->color_correction_mode == GB_COLOR_CORRECTION_LOW_CONTRAST) { 0 0 0
422 0 r = new_r;
423 0 g = new_g;
424 0 b = new_b;
425  
426 0 new_r = new_r * 15 / 16 + ( g + b) / 32;
427 0 new_g = new_g * 15 / 16 + (r + b) / 32;
428 0 new_b = new_b * 15 / 16 + (r + g ) / 32;
429
430 0 ▶ if (agb) {
Branch: 1 branch
BranchTrueFalseTotal
if (agb) { 0 0 0
431 0 new_r = new_r * (167 - 27) / 255 + 27;
432 0 new_g = new_g * (165 - 24) / 255 + 24;
433 0 new_b = new_b * (157 - 22) / 255 + 22;
434 }
435 else {
436 0 new_r = new_r * (162 - 45) / 255 + 45;
437 0 new_g = new_g * (167 - 41) / 255 + 41;
438 0 new_b = new_b * (157 - 38) / 255 + 38;
439 }
440 }
441 0 ▶ else if (gb->color_correction_mode == GB_COLOR_CORRECTION_MODERN_BOOST_CONTRAST) {
Branch: 1 branch
BranchTrueFalseTotal
else if (gb->color_correction_mode == GB_COLOR_CORRECTION_MODERN_BOOST_CONTRAST) { 0 0 0
442 0 uint8_t old_max = MAX(r, MAX(g, b));
443 0 uint8_t new_max = MAX(new_r, MAX(new_g, new_b));
444
445 0 ▶ if (new_max != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (new_max != 0) { 0 0 0
446 0 new_r = new_r * old_max / new_max;
447 0 new_g = new_g * old_max / new_max;
448 0 new_b = new_b * old_max / new_max;
449 }
450
451 0 uint8_t old_min = MIN(r, MIN(g, b));
452 0 uint8_t new_min = MIN(new_r, MIN(new_g, new_b));
453
454 0 ▶ if (new_min != 0xFF) {
Branch: 1 branch
BranchTrueFalseTotal
if (new_min != 0xFF) { 0 0 0
455 0 new_r = 0xFF - (0xFF - new_r) * (0xFF - old_min) / (0xFF - new_min);
456 0 new_g = 0xFF - (0xFF - new_g) * (0xFF - old_min) / (0xFF - new_min);
457 0 new_b = 0xFF - (0xFF - new_b) * (0xFF - old_min) / (0xFF - new_min);
458 }
459 }
460 0 r = new_r;
461 0 g = new_g;
462 0 b = new_b;
463 }
464 }
465
466 1415 ▶ if (gb->light_temperature) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->light_temperature) { 0 1415 1415
467 double light_r, light_g, light_b;
468 0 temperature_tint(gb->light_temperature, &light_r, &light_g, &light_b);
469 0 r = round(light_r * r);
470 0 g = round(light_g * g);
471 0 b = round(light_b * b);
472 }
473
474 1415 return gb->rgb_encode_callback(gb, r, g, b);
475 }
476  
477 2368 void GB_palette_changed(GB_gameboy_t *gb, bool background_palette, uint8_t index)
478 {
479 2368 ▶ if (!gb->rgb_encode_callback || !GB_is_cgb(gb)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!gb->rgb_encode_callback || !GB_is_cgb(gb)) return; 1536 832 2368
if (!gb->rgb_encode_callback || !GB_is_cgb(gb)) return; 192 1344 1536
480 1344 ▶ uint8_t *palette_data = background_palette? gb->background_palettes_data : gb->object_palettes_data;
Branch: 1 branch
BranchTrueFalseTotal
uint8_t *palette_data = background_palette? gb->background_palettes_data : gb->object_palettes_data; 832 512 1344
481 1344 uint16_t color = palette_data[index & ~1] | (palette_data[index | 1] << 8);
482  
483 1344 ▶ (background_palette? gb->background_palettes_rgb : gb->object_palettes_rgb)[index / 2] = GB_convert_rgb15(gb, color, false);
Branch: 1 branch
BranchTrueFalseTotal
(background_palette? gb->background_palettes_rgb : gb->object_palettes_rgb)[index / 2] = GB_convert_rgb15(gb, color, false); 832 512 1344
484 }
485  
486 1 void GB_set_color_correction_mode(GB_gameboy_t *gb, GB_color_correction_mode_t mode)
487 {
488 1 gb->color_correction_mode = mode;
489 1 ▶ if (GB_is_cgb(gb)) {
Branch: 1 branch
BranchTrueFalseTotal
if (GB_is_cgb(gb)) { 0 1 1
490 0 ▶ nounroll for (unsigned i = 0; i < 32; i++) {
Branch: 1 branch
BranchTrueFalseTotal
nounroll for (unsigned i = 0; i < 32; i++) { 0 0 0
491 0 GB_palette_changed(gb, false, i * 2);
492 0 GB_palette_changed(gb, true, i * 2);
493 }
494 }
495 1 }
496  
497 0 void GB_set_light_temperature(GB_gameboy_t *gb, double temperature)
498 {
499 0 gb->light_temperature = temperature;
500 0 ▶ if (GB_is_cgb(gb)) {
Branch: 1 branch
BranchTrueFalseTotal
if (GB_is_cgb(gb)) { 0 0 0
501 0 ▶ nounroll for (unsigned i = 0; i < 32; i++) {
Branch: 1 branch
BranchTrueFalseTotal
nounroll for (unsigned i = 0; i < 32; i++) { 0 0 0
502 0 GB_palette_changed(gb, false, i * 2);
503 0 GB_palette_changed(gb, true, i * 2);
504 }
505 }
506 0 }
507  
508 63643 static void wy_check(GB_gameboy_t *gb)
509 {
510 63643 ▶ if (!(gb->io_registers[GB_IO_LCDC] & GB_LCDC_ENABLE)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!(gb->io_registers[GB_IO_LCDC] & GB_LCDC_ENABLE)) return; 1 63642 63643
511
512 63642 uint8_t comparison = gb->current_line;
513 63642 ▶ if ((!GB_is_cgb(gb) || gb->cgb_double_speed) && gb->ly_for_comparison != (uint8_t)-1) {
Branch: 3 branches
BranchTrueFalseTotal
if ((!GB_is_cgb(gb) || gb->cgb_double_speed) && gb->ly_for_comparison != (uint8_t)-1) { 56155 7487 63642
if ((!GB_is_cgb(gb) || gb->cgb_double_speed) && gb->ly_for_comparison != (uint8_t)-1) { 0 56155 56155
if ((!GB_is_cgb(gb) || gb->cgb_double_speed) && gb->ly_for_comparison != (uint8_t)-1) { 7487 0 7487
514 7487 comparison = gb->ly_for_comparison;
515 }
516
517 63642 ▶ if ((gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE) &&
Branch: 1 branch
BranchTrueFalseTotal
if ((gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE) && 0 63642 63642
518 0 ▶ gb->io_registers[GB_IO_WY] == comparison) {
Branch: 1 branch
BranchTrueFalseTotal
gb->io_registers[GB_IO_WY] == comparison) { 0 0 0
519 0 gb->wy_triggered = true;
520 }
521 }
522  
523 164071 void GB_STAT_update(GB_gameboy_t *gb)
524 {
525 164071 ▶ if (!(gb->io_registers[GB_IO_LCDC] & GB_LCDC_ENABLE)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!(gb->io_registers[GB_IO_LCDC] & GB_LCDC_ENABLE)) return; 0 164071 164071
526 164071 ▶ if (GB_is_dma_active(gb) && (gb->io_registers[GB_IO_STAT] & 3) == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (GB_is_dma_active(gb) && (gb->io_registers[GB_IO_STAT] & 3) == 2) { 0 164071 164071
if (GB_is_dma_active(gb) && (gb->io_registers[GB_IO_STAT] & 3) == 2) { 0 0 0
527 0 gb->io_registers[GB_IO_STAT] &= ~3;
528 }
529
530 164071 bool previous_interrupt_line = gb->stat_interrupt_line;
531 /* Set LY=LYC bit */
532 164071 ▶ if (gb->ly_for_comparison != (uint16_t)-1 || (gb->model <= GB_MODEL_CGB_C && !gb->cgb_double_speed)) {
Branch: 3 branches
BranchTrueFalseTotal
if (gb->ly_for_comparison != (uint16_t)-1 || (gb->model <= GB_MODEL_CGB_C && !gb->cgb_double_speed)) { 33784 130287 164071
if (gb->ly_for_comparison != (uint16_t)-1 || (gb->model <= GB_MODEL_CGB_C && !gb->cgb_double_speed)) { 3994 29790 33784
if (gb->ly_for_comparison != (uint16_t)-1 || (gb->model <= GB_MODEL_CGB_C && !gb->cgb_double_speed)) { 3994 0 3994
533 134281 ▶ if (gb->ly_for_comparison == gb->io_registers[GB_IO_LYC]) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->ly_for_comparison == gb->io_registers[GB_IO_LYC]) { 1308 132973 134281
534 1308 gb->lyc_interrupt_line = true;
535 1308 gb->io_registers[GB_IO_STAT] |= 4;
536 }
537 else {
538 132973 ▶ if (gb->ly_for_comparison != (uint16_t)-1) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->ly_for_comparison != (uint16_t)-1) { 128979 3994 132973
539 128979 gb->lyc_interrupt_line = false;
540 }
541 132973 gb->io_registers[GB_IO_STAT] &= ~4;
542 }
543 }
544
545 164071 switch (gb->mode_for_interrupt) {
546 32266 case 0: gb->stat_interrupt_line = gb->io_registers[GB_IO_STAT] & 8; break;
547 4736 case 1: gb->stat_interrupt_line = gb->io_registers[GB_IO_STAT] & 0x10; break;
548 63421 case 2: gb->stat_interrupt_line = gb->io_registers[GB_IO_STAT] & 0x20; break;
549 63648 default: gb->stat_interrupt_line = false;
550 }
551
552 /* User requested a LY=LYC interrupt and the LY=LYC bit is on */
553 164071 ▶ if ((gb->io_registers[GB_IO_STAT] & 0x40) && gb->lyc_interrupt_line) {
Branch: 2 branches
BranchTrueFalseTotal
if ((gb->io_registers[GB_IO_STAT] & 0x40) && gb->lyc_interrupt_line) { 0 164071 164071
if ((gb->io_registers[GB_IO_STAT] & 0x40) && gb->lyc_interrupt_line) { 0 0 0
554 0 gb->stat_interrupt_line = true;
555 }
556
557 164071 ▶ if (gb->stat_interrupt_line && !previous_interrupt_line) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->stat_interrupt_line && !previous_interrupt_line) { 0 164071 164071
if (gb->stat_interrupt_line && !previous_interrupt_line) { 0 0 0
558 0 gb->io_registers[GB_IO_IF] |= 2;
559 }
560 }
561  
562 0 void GB_lcd_off(GB_gameboy_t *gb)
563 {
564 0 gb->cycles_for_line = 0;
565 0 gb->display_state = 0;
566 0 gb->display_cycles = 0;
567 /* When the LCD is disabled, state is constant */
568
569 0 ▶ if (gb->hdma_on_hblank && (gb->io_registers[GB_IO_STAT] & 3)) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->hdma_on_hblank && (gb->io_registers[GB_IO_STAT] & 3)) { 0 0 0
if (gb->hdma_on_hblank && (gb->io_registers[GB_IO_STAT] & 3)) { 0 0 0
570 0 gb->hdma_on = true;
571 }
572
573 /* When the LCD is off, LY is 0 and STAT mode is 0. */
574 0 gb->io_registers[GB_IO_LY] = 0;
575 0 gb->io_registers[GB_IO_STAT] &= ~3;
576
577
578 0 gb->oam_read_blocked = false;
579 0 gb->vram_read_blocked = false;
580 0 gb->oam_write_blocked = false;
581 0 gb->vram_write_blocked = false;
582 0 gb->cgb_palettes_blocked = false;
583
584 0 gb->current_line = 0;
585 0 gb->ly_for_comparison = 0;
586
587 0 gb->accessed_oam_row = -1;
588 0 gb->wy_triggered = false;
589
590 0 ▶ if (unlikely(gb->lcd_line_callback)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(gb->lcd_line_callback)) { 0 0 0
591 0 gb->lcd_line_callback(gb, 0);
592 }
593 0 }
594  
595 2545440 static inline uint8_t oam_read(GB_gameboy_t *gb, uint8_t addr)
596 {
597 2545440 ▶ if (unlikely(gb->oam_ppu_blocked)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(gb->oam_ppu_blocked)) { 0 2545440 2545440
598 0 return 0xFF;
599 }
600 2545440 ▶ if (unlikely(gb->dma_current_dest <= 0xA0 && gb->dma_current_dest > 0)) { // TODO: what happens in the last and first M cycles?
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(gb->dma_current_dest <= 0xA0 && gb->dma_current_dest > 0)) { // TODO: what happens in the last and first M cycles? 0 2545440 2545440
if (unlikely(gb->dma_current_dest <= 0xA0 && gb->dma_current_dest > 0)) { // TODO: what happens in the last and first M cycles? 0 0 0
601 0 ▶ if (gb->hdma_in_progress) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->hdma_in_progress) { 0 0 0
602 0 return GB_read_oam(gb, (gb->hdma_current_src & ~1) | (addr & 1));
603 }
604 0 ▶ if (gb->dma_current_dest != 0xA0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->dma_current_dest != 0xA0) { 0 0 0
605 0 return gb->oam[(gb->dma_current_dest & ~1) | (addr & 1)];
606 }
607 }
608 2545440 return gb->oam[addr];
609 }
610  
611 1272720 static void add_object_from_index(GB_gameboy_t *gb, unsigned index)
612 {
613 1272720 ▶ if (likely(!GB_is_dma_active(gb) || gb->halted || gb->stopped)) {
Branch: 4 branches
BranchTrueFalseTotal
if (likely(!GB_is_dma_active(gb) || gb->halted || gb->stopped)) { 0 1272720 1272720
if (likely(!GB_is_dma_active(gb) || gb->halted || gb->stopped)) { 0 0 0
if (likely(!GB_is_dma_active(gb) || gb->halted || gb->stopped)) { 0 1272720 1272720
if (likely(!GB_is_dma_active(gb) || gb->halted || gb->stopped)) { 0 0 0
614 1272720 gb->mode2_y_bus = oam_read(gb, index * 4);
615 1272720 gb->mode2_x_bus = oam_read(gb, index * 4 + 1);
616 }
617  
618 1272720 ▶ if (unlikely(gb->n_visible_objs == 10)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(gb->n_visible_objs == 10)) return; 0 1272720 1272720
619
620 /* TODO: It appears that DMA blocks PPU access to OAM, but it needs verification. */
621 1272720 ▶ if (unlikely(GB_is_dma_active(gb) && (gb->halted || gb->stopped))) {
Branch: 4 branches
BranchTrueFalseTotal
if (unlikely(GB_is_dma_active(gb) && (gb->halted || gb->stopped))) { 0 1272720 1272720
if (unlikely(GB_is_dma_active(gb) && (gb->halted || gb->stopped))) { 0 0 0
if (unlikely(GB_is_dma_active(gb) && (gb->halted || gb->stopped))) { 0 0 0
if (unlikely(GB_is_dma_active(gb) && (gb->halted || gb->stopped))) { 0 0 0
622 0 ▶ if (gb->model < GB_MODEL_CGB_E) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->model < GB_MODEL_CGB_E) { 0 0 0
623 0 return;
624 }
625 /* CGB-0 to CGB-D: Halted DMA blocks Mode 2;
626 Pre-CGB: Unit specific behavior, some units read FFs, some units read using
627 several different corruption pattterns. For simplicity, we emulate
628 FFs. */
629 }
630
631 1272720 ▶ if (unlikely(gb->oam_ppu_blocked)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(gb->oam_ppu_blocked)) { 0 1272720 1272720
632 0 return;
633 }
634
635 1272720 bool height_16 = (gb->io_registers[GB_IO_LCDC] & GB_LCDC_OBJ_SIZE) != 0;
636 1272720 signed y = gb->mode2_y_bus - 16;
637 /* This reverse sorts the visible objects by location and priority */
638 1272720 ▶ if (y <= gb->current_line && y + (height_16? 16 : 8) > gb->current_line) {
Branch: 3 branches
BranchTrueFalseTotal
if (y <= gb->current_line && y + (height_16? 16 : 8) > gb->current_line) { 1130332 142388 1272720
if (y <= gb->current_line && y + (height_16? 16 : 8) > gb->current_line) { 0 1130332 1130332
if (y <= gb->current_line && y + (height_16? 16 : 8) > gb->current_line) { 1248 1129084 1130332
639 1248 unsigned j = 0;
640 2080 ▶ for (; j < gb->n_visible_objs; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; j < gb->n_visible_objs; j++) { 832 1248 2080
641 832 ▶ if (gb->objects_x[j] <= gb->mode2_x_bus) break;
Branch: 1 branch
BranchTrueFalseTotal
if (gb->objects_x[j] <= gb->mode2_x_bus) break; 0 832 832
642 }
643 1248 memmove(gb->visible_objs + j + 1, gb->visible_objs + j, gb->n_visible_objs - j);
644 1248 memmove(gb->objects_x + j + 1, gb->objects_x + j, gb->n_visible_objs - j);
645 1248 memmove(gb->objects_y + j + 1, gb->objects_y + j, gb->n_visible_objs - j);
646 1248 gb->visible_objs[j] = index;
647 1248 gb->objects_x[j] = gb->mode2_x_bus;
648 1248 gb->objects_y[j] = gb->mode2_y_bus;
649 1248 gb->n_visible_objs++;
650 }
651 }
652  
653 5345592 static void render_pixel_if_possible(GB_gameboy_t *gb)
654 {
655 5345592 const GB_fifo_item_t *fifo_item = NULL;
656 5345592 const GB_fifo_item_t *oam_fifo_item = NULL;
657 5345592 bool draw_oam = false;
658 5345592 bool bg_enabled = true, bg_priority = false;
659
660 // Rendering (including scrolling adjustment) does not occur as long as an object at x=0 is pending
661 5345592 ▶ if (gb->n_visible_objs != 0 &&
Branch: 1 branch
BranchTrueFalseTotal
if (gb->n_visible_objs != 0 && 96512 5249080 5345592
662 96512 ▶ (gb->io_registers[GB_IO_LCDC] & GB_LCDC_OBJ_EN || GB_is_cgb(gb)) &&
Branch: 2 branches
BranchTrueFalseTotal
(gb->io_registers[GB_IO_LCDC] & GB_LCDC_OBJ_EN || GB_is_cgb(gb)) && 96512 0 96512
(gb->io_registers[GB_IO_LCDC] & GB_LCDC_OBJ_EN || GB_is_cgb(gb)) && 0 96512 96512
663 0 ▶ gb->objects_x[gb->n_visible_objs - 1] == 0) {
Branch: 1 branch
BranchTrueFalseTotal
gb->objects_x[gb->n_visible_objs - 1] == 0) { 0 0 0
664 0 return;
665 }
666
667 5345592 ▶ if (unlikely(!fifo_size(&gb->bg_fifo))) return;
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!fifo_size(&gb->bg_fifo))) return; 0 5345592 5345592
668
669 5345592 ▶ if (unlikely(gb->insert_bg_pixel)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(gb->insert_bg_pixel)) { 0 5345592 5345592
670 0 gb->insert_bg_pixel = false;
671 0 fifo_item = ({static const GB_fifo_item_t empty_item = {0,}; &empty_item;});
672 }
673 else {
674 5345592 fifo_item = fifo_pop(&gb->bg_fifo);
675 }
676 5345592 bg_priority = fifo_item->bg_priority;
677
678 5345592 ▶ if (fifo_size(&gb->oam_fifo)) {
Branch: 1 branch
BranchTrueFalseTotal
if (fifo_size(&gb->oam_fifo)) { 0 5345592 5345592
679 0 oam_fifo_item = fifo_pop(&gb->oam_fifo);
680 0 ▶ if (oam_fifo_item->pixel && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_OBJ_EN) && unlikely(!gb->objects_disabled)) {
Branch: 3 branches
BranchTrueFalseTotal
if (oam_fifo_item->pixel && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_OBJ_EN) && unlikely(!gb->objects_disabled)) { 0 0 0
if (oam_fifo_item->pixel && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_OBJ_EN) && unlikely(!gb->objects_disabled)) { 0 0 0
if (oam_fifo_item->pixel && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_OBJ_EN) && unlikely(!gb->objects_disabled)) { 0 0 0
681 0 draw_oam = true;
682 0 bg_priority |= oam_fifo_item->bg_priority;
683 }
684 }
685
686 // (gb->position_in_line + 16 < 8) is (gb->position_in_line < -8) in unsigned logic
687 5345592 ▶ if (((uint8_t)(gb->position_in_line + 16) < 8)) {
Branch: 1 branch
BranchTrueFalseTotal
if (((uint8_t)(gb->position_in_line + 16) < 8)) { 31819 5313773 5345592
688 31819 ▶ if (gb->position_in_line == (uint8_t)-17) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->position_in_line == (uint8_t)-17) { 0 31819 31819
689 0 gb->position_in_line = -16;
690 }
691 31819 ▶ else if ((gb->position_in_line & 7) == (gb->io_registers[GB_IO_SCX] & 7)) {
Branch: 1 branch
BranchTrueFalseTotal
else if ((gb->position_in_line & 7) == (gb->io_registers[GB_IO_SCX] & 7)) { 31819 0 31819
692 31819 gb->position_in_line = -8;
693 }
694 0 ▶ else if (gb->window_is_being_fetched && (gb->position_in_line & 7) == 6 && (gb->io_registers[GB_IO_SCX] & 7) == 7) { // TODO: Why does this happen?
Branch: 3 branches
BranchTrueFalseTotal
else if (gb->window_is_being_fetched && (gb->position_in_line & 7) == 6 && (gb->io_registers[GB_IO_SCX] & 7) == 7) { // TODO: Why does this happen? 0 0 0
else if (gb->window_is_being_fetched && (gb->position_in_line & 7) == 6 && (gb->io_registers[GB_IO_SCX] & 7) == 7) { // TODO: Why does this happen? 0 0 0
else if (gb->window_is_being_fetched && (gb->position_in_line & 7) == 6 && (gb->io_registers[GB_IO_SCX] & 7) == 7) { // TODO: Why does this happen? 0 0 0
695 0 gb->position_in_line = -8;
696 }
697 0 ▶ else if (gb->position_in_line == (uint8_t) -9) {
Branch: 1 branch
BranchTrueFalseTotal
else if (gb->position_in_line == (uint8_t) -9) { 0 0 0
698 0 gb->position_in_line = -16;
699 0 return;
700 }
701 else {
702 0 gb->line_has_fractional_scrolling = true;
703 }
704 }
705  
706 5345592 gb->window_is_being_fetched = false;
707
708 /* Drop pixels for scrollings */
709 5345592 ▶ if (gb->position_in_line >= 160 || (gb->disable_rendering && !gb->sgb)) {
Branch: 3 branches
BranchTrueFalseTotal
if (gb->position_in_line >= 160 || (gb->disable_rendering && !gb->sgb)) { 5091040 254552 5345592
if (gb->position_in_line >= 160 || (gb->disable_rendering && !gb->sgb)) { 0 5091040 5091040
if (gb->position_in_line >= 160 || (gb->disable_rendering && !gb->sgb)) { 0 0 0
710 254552 gb->position_in_line++;
711 254552 return;
712 }
713
714 /* Mixing */
715
716 5091040 ▶ if ((gb->io_registers[GB_IO_LCDC] & GB_LCDC_BG_EN) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((gb->io_registers[GB_IO_LCDC] & GB_LCDC_BG_EN) == 0) { 0 5091040 5091040
717 0 ▶ if (gb->cgb_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->cgb_mode) { 0 0 0
718 0 bg_priority = false;
719 }
720 else {
721 0 bg_enabled = false;
722 }
723 }
724
725 5091040 ▶ if (unlikely(gb->background_disabled)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(gb->background_disabled)) { 0 5091040 5091040
726 0 bg_enabled = false;
727 static const GB_fifo_item_t empty_item = {0,};
728 0 fifo_item = &empty_item;
729 }
730  
731 5091040 uint8_t icd_pixel = 0;
732 5091040 uint32_t *dest = NULL;
733 5091040 ▶ if (!gb->sgb) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gb->sgb) { 5091040 0 5091040
734 5091040 ▶ if (gb->border_mode != GB_BORDER_ALWAYS) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->border_mode != GB_BORDER_ALWAYS) { 5091040 0 5091040
735 5091040 dest = gb->screen + gb->lcd_x + gb->current_line * WIDTH;
736 }
737 else {
738 0 dest = gb->screen + gb->lcd_x + gb->current_line * BORDERED_WIDTH + (BORDERED_WIDTH - WIDTH) / 2 + (BORDERED_HEIGHT - LINES) / 2 * BORDERED_WIDTH;
739 }
740 }
741
742 {
743 5091040 ▶ uint8_t pixel = bg_enabled? fifo_item->pixel : 0;
Branch: 1 branch
BranchTrueFalseTotal
uint8_t pixel = bg_enabled? fifo_item->pixel : 0; 5091040 0 5091040
744 5091040 ▶ if (pixel && bg_priority) {
Branch: 2 branches
BranchTrueFalseTotal
if (pixel && bg_priority) { 310050 4780990 5091040
if (pixel && bg_priority) { 0 310050 310050
745 0 draw_oam = false;
746 }
747 5091040 ▶ if (!gb->cgb_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gb->cgb_mode) { 599040 4492000 5091040
748 599040 pixel = ((gb->io_registers[GB_IO_BGP] >> (pixel << 1)) & 3);
749 }
750 5091040 ▶ if (gb->sgb) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb) { 0 5091040 5091040
751 0 ▶ if (gb->current_lcd_line < LINES) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->current_lcd_line < LINES) { 0 0 0
752 0 ▶ gb->sgb->screen_buffer[gb->lcd_x + gb->current_lcd_line * WIDTH] = gb->stopped? 0 : pixel;
Branch: 1 branch
BranchTrueFalseTotal
gb->sgb->screen_buffer[gb->lcd_x + gb->current_lcd_line * WIDTH] = gb->stopped? 0 : pixel; 0 0 0
753 }
754 }
755 5091040 ▶ else if (gb->model & GB_MODEL_NO_SFC_BIT) {
Branch: 1 branch
BranchTrueFalseTotal
else if (gb->model & GB_MODEL_NO_SFC_BIT) { 0 5091040 5091040
756 0 ▶ if (gb->icd_pixel_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->icd_pixel_callback) { 0 0 0
757 0 icd_pixel = pixel;
758 }
759 }
760 5091040 ▶ else if (gb->cgb_palettes_ppu_blocked) {
Branch: 1 branch
BranchTrueFalseTotal
else if (gb->cgb_palettes_ppu_blocked) { 0 5091040 5091040
761 0 *dest = gb->rgb_encode_callback(gb, 0, 0, 0);
762 }
763 else {
764 5091040 *dest = gb->background_palettes_rgb[fifo_item->palette * 4 + pixel];
765 }
766 }
767
768 5091040 ▶ if (draw_oam) {
Branch: 1 branch
BranchTrueFalseTotal
if (draw_oam) { 0 5091040 5091040
769 0 uint8_t pixel = oam_fifo_item->pixel;
770 0 ▶ if (!gb->cgb_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gb->cgb_mode) { 0 0 0
771 /* Todo: Verify access timings */
772 0 ▶ pixel = ((gb->io_registers[oam_fifo_item->palette? GB_IO_OBP1 : GB_IO_OBP0] >> (pixel << 1)) & 3);
Branch: 1 branch
BranchTrueFalseTotal
pixel = ((gb->io_registers[oam_fifo_item->palette? GB_IO_OBP1 : GB_IO_OBP0] >> (pixel << 1)) & 3); 0 0 0
773 }
774 0 ▶ if (gb->sgb) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb) { 0 0 0
775 0 ▶ if (gb->current_lcd_line < LINES) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->current_lcd_line < LINES) { 0 0 0
776 0 ▶ gb->sgb->screen_buffer[gb->lcd_x + gb->current_lcd_line * WIDTH] = gb->stopped? 0 : pixel;
Branch: 1 branch
BranchTrueFalseTotal
gb->sgb->screen_buffer[gb->lcd_x + gb->current_lcd_line * WIDTH] = gb->stopped? 0 : pixel; 0 0 0
777 }
778 }
779 0 ▶ else if (gb->model & GB_MODEL_NO_SFC_BIT) {
Branch: 1 branch
BranchTrueFalseTotal
else if (gb->model & GB_MODEL_NO_SFC_BIT) { 0 0 0
780 0 ▶ if (gb->icd_pixel_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->icd_pixel_callback) { 0 0 0
781 0 icd_pixel = pixel;
782 }
783 }
784 0 ▶ else if (gb->cgb_palettes_ppu_blocked) {
Branch: 1 branch
BranchTrueFalseTotal
else if (gb->cgb_palettes_ppu_blocked) { 0 0 0
785 0 *dest = gb->rgb_encode_callback(gb, 0, 0, 0);
786 }
787 else {
788 0 *dest = gb->object_palettes_rgb[oam_fifo_item->palette * 4 + pixel];
789 }
790 }
791
792 5091040 ▶ if (gb->model & GB_MODEL_NO_SFC_BIT) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->model & GB_MODEL_NO_SFC_BIT) { 0 5091040 5091040
793 0 ▶ if (gb->icd_pixel_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->icd_pixel_callback) { 0 0 0
794 0 gb->icd_pixel_callback(gb, icd_pixel);
795 }
796 }
797
798 5091040 gb->position_in_line++;
799 5091040 gb->lcd_x++;
800 }
801  
802 2004597 static inline void dma_sync(GB_gameboy_t *gb, unsigned *cycles)
803 {
804 2004597 ▶ if (unlikely(GB_is_dma_active(gb))) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(GB_is_dma_active(gb))) { 0 2004597 2004597
805 0 unsigned offset = *cycles - gb->display_cycles; // Time passed in 8MHz ticks
806 0 ▶ if (offset) {
Branch: 1 branch
BranchTrueFalseTotal
if (offset) { 0 0 0
807 0 *cycles = gb->display_cycles;
808 0 ▶ if (!gb->cgb_double_speed) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gb->cgb_double_speed) { 0 0 0
809 0 offset >>= 1; // Convert to T-cycles
810 }
811 0 unsigned old = gb->dma_cycles;
812 0 gb->dma_cycles = offset;
813 0 GB_dma_run(gb);
814 0 gb->dma_cycles = old - offset;
815 }
816 }
817 2004597 }
818  
819 825447 static inline uint8_t fetcher_y(GB_gameboy_t *gb)
820 {
821 825447 ▶ return gb->wx_triggered? gb->window_y : gb->current_line + gb->io_registers[GB_IO_SCY];
Branch: 1 branch
BranchTrueFalseTotal
return gb->wx_triggered? gb->window_y : gb->current_line + gb->io_registers[GB_IO_SCY]; 0 825447 825447
822 }
823  
824 2594172 static inline uint8_t vram_read(GB_gameboy_t *gb, uint16_t addr)
825 {
826 2594172 ▶ if (unlikely(gb->vram_ppu_blocked)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(gb->vram_ppu_blocked)) { 0 2594172 2594172
827 0 return 0xFF;
828 }
829 2594172 ▶ if (unlikely(gb->hdma_in_progress)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(gb->hdma_in_progress)) { 0 2594172 2594172
830 0 gb->addr_for_hdma_conflict = addr;
831 0 return 0;
832 }
833 // TODO: what if both?
834 2594172 ▶ else if (unlikely(gb->dma_current_dest <= 0xA0 && gb->dma_current_dest > 0 && (gb->dma_current_src & 0xE000) == 0x8000)) { // TODO: what happens in the last and first M cycles?
Branch: 4 branches
BranchTrueFalseTotal
else if (unlikely(gb->dma_current_dest <= 0xA0 && gb->dma_current_dest > 0 && (gb->dma_current_src & 0xE000) == 0x8000)) { // TODO: what happens in the last and first M cycles? 0 2594172 2594172
else if (unlikely(gb->dma_current_dest <= 0xA0 && gb->dma_current_dest > 0 && (gb->dma_current_src & 0xE000) == 0x8000)) { // TODO: what happens in the last and first M cycles? 0 0 0
else if (unlikely(gb->dma_current_dest <= 0xA0 && gb->dma_current_dest > 0 && (gb->dma_current_src & 0xE000) == 0x8000)) { // TODO: what happens in the last and first M cycles? 0 2594172 2594172
else if (unlikely(gb->dma_current_dest <= 0xA0 && gb->dma_current_dest > 0 && (gb->dma_current_src & 0xE000) == 0x8000)) { // TODO: what happens in the last and first M cycles? 0 0 0
835 // DMAing from VRAM!
836 /* TODO: AGS has its own, very different pattern, but AGS is not currently a supported model */
837 /* TODO: Research this when researching odd modes */
838 /* TODO: probably not 100% on the first few reads during halt/stop modes*/
839 0 ▶ unsigned offset = 1 - (gb->halted || gb->stopped);
Branch: 2 branches
BranchTrueFalseTotal
unsigned offset = 1 - (gb->halted || gb->stopped); 0 0 0
unsigned offset = 1 - (gb->halted || gb->stopped); 0 0 0
840 0 ▶ if (GB_is_cgb(gb)) {
Branch: 1 branch
BranchTrueFalseTotal
if (GB_is_cgb(gb)) { 0 0 0
841 0 ▶ if (gb->dma_ppu_vram_conflict) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->dma_ppu_vram_conflict) { 0 0 0
842 0 addr = (gb->dma_ppu_vram_conflict_addr & 0x1FFF) | (addr & 0x2000);
843 }
844 0 ▶ else if (gb->dma_cycles_modulo && !gb->halted && !gb->stopped) {
Branch: 3 branches
BranchTrueFalseTotal
else if (gb->dma_cycles_modulo && !gb->halted && !gb->stopped) { 0 0 0
else if (gb->dma_cycles_modulo && !gb->halted && !gb->stopped) { 0 0 0
else if (gb->dma_cycles_modulo && !gb->halted && !gb->stopped) { 0 0 0
845 0 addr &= 0x2000;
846 0 addr |= ((gb->dma_current_src - offset) & 0x1FFF);
847 }
848 else {
849 0 addr &= 0x2000 | ((gb->dma_current_src - offset) & 0x1FFF);
850 0 gb->dma_ppu_vram_conflict_addr = addr;
851 0 ▶ gb->dma_ppu_vram_conflict = !gb->halted && !gb->stopped;
Branch: 2 branches
BranchTrueFalseTotal
gb->dma_ppu_vram_conflict = !gb->halted && !gb->stopped; 0 0 0
gb->dma_ppu_vram_conflict = !gb->halted && !gb->stopped; 0 0 0
852 }
853 }
854 else {
855 0 addr |= ((gb->dma_current_src - offset) & 0x1FFF);
856 }
857 0 ▶ gb->oam[gb->dma_current_dest - offset] = gb->vram[(addr & 0x1FFF) | (gb->cgb_vram_bank? 0x2000 : 0)];
Branch: 1 branch
BranchTrueFalseTotal
gb->oam[gb->dma_current_dest - offset] = gb->vram[(addr & 0x1FFF) | (gb->cgb_vram_bank? 0x2000 : 0)]; 0 0 0
858 }
859 2594172 return gb->vram[addr];
860 }
861  
862 typedef enum {
863 /* VRAM reads take 2 T-cycles. In read address is determined in the first
864 cycle, and the read actually completes in the second cycle.*/
865 GB_FETCHER_GET_TILE_T1,
866 GB_FETCHER_GET_TILE_T2,
867 GB_FETCHER_GET_TILE_DATA_LOWER_T1,
868 GB_FETCHER_GET_TILE_DATA_LOWER_T2,
869 GB_FETCHER_GET_TILE_DATA_HIGH_T1,
870 GB_FETCHER_GET_TILE_DATA_HIGH_T2,
871 GB_FETCHER_PUSH,
872 } fetcher_step_t;
873  
874 0 static uint8_t data_for_tile_sel_glitch(GB_gameboy_t *gb, bool *should_use, bool *cgb_d_glitch)
875 {
876 /*
877 Based on Matt Currie's research here:
878 https://github.com/mattcurrie/mealybug-tearoom-tests/blob/master/the-comprehensive-game-boy-ppu-documentation.md#tile_sel-bit-4
879 */
880 0 *should_use = true;
881 0 *cgb_d_glitch = false;
882
883 0 ▶ if (gb->last_tileset) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->last_tileset) { 0 0 0
884 0 ▶ if (gb->model != GB_MODEL_CGB_D) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->model != GB_MODEL_CGB_D) { 0 0 0
885 0 *should_use = !(gb->current_tile & 0x80);
886 0 return gb->current_tile;
887 }
888 0 *cgb_d_glitch = true;
889 0 *should_use = false;
890 0 gb->last_tile_data_address &= ~0x1000;
891 0 ▶ if (gb->fetcher_state == GB_FETCHER_GET_TILE_DATA_LOWER_T2) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->fetcher_state == GB_FETCHER_GET_TILE_DATA_LOWER_T2) { 0 0 0
892 0 *cgb_d_glitch = true;
893 0 return 0;
894 }
895 0 return 0;
896 }
897 0 return gb->data_for_sel_glitch;
898 }
899  
900 2004597 internal void GB_update_wx_glitch(GB_gameboy_t *gb)
901 {
902 2004597 ▶ if (!GB_is_cgb(gb)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!GB_is_cgb(gb)) return; 235872 1768725 2004597
903 1768725 ▶ if (!(gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE) || !gb->wy_triggered) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE) || !gb->wy_triggered) { 0 1768725 1768725
if (!(gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE) || !gb->wy_triggered) { 0 0 0
904 1768725 gb->cgb_wx_glitch = false;
905 1768725 return;
906 }
907 0 ▶ if (unlikely(gb->io_registers[GB_IO_WX] == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(gb->io_registers[GB_IO_WX] == 0)) { 0 0 0
908 // (gb->position_in_line + 16 <= 8) is (gb->position_in_line <= -8) in unsigned
909 0 ▶ gb->cgb_wx_glitch = ((uint8_t)(gb->position_in_line + 16) <= 8 ||
Branch: 1 branch
BranchTrueFalseTotal
gb->cgb_wx_glitch = ((uint8_t)(gb->position_in_line + 16) <= 8 || 0 0 0
910 0 ▶ (gb->position_in_line == (uint8_t)-7 && gb->line_has_fractional_scrolling));
Branch: 2 branches
BranchTrueFalseTotal
(gb->position_in_line == (uint8_t)-7 && gb->line_has_fractional_scrolling)); 0 0 0
(gb->position_in_line == (uint8_t)-7 && gb->line_has_fractional_scrolling)); 0 0 0
911 0 return;
912 }
913 0 gb->cgb_wx_glitch = (uint8_t)(gb->position_in_line + 7 + gb->window_is_being_fetched) == gb->io_registers[GB_IO_WX];
914 }
915  
916 5345592 static void advance_fetcher_state_machine(GB_gameboy_t *gb, unsigned *cycles)
917 {
918 5345592 switch ((fetcher_step_t)gb->fetcher_state) {
919 668199 case GB_FETCHER_GET_TILE_T1: {
920 668199 GB_update_wx_glitch(gb);
921 668199 uint16_t map = 0x1800;
922
923 668199 ▶ if (!(gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE)) { 668199 0 668199
924 668199 gb->wx_triggered = false;
925 }
926
927 668199 ▶ if (gb->io_registers[GB_IO_LCDC] & GB_LCDC_BG_MAP && !gb->wx_triggered) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->io_registers[GB_IO_LCDC] & GB_LCDC_BG_MAP && !gb->wx_triggered) { 0 668199 668199
if (gb->io_registers[GB_IO_LCDC] & GB_LCDC_BG_MAP && !gb->wx_triggered) { 0 0 0
928 0 map = 0x1C00;
929 }
930 668199 ▶ else if (gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_MAP && gb->wx_triggered) {
Branch: 2 branches
BranchTrueFalseTotal
else if (gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_MAP && gb->wx_triggered) { 0 668199 668199
else if (gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_MAP && gb->wx_triggered) { 0 0 0
931 0 map = 0x1C00;
932 }
933
934 668199 uint8_t y = fetcher_y(gb);
935 668199 uint8_t x = 0;
936 668199 ▶ if (gb->wx_triggered) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->wx_triggered) { 0 668199 668199
937 0 x = gb->window_tile_x;
938 }
939 668199 ▶ else if ((uint8_t)(gb->position_in_line + 16) < 8) {
Branch: 1 branch
BranchTrueFalseTotal
else if ((uint8_t)(gb->position_in_line + 16) < 8) { 0 668199 668199
940 0 x = gb->io_registers[GB_IO_SCX] >> 3;
941 }
942 else {
943 668199 ▶ x = ((gb->io_registers[GB_IO_SCX] + gb->position_in_line + 8 - (GB_is_cgb(gb) && !gb->during_object_fetch)) / 8) & 0x1F;
Branch: 2 branches
BranchTrueFalseTotal
x = ((gb->io_registers[GB_IO_SCX] + gb->position_in_line + 8 - (GB_is_cgb(gb) && !gb->during_object_fetch)) / 8) & 0x1F; 589575 78624 668199
x = ((gb->io_registers[GB_IO_SCX] + gb->position_in_line + 8 - (GB_is_cgb(gb) && !gb->during_object_fetch)) / 8) & 0x1F; 589575 0 589575
944 }
945 668199 ▶ if (gb->model > GB_MODEL_CGB_C) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->model > GB_MODEL_CGB_C) { 589575 78624 668199
946 /* This value is cached on the CGB-D and newer, so it cannot be used to mix tiles together */
947 589575 gb->fetcher_y = y;
948 }
949 668199 gb->last_tile_index_address = map + x + y / 8 * 32;
950 }
951 668199 gb->fetcher_state++;
952 668199 break;
953 668199 case GB_FETCHER_GET_TILE_T2: {
954 668199 ▶ if (gb->cgb_wx_glitch) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->cgb_wx_glitch) { 0 668199 668199
955 0 gb->fetcher_state++;
956 0 break;
957 }
958 668199 dma_sync(gb, cycles);
959 668199 gb->current_tile = vram_read(gb, gb->last_tile_index_address);
960 668199 ▶ if (GB_is_cgb(gb)) {
Branch: 1 branch
BranchTrueFalseTotal
if (GB_is_cgb(gb)) { 589575 78624 668199
961 /* The CGB actually accesses both the tile index AND the attributes in the same T-cycle.
962 This probably means the CGB has a 16-bit data bus for the VRAM. */
963 589575 gb->current_tile_attributes = vram_read(gb, gb->last_tile_index_address + 0x2000);
964 }
965 }
966 668199 gb->fetcher_state++;
967 668199 break;
968
969 668199 case GB_FETCHER_GET_TILE_DATA_LOWER_T1: {
970 668199 GB_update_wx_glitch(gb);
971  
972 668199 uint8_t y_flip = 0;
973 668199 uint16_t tile_address = 0;
974 668199 ▶ uint8_t y = gb->model > GB_MODEL_CGB_C ? gb->fetcher_y : fetcher_y(gb);
Branch: 1 branch
BranchTrueFalseTotal
uint8_t y = gb->model > GB_MODEL_CGB_C ? gb->fetcher_y : fetcher_y(gb); 589575 78624 668199
975
976 668199 gb->last_tileset = gb->io_registers[GB_IO_LCDC] & GB_LCDC_TILE_SEL;
977 668199 ▶ if (gb->last_tileset) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->last_tileset) { 668199 0 668199
978 668199 tile_address = gb->current_tile * 0x10;
979 }
980 else {
981 0 tile_address = (int8_t)gb->current_tile * 0x10 + 0x1000;
982 }
983 668199 ▶ if (gb->current_tile_attributes & 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->current_tile_attributes & 8) { 90600 577599 668199
984 90600 tile_address += 0x2000;
985 }
986 668199 ▶ if (gb->current_tile_attributes & 0x40) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->current_tile_attributes & 0x40) { 0 668199 668199
987 0 y_flip = 0x7;
988 }
989 668199 gb->last_tile_data_address = tile_address + ((y & 7) ^ y_flip) * 2;
990 }
991 668199 gb->fetcher_state++;
992 668199 break;
993
994 668199 case GB_FETCHER_GET_TILE_DATA_LOWER_T2: {
995 668199 ▶ if (gb->cgb_wx_glitch) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->cgb_wx_glitch) { 0 668199 668199
996 0 gb->current_tile_data[0] = gb->current_tile_data[1];
997 0 gb->fetcher_state++;
998 0 break;
999 }
1000 668199 dma_sync(gb, cycles);
1001 668199 bool use_glitched = false;
1002 668199 bool cgb_d_glitch = false;
1003 668199 ▶ if (gb->tile_sel_glitch) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->tile_sel_glitch) { 0 668199 668199
1004 0 gb->current_tile_data[0] = data_for_tile_sel_glitch(gb, &use_glitched, &cgb_d_glitch);
1005 }
1006 668199 ▶ if (!use_glitched) {
Branch: 1 branch
BranchTrueFalseTotal
if (!use_glitched) { 668199 0 668199
1007 668199 gb->current_tile_data[0] = vram_read(gb, gb->last_tile_data_address);
1008 }
1009 668199 ▶ if (gb->last_tileset && gb->tile_sel_glitch) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->last_tileset && gb->tile_sel_glitch) { 668199 0 668199
if (gb->last_tileset && gb->tile_sel_glitch) { 0 668199 668199
1010 0 gb->data_for_sel_glitch = vram_read(gb, gb->last_tile_data_address);
1011 }
1012 668199 ▶ else if (cgb_d_glitch) {
Branch: 1 branch
BranchTrueFalseTotal
else if (cgb_d_glitch) { 0 668199 668199
1013 0 gb->data_for_sel_glitch = vram_read(gb, gb->last_tile_data_address & ~0x1000);
1014 }
1015 }
1016 668199 gb->fetcher_state++;
1017 668199 break;
1018
1019 668199 case GB_FETCHER_GET_TILE_DATA_HIGH_T1: {
1020 668199 GB_update_wx_glitch(gb);
1021  
1022 668199 uint16_t tile_address = 0;
1023 668199 ▶ uint8_t y = gb->model > GB_MODEL_CGB_C ? gb->fetcher_y : fetcher_y(gb);
Branch: 1 branch
BranchTrueFalseTotal
uint8_t y = gb->model > GB_MODEL_CGB_C ? gb->fetcher_y : fetcher_y(gb); 589575 78624 668199
1024
1025 668199 gb->last_tileset = gb->io_registers[GB_IO_LCDC] & GB_LCDC_TILE_SEL;
1026 668199 ▶ if (gb->last_tileset) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->last_tileset) { 668199 0 668199
1027 668199 tile_address = gb->current_tile * 0x10;
1028 }
1029 else {
1030 0 tile_address = (int8_t)gb->current_tile * 0x10 + 0x1000;
1031 }
1032 668199 ▶ if (gb->current_tile_attributes & 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->current_tile_attributes & 8) { 90600 577599 668199
1033 90600 tile_address += 0x2000;
1034 }
1035 668199 uint8_t y_flip = 0;
1036 668199 ▶ if (gb->current_tile_attributes & 0x40) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->current_tile_attributes & 0x40) { 0 668199 668199
1037 0 y_flip = 0x7;
1038 }
1039 668199 gb->last_tile_data_address = tile_address + ((y & 7) ^ y_flip) * 2 + 1;
1040 }
1041 668199 gb->fetcher_state++;
1042 668199 break;
1043
1044 668199 case GB_FETCHER_GET_TILE_DATA_HIGH_T2: {
1045 668199 ▶ if (gb->cgb_wx_glitch) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->cgb_wx_glitch) { 0 668199 668199
1046 0 gb->current_tile_data[1] = gb->current_tile_data[0];
1047 0 gb->fetcher_state++;
1048 0 ▶ if (gb->wx_triggered) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->wx_triggered) { 0 0 0
1049 0 gb->window_tile_x++;
1050 0 gb->window_tile_x &= 0x1F;
1051 }
1052 0 break;
1053 }
1054 668199 dma_sync(gb, cycles);
1055 668199 bool use_glitched = false;
1056 668199 bool cgb_d_glitch = false;
1057 668199 ▶ if (gb->tile_sel_glitch) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->tile_sel_glitch) { 0 668199 668199
1058 0 gb->current_tile_data[1] = data_for_tile_sel_glitch(gb, &use_glitched, &cgb_d_glitch);
1059 0 ▶ if (cgb_d_glitch) {
Branch: 1 branch
BranchTrueFalseTotal
if (cgb_d_glitch) { 0 0 0
1060 0 gb->last_tile_data_address--;
1061 }
1062 }
1063 668199 ▶ if (!use_glitched) {
Branch: 1 branch
BranchTrueFalseTotal
if (!use_glitched) { 668199 0 668199
1064 668199 gb->data_for_sel_glitch = gb->current_tile_data[1] =
1065 668199 vram_read(gb, gb->last_tile_data_address);
1066 }
1067 668199 ▶ if (gb->last_tileset && gb->tile_sel_glitch) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->last_tileset && gb->tile_sel_glitch) { 668199 0 668199
if (gb->last_tileset && gb->tile_sel_glitch) { 0 668199 668199
1068 0 gb->data_for_sel_glitch = vram_read(gb, gb->last_tile_data_address);
1069  
1070 }
1071 668199 ▶ else if (cgb_d_glitch) {
Branch: 1 branch
BranchTrueFalseTotal
else if (cgb_d_glitch) { 0 668199 668199
1072 0 gb->data_for_sel_glitch = vram_read(gb, (gb->tile_sel_glitch & ~0x1000) + 1);
1073 }
1074 }
1075 668199 ▶ if (gb->wx_triggered) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->wx_triggered) { 0 668199 668199
1076 0 gb->window_tile_x++;
1077 0 gb->window_tile_x &= 0x1F;
1078 }
1079
1080 // fallthrough
1081 default:
1082 case GB_FETCHER_PUSH: {
1083 2004597 gb->fetcher_state = GB_FETCHER_PUSH;
1084 2004597 ▶ if (fifo_size(&gb->bg_fifo) > 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (fifo_size(&gb->bg_fifo) > 0) break; 1336398 668199 2004597
1085
1086 668199 ▶ if (unlikely(gb->wy_triggered && !(gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE) && !GB_is_cgb(gb) && !gb->disable_window_pixel_insertion_glitch)) {
Branch: 6 branches
BranchTrueFalseTotal
if (unlikely(gb->wy_triggered && !(gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE) && !GB_is_cgb(gb) && !gb->disable_window_pixel_insertion_glitch)) { 0 668199 668199
if (unlikely(gb->wy_triggered && !(gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE) && !GB_is_cgb(gb) && !gb->disable_window_pixel_insertion_glitch)) { 0 0 0
if (unlikely(gb->wy_triggered && !(gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE) && !GB_is_cgb(gb) && !gb->disable_window_pixel_insertion_glitch)) { 0 668199 668199
if (unlikely(gb->wy_triggered && !(gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE) && !GB_is_cgb(gb) && !gb->disable_window_pixel_insertion_glitch)) { 0 0 0
if (unlikely(gb->wy_triggered && !(gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE) && !GB_is_cgb(gb) && !gb->disable_window_pixel_insertion_glitch)) { 0 668199 668199
if (unlikely(gb->wy_triggered && !(gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE) && !GB_is_cgb(gb) && !gb->disable_window_pixel_insertion_glitch)) { 0 0 0
1087 /* See https://github.com/LIJI32/SameBoy/issues/278 for documentation */
1088 0 uint8_t logical_position = gb->position_in_line + 7;
1089 0 ▶ if (logical_position > 167) {
Branch: 1 branch
BranchTrueFalseTotal
if (logical_position > 167) { 0 0 0
1090 0 logical_position = 0;
1091 }
1092 0 ▶ if (gb->io_registers[GB_IO_WX] == logical_position) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->io_registers[GB_IO_WX] == logical_position) { 0 0 0
1093 0 gb->bg_fifo.read_end--;
1094 0 gb->bg_fifo.read_end &= GB_FIFO_LENGTH - 1;
1095 0 gb->bg_fifo.fifo[gb->bg_fifo.read_end] = (GB_fifo_item_t){0,};
1096 0 gb->bg_fifo.size = 1;
1097 0 break;
1098 }
1099 }
1100
1101 668199 fifo_push_bg_row(&gb->bg_fifo, gb->current_tile_data[0], gb->current_tile_data[1],
1102 668199 gb->current_tile_attributes & 7, gb->current_tile_attributes & 0x80, gb->current_tile_attributes & 0x20);
1103 668199 gb->fetcher_state = GB_FETCHER_GET_TILE_T1;
1104 }
1105 668199 break;
1106 }
1107 5345592 }
1108  
1109 0 static uint16_t get_object_line_address(GB_gameboy_t *gb, uint8_t y, uint8_t tile, uint8_t flags)
1110 {
1111 0 bool height_16 = (gb->io_registers[GB_IO_LCDC] & GB_LCDC_OBJ_SIZE) != 0; /* Todo: Which T-cycle actually reads this? */
1112 0 ▶ uint8_t tile_y = (gb->current_line - y) & (height_16? 0xF : 7);
Branch: 1 branch
BranchTrueFalseTotal
uint8_t tile_y = (gb->current_line - y) & (height_16? 0xF : 7); 0 0 0
1113
1114 0 ▶ if (flags & 0x40) { /* Flip Y */
Branch: 1 branch
BranchTrueFalseTotal
if (flags & 0x40) { /* Flip Y */ 0 0 0
1115 0 ▶ tile_y ^= height_16? 0xF : 7;
Branch: 1 branch
BranchTrueFalseTotal
tile_y ^= height_16? 0xF : 7; 0 0 0
1116 }
1117
1118 /* Todo: I'm not 100% sure an access to OAM can't trigger the OAM bug while we're accessing this */
1119 0 ▶ uint16_t line_address = (height_16? tile & 0xFE : tile) * 0x10 + tile_y * 2;
Branch: 1 branch
BranchTrueFalseTotal
uint16_t line_address = (height_16? tile & 0xFE : tile) * 0x10 + tile_y * 2; 0 0 0
1120
1121 0 ▶ if (gb->cgb_mode && (flags & 0x8)) { /* Use VRAM bank 2 */
Branch: 2 branches
BranchTrueFalseTotal
if (gb->cgb_mode && (flags & 0x8)) { /* Use VRAM bank 2 */ 0 0 0
if (gb->cgb_mode && (flags & 0x8)) { /* Use VRAM bank 2 */ 0 0 0
1122 0 line_address += 0x2000;
1123 }
1124 0 return line_address;
1125 }
1126  
1127 0 static inline uint8_t flip(uint8_t x)
1128 {
1129 0 x = (x & 0xF0) >> 4 | (x & 0x0F) << 4;
1130 0 x = (x & 0xCC) >> 2 | (x & 0x33) << 2;
1131 0 x = (x & 0xAA) >> 1 | (x & 0x55) << 1;
1132 0 return x;
1133 }
1134  
1135 105 static inline void get_tile_data(const GB_gameboy_t *gb, uint8_t tile_x, uint8_t y, uint16_t map, uint8_t *attributes, uint8_t *data0, uint8_t *data1)
1136 {
1137 105 uint8_t current_tile = gb->vram[map + (tile_x & 0x1F) + y / 8 * 32];
1138 105 ▶ *attributes = GB_is_cgb(gb)? gb->vram[0x2000 + map + (tile_x & 0x1F) + y / 8 * 32] : 0;
Branch: 1 branch
BranchTrueFalseTotal
*attributes = GB_is_cgb(gb)? gb->vram[0x2000 + map + (tile_x & 0x1F) + y / 8 * 32] : 0; 105 0 105
1139
1140 105 uint16_t tile_address = 0;
1141
1142 105 ▶ if (gb->io_registers[GB_IO_LCDC] & GB_LCDC_TILE_SEL) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->io_registers[GB_IO_LCDC] & GB_LCDC_TILE_SEL) { 105 0 105
1143 105 tile_address = current_tile * 0x10;
1144 }
1145 else {
1146 0 tile_address = (int8_t)current_tile * 0x10 + 0x1000;
1147 }
1148 105 ▶ if (*attributes & 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (*attributes & 8) { 0 105 105
1149 0 tile_address += 0x2000;
1150 }
1151 105 uint8_t y_flip = 0;
1152 105 ▶ if (*attributes & 0x40) {
Branch: 1 branch
BranchTrueFalseTotal
if (*attributes & 0x40) { 0 105 105
1153 0 y_flip = 0x7;
1154 }
1155
1156 105 *data0 = gb->vram[tile_address + ((y & 7) ^ y_flip) * 2];
1157 105 *data1 = gb->vram[tile_address + ((y & 7) ^ y_flip) * 2 + 1];
1158
1159 105 ▶ if (*attributes & 0x20) {
Branch: 1 branch
BranchTrueFalseTotal
if (*attributes & 0x20) { 0 105 105
1160 0 *data0 = flip(*data0);
1161 0 *data1 = flip(*data1);
1162 }
1163  
1164 105 }
1165  
1166 5 static void render_line(GB_gameboy_t *gb)
1167 {
1168 5 ▶ if (gb->disable_rendering) return;
Branch: 1 branch
BranchTrueFalseTotal
if (gb->disable_rendering) return; 0 5 5
1169 5 ▶ if (!gb->screen) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!gb->screen) return; 0 5 5
1170 5 ▶ if (gb->current_line > 144) return; // Corrupt save state
Branch: 1 branch
BranchTrueFalseTotal
if (gb->current_line > 144) return; // Corrupt save state 0 5 5
1171
1172 struct {
1173 unsigned pixel:2; // Color, 0-3
1174 unsigned priority:6; // Object priority – 0 in DMG, OAM index in CGB
1175 unsigned palette:3; // Palette, 0 - 7 (CGB); 0-1 in DMG (or just 0 for BG)
1176 bool bg_priority:1; // BG priority bit
1177 } _object_buffer[160 + 16]; // allocate extra to avoid per pixel checks
1178 static const uint8_t empty_object_buffer[sizeof(_object_buffer)];
1179 const typeof(_object_buffer[0]) *object_buffer;
1180
1181 5 ▶ if (gb->n_visible_objs && !gb->objects_disabled && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_OBJ_EN)) {
Branch: 3 branches
BranchTrueFalseTotal
if (gb->n_visible_objs && !gb->objects_disabled && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_OBJ_EN)) { 0 5 5
if (gb->n_visible_objs && !gb->objects_disabled && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_OBJ_EN)) { 0 0 0
if (gb->n_visible_objs && !gb->objects_disabled && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_OBJ_EN)) { 0 0 0
1182 0 object_buffer = &_object_buffer[0];
1183 0 object_t *objects = (object_t *) &gb->oam;
1184 0 memset(_object_buffer, 0, sizeof(_object_buffer));
1185  
1186 0 ▶ while (gb->n_visible_objs) {
Branch: 1 branch
BranchTrueFalseTotal
while (gb->n_visible_objs) { 0 0 0
1187 0 unsigned object_index = gb->visible_objs[gb->n_visible_objs - 1];
1188 0 ▶ unsigned priority = (gb->object_priority == GB_OBJECT_PRIORITY_X)? 0 : object_index;
Branch: 1 branch
BranchTrueFalseTotal
unsigned priority = (gb->object_priority == GB_OBJECT_PRIORITY_X)? 0 : object_index; 0 0 0
1189 0 const object_t *object = &objects[object_index];
1190 0 gb->n_visible_objs--;
1191
1192 0 uint16_t line_address = get_object_line_address(gb, object->y, object->tile, object->flags);
1193 0 uint8_t data0 = gb->vram[line_address];
1194 0 uint8_t data1 = gb->vram[line_address + 1];
1195 0 ▶ if (gb->n_visible_objs == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->n_visible_objs == 0) { 0 0 0
1196 0 gb->data_for_sel_glitch = data1;
1197 }
1198 0 ▶ if (object->flags & 0x20) {
Branch: 1 branch
BranchTrueFalseTotal
if (object->flags & 0x20) { 0 0 0
1199 0 data0 = flip(data0);
1200 0 data1 = flip(data1);
1201 }
1202  
1203 0 typeof(_object_buffer[0]) *p = _object_buffer + object->x;
1204 0 ▶ if (object->x >= 168) {
Branch: 1 branch
BranchTrueFalseTotal
if (object->x >= 168) { 0 0 0
1205 0 continue;
1206 }
1207 0 ▶ unrolled for (unsigned x = 0; x < 8; x++) {
Branch: 1 branch
BranchTrueFalseTotal
unrolled for (unsigned x = 0; x < 8; x++) { 0 0 0
1208 0 unsigned pixel = (data0 >> 7) | ((data1 >> 7) << 1);
1209 0 data0 <<= 1;
1210 0 data1 <<= 1;
1211 0 ▶ if (pixel && (!p->pixel || priority < p->priority)) {
Branch: 3 branches
BranchTrueFalseTotal
if (pixel && (!p->pixel || priority < p->priority)) { 0 0 0
if (pixel && (!p->pixel || priority < p->priority)) { 0 0 0
if (pixel && (!p->pixel || priority < p->priority)) { 0 0 0
1212 0 p->pixel = pixel;
1213 0 p->priority = priority;
1214
1215 0 ▶ if (gb->cgb_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->cgb_mode) { 0 0 0
1216 0 p->palette = object->flags & 0x7;
1217 }
1218 else {
1219 0 p->palette = (object->flags & 0x10) >> 4;
1220 }
1221 0 p->bg_priority = object->flags & 0x80;
1222 }
1223 0 p++;
1224 }
1225 }
1226 }
1227 else {
1228 5 object_buffer = (const void *)empty_object_buffer;
1229 }
1230
1231
1232 5 uint32_t *restrict p = gb->screen;
1233 5 typeof(object_buffer[0]) *object_buffer_pointer = object_buffer + 8;
1234 5 ▶ if (gb->border_mode == GB_BORDER_ALWAYS) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->border_mode == GB_BORDER_ALWAYS) { 0 5 5
1235 0 p += (BORDERED_WIDTH - (WIDTH)) / 2 + BORDERED_WIDTH * (BORDERED_HEIGHT - LINES) / 2;
1236 0 p += BORDERED_WIDTH * gb->current_line;
1237 }
1238 else {
1239 5 p += WIDTH * gb->current_line;
1240 }
1241
1242 5 ▶ if (unlikely(gb->background_disabled) || (!gb->cgb_mode && !(gb->io_registers[GB_IO_LCDC] & GB_LCDC_BG_EN))) {
Branch: 3 branches
BranchTrueFalseTotal
if (unlikely(gb->background_disabled) || (!gb->cgb_mode && !(gb->io_registers[GB_IO_LCDC] & GB_LCDC_BG_EN))) { 5 0 5
if (unlikely(gb->background_disabled) || (!gb->cgb_mode && !(gb->io_registers[GB_IO_LCDC] & GB_LCDC_BG_EN))) { 0 5 5
if (unlikely(gb->background_disabled) || (!gb->cgb_mode && !(gb->io_registers[GB_IO_LCDC] & GB_LCDC_BG_EN))) { 0 0 0
1243 0 ▶ uint32_t bg = gb->background_palettes_rgb[gb->cgb_mode? 0 : (gb->io_registers[GB_IO_BGP] & 3)];
Branch: 1 branch
BranchTrueFalseTotal
uint32_t bg = gb->background_palettes_rgb[gb->cgb_mode? 0 : (gb->io_registers[GB_IO_BGP] & 3)]; 0 0 0
1244 0 ▶ for (unsigned i = 160; i--;) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 160; i--;) { 0 0 0
1245 0 ▶ if (unlikely(object_buffer_pointer->pixel)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(object_buffer_pointer->pixel)) { 0 0 0
1246 0 uint8_t pixel = object_buffer_pointer->pixel;
1247 0 ▶ if (!gb->cgb_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gb->cgb_mode) { 0 0 0
1248 0 pixel = ((gb->io_registers[GB_IO_OBP0 + object_buffer_pointer->palette] >> (pixel << 1)) & 3);
1249 }
1250 0 *(p++) = gb->object_palettes_rgb[pixel + (object_buffer_pointer->palette & 7) * 4];
1251 }
1252 else {
1253 0 *(p++) = bg;
1254 }
1255 0 object_buffer_pointer++;
1256 }
1257 0 return;
1258 }
1259
1260 5 unsigned pixels = 0;
1261 5 uint8_t tile_x = gb->io_registers[GB_IO_SCX] / 8;
1262 5 unsigned fractional_scroll = gb->io_registers[GB_IO_SCX] & 7;
1263 5 uint16_t map = 0x1800;
1264 5 ▶ if (gb->io_registers[GB_IO_LCDC] & GB_LCDC_BG_MAP) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->io_registers[GB_IO_LCDC] & GB_LCDC_BG_MAP) { 0 5 5
1265 0 map = 0x1C00;
1266 }
1267 5 uint8_t y = gb->current_line + gb->io_registers[GB_IO_SCY];
1268 uint8_t attributes;
1269 uint8_t data0, data1;
1270 5 get_tile_data(gb, tile_x, y, map, &attributes, &data0, &data1);
1271
1272 #define DO_PIXEL() \
1273 uint8_t pixel = (data0 >> 7) | ((data1 >> 7) << 1);\
1274 data0 <<= 1;\
1275 data1 <<= 1;\
1276 \
1277 if (unlikely(object_buffer_pointer->pixel) && (pixel == 0 || !(object_buffer_pointer->bg_priority || (attributes & 0x80)) || !(gb->io_registers[GB_IO_LCDC] & GB_LCDC_BG_EN))) {\
1278 pixel = object_buffer_pointer->pixel;\
1279 if (!gb->cgb_mode) {\
1280 pixel = ((gb->io_registers[GB_IO_OBP0 + object_buffer_pointer->palette] >> (pixel << 1)) & 3);\
1281 }\
1282 *(p++) = gb->object_palettes_rgb[pixel + (object_buffer_pointer->palette & 7) * 4];\
1283 }\
1284 else {\
1285 if (!gb->cgb_mode) {\
1286 pixel = ((gb->io_registers[GB_IO_BGP] >> (pixel << 1)) & 3);\
1287 }\
1288 *(p++) = gb->background_palettes_rgb[pixel + (attributes & 7) * 4];\
1289 }\
1290 pixels++;\
1291 object_buffer_pointer++\
1292
1293 // First 1-8 pixels
1294 5 data0 <<= fractional_scroll;
1295 5 data1 <<= fractional_scroll;
1296 5 ▶ bool check_window = gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE);
Branch: 2 branches
BranchTrueFalseTotal
bool check_window = gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE); 0 5 5
bool check_window = gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE); 0 0 0
1297 45 ▶ nounroll for (unsigned i = fractional_scroll; i < 8; i++) {
Branch: 1 branch
BranchTrueFalseTotal
nounroll for (unsigned i = fractional_scroll; i < 8; i++) { 40 5 45
1298 40 ▶ if (check_window && gb->io_registers[GB_IO_WX] == pixels + 7) {
Branch: 2 branches
BranchTrueFalseTotal
if (check_window && gb->io_registers[GB_IO_WX] == pixels + 7) { 0 40 40
if (check_window && gb->io_registers[GB_IO_WX] == pixels + 7) { 0 0 0
1299 0 activate_window:
1300 0 check_window = false;
1301 0 ▶ map = gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_MAP? 0x1C00 : 0x1800;
Branch: 1 branch
BranchTrueFalseTotal
map = gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_MAP? 0x1C00 : 0x1800; 0 0 0
1302 0 tile_x = -1;
1303 0 y = ++gb->window_y;
1304 0 break;
1305 }
1306 40 ▶ DO_PIXEL();
Branch: 7 branches
BranchTrueFalseTotal
DO_PIXEL(); 0 40 40
DO_PIXEL(); 0 0 0
DO_PIXEL(); 0 0 0
DO_PIXEL(); 0 0 0
DO_PIXEL(); 0 0 0
DO_PIXEL(); 0 0 0
DO_PIXEL(); 0 40 40
1307 }
1308 5 tile_x++;
1309
1310 95 ▶ while (pixels < 160 - 8) {
Branch: 1 branch
BranchTrueFalseTotal
while (pixels < 160 - 8) { 90 5 95
1311 90 get_tile_data(gb, tile_x, y, map, &attributes, &data0, &data1);
1312 810 ▶ nounroll for (unsigned i = 0; i < 8; i++) {
Branch: 1 branch
BranchTrueFalseTotal
nounroll for (unsigned i = 0; i < 8; i++) { 720 90 810
1313 720 ▶ if (check_window && gb->io_registers[GB_IO_WX] == pixels + 7) {
Branch: 2 branches
BranchTrueFalseTotal
if (check_window && gb->io_registers[GB_IO_WX] == pixels + 7) { 0 720 720
if (check_window && gb->io_registers[GB_IO_WX] == pixels + 7) { 0 0 0
1314 0 goto activate_window;
1315 }
1316 720 ▶ DO_PIXEL();
Branch: 7 branches
BranchTrueFalseTotal
DO_PIXEL(); 0 720 720
DO_PIXEL(); 0 0 0
DO_PIXEL(); 0 0 0
DO_PIXEL(); 0 0 0
DO_PIXEL(); 0 0 0
DO_PIXEL(); 0 0 0
DO_PIXEL(); 0 720 720
1317 }
1318 90 tile_x++;
1319 }
1320
1321 5 gb->fetcher_state = (160 - pixels) & 7;
1322 5 get_tile_data(gb, tile_x, y, map, &attributes, &data0, &data1);
1323 45 ▶ while (pixels < 160) {
Branch: 1 branch
BranchTrueFalseTotal
while (pixels < 160) { 40 5 45
1324 40 ▶ if (check_window && gb->io_registers[GB_IO_WX] == pixels + 7) {
Branch: 2 branches
BranchTrueFalseTotal
if (check_window && gb->io_registers[GB_IO_WX] == pixels + 7) { 0 40 40
if (check_window && gb->io_registers[GB_IO_WX] == pixels + 7) { 0 0 0
1325 0 goto activate_window;
1326 }
1327 40 ▶ DO_PIXEL();
Branch: 7 branches
BranchTrueFalseTotal
DO_PIXEL(); 0 40 40
DO_PIXEL(); 0 0 0
DO_PIXEL(); 0 0 0
DO_PIXEL(); 0 0 0
DO_PIXEL(); 0 0 0
DO_PIXEL(); 0 0 0
DO_PIXEL(); 0 40 40
1328 }
1329 5 tile_x++;
1330  
1331 5 get_tile_data(gb, tile_x, y, map, &attributes, gb->current_tile_data, gb->current_tile_data + 1);
1332 #undef DO_PIXEL
1333 }
1334  
1335 0 static void render_line_sgb(GB_gameboy_t *gb)
1336 {
1337 0 ▶ if (gb->current_line > 144) return; // Corrupt save state
Branch: 1 branch
BranchTrueFalseTotal
if (gb->current_line > 144) return; // Corrupt save state 0 0 0
1338
1339 struct {
1340 unsigned pixel:2; // Color, 0-3
1341 unsigned palette:1; // Palette, 0 - 7 (CGB); 0-1 in DMG (or just 0 for BG)
1342 bool bg_priority:1; // BG priority bit
1343 } _object_buffer[160 + 16]; // allocate extra to avoid per pixel checks
1344 static const uint8_t empty_object_buffer[sizeof(_object_buffer)];
1345 const typeof(_object_buffer[0]) *object_buffer;
1346
1347 0 ▶ if (gb->n_visible_objs && !gb->objects_disabled && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_OBJ_EN)) {
Branch: 3 branches
BranchTrueFalseTotal
if (gb->n_visible_objs && !gb->objects_disabled && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_OBJ_EN)) { 0 0 0
if (gb->n_visible_objs && !gb->objects_disabled && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_OBJ_EN)) { 0 0 0
if (gb->n_visible_objs && !gb->objects_disabled && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_OBJ_EN)) { 0 0 0
1348 0 object_buffer = &_object_buffer[0];
1349 0 object_t *objects = (object_t *) &gb->oam;
1350 0 memset(_object_buffer, 0, sizeof(_object_buffer));
1351
1352 0 ▶ while (gb->n_visible_objs) {
Branch: 1 branch
BranchTrueFalseTotal
while (gb->n_visible_objs) { 0 0 0
1353 0 const object_t *object = &objects[gb->visible_objs[gb->n_visible_objs - 1]];
1354 0 gb->n_visible_objs--;
1355
1356 0 uint16_t line_address = get_object_line_address(gb, object->y, object->tile, object->flags);
1357 0 uint8_t data0 = gb->vram[line_address];
1358 0 uint8_t data1 = gb->vram[line_address + 1];
1359 0 ▶ if (object->flags & 0x20) {
Branch: 1 branch
BranchTrueFalseTotal
if (object->flags & 0x20) { 0 0 0
1360 0 data0 = flip(data0);
1361 0 data1 = flip(data1);
1362 }
1363
1364 0 typeof(_object_buffer[0]) *p = _object_buffer + object->x;
1365 0 ▶ if (object->x >= 168) {
Branch: 1 branch
BranchTrueFalseTotal
if (object->x >= 168) { 0 0 0
1366 0 continue;
1367 }
1368 0 ▶ unrolled for (unsigned x = 0; x < 8; x++) {
Branch: 1 branch
BranchTrueFalseTotal
unrolled for (unsigned x = 0; x < 8; x++) { 0 0 0
1369 0 unsigned pixel = (data0 >> 7) | ((data1 >> 7) << 1);
1370 0 data0 <<= 1;
1371 0 data1 <<= 1;
1372 0 ▶ if (!p->pixel) {
Branch: 1 branch
BranchTrueFalseTotal
if (!p->pixel) { 0 0 0
1373 0 p->pixel = pixel;
1374 0 p->palette = (object->flags & 0x10) >> 4;
1375 0 p->bg_priority = object->flags & 0x80;
1376 }
1377 0 p++;
1378 }
1379 }
1380 }
1381 else {
1382 0 object_buffer = (const void *)empty_object_buffer;
1383 }
1384
1385
1386 0 uint8_t *restrict p = gb->sgb->screen_buffer;
1387 0 typeof(object_buffer[0]) *object_buffer_pointer = object_buffer + 8;
1388 0 p += WIDTH * gb->current_line;
1389
1390 0 ▶ if (unlikely(gb->background_disabled) || (!gb->cgb_mode && !(gb->io_registers[GB_IO_LCDC] & GB_LCDC_BG_EN))) {
Branch: 3 branches
BranchTrueFalseTotal
if (unlikely(gb->background_disabled) || (!gb->cgb_mode && !(gb->io_registers[GB_IO_LCDC] & GB_LCDC_BG_EN))) { 0 0 0
if (unlikely(gb->background_disabled) || (!gb->cgb_mode && !(gb->io_registers[GB_IO_LCDC] & GB_LCDC_BG_EN))) { 0 0 0
if (unlikely(gb->background_disabled) || (!gb->cgb_mode && !(gb->io_registers[GB_IO_LCDC] & GB_LCDC_BG_EN))) { 0 0 0
1391 0 ▶ for (unsigned i = 160; i--;) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 160; i--;) { 0 0 0
1392 0 ▶ if (unlikely(object_buffer_pointer->pixel)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(object_buffer_pointer->pixel)) { 0 0 0
1393 0 uint8_t pixel = object_buffer_pointer->pixel;
1394 0 pixel = ((gb->io_registers[GB_IO_OBP0 + object_buffer_pointer->palette] >> (pixel << 1)) & 3);
1395 0 *(p++) = pixel;
1396 }
1397 else {
1398 0 *(p++) = gb->io_registers[GB_IO_BGP] & 3;
1399 }
1400 0 object_buffer_pointer++;
1401 }
1402 0 return;
1403 }
1404
1405 0 unsigned pixels = 0;
1406 0 uint8_t tile_x = gb->io_registers[GB_IO_SCX] / 8;
1407 0 unsigned fractional_scroll = gb->io_registers[GB_IO_SCX] & 7;
1408 0 uint16_t map = 0x1800;
1409 0 ▶ if (gb->io_registers[GB_IO_LCDC] & GB_LCDC_BG_MAP) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->io_registers[GB_IO_LCDC] & GB_LCDC_BG_MAP) { 0 0 0
1410 0 map = 0x1C00;
1411 }
1412 0 uint8_t y = gb->current_line + gb->io_registers[GB_IO_SCY];
1413 uint8_t attributes;
1414 uint8_t data0, data1;
1415 0 get_tile_data(gb, tile_x, y, map, &attributes, &data0, &data1);
1416
1417 #define DO_PIXEL() \
1418 uint8_t pixel = (data0 >> 7) | ((data1 >> 7) << 1);\
1419 data0 <<= 1;\
1420 data1 <<= 1;\
1421 \
1422 if (unlikely(object_buffer_pointer->pixel) && (pixel == 0 || !object_buffer_pointer->bg_priority || !(gb->io_registers[GB_IO_LCDC] & GB_LCDC_BG_EN))) {\
1423 pixel = object_buffer_pointer->pixel;\
1424 pixel = ((gb->io_registers[GB_IO_OBP0 + object_buffer_pointer->palette] >> (pixel << 1)) & 3);\
1425 *(p++) = pixel;\
1426 }\
1427 else {\
1428 pixel = ((gb->io_registers[GB_IO_BGP] >> (pixel << 1)) & 3);\
1429 *(p++) = pixel;\
1430 }\
1431 pixels++;\
1432 object_buffer_pointer++\
1433  
1434 // First 1-8 pixels
1435 0 data0 <<= fractional_scroll;
1436 0 data1 <<= fractional_scroll;
1437 0 ▶ bool check_window = gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE);
Branch: 2 branches
BranchTrueFalseTotal
bool check_window = gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE); 0 0 0
bool check_window = gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE); 0 0 0
1438 0 ▶ nounroll for (unsigned i = fractional_scroll; i < 8; i++) {
Branch: 1 branch
BranchTrueFalseTotal
nounroll for (unsigned i = fractional_scroll; i < 8; i++) { 0 0 0
1439 0 ▶ if (check_window && gb->io_registers[GB_IO_WX] == pixels + 7) {
Branch: 2 branches
BranchTrueFalseTotal
if (check_window && gb->io_registers[GB_IO_WX] == pixels + 7) { 0 0 0
if (check_window && gb->io_registers[GB_IO_WX] == pixels + 7) { 0 0 0
1440 0 activate_window:
1441 0 check_window = false;
1442 0 ▶ map = gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_MAP? 0x1C00 : 0x1800;
Branch: 1 branch
BranchTrueFalseTotal
map = gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_MAP? 0x1C00 : 0x1800; 0 0 0
1443 0 tile_x = -1;
1444 0 y = ++gb->window_y;
1445 0 break;
1446 }
1447 0 ▶ DO_PIXEL();
Branch: 4 branches
BranchTrueFalseTotal
DO_PIXEL(); 0 0 0
DO_PIXEL(); 0 0 0
DO_PIXEL(); 0 0 0
DO_PIXEL(); 0 0 0
1448 }
1449 0 tile_x++;
1450
1451 0 ▶ while (pixels < 160 - 8) {
Branch: 1 branch
BranchTrueFalseTotal
while (pixels < 160 - 8) { 0 0 0
1452 0 get_tile_data(gb, tile_x, y, map, &attributes, &data0, &data1);
1453 0 ▶ nounroll for (unsigned i = 0; i < 8; i++) {
Branch: 1 branch
BranchTrueFalseTotal
nounroll for (unsigned i = 0; i < 8; i++) { 0 0 0
1454 0 ▶ if (check_window && gb->io_registers[GB_IO_WX] == pixels + 7) {
Branch: 2 branches
BranchTrueFalseTotal
if (check_window && gb->io_registers[GB_IO_WX] == pixels + 7) { 0 0 0
if (check_window && gb->io_registers[GB_IO_WX] == pixels + 7) { 0 0 0
1455 0 goto activate_window;
1456 }
1457 0 ▶ DO_PIXEL();
Branch: 4 branches
BranchTrueFalseTotal
DO_PIXEL(); 0 0 0
DO_PIXEL(); 0 0 0
DO_PIXEL(); 0 0 0
DO_PIXEL(); 0 0 0
1458 }
1459 0 tile_x++;
1460 }
1461
1462 0 get_tile_data(gb, tile_x, y, map, &attributes, &data0, &data1);
1463 0 ▶ while (pixels < 160) {
Branch: 1 branch
BranchTrueFalseTotal
while (pixels < 160) { 0 0 0
1464 0 ▶ if (check_window && gb->io_registers[GB_IO_WX] == pixels + 7) {
Branch: 2 branches
BranchTrueFalseTotal
if (check_window && gb->io_registers[GB_IO_WX] == pixels + 7) { 0 0 0
if (check_window && gb->io_registers[GB_IO_WX] == pixels + 7) { 0 0 0
1465 0 goto activate_window;
1466 }
1467 0 ▶ DO_PIXEL();
Branch: 4 branches
BranchTrueFalseTotal
DO_PIXEL(); 0 0 0
DO_PIXEL(); 0 0 0
DO_PIXEL(); 0 0 0
DO_PIXEL(); 0 0 0
1468 }
1469 }
1470  
1471 31824 static inline uint16_t mode3_batching_length(GB_gameboy_t *gb)
1472 {
1473 31824 ▶ if (gb->position_in_line != (uint8_t)-16) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (gb->position_in_line != (uint8_t)-16) return 0; 0 31824 31824
1474 31824 ▶ if (gb->model & GB_MODEL_NO_SFC_BIT) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (gb->model & GB_MODEL_NO_SFC_BIT) return 0; 0 31824 31824
1475 31824 ▶ if (gb->hdma_on) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (gb->hdma_on) return 0; 0 31824 31824
1476 31824 ▶ if (gb->stopped) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (gb->stopped) return 0; 0 31824 31824
1477 31824 ▶ if (GB_is_dma_active(gb)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (GB_is_dma_active(gb)) return 0; 0 31824 31824
1478 31824 ▶ if (gb->wx_triggered) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (gb->wx_triggered) return 0; 0 31824 31824
1479 31824 ▶ if (gb->wy_triggered) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->wy_triggered) { 0 31824 31824
1480 0 ▶ if (gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE) { 0 0 0
1481 0 ▶ if ((gb->io_registers[GB_IO_WX] < 7 || gb->io_registers[GB_IO_WX] == 166 || gb->io_registers[GB_IO_WX] == 167)) {
Branch: 3 branches
BranchTrueFalseTotal
if ((gb->io_registers[GB_IO_WX] < 7 || gb->io_registers[GB_IO_WX] == 166 || gb->io_registers[GB_IO_WX] == 167)) { 0 0 0
if ((gb->io_registers[GB_IO_WX] < 7 || gb->io_registers[GB_IO_WX] == 166 || gb->io_registers[GB_IO_WX] == 167)) { 0 0 0
if ((gb->io_registers[GB_IO_WX] < 7 || gb->io_registers[GB_IO_WX] == 166 || gb->io_registers[GB_IO_WX] == 167)) { 0 0 0
1482 0 return 0;
1483 }
1484 }
1485 else {
1486 0 ▶ if (gb->io_registers[GB_IO_WX] < 167 && !GB_is_cgb(gb)) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->io_registers[GB_IO_WX] < 167 && !GB_is_cgb(gb)) { 0 0 0
if (gb->io_registers[GB_IO_WX] < 167 && !GB_is_cgb(gb)) { 0 0 0
1487 0 return 0;
1488 }
1489 }
1490 }
1491  
1492 // No objects or window, timing is trivial
1493 31824 ▶ if (gb->n_visible_objs == 0 && !(gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE))) return 167 + (gb->io_registers[GB_IO_SCX] & 7);
Branch: 3 branches
BranchTrueFalseTotal
if (gb->n_visible_objs == 0 && !(gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE))) return 167 + (gb->io_registers[GB_IO_SCX] & 7); 31200 624 31824
if (gb->n_visible_objs == 0 && !(gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE))) return 167 + (gb->io_registers[GB_IO_SCX] & 7); 0 31200 31200
if (gb->n_visible_objs == 0 && !(gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE))) return 167 + (gb->io_registers[GB_IO_SCX] & 7); 0 0 0
1494  
1495 624 ▶ if (gb->hdma_on_hblank) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (gb->hdma_on_hblank) return 0; 0 624 624
1496
1497 // 300 is a bit more than the maximum Mode 3 length
1498
1499 // No HBlank interrupt
1500 624 ▶ if (!(gb->io_registers[GB_IO_STAT] & 0x8)) return 300;
Branch: 1 branch
BranchTrueFalseTotal
if (!(gb->io_registers[GB_IO_STAT] & 0x8)) return 300; 624 0 624
1501 // No STAT interrupt requested
1502 0 ▶ if (!(gb->interrupt_enable & 2)) return 300;
Branch: 1 branch
BranchTrueFalseTotal
if (!(gb->interrupt_enable & 2)) return 300; 0 0 0
1503
1504
1505 0 return 0;
1506 }
1507  
1508 96928 static inline uint8_t x_for_object_match(GB_gameboy_t *gb)
1509 {
1510 96928 uint8_t ret = gb->position_in_line + 8;
1511 96928 ▶ if (ret > (uint8_t)-16) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ret > (uint8_t)-16) return 0; 624 96304 96928
1512 96304 return ret;
1513 }
1514  
1515 280 static void update_frame_parity(GB_gameboy_t *gb)
1516 {
1517 280 ▶ if (gb->model <= GB_MODEL_CGB_E) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->model <= GB_MODEL_CGB_E) { 280 0 280
1518 280 gb->is_odd_frame ^= true;
1519 }
1520 else {
1521 // Faster than division, it's normally only once
1522 0 ▶ while (gb->frame_parity_ticks > LCDC_PERIOD * 2) {
Branch: 1 branch
BranchTrueFalseTotal
while (gb->frame_parity_ticks > LCDC_PERIOD * 2) { 0 0 0
1523 0 gb->frame_parity_ticks -= LCDC_PERIOD * 2;
1524 0 gb->is_odd_frame ^= true;
1525 }
1526 }
1527 280 }
1528  
1529 /*
1530 TODO: It seems that the STAT register's mode bits are always "late" by 4 T-cycles.
1531 The PPU logic can be greatly simplified if that delay is simply emulated.
1532 */
1533 5815454 void GB_display_run(GB_gameboy_t *gb, unsigned cycles, bool force)
1534 {
1535 5815454 ▶ if (gb->wy_triggered) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->wy_triggered) { 0 5815454 5815454
1536 0 gb->wy_check_scheduled = false;
1537 }
1538 5815454 ▶ else if (gb->wy_check_scheduled) {
Branch: 1 branch
BranchTrueFalseTotal
else if (gb->wy_check_scheduled) { 7 5815447 5815454
1539 7 force = true;
1540 unsigned cycles_to_check;
1541 // TODO: When speed-switching while the LCD is on, the modulo might be affected. Odd-modes are going to be fun.
1542 7 ▶ if (gb->cgb_double_speed) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->cgb_double_speed) { 0 7 7
1543 0 cycles_to_check = (8 - ((gb->wy_check_modulo + 6) & 7));
1544 }
1545 7 ▶ else if (GB_is_cgb(gb)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (GB_is_cgb(gb)) { 5 2 7
1546 5 cycles_to_check = (8 - ((gb->wy_check_modulo + 0) & 7));
1547 }
1548 else {
1549 2 cycles_to_check = (8 - ((gb->wy_check_modulo + 2) & 7));
1550 }
1551
1552 7 ▶ if (cycles >= cycles_to_check) {
Branch: 1 branch
BranchTrueFalseTotal
if (cycles >= cycles_to_check) { 7 0 7
1553 7 gb->wy_check_scheduled = false;
1554 7 GB_display_run(gb, cycles_to_check, true);
1555 7 wy_check(gb);
1556 7 ▶ if (gb->display_state == 21 && GB_is_cgb(gb) && !gb->cgb_double_speed) {
Branch: 3 branches
BranchTrueFalseTotal
if (gb->display_state == 21 && GB_is_cgb(gb) && !gb->cgb_double_speed) { 0 7 7
if (gb->display_state == 21 && GB_is_cgb(gb) && !gb->cgb_double_speed) { 0 0 0
if (gb->display_state == 21 && GB_is_cgb(gb) && !gb->cgb_double_speed) { 0 0 0
1557 0 gb->wy_just_checked = true;
1558 }
1559 7 cycles -= cycles_to_check;
1560 }
1561 }
1562  
1563 5815454 ▶ if (unlikely((gb->io_registers[GB_IO_LCDC] & GB_LCDC_ENABLE) && (signed)(gb->cycles_for_line * 2 + cycles + gb->display_cycles) > LINE_LENGTH * 2)) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely((gb->io_registers[GB_IO_LCDC] & GB_LCDC_ENABLE) && (signed)(gb->cycles_for_line * 2 + cycles + gb->display_cycles) > LINE_LENGTH * 2)) { 4515439 1300015 5815454
if (unlikely((gb->io_registers[GB_IO_LCDC] & GB_LCDC_ENABLE) && (signed)(gb->cycles_for_line * 2 + cycles + gb->display_cycles) > LINE_LENGTH * 2)) { 0 4515439 4515439
1564 0 unsigned first_batch = (LINE_LENGTH * 2 - gb->cycles_for_line * 2 + gb->display_cycles);
1565 0 GB_display_run(gb, first_batch, force);
1566 0 cycles -= first_batch;
1567 0 ▶ if (gb->display_state == 22) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->display_state == 22) { 0 0 0
1568 0 gb->io_registers[GB_IO_STAT] &= ~3;
1569 0 gb->mode_for_interrupt = 0;
1570 0 GB_STAT_update(gb);
1571 }
1572 0 gb->display_state = 9;
1573 0 gb->display_cycles = 0;
1574 }
1575 5815454 ▶ if (unlikely(gb->delayed_glitch_hblank_interrupt && cycles && gb->current_line < LINES)) {
Branch: 4 branches
BranchTrueFalseTotal
if (unlikely(gb->delayed_glitch_hblank_interrupt && cycles && gb->current_line < LINES)) { 0 5815454 5815454
if (unlikely(gb->delayed_glitch_hblank_interrupt && cycles && gb->current_line < LINES)) { 0 0 0
if (unlikely(gb->delayed_glitch_hblank_interrupt && cycles && gb->current_line < LINES)) { 0 5815454 5815454
if (unlikely(gb->delayed_glitch_hblank_interrupt && cycles && gb->current_line < LINES)) { 0 0 0
1576 0 gb->delayed_glitch_hblank_interrupt = false;
1577 0 gb->mode_for_interrupt = 0;
1578 0 GB_STAT_update(gb);
1579 0 gb->mode_for_interrupt = 3;
1580 }
1581 5815454 gb->cycles_since_vblank_callback += cycles / 2;
1582
1583 5815454 gb->frame_parity_ticks += cycles;
1584 5815454 gb->wy_check_modulo += cycles;
1585
1586 5815454 ▶ if (cycles < gb->frame_repeat_countdown) {
Branch: 1 branch
BranchTrueFalseTotal
if (cycles < gb->frame_repeat_countdown) { 4400573 1414881 5815454
1587 4400573 gb->frame_repeat_countdown -= cycles;
1588 }
1589 else {
1590 1414881 gb->frame_repeat_countdown = 0;
1591 }
1592  
1593 /* The PPU does not advance while in STOP mode on the DMG */
1594 5815454 ▶ if (gb->stopped && !GB_is_cgb(gb)) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->stopped && !GB_is_cgb(gb)) { 0 5815454 5815454
if (gb->stopped && !GB_is_cgb(gb)) { 0 0 0
1595 0 ▶ if (gb->cycles_since_vblank_callback >= LCDC_PERIOD) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->cycles_since_vblank_callback >= LCDC_PERIOD) { 0 0 0
1596 0 GB_display_vblank(gb, GB_VBLANK_TYPE_ARTIFICIAL);
1597 }
1598 0 return;
1599 }
1600
1601 5815454 ▶ GB_BATCHABLE_STATE_MACHINE(gb, display, cycles, 2, !force) {
Branch: 1 branch
BranchTrueFalseTotal
GB_BATCHABLE_STATE_MACHINE(gb, display, cycles, 2, !force) { 3614716 2200738 5815454
1602 64 GB_STATE(gb, display, 1);
1603 6 GB_STATE(gb, display, 2);
1604 121319 GB_STATE(gb, display, 3);
1605 5 GB_STATE(gb, display, 4);
1606 95553 GB_STATE(gb, display, 5);
1607 0 GB_STATE(gb, display, 6);
1608 31818 GB_STATE(gb, display, 7);
1609 572619 GB_STATE(gb, display, 8);
1610 0 GB_STATE(gb, display, 9);
1611 0 GB_STATE(gb, display, 10);
1612 31824 GB_STATE(gb, display, 11);
1613 1941 GB_STATE(gb, display, 12);
1614 1935 GB_STATE(gb, display, 13);
1615 215 GB_STATE(gb, display, 14);
1616 215 GB_STATE(gb, display, 15);
1617 214 GB_STATE(gb, display, 16);
1618 215 GB_STATE(gb, display, 17);
1619
1620 0 GB_STATE(gb, display, 19);
1621 0 GB_STATE(gb, display, 20);
1622 1247093 GB_STATE(gb, display, 21);
1623 0 GB_STATE(gb, display, 22);
1624 0 GB_STATE(gb, display, 23);
1625 0 GB_STATE(gb, display, 24);
1626 0 GB_STATE(gb, display, 26);
1627 0 GB_STATE(gb, display, 27);
1628 0 GB_STATE(gb, display, 28);
1629 215 GB_STATE(gb, display, 29);
1630
1631 31824 GB_STATE(gb, display, 31);
1632 31818 GB_STATE(gb, display, 32);
1633 6 GB_STATE(gb, display, 33);
1634 0 GB_STATE(gb, display, 34);
1635 0 GB_STATE(gb, display, 35);
1636 31818 GB_STATE(gb, display, 36);
1637 6 GB_STATE(gb, display, 37);
1638 0 GB_STATE(gb, display, 38);
1639 0 GB_STATE(gb, display, 39);
1640 0 GB_STATE(gb, display, 40);
1641 0 GB_STATE(gb, display, 41);
1642 0 GB_STATE(gb, display, 42);
1643 0 GB_STATE(gb, display, 43);
1644 }
1645
1646 15 gb->wy_check_modulo = cycles;
1647 15 gb->wy_just_checked = false;
1648
1649 15 ▶ if (!(gb->io_registers[GB_IO_LCDC] & GB_LCDC_ENABLE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(gb->io_registers[GB_IO_LCDC] & GB_LCDC_ENABLE)) { 9 6 15
1650 while (true) {
1651 73 ▶ if (gb->cycles_since_vblank_callback < LCDC_PERIOD) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->cycles_since_vblank_callback < LCDC_PERIOD) { 73 0 73
1652 137 ▶ GB_SLEEP(gb, display, 1, LCDC_PERIOD - gb->cycles_since_vblank_callback);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 1, LCDC_PERIOD - gb->cycles_since_vblank_callback); 73 0 73
1653 }
1654 64 update_frame_parity(gb); // TODO: test actual timing
1655 64 GB_display_vblank(gb, GB_VBLANK_TYPE_LCD_OFF);
1656 }
1657 return;
1658 }
1659
1660 6 ▶ if (!GB_is_cgb(gb)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!GB_is_cgb(gb)) { 1 5 6
1661 1 ▶ GB_SLEEP(gb, display, 23, 1);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 23, 1); 0 1 1
1662 }
1663  
1664 /* Handle mode 2 on the very first line 0 */
1665 6 gb->current_line = 0;
1666 6 gb->window_y = -1;
1667 6 gb->wy_triggered = false;
1668 6 gb->position_in_line = -16;
1669 6 gb->line_has_fractional_scrolling = false;
1670
1671 6 gb->ly_for_comparison = 0;
1672 6 gb->io_registers[GB_IO_STAT] &= ~3;
1673 6 gb->mode_for_interrupt = -1;
1674 6 gb->oam_read_blocked = false;
1675 6 gb->vram_read_blocked = false;
1676 6 gb->oam_write_blocked = false;
1677 6 gb->vram_write_blocked = false;
1678 6 gb->cgb_palettes_blocked = false;
1679 6 gb->cycles_for_line = MODE2_LENGTH - 4;
1680 6 GB_STAT_update(gb);
1681 12 ▶ GB_SLEEP(gb, display, 2, MODE2_LENGTH - 4);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 2, MODE2_LENGTH - 4); 6 0 6
1682
1683 6 gb->oam_write_blocked = true;
1684 6 gb->cycles_for_line += 2;
1685 6 GB_STAT_update(gb);
1686 6 ▶ GB_SLEEP(gb, display, 34, 2);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 34, 2); 0 6 6
1687
1688 6 gb->n_visible_objs = 0;
1689 6 gb->orig_n_visible_objs = 0;
1690 6 gb->cycles_for_line += 8; // Mode 0 is shorter on the first line 0, so we augment cycles_for_line by 8 extra cycles.
1691  
1692 6 gb->io_registers[GB_IO_STAT] &= ~3;
1693 6 gb->io_registers[GB_IO_STAT] |= 3;
1694 6 gb->mode_for_interrupt = 3;
1695  
1696 6 gb->oam_write_blocked = true;
1697 6 gb->oam_read_blocked = true;
1698 6 gb->vram_read_blocked = gb->cgb_double_speed;
1699 6 gb->vram_write_blocked = gb->cgb_double_speed;
1700 6 ▶ if (!GB_is_cgb(gb)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!GB_is_cgb(gb)) { 1 5 6
1701 1 gb->vram_read_blocked = true;
1702 1 gb->vram_write_blocked = true;
1703 }
1704 6 gb->cycles_for_line += 2;
1705 12 ▶ GB_SLEEP(gb, display, 37, 2);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 37, 2); 6 0 6
1706
1707 6 gb->cgb_palettes_blocked = true;
1708 6 gb->cycles_for_line += 3;
1709 6 ▶ GB_SLEEP(gb, display, 38, 3);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 38, 3); 0 6 6
1710
1711 6 gb->vram_read_blocked = true;
1712 6 gb->vram_write_blocked = true;
1713 6 gb->wx_triggered = false;
1714 6 goto mode_3_start;
1715  
1716 // Mode 3 abort, state 9
1717 0 display9: {
1718 // TODO: Timing of things in this scenario is almost completely untested
1719 0 ▶ if (gb->current_line < LINES && !GB_is_sgb(gb) && !gb->disable_rendering) {
Branch: 3 branches
BranchTrueFalseTotal
if (gb->current_line < LINES && !GB_is_sgb(gb) && !gb->disable_rendering) { 0 0 0
if (gb->current_line < LINES && !GB_is_sgb(gb) && !gb->disable_rendering) { 0 0 0
if (gb->current_line < LINES && !GB_is_sgb(gb) && !gb->disable_rendering) { 0 0 0
1720 0 GB_log(gb, "The ROM is preventing line %d from fully rendering, this could damage a real device's LCD display.\n", gb->current_line);
1721 0 uint32_t *dest = NULL;
1722 0 ▶ if (gb->border_mode != GB_BORDER_ALWAYS) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->border_mode != GB_BORDER_ALWAYS) { 0 0 0
1723 0 dest = gb->screen + gb->lcd_x + gb->current_line * WIDTH;
1724 }
1725 else {
1726 0 dest = gb->screen + gb->lcd_x + gb->current_line * BORDERED_WIDTH + (BORDERED_WIDTH - WIDTH) / 2 + (BORDERED_HEIGHT - LINES) / 2 * BORDERED_WIDTH;
1727 }
1728 0 ▶ uint32_t color = GB_is_cgb(gb)? GB_convert_rgb15(gb, 0x7FFF, false) : gb->background_palettes_rgb[4];
Branch: 1 branch
BranchTrueFalseTotal
uint32_t color = GB_is_cgb(gb)? GB_convert_rgb15(gb, 0x7FFF, false) : gb->background_palettes_rgb[4]; 0 0 0
1729 0 ▶ while (gb->lcd_x < 160) {
Branch: 1 branch
BranchTrueFalseTotal
while (gb->lcd_x < 160) { 0 0 0
1730 0 *(dest++) = color;
1731 0 gb->lcd_x++;
1732 }
1733 }
1734 0 gb->n_visible_objs = gb->orig_n_visible_objs;
1735 0 gb->current_line++;
1736 0 wy_check(gb);
1737 0 gb->cycles_for_line = 0;
1738 0 ▶ if (gb->current_line != LINES) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->current_line != LINES) { 0 0 0
1739 0 gb->cycles_for_line = 2;
1740 0 ▶ GB_SLEEP(gb, display, 28, 2);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 28, 2); 0 0 0
1741 0 gb->io_registers[GB_IO_LY] = gb->current_line;
1742 0 ▶ if (gb->position_in_line >= 156 && gb->position_in_line < (uint8_t)-16) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->position_in_line >= 156 && gb->position_in_line < (uint8_t)-16) { 0 0 0
if (gb->position_in_line >= 156 && gb->position_in_line < (uint8_t)-16) { 0 0 0
1743 0 gb->delayed_glitch_hblank_interrupt = true;
1744 }
1745 0 GB_STAT_update(gb);
1746 0 gb->position_in_line = -15;
1747 0 goto mode_3_start;
1748 }
1749 else {
1750 0 ▶ if (gb->position_in_line >= 156 && gb->position_in_line < (uint8_t)-16) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->position_in_line >= 156 && gb->position_in_line < (uint8_t)-16) { 0 0 0
if (gb->position_in_line >= 156 && gb->position_in_line < (uint8_t)-16) { 0 0 0
1751 0 gb->delayed_glitch_hblank_interrupt = true;
1752 }
1753 0 gb->position_in_line = -16;
1754 0 gb->line_has_fractional_scrolling = false;
1755 }
1756 }
1757
1758 while (true) {
1759 /* Lines 0 - 143 */
1760 32039 ▶ for (; gb->current_line < LINES; gb->current_line++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; gb->current_line < LINES; gb->current_line++) { 31818 221 32039
1761 31818 wy_check(gb);
1762
1763 31818 ▶ if (unlikely(gb->lcd_line_callback)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(gb->lcd_line_callback)) { 0 31818 31818
1764 0 gb->lcd_line_callback(gb, gb->current_line);
1765 }
1766
1767 31818 ▶ gb->oam_write_blocked = GB_is_cgb(gb) && !gb->cgb_double_speed;
Branch: 2 branches
BranchTrueFalseTotal
gb->oam_write_blocked = GB_is_cgb(gb) && !gb->cgb_double_speed; 28075 3743 31818
gb->oam_write_blocked = GB_is_cgb(gb) && !gb->cgb_double_speed; 28075 0 28075
1768 31818 gb->accessed_oam_row = 0;
1769
1770 31818 ▶ GB_SLEEP(gb, display, 35, 2);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 35, 2); 0 31818 31818
1771 31818 gb->oam_write_blocked = GB_is_cgb(gb);
1772
1773 31818 ▶ GB_SLEEP(gb, display, 6, 1);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 6, 1); 0 31818 31818
1774 31818 gb->io_registers[GB_IO_LY] = gb->current_line;
1775 31818 ▶ gb->oam_read_blocked = !gb->cgb_double_speed || gb->model >= GB_MODEL_CGB_D;
Branch: 2 branches
BranchTrueFalseTotal
gb->oam_read_blocked = !gb->cgb_double_speed || gb->model >= GB_MODEL_CGB_D; 0 31818 31818
gb->oam_read_blocked = !gb->cgb_double_speed || gb->model >= GB_MODEL_CGB_D; 0 0 0
1776 31818 ▶ gb->ly_for_comparison = gb->current_line? -1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
gb->ly_for_comparison = gb->current_line? -1 : 0; 31603 215 31818
1777
1778 /* The OAM STAT interrupt occurs 1 T-cycle before STAT actually changes, except on line 0.
1779 PPU glitch? */
1780 31818 ▶ if (gb->current_line != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->current_line != 0) { 31603 215 31818
1781 31603 gb->mode_for_interrupt = 2;
1782 31603 gb->io_registers[GB_IO_STAT] &= ~3;
1783 }
1784 215 ▶ else if (!GB_is_cgb(gb)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (!GB_is_cgb(gb)) { 25 190 215
1785 25 gb->io_registers[GB_IO_STAT] &= ~3;
1786 }
1787 31818 GB_STAT_update(gb);
1788  
1789 63636 ▶ GB_SLEEP(gb, display, 7, 1);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 7, 1); 31818 0 31818
1790 31818 gb->oam_read_blocked = true;
1791 31818 gb->io_registers[GB_IO_STAT] &= ~3;
1792 31818 gb->io_registers[GB_IO_STAT] |= 2;
1793 31818 gb->mode_for_interrupt = 2;
1794 31818 gb->oam_write_blocked = true;
1795 31818 gb->ly_for_comparison = gb->current_line;
1796 31818 wy_check(gb);
1797
1798 31818 GB_STAT_update(gb);
1799 31818 gb->mode_for_interrupt = -1;
1800 31818 GB_STAT_update(gb);
1801 31818 gb->n_visible_objs = 0;
1802 31818 gb->orig_n_visible_objs = 0;
1803
1804 31818 ▶ if (!GB_is_dma_active(gb) && !gb->oam_ppu_blocked) {
Branch: 2 branches
BranchTrueFalseTotal
if (!GB_is_dma_active(gb) && !gb->oam_ppu_blocked) { 31818 0 31818
if (!GB_is_dma_active(gb) && !gb->oam_ppu_blocked) { 31818 0 31818
1805 127371 ▶ GB_BATCHPOINT(gb, display, 5, 80);
Branch: 2 branches
BranchTrueFalseTotal
GB_BATCHPOINT(gb, display, 5, 80); 95559 31812 127371
GB_BATCHPOINT(gb, display, 5, 80); 95553 6 95559
1806 }
1807 1304538 ▶ for (gb->oam_search_index = 0; gb->oam_search_index < 40; gb->oam_search_index++) {
Branch: 1 branch
BranchTrueFalseTotal
for (gb->oam_search_index = 0; gb->oam_search_index < 40; gb->oam_search_index++) { 1272720 31818 1304538
1808 1272720 ▶ if (GB_is_cgb(gb)) {
Branch: 1 branch
BranchTrueFalseTotal
if (GB_is_cgb(gb)) { 1123000 149720 1272720
1809 1123000 add_object_from_index(gb, gb->oam_search_index);
1810 /* The CGB does not care about the accessed OAM row as there's no OAM bug */
1811 }
1812 1845339 ▶ GB_SLEEP(gb, display, 8, 2);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 8, 2); 572619 700101 1272720
1813 1272720 ▶ if (!GB_is_cgb(gb)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!GB_is_cgb(gb)) { 149720 1123000 1272720
1814 149720 add_object_from_index(gb, gb->oam_search_index);
1815 149720 gb->accessed_oam_row = (gb->oam_search_index & ~1) * 4 + 8;
1816 }
1817 1272720 ▶ if (gb->oam_search_index == 37) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->oam_search_index == 37) { 31818 1240902 1272720
1818 31818 gb->vram_read_blocked = !GB_is_cgb(gb);
1819 31818 gb->vram_write_blocked = false;
1820 31818 gb->cgb_palettes_blocked = false;
1821 31818 gb->oam_write_blocked = GB_is_cgb(gb);
1822 }
1823 }
1824 31818 gb->cycles_for_line = MODE2_LENGTH + 4;
1825 31818 gb->orig_n_visible_objs = gb->n_visible_objs;
1826 31818 gb->accessed_oam_row = -1;
1827 31818 gb->io_registers[GB_IO_STAT] &= ~3;
1828 31818 gb->io_registers[GB_IO_STAT] |= 3;
1829 31818 gb->mode_for_interrupt = 3;
1830 31818 gb->vram_read_blocked = true;
1831 31818 gb->vram_write_blocked = true;
1832 31818 gb->cgb_palettes_blocked = false;
1833 31818 gb->oam_write_blocked = true;
1834 31818 gb->oam_read_blocked = true;
1835  
1836 31818 GB_STAT_update(gb);
1837  
1838
1839 31818 gb->cycles_for_line += 3;
1840 31818 ▶ GB_SLEEP(gb, display, 10, 3);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 10, 3); 0 31818 31818
1841
1842 31818 gb->cgb_palettes_blocked = true;
1843 31818 gb->cycles_for_line += 2;
1844 63636 ▶ GB_SLEEP(gb, display, 32, 2);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 32, 2); 0 31818 31818
1845 0 mode_3_start:
1846 31824 gb->disable_window_pixel_insertion_glitch = false;
1847  
1848 31824 fifo_clear(&gb->bg_fifo);
1849 31824 fifo_clear(&gb->oam_fifo);
1850 /* Fill the FIFO with 8 pixels of "junk", it's going to be dropped anyway. */
1851 31824 fifo_push_bg_row(&gb->bg_fifo, 0, 0, 0, false, false);
1852 31824 gb->lcd_x = 0;
1853
1854 /* The actual rendering cycle */
1855 31824 gb->fetcher_state = GB_FETCHER_GET_TILE_T1;
1856 31824 ▶ if ((gb->mode3_batching_length = mode3_batching_length(gb))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((gb->mode3_batching_length = mode3_batching_length(gb))) { 31824 0 31824
1857 153143 ▶ GB_BATCHPOINT(gb, display, 3, gb->mode3_batching_length);
Branch: 2 branches
BranchTrueFalseTotal
GB_BATCHPOINT(gb, display, 3, gb->mode3_batching_length); 121324 31819 153143
GB_BATCHPOINT(gb, display, 3, gb->mode3_batching_length); 121319 5 121324
1858 31824 ▶ if (GB_BATCHED_CYCLES(gb, display) >= gb->mode3_batching_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (GB_BATCHED_CYCLES(gb, display) >= gb->mode3_batching_length) { 5 31819 31824
1859 // Successfully batched!
1860 5 gb->lcd_x = gb->position_in_line = 160;
1861 5 gb->cycles_for_line += gb->mode3_batching_length;
1862 5 ▶ if (gb->sgb) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->sgb) { 0 5 5
1863 0 render_line_sgb(gb);
1864 }
1865 else {
1866 5 render_line(gb);
1867 }
1868 10 ▶ GB_SLEEP(gb, display, 4, gb->mode3_batching_length);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 4, gb->mode3_batching_length); 5 0 5
1869 5 goto skip_slow_mode_3;
1870 }
1871 }
1872 while (true) {
1873 /* Handle window */
1874 /* TODO: It appears that WX checks if the window begins *next* pixel, not *this* pixel. For this reason,
1875 WX=167 has no effect at all (It checks if the PPU X position is 161, which never happens) and WX=166
1876 has weird artifacts (It appears to activate the window during HBlank, as PPU X is temporarily 160 at
1877 that point. The code should be updated to represent this, and this will fix the time travel hack in
1878 WX's access conflict code. */
1879 5345592 gb->wx_166_interrupt_glitch = false;
1880 5345592 ▶ if (unlikely(gb->wy_just_checked)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(gb->wy_just_checked)) { 0 5345592 5345592
1881 0 gb->wy_just_checked = false;
1882 }
1883 5345592 ▶ else if (!gb->wx_triggered && gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE)) {
Branch: 3 branches
BranchTrueFalseTotal
else if (!gb->wx_triggered && gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE)) { 5345592 0 5345592
else if (!gb->wx_triggered && gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE)) { 0 5345592 5345592
else if (!gb->wx_triggered && gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE)) { 0 0 0
1884 0 bool should_activate_window = false;
1885 0 ▶ if (unlikely(gb->io_registers[GB_IO_WX] == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(gb->io_registers[GB_IO_WX] == 0)) { 0 0 0
1886 0 ▶ if (gb->position_in_line == (uint8_t)-7) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->position_in_line == (uint8_t)-7) { 0 0 0
1887 0 should_activate_window = true;
1888 }
1889 0 ▶ else if (gb->position_in_line == (uint8_t)-16 && (gb->io_registers[GB_IO_SCX] & 7)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (gb->position_in_line == (uint8_t)-16 && (gb->io_registers[GB_IO_SCX] & 7)) { 0 0 0
else if (gb->position_in_line == (uint8_t)-16 && (gb->io_registers[GB_IO_SCX] & 7)) { 0 0 0
1890 0 should_activate_window = true;
1891 }
1892 0 ▶ else if (gb->position_in_line >= (uint8_t)-15 && gb->position_in_line <= (uint8_t)-8) {
Branch: 2 branches
BranchTrueFalseTotal
else if (gb->position_in_line >= (uint8_t)-15 && gb->position_in_line <= (uint8_t)-8) { 0 0 0
else if (gb->position_in_line >= (uint8_t)-15 && gb->position_in_line <= (uint8_t)-8) { 0 0 0
1893 0 should_activate_window = true;
1894 }
1895 }
1896 0 ▶ else if (gb->io_registers[GB_IO_WX] < 166 + GB_is_cgb(gb)) { // TODO: 166 on the CGB behaves a bit weird
Branch: 1 branch
BranchTrueFalseTotal
else if (gb->io_registers[GB_IO_WX] < 166 + GB_is_cgb(gb)) { // TODO: 166 on the CGB behaves a bit weird 0 0 0
1897 0 ▶ if (gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7)) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7)) { 0 0 0
1898 0 should_activate_window = true;
1899 }
1900 0 ▶ else if (!GB_is_cgb(gb) && gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 6) && !gb->wx_just_changed) {
Branch: 3 branches
BranchTrueFalseTotal
else if (!GB_is_cgb(gb) && gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 6) && !gb->wx_just_changed) { 0 0 0
else if (!GB_is_cgb(gb) && gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 6) && !gb->wx_just_changed) { 0 0 0
else if (!GB_is_cgb(gb) && gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 6) && !gb->wx_just_changed) { 0 0 0
1901 0 should_activate_window = true;
1902 /* LCD-PPU horizontal desync! It only appears to happen on DMGs, but not all of them.
1903 This doesn't seem to be CPU revision dependent, but most revisions */
1904 0 ▶ if ((gb->model & GB_MODEL_FAMILY_MASK) == GB_MODEL_DMG_FAMILY && !GB_is_sgb(gb)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((gb->model & GB_MODEL_FAMILY_MASK) == GB_MODEL_DMG_FAMILY && !GB_is_sgb(gb)) { 0 0 0
if ((gb->model & GB_MODEL_FAMILY_MASK) == GB_MODEL_DMG_FAMILY && !GB_is_sgb(gb)) { 0 0 0
1905 0 ▶ if (gb->lcd_x > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->lcd_x > 0) { 0 0 0
1906 0 gb->lcd_x--;
1907 }
1908 }
1909 }
1910 }
1911
1912 0 ▶ if (should_activate_window) {
Branch: 1 branch
BranchTrueFalseTotal
if (should_activate_window) { 0 0 0
1913 0 gb->window_y++;
1914 0 gb->window_tile_x = 0;
1915 0 fifo_clear(&gb->bg_fifo);
1916 /* TODO: Verify fetcher access timings in this case */
1917 0 ▶ if (gb->io_registers[GB_IO_WX] == 0 && (gb->io_registers[GB_IO_SCX] & 7) && !GB_is_cgb(gb)) {
Branch: 3 branches
BranchTrueFalseTotal
if (gb->io_registers[GB_IO_WX] == 0 && (gb->io_registers[GB_IO_SCX] & 7) && !GB_is_cgb(gb)) { 0 0 0
if (gb->io_registers[GB_IO_WX] == 0 && (gb->io_registers[GB_IO_SCX] & 7) && !GB_is_cgb(gb)) { 0 0 0
if (gb->io_registers[GB_IO_WX] == 0 && (gb->io_registers[GB_IO_SCX] & 7) && !GB_is_cgb(gb)) { 0 0 0
1918 0 gb->cycles_for_line += 1;
1919 0 ▶ GB_SLEEP(gb, display, 42, 1);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 42, 1); 0 0 0
1920 }
1921 0 ▶ else if (gb->io_registers[GB_IO_WX] == 166) {
Branch: 1 branch
BranchTrueFalseTotal
else if (gb->io_registers[GB_IO_WX] == 166) { 0 0 0
1922 0 gb->wx_166_interrupt_glitch = true;
1923 }
1924 0 gb->wx_triggered = true;
1925 0 gb->fetcher_state = GB_FETCHER_GET_TILE_T1;
1926 0 gb->window_is_being_fetched = true;
1927 }
1928 0 ▶ else if (!GB_is_cgb(gb) && gb->io_registers[GB_IO_WX] == 166 && gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7)) {
Branch: 3 branches
BranchTrueFalseTotal
else if (!GB_is_cgb(gb) && gb->io_registers[GB_IO_WX] == 166 && gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7)) { 0 0 0
else if (!GB_is_cgb(gb) && gb->io_registers[GB_IO_WX] == 166 && gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7)) { 0 0 0
else if (!GB_is_cgb(gb) && gb->io_registers[GB_IO_WX] == 166 && gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7)) { 0 0 0
1929 0 gb->window_y++;
1930 }
1931 }
1932  
1933 5345592 ▶ if (unlikely(gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) &&
Branch: 12 branches
BranchTrueFalseTotal
if (unlikely(gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) && 31819 5313773 5345592
if (unlikely(gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) && 28075 3744 31819
if (unlikely(gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) && 28075 0 28075
if (unlikely(gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) && 31819 0 31819
if (unlikely(gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) && 31819 5313773 5345592
if (unlikely(gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) && 0 31819 31819
if (unlikely(gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) && 0 5345592 5345592
if (unlikely(gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) && 0 0 0
if (unlikely(gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) && 0 5345592 5345592
if (unlikely(gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) && 0 0 0
if (unlikely(gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) && 0 5345592 5345592
if (unlikely(gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) && 0 0 0
1934 (!GB_is_cgb(gb) || gb->io_registers[GB_IO_WX] == 0) &&
1935 gb->wx_triggered && !gb->window_is_being_fetched &&
1936 gb->fetcher_state == GB_FETCHER_GET_TILE_T1 &&
1937 gb->bg_fifo.size == 8)) {
1938 // Insert a pixel right at the FIFO's end
1939 0 gb->insert_bg_pixel = true;
1940 }
1941
1942 /* Handle objects */
1943 /* When the object enabled bit is off, this proccess is skipped entirely on the DMG, but not on the CGB.
1944 On the CGB, this bit is checked only when the pixel is actually popped from the FIFO. */
1945
1946 5442936 while (gb->n_visible_objs != 0 &&
1947 96928 gb->objects_x[gb->n_visible_objs - 1] < x_for_object_match(gb)) {
1948 416 gb->n_visible_objs--;
1949 }
1950
1951 5345592 gb->during_object_fetch = true;
1952 5345592 while (gb->n_visible_objs != 0 &&
1953 5345592 ▶ (gb->io_registers[GB_IO_LCDC] & GB_LCDC_OBJ_EN || GB_is_cgb(gb)) &&
Branch: 2 branches
BranchTrueFalseTotal
(gb->io_registers[GB_IO_LCDC] & GB_LCDC_OBJ_EN || GB_is_cgb(gb)) && 5249080 96512 5345592
(gb->io_registers[GB_IO_LCDC] & GB_LCDC_OBJ_EN || GB_is_cgb(gb)) && 96512 0 96512
1954 0 gb->objects_x[gb->n_visible_objs - 1] == x_for_object_match(gb)) {
1955
1956 0 ▶ while (gb->fetcher_state < GB_FETCHER_GET_TILE_DATA_HIGH_T2 || fifo_size(&gb->bg_fifo) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
while (gb->fetcher_state < GB_FETCHER_GET_TILE_DATA_HIGH_T2 || fifo_size(&gb->bg_fifo) == 0) { 0 0 0
while (gb->fetcher_state < GB_FETCHER_GET_TILE_DATA_HIGH_T2 || fifo_size(&gb->bg_fifo) == 0) { 0 0 0
1957 0 advance_fetcher_state_machine(gb, &cycles);
1958 0 gb->cycles_for_line++;
1959 0 ▶ GB_SLEEP(gb, display, 27, 1);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 27, 1); 0 0 0
1960 0 ▶ if (gb->object_fetch_aborted) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->object_fetch_aborted) { 0 0 0
1961 0 goto abort_fetching_object;
1962 }
1963 }
1964
1965 /* TODO: Can this be deleted? { */
1966 0 advance_fetcher_state_machine(gb, &cycles);
1967 0 gb->cycles_for_line++;
1968 0 ▶ GB_SLEEP(gb, display, 41, 1);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 41, 1); 0 0 0
1969 0 ▶ if (gb->object_fetch_aborted) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->object_fetch_aborted) { 0 0 0
1970 0 goto abort_fetching_object;
1971 }
1972 /* } */
1973
1974 0 advance_fetcher_state_machine(gb, &cycles);
1975 0 dma_sync(gb, &cycles);
1976 0 gb->mode2_y_bus = oam_read(gb, gb->visible_objs[gb->n_visible_objs - 1] * 4 + 2);
1977 0 gb->object_flags = oam_read(gb, gb->visible_objs[gb->n_visible_objs - 1] * 4 + 3);
1978
1979 0 gb->cycles_for_line += 2;
1980 0 ▶ GB_SLEEP(gb, display, 20, 2);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 20, 2); 0 0 0
1981 0 ▶ if (gb->object_fetch_aborted) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->object_fetch_aborted) { 0 0 0
1982 0 goto abort_fetching_object;
1983 }
1984
1985 /* TODO: timing not verified */
1986 0 dma_sync(gb, &cycles);
1987 0 gb->object_low_line_address = get_object_line_address(gb,
1988 0 gb->objects_y[gb->n_visible_objs - 1],
1989 0 gb->mode2_y_bus,
1990 0 gb->object_flags);
1991 0 gb->object_tile_data[0] = vram_read(gb, gb->object_low_line_address);
1992  
1993
1994 0 gb->cycles_for_line += 2;
1995 0 ▶ GB_SLEEP(gb, display, 39, 2);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 39, 2); 0 0 0
1996 0 ▶ if (gb->object_fetch_aborted) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->object_fetch_aborted) { 0 0 0
1997 0 goto abort_fetching_object;
1998 }
1999
2000 0 gb->during_object_fetch = false;
2001 0 gb->cycles_for_line++;
2002 0 gb->object_low_line_address = get_object_line_address(gb,
2003 0 gb->objects_y[gb->n_visible_objs - 1],
2004 0 gb->mode2_y_bus,
2005 0 gb->object_flags);
2006  
2007 0 dma_sync(gb, &cycles);
2008 0 gb->object_tile_data[1] = vram_read(gb, gb->object_low_line_address + 1);
2009 0 ▶ GB_SLEEP(gb, display, 40, 1);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 40, 1); 0 0 0
2010  
2011
2012 0 uint8_t palette = (gb->object_flags & 0x10) ? 1 : 0;
2013 0 ▶ if (gb->cgb_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->cgb_mode) { 0 0 0
2014 0 palette = gb->object_flags & 0x7;
2015 }
2016 0 fifo_overlay_object_row(&gb->oam_fifo,
2017 0 gb->object_tile_data[0],
2018 0 gb->object_tile_data[1],
2019 palette,
2020 0 gb->object_flags & 0x80,
2021 0 gb->object_priority == GB_OBJECT_PRIORITY_INDEX? gb->visible_objs[gb->n_visible_objs - 1] : 0,
2022 0 ▶ gb->object_flags & 0x20);
Branch: 1 branch
BranchTrueFalseTotal
gb->object_flags & 0x20); 0 0 0
2023  
2024 0 ▶ gb->data_for_sel_glitch = gb->vram_ppu_blocked? 0xFF : gb->vram[gb->object_low_line_address + 1];
Branch: 1 branch
BranchTrueFalseTotal
gb->data_for_sel_glitch = gb->vram_ppu_blocked? 0xFF : gb->vram[gb->object_low_line_address + 1]; 0 0 0
2025 0 gb->n_visible_objs--;
2026 }
2027
2028 5345592 abort_fetching_object:
2029 5345592 gb->object_fetch_aborted = false;
2030 5345592 gb->during_object_fetch = false;
2031
2032 5345592 render_pixel_if_possible(gb);
2033 5345592 advance_fetcher_state_machine(gb, &cycles);
2034 5345592 ▶ if (gb->position_in_line == 160) break;
Branch: 1 branch
BranchTrueFalseTotal
if (gb->position_in_line == 160) break; 31819 5313773 5345592
2035  
2036 5313773 gb->cycles_for_line++;
2037 6560866 ▶ GB_SLEEP(gb, display, 21, 1);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 21, 1); 1247093 4066680 5313773
2038 5313773 ▶ if (unlikely(gb->wx_166_interrupt_glitch)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(gb->wx_166_interrupt_glitch)) { 0 5313773 5313773
2039 0 gb->mode_for_interrupt = 0;
2040 0 GB_STAT_update(gb);
2041 }
2042 }
2043 31824 skip_slow_mode_3:
2044 31824 gb->position_in_line = -16;
2045 31824 gb->line_has_fractional_scrolling = false;
2046
2047  
2048 /* TODO: Commented code seems incorrect (glitches Tesserae), verify further */
2049  
2050 31824 ▶ if (gb->fetcher_state == GB_FETCHER_GET_TILE_DATA_HIGH_T1 ||
Branch: 1 branch
BranchTrueFalseTotal
if (gb->fetcher_state == GB_FETCHER_GET_TILE_DATA_HIGH_T1 || 31824 0 31824
2051 31824 ▶ gb->fetcher_state == GB_FETCHER_GET_TILE_DATA_HIGH_T2) {
Branch: 1 branch
BranchTrueFalseTotal
gb->fetcher_state == GB_FETCHER_GET_TILE_DATA_HIGH_T2) { 0 31824 31824
2052 // Make sure current_tile_data[1] holds the last tile data byte read
2053 0 gb->current_tile_data[1] = gb->current_tile_data[0];
2054
2055 //gb->data_for_sel_glitch = gb->current_tile_data[0];
2056 }
2057 /*
2058 else {
2059 gb->data_for_sel_glitch = gb->current_tile_data[1];
2060 }
2061 */
2062 31824 ▶ while (gb->lcd_x != 160 && !gb->disable_rendering && gb->screen && !gb->sgb) {
Branch: 4 branches
BranchTrueFalseTotal
while (gb->lcd_x != 160 && !gb->disable_rendering && gb->screen && !gb->sgb) { 0 31824 31824
while (gb->lcd_x != 160 && !gb->disable_rendering && gb->screen && !gb->sgb) { 0 0 0
while (gb->lcd_x != 160 && !gb->disable_rendering && gb->screen && !gb->sgb) { 0 0 0
while (gb->lcd_x != 160 && !gb->disable_rendering && gb->screen && !gb->sgb) { 0 0 0
2063 /* Oh no! The PPU and LCD desynced! Fill the rest of the line with the last color. */
2064 0 uint32_t *dest = NULL;
2065 0 ▶ if (gb->border_mode != GB_BORDER_ALWAYS) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->border_mode != GB_BORDER_ALWAYS) { 0 0 0
2066 0 dest = gb->screen + gb->lcd_x + gb->current_line * WIDTH;
2067 }
2068 else {
2069 0 dest = gb->screen + gb->lcd_x + gb->current_line * BORDERED_WIDTH + (BORDERED_WIDTH - WIDTH) / 2 + (BORDERED_HEIGHT - LINES) / 2 * BORDERED_WIDTH;
2070 }
2071 0 ▶ *dest = (gb->lcd_x == 0)? gb->background_palettes_rgb[0] : dest[-1];
Branch: 1 branch
BranchTrueFalseTotal
*dest = (gb->lcd_x == 0)? gb->background_palettes_rgb[0] : dest[-1]; 0 0 0
2072 0 gb->lcd_x++;
2073  
2074 }
2075
2076 /* TODO: Verify timing { */
2077 31824 ▶ if (gb->current_line == 143) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->current_line == 143) { 221 31603 31824
2078 221 gb->window_y = -1;
2079 }
2080 31824 ▶ if (!GB_is_cgb(gb) && gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE) && gb->io_registers[GB_IO_WX] == 166) {
Branch: 4 branches
BranchTrueFalseTotal
if (!GB_is_cgb(gb) && gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE) && gb->io_registers[GB_IO_WX] == 166) { 3744 28080 31824
if (!GB_is_cgb(gb) && gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE) && gb->io_registers[GB_IO_WX] == 166) { 0 3744 3744
if (!GB_is_cgb(gb) && gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE) && gb->io_registers[GB_IO_WX] == 166) { 0 0 0
if (!GB_is_cgb(gb) && gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & GB_LCDC_WIN_ENABLE) && gb->io_registers[GB_IO_WX] == 166) { 0 0 0
2081 0 gb->wx_triggered = true;
2082 0 gb->window_tile_x = 1;
2083 0 gb->window_y++;
2084 }
2085 else {
2086 31824 gb->wx_triggered = false;
2087 }
2088 /* } */
2089
2090 31824 ▶ if (!gb->cgb_double_speed) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gb->cgb_double_speed) { 31824 0 31824
2091 31824 gb->io_registers[GB_IO_STAT] &= ~3;
2092 31824 gb->mode_for_interrupt = 0;
2093 31824 gb->oam_read_blocked = gb->model >= GB_MODEL_CGB_D;
2094 31824 gb->vram_read_blocked = false;
2095 31824 gb->oam_write_blocked = false;
2096 31824 gb->vram_write_blocked = false;
2097 }
2098
2099 31824 gb->cycles_for_line++;
2100 31824 ▶ GB_SLEEP(gb, display, 22, 1);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 22, 1); 0 31824 31824
2101
2102 31824 gb->io_registers[GB_IO_STAT] &= ~3;
2103 31824 gb->mode_for_interrupt = 0;
2104 31824 gb->oam_read_blocked = false;
2105 31824 gb->vram_read_blocked = false;
2106 31824 gb->oam_write_blocked = false;
2107 31824 gb->vram_write_blocked = false;
2108 31824 GB_STAT_update(gb);
2109  
2110 /* Todo: Measure this value */
2111 31824 gb->cycles_for_line += 2;
2112 31830 ▶ GB_SLEEP(gb, display, 33, 2);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 33, 2); 6 31818 31824
2113 31824 gb->cgb_palettes_blocked = !gb->cgb_double_speed;
2114
2115 31824 ▶ if (gb->hdma_on_hblank && !gb->halted && !gb->stopped) {
Branch: 3 branches
BranchTrueFalseTotal
if (gb->hdma_on_hblank && !gb->halted && !gb->stopped) { 0 31824 31824
if (gb->hdma_on_hblank && !gb->halted && !gb->stopped) { 0 0 0
if (gb->hdma_on_hblank && !gb->halted && !gb->stopped) { 0 0 0
2116 0 gb->hdma_on = true;
2117 }
2118
2119 31824 gb->cycles_for_line += 2;
2120 63642 ▶ GB_SLEEP(gb, display, 36, 2);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 36, 2); 31818 6 31824
2121 31824 gb->cgb_palettes_blocked = false;
2122
2123 31824 ▶ if (gb->cycles_for_line > LINE_LENGTH - 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->cycles_for_line > LINE_LENGTH - 2) { 0 31824 31824
2124 0 gb->cycles_for_line = 0;
2125 0 ▶ GB_SLEEP(gb, display, 43, LINE_LENGTH - gb->cycles_for_line);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 43, LINE_LENGTH - gb->cycles_for_line); 0 0 0
2126 0 goto display9;
2127 }
2128
2129 {
2130 31824 uint16_t cycles_for_line = gb->cycles_for_line;
2131 31824 gb->cycles_for_line = 0;
2132 63648 ▶ GB_SLEEP(gb, display, 11, LINE_LENGTH - cycles_for_line - 2);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 11, LINE_LENGTH - cycles_for_line - 2); 31824 0 31824
2133 }
2134
2135 31824 gb->cycles_for_line = 0;
2136 63648 ▶ GB_SLEEP(gb, display, 31, 2);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 31, 2); 31824 0 31824
2137 31824 ▶ if (gb->current_line != LINES - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->current_line != LINES - 1) { 31603 221 31824
2138 31603 gb->mode_for_interrupt = 2;
2139 }
2140
2141 // Todo: unverified timing
2142 31824 gb->current_lcd_line++;
2143 31824 ▶ if (gb->current_lcd_line == LINES && GB_is_sgb(gb)) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->current_lcd_line == LINES && GB_is_sgb(gb)) { 221 31603 31824
if (gb->current_lcd_line == LINES && GB_is_sgb(gb)) { 0 221 221
2144 0 GB_display_vblank(gb, GB_VBLANK_TYPE_NORMAL_FRAME);
2145 }
2146
2147 31824 ▶ if (gb->icd_hreset_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->icd_hreset_callback) { 0 31824 31824
2148 0 gb->icd_hreset_callback(gb);
2149 }
2150 }
2151 /* Lines 144 - 152 */
2152 2156 ▶ for (; gb->current_line < VIRTUAL_LINES - 1; gb->current_line++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; gb->current_line < VIRTUAL_LINES - 1; gb->current_line++) { 1941 215 2156
2153 1941 gb->ly_for_comparison = -1;
2154 1941 ▶ if (unlikely(gb->lcd_line_callback)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(gb->lcd_line_callback)) { 0 1941 1941
2155 0 gb->lcd_line_callback(gb, gb->current_line);
2156 }
2157 1941 GB_STAT_update(gb);
2158 1941 ▶ GB_SLEEP(gb, display, 26, 2);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 26, 2); 0 1941 1941
2159 1941 gb->io_registers[GB_IO_LY] = gb->current_line;
2160 1941 ▶ if (gb->current_line == LINES && !gb->stat_interrupt_line && (gb->io_registers[GB_IO_STAT] & 0x20)) {
Branch: 3 branches
BranchTrueFalseTotal
if (gb->current_line == LINES && !gb->stat_interrupt_line && (gb->io_registers[GB_IO_STAT] & 0x20)) { 221 1720 1941
if (gb->current_line == LINES && !gb->stat_interrupt_line && (gb->io_registers[GB_IO_STAT] & 0x20)) { 221 0 221
if (gb->current_line == LINES && !gb->stat_interrupt_line && (gb->io_registers[GB_IO_STAT] & 0x20)) { 0 221 221
2161 0 gb->io_registers[GB_IO_IF] |= 2;
2162 }
2163 3882 ▶ GB_SLEEP(gb, display, 12, 2);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 12, 2); 1941 0 1941
2164 1941 ▶ if (gb->delayed_glitch_hblank_interrupt) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->delayed_glitch_hblank_interrupt) { 0 1941 1941
2165 0 gb->delayed_glitch_hblank_interrupt = false;
2166 0 gb->mode_for_interrupt = 0;
2167 }
2168 1941 gb->ly_for_comparison = gb->current_line;
2169 1941 GB_STAT_update(gb);
2170 1941 ▶ GB_SLEEP(gb, display, 24, 1);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 24, 1); 0 1941 1941
2171  
2172 1941 ▶ if (gb->current_line == LINES) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->current_line == LINES) { 221 1720 1941
2173 /* Entering VBlank state triggers the OAM interrupt */
2174 221 gb->io_registers[GB_IO_STAT] &= ~3;
2175 221 gb->io_registers[GB_IO_STAT] |= 1;
2176 221 gb->io_registers[GB_IO_IF] |= 1;
2177 221 ▶ if (!gb->stat_interrupt_line && (gb->io_registers[GB_IO_STAT] & 0x20)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!gb->stat_interrupt_line && (gb->io_registers[GB_IO_STAT] & 0x20)) { 221 0 221
if (!gb->stat_interrupt_line && (gb->io_registers[GB_IO_STAT] & 0x20)) { 0 221 221
2178 0 gb->io_registers[GB_IO_IF] |= 2;
2179 }
2180 221 gb->mode_for_interrupt = 1;
2181 221 GB_STAT_update(gb);
2182
2183 221 ▶ if (gb->frame_skip_state == GB_FRAMESKIP_LCD_TURNED_ON) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->frame_skip_state == GB_FRAMESKIP_LCD_TURNED_ON) { 6 215 221
2184 6 ▶ if (GB_is_cgb(gb)) {
Branch: 1 branch
BranchTrueFalseTotal
if (GB_is_cgb(gb)) { 5 1 6
2185 5 GB_display_vblank(gb, GB_VBLANK_TYPE_NORMAL_FRAME);
2186 5 gb->frame_skip_state = GB_FRAMESKIP_FIRST_FRAME_RENDERED;
2187 }
2188 else {
2189 1 ▶ if (!GB_is_sgb(gb)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!GB_is_sgb(gb)) { 1 0 1
2190 1 update_frame_parity(gb); // TODO: test actual timing
2191 1 GB_display_vblank(gb, GB_VBLANK_TYPE_NORMAL_FRAME);
2192 }
2193 1 gb->frame_skip_state = GB_FRAMESKIP_FIRST_FRAME_RENDERED;
2194 }
2195 }
2196 else {
2197 215 ▶ if (!GB_is_sgb(gb)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!GB_is_sgb(gb)) { 215 0 215
2198 215 update_frame_parity(gb); // TODO: test actual timing
2199 215 GB_display_vblank(gb, GB_VBLANK_TYPE_NORMAL_FRAME);
2200 }
2201 }
2202 }
2203
2204 /* 3640 is just a few cycles less than 4 lines, no clue where the
2205 AGB constant comes from (These are measured and confirmed) */
2206 1941 ▶ gb->frame_repeat_countdown = LINES * LINE_LENGTH * 2 + (gb->model > GB_MODEL_CGB_E? 5982 : 3640); // 8MHz units
Branch: 1 branch
BranchTrueFalseTotal
gb->frame_repeat_countdown = LINES * LINE_LENGTH * 2 + (gb->model > GB_MODEL_CGB_E? 5982 : 3640); // 8MHz units 0 1941 1941
2207 1941 ▶ if (gb->display_cycles < gb->frame_repeat_countdown) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->display_cycles < gb->frame_repeat_countdown) { 1941 0 1941
2208 1941 gb->frame_repeat_countdown -= gb->display_cycles;
2209 }
2210 else {
2211 0 gb->frame_repeat_countdown = 0;
2212 }
2213
2214 3876 ▶ GB_SLEEP(gb, display, 13, LINE_LENGTH - 5);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 13, LINE_LENGTH - 5); 1941 0 1941
2215 }
2216
2217 /* Lines 153 */
2218 215 gb->ly_for_comparison = -1;
2219 215 GB_STAT_update(gb);
2220 215 ▶ GB_SLEEP(gb, display, 19, 2);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 19, 2); 0 215 215
2221 215 gb->io_registers[GB_IO_LY] = 153;
2222 430 ▶ GB_SLEEP(gb, display, 14, (gb->model > GB_MODEL_CGB_C)? 2: 4);
Branch: 2 branches
BranchTrueFalseTotal
GB_SLEEP(gb, display, 14, (gb->model > GB_MODEL_CGB_C)? 2: 4); 190 25 215
GB_SLEEP(gb, display, 14, (gb->model > GB_MODEL_CGB_C)? 2: 4); 215 0 215
2223
2224 215 ▶ if (gb->model <= GB_MODEL_CGB_C && !gb->cgb_double_speed) {
Branch: 2 branches
BranchTrueFalseTotal
if (gb->model <= GB_MODEL_CGB_C && !gb->cgb_double_speed) { 25 190 215
if (gb->model <= GB_MODEL_CGB_C && !gb->cgb_double_speed) { 25 0 25
2225 25 gb->io_registers[GB_IO_LY] = 0;
2226 }
2227 215 gb->ly_for_comparison = 153;
2228 215 GB_STAT_update(gb);
2229 430 ▶ GB_SLEEP(gb, display, 15, (gb->model > GB_MODEL_CGB_C)? 4: 2);
Branch: 2 branches
BranchTrueFalseTotal
GB_SLEEP(gb, display, 15, (gb->model > GB_MODEL_CGB_C)? 4: 2); 190 25 215
GB_SLEEP(gb, display, 15, (gb->model > GB_MODEL_CGB_C)? 4: 2); 215 0 215
2230
2231 215 gb->io_registers[GB_IO_LY] = 0;
2232 215 ▶ gb->ly_for_comparison = (gb->model > GB_MODEL_CGB_C || gb->cgb_double_speed)? 153 : -1;
Branch: 2 branches
BranchTrueFalseTotal
gb->ly_for_comparison = (gb->model > GB_MODEL_CGB_C || gb->cgb_double_speed)? 153 : -1; 25 190 215
gb->ly_for_comparison = (gb->model > GB_MODEL_CGB_C || gb->cgb_double_speed)? 153 : -1; 0 25 25
2233 215 GB_STAT_update(gb);
2234 429 ▶ GB_SLEEP(gb, display, 16, 4);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 16, 4); 214 1 215
2235
2236 215 gb->ly_for_comparison = 0;
2237 215 GB_STAT_update(gb);
2238 430 ▶ GB_SLEEP(gb, display, 29, 12); /* Writing to LYC during this period on a CGB has side effects */
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 29, 12); /* Writing to LYC during this period on a CGB has side effects */ 215 0 215
2239 430 ▶ GB_SLEEP(gb, display, 17, LINE_LENGTH - 24);
Branch: 1 branch
BranchTrueFalseTotal
GB_SLEEP(gb, display, 17, LINE_LENGTH - 24); 215 0 215
2240
2241
2242 215 gb->current_line = 0;
2243 215 gb->wy_triggered = false;
2244
2245 // TODO: not the correct timing
2246 215 gb->current_lcd_line = 0;
2247 215 ▶ if (gb->icd_vreset_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->icd_vreset_callback) { 0 215 215
2248 0 gb->icd_vreset_callback(gb);
2249 }
2250 }
2251 }
2252  
2253 0 void GB_draw_tileset(GB_gameboy_t *gb, uint32_t *dest, GB_palette_type_t palette_type, uint8_t palette_index)
2254 {
2255 uint32_t none_palette[4];
2256 0 uint32_t *palette = NULL;
2257
2258 0 ▶ switch (GB_is_cgb(gb)? palette_type : GB_PALETTE_NONE) {
Branch: 1 branch
BranchTrueFalseTotal
switch (GB_is_cgb(gb)? palette_type : GB_PALETTE_NONE) { 0 0 0
2259 0 default:
2260 case GB_PALETTE_NONE:
2261 0 none_palette[0] = gb->rgb_encode_callback(gb, 0xFF, 0xFF, 0xFF);
2262 0 none_palette[1] = gb->rgb_encode_callback(gb, 0xAA, 0xAA, 0xAA);
2263 0 none_palette[2] = gb->rgb_encode_callback(gb, 0x55, 0x55, 0x55);
2264 0 none_palette[3] = gb->rgb_encode_callback(gb, 0, 0, 0 );
2265 0 palette = none_palette;
2266 0 break;
2267 0 case GB_PALETTE_BACKGROUND:
2268 0 palette = gb->background_palettes_rgb + (4 * (palette_index & 7));
2269 0 break;
2270 0 case GB_PALETTE_OAM:
2271 0 palette = gb->object_palettes_rgb + (4 * (palette_index & 7));
2272 0 break;
2273 }
2274
2275 0 ▶ for (unsigned y = 0; y < 192; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned y = 0; y < 192; y++) { 0 0 0
2276 0 ▶ for (unsigned x = 0; x < 256; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned x = 0; x < 256; x++) { 0 0 0
2277 0 ▶ if (x >= 128 && !GB_is_cgb(gb)) {
Branch: 2 branches
BranchTrueFalseTotal
if (x >= 128 && !GB_is_cgb(gb)) { 0 0 0
if (x >= 128 && !GB_is_cgb(gb)) { 0 0 0
2278 0 *(dest++) = gb->background_palettes_rgb[0];
2279 0 continue;
2280 }
2281 0 uint16_t tile = (x % 128) / 8 + y / 8 * 16;
2282 0 ▶ uint16_t tile_address = tile * 0x10 + (x >= 128? 0x2000 : 0);
Branch: 1 branch
BranchTrueFalseTotal
uint16_t tile_address = tile * 0x10 + (x >= 128? 0x2000 : 0); 0 0 0
2283 0 uint8_t pixel = (((gb->vram[tile_address + (y & 7) * 2 ] >> ((~x)&7)) & 1 ) |
2284 0 ((gb->vram[tile_address + (y & 7) * 2 + 1] >> ((~x)&7)) & 1) << 1);
2285
2286 0 ▶ if (!gb->cgb_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gb->cgb_mode) { 0 0 0
2287 0 ▶ if (palette_type == GB_PALETTE_BACKGROUND) {
Branch: 1 branch
BranchTrueFalseTotal
if (palette_type == GB_PALETTE_BACKGROUND) { 0 0 0
2288 0 pixel = ((gb->io_registers[GB_IO_BGP] >> (pixel << 1)) & 3);
2289 }
2290 0 ▶ else if (!gb->cgb_mode) {
Branch: 1 branch
BranchTrueFalseTotal
else if (!gb->cgb_mode) { 0 0 0
2291 0 ▶ if (palette_type == GB_PALETTE_OAM) {
Branch: 1 branch
BranchTrueFalseTotal
if (palette_type == GB_PALETTE_OAM) { 0 0 0
2292 0 ▶ pixel = ((gb->io_registers[palette_index == 0? GB_IO_OBP0 : GB_IO_OBP1] >> (pixel << 1)) & 3);
Branch: 1 branch
BranchTrueFalseTotal
pixel = ((gb->io_registers[palette_index == 0? GB_IO_OBP0 : GB_IO_OBP1] >> (pixel << 1)) & 3); 0 0 0
2293 }
2294 }
2295 }
2296
2297
2298 0 *(dest++) = palette[pixel];
2299 }
2300 }
2301 0 }
2302  
2303 0 void GB_draw_tilemap(GB_gameboy_t *gb, uint32_t *dest, GB_palette_type_t palette_type, uint8_t palette_index, GB_map_type_t map_type, GB_tileset_type_t tileset_type)
2304 {
2305 uint32_t none_palette[4];
2306 0 uint32_t *palette = NULL;
2307 0 uint16_t map = 0x1800;
2308
2309 0 ▶ switch (GB_is_cgb(gb)? palette_type : GB_PALETTE_NONE) {
Branch: 1 branch
BranchTrueFalseTotal
switch (GB_is_cgb(gb)? palette_type : GB_PALETTE_NONE) { 0 0 0
2310 0 case GB_PALETTE_NONE:
2311 0 none_palette[0] = gb->rgb_encode_callback(gb, 0xFF, 0xFF, 0xFF);
2312 0 none_palette[1] = gb->rgb_encode_callback(gb, 0xAA, 0xAA, 0xAA);
2313 0 none_palette[2] = gb->rgb_encode_callback(gb, 0x55, 0x55, 0x55);
2314 0 none_palette[3] = gb->rgb_encode_callback(gb, 0, 0, 0 );
2315 0 palette = none_palette;
2316 0 break;
2317 0 case GB_PALETTE_BACKGROUND:
2318 0 palette = gb->background_palettes_rgb + (4 * (palette_index & 7));
2319 0 break;
2320 0 case GB_PALETTE_OAM:
2321 0 palette = gb->object_palettes_rgb + (4 * (palette_index & 7));
2322 0 break;
2323 0 case GB_PALETTE_AUTO:
2324 0 break;
2325 }
2326
2327 0 ▶ if (map_type == GB_MAP_9C00 || (map_type == GB_MAP_AUTO && gb->io_registers[GB_IO_LCDC] & GB_LCDC_BG_MAP)) {
Branch: 3 branches
BranchTrueFalseTotal
if (map_type == GB_MAP_9C00 || (map_type == GB_MAP_AUTO && gb->io_registers[GB_IO_LCDC] & GB_LCDC_BG_MAP)) { 0 0 0
if (map_type == GB_MAP_9C00 || (map_type == GB_MAP_AUTO && gb->io_registers[GB_IO_LCDC] & GB_LCDC_BG_MAP)) { 0 0 0
if (map_type == GB_MAP_9C00 || (map_type == GB_MAP_AUTO && gb->io_registers[GB_IO_LCDC] & GB_LCDC_BG_MAP)) { 0 0 0
2328 0 map = 0x1C00;
2329 }
2330
2331 0 ▶ if (tileset_type == GB_TILESET_AUTO) {
Branch: 1 branch
BranchTrueFalseTotal
if (tileset_type == GB_TILESET_AUTO) { 0 0 0
2332 0 ▶ tileset_type = (gb->io_registers[GB_IO_LCDC] & GB_LCDC_TILE_SEL)? GB_TILESET_8800 : GB_TILESET_8000;
Branch: 1 branch
BranchTrueFalseTotal
tileset_type = (gb->io_registers[GB_IO_LCDC] & GB_LCDC_TILE_SEL)? GB_TILESET_8800 : GB_TILESET_8000; 0 0 0
2333 }
2334
2335 0 ▶ for (unsigned y = 0; y < 256; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned y = 0; y < 256; y++) { 0 0 0
2336 0 ▶ for (unsigned x = 0; x < 256; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned x = 0; x < 256; x++) { 0 0 0
2337 0 uint8_t tile = gb->vram[map + x/8 + y/8 * 32];
2338 uint16_t tile_address;
2339 0 uint8_t attributes = 0;
2340
2341 0 ▶ if (tileset_type == GB_TILESET_8800) {
Branch: 1 branch
BranchTrueFalseTotal
if (tileset_type == GB_TILESET_8800) { 0 0 0
2342 0 tile_address = tile * 0x10;
2343 }
2344 else {
2345 0 tile_address = (int8_t) tile * 0x10 + 0x1000;
2346 }
2347
2348 0 ▶ if (gb->cgb_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (gb->cgb_mode) { 0 0 0
2349 0 attributes = gb->vram[map + x/8 + y/8 * 32 + 0x2000];
2350 }
2351
2352 0 ▶ if (attributes & 0x8) {
Branch: 1 branch
BranchTrueFalseTotal
if (attributes & 0x8) { 0 0 0
2353 0 tile_address += 0x2000;
2354 }
2355
2356 0 ▶ uint8_t pixel = (((gb->vram[tile_address + (((attributes & 0x40)? ~y : y) & 7) * 2 ] >> (((attributes & 0x20)? x : ~x)&7)) & 1 ) |
Branch: 2 branches
BranchTrueFalseTotal
uint8_t pixel = (((gb->vram[tile_address + (((attributes & 0x40)? ~y : y) & 7) * 2 ] >> (((attributes & 0x20)? x : ~x)&7)) & 1 ) | 0 0 0
uint8_t pixel = (((gb->vram[tile_address + (((attributes & 0x40)? ~y : y) & 7) * 2 ] >> (((attributes & 0x20)? x : ~x)&7)) & 1 ) | 0 0 0
2357 0 ▶ ((gb->vram[tile_address + (((attributes & 0x40)? ~y : y) & 7) * 2 + 1] >> (((attributes & 0x20)? x : ~x)&7)) & 1) << 1);
Branch: 2 branches
BranchTrueFalseTotal
((gb->vram[tile_address + (((attributes & 0x40)? ~y : y) & 7) * 2 + 1] >> (((attributes & 0x20)? x : ~x)&7)) & 1) << 1); 0 0 0
((gb->vram[tile_address + (((attributes & 0x40)? ~y : y) & 7) * 2 + 1] >> (((attributes & 0x20)? x : ~x)&7)) & 1) << 1); 0 0 0
2358
2359 0 ▶ if (!gb->cgb_mode && (palette_type == GB_PALETTE_BACKGROUND || palette_type == GB_PALETTE_AUTO)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!gb->cgb_mode && (palette_type == GB_PALETTE_BACKGROUND || palette_type == GB_PALETTE_AUTO)) { 0 0 0
if (!gb->cgb_mode && (palette_type == GB_PALETTE_BACKGROUND || palette_type == GB_PALETTE_AUTO)) { 0 0 0
if (!gb->cgb_mode && (palette_type == GB_PALETTE_BACKGROUND || palette_type == GB_PALETTE_AUTO)) { 0 0 0
2360 0 pixel = ((gb->io_registers[GB_IO_BGP] >> (pixel << 1)) & 3);
2361 }
2362
2363 0 ▶ if (palette) {
Branch: 1 branch
BranchTrueFalseTotal
if (palette) { 0 0 0
2364 0 *(dest++) = palette[pixel];
2365 }
2366 else {
2367 0 *(dest++) = gb->background_palettes_rgb[(attributes & 7) * 4 + pixel];
2368 }
2369 }
2370 }
2371 0 }
2372  
2373 0 uint8_t GB_get_oam_info(GB_gameboy_t *gb, GB_oam_info_t *dest, uint8_t *object_height)
2374 {
2375 0 uint8_t count = 0;
2376 0 ▶ *object_height = (gb->io_registers[GB_IO_LCDC] & GB_LCDC_OBJ_SIZE) ? 16:8;
Branch: 1 branch
BranchTrueFalseTotal
*object_height = (gb->io_registers[GB_IO_LCDC] & GB_LCDC_OBJ_SIZE) ? 16:8; 0 0 0
2377 0 uint8_t oam_to_dest_index[40] = {0,};
2378 0 ▶ for (signed y = 0; y < LINES; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (signed y = 0; y < LINES; y++) { 0 0 0
2379 0 object_t *object = (object_t *) &gb->oam;
2380 0 uint8_t objects_in_line = 0;
2381 0 bool obscured = false;
2382 0 ▶ for (uint8_t i = 0; i < 40; i++, object++) {
Branch: 1 branch
BranchTrueFalseTotal
for (uint8_t i = 0; i < 40; i++, object++) { 0 0 0
2383 0 signed object_y = object->y - 16;
2384 // Is object not in this line?
2385 0 ▶ if (object_y > y || object_y + *object_height <= y) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (object_y > y || object_y + *object_height <= y) continue; 0 0 0
if (object_y > y || object_y + *object_height <= y) continue; 0 0 0
2386 0 ▶ if (++objects_in_line == 11) obscured = true;
Branch: 1 branch
BranchTrueFalseTotal
if (++objects_in_line == 11) obscured = true; 0 0 0
2387
2388 0 GB_oam_info_t *info = NULL;
2389 0 ▶ if (!oam_to_dest_index[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (!oam_to_dest_index[i]) { 0 0 0
2390 0 info = dest + count;
2391 0 oam_to_dest_index[i] = ++count;
2392 0 info->x = object->x;
2393 0 info->y = object->y;
2394 0 ▶ info->tile = *object_height == 16? object->tile & 0xFE : object->tile;
Branch: 1 branch
BranchTrueFalseTotal
info->tile = *object_height == 16? object->tile & 0xFE : object->tile; 0 0 0
2395 0 info->flags = object->flags;
2396 0 info->obscured_by_line_limit = false;
2397 0 info->oam_addr = 0xFE00 + i * sizeof(*object);
2398 }
2399 else {
2400 0 info = dest + oam_to_dest_index[i] - 1;
2401 }
2402 0 info->obscured_by_line_limit |= obscured;
2403 }
2404 }
2405
2406 0 ▶ for (unsigned i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 0; i < count; i++) { 0 0 0
2407 0 uint16_t vram_address = dest[i].tile * 0x10;
2408 0 uint8_t flags = dest[i].flags;
2409 0 ▶ uint8_t palette = gb->cgb_mode? (flags & 7) : ((flags & 0x10)? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
uint8_t palette = gb->cgb_mode? (flags & 7) : ((flags & 0x10)? 1 : 0); 0 0 0
2410 0 ▶ if (GB_is_cgb(gb) && (flags & 0x8)) {
Branch: 2 branches
BranchTrueFalseTotal
if (GB_is_cgb(gb) && (flags & 0x8)) { 0 0 0
if (GB_is_cgb(gb) && (flags & 0x8)) { 0 0 0
2411 0 vram_address += 0x2000;
2412 }
2413
2414 0 ▶ uint8_t dmg_palette = gb->io_registers[palette? GB_IO_OBP1:GB_IO_OBP0];
Branch: 1 branch
BranchTrueFalseTotal
uint8_t dmg_palette = gb->io_registers[palette? GB_IO_OBP1:GB_IO_OBP0]; 0 0 0
2415 0 ▶ if (dmg_palette == 0xFF) {
Branch: 1 branch
BranchTrueFalseTotal
if (dmg_palette == 0xFF) { 0 0 0
2416 0 dmg_palette = 0xFC;
2417 }
2418 0 ▶ else if (dmg_palette == 0x00) {
Branch: 1 branch
BranchTrueFalseTotal
else if (dmg_palette == 0x00) { 0 0 0
2419 0 dmg_palette = 0x03;
2420 }
2421  
2422 0 ▶ for (unsigned y = 0; y < *object_height; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned y = 0; y < *object_height; y++) { 0 0 0
2423 0 ▶ unrolled for (unsigned x = 0; x < 8; x++) {
Branch: 1 branch
BranchTrueFalseTotal
unrolled for (unsigned x = 0; x < 8; x++) { 0 0 0
2424 0 uint8_t color = (((gb->vram[vram_address ] >> ((~x)&7)) & 1 ) |
2425 0 ((gb->vram[vram_address + 1] >> ((~x)&7)) & 1) << 1 );
2426
2427 0 ▶ if (!gb->cgb_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gb->cgb_mode) { 0 0 0
2428 0 color = (dmg_palette >> (color << 1)) & 3;
2429 }
2430 0 ▶ dest[i].image[((flags & 0x20)?7-x:x) + ((flags & 0x40)?*object_height - 1 -y:y) * 8] = gb->object_palettes_rgb[palette * 4 + color];
Branch: 2 branches
BranchTrueFalseTotal
dest[i].image[((flags & 0x20)?7-x:x) + ((flags & 0x40)?*object_height - 1 -y:y) * 8] = gb->object_palettes_rgb[palette * 4 + color]; 0 0 0
dest[i].image[((flags & 0x20)?7-x:x) + ((flags & 0x40)?*object_height - 1 -y:y) * 8] = gb->object_palettes_rgb[palette * 4 + color]; 0 0 0
2431 }
2432 0 vram_address += 2;
2433 }
2434 }
2435 0 return count;
2436 }
2437  
2438  
2439 0 bool GB_is_odd_frame(GB_gameboy_t *gb)
2440 {
2441 0 return gb->is_odd_frame;
2442 }
2443  
2444 0 void GB_set_object_rendering_disabled(GB_gameboy_t *gb, bool disabled)
2445 {
2446 0 gb->objects_disabled = disabled;
2447 0 }
2448  
2449 0 void GB_set_background_rendering_disabled(GB_gameboy_t *gb, bool disabled)
2450 {
2451 0 gb->background_disabled = disabled;
2452 0 }
2453  
2454 0 bool GB_is_object_rendering_disabled(GB_gameboy_t *gb)
2455 {
2456 0 return gb->objects_disabled;
2457 }
2458  
2459 0 bool GB_is_background_rendering_disabled(GB_gameboy_t *gb)
2460 {
2461 0 return gb->background_disabled;
2462 }
2463  
2464 13 unsigned GB_get_screen_width(GB_gameboy_t *gb)
2465 {
2466 13 switch (gb->border_mode) {
2467 13 default:
2468 case GB_BORDER_SGB:
2469 13 ▶ return GB_is_hle_sgb(gb)? 256 : 160;
Branch: 1 branch
BranchTrueFalseTotal
return GB_is_hle_sgb(gb)? 256 : 160; 0 13 13
2470 0 case GB_BORDER_NEVER:
2471 0 return 160;
2472 0 case GB_BORDER_ALWAYS:
2473 0 return 256;
2474 }
2475 }
2476  
2477 13 unsigned GB_get_screen_height(GB_gameboy_t *gb)
2478 {
2479 13 switch (gb->border_mode) {
2480 13 default:
2481 case GB_BORDER_SGB:
2482 13 ▶ return GB_is_hle_sgb(gb)? 224 : 144;
Branch: 1 branch
BranchTrueFalseTotal
return GB_is_hle_sgb(gb)? 224 : 144; 0 13 13
2483 0 case GB_BORDER_NEVER:
2484 0 return 144;
2485 0 case GB_BORDER_ALWAYS:
2486 0 return 224;
2487 }
2488 }
2489  
2490 0 double GB_get_usual_frame_rate(GB_gameboy_t *gb)
2491 {
2492 0 return GB_get_clock_rate(gb) / (double)LCDC_PERIOD;
2493 }
2494  
2495 0 void GB_set_enable_skipped_frame_vblank_callbacks(GB_gameboy_t *gb, bool enable)
2496 {
2497 0 gb->enable_skipped_frame_vblank_callbacks = enable;
2498 0 }
« src/Core/joypad.c ↑ Summary src/Core/sm83_cpu.c »