| 1 |
|
| #include "gb.h" |
| 2 |
|
| #include "cheats.h" |
| 3 |
|
| #include <stdio.h> |
| 4 |
|
| #include <assert.h> |
| 5 |
|
| #include <errno.h> |
| 6 |
|
| #include <string.h> |
| 7 |
|
| #include <stdlib.h> |
| 8 |
|
| |
| 9 |
4 |
| static inline uint8_t hash_addr(uint16_t addr) |
| 10 |
|
| { |
| 11 |
4 |
| return addr; |
| 12 |
|
| } |
| 13 |
|
| |
| 14 |
0 |
| static uint16_t bank_for_addr(GB_gameboy_t *gb, uint16_t addr) |
| 15 |
|
| { |
| 16 |
0 |
▶ | if (addr < 0x4000) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (addr < 0x4000) { |
0 |
0 |
0 |
|
| 17 |
0 |
| return gb->mbc_rom0_bank; |
| 18 |
|
| } |
| 19 |
|
| |
| 20 |
0 |
▶ | if (addr < 0x8000) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (addr < 0x8000) { |
0 |
0 |
0 |
|
| 21 |
0 |
| return gb->mbc_rom_bank; |
| 22 |
|
| } |
| 23 |
|
| |
| 24 |
0 |
▶ | if (addr < 0xD000) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (addr < 0xD000) { |
0 |
0 |
0 |
|
| 25 |
0 |
| return 0; |
| 26 |
|
| } |
| 27 |
|
| |
| 28 |
0 |
▶ | if (addr < 0xE000) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (addr < 0xE000) { |
0 |
0 |
0 |
|
| 29 |
0 |
| return gb->cgb_ram_bank; |
| 30 |
|
| } |
| 31 |
|
| |
| 32 |
0 |
| return 0; |
| 33 |
|
| } |
| 34 |
|
| |
| 35 |
0 |
| static noinline void apply_cheat(GB_gameboy_t *gb, uint16_t address, uint8_t *value) |
| 36 |
|
| { |
| 37 |
0 |
▶ | if (unlikely(!gb->boot_rom_finished)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (unlikely(!gb->boot_rom_finished)) return; |
0 |
0 |
0 |
|
| 38 |
0 |
| const GB_cheat_hash_t *hash = gb->cheat_hash[hash_addr(address)]; |
| 39 |
0 |
▶ | if (likely(!hash)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (likely(!hash)) return; |
0 |
0 |
0 |
|
| 40 |
|
| |
| 41 |
0 |
▶ | for (unsigned i = 0; i < hash->size; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned i = 0; i < hash->size; i++) { |
0 |
0 |
0 |
|
| 42 |
0 |
| GB_cheat_t *cheat = hash->cheats[i]; |
| 43 |
0 |
▶ | if (cheat->address == address && cheat->enabled && (!cheat->use_old_value || cheat->old_value == *value)) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (cheat->address == address && cheat->enabled && (!cheat->use_old_value || cheat->old_value == *value)) { |
0 |
0 |
0 |
| if (cheat->address == address && cheat->enabled && (!cheat->use_old_value || cheat->old_value == *value)) { |
0 |
0 |
0 |
| if (cheat->address == address && cheat->enabled && (!cheat->use_old_value || cheat->old_value == *value)) { |
0 |
0 |
0 |
| if (cheat->address == address && cheat->enabled && (!cheat->use_old_value || cheat->old_value == *value)) { |
0 |
0 |
0 |
|
| 44 |
0 |
▶ | if (cheat->bank == GB_CHEAT_ANY_BANK || cheat->bank == bank_for_addr(gb, address)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (cheat->bank == GB_CHEAT_ANY_BANK || cheat->bank == bank_for_addr(gb, address)) { |
0 |
0 |
0 |
| if (cheat->bank == GB_CHEAT_ANY_BANK || cheat->bank == bank_for_addr(gb, address)) { |
0 |
0 |
0 |
|
| 45 |
0 |
| *value = cheat->value; |
| 46 |
0 |
| break; |
| 47 |
|
| } |
| 48 |
|
| } |
| 49 |
|
| } |
| 50 |
|
| } |
| 51 |
|
| |
| 52 |
4104915 |
| void GB_apply_cheat(GB_gameboy_t *gb, uint16_t address, uint8_t *value) |
| 53 |
|
| { |
| 54 |
4104915 |
▶ | if (likely(!gb->cheat_enabled)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (likely(!gb->cheat_enabled)) return; |
4104915 |
0 |
4104915 |
|
| 55 |
0 |
▶ | if (likely(gb->cheat_count == 0)) return; // Optimization |
Branch: 1 branch
| Branch | True | False | Total |
| if (likely(gb->cheat_count == 0)) return; // Optimization |
0 |
0 |
0 |
|
| 56 |
0 |
| apply_cheat(gb, address, value); |
| 57 |
|
| } |
| 58 |
|
| |
| 59 |
2 |
| bool GB_cheats_enabled(GB_gameboy_t *gb) |
| 60 |
|
| { |
| 61 |
2 |
| return gb->cheat_enabled; |
| 62 |
|
| } |
| 63 |
|
| |
| 64 |
1 |
| void GB_set_cheats_enabled(GB_gameboy_t *gb, bool enabled) |
| 65 |
|
| { |
| 66 |
1 |
| gb->cheat_enabled = enabled; |
| 67 |
1 |
| } |
| 68 |
|
| |
| 69 |
2 |
| const GB_cheat_t *GB_add_cheat(GB_gameboy_t *gb, const char *description, uint16_t address, uint16_t bank, uint8_t value, uint8_t old_value, bool use_old_value, bool enabled) |
| 70 |
|
| { |
| 71 |
|
| GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb) |
| 72 |
|
| |
| 73 |
2 |
| GB_cheat_t *cheat = malloc(sizeof(*cheat)); |
| 74 |
2 |
| cheat->address = address; |
| 75 |
2 |
| cheat->bank = bank; |
| 76 |
2 |
| cheat->value = value; |
| 77 |
2 |
| cheat->old_value = old_value; |
| 78 |
2 |
| cheat->use_old_value = use_old_value; |
| 79 |
2 |
| cheat->enabled = enabled; |
| 80 |
2 |
| strncpy(cheat->description, description, sizeof(cheat->description)); |
| 81 |
2 |
| cheat->description[sizeof(cheat->description) - 1] = 0; |
| 82 |
2 |
| gb->cheats = realloc(gb->cheats, (++gb->cheat_count) * sizeof(gb->cheats[0])); |
| 83 |
2 |
| gb->cheats[gb->cheat_count - 1] = cheat; |
| 84 |
|
| |
| 85 |
2 |
| GB_cheat_hash_t **hash = &gb->cheat_hash[hash_addr(address)]; |
| 86 |
2 |
▶ | if (!*hash) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!*hash) { |
2 |
0 |
2 |
|
| 87 |
2 |
| *hash = malloc(sizeof(GB_cheat_hash_t) + sizeof(cheat)); |
| 88 |
2 |
| (*hash)->size = 1; |
| 89 |
2 |
| (*hash)->cheats[0] = cheat; |
| 90 |
|
| } |
| 91 |
|
| else { |
| 92 |
0 |
| (*hash)->size++; |
| 93 |
0 |
| *hash = realloc(*hash, sizeof(GB_cheat_hash_t) + sizeof(cheat) * (*hash)->size); |
| 94 |
0 |
| (*hash)->cheats[(*hash)->size - 1] = cheat; |
| 95 |
|
| } |
| 96 |
|
| |
| 97 |
2 |
| return cheat; |
| 98 |
|
| } |
| 99 |
|
| |
| 100 |
0 |
| const GB_cheat_t *const *GB_get_cheats(GB_gameboy_t *gb, size_t *size) |
| 101 |
|
| { |
| 102 |
0 |
▶ | if (size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (size) { |
0 |
0 |
0 |
|
| 103 |
0 |
| *size = gb->cheat_count; |
| 104 |
|
| } |
| 105 |
0 |
| return (void *)gb->cheats; |
| 106 |
|
| } |
| 107 |
|
| |
| 108 |
2 |
| void GB_remove_cheat(GB_gameboy_t *gb, const GB_cheat_t *cheat) |
| 109 |
|
| { |
| 110 |
|
| GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb) |
| 111 |
|
| |
| 112 |
2 |
▶ | for (unsigned i = 0; i < gb->cheat_count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned i = 0; i < gb->cheat_count; i++) { |
2 |
0 |
2 |
|
| 113 |
2 |
▶ | if (gb->cheats[i] == cheat) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->cheats[i] == cheat) { |
2 |
0 |
2 |
|
| 114 |
2 |
| gb->cheats[i] = gb->cheats[--gb->cheat_count]; |
| 115 |
2 |
▶ | if (gb->cheat_count == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->cheat_count == 0) { |
1 |
1 |
2 |
|
| 116 |
1 |
| free(gb->cheats); |
| 117 |
1 |
| gb->cheats = NULL; |
| 118 |
|
| } |
| 119 |
|
| else { |
| 120 |
1 |
| gb->cheats = realloc(gb->cheats, gb->cheat_count * sizeof(gb->cheats[0])); |
| 121 |
|
| } |
| 122 |
2 |
| break; |
| 123 |
|
| } |
| 124 |
|
| } |
| 125 |
|
| |
| 126 |
2 |
| GB_cheat_hash_t **hash = &gb->cheat_hash[hash_addr(cheat->address)]; |
| 127 |
2 |
▶ | for (unsigned i = 0; i < (*hash)->size; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned i = 0; i < (*hash)->size; i++) { |
2 |
0 |
2 |
|
| 128 |
2 |
▶ | if ((*hash)->cheats[i] == cheat) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((*hash)->cheats[i] == cheat) { |
2 |
0 |
2 |
|
| 129 |
2 |
| (*hash)->cheats[i] = (*hash)->cheats[--(*hash)->size]; |
| 130 |
2 |
▶ | if ((*hash)->size == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((*hash)->size == 0) { |
2 |
0 |
2 |
|
| 131 |
2 |
| free(*hash); |
| 132 |
2 |
| *hash = NULL; |
| 133 |
|
| } |
| 134 |
|
| else { |
| 135 |
0 |
| *hash = realloc(*hash, sizeof(GB_cheat_hash_t) + sizeof(cheat) * (*hash)->size); |
| 136 |
|
| } |
| 137 |
2 |
| break; |
| 138 |
|
| } |
| 139 |
|
| } |
| 140 |
|
| |
| 141 |
2 |
| free((void *)cheat); |
| 142 |
2 |
| } |
| 143 |
|
| |
| 144 |
17 |
| void GB_remove_all_cheats(GB_gameboy_t *gb) |
| 145 |
|
| { |
| 146 |
19 |
▶ | while (gb->cheat_count) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (gb->cheat_count) { |
2 |
17 |
19 |
|
| 147 |
2 |
| GB_remove_cheat(gb, gb->cheats[0]); |
| 148 |
|
| } |
| 149 |
17 |
| } |
| 150 |
|
| |
| 151 |
2 |
| const GB_cheat_t *GB_import_cheat(GB_gameboy_t *gb, const char *cheat, const char *description, bool enabled) |
| 152 |
|
| { |
| 153 |
|
| GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb) |
| 154 |
|
| |
| 155 |
|
| uint8_t dummy; |
| 156 |
|
| /* GameShark */ |
| 157 |
2 |
▶ | if (strlen(cheat) == 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strlen(cheat) == 8) { |
1 |
1 |
2 |
|
| 158 |
|
| #ifdef _WIN32 |
| 159 |
|
| // The hh modifier is not supported on old MSVCRT, it's completely ignored |
| 160 |
|
| uint32_t bank = 0; |
| 161 |
|
| uint32_t value = 0; |
| 162 |
|
| #pragma GCC diagnostic ignored "-Wformat" |
| 163 |
|
| #else |
| 164 |
|
| uint8_t bank; |
| 165 |
|
| uint8_t value; |
| 166 |
|
| #endif |
| 167 |
|
| uint16_t address; |
| 168 |
1 |
▶ | if (sscanf(cheat, "%02hhx%02hhx%04hx%c", &bank, &value, &address, &dummy) == 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sscanf(cheat, "%02hhx%02hhx%04hx%c", &bank, &value, &address, &dummy) == 3) { |
1 |
0 |
1 |
|
| 169 |
1 |
| address = __builtin_bswap16(address); |
| 170 |
1 |
▶ | return GB_add_cheat(gb, description, address, bank == 1? GB_CHEAT_ANY_BANK : (bank & 0xF), value, 0, false, enabled); |
Branch: 1 branch
| Branch | True | False | Total |
| return GB_add_cheat(gb, description, address, bank == 1? GB_CHEAT_ANY_BANK : (bank & 0xF), value, 0, false, enabled); |
0 |
1 |
1 |
|
| 171 |
|
| } |
| 172 |
|
| } |
| 173 |
|
| |
| 174 |
|
| /* Game Genie */ |
| 175 |
|
| { |
| 176 |
1 |
| char stripped_cheat[10] = {0,}; |
| 177 |
10 |
▶ | for (unsigned i = 0; i < 9 && *cheat; i++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (unsigned i = 0; i < 9 && *cheat; i++) { |
9 |
1 |
10 |
| for (unsigned i = 0; i < 9 && *cheat; i++) { |
9 |
0 |
9 |
|
| 178 |
9 |
| stripped_cheat[i] = *(cheat++); |
| 179 |
11 |
▶ | while (*cheat == '-') { |
Branch: 1 branch
| Branch | True | False | Total |
| while (*cheat == '-') { |
2 |
9 |
11 |
|
| 180 |
2 |
| cheat++; |
| 181 |
|
| } |
| 182 |
|
| } |
| 183 |
|
| |
| 184 |
|
| // Delete the 7th character; |
| 185 |
1 |
| stripped_cheat[7] = stripped_cheat[8]; |
| 186 |
1 |
| stripped_cheat[8] = 0; |
| 187 |
|
| |
| 188 |
|
| #ifdef _WIN32 |
| 189 |
|
| uint32_t old_value = 0; |
| 190 |
|
| uint32_t value = 0; |
| 191 |
|
| #else |
| 192 |
|
| uint8_t old_value; |
| 193 |
|
| uint8_t value; |
| 194 |
|
| #endif |
| 195 |
|
| uint16_t address; |
| 196 |
1 |
▶ | if (strlen(stripped_cheat) != 8 && strlen(stripped_cheat) != 6) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (strlen(stripped_cheat) != 8 && strlen(stripped_cheat) != 6) { |
0 |
1 |
1 |
| if (strlen(stripped_cheat) != 8 && strlen(stripped_cheat) != 6) { |
0 |
0 |
0 |
|
| 197 |
1 |
| return NULL; |
| 198 |
|
| } |
| 199 |
1 |
▶ | if (sscanf(stripped_cheat, "%02hhx%04hx%02hhx%c", &value, &address, &old_value, &dummy) == 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sscanf(stripped_cheat, "%02hhx%04hx%02hhx%c", &value, &address, &old_value, &dummy) == 3) { |
1 |
0 |
1 |
|
| 200 |
1 |
| address = (uint16_t)(address >> 4) | (uint16_t)(address << 12); |
| 201 |
1 |
| address ^= 0xF000; |
| 202 |
1 |
▶ | if (address > 0x7FFF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (address > 0x7FFF) { |
0 |
1 |
1 |
|
| 203 |
0 |
| return false; |
| 204 |
|
| } |
| 205 |
1 |
| old_value = (uint8_t)(old_value >> 2) | (uint8_t)(old_value << 6); |
| 206 |
1 |
| old_value ^= 0xBA; |
| 207 |
1 |
| return GB_add_cheat(gb, description, address, GB_CHEAT_ANY_BANK, value, old_value, true, enabled); |
| 208 |
|
| } |
| 209 |
|
| |
| 210 |
0 |
▶ | if (sscanf(stripped_cheat, "%02hhx%04hx%c", &value, &address, &dummy) == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sscanf(stripped_cheat, "%02hhx%04hx%c", &value, &address, &dummy) == 2) { |
0 |
0 |
0 |
|
| 211 |
0 |
| address = (uint16_t)(address >> 4) | (uint16_t)(address << 12); |
| 212 |
0 |
| address ^= 0xF000; |
| 213 |
0 |
▶ | if (address > 0x7FFF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (address > 0x7FFF) { |
0 |
0 |
0 |
|
| 214 |
0 |
| return false; |
| 215 |
|
| } |
| 216 |
0 |
| return GB_add_cheat(gb, description, address, GB_CHEAT_ANY_BANK, value, false, true, enabled); |
| 217 |
|
| } |
| 218 |
|
| } |
| 219 |
0 |
| return NULL; |
| 220 |
|
| } |
| 221 |
|
| |
| 222 |
0 |
| void GB_update_cheat(GB_gameboy_t *gb, const GB_cheat_t *_cheat, const char *description, uint16_t address, uint16_t bank, uint8_t value, uint8_t old_value, bool use_old_value, bool enabled) |
| 223 |
|
| { |
| 224 |
|
| GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb) |
| 225 |
|
| |
| 226 |
0 |
| GB_cheat_t *cheat = NULL; |
| 227 |
0 |
▶ | for (unsigned i = 0; i < gb->cheat_count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned i = 0; i < gb->cheat_count; i++) { |
0 |
0 |
0 |
|
| 228 |
0 |
▶ | if (gb->cheats[i] == _cheat) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->cheats[i] == _cheat) { |
0 |
0 |
0 |
|
| 229 |
0 |
| cheat = gb->cheats[i]; |
| 230 |
0 |
| break; |
| 231 |
|
| } |
| 232 |
|
| } |
| 233 |
|
| |
| 234 |
|
| assert(cheat); |
| 235 |
0 |
▶ | if (!cheat) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cheat) return; |
0 |
0 |
0 |
|
| 236 |
|
| |
| 237 |
0 |
▶ | if (cheat->address != address) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cheat->address != address) { |
0 |
0 |
0 |
|
| 238 |
|
| /* Remove from old bucket */ |
| 239 |
0 |
| GB_cheat_hash_t **hash = &gb->cheat_hash[hash_addr(cheat->address)]; |
| 240 |
0 |
▶ | for (unsigned i = 0; i < (*hash)->size; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned i = 0; i < (*hash)->size; i++) { |
0 |
0 |
0 |
|
| 241 |
0 |
▶ | if ((*hash)->cheats[i] == cheat) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((*hash)->cheats[i] == cheat) { |
0 |
0 |
0 |
|
| 242 |
0 |
| (*hash)->cheats[i] = (*hash)->cheats[--(*hash)->size]; |
| 243 |
0 |
▶ | if ((*hash)->size == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((*hash)->size == 0) { |
0 |
0 |
0 |
|
| 244 |
0 |
| free(*hash); |
| 245 |
0 |
| *hash = NULL; |
| 246 |
|
| } |
| 247 |
|
| else { |
| 248 |
0 |
| *hash = realloc(*hash, sizeof(GB_cheat_hash_t) + sizeof(cheat) * (*hash)->size); |
| 249 |
|
| } |
| 250 |
0 |
| break; |
| 251 |
|
| } |
| 252 |
|
| } |
| 253 |
0 |
| cheat->address = address; |
| 254 |
|
| |
| 255 |
|
| /* Add to new bucket */ |
| 256 |
0 |
| hash = &gb->cheat_hash[hash_addr(address)]; |
| 257 |
0 |
▶ | if (!*hash) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!*hash) { |
0 |
0 |
0 |
|
| 258 |
0 |
| *hash = malloc(sizeof(GB_cheat_hash_t) + sizeof(cheat)); |
| 259 |
0 |
| (*hash)->size = 1; |
| 260 |
0 |
| (*hash)->cheats[0] = cheat; |
| 261 |
|
| } |
| 262 |
|
| else { |
| 263 |
0 |
| (*hash)->size++; |
| 264 |
0 |
| *hash = realloc(*hash, sizeof(GB_cheat_hash_t) + sizeof(cheat) * (*hash)->size); |
| 265 |
0 |
| (*hash)->cheats[(*hash)->size - 1] = cheat; |
| 266 |
|
| } |
| 267 |
|
| } |
| 268 |
0 |
| cheat->bank = bank; |
| 269 |
0 |
| cheat->value = value; |
| 270 |
0 |
| cheat->old_value = old_value; |
| 271 |
0 |
| cheat->use_old_value = use_old_value; |
| 272 |
0 |
| cheat->enabled = enabled; |
| 273 |
0 |
▶ | if (description != cheat->description) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (description != cheat->description) { |
0 |
0 |
0 |
|
| 274 |
0 |
| strncpy(cheat->description, description, sizeof(cheat->description)); |
| 275 |
0 |
| cheat->description[sizeof(cheat->description) - 1] = 0; |
| 276 |
|
| } |
| 277 |
|
| } |
| 278 |
|
| |
| 279 |
|
| #define CHEAT_MAGIC 'SBCh' |
| 280 |
|
| |
| 281 |
0 |
| int GB_load_cheats(GB_gameboy_t *gb, const char *path, bool replace_existing) |
| 282 |
|
| { |
| 283 |
|
| GB_ASSERT_NOT_RUNNING_OTHER_THREAD(gb) |
| 284 |
|
| |
| 285 |
0 |
| FILE *f = fopen(path, "rb"); |
| 286 |
0 |
▶ | if (!f) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!f) { |
0 |
0 |
0 |
|
| 287 |
0 |
| return errno; |
| 288 |
|
| } |
| 289 |
|
| |
| 290 |
0 |
| uint32_t magic = 0; |
| 291 |
0 |
| uint32_t struct_size = 0; |
| 292 |
0 |
▶ | if (fread(&magic, sizeof(magic), 1, f) != 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fread(&magic, sizeof(magic), 1, f) != 1) { |
0 |
0 |
0 |
|
| 293 |
0 |
| goto error; |
| 294 |
|
| } |
| 295 |
0 |
▶ | if (fread(&struct_size, sizeof(struct_size), 1, f) != 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fread(&struct_size, sizeof(struct_size), 1, f) != 1) { |
0 |
0 |
0 |
|
| 296 |
0 |
| goto error; |
| 297 |
|
| } |
| 298 |
0 |
▶ | if (magic != LE32(CHEAT_MAGIC) && magic != BE32(CHEAT_MAGIC)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (magic != LE32(CHEAT_MAGIC) && magic != BE32(CHEAT_MAGIC)) { |
0 |
0 |
0 |
| if (magic != LE32(CHEAT_MAGIC) && magic != BE32(CHEAT_MAGIC)) { |
0 |
0 |
0 |
|
| 299 |
0 |
| GB_log(gb, "The file is not a SameBoy cheat database"); |
| 300 |
0 |
| goto error; |
| 301 |
|
| } |
| 302 |
|
| |
| 303 |
0 |
▶ | if (struct_size != sizeof(GB_cheat_t)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (struct_size != sizeof(GB_cheat_t)) { |
0 |
0 |
0 |
|
| 304 |
0 |
| GB_log(gb, "This cheat database is not compatible with this version of SameBoy"); |
| 305 |
0 |
| goto error; |
| 306 |
|
| } |
| 307 |
|
| |
| 308 |
|
| // Remove all cheats first |
| 309 |
0 |
▶ | if (replace_existing) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (replace_existing) { |
0 |
0 |
0 |
|
| 310 |
0 |
| GB_remove_all_cheats(gb); |
| 311 |
|
| } |
| 312 |
|
| |
| 313 |
|
| GB_cheat_t cheat; |
| 314 |
0 |
▶ | while (fread(&cheat, sizeof(cheat), 1, f) == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (fread(&cheat, sizeof(cheat), 1, f) == 1) { |
0 |
0 |
0 |
|
| 315 |
0 |
▶ | if (magic != CHEAT_MAGIC) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (magic != CHEAT_MAGIC) { |
0 |
0 |
0 |
|
| 316 |
0 |
| cheat.address = __builtin_bswap16(cheat.address); |
| 317 |
0 |
| cheat.bank = __builtin_bswap16(cheat.bank); |
| 318 |
|
| } |
| 319 |
0 |
| cheat.description[sizeof(cheat.description) - 1] = 0; |
| 320 |
0 |
| GB_add_cheat(gb, cheat.description, cheat.address, cheat.bank, cheat.value, cheat.old_value, cheat.use_old_value, cheat.enabled); |
| 321 |
|
| } |
| 322 |
|
| |
| 323 |
0 |
| fclose(f); |
| 324 |
0 |
| return 0; |
| 325 |
|
| |
| 326 |
0 |
| error: |
| 327 |
0 |
| fclose(f); |
| 328 |
0 |
| return -1; |
| 329 |
|
| } |
| 330 |
|
| |
| 331 |
0 |
| int GB_save_cheats(GB_gameboy_t *gb, const char *path) |
| 332 |
|
| { |
| 333 |
0 |
▶ | if (!gb->cheat_count) return 0; // Nothing to save. |
Branch: 1 branch
| Branch | True | False | Total |
| if (!gb->cheat_count) return 0; // Nothing to save. |
0 |
0 |
0 |
|
| 334 |
0 |
| FILE *f = fopen(path, "wb"); |
| 335 |
0 |
▶ | if (!f) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!f) { |
0 |
0 |
0 |
|
| 336 |
0 |
| GB_log(gb, "Could not dump cheat database: %s.\n", strerror(errno)); |
| 337 |
0 |
| return errno; |
| 338 |
|
| } |
| 339 |
|
| |
| 340 |
0 |
| uint32_t magic = CHEAT_MAGIC; |
| 341 |
0 |
| uint32_t struct_size = sizeof(GB_cheat_t); |
| 342 |
|
| |
| 343 |
0 |
▶ | if (fwrite(&magic, sizeof(magic), 1, f) != 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fwrite(&magic, sizeof(magic), 1, f) != 1) { |
0 |
0 |
0 |
|
| 344 |
0 |
| fclose(f); |
| 345 |
0 |
| return EIO; |
| 346 |
|
| } |
| 347 |
|
| |
| 348 |
0 |
▶ | if (fwrite(&struct_size, sizeof(struct_size), 1, f) != 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fwrite(&struct_size, sizeof(struct_size), 1, f) != 1) { |
0 |
0 |
0 |
|
| 349 |
0 |
| fclose(f); |
| 350 |
0 |
| return EIO; |
| 351 |
|
| } |
| 352 |
|
| |
| 353 |
0 |
▶ | for (size_t i = 0; i <gb->cheat_count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i <gb->cheat_count; i++) { |
0 |
0 |
0 |
|
| 354 |
0 |
▶ | if (fwrite(gb->cheats[i], sizeof(*gb->cheats[i]), 1, f) != 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fwrite(gb->cheats[i], sizeof(*gb->cheats[i]), 1, f) != 1) { |
0 |
0 |
0 |
|
| 355 |
0 |
| fclose(f); |
| 356 |
0 |
| return EIO; |
| 357 |
|
| } |
| 358 |
|
| } |
| 359 |
|
| |
| 360 |
0 |
| fclose(f); |
| 361 |
0 |
| return 0; |
| 362 |
|
| } |