| 1 |
|
| #include "gb.h" |
| 2 |
|
| #include <stdio.h> |
| 3 |
|
| #include <errno.h> |
| 4 |
|
| #include <assert.h> |
| 5 |
|
| #include <string.h> |
| 6 |
|
| #include <stdlib.h> |
| 7 |
|
| |
| 8 |
|
| #ifdef GB_BIG_ENDIAN |
| 9 |
|
| #define BESS_NAME "SameBoy v" GB_VERSION " (Big Endian)" |
| 10 |
|
| #else |
| 11 |
|
| #define BESS_NAME "SameBoy v" GB_VERSION |
| 12 |
|
| #endif |
| 13 |
|
| |
| 14 |
|
| _Static_assert((GB_SECTION_OFFSET(core_state) & 7) == 0, "Section core_state is not aligned"); |
| 15 |
|
| _Static_assert((GB_SECTION_OFFSET(dma) & 7) == 0, "Section dma is not aligned"); |
| 16 |
|
| _Static_assert((GB_SECTION_OFFSET(mbc) & 7) == 0, "Section mbc is not aligned"); |
| 17 |
|
| _Static_assert((GB_SECTION_OFFSET(hram) & 7) == 0, "Section hram is not aligned"); |
| 18 |
|
| _Static_assert((GB_SECTION_OFFSET(timing) & 7) == 0, "Section timing is not aligned"); |
| 19 |
|
| _Static_assert((GB_SECTION_OFFSET(apu) & 7) == 0, "Section apu is not aligned"); |
| 20 |
|
| _Static_assert((GB_SECTION_OFFSET(rtc) & 7) == 0, "Section rtc is not aligned"); |
| 21 |
|
| _Static_assert((GB_SECTION_OFFSET(video) & 7) == 0, "Section video is not aligned"); |
| 22 |
|
| _Static_assert((GB_SECTION_OFFSET(accessory) & 7) == 0, "Section accessory is not aligned"); |
| 23 |
|
| |
| 24 |
|
| typedef struct __attribute__((packed)) { |
| 25 |
|
| uint32_t magic; |
| 26 |
|
| uint32_t size; |
| 27 |
|
| } BESS_block_t; |
| 28 |
|
| |
| 29 |
|
| typedef struct __attribute__((packed)) { |
| 30 |
|
| uint32_t size; |
| 31 |
|
| uint32_t offset; |
| 32 |
|
| } BESS_buffer_t; |
| 33 |
|
| |
| 34 |
|
| typedef struct __attribute__((packed)) { |
| 35 |
|
| uint32_t start_offset; |
| 36 |
|
| uint32_t magic; |
| 37 |
|
| } BESS_footer_t; |
| 38 |
|
| |
| 39 |
|
| typedef struct __attribute__((packed)) { |
| 40 |
|
| BESS_block_t header; |
| 41 |
|
| uint16_t major; |
| 42 |
|
| uint16_t minor; |
| 43 |
|
| union { |
| 44 |
|
| struct { |
| 45 |
|
| char family; |
| 46 |
|
| char model; |
| 47 |
|
| char revision; |
| 48 |
|
| char padding; |
| 49 |
|
| }; |
| 50 |
|
| uint32_t full_model; |
| 51 |
|
| }; |
| 52 |
|
| |
| 53 |
|
| uint16_t pc; |
| 54 |
|
| uint16_t af; |
| 55 |
|
| uint16_t bc; |
| 56 |
|
| uint16_t de; |
| 57 |
|
| uint16_t hl; |
| 58 |
|
| uint16_t sp; |
| 59 |
|
| |
| 60 |
|
| uint8_t ime; |
| 61 |
|
| uint8_t ie; |
| 62 |
|
| uint8_t execution_mode; // 0 = running; 1 = halted; 2 = stopped |
| 63 |
|
| uint8_t _padding; |
| 64 |
|
| |
| 65 |
|
| uint8_t io_registers[0x80]; |
| 66 |
|
| |
| 67 |
|
| BESS_buffer_t ram; |
| 68 |
|
| BESS_buffer_t vram; |
| 69 |
|
| BESS_buffer_t mbc_ram; |
| 70 |
|
| BESS_buffer_t oam; |
| 71 |
|
| BESS_buffer_t hram; |
| 72 |
|
| BESS_buffer_t background_palettes; |
| 73 |
|
| BESS_buffer_t object_palettes; |
| 74 |
|
| } BESS_CORE_t; |
| 75 |
|
| |
| 76 |
|
| typedef struct __attribute__((packed)) { |
| 77 |
|
| BESS_block_t header; |
| 78 |
|
| uint8_t extra_oam[96]; |
| 79 |
|
| } BESS_XOAM_t; |
| 80 |
|
| |
| 81 |
|
| typedef struct __attribute__((packed)) { |
| 82 |
|
| BESS_block_t header; |
| 83 |
|
| BESS_buffer_t border_tiles; |
| 84 |
|
| BESS_buffer_t border_tilemap; |
| 85 |
|
| BESS_buffer_t border_palettes; |
| 86 |
|
| |
| 87 |
|
| BESS_buffer_t active_palettes; |
| 88 |
|
| BESS_buffer_t ram_palettes; |
| 89 |
|
| BESS_buffer_t attribute_map; |
| 90 |
|
| BESS_buffer_t attribute_files; |
| 91 |
|
| |
| 92 |
|
| uint8_t multiplayer_state; |
| 93 |
|
| } BESS_SGB_t; |
| 94 |
|
| |
| 95 |
|
| typedef struct __attribute__((packed)){ |
| 96 |
|
| BESS_block_t header; |
| 97 |
|
| char title[0x10]; |
| 98 |
|
| uint8_t checksum[2]; |
| 99 |
|
| } BESS_INFO_t; |
| 100 |
|
| |
| 101 |
|
| /* Same RTC format as used by VBA, BGB and SameBoy in battery saves*/ |
| 102 |
|
| typedef struct __attribute__((packed)){ |
| 103 |
|
| BESS_block_t header; |
| 104 |
|
| struct { |
| 105 |
|
| uint8_t seconds; |
| 106 |
|
| uint8_t padding1[3]; |
| 107 |
|
| uint8_t minutes; |
| 108 |
|
| uint8_t padding2[3]; |
| 109 |
|
| uint8_t hours; |
| 110 |
|
| uint8_t padding3[3]; |
| 111 |
|
| uint8_t days; |
| 112 |
|
| uint8_t padding4[3]; |
| 113 |
|
| uint8_t high; |
| 114 |
|
| uint8_t padding5[3]; |
| 115 |
|
| } real, latched; |
| 116 |
|
| uint64_t last_rtc_second; |
| 117 |
|
| } BESS_RTC_t; |
| 118 |
|
| |
| 119 |
|
| /* Same HuC3 RTC format as used by SameBoy and BGB in battery saves */ |
| 120 |
|
| typedef struct __attribute__((packed)){ |
| 121 |
|
| BESS_block_t header; |
| 122 |
|
| GB_huc3_rtc_time_t data; |
| 123 |
|
| } BESS_HUC3_t; |
| 124 |
|
| |
| 125 |
|
| typedef struct __attribute__((packed)) { |
| 126 |
|
| BESS_block_t header; |
| 127 |
|
| |
| 128 |
|
| // Flags |
| 129 |
|
| bool latch_ready:1; |
| 130 |
|
| bool eeprom_do:1; |
| 131 |
|
| bool eeprom_di:1; |
| 132 |
|
| bool eeprom_clk:1; |
| 133 |
|
| bool eeprom_cs:1; |
| 134 |
|
| bool eeprom_write_enabled:1; |
| 135 |
|
| uint8_t padding:2; |
| 136 |
|
| |
| 137 |
|
| uint8_t argument_bits_left; |
| 138 |
|
| |
| 139 |
|
| uint16_t eeprom_command; |
| 140 |
|
| uint16_t read_bits; |
| 141 |
|
| |
| 142 |
|
| uint16_t x_latch; |
| 143 |
|
| uint16_t y_latch; |
| 144 |
|
| |
| 145 |
|
| } BESS_MBC7_t; |
| 146 |
|
| |
| 147 |
|
| typedef struct __attribute__((packed)){ |
| 148 |
|
| BESS_block_t header; |
| 149 |
|
| uint64_t last_rtc_second; |
| 150 |
|
| uint8_t real_rtc_data[4]; |
| 151 |
|
| uint8_t latched_rtc_data[4]; |
| 152 |
|
| uint8_t mr4; |
| 153 |
|
| } BESS_TPP1_t; |
| 154 |
|
| |
| 155 |
|
| typedef struct __attribute__((packed)) { |
| 156 |
|
| uint16_t address; |
| 157 |
|
| uint8_t value; |
| 158 |
|
| } BESS_MBC_pair_t; |
| 159 |
|
| |
| 160 |
|
| typedef struct virtual_file_s virtual_file_t; |
| 161 |
|
| struct virtual_file_s |
| 162 |
|
| { |
| 163 |
|
| size_t (*read)(virtual_file_t *file, void *dest, size_t length); |
| 164 |
|
| size_t (*write)(virtual_file_t *file, const void *dest, size_t length); |
| 165 |
|
| void (*seek)(virtual_file_t *file, ssize_t ammount, int origin); |
| 166 |
|
| size_t (*tell)(virtual_file_t *file); |
| 167 |
|
| union { |
| 168 |
|
| FILE *file; |
| 169 |
|
| struct { |
| 170 |
|
| uint8_t *buffer; |
| 171 |
|
| size_t position; |
| 172 |
|
| size_t size; |
| 173 |
|
| }; |
| 174 |
|
| }; |
| 175 |
|
| }; |
| 176 |
|
| |
| 177 |
0 |
| static size_t file_read(virtual_file_t *file, void *dest, size_t length) |
| 178 |
|
| { |
| 179 |
0 |
| return fread(dest, 1, length, file->file); |
| 180 |
|
| } |
| 181 |
|
| |
| 182 |
0 |
| static void file_seek(virtual_file_t *file, ssize_t ammount, int origin) |
| 183 |
|
| { |
| 184 |
0 |
| fseek(file->file, ammount, origin); |
| 185 |
0 |
| } |
| 186 |
|
| |
| 187 |
0 |
| static size_t file_write(virtual_file_t *file, const void *src, size_t length) |
| 188 |
|
| { |
| 189 |
0 |
| return fwrite(src, 1, length, file->file); |
| 190 |
|
| } |
| 191 |
|
| |
| 192 |
0 |
| static size_t file_tell(virtual_file_t *file) |
| 193 |
|
| { |
| 194 |
0 |
| return ftell(file->file); |
| 195 |
|
| } |
| 196 |
|
| |
| 197 |
66 |
| static size_t buffer_read(virtual_file_t *file, void *dest, size_t length) |
| 198 |
|
| { |
| 199 |
66 |
▶ | if (length & 0x80000000) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (length & 0x80000000) { |
0 |
66 |
66 |
|
| 200 |
0 |
| return 0; |
| 201 |
|
| } |
| 202 |
66 |
| errno = 0; |
| 203 |
66 |
▶ | if (length > file->size - file->position) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (length > file->size - file->position) { |
0 |
66 |
66 |
|
| 204 |
0 |
| errno = EIO; |
| 205 |
0 |
| length = file->size - file->position; |
| 206 |
|
| } |
| 207 |
|
| |
| 208 |
66 |
| memcpy(dest, file->buffer + file->position, length); |
| 209 |
66 |
| file->position += length; |
| 210 |
|
| |
| 211 |
66 |
| return length; |
| 212 |
|
| } |
| 213 |
|
| |
| 214 |
3 |
| static void buffer_seek(virtual_file_t *file, ssize_t ammount, int origin) |
| 215 |
|
| { |
| 216 |
3 |
| switch (origin) { |
| 217 |
0 |
| case SEEK_SET: |
| 218 |
0 |
| file->position = ammount; |
| 219 |
0 |
| break; |
| 220 |
3 |
| case SEEK_CUR: |
| 221 |
3 |
| file->position += ammount; |
| 222 |
3 |
| break; |
| 223 |
0 |
| case SEEK_END: |
| 224 |
0 |
| file->position = file->size + ammount; |
| 225 |
0 |
| break; |
| 226 |
0 |
| default: |
| 227 |
0 |
| break; |
| 228 |
|
| } |
| 229 |
|
| |
| 230 |
3 |
▶ | if (file->position > file->size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->position > file->size) { |
0 |
3 |
3 |
|
| 231 |
0 |
| file->position = file->size; |
| 232 |
|
| } |
| 233 |
3 |
| } |
| 234 |
|
| |
| 235 |
2702 |
| static size_t buffer_write(virtual_file_t *file, const void *src, size_t size) |
| 236 |
|
| { |
| 237 |
2702 |
| memcpy(file->buffer + file->position, src, size); |
| 238 |
2702 |
| file->position += size; |
| 239 |
2702 |
| return size; |
| 240 |
|
| } |
| 241 |
|
| |
| 242 |
612 |
| static size_t buffer_tell(virtual_file_t *file) |
| 243 |
|
| { |
| 244 |
612 |
| return file->position; |
| 245 |
|
| } |
| 246 |
|
| |
| 247 |
2 |
| static size_t bess_size_for_cartridge(const GB_cartridge_t *cart) |
| 248 |
|
| { |
| 249 |
2 |
| switch (cart->mbc_type) { |
| 250 |
2 |
| default: |
| 251 |
2 |
| case GB_NO_MBC: return 0; |
| 252 |
0 |
| case GB_MBC1: |
| 253 |
0 |
| return sizeof(BESS_block_t) + 4 * sizeof(BESS_MBC_pair_t); |
| 254 |
0 |
| case GB_MBC2: |
| 255 |
0 |
| return sizeof(BESS_block_t) + 2 * sizeof(BESS_MBC_pair_t); |
| 256 |
0 |
| case GB_MBC3: |
| 257 |
0 |
▶ | return sizeof(BESS_block_t) + 3 * sizeof(BESS_MBC_pair_t) + (cart->has_rtc? sizeof(BESS_RTC_t) : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| return sizeof(BESS_block_t) + 3 * sizeof(BESS_MBC_pair_t) + (cart->has_rtc? sizeof(BESS_RTC_t) : 0); |
0 |
0 |
0 |
|
| 258 |
0 |
| case GB_MBC5: |
| 259 |
0 |
| return sizeof(BESS_block_t) + 4 * sizeof(BESS_MBC_pair_t); |
| 260 |
0 |
| case GB_CAMERA: |
| 261 |
0 |
| return sizeof(BESS_block_t) + 3 * sizeof(BESS_MBC_pair_t); |
| 262 |
0 |
| case GB_MBC7: |
| 263 |
0 |
| return sizeof(BESS_block_t) + 3 * sizeof(BESS_MBC_pair_t) + sizeof(BESS_MBC7_t); |
| 264 |
0 |
| case GB_MMM01: |
| 265 |
0 |
| return sizeof(BESS_block_t) + 8 * sizeof(BESS_MBC_pair_t); |
| 266 |
0 |
| case GB_HUC1: |
| 267 |
0 |
| return sizeof(BESS_block_t) + 3 * sizeof(BESS_MBC_pair_t); |
| 268 |
0 |
| case GB_HUC3: |
| 269 |
0 |
| return sizeof(BESS_block_t) + 3 * sizeof(BESS_MBC_pair_t) + sizeof(BESS_HUC3_t); |
| 270 |
0 |
| case GB_TPP1: |
| 271 |
0 |
| return sizeof(BESS_block_t) + 4 * sizeof(BESS_MBC_pair_t) + sizeof(BESS_TPP1_t); |
| 272 |
|
| } |
| 273 |
|
| } |
| 274 |
|
| |
| 275 |
310 |
| size_t GB_get_save_state_size_no_bess(GB_gameboy_t *gb) |
| 276 |
|
| { |
| 277 |
|
| return GB_SECTION_SIZE(header) |
| 278 |
|
| + GB_SECTION_SIZE(core_state) + sizeof(uint32_t) |
| 279 |
|
| + GB_SECTION_SIZE(dma ) + sizeof(uint32_t) |
| 280 |
|
| + GB_SECTION_SIZE(mbc ) + sizeof(uint32_t) |
| 281 |
|
| + GB_SECTION_SIZE(hram ) + sizeof(uint32_t) |
| 282 |
|
| + GB_SECTION_SIZE(timing ) + sizeof(uint32_t) |
| 283 |
|
| + GB_SECTION_SIZE(apu ) + sizeof(uint32_t) |
| 284 |
|
| + GB_SECTION_SIZE(rtc ) + sizeof(uint32_t) |
| 285 |
|
| + GB_SECTION_SIZE(video ) + sizeof(uint32_t) |
| 286 |
|
| + GB_SECTION_SIZE(accessory ) + sizeof(uint32_t) |
| 287 |
310 |
▶ | + (GB_is_hle_sgb(gb)? sizeof(*gb->sgb) + sizeof(uint32_t) : 0) |
Branch: 1 branch
| Branch | True | False | Total |
| + (GB_is_hle_sgb(gb)? sizeof(*gb->sgb) + sizeof(uint32_t) : 0) |
0 |
310 |
310 |
|
| 288 |
310 |
| + gb->mbc_ram_size |
| 289 |
310 |
| + gb->ram_size |
| 290 |
310 |
| + gb->vram_size; |
| 291 |
|
| } |
| 292 |
|
| |
| 293 |
2 |
| size_t GB_get_save_state_size(GB_gameboy_t *gb) |
| 294 |
|
| { |
| 295 |
2 |
| return GB_get_save_state_size_no_bess(gb) + |
| 296 |
|
| // BESS |
| 297 |
|
| + sizeof(BESS_block_t) // NAME |
| 298 |
|
| + sizeof(BESS_NAME) - 1 |
| 299 |
|
| + sizeof(BESS_INFO_t) // INFO |
| 300 |
|
| + sizeof(BESS_CORE_t) |
| 301 |
|
| + sizeof(BESS_XOAM_t) |
| 302 |
2 |
▶ | + (gb->sgb? sizeof(BESS_SGB_t) : 0) |
Branch: 1 branch
| Branch | True | False | Total |
| + (gb->sgb? sizeof(BESS_SGB_t) : 0) |
0 |
2 |
2 |
|
| 303 |
2 |
| + bess_size_for_cartridge(gb->cartridge_type) // MBC & RTC/HUC3/TPP1/MBC7 block |
| 304 |
|
| + sizeof(BESS_block_t) // END block |
| 305 |
2 |
| + sizeof(BESS_footer_t); |
| 306 |
|
| } |
| 307 |
|
| |
| 308 |
3 |
| static bool verify_and_update_state_compatibility(GB_gameboy_t *gb, GB_gameboy_t *save, bool *attempt_bess) |
| 309 |
|
| { |
| 310 |
3 |
| *attempt_bess = false; |
| 311 |
|
| |
| 312 |
3 |
▶ | if (gb->version != save->version) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->version != save->version) { |
0 |
3 |
3 |
|
| 313 |
0 |
| GB_log(gb, "The save state is for a different version of SameBoy.\n"); |
| 314 |
0 |
| *attempt_bess = true; |
| 315 |
0 |
| return false; |
| 316 |
|
| } |
| 317 |
|
| |
| 318 |
3 |
▶ | if (GB_is_cgb(gb) != GB_is_cgb(save) || GB_is_hle_sgb(gb) != GB_is_hle_sgb(save)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (GB_is_cgb(gb) != GB_is_cgb(save) || GB_is_hle_sgb(gb) != GB_is_hle_sgb(save)) { |
3 |
0 |
3 |
| if (GB_is_cgb(gb) != GB_is_cgb(save) || GB_is_hle_sgb(gb) != GB_is_hle_sgb(save)) { |
0 |
3 |
3 |
|
| 319 |
0 |
| GB_log(gb, "The save state is for a different Game Boy model. Try changing the emulated model.\n"); |
| 320 |
0 |
| return false; |
| 321 |
|
| } |
| 322 |
|
| |
| 323 |
3 |
▶ | if (gb->mbc_ram_size < save->mbc_ram_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->mbc_ram_size < save->mbc_ram_size) { |
0 |
3 |
3 |
|
| 324 |
0 |
| GB_log(gb, "The save state has non-matching MBC RAM size.\n"); |
| 325 |
0 |
| return false; |
| 326 |
|
| } |
| 327 |
|
| |
| 328 |
3 |
▶ | if (gb->vram_size != save->vram_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->vram_size != save->vram_size) { |
0 |
3 |
3 |
|
| 329 |
0 |
| GB_log(gb, "The save state has non-matching VRAM size. Try changing the emulated model.\n"); |
| 330 |
0 |
| return false; |
| 331 |
|
| } |
| 332 |
|
| |
| 333 |
3 |
▶ | if (GB_is_hle_sgb(gb) != GB_is_hle_sgb(save)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GB_is_hle_sgb(gb) != GB_is_hle_sgb(save)) { |
0 |
3 |
3 |
|
| 334 |
0 |
▶ | GB_log(gb, "The save state is %sfor a Super Game Boy. Try changing the emulated model.\n", GB_is_hle_sgb(save)? "" : "not "); |
Branch: 1 branch
| Branch | True | False | Total |
| GB_log(gb, "The save state is %sfor a Super Game Boy. Try changing the emulated model.\n", GB_is_hle_sgb(save)? "" : "not "); |
0 |
0 |
0 |
|
| 335 |
0 |
| return false; |
| 336 |
|
| } |
| 337 |
|
| |
| 338 |
3 |
▶ | if (gb->ram_size != save->ram_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->ram_size != save->ram_size) { |
0 |
3 |
3 |
|
| 339 |
0 |
| GB_log(gb, "The save state has non-matching RAM size. Try changing the emulated model.\n"); |
| 340 |
0 |
| return false; |
| 341 |
|
| } |
| 342 |
|
| |
| 343 |
3 |
▶ | if (gb->accessory != save->accessory) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->accessory != save->accessory) { |
0 |
3 |
3 |
|
| 344 |
0 |
| memset(GB_GET_SECTION(save, accessory), 0, GB_SECTION_SIZE(accessory)); |
| 345 |
|
| } |
| 346 |
|
| |
| 347 |
3 |
| switch (save->model) { |
| 348 |
0 |
| case GB_MODEL_DMG_B: return true; |
| 349 |
0 |
| case GB_MODEL_SGB_NTSC: return true; |
| 350 |
0 |
| case GB_MODEL_SGB_PAL: return true; |
| 351 |
0 |
| case GB_MODEL_SGB_NTSC_NO_SFC: return true; |
| 352 |
0 |
| case GB_MODEL_SGB_PAL_NO_SFC: return true; |
| 353 |
0 |
| case GB_MODEL_MGB: return true; |
| 354 |
0 |
| case GB_MODEL_SGB2: return true; |
| 355 |
0 |
| case GB_MODEL_SGB2_NO_SFC: return true; |
| 356 |
0 |
| case GB_MODEL_CGB_0: return true; |
| 357 |
0 |
| case GB_MODEL_CGB_A: return true; |
| 358 |
0 |
| case GB_MODEL_CGB_B: return true; |
| 359 |
0 |
| case GB_MODEL_CGB_C: return true; |
| 360 |
0 |
| case GB_MODEL_CGB_D: return true; |
| 361 |
3 |
| case GB_MODEL_CGB_E: return true; |
| 362 |
0 |
| case GB_MODEL_AGB_A: return true; |
| 363 |
0 |
| case GB_MODEL_GBP_A: return true; |
| 364 |
|
| } |
| 365 |
0 |
▶ | if ((gb->model & GB_MODEL_FAMILY_MASK) == (save->model & GB_MODEL_FAMILY_MASK)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((gb->model & GB_MODEL_FAMILY_MASK) == (save->model & GB_MODEL_FAMILY_MASK)) { |
0 |
0 |
0 |
|
| 366 |
0 |
| save->model = gb->model; |
| 367 |
0 |
| return true; |
| 368 |
|
| } |
| 369 |
|
| |
| 370 |
0 |
| GB_log(gb, "This save state is for an unknown Game Boy model\n"); |
| 371 |
0 |
| return false; |
| 372 |
|
| } |
| 373 |
|
| |
| 374 |
3 |
| static void sanitize_state(GB_gameboy_t *gb) |
| 375 |
|
| { |
| 376 |
99 |
▶ | for (unsigned i = 0; i < 32; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned i = 0; i < 32; i++) { |
96 |
3 |
99 |
|
| 377 |
96 |
| GB_palette_changed(gb, false, i * 2); |
| 378 |
96 |
| GB_palette_changed(gb, true, i * 2); |
| 379 |
|
| } |
| 380 |
|
| |
| 381 |
3 |
| gb->bg_fifo.read_end &= GB_FIFO_LENGTH - 1; |
| 382 |
3 |
| gb->oam_fifo.read_end &= GB_FIFO_LENGTH - 1; |
| 383 |
3 |
| gb->last_tile_index_address &= 0x1FFF; |
| 384 |
3 |
| gb->window_tile_x &= 0x1F; |
| 385 |
|
| |
| 386 |
|
| /* These are kind of DOS-ish if too large */ |
| 387 |
3 |
▶ | if (abs(gb->display_cycles) > 0x80000) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (abs(gb->display_cycles) > 0x80000) { |
0 |
3 |
3 |
|
| 388 |
0 |
| gb->display_cycles = 0; |
| 389 |
|
| } |
| 390 |
|
| |
| 391 |
3 |
▶ | if (abs(gb->div_cycles) > 0x8000) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (abs(gb->div_cycles) > 0x8000) { |
0 |
3 |
3 |
|
| 392 |
0 |
| gb->div_cycles = 0; |
| 393 |
|
| } |
| 394 |
|
| |
| 395 |
3 |
▶ | if (!GB_is_cgb(gb)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!GB_is_cgb(gb)) { |
0 |
3 |
3 |
|
| 396 |
0 |
| gb->cgb_mode = false; |
| 397 |
|
| } |
| 398 |
|
| |
| 399 |
3 |
▶ | if (gb->ram_size == 0x8000) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->ram_size == 0x8000) { |
3 |
0 |
3 |
|
| 400 |
3 |
| gb->cgb_ram_bank &= 0x7; |
| 401 |
|
| } |
| 402 |
|
| else { |
| 403 |
0 |
| gb->cgb_ram_bank = 1; |
| 404 |
|
| } |
| 405 |
3 |
▶ | if (gb->vram_size != 0x4000) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->vram_size != 0x4000) { |
0 |
3 |
3 |
|
| 406 |
0 |
| gb->cgb_vram_bank = 0; |
| 407 |
|
| } |
| 408 |
3 |
▶ | if (!GB_is_cgb(gb)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!GB_is_cgb(gb)) { |
0 |
3 |
3 |
|
| 409 |
0 |
| gb->current_tile_attributes = 0; |
| 410 |
|
| } |
| 411 |
|
| |
| 412 |
3 |
| gb->object_low_line_address &= (gb->vram_size - 1) & ~1; |
| 413 |
3 |
▶ | if (gb->lcd_x > gb->position_in_line) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->lcd_x > gb->position_in_line) { |
0 |
3 |
3 |
|
| 414 |
0 |
| gb->lcd_x = gb->position_in_line; |
| 415 |
|
| } |
| 416 |
|
| |
| 417 |
3 |
▶ | if (gb->sgb) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->sgb) { |
0 |
3 |
3 |
|
| 418 |
0 |
▶ | if (gb->sgb->player_count != 1 && gb->sgb->player_count != 2 && gb->sgb->player_count != 4) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (gb->sgb->player_count != 1 && gb->sgb->player_count != 2 && gb->sgb->player_count != 4) { |
0 |
0 |
0 |
| if (gb->sgb->player_count != 1 && gb->sgb->player_count != 2 && gb->sgb->player_count != 4) { |
0 |
0 |
0 |
| if (gb->sgb->player_count != 1 && gb->sgb->player_count != 2 && gb->sgb->player_count != 4) { |
0 |
0 |
0 |
|
| 419 |
0 |
| gb->sgb->player_count = 1; |
| 420 |
|
| } |
| 421 |
0 |
| gb->sgb->current_player &= gb->sgb->player_count - 1; |
| 422 |
|
| } |
| 423 |
3 |
| GB_update_clock_rate(gb); |
| 424 |
|
| |
| 425 |
3 |
▶ | if (gb->camera_update_request_callback) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->camera_update_request_callback) { |
0 |
3 |
3 |
|
| 426 |
0 |
| GB_camera_updated(gb); |
| 427 |
|
| } |
| 428 |
|
| |
| 429 |
3 |
▶ | if (!gb->apu.apu_cycles_in_2mhz) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!gb->apu.apu_cycles_in_2mhz) { |
0 |
3 |
3 |
|
| 430 |
0 |
| gb->apu.apu_cycles >>= 2; |
| 431 |
0 |
| gb->apu.apu_cycles_in_2mhz = true; |
| 432 |
|
| } |
| 433 |
|
| |
| 434 |
3 |
▶ | if (gb->n_visible_objs > 10) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->n_visible_objs > 10) { |
0 |
3 |
3 |
|
| 435 |
0 |
| gb->n_visible_objs = 10; |
| 436 |
|
| } |
| 437 |
3 |
| } |
| 438 |
|
| |
| 439 |
1098 |
| static bool dump_section(virtual_file_t *file, const void *src, uint32_t size) |
| 440 |
|
| { |
| 441 |
1098 |
▶ | if (file->write(file, &size, sizeof(size)) != sizeof(size)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->write(file, &size, sizeof(size)) != sizeof(size)) { |
0 |
1098 |
1098 |
|
| 442 |
0 |
| return false; |
| 443 |
|
| } |
| 444 |
|
| |
| 445 |
1098 |
▶ | if (file->write(file, src, size) != size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->write(file, src, size) != size) { |
0 |
1098 |
1098 |
|
| 446 |
0 |
| return false; |
| 447 |
|
| } |
| 448 |
|
| |
| 449 |
1098 |
| return true; |
| 450 |
|
| } |
| 451 |
|
| |
| 452 |
|
| #define DUMP_SECTION(gb, f, section) dump_section(f, GB_GET_SECTION(gb, section), GB_SECTION_SIZE(section)) |
| 453 |
|
| |
| 454 |
2 |
| static int save_bess_mbc_block(GB_gameboy_t *gb, virtual_file_t *file) |
| 455 |
|
| { |
| 456 |
|
| |
| 457 |
2 |
| BESS_block_t mbc_block = {BE32('MBC '), 0}; |
| 458 |
|
| BESS_MBC_pair_t pairs[8]; |
| 459 |
2 |
| switch (gb->cartridge_type->mbc_type) { |
| 460 |
2 |
| default: |
| 461 |
2 |
| case GB_NO_MBC: return 0; |
| 462 |
0 |
| case GB_MBC1: |
| 463 |
0 |
▶ | pairs[0] = (BESS_MBC_pair_t){LE16(0x0000), gb->mbc_ram_enable? 0xA : 0x0}; |
Branch: 1 branch
| Branch | True | False | Total |
| pairs[0] = (BESS_MBC_pair_t){LE16(0x0000), gb->mbc_ram_enable? 0xA : 0x0}; |
0 |
0 |
0 |
|
| 464 |
0 |
| pairs[1] = (BESS_MBC_pair_t){LE16(0x2000), gb->mbc1.bank_low}; |
| 465 |
0 |
| pairs[2] = (BESS_MBC_pair_t){LE16(0x4000), gb->mbc1.bank_high}; |
| 466 |
0 |
| pairs[3] = (BESS_MBC_pair_t){LE16(0x6000), gb->mbc1.mode}; |
| 467 |
0 |
| mbc_block.size = 4 * sizeof(pairs[0]); |
| 468 |
0 |
| break; |
| 469 |
0 |
| case GB_MBC2: |
| 470 |
0 |
▶ | pairs[0] = (BESS_MBC_pair_t){LE16(0x0000), gb->mbc_ram_enable? 0xA : 0x0}; |
Branch: 1 branch
| Branch | True | False | Total |
| pairs[0] = (BESS_MBC_pair_t){LE16(0x0000), gb->mbc_ram_enable? 0xA : 0x0}; |
0 |
0 |
0 |
|
| 471 |
0 |
| pairs[1] = (BESS_MBC_pair_t){LE16(0x0100), gb->mbc2.rom_bank}; |
| 472 |
0 |
| mbc_block.size = 2 * sizeof(pairs[0]); |
| 473 |
0 |
| break; |
| 474 |
0 |
| case GB_MBC3: |
| 475 |
0 |
▶ | pairs[0] = (BESS_MBC_pair_t){LE16(0x0000), gb->mbc_ram_enable? 0xA : 0x0}; |
Branch: 1 branch
| Branch | True | False | Total |
| pairs[0] = (BESS_MBC_pair_t){LE16(0x0000), gb->mbc_ram_enable? 0xA : 0x0}; |
0 |
0 |
0 |
|
| 476 |
0 |
| pairs[1] = (BESS_MBC_pair_t){LE16(0x2000), gb->mbc3.rom_bank}; |
| 477 |
0 |
| pairs[2] = (BESS_MBC_pair_t){LE16(0x4000), gb->mbc3.ram_bank | (gb->mbc3.rtc_mapped? 8 : 0)}; |
| 478 |
0 |
| mbc_block.size = 3 * sizeof(pairs[0]); |
| 479 |
0 |
| break; |
| 480 |
0 |
| case GB_MBC5: |
| 481 |
0 |
▶ | pairs[0] = (BESS_MBC_pair_t){LE16(0x0000), gb->mbc_ram_enable? 0xA : 0x0}; |
Branch: 1 branch
| Branch | True | False | Total |
| pairs[0] = (BESS_MBC_pair_t){LE16(0x0000), gb->mbc_ram_enable? 0xA : 0x0}; |
0 |
0 |
0 |
|
| 482 |
0 |
| pairs[1] = (BESS_MBC_pair_t){LE16(0x2000), gb->mbc5.rom_bank_low}; |
| 483 |
0 |
| pairs[2] = (BESS_MBC_pair_t){LE16(0x3000), gb->mbc5.rom_bank_high}; |
| 484 |
0 |
| pairs[3] = (BESS_MBC_pair_t){LE16(0x4000), gb->mbc5.ram_bank}; |
| 485 |
0 |
| mbc_block.size = 4 * sizeof(pairs[0]); |
| 486 |
0 |
| break; |
| 487 |
0 |
| case GB_CAMERA: |
| 488 |
0 |
▶ | pairs[0] = (BESS_MBC_pair_t){LE16(0x0000), gb->mbc_ram_enable? 0xA : 0x0}; |
Branch: 1 branch
| Branch | True | False | Total |
| pairs[0] = (BESS_MBC_pair_t){LE16(0x0000), gb->mbc_ram_enable? 0xA : 0x0}; |
0 |
0 |
0 |
|
| 489 |
0 |
| pairs[1] = (BESS_MBC_pair_t){LE16(0x2000), gb->mbc5.rom_bank_low}; |
| 490 |
0 |
| pairs[2] = (BESS_MBC_pair_t){LE16(0x4000), gb->mbc5.ram_bank}; |
| 491 |
0 |
| mbc_block.size = 3 * sizeof(pairs[0]); |
| 492 |
0 |
| break; |
| 493 |
0 |
| case GB_MBC7: |
| 494 |
0 |
▶ | pairs[0] = (BESS_MBC_pair_t){LE16(0x0000), gb->mbc_ram_enable? 0xA : 0x0}; |
Branch: 1 branch
| Branch | True | False | Total |
| pairs[0] = (BESS_MBC_pair_t){LE16(0x0000), gb->mbc_ram_enable? 0xA : 0x0}; |
0 |
0 |
0 |
|
| 495 |
0 |
| pairs[1] = (BESS_MBC_pair_t){LE16(0x2000), gb->mbc7.rom_bank}; |
| 496 |
0 |
| pairs[2] = (BESS_MBC_pair_t){LE16(0x4000), gb->mbc7.secondary_ram_enable? 0x40 : 0}; |
| 497 |
0 |
| mbc_block.size = 3 * sizeof(pairs[0]); |
| 498 |
0 |
| break; |
| 499 |
0 |
| case GB_MMM01: |
| 500 |
0 |
| pairs[0] = (BESS_MBC_pair_t){LE16(0x2000), (gb->mmm01.rom_bank_low & (gb->mmm01.rom_bank_mask << 1)) | (gb->mmm01.rom_bank_mid << 5)}; |
| 501 |
0 |
| pairs[1] = (BESS_MBC_pair_t){LE16(0x6000), gb->mmm01.mbc1_mode | (gb->mmm01.rom_bank_mask << 2) | (gb->mmm01.multiplex_mode << 6)}; |
| 502 |
0 |
| pairs[2] = (BESS_MBC_pair_t){LE16(0x4000), gb->mmm01.ram_bank_low | (gb->mmm01.ram_bank_high << 2) | (gb->mmm01.rom_bank_high << 4) | (gb->mmm01.mbc1_mode_disable << 6)}; |
| 503 |
0 |
▶ | pairs[3] = (BESS_MBC_pair_t){LE16(0x0000), (gb->mbc_ram_enable? 0xA : 0x0) | (gb->mmm01.ram_bank_mask << 4) | (gb->mmm01.locked << 6)}; |
Branch: 1 branch
| Branch | True | False | Total |
| pairs[3] = (BESS_MBC_pair_t){LE16(0x0000), (gb->mbc_ram_enable? 0xA : 0x0) | (gb->mmm01.ram_bank_mask << 4) | (gb->mmm01.locked << 6)}; |
0 |
0 |
0 |
|
| 504 |
|
| /* For compatibility with emulators that inaccurately emulate MMM01, and also require two writes per register */ |
| 505 |
0 |
| pairs[4] = (BESS_MBC_pair_t){LE16(0x2000), (gb->mmm01.rom_bank_low & ~(gb->mmm01.rom_bank_mask << 1))}; |
| 506 |
0 |
| pairs[5] = pairs[1]; |
| 507 |
0 |
| pairs[6] = pairs[2]; |
| 508 |
0 |
| pairs[7] = pairs[3]; |
| 509 |
0 |
| mbc_block.size = 8 * sizeof(pairs[0]); |
| 510 |
0 |
| break; |
| 511 |
0 |
| case GB_HUC1: |
| 512 |
0 |
▶ | pairs[0] = (BESS_MBC_pair_t){LE16(0x0000), gb->huc1.ir_mode? 0xE : 0x0}; |
Branch: 1 branch
| Branch | True | False | Total |
| pairs[0] = (BESS_MBC_pair_t){LE16(0x0000), gb->huc1.ir_mode? 0xE : 0x0}; |
0 |
0 |
0 |
|
| 513 |
0 |
| pairs[1] = (BESS_MBC_pair_t){LE16(0x2000), gb->huc1.bank_low}; |
| 514 |
0 |
| pairs[2] = (BESS_MBC_pair_t){LE16(0x4000), gb->huc1.bank_high}; |
| 515 |
0 |
| mbc_block.size = 3 * sizeof(pairs[0]); |
| 516 |
0 |
| case GB_HUC3: |
| 517 |
0 |
| pairs[0] = (BESS_MBC_pair_t){LE16(0x0000), gb->huc3.mode}; |
| 518 |
0 |
| pairs[1] = (BESS_MBC_pair_t){LE16(0x2000), gb->huc3.rom_bank}; |
| 519 |
0 |
| pairs[2] = (BESS_MBC_pair_t){LE16(0x4000), gb->huc3.ram_bank}; |
| 520 |
0 |
| mbc_block.size = 3 * sizeof(pairs[0]); |
| 521 |
0 |
| break; |
| 522 |
0 |
| case GB_TPP1: |
| 523 |
0 |
| pairs[0] = (BESS_MBC_pair_t){LE16(0x0000), gb->tpp1.rom_bank}; |
| 524 |
0 |
| pairs[1] = (BESS_MBC_pair_t){LE16(0x0001), gb->tpp1.rom_bank >> 8}; |
| 525 |
0 |
| pairs[2] = (BESS_MBC_pair_t){LE16(0x0002), gb->tpp1.rom_bank}; |
| 526 |
0 |
| pairs[3] = (BESS_MBC_pair_t){LE16(0x0003), gb->tpp1.mode}; |
| 527 |
0 |
| mbc_block.size = 4 * sizeof(pairs[0]); |
| 528 |
0 |
| break; |
| 529 |
|
| } |
| 530 |
|
| |
| 531 |
0 |
| mbc_block.size = LE32(mbc_block.size); |
| 532 |
|
| |
| 533 |
0 |
▶ | if (file->write(file, &mbc_block, sizeof(mbc_block)) != sizeof(mbc_block)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->write(file, &mbc_block, sizeof(mbc_block)) != sizeof(mbc_block)) { |
0 |
0 |
0 |
|
| 534 |
0 |
| return errno; |
| 535 |
|
| } |
| 536 |
|
| |
| 537 |
0 |
▶ | if (file->write(file, &pairs, LE32(mbc_block.size)) != LE32(mbc_block.size)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->write(file, &pairs, LE32(mbc_block.size)) != LE32(mbc_block.size)) { |
0 |
0 |
0 |
|
| 538 |
0 |
| return errno; |
| 539 |
|
| } |
| 540 |
|
| |
| 541 |
0 |
| return 0; |
| 542 |
|
| } |
| 543 |
|
| |
| 544 |
2 |
| static const uint8_t *get_header_bank(GB_gameboy_t *gb) |
| 545 |
|
| { |
| 546 |
2 |
▶ | if (gb->cartridge_type->mbc_type == GB_MMM01) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->cartridge_type->mbc_type == GB_MMM01) { |
0 |
2 |
2 |
|
| 547 |
0 |
| return gb->rom + gb->rom_size - 0x8000; |
| 548 |
|
| } |
| 549 |
2 |
| return gb->rom; |
| 550 |
|
| } |
| 551 |
|
| |
| 552 |
122 |
| static int save_state_internal(GB_gameboy_t *gb, virtual_file_t *file, bool append_bess) |
| 553 |
|
| { |
| 554 |
122 |
| errno = 0; |
| 555 |
122 |
▶ | if (file->write(file, GB_GET_SECTION(gb, header), GB_SECTION_SIZE(header)) != GB_SECTION_SIZE(header)) goto error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->write(file, GB_GET_SECTION(gb, header), GB_SECTION_SIZE(header)) != GB_SECTION_SIZE(header)) goto error; |
0 |
122 |
122 |
|
| 556 |
122 |
▶ | if (!DUMP_SECTION(gb, file, core_state)) goto error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!DUMP_SECTION(gb, file, core_state)) goto error; |
0 |
122 |
122 |
|
| 557 |
122 |
▶ | if (!DUMP_SECTION(gb, file, dma )) goto error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!DUMP_SECTION(gb, file, dma )) goto error; |
0 |
122 |
122 |
|
| 558 |
122 |
▶ | if (!DUMP_SECTION(gb, file, mbc )) goto error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!DUMP_SECTION(gb, file, mbc )) goto error; |
0 |
122 |
122 |
|
| 559 |
122 |
| uint32_t hram_offset = file->tell(file) + 4; |
| 560 |
122 |
▶ | if (!DUMP_SECTION(gb, file, hram )) goto error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!DUMP_SECTION(gb, file, hram )) goto error; |
0 |
122 |
122 |
|
| 561 |
122 |
▶ | if (!DUMP_SECTION(gb, file, timing )) goto error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!DUMP_SECTION(gb, file, timing )) goto error; |
0 |
122 |
122 |
|
| 562 |
122 |
▶ | if (!DUMP_SECTION(gb, file, apu )) goto error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!DUMP_SECTION(gb, file, apu )) goto error; |
0 |
122 |
122 |
|
| 563 |
122 |
▶ | if (!DUMP_SECTION(gb, file, rtc )) goto error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!DUMP_SECTION(gb, file, rtc )) goto error; |
0 |
122 |
122 |
|
| 564 |
122 |
| uint32_t video_offset = file->tell(file) + 4; |
| 565 |
122 |
▶ | if (!DUMP_SECTION(gb, file, video )) goto error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!DUMP_SECTION(gb, file, video )) goto error; |
0 |
122 |
122 |
|
| 566 |
122 |
▶ | if (!DUMP_SECTION(gb, file, accessory )) goto error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!DUMP_SECTION(gb, file, accessory )) goto error; |
0 |
122 |
122 |
|
| 567 |
|
| |
| 568 |
122 |
| uint32_t sgb_offset = 0; |
| 569 |
|
| |
| 570 |
122 |
▶ | if (GB_is_hle_sgb(gb)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GB_is_hle_sgb(gb)) { |
0 |
122 |
122 |
|
| 571 |
0 |
| sgb_offset = file->tell(file) + 4; |
| 572 |
0 |
▶ | if (!dump_section(file, gb->sgb, sizeof(*gb->sgb))) goto error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!dump_section(file, gb->sgb, sizeof(*gb->sgb))) goto error; |
0 |
0 |
0 |
|
| 573 |
|
| } |
| 574 |
|
| |
| 575 |
|
| |
| 576 |
122 |
| BESS_CORE_t bess_core = {0,}; |
| 577 |
|
| |
| 578 |
122 |
| bess_core.mbc_ram.offset = LE32(file->tell(file)); |
| 579 |
122 |
| bess_core.mbc_ram.size = LE32(gb->mbc_ram_size); |
| 580 |
122 |
▶ | if (file->write(file, gb->mbc_ram, gb->mbc_ram_size) != gb->mbc_ram_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->write(file, gb->mbc_ram, gb->mbc_ram_size) != gb->mbc_ram_size) { |
0 |
122 |
122 |
|
| 581 |
0 |
| goto error; |
| 582 |
|
| } |
| 583 |
|
| |
| 584 |
122 |
| bess_core.ram.offset = LE32(file->tell(file)); |
| 585 |
122 |
| bess_core.ram.size = LE32(gb->ram_size); |
| 586 |
122 |
▶ | if (file->write(file, gb->ram, gb->ram_size) != gb->ram_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->write(file, gb->ram, gb->ram_size) != gb->ram_size) { |
0 |
122 |
122 |
|
| 587 |
0 |
| goto error; |
| 588 |
|
| } |
| 589 |
|
| |
| 590 |
122 |
| bess_core.vram.offset = LE32(file->tell(file)); |
| 591 |
122 |
| bess_core.vram.size = LE32(gb->vram_size); |
| 592 |
122 |
▶ | if (file->write(file, gb->vram, gb->vram_size) != gb->vram_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->write(file, gb->vram, gb->vram_size) != gb->vram_size) { |
0 |
122 |
122 |
|
| 593 |
0 |
| goto error; |
| 594 |
|
| } |
| 595 |
|
| |
| 596 |
122 |
▶ | if (!append_bess) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!append_bess) return 0; |
120 |
2 |
122 |
|
| 597 |
|
| |
| 598 |
2 |
| BESS_footer_t bess_footer = { |
| 599 |
2 |
| .start_offset = LE32(file->tell(file)), |
| 600 |
|
| .magic = BE32('BESS'), |
| 601 |
|
| }; |
| 602 |
|
| |
| 603 |
|
| /* BESS NAME */ |
| 604 |
|
| |
| 605 |
|
| static const BESS_block_t bess_name = {BE32('NAME'), LE32(sizeof(BESS_NAME) - 1)}; |
| 606 |
|
| |
| 607 |
2 |
▶ | if (file->write(file, &bess_name, sizeof(bess_name)) != sizeof(bess_name)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->write(file, &bess_name, sizeof(bess_name)) != sizeof(bess_name)) { |
0 |
2 |
2 |
|
| 608 |
0 |
| goto error; |
| 609 |
|
| } |
| 610 |
|
| |
| 611 |
2 |
▶ | if (file->write(file, BESS_NAME, sizeof(BESS_NAME) - 1) != sizeof(BESS_NAME) - 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->write(file, BESS_NAME, sizeof(BESS_NAME) - 1) != sizeof(BESS_NAME) - 1) { |
0 |
2 |
2 |
|
| 612 |
0 |
| goto error; |
| 613 |
|
| } |
| 614 |
|
| |
| 615 |
|
| /* BESS INFO */ |
| 616 |
|
| |
| 617 |
|
| static const BESS_block_t bess_info = {BE32('INFO'), LE32(sizeof(BESS_INFO_t) - sizeof(BESS_block_t))}; |
| 618 |
|
| |
| 619 |
2 |
▶ | if (file->write(file, &bess_info, sizeof(bess_info)) != sizeof(bess_info)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->write(file, &bess_info, sizeof(bess_info)) != sizeof(bess_info)) { |
0 |
2 |
2 |
|
| 620 |
0 |
| goto error; |
| 621 |
|
| } |
| 622 |
|
| |
| 623 |
2 |
| const uint8_t *bank = get_header_bank(gb); |
| 624 |
|
| |
| 625 |
2 |
▶ | if (file->write(file, bank + 0x134, 0x10) != 0x10) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->write(file, bank + 0x134, 0x10) != 0x10) { |
0 |
2 |
2 |
|
| 626 |
0 |
| goto error; |
| 627 |
|
| } |
| 628 |
|
| |
| 629 |
2 |
▶ | if (file->write(file, bank + 0x14E, 2) != 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->write(file, bank + 0x14E, 2) != 2) { |
0 |
2 |
2 |
|
| 630 |
0 |
| goto error; |
| 631 |
|
| } |
| 632 |
|
| |
| 633 |
|
| /* BESS CORE */ |
| 634 |
|
| |
| 635 |
2 |
| bess_core.header = (BESS_block_t){BE32('CORE'), LE32(sizeof(bess_core) - sizeof(bess_core.header))}; |
| 636 |
2 |
| bess_core.major = LE16(1); |
| 637 |
2 |
| bess_core.minor = LE16(1); |
| 638 |
2 |
| switch (gb->model) { |
| 639 |
|
| |
| 640 |
0 |
| case GB_MODEL_DMG_B: bess_core.full_model = BE32('GDB '); break; |
| 641 |
0 |
| case GB_MODEL_MGB: bess_core.full_model = BE32('GM '); break; |
| 642 |
|
| |
| 643 |
0 |
| case GB_MODEL_SGB_NTSC: |
| 644 |
|
| case GB_MODEL_SGB_NTSC_NO_SFC: |
| 645 |
0 |
| bess_core.full_model = BE32('SN '); break; |
| 646 |
|
| |
| 647 |
0 |
| case GB_MODEL_SGB_PAL_NO_SFC: |
| 648 |
|
| case GB_MODEL_SGB_PAL: |
| 649 |
0 |
| bess_core.full_model = BE32('SP '); break; |
| 650 |
|
| |
| 651 |
0 |
| case GB_MODEL_SGB2_NO_SFC: |
| 652 |
|
| case GB_MODEL_SGB2: |
| 653 |
0 |
| bess_core.full_model = BE32('S2 '); break; |
| 654 |
|
| |
| 655 |
0 |
| case GB_MODEL_CGB_0: bess_core.full_model = BE32('CC0 '); break; |
| 656 |
0 |
| case GB_MODEL_CGB_A: bess_core.full_model = BE32('CCA '); break; |
| 657 |
0 |
| case GB_MODEL_CGB_B: bess_core.full_model = BE32('CCB '); break; |
| 658 |
0 |
| case GB_MODEL_CGB_C: bess_core.full_model = BE32('CCC '); break; |
| 659 |
0 |
| case GB_MODEL_CGB_D: bess_core.full_model = BE32('CCD '); break; |
| 660 |
2 |
| case GB_MODEL_CGB_E: bess_core.full_model = BE32('CCE '); break; |
| 661 |
0 |
| case GB_MODEL_AGB_A: |
| 662 |
|
| case GB_MODEL_GBP_A: |
| 663 |
0 |
| bess_core.full_model = BE32('CAA '); break; |
| 664 |
|
| } |
| 665 |
|
| |
| 666 |
2 |
| bess_core.pc = LE16(gb->pc); |
| 667 |
2 |
| bess_core.af = LE16(gb->af); |
| 668 |
2 |
| bess_core.bc = LE16(gb->bc); |
| 669 |
2 |
| bess_core.de = LE16(gb->de); |
| 670 |
2 |
| bess_core.hl = LE16(gb->hl); |
| 671 |
2 |
| bess_core.sp = LE16(gb->sp); |
| 672 |
|
| |
| 673 |
2 |
| bess_core.ime = gb->ime; |
| 674 |
2 |
| bess_core.ie = gb->interrupt_enable; |
| 675 |
2 |
| bess_core.execution_mode = 0; |
| 676 |
2 |
▶ | if (gb->halted) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->halted) { |
0 |
2 |
2 |
|
| 677 |
0 |
| bess_core.execution_mode = 1; |
| 678 |
|
| } |
| 679 |
2 |
▶ | else if (gb->stopped) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (gb->stopped) { |
0 |
2 |
2 |
|
| 680 |
0 |
| bess_core.execution_mode = 2; |
| 681 |
|
| } |
| 682 |
|
| |
| 683 |
2 |
| memcpy(bess_core.io_registers, gb->io_registers, sizeof(gb->io_registers)); |
| 684 |
2 |
| bess_core.io_registers[GB_IO_DIV] = gb->div_counter >> 8; |
| 685 |
2 |
| bess_core.io_registers[GB_IO_BANK] = gb->boot_rom_finished; |
| 686 |
2 |
▶ | bess_core.io_registers[GB_IO_KEY1] |= gb->cgb_double_speed? 0x80 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| bess_core.io_registers[GB_IO_KEY1] |= gb->cgb_double_speed? 0x80 : 0; |
0 |
2 |
2 |
|
| 687 |
2 |
| bess_core.hram.size = LE32(sizeof(gb->hram)); |
| 688 |
2 |
| bess_core.hram.offset = LE32(hram_offset + offsetof(GB_gameboy_t, hram) - GB_SECTION_OFFSET(hram)); |
| 689 |
2 |
| bess_core.oam.size = LE32(sizeof(gb->oam)); |
| 690 |
2 |
| bess_core.oam.offset = LE32(video_offset + offsetof(GB_gameboy_t, oam) - GB_SECTION_OFFSET(video)); |
| 691 |
2 |
▶ | if (GB_is_cgb(gb)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GB_is_cgb(gb)) { |
2 |
0 |
2 |
|
| 692 |
2 |
| bess_core.background_palettes.size = LE32(sizeof(gb->background_palettes_data)); |
| 693 |
2 |
| bess_core.background_palettes.offset = LE32(video_offset + offsetof(GB_gameboy_t, background_palettes_data) - GB_SECTION_OFFSET(video)); |
| 694 |
2 |
| bess_core.object_palettes.size = LE32(sizeof(gb->object_palettes_data)); |
| 695 |
2 |
| bess_core.object_palettes.offset = LE32(video_offset + offsetof(GB_gameboy_t, object_palettes_data) - GB_SECTION_OFFSET(video)); |
| 696 |
|
| } |
| 697 |
|
| |
| 698 |
2 |
▶ | if (file->write(file, &bess_core, sizeof(bess_core)) != sizeof(bess_core)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->write(file, &bess_core, sizeof(bess_core)) != sizeof(bess_core)) { |
0 |
2 |
2 |
|
| 699 |
0 |
| goto error; |
| 700 |
|
| } |
| 701 |
|
| |
| 702 |
|
| /* BESS XOAM */ |
| 703 |
|
| |
| 704 |
2 |
| BESS_XOAM_t bess_xoam = {0,}; |
| 705 |
2 |
| bess_xoam.header = (BESS_block_t){BE32('XOAM'), LE32(sizeof(bess_xoam) - sizeof(bess_xoam.header))}; |
| 706 |
2 |
▶ | if (GB_is_cgb(gb)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GB_is_cgb(gb)) { |
2 |
0 |
2 |
|
| 707 |
2 |
| memcpy(bess_xoam.extra_oam, gb->extra_oam, sizeof(bess_xoam.extra_oam)); |
| 708 |
|
| } |
| 709 |
|
| |
| 710 |
2 |
▶ | if (file->write(file, &bess_xoam, sizeof(bess_xoam)) != sizeof(bess_xoam)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->write(file, &bess_xoam, sizeof(bess_xoam)) != sizeof(bess_xoam)) { |
0 |
2 |
2 |
|
| 711 |
0 |
| goto error; |
| 712 |
|
| } |
| 713 |
|
| |
| 714 |
2 |
| save_bess_mbc_block(gb, file); |
| 715 |
2 |
▶ | if (gb->cartridge_type->has_rtc) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->cartridge_type->has_rtc) { |
0 |
2 |
2 |
|
| 716 |
0 |
▶ | if (gb->cartridge_type ->mbc_type == GB_TPP1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->cartridge_type ->mbc_type == GB_TPP1) { |
0 |
0 |
0 |
|
| 717 |
0 |
| BESS_TPP1_t bess_tpp1 = {0,}; |
| 718 |
0 |
| bess_tpp1.header = (BESS_block_t){BE32('TPP1'), LE32(sizeof(bess_tpp1) - sizeof(bess_tpp1.header))}; |
| 719 |
|
| |
| 720 |
0 |
| bess_tpp1.last_rtc_second = LE64(gb->last_rtc_second); |
| 721 |
0 |
▶ | unrolled for (unsigned i = 4; i--;) { |
Branch: 1 branch
| Branch | True | False | Total |
| unrolled for (unsigned i = 4; i--;) { |
0 |
0 |
0 |
|
| 722 |
0 |
| bess_tpp1.real_rtc_data[i] = gb->rtc_real.data[i ^ 3]; |
| 723 |
0 |
| bess_tpp1.latched_rtc_data[i] = gb->rtc_latched.data[i ^ 3]; |
| 724 |
|
| } |
| 725 |
0 |
| bess_tpp1.mr4 = gb->tpp1_mr4; |
| 726 |
|
| |
| 727 |
0 |
▶ | if (file->write(file, &bess_tpp1, sizeof(bess_tpp1)) != sizeof(bess_tpp1)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->write(file, &bess_tpp1, sizeof(bess_tpp1)) != sizeof(bess_tpp1)) { |
0 |
0 |
0 |
|
| 728 |
0 |
| goto error; |
| 729 |
|
| } |
| 730 |
|
| } |
| 731 |
0 |
▶ | else if (gb->cartridge_type ->mbc_type != GB_HUC3) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (gb->cartridge_type ->mbc_type != GB_HUC3) { |
0 |
0 |
0 |
|
| 732 |
0 |
| BESS_RTC_t bess_rtc = {0,}; |
| 733 |
0 |
| bess_rtc.header = (BESS_block_t){BE32('RTC '), LE32(sizeof(bess_rtc) - sizeof(bess_rtc.header))}; |
| 734 |
0 |
| bess_rtc.real.seconds = gb->rtc_real.seconds; |
| 735 |
0 |
| bess_rtc.real.minutes = gb->rtc_real.minutes; |
| 736 |
0 |
| bess_rtc.real.hours = gb->rtc_real.hours; |
| 737 |
0 |
| bess_rtc.real.days = gb->rtc_real.days; |
| 738 |
0 |
| bess_rtc.real.high = gb->rtc_real.high; |
| 739 |
0 |
| bess_rtc.latched.seconds = gb->rtc_latched.seconds; |
| 740 |
0 |
| bess_rtc.latched.minutes = gb->rtc_latched.minutes; |
| 741 |
0 |
| bess_rtc.latched.hours = gb->rtc_latched.hours; |
| 742 |
0 |
| bess_rtc.latched.days = gb->rtc_latched.days; |
| 743 |
0 |
| bess_rtc.latched.high = gb->rtc_latched.high; |
| 744 |
0 |
| bess_rtc.last_rtc_second = LE64(gb->last_rtc_second); |
| 745 |
0 |
▶ | if (file->write(file, &bess_rtc, sizeof(bess_rtc)) != sizeof(bess_rtc)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->write(file, &bess_rtc, sizeof(bess_rtc)) != sizeof(bess_rtc)) { |
0 |
0 |
0 |
|
| 746 |
0 |
| goto error; |
| 747 |
|
| } |
| 748 |
|
| } |
| 749 |
|
| else { |
| 750 |
0 |
| BESS_HUC3_t bess_huc3 = {0,}; |
| 751 |
0 |
| bess_huc3.header = (BESS_block_t){BE32('HUC3'), LE32(sizeof(bess_huc3) - sizeof(bess_huc3.header))}; |
| 752 |
|
| |
| 753 |
0 |
| bess_huc3.data = (GB_huc3_rtc_time_t) { |
| 754 |
0 |
| LE64(gb->last_rtc_second), |
| 755 |
0 |
| LE16(gb->huc3.minutes), |
| 756 |
0 |
| LE16(gb->huc3.days), |
| 757 |
0 |
| LE16(gb->huc3.alarm_minutes), |
| 758 |
0 |
| LE16(gb->huc3.alarm_days), |
| 759 |
0 |
| gb->huc3.alarm_enabled, |
| 760 |
|
| }; |
| 761 |
0 |
▶ | if (file->write(file, &bess_huc3, sizeof(bess_huc3)) != sizeof(bess_huc3)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->write(file, &bess_huc3, sizeof(bess_huc3)) != sizeof(bess_huc3)) { |
0 |
0 |
0 |
|
| 762 |
0 |
| goto error; |
| 763 |
|
| } |
| 764 |
|
| } |
| 765 |
|
| } |
| 766 |
|
| |
| 767 |
2 |
▶ | if (gb->cartridge_type ->mbc_type == GB_MBC7) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->cartridge_type ->mbc_type == GB_MBC7) { |
0 |
2 |
2 |
|
| 768 |
0 |
| BESS_MBC7_t bess_mbc7 = { |
| 769 |
0 |
| .latch_ready = gb->mbc7.latch_ready, |
| 770 |
0 |
| .eeprom_do = gb->mbc7.eeprom_do, |
| 771 |
0 |
| .eeprom_di = gb->mbc7.eeprom_di, |
| 772 |
0 |
| .eeprom_clk = gb->mbc7.eeprom_clk, |
| 773 |
0 |
| .eeprom_cs = gb->mbc7.eeprom_cs, |
| 774 |
0 |
| .eeprom_write_enabled = gb->mbc7.eeprom_write_enabled, |
| 775 |
|
| |
| 776 |
0 |
| .argument_bits_left = gb->mbc7.argument_bits_left, |
| 777 |
|
| |
| 778 |
0 |
| .eeprom_command = LE16(gb->mbc7.eeprom_command), |
| 779 |
0 |
| .read_bits = LE16(gb->mbc7.read_bits), |
| 780 |
|
| |
| 781 |
0 |
| .x_latch = LE16(gb->mbc7.x_latch), |
| 782 |
0 |
| .y_latch = LE16(gb->mbc7.y_latch), |
| 783 |
|
| }; |
| 784 |
0 |
| bess_mbc7.header = (BESS_block_t){BE32('MBC7'), LE32(sizeof(bess_mbc7) - sizeof(bess_mbc7.header))}; |
| 785 |
|
| |
| 786 |
0 |
▶ | if (file->write(file, &bess_mbc7, sizeof(bess_mbc7)) != sizeof(bess_mbc7)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->write(file, &bess_mbc7, sizeof(bess_mbc7)) != sizeof(bess_mbc7)) { |
0 |
0 |
0 |
|
| 787 |
0 |
| goto error; |
| 788 |
|
| } |
| 789 |
|
| } |
| 790 |
|
| |
| 791 |
2 |
| bool needs_sgb_padding = false; |
| 792 |
2 |
▶ | if (gb->sgb) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->sgb) { |
0 |
2 |
2 |
|
| 793 |
|
| /* BESS SGB */ |
| 794 |
0 |
▶ | if (gb->sgb->disable_commands) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->sgb->disable_commands) { |
0 |
0 |
0 |
|
| 795 |
0 |
| needs_sgb_padding = true; |
| 796 |
|
| } |
| 797 |
|
| else { |
| 798 |
0 |
| BESS_SGB_t bess_sgb = {{BE32('SGB '), LE32(sizeof(bess_sgb) - sizeof(bess_sgb.header))}, }; |
| 799 |
|
| |
| 800 |
0 |
| bess_sgb.border_tiles = (BESS_buffer_t){LE32(sizeof(gb->sgb->pending_border.tiles)), |
| 801 |
0 |
| LE32(sgb_offset + offsetof(GB_sgb_t, pending_border.tiles))}; |
| 802 |
0 |
| bess_sgb.border_tilemap = (BESS_buffer_t){LE32(sizeof(gb->sgb->pending_border.map)), |
| 803 |
0 |
| LE32(sgb_offset + offsetof(GB_sgb_t, pending_border.map))}; |
| 804 |
0 |
| bess_sgb.border_palettes = (BESS_buffer_t){LE32(sizeof(gb->sgb->pending_border.palette)), |
| 805 |
0 |
| LE32(sgb_offset + offsetof(GB_sgb_t, pending_border.palette))}; |
| 806 |
|
| |
| 807 |
0 |
| bess_sgb.active_palettes = (BESS_buffer_t){LE32(sizeof(gb->sgb->effective_palettes)), |
| 808 |
0 |
| LE32(sgb_offset + offsetof(GB_sgb_t, effective_palettes))}; |
| 809 |
0 |
| bess_sgb.ram_palettes = (BESS_buffer_t){LE32(sizeof(gb->sgb->ram_palettes)), |
| 810 |
0 |
| LE32(sgb_offset + offsetof(GB_sgb_t, ram_palettes))}; |
| 811 |
0 |
| bess_sgb.attribute_map = (BESS_buffer_t){LE32(sizeof(gb->sgb->attribute_map)), |
| 812 |
0 |
| LE32(sgb_offset + offsetof(GB_sgb_t, attribute_map))}; |
| 813 |
0 |
| bess_sgb.attribute_files = (BESS_buffer_t){LE32(sizeof(gb->sgb->attribute_files)), |
| 814 |
0 |
| LE32(sgb_offset + offsetof(GB_sgb_t, attribute_files))}; |
| 815 |
|
| |
| 816 |
0 |
| bess_sgb.multiplayer_state = (gb->sgb->player_count << 4) | gb->sgb->current_player; |
| 817 |
0 |
▶ | if (file->write(file, &bess_sgb, sizeof(bess_sgb)) != sizeof(bess_sgb)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->write(file, &bess_sgb, sizeof(bess_sgb)) != sizeof(bess_sgb)) { |
0 |
0 |
0 |
|
| 818 |
0 |
| goto error; |
| 819 |
|
| } |
| 820 |
|
| } |
| 821 |
|
| } |
| 822 |
|
| |
| 823 |
|
| /* BESS END */ |
| 824 |
|
| |
| 825 |
|
| static const BESS_block_t bess_end = {BE32('END '), 0}; |
| 826 |
|
| |
| 827 |
2 |
▶ | if (file->write(file, &bess_end, sizeof(bess_end)) != sizeof(bess_end)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->write(file, &bess_end, sizeof(bess_end)) != sizeof(bess_end)) { |
0 |
2 |
2 |
|
| 828 |
0 |
| goto error; |
| 829 |
|
| } |
| 830 |
|
| |
| 831 |
2 |
▶ | if (needs_sgb_padding) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (needs_sgb_padding) { |
0 |
2 |
2 |
|
| 832 |
|
| static const uint8_t sgb_padding[sizeof(BESS_SGB_t)] = {0,}; |
| 833 |
0 |
| file->write(file, sgb_padding, sizeof(sgb_padding)); |
| 834 |
|
| } |
| 835 |
|
| |
| 836 |
|
| /* BESS Footer */ |
| 837 |
|
| |
| 838 |
2 |
▶ | if (file->write(file, &bess_footer, sizeof(bess_footer)) != sizeof(bess_footer)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->write(file, &bess_footer, sizeof(bess_footer)) != sizeof(bess_footer)) { |
0 |
2 |
2 |
|
| 839 |
0 |
| goto error; |
| 840 |
|
| } |
| 841 |
|
| |
| 842 |
2 |
| return 0;; |
| 843 |
0 |
| error: |
| 844 |
0 |
▶ | if (errno == 0) return EIO; |
Branch: 1 branch
| Branch | True | False | Total |
| if (errno == 0) return EIO; |
0 |
0 |
0 |
|
| 845 |
0 |
| return errno; |
| 846 |
|
| } |
| 847 |
|
| |
| 848 |
0 |
| int GB_save_state(GB_gameboy_t *gb, const char *path) |
| 849 |
|
| { |
| 850 |
|
| GB_ASSERT_NOT_RUNNING(gb) |
| 851 |
0 |
| FILE *f = fopen(path, "wb"); |
| 852 |
0 |
▶ | if (!f) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!f) { |
0 |
0 |
0 |
|
| 853 |
0 |
| GB_log(gb, "Could not open save state: %s.\n", strerror(errno)); |
| 854 |
0 |
| return errno; |
| 855 |
|
| } |
| 856 |
0 |
| virtual_file_t file = { |
| 857 |
|
| .write = file_write, |
| 858 |
|
| .seek = file_seek, |
| 859 |
|
| .tell = file_tell, |
| 860 |
|
| .file = f, |
| 861 |
|
| }; |
| 862 |
0 |
| int ret = save_state_internal(gb, &file, true); |
| 863 |
0 |
| fclose(f); |
| 864 |
0 |
| return ret; |
| 865 |
|
| } |
| 866 |
|
| |
| 867 |
2 |
| void GB_save_state_to_buffer(GB_gameboy_t *gb, uint8_t *buffer) |
| 868 |
|
| { |
| 869 |
|
| GB_ASSERT_NOT_RUNNING(gb) |
| 870 |
2 |
| virtual_file_t file = { |
| 871 |
|
| .write = buffer_write, |
| 872 |
|
| .seek = buffer_seek, |
| 873 |
|
| .tell = buffer_tell, |
| 874 |
|
| .buffer = (uint8_t *)buffer, |
| 875 |
|
| .position = 0, |
| 876 |
|
| }; |
| 877 |
|
| |
| 878 |
2 |
| save_state_internal(gb, &file, true); |
| 879 |
|
| assert(file.position == GB_get_save_state_size(gb)); |
| 880 |
2 |
| } |
| 881 |
|
| |
| 882 |
120 |
| void GB_save_state_to_buffer_no_bess(GB_gameboy_t *gb, uint8_t *buffer) |
| 883 |
|
| { |
| 884 |
120 |
| virtual_file_t file = { |
| 885 |
|
| .write = buffer_write, |
| 886 |
|
| .seek = buffer_seek, |
| 887 |
|
| .tell = buffer_tell, |
| 888 |
|
| .buffer = (uint8_t *)buffer, |
| 889 |
|
| .position = 0, |
| 890 |
|
| }; |
| 891 |
|
| |
| 892 |
120 |
| save_state_internal(gb, &file, false); |
| 893 |
|
| assert(file.position == GB_get_save_state_size_no_bess(gb)); |
| 894 |
120 |
| } |
| 895 |
|
| |
| 896 |
27 |
| static bool read_section(virtual_file_t *file, void *dest, uint32_t size) |
| 897 |
|
| { |
| 898 |
27 |
| uint32_t saved_size = 0; |
| 899 |
27 |
▶ | if (file->read(file, &saved_size, sizeof(size)) != sizeof(size)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->read(file, &saved_size, sizeof(size)) != sizeof(size)) { |
0 |
27 |
27 |
|
| 900 |
0 |
| return false; |
| 901 |
|
| } |
| 902 |
|
| |
| 903 |
27 |
▶ | if (saved_size <= size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (saved_size <= size) { |
27 |
0 |
27 |
|
| 904 |
27 |
▶ | if (file->read(file, dest, saved_size) != saved_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->read(file, dest, saved_size) != saved_size) { |
0 |
27 |
27 |
|
| 905 |
0 |
| return false; |
| 906 |
|
| } |
| 907 |
|
| } |
| 908 |
|
| else { |
| 909 |
0 |
▶ | if (file->read(file, dest, size) != size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->read(file, dest, size) != size) { |
0 |
0 |
0 |
|
| 910 |
0 |
| return false; |
| 911 |
|
| } |
| 912 |
0 |
| file->seek(file, saved_size - size, SEEK_CUR); |
| 913 |
|
| } |
| 914 |
|
| |
| 915 |
27 |
| return true; |
| 916 |
|
| } |
| 917 |
|
| |
| 918 |
0 |
| static void read_bess_buffer(const BESS_buffer_t *buffer, virtual_file_t *file, uint8_t *dest, size_t max_size) |
| 919 |
|
| { |
| 920 |
0 |
| size_t pos = file->tell(file); |
| 921 |
0 |
| file->seek(file, LE32(buffer->offset), SEEK_SET); |
| 922 |
0 |
| file->read(file, dest, MIN(LE32(buffer->size), max_size)); |
| 923 |
0 |
| file->seek(file, pos, SEEK_SET); |
| 924 |
|
| |
| 925 |
0 |
▶ | if (LE32(buffer->size) < max_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (LE32(buffer->size) < max_size) { |
0 |
0 |
0 |
|
| 926 |
0 |
| memset(dest + LE32(buffer->size), 0, max_size - LE32(buffer->size)); |
| 927 |
|
| } |
| 928 |
0 |
| } |
| 929 |
|
| |
| 930 |
0 |
| static int load_bess_save(GB_gameboy_t *gb, virtual_file_t *file, bool is_sameboy) |
| 931 |
|
| { |
| 932 |
0 |
| char emulator_name[65] = {0,}; |
| 933 |
0 |
| file->seek(file, -sizeof(BESS_footer_t), SEEK_END); |
| 934 |
0 |
| BESS_footer_t footer = {0, }; |
| 935 |
0 |
| file->read(file, &footer, sizeof(footer)); |
| 936 |
0 |
▶ | if (footer.magic != BE32('BESS')) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (footer.magic != BE32('BESS')) { |
0 |
0 |
0 |
|
| 937 |
|
| // Not a BESS file |
| 938 |
0 |
▶ | if (!is_sameboy) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!is_sameboy) { |
0 |
0 |
0 |
|
| 939 |
0 |
| GB_log(gb, "The file is not a save state, or is from an incompatible operating system.\n"); |
| 940 |
|
| } |
| 941 |
0 |
| return -1; |
| 942 |
|
| } |
| 943 |
|
| |
| 944 |
|
| GB_gameboy_t save; |
| 945 |
0 |
| GB_init(&save, gb->model); |
| 946 |
0 |
| save.cartridge_type = gb->cartridge_type; |
| 947 |
|
| |
| 948 |
0 |
| file->seek(file, LE32(footer.start_offset), SEEK_SET); |
| 949 |
0 |
| bool found_core = false; |
| 950 |
0 |
| BESS_CORE_t core = {0,}; |
| 951 |
0 |
| bool found_sgb = false; |
| 952 |
0 |
| BESS_SGB_t sgb = {0,}; |
| 953 |
0 |
| while (true) { |
| 954 |
|
| BESS_block_t block; |
| 955 |
0 |
▶ | if (file->read(file, &block, sizeof(block)) != sizeof(block)) goto error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->read(file, &block, sizeof(block)) != sizeof(block)) goto error; |
0 |
0 |
0 |
|
| 956 |
0 |
| switch (block.magic) { |
| 957 |
0 |
| case BE32('CORE'): |
| 958 |
0 |
▶ | if (found_core) goto parse_error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (found_core) goto parse_error; |
0 |
0 |
0 |
|
| 959 |
0 |
| found_core = true; |
| 960 |
0 |
▶ | if (LE32(block.size) > sizeof(core) - sizeof(block)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (LE32(block.size) > sizeof(core) - sizeof(block)) { |
0 |
0 |
0 |
|
| 961 |
0 |
▶ | if (file->read(file, &core.header + 1, sizeof(core) - sizeof(block)) != sizeof(core) - sizeof(block)) goto error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->read(file, &core.header + 1, sizeof(core) - sizeof(block)) != sizeof(core) - sizeof(block)) goto error; |
0 |
0 |
0 |
|
| 962 |
0 |
| file->seek(file, LE32(block.size) - (sizeof(core) - sizeof(block)), SEEK_CUR); |
| 963 |
|
| } |
| 964 |
|
| else { |
| 965 |
0 |
▶ | if (file->read(file, &core.header + 1, LE32(block.size)) != LE32(block.size)) goto error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->read(file, &core.header + 1, LE32(block.size)) != LE32(block.size)) goto error; |
0 |
0 |
0 |
|
| 966 |
|
| } |
| 967 |
|
| |
| 968 |
0 |
▶ | if (core.major != LE16(1)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (core.major != LE16(1)) { |
0 |
0 |
0 |
|
| 969 |
0 |
| GB_log(gb, "This save state uses an incompatible version of the BESS specification"); |
| 970 |
0 |
| GB_free(&save); |
| 971 |
0 |
| return -1; |
| 972 |
|
| } |
| 973 |
|
| |
| 974 |
0 |
| switch (core.family) { |
| 975 |
0 |
| case 'C': |
| 976 |
0 |
▶ | if (!GB_is_cgb(&save)) goto wrong_model; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!GB_is_cgb(&save)) goto wrong_model; |
0 |
0 |
0 |
|
| 977 |
0 |
| break; |
| 978 |
0 |
| case 'S': |
| 979 |
0 |
▶ | if (!GB_is_sgb(&save)) goto wrong_model; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!GB_is_sgb(&save)) goto wrong_model; |
0 |
0 |
0 |
|
| 980 |
0 |
| break; |
| 981 |
0 |
| case 'G': |
| 982 |
0 |
▶ | if (GB_is_cgb(&save) || GB_is_sgb(&save)) goto wrong_model; |
Branch: 2 branches
| Branch | True | False | Total |
| if (GB_is_cgb(&save) || GB_is_sgb(&save)) goto wrong_model; |
0 |
0 |
0 |
| if (GB_is_cgb(&save) || GB_is_sgb(&save)) goto wrong_model; |
0 |
0 |
0 |
|
| 983 |
0 |
| break; |
| 984 |
|
| default: |
| 985 |
0 |
| wrong_model: |
| 986 |
0 |
| GB_log(gb, "The save state is for a different model. Try changing the emulated model.\n"); |
| 987 |
0 |
| GB_free(&save); |
| 988 |
0 |
| return -1; |
| 989 |
|
| } |
| 990 |
|
| |
| 991 |
|
| |
| 992 |
0 |
| save.pc = LE16(core.pc); |
| 993 |
0 |
| save.af = LE16(core.af); |
| 994 |
0 |
| save.bc = LE16(core.bc); |
| 995 |
0 |
| save.de = LE16(core.de); |
| 996 |
0 |
| save.hl = LE16(core.hl); |
| 997 |
0 |
| save.sp = LE16(core.sp); |
| 998 |
|
| |
| 999 |
0 |
| save.ime = core.ime; |
| 1000 |
0 |
| save.interrupt_enable = core.ie; |
| 1001 |
|
| |
| 1002 |
0 |
| save.halted = core.execution_mode == 1; |
| 1003 |
0 |
| save.stopped = core.execution_mode == 2; |
| 1004 |
|
| |
| 1005 |
|
| // Done early for compatibility with 0.14.x |
| 1006 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_SVBK, core.io_registers[GB_IO_SVBK]); |
| 1007 |
|
| // CPU related |
| 1008 |
|
| |
| 1009 |
|
| // Determines DMG mode |
| 1010 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_KEY0, core.io_registers[GB_IO_KEY0]); |
| 1011 |
0 |
| save.boot_rom_finished = core.io_registers[GB_IO_BANK]; |
| 1012 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_KEY1, core.io_registers[GB_IO_KEY1]); |
| 1013 |
0 |
▶ | if (save.cgb_mode) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (save.cgb_mode) { |
0 |
0 |
0 |
|
| 1014 |
0 |
| save.cgb_double_speed = core.io_registers[GB_IO_KEY1] & 0x80; |
| 1015 |
0 |
| save.object_priority = GB_OBJECT_PRIORITY_INDEX; |
| 1016 |
|
| } |
| 1017 |
|
| else { |
| 1018 |
0 |
| save.object_priority = GB_OBJECT_PRIORITY_X; |
| 1019 |
|
| } |
| 1020 |
|
| |
| 1021 |
|
| // Timers, Joypad and Serial |
| 1022 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_JOYP, core.io_registers[GB_IO_JOYP]); |
| 1023 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_SB, core.io_registers[GB_IO_SB]); |
| 1024 |
0 |
| save.io_registers[GB_IO_SC] = core.io_registers[GB_IO_SC]; |
| 1025 |
0 |
| save.div_counter = core.io_registers[GB_IO_DIV] << 8; |
| 1026 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_TIMA, core.io_registers[GB_IO_TIMA]); |
| 1027 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_TMA, core.io_registers[GB_IO_TMA]); |
| 1028 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_TAC, core.io_registers[GB_IO_TAC]); |
| 1029 |
|
| |
| 1030 |
|
| // APU |
| 1031 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_NR52, core.io_registers[GB_IO_NR52]); |
| 1032 |
0 |
▶ | for (unsigned i = GB_IO_NR10; i < GB_IO_NR52; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned i = GB_IO_NR10; i < GB_IO_NR52; i++) { |
0 |
0 |
0 |
|
| 1033 |
0 |
| uint8_t value = core.io_registers[i]; |
| 1034 |
0 |
▶ | if (i == GB_IO_NR14 || i == GB_IO_NR24 || i == GB_IO_NR34 || i == GB_IO_NR44) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (i == GB_IO_NR14 || i == GB_IO_NR24 || i == GB_IO_NR34 || i == GB_IO_NR44) { |
0 |
0 |
0 |
| if (i == GB_IO_NR14 || i == GB_IO_NR24 || i == GB_IO_NR34 || i == GB_IO_NR44) { |
0 |
0 |
0 |
| if (i == GB_IO_NR14 || i == GB_IO_NR24 || i == GB_IO_NR34 || i == GB_IO_NR44) { |
0 |
0 |
0 |
| if (i == GB_IO_NR14 || i == GB_IO_NR24 || i == GB_IO_NR34 || i == GB_IO_NR44) { |
0 |
0 |
0 |
|
| 1035 |
0 |
| value &= ~0x80; |
| 1036 |
|
| } |
| 1037 |
0 |
| GB_write_memory(&save, 0xFF00 + i, value); |
| 1038 |
|
| } |
| 1039 |
|
| |
| 1040 |
0 |
▶ | for (unsigned i = GB_IO_WAV_START; i <= GB_IO_WAV_END; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned i = GB_IO_WAV_START; i <= GB_IO_WAV_END; i++) { |
0 |
0 |
0 |
|
| 1041 |
0 |
| GB_write_memory(&save, 0xFF00 + i, core.io_registers[i]); |
| 1042 |
|
| } |
| 1043 |
|
| |
| 1044 |
|
| // PPU |
| 1045 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_LCDC, core.io_registers[GB_IO_LCDC]); |
| 1046 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_STAT, core.io_registers[GB_IO_STAT]); |
| 1047 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_SCY, core.io_registers[GB_IO_SCY]); |
| 1048 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_SCX, core.io_registers[GB_IO_SCX]); |
| 1049 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_LYC, core.io_registers[GB_IO_LYC]); |
| 1050 |
0 |
| save.io_registers[GB_IO_DMA] = core.io_registers[GB_IO_DMA]; |
| 1051 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_BGP, core.io_registers[GB_IO_BGP]); |
| 1052 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_OBP0, core.io_registers[GB_IO_OBP0]); |
| 1053 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_OBP1, core.io_registers[GB_IO_OBP1]); |
| 1054 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_WX, core.io_registers[GB_IO_WX]); |
| 1055 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_WY, core.io_registers[GB_IO_WY]); |
| 1056 |
|
| |
| 1057 |
|
| // Other registers |
| 1058 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_VBK, core.io_registers[GB_IO_VBK]); |
| 1059 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_HDMA1, core.io_registers[GB_IO_HDMA1]); |
| 1060 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_HDMA2, core.io_registers[GB_IO_HDMA2]); |
| 1061 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_HDMA3, core.io_registers[GB_IO_HDMA3]); |
| 1062 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_HDMA4, core.io_registers[GB_IO_HDMA4]); |
| 1063 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_RP, core.io_registers[GB_IO_RP]); |
| 1064 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_BGPI, core.io_registers[GB_IO_BGPI]); |
| 1065 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_OBPI, core.io_registers[GB_IO_OBPI]); |
| 1066 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_OPRI, core.io_registers[GB_IO_OPRI]); |
| 1067 |
|
| |
| 1068 |
|
| // Interrupts |
| 1069 |
0 |
| GB_write_memory(&save, 0xFF00 + GB_IO_IF, core.io_registers[GB_IO_IF]); |
| 1070 |
|
| |
| 1071 |
|
| /* Required to be compatible with both SameBoy 0.14.x AND BGB */ |
| 1072 |
0 |
▶ | if (GB_is_cgb(&save) && !save.cgb_mode && save.cgb_ram_bank == 7) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (GB_is_cgb(&save) && !save.cgb_mode && save.cgb_ram_bank == 7) { |
0 |
0 |
0 |
| if (GB_is_cgb(&save) && !save.cgb_mode && save.cgb_ram_bank == 7) { |
0 |
0 |
0 |
| if (GB_is_cgb(&save) && !save.cgb_mode && save.cgb_ram_bank == 7) { |
0 |
0 |
0 |
|
| 1073 |
0 |
| save.cgb_ram_bank = 1; |
| 1074 |
|
| } |
| 1075 |
|
| |
| 1076 |
0 |
| break; |
| 1077 |
0 |
| case BE32('NAME'): |
| 1078 |
0 |
▶ | if (LE32(block.size) > sizeof(emulator_name) - 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (LE32(block.size) > sizeof(emulator_name) - 1) { |
0 |
0 |
0 |
|
| 1079 |
0 |
| file->seek(file, LE32(block.size), SEEK_CUR); |
| 1080 |
|
| } |
| 1081 |
|
| else { |
| 1082 |
0 |
| file->read(file, emulator_name, LE32(block.size)); |
| 1083 |
|
| } |
| 1084 |
0 |
| break; |
| 1085 |
0 |
| case BE32('INFO'): { |
| 1086 |
0 |
| BESS_INFO_t bess_info = {0,}; |
| 1087 |
0 |
▶ | if (LE32(block.size) != sizeof(bess_info) - sizeof(block)) goto parse_error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (LE32(block.size) != sizeof(bess_info) - sizeof(block)) goto parse_error; |
0 |
0 |
0 |
|
| 1088 |
0 |
▶ | if (file->read(file, &bess_info.header + 1, LE32(block.size)) != LE32(block.size)) goto error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->read(file, &bess_info.header + 1, LE32(block.size)) != LE32(block.size)) goto error; |
0 |
0 |
0 |
|
| 1089 |
0 |
| const uint8_t *bank = get_header_bank(gb); |
| 1090 |
0 |
▶ | if (memcmp(bess_info.title, bank + 0x134, sizeof(bess_info.title))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(bess_info.title, bank + 0x134, sizeof(bess_info.title))) { |
0 |
0 |
0 |
|
| 1091 |
0 |
| char ascii_title[0x11] = {0,}; |
| 1092 |
0 |
▶ | for (unsigned i = 0; i < 0x10; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned i = 0; i < 0x10; i++) { |
0 |
0 |
0 |
|
| 1093 |
0 |
▶ | if (bess_info.title[i] < 0x20 || bess_info.title[i] > 0x7E) break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (bess_info.title[i] < 0x20 || bess_info.title[i] > 0x7E) break; |
0 |
0 |
0 |
| if (bess_info.title[i] < 0x20 || bess_info.title[i] > 0x7E) break; |
0 |
0 |
0 |
|
| 1094 |
0 |
| ascii_title[i] = bess_info.title[i]; |
| 1095 |
|
| } |
| 1096 |
0 |
| GB_log(gb, "Save state was made on another ROM: '%s'\n", ascii_title); |
| 1097 |
|
| } |
| 1098 |
0 |
▶ | else if (memcmp(bess_info.checksum, bank + 0x14E, 2)) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (memcmp(bess_info.checksum, bank + 0x14E, 2)) { |
0 |
0 |
0 |
|
| 1099 |
0 |
| GB_log(gb, "Save state was potentially made on another revision of the same ROM.\n"); |
| 1100 |
|
| } |
| 1101 |
0 |
| break; |
| 1102 |
|
| } |
| 1103 |
0 |
| case BE32('XOAM'): |
| 1104 |
0 |
▶ | if (!found_core) goto parse_error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!found_core) goto parse_error; |
0 |
0 |
0 |
|
| 1105 |
0 |
▶ | if (LE32(block.size) != 96) goto parse_error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (LE32(block.size) != 96) goto parse_error; |
0 |
0 |
0 |
|
| 1106 |
0 |
| file->read(file, save.extra_oam, sizeof(save.extra_oam)); |
| 1107 |
0 |
| break; |
| 1108 |
0 |
| case BE32('MBC '): |
| 1109 |
0 |
▶ | if (!found_core) goto parse_error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!found_core) goto parse_error; |
0 |
0 |
0 |
|
| 1110 |
0 |
▶ | if (LE32(block.size) % 3 != 0) goto parse_error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (LE32(block.size) % 3 != 0) goto parse_error; |
0 |
0 |
0 |
|
| 1111 |
0 |
▶ | if (LE32(block.size) > 0x1000) goto parse_error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (LE32(block.size) > 0x1000) goto parse_error; |
0 |
0 |
0 |
|
| 1112 |
|
| /* Inject some default writes, as some emulators omit them */ |
| 1113 |
0 |
▶ | if (gb->cartridge_type->mbc_type == GB_MMM01) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->cartridge_type->mbc_type == GB_MMM01) { |
0 |
0 |
0 |
|
| 1114 |
0 |
| GB_write_memory(&save, 0x6000, 0x30); |
| 1115 |
0 |
| GB_write_memory(&save, 0x4000, 0x70); |
| 1116 |
|
| } |
| 1117 |
0 |
▶ | for (unsigned i = LE32(block.size); i > 0; i -= 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned i = LE32(block.size); i > 0; i -= 3) { |
0 |
0 |
0 |
|
| 1118 |
|
| BESS_MBC_pair_t pair; |
| 1119 |
0 |
| file->read(file, &pair, sizeof(pair)); |
| 1120 |
0 |
▶ | if (LE16(pair.address) >= 0x8000 && LE16(pair.address) < 0xA000) goto parse_error; |
Branch: 2 branches
| Branch | True | False | Total |
| if (LE16(pair.address) >= 0x8000 && LE16(pair.address) < 0xA000) goto parse_error; |
0 |
0 |
0 |
| if (LE16(pair.address) >= 0x8000 && LE16(pair.address) < 0xA000) goto parse_error; |
0 |
0 |
0 |
|
| 1121 |
0 |
▶ | if (LE16(pair.address) >= 0xC000) goto parse_error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (LE16(pair.address) >= 0xC000) goto parse_error; |
0 |
0 |
0 |
|
| 1122 |
0 |
| GB_write_memory(&save, LE16(pair.address), pair.value); |
| 1123 |
|
| } |
| 1124 |
0 |
| break; |
| 1125 |
0 |
| case BE32('RTC '): |
| 1126 |
0 |
▶ | if (!found_core) goto parse_error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!found_core) goto parse_error; |
0 |
0 |
0 |
|
| 1127 |
|
| BESS_RTC_t bess_rtc; |
| 1128 |
0 |
▶ | if (LE32(block.size) != sizeof(bess_rtc) - sizeof(block)) goto parse_error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (LE32(block.size) != sizeof(bess_rtc) - sizeof(block)) goto parse_error; |
0 |
0 |
0 |
|
| 1129 |
0 |
▶ | if (file->read(file, &bess_rtc.header + 1, LE32(block.size)) != LE32(block.size)) goto error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->read(file, &bess_rtc.header + 1, LE32(block.size)) != LE32(block.size)) goto error; |
0 |
0 |
0 |
|
| 1130 |
0 |
▶ | if (!gb->cartridge_type->has_rtc || gb->cartridge_type->mbc_type != GB_MBC3) break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!gb->cartridge_type->has_rtc || gb->cartridge_type->mbc_type != GB_MBC3) break; |
0 |
0 |
0 |
| if (!gb->cartridge_type->has_rtc || gb->cartridge_type->mbc_type != GB_MBC3) break; |
0 |
0 |
0 |
|
| 1131 |
0 |
| save.rtc_real.seconds = bess_rtc.real.seconds; |
| 1132 |
0 |
| save.rtc_real.minutes = bess_rtc.real.minutes; |
| 1133 |
0 |
| save.rtc_real.hours = bess_rtc.real.hours; |
| 1134 |
0 |
| save.rtc_real.days = bess_rtc.real.days; |
| 1135 |
0 |
| save.rtc_real.high = bess_rtc.real.high; |
| 1136 |
0 |
| save.rtc_latched.seconds = bess_rtc.latched.seconds; |
| 1137 |
0 |
| save.rtc_latched.minutes = bess_rtc.latched.minutes; |
| 1138 |
0 |
| save.rtc_latched.hours = bess_rtc.latched.hours; |
| 1139 |
0 |
| save.rtc_latched.days = bess_rtc.latched.days; |
| 1140 |
0 |
| save.rtc_latched.high = bess_rtc.latched.high; |
| 1141 |
0 |
▶ | if (gb->rtc_mode == GB_RTC_MODE_SYNC_TO_HOST) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->rtc_mode == GB_RTC_MODE_SYNC_TO_HOST) { |
0 |
0 |
0 |
|
| 1142 |
0 |
| save.last_rtc_second = MIN(LE64(bess_rtc.last_rtc_second), time(NULL)); |
| 1143 |
|
| } |
| 1144 |
|
| |
| 1145 |
0 |
| break; |
| 1146 |
0 |
| case BE32('HUC3'): |
| 1147 |
0 |
▶ | if (!found_core) goto parse_error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!found_core) goto parse_error; |
0 |
0 |
0 |
|
| 1148 |
|
| BESS_HUC3_t bess_huc3; |
| 1149 |
0 |
▶ | if (LE32(block.size) != sizeof(bess_huc3) - sizeof(block)) goto parse_error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (LE32(block.size) != sizeof(bess_huc3) - sizeof(block)) goto parse_error; |
0 |
0 |
0 |
|
| 1150 |
0 |
▶ | if (file->read(file, &bess_huc3.header + 1, LE32(block.size)) != LE32(block.size)) goto error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->read(file, &bess_huc3.header + 1, LE32(block.size)) != LE32(block.size)) goto error; |
0 |
0 |
0 |
|
| 1151 |
0 |
▶ | if (gb->cartridge_type->mbc_type != GB_HUC3) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->cartridge_type->mbc_type != GB_HUC3) break; |
0 |
0 |
0 |
|
| 1152 |
0 |
▶ | if (gb->rtc_mode == GB_RTC_MODE_SYNC_TO_HOST) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->rtc_mode == GB_RTC_MODE_SYNC_TO_HOST) { |
0 |
0 |
0 |
|
| 1153 |
0 |
| save.last_rtc_second = MIN(LE64(bess_huc3.data.last_rtc_second), time(NULL)); |
| 1154 |
|
| } |
| 1155 |
0 |
| save.huc3.minutes = LE16(bess_huc3.data.minutes); |
| 1156 |
0 |
| save.huc3.days = LE16(bess_huc3.data.days); |
| 1157 |
0 |
| save.huc3.alarm_minutes = LE16(bess_huc3.data.alarm_minutes); |
| 1158 |
0 |
| save.huc3.alarm_days = LE16(bess_huc3.data.alarm_days); |
| 1159 |
0 |
| save.huc3.alarm_enabled = bess_huc3.data.alarm_enabled; |
| 1160 |
0 |
| break; |
| 1161 |
0 |
| case BE32('TPP1'): |
| 1162 |
0 |
▶ | if (!found_core) goto parse_error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!found_core) goto parse_error; |
0 |
0 |
0 |
|
| 1163 |
|
| BESS_TPP1_t bess_tpp1; |
| 1164 |
0 |
▶ | if (LE32(block.size) != sizeof(bess_tpp1) - sizeof(block)) goto parse_error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (LE32(block.size) != sizeof(bess_tpp1) - sizeof(block)) goto parse_error; |
0 |
0 |
0 |
|
| 1165 |
0 |
▶ | if (file->read(file, &bess_tpp1.header + 1, LE32(block.size)) != LE32(block.size)) goto error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->read(file, &bess_tpp1.header + 1, LE32(block.size)) != LE32(block.size)) goto error; |
0 |
0 |
0 |
|
| 1166 |
0 |
▶ | if (gb->cartridge_type->mbc_type != GB_TPP1) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->cartridge_type->mbc_type != GB_TPP1) break; |
0 |
0 |
0 |
|
| 1167 |
0 |
▶ | if (gb->rtc_mode == GB_RTC_MODE_SYNC_TO_HOST) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->rtc_mode == GB_RTC_MODE_SYNC_TO_HOST) { |
0 |
0 |
0 |
|
| 1168 |
0 |
| save.last_rtc_second = MIN(LE64(bess_tpp1.last_rtc_second), time(NULL)); |
| 1169 |
|
| } |
| 1170 |
0 |
▶ | unrolled for (unsigned i = 4; i--;) { |
Branch: 1 branch
| Branch | True | False | Total |
| unrolled for (unsigned i = 4; i--;) { |
0 |
0 |
0 |
|
| 1171 |
0 |
| save.rtc_real.data[i ^ 3] = bess_tpp1.real_rtc_data[i]; |
| 1172 |
0 |
| save.rtc_latched.data[i ^ 3] = bess_tpp1.latched_rtc_data[i]; |
| 1173 |
|
| } |
| 1174 |
0 |
| save.tpp1_mr4 = bess_tpp1.mr4; |
| 1175 |
0 |
| break; |
| 1176 |
0 |
| case BE32('MBC7'): |
| 1177 |
0 |
▶ | if (!found_core) goto parse_error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!found_core) goto parse_error; |
0 |
0 |
0 |
|
| 1178 |
|
| BESS_MBC7_t bess_mbc7; |
| 1179 |
0 |
▶ | if (LE32(block.size) != sizeof(bess_mbc7) - sizeof(block)) goto parse_error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (LE32(block.size) != sizeof(bess_mbc7) - sizeof(block)) goto parse_error; |
0 |
0 |
0 |
|
| 1180 |
0 |
▶ | if (file->read(file, &bess_mbc7.header + 1, LE32(block.size)) != LE32(block.size)) goto error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->read(file, &bess_mbc7.header + 1, LE32(block.size)) != LE32(block.size)) goto error; |
0 |
0 |
0 |
|
| 1181 |
0 |
▶ | if (gb->cartridge_type->mbc_type != GB_MBC7) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->cartridge_type->mbc_type != GB_MBC7) break; |
0 |
0 |
0 |
|
| 1182 |
|
| |
| 1183 |
0 |
| save.mbc7.latch_ready = bess_mbc7.latch_ready; |
| 1184 |
0 |
| save.mbc7.eeprom_do = bess_mbc7.eeprom_do; |
| 1185 |
0 |
| save.mbc7.eeprom_di = bess_mbc7.eeprom_di; |
| 1186 |
0 |
| save.mbc7.eeprom_clk = bess_mbc7.eeprom_clk; |
| 1187 |
0 |
| save.mbc7.eeprom_cs = bess_mbc7.eeprom_cs; |
| 1188 |
0 |
| save.mbc7.eeprom_write_enabled = bess_mbc7.eeprom_write_enabled; |
| 1189 |
|
| |
| 1190 |
0 |
| save.mbc7.argument_bits_left = bess_mbc7.argument_bits_left; |
| 1191 |
|
| |
| 1192 |
0 |
| save.mbc7.eeprom_command = LE16(bess_mbc7.eeprom_command); |
| 1193 |
0 |
| save.mbc7.read_bits = LE16(bess_mbc7.read_bits); |
| 1194 |
|
| |
| 1195 |
0 |
| save.mbc7.x_latch = LE16(bess_mbc7.x_latch); |
| 1196 |
0 |
| save.mbc7.y_latch = LE16(bess_mbc7.y_latch); |
| 1197 |
|
| |
| 1198 |
0 |
| break; |
| 1199 |
0 |
| case BE32('SGB '): |
| 1200 |
0 |
▶ | if (!found_core) goto parse_error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!found_core) goto parse_error; |
0 |
0 |
0 |
|
| 1201 |
0 |
▶ | if (!gb->sgb) goto parse_error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!gb->sgb) goto parse_error; |
0 |
0 |
0 |
|
| 1202 |
0 |
▶ | if (LE32(block.size) > sizeof(sgb) - sizeof(block)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (LE32(block.size) > sizeof(sgb) - sizeof(block)) { |
0 |
0 |
0 |
|
| 1203 |
0 |
▶ | if (file->read(file, &sgb.header + 1, sizeof(sgb) - sizeof(block)) != sizeof(sgb) - sizeof(block)) goto error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->read(file, &sgb.header + 1, sizeof(sgb) - sizeof(block)) != sizeof(sgb) - sizeof(block)) goto error; |
0 |
0 |
0 |
|
| 1204 |
0 |
| file->seek(file, LE32(block.size) - (sizeof(sgb) - sizeof(block)), SEEK_CUR); |
| 1205 |
|
| } |
| 1206 |
|
| else { |
| 1207 |
0 |
▶ | if (file->read(file, &sgb.header + 1, LE32(block.size)) != LE32(block.size)) goto error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->read(file, &sgb.header + 1, LE32(block.size)) != LE32(block.size)) goto error; |
0 |
0 |
0 |
|
| 1208 |
|
| } |
| 1209 |
0 |
| found_sgb = true; |
| 1210 |
0 |
| break; |
| 1211 |
0 |
| case BE32('END '): |
| 1212 |
0 |
▶ | if (!found_core) goto parse_error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!found_core) goto parse_error; |
0 |
0 |
0 |
|
| 1213 |
0 |
▶ | if (LE32(block.size) != 0) goto parse_error; |
Branch: 1 branch
| Branch | True | False | Total |
| if (LE32(block.size) != 0) goto parse_error; |
0 |
0 |
0 |
|
| 1214 |
0 |
| goto done; |
| 1215 |
0 |
| default: |
| 1216 |
0 |
| file->seek(file, LE32(block.size), SEEK_CUR); |
| 1217 |
0 |
| break; |
| 1218 |
|
| } |
| 1219 |
|
| } |
| 1220 |
0 |
| done: |
| 1221 |
0 |
| save.mbc_ram_size = gb->mbc_ram_size; |
| 1222 |
0 |
| memcpy(gb, &save, GB_SECTION_OFFSET(unsaved)); |
| 1223 |
|
| assert(GB_get_save_state_size(gb) == GB_get_save_state_size(&save)); |
| 1224 |
0 |
| GB_free(&save); |
| 1225 |
0 |
| read_bess_buffer(&core.ram, file, gb->ram, gb->ram_size); |
| 1226 |
0 |
| read_bess_buffer(&core.vram, file, gb->vram, gb->vram_size); |
| 1227 |
0 |
| read_bess_buffer(&core.mbc_ram, file, gb->mbc_ram, gb->mbc_ram_size); |
| 1228 |
0 |
| read_bess_buffer(&core.oam, file, gb->oam, sizeof(gb->oam)); |
| 1229 |
0 |
| read_bess_buffer(&core.hram, file, gb->hram, sizeof(gb->hram)); |
| 1230 |
0 |
| read_bess_buffer(&core.background_palettes, file, gb->background_palettes_data, sizeof(gb->background_palettes_data)); |
| 1231 |
0 |
| read_bess_buffer(&core.object_palettes, file, gb->object_palettes_data, sizeof(gb->object_palettes_data)); |
| 1232 |
0 |
▶ | if (gb->sgb) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->sgb) { |
0 |
0 |
0 |
|
| 1233 |
0 |
| memset(gb->sgb, 0, sizeof(*gb->sgb)); |
| 1234 |
0 |
| GB_sgb_load_default_data(gb); |
| 1235 |
0 |
▶ | if (gb->boot_rom_finished) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->boot_rom_finished) { |
0 |
0 |
0 |
|
| 1236 |
0 |
| gb->sgb->intro_animation = GB_SGB_INTRO_ANIMATION_LENGTH; |
| 1237 |
0 |
▶ | if (!found_sgb) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!found_sgb) { |
0 |
0 |
0 |
|
| 1238 |
0 |
| gb->sgb->disable_commands = true; |
| 1239 |
|
| } |
| 1240 |
|
| else { |
| 1241 |
0 |
| read_bess_buffer(&sgb.border_tiles, file, gb->sgb->border.tiles, sizeof(gb->sgb->border.tiles)); |
| 1242 |
0 |
| read_bess_buffer(&sgb.border_tilemap, file, (void *)gb->sgb->border.map, sizeof(gb->sgb->border.map)); |
| 1243 |
0 |
| read_bess_buffer(&sgb.border_palettes, file, (void *)gb->sgb->border.palette, sizeof(gb->sgb->border.palette)); |
| 1244 |
|
| |
| 1245 |
0 |
| read_bess_buffer(&sgb.active_palettes, file, (void *)gb->sgb->effective_palettes, sizeof(gb->sgb->effective_palettes)); |
| 1246 |
0 |
| read_bess_buffer(&sgb.ram_palettes, file, (void *)gb->sgb->ram_palettes, sizeof(gb->sgb->ram_palettes)); |
| 1247 |
0 |
| read_bess_buffer(&sgb.attribute_map, file, (void *)gb->sgb->attribute_map, sizeof(gb->sgb->attribute_map)); |
| 1248 |
0 |
| read_bess_buffer(&sgb.attribute_files, file, (void *)gb->sgb->attribute_files, sizeof(gb->sgb->attribute_files)); |
| 1249 |
|
| |
| 1250 |
0 |
| gb->sgb->effective_palettes[12] = gb->sgb->effective_palettes[8] = |
| 1251 |
0 |
| gb->sgb->effective_palettes[4] = gb->sgb->effective_palettes[0]; |
| 1252 |
|
| |
| 1253 |
0 |
| gb->sgb->player_count = sgb.multiplayer_state >> 4; |
| 1254 |
0 |
| gb->sgb->current_player = sgb.multiplayer_state & 0xF; |
| 1255 |
0 |
▶ | if (gb->sgb->player_count > 4 || gb->sgb->player_count == 3 || gb->sgb->player_count == 0) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (gb->sgb->player_count > 4 || gb->sgb->player_count == 3 || gb->sgb->player_count == 0) { |
0 |
0 |
0 |
| if (gb->sgb->player_count > 4 || gb->sgb->player_count == 3 || gb->sgb->player_count == 0) { |
0 |
0 |
0 |
| if (gb->sgb->player_count > 4 || gb->sgb->player_count == 3 || gb->sgb->player_count == 0) { |
0 |
0 |
0 |
|
| 1256 |
0 |
| gb->sgb->player_count = 1; |
| 1257 |
0 |
| gb->sgb->current_player = 0; |
| 1258 |
|
| } |
| 1259 |
|
| } |
| 1260 |
|
| } |
| 1261 |
|
| else { |
| 1262 |
|
| // Effectively reset if didn't finish the boot ROM |
| 1263 |
0 |
| gb->pc = 0; |
| 1264 |
|
| } |
| 1265 |
|
| } |
| 1266 |
0 |
▶ | if (emulator_name[0]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (emulator_name[0]) { |
0 |
0 |
0 |
|
| 1267 |
0 |
| GB_log(gb, "Save state imported from %s.\n", emulator_name); |
| 1268 |
|
| } |
| 1269 |
|
| else { |
| 1270 |
0 |
| GB_log(gb, "Save state imported from another emulator.\n"); // SameBoy always contains a NAME block |
| 1271 |
|
| } |
| 1272 |
0 |
▶ | for (unsigned i = 0; i < 32; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (unsigned i = 0; i < 32; i++) { |
0 |
0 |
0 |
|
| 1273 |
0 |
| GB_palette_changed(gb, false, i * 2); |
| 1274 |
0 |
| GB_palette_changed(gb, true, i * 2); |
| 1275 |
|
| } |
| 1276 |
0 |
| gb->apu_output.sample_fraction = 0; |
| 1277 |
0 |
| return 0; |
| 1278 |
0 |
| parse_error: |
| 1279 |
0 |
| errno = -1; |
| 1280 |
0 |
| error: |
| 1281 |
0 |
▶ | if (emulator_name[0]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (emulator_name[0]) { |
0 |
0 |
0 |
|
| 1282 |
0 |
| GB_log(gb, "Attempted to import a save state from %s, but the save state is invalid.\n", emulator_name); |
| 1283 |
|
| } |
| 1284 |
|
| else { |
| 1285 |
0 |
| GB_log(gb, "Attempted to import a save state from a different emulator or incompatible version, but the save state is invalid.\n"); |
| 1286 |
|
| } |
| 1287 |
0 |
| GB_free(&save); |
| 1288 |
0 |
| sanitize_state(gb); |
| 1289 |
0 |
| return errno; |
| 1290 |
|
| } |
| 1291 |
|
| |
| 1292 |
3 |
| static int load_state_internal(GB_gameboy_t *gb, virtual_file_t *file) |
| 1293 |
|
| { |
| 1294 |
|
| GB_gameboy_t save; |
| 1295 |
|
| |
| 1296 |
|
| /* Every unread value should be kept the same. */ |
| 1297 |
3 |
| memcpy(&save, gb, sizeof(save)); |
| 1298 |
|
| /* ...Except ram size, we use it to detect old saves with incorrect ram sizes */ |
| 1299 |
3 |
| save.ram_size = 0; |
| 1300 |
|
| |
| 1301 |
|
| |
| 1302 |
3 |
▶ | if (file->read(file, GB_GET_SECTION(&save, header), GB_SECTION_SIZE(header)) != GB_SECTION_SIZE(header)) return errno; |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->read(file, GB_GET_SECTION(&save, header), GB_SECTION_SIZE(header)) != GB_SECTION_SIZE(header)) return errno; |
0 |
3 |
3 |
|
| 1303 |
3 |
▶ | if (gb->magic != save.magic) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->magic != save.magic) { |
0 |
3 |
3 |
|
| 1304 |
0 |
| return load_bess_save(gb, file, false); |
| 1305 |
|
| } |
| 1306 |
|
| #define READ_SECTION(gb, file, section) read_section(file, GB_GET_SECTION(gb, section), GB_SECTION_SIZE(section)) |
| 1307 |
3 |
▶ | if (!READ_SECTION(&save, file, core_state)) return errno ?: EIO; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!READ_SECTION(&save, file, core_state)) return errno ?: EIO; |
0 |
3 |
3 |
| if (!READ_SECTION(&save, file, core_state)) return errno ?: EIO; |
0 |
0 |
0 |
|
| 1308 |
3 |
▶ | if (!READ_SECTION(&save, file, dma )) return errno ?: EIO; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!READ_SECTION(&save, file, dma )) return errno ?: EIO; |
0 |
3 |
3 |
| if (!READ_SECTION(&save, file, dma )) return errno ?: EIO; |
0 |
0 |
0 |
|
| 1309 |
3 |
▶ | if (!READ_SECTION(&save, file, mbc )) return errno ?: EIO; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!READ_SECTION(&save, file, mbc )) return errno ?: EIO; |
0 |
3 |
3 |
| if (!READ_SECTION(&save, file, mbc )) return errno ?: EIO; |
0 |
0 |
0 |
|
| 1310 |
3 |
▶ | if (!READ_SECTION(&save, file, hram )) return errno ?: EIO; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!READ_SECTION(&save, file, hram )) return errno ?: EIO; |
0 |
3 |
3 |
| if (!READ_SECTION(&save, file, hram )) return errno ?: EIO; |
0 |
0 |
0 |
|
| 1311 |
3 |
▶ | if (!READ_SECTION(&save, file, timing )) return errno ?: EIO; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!READ_SECTION(&save, file, timing )) return errno ?: EIO; |
0 |
3 |
3 |
| if (!READ_SECTION(&save, file, timing )) return errno ?: EIO; |
0 |
0 |
0 |
|
| 1312 |
3 |
▶ | if (!READ_SECTION(&save, file, apu )) return errno ?: EIO; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!READ_SECTION(&save, file, apu )) return errno ?: EIO; |
0 |
3 |
3 |
| if (!READ_SECTION(&save, file, apu )) return errno ?: EIO; |
0 |
0 |
0 |
|
| 1313 |
3 |
▶ | if (!READ_SECTION(&save, file, rtc )) return errno ?: EIO; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!READ_SECTION(&save, file, rtc )) return errno ?: EIO; |
0 |
3 |
3 |
| if (!READ_SECTION(&save, file, rtc )) return errno ?: EIO; |
0 |
0 |
0 |
|
| 1314 |
3 |
▶ | if (!READ_SECTION(&save, file, video )) return errno ?: EIO; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!READ_SECTION(&save, file, video )) return errno ?: EIO; |
0 |
3 |
3 |
| if (!READ_SECTION(&save, file, video )) return errno ?: EIO; |
0 |
0 |
0 |
|
| 1315 |
3 |
▶ | if (!READ_SECTION(&save, file, accessory )) return errno ?: EIO; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!READ_SECTION(&save, file, accessory )) return errno ?: EIO; |
0 |
3 |
3 |
| if (!READ_SECTION(&save, file, accessory )) return errno ?: EIO; |
0 |
0 |
0 |
|
| 1316 |
|
| |
| 1317 |
|
| |
| 1318 |
3 |
| bool attempt_bess = false; |
| 1319 |
3 |
▶ | if (!verify_and_update_state_compatibility(gb, &save, &attempt_bess)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!verify_and_update_state_compatibility(gb, &save, &attempt_bess)) { |
0 |
3 |
3 |
|
| 1320 |
0 |
▶ | if (attempt_bess) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (attempt_bess) { |
0 |
0 |
0 |
|
| 1321 |
0 |
| return load_bess_save(gb, file, true); |
| 1322 |
|
| } |
| 1323 |
0 |
| return errno; |
| 1324 |
|
| } |
| 1325 |
|
| |
| 1326 |
3 |
▶ | if (GB_is_hle_sgb(gb)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GB_is_hle_sgb(gb)) { |
0 |
3 |
3 |
|
| 1327 |
0 |
▶ | if (!read_section(file, gb->sgb, sizeof(*gb->sgb))) return errno ?: EIO; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!read_section(file, gb->sgb, sizeof(*gb->sgb))) return errno ?: EIO; |
0 |
0 |
0 |
| if (!read_section(file, gb->sgb, sizeof(*gb->sgb))) return errno ?: EIO; |
0 |
0 |
0 |
|
| 1328 |
|
| } |
| 1329 |
|
| |
| 1330 |
3 |
| memset(gb->mbc_ram + save.mbc_ram_size, 0xFF, gb->mbc_ram_size - save.mbc_ram_size); |
| 1331 |
3 |
▶ | if (file->read(file, gb->mbc_ram, save.mbc_ram_size) != save.mbc_ram_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->read(file, gb->mbc_ram, save.mbc_ram_size) != save.mbc_ram_size) { |
0 |
3 |
3 |
|
| 1332 |
0 |
▶ | return errno ?: EIO; |
Branch: 1 branch
| Branch | True | False | Total |
| return errno ?: EIO; |
0 |
0 |
0 |
|
| 1333 |
|
| } |
| 1334 |
|
| |
| 1335 |
3 |
▶ | if (file->read(file, gb->ram, gb->ram_size) != gb->ram_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->read(file, gb->ram, gb->ram_size) != gb->ram_size) { |
0 |
3 |
3 |
|
| 1336 |
0 |
▶ | return errno ?: EIO; |
Branch: 1 branch
| Branch | True | False | Total |
| return errno ?: EIO; |
0 |
0 |
0 |
|
| 1337 |
|
| } |
| 1338 |
|
| |
| 1339 |
|
| /* Fix for 0.11 save states that allocate twice the amount of RAM in CGB instances */ |
| 1340 |
3 |
| file->seek(file, save.ram_size - gb->ram_size, SEEK_CUR); |
| 1341 |
|
| |
| 1342 |
3 |
▶ | if (file->read(file, gb->vram, gb->vram_size) != gb->vram_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->read(file, gb->vram, gb->vram_size) != gb->vram_size) { |
0 |
3 |
3 |
|
| 1343 |
0 |
▶ | return errno ?: EIO; |
Branch: 1 branch
| Branch | True | False | Total |
| return errno ?: EIO; |
0 |
0 |
0 |
|
| 1344 |
|
| } |
| 1345 |
|
| |
| 1346 |
3 |
| uint32_t ram_size = gb->ram_size; |
| 1347 |
3 |
| uint32_t mbc_ram_size = gb->mbc_ram_size; |
| 1348 |
3 |
| memcpy(gb, &save, sizeof(save)); |
| 1349 |
3 |
| gb->ram_size = ram_size; |
| 1350 |
3 |
| gb->mbc_ram_size = mbc_ram_size; |
| 1351 |
|
| |
| 1352 |
3 |
| sanitize_state(gb); |
| 1353 |
3 |
| GB_rewind_invalidate_for_backstepping(gb); |
| 1354 |
3 |
| gb->apu_output.sample_fraction = 0; |
| 1355 |
3 |
| return 0; |
| 1356 |
|
| } |
| 1357 |
|
| |
| 1358 |
0 |
| int GB_load_state(GB_gameboy_t *gb, const char *path) |
| 1359 |
|
| { |
| 1360 |
|
| GB_ASSERT_NOT_RUNNING(gb) |
| 1361 |
0 |
| FILE *f = fopen(path, "rb"); |
| 1362 |
0 |
▶ | if (!f) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!f) { |
0 |
0 |
0 |
|
| 1363 |
0 |
| GB_log(gb, "Could not open save state: %s.\n", strerror(errno)); |
| 1364 |
0 |
| return errno; |
| 1365 |
|
| } |
| 1366 |
0 |
| virtual_file_t file = { |
| 1367 |
|
| .read = file_read, |
| 1368 |
|
| .seek = file_seek, |
| 1369 |
|
| .tell = file_tell, |
| 1370 |
|
| .file = f, |
| 1371 |
|
| }; |
| 1372 |
0 |
| int ret = load_state_internal(gb, &file); |
| 1373 |
0 |
| fclose(f); |
| 1374 |
0 |
| return ret; |
| 1375 |
|
| } |
| 1376 |
|
| |
| 1377 |
3 |
| int GB_load_state_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t length) |
| 1378 |
|
| { |
| 1379 |
|
| GB_ASSERT_NOT_RUNNING(gb) |
| 1380 |
3 |
| virtual_file_t file = { |
| 1381 |
|
| .read = buffer_read, |
| 1382 |
|
| .seek = buffer_seek, |
| 1383 |
|
| .tell = buffer_tell, |
| 1384 |
|
| .buffer = (uint8_t *)buffer, |
| 1385 |
|
| .position = 0, |
| 1386 |
|
| .size = length, |
| 1387 |
|
| }; |
| 1388 |
|
| |
| 1389 |
3 |
| return load_state_internal(gb, &file); |
| 1390 |
|
| } |
| 1391 |
|
| |
| 1392 |
0 |
| static int get_state_model_bess(virtual_file_t *file, GB_model_t *model) |
| 1393 |
|
| { |
| 1394 |
0 |
| file->seek(file, -sizeof(BESS_footer_t), SEEK_END); |
| 1395 |
0 |
| BESS_footer_t footer = {0, }; |
| 1396 |
0 |
| file->read(file, &footer, sizeof(footer)); |
| 1397 |
0 |
▶ | if (footer.magic != BE32('BESS')) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (footer.magic != BE32('BESS')) { |
0 |
0 |
0 |
|
| 1398 |
0 |
| return -1; |
| 1399 |
|
| } |
| 1400 |
|
| |
| 1401 |
0 |
| file->seek(file, LE32(footer.start_offset), SEEK_SET); |
| 1402 |
0 |
| while (true) { |
| 1403 |
|
| BESS_block_t block; |
| 1404 |
0 |
▶ | if (file->read(file, &block, sizeof(block)) != sizeof(block)) return errno; |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->read(file, &block, sizeof(block)) != sizeof(block)) return errno; |
0 |
0 |
0 |
|
| 1405 |
0 |
▶ | switch (block.magic) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch (block.magic) { |
0 |
0 |
0 |
|
| 1406 |
0 |
| case BE32('CORE'): { |
| 1407 |
0 |
| BESS_CORE_t core = {0,}; |
| 1408 |
0 |
▶ | if (LE32(block.size) > sizeof(core) - sizeof(block)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (LE32(block.size) > sizeof(core) - sizeof(block)) { |
0 |
0 |
0 |
|
| 1409 |
0 |
▶ | if (file->read(file, &core.header + 1, sizeof(core) - sizeof(block)) != sizeof(core) - sizeof(block)) return errno; |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->read(file, &core.header + 1, sizeof(core) - sizeof(block)) != sizeof(core) - sizeof(block)) return errno; |
0 |
0 |
0 |
|
| 1410 |
0 |
| file->seek(file, LE32(block.size) - (sizeof(core) - sizeof(block)), SEEK_CUR); |
| 1411 |
|
| } |
| 1412 |
|
| else { |
| 1413 |
0 |
▶ | if (file->read(file, &core.header + 1, LE32(block.size)) != LE32(block.size)) return errno; |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->read(file, &core.header + 1, LE32(block.size)) != LE32(block.size)) return errno; |
0 |
0 |
0 |
|
| 1414 |
|
| } |
| 1415 |
|
| |
| 1416 |
0 |
| switch (core.full_model) { |
| 1417 |
0 |
| case BE32('GDB '): *model = GB_MODEL_DMG_B; return 0; |
| 1418 |
0 |
| case BE32('GM '): *model = GB_MODEL_MGB; return 0; |
| 1419 |
0 |
| case BE32('SN '): *model = GB_MODEL_SGB_NTSC_NO_SFC; return 0; |
| 1420 |
0 |
| case BE32('SP '): *model = GB_MODEL_SGB_PAL; return 0; |
| 1421 |
0 |
| case BE32('S2 '): *model = GB_MODEL_SGB2; return 0; |
| 1422 |
0 |
| case BE32('CC0 '): *model = GB_MODEL_CGB_0; return 0; |
| 1423 |
0 |
| case BE32('CCA '): *model = GB_MODEL_CGB_A; return 0; |
| 1424 |
0 |
| case BE32('CCB '): *model = GB_MODEL_CGB_B; return 0; |
| 1425 |
0 |
| case BE32('CCC '): *model = GB_MODEL_CGB_C; return 0; |
| 1426 |
0 |
| case BE32('CCD '): *model = GB_MODEL_CGB_D; return 0; |
| 1427 |
0 |
| case BE32('CCE '): *model = GB_MODEL_CGB_E; return 0; |
| 1428 |
0 |
| case BE32('CAA '): *model = GB_MODEL_AGB_A; return 0; |
| 1429 |
|
| } |
| 1430 |
0 |
| return -1; |
| 1431 |
|
| |
| 1432 |
0 |
| default: |
| 1433 |
0 |
| file->seek(file, LE32(block.size), SEEK_CUR); |
| 1434 |
0 |
| break; |
| 1435 |
|
| } |
| 1436 |
|
| } |
| 1437 |
|
| } |
| 1438 |
|
| return -1; |
| 1439 |
|
| } |
| 1440 |
|
| |
| 1441 |
|
| |
| 1442 |
0 |
| static int get_state_model_internal(virtual_file_t *file, GB_model_t *model) |
| 1443 |
|
| { |
| 1444 |
|
| GB_gameboy_t save; |
| 1445 |
|
| |
| 1446 |
|
| |
| 1447 |
0 |
▶ | if (file->read(file, GB_GET_SECTION(&save, header), GB_SECTION_SIZE(header)) != GB_SECTION_SIZE(header)) return errno; |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->read(file, GB_GET_SECTION(&save, header), GB_SECTION_SIZE(header)) != GB_SECTION_SIZE(header)) return errno; |
0 |
0 |
0 |
|
| 1448 |
0 |
▶ | if (save.magic != GB_state_magic()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (save.magic != GB_state_magic()) { |
0 |
0 |
0 |
|
| 1449 |
0 |
| return get_state_model_bess(file, model); |
| 1450 |
|
| } |
| 1451 |
0 |
▶ | if (!READ_SECTION(&save, file, core_state)) return errno ?: EIO; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!READ_SECTION(&save, file, core_state)) return errno ?: EIO; |
0 |
0 |
0 |
| if (!READ_SECTION(&save, file, core_state)) return errno ?: EIO; |
0 |
0 |
0 |
|
| 1452 |
0 |
| *model = save.model; |
| 1453 |
0 |
| return 0; |
| 1454 |
|
| } |
| 1455 |
|
| |
| 1456 |
0 |
| int GB_get_state_model(const char *path, GB_model_t *model) |
| 1457 |
|
| { |
| 1458 |
0 |
| FILE *f = fopen(path, "rb"); |
| 1459 |
0 |
▶ | if (!f) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!f) { |
0 |
0 |
0 |
|
| 1460 |
0 |
| return errno; |
| 1461 |
|
| } |
| 1462 |
0 |
| virtual_file_t file = { |
| 1463 |
|
| .read = file_read, |
| 1464 |
|
| .seek = file_seek, |
| 1465 |
|
| .tell = file_tell, |
| 1466 |
|
| .file = f, |
| 1467 |
|
| }; |
| 1468 |
0 |
| int ret = get_state_model_internal(&file, model); |
| 1469 |
0 |
| fclose(f); |
| 1470 |
0 |
| return ret; |
| 1471 |
|
| } |
| 1472 |
|
| |
| 1473 |
0 |
| int GB_get_state_model_from_buffer(const uint8_t *buffer, size_t length, GB_model_t *model) |
| 1474 |
|
| { |
| 1475 |
0 |
| virtual_file_t file = { |
| 1476 |
|
| .read = buffer_read, |
| 1477 |
|
| .seek = buffer_seek, |
| 1478 |
|
| .tell = buffer_tell, |
| 1479 |
|
| .buffer = (uint8_t *)buffer, |
| 1480 |
|
| .position = 0, |
| 1481 |
|
| .size = length, |
| 1482 |
|
| }; |
| 1483 |
|
| |
| 1484 |
0 |
| return get_state_model_internal(&file, model); |
| 1485 |
|
| } |
| 1486 |
|
| |
| 1487 |
|
| |
| 1488 |
0 |
| bool GB_is_save_state(const char *path) |
| 1489 |
|
| { |
| 1490 |
0 |
| bool ret = false; |
| 1491 |
0 |
| FILE *f = fopen(path, "rb"); |
| 1492 |
0 |
▶ | if (!f) return false; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!f) return false; |
0 |
0 |
0 |
|
| 1493 |
0 |
| uint32_t magic = 0; |
| 1494 |
0 |
| fread(&magic, sizeof(magic), 1, f); |
| 1495 |
0 |
▶ | if (magic == GB_state_magic()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (magic == GB_state_magic()) { |
0 |
0 |
0 |
|
| 1496 |
0 |
| ret = true; |
| 1497 |
0 |
| goto exit; |
| 1498 |
|
| } |
| 1499 |
|
| |
| 1500 |
0 |
| fseek(f, -sizeof(magic), SEEK_END); |
| 1501 |
0 |
| fread(&magic, sizeof(magic), 1, f); |
| 1502 |
0 |
▶ | if (magic == BE32('BESS')) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (magic == BE32('BESS')) { |
0 |
0 |
0 |
|
| 1503 |
0 |
| ret = true; |
| 1504 |
|
| } |
| 1505 |
|
| |
| 1506 |
0 |
| exit: |
| 1507 |
0 |
| fclose(f); |
| 1508 |
0 |
| return ret; |
| 1509 |
|
| } |