src/mess/machine/nes.c
| r7957 | r7958 | |
| 15 | 15 | /* Set to dump info about the inputs to the errorlog */ |
| 16 | 16 | #define LOG_JOY 0 |
| 17 | 17 | |
| 18 | | /* Set to generate prg (16k) & chr (8k) chunk files when the cart is loaded */ |
| 18 | /* Set to generate prg & chr files when the cart is loaded */ |
| 19 | 19 | #define SPLIT_PRG 0 |
| 20 | 20 | #define SPLIT_CHR 0 |
| 21 | 21 | |
| r7957 | r7958 | |
| 168 | 168 | { |
| 169 | 169 | nes_state *state = (nes_state *)machine->driver_data; |
| 170 | 170 | |
| 171 | | state->ppu = devtag_get_device(machine, "ppu"); |
| 172 | | state->sound = devtag_get_device(machine, "nessound"); |
| 173 | | state->cart = devtag_get_device(machine, "cart"); |
| 174 | | |
| 175 | 171 | /* Some carts have extra RAM and require it on at startup, e.g. Metroid */ |
| 176 | 172 | state->mid_ram_enable = 1; |
| 177 | 173 | |
| r7957 | r7958 | |
| 189 | 185 | devtag_get_device(machine, "maincpu")->reset(); |
| 190 | 186 | } |
| 191 | 187 | |
| 192 | | emu_timer *nes_irq_timer; |
| 193 | | |
| 194 | 188 | static TIMER_CALLBACK( nes_irq_callback ) |
| 195 | 189 | { |
| 196 | | cputag_set_input_line(machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE); |
| 197 | | timer_adjust_oneshot(nes_irq_timer, attotime_never, 0); |
| 190 | nes_state *state = (nes_state *)machine->driver_data; |
| 191 | cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE); |
| 192 | timer_adjust_oneshot(state->irq_timer, attotime_never, 0); |
| 198 | 193 | } |
| 199 | 194 | |
| 195 | static void nes_state_register( running_machine *machine ) |
| 196 | { |
| 197 | nes_state *state = (nes_state *)machine->driver_data; |
| 198 | |
| 199 | state_save_register_global(machine, state->MMC5_floodtile); |
| 200 | state_save_register_global(machine, state->MMC5_floodattr); |
| 201 | state_save_register_global_array(machine, state->mmc5_vram); |
| 202 | state_save_register_global(machine, state->mmc5_vram_control); |
| 203 | |
| 204 | state_save_register_global_array(machine, state->nes_vram_sprite); |
| 205 | state_save_register_global(machine, state->last_frame_flip); |
| 206 | state_save_register_global(machine, state->scanlines_per_frame); |
| 207 | state_save_register_global(machine, state->mid_ram_enable); |
| 208 | |
| 209 | // shared mapper variables |
| 210 | state_save_register_global(machine, state->IRQ_enable); |
| 211 | state_save_register_global(machine, state->IRQ_enable_latch); |
| 212 | state_save_register_global(machine, state->IRQ_count); |
| 213 | state_save_register_global(machine, state->IRQ_count_latch); |
| 214 | state_save_register_global(machine, state->IRQ_toggle); |
| 215 | state_save_register_global(machine, state->IRQ_reset); |
| 216 | state_save_register_global(machine, state->IRQ_status); |
| 217 | state_save_register_global(machine, state->IRQ_mode); |
| 218 | state_save_register_global(machine, state->mult1); |
| 219 | state_save_register_global(machine, state->mult2); |
| 220 | state_save_register_global(machine, state->mmc_chr_source); |
| 221 | state_save_register_global(machine, state->mmc_cmd1); |
| 222 | state_save_register_global(machine, state->mmc_cmd2); |
| 223 | state_save_register_global(machine, state->mmc_count); |
| 224 | state_save_register_global(machine, state->mmc_prg_base); |
| 225 | state_save_register_global(machine, state->mmc_prg_mask); |
| 226 | state_save_register_global(machine, state->mmc_chr_base); |
| 227 | state_save_register_global(machine, state->mmc_chr_mask); |
| 228 | state_save_register_global_array(machine, state->prg_bank); |
| 229 | state_save_register_global_array(machine, state->vrom_bank); |
| 230 | state_save_register_global_array(machine, state->extra_bank); |
| 231 | |
| 232 | state_save_register_global(machine, state->fds_motor_on); |
| 233 | state_save_register_global(machine, state->fds_door_closed); |
| 234 | state_save_register_global(machine, state->fds_current_side); |
| 235 | state_save_register_global(machine, state->fds_head_position); |
| 236 | state_save_register_global(machine, state->fds_status0); |
| 237 | state_save_register_global(machine, state->fds_read_mode); |
| 238 | state_save_register_global(machine, state->fds_write_reg); |
| 239 | state_save_register_global(machine, state->fds_last_side); |
| 240 | state_save_register_global(machine, state->fds_count); |
| 241 | } |
| 242 | |
| 200 | 243 | MACHINE_START( nes ) |
| 201 | 244 | { |
| 245 | nes_state *state = (nes_state *)machine->driver_data; |
| 246 | |
| 202 | 247 | init_nes_core(machine); |
| 203 | 248 | add_exit_callback(machine, nes_machine_stop); |
| 204 | 249 | |
| 205 | | nes_irq_timer = timer_alloc(machine, nes_irq_callback, NULL); |
| 250 | state->maincpu = devtag_get_device(machine, "maincpu"); |
| 251 | state->ppu = devtag_get_device(machine, "ppu"); |
| 252 | state->sound = devtag_get_device(machine, "nessound"); |
| 253 | state->cart = devtag_get_device(machine, "cart"); |
| 254 | |
| 255 | state->irq_timer = timer_alloc(machine, nes_irq_callback, NULL); |
| 256 | nes_state_register(machine); |
| 206 | 257 | } |
| 207 | 258 | |
| 208 | 259 | static void nes_machine_stop( running_machine *machine ) |
| r7957 | r7958 | |
| 414 | 465 | |
| 415 | 466 | if (cfg & 0x0f00) |
| 416 | 467 | state->in_0.i0 |= (state->in_2.i0 << 8) | (0x08 << 16); |
| 468 | |
| 417 | 469 | if (cfg & 0xf000) |
| 418 | 470 | state->in_1.i0 |= (state->in_3.i0 << 8) | (0x04 << 16); |
| 419 | 471 | } |
| 420 | 472 | |
| 421 | 473 | |
| 422 | | |
| 423 | 474 | WRITE8_HANDLER( nes_IN1_w ) |
| 424 | 475 | { |
| 425 | 476 | } |
| 426 | 477 | |
| 427 | 478 | |
| 428 | | |
| 429 | 479 | DEVICE_IMAGE_LOAD( nes_cart ) |
| 430 | 480 | { |
| 431 | 481 | nes_state *state = (nes_state *)image->machine->driver_data; |
| r7957 | r7958 | |
| 437 | 487 | char m; |
| 438 | 488 | int i; |
| 439 | 489 | |
| 440 | | /* Check first 4 bytes of the image to decide if it is UNIF or iNES */ |
| 441 | | /* Unfortunately, many .unf files have been released as .nes, so we cannot rely on extensions only */ |
| 442 | | memset(magic, '\0', sizeof(magic)); |
| 443 | | image_fread(image, magic, 4); |
| 444 | | |
| 445 | | if ((magic[0] == 'N') && (magic[1] == 'E') && (magic[2] == 'S')) /* If header starts with 'NES' it is iNES */ |
| 490 | if (image_software_entry(image) == NULL) |
| 446 | 491 | { |
| 447 | | state->format = 1; // we use this to select between mapper_reset / unif_reset |
| 448 | | |
| 449 | | mapinfo = image_extrainfo(image); |
| 450 | | |
| 451 | | if (mapinfo) |
| 492 | /* Check first 4 bytes of the image to decide if it is UNIF or iNES */ |
| 493 | /* Unfortunately, many .unf files have been released as .nes, so we cannot rely on extensions only */ |
| 494 | memset(magic, '\0', sizeof(magic)); |
| 495 | image_fread(image, magic, 4); |
| 496 | |
| 497 | if ((magic[0] == 'N') && (magic[1] == 'E') && (magic[2] == 'S')) /* If header starts with 'NES' it is iNES */ |
| 452 | 498 | { |
| 453 | | if (4 == sscanf(mapinfo,"%d %d %d %d", &mapint1, &mapint2, &mapint3, &mapint4)) |
| 499 | state->format = 1; // we use this to select between mapper_reset / unif_reset |
| 500 | |
| 501 | mapinfo = image_extrainfo(image); |
| 502 | |
| 503 | if (mapinfo) |
| 454 | 504 | { |
| 455 | | state->mapper = mapint1; |
| 456 | | local_options = mapint2 & 0x0f; |
| 457 | | state->crc_hack = mapint2 & 0xf0; // this is used to differentiate among variants of the same Mapper (see nes_mmc.c) |
| 458 | | state->prg_chunks = mapint3; |
| 459 | | state->chr_chunks = mapint4; |
| 460 | | logerror("NES.HSI info: %d %d %d %d\n", mapint1, mapint2, mapint3, mapint4); |
| 461 | | goodcrcinfo = 1; |
| 505 | if (4 == sscanf(mapinfo,"%d %d %d %d", &mapint1, &mapint2, &mapint3, &mapint4)) |
| 506 | { |
| 507 | state->mapper = mapint1; |
| 508 | local_options = mapint2 & 0x0f; |
| 509 | state->crc_hack = mapint2 & 0xf0; // this is used to differentiate among variants of the same Mapper (see nes_mmc.c) |
| 510 | state->prg_chunks = mapint3; |
| 511 | state->chr_chunks = mapint4; |
| 512 | logerror("NES.HSI info: %d %d %d %d\n", mapint1, mapint2, mapint3, mapint4); |
| 513 | goodcrcinfo = 1; |
| 514 | } |
| 515 | else |
| 516 | { |
| 517 | logerror("NES: [%s], Invalid mapinfo found\n", mapinfo); |
| 518 | } |
| 462 | 519 | } |
| 463 | 520 | else |
| 464 | 521 | { |
| 465 | | logerror("NES: [%s], Invalid mapinfo found\n",mapinfo); |
| 522 | logerror("NES: No extrainfo found\n"); |
| 466 | 523 | } |
| 467 | | } |
| 468 | | else |
| 469 | | { |
| 470 | | logerror("NES: No extrainfo found\n"); |
| 471 | | } |
| 472 | | |
| 473 | | if (!goodcrcinfo) |
| 474 | | { |
| 475 | | // image_extrainfo() resets the file position back to start. |
| 476 | | // Let's skip past the magic header once again. |
| 477 | | image_fseek(image, 4, SEEK_SET); |
| 478 | | |
| 479 | | image_fread(image, &state->prg_chunks, 1); |
| 480 | | image_fread(image, &state->chr_chunks, 1); |
| 481 | | /* Read the first ROM option byte (offset 6) */ |
| 482 | | image_fread(image, &m, 1); |
| 483 | | |
| 484 | | /* Interpret the iNES header flags */ |
| 485 | | state->mapper = (m & 0xf0) >> 4; |
| 486 | | local_options = m & 0x0f; |
| 487 | | |
| 488 | | /* Read the second ROM option byte (offset 7) */ |
| 489 | | image_fread(image, &m, 1); |
| 490 | | |
| 491 | | /* Check for skanky headers */ |
| 492 | | image_fread(image, &skank, 8); |
| 493 | | |
| 494 | | /* If the header has junk in the unused bytes, assume the extra mapper byte is also invalid */ |
| 495 | | /* We only check the first 4 unused bytes for now */ |
| 496 | | for (i = 0; i < 4; i ++) |
| 524 | |
| 525 | if (!goodcrcinfo) |
| 497 | 526 | { |
| 498 | | logerror("%02x ", skank[i]); |
| 499 | | if (skank[i] != 0x00) |
| 527 | // image_extrainfo() resets the file position back to start. |
| 528 | // Let's skip past the magic header once again. |
| 529 | image_fseek(image, 4, SEEK_SET); |
| 530 | |
| 531 | image_fread(image, &state->prg_chunks, 1); |
| 532 | image_fread(image, &state->chr_chunks, 1); |
| 533 | /* Read the first ROM option byte (offset 6) */ |
| 534 | image_fread(image, &m, 1); |
| 535 | |
| 536 | /* Interpret the iNES header flags */ |
| 537 | state->mapper = (m & 0xf0) >> 4; |
| 538 | local_options = m & 0x0f; |
| 539 | |
| 540 | /* Read the second ROM option byte (offset 7) */ |
| 541 | image_fread(image, &m, 1); |
| 542 | |
| 543 | /* Check for skanky headers */ |
| 544 | image_fread(image, &skank, 8); |
| 545 | |
| 546 | /* If the header has junk in the unused bytes, assume the extra mapper byte is also invalid */ |
| 547 | /* We only check the first 4 unused bytes for now */ |
| 548 | for (i = 0; i < 4; i ++) |
| 500 | 549 | { |
| 501 | | logerror("(skank: %d)", i); |
| 550 | logerror("%02x ", skank[i]); |
| 551 | if (skank[i] != 0x00) |
| 552 | { |
| 553 | logerror("(skank: %d)", i); |
| 554 | } |
| 502 | 555 | } |
| 556 | logerror("\n"); |
| 557 | |
| 558 | state->mapper = state->mapper | (m & 0xf0); |
| 503 | 559 | } |
| 504 | | logerror("\n"); |
| 505 | | |
| 506 | | state->mapper = state->mapper | (m & 0xf0); |
| 507 | | } |
| 508 | | |
| 509 | | state->hard_mirroring = local_options & 0x01; |
| 510 | | state->battery = local_options & 0x02; |
| 511 | | state->trainer = local_options & 0x04; |
| 512 | | state->four_screen_vram = local_options & 0x08; |
| 513 | | |
| 514 | | if (state->battery) |
| 515 | | logerror("-- Battery found\n"); |
| 516 | | |
| 517 | | if (state->trainer) |
| 518 | | logerror("-- Trainer found\n"); |
| 519 | | |
| 520 | | if (state->four_screen_vram) |
| 521 | | logerror("-- 4-screen VRAM\n"); |
| 522 | | |
| 523 | | /* Free the regions that were allocated by the ROM loader */ |
| 524 | | memory_region_free(image->machine, "maincpu"); |
| 525 | | memory_region_free(image->machine, "gfx1"); |
| 526 | | |
| 527 | | /* Allocate them again with the proper size */ |
| 528 | | memory_region_alloc(image->machine, "maincpu", 0x10000 + (state->prg_chunks + 1) * 0x4000,0); |
| 529 | | if (state->chr_chunks) |
| 530 | | memory_region_alloc(image->machine, "gfx1", state->chr_chunks * 0x2000,0); |
| 531 | | |
| 532 | | state->rom = memory_region(image->machine, "maincpu"); |
| 533 | | state->vrom = memory_region(image->machine, "gfx1"); |
| 534 | | state->vram = memory_region(image->machine, "gfx2"); |
| 535 | | state->wram = memory_region(image->machine, "user1"); |
| 536 | | |
| 537 | | /* Position past the header */ |
| 538 | | image_fseek(image, 16, SEEK_SET); |
| 539 | | |
| 540 | | /* Load the 0x200 byte trainer at 0x7000 if it exists */ |
| 541 | | if (state->trainer) |
| 542 | | image_fread(image, &state->wram[0x1000], 0x200); |
| 543 | | |
| 544 | | /* Read in the program chunks */ |
| 545 | | if (state->prg_chunks == 1) |
| 546 | | { |
| 547 | | image_fread(image, &state->rom[0x14000], 0x4000); |
| 548 | | /* Mirror this bank into $8000 */ |
| 549 | | memcpy(&state->rom[0x10000], &state->rom [0x14000], 0x4000); |
| 550 | | } |
| 551 | | else |
| 552 | | image_fread(image, &state->rom[0x10000], 0x4000 * state->prg_chunks); |
| 553 | | |
| 560 | |
| 561 | state->hard_mirroring = local_options & 0x01; |
| 562 | state->battery = local_options & 0x02; |
| 563 | state->trainer = local_options & 0x04; |
| 564 | state->four_screen_vram = local_options & 0x08; |
| 565 | |
| 566 | if (state->battery) |
| 567 | logerror("-- Battery found\n"); |
| 568 | |
| 569 | if (state->trainer) |
| 570 | logerror("-- Trainer found\n"); |
| 571 | |
| 572 | if (state->four_screen_vram) |
| 573 | logerror("-- 4-screen VRAM\n"); |
| 574 | |
| 575 | /* Free the regions that were allocated by the ROM loader */ |
| 576 | memory_region_free(image->machine, "maincpu"); |
| 577 | memory_region_free(image->machine, "gfx1"); |
| 578 | |
| 579 | /* Allocate them again with the proper size */ |
| 580 | memory_region_alloc(image->machine, "maincpu", 0x10000 + (state->prg_chunks + 1) * 0x4000, 0); |
| 581 | if (state->chr_chunks) |
| 582 | memory_region_alloc(image->machine, "gfx1", state->chr_chunks * 0x2000, 0); |
| 583 | |
| 584 | state->rom = memory_region(image->machine, "maincpu"); |
| 585 | state->vrom = memory_region(image->machine, "gfx1"); |
| 586 | state->vram = memory_region(image->machine, "gfx2"); |
| 587 | state->wram = memory_region(image->machine, "user1"); |
| 588 | |
| 589 | /* Position past the header */ |
| 590 | image_fseek(image, 16, SEEK_SET); |
| 591 | |
| 592 | /* Load the 0x200 byte trainer at 0x7000 if it exists */ |
| 593 | if (state->trainer) |
| 594 | image_fread(image, &state->wram[0x1000], 0x200); |
| 595 | |
| 596 | /* Read in the program chunks */ |
| 597 | if (state->prg_chunks == 1) |
| 598 | { |
| 599 | image_fread(image, &state->rom[0x14000], 0x4000); |
| 600 | /* Mirror this bank into $8000 */ |
| 601 | memcpy(&state->rom[0x10000], &state->rom [0x14000], 0x4000); |
| 602 | } |
| 603 | else |
| 604 | image_fread(image, &state->rom[0x10000], 0x4000 * state->prg_chunks); |
| 605 | |
| 554 | 606 | #if SPLIT_PRG |
| 555 | | { |
| 556 | | FILE *prgout; |
| 557 | | char outname[255]; |
| 558 | | |
| 559 | | for (i = 0; i < state->prg_chunks; i ++) |
| 560 | 607 | { |
| 561 | | sprintf(outname, "%s.p%d", image_filename(image), i); |
| 608 | FILE *prgout; |
| 609 | char outname[255]; |
| 610 | |
| 611 | sprintf(outname, "%s.prg", image_filename(image)); |
| 562 | 612 | prgout = fopen(outname, "wb"); |
| 563 | 613 | if (prgout) |
| 564 | 614 | { |
| 565 | | fwrite(&state->rom[0x10000 + 0x4000 * i], 1, 0x4000, prgout); |
| 566 | | fclose(prgout); |
| 615 | fwrite(&state->rom[0x10000], 1, 0x4000 * state->prg_chunks, prgout); |
| 616 | printf("Created PRG chunk\n"); |
| 567 | 617 | } |
| 618 | |
| 619 | fclose(prgout); |
| 568 | 620 | } |
| 569 | | } |
| 570 | | #endif /* SPLIT_PRG */ |
| 571 | | |
| 572 | | logerror("**\n"); |
| 573 | | logerror("Mapper: %d\n", state->mapper); |
| 574 | | logerror("PRG chunks: %02x, size: %06x\n", state->prg_chunks, 0x4000 * state->prg_chunks); |
| 575 | | // printf("Mapper: %d\n", state->mapper); |
| 576 | | // printf("PRG chunks: %02x, size: %06x\n", state->prg_chunks, 0x4000 * state->prg_chunks); |
| 577 | | |
| 578 | | /* Read in any chr chunks */ |
| 579 | | if (state->chr_chunks > 0) |
| 580 | | { |
| 581 | | image_fread(image, state->vrom, state->chr_chunks * 0x2000); |
| 582 | | if (state->mapper == 2) |
| 583 | | logerror("Warning: VROM has been found in VRAM-based mapper. Either the mapper is set wrong or the ROM image is incorrect.\n"); |
| 584 | | } |
| 585 | | |
| 621 | #endif |
| 622 | |
| 623 | logerror("**\n"); |
| 624 | logerror("Mapper: %d\n", state->mapper); |
| 625 | logerror("PRG chunks: %02x, size: %06x\n", state->prg_chunks, 0x4000 * state->prg_chunks); |
| 626 | // printf("Mapper: %d\n", state->mapper); |
| 627 | // printf("PRG chunks: %02x, size: %06x\n", state->prg_chunks, 0x4000 * state->prg_chunks); |
| 628 | |
| 629 | /* Read in any chr chunks */ |
| 630 | if (state->chr_chunks > 0) |
| 631 | { |
| 632 | image_fread(image, state->vrom, state->chr_chunks * 0x2000); |
| 633 | if (state->mapper == 2) |
| 634 | logerror("Warning: VROM has been found in VRAM-based mapper. Either the mapper is set wrong or the ROM image is incorrect.\n"); |
| 635 | } |
| 636 | |
| 586 | 637 | #if SPLIT_CHR |
| 587 | | { |
| 588 | | FILE *chrout; |
| 589 | | char outname[255]; |
| 590 | | |
| 591 | | for (i = 0; i < state->chr_chunks; i ++) |
| 592 | 638 | { |
| 593 | | sprintf(outname, "%s.c%d", image_filename(image), i); |
| 639 | FILE *chrout; |
| 640 | char outname[255]; |
| 641 | |
| 642 | sprintf(outname, "%s.chr", image_filename(image)); |
| 594 | 643 | chrout= fopen(outname, "wb"); |
| 595 | 644 | if (chrout) |
| 596 | 645 | { |
| 597 | | fwrite(&state->rom[0x10000 + 0x2000 * i], 1, 0x2000, chrout); |
| 598 | | fclose(chrout); |
| 646 | fwrite(state->vrom, 1, 0x2000 * state->chr_chunks, chrout); |
| 647 | printf("Created CHR chunk\n"); |
| 599 | 648 | } |
| 649 | fclose(chrout); |
| 600 | 650 | } |
| 651 | #endif |
| 652 | |
| 653 | logerror("CHR chunks: %02x, size: %06x\n", state->chr_chunks, 0x4000 * state->chr_chunks); |
| 654 | logerror("**\n"); |
| 655 | // printf("CHR chunks: %02x, size: %06x\n", state->chr_chunks, 0x4000 * state->chr_chunks); |
| 656 | // printf("**\n"); |
| 657 | |
| 658 | /* Attempt to load a battery file for this ROM. If successful, we */ |
| 659 | /* must wait until later to move it to the system memory. */ |
| 660 | if (state->battery) |
| 661 | image_battery_load(image, state->battery_data, NES_BATTERY_SIZE, 0x00); |
| 662 | |
| 601 | 663 | } |
| 602 | | #endif /* SPLIT_CHR */ |
| 603 | | |
| 604 | | logerror("CHR chunks: %02x, size: %06x\n", state->chr_chunks, 0x4000 * state->chr_chunks); |
| 605 | | logerror("**\n"); |
| 606 | | // printf("CHR chunks: %02x, size: %06x\n", state->chr_chunks, 0x4000 * state->chr_chunks); |
| 607 | | // printf("**\n"); |
| 608 | | |
| 609 | | /* Attempt to load a battery file for this ROM. If successful, we */ |
| 610 | | /* must wait until later to move it to the system memory. */ |
| 611 | | if (state->battery) |
| 612 | | image_battery_load(image, state->battery_data, NES_BATTERY_SIZE, 0x00); |
| 613 | | |
| 614 | | } |
| 615 | | else if ((magic[0] == 'U') && (magic[1] == 'N') && (magic[2] == 'I') && (magic[3] == 'F')) /* If header starts with 'UNIF' it is UNIF */ |
| 616 | | { |
| 617 | | UINT32 unif_ver = 0; |
| 618 | | char magic2[4]; |
| 619 | | UINT8 buffer[4]; |
| 620 | | UINT32 chunk_length = 0, read_length = 0x20; |
| 621 | | UINT32 prg_start = 0, chr_start = 0; |
| 622 | | int prg_left = 0, chr_left = 0; // used to verify integrity of our unif image |
| 623 | | char unif_mapr[32]; // here we should store MAPR chunks |
| 624 | | UINT32 size = image_length(image); |
| 625 | | const unif *unif_board; |
| 626 | | int mapr_chunk_found = 0; |
| 627 | | |
| 628 | | state->format = 2; // we use this to select between mapper_reset / unif_reset |
| 629 | | |
| 630 | | image_fread(image, &buffer, 4); |
| 631 | | unif_ver = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 632 | | logerror("UNIF file found, version %d\n", unif_ver); |
| 633 | | |
| 634 | | if (size <= 0x20) |
| 664 | else if ((magic[0] == 'U') && (magic[1] == 'N') && (magic[2] == 'I') && (magic[3] == 'F')) /* If header starts with 'UNIF' it is UNIF */ |
| 635 | 665 | { |
| 636 | | logerror("%s only contains the UNIF header and no data.\n", image_filename(image)); |
| 637 | | return INIT_FAIL; |
| 638 | | } |
| 639 | | |
| 640 | | do |
| 641 | | { |
| 642 | | image_fseek(image, read_length, SEEK_SET); |
| 643 | | |
| 644 | | memset(magic2, '\0', sizeof(magic2)); |
| 645 | | image_fread(image, &magic2, 4); |
| 646 | | |
| 666 | UINT32 unif_ver = 0; |
| 667 | char magic2[4]; |
| 668 | UINT8 buffer[4]; |
| 669 | UINT32 chunk_length = 0, read_length = 0x20; |
| 670 | UINT32 prg_start = 0, chr_start = 0; |
| 671 | int prg_left = 0, chr_left = 0; // used to verify integrity of our unif image |
| 672 | char unif_mapr[32]; // here we should store MAPR chunks |
| 673 | UINT32 size = image_length(image); |
| 674 | const unif *unif_board; |
| 675 | int mapr_chunk_found = 0; |
| 676 | |
| 677 | state->format = 2; // we use this to select between mapper_reset / unif_reset |
| 678 | |
| 679 | image_fread(image, &buffer, 4); |
| 680 | unif_ver = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 681 | logerror("UNIF file found, version %d\n", unif_ver); |
| 682 | |
| 683 | if (size <= 0x20) |
| 684 | { |
| 685 | logerror("%s only contains the UNIF header and no data.\n", image_filename(image)); |
| 686 | return INIT_FAIL; |
| 687 | } |
| 688 | |
| 689 | do |
| 690 | { |
| 691 | image_fseek(image, read_length, SEEK_SET); |
| 692 | |
| 693 | memset(magic2, '\0', sizeof(magic2)); |
| 694 | image_fread(image, &magic2, 4); |
| 695 | |
| 647 | 696 | #if 0 |
| 648 | | // unfortunately, the MAPR chunk is not always the first chunk (see Super 24-in-1) |
| 649 | | /* Preliminary checks: the first chunk MUST be MAPR! */ |
| 650 | | if (read_length == 0x20 && ((magic2[0] != 'M') || (magic2[1] != 'A') || (magic2[2] != 'P') || (magic2[3] != 'R'))) |
| 651 | | fatalerror("First chunk of data in UNIF should be [MAPR]. Check if your image has been corrupted\n"); |
| 652 | | |
| 653 | | /* Preliminary checks: multiple MAPR chunks are FORBIDDEN! */ |
| 654 | | if (read_length > 0x20 && ((magic2[0] == 'M') && (magic2[1] == 'A') && (magic2[2] == 'P') && (magic2[3] == 'R'))) |
| 655 | | fatalerror("UNIF should not have multiple [MAPR] chunks. Check if your image has been corrupted\n"); |
| 697 | // unfortunately, the MAPR chunk is not always the first chunk (see Super 24-in-1) |
| 698 | /* Preliminary checks: the first chunk MUST be MAPR! */ |
| 699 | if (read_length == 0x20 && ((magic2[0] != 'M') || (magic2[1] != 'A') || (magic2[2] != 'P') || (magic2[3] != 'R'))) |
| 700 | fatalerror("First chunk of data in UNIF should be [MAPR]. Check if your image has been corrupted\n"); |
| 701 | |
| 702 | /* Preliminary checks: multiple MAPR chunks are FORBIDDEN! */ |
| 703 | if (read_length > 0x20 && ((magic2[0] == 'M') && (magic2[1] == 'A') && (magic2[2] == 'P') && (magic2[3] == 'R'))) |
| 704 | fatalerror("UNIF should not have multiple [MAPR] chunks. Check if your image has been corrupted\n"); |
| 656 | 705 | #endif |
| 657 | | |
| 658 | | /* we first run through the whole image to find a [MAPR] chunk */ |
| 659 | | /* when found, we set mapr_chunk_found=1 and we go back to load other chunks! */ |
| 660 | | if (!mapr_chunk_found) |
| 661 | | { |
| 662 | | if ((magic2[0] == 'M') && (magic2[1] == 'A') && (magic2[2] == 'P') && (magic2[3] == 'R')) |
| 706 | |
| 707 | /* we first run through the whole image to find a [MAPR] chunk */ |
| 708 | /* when found, we set mapr_chunk_found=1 and we go back to load other chunks! */ |
| 709 | if (!mapr_chunk_found) |
| 663 | 710 | { |
| 664 | | mapr_chunk_found = 1; |
| 665 | | logerror("[MAPR] chunk found: "); |
| 666 | | image_fread(image, &buffer, 4); |
| 667 | | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 668 | | |
| 669 | | if (chunk_length <= 0x20) |
| 670 | | image_fread(image, &unif_mapr, chunk_length); |
| 671 | | |
| 672 | | unif_board = nes_unif_lookup(unif_mapr); |
| 673 | | logerror("%s\n", unif_mapr); |
| 674 | | |
| 675 | | if (unif_board == NULL) |
| 711 | if ((magic2[0] == 'M') && (magic2[1] == 'A') && (magic2[2] == 'P') && (magic2[3] == 'R')) |
| 676 | 712 | { |
| 677 | | fatalerror("Unsupported UNIF board %s.\n", unif_mapr); |
| 678 | | // logerror("Unsupported UNIF board %s.\n", unif_mapr); |
| 713 | mapr_chunk_found = 1; |
| 714 | logerror("[MAPR] chunk found: "); |
| 715 | image_fread(image, &buffer, 4); |
| 716 | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 717 | |
| 718 | if (chunk_length <= 0x20) |
| 719 | image_fread(image, &unif_mapr, chunk_length); |
| 720 | |
| 721 | unif_board = nes_unif_lookup(unif_mapr); |
| 722 | logerror("%s\n", unif_mapr); |
| 723 | |
| 724 | if (unif_board == NULL) |
| 725 | { |
| 726 | fatalerror("Unsupported UNIF board %s.\n", unif_mapr); |
| 727 | // logerror("Unsupported UNIF board %s.\n", unif_mapr); |
| 728 | } |
| 729 | |
| 730 | state->mapper = 0; // this allows us to set up memory handlers without duplicating code (for the moment) |
| 731 | state->board = unif_board->board; |
| 732 | state->prg_chunks = unif_board->prgrom; |
| 733 | state->chr_chunks = unif_board->chrrom; |
| 734 | state->battery = unif_board->wram; // we should implement WRAM banks... |
| 735 | // state->hard_mirroring = unif_board->nt; |
| 736 | // state->four_screen_vram = ; |
| 737 | |
| 738 | /* Free the regions that were allocated by the ROM loader */ |
| 739 | memory_region_free(image->machine, "maincpu"); |
| 740 | memory_region_free(image->machine, "gfx1"); |
| 741 | |
| 742 | /* Allocate them again with the proper size */ |
| 743 | memory_region_alloc(image->machine, "maincpu", 0x10000 + (state->prg_chunks + 1) * 0x4000, 0); |
| 744 | if (state->chr_chunks) |
| 745 | memory_region_alloc(image->machine, "gfx1", state->chr_chunks * 0x2000, 0); |
| 746 | |
| 747 | state->rom = memory_region(image->machine, "maincpu"); |
| 748 | state->vrom = memory_region(image->machine, "gfx1"); |
| 749 | state->vram = memory_region(image->machine, "gfx2"); |
| 750 | state->wram = memory_region(image->machine, "user1"); |
| 751 | |
| 752 | /* for validation purposes */ |
| 753 | prg_left = unif_board->prgrom * 0x4000; |
| 754 | chr_left = unif_board->chrrom * 0x2000; |
| 755 | |
| 756 | /* now that we found the MAPR chunk, we can go back to load other chunks */ |
| 757 | image_fseek(image, 0x20, SEEK_SET); |
| 758 | read_length = 0x20; |
| 679 | 759 | } |
| 680 | | |
| 681 | | state->mapper = 0; // this allows us to set up memory handlers without duplicating code (for the moment) |
| 682 | | state->board = unif_board->board; |
| 683 | | state->prg_chunks = unif_board->prgrom; |
| 684 | | state->chr_chunks = unif_board->chrrom; |
| 685 | | state->battery = unif_board->wram; // we should implement WRAM banks... |
| 686 | | // state->hard_mirroring = unif_board->nt; |
| 687 | | // state->four_screen_vram = ; |
| 688 | | |
| 689 | | /* Free the regions that were allocated by the ROM loader */ |
| 690 | | memory_region_free(image->machine, "maincpu"); |
| 691 | | memory_region_free(image->machine, "gfx1"); |
| 692 | | |
| 693 | | /* Allocate them again with the proper size */ |
| 694 | | memory_region_alloc(image->machine, "maincpu", 0x10000 + (state->prg_chunks + 1) * 0x4000, 0); |
| 695 | | if (state->chr_chunks) |
| 696 | | memory_region_alloc(image->machine, "gfx1", state->chr_chunks * 0x2000, 0); |
| 697 | | |
| 698 | | state->rom = memory_region(image->machine, "maincpu"); |
| 699 | | state->vrom = memory_region(image->machine, "gfx1"); |
| 700 | | state->vram = memory_region(image->machine, "gfx2"); |
| 701 | | state->wram = memory_region(image->machine, "user1"); |
| 702 | | |
| 703 | | /* for validation purposes */ |
| 704 | | prg_left = unif_board->prgrom * 0x4000; |
| 705 | | chr_left = unif_board->chrrom * 0x2000; |
| 706 | | |
| 707 | | /* now that we found the MAPR chunk, we can go back to load other chunks */ |
| 708 | | image_fseek(image, 0x20, SEEK_SET); |
| 709 | | read_length = 0x20; |
| 760 | else |
| 761 | { |
| 762 | logerror("Skip this chunk. We need a [MAPR] chunk before anything else.\n"); |
| 763 | image_fread(image, &buffer, 4); |
| 764 | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 765 | |
| 766 | read_length += (chunk_length + 8); |
| 767 | } |
| 710 | 768 | } |
| 711 | 769 | else |
| 712 | 770 | { |
| 713 | | logerror("Skip this chunk. We need a [MAPR] chunk before anything else.\n"); |
| 714 | | image_fread(image, &buffer, 4); |
| 715 | | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 716 | | |
| 717 | | read_length += (chunk_length + 8); |
| 771 | /* What kind of chunk do we have here? */ |
| 772 | if ((magic2[0] == 'M') && (magic2[1] == 'A') && (magic2[2] == 'P') && (magic2[3] == 'R')) |
| 773 | { |
| 774 | /* The [MAPR] chunk has already been read, so we skip it */ |
| 775 | /* TO DO: it would be nice to check if more than one MAPR chunk is present */ |
| 776 | logerror("[MAPR] chunk found (in the 2nd run). Already loaded.\n"); |
| 777 | image_fread(image, &buffer, 4); |
| 778 | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 779 | |
| 780 | read_length += (chunk_length + 8); |
| 781 | } |
| 782 | else if ((magic2[0] == 'R') && (magic2[1] == 'E') && (magic2[2] == 'A') && (magic2[3] == 'D')) |
| 783 | { |
| 784 | logerror("[READ] chunk found. No support yet.\n"); |
| 785 | image_fread(image, &buffer, 4); |
| 786 | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 787 | |
| 788 | read_length += (chunk_length + 8); |
| 789 | } |
| 790 | else if ((magic2[0] == 'N') && (magic2[1] == 'A') && (magic2[2] == 'M') && (magic2[3] == 'E')) |
| 791 | { |
| 792 | logerror("[NAME] chunk found. No support yet.\n"); |
| 793 | image_fread(image, &buffer, 4); |
| 794 | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 795 | |
| 796 | read_length += (chunk_length + 8); |
| 797 | } |
| 798 | else if ((magic2[0] == 'W') && (magic2[1] == 'R') && (magic2[2] == 'T') && (magic2[3] == 'R')) |
| 799 | { |
| 800 | logerror("[WRTR] chunk found. No support yet.\n"); |
| 801 | image_fread(image, &buffer, 4); |
| 802 | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 803 | |
| 804 | read_length += (chunk_length + 8); |
| 805 | } |
| 806 | else if ((magic2[0] == 'T') && (magic2[1] == 'V') && (magic2[2] == 'C') && (magic2[3] == 'I')) |
| 807 | { |
| 808 | logerror("[TVCI] chunk found. No support yet.\n"); |
| 809 | image_fread(image, &buffer, 4); |
| 810 | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 811 | |
| 812 | read_length += (chunk_length + 8); |
| 813 | } |
| 814 | else if ((magic2[0] == 'D') && (magic2[1] == 'I') && (magic2[2] == 'N') && (magic2[3] == 'F')) |
| 815 | { |
| 816 | logerror("[DINF] chunk found. No support yet.\n"); |
| 817 | image_fread(image, &buffer, 4); |
| 818 | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 819 | |
| 820 | read_length += (chunk_length + 8); |
| 821 | } |
| 822 | else if ((magic2[0] == 'C') && (magic2[1] == 'T') && (magic2[2] == 'R') && (magic2[3] == 'L')) |
| 823 | { |
| 824 | logerror("[CTRL] chunk found. No support yet.\n"); |
| 825 | image_fread(image, &buffer, 4); |
| 826 | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 827 | |
| 828 | read_length += (chunk_length + 8); |
| 829 | } |
| 830 | else if ((magic2[0] == 'B') && (magic2[1] == 'A') && (magic2[2] == 'T') && (magic2[3] == 'R')) |
| 831 | { |
| 832 | logerror("[BATR] chunk found. No support yet.\n"); |
| 833 | image_fread(image, &buffer, 4); |
| 834 | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 835 | |
| 836 | read_length += (chunk_length + 8); |
| 837 | } |
| 838 | else if ((magic2[0] == 'V') && (magic2[1] == 'R') && (magic2[2] == 'O') && (magic2[3] == 'R')) |
| 839 | { |
| 840 | logerror("[VROR] chunk found. No support yet.\n"); |
| 841 | image_fread(image, &buffer, 4); |
| 842 | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 843 | |
| 844 | read_length += (chunk_length + 8); |
| 845 | } |
| 846 | else if ((magic2[0] == 'M') && (magic2[1] == 'I') && (magic2[2] == 'R') && (magic2[3] == 'R')) |
| 847 | { |
| 848 | logerror("[MIRR] chunk found. No support yet.\n"); |
| 849 | image_fread(image, &buffer, 4); |
| 850 | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 851 | |
| 852 | read_length += (chunk_length + 8); |
| 853 | } |
| 854 | else if ((magic2[0] == 'P') && (magic2[1] == 'C') && (magic2[2] == 'K')) |
| 855 | { |
| 856 | logerror("[PCK%c] chunk found. No support yet.\n", magic2[3]); |
| 857 | image_fread(image, &buffer, 4); |
| 858 | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 859 | |
| 860 | read_length += (chunk_length + 8); |
| 861 | } |
| 862 | else if ((magic2[0] == 'C') && (magic2[1] == 'C') && (magic2[2] == 'K')) |
| 863 | { |
| 864 | logerror("[CCK%c] chunk found. No support yet.\n", magic2[3]); |
| 865 | image_fread(image, &buffer, 4); |
| 866 | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 867 | |
| 868 | read_length += (chunk_length + 8); |
| 869 | } |
| 870 | else if ((magic2[0] == 'P') && (magic2[1] == 'R') && (magic2[2] == 'G')) |
| 871 | { |
| 872 | logerror("[PRG%c] chunk found. ", magic2[3]); |
| 873 | image_fread(image, &buffer, 4); |
| 874 | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 875 | |
| 876 | logerror("It consists of %d 16K-blocks.\n", chunk_length / 0x4000); |
| 877 | /* Validation */ |
| 878 | prg_left -= chunk_length; |
| 879 | if (prg_left < 0) |
| 880 | fatalerror("PRG chunks larger than expected by board %s!\n", unif_mapr); |
| 881 | |
| 882 | /* Read in the program chunks */ |
| 883 | if (state->prg_chunks == 1) |
| 884 | { |
| 885 | image_fread(image, &state->rom[0x14000], chunk_length); |
| 886 | /* Mirror the only bank into $8000 */ |
| 887 | memcpy(&state->rom[0x10000], &state->rom[0x14000], chunk_length); |
| 888 | } |
| 889 | else |
| 890 | image_fread(image, &state->rom[0x10000 + prg_start], chunk_length); |
| 891 | |
| 892 | prg_start += chunk_length; |
| 893 | read_length += (chunk_length + 8); |
| 894 | } |
| 895 | else if ((magic2[0] == 'C') && (magic2[1] == 'H') && (magic2[2] == 'R')) |
| 896 | { |
| 897 | logerror("[CHR%c] chunk found. ", magic2[3]); |
| 898 | image_fread(image, &buffer, 4); |
| 899 | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 900 | |
| 901 | logerror("It consists of %d 8K-blocks.\n", chunk_length / 0x2000); |
| 902 | /* validation */ |
| 903 | chr_left -= chunk_length; |
| 904 | if (chr_left < 0) |
| 905 | fatalerror("CHR chunks larger than expected by board %s!\n", unif_mapr); |
| 906 | |
| 907 | /* Read in the vrom chunks */ |
| 908 | image_fread(image, state->vrom + chr_start, chunk_length); |
| 909 | |
| 910 | chr_start += chunk_length; |
| 911 | read_length += (chunk_length + 8); |
| 912 | } |
| 913 | else |
| 914 | { |
| 915 | printf("Unsupported UNIF chunk. Please report the problem at MESS Board.\n"); |
| 916 | read_length = size; |
| 917 | } |
| 718 | 918 | } |
| 719 | | } |
| 720 | | else |
| 721 | | { |
| 722 | | /* What kind of chunk do we have here? */ |
| 723 | | if ((magic2[0] == 'M') && (magic2[1] == 'A') && (magic2[2] == 'P') && (magic2[3] == 'R')) |
| 724 | | { |
| 725 | | /* The [MAPR] chunk has already been read, so we skip it */ |
| 726 | | /* TO DO: it would be nice to check if more than one MAPR chunk is present */ |
| 727 | | logerror("[MAPR] chunk found (in the 2nd run). Already loaded.\n"); |
| 728 | | image_fread(image, &buffer, 4); |
| 729 | | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 919 | } while (size > read_length); |
| 920 | |
| 921 | if (!mapr_chunk_found ) |
| 922 | fatalerror("UNIF should have a [MAPR] chunk to work. Check if your image has been corrupted\n"); |
| 923 | |
| 924 | logerror("UNIF support is only very preliminary.\n"); |
| 925 | } |
| 926 | else |
| 927 | { |
| 928 | logerror("%s is NOT a file in either iNES or UNIF format.\n", image_filename(image)); |
| 929 | return INIT_FAIL; |
| 930 | } |
| 931 | } |
| 932 | else |
| 933 | { |
| 934 | UINT32 prg_size = image_get_software_region_length(image, "prg"); |
| 935 | UINT32 chr_size = image_get_software_region_length(image, "chr"); |
| 730 | 936 | |
| 731 | | read_length += (chunk_length + 8); |
| 732 | | } |
| 733 | | else if ((magic2[0] == 'R') && (magic2[1] == 'E') && (magic2[2] == 'A') && (magic2[3] == 'D')) |
| 734 | | { |
| 735 | | logerror("[READ] chunk found. No support yet.\n"); |
| 736 | | image_fread(image, &buffer, 4); |
| 737 | | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 937 | /* Free the regions that were allocated by the ROM loader */ |
| 938 | memory_region_free(image->machine, "maincpu"); |
| 939 | memory_region_free(image->machine, "gfx1"); |
| 738 | 940 | |
| 739 | | read_length += (chunk_length + 8); |
| 740 | | } |
| 741 | | else if ((magic2[0] == 'N') && (magic2[1] == 'A') && (magic2[2] == 'M') && (magic2[3] == 'E')) |
| 742 | | { |
| 743 | | logerror("[NAME] chunk found. No support yet.\n"); |
| 744 | | image_fread(image, &buffer, 4); |
| 745 | | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 941 | /* Allocate them again with the proper size */ |
| 746 | 942 | |
| 747 | | read_length += (chunk_length + 8); |
| 748 | | } |
| 749 | | else if ((magic2[0] == 'W') && (magic2[1] == 'R') && (magic2[2] == 'T') && (magic2[3] == 'R')) |
| 750 | | { |
| 751 | | logerror("[WRTR] chunk found. No support yet.\n"); |
| 752 | | image_fread(image, &buffer, 4); |
| 753 | | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 943 | // FIXME: here we have a +0x4000 to handle games with a single 16k prg chunk, |
| 944 | // but we should simply reload it in the .xml and use here the proper prg_size!! |
| 945 | memory_region_alloc(image->machine, "maincpu", 0x10000 + prg_size + 0x4000, 0); |
| 754 | 946 | |
| 755 | | read_length += (chunk_length + 8); |
| 756 | | } |
| 757 | | else if ((magic2[0] == 'T') && (magic2[1] == 'V') && (magic2[2] == 'C') && (magic2[3] == 'I')) |
| 758 | | { |
| 759 | | logerror("[TVCI] chunk found. No support yet.\n"); |
| 760 | | image_fread(image, &buffer, 4); |
| 761 | | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 947 | if (chr_size) |
| 948 | memory_region_alloc(image->machine, "gfx1", chr_size, 0); |
| 949 | |
| 950 | state->rom = memory_region(image->machine, "maincpu"); |
| 951 | state->vrom = memory_region(image->machine, "gfx1"); |
| 952 | state->vram = memory_region(image->machine, "gfx2"); |
| 953 | state->wram = memory_region(image->machine, "user1"); |
| 762 | 954 | |
| 763 | | read_length += (chunk_length + 8); |
| 764 | | } |
| 765 | | else if ((magic2[0] == 'D') && (magic2[1] == 'I') && (magic2[2] == 'N') && (magic2[3] == 'F')) |
| 766 | | { |
| 767 | | logerror("[DINF] chunk found. No support yet.\n"); |
| 768 | | image_fread(image, &buffer, 4); |
| 769 | | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 955 | memcpy(state->rom + 0x10000, image_get_software_region(image, "prg"), prg_size); |
| 956 | // FIXME: as above, we should reload the single prg chunk in the .xml, not copying it here |
| 957 | if (prg_size == 0x4000) |
| 958 | memcpy(state->rom + 0x14000, image_get_software_region(image, "prg"), prg_size); |
| 770 | 959 | |
| 771 | | read_length += (chunk_length + 8); |
| 772 | | } |
| 773 | | else if ((magic2[0] == 'C') && (magic2[1] == 'T') && (magic2[2] == 'R') && (magic2[3] == 'L')) |
| 774 | | { |
| 775 | | logerror("[CTRL] chunk found. No support yet.\n"); |
| 776 | | image_fread(image, &buffer, 4); |
| 777 | | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 960 | if (chr_size) |
| 961 | memcpy(state->vrom, image_get_software_region(image, "chr"), chr_size); |
| 778 | 962 | |
| 779 | | read_length += (chunk_length + 8); |
| 780 | | } |
| 781 | | else if ((magic2[0] == 'B') && (magic2[1] == 'A') && (magic2[2] == 'T') && (magic2[3] == 'R')) |
| 782 | | { |
| 783 | | logerror("[BATR] chunk found. No support yet.\n"); |
| 784 | | image_fread(image, &buffer, 4); |
| 785 | | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 963 | state->prg_chunks = prg_size / 0x4000; |
| 964 | state->chr_chunks = chr_size / 0x2000; |
| 786 | 965 | |
| 787 | | read_length += (chunk_length + 8); |
| 788 | | } |
| 789 | | else if ((magic2[0] == 'V') && (magic2[1] == 'R') && (magic2[2] == 'O') && (magic2[3] == 'R')) |
| 790 | | { |
| 791 | | logerror("[VROR] chunk found. No support yet.\n"); |
| 792 | | image_fread(image, &buffer, 4); |
| 793 | | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 966 | state->format = 1; // temporarily treat this as a ines file |
| 967 | state->mapper = 4; // this should depend on the 'feature' field of the .xml file |
| 968 | // FIXME: we need to handle the remaining variables. on the short term, we might |
| 969 | // create a table for the various mappers (based on the 'feature' value in xml), |
| 970 | // but on the long term we need to rework the whole emulation to first use the |
| 971 | // xml list and have mappers and unif boards to fall back to particular 'feature' |
| 972 | // values! |
| 794 | 973 | |
| 795 | | read_length += (chunk_length + 8); |
| 796 | | } |
| 797 | | else if ((magic2[0] == 'M') && (magic2[1] == 'I') && (magic2[2] == 'R') && (magic2[3] == 'R')) |
| 798 | | { |
| 799 | | logerror("[MIRR] chunk found. No support yet.\n"); |
| 800 | | image_fread(image, &buffer, 4); |
| 801 | | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 802 | | |
| 803 | | read_length += (chunk_length + 8); |
| 804 | | } |
| 805 | | else if ((magic2[0] == 'P') && (magic2[1] == 'C') && (magic2[2] == 'K')) |
| 806 | | { |
| 807 | | logerror("[PCK%c] chunk found. No support yet.\n", magic2[3]); |
| 808 | | image_fread(image, &buffer, 4); |
| 809 | | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 810 | | |
| 811 | | read_length += (chunk_length + 8); |
| 812 | | } |
| 813 | | else if ((magic2[0] == 'C') && (magic2[1] == 'C') && (magic2[2] == 'K')) |
| 814 | | { |
| 815 | | logerror("[CCK%c] chunk found. No support yet.\n", magic2[3]); |
| 816 | | image_fread(image, &buffer, 4); |
| 817 | | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 818 | | |
| 819 | | read_length += (chunk_length + 8); |
| 820 | | } |
| 821 | | else if ((magic2[0] == 'P') && (magic2[1] == 'R') && (magic2[2] == 'G')) |
| 822 | | { |
| 823 | | logerror("[PRG%c] chunk found. ", magic2[3]); |
| 824 | | image_fread(image, &buffer, 4); |
| 825 | | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 826 | | |
| 827 | | logerror("It consists of %d 16K-blocks.\n", chunk_length / 0x4000); |
| 828 | | /* Validation */ |
| 829 | | prg_left -= chunk_length; |
| 830 | | if (prg_left < 0) |
| 831 | | fatalerror("PRG chunks larger than expected by board %s!\n", unif_mapr); |
| 832 | | |
| 833 | | /* Read in the program chunks */ |
| 834 | | if (state->prg_chunks == 1) |
| 835 | | { |
| 836 | | image_fread(image, &state->rom[0x14000], chunk_length); |
| 837 | | /* Mirror the only bank into $8000 */ |
| 838 | | memcpy(&state->rom[0x10000], &state->rom[0x14000], chunk_length); |
| 839 | | } |
| 840 | | else |
| 841 | | image_fread(image, &state->rom[0x10000 + prg_start], chunk_length); |
| 842 | | |
| 843 | | prg_start += chunk_length; |
| 844 | | read_length += (chunk_length + 8); |
| 845 | | } |
| 846 | | else if ((magic2[0] == 'C') && (magic2[1] == 'H') && (magic2[2] == 'R')) |
| 847 | | { |
| 848 | | logerror("[CHR%c] chunk found. ", magic2[3]); |
| 849 | | image_fread(image, &buffer, 4); |
| 850 | | chunk_length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24); |
| 851 | | |
| 852 | | logerror("It consists of %d 8K-blocks.\n", chunk_length / 0x2000); |
| 853 | | /* validation */ |
| 854 | | chr_left -= chunk_length; |
| 855 | | if (chr_left < 0) |
| 856 | | fatalerror("CHR chunks larger than expected by board %s!\n", unif_mapr); |
| 857 | | |
| 858 | | /* Read in the vrom chunks */ |
| 859 | | image_fread(image, state->vrom + chr_start, chunk_length); |
| 860 | | |
| 861 | | chr_start += chunk_length; |
| 862 | | read_length += (chunk_length + 8); |
| 863 | | } |
| 864 | | else |
| 865 | | { |
| 866 | | printf("Unsupported UNIF chunk. Please report the problem at MESS Board.\n"); |
| 867 | | read_length = size; |
| 868 | | } |
| 869 | | } |
| 870 | | } while (size > read_length); |
| 871 | | |
| 872 | | if (!mapr_chunk_found ) |
| 873 | | fatalerror("UNIF should have a [MAPR] chunk to work. Check if your image has been corrupted\n"); |
| 874 | | |
| 875 | | logerror("UNIF support is only very preliminary.\n"); |
| 974 | //state->crc_hack = ; |
| 975 | //state->hard_mirroring = ; |
| 976 | //state->battery = ; |
| 977 | //state->trainer = ; |
| 978 | //state->four_screen_vram = ; |
| 876 | 979 | } |
| 877 | | else |
| 878 | | { |
| 879 | | logerror("%s is NOT a file in either iNES or UNIF format.\n", image_filename(image)); |
| 880 | | return INIT_FAIL; |
| 881 | | } |
| 882 | | |
| 980 | |
| 883 | 981 | return INIT_PASS; |
| 884 | 982 | } |
| 885 | 983 | |
| r7957 | r7958 | |
| 939 | 1037 | state->fds_sides = 0; |
| 940 | 1038 | state->fds_data = NULL; |
| 941 | 1039 | |
| 942 | | floppy_install_load_proc(floppy_get_device(machine,0), nes_load_proc); |
| 943 | | floppy_install_unload_proc(floppy_get_device(machine,0), nes_unload_proc); |
| 1040 | floppy_install_load_proc(floppy_get_device(machine, 0), nes_load_proc); |
| 1041 | floppy_install_unload_proc(floppy_get_device(machine, 0), nes_unload_proc); |
| 944 | 1042 | } |
src/mess/machine/nes_ines.c
| r7957 | r7958 | |
| 258 | 258 | { |
| 259 | 259 | LOG_MMC(("irq fired, scanline: %d (MAME %d, beam pos: %d)\n", scanline, |
| 260 | 260 | video_screen_get_vpos(device->machine->primary_screen), video_screen_get_hpos(device->machine->primary_screen))); |
| 261 | | cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE); |
| 261 | cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE); |
| 262 | 262 | } |
| 263 | 263 | } |
| 264 | 264 | } |
| r7957 | r7958 | |
| 360 | 360 | if (scanline == state->IRQ_count) |
| 361 | 361 | { |
| 362 | 362 | if (state->IRQ_enable) |
| 363 | | cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE); |
| 363 | cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE); |
| 364 | 364 | |
| 365 | 365 | state->IRQ_status = 0xff; |
| 366 | 366 | } |
| r7957 | r7958 | |
| 932 | 932 | { |
| 933 | 933 | if ((0xffff - state->IRQ_count) < 114) |
| 934 | 934 | { |
| 935 | | cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE); |
| 935 | cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE); |
| 936 | 936 | state->IRQ_count = 0xffff; |
| 937 | 937 | state->IRQ_enable = 0; |
| 938 | 938 | } |
| r7957 | r7958 | |
| 1486 | 1486 | { |
| 1487 | 1487 | if (state->IRQ_count <= 114) |
| 1488 | 1488 | { |
| 1489 | | cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE); |
| 1489 | cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE); |
| 1490 | 1490 | state->IRQ_count = (0xffff - 114 + state->IRQ_count); // wrap around the 16 bits counter |
| 1491 | 1491 | } |
| 1492 | 1492 | state->IRQ_count -= 114; |
| r7957 | r7958 | |
| 1630 | 1630 | { |
| 1631 | 1631 | if ((state->IRQ_count & 0x000f) < 114) // always true, but we only update the IRQ once per scanlines so we cannot be more precise :( |
| 1632 | 1632 | { |
| 1633 | | cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE); |
| 1633 | cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE); |
| 1634 | 1634 | state->IRQ_count = (state->IRQ_count & ~0x000f) | (0x0f - (114 & 0x0f) + (state->IRQ_count & 0x000f)); // sort of wrap around the counter |
| 1635 | 1635 | } |
| 1636 | 1636 | // decrements should not affect upper bits, so we don't do anything here (114 > 0x0f) |
| r7957 | r7958 | |
| 1639 | 1639 | { |
| 1640 | 1640 | if ((state->IRQ_count & 0x00ff) < 114) |
| 1641 | 1641 | { |
| 1642 | | cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE); |
| 1642 | cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE); |
| 1643 | 1643 | state->IRQ_count = (state->IRQ_count & ~0x00ff) | (0xff - 114 + (state->IRQ_count & 0x00ff)); // wrap around the 8 bits counter |
| 1644 | 1644 | } |
| 1645 | 1645 | else |
| r7957 | r7958 | |
| 1649 | 1649 | { |
| 1650 | 1650 | if ((state->IRQ_count & 0x0fff) < 114) |
| 1651 | 1651 | { |
| 1652 | | cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE); |
| 1652 | cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE); |
| 1653 | 1653 | state->IRQ_count = (state->IRQ_count & ~0x0fff) | (0xfff - 114 + (state->IRQ_count & 0x0fff)); // wrap around the 12 bits counter |
| 1654 | 1654 | } |
| 1655 | 1655 | else |
| r7957 | r7958 | |
| 1657 | 1657 | } |
| 1658 | 1658 | else if (state->IRQ_count < 114) |
| 1659 | 1659 | { |
| 1660 | | cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE); |
| 1660 | cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE); |
| 1661 | 1661 | state->IRQ_count = (0xffff - 114 + state->IRQ_count); // wrap around the 16 bits counter |
| 1662 | 1662 | } |
| 1663 | 1663 | else |
| r7957 | r7958 | |
| 1774 | 1774 | { |
| 1775 | 1775 | if (state->IRQ_count >= (0x7fff - 114)) |
| 1776 | 1776 | { |
| 1777 | | cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE); |
| 1777 | cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE); |
| 1778 | 1778 | state->IRQ_count = 0; |
| 1779 | 1779 | } |
| 1780 | 1780 | else |
| r7957 | r7958 | |
| 1884 | 1884 | nes_state *state = (nes_state *)device->machine->driver_data; |
| 1885 | 1885 | |
| 1886 | 1886 | if (state->IRQ_enable_latch) |
| 1887 | | cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE); |
| 1887 | cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE); |
| 1888 | 1888 | |
| 1889 | 1889 | if (state->IRQ_enable) |
| 1890 | 1890 | { |
| 1891 | 1891 | if (state->IRQ_count <= 114) |
| 1892 | 1892 | { |
| 1893 | | cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE); |
| 1893 | cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE); |
| 1894 | 1894 | state->IRQ_enable = 0; |
| 1895 | 1895 | state->fds_status0 |= 0x01; |
| 1896 | 1896 | } |
| r7957 | r7958 | |
| 2031 | 2031 | { |
| 2032 | 2032 | state->IRQ_count = state->IRQ_count_latch; |
| 2033 | 2033 | state->IRQ_enable = state->IRQ_enable_latch; |
| 2034 | | cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE); |
| 2034 | cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE); |
| 2035 | 2035 | } |
| 2036 | 2036 | } |
| 2037 | 2037 | |
| r7957 | r7958 | |
| 2850 | 2850 | { |
| 2851 | 2851 | LOG_MMC(("irq fired, scanline: %d (MAME %d, beam pos: %d)\n", scanline, |
| 2852 | 2852 | video_screen_get_vpos(device->machine->primary_screen), video_screen_get_hpos(device->machine->primary_screen))); |
| 2853 | | cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE); |
| 2853 | cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE); |
| 2854 | 2854 | state->IRQ_enable = 0; |
| 2855 | 2855 | } |
| 2856 | 2856 | } |
| r7957 | r7958 | |
| 3008 | 3008 | { |
| 3009 | 3009 | state->IRQ_count = (state->IRQ_count + 1) & 0xfff; |
| 3010 | 3010 | state->IRQ_enable = 0; |
| 3011 | | cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE); |
| 3011 | cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE); |
| 3012 | 3012 | } |
| 3013 | 3013 | else |
| 3014 | 3014 | state->IRQ_count += 114; |
| r7957 | r7958 | |
| 3076 | 3076 | |
| 3077 | 3077 | *************************************************************/ |
| 3078 | 3078 | |
| 3079 | | extern emu_timer *nes_irq_timer; |
| 3080 | | |
| 3081 | 3079 | static WRITE8_HANDLER( mapper42_w ) |
| 3082 | 3080 | { |
| 3083 | 3081 | nes_state *state = (nes_state *)space->machine->driver_data; |
| r7957 | r7958 | |
| 3098 | 3096 | if (!state->IRQ_enable && (data & 0x02)) |
| 3099 | 3097 | { |
| 3100 | 3098 | state->IRQ_enable = 1; |
| 3101 | | timer_adjust_oneshot(nes_irq_timer, cputag_clocks_to_attotime(space->machine, "maincpu", 24576), 0); |
| 3099 | timer_adjust_oneshot(state->irq_timer, cpu_clocks_to_attotime(state->maincpu, 24576), 0); |
| 3102 | 3100 | } |
| 3103 | 3101 | if (!(data & 0x02)) |
| 3104 | 3102 | { |
| 3105 | 3103 | state->IRQ_enable = 0; |
| 3106 | | timer_adjust_oneshot(nes_irq_timer, attotime_never, 0); |
| 3104 | timer_adjust_oneshot(state->irq_timer, attotime_never, 0); |
| 3107 | 3105 | } |
| 3108 | 3106 | break; |
| 3109 | 3107 | } |
| r7957 | r7958 | |
| 3436 | 3434 | if (state->IRQ_count < 0x1000) |
| 3437 | 3435 | { |
| 3438 | 3436 | if ((0x1000 - state->IRQ_count) <= 114) |
| 3439 | | cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE); |
| 3437 | cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE); |
| 3440 | 3438 | else |
| 3441 | 3439 | state->IRQ_count += 114; |
| 3442 | 3440 | } |
| r7957 | r7958 | |
| 3807 | 3805 | { |
| 3808 | 3806 | LOG_MMC(("irq fired, scanline: %d (MAME %d, beam pos: %d)\n", scanline, |
| 3809 | 3807 | video_screen_get_vpos(device->machine->primary_screen), video_screen_get_hpos(device->machine->primary_screen))); |
| 3810 | | cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE); |
| 3808 | cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE); |
| 3811 | 3809 | } |
| 3812 | 3810 | } |
| 3813 | 3811 | } |
| r7957 | r7958 | |
| 3834 | 3832 | { |
| 3835 | 3833 | LOG_MMC(("irq fired, scanline: %d (MAME %d, beam pos: %d)\n", scanline, |
| 3836 | 3834 | video_screen_get_vpos(device->machine->primary_screen), video_screen_get_hpos(device->machine->primary_screen))); |
| 3837 | | cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE); |
| 3835 | cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE); |
| 3838 | 3836 | } |
| 3839 | 3837 | } |
| 3840 | 3838 | } |
| r7957 | r7958 | |
| 3980 | 3978 | if (state->IRQ_count <= 114) |
| 3981 | 3979 | { |
| 3982 | 3980 | state->IRQ_enable = 0; |
| 3983 | | cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE); |
| 3981 | cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE); |
| 3984 | 3982 | } |
| 3985 | 3983 | } |
| 3986 | 3984 | } |
| r7957 | r7958 | |
| 4086 | 4084 | { |
| 4087 | 4085 | state->IRQ_enable = 0; |
| 4088 | 4086 | state->IRQ_count = 0xffff; |
| 4089 | | cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE); |
| 4087 | cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE); |
| 4090 | 4088 | } |
| 4091 | 4089 | else |
| 4092 | 4090 | state->IRQ_count -= 114; |
| r7957 | r7958 | |
| 4260 | 4258 | if (state->IRQ_count <= 114) |
| 4261 | 4259 | { |
| 4262 | 4260 | state->IRQ_count = 0xffff; |
| 4263 | | cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE); |
| 4261 | cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE); |
| 4264 | 4262 | } |
| 4265 | 4263 | else |
| 4266 | 4264 | state->IRQ_count -= 114; |
| r7957 | r7958 | |
| 5540 | 5538 | { |
| 5541 | 5539 | if ((0xffff - state->IRQ_count) < 114) |
| 5542 | 5540 | { |
| 5543 | | cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE); |
| 5541 | cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE); |
| 5544 | 5542 | state->IRQ_enable = 0; |
| 5545 | 5543 | } |
| 5546 | 5544 | |
| r7957 | r7958 | |
| 5940 | 5938 | { |
| 5941 | 5939 | state->IRQ_count--; |
| 5942 | 5940 | if (!state->IRQ_count) |
| 5943 | | cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE); |
| 5941 | cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE); |
| 5944 | 5942 | } |
| 5945 | 5943 | } |
| 5946 | 5944 | } |
| r7957 | r7958 | |
| 9746 | 9744 | state->IRQ_count = 0; |
| 9747 | 9745 | LOG_MMC(("irq fired, scanline: %d (MAME %d, beam pos: %d)\n", scanline, |
| 9748 | 9746 | video_screen_get_vpos(device->machine->primary_screen), video_screen_get_hpos(device->machine->primary_screen))); |
| 9749 | | cputag_set_input_line(device->machine, "maincpu", M6502_IRQ_LINE, HOLD_LINE); |
| 9747 | cpu_set_input_line(state->maincpu, M6502_IRQ_LINE, HOLD_LINE); |
| 9750 | 9748 | } |
| 9751 | 9749 | } |
| 9752 | 9750 | |