| 1 |
|
| #include <stdio.h> |
| 2 |
|
| #include <stdbool.h> |
| 3 |
|
| #include <assert.h> |
| 4 |
|
| #include "gb.h" |
| 5 |
|
| |
| 6 |
|
| |
| 7 |
|
| typedef void opcode_t(GB_gameboy_t *gb, uint8_t opcode); |
| 8 |
|
| |
| 9 |
|
| typedef enum { |
| 10 |
|
| /* Default behavior. If the CPU writes while another component reads, it reads the old value */ |
| 11 |
|
| GB_CONFLICT_READ_OLD, |
| 12 |
|
| /* If the CPU writes while another component reads, it reads the new value */ |
| 13 |
|
| GB_CONFLICT_READ_NEW, |
| 14 |
|
| /* If the CPU and another component write at the same time, the CPU's value "wins" */ |
| 15 |
|
| GB_CONFLICT_WRITE_CPU, |
| 16 |
|
| /* Register specific values */ |
| 17 |
|
| GB_CONFLICT_STAT_CGB, |
| 18 |
|
| GB_CONFLICT_STAT_DMG, |
| 19 |
|
| GB_CONFLICT_PALETTE_DMG, |
| 20 |
|
| GB_CONFLICT_PALETTE_CGB, |
| 21 |
|
| GB_CONFLICT_DMG_LCDC, |
| 22 |
|
| GB_CONFLICT_SGB_LCDC, |
| 23 |
|
| GB_CONFLICT_WX_DMG, |
| 24 |
|
| GB_CONFLICT_LCDC_CGB, |
| 25 |
|
| GB_CONFLICT_LCDC_CGB_DOUBLE, |
| 26 |
|
| GB_CONFLICT_STAT_CGB_DOUBLE, |
| 27 |
|
| GB_CONFLICT_NR10_CGB_DOUBLE, |
| 28 |
|
| GB_CONFLICT_SCX_DMG_AND_CGB_DOUBLE, |
| 29 |
|
| } conflict_t; |
| 30 |
|
| |
| 31 |
|
| static const conflict_t cgb_conflict_map[0x80] = { |
| 32 |
|
| [GB_IO_LCDC] = GB_CONFLICT_LCDC_CGB, |
| 33 |
|
| [GB_IO_IF] = GB_CONFLICT_WRITE_CPU, |
| 34 |
|
| [GB_IO_LYC] = GB_CONFLICT_WRITE_CPU, |
| 35 |
|
| [GB_IO_WY] = GB_CONFLICT_READ_OLD, |
| 36 |
|
| [GB_IO_STAT] = GB_CONFLICT_STAT_CGB, |
| 37 |
|
| [GB_IO_BGP] = GB_CONFLICT_PALETTE_CGB, |
| 38 |
|
| [GB_IO_OBP0] = GB_CONFLICT_PALETTE_CGB, |
| 39 |
|
| [GB_IO_OBP1] = GB_CONFLICT_PALETTE_CGB, |
| 40 |
|
| [GB_IO_SCX] = GB_CONFLICT_READ_OLD, |
| 41 |
|
| [GB_IO_WX] = GB_CONFLICT_WRITE_CPU, |
| 42 |
|
| }; |
| 43 |
|
| |
| 44 |
|
| static const conflict_t cgb_double_conflict_map[0x80] = { |
| 45 |
|
| [GB_IO_LCDC] = GB_CONFLICT_LCDC_CGB_DOUBLE, |
| 46 |
|
| [GB_IO_IF] = GB_CONFLICT_WRITE_CPU, |
| 47 |
|
| [GB_IO_LYC] = GB_CONFLICT_READ_OLD, |
| 48 |
|
| [GB_IO_WY] = GB_CONFLICT_READ_OLD, |
| 49 |
|
| [GB_IO_STAT] = GB_CONFLICT_STAT_CGB_DOUBLE, |
| 50 |
|
| [GB_IO_NR10] = GB_CONFLICT_NR10_CGB_DOUBLE, |
| 51 |
|
| [GB_IO_SCX] = GB_CONFLICT_SCX_DMG_AND_CGB_DOUBLE, |
| 52 |
|
| [GB_IO_WX] = GB_CONFLICT_READ_OLD, |
| 53 |
|
| }; |
| 54 |
|
| |
| 55 |
|
| /* Todo: verify on an MGB */ |
| 56 |
|
| static const conflict_t dmg_conflict_map[0x80] = { |
| 57 |
|
| [GB_IO_IF] = GB_CONFLICT_WRITE_CPU, |
| 58 |
|
| [GB_IO_LYC] = GB_CONFLICT_READ_OLD, |
| 59 |
|
| [GB_IO_LCDC] = GB_CONFLICT_DMG_LCDC, |
| 60 |
|
| [GB_IO_SCY] = GB_CONFLICT_READ_NEW, |
| 61 |
|
| [GB_IO_STAT] = GB_CONFLICT_STAT_DMG, |
| 62 |
|
| [GB_IO_BGP] = GB_CONFLICT_PALETTE_DMG, |
| 63 |
|
| [GB_IO_OBP0] = GB_CONFLICT_PALETTE_DMG, |
| 64 |
|
| [GB_IO_OBP1] = GB_CONFLICT_PALETTE_DMG, |
| 65 |
|
| [GB_IO_WY] = GB_CONFLICT_READ_OLD, |
| 66 |
|
| [GB_IO_WX] = GB_CONFLICT_WX_DMG, |
| 67 |
|
| [GB_IO_SCX] = GB_CONFLICT_SCX_DMG_AND_CGB_DOUBLE, |
| 68 |
|
| }; |
| 69 |
|
| |
| 70 |
|
| /* Todo: Verify on an SGB1 */ |
| 71 |
|
| static const conflict_t sgb_conflict_map[0x80] = { |
| 72 |
|
| [GB_IO_IF] = GB_CONFLICT_WRITE_CPU, |
| 73 |
|
| [GB_IO_LYC] = GB_CONFLICT_READ_OLD, |
| 74 |
|
| [GB_IO_LCDC] = GB_CONFLICT_SGB_LCDC, |
| 75 |
|
| [GB_IO_SCY] = GB_CONFLICT_READ_NEW, |
| 76 |
|
| [GB_IO_STAT] = GB_CONFLICT_STAT_DMG, |
| 77 |
|
| [GB_IO_BGP] = GB_CONFLICT_READ_NEW, |
| 78 |
|
| [GB_IO_OBP0] = GB_CONFLICT_READ_NEW, |
| 79 |
|
| [GB_IO_OBP1] = GB_CONFLICT_READ_NEW, |
| 80 |
|
| [GB_IO_WY] = GB_CONFLICT_READ_OLD, |
| 81 |
|
| [GB_IO_WX] = GB_CONFLICT_WX_DMG, |
| 82 |
|
| [GB_IO_SCX] = GB_CONFLICT_SCX_DMG_AND_CGB_DOUBLE, |
| 83 |
|
| }; |
| 84 |
|
| |
| 85 |
4104911 |
| static uint8_t cycle_read(GB_gameboy_t *gb, uint16_t addr) |
| 86 |
|
| { |
| 87 |
4104911 |
▶ | if (gb->pending_cycles) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->pending_cycles) { |
2240458 |
1864453 |
4104911 |
|
| 88 |
2240458 |
| GB_advance_cycles(gb, gb->pending_cycles); |
| 89 |
|
| } |
| 90 |
4104911 |
| gb->address_bus = addr; |
| 91 |
4104911 |
| uint8_t ret = GB_read_memory(gb, addr); |
| 92 |
4104911 |
| gb->pending_cycles = 4; |
| 93 |
4104911 |
| return ret; |
| 94 |
|
| } |
| 95 |
|
| |
| 96 |
|
| /* A special case for IF during ISR, returns the old value of IF. */ |
| 97 |
|
| /* TODO: Verify the timing, it might be wrong in cases where, in the same M cycle, IF |
| 98 |
|
| is both read be the CPU, modified by the ISR, and modified by an actual interrupt. |
| 99 |
|
| If this timing proves incorrect, the ISR emulation must be updated so IF reads are |
| 100 |
|
| timed correctly. */ |
| 101 |
|
| /* TODO: Does this affect the address bus? Verify. */ |
| 102 |
0 |
| static uint8_t cycle_write_if(GB_gameboy_t *gb, uint8_t value) |
| 103 |
|
| { |
| 104 |
|
| assert(gb->pending_cycles); |
| 105 |
0 |
| GB_advance_cycles(gb, gb->pending_cycles); |
| 106 |
0 |
| gb->address_bus = 0xFF00 + GB_IO_IF; |
| 107 |
0 |
| uint8_t old = (gb->io_registers[GB_IO_IF]) & 0x1F; |
| 108 |
0 |
| GB_write_memory(gb, 0xFF00 + GB_IO_IF, value); |
| 109 |
0 |
| gb->pending_cycles = 4; |
| 110 |
0 |
| return old; |
| 111 |
|
| } |
| 112 |
|
| |
| 113 |
142787 |
| static void cycle_write(GB_gameboy_t *gb, uint16_t addr, uint8_t value) |
| 114 |
|
| { |
| 115 |
|
| assert(gb->pending_cycles); |
| 116 |
142787 |
| conflict_t conflict = GB_CONFLICT_READ_OLD; |
| 117 |
142787 |
▶ | if ((addr & 0xFF80) == 0xFF00) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((addr & 0xFF80) == 0xFF00) { |
1494 |
141293 |
142787 |
|
| 118 |
1494 |
| const conflict_t *map = NULL; |
| 119 |
1494 |
▶ | if (GB_is_cgb(gb)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GB_is_cgb(gb)) { |
1439 |
55 |
1494 |
|
| 120 |
1439 |
▶ | map = gb->cgb_double_speed? cgb_double_conflict_map : cgb_conflict_map; |
Branch: 1 branch
| Branch | True | False | Total |
| map = gb->cgb_double_speed? cgb_double_conflict_map : cgb_conflict_map; |
0 |
1439 |
1439 |
|
| 121 |
|
| } |
| 122 |
55 |
▶ | else if (GB_is_sgb(gb)) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (GB_is_sgb(gb)) { |
0 |
55 |
55 |
|
| 123 |
0 |
| map = sgb_conflict_map; |
| 124 |
|
| } |
| 125 |
|
| else { |
| 126 |
55 |
| map = dmg_conflict_map; |
| 127 |
|
| } |
| 128 |
1494 |
| conflict = map[addr & 0x7F]; |
| 129 |
|
| } |
| 130 |
142787 |
| switch (conflict) { |
| 131 |
142534 |
| case GB_CONFLICT_READ_OLD: |
| 132 |
142534 |
| GB_advance_cycles(gb, gb->pending_cycles); |
| 133 |
142534 |
| GB_write_memory(gb, addr, value); |
| 134 |
142534 |
| gb->pending_cycles = 4; |
| 135 |
142534 |
| break; |
| 136 |
|
| |
| 137 |
16 |
| case GB_CONFLICT_READ_NEW: |
| 138 |
16 |
| GB_advance_cycles(gb, gb->pending_cycles - 1); |
| 139 |
16 |
| GB_write_memory(gb, addr, value); |
| 140 |
16 |
| gb->pending_cycles = 5; |
| 141 |
16 |
| break; |
| 142 |
|
| |
| 143 |
221 |
| case GB_CONFLICT_WRITE_CPU: |
| 144 |
221 |
| GB_advance_cycles(gb, gb->pending_cycles + 1); |
| 145 |
221 |
| GB_write_memory(gb, addr, value); |
| 146 |
221 |
| gb->pending_cycles = 3; |
| 147 |
221 |
| break; |
| 148 |
|
| |
| 149 |
|
| /* The DMG STAT-write bug is basically the STAT register being read as FF for a single T-cycle */ |
| 150 |
0 |
| case GB_CONFLICT_STAT_DMG: |
| 151 |
0 |
| GB_advance_cycles(gb, gb->pending_cycles); |
| 152 |
0 |
| GB_display_sync(gb); |
| 153 |
|
| /* State 7 is the edge between HBlank and OAM mode, and it behaves a bit weird. |
| 154 |
|
| The OAM interrupt seems to be blocked by HBlank interrupts in that case, despite |
| 155 |
|
| the timing not making much sense for that. |
| 156 |
|
| This is a hack to simulate this effect */ |
| 157 |
0 |
▶ | if (gb->display_state == 7 && (gb->io_registers[GB_IO_STAT] & 0x28) == 0x08) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (gb->display_state == 7 && (gb->io_registers[GB_IO_STAT] & 0x28) == 0x08) { |
0 |
0 |
0 |
| if (gb->display_state == 7 && (gb->io_registers[GB_IO_STAT] & 0x28) == 0x08) { |
0 |
0 |
0 |
|
| 158 |
0 |
| GB_write_memory(gb, addr, ~0x20); |
| 159 |
|
| } |
| 160 |
|
| else { |
| 161 |
0 |
| GB_write_memory(gb, addr, 0xFF); |
| 162 |
|
| } |
| 163 |
0 |
| GB_advance_cycles(gb, 1); |
| 164 |
0 |
| GB_write_memory(gb, addr, value); |
| 165 |
0 |
| gb->pending_cycles = 3; |
| 166 |
0 |
| break; |
| 167 |
|
| |
| 168 |
0 |
| case GB_CONFLICT_STAT_CGB: { |
| 169 |
|
| /* Todo: Verify this with SCX adjustments */ |
| 170 |
|
| /* The LYC bit behaves differently */ |
| 171 |
0 |
| uint8_t old_value = gb->io_registers[GB_IO_STAT]; |
| 172 |
0 |
| GB_advance_cycles(gb, gb->pending_cycles); |
| 173 |
0 |
| GB_write_memory(gb, addr, (old_value & 0x40) | (value & ~0x40)); |
| 174 |
0 |
| GB_advance_cycles(gb, 1); |
| 175 |
0 |
| GB_write_memory(gb, addr, value); |
| 176 |
0 |
| gb->pending_cycles = 3; |
| 177 |
0 |
| break; |
| 178 |
|
| } |
| 179 |
|
| |
| 180 |
0 |
| case GB_CONFLICT_STAT_CGB_DOUBLE: { |
| 181 |
0 |
| uint8_t old_value = gb->io_registers[GB_IO_STAT]; |
| 182 |
0 |
| GB_advance_cycles(gb, gb->pending_cycles); |
| 183 |
0 |
| GB_write_memory(gb, addr, (value & ~8) | (old_value & 8)); |
| 184 |
0 |
| GB_advance_cycles(gb, 1); |
| 185 |
0 |
| GB_write_memory(gb, addr, value); |
| 186 |
0 |
| gb->pending_cycles = 3; |
| 187 |
0 |
| break; |
| 188 |
|
| } |
| 189 |
|
| |
| 190 |
|
| /* There is some "time travel" going on with these two values, as it appears |
| 191 |
|
| that there's some off-by-1-T-cycle timing issue in the PPU implementation. |
| 192 |
|
| |
| 193 |
|
| This is should be accurate for every measureable scenario, though. */ |
| 194 |
|
| |
| 195 |
3 |
| case GB_CONFLICT_PALETTE_DMG: { |
| 196 |
3 |
| GB_advance_cycles(gb, gb->pending_cycles - 2); |
| 197 |
3 |
| uint8_t old_value = GB_read_memory(gb, addr); |
| 198 |
3 |
| GB_write_memory(gb, addr, value | old_value); |
| 199 |
3 |
| GB_advance_cycles(gb, 1); |
| 200 |
3 |
| GB_write_memory(gb, addr, value); |
| 201 |
3 |
| gb->pending_cycles = 5; |
| 202 |
3 |
| break; |
| 203 |
|
| } |
| 204 |
|
| |
| 205 |
7 |
| case GB_CONFLICT_PALETTE_CGB: { |
| 206 |
7 |
▶ | if (gb->model >= GB_MODEL_CGB_D) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->model >= GB_MODEL_CGB_D) { |
7 |
0 |
7 |
|
| 207 |
7 |
| GB_advance_cycles(gb, gb->pending_cycles - 2); |
| 208 |
7 |
| GB_write_memory(gb, addr, value); |
| 209 |
7 |
| gb->pending_cycles = 6; |
| 210 |
|
| } |
| 211 |
|
| else { |
| 212 |
0 |
| GB_advance_cycles(gb, gb->pending_cycles - 1); |
| 213 |
0 |
| GB_write_memory(gb, addr, value); |
| 214 |
0 |
| gb->pending_cycles = 5; |
| 215 |
|
| } |
| 216 |
7 |
| break; |
| 217 |
|
| } |
| 218 |
|
| |
| 219 |
1 |
| case GB_CONFLICT_DMG_LCDC: { |
| 220 |
|
| /* Similar to the palette registers, these interact directly with the LCD, so they appear to be affected by |
| 221 |
|
| it. Both my DMG (B, blob) and Game Boy Light behave this way though. |
| 222 |
|
| |
| 223 |
|
| Additionally, LCDC.1 is very nasty because it is read both by the FIFO when popping pixels, and the |
| 224 |
|
| object-fetching state machine, and both behave differently when it comes to access conflicts. Hacks ahead. |
| 225 |
|
| */ |
| 226 |
|
| |
| 227 |
1 |
| uint8_t old_value = GB_read_memory(gb, addr); |
| 228 |
1 |
| GB_advance_cycles(gb, gb->pending_cycles - 2); |
| 229 |
1 |
| GB_display_sync(gb); |
| 230 |
1 |
▶ | if (gb->model != GB_MODEL_MGB && gb->position_in_line == 0 && !(value & GB_LCDC_OBJ_EN)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (gb->model != GB_MODEL_MGB && gb->position_in_line == 0 && !(value & GB_LCDC_OBJ_EN)) { |
1 |
0 |
1 |
| if (gb->model != GB_MODEL_MGB && gb->position_in_line == 0 && !(value & GB_LCDC_OBJ_EN)) { |
1 |
0 |
1 |
| if (gb->model != GB_MODEL_MGB && gb->position_in_line == 0 && !(value & GB_LCDC_OBJ_EN)) { |
1 |
0 |
1 |
|
| 231 |
1 |
| old_value &= ~GB_LCDC_OBJ_EN; |
| 232 |
|
| } |
| 233 |
0 |
▶ | else if (gb->during_object_fetch && !(value & GB_LCDC_OBJ_EN)) { |
Branch: 2 branches
| Branch | True | False | Total |
| else if (gb->during_object_fetch && !(value & GB_LCDC_OBJ_EN)) { |
0 |
0 |
0 |
| else if (gb->during_object_fetch && !(value & GB_LCDC_OBJ_EN)) { |
0 |
0 |
0 |
|
| 234 |
0 |
| old_value &= ~GB_LCDC_OBJ_EN; |
| 235 |
|
| } |
| 236 |
|
| |
| 237 |
1 |
| GB_write_memory(gb, addr, old_value | (value & GB_LCDC_BG_EN)); |
| 238 |
1 |
| GB_advance_cycles(gb, 1); |
| 239 |
1 |
| GB_write_memory(gb, addr, value); |
| 240 |
|
| |
| 241 |
1 |
▶ | if ((old_value & GB_LCDC_WIN_ENABLE) && !(value & GB_LCDC_WIN_ENABLE) && gb->window_is_being_fetched) { |
Branch: 3 branches
| Branch | True | False | Total |
| if ((old_value & GB_LCDC_WIN_ENABLE) && !(value & GB_LCDC_WIN_ENABLE) && gb->window_is_being_fetched) { |
0 |
1 |
1 |
| if ((old_value & GB_LCDC_WIN_ENABLE) && !(value & GB_LCDC_WIN_ENABLE) && gb->window_is_being_fetched) { |
0 |
0 |
0 |
| if ((old_value & GB_LCDC_WIN_ENABLE) && !(value & GB_LCDC_WIN_ENABLE) && gb->window_is_being_fetched) { |
0 |
0 |
0 |
|
| 242 |
0 |
| gb->disable_window_pixel_insertion_glitch = true; |
| 243 |
|
| } |
| 244 |
1 |
| gb->pending_cycles = 5; |
| 245 |
1 |
| break; |
| 246 |
|
| } |
| 247 |
|
| |
| 248 |
0 |
| case GB_CONFLICT_SGB_LCDC: { |
| 249 |
|
| /* Simplified version of the above */ |
| 250 |
|
| |
| 251 |
0 |
| uint8_t old_value = GB_read_memory(gb, addr); |
| 252 |
0 |
| GB_advance_cycles(gb, gb->pending_cycles - 2); |
| 253 |
|
| /* Hack to force aborting object fetch */ |
| 254 |
0 |
| GB_write_memory(gb, addr, value); |
| 255 |
0 |
| GB_write_memory(gb, addr, old_value); |
| 256 |
0 |
| GB_advance_cycles(gb, 1); |
| 257 |
0 |
| GB_write_memory(gb, addr, value); |
| 258 |
0 |
| gb->pending_cycles = 5; |
| 259 |
0 |
| break; |
| 260 |
|
| } |
| 261 |
|
| |
| 262 |
0 |
| case GB_CONFLICT_WX_DMG: |
| 263 |
0 |
| GB_advance_cycles(gb, gb->pending_cycles); |
| 264 |
0 |
| GB_write_memory(gb, addr, value); |
| 265 |
0 |
| gb->wx_just_changed = true; |
| 266 |
0 |
| GB_advance_cycles(gb, 1); |
| 267 |
0 |
| gb->wx_just_changed = false; |
| 268 |
0 |
| gb->pending_cycles = 3; |
| 269 |
0 |
| break; |
| 270 |
|
| |
| 271 |
5 |
| case GB_CONFLICT_LCDC_CGB: { |
| 272 |
5 |
| uint8_t old = gb->io_registers[GB_IO_LCDC]; |
| 273 |
5 |
▶ | if ((~value & old) & GB_LCDC_TILE_SEL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((~value & old) & GB_LCDC_TILE_SEL) { |
0 |
5 |
5 |
|
| 274 |
0 |
| GB_advance_cycles(gb, gb->pending_cycles); |
| 275 |
0 |
| GB_write_memory(gb, addr, value); |
| 276 |
0 |
| gb->tile_sel_glitch = true; |
| 277 |
0 |
| GB_advance_cycles(gb, 1); |
| 278 |
0 |
| gb->tile_sel_glitch = false; |
| 279 |
0 |
| gb->pending_cycles = 3; |
| 280 |
|
| } |
| 281 |
|
| else { |
| 282 |
5 |
| GB_advance_cycles(gb, gb->pending_cycles); |
| 283 |
5 |
| GB_write_memory(gb, addr, value); |
| 284 |
5 |
| gb->pending_cycles = 4; |
| 285 |
|
| } |
| 286 |
5 |
| break; |
| 287 |
|
| } |
| 288 |
0 |
| case GB_CONFLICT_LCDC_CGB_DOUBLE: { |
| 289 |
0 |
| uint8_t old = gb->io_registers[GB_IO_LCDC]; |
| 290 |
|
| // TODO: Verify for CGB ≤ C for BG_EN and OBJ_EN. |
| 291 |
0 |
| GB_advance_cycles(gb, gb->pending_cycles - 2); |
| 292 |
0 |
| GB_write_memory(gb, addr, (value & ~(GB_LCDC_BG_EN | GB_LCDC_ENABLE)) | (old & (GB_LCDC_BG_EN | GB_LCDC_ENABLE))); |
| 293 |
|
| // TODO: This condition is different from single speed mode. Why? What about odd modes? |
| 294 |
0 |
| gb->tile_sel_glitch = ((value ^ old) & GB_LCDC_TILE_SEL); |
| 295 |
0 |
| GB_advance_cycles(gb, 2); |
| 296 |
0 |
| gb->tile_sel_glitch = false; |
| 297 |
0 |
| GB_write_memory(gb, addr, value); |
| 298 |
0 |
| gb->pending_cycles = 4; |
| 299 |
0 |
| break; |
| 300 |
|
| } |
| 301 |
|
| |
| 302 |
0 |
| case GB_CONFLICT_SCX_DMG_AND_CGB_DOUBLE: |
| 303 |
0 |
| GB_advance_cycles(gb, gb->pending_cycles - 2); |
| 304 |
0 |
| GB_write_memory(gb, addr, value); |
| 305 |
0 |
| gb->pending_cycles = 6; |
| 306 |
0 |
| break; |
| 307 |
|
| |
| 308 |
0 |
| case GB_CONFLICT_NR10_CGB_DOUBLE: { |
| 309 |
0 |
| GB_advance_cycles(gb, gb->pending_cycles - 1); |
| 310 |
0 |
▶ | gb->apu_output.square_sweep_disable_stepping = gb->model <= GB_MODEL_CGB_C && (value & 7) == 0; |
Branch: 2 branches
| Branch | True | False | Total |
| gb->apu_output.square_sweep_disable_stepping = gb->model <= GB_MODEL_CGB_C && (value & 7) == 0; |
0 |
0 |
0 |
| gb->apu_output.square_sweep_disable_stepping = gb->model <= GB_MODEL_CGB_C && (value & 7) == 0; |
0 |
0 |
0 |
|
| 311 |
0 |
| GB_advance_cycles(gb, 1); |
| 312 |
0 |
| gb->apu_output.square_sweep_disable_stepping = false; |
| 313 |
0 |
| GB_write_memory(gb, addr, value); |
| 314 |
0 |
| gb->pending_cycles = 4; |
| 315 |
0 |
| break; |
| 316 |
|
| } |
| 317 |
|
| } |
| 318 |
142787 |
| gb->address_bus = addr; |
| 319 |
142787 |
| } |
| 320 |
|
| |
| 321 |
2128 |
| static void cycle_no_access(GB_gameboy_t *gb) |
| 322 |
|
| { |
| 323 |
2128 |
| gb->pending_cycles += 4; |
| 324 |
2128 |
| } |
| 325 |
|
| |
| 326 |
795453 |
| static void cycle_oam_bug(GB_gameboy_t *gb, uint8_t register_id) |
| 327 |
|
| { |
| 328 |
795453 |
▶ | if (gb->pending_cycles) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->pending_cycles) { |
795453 |
0 |
795453 |
|
| 329 |
795453 |
| GB_advance_cycles(gb, gb->pending_cycles); |
| 330 |
|
| } |
| 331 |
795453 |
| gb->address_bus = gb->registers[register_id]; |
| 332 |
795453 |
| GB_trigger_oam_bug(gb, gb->registers[register_id]); /* Todo: test T-cycle timing */ |
| 333 |
795453 |
| gb->pending_cycles = 4; |
| 334 |
795453 |
| } |
| 335 |
|
| |
| 336 |
1864453 |
| static void flush_pending_cycles(GB_gameboy_t *gb) |
| 337 |
|
| { |
| 338 |
1864453 |
▶ | if (gb->pending_cycles) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->pending_cycles) { |
1864453 |
0 |
1864453 |
|
| 339 |
1864453 |
| GB_advance_cycles(gb, gb->pending_cycles); |
| 340 |
|
| } |
| 341 |
1864453 |
| gb->pending_cycles = 0; |
| 342 |
1864453 |
| } |
| 343 |
|
| |
| 344 |
|
| /* Todo: test if multi-byte opcodes trigger the OAM bug correctly */ |
| 345 |
|
| |
| 346 |
0 |
| static void ill(GB_gameboy_t *gb, uint8_t opcode) |
| 347 |
|
| { |
| 348 |
0 |
| GB_log(gb, "Illegal Opcode. Halting.\n"); |
| 349 |
0 |
| gb->interrupt_enable = 0; |
| 350 |
0 |
| gb->halted = true; |
| 351 |
0 |
| } |
| 352 |
|
| |
| 353 |
0 |
| static void nop(GB_gameboy_t *gb, uint8_t opcode) |
| 354 |
|
| { |
| 355 |
0 |
| } |
| 356 |
|
| |
| 357 |
0 |
| static void enter_stop_mode(GB_gameboy_t *gb) |
| 358 |
|
| { |
| 359 |
0 |
| GB_write_memory(gb, 0xFF00 + GB_IO_DIV, 0); |
| 360 |
0 |
▶ | if (!gb->ime) { // TODO: I don't trust this if, |
Branch: 1 branch
| Branch | True | False | Total |
| if (!gb->ime) { // TODO: I don't trust this if, |
0 |
0 |
0 |
|
| 361 |
0 |
| gb->div_cycles = -4; // Emulate the CPU-side DIV-reset signal being held |
| 362 |
|
| } |
| 363 |
0 |
| gb->stopped = true; |
| 364 |
0 |
| gb->allow_hdma_on_wake = (gb->io_registers[GB_IO_STAT] & 3); |
| 365 |
0 |
| gb->oam_ppu_blocked = !gb->oam_read_blocked; |
| 366 |
0 |
| gb->vram_ppu_blocked = !gb->vram_read_blocked; |
| 367 |
0 |
| gb->cgb_palettes_ppu_blocked = !gb->cgb_palettes_blocked; |
| 368 |
0 |
| } |
| 369 |
|
| |
| 370 |
0 |
| static void leave_stop_mode(GB_gameboy_t *gb) |
| 371 |
|
| { |
| 372 |
0 |
| gb->stopped = false; |
| 373 |
0 |
▶ | if (gb->hdma_on_hblank && (gb->io_registers[GB_IO_STAT] & 3) == 0 && gb->allow_hdma_on_wake) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (gb->hdma_on_hblank && (gb->io_registers[GB_IO_STAT] & 3) == 0 && gb->allow_hdma_on_wake) { |
0 |
0 |
0 |
| if (gb->hdma_on_hblank && (gb->io_registers[GB_IO_STAT] & 3) == 0 && gb->allow_hdma_on_wake) { |
0 |
0 |
0 |
| if (gb->hdma_on_hblank && (gb->io_registers[GB_IO_STAT] & 3) == 0 && gb->allow_hdma_on_wake) { |
0 |
0 |
0 |
|
| 374 |
0 |
| gb->hdma_on = true; |
| 375 |
|
| } |
| 376 |
|
| // TODO: verify this |
| 377 |
0 |
| gb->dma_cycles = 4; |
| 378 |
0 |
| GB_dma_run(gb); |
| 379 |
0 |
| gb->oam_ppu_blocked = false; |
| 380 |
0 |
| gb->vram_ppu_blocked = false; |
| 381 |
0 |
| gb->cgb_palettes_ppu_blocked = false; |
| 382 |
0 |
| } |
| 383 |
|
| |
| 384 |
|
| /* TODO: Speed switch timing needs far more tests. Double to single is wrong to avoid odd mode. */ |
| 385 |
0 |
| static void stop(GB_gameboy_t *gb, uint8_t opcode) |
| 386 |
|
| { |
| 387 |
0 |
| flush_pending_cycles(gb); |
| 388 |
0 |
| GB_read_memory(gb, gb->pc); // Timing is completely unverified, and only affects STOP triggering the OAM bug |
| 389 |
0 |
▶ | if ((gb->io_registers[GB_IO_JOYP] & 0x30) != 0x30) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((gb->io_registers[GB_IO_JOYP] & 0x30) != 0x30) { |
0 |
0 |
0 |
|
| 390 |
0 |
| gb->joyp_accessed = true; |
| 391 |
|
| } |
| 392 |
0 |
| bool exit_by_joyp = ((gb->io_registers[GB_IO_JOYP] & 0xF) != 0xF); |
| 393 |
0 |
▶ | bool speed_switch = (gb->io_registers[GB_IO_KEY1] & 0x1) && !exit_by_joyp; |
Branch: 2 branches
| Branch | True | False | Total |
| bool speed_switch = (gb->io_registers[GB_IO_KEY1] & 0x1) && !exit_by_joyp; |
0 |
0 |
0 |
| bool speed_switch = (gb->io_registers[GB_IO_KEY1] & 0x1) && !exit_by_joyp; |
0 |
0 |
0 |
|
| 394 |
0 |
▶ | bool immediate_exit = speed_switch || exit_by_joyp; |
Branch: 2 branches
| Branch | True | False | Total |
| bool immediate_exit = speed_switch || exit_by_joyp; |
0 |
0 |
0 |
| bool immediate_exit = speed_switch || exit_by_joyp; |
0 |
0 |
0 |
|
| 395 |
0 |
| bool interrupt_pending = (gb->interrupt_enable & gb->io_registers[GB_IO_IF] & 0x1F); |
| 396 |
|
| // When entering with IF&IE, the 2nd byte of STOP is actually executed |
| 397 |
0 |
▶ | if (!exit_by_joyp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!exit_by_joyp) { |
0 |
0 |
0 |
|
| 398 |
0 |
▶ | if (!immediate_exit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!immediate_exit) { |
0 |
0 |
0 |
|
| 399 |
0 |
| GB_dma_run(gb); |
| 400 |
|
| } |
| 401 |
0 |
| enter_stop_mode(gb); |
| 402 |
|
| } |
| 403 |
|
| |
| 404 |
0 |
▶ | if (!interrupt_pending) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!interrupt_pending) { |
0 |
0 |
0 |
|
| 405 |
0 |
| cycle_read(gb, gb->pc++); |
| 406 |
|
| } |
| 407 |
|
| |
| 408 |
|
| /* Todo: speed switching takes 2 extra T-cycles (so 2 PPU ticks in single->double and 1 PPU tick in double->single) */ |
| 409 |
0 |
▶ | if (speed_switch) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (speed_switch) { |
0 |
0 |
0 |
|
| 410 |
0 |
| flush_pending_cycles(gb); |
| 411 |
|
| |
| 412 |
0 |
▶ | if (gb->io_registers[GB_IO_LCDC] & GB_LCDC_ENABLE && gb->cgb_double_speed) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (gb->io_registers[GB_IO_LCDC] & GB_LCDC_ENABLE && gb->cgb_double_speed) { |
0 |
0 |
0 |
| if (gb->io_registers[GB_IO_LCDC] & GB_LCDC_ENABLE && gb->cgb_double_speed) { |
0 |
0 |
0 |
|
| 413 |
0 |
| GB_log(gb, "ROM triggered a PPU odd mode, which is currently not supported. Reverting to even-mode.\n"); |
| 414 |
0 |
▶ | if (gb->double_speed_alignment & 7) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->double_speed_alignment & 7) { |
0 |
0 |
0 |
|
| 415 |
0 |
| gb->speed_switch_freeze = 2; |
| 416 |
|
| } |
| 417 |
|
| } |
| 418 |
0 |
▶ | if (gb->apu.global_enable && gb->cgb_double_speed) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (gb->apu.global_enable && gb->cgb_double_speed) { |
0 |
0 |
0 |
| if (gb->apu.global_enable && gb->cgb_double_speed) { |
0 |
0 |
0 |
|
| 419 |
0 |
| GB_log(gb, "ROM triggered an APU odd mode, which is currently not tested.\n"); |
| 420 |
|
| } |
| 421 |
0 |
▶ | if (gb->cartridge_type->mbc_type == GB_CAMERA && (gb->camera_registers[GB_CAMERA_SHOOT_AND_1D_FLAGS] & 1) && !gb->cgb_double_speed) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (gb->cartridge_type->mbc_type == GB_CAMERA && (gb->camera_registers[GB_CAMERA_SHOOT_AND_1D_FLAGS] & 1) && !gb->cgb_double_speed) { |
0 |
0 |
0 |
| if (gb->cartridge_type->mbc_type == GB_CAMERA && (gb->camera_registers[GB_CAMERA_SHOOT_AND_1D_FLAGS] & 1) && !gb->cgb_double_speed) { |
0 |
0 |
0 |
| if (gb->cartridge_type->mbc_type == GB_CAMERA && (gb->camera_registers[GB_CAMERA_SHOOT_AND_1D_FLAGS] & 1) && !gb->cgb_double_speed) { |
0 |
0 |
0 |
|
| 422 |
0 |
| GB_log(gb, "ROM entered double speed mode with a camera cartridge, this could damage a real cartridge's camera.\n"); |
| 423 |
|
| } |
| 424 |
|
| |
| 425 |
0 |
▶ | if (gb->cgb_double_speed) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->cgb_double_speed) { |
0 |
0 |
0 |
|
| 426 |
0 |
| gb->cgb_double_speed = false; |
| 427 |
|
| } |
| 428 |
|
| else { |
| 429 |
0 |
| gb->speed_switch_countdown = 6; |
| 430 |
0 |
| gb->speed_switch_freeze = 1; |
| 431 |
|
| } |
| 432 |
|
| |
| 433 |
0 |
▶ | if (!interrupt_pending) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!interrupt_pending) { |
0 |
0 |
0 |
|
| 434 |
0 |
| gb->speed_switch_halt_countdown = 0x20008; |
| 435 |
0 |
| gb->speed_switch_freeze = 5; |
| 436 |
|
| } |
| 437 |
|
| |
| 438 |
0 |
| gb->io_registers[GB_IO_KEY1] = 0; |
| 439 |
|
| } |
| 440 |
|
| |
| 441 |
0 |
▶ | if (immediate_exit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (immediate_exit) { |
0 |
0 |
0 |
|
| 442 |
0 |
| leave_stop_mode(gb); |
| 443 |
0 |
▶ | if (!interrupt_pending) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!interrupt_pending) { |
0 |
0 |
0 |
|
| 444 |
0 |
| GB_dma_run(gb); |
| 445 |
0 |
| gb->halted = true; |
| 446 |
0 |
| gb->just_halted = true; |
| 447 |
0 |
| gb->allow_hdma_on_wake = (gb->io_registers[GB_IO_STAT] & 3); |
| 448 |
|
| } |
| 449 |
|
| else { |
| 450 |
0 |
| gb->speed_switch_halt_countdown = 0; |
| 451 |
|
| } |
| 452 |
|
| } |
| 453 |
0 |
| } |
| 454 |
|
| |
| 455 |
|
| /* Operand naming conventions for functions: |
| 456 |
|
| r = 8-bit register |
| 457 |
|
| lr = low 8-bit register |
| 458 |
|
| hr = high 8-bit register |
| 459 |
|
| rr = 16-bit register |
| 460 |
|
| d8 = 8-bit imm |
| 461 |
|
| d16 = 16-bit imm |
| 462 |
|
| d.. = [..] |
| 463 |
|
| cc = condition code (z, nz, c, nc) |
| 464 |
|
| */ |
| 465 |
|
| |
| 466 |
490 |
| static void ld_rr_d16(GB_gameboy_t *gb, uint8_t opcode) |
| 467 |
|
| { |
| 468 |
|
| uint8_t register_id; |
| 469 |
|
| uint16_t value; |
| 470 |
490 |
| register_id = (opcode >> 4) + 1; |
| 471 |
490 |
| value = cycle_read(gb, gb->pc++); |
| 472 |
490 |
| value |= cycle_read(gb, gb->pc++) << 8; |
| 473 |
490 |
| gb->registers[register_id] = value; |
| 474 |
490 |
| } |
| 475 |
|
| |
| 476 |
4137 |
| static void ld_drr_a(GB_gameboy_t *gb, uint8_t opcode) |
| 477 |
|
| { |
| 478 |
|
| uint8_t register_id; |
| 479 |
4137 |
| register_id = (opcode >> 4) + 1; |
| 480 |
4137 |
| cycle_write(gb, gb->registers[register_id], gb->af >> 8); |
| 481 |
4137 |
| } |
| 482 |
|
| |
| 483 |
9721 |
| static void inc_rr(GB_gameboy_t *gb, uint8_t opcode) |
| 484 |
|
| { |
| 485 |
9721 |
| uint8_t register_id = (opcode >> 4) + 1; |
| 486 |
9721 |
| cycle_oam_bug(gb, register_id); |
| 487 |
9721 |
| gb->registers[register_id]++; |
| 488 |
9721 |
| } |
| 489 |
|
| |
| 490 |
1040 |
| static void inc_hr(GB_gameboy_t *gb, uint8_t opcode) |
| 491 |
|
| { |
| 492 |
|
| uint8_t register_id; |
| 493 |
1040 |
| register_id = ((opcode >> 4) + 1) & 0x03; |
| 494 |
1040 |
| gb->registers[register_id] += 0x100; |
| 495 |
1040 |
| gb->af &= ~(GB_SUBTRACT_FLAG | GB_ZERO_FLAG | GB_HALF_CARRY_FLAG); |
| 496 |
|
| |
| 497 |
1040 |
▶ | if ((gb->registers[register_id] & 0x0F00) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((gb->registers[register_id] & 0x0F00) == 0) { |
66 |
974 |
1040 |
|
| 498 |
66 |
| gb->af |= GB_HALF_CARRY_FLAG; |
| 499 |
|
| } |
| 500 |
|
| |
| 501 |
1040 |
▶ | if ((gb->registers[register_id] & 0xFF00) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((gb->registers[register_id] & 0xFF00) == 0) { |
12 |
1028 |
1040 |
|
| 502 |
12 |
| gb->af |= GB_ZERO_FLAG; |
| 503 |
|
| } |
| 504 |
1040 |
| } |
| 505 |
4713 |
| static void dec_hr(GB_gameboy_t *gb, uint8_t opcode) |
| 506 |
|
| { |
| 507 |
|
| uint8_t register_id; |
| 508 |
4713 |
| register_id = ((opcode >> 4) + 1) & 0x03; |
| 509 |
4713 |
| gb->registers[register_id] -= 0x100; |
| 510 |
4713 |
| gb->af &= ~(GB_ZERO_FLAG | GB_HALF_CARRY_FLAG); |
| 511 |
4713 |
| gb->af |= GB_SUBTRACT_FLAG; |
| 512 |
|
| |
| 513 |
4713 |
▶ | if ((gb->registers[register_id] & 0x0F00) == 0xF00) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((gb->registers[register_id] & 0x0F00) == 0xF00) { |
88 |
4625 |
4713 |
|
| 514 |
88 |
| gb->af |= GB_HALF_CARRY_FLAG; |
| 515 |
|
| } |
| 516 |
|
| |
| 517 |
4713 |
▶ | if ((gb->registers[register_id] & 0xFF00) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((gb->registers[register_id] & 0xFF00) == 0) { |
862 |
3851 |
4713 |
|
| 518 |
862 |
| gb->af |= GB_ZERO_FLAG; |
| 519 |
|
| } |
| 520 |
4713 |
| } |
| 521 |
|
| |
| 522 |
1416 |
| static void ld_hr_d8(GB_gameboy_t *gb, uint8_t opcode) |
| 523 |
|
| { |
| 524 |
|
| uint8_t register_id; |
| 525 |
1416 |
| register_id = ((opcode >> 4) + 1) & 0x03; |
| 526 |
1416 |
| gb->registers[register_id] &= 0xFF; |
| 527 |
1416 |
| gb->registers[register_id] |= cycle_read(gb, gb->pc++) << 8; |
| 528 |
1416 |
| } |
| 529 |
|
| |
| 530 |
0 |
| static void rlca(GB_gameboy_t *gb, uint8_t opcode) |
| 531 |
|
| { |
| 532 |
0 |
| bool carry = (gb->af & 0x8000) != 0; |
| 533 |
|
| |
| 534 |
0 |
| gb->af = (gb->af & 0xFF00) << 1; |
| 535 |
0 |
▶ | if (carry) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (carry) { |
0 |
0 |
0 |
|
| 536 |
0 |
| gb->af |= GB_CARRY_FLAG | 0x0100; |
| 537 |
|
| } |
| 538 |
0 |
| } |
| 539 |
|
| |
| 540 |
0 |
| static void rla(GB_gameboy_t *gb, uint8_t opcode) |
| 541 |
|
| { |
| 542 |
0 |
| bool bit7 = (gb->af & 0x8000) != 0; |
| 543 |
0 |
| bool carry = (gb->af & GB_CARRY_FLAG) != 0; |
| 544 |
|
| |
| 545 |
0 |
| gb->af = (gb->af & 0xFF00) << 1; |
| 546 |
0 |
▶ | if (carry) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (carry) { |
0 |
0 |
0 |
|
| 547 |
0 |
| gb->af |= 0x0100; |
| 548 |
|
| } |
| 549 |
0 |
▶ | if (bit7) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (bit7) { |
0 |
0 |
0 |
|
| 550 |
0 |
| gb->af |= GB_CARRY_FLAG; |
| 551 |
|
| } |
| 552 |
0 |
| } |
| 553 |
|
| |
| 554 |
0 |
| static void ld_da16_sp(GB_gameboy_t *gb, uint8_t opcode) |
| 555 |
|
| { |
| 556 |
|
| /* Todo: Verify order is correct */ |
| 557 |
|
| uint16_t addr; |
| 558 |
0 |
| addr = cycle_read(gb, gb->pc++); |
| 559 |
0 |
| addr |= cycle_read(gb, gb->pc++) << 8; |
| 560 |
0 |
| cycle_write(gb, addr, gb->sp & 0xFF); |
| 561 |
0 |
| cycle_write(gb, addr + 1, gb->sp >> 8); |
| 562 |
0 |
| } |
| 563 |
|
| |
| 564 |
20 |
| static void add_hl_rr(GB_gameboy_t *gb, uint8_t opcode) |
| 565 |
|
| { |
| 566 |
20 |
| uint16_t hl = gb->hl; |
| 567 |
|
| uint16_t rr; |
| 568 |
|
| uint8_t register_id; |
| 569 |
20 |
| cycle_no_access(gb); |
| 570 |
20 |
| register_id = (opcode >> 4) + 1; |
| 571 |
20 |
| rr = gb->registers[register_id]; |
| 572 |
20 |
| gb->hl = hl + rr; |
| 573 |
20 |
| gb->af &= ~(GB_SUBTRACT_FLAG | GB_CARRY_FLAG | GB_HALF_CARRY_FLAG); |
| 574 |
|
| |
| 575 |
|
| /* The meaning of the Half Carry flag is really hard to track -_- */ |
| 576 |
20 |
▶ | if (((hl & 0xFFF) + (rr & 0xFFF)) & 0x1000) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (((hl & 0xFFF) + (rr & 0xFFF)) & 0x1000) { |
0 |
20 |
20 |
|
| 577 |
0 |
| gb->af |= GB_HALF_CARRY_FLAG; |
| 578 |
|
| } |
| 579 |
|
| |
| 580 |
20 |
▶ | if ( ((unsigned) hl + (unsigned) rr) & 0x10000) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( ((unsigned) hl + (unsigned) rr) & 0x10000) { |
0 |
20 |
20 |
|
| 581 |
0 |
| gb->af |= GB_CARRY_FLAG; |
| 582 |
|
| } |
| 583 |
20 |
| } |
| 584 |
|
| |
| 585 |
1752 |
| static void ld_a_drr(GB_gameboy_t *gb, uint8_t opcode) |
| 586 |
|
| { |
| 587 |
|
| uint8_t register_id; |
| 588 |
1752 |
| register_id = (opcode >> 4) + 1; |
| 589 |
1752 |
| gb->af &= 0xFF; |
| 590 |
1752 |
| gb->af |= cycle_read(gb, gb->registers[register_id]) << 8; |
| 591 |
1752 |
| } |
| 592 |
|
| |
| 593 |
624 |
| static void dec_rr(GB_gameboy_t *gb, uint8_t opcode) |
| 594 |
|
| { |
| 595 |
624 |
| uint8_t register_id = (opcode >> 4) + 1; |
| 596 |
624 |
| cycle_oam_bug(gb, register_id); |
| 597 |
624 |
| gb->registers[register_id]--; |
| 598 |
624 |
| } |
| 599 |
|
| |
| 600 |
765 |
| static void inc_lr(GB_gameboy_t *gb, uint8_t opcode) |
| 601 |
|
| { |
| 602 |
|
| uint8_t register_id; |
| 603 |
|
| uint8_t value; |
| 604 |
765 |
| register_id = (opcode >> 4) + 1; |
| 605 |
|
| |
| 606 |
765 |
| value = (gb->registers[register_id] & 0xFF) + 1; |
| 607 |
765 |
| gb->registers[register_id] = (gb->registers[register_id] & 0xFF00) | value; |
| 608 |
|
| |
| 609 |
765 |
| gb->af &= ~(GB_SUBTRACT_FLAG | GB_ZERO_FLAG | GB_HALF_CARRY_FLAG); |
| 610 |
|
| |
| 611 |
765 |
▶ | if ((gb->registers[register_id] & 0x0F) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((gb->registers[register_id] & 0x0F) == 0) { |
10 |
755 |
765 |
|
| 612 |
10 |
| gb->af |= GB_HALF_CARRY_FLAG; |
| 613 |
|
| } |
| 614 |
|
| |
| 615 |
765 |
▶ | if ((gb->registers[register_id] & 0xFF) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((gb->registers[register_id] & 0xFF) == 0) { |
0 |
765 |
765 |
|
| 616 |
0 |
| gb->af |= GB_ZERO_FLAG; |
| 617 |
|
| } |
| 618 |
765 |
| } |
| 619 |
2446 |
| static void dec_lr(GB_gameboy_t *gb, uint8_t opcode) |
| 620 |
|
| { |
| 621 |
|
| uint8_t register_id; |
| 622 |
|
| uint8_t value; |
| 623 |
2446 |
| register_id = (opcode >> 4) + 1; |
| 624 |
|
| |
| 625 |
2446 |
| value = (gb->registers[register_id] & 0xFF) - 1; |
| 626 |
2446 |
| gb->registers[register_id] = (gb->registers[register_id] & 0xFF00) | value; |
| 627 |
|
| |
| 628 |
2446 |
| gb->af &= ~(GB_ZERO_FLAG | GB_HALF_CARRY_FLAG); |
| 629 |
2446 |
| gb->af |= GB_SUBTRACT_FLAG; |
| 630 |
|
| |
| 631 |
2446 |
▶ | if ((gb->registers[register_id] & 0x0F) == 0xF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((gb->registers[register_id] & 0x0F) == 0xF) { |
100 |
2346 |
2446 |
|
| 632 |
100 |
| gb->af |= GB_HALF_CARRY_FLAG; |
| 633 |
|
| } |
| 634 |
|
| |
| 635 |
2446 |
▶ | if ((gb->registers[register_id] & 0xFF) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((gb->registers[register_id] & 0xFF) == 0) { |
119 |
2327 |
2446 |
|
| 636 |
119 |
| gb->af |= GB_ZERO_FLAG; |
| 637 |
|
| } |
| 638 |
2446 |
| } |
| 639 |
|
| |
| 640 |
939 |
| static void ld_lr_d8(GB_gameboy_t *gb, uint8_t opcode) |
| 641 |
|
| { |
| 642 |
|
| uint8_t register_id; |
| 643 |
939 |
| register_id = (opcode >> 4) + 1; |
| 644 |
939 |
| gb->registers[register_id] &= 0xFF00; |
| 645 |
939 |
| gb->registers[register_id] |= cycle_read(gb, gb->pc++); |
| 646 |
939 |
| } |
| 647 |
|
| |
| 648 |
0 |
| static void rrca(GB_gameboy_t *gb, uint8_t opcode) |
| 649 |
|
| { |
| 650 |
0 |
| bool carry = (gb->af & 0x100) != 0; |
| 651 |
|
| |
| 652 |
0 |
| gb->af = (gb->af >> 1) & 0xFF00; |
| 653 |
0 |
▶ | if (carry) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (carry) { |
0 |
0 |
0 |
|
| 654 |
0 |
| gb->af |= GB_CARRY_FLAG | 0x8000; |
| 655 |
|
| } |
| 656 |
0 |
| } |
| 657 |
|
| |
| 658 |
40 |
| static void rra(GB_gameboy_t *gb, uint8_t opcode) |
| 659 |
|
| { |
| 660 |
40 |
| bool bit1 = (gb->af & 0x0100) != 0; |
| 661 |
40 |
| bool carry = (gb->af & GB_CARRY_FLAG) != 0; |
| 662 |
|
| |
| 663 |
40 |
| gb->af = (gb->af >> 1) & 0xFF00; |
| 664 |
40 |
▶ | if (carry) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (carry) { |
0 |
40 |
40 |
|
| 665 |
0 |
| gb->af |= 0x8000; |
| 666 |
|
| } |
| 667 |
40 |
▶ | if (bit1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (bit1) { |
30 |
10 |
40 |
|
| 668 |
30 |
| gb->af |= GB_CARRY_FLAG; |
| 669 |
|
| } |
| 670 |
40 |
| } |
| 671 |
|
| |
| 672 |
3690 |
| static void jr_r8(GB_gameboy_t *gb, uint8_t opcode) |
| 673 |
|
| { |
| 674 |
3690 |
| int8_t offset = (int8_t)cycle_read(gb, gb->pc++); |
| 675 |
3690 |
| cycle_oam_bug(gb, GB_REGISTER_PC); |
| 676 |
3690 |
| gb->pc += offset; |
| 677 |
3690 |
| } |
| 678 |
|
| |
| 679 |
784616 |
| static bool condition_code(GB_gameboy_t *gb, uint8_t opcode) |
| 680 |
|
| { |
| 681 |
784616 |
| switch ((opcode >> 3) & 0x3) { |
| 682 |
10926 |
| case 0: |
| 683 |
10926 |
| return !(gb->af & GB_ZERO_FLAG); |
| 684 |
763700 |
| case 1: |
| 685 |
763700 |
| return (gb->af & GB_ZERO_FLAG); |
| 686 |
972 |
| case 2: |
| 687 |
972 |
| return !(gb->af & GB_CARRY_FLAG); |
| 688 |
9018 |
| case 3: |
| 689 |
9018 |
| return (gb->af & GB_CARRY_FLAG); |
| 690 |
0 |
| nodefault; |
| 691 |
|
| } |
| 692 |
|
| |
| 693 |
|
| return false; |
| 694 |
|
| } |
| 695 |
|
| |
| 696 |
784422 |
| static void jr_cc_r8(GB_gameboy_t *gb, uint8_t opcode) |
| 697 |
|
| { |
| 698 |
784422 |
| int8_t offset = cycle_read(gb, gb->pc++); |
| 699 |
784422 |
▶ | if (condition_code(gb, opcode)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (condition_code(gb, opcode)) { |
775605 |
8817 |
784422 |
|
| 700 |
775605 |
| gb->pc += offset; |
| 701 |
775605 |
| cycle_oam_bug(gb, GB_REGISTER_PC); |
| 702 |
|
| } |
| 703 |
784422 |
| } |
| 704 |
|
| |
| 705 |
0 |
| static void daa(GB_gameboy_t *gb, uint8_t opcode) |
| 706 |
|
| { |
| 707 |
0 |
| int16_t result = gb->af >> 8; |
| 708 |
|
| |
| 709 |
0 |
| gb->af &= ~(0xFF00 | GB_ZERO_FLAG); |
| 710 |
|
| |
| 711 |
0 |
▶ | if (gb->af & GB_SUBTRACT_FLAG) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->af & GB_SUBTRACT_FLAG) { |
0 |
0 |
0 |
|
| 712 |
0 |
▶ | if (gb->af & GB_HALF_CARRY_FLAG) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->af & GB_HALF_CARRY_FLAG) { |
0 |
0 |
0 |
|
| 713 |
0 |
| result = (result - 0x06) & 0xFF; |
| 714 |
|
| } |
| 715 |
|
| |
| 716 |
0 |
▶ | if (gb->af & GB_CARRY_FLAG) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->af & GB_CARRY_FLAG) { |
0 |
0 |
0 |
|
| 717 |
0 |
| result -= 0x60; |
| 718 |
|
| } |
| 719 |
|
| } |
| 720 |
|
| else { |
| 721 |
0 |
▶ | if ((gb->af & GB_HALF_CARRY_FLAG) || (result & 0x0F) > 0x09) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((gb->af & GB_HALF_CARRY_FLAG) || (result & 0x0F) > 0x09) { |
0 |
0 |
0 |
| if ((gb->af & GB_HALF_CARRY_FLAG) || (result & 0x0F) > 0x09) { |
0 |
0 |
0 |
|
| 722 |
0 |
| result += 0x06; |
| 723 |
|
| } |
| 724 |
|
| |
| 725 |
0 |
▶ | if ((gb->af & GB_CARRY_FLAG) || result > 0x9F) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((gb->af & GB_CARRY_FLAG) || result > 0x9F) { |
0 |
0 |
0 |
| if ((gb->af & GB_CARRY_FLAG) || result > 0x9F) { |
0 |
0 |
0 |
|
| 726 |
0 |
| result += 0x60; |
| 727 |
|
| } |
| 728 |
|
| } |
| 729 |
|
| |
| 730 |
0 |
▶ | if ((result & 0xFF) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((result & 0xFF) == 0) { |
0 |
0 |
0 |
|
| 731 |
0 |
| gb->af |= GB_ZERO_FLAG; |
| 732 |
|
| } |
| 733 |
|
| |
| 734 |
0 |
▶ | if ((result & 0x100) == 0x100) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((result & 0x100) == 0x100) { |
0 |
0 |
0 |
|
| 735 |
0 |
| gb->af |= GB_CARRY_FLAG; |
| 736 |
|
| } |
| 737 |
|
| |
| 738 |
0 |
| gb->af &= ~GB_HALF_CARRY_FLAG; |
| 739 |
0 |
| gb->af |= result << 8; |
| 740 |
0 |
| } |
| 741 |
|
| |
| 742 |
346 |
| static void cpl(GB_gameboy_t *gb, uint8_t opcode) |
| 743 |
|
| { |
| 744 |
346 |
| gb->af ^= 0xFF00; |
| 745 |
346 |
| gb->af |= GB_HALF_CARRY_FLAG | GB_SUBTRACT_FLAG; |
| 746 |
346 |
| } |
| 747 |
|
| |
| 748 |
1213 |
| static void scf(GB_gameboy_t *gb, uint8_t opcode) |
| 749 |
|
| { |
| 750 |
1213 |
| gb->af |= GB_CARRY_FLAG; |
| 751 |
1213 |
| gb->af &= ~(GB_HALF_CARRY_FLAG | GB_SUBTRACT_FLAG); |
| 752 |
1213 |
| } |
| 753 |
|
| |
| 754 |
0 |
| static void ccf(GB_gameboy_t *gb, uint8_t opcode) |
| 755 |
|
| { |
| 756 |
0 |
| gb->af ^= GB_CARRY_FLAG; |
| 757 |
0 |
| gb->af &= ~(GB_HALF_CARRY_FLAG | GB_SUBTRACT_FLAG); |
| 758 |
0 |
| } |
| 759 |
|
| |
| 760 |
124285 |
| static void ld_dhli_a(GB_gameboy_t *gb, uint8_t opcode) |
| 761 |
|
| { |
| 762 |
124285 |
| cycle_write(gb, gb->hl++, gb->af >> 8); |
| 763 |
124285 |
| } |
| 764 |
|
| |
| 765 |
24 |
| static void ld_dhld_a(GB_gameboy_t *gb, uint8_t opcode) |
| 766 |
|
| { |
| 767 |
24 |
| cycle_write(gb, gb->hl--, gb->af >> 8); |
| 768 |
24 |
| } |
| 769 |
|
| |
| 770 |
1103 |
| static void ld_a_dhli(GB_gameboy_t *gb, uint8_t opcode) |
| 771 |
|
| { |
| 772 |
1103 |
| gb->af &= 0xFF; |
| 773 |
1103 |
| gb->af |= cycle_read(gb, gb->hl++) << 8; |
| 774 |
1103 |
| } |
| 775 |
|
| |
| 776 |
32 |
| static void ld_a_dhld(GB_gameboy_t *gb, uint8_t opcode) |
| 777 |
|
| { |
| 778 |
32 |
| gb->af &= 0xFF; |
| 779 |
32 |
| gb->af |= cycle_read(gb, gb->hl--) << 8; |
| 780 |
32 |
| } |
| 781 |
|
| |
| 782 |
954 |
| static void inc_dhl(GB_gameboy_t *gb, uint8_t opcode) |
| 783 |
|
| { |
| 784 |
|
| uint8_t value; |
| 785 |
954 |
| value = cycle_read(gb, gb->hl) + 1; |
| 786 |
954 |
| cycle_write(gb, gb->hl, value); |
| 787 |
|
| |
| 788 |
954 |
| gb->af &= ~(GB_SUBTRACT_FLAG | GB_ZERO_FLAG | GB_HALF_CARRY_FLAG); |
| 789 |
954 |
▶ | if ((value & 0x0F) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((value & 0x0F) == 0) { |
0 |
954 |
954 |
|
| 790 |
0 |
| gb->af |= GB_HALF_CARRY_FLAG; |
| 791 |
|
| } |
| 792 |
|
| |
| 793 |
954 |
▶ | if ((value & 0xFF) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((value & 0xFF) == 0) { |
0 |
954 |
954 |
|
| 794 |
0 |
| gb->af |= GB_ZERO_FLAG; |
| 795 |
|
| } |
| 796 |
954 |
| } |
| 797 |
|
| |
| 798 |
48 |
| static void dec_dhl(GB_gameboy_t *gb, uint8_t opcode) |
| 799 |
|
| { |
| 800 |
|
| uint8_t value; |
| 801 |
48 |
| value = cycle_read(gb, gb->hl) - 1; |
| 802 |
48 |
| cycle_write(gb, gb->hl, value); |
| 803 |
|
| |
| 804 |
48 |
| gb->af &= ~( GB_ZERO_FLAG | GB_HALF_CARRY_FLAG); |
| 805 |
48 |
| gb->af |= GB_SUBTRACT_FLAG; |
| 806 |
48 |
▶ | if ((value & 0x0F) == 0x0F) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((value & 0x0F) == 0x0F) { |
3 |
45 |
48 |
|
| 807 |
3 |
| gb->af |= GB_HALF_CARRY_FLAG; |
| 808 |
|
| } |
| 809 |
|
| |
| 810 |
48 |
▶ | if ((value & 0xFF) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((value & 0xFF) == 0) { |
1 |
47 |
48 |
|
| 811 |
1 |
| gb->af |= GB_ZERO_FLAG; |
| 812 |
|
| } |
| 813 |
48 |
| } |
| 814 |
|
| |
| 815 |
275 |
| static void ld_dhl_d8(GB_gameboy_t *gb, uint8_t opcode) |
| 816 |
|
| { |
| 817 |
275 |
| uint8_t data = cycle_read(gb, gb->pc++); |
| 818 |
275 |
| cycle_write(gb, gb->hl, data); |
| 819 |
275 |
| } |
| 820 |
|
| |
| 821 |
896354 |
| static uint8_t get_src_value(GB_gameboy_t *gb, uint8_t opcode) |
| 822 |
|
| { |
| 823 |
|
| uint8_t src_register_id; |
| 824 |
|
| uint8_t src_low; |
| 825 |
896354 |
| src_register_id = ((opcode >> 1) + 1) & 3; |
| 826 |
896354 |
| src_low = opcode & 1; |
| 827 |
896354 |
▶ | if (src_register_id == GB_REGISTER_AF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (src_register_id == GB_REGISTER_AF) { |
753358 |
142996 |
896354 |
|
| 828 |
753358 |
▶ | if (src_low) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (src_low) { |
113825 |
639533 |
753358 |
|
| 829 |
113825 |
| return gb->af >> 8; |
| 830 |
|
| } |
| 831 |
639533 |
| return cycle_read(gb, gb->hl); |
| 832 |
|
| } |
| 833 |
142996 |
▶ | if (src_low) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (src_low) { |
7878 |
135118 |
142996 |
|
| 834 |
7878 |
| return gb->registers[src_register_id] & 0xFF; |
| 835 |
|
| } |
| 836 |
135118 |
| return gb->registers[src_register_id] >> 8; |
| 837 |
|
| } |
| 838 |
|
| |
| 839 |
21324 |
| static void set_src_value(GB_gameboy_t *gb, uint8_t opcode, uint8_t value) |
| 840 |
|
| { |
| 841 |
|
| uint8_t src_register_id; |
| 842 |
|
| uint8_t src_low; |
| 843 |
21324 |
| src_register_id = ((opcode >> 1) + 1) & 3; |
| 844 |
21324 |
| src_low = opcode & 1; |
| 845 |
|
| |
| 846 |
21324 |
▶ | if (src_register_id == GB_REGISTER_AF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (src_register_id == GB_REGISTER_AF) { |
898 |
20426 |
21324 |
|
| 847 |
898 |
▶ | if (src_low) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (src_low) { |
677 |
221 |
898 |
|
| 848 |
677 |
| gb->af &= 0xFF; |
| 849 |
677 |
| gb->af |= value << 8; |
| 850 |
|
| } |
| 851 |
|
| else { |
| 852 |
221 |
| cycle_write(gb, gb->hl, value); |
| 853 |
|
| } |
| 854 |
|
| } |
| 855 |
|
| else { |
| 856 |
20426 |
▶ | if (src_low) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (src_low) { |
6394 |
14032 |
20426 |
|
| 857 |
6394 |
| gb->registers[src_register_id] &= 0xFF00; |
| 858 |
6394 |
| gb->registers[src_register_id] |= value; |
| 859 |
|
| } |
| 860 |
|
| else { |
| 861 |
14032 |
| gb->registers[src_register_id] &= 0xFF; |
| 862 |
14032 |
| gb->registers[src_register_id] |= value << 8; |
| 863 |
|
| } |
| 864 |
|
| } |
| 865 |
21324 |
| } |
| 866 |
|
| |
| 867 |
|
| /* The LD r,r instruction is extremely common and extremely simple. Decoding this opcode at runtime is a significent |
| 868 |
|
| performance hit, so we generate functions for every ld x,y couple (including [hl]) at compile time using macros. */ |
| 869 |
|
| |
| 870 |
|
| /* Todo: It's probably wise to do the same to all opcodes. */ |
| 871 |
|
| |
| 872 |
|
| #define LD_X_Y(x, y) \ |
| 873 |
|
| static void ld_##x##_##y(GB_gameboy_t *gb, uint8_t opcode) \ |
| 874 |
|
| { \ |
| 875 |
|
| gb->x = gb->y;\ |
| 876 |
|
| } |
| 877 |
|
| |
| 878 |
|
| #define LD_X_DHL(x) \ |
| 879 |
|
| static void ld_##x##_##dhl(GB_gameboy_t *gb, uint8_t opcode) \ |
| 880 |
|
| { \ |
| 881 |
|
| gb->x = cycle_read(gb, gb->hl); \ |
| 882 |
|
| } |
| 883 |
|
| |
| 884 |
|
| #define LD_DHL_Y(y) \ |
| 885 |
|
| static void ld_##dhl##_##y(GB_gameboy_t *gb, uint8_t opcode) \ |
| 886 |
|
| { \ |
| 887 |
|
| cycle_write(gb, gb->hl, gb->y); \ |
| 888 |
|
| } |
| 889 |
|
| |
| 890 |
0 |
| LD_X_Y(b,c) LD_X_Y(b,d) LD_X_Y(b,e) LD_X_Y(b,h) LD_X_Y(b,l) LD_X_DHL(b) LD_X_Y(b,a) |
| 891 |
0 |
| LD_X_Y(c,b) LD_X_Y(c,d) LD_X_Y(c,e) LD_X_Y(c,h) LD_X_Y(c,l) LD_X_DHL(c) LD_X_Y(c,a) |
| 892 |
0 |
| LD_X_Y(d,b) LD_X_Y(d,c) LD_X_Y(d,e) LD_X_Y(d,h) LD_X_Y(d,l) LD_X_DHL(d) LD_X_Y(d,a) |
| 893 |
0 |
| LD_X_Y(e,b) LD_X_Y(e,c) LD_X_Y(e,d) LD_X_Y(e,h) LD_X_Y(e,l) LD_X_DHL(e) LD_X_Y(e,a) |
| 894 |
0 |
| LD_X_Y(h,b) LD_X_Y(h,c) LD_X_Y(h,d) LD_X_Y(h,e) LD_X_Y(h,l) LD_X_DHL(h) LD_X_Y(h,a) |
| 895 |
0 |
| LD_X_Y(l,b) LD_X_Y(l,c) LD_X_Y(l,d) LD_X_Y(l,e) LD_X_Y(l,h) LD_X_DHL(l) LD_X_Y(l,a) |
| 896 |
0 |
| LD_DHL_Y(b) LD_DHL_Y(c) LD_DHL_Y(d) LD_DHL_Y(e) LD_DHL_Y(h) LD_DHL_Y(l) LD_DHL_Y(a) |
| 897 |
0 |
| LD_X_Y(a,b) LD_X_Y(a,c) LD_X_Y(a,d) LD_X_Y(a,e) LD_X_Y(a,h) LD_X_Y(a,l) LD_X_DHL(a) |
| 898 |
|
| |
| 899 |
|
| // fire the debugger if software breakpoints are enabled |
| 900 |
0 |
| static void ld_b_b(GB_gameboy_t *gb, uint8_t opcode) |
| 901 |
|
| { |
| 902 |
|
| #ifndef GB_DISABLE_DEBUGGER |
| 903 |
|
| if (gb->has_software_breakpoints) { |
| 904 |
|
| GB_debugger_break(gb); |
| 905 |
|
| } |
| 906 |
|
| #endif |
| 907 |
0 |
| } |
| 908 |
|
| |
| 909 |
732 |
| static void add_a_r(GB_gameboy_t *gb, uint8_t opcode) |
| 910 |
|
| { |
| 911 |
|
| uint8_t value, a; |
| 912 |
732 |
| value = get_src_value(gb, opcode); |
| 913 |
732 |
| a = gb->af >> 8; |
| 914 |
732 |
| gb->af = (a + value) << 8; |
| 915 |
732 |
▶ | if ((uint8_t)(a + value) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((uint8_t)(a + value) == 0) { |
24 |
708 |
732 |
|
| 916 |
24 |
| gb->af |= GB_ZERO_FLAG; |
| 917 |
|
| } |
| 918 |
732 |
▶ | if ((a & 0xF) + (value & 0xF) > 0x0F) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((a & 0xF) + (value & 0xF) > 0x0F) { |
333 |
399 |
732 |
|
| 919 |
333 |
| gb->af |= GB_HALF_CARRY_FLAG; |
| 920 |
|
| } |
| 921 |
732 |
▶ | if (((unsigned) a) + ((unsigned) value) > 0xFF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (((unsigned) a) + ((unsigned) value) > 0xFF) { |
224 |
508 |
732 |
|
| 922 |
224 |
| gb->af |= GB_CARRY_FLAG; |
| 923 |
|
| } |
| 924 |
732 |
| } |
| 925 |
|
| |
| 926 |
32 |
| static void adc_a_r(GB_gameboy_t *gb, uint8_t opcode) |
| 927 |
|
| { |
| 928 |
|
| uint8_t value, a, carry; |
| 929 |
32 |
| value = get_src_value(gb, opcode); |
| 930 |
32 |
| a = gb->af >> 8; |
| 931 |
32 |
| carry = (gb->af & GB_CARRY_FLAG) != 0; |
| 932 |
32 |
| gb->af = (a + value + carry) << 8; |
| 933 |
|
| |
| 934 |
32 |
▶ | if ((uint8_t)(a + value + carry) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((uint8_t)(a + value + carry) == 0) { |
0 |
32 |
32 |
|
| 935 |
0 |
| gb->af |= GB_ZERO_FLAG; |
| 936 |
|
| } |
| 937 |
32 |
▶ | if ((a & 0xF) + (value & 0xF) + carry > 0x0F) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((a & 0xF) + (value & 0xF) + carry > 0x0F) { |
1 |
31 |
32 |
|
| 938 |
1 |
| gb->af |= GB_HALF_CARRY_FLAG; |
| 939 |
|
| } |
| 940 |
32 |
▶ | if (((unsigned) a) + ((unsigned) value) + carry > 0xFF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (((unsigned) a) + ((unsigned) value) + carry > 0xFF) { |
0 |
32 |
32 |
|
| 941 |
0 |
| gb->af |= GB_CARRY_FLAG; |
| 942 |
|
| } |
| 943 |
32 |
| } |
| 944 |
|
| |
| 945 |
0 |
| static void sub_a_r(GB_gameboy_t *gb, uint8_t opcode) |
| 946 |
|
| { |
| 947 |
|
| uint8_t value, a; |
| 948 |
0 |
| value = get_src_value(gb, opcode); |
| 949 |
0 |
| a = gb->af >> 8; |
| 950 |
0 |
| gb->af = ((a - value) << 8) | GB_SUBTRACT_FLAG; |
| 951 |
0 |
▶ | if (a == value) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (a == value) { |
0 |
0 |
0 |
|
| 952 |
0 |
| gb->af |= GB_ZERO_FLAG; |
| 953 |
|
| } |
| 954 |
0 |
▶ | if ((a & 0xF) < (value & 0xF)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((a & 0xF) < (value & 0xF)) { |
0 |
0 |
0 |
|
| 955 |
0 |
| gb->af |= GB_HALF_CARRY_FLAG; |
| 956 |
|
| } |
| 957 |
0 |
▶ | if (a < value) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (a < value) { |
0 |
0 |
0 |
|
| 958 |
0 |
| gb->af |= GB_CARRY_FLAG; |
| 959 |
|
| } |
| 960 |
0 |
| } |
| 961 |
|
| |
| 962 |
0 |
| static void sbc_a_r(GB_gameboy_t *gb, uint8_t opcode) |
| 963 |
|
| { |
| 964 |
|
| uint8_t value, a, carry; |
| 965 |
0 |
| value = get_src_value(gb, opcode); |
| 966 |
0 |
| a = gb->af >> 8; |
| 967 |
0 |
| carry = (gb->af & GB_CARRY_FLAG) != 0; |
| 968 |
0 |
| gb->af = (((uint8_t)(a - value - carry)) << 8) | GB_SUBTRACT_FLAG; |
| 969 |
|
| |
| 970 |
0 |
▶ | if ((uint8_t) (a - value - carry) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((uint8_t) (a - value - carry) == 0) { |
0 |
0 |
0 |
|
| 971 |
0 |
| gb->af |= GB_ZERO_FLAG; |
| 972 |
|
| } |
| 973 |
0 |
▶ | if ((a & 0xF) < (value & 0xF) + carry) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((a & 0xF) < (value & 0xF) + carry) { |
0 |
0 |
0 |
|
| 974 |
0 |
| gb->af |= GB_HALF_CARRY_FLAG; |
| 975 |
|
| } |
| 976 |
0 |
▶ | if (((unsigned) a) - ((unsigned) value) - carry > 0xFF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (((unsigned) a) - ((unsigned) value) - carry > 0xFF) { |
0 |
0 |
0 |
|
| 977 |
0 |
| gb->af |= GB_CARRY_FLAG; |
| 978 |
|
| } |
| 979 |
0 |
| } |
| 980 |
|
| |
| 981 |
888 |
| static void and_a_r(GB_gameboy_t *gb, uint8_t opcode) |
| 982 |
|
| { |
| 983 |
|
| uint8_t value, a; |
| 984 |
888 |
| value = get_src_value(gb, opcode); |
| 985 |
888 |
| a = gb->af >> 8; |
| 986 |
888 |
| gb->af = ((a & value) << 8) | GB_HALF_CARRY_FLAG; |
| 987 |
888 |
▶ | if ((a & value) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((a & value) == 0) { |
515 |
373 |
888 |
|
| 988 |
515 |
| gb->af |= GB_ZERO_FLAG; |
| 989 |
|
| } |
| 990 |
888 |
| } |
| 991 |
|
| |
| 992 |
112723 |
| static void xor_a_r(GB_gameboy_t *gb, uint8_t opcode) |
| 993 |
|
| { |
| 994 |
|
| uint8_t value, a; |
| 995 |
112723 |
| value = get_src_value(gb, opcode); |
| 996 |
112723 |
| a = gb->af >> 8; |
| 997 |
112723 |
| gb->af = (a ^ value) << 8; |
| 998 |
112723 |
▶ | if ((a ^ value) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((a ^ value) == 0) { |
112723 |
0 |
112723 |
|
| 999 |
112723 |
| gb->af |= GB_ZERO_FLAG; |
| 1000 |
|
| } |
| 1001 |
112723 |
| } |
| 1002 |
|
| |
| 1003 |
549 |
| static void or_a_r(GB_gameboy_t *gb, uint8_t opcode) |
| 1004 |
|
| { |
| 1005 |
|
| uint8_t value, a; |
| 1006 |
549 |
| value = get_src_value(gb, opcode); |
| 1007 |
549 |
| a = gb->af >> 8; |
| 1008 |
549 |
| gb->af = (a | value) << 8; |
| 1009 |
549 |
▶ | if ((a | value) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((a | value) == 0) { |
240 |
309 |
549 |
|
| 1010 |
240 |
| gb->af |= GB_ZERO_FLAG; |
| 1011 |
|
| } |
| 1012 |
549 |
| } |
| 1013 |
|
| |
| 1014 |
0 |
| static void cp_a_r(GB_gameboy_t *gb, uint8_t opcode) |
| 1015 |
|
| { |
| 1016 |
|
| uint8_t value, a; |
| 1017 |
0 |
| value = get_src_value(gb, opcode); |
| 1018 |
0 |
| a = gb->af >> 8; |
| 1019 |
0 |
| gb->af &= 0xFF00; |
| 1020 |
0 |
| gb->af |= GB_SUBTRACT_FLAG; |
| 1021 |
0 |
▶ | if (a == value) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (a == value) { |
0 |
0 |
0 |
|
| 1022 |
0 |
| gb->af |= GB_ZERO_FLAG; |
| 1023 |
|
| } |
| 1024 |
0 |
▶ | if ((a & 0xF) < (value & 0xF)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((a & 0xF) < (value & 0xF)) { |
0 |
0 |
0 |
|
| 1025 |
0 |
| gb->af |= GB_HALF_CARRY_FLAG; |
| 1026 |
|
| } |
| 1027 |
0 |
▶ | if (a < value) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (a < value) { |
0 |
0 |
0 |
|
| 1028 |
0 |
| gb->af |= GB_CARRY_FLAG; |
| 1029 |
|
| } |
| 1030 |
0 |
| } |
| 1031 |
|
| |
| 1032 |
0 |
| static void halt(GB_gameboy_t *gb, uint8_t opcode) |
| 1033 |
|
| { |
| 1034 |
0 |
| cycle_read(gb, gb->pc); |
| 1035 |
|
| assert(gb->pending_cycles == 4); |
| 1036 |
0 |
| gb->pending_cycles = 0; |
| 1037 |
|
| |
| 1038 |
|
| /* Despite what some online documentations say, the HALT bug also happens on a CGB, in both CGB and DMG modes. */ |
| 1039 |
0 |
▶ | if (((gb->interrupt_enable & gb->io_registers[GB_IO_IF] & 0x1F) != 0)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (((gb->interrupt_enable & gb->io_registers[GB_IO_IF] & 0x1F) != 0)) { |
0 |
0 |
0 |
|
| 1040 |
0 |
▶ | if (gb->ime) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->ime) { |
0 |
0 |
0 |
|
| 1041 |
0 |
| gb->halted = false; |
| 1042 |
0 |
| gb->pc--; |
| 1043 |
|
| } |
| 1044 |
|
| else { |
| 1045 |
0 |
| gb->halted = false; |
| 1046 |
0 |
| gb->halt_bug = true; |
| 1047 |
|
| } |
| 1048 |
|
| } |
| 1049 |
|
| else { |
| 1050 |
0 |
| gb->halted = true; |
| 1051 |
0 |
| gb->allow_hdma_on_wake = (gb->io_registers[GB_IO_STAT] & 3); |
| 1052 |
|
| } |
| 1053 |
0 |
| gb->just_halted = true; |
| 1054 |
0 |
| } |
| 1055 |
|
| |
| 1056 |
3874 |
| static void pop_rr(GB_gameboy_t *gb, uint8_t opcode) |
| 1057 |
|
| { |
| 1058 |
|
| uint8_t register_id; |
| 1059 |
3874 |
| register_id = ((opcode >> 4) + 1) & 3; |
| 1060 |
3874 |
| gb->registers[register_id] = cycle_read(gb, gb->sp++); |
| 1061 |
3874 |
| gb->registers[register_id] |= cycle_read(gb, gb->sp++) << 8; |
| 1062 |
3874 |
| gb->af &= 0xFFF0; // Make sure we don't set impossible flags on F! See Blargg's PUSH AF test. |
| 1063 |
3874 |
| } |
| 1064 |
|
| |
| 1065 |
0 |
| static void jp_cc_a16(GB_gameboy_t *gb, uint8_t opcode) |
| 1066 |
|
| { |
| 1067 |
0 |
| uint16_t addr = cycle_read(gb, gb->pc++); |
| 1068 |
0 |
| addr |= (cycle_read(gb, gb->pc++) << 8); |
| 1069 |
0 |
▶ | if (condition_code(gb, opcode)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (condition_code(gb, opcode)) { |
0 |
0 |
0 |
|
| 1070 |
0 |
| cycle_no_access(gb); |
| 1071 |
0 |
| gb->pc = addr; |
| 1072 |
|
| } |
| 1073 |
0 |
| } |
| 1074 |
|
| |
| 1075 |
0 |
| static void jp_a16(GB_gameboy_t *gb, uint8_t opcode) |
| 1076 |
|
| { |
| 1077 |
0 |
| uint16_t addr = cycle_read(gb, gb->pc); |
| 1078 |
0 |
| addr |= (cycle_read(gb, gb->pc + 1) << 8); |
| 1079 |
0 |
| cycle_no_access(gb); |
| 1080 |
0 |
| gb->pc = addr; |
| 1081 |
|
| |
| 1082 |
0 |
| } |
| 1083 |
|
| |
| 1084 |
0 |
| static void call_cc_a16(GB_gameboy_t *gb, uint8_t opcode) |
| 1085 |
|
| { |
| 1086 |
0 |
| uint16_t call_addr = gb->pc - 1; |
| 1087 |
0 |
| uint16_t addr = cycle_read(gb, gb->pc++); |
| 1088 |
0 |
| addr |= (cycle_read(gb, gb->pc++) << 8); |
| 1089 |
0 |
▶ | if (condition_code(gb, opcode)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (condition_code(gb, opcode)) { |
0 |
0 |
0 |
|
| 1090 |
0 |
| cycle_oam_bug(gb, GB_REGISTER_SP); |
| 1091 |
0 |
| cycle_write(gb, --gb->sp, (gb->pc) >> 8); |
| 1092 |
0 |
| cycle_write(gb, --gb->sp, (gb->pc) & 0xFF); |
| 1093 |
0 |
| gb->pc = addr; |
| 1094 |
|
| |
| 1095 |
|
| GB_debugger_call_hook(gb, call_addr); |
| 1096 |
|
| } |
| 1097 |
0 |
| } |
| 1098 |
|
| |
| 1099 |
3880 |
| static void push_rr(GB_gameboy_t *gb, uint8_t opcode) |
| 1100 |
|
| { |
| 1101 |
|
| uint8_t register_id; |
| 1102 |
3880 |
| cycle_oam_bug(gb, GB_REGISTER_SP); |
| 1103 |
3880 |
| register_id = ((opcode >> 4) + 1) & 3; |
| 1104 |
3880 |
| cycle_write(gb, --gb->sp, (gb->registers[register_id]) >> 8); |
| 1105 |
3880 |
| cycle_write(gb, --gb->sp, (gb->registers[register_id]) & 0xFF); |
| 1106 |
3880 |
| } |
| 1107 |
|
| |
| 1108 |
45 |
| static void add_a_d8(GB_gameboy_t *gb, uint8_t opcode) |
| 1109 |
|
| { |
| 1110 |
|
| uint8_t value, a; |
| 1111 |
45 |
| value = cycle_read(gb, gb->pc++); |
| 1112 |
45 |
| a = gb->af >> 8; |
| 1113 |
45 |
| gb->af = (a + value) << 8; |
| 1114 |
45 |
▶ | if ((uint8_t) (a + value) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((uint8_t) (a + value) == 0) { |
0 |
45 |
45 |
|
| 1115 |
0 |
| gb->af |= GB_ZERO_FLAG; |
| 1116 |
|
| } |
| 1117 |
45 |
▶ | if ((a & 0xF) + (value & 0xF) > 0x0F) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((a & 0xF) + (value & 0xF) > 0x0F) { |
15 |
30 |
45 |
|
| 1118 |
15 |
| gb->af |= GB_HALF_CARRY_FLAG; |
| 1119 |
|
| } |
| 1120 |
45 |
▶ | if (((unsigned) a) + ((unsigned) value) > 0xFF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (((unsigned) a) + ((unsigned) value) > 0xFF) { |
15 |
30 |
45 |
|
| 1121 |
15 |
| gb->af |= GB_CARRY_FLAG; |
| 1122 |
|
| } |
| 1123 |
45 |
| } |
| 1124 |
|
| |
| 1125 |
0 |
| static void adc_a_d8(GB_gameboy_t *gb, uint8_t opcode) |
| 1126 |
|
| { |
| 1127 |
|
| uint8_t value, a, carry; |
| 1128 |
0 |
| value = cycle_read(gb, gb->pc++); |
| 1129 |
0 |
| a = gb->af >> 8; |
| 1130 |
0 |
| carry = (gb->af & GB_CARRY_FLAG) != 0; |
| 1131 |
0 |
| gb->af = (a + value + carry) << 8; |
| 1132 |
|
| |
| 1133 |
0 |
▶ | if (gb->af == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->af == 0) { |
0 |
0 |
0 |
|
| 1134 |
0 |
| gb->af |= GB_ZERO_FLAG; |
| 1135 |
|
| } |
| 1136 |
0 |
▶ | if ((a & 0xF) + (value & 0xF) + carry > 0x0F) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((a & 0xF) + (value & 0xF) + carry > 0x0F) { |
0 |
0 |
0 |
|
| 1137 |
0 |
| gb->af |= GB_HALF_CARRY_FLAG; |
| 1138 |
|
| } |
| 1139 |
0 |
▶ | if (((unsigned) a) + ((unsigned) value) + carry > 0xFF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (((unsigned) a) + ((unsigned) value) + carry > 0xFF) { |
0 |
0 |
0 |
|
| 1140 |
0 |
| gb->af |= GB_CARRY_FLAG; |
| 1141 |
|
| } |
| 1142 |
0 |
| } |
| 1143 |
|
| |
| 1144 |
570 |
| static void sub_a_d8(GB_gameboy_t *gb, uint8_t opcode) |
| 1145 |
|
| { |
| 1146 |
|
| uint8_t value, a; |
| 1147 |
570 |
| value = cycle_read(gb, gb->pc++); |
| 1148 |
570 |
| a = gb->af >> 8; |
| 1149 |
570 |
| gb->af = ((a - value) << 8) | GB_SUBTRACT_FLAG; |
| 1150 |
570 |
▶ | if (a == value) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (a == value) { |
10 |
560 |
570 |
|
| 1151 |
10 |
| gb->af |= GB_ZERO_FLAG; |
| 1152 |
|
| } |
| 1153 |
570 |
▶ | if ((a & 0xF) < (value & 0xF)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((a & 0xF) < (value & 0xF)) { |
70 |
500 |
570 |
|
| 1154 |
70 |
| gb->af |= GB_HALF_CARRY_FLAG; |
| 1155 |
|
| } |
| 1156 |
570 |
▶ | if (a < value) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (a < value) { |
135 |
435 |
570 |
|
| 1157 |
135 |
| gb->af |= GB_CARRY_FLAG; |
| 1158 |
|
| } |
| 1159 |
570 |
| } |
| 1160 |
|
| |
| 1161 |
0 |
| static void sbc_a_d8(GB_gameboy_t *gb, uint8_t opcode) |
| 1162 |
|
| { |
| 1163 |
|
| uint8_t value, a, carry; |
| 1164 |
0 |
| value = cycle_read(gb, gb->pc++); |
| 1165 |
0 |
| a = gb->af >> 8; |
| 1166 |
0 |
| carry = (gb->af & GB_CARRY_FLAG) != 0; |
| 1167 |
0 |
| gb->af = ((a - value - carry) << 8) | GB_SUBTRACT_FLAG; |
| 1168 |
|
| |
| 1169 |
0 |
▶ | if ((uint8_t) (a - value - carry) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((uint8_t) (a - value - carry) == 0) { |
0 |
0 |
0 |
|
| 1170 |
0 |
| gb->af |= GB_ZERO_FLAG; |
| 1171 |
|
| } |
| 1172 |
0 |
▶ | if ((a & 0xF) < (value & 0xF) + carry) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((a & 0xF) < (value & 0xF) + carry) { |
0 |
0 |
0 |
|
| 1173 |
0 |
| gb->af |= GB_HALF_CARRY_FLAG; |
| 1174 |
|
| } |
| 1175 |
0 |
▶ | if (((unsigned) a) - ((unsigned) value) - carry > 0xFF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (((unsigned) a) - ((unsigned) value) - carry > 0xFF) { |
0 |
0 |
0 |
|
| 1176 |
0 |
| gb->af |= GB_CARRY_FLAG; |
| 1177 |
|
| } |
| 1178 |
0 |
| } |
| 1179 |
|
| |
| 1180 |
1222 |
| static void and_a_d8(GB_gameboy_t *gb, uint8_t opcode) |
| 1181 |
|
| { |
| 1182 |
|
| uint8_t value, a; |
| 1183 |
1222 |
| value = cycle_read(gb, gb->pc++); |
| 1184 |
1222 |
| a = gb->af >> 8; |
| 1185 |
1222 |
| gb->af = ((a & value) << 8) | GB_HALF_CARRY_FLAG; |
| 1186 |
1222 |
▶ | if ((a & value) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((a & value) == 0) { |
405 |
817 |
1222 |
|
| 1187 |
405 |
| gb->af |= GB_ZERO_FLAG; |
| 1188 |
|
| } |
| 1189 |
1222 |
| } |
| 1190 |
|
| |
| 1191 |
48 |
| static void xor_a_d8(GB_gameboy_t *gb, uint8_t opcode) |
| 1192 |
|
| { |
| 1193 |
|
| uint8_t value, a; |
| 1194 |
48 |
| value = cycle_read(gb, gb->pc++); |
| 1195 |
48 |
| a = gb->af >> 8; |
| 1196 |
48 |
| gb->af = (a ^ value) << 8; |
| 1197 |
48 |
▶ | if ((a ^ value) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((a ^ value) == 0) { |
1 |
47 |
48 |
|
| 1198 |
1 |
| gb->af |= GB_ZERO_FLAG; |
| 1199 |
|
| } |
| 1200 |
48 |
| } |
| 1201 |
|
| |
| 1202 |
272 |
| static void or_a_d8(GB_gameboy_t *gb, uint8_t opcode) |
| 1203 |
|
| { |
| 1204 |
|
| uint8_t value, a; |
| 1205 |
272 |
| value = cycle_read(gb, gb->pc++); |
| 1206 |
272 |
| a = gb->af >> 8; |
| 1207 |
272 |
| gb->af = (a | value) << 8; |
| 1208 |
272 |
▶ | if ((a | value) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((a | value) == 0) { |
0 |
272 |
272 |
|
| 1209 |
0 |
| gb->af |= GB_ZERO_FLAG; |
| 1210 |
|
| } |
| 1211 |
272 |
| } |
| 1212 |
|
| |
| 1213 |
2452 |
| static void cp_a_d8(GB_gameboy_t *gb, uint8_t opcode) |
| 1214 |
|
| { |
| 1215 |
|
| uint8_t value, a; |
| 1216 |
2452 |
| value = cycle_read(gb, gb->pc++); |
| 1217 |
2452 |
| a = gb->af >> 8; |
| 1218 |
2452 |
| gb->af &= 0xFF00; |
| 1219 |
2452 |
| gb->af |= GB_SUBTRACT_FLAG; |
| 1220 |
2452 |
▶ | if (a == value) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (a == value) { |
465 |
1987 |
2452 |
|
| 1221 |
465 |
| gb->af |= GB_ZERO_FLAG; |
| 1222 |
|
| } |
| 1223 |
2452 |
▶ | if ((a & 0xF) < (value & 0xF)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((a & 0xF) < (value & 0xF)) { |
1159 |
1293 |
2452 |
|
| 1224 |
1159 |
| gb->af |= GB_HALF_CARRY_FLAG; |
| 1225 |
|
| } |
| 1226 |
2452 |
▶ | if (a < value) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (a < value) { |
1862 |
590 |
2452 |
|
| 1227 |
1862 |
| gb->af |= GB_CARRY_FLAG; |
| 1228 |
|
| } |
| 1229 |
2452 |
| } |
| 1230 |
|
| |
| 1231 |
0 |
| static void rst(GB_gameboy_t *gb, uint8_t opcode) |
| 1232 |
|
| { |
| 1233 |
0 |
| uint16_t call_addr = gb->pc - 1; |
| 1234 |
0 |
| cycle_oam_bug(gb, GB_REGISTER_SP); |
| 1235 |
0 |
| cycle_write(gb, --gb->sp, (gb->pc) >> 8); |
| 1236 |
0 |
| cycle_write(gb, --gb->sp, (gb->pc) & 0xFF); |
| 1237 |
0 |
| gb->pc = opcode ^ 0xC7; |
| 1238 |
|
| GB_debugger_call_hook(gb, call_addr); |
| 1239 |
0 |
| } |
| 1240 |
|
| |
| 1241 |
1914 |
| static void ret(GB_gameboy_t *gb, uint8_t opcode) |
| 1242 |
|
| { |
| 1243 |
|
| GB_debugger_ret_hook(gb); |
| 1244 |
1914 |
| gb->pc = cycle_read(gb, gb->sp++); |
| 1245 |
1914 |
| gb->pc |= cycle_read(gb, gb->sp++) << 8; |
| 1246 |
1914 |
| cycle_no_access(gb); |
| 1247 |
1914 |
| } |
| 1248 |
|
| |
| 1249 |
0 |
| static void reti(GB_gameboy_t *gb, uint8_t opcode) |
| 1250 |
|
| { |
| 1251 |
0 |
| ret(gb, opcode); |
| 1252 |
0 |
| gb->ime = true; |
| 1253 |
0 |
| } |
| 1254 |
|
| |
| 1255 |
194 |
| static void ret_cc(GB_gameboy_t *gb, uint8_t opcode) |
| 1256 |
|
| { |
| 1257 |
194 |
▶ | if (condition_code(gb, opcode)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (condition_code(gb, opcode)) { |
194 |
0 |
194 |
|
| 1258 |
194 |
| cycle_no_access(gb); |
| 1259 |
194 |
| ret(gb, opcode); |
| 1260 |
|
| } |
| 1261 |
|
| else { |
| 1262 |
0 |
| cycle_no_access(gb); |
| 1263 |
|
| } |
| 1264 |
194 |
| } |
| 1265 |
|
| |
| 1266 |
1933 |
| static void call_a16(GB_gameboy_t *gb, uint8_t opcode) |
| 1267 |
|
| { |
| 1268 |
1933 |
| uint16_t call_addr = gb->pc - 1; |
| 1269 |
1933 |
| uint16_t addr = cycle_read(gb, gb->pc++); |
| 1270 |
1933 |
| addr |= (cycle_read(gb, gb->pc++) << 8); |
| 1271 |
1933 |
| cycle_oam_bug(gb, GB_REGISTER_SP); |
| 1272 |
1933 |
| cycle_write(gb, --gb->sp, (gb->pc) >> 8); |
| 1273 |
1933 |
| cycle_write(gb, --gb->sp, (gb->pc) & 0xFF); |
| 1274 |
1933 |
| gb->pc = addr; |
| 1275 |
|
| GB_debugger_call_hook(gb, call_addr); |
| 1276 |
1933 |
| } |
| 1277 |
|
| |
| 1278 |
851 |
| static void ld_da8_a(GB_gameboy_t *gb, uint8_t opcode) |
| 1279 |
|
| { |
| 1280 |
851 |
| uint8_t temp = cycle_read(gb, gb->pc++); |
| 1281 |
851 |
| cycle_write(gb, 0xFF00 + temp, gb->af >> 8); |
| 1282 |
851 |
| } |
| 1283 |
|
| |
| 1284 |
194 |
| static void ld_a_da8(GB_gameboy_t *gb, uint8_t opcode) |
| 1285 |
|
| { |
| 1286 |
194 |
| gb->af &= 0xFF; |
| 1287 |
194 |
| uint8_t temp = cycle_read(gb, gb->pc++); |
| 1288 |
194 |
| gb->af |= cycle_read(gb, 0xFF00 + temp) << 8; |
| 1289 |
194 |
| } |
| 1290 |
|
| |
| 1291 |
325 |
| static void ld_dc_a(GB_gameboy_t *gb, uint8_t opcode) |
| 1292 |
|
| { |
| 1293 |
325 |
| cycle_write(gb, 0xFF00 + (gb->bc & 0xFF), gb->af >> 8); |
| 1294 |
325 |
| } |
| 1295 |
|
| |
| 1296 |
0 |
| static void ld_a_dc(GB_gameboy_t *gb, uint8_t opcode) |
| 1297 |
|
| { |
| 1298 |
0 |
| gb->af &= 0xFF; |
| 1299 |
0 |
| gb->af |= cycle_read(gb, 0xFF00 + (gb->bc & 0xFF)) << 8; |
| 1300 |
0 |
| } |
| 1301 |
|
| |
| 1302 |
0 |
| static void add_sp_r8(GB_gameboy_t *gb, uint8_t opcode) |
| 1303 |
|
| { |
| 1304 |
|
| int16_t offset; |
| 1305 |
0 |
| uint16_t sp = gb->sp; |
| 1306 |
0 |
| offset = (int8_t) cycle_read(gb, gb->pc++); |
| 1307 |
0 |
| cycle_no_access(gb); |
| 1308 |
0 |
| cycle_no_access(gb); |
| 1309 |
0 |
| gb->sp += offset; |
| 1310 |
|
| |
| 1311 |
0 |
| gb->af &= 0xFF00; |
| 1312 |
|
| |
| 1313 |
|
| /* A new instruction, a new meaning for Half Carry! */ |
| 1314 |
0 |
▶ | if ((sp & 0xF) + (offset & 0xF) > 0xF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((sp & 0xF) + (offset & 0xF) > 0xF) { |
0 |
0 |
0 |
|
| 1315 |
0 |
| gb->af |= GB_HALF_CARRY_FLAG; |
| 1316 |
|
| } |
| 1317 |
|
| |
| 1318 |
0 |
▶ | if ((sp & 0xFF) + (offset & 0xFF) > 0xFF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((sp & 0xFF) + (offset & 0xFF) > 0xFF) { |
0 |
0 |
0 |
|
| 1319 |
0 |
| gb->af |= GB_CARRY_FLAG; |
| 1320 |
|
| } |
| 1321 |
0 |
| } |
| 1322 |
|
| |
| 1323 |
0 |
| static void jp_hl(GB_gameboy_t *gb, uint8_t opcode) |
| 1324 |
|
| { |
| 1325 |
0 |
| gb->pc = gb->hl; |
| 1326 |
0 |
| } |
| 1327 |
|
| |
| 1328 |
1 |
| static void ld_da16_a(GB_gameboy_t *gb, uint8_t opcode) |
| 1329 |
|
| { |
| 1330 |
|
| uint16_t addr; |
| 1331 |
1 |
| addr = cycle_read(gb, gb->pc++); |
| 1332 |
1 |
| addr |= cycle_read(gb, gb->pc++) << 8; |
| 1333 |
1 |
| cycle_write(gb, addr, gb->af >> 8); |
| 1334 |
1 |
| } |
| 1335 |
|
| |
| 1336 |
0 |
| static void ld_a_da16(GB_gameboy_t *gb, uint8_t opcode) |
| 1337 |
|
| { |
| 1338 |
|
| uint16_t addr; |
| 1339 |
0 |
| gb->af &= 0xFF; |
| 1340 |
0 |
| addr = cycle_read(gb, gb->pc++); |
| 1341 |
0 |
| addr |= cycle_read(gb, gb->pc++) << 8; |
| 1342 |
0 |
| gb->af |= cycle_read(gb, addr) << 8; |
| 1343 |
0 |
| } |
| 1344 |
|
| |
| 1345 |
0 |
| static void di(GB_gameboy_t *gb, uint8_t opcode) |
| 1346 |
|
| { |
| 1347 |
|
| /* DI is NOT delayed, not even on a CGB. Mooneye's di_timing-GS test fails on a CGB |
| 1348 |
|
| for different reasons. */ |
| 1349 |
0 |
| gb->ime = false; |
| 1350 |
0 |
| } |
| 1351 |
|
| |
| 1352 |
0 |
| static void ei(GB_gameboy_t *gb, uint8_t opcode) |
| 1353 |
|
| { |
| 1354 |
|
| /* ei is actually "disable interrupts for one instruction, then enable them". */ |
| 1355 |
0 |
▶ | if (!gb->ime && !gb->ime_toggle) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!gb->ime && !gb->ime_toggle) { |
0 |
0 |
0 |
| if (!gb->ime && !gb->ime_toggle) { |
0 |
0 |
0 |
|
| 1356 |
0 |
| gb->ime_toggle = true; |
| 1357 |
|
| } |
| 1358 |
0 |
| } |
| 1359 |
|
| |
| 1360 |
0 |
| static void ld_hl_sp_r8(GB_gameboy_t *gb, uint8_t opcode) |
| 1361 |
|
| { |
| 1362 |
|
| int16_t offset; |
| 1363 |
0 |
| gb->af &= 0xFF00; |
| 1364 |
0 |
| offset = (int8_t) cycle_read(gb, gb->pc++); |
| 1365 |
0 |
| cycle_no_access(gb); |
| 1366 |
0 |
| gb->hl = gb->sp + offset; |
| 1367 |
|
| |
| 1368 |
0 |
▶ | if ((gb->sp & 0xF) + (offset & 0xF) > 0xF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((gb->sp & 0xF) + (offset & 0xF) > 0xF) { |
0 |
0 |
0 |
|
| 1369 |
0 |
| gb->af |= GB_HALF_CARRY_FLAG; |
| 1370 |
|
| } |
| 1371 |
|
| |
| 1372 |
0 |
▶ | if ((gb->sp & 0xFF) + (offset & 0xFF) > 0xFF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((gb->sp & 0xFF) + (offset & 0xFF) > 0xFF) { |
0 |
0 |
0 |
|
| 1373 |
0 |
| gb->af |= GB_CARRY_FLAG; |
| 1374 |
|
| } |
| 1375 |
0 |
| } |
| 1376 |
|
| |
| 1377 |
0 |
| static void ld_sp_hl(GB_gameboy_t *gb, uint8_t opcode) |
| 1378 |
|
| { |
| 1379 |
0 |
| gb->sp = gb->hl; |
| 1380 |
0 |
| cycle_oam_bug(gb, GB_REGISTER_HL); |
| 1381 |
0 |
| } |
| 1382 |
|
| |
| 1383 |
0 |
| static void rlc_r(GB_gameboy_t *gb, uint8_t opcode) |
| 1384 |
|
| { |
| 1385 |
|
| bool carry; |
| 1386 |
|
| uint8_t value; |
| 1387 |
0 |
| value = get_src_value(gb, opcode); |
| 1388 |
0 |
| carry = (value & 0x80) != 0; |
| 1389 |
0 |
| gb->af &= 0xFF00; |
| 1390 |
0 |
| set_src_value(gb, opcode, (value << 1) | carry); |
| 1391 |
0 |
▶ | if (carry) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (carry) { |
0 |
0 |
0 |
|
| 1392 |
0 |
| gb->af |= GB_CARRY_FLAG; |
| 1393 |
|
| } |
| 1394 |
0 |
▶ | if (value == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (value == 0) { |
0 |
0 |
0 |
|
| 1395 |
0 |
| gb->af |= GB_ZERO_FLAG; |
| 1396 |
|
| } |
| 1397 |
0 |
| } |
| 1398 |
|
| |
| 1399 |
0 |
| static void rrc_r(GB_gameboy_t *gb, uint8_t opcode) |
| 1400 |
|
| { |
| 1401 |
|
| bool carry; |
| 1402 |
|
| uint8_t value; |
| 1403 |
0 |
| value = get_src_value(gb, opcode); |
| 1404 |
0 |
| carry = (value & 0x01) != 0; |
| 1405 |
0 |
| gb->af &= 0xFF00; |
| 1406 |
0 |
| value = (value >> 1) | (carry << 7); |
| 1407 |
0 |
| set_src_value(gb, opcode, value); |
| 1408 |
0 |
▶ | if (carry) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (carry) { |
0 |
0 |
0 |
|
| 1409 |
0 |
| gb->af |= GB_CARRY_FLAG; |
| 1410 |
|
| } |
| 1411 |
0 |
▶ | if (value == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (value == 0) { |
0 |
0 |
0 |
|
| 1412 |
0 |
| gb->af |= GB_ZERO_FLAG; |
| 1413 |
|
| } |
| 1414 |
0 |
| } |
| 1415 |
|
| |
| 1416 |
7357 |
| static void rl_r(GB_gameboy_t *gb, uint8_t opcode) |
| 1417 |
|
| { |
| 1418 |
|
| bool carry; |
| 1419 |
|
| uint8_t value; |
| 1420 |
|
| bool bit7; |
| 1421 |
7357 |
| value = get_src_value(gb, opcode); |
| 1422 |
7357 |
| carry = (gb->af & GB_CARRY_FLAG) != 0; |
| 1423 |
7357 |
| bit7 = (value & 0x80) != 0; |
| 1424 |
|
| |
| 1425 |
7357 |
| gb->af &= 0xFF00; |
| 1426 |
7357 |
| value = (value << 1) | carry; |
| 1427 |
7357 |
| set_src_value(gb, opcode, value); |
| 1428 |
7357 |
▶ | if (bit7) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (bit7) { |
807 |
6550 |
7357 |
|
| 1429 |
807 |
| gb->af |= GB_CARRY_FLAG; |
| 1430 |
|
| } |
| 1431 |
7357 |
▶ | if (value == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (value == 0) { |
6144 |
1213 |
7357 |
|
| 1432 |
6144 |
| gb->af |= GB_ZERO_FLAG; |
| 1433 |
|
| } |
| 1434 |
7357 |
| } |
| 1435 |
|
| |
| 1436 |
40 |
| static void rr_r(GB_gameboy_t *gb, uint8_t opcode) |
| 1437 |
|
| { |
| 1438 |
|
| bool carry; |
| 1439 |
|
| uint8_t value; |
| 1440 |
|
| bool bit1; |
| 1441 |
|
| |
| 1442 |
40 |
| value = get_src_value(gb, opcode); |
| 1443 |
40 |
| carry = (gb->af & GB_CARRY_FLAG) != 0; |
| 1444 |
40 |
| bit1 = (value & 0x1) != 0; |
| 1445 |
|
| |
| 1446 |
40 |
| gb->af &= 0xFF00; |
| 1447 |
40 |
| value = (value >> 1) | (carry << 7); |
| 1448 |
40 |
| set_src_value(gb, opcode, value); |
| 1449 |
40 |
▶ | if (bit1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (bit1) { |
30 |
10 |
40 |
|
| 1450 |
30 |
| gb->af |= GB_CARRY_FLAG; |
| 1451 |
|
| } |
| 1452 |
40 |
▶ | if (value == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (value == 0) { |
0 |
40 |
40 |
|
| 1453 |
0 |
| gb->af |= GB_ZERO_FLAG; |
| 1454 |
|
| } |
| 1455 |
40 |
| } |
| 1456 |
|
| |
| 1457 |
12769 |
| static void sla_r(GB_gameboy_t *gb, uint8_t opcode) |
| 1458 |
|
| { |
| 1459 |
|
| uint8_t value; |
| 1460 |
|
| bool carry; |
| 1461 |
12769 |
| value = get_src_value(gb, opcode); |
| 1462 |
12769 |
| carry = (value & 0x80) != 0; |
| 1463 |
12769 |
| gb->af &= 0xFF00; |
| 1464 |
12769 |
| set_src_value(gb, opcode, (value << 1)); |
| 1465 |
12769 |
▶ | if (carry) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (carry) { |
6536 |
6233 |
12769 |
|
| 1466 |
6536 |
| gb->af |= GB_CARRY_FLAG; |
| 1467 |
|
| } |
| 1468 |
12769 |
▶ | if ((value & 0x7F) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((value & 0x7F) == 0) { |
4284 |
8485 |
12769 |
|
| 1469 |
4284 |
| gb->af |= GB_ZERO_FLAG; |
| 1470 |
|
| } |
| 1471 |
12769 |
| } |
| 1472 |
|
| |
| 1473 |
30 |
| static void sra_r(GB_gameboy_t *gb, uint8_t opcode) |
| 1474 |
|
| { |
| 1475 |
|
| uint8_t bit7; |
| 1476 |
|
| uint8_t value; |
| 1477 |
30 |
| value = get_src_value(gb, opcode); |
| 1478 |
30 |
| bit7 = value & 0x80; |
| 1479 |
30 |
| gb->af &= 0xFF00; |
| 1480 |
30 |
▶ | if (value & 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (value & 1) { |
15 |
15 |
30 |
|
| 1481 |
15 |
| gb->af |= GB_CARRY_FLAG; |
| 1482 |
|
| } |
| 1483 |
30 |
| value = (value >> 1) | bit7; |
| 1484 |
30 |
| set_src_value(gb, opcode, value); |
| 1485 |
30 |
▶ | if (value == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (value == 0) { |
2 |
28 |
30 |
|
| 1486 |
2 |
| gb->af |= GB_ZERO_FLAG; |
| 1487 |
|
| } |
| 1488 |
30 |
| } |
| 1489 |
|
| |
| 1490 |
287 |
| static void srl_r(GB_gameboy_t *gb, uint8_t opcode) |
| 1491 |
|
| { |
| 1492 |
|
| uint8_t value; |
| 1493 |
287 |
| value = get_src_value(gb, opcode); |
| 1494 |
287 |
| gb->af &= 0xFF00; |
| 1495 |
287 |
| set_src_value(gb, opcode, (value >> 1)); |
| 1496 |
287 |
▶ | if (value & 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (value & 1) { |
162 |
125 |
287 |
|
| 1497 |
162 |
| gb->af |= GB_CARRY_FLAG; |
| 1498 |
|
| } |
| 1499 |
287 |
▶ | if (!(value >> 1)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(value >> 1)) { |
0 |
287 |
287 |
|
| 1500 |
0 |
| gb->af |= GB_ZERO_FLAG; |
| 1501 |
|
| } |
| 1502 |
287 |
| } |
| 1503 |
|
| |
| 1504 |
600 |
| static void swap_r(GB_gameboy_t *gb, uint8_t opcode) |
| 1505 |
|
| { |
| 1506 |
|
| uint8_t value; |
| 1507 |
600 |
| value = get_src_value(gb, opcode); |
| 1508 |
600 |
| gb->af &= 0xFF00; |
| 1509 |
600 |
| set_src_value(gb, opcode, (value >> 4) | (value << 4)); |
| 1510 |
600 |
▶ | if (!value) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!value) { |
360 |
240 |
600 |
|
| 1511 |
360 |
| gb->af |= GB_ZERO_FLAG; |
| 1512 |
|
| } |
| 1513 |
600 |
| } |
| 1514 |
|
| |
| 1515 |
760347 |
| static void bit_r(GB_gameboy_t *gb, uint8_t opcode) |
| 1516 |
|
| { |
| 1517 |
|
| uint8_t value; |
| 1518 |
|
| uint8_t bit; |
| 1519 |
760347 |
| value = get_src_value(gb, opcode); |
| 1520 |
760347 |
| bit = 1 << ((opcode >> 3) & 7); |
| 1521 |
760347 |
▶ | if ((opcode & 0xC0) == 0x40) { /* Bit */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ((opcode & 0xC0) == 0x40) { /* Bit */ |
760106 |
241 |
760347 |
|
| 1522 |
760106 |
| gb->af &= 0xFF00 | GB_CARRY_FLAG; |
| 1523 |
760106 |
| gb->af |= GB_HALF_CARRY_FLAG; |
| 1524 |
760106 |
▶ | if (!(bit & value)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(bit & value)) { |
759751 |
355 |
760106 |
|
| 1525 |
759751 |
| gb->af |= GB_ZERO_FLAG; |
| 1526 |
|
| } |
| 1527 |
|
| } |
| 1528 |
241 |
▶ | else if ((opcode & 0xC0) == 0x80) { /* res */ |
Branch: 1 branch
| Branch | True | False | Total |
| else if ((opcode & 0xC0) == 0x80) { /* res */ |
241 |
0 |
241 |
|
| 1529 |
241 |
| set_src_value(gb, opcode, value & ~bit); |
| 1530 |
|
| } |
| 1531 |
0 |
▶ | else if ((opcode & 0xC0) == 0xC0) { /* set */ |
Branch: 1 branch
| Branch | True | False | Total |
| else if ((opcode & 0xC0) == 0xC0) { /* set */ |
0 |
0 |
0 |
|
| 1532 |
0 |
| set_src_value(gb, opcode, value | bit); |
| 1533 |
|
| } |
| 1534 |
760347 |
| } |
| 1535 |
|
| |
| 1536 |
781430 |
| static void cb_prefix(GB_gameboy_t *gb, uint8_t opcode) |
| 1537 |
|
| { |
| 1538 |
781430 |
| opcode = cycle_read(gb, gb->pc++); |
| 1539 |
781430 |
| switch (opcode >> 3) { |
| 1540 |
0 |
| case 0: |
| 1541 |
0 |
| rlc_r(gb, opcode); |
| 1542 |
0 |
| break; |
| 1543 |
0 |
| case 1: |
| 1544 |
0 |
| rrc_r(gb, opcode); |
| 1545 |
0 |
| break; |
| 1546 |
7357 |
| case 2: |
| 1547 |
7357 |
| rl_r(gb, opcode); |
| 1548 |
7357 |
| break; |
| 1549 |
40 |
| case 3: |
| 1550 |
40 |
| rr_r(gb, opcode); |
| 1551 |
40 |
| break; |
| 1552 |
12769 |
| case 4: |
| 1553 |
12769 |
| sla_r(gb, opcode); |
| 1554 |
12769 |
| break; |
| 1555 |
30 |
| case 5: |
| 1556 |
30 |
| sra_r(gb, opcode); |
| 1557 |
30 |
| break; |
| 1558 |
600 |
| case 6: |
| 1559 |
600 |
| swap_r(gb, opcode); |
| 1560 |
600 |
| break; |
| 1561 |
287 |
| case 7: |
| 1562 |
287 |
| srl_r(gb, opcode); |
| 1563 |
287 |
| break; |
| 1564 |
760347 |
| default: |
| 1565 |
760347 |
| bit_r(gb, opcode); |
| 1566 |
760347 |
| break; |
| 1567 |
|
| } |
| 1568 |
781430 |
| } |
| 1569 |
|
| |
| 1570 |
|
| static opcode_t *opcodes[256] = { |
| 1571 |
|
| /* X0 X1 X2 X3 X4 X5 X6 X7 */ |
| 1572 |
|
| /* X8 X9 Xa Xb Xc Xd Xe Xf */ |
| 1573 |
|
| nop, ld_rr_d16, ld_drr_a, inc_rr, inc_hr, dec_hr, ld_hr_d8, rlca, /* 0X */ |
| 1574 |
|
| ld_da16_sp, add_hl_rr, ld_a_drr, dec_rr, inc_lr, dec_lr, ld_lr_d8, rrca, |
| 1575 |
|
| stop, ld_rr_d16, ld_drr_a, inc_rr, inc_hr, dec_hr, ld_hr_d8, rla, /* 1X */ |
| 1576 |
|
| jr_r8, add_hl_rr, ld_a_drr, dec_rr, inc_lr, dec_lr, ld_lr_d8, rra, |
| 1577 |
|
| jr_cc_r8, ld_rr_d16, ld_dhli_a, inc_rr, inc_hr, dec_hr, ld_hr_d8, daa, /* 2X */ |
| 1578 |
|
| jr_cc_r8, add_hl_rr, ld_a_dhli, dec_rr, inc_lr, dec_lr, ld_lr_d8, cpl, |
| 1579 |
|
| jr_cc_r8, ld_rr_d16, ld_dhld_a, inc_rr, inc_dhl, dec_dhl, ld_dhl_d8, scf, /* 3X */ |
| 1580 |
|
| jr_cc_r8, add_hl_rr, ld_a_dhld, dec_rr, inc_hr, dec_hr, ld_hr_d8, ccf, |
| 1581 |
|
| ld_b_b, ld_b_c, ld_b_d, ld_b_e, ld_b_h, ld_b_l, ld_b_dhl, ld_b_a, /* 4X */ |
| 1582 |
|
| ld_c_b, nop, ld_c_d, ld_c_e, ld_c_h, ld_c_l, ld_c_dhl, ld_c_a, |
| 1583 |
|
| ld_d_b, ld_d_c, nop, ld_d_e, ld_d_h, ld_d_l, ld_d_dhl, ld_d_a, /* 5X */ |
| 1584 |
|
| ld_e_b, ld_e_c, ld_e_d, nop, ld_e_h, ld_e_l, ld_e_dhl, ld_e_a, |
| 1585 |
|
| ld_h_b, ld_h_c, ld_h_d, ld_h_e, nop, ld_h_l, ld_h_dhl, ld_h_a, /* 6X */ |
| 1586 |
|
| ld_l_b, ld_l_c, ld_l_d, ld_l_e, ld_l_h, nop, ld_l_dhl, ld_l_a, |
| 1587 |
|
| ld_dhl_b, ld_dhl_c, ld_dhl_d, ld_dhl_e, ld_dhl_h, ld_dhl_l, halt, ld_dhl_a, /* 7X */ |
| 1588 |
|
| ld_a_b, ld_a_c, ld_a_d, ld_a_e, ld_a_h, ld_a_l, ld_a_dhl, nop, |
| 1589 |
|
| add_a_r, add_a_r, add_a_r, add_a_r, add_a_r, add_a_r, add_a_r, add_a_r, /* 8X */ |
| 1590 |
|
| adc_a_r, adc_a_r, adc_a_r, adc_a_r, adc_a_r, adc_a_r, adc_a_r, adc_a_r, |
| 1591 |
|
| sub_a_r, sub_a_r, sub_a_r, sub_a_r, sub_a_r, sub_a_r, sub_a_r, sub_a_r, /* 9X */ |
| 1592 |
|
| sbc_a_r, sbc_a_r, sbc_a_r, sbc_a_r, sbc_a_r, sbc_a_r, sbc_a_r, sbc_a_r, |
| 1593 |
|
| and_a_r, and_a_r, and_a_r, and_a_r, and_a_r, and_a_r, and_a_r, and_a_r, /* aX */ |
| 1594 |
|
| xor_a_r, xor_a_r, xor_a_r, xor_a_r, xor_a_r, xor_a_r, xor_a_r, xor_a_r, |
| 1595 |
|
| or_a_r, or_a_r, or_a_r, or_a_r, or_a_r, or_a_r, or_a_r, or_a_r, /* bX */ |
| 1596 |
|
| cp_a_r, cp_a_r, cp_a_r, cp_a_r, cp_a_r, cp_a_r, cp_a_r, cp_a_r, |
| 1597 |
|
| ret_cc, pop_rr, jp_cc_a16, jp_a16, call_cc_a16,push_rr, add_a_d8, rst, /* cX */ |
| 1598 |
|
| ret_cc, ret, jp_cc_a16, cb_prefix, call_cc_a16,call_a16, adc_a_d8, rst, |
| 1599 |
|
| ret_cc, pop_rr, jp_cc_a16, ill, call_cc_a16,push_rr, sub_a_d8, rst, /* dX */ |
| 1600 |
|
| ret_cc, reti, jp_cc_a16, ill, call_cc_a16,ill, sbc_a_d8, rst, |
| 1601 |
|
| ld_da8_a, pop_rr, ld_dc_a, ill, ill, push_rr, and_a_d8, rst, /* eX */ |
| 1602 |
|
| add_sp_r8, jp_hl, ld_da16_a, ill, ill, ill, xor_a_d8, rst, |
| 1603 |
|
| ld_a_da8, pop_rr, ld_a_dc, di, ill, push_rr, or_a_d8, rst, /* fX */ |
| 1604 |
|
| ld_hl_sp_r8,ld_sp_hl, ld_a_da16, ei, ill, ill, cp_a_d8, rst, |
| 1605 |
|
| }; |
| 1606 |
1864453 |
| void GB_cpu_run(GB_gameboy_t *gb) |
| 1607 |
|
| { |
| 1608 |
1864453 |
▶ | if (unlikely(gb->stopped)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (unlikely(gb->stopped)) { |
0 |
1864453 |
1864453 |
|
| 1609 |
0 |
| GB_timing_sync(gb); |
| 1610 |
0 |
| GB_advance_cycles(gb, 4); |
| 1611 |
0 |
▶ | if ((gb->io_registers[GB_IO_JOYP] & 0x30) != 0x30) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((gb->io_registers[GB_IO_JOYP] & 0x30) != 0x30) { |
0 |
0 |
0 |
|
| 1612 |
0 |
| gb->joyp_accessed = true; |
| 1613 |
|
| } |
| 1614 |
0 |
▶ | if ((gb->io_registers[GB_IO_JOYP] & 0xF) != 0xF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((gb->io_registers[GB_IO_JOYP] & 0xF) != 0xF) { |
0 |
0 |
0 |
|
| 1615 |
0 |
| leave_stop_mode(gb); |
| 1616 |
0 |
| GB_advance_cycles(gb, 8); |
| 1617 |
|
| } |
| 1618 |
0 |
| return; |
| 1619 |
|
| } |
| 1620 |
|
| |
| 1621 |
1864453 |
▶ | if ((gb->interrupt_enable & 0x10) && (gb->ime || gb->halted)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if ((gb->interrupt_enable & 0x10) && (gb->ime || gb->halted)) { |
0 |
1864453 |
1864453 |
| if ((gb->interrupt_enable & 0x10) && (gb->ime || gb->halted)) { |
0 |
0 |
0 |
| if ((gb->interrupt_enable & 0x10) && (gb->ime || gb->halted)) { |
0 |
0 |
0 |
|
| 1622 |
0 |
| GB_timing_sync(gb); |
| 1623 |
|
| } |
| 1624 |
|
| |
| 1625 |
1864453 |
▶ | if (gb->halted && !GB_is_cgb(gb) && !gb->just_halted) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (gb->halted && !GB_is_cgb(gb) && !gb->just_halted) { |
0 |
1864453 |
1864453 |
| if (gb->halted && !GB_is_cgb(gb) && !gb->just_halted) { |
0 |
0 |
0 |
| if (gb->halted && !GB_is_cgb(gb) && !gb->just_halted) { |
0 |
0 |
0 |
|
| 1626 |
0 |
| GB_advance_cycles(gb, 2); |
| 1627 |
|
| } |
| 1628 |
|
| |
| 1629 |
1864453 |
| uint8_t interrupt_queue = gb->interrupt_enable & gb->io_registers[GB_IO_IF] & 0x1F; |
| 1630 |
|
| |
| 1631 |
1864453 |
▶ | if (gb->halted) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->halted) { |
0 |
1864453 |
1864453 |
|
| 1632 |
0 |
▶ | GB_advance_cycles(gb, (GB_is_cgb(gb) || gb->just_halted) ? 4 : 2); |
Branch: 2 branches
| Branch | True | False | Total |
| GB_advance_cycles(gb, (GB_is_cgb(gb) || gb->just_halted) ? 4 : 2); |
0 |
0 |
0 |
| GB_advance_cycles(gb, (GB_is_cgb(gb) || gb->just_halted) ? 4 : 2); |
0 |
0 |
0 |
|
| 1633 |
|
| } |
| 1634 |
1864453 |
| gb->just_halted = false; |
| 1635 |
|
| |
| 1636 |
1864453 |
| bool effective_ime = gb->ime; |
| 1637 |
1864453 |
▶ | if (gb->ime_toggle) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->ime_toggle) { |
0 |
1864453 |
1864453 |
|
| 1638 |
0 |
| gb->ime = !gb->ime; |
| 1639 |
0 |
| gb->ime_toggle = false; |
| 1640 |
|
| } |
| 1641 |
|
| |
| 1642 |
|
| /* Wake up from HALT mode without calling interrupt code. */ |
| 1643 |
1864453 |
▶ | if (gb->halted && !effective_ime && interrupt_queue) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (gb->halted && !effective_ime && interrupt_queue) { |
0 |
1864453 |
1864453 |
| if (gb->halted && !effective_ime && interrupt_queue) { |
0 |
0 |
0 |
| if (gb->halted && !effective_ime && interrupt_queue) { |
0 |
0 |
0 |
|
| 1644 |
0 |
| gb->halted = false; |
| 1645 |
0 |
▶ | if (gb->hdma_on_hblank && (gb->io_registers[GB_IO_STAT] & 3) == 0 && gb->allow_hdma_on_wake) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (gb->hdma_on_hblank && (gb->io_registers[GB_IO_STAT] & 3) == 0 && gb->allow_hdma_on_wake) { |
0 |
0 |
0 |
| if (gb->hdma_on_hblank && (gb->io_registers[GB_IO_STAT] & 3) == 0 && gb->allow_hdma_on_wake) { |
0 |
0 |
0 |
| if (gb->hdma_on_hblank && (gb->io_registers[GB_IO_STAT] & 3) == 0 && gb->allow_hdma_on_wake) { |
0 |
0 |
0 |
|
| 1646 |
0 |
| gb->hdma_on = true; |
| 1647 |
|
| } |
| 1648 |
0 |
| gb->dma_cycles = 4; |
| 1649 |
0 |
| GB_dma_run(gb); |
| 1650 |
0 |
| gb->speed_switch_halt_countdown = 0; |
| 1651 |
|
| } |
| 1652 |
|
| |
| 1653 |
|
| /* Call interrupt */ |
| 1654 |
1864453 |
▶ | else if (unlikely(effective_ime && interrupt_queue)) { |
Branch: 2 branches
| Branch | True | False | Total |
| else if (unlikely(effective_ime && interrupt_queue)) { |
0 |
1864453 |
1864453 |
| else if (unlikely(effective_ime && interrupt_queue)) { |
0 |
0 |
0 |
|
| 1655 |
0 |
| gb->halted = false; |
| 1656 |
0 |
▶ | if (gb->hdma_on_hblank && (gb->io_registers[GB_IO_STAT] & 3) == 0 && gb->allow_hdma_on_wake) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (gb->hdma_on_hblank && (gb->io_registers[GB_IO_STAT] & 3) == 0 && gb->allow_hdma_on_wake) { |
0 |
0 |
0 |
| if (gb->hdma_on_hblank && (gb->io_registers[GB_IO_STAT] & 3) == 0 && gb->allow_hdma_on_wake) { |
0 |
0 |
0 |
| if (gb->hdma_on_hblank && (gb->io_registers[GB_IO_STAT] & 3) == 0 && gb->allow_hdma_on_wake) { |
0 |
0 |
0 |
|
| 1657 |
0 |
| gb->hdma_on = true; |
| 1658 |
|
| } |
| 1659 |
|
| // TODO: verify the timing! |
| 1660 |
0 |
| gb->dma_cycles = 4; |
| 1661 |
0 |
| GB_dma_run(gb); |
| 1662 |
0 |
| gb->speed_switch_halt_countdown = 0; |
| 1663 |
0 |
| uint16_t call_addr = gb->pc; |
| 1664 |
|
| |
| 1665 |
0 |
| cycle_read(gb, gb->pc++); |
| 1666 |
0 |
| cycle_oam_bug(gb, GB_REGISTER_PC); |
| 1667 |
0 |
| gb->pc--; |
| 1668 |
0 |
| GB_trigger_oam_bug(gb, gb->sp); /* Todo: test T-cycle timing */ |
| 1669 |
0 |
| cycle_no_access(gb); |
| 1670 |
|
| |
| 1671 |
0 |
| cycle_write(gb, --gb->sp, (gb->pc) >> 8); |
| 1672 |
0 |
| interrupt_queue = gb->interrupt_enable; |
| 1673 |
|
| |
| 1674 |
0 |
▶ | if (gb->sp == GB_IO_IF + 0xFF00 + 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->sp == GB_IO_IF + 0xFF00 + 1) { |
0 |
0 |
0 |
|
| 1675 |
0 |
| gb->sp--; |
| 1676 |
0 |
| interrupt_queue &= cycle_write_if(gb, (gb->pc) & 0xFF); |
| 1677 |
|
| } |
| 1678 |
|
| else { |
| 1679 |
0 |
| cycle_write(gb, --gb->sp, (gb->pc) & 0xFF); |
| 1680 |
0 |
| interrupt_queue &= (gb->io_registers[GB_IO_IF]) & 0x1F; |
| 1681 |
|
| } |
| 1682 |
|
| |
| 1683 |
0 |
▶ | if (interrupt_queue) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (interrupt_queue) { |
0 |
0 |
0 |
|
| 1684 |
0 |
| uint8_t interrupt_bit = 0; |
| 1685 |
0 |
▶ | while (!(interrupt_queue & 1)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (!(interrupt_queue & 1)) { |
0 |
0 |
0 |
|
| 1686 |
0 |
| interrupt_queue >>= 1; |
| 1687 |
0 |
| interrupt_bit++; |
| 1688 |
|
| } |
| 1689 |
|
| assert(gb->pending_cycles > 2); |
| 1690 |
0 |
| gb->pending_cycles -= 2; |
| 1691 |
0 |
| flush_pending_cycles(gb); |
| 1692 |
0 |
| gb->pending_cycles = 2; |
| 1693 |
0 |
| gb->io_registers[GB_IO_IF] &= ~(1 << interrupt_bit); |
| 1694 |
0 |
| gb->pc = interrupt_bit * 8 + 0x40; |
| 1695 |
|
| } |
| 1696 |
|
| else { |
| 1697 |
0 |
| gb->pc = 0; |
| 1698 |
|
| } |
| 1699 |
0 |
| gb->ime = false; |
| 1700 |
|
| GB_debugger_call_hook(gb, call_addr); |
| 1701 |
|
| } |
| 1702 |
|
| /* Run mode */ |
| 1703 |
1864453 |
▶ | else if (!gb->halted) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (!gb->halted) { |
1864453 |
0 |
1864453 |
|
| 1704 |
1864453 |
| uint8_t opcode = cycle_read(gb, gb->pc++); |
| 1705 |
1864453 |
▶ | if (unlikely(gb->hdma_on)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (unlikely(gb->hdma_on)) { |
0 |
1864453 |
1864453 |
|
| 1706 |
0 |
| GB_hdma_run(gb); |
| 1707 |
|
| } |
| 1708 |
1864453 |
▶ | if (unlikely(gb->execution_callback)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (unlikely(gb->execution_callback)) { |
0 |
1864453 |
1864453 |
|
| 1709 |
0 |
| gb->execution_callback(gb, gb->pc - 1, opcode); |
| 1710 |
|
| } |
| 1711 |
1864453 |
▶ | if (unlikely(gb->halt_bug)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (unlikely(gb->halt_bug)) { |
0 |
1864453 |
1864453 |
|
| 1712 |
0 |
| gb->pc--; |
| 1713 |
0 |
| gb->halt_bug = false; |
| 1714 |
|
| } |
| 1715 |
1864453 |
| opcodes[opcode](gb, opcode); |
| 1716 |
|
| } |
| 1717 |
|
| |
| 1718 |
1864453 |
| flush_pending_cycles(gb); |
| 1719 |
|
| } |