| 1 |
|
| #include "gb.h" |
| 2 |
|
| |
| 3 |
|
| static uint32_t noise_seed = 0; |
| 4 |
|
| |
| 5 |
|
| /* This is not a complete emulation of the camera chip. Only the features used by the Game Boy Camera ROMs are supported. |
| 6 |
|
| We also do not emulate the timing of the real cart when a webcam is used, as it might be actually faster than the webcam. */ |
| 7 |
|
| |
| 8 |
0 |
| static uint8_t generate_noise(uint8_t x, uint8_t y) |
| 9 |
|
| { |
| 10 |
0 |
| uint32_t value = (x * 151 + y * 149) ^ noise_seed; |
| 11 |
0 |
| uint32_t hash = 0; |
| 12 |
|
| |
| 13 |
0 |
▶ | while (value) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (value) { |
0 |
0 |
0 |
|
| 14 |
0 |
| hash <<= 1; |
| 15 |
0 |
▶ | if (hash & 0x100) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hash & 0x100) { |
0 |
0 |
0 |
|
| 16 |
0 |
| hash ^= 0x101; |
| 17 |
|
| } |
| 18 |
0 |
▶ | if (value & 0x80000000) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (value & 0x80000000) { |
0 |
0 |
0 |
|
| 19 |
0 |
| hash ^= 0xA1; |
| 20 |
|
| } |
| 21 |
0 |
| value <<= 1; |
| 22 |
|
| } |
| 23 |
0 |
| return hash; |
| 24 |
|
| } |
| 25 |
|
| |
| 26 |
0 |
| static long get_processed_color(GB_gameboy_t *gb, uint8_t x, uint8_t y) |
| 27 |
|
| { |
| 28 |
0 |
▶ | if (x == 128) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (x == 128) { |
0 |
0 |
0 |
|
| 29 |
0 |
| x = 127; |
| 30 |
|
| } |
| 31 |
0 |
▶ | else if (x > 128) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (x > 128) { |
0 |
0 |
0 |
|
| 32 |
0 |
| x = 0; |
| 33 |
|
| } |
| 34 |
|
| |
| 35 |
0 |
▶ | if (y == 112) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (y == 112) { |
0 |
0 |
0 |
|
| 36 |
0 |
| y = 111; |
| 37 |
|
| } |
| 38 |
0 |
▶ | else if (y >= 112) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (y >= 112) { |
0 |
0 |
0 |
|
| 39 |
0 |
| y = 0; |
| 40 |
|
| } |
| 41 |
|
| |
| 42 |
0 |
▶ | long color = gb->camera_get_pixel_callback? gb->camera_get_pixel_callback(gb, x, y) : (generate_noise(x, y)); |
Branch: 1 branch
| Branch | True | False | Total |
| long color = gb->camera_get_pixel_callback? gb->camera_get_pixel_callback(gb, x, y) : (generate_noise(x, y)); |
0 |
0 |
0 |
|
| 43 |
|
| |
| 44 |
|
| static const double gain_values[] = |
| 45 |
|
| {0.8809390, 0.9149149, 0.9457498, 0.9739758, |
| 46 |
|
| 1.0000000, 1.0241412, 1.0466537, 1.0677433, |
| 47 |
|
| 1.0875793, 1.1240310, 1.1568911, 1.1868043, |
| 48 |
|
| 1.2142561, 1.2396208, 1.2743837, 1.3157323, |
| 49 |
|
| 1.3525190, 1.3856512, 1.4157897, 1.4434309, |
| 50 |
|
| 1.4689574, 1.4926697, 1.5148087, 1.5355703, |
| 51 |
|
| 1.5551159, 1.5735801, 1.5910762, 1.6077008, |
| 52 |
|
| 1.6235366, 1.6386550, 1.6531183, 1.6669808}; |
| 53 |
|
| /* Multiply color by gain value */ |
| 54 |
0 |
| color *= gain_values[gb->camera_registers[GB_CAMERA_GAIN_AND_EDGE_ENHACEMENT_FLAGS] & 0x1F]; |
| 55 |
|
| |
| 56 |
|
| |
| 57 |
|
| /* Color is multiplied by the exposure register to simulate exposure. */ |
| 58 |
0 |
| color = color * ((gb->camera_registers[GB_CAMERA_EXPOSURE_HIGH] << 8) + gb->camera_registers[GB_CAMERA_EXPOSURE_LOW]) / 0x1000; |
| 59 |
|
| |
| 60 |
0 |
| return color; |
| 61 |
|
| } |
| 62 |
|
| |
| 63 |
0 |
| uint8_t GB_camera_read_image(GB_gameboy_t *gb, uint16_t addr) |
| 64 |
|
| { |
| 65 |
0 |
| uint8_t tile_x = addr / 0x10 % 0x10; |
| 66 |
0 |
| uint8_t tile_y = addr / 0x10 / 0x10; |
| 67 |
|
| |
| 68 |
0 |
| uint8_t y = ((addr >> 1) & 0x7) + tile_y * 8; |
| 69 |
0 |
| uint8_t bit = addr & 1; |
| 70 |
|
| |
| 71 |
0 |
| uint8_t ret = 0; |
| 72 |
|
| |
| 73 |
0 |
▶ | for (uint8_t x = tile_x * 8; x < tile_x * 8 + 8; x++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (uint8_t x = tile_x * 8; x < tile_x * 8 + 8; x++) { |
0 |
0 |
0 |
|
| 74 |
|
| |
| 75 |
0 |
| long color = get_processed_color(gb, x, y); |
| 76 |
|
| |
| 77 |
|
| static const double edge_enhancement_ratios[] = {0.5, 0.75, 1, 1.25, 2, 3, 4, 5}; |
| 78 |
0 |
| double edge_enhancement_ratio = edge_enhancement_ratios[(gb->camera_registers[GB_CAMERA_EDGE_ENHANCEMENT_INVERT_AND_VOLTAGE] >> 4) & 0x7]; |
| 79 |
0 |
▶ | if ((gb->camera_registers[GB_CAMERA_GAIN_AND_EDGE_ENHACEMENT_FLAGS] & 0xE0) == 0xE0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((gb->camera_registers[GB_CAMERA_GAIN_AND_EDGE_ENHACEMENT_FLAGS] & 0xE0) == 0xE0) { |
0 |
0 |
0 |
|
| 80 |
0 |
| color += (color * 4) * edge_enhancement_ratio; |
| 81 |
0 |
| color -= get_processed_color(gb, x - 1, y) * edge_enhancement_ratio; |
| 82 |
0 |
| color -= get_processed_color(gb, x + 1, y) * edge_enhancement_ratio; |
| 83 |
0 |
| color -= get_processed_color(gb, x, y - 1) * edge_enhancement_ratio; |
| 84 |
0 |
| color -= get_processed_color(gb, x, y + 1) * edge_enhancement_ratio; |
| 85 |
|
| } |
| 86 |
|
| |
| 87 |
|
| |
| 88 |
|
| /* The camera's registers are used as a threshold pattern, which defines the dithering */ |
| 89 |
0 |
| uint8_t pattern_base = ((x & 3) + (y & 3) * 4) * 3 + GB_CAMERA_DITHERING_PATTERN_START; |
| 90 |
|
| |
| 91 |
0 |
▶ | if (color < gb->camera_registers[pattern_base]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (color < gb->camera_registers[pattern_base]) { |
0 |
0 |
0 |
|
| 92 |
0 |
| color = 3; |
| 93 |
|
| } |
| 94 |
0 |
▶ | else if (color < gb->camera_registers[pattern_base + 1]) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (color < gb->camera_registers[pattern_base + 1]) { |
0 |
0 |
0 |
|
| 95 |
0 |
| color = 2; |
| 96 |
|
| } |
| 97 |
0 |
▶ | else if (color < gb->camera_registers[pattern_base + 2]) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (color < gb->camera_registers[pattern_base + 2]) { |
0 |
0 |
0 |
|
| 98 |
0 |
| color = 1; |
| 99 |
|
| } |
| 100 |
|
| else { |
| 101 |
0 |
| color = 0; |
| 102 |
|
| } |
| 103 |
|
| |
| 104 |
0 |
| ret <<= 1; |
| 105 |
0 |
| ret |= (color >> bit) & 1; |
| 106 |
|
| } |
| 107 |
|
| |
| 108 |
0 |
| return ret; |
| 109 |
|
| } |
| 110 |
|
| |
| 111 |
0 |
| void GB_set_camera_get_pixel_callback(GB_gameboy_t *gb, GB_camera_get_pixel_callback_t callback) |
| 112 |
|
| { |
| 113 |
|
| if (!callback) { |
| 114 |
|
| GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb) |
| 115 |
|
| } |
| 116 |
0 |
| gb->camera_get_pixel_callback = callback; |
| 117 |
0 |
| } |
| 118 |
|
| |
| 119 |
0 |
| void GB_set_camera_update_request_callback(GB_gameboy_t *gb, GB_camera_update_request_callback_t callback) |
| 120 |
|
| { |
| 121 |
|
| if (!callback) { |
| 122 |
|
| GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb) |
| 123 |
|
| } |
| 124 |
0 |
▶ | if (gb->camera_countdown > 0 && callback) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (gb->camera_countdown > 0 && callback) { |
0 |
0 |
0 |
| if (gb->camera_countdown > 0 && callback) { |
0 |
0 |
0 |
|
| 125 |
0 |
| GB_log(gb, "Camera update request callback set while camera was proccessing, clearing camera countdown.\n"); |
| 126 |
0 |
| gb->camera_countdown = 0; |
| 127 |
0 |
| GB_camera_updated(gb); |
| 128 |
|
| } |
| 129 |
|
| |
| 130 |
0 |
| gb->camera_update_request_callback = callback; |
| 131 |
0 |
| } |
| 132 |
|
| |
| 133 |
0 |
| void GB_camera_updated(GB_gameboy_t *gb) |
| 134 |
|
| { |
| 135 |
0 |
| gb->camera_registers[GB_CAMERA_SHOOT_AND_1D_FLAGS] &= ~1; |
| 136 |
0 |
| } |
| 137 |
|
| |
| 138 |
0 |
| void GB_camera_write_register(GB_gameboy_t *gb, uint16_t addr, uint8_t value) |
| 139 |
|
| { |
| 140 |
0 |
| addr &= 0x7F; |
| 141 |
0 |
▶ | if (addr == GB_CAMERA_SHOOT_AND_1D_FLAGS) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (addr == GB_CAMERA_SHOOT_AND_1D_FLAGS) { |
0 |
0 |
0 |
|
| 142 |
0 |
| value &= 0x7; |
| 143 |
0 |
| noise_seed = GB_random(); |
| 144 |
0 |
▶ | if ((value & 1) && !(gb->camera_registers[GB_CAMERA_SHOOT_AND_1D_FLAGS] & 1)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((value & 1) && !(gb->camera_registers[GB_CAMERA_SHOOT_AND_1D_FLAGS] & 1)) { |
0 |
0 |
0 |
| if ((value & 1) && !(gb->camera_registers[GB_CAMERA_SHOOT_AND_1D_FLAGS] & 1)) { |
0 |
0 |
0 |
|
| 145 |
0 |
▶ | if (gb->camera_update_request_callback) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->camera_update_request_callback) { |
0 |
0 |
0 |
|
| 146 |
0 |
| gb->camera_update_request_callback(gb); |
| 147 |
|
| } |
| 148 |
|
| else { |
| 149 |
|
| /* If no callback is set, wait the amount of time the real camera would take before clearing the busy bit */ |
| 150 |
0 |
| uint16_t exposure = (gb->camera_registers[GB_CAMERA_EXPOSURE_HIGH] << 8) | gb->camera_registers[GB_CAMERA_EXPOSURE_LOW]; |
| 151 |
0 |
▶ | gb->camera_countdown = 129792 + ((gb->camera_registers[GB_CAMERA_GAIN_AND_EDGE_ENHACEMENT_FLAGS] & 0x80)? 0 : 2048) + (exposure * 64) + (gb->camera_alignment & 4); |
Branch: 1 branch
| Branch | True | False | Total |
| gb->camera_countdown = 129792 + ((gb->camera_registers[GB_CAMERA_GAIN_AND_EDGE_ENHACEMENT_FLAGS] & 0x80)? 0 : 2048) + (exposure * 64) + (gb->camera_alignment & 4); |
0 |
0 |
0 |
|
| 152 |
|
| } |
| 153 |
|
| } |
| 154 |
|
| |
| 155 |
0 |
▶ | if (!(value & 1) && (gb->camera_registers[GB_CAMERA_SHOOT_AND_1D_FLAGS] & 1)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(value & 1) && (gb->camera_registers[GB_CAMERA_SHOOT_AND_1D_FLAGS] & 1)) { |
0 |
0 |
0 |
| if (!(value & 1) && (gb->camera_registers[GB_CAMERA_SHOOT_AND_1D_FLAGS] & 1)) { |
0 |
0 |
0 |
|
| 156 |
|
| /* We don't support cancelling a camera shoot */ |
| 157 |
0 |
| GB_log(gb, "ROM attempted to cancel camera shoot, which is currently not supported. The camera shoot will not be cancelled.\n"); |
| 158 |
0 |
| value |= 1; |
| 159 |
|
| } |
| 160 |
|
| |
| 161 |
0 |
| gb->camera_registers[GB_CAMERA_SHOOT_AND_1D_FLAGS] = value; |
| 162 |
|
| } |
| 163 |
|
| else { |
| 164 |
0 |
▶ | if (addr >= 0x36) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (addr >= 0x36) { |
0 |
0 |
0 |
|
| 165 |
0 |
| GB_log(gb, "Wrote invalid camera register %02x: %2x\n", addr, value); |
| 166 |
0 |
| return; |
| 167 |
|
| } |
| 168 |
0 |
| gb->camera_registers[addr] = value; |
| 169 |
|
| } |
| 170 |
|
| } |
| 171 |
0 |
| uint8_t GB_camera_read_register(GB_gameboy_t *gb, uint16_t addr) |
| 172 |
|
| { |
| 173 |
0 |
▶ | if ((addr & 0x7F) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((addr & 0x7F) == 0) { |
0 |
0 |
0 |
|
| 174 |
0 |
| return gb->camera_registers[GB_CAMERA_SHOOT_AND_1D_FLAGS]; |
| 175 |
|
| } |
| 176 |
0 |
| return 0; |
| 177 |
|
| } |